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 26void dsqr<T:IInterface>(T obj, inout DifferentialPair<float> x, float dOut) 27{ 28 float diff = 2.0 * x.p * dOut; 29 updateDiff(x, diff); 30} 31 32[BackwardDerivative(dsqr)] 33float sqr<T:IInterface>(T obj, float x) 34{ 35 return no_diff(obj.calc(x)) + x * x; 36} 37 38// Use automatically differentiated outer function to triger the primal/propagate func generation logic 39// on a function that has user provided backward derivative. 40[BackwardDifferentiable] 41float sqr_outter<T:IInterface>(T obj, float x) 42{ 43 return sqr(obj, x); 44} 45 46//TEST_INPUT: type_conformance A:IInterface = 0 47//TEST_INPUT: type_conformance B:IInterface = 1 48 49 50[numthreads(1, 1, 1)] 51void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) 52{ 53 var obj = createDynamicObject<IInterface>(dispatchThreadID.x, 0); // A 54 var p = DifferentialPair<float>(2.0, 1.0); 55 __bwd_diff(sqr_outter)(obj, p, 1.0); // A.calc, expect 4 56 outputBuffer[0] = p.d; 57 58 obj = createDynamicObject<IInterface>(dispatchThreadID.x + 1, 0); // B 59 p = DifferentialPair<float>(1.5, 1.0); 60 __bwd_diff(sqr)(obj, p, 1.0); // A.calc, expect 4 61 outputBuffer[1] = p.d; // B.calc, expect 3 62}