summaryrefslogtreecommitdiffstats
path: root/tests/autodiff/derived-interface.slang
blob: 5d1ac175f355be58265b71c49c9b9a9907f0fd6a (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
51
52
53
54
55
56
//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], stride=4):out,name=outputBuffer
RWStructuredBuffer<float> outputBuffer;

interface IFoo : IDifferentiable
{
    [Differentiable]
    __init<T:IFoo>(T d); 
    
    [Differentiable]
    static This create(Differential d);

    [Differentiable]
    float getVal();
}

struct Impl : IFoo
{
    float x;
    [Differentiable]
    __init<T : IFoo>(T d)
    {
        this = (Impl)d;
    }
    
    [Differentiable]
    static This create(Differential d)
    {
        This v;
        v.x = d.x * d.x;
        return v;
    }

    [Differentiable]
    float getVal() { return x; }
}

[Differentiable]
float test<T:IFoo>(T.Differential d)
{
    return T.create(d).getVal();
}

[numthreads(1,1,1)]
void computeMain(uint threadId: SV_DispatchThreadID)
{
    Impl.Differential d;
    d.x = 3.0;
    Impl.Differential dd;
    dd.x = 1.0;
    outputBuffer[0] = fwd_diff(test<Impl>)(diffPair(d, dd)).d;
    // CHECK: 6.0
}