yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
d10732742
master
1// Test calling differentiable function through dynamic dispatch. 2 3//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type 4//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type 5//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type 6 7//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer 8RWStructuredBuffer<float> outputBuffer; 9 10[anyValueSize(16)] 11interface IInterface 12{ 13 static float calc(float x); 14} 15 16struct A : IInterface 17{ 18 static float calc(float x) { return 1.0; } 19}; 20 21struct B : IInterface 22{ 23 static float calc(float x) { return 2.0; } 24}; 25 26[BackwardDifferentiable] 27float sqr<T:IInterface>(T obj, float x) 28{ 29 return no_diff(obj.calc(x)) + x * x; 30} 31 32//TEST_INPUT: type_conformance A:IInterface = 0 33//TEST_INPUT: type_conformance B:IInterface = 1 34 35 36[numthreads(1, 1, 1)] 37void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) 38{ 39 var obj = createDynamicObject<IInterface>(dispatchThreadID.x, 0); // A 40 var p = DifferentialPair<float>(2.0, 1.0); 41 __bwd_diff(sqr)(obj, p, 1.0); // A.calc, expect 4 42 outputBuffer[0] = p.d; 43 44 obj = createDynamicObject<IInterface>(dispatchThreadID.x + 1, 0); // B 45 p = DifferentialPair<float>(1.5, 1.0); 46 __bwd_diff(sqr)(obj, p, 1.0); // A.calc, expect 4 47 outputBuffer[1] = p.d; // B.calc, expect 3 48}