yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
d10732742
master
1//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type 2//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -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], stride=4):out,name=outputBuffer 6RWStructuredBuffer<float> outputBuffer; 7 8typedef DifferentialPair<float3> dpfloat3; 9typedef float3.Differential dfloat3; 10 11typedef DifferentialPair<float2> dpfloat2; 12typedef float2.Differential dfloat2; 13 14[BackwardDifferentiable] 15float2 test_reshape(float3 x, float3 y, int i, int j) 16{ 17 float2x3 m = float2x3(x, y); 18 let mSmall = float2x2(m); 19 return mSmall[i] + mSmall[j]; 20} 21 22[BackwardDifferentiable] 23float3 test_vectorFromScalar(float x) 24{ 25 return float3(x); 26} 27 28[BackwardDifferentiable] 29float3x3 test_matrixFromScalar(float x) 30{ 31 return float3x3(x); 32} 33 34[BackwardDifferentiable] 35float2x2 test_matrixConstruct(float a, float b, float c, float d) 36{ 37 return float2x2(a, b, c, d); 38} 39 40[BackwardDifferentiable] 41float3 test_makeVector(float x, float2 y) 42{ 43 return float3(x, y); 44} 45 46[numthreads(1, 1, 1)] 47void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) 48{ 49 { 50 dpfloat3 dpx = dpfloat3(float3(2.0, 3.0, 4.0), float3(0.0, 0.0, 0.0)); 51 dpfloat3 dpy = dpfloat3(float3(1.5, 2.5, 3.5), float3(0.0, 0.0, 0.0)); 52 53 __bwd_diff(test_reshape)(dpx, dpy, 0, 1, dfloat2(1.0, 2.0)); 54 outputBuffer[0] = dpx.d.y; // Expect: 2 55 outputBuffer[1] = dpy.d.y; // Expect: 2 56 } 57 58 { 59 DifferentialPair<float> dpx = diffPair(1.0, 0.0); 60 61 __bwd_diff(test_vectorFromScalar)(dpx, dfloat3(2.0)); 62 outputBuffer[2] = dpx.d; // Expect: 6.0 63 } 64 65 { 66 DifferentialPair<float> dpx = diffPair(1.0, 0.0); 67 68 __bwd_diff(test_matrixFromScalar)(dpx, float3x3(1.0)); 69 outputBuffer[3] = dpx.d; // Expect: 9.0 70 } 71 { 72 DifferentialPair<float> dpa = diffPair(1.0, 0.0); 73 DifferentialPair<float> dpb = diffPair(1.0, 0.0); 74 DifferentialPair<float> dpc = diffPair(1.0, 0.0); 75 DifferentialPair<float> dpd = diffPair(1.0, 0.0); 76 77 __bwd_diff(test_matrixConstruct)(dpa, dpb, dpc, dpd, float2x2(1.0, 2.0, 3.0, 4.0)); 78 outputBuffer[4] = dpa.d; // Expect: 1.0 79 outputBuffer[5] = dpb.d; // Expect: 2.0 80 outputBuffer[6] = dpc.d; // Expect: 3.0 81 outputBuffer[7] = dpd.d; // Expect: 4.0 82 } 83 { 84 DifferentialPair<float> dpx = diffPair(1.0, 0.0); 85 dpfloat2 dpy = dpfloat2(float2(1.5, 2.5), float2(0.0, 0.0)); 86 87 __bwd_diff(test_makeVector)(dpx, dpy, float3(1.0, 1.5, 2.0)); 88 outputBuffer[8] = dpx.d; // Expect: 1.0 89 outputBuffer[9] = dpy.d.x; // Expect: 1.5 90 outputBuffer[10] = dpy.d.y; // Expect: 2.0 91 } 92 93}