summaryrefslogtreecommitdiffstats
path: root/tests/bugs/gh-9918.slang
blob: 3f1eced4d7acf0fcdede8b2babc8a6bae2c69f17 (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
85
86
87
88
89
90
91
92
93
94
95
//TEST:SIMPLE(filecheck=CHECK_SPIRV): -target spirv -entry entry_mesh
//TEST:SIMPLE(filecheck=CHECK_GLSL): -target glsl -entry entry_mesh
//TEST:SIMPLE(filecheck=CHECK_HLSL): -target hlsl -entry entry_mesh

//CHECK_SPIRV: OpEntryPoint
//CHECK_GLSL: void main()
//CHECK_HLSL: void entry_mesh


const static float2 positions[3] = {
    float2(0.0, -0.5),
    float2(0.5, 0.5),
    float2(-0.5, 0.5)
};

interface VertexI
{
    [mutating]
    func set_pos(float4 v);
}

interface PrimitiveI
{
    [mutating]
    func set_tint(float3 v);
}

struct Vertex : VertexI
{
    float4 pos : SV_Position;
    [mutating]
    func set_pos(float4 v) { pos = v; }
};

struct Primitive : PrimitiveI
{
    [[vk::location(0)]] nointerpolation float3 tint;
    [mutating]
    func set_tint(float3 v) { tint = v; }
}

const static uint MAX_VERTS = 3;
const static uint MAX_PRIMS = 1;

void mesh_fn<V : VertexI, P : PrimitiveI>(
    in uint tig,
    OutputIndices<uint3, MAX_PRIMS> triangles,
    OutputVertices<V, MAX_VERTS> verts,
    OutputPrimitives<P, MAX_PRIMS> primitives)
{
    const uint numVertices = 3;
    const uint numPrimitives = 1;
    SetMeshOutputCounts(numVertices, numPrimitives);

    if (tig < numVertices) {
        V v;
        if (V is Vertex)
        {
            Vertex v2 = reinterpret<Vertex>(v);
            v2.pos = float4(positions[tig], 0, 1);
            v = reinterpret<V>(v2);
        }
        verts[tig] = v;
    }

    if (tig < numPrimitives) {
        triangles[tig] = uint3(0, 1, 2);
        primitives[tig].set_tint(float3(1, 0, 0));
    }
}

[outputtopology("triangle")]
[numthreads(3, 1, 1)]
[shader("mesh")]
void entry_mesh(
    in uint tig: SV_GroupIndex,
    OutputIndices<uint3, MAX_PRIMS> triangles,
    OutputVertices<Vertex, MAX_VERTS> verts,
    OutputPrimitives<Primitive, MAX_PRIMS> primitives)
{
    mesh_fn(tig, triangles, verts, primitives);
}

struct FragmentOut
{
    [[vk::location(0)]] float3 color;
};

[shader("fragment")]
FragmentOut entry_fragment(in Vertex vertex, in Primitive prim)
{
    FragmentOut frag_out;
    frag_out.color = prim.tint;
    return frag_out;
}