yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

16-Bit-DogSupport use of `this` with Mesh Shader Outputs (`IRMeshOutputRef`) (#6920)b02771f23

master
2.1 KiB95 linesraw
1//TEST:SIMPLE(filecheck=CHECK_SPIRV): -target spirv -entry entry_mesh
2//TEST:SIMPLE(filecheck=CHECK_GLSL): -target glsl -entry entry_mesh
3//TEST:SIMPLE(filecheck=CHECK_HLSL): -target hlsl -entry entry_mesh
4
5//CHECK_SPIRV: OpEntryPoint
6//CHECK_GLSL: void main()
7//CHECK_HLSL: void entry_mesh
8
9
10const static float2 positions[3] = {
11    float2(0.0, -0.5),
12    float2(0.5, 0.5),
13    float2(-0.5, 0.5)
14};
15
16interface VertexI
17{
18    [mutating]
19    func set_pos(float4 v);
20}
21
22interface PrimitiveI
23{
24    [mutating]
25    func set_tint(float3 v);
26}
27
28struct Vertex : VertexI
29{
30    float4 pos : SV_Position;
31    [mutating]
32    func set_pos(float4 v) { pos = v; }
33};
34
35struct Primitive : PrimitiveI
36{
37    [[vk::location(0)]] nointerpolation float3 tint;
38    [mutating]
39    func set_tint(float3 v) { tint = v; }
40}
41
42const static uint MAX_VERTS = 3;
43const static uint MAX_PRIMS = 1;
44
45void mesh_fn<V : VertexI, P : PrimitiveI>(
46    in uint tig,
47    OutputIndices<uint3, MAX_PRIMS> triangles,
48    OutputVertices<V, MAX_VERTS> verts,
49    OutputPrimitives<P, MAX_PRIMS> primitives)
50{
51    const uint numVertices = 3;
52    const uint numPrimitives = 1;
53    SetMeshOutputCounts(numVertices, numPrimitives);
54
55    if (tig < numVertices) {
56        V v;
57        if (V is Vertex)
58        {
59            Vertex v2 = reinterpret<Vertex>(v);
60            v2.pos = float4(positions[tig], 0, 1);
61            v = reinterpret<V>(v2);
62        }
63        verts[tig] = v;
64    }
65
66    if (tig < numPrimitives) {
67        triangles[tig] = uint3(0, 1, 2);
68        primitives[tig].set_tint(float3(1, 0, 0));
69    }
70}
71
72[outputtopology("triangle")]
73[numthreads(3, 1, 1)]
74[shader("mesh")]
75void entry_mesh(
76    in uint tig: SV_GroupIndex,
77    OutputIndices<uint3, MAX_PRIMS> triangles,
78    OutputVertices<Vertex, MAX_VERTS> verts,
79    OutputPrimitives<Primitive, MAX_PRIMS> primitives)
80{
81    mesh_fn(tig, triangles, verts, primitives);
82}
83
84struct FragmentOut
85{
86    [[vk::location(0)]] float3 color;
87};
88
89[shader("fragment")]
90FragmentOut entry_fragment(in Vertex vertex, in Primitive prim)
91{
92    FragmentOut frag_out;
93    frag_out.color = prim.tint;
94    return frag_out;
95}