yum-mirror/slang

Making it easier to work with shaders

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

Sai Praveen BangaruAdded `[BackwardDifferentiable]` tags for intrinsic + builtin methods (#2732)7292edbd3

master
1.1 KiB47 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
8[BackwardDifferentiable]
9float f(float x)
10{
11    return sin(x) + x * x;
12}
13
14[BackwardDifferentiable]
15float outerF(float x)
16{
17    return f(pow(x, 3));
18}
19
20[BackwardDifferentiable]
21float df(float x)
22{
23    return __fwd_diff(outerF)(DifferentialPair<float>(x, 1.0)).d; // x^3 + x^6
24}
25
26[numthreads(1, 1, 1)]
27void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
28{
29    // Given f(x) = sin(x^3) + x^6
30    // f'(x) = cos(x^3)*(3*x^2) + 6*x^5
31    // f''(x) = 6*cos(x^3)*x - 9*sin(x^3)*(x^4) + 30*x^4
32    //
33
34    // Expect f''(4) = -0.291
35    {
36        var p = diffPair(1.0, 0.0);
37        __bwd_diff(df)(p, 1.0);
38        outputBuffer[0] = p.d;
39    }
40
41    // Expect f''(4) = -0.653643
42    {
43        var p = diffPair(2.0, 0.0);
44        __bwd_diff(df)(p, 0.5);
45        outputBuffer[1] = p.d;
46    }
47}