yum-mirror/slang

Making it easier to work with shaders

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

Harsh Aggarwal (NVIDIA)Fix 7723 - Add autodiff tests (#7919)d10732742

master
1.2 KiB57 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:-cuda -compute -shaderobj -output-using-type
4
5//TEST_INPUT:ubuffer(data=[0], stride=4):out,name=outputBuffer
6RWStructuredBuffer<float> outputBuffer;
7
8struct A
9{
10    float x;
11
12    float f(float v)
13    {
14        return v * v;
15    }
16}
17
18extension A
19{
20    [ForwardDerivativeOf(f)]
21    DifferentialPair<float> diff_f(DifferentialPair<float> v)
22    {
23        return diffPair(v.p * v.p, v.p * v.d * 2.0);
24    }
25}
26
27struct Foo<T : IDifferentiable>
28{
29    T value;
30    T doThing() { return value; }
31}
32
33extension<T : IDifferentiable> Foo<T>
34{
35    [ForwardDerivativeOf(doThing)]
36    [NoDiffThis]
37    DifferentialPair<T> diff_doThing()
38    {
39        return diffPair(value, T.dzero());
40    }
41}
42
43
44[ForwardDifferentiable]
45float test(Foo<float> obj, float v)
46{
47    return obj.doThing() * v;
48}
49
50[numthreads(1, 1, 1)]
51void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
52{
53    Foo<float> a = {0.0};
54    var p = diffPair(3.0, 1.0);
55    let rs = __fwd_diff(test)(a, p);
56    outputBuffer[0] = rs.d;
57}