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
2.2 KiB89 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 0 0 0], stride=4):out,name=outputBuffer
6RWStructuredBuffer<float> outputBuffer;
7
8typedef DifferentialPair<float> dpfloat;
9typedef DifferentialPair<float3> dpfloat3;
10
11[ForwardDerivative(pow_jvp)]
12float pow_(float x, float n)
13{
14    return pow<float>(x, n);
15}
16
17[ForwardDerivative(max_jvp)]
18float max_(float x, float y)
19{
20    return max<float>(x, y);
21}
22
23dpfloat pow_jvp(dpfloat x, dpfloat n)
24{
25    return dpfloat(
26        pow(x.p, n.p),
27        x.d * n.p * pow(x.p, n.p-1) + 
28            ((n.d != 0.0) ? (n.d * pow(x.p, n.p) * log(x.p)) : 0.0));
29}
30
31dpfloat max_jvp(dpfloat x, dpfloat y)
32{
33    return dpfloat(
34        max(x.p, y.p),
35        (x.p > y.p) ? x.d : y.d);
36}
37
38
39/* Fresnel Schlick example */
40[ForwardDifferentiable]
41float3 fresnel(float3 f0, float3 f90, float cosTheta)
42{
43    return f0 + (f90 - f0) * pow_(max_(1 - cosTheta, 0.0), 5);
44}
45
46[ForwardDifferentiable]
47float g(float a, float b, float c)
48{
49    return fresnel(float3(a), float3(b), 2 * c * c).y;
50}
51
52[numthreads(1, 1, 1)]
53void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
54{
55     {
56        float3 f0 = float3(0.2, 0.2, 0.2);
57        float3 f90 = float3(0.7, 0.7, 0.7);
58        float cosTheta = 0.5;
59
60        float3 d_f0 = float3(0.1, 0.1, 0.1);
61        float3 d_f90 = float3(0.9, 0.9, 0.9);
62        float d_cosTheta = 1.0;
63
64        outputBuffer[0] = __fwd_diff(fresnel)(
65            dpfloat3(f0, d_f0),
66            dpfloat3(f90, d_f90),
67            dpfloat(cosTheta, d_cosTheta)).d.y; // Expect: -0.031250
68
69        float a = 1.0;
70        float b = -0.4;
71        float c = 0.5;
72
73        float da = -0.4;
74        float db = -1.0;
75        float dc = 0.2;
76
77        outputBuffer[1] = __fwd_diff(g)(
78            dpfloat(a, da),
79            dpfloat(b, db),
80            dpfloat(c, dc)).d;                 // Expect: -0.24375
81
82        outputBuffer[2] = g(a, b, c);       // Expect: 0.95625
83
84        outputBuffer[3] = __fwd_diff(g)(
85            dpfloat(a, da),
86            dpfloat(b, db),
87            dpfloat(3.0, dc)).d;               // Expect: -0.4;
88    }
89}