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 : IDifferentiable 12{ 13 [Differentiable] 14 float calc(float x); 15} 16 17struct A : IInterface 18{ 19 float data1; 20 21 [Differentiable] 22 __init(float data1) { this.data1 = data1; } 23 24 [Differentiable] 25 float calc(float x) { return x * x * x * data1; } 26}; 27 28struct B : IInterface 29{ 30 float data1; 31 float data2; 32 33 [Differentiable] 34 __init(float data1, float data2) { this.data1 = data1; this.data2 = data2; } 35 36 [Differentiable] 37 float calc(float x) { return x * x * data1 * data2; } 38}; 39 40[Differentiable] 41float doThing(IInterface obj, float x) 42{ 43 return obj.calc(x); 44} 45 46[Differentiable] 47float f(uint id, float x) 48{ 49 IInterface obj; 50 51 if (id == 0) 52 obj = A(x); // x^4 53 else 54 obj = B(x, x); // x^4 55 56 return doThing(obj, x) + doThing(obj, x); // 2 * x^4 57} 58 59//TEST_INPUT: type_conformance A:IInterface = 0 60//TEST_INPUT: type_conformance B:IInterface = 1 61 62[numthreads(1, 1, 1)] 63void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) 64{ 65 { 66 var dpx = diffPair(1.0); 67 bwd_diff(f)(dispatchThreadID.x, dpx, 2.0); 68 outputBuffer[0] = dpx.d; // Expect: 2 * 4 * x^3 * dx = 8 * x^3 * dx = 16 69 } 70 71 { 72 var dpx = diffPair(1.5); 73 bwd_diff(f)(dispatchThreadID.x + 1, dpx, 1.0); 74 outputBuffer[1] = dpx.d; // Expect: 2 * 4 * x^3 * dx = 8 * (1.5)^3 * 1.0 = 27 75 } 76}