summaryrefslogtreecommitdiff
path: root/tests/language-feature/bit-cast/struct-bit-cast.slang
blob: 9c4a039c01ceefecfe175e02f24b48e99302d350 (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
// struct-bit-cast.slang

//TEST(compute):COMPARE_COMPUTE: -shaderobj

// Test that bit_cast works for bit-reinterpreting one struct type as another.

struct Foo
{
    uint a;
    float b;
    float2 fvec;
}

struct Inner
{
    int v;
    uint s;
}

struct Bar
{
    int u;
    Inner i;
    uint t;
}

int test0(int val)
{
    Bar b;
    b.u = val;
    b.i.v = asint(2.0f);
    b.i.s = asuint(1.25);
    b.t = asuint(0.25);
    Foo f = bit_cast<Foo, Bar>(b);
    return f.a + (int)f.b + int(float(f.fvec.x / f.fvec.y)); // val + 2 + 5
}

struct Smaller
{
    int s;
}

struct Larger
{
    int x, y;
}

int test1()
{
    Smaller s = {1};
    int v0 = bit_cast<Larger, Smaller>(s).y; // 0.
    Larger l = {1, 2};
    int v1 = bit_cast<Smaller, Larger>(l).s; // 1.
    return v0 + v1;
}


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

[numthreads(4, 1, 1)]
void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
    uint tid = dispatchThreadID.x;
    int inVal = tid;
    int outVal = test0(inVal) + test1();
    outputBuffer[tid] = outVal;
}