Files
mandy/tests/semantic/test_ms_lds_need.py
Jan Trencansky cd5ee11079 rdna2spirv(r9b): unconditional CS/MS LDS bounds, PS EarlyFragmentTests, mesh epilogue & PS quad swizzle
Round 9b translator fixes for rendering correctness and Mandy d3d12/ parity:

1. Unconditional CS & MS LDS OOB bounds (Mandy 21633b328 parity):
   - Record g.lds_declared from PSV0.shared for MS and RSRC2.LDS_SIZE for CS.
   - Whenever g.lds_declared > 0 (opt-out --no-lds-bounds), set
     g.lds_dwords = g.lds_bound = min(g.lds_declared, 16384) so every LDS
     read, write, sub-dword, wide, and atomic op goes through lds_access()
     (OOB reads return 0, OOB writes/atomics dropped instead of wrapping).

2. PS DB_SHADER_CONTROL bit 12 EarlyFragmentTests (Mandy f33940415/bf479a3cd):
   - Add fssm_header.db_shader_control() reading u32 at E + 0x138 (0x24C for
     rec = 0xB0; 3,023/12,101 corpus PS have bit 12 DEPTH_BEFORE_SHADER set).
   - Emit OpExecutionMode %main EarlyFragmentTests (opt-out --no-early-depth)
     and record early_depth in shader_meta when bit 12 is set and the PS does
     not export depth/stencil (no exp mrtz, DB_SHADER_CONTROL & 0x7 == 0, no
     OSG1 SV_Depth*/SV_StencilRef).

3. Defensive mesh epilogue & PS helper-lane quad swizzle:
   - MeshEXT and --mesh-compute epilogues explicitly write CullPrimitiveEXT =
     true (1) and (0, 0, 0) when %mli < %msp and !%ms_has_prim.
   - Lower quad-local ds_swizzle_b32 in PS via OpGroupNonUniformQuadBroadcast /
     OpGroupNonUniformQuadSwap (GroupNonUniformQuad capability) so Vulkan 2x2
     helper invocations participate on AMD RADV while EXEC-masked lanes still
     read 0.

Verified: compileall, check_no_game_data.py, 6,674/6,674 semantic tests (0
failures), 200/200 corpus slice (200/200 spirv-val), plus targeted LDS MS
(5/5), LDS CS (60/60), early-depth PS (120/120), and PS quad-swizzle (50/50)
corpus slices (100% spirv-val). AI-assisted.
2026-11-29 09:25:40 -05:00

73 lines
3.3 KiB
Python

"""SHADER-562: a mesh shader's LDS array covers its constant ds offsets with the access's own element size.
read2/write2 offset0/offset1 count elements: 4 bytes for _b32, 8 for _b64, x64 for st64 (`ds_const_extent`, as the
compute path does since SHADER-309). The mesh loop counted every element as 8 bytes, so a 32 KiB mesh shader whose
st64 b32 offsets stay inside its allocation still doubled its array to 64 KiB.
The programs and the PSV0 part are written here; no game data.
"""
import os
import struct
import sys
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
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 lds_dwords(op, data, off1, group_shared=32768):
prog = ["s_mov_b64 exec, -1",
"v_lshlrev_b32_e32 v1, 2, v0",
f"{op} v1, {data} offset1:{off1}",
"s_endpgm"]
support.translate_lines("ms_lds_need", prog, dxbc=mesh_dxbc(group_shared))
return rdna2spirv.translate.last_gen.lds_dwords
class MeshLdsNeedTests(unittest.TestCase):
def test_st64_b32_offsets_inside_the_allocation_keep_it(self):
# offset1:112 st64 b32 = 112 * 64 * 4 = 28,672 B, inside 32 KiB: the array is the allocation, 8,192 dwords
# (counting 8 bytes per element made it 14,340 dwords, so a 64 KiB array)
self.assertEqual(lds_dwords("ds_write2st64_b32", "v0, v0", 112), 8192)
def test_b32_offsets_count_dwords(self):
# offset1:255 b32 ends at byte 1,023 + 1: 256 dwords, so a small allocation stays small (16 dwords minimum)
self.assertEqual(lds_dwords("ds_write2_b32", "v0, v0", 255, group_shared=0), 256)
def test_b64_offsets_count_qwords(self):
# offset1:112 st64 b64 = 112 * 64 * 8 = 57,344 B + 8: need is 14,338 dwords (past 32 KiB).
# With LDS_BOUNDS=True (default), a declared 32 KiB allocation stays at 8,192 dwords and bounds-checks;
# with --no-lds-bounds (LDS_BOUNDS=False), the array grows to 64 KiB.
self.assertEqual(lds_dwords("ds_write2st64_b64", "v[2:3], v[2:3]", 112), 8192)
self.assertEqual(rdna2spirv.translate.last_gen.lds_bound, 8192)
self.assertEqual(rdna2spirv.translate.last_gen.lds_need, 14338)
prev = rdna2spirv.LDS_BOUNDS
rdna2spirv.LDS_BOUNDS = False
try:
self.assertEqual(lds_dwords("ds_write2st64_b64", "v[2:3], v[2:3]", 112), 16384)
finally:
rdna2spirv.LDS_BOUNDS = prev
# offset1:255 b64 ends at byte 2,047 + 1: 512 dwords (group_shared=0 keeps the offset-driven size)
self.assertEqual(lds_dwords("ds_write2_b64", "v[2:3], v[2:3]", 255, group_shared=0), 512)
if __name__ == "__main__":
unittest.main()