"""A mesh shader's NGG primitive export and allocation, executed: `exp prim` and `s_sendmsg(MSG_GS_ALLOC_REQ)`. The program is fixtures/ms_prim_export.asm (hand-written, no game data): 72 threads in two wave64 waves, m0 = 68 primitives << 12 | 70 vertices, and every thread t exports vertex t at (t, 0, 0, 1) and primitive t with the indices (t, t + 1, t + 2), edge flags when t is odd and the null-primitive bit when t & 2. It is translated as a mesh shader (MeshEXT) and with --mesh-compute, and both modules are run by spirv_mini_interp.run_workgroup as one 72-invocation workgroup on a 64-wide subgroup. The rules, the GFX10 NGG formats (Mesa 22.3, src/amd/llvm/ac_llvm_build.c): * `exp prim` (ac_pack_prim_export, 4518-4539): bits 0..8 index 0, bit 9 edge flag 0, bits 10..18 index 1, bit 19 edge flag 1, bits 20..28 index 2, bit 29 edge flag 2, bit 31 "null primitive (skip)". So the edge flags are not part of an index, and a null primitive is not drawn: CullPrimitiveEXT is true for it. * GS_ALLOC_REQ (ac_build_sendmsg_gs_alloc_req, 4475-4477): m0 = prim_cnt << 12 | vtx_cnt, the subgroup's vertex and primitive counts. Thread t exports vertex t and primitive t; only threads below the vertex count have a vertex and only threads below the primitive count a primitive (Mesa ac_nir_lower_ngg.c, emit_ms_finale, 4056-4067). Expected: counts (70, 68); vertex t at (t, 0, 0, 1) for t < 70; primitive t = (t, t + 1, t + 2), or (0, 0, 0) and culled when t & 2, for t < 68; nothing for vertices 70, 71 and primitives 68..71. Vertices 64..69 and primitives 64..67 come from the second wave (s3[27:24] = 1). The null primitive's index fields are not kept. Every GFX10 mesh epilogue sets bit 31 when an index field is at or past the vertex count and leaves the fields as they are; Vulkan leaves a primitive index at or past the vertex count undefined even on a culled primitive (VK_EXT_mesh_shader: PrimitiveTriangleIndicesEXT), so a null primitive writes the indices (0, 0, 0) and every other index is clamped to the vertex count - 1, as Mesa's mesh lowering clamps them (Mesa 22.3 ac_nir_lower_ngg.c line 4112). With no vertex the primitive count is 0. MeshNullPrimitiveClamp checks this on fixtures/ms_prim_null_clamp.asm (128 threads, two wave64 waves) at several vertex counts. """ import os import struct import unittest import support import rdna2spirv import spirv_mini_interp as si FIXTURES = os.path.join(os.path.dirname(os.path.abspath(__file__)), "fixtures") FIXTURE = os.path.join(FIXTURES, "ms_prim_export.asm") THREADS = MAX_OUT = 72 VERTS, PRIMS = 70, 68 OUT = 0x10000 # --mesh-compute: the workgroup's slot, at the output address ONE = 0x3F800000 def _dxbc_psv0(threads, max_vertices, max_primitives): """A DXBC container with one PSV0 part: a triangle mesh shader (stage 13) and its limits.""" r = bytearray(48) struct.pack_into("= _clamp_counts(verts)[1]: return None if t & 3 in (1, 2): return (0, 0, 0), True return (t & 7, (t & 7) + 1, (t & 7) + 2), False class MeshNullPrimitiveClamp(unittest.TestCase): def test_mesh_shader_indices_in_range(self): for verts in CLAMP_VERTS: spv, _ = _clamp_translate(False, verts) _, outs = _run(spv, [0] * 36, CLAMP_THREADS) for t, out in enumerate(outs): with self.subTest(vertices=verts, invocation=t): self.assertEqual(out["OpSetMeshOutputsEXT"], _clamp_counts(verts)) idx, cull = out["%ms_prim_idx"][3 * t:3 * t + 3], out["%ms_cull"][t] prim = _clamp_primitive(t, verts) if prim: self.assertTrue(all(i < verts for i in idx), idx) self.assertEqual(idx, prim[0] if prim else (0, 0, 0)) self.assertEqual(cull, int(prim[1]) if prim else 0) def test_mesh_compute_indices_in_range(self): for verts in CLAMP_VERTS: spv, lay = _clamp_translate(True, verts) memory, _ = _run(spv, [0] * 36 + [OUT, 0], CLAMP_THREADS) self.assertEqual((memory.get(OUT), memory.get(OUT + 4)), _clamp_counts(verts)) for t in range(CLAMP_THREADS): with self.subTest(vertices=verts, thread=t): p = OUT + lay["primitives"] + t * lay["primitive_stride"] words = tuple(memory.get(p + 4 * k) for k in range(4)) prim = _clamp_primitive(t, verts) if prim: self.assertTrue(all(i < verts for i in words[:3]), words) self.assertEqual(words, prim[0] + (int(prim[1]),) if prim else (None,) * 4) def test_unemitted_primitive_slot_is_culled_with_zero_indices(self): # Defensive epilogue (FIX 9b-3): if an invocation with %mli < %msp never executes `exp prim` # (!%ms_has_prim), write CullPrimitiveEXT = true (1 in --mesh-compute) and indices (0, 0, 0) # instead of leaving the primitive slot uninitialized. # 64 threads allocate 4 vertices and 4 primitives, but only even threads (0, 2) run `exp prim`; # odd threads (1, 3) skip `exp prim` (!%ms_has_prim). lines = [ "s_mov_b64 exec, -1", "s_mov_b32 m0, 0x00004004", # 4 primitives << 12 | 4 vertices "s_sendmsg sendmsg(MSG_GS_ALLOC_REQ)", "v_mov_b32_e32 v1, 0", "v_mov_b32_e32 v2, 0x3f800000", "exp pos0 v1, v1, v1, v2 done", "v_mbcnt_lo_u32_b32 v0, -1, 0", "v_and_b32_e32 v3, 1, v0", "v_cmp_eq_u32_e64 exec, 0, v3", # only even threads execute `exp prim` "v_mov_b32_e32 v4, 0x00200400", # (0, 1, 2), non-null "exp prim v4, off, off, off done", "s_mov_b64 exec, -1", "s_endpgm", ] for mesh_compute in (False, True): with self.subTest(mesh_compute=mesh_compute): old = rdna2spirv.MESH_COMPUTE rdna2spirv.MESH_COMPUTE = mesh_compute try: spv = support.translate_lines("ms_unemitted_prim", lines, dxbc=_dxbc_psv0(64, 64, 64)) lay = rdna2spirv.translate.last_gen.msc_layout if mesh_compute else None finally: rdna2spirv.MESH_COMPUTE = old if not mesh_compute: _, outs = _run(spv, [0] * 36, 64) for t in range(4): idx = outs[t]["%ms_prim_idx"][3 * t:3 * t + 3] cull = outs[t]["%ms_cull"][t] if t % 2 == 0: self.assertEqual((idx, cull), ((0, 1, 2), 0)) else: self.assertEqual((idx, cull), ((0, 0, 0), 1)) else: memory, _ = _run(spv, [0] * 36 + [OUT, 0], 64) for t in range(4): p = OUT + lay["primitives"] + t * lay["primitive_stride"] words = tuple(memory.get(p + 4 * k) for k in range(4)) if t % 2 == 0: self.assertEqual(words, (0, 1, 2, 0)) else: self.assertEqual(words, (0, 0, 0, 1)) if __name__ == "__main__": unittest.main()