summaryrefslogtreecommitdiffstats
path: root/tests/autodiff-dstdlib/vector-length.slang
blob: c5064e54e2fbfbfadf36e6fbf205b078de7a9434 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type

//TEST_INPUT:ubuffer(data=[0 0 0], stride=4):out,name=outputBuffer
RWStructuredBuffer<float> outputBuffer;

float lengthDiffX(float3 x)
{
    return (length(float3(x.x + 0.001, x.yz)) - length(float3(x.x - 0.001, x.yz))) / 0.002;
}
float lengthDiffY(float3 x)
{
    return (length(float3(x.x, x.y + 0.001, x.z)) - length(float3(x.x, x.y - 0.001, x.z))) / 0.002;
}
float lengthDiffZ(float3 x)
{
    return (length(float3(x.xy, x.z + 0.001)) - length(float3(x.xy, x.z - 0.001))) / 0.002;
}

[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
    {
        let x = float3(12, 23, 31);
        var dpx = diffPair(x);
        __bwd_diff(length)(dpx, 1.0);
        outputBuffer[0] = dpx.d[0];
        outputBuffer[1] = dpx.d[1];
        outputBuffer[2] = dpx.d[2];

        // for reference:
        //outputBuffer[3] = lengthDiffX(x);
        //outputBuffer[4] = lengthDiffY(x);
        //outputBuffer[5] = lengthDiffZ(x);
    }
}