yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
d10732742
master
1//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type 2//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type 3//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type 4 5//TEST_INPUT:ubuffer(data=[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], stride=4):out,name=outputBuffer 6RWStructuredBuffer<float> outputBuffer; 7 8typedef DifferentialPair<float> dpfloat; 9typedef DifferentialPair<float2> dpfloat2; 10 11[BackwardDifferentiable] 12float diffMax(float x, float y) 13{ 14 return max(x, y); 15} 16 17[BackwardDifferentiable] 18float2 diffMax(float2 x, float2 y) 19{ 20 return max(x, y); 21} 22 23[BackwardDifferentiable] 24float diffMin(float x, float y) 25{ 26 return min(x, y); 27} 28 29[BackwardDifferentiable] 30float2 diffMin(float2 x, float2 y) 31{ 32 return min(x, y); 33} 34 35[numthreads(1, 1, 1)] 36void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID) 37{ 38 // Test max() with x < y 39 { 40 dpfloat dpx = dpfloat(2.0, 1.0); 41 dpfloat dpy = dpfloat(5.0, -2.0); 42 dpfloat res = __fwd_diff(diffMax)(dpx, dpy); 43 outputBuffer[0] = res.p; // Expect: 5.000000 44 outputBuffer[1] = res.d; // Expect: -2.000000 45 } 46 47 // Test max() with x == y 48 { 49 dpfloat dpx = dpfloat(3.0, 1.0); 50 dpfloat dpy = dpfloat(3.0, -2.0); 51 dpfloat res = __fwd_diff(diffMax)(dpx, dpy); 52 outputBuffer[2] = res.p; // Expect: 3.000000 53 outputBuffer[3] = res.d; // Expect: -0.500000 (average of 1.0 and -2.0) 54 } 55 56 // Test min() with x > y 57 { 58 dpfloat dpx = dpfloat(5.0, 1.0); 59 dpfloat dpy = dpfloat(2.0, -2.0); 60 dpfloat res = __fwd_diff(diffMin)(dpx, dpy); 61 outputBuffer[4] = res.p; // Expect: 2.000000 62 outputBuffer[5] = res.d; // Expect: -2.000000 63 } 64 65 // Test min() with x == y 66 { 67 dpfloat dpx = dpfloat(3.0, 1.0); 68 dpfloat dpy = dpfloat(3.0, -2.0); 69 dpfloat res = __fwd_diff(diffMin)(dpx, dpy); 70 outputBuffer[6] = res.p; // Expect: 3.000000 71 outputBuffer[7] = res.d; // Expect: -0.500000 (average of 1.0 and -2.0) 72 } 73 74 // Test backward-mode max() with x == y 75 { 76 dpfloat dpx = dpfloat(3.0, 0.0); 77 dpfloat dpy = dpfloat(3.0, 0.0); 78 __bwd_diff(diffMax)(dpx, dpy, 1.0); 79 outputBuffer[8] = dpx.d; // Expect: 0.500000 (half of gradient) 80 outputBuffer[9] = dpy.d; // Expect: 0.500000 (half of gradient) 81 } 82 83 // Test backward-mode min() with x == y 84 { 85 dpfloat dpx = dpfloat(3.0, 0.0); 86 dpfloat dpy = dpfloat(3.0, 0.0); 87 __bwd_diff(diffMin)(dpx, dpy, 1.0); 88 outputBuffer[10] = dpx.d; // Expect: 0.500000 (half of gradient) 89 outputBuffer[11] = dpy.d; // Expect: 0.500000 (half of gradient) 90 } 91 92 // Test vector max() with x == y 93 { 94 dpfloat2 dpx = dpfloat2(float2(3.0, 4.0), float2(1.0, 2.0)); 95 dpfloat2 dpy = dpfloat2(float2(3.0, 2.0), float2(-2.0, -3.0)); 96 dpfloat2 res = __fwd_diff(diffMax)(dpx, dpy); 97 outputBuffer[12] = res.p[0]; // Expect: 3.000000 98 outputBuffer[13] = res.d[0]; // Expect: -0.500000 (average of 1.0 and -2.0) 99 outputBuffer[14] = res.p[1]; // Expect: 4.000000 100 outputBuffer[15] = res.d[1]; // Expect: 2.000000 101 } 102 103 // Test vector min() with x == y 104 { 105 dpfloat2 dpx = dpfloat2(float2(3.0, 4.0), float2(1.0, 2.0)); 106 dpfloat2 dpy = dpfloat2(float2(3.0, 2.0), float2(-2.0, -3.0)); 107 dpfloat2 res = __fwd_diff(diffMin)(dpx, dpy); 108 outputBuffer[16] = res.p[0]; // Expect: 3.000000 109 outputBuffer[17] = res.d[0]; // Expect: -0.500000 (average of 1.0 and -2.0) 110 outputBuffer[18] = res.p[1]; // Expect: 2.000000 111 outputBuffer[19] = res.d[1]; // Expect: -3.000000 112 } 113}