summaryrefslogtreecommitdiffstats
path: root/tests/autodiff/out-parameters-2.slang
blob: ea4a78654371a8acee32243fb26289f349d21a64 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-slang -compute -shaderobj -output-using-type
//TEST(compute, vulkan):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-vk -compute -shaderobj -output-using-type
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-cuda -compute -shaderobj -output-using-type

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

typedef DifferentialPair<float> dpfloat;

struct Foo : IDifferentiable
{
    float a;
    int b;
}

[PreferCheckpoint]
float k()
{
    return outputBuffer[3] + 1;
}

[Differentiable]
void h(float x, float y, out Foo result)
{
    float p = no_diff k();
    float m = x + y + p;
    float n = x - y;
    float r = m * n + 2 * x * y;

    result = {r, 2};
}

[numthreads(1, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
    float x = 2.0;
    float y = 3.5;
    float dx = 1.0;
    float dy = 0.5;

    dpfloat dresult;
    dpfloat dpx = diffPair(x);
    dpfloat dpy = diffPair(y);
    Foo.Differential dFoo;
    dFoo.a = 1.0;
    bwd_diff(h)(dpx, dpy, dFoo);

    outputBuffer[0] = dpx.d; // CHECK: 12.0
    outputBuffer[1] = dpy.d; // CHECK: -4.0
}