summaryrefslogtreecommitdiffstats
path: root/tests/experiments/generic/equality-4.slang
blob: 44c0493238756476762935d0e60da1181cdc3d62 (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
//DISABLE_TEST(compute):COMPARE_COMPUTE_EX:-slang -compute -shaderobj

/* A test for equality around interface types

Here again trying to apply equality *outside* of the types (MyStruct) definition.  

Doesn't work:

.slang(24): error 30019: expected an expression of type 'Type', got 'T'
    return T::isEqual(a, b);
    
Note! This may be somewhat of a silly example for equality. We could get what we want here by just
implementing 'isEqual(MyStruct a, MyStruct b)` as a free function and use overloading.     
 */

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

struct MyStruct
{
    int a = 10;
};

interface IEquality
{
    associatedtype Type;
    static bool isEqual(Type a, Type b);
}

extension MyStruct : IEquality
{
    // Do I need this? Is the type This?
    typedef MyStruct Type;
    static bool isEqual(Type a, Type b) { return a.a == b.a; }
};

__generic<T : IEquality>
bool isEqual(T a, T b)
{
    return T::isEqual(a, b);
}
             
[numthreads(4, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
    int index = dispatchThreadID.x;

    MyStruct a = { 1 };
    MyStruct b = { 2 };
    
    bool res = isEqual(a, b);

	outputBuffer[index] = 1 + int(res);
}