Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2622,7 +2622,9 @@ Pipeline creation:
- The index's `max_vertices`/`max_primitives` already size the SPIR-V mesh output arrays. Vulkan has no
per-pipeline mesh output limit: check them against
`VkPhysicalDeviceMeshShaderPropertiesEXT::maxMeshOutputVertices`/`maxMeshOutputPrimitives` and fail at pipeline
creation if the device is smaller.
creation if the device is smaller. Workgroup memory (`lds_bytes`) is checked against
`maxMeshSharedMemorySize` (and `maxMeshPayloadAndSharedMemorySize`) by `vulkan_mesh_limits.h`; pass the device's
value to `rdna2spirv.py --max-mesh-shared-memory` when translating for it (default 0: no check at translation; 28672 is the spec minimum).
- Vertex input comes from the PSO input layout plus the shader's ISG1 signature. The translator's rule is
**location = D3D input register**, not the semantic's HLSL slot. Match the layout element's semantic name/index
to the ISG1 element to get the register, then create a `VkVertexInputAttributeDescription` with that location
Expand Down
2 changes: 1 addition & 1 deletion docs/design.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions tests/semantic/fixtures/ms_shared_limit.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
; Mesh shader (NGG, wave64), PSV0: 128 threads, 64/64 outputs, triangles; the group-shared size is the DXBC's.
; One ds_write_b32 gives the shader an LDS array; the workgroup-memory limit tests size it through PSV0.
s_mov_b64 exec, -1
v_lshlrev_b32_e32 v1, 2, v0
ds_write_b32 v1, v0
s_endpgm
189 changes: 189 additions & 0 deletions tests/semantic/test_ms_shared_memory_limit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
"""A mesh shader's Workgroup memory fits in maxMeshSharedMemorySize (SHADER-MESH-SHARED-LIMIT).

`rdna2spirv.MAX_MESH_SHARED_BYTES` (`--max-mesh-shared-memory`, default 0: no check;
28672 is the VK_EXT_mesh_shader minimum `maxMeshSharedMemorySize`) caps what a MeshEXT module declares in the Workgroup storage class: the LDS array
(PSV0's shared size grown to the furthest ds offset, a power of two), the 4-byte `%ms_counts` and, under
`--wave64-emulate`, `%x_buf` — the same total `lds_bytes` records for the runtime's device-limit check
(vk_pipeline_check / mesh_limit_refusal). Over the cap the shader is refused (`Unsupported`, with the byte
counts). A `--mesh-compute` module is GLCompute and runs as a compute shader, so it keeps the compute cap
(`MAX_SHARED_BYTES`, `--max-shared-memory`). There is no allocation to shrink to: the array is the program's need.

The program is fixtures/ms_shared_limit.asm (hand-written, no game data) and the PSV0 part is written here; the
Workgroup bytes are counted from the emitted SPIR-V. The last test runs the tool's CLI end to end.
"""
import json
import os
import struct
import subprocess
import sys
import tempfile
import unittest

HERE = os.path.dirname(os.path.abspath(__file__))
if HERE not in sys.path:
sys.path.insert(0, HERE) # python -I leaves the script's folder off sys.path

import support # noqa: E402 (support puts tools/ on sys.path, honouring LEONIDA_TOOLS_DIR)

rdna2spirv = support.rdna2spirv
FIXTURE = os.path.join(HERE, "fixtures", "ms_shared_limit.asm")
TOOL = os.path.join(support.ROOT, "tools", "rdna2spirv.py")


def mesh_dxbc(group_shared, threads=128):
"""DXBC with one PSV0 part: the fields mesh_psv() reads (stage 13, 64/64 triangles, NumThreads, shared bytes)."""
r = bytearray(48)
struct.pack_into("<I", r, 0, group_shared)
struct.pack_into("<HH", r, 12, 64, 64)
r[24], r[27] = 13, 2
struct.pack_into("<III", r, 36, threads, 1, 1)
part = b"PSV0" + struct.pack("<I", 4 + len(r)) + struct.pack("<I", len(r)) + bytes(r)
head = 32 + 4
return b"DXBC" + bytes(16) + struct.pack("<IIII", 1, head + len(part), 1, head) + part


def fixture_lines():
with open(FIXTURE, encoding="utf-8") as f:
return [ln.split(";")[0].strip() for ln in f if ln.split(";")[0].strip()]


def translate(group_shared=0, emulate=False, mesh_compute=False, limit=None):
prev = rdna2spirv.WAVE64_EMULATE, rdna2spirv.MESH_COMPUTE, rdna2spirv.MAX_MESH_SHARED_BYTES
rdna2spirv.WAVE64_EMULATE, rdna2spirv.MESH_COMPUTE = emulate, mesh_compute
if limit is not None:
rdna2spirv.MAX_MESH_SHARED_BYTES = limit
try:
spv = support.translate_lines("ms_shared_limit", fixture_lines(), dxbc=mesh_dxbc(group_shared))
finally:
rdna2spirv.WAVE64_EMULATE, rdna2spirv.MESH_COMPUTE, rdna2spirv.MAX_MESH_SHARED_BYTES = prev
return spv


def workgroup_bytes(spv):
"""{variable: bytes} of every Workgroup-storage-class OpVariable, sized from its pointee type. Only the
Workgroup pointees are sized: a mesh module's Output arrays (vectors, structs) need no size here."""
consts, defs, ptrs, out = {}, {}, {}, {}
for ln in spv.splitlines():
t = ln.split()
if len(t) < 3 or t[1] != "=":
continue
rid, op, args = t[0], t[2], t[3:]
if op == "OpConstant":
consts[rid] = int(args[1], 0)
elif op in ("OpTypeInt", "OpTypeFloat", "OpTypeVector", "OpTypeBool", "OpTypeArray", "OpTypeStruct",
"OpTypeRuntimeArray"):
defs[rid] = (op, args)
elif op == "OpTypePointer" and args[0] == "Workgroup":
ptrs[rid] = args[1]
elif op == "OpVariable" and args[1:2] == ["Workgroup"]:
out[rid] = ptrs[args[0]]

def size(t):
if t in sizes:
return sizes[t]
op, args = defs[t]
if op in ("OpTypeInt", "OpTypeFloat"):
s = int(args[0]) // 8
elif op == "OpTypeVector":
s = int(args[1]) * size(args[0])
elif op == "OpTypeBool":
s = 4
elif op == "OpTypeArray":
s = size(args[0]) * consts[args[1]]
elif op == "OpTypeStruct":
s = sum(size(a) for a in args)
else: # OpTypeRuntimeArray and anything unsized
s = 0
sizes[t] = s
return s

sizes = {}
return {name: size(t) for name, t in out.items()}


class MeshSharedMemoryLimitTests(unittest.TestCase):
def test_under_limit_translates(self):
# 16 KiB of group-shared + the 4-byte %ms_counts: under every cap, translated as MeshEXT
spv = translate(16384)
self.assertIn("OpEntryPoint MeshEXT", spv)
self.assertEqual(workgroup_bytes(spv), {"%lds": 16384, "%ms_counts": 4})
meta = rdna2spirv.shader_meta(rdna2spirv.translate.last_gen, spv)
self.assertEqual(meta["lds_bytes"], 16388)

def test_over_limit_is_refused_with_the_byte_counts(self):
# 28,672 B of group-shared rounds the array up to 32,768 B (a power of two), + 4 = 32,772 B
# against the VK_EXT_mesh_shader minimum, 28672
with self.assertRaises(rdna2spirv.Unsupported) as cm:
translate(28672, limit=28672)
msg = str(cm.exception)
self.assertIn("MS workgroup memory 32772 B", msg)
self.assertIn("LDS 32768 B", msg)
self.assertIn("%ms_counts 4 B", msg)
self.assertIn("maxMeshSharedMemorySize 28672 B", msg)

def test_default_checks_nothing(self):
# Without --max-mesh-shared-memory nothing a mesh shader translated to before is refused: a full 64 KiB LDS
# array plus the 4-byte %ms_counts (65,540 B) still translates, and lds_bytes carries it to the runtime
spv = translate(65536)
self.assertEqual(sum(workgroup_bytes(spv).values()), 65540)
self.assertEqual(rdna2spirv.shader_meta(rdna2spirv.translate.last_gen, spv)["lds_bytes"], 65540)

def test_limit_is_the_whole_workgroup_total(self):
# The boundary is the module's whole Workgroup total: exactly it passes, one byte under refuses
spv = translate(28672, limit=32772)
self.assertEqual(sum(workgroup_bytes(spv).values()), 32772)
with self.assertRaises(rdna2spirv.Unsupported):
translate(28672, limit=32771)

def test_wave64_exchange_counts_too(self):
# --wave64-emulate's %x_buf (128 threads = 2 waves: 2L + 6W + 7 dwords = 1,100 B) is Workgroup memory too
with self.assertRaises(rdna2spirv.Unsupported) as cm:
translate(28672, emulate=True, limit=28672)
msg = str(cm.exception)
self.assertIn("MS workgroup memory 33872 B", msg)
self.assertIn("wave64 exchange 1100 B", msg)
spv = translate(8192, emulate=True)
self.assertEqual(workgroup_bytes(spv), {"%lds": 8192, "%ms_counts": 4, "%x_buf": 1100})

def test_mesh_compute_uses_the_compute_cap(self):
# --mesh-compute emits GLCompute and runs as a compute shader: 32,772 B is over the mesh minimum but
# under the compute cap, so it translates; over the compute cap it is refused as compute
spv = translate(28672, mesh_compute=True)
self.assertIn("OpEntryPoint GLCompute", spv)
with self.assertRaises(rdna2spirv.Unsupported) as cm:
translate(65536, mesh_compute=True)
self.assertIn("maxComputeSharedMemorySize", str(cm.exception))

def test_cli_max_mesh_shared_memory(self):
# The flag end to end: the default accepts the 32,772 B module and records the whole total as lds_bytes;
# --max-mesh-shared-memory 28672 (the VK_EXT_mesh_shader minimum) refuses it
with tempfile.TemporaryDirectory() as tmp:
dis = os.path.join(tmp, "disasm")
os.mkdir(dis)
with open(os.path.join(dis, "ms_limit.s"), "w", encoding="utf-8", newline="\n") as f:
f.write("; ms_limit hand-written test program\n")
for ln in fixture_lines():
f.write("\t" + ln + "\n")
with open(os.path.join(tmp, "ms_limit.dxbc"), "wb") as f:
f.write(mesh_dxbc(28672))

def run(*extra):
out = os.path.join(tmp, "out" + str(len(extra)))
cmd = [sys.executable, "-I", TOOL, os.path.join(dis, "ms_limit.s"), "--out", out,
"--assembler", "python", "--no-validate"] + list(extra)
return subprocess.run(cmd, capture_output=True, text=True), out

r, out = run("--max-mesh-shared-memory", "28672")
self.assertEqual(r.returncode, 0, r.stderr)
self.assertIn("over maxMeshSharedMemorySize 28672 B", r.stdout)
self.assertFalse(os.path.exists(os.path.join(out, "ms_limit.spv")))

r, out = run()
self.assertEqual(r.returncode, 0, r.stderr)
self.assertTrue(os.path.exists(os.path.join(out, "ms_limit.spv")))
with open(os.path.join(out, "index.json"), encoding="utf-8") as f:
self.assertEqual(json.load(f)["ms_limit"]["lds_bytes"], 32772)


if __name__ == "__main__":
unittest.main()
33 changes: 31 additions & 2 deletions tools/rdna2spirv.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@
# Vulkan minimum is 16384. The translator runs offline, so it records the exact total as lds_bytes in index.json and
# the runtime compares that with the device's limit before it creates the pipeline. spirv-val checks neither.
MAX_SHARED_BYTES = 65536
# --max-mesh-shared-memory: the same cap for a mesh shader's Workgroup memory (the LDS array, %ms_counts and
# %x_buf), against maxMeshSharedMemorySize. 0 (the default) checks nothing, so every mesh shader that translates
# today still does: a 64 KiB LDS array plus the 4-byte %ms_counts is already over 64 KiB, and the runtime checks
# lds_bytes against the device's own limit before it creates the pipeline. Pass a device's maxMeshSharedMemorySize
# (28672 is the VK_EXT_mesh_shader minimum) to refuse at translation, with the byte counts, what that device can't
# run. A --mesh-compute module is GLCompute and keeps the compute cap above.
MAX_MESH_SHARED_BYTES = 0
WAVE64_GRAPHICS = "warn" # --wave64-graphics: tier C wave64 VS/PS/GS (not exact on 32-wide subgroups): warn | error | off
W64G_COUNTS = Counter() # tiers of the wave64 VS/PS/GS translated so far, for the summary line
# --bary-fallback: raw barycentric reads come from an ordinary perspective-interpolated vec3 input (the location is
Expand Down Expand Up @@ -16684,6 +16691,20 @@ def assemble(gen):
raise Unsupported(f"CS workgroup memory {wg} B (LDS {lds_b} B + "
f"{'wave64 exchange' if gen.emulate else 'barrier rounds'} {gen.x_buf_bytes()} B) "
f"over maxComputeSharedMemorySize {MAX_SHARED_BYTES} B")
if ms:
# A mesh shader's Workgroup memory is its LDS array (PSV0's shared size grown to the furthest ds offset,
# a power of two), the 4-byte %ms_counts and, under --wave64-emulate, %x_buf: the same total lds_bytes
# records. A MeshEXT module answers to maxMeshSharedMemorySize (Vulkan minimum 28672,
# VK_EXT_mesh_shader); --mesh-compute emits GLCompute and runs as a compute shader, so it keeps the
# compute cap. There is no LDS allocation to shrink to: the array is already the program's need.
lds_b = 4 * gen.lds_dwords if gen.uses_lds else 0
wg = lds_b + gen.x_buf_bytes() + 4
limit, limit_name = ((MAX_SHARED_BYTES, "maxComputeSharedMemorySize") if msc else
(MAX_MESH_SHARED_BYTES, "maxMeshSharedMemorySize"))
if limit and wg > limit:
raise Unsupported(f"MS workgroup memory {wg} B (LDS {lds_b} B"
+ (f" + wave64 exchange {gen.x_buf_bytes()} B" if gen.emulate else "")
+ f" + %ms_counts 4 B) over {limit_name} {limit} B")
if gen.frag_inputs or bary:
dec += ["OpDecorate %frag_coord BuiltIn FragCoord"]
gvars += ["%frag_coord = OpVariable %_ptr_Input_v4 Input"]
Expand Down Expand Up @@ -19735,7 +19756,8 @@ def shader_meta(g, asm, src_path=None, fssm_path=None):
m["gs_input_locations"] = sorted(r for r, sv in g.gs["in"] if sv != 1)
m["gs_output_locations"] = sorted(r for r, sv in g.gs["out"] if sv != 1)
# Every Workgroup byte of a compute shader (LDS + %x_buf of the emulation or the barrier rounds; SHADER-309): the
# runtime compares it with the device's maxComputeSharedMemorySize. A mesh shader also has its 4-byte %ms_counts.
# runtime compares it with the device's maxComputeSharedMemorySize, and a mesh shader's (LDS + %ms_counts + %x_buf)
# with its maxMeshSharedMemorySize, both capped at translation by --max-shared-memory / --max-mesh-shared-memory.
lds = (4 * g.lds_dwords if g.uses_lds else 0) + (g.x_buf_bytes() if g.cs or g.emulate else 0) + (4 if g.ms else 0)
if lds:
m["lds_bytes"] = lds
Expand Down Expand Up @@ -20071,7 +20093,7 @@ def _out_inside_input(src, out):


def main():
global MAX_SHARED_BYTES, MAX_STRUCTURED_DEPTH, LOOP_LIMIT, DESCRIPTOR_HASH, DESCRIPTOR_TABLE
global MAX_SHARED_BYTES, MAX_MESH_SHARED_BYTES, MAX_STRUCTURED_DEPTH, LOOP_LIMIT, DESCRIPTOR_HASH, DESCRIPTOR_TABLE
ap = argparse.ArgumentParser()
ap.add_argument("src")
ap.add_argument("--out", required=True)
Expand Down Expand Up @@ -20133,6 +20155,12 @@ def main():
"(default %(default)s, the 64 KiB hard cap): over it the LDS array shrinks to the program's "
"need, or the shader is refused (Unsupported). The device's own maxComputeSharedMemorySize "
"(32768 on some drivers) is checked by the runtime against lds_bytes in index.json")
ap.add_argument("--max-mesh-shared-memory", type=int, default=MAX_MESH_SHARED_BYTES, metavar="BYTES",
help="cap on a mesh shader's workgroup memory, its LDS plus %%ms_counts and what "
"--wave64-emulate adds (default 0: no check; 28672 is the VK_EXT_mesh_shader minimum "
"maxMeshSharedMemorySize): over it the shader is refused (Unsupported). The device's own "
"maxMeshSharedMemorySize is checked by the runtime against lds_bytes in index.json; a "
"--mesh-compute module is compute and uses --max-shared-memory")
ap.add_argument("--pc-tail-ubo", action="store_true",
help="keep only s0..s31 (128 bytes) in push constants and read the heap base, code base and "
"--mesh-compute output address from a uniform block at set 1, binding 0 (metadata: pc_tail), "
Expand Down Expand Up @@ -20175,6 +20203,7 @@ def main():
if ancestor and not os.path.isdir(ancestor):
ap.error(f"cannot create {a.out}: {ancestor} is a file, not a folder")
MAX_SHARED_BYTES = a.max_shared_memory
MAX_MESH_SHARED_BYTES = a.max_mesh_shared_memory
DESCRIPTOR_HASH = a.descriptor_hash
DESCRIPTOR_TABLE = a.descriptor_table
LOOP_LIMIT = a.loop_limit
Expand Down
Loading