yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
4ae6e9d8b
master
1// struct-inherit-interface-requirement.slang 2 3//TEST(compute):COMPARE_COMPUTE: -shaderobj 4//TEST(compute):COMPARE_COMPUTE: -vk -shaderobj 5 6// Test that a `struct` type can use an inherited 7// member to satisfy an interface requirement. 8#pragma warning(disable:30816) 9 10interface ITweak 11{ 12 int tweak(int val); 13 int twiddle(int val); 14} 15 16// Note: `Base` intentionally doesn't inherit from `ITweak`, 17// but it *does* provide a method that could satisfy one 18// of the interface requirements. 19// 20struct Base 21{ 22 int a; 23 24 int tweak(int val) { return val ^ a; } 25} 26 27struct Derived : Base, ITweak 28{ 29 // Note: it is important for this type to have an additional 30 // field beyond the one in `Base`, because it ensures that 31 // the two types `Base` and `Derived` aren't structurally 32 // equivalent when compiled through HLSL (which silently allows 33 // certain type mismatches so long as there is a memberwise 34 // structural match). 35 int b; 36 37 int twiddle(int val) 38 { 39 return val + b; 40 } 41} 42 43int tweakAndTwiddle<T : ITweak>(T tweaker, int val) 44{ 45 int tmp = val; 46 tmp = tweaker.tweak(tmp); 47 tmp = tweaker.twiddle(tmp); 48 return tmp; 49} 50 51 52int test(int val) 53{ 54 Derived d; 55 d.a = 0xFF; 56 d.b = 1; 57 58 return tweakAndTwiddle(d, val); 59} 60 61//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer 62RWStructuredBuffer<int> outputBuffer; 63 64[numthreads(4, 1, 1)] 65void computeMain(int3 dispatchThreadID : SV_DispatchThreadID) 66{ 67 int tid = dispatchThreadID.x; 68 int inVal = tid; 69 int outVal = test(inVal); 70 outputBuffer[tid] = outVal; 71}