yum-mirror/slang

Making it easier to work with shaders

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

Yong HeFix WGSL parameter block binding. (#5500)2533125cb

master
1.4 KiB43 linesraw
1// Test that the compiler can hoist the texture sample out of the loop.
2// We currently have no way to verify the hoist actually take place, but this test
3// allows us to manually inspect the generated code to see that happen.
4// TODO: use a pattern checker on the resulting code to make sure there is only
5// one call to `SampleLevel`.
6
7//TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj -output-using-type
8//TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -compute -shaderobj -output-using-type
9//TEST(compute):COMPARE_COMPUTE_EX:-cpu -compute -output-using-type -shaderobj
10//TEST(compute):COMPARE_COMPUTE:-slang -compute -mtl -output-using-type -render-features argument-buffer-tier-2
11
12//TEST_INPUT:ubuffer(data=[0 0 0 0 0], stride=4):out,name=outputBuffer
13RWStructuredBuffer<float> outputBuffer;
14
15struct Params : IDifferentiable
16{
17    Texture2D tex;
18    SamplerState sampler;
19}
20
21//TEST_INPUT:set gParams = new Params{Texture2D(size=4, content=one), Sampler}
22ParameterBlock<Params> gParams;
23
24[BackwardDifferentiable]
25float f(float x)
26{
27    float sum = 0.0;
28    for (int i = 0; i < 3; i++)
29    {
30        float t = (no_diff gParams.tex.SampleLevel(gParams.sampler, float2(0.0), 0)).x;
31        sum += t * x * x;
32    }
33    return sum;
34}
35
36[numthreads(1, 1, 1)]
37void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
38{
39    var dpa = diffPair(3.0);
40
41    __bwd_diff(f)(dpa, 1.0);
42    outputBuffer[0] = dpa.d.x; // Expect: 18
43}