yum-mirror/slang

Making it easier to work with shaders

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

Yong HeSupport high order diff pattern: `bwd_diff(fwd_diff(f))`. (#2695)a911ca6e0

master
1.1 KiB51 linesraw
1//TEST(compute):COMPARE_COMPUTE_EX:-cpu -compute -output-using-type -shaderobj
2//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
3//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
4
5//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
6RWStructuredBuffer<float> outputBuffer;
7
8struct A : IDifferentiable
9{
10    float x;
11    int nx;
12}
13
14[BackwardDifferentiable]
15float f(A x)
16{
17    return x.x * x.x;
18}
19
20[BackwardDifferentiable]
21float outerF(A x)
22{
23    A nx;
24    nx.x = x.x * x.x;
25    nx.nx = 2;//x.nx;
26    return f(nx);
27}
28
29[BackwardDifferentiable]
30float df(A x)
31{
32    A.Differential ad;
33    ad.x = 1.0;
34    return __fwd_diff(outerF)(DifferentialPair<A>(x, ad)).d; // 4*x^3
35}
36
37[numthreads(1, 1, 1)]
38void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
39{
40    // Given f(x) = x^4,
41    // f''(x) = 12 * x^2
42    // Expect f''(4) = 192
43    A a;
44    a.x = 4.0;
45    a.nx = 54;
46    A.Differential ad;
47    ad.x = 1.0;
48    var p = diffPair(a, ad);
49    __bwd_diff(df)(p, 1.0);
50    outputBuffer[0] = p.d.x;
51}