yum-mirror/slang

Making it easier to work with shaders

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

Anders LeinoEnable a bunch of WGPU tests (#5513)43df1da01

master
2.3 KiB84 linesraw
1// nested-num.slang
2
3// Test enums defined nested in a struct work as expected. 
4
5//TEST(compute):COMPARE_COMPUTE: -shaderobj
6
7struct Outer
8{
9    enum Channel
10    {
11        Red,
12        Green,
13        Blue,
14        Alpha,
15    }
16    
17    static int doSomething(int v) { return v + 1; }
18    typedef int SomeType;
19    
20    static int someValue = 10;
21    
22    static int getHeuristicResult()
23    {
24        // This tests that the cast heuristic works here. 
25        // when the compiler cannot determine if it's a type or not at this point, 
26        // because these declarations have not been seen.
27            
28        // Has whitespace (between + and the next thing) -> must be an expression
29        int value = (Inner::anotherValue) + 1;
30        // No whitespace, so assumed to be a cast
31        Inner::Enum anotherValue = (Inner::Enum) +1;
32        
33        return Inner::doSomethingElse(value + (int)anotherValue);
34    }
35    
36    struct Inner
37    {
38        enum Enum
39        {
40            A,
41            B,
42        };
43        
44        static int anotherValue = 10;
45        static int doSomethingElse(int a) { return a - 1; }
46    }
47}
48
49int test(int val)
50{
51    Outer::Channel channel = (Outer::Channel)val; // Outer::Channel(val);
52    Outer::Channel otherChannel = channel; // (Outer::Channel)val;
53
54    typedef Outer::Channel Channel;
55
56    int result = 0;
57    if(channel == Channel.Red)          result += 1;
58    if(channel != Channel.Green)        result += 16;
59    if(otherChannel == Channel.Blue)    result += 16*16;
60    if(otherChannel != Channel.Alpha)   result += 16*16*16;
61
62    return result;
63}
64
65//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
66RWStructuredBuffer<int> outputBuffer;
67
68[numthreads(4, 1, 1)]
69void computeMain(int3 dispatchThreadID : SV_DispatchThreadID)
70{
71    int value = (Outer::someValue) + 1 + Outer::getHeuristicResult();
72    Outer::Channel anotherValue = (Outer::Channel) +1;
73    
74    // These work because the type can be determined because they are already declared at this point
75    // Check can see this is a function call
76    value = (Outer::doSomething)(value);
77    // Check can see this is a cast
78    value += (Outer::SomeType)(value);
79    
80    int tid = dispatchThreadID.x;
81    int inVal = tid;
82    int outVal = test(inVal) + value * 2 + int(anotherValue) * 4;
83    outputBuffer[tid] = outVal;
84}