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