yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
1b82501dd
master
1//TEST(compute):COMPARE_COMPUTE: -shaderobj -output-using-type 2//TEST(compute):COMPARE_COMPUTE: -vk -shaderobj -output-using-type 3//TEST(compute):COMPARE_COMPUTE:-cpu -shaderobj -output-using-type 4 5//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0 0], stride=4):out,name=outputBuffer 6RWStructuredBuffer<float> outputBuffer; 7 8[Differentiable] 9float testFunc(float a) 10{ 11 float x = a / (abs(a) + 0.5f); 12 { 13 defer x = sqrt(x); 14 15 x = x * 0.5f + 0.5f; 16 17 if (a < 0) 18 { 19 x += 1.0f; 20 // NOTE suprising but correct behaviour here: 'defer' occurs after 21 // the return statement's value has been computed, so mutating 'x' 22 // no longer affects anything. 23 return x; 24 } 25 26 x += 0.5f; 27 } 28 return x; 29} 30 31[numthreads(1, 1, 1)] 32void computeMain(int3 dispatchThreadID: SV_DispatchThreadID) 33{ 34 outputBuffer[0] = testFunc(0); 35 outputBuffer[1] = testFunc(0.5); 36 outputBuffer[2] = testFunc(-0.5); 37 38 DifferentialPair<float> d1 = diffPair(0.0); 39 bwd_diff(testFunc)(d1, 1.0); 40 DifferentialPair<float> d2 = diffPair(0.5); 41 bwd_diff(testFunc)(d2, 1.0); 42 DifferentialPair<float> d3 = diffPair(-0.5); 43 bwd_diff(testFunc)(d3, 1.0); 44 45 outputBuffer[3] = d1.d; 46 outputBuffer[4] = d2.d; 47 outputBuffer[5] = d3.d; 48 49 d1 = diffPair(0.0, 1.0); 50 d2 = diffPair(0.5, 1.0); 51 d3 = diffPair(-0.5, 1.0); 52 53 outputBuffer[6] = fwd_diff(testFunc)(d1).d; 54 outputBuffer[7] = fwd_diff(testFunc)(d2).d; 55 outputBuffer[8] = fwd_diff(testFunc)(d3).d; 56}