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 [ForwardDifferentiable] 14 float calc(float x); 15} 16 17struct A : IInterface 18{ 19 float z; 20 21 [ForwardDifferentiable] 22 [NoDiffThis] 23 float calc(float x) { return x * x * x; } 24}; 25 26struct B : IInterface 27{ 28 float z; 29 30 [ForwardDifferentiable] 31 [NoDiffThis] 32 float calc(float x) { return x * x + z; } 33}; 34 35[ForwardDifferentiable] 36float sqr<T:IInterface>(T obj, float x) 37{ 38 return obj.calc(x); 39} 40 41//TEST_INPUT: type_conformance A:IInterface = 0 42//TEST_INPUT: type_conformance B:IInterface = 1 43 44 45[numthreads(1, 1, 1)] 46void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) 47{ 48 var obj = createDynamicObject<IInterface>(dispatchThreadID.x, 0); // A 49 outputBuffer[0] = __fwd_diff(sqr)(obj, DifferentialPair<float>(2.0, 1.0)).d; // A.calc, expect 12 50 51 obj = createDynamicObject<IInterface>(dispatchThreadID.x + 1, 0); // B 52 outputBuffer[1] = __fwd_diff(sqr)(obj, DifferentialPair<float>(1.5, 1.0)).d; // B.calc, expect 3 53}