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):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type 5//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -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 26DifferentialPair<float> dsqr<T:IInterface>(T obj, DifferentialPair<float> x) 27{ 28 float primal = obj.calc(x.p) + x.p * x.p; 29 float diff = 2.0 * x.p * x.d; 30 return diffPair(primal, diff); 31} 32 33[ForwardDerivative(dsqr)] 34float sqr<T:IInterface>(T obj, float x) 35{ 36 return no_diff(obj.calc(x)) + x * x; 37} 38 39//TEST_INPUT: type_conformance A:IInterface = 0 40//TEST_INPUT: type_conformance B:IInterface = 1 41 42 43[numthreads(1, 1, 1)] 44void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) 45{ 46 var obj = createDynamicObject<IInterface>(dispatchThreadID.x, 0); // A 47 var p = DifferentialPair<float>(2.0, 1.0); 48 49 outputBuffer[0] = __fwd_diff(sqr)(obj, p).d; // A.calc, expect 4 50 51 obj = createDynamicObject<IInterface>(dispatchThreadID.x + 1, 0); // B 52 p = DifferentialPair<float>(1.5, 1.0); 53 outputBuffer[1] = __fwd_diff(sqr)(obj, p).d; // B.calc, expect 3 54}