yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
c3df36043
master
1//TEST:SIMPLE(filecheck=CHECK): -target hlsl -profile cs_5_0 -entry computeMain -line-directive-mode none 2//TEST:SIMPLE(filecheck=CHECK): -target cuda -profile cs_5_0 -entry computeMain -line-directive-mode none 3//DISABLE_TEST:SIMPLE(filecheck=CTX):-target glsl -stage compute -entry computeMain -report-checkpoint-intermediates 4 5//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer 6RWStructuredBuffer<float> outputBuffer; 7 8typedef DifferentialPair<float> dpfloat; 9typedef float.Differential dfloat; 10 11[BackwardDerivative(bwd_load)] 12float load(uint idx) 13{ 14 return outputBuffer[idx]; 15} 16 17void bwd_load(uint idx, float dOut) 18{ 19 outputBuffer[idx + 2] += dOut; 20} 21 22[BackwardDerivative(bwd_store)] 23void store(uint idx, float a) 24{ 25 outputBuffer[idx] = a; 26} 27 28[ForceInline] 29float inner_bwd_store(uint idx) 30{ 31 return outputBuffer[idx + 2]; 32} 33 34[ForceInline] 35void bwd_store(uint idx, inout DifferentialPair<float> a) 36{ 37 a = diffPair(a.p, inner_bwd_store(idx)); 38} 39 40[BackwardDerivative(bwd_g)] 41float g(float x) 42{ 43 return load(1) * load(1); 44} 45 46void bwd_g(inout DifferentialPair<float> x, float dOut) 47{ 48 float y = load(1); 49 x = diffPair(x.p + 2 * y, x.d + 2 * y * dOut); 50 store(0, x.d); 51} 52 53[BackwardDifferentiable] 54float f(int p, float x) 55{ 56 float y = g(x); 57 58 store(0, y); 59 60 return 0; 61} 62 63// Check that there are no calls to primal_ctx_f in bwd_f. 64 65// CHECK: void s_bwd_f_{{[0-9]+}} 66// CHECK-NOT: s_primal_ctx_f_{{[0-9]+}} 67// CHECK: return 68 69[numthreads(1, 1, 1)] 70void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) 71{ 72 dpfloat dpa = dpfloat(2.0, 0.0); 73 74 bwd_diff(f)(0, dpa, 1.0f); 75 outputBuffer[0] = dpa.d; // Expect: 1 76} 77// CTX: note: