yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
945409c4c
master
1// Test calling differentiable function through dynamic dispatch. 2 3//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type 4//DISABLE_TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type 5 6//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer 7RWStructuredBuffer<float> outputBuffer; 8 9[anyValueSize(16)] 10interface IInterface : IDifferentiable 11{ 12 [Differentiable] 13 float calc(float x); 14} 15 16struct A : IInterface 17{ 18 int data1; 19 20 __init(int data1) { this.data1 = data1; } 21 22 [Differentiable] 23 float calc(float x) { return x * x * x * data1; } 24}; 25 26struct B : IInterface 27{ 28 int data1; 29 int data2; 30 31 __init(int data1, int data2) { this.data1 = data1; this.data2 = data2; } 32 33 [Differentiable] 34 float calc(float x) { return x * x * data1 * data2; } 35}; 36 37[Differentiable] 38float doThing(IInterface obj, IInterface obj2, float x) 39{ 40 return obj.calc(x); 41} 42 43//TEST_INPUT: type_conformance A:IInterface = 0 44//TEST_INPUT: type_conformance B:IInterface = 1 45 46[numthreads(1, 1, 1)] 47void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) 48{ 49 { 50 IInterface obj; 51 if (dispatchThreadID.x == 0) 52 obj = A(2); 53 else 54 obj = B(2, 3); 55 56 IInterface obj2 = A(3); 57 58 var dpx = diffPair(1.0); 59 DifferentialPair<IInterface> dpobj = diffPair<IInterface>(obj); 60 DifferentialPair<IInterface> dpobj2 = diffPair<IInterface>(obj2); 61 62 bwd_diff(doThing)(dpobj, dpobj2, dpx, 2.0); // A.calc, expect 12 63 64 IDifferentiable objd = dpobj.d; 65 66 outputBuffer[0] = (isNullDifferential((objd)) ? 1.f : 0.f); 67 } 68}