yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
7c2ff5475
master
1//TEST(compute):COMPARE_COMPUTE_EX:-dx12 -compute -shaderobj -output-using-type 2//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type 3//TEST(compute):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type 4//TEST(compute):COMPARE_COMPUTE_EX:-wgpu -compute -shaderobj -output-using-type 5//TEST:SIMPLE(filecheck=CHECK): -target hlsl -profile cs_5_0 -entry computeMain -line-directive-mode none 6//DISABLE_TEST:SIMPLE(filecheck=CHK):-target glsl -stage compute -entry computeMain -report-checkpoint-intermediates 7 8//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer 9RWStructuredBuffer<float> outputBuffer; 10 11typedef DifferentialPair<float> dpfloat; 12typedef float.Differential dfloat; 13 14// Test that compute does not have a context. 15// CHECK-NOT: struct {{[a-zA-Z0-9_]*}}_compute_{{[a-zA-Z0-9_]*}} 16 17[BackwardDifferentiable] 18[PreferRecompute] 19float compute(float x, float y) 20{ 21 return x * y; 22} 23 24[BackwardDifferentiable] 25[ForceInline] 26float infinitesimal(float x) 27{ 28 return x - detach(x); 29} 30 31// Test that computeLoop compiles to just return 0. 32// CHECK: float3 computeLoop{{[_0-9]*}}(float y{{[_0-9]*}}) 33// CHECK-NOT: for{{.*}} 34// CHECK: return (float3)0 35 36// Test that computeLoop's intermediates have no float sitting 37// around (must not cache the outvar from 'compute()') 38// CHECK: struct s_bwd_prop_computeLoop_Intermediates 39// CHECK-NEXT: { 40// CHECK-NOT: {{[A-Za-z0-9_]+}} {{[A-Za-z0-9_]+}}[{{.*}}] 41// CHECK: } 42 43[PreferRecompute] 44[BackwardDifferentiable] 45[ForceInline] 46float3 infinitesimal(float3 x) 47{ 48 return x - detach(x); 49} 50 51//CHK: note: checkpointing context of 20 bytes associated with function: 'computeLoop' 52[BackwardDifferentiable] 53[PreferRecompute] 54float3 computeLoop(float y) 55{ 56 //CHK: note: 4 bytes (float) used to checkpoint the following item: 57 float w = 0; 58 59 //CHK: note: 12 bytes (Vector<float, 3> ) used to checkpoint the following item: 60 float3 w3 = float3(0, 0, 0); 61 62 //CHK: note: 4 bytes (int32_t) used for a loop counter here: 63 for (int i = 0; i < 8; i++) 64 { 65 float k = compute(i, y); 66 float g = select(k > 0.0, k, 0.0); 67 w += g; 68 w3 += float3(g) * y; 69 } 70 71 var p = (w3 / float3(w)); 72 return infinitesimal(p); 73} 74 75// Since computeLoop is recomputed, test_simple_loop should have nothing to store 76// therefore we check that there is no intermediate context type generated for test_simple_loop. 77 78// CHECK-NOT: struct {{[a-zA-Z0-9_]*}}test_simple_loop{{[a-zA-Z0-9_]*}} 79[BackwardDifferentiable] 80float test_simple_loop(float y) 81{ 82 float3 x = computeLoop(y); 83 return y + x.x; 84} 85 86[numthreads(1, 1, 1)] 87void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID) 88{ 89 { 90 dpfloat dpa = dpfloat(1.0, 0.0); 91 92 __bwd_diff(test_simple_loop)(dpa, 1.0f); 93 outputBuffer[0] = dpa.d; // Expect: 1.0 94 } 95 96 { 97 dpfloat dpa = dpfloat(0.4, 0.0); 98 99 __bwd_diff(test_simple_loop)(dpa, 0.5f); 100 outputBuffer[1] = dpa.d; // Expect: 2.0 101 } 102 103 outputBuffer[2] = computeLoop(1.0).x; 104} 105 106//CHK-NOT: note