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 KiB64 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 0 0], stride=4):out,name=outputBuffer
6RWStructuredBuffer<float> outputBuffer;
7
8[Differentiable]
9[PreferRecompute]
10float3 diffRayIntersectTriangle(no_diff float3 rayOrigin, float3 rayDir, no_diff float3 p[3])
11{
12    float3 e1 = p[1] - p[0];
13    float3 e2 = p[2] - p[0];
14    float3 pVec = cross(rayDir, e2);
15    float divisor = dot(pVec, e1);
16    float3 s = rayOrigin - p[0];
17    float u = dot(s, pVec) / divisor;
18    float3 qVec = cross(s, e1);
19    float v = dot(rayDir, qVec) / divisor;
20    float t = dot(e2, qVec) / divisor;
21    return float3(u, v, t);
22}
23
24[Differentiable]
25[PreferRecompute]
26float3 diffRayIntersectTriangle2(no_diff float3 rayOrigin, float3 rayTarget, no_diff float3 p[3])
27{
28    float3 rayDir = normalize(rayTarget - rayOrigin);
29    float3 uvt = diffRayIntersectTriangle(rayOrigin, rayDir, p);
30    float3 result = (1.f - uvt.x - uvt.y) * p[0] + uvt.x * p[1] + uvt.y * p[2];
31    return result;
32}
33
34[numthreads(1, 1, 1)]
35void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
36{
37    float3 shadePos = float3(0.674034, 0.0, 0.123171);
38    float3 targetPos = float3(0.5, 0.2, -1.0);
39    float3 triPos[3] = { float3(0.0, 1.0, -1.0), float3(1.0, 1.0, 0.0), float3(0.0, 1.0, 0.0) };
40
41    // Forward-mode
42    DifferentialPair<float3> dpIsectPos = fwd_diff(diffRayIntersectTriangle2)(
43        shadePos,
44        DifferentialPair<float3>(targetPos, float3(1.0, 0.0, 0.0)),
45        triPos
46    );
47
48    outputBuffer[0] = dpIsectPos.d[0]; // Expect: 5.0
49    outputBuffer[1] = dpIsectPos.d[1]; // Expect: 0.0
50    outputBuffer[2] = dpIsectPos.d[2]; // Expect: 0.0
51
52    // Reverse-mode
53    DifferentialPair<float3> dpTargetPos = diffPair(targetPos, float3(0.f));
54    bwd_diff(diffRayIntersectTriangle2)(
55        shadePos,
56        dpTargetPos,
57        triPos,
58        float3(1.f, 1.f, 1.f)
59    );
60
61    outputBuffer[3] = dpTargetPos.d[0]; // Expect: 5.0
62    outputBuffer[4] = dpTargetPos.d[1]; // Expect: 32.4301
63    outputBuffer[5] = dpTargetPos.d[2]; // Expect: 5.0
64}