summaryrefslogtreecommitdiffstats
path: root/tests/metal/simple-mesh.slang
blob: dc149f71fd0dcd956d5ca8b62f4b0f9f5863ecb3 (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
//TEST:SIMPLE(filecheck=METAL): -entry meshMain -stage mesh -target metal
//TEST:SIMPLE(filecheck=METALLIB): -entry meshMain -stage mesh -target metallib

// METALLIB-NOT: error :
// METALLIB: @meshMain

//
// Mesh shader
//

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

const static float3 colors[3] = {
    float3(1.0, 1.0, 0.0),
    float3(0.0, 1.0, 1.0),
    float3(1.0, 0.0, 1.0)
};

struct MeshPayload
{
    int exponent;
};


struct Vertex
{
    float4 pos : SV_Position;
    float3 color : Color;
    int index : Index;
    int value : Value;
};

struct Primitive
{
    uint prim : SV_PrimitiveID;
};

const static uint MAX_VERTS = 12;
const static uint MAX_PRIMS = 4;

[outputtopology("triangle")]
[shader("mesh")]
[numthreads(12, 1, 1)]
void meshMain(
    in uint tig: SV_GroupIndex,
    in payload MeshPayload meshPayload,
    // METAL: const MeshPayload_0 object_data* meshPayload_0
    OutputVertices<Vertex, MAX_VERTS> verts,
    OutputIndices<uint3, MAX_PRIMS> triangles,
    OutputPrimitives<Primitive, MAX_PRIMS> primitives
    )
{
    const uint numVertices = 12;
    const uint numPrimitives = 4;
    SetMeshOutputCounts(numVertices, numPrimitives);

    if (tig < numVertices)
    {
        const int tri = tig / 3;
        verts[tig] = { float4(positions[tig % 3], 0, 1), colors[tig % 3], tri, int(pow(tri, meshPayload.exponent)) };
    }

    if (tig < numPrimitives)
    {
        // METAL: _slang_mesh.set_index({{.*}}+0,{{.*}}[0]);
        // METAL: _slang_mesh.set_index({{.*}}+1,{{.*}}[1]);
        // METAL: _slang_mesh.set_index({{.*}}+2,{{.*}}[2]);
        triangles[tig] = tig * 3 + uint3(0, 1, 2);
        // METAL: _slang_mesh.set_primitive({{.*}}
        primitives[tig] = { tig };
    }
}