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.0 KiB65 linesraw
1//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
2//TEST(compute):COMPARE_COMPUTE_EX:-cuda -compute -shaderobj -output-using-type
3//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
4//TEST:SIMPLE(filecheck=CHECK): -target hlsl -profile cs_5_0 -entry computeMain -line-directive-mode none
5
6//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
7RWStructuredBuffer<float> outputBuffer;
8
9struct A : IDifferentiable
10{
11    float data[5];
12};
13
14// Check that the intermediate context of B.eval does not have any arrays.
15// This will fail if the induction variable is not properly detected, or
16// if the various loop restructuring passes accidentally introduce additional 
17// loop state.
18// 
19
20// CHECK: struct s_bwd_prop_B_eval_Intermediates_0
21// CHECK-NOT: int {{[A-Za-z0-9_]+}}[{{.*}}]
22// CHECK: }
23
24__generic<let TBsdfCount : int>
25struct B
26{
27    [Differentiable]
28    float3 eval(const A miData, const float3 wi, const float3 wo)
29    {
30        float3 albedo;
31        for (uint i = 0; i < 3; i++) albedo[i] = miData.data[i];
32
33        float3 result = float3(1.f);
34        [ForceUnroll] for (uint i = 0; i < TBsdfCount; i++) result *= albedo;
35        return result;
36    }
37};
38
39[Differentiable]
40float3 outerEval(const A miData, const float3 wi, const float3 wo)
41{
42    B<3> b;
43    return b.eval(miData, wi, wo);
44}
45
46[numthreads(1, 1, 1)]
47void computeMain(uint3 dispatchThreadID: SV_DispatchThreadID)
48{
49    float3 wi = float3(2.0, 3.0, 0);
50    float3 wo = float3(1.0, 1.0, 0);
51    float data[5] = { 1, 2, 3, 4, 5 };
52    A dataStruct = { data };
53
54    float3 val = outerEval(dataStruct, wi, wo);
55    outputBuffer[0] = val.x;
56
57    DifferentialPair<float3> dpwi = diffPair(wi);
58    DifferentialPair<float3> dpwo = diffPair(wo);
59    DifferentialPair<A> dpdata = diffPair(dataStruct);
60    float3 dOut = float3(1.0, 0.0, 0.0);
61    __bwd_diff(outerEval)(dpdata, dpwi, dpwo, dOut);
62
63    // Write output
64    outputBuffer[0] = dpdata.d.data[0];
65}