summaryrefslogtreecommitdiffstats
path: root/tests/language-feature/enums/nested-enum.slang
blob: 5b45abae78f5b13543feac4cf1b441d3c7144d48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// nested-num.slang

// Test enums defined nested in a struct work as expected. 

//TEST(compute):COMPARE_COMPUTE: -shaderobj

struct Outer
{
    enum Channel
    {
        Red,
        Green,
        Blue,
        Alpha,
    }
    
    static int doSomething(int v) { return v + 1; }
    typedef int SomeType;
    
    static int someValue = 10;
    
    static int getHeuristicResult()
    {
        // This tests that the cast heuristic works here. 
        // when the compiler cannot determine if it's a type or not at this point, 
        // because these declarations have not been seen.
            
        // Has whitespace (between + and the next thing) -> must be an expression
        int value = (Inner::anotherValue) + 1;
        // No whitespace, so assumed to be a cast
        Inner::Enum anotherValue = (Inner::Enum) +1;
        
        return Inner::doSomethingElse(value + (int)anotherValue);
    }
    
    struct Inner
    {
        enum Enum
        {
            A,
            B,
        };
        
        static int anotherValue = 10;
        static int doSomethingElse(int a) { return a - 1; }
    }
}

int test(int val)
{
    Outer::Channel channel = (Outer::Channel)val; // Outer::Channel(val);
    Outer::Channel otherChannel = channel; // (Outer::Channel)val;

    typedef Outer::Channel Channel;

    int result = 0;
    if(channel == Channel.Red)          result += 1;
    if(channel != Channel.Green)        result += 16;
    if(otherChannel == Channel.Blue)    result += 16*16;
    if(otherChannel != Channel.Alpha)   result += 16*16*16;

    return result;
}

//TEST_INPUT:ubuffer(data=[0 0 0 0], stride=4):out,name=outputBuffer
RWStructuredBuffer<int> outputBuffer;

[numthreads(4, 1, 1)]
void computeMain(int3 dispatchThreadID : SV_DispatchThreadID)
{
    int value = (Outer::someValue) + 1 + Outer::getHeuristicResult();
    Outer::Channel anotherValue = (Outer::Channel) +1;
    
    // These work because the type can be determined because they are already declared at this point
    // Check can see this is a function call
    value = (Outer::doSomething)(value);
    // Check can see this is a cast
    value += (Outer::SomeType)(value);
    
    int tid = dispatchThreadID.x;
    int inVal = tid;
    int outVal = test(inVal) + value * 2 + int(anotherValue) * 4;
    outputBuffer[tid] = outVal;
}