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.8 KiB70 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<float2> dpfloat2;
9typedef DifferentialPair<float3> dpfloat3;
10typedef DifferentialPair<float4> dpfloat4;
11
12[ForwardDifferentiable]
13float3 f(float3 x)
14{
15    return x;
16}
17
18[ForwardDifferentiable]
19float3 g(float3 x, float3 y)
20{
21    float3 a = x + y;
22    float3 b = x - y;
23    return a * b + 2 * x * y;
24}
25
26[ForwardDifferentiable]
27float2 h(float2 x, float2 y)
28{
29    float2 a = x + y;
30    float2 b = x - y;
31    return a * b + 2 * x * y;
32}
33
34[ForwardDifferentiable]
35float4 j(float4 x, float4 y)
36{
37    float4 a = x + y;
38    float4 b = x - y;
39    return a * b + 2 * x * y;
40}
41
42[numthreads(1, 1, 1)]
43void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
44{
45    {
46        float3 a = float3(2.0, 2.0, 2.0);
47        float3 b = float3(1.5, 1.5, 1.5);
48        float3 da = float3(1.0, 1.0, 1.0);
49
50        float2 a2 = float2(2.0, 1.0);
51        float2 b2 = float2(1.5, -2.0);
52
53        float4 a4 = float4(2.0, 1.0, 0.0, 2.0);
54        float4 b4 = float4(1.5, -2.0, 1.0, 1.5);
55
56        outputBuffer[0] = __fwd_diff(f)(dpfloat3(a, da)).d.z;    // Expect: 1
57
58        outputBuffer[1] = __fwd_diff(g)(
59            dpfloat3(a, da),
60            dpfloat3(b, float3(2.0, 1.0, 0.0))).d.y;        // Expect: 8
61
62        outputBuffer[2] = __fwd_diff(h)(
63            dpfloat2(a2, float2(1.0, 0.0)),
64            dpfloat2(b2, float2(1.0, 1.0))).d.x;            // Expect: 8
65
66        outputBuffer[3] = __fwd_diff(j)(
67            dpfloat4(a4, float4(1.0)),
68            dpfloat4(b4, float4(2.0))).d.w;                 // Expect: 9
69    }
70}