yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f02b08490
master
1// interface-shader-param-in-struct.slang 2 3// This test puts interface-type shader parameters 4// inside of structure types to make sure that works 5 6//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute 7//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -dx12 -profile sm_6_0 8//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute 9//DISABLE_TEST(compute):COMPARE_COMPUTE:-slang -shaderobj -mtl 10// Passing, but: slang-test: Test context Slang session is leaking #5610 11//DISABLE_TEST(compute):COMPARE_COMPUTE:-wgpu 12 13// A lot of the setup is the same as for `interface-shader-param`, 14// so look there if you want the comments. 15 16interface IRandomNumberGenerator 17{ 18 [mutating] 19 int randomInt(); 20} 21 22interface IRandomNumberGenerationStrategy 23{ 24 associatedtype Generator : IRandomNumberGenerator; 25 Generator makeGenerator(int seed); 26} 27 28interface IModifier 29{ 30 int modify(int val); 31} 32 33int test( 34 int seed, 35 IRandomNumberGenerationStrategy inStrategy, 36 IModifier modifier) 37{ 38 let strategy = inStrategy; 39 var generator = strategy.makeGenerator(seed); 40 let unused = generator.randomInt(); 41 let val = generator.randomInt(); 42 let modifiedVal = modifier.modify(val); 43 return modifiedVal; 44} 45 46 47//TEST_INPUT:set gOutputBuffer = out ubuffer(data=[0 0 0 0], stride=4) 48RWStructuredBuffer<int> gOutputBuffer; 49 50// Note: even though `C` doesn't include any 51// uniform/ordinary dat as declared, it gets a 52// constant buffer allocated for it because 53// there is no way to rule out the possibility 54// that it *will* contain uniform/ordinary data 55// after specialization. 56// 57//TEST_INPUT:set C = new{ new MyStrategy{ ubuffer(data=[1 2 4 8], stride=4) } } 58//TEST_INPUT: globalExistentialType MyStrategy 59cbuffer C 60{ 61 IRandomNumberGenerationStrategy gStrategy; 62} 63 64struct Stuff 65{ 66 IModifier modifier; 67 int extra; 68} 69 70[numthreads(4, 1, 1)] 71void computeMain( 72//TEST_INPUT:set stuff = { new MyModifier{ ubuffer(data=[16 32 64 128], stride=4) }, 256 } 73//TEST_INPUT: entryPointExistentialType MyModifier 74 uniform Stuff stuff, 75 76 int3 dispatchThreadID : SV_DispatchThreadID) 77{ 78 let tid = dispatchThreadID.x; 79 80 let inputVal : int = tid; 81 let outputVal = test(inputVal, gStrategy, stuff.modifier) 82 + stuff.extra*stuff.extra; 83 84 gOutputBuffer[tid] = outputVal; 85} 86 87// Okay, now we get to the part that is unique starting 88// in this test: we add data to the concrete types 89// that we will use as parameters. 90 91struct MyStrategy : IRandomNumberGenerationStrategy 92{ 93 RWStructuredBuffer<int> globalSeeds; 94 95 struct Generator : IRandomNumberGenerator 96 { 97 int state; 98 99 [mutating] 100 int randomInt() 101 { 102 return state++; 103 } 104 } 105 106 Generator makeGenerator(int seed) 107 { 108 Generator generator = { globalSeeds[seed] }; 109 return generator; 110 } 111} 112 113struct MyModifier : IModifier 114{ 115 RWStructuredBuffer<int> localModifiers; 116 117 int modify(int val) 118 { 119 return val ^ localModifiers[val & 3]; 120 } 121}