yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
b61be5e6f
master
1//TEST(compute):COMPARE_COMPUTE_EX:-cpu -compute -output-using-type -shaderobj 2//TEST(compute):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type 3//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type 4//TEST(compute):COMPARE_COMPUTE_EX:-dx12 -compute -shaderobj -output-using-type 5 6 7//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer 8RWStructuredBuffer<float> outputBuffer; 9 10struct A : IDifferentiable 11{ 12 float x; 13 int nx; 14} 15 16[BackwardDifferentiable] 17float f(A x) 18{ 19 A rs; 20 rs.x = 1.0; 21 for (int i = 0; i < 2; i++) 22 rs.x = rs.x * x.x; 23 return rs.x; 24} 25 26[BackwardDifferentiable] 27float outerF(A x) 28{ 29 A nx; 30 nx.x = 1.0; 31 int i = 0; 32 [MaxIters(3)] 33 do 34 { 35 nx.x = nx.x * x.x; 36 i++; 37 } 38 while (i < 2); 39 nx.nx = 2;//x.nx; 40 return f(nx); 41} 42 43[BackwardDifferentiable] 44float df(A x) 45{ 46 A.Differential ad; 47 ad.x = 0.0; 48 var p = diffPair(x, ad); 49 __bwd_diff(outerF)(p, 1.0); 50 return p.d.x; 51} 52 53[numthreads(1, 1, 1)] 54void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) 55{ 56 // Given f(x) = x^4, 57 // f''(x) = 12 * x^2 58 // Expect f''(4) = 192 59 A a; 60 a.x = 4.0; 61 a.nx = 54; 62 A.Differential ad; 63 ad.x = 1.0; 64 var p = diffPair(a, ad); 65 outputBuffer[0] = __fwd_diff(df)(p).d; 66}