summaryrefslogtreecommitdiffstats
path: root/tests/autodiff/generic-constructor.slang
blob: 9084b6e2753313c55210ca9a49163c81f0a429e9 (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 0], stride=4):out,name=outputBuffer
RWStructuredBuffer<float> outputBuffer;

interface IFoo : IDifferentiable
{
    [Differentiable]
    __init(Differential v);
}

struct Impl : IFoo
{
    float x;

    [Differentiable]
    __init(Differential v)
    {
        x = v.x;
    }

    // We have to add this __init so that the following code can still work:
    // Impl.Differential v0 = { (float)x };
    // because when there is a explicit constructor defined, we will not fall back
    // to legacy constructor. So this construction will fail.
    [Differentiable]
    __init(float v)
    {
        x = v;
    }
}

[Differentiable]
float test(float x)
{
    Impl.Differential v0 = { x };
    var v1 = Impl(v0);
    return v1.x * v1.x;
}

[numthreads(1,1,1)]
void computeMain(uint tid : SV_DispatchThreadID)
{
    var p = diffPair(3.0, 0.0);
    bwd_diff(test)(p, 1.0);
    outputBuffer[tid] = p.d;
    // CHECK: 6.0
}