yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Yong HeOverhaul `transposeParameterBlock` to support `inout` params. (#2621)228e71dab

master
1.7 KiB65 linesraw
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:-cpu -compute -output-using-type -shaderobj
4
5//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer
6RWStructuredBuffer<float> outputBuffer;
7
8struct D : IDifferentiable
9{
10    float n;
11    float m;
12}
13struct C : IDifferentiable
14{
15    float3 t;
16    float v;
17}
18
19struct B : IDifferentiable
20{
21    float2 f;
22    C arr[2];
23}
24
25struct A : IDifferentiable
26{
27    float x;
28    float y;
29    B fb;
30    C aarr[3];
31    D dv;
32};
33
34[BackwardDifferentiable]
35A f(A a, int i)
36{
37    A aout;
38    aout.y = 2 * a.x;
39    aout.y = aout.y + 2 * a.x;
40    aout.x = aout.y + 5 * a.x;
41    aout.aarr[1].t = float3(a.y, 0.0, a.x);
42    aout.aarr[1].t = float3(a.y, 1.0, a.x + 1.0);
43    D nd = { a.x * 4.0f, 1.0f };
44    aout.dv = nd;
45    aout.dv.m = aout.dv.n * 0.5f;
46    // Test that writes to a potentially dynamic address multiple times
47    // is allowed and will propagate the correct derivative.
48    aout.fb.arr[i].v = a.x * 2.0; // since this value is overwritten, the diff will not accumulate to a.x
49    aout.fb.arr[i].v = a.x * 3.0;
50    return aout;
51}
52
53[numthreads(1, 1, 1)]
54void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
55{
56    A a = {1.0, 2.0};
57
58    var dpa = diffPair(a);
59
60    A.Differential dout = { 1.0, 1.0, { float2(0), { { float3(1.0), 1.0 }, { float3(1.0), 1.0 } } }, { { float3(1.0), 1.0 }, { float3(1.0), 1.0 }, { float3(1.0), 1.0 } }, {1.0, 1.0} };
61    
62    __bwd_diff(f)(dpa, 1, dout);
63    outputBuffer[0] = dpa.d.x; // Expect: 23
64    outputBuffer[1] = dpa.d.y; // Expect: 0
65}