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//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer 7RWStructuredBuffer<float> outputBuffer; 8 9struct A : IDifferentiable 10{ 11 float x; 12 int nx; 13} 14 15[BackwardDifferentiable] 16float f(A x) 17{ 18 A rs; 19 rs.x = 1.0; 20 for (int i = 0; i < 2; i++) 21 rs.x = rs.x * x.x; 22 return rs.x; 23} 24 25[BackwardDifferentiable] 26float outerF(A x) 27{ 28 A nx; 29 nx.x = 1.0; 30 for (int i = 0; i < 2; i++) 31 nx.x = nx.x * x.x; 32 nx.nx = 2;//x.nx; 33 return f(nx); 34} 35 36[BackwardDifferentiable] 37float df(A x) 38{ 39 A.Differential ad; 40 ad.x = 0.0; 41 var p = diffPair(x, ad); 42 __bwd_diff(outerF)(p, 1.0); 43 return p.d.x; 44} 45 46[numthreads(1, 1, 1)] 47void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) 48{ 49 // Given f(x) = x^4, 50 // f''(x) = 12 * x^2 51 // Expect f''(4) = 192 52 A a; 53 a.x = 4.0; 54 a.nx = 54; 55 A.Differential ad; 56 ad.x = 1.0; 57 var p = diffPair(a, ad); 58 outputBuffer[0] = __fwd_diff(df)(p).d; 59}