"""feature_scan: stage census, SFI0 flags, output streams, MMA/GDS opcodes and code-base counts on a synthetic work folder. python -I tests/test_feature_scan.py Every input is built here: tiny DXBC containers with only the parts the scanner reads, three hand-written .s listings and a two-entry spirv/index.json. No game data. """ import contextlib import io import json import os import struct import subprocess import sys import tempfile import unittest from unittest import mock TOOLS = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "tools") sys.path.insert(0, TOOLS) import feature_scan as F # noqa: E402 import report_paths # noqa: E402 failures = 0 def check(cond, what): global failures print(("PASS " if cond else "FAIL ") + what) failures += not cond def signature(streams): """ISG1/OSG1 payload: count, first-element offset, 32-byte elements with the stream index first.""" body = struct.pack(" bytes 0, 6, 5 (three 0..7) and 12 (one 12) ("perm_imm_ps", "ps", "\tv_perm_b32 v0, v1, v2, 0x0c050600\n" "\ts_endpgm\n"), # Constant SGPR selector 0x0b0a0908 -> bytes 8, 9, 10, 11 (four 8..11 sign-extend) ("perm_sreg_cs", "cs", "\ts_mov_b32 s4, 0x0b0a0908\n" "\tv_perm_b32 v3, v1, v2, s4\n" "\ts_endpgm\n"), # Constant VGPR selector 0x0d0e0f08 -> bytes 8 (one 8..11) and 15, 14, 13 (three >= 13) ("perm_vreg_vs", "vs", "\tv_mov_b32_e32 v6, 0x0d0e0f08\n" "\tv_perm_b32 v7, v1, v2, v6\n" "\ts_endpgm\n"), # Clobbered SGPR and VGPR before v_perm_b32 -> both unresolved ("perm_clobber_ps", "ps", "\ts_mov_b32 s5, 0x0c050600\n" "\ts_add_u32 s5, s5, 1\n" "\tv_perm_b32 v4, v1, v2, s5\n" "\tv_mov_b32 v8, 0x0c050600\n" "\tv_add_nc_u32 v8, v8, v1\n" "\tv_perm_b32 v9, v1, v2, v8\n" "\ts_endpgm\n"), # The scalar carry is the second destination; wave64 writes both halves of its pair. ("perm_carry_w32", "cs", "\ts_mov_b32 s4, 0x08080808\n" "\tv_add_co_u32 v8, s4, v1, v2\n" "\tv_perm_b32 v9, v1, v2, s4\n" "\ts_endpgm\n"), ("perm_carry_w64", "vs", "\ts_mov_b32 s5, 0x08080808\n" "\tv_add_co_u32 v8, s[4:5], v1, v2\n" "\tv_perm_b32 v9, v1, v2, s5\n" "\ts_endpgm\n"), ] index = [] for name, stage, listing in synthetic: with open(os.path.join(dx, name + ".dxbc"), "wb") as f: f.write(dxbc([("SFI0", struct.pack("= 24 else b"") body += rec + bytes(size - len(rec)) return body[:-4] if truncate else body def listing(lines): """A .s listing with `; encoding:` comments of the right length, so pcs and branch targets are real.""" return "".join(f"\t{code} ; encoding: [{','.join(['0x00'] * n)}]\n" for code, n in lines) def test_typed_buffers_and_psv0(): """ยง8.2 #16: PSV0 counter/typed-buffer bindings, MTBUF formats and where each MUBUF V# word 3 comes from.""" with tempfile.TemporaryDirectory() as tmp: sh = os.path.join(tmp, "shaders") dx = os.path.join(sh, "dxbc") dis = os.path.join(dx, "disasm") os.makedirs(dis) cs = listing([ ("s_load_dwordx4 s[8:11], s[0:1], 0x0", 8), # pc 0 ("s_mov_b32 s15, 0x38000", 8), # 8: format 56, 8_8_8_8_UNORM ("s_mov_b32 s19, 0x24000", 8), # 16: format 36, 10_11_11_FLOAT ("s_or_b32 s23, s23, 0x1000", 8), # 24: computed ("s_cmp_eq_u32 s11, 0", 4), # 32: reads s11, writes nothing ("buffer_load_format_xyzw v[0:3], v4, s[8:11], 0 idxen", 8), # 36: memory ("buffer_load_format_x v5, v4, s[12:15], 0 idxen", 8), # 44: literal ("buffer_store_format_x v5, v4, s[16:19], 0 idxen", 8), # 52: literal, not in the table ("buffer_load_format_x v6, v4, s[20:23], 0 idxen", 8), # 60: computed ("buffer_load_format_x v7, v4, s[24:27], 0 idxen", 8), # 68: never written: shader input ("s_cbranch_scc0 1", 4), # 76: to 84; 80 and 84 lead blocks ("s_nop 0", 4), # 80 ("buffer_load_format_x v8, v4, s[8:11], 0 idxen", 8), # 84: loaded in an earlier block ("buffer_load_format_x v9, v4, s[12:15], 0 idxen", 8), # 92: literal from an earlier block ("tbuffer_load_format_xy v[0:1], v4, s[8:11], 0 format:[BUF_FMT_16_16_SNORM] idxen", 8), ("tbuffer_load_format_x v2, v4, s[8:11], 0 idxen", 8), # no format: GFX10 default 8_UNORM ("tbuffer_store_format_x v2, v4, s[8:11], 0 format:[BUF_FMT_10_11_11_FLOAT] idxen", 8), ("s_endpgm", 4)]) ps = listing([("s_mov_b32 s11, 0", 4), # word 3 0: FORMAT INVALID ("buffer_load_format_x v0, v1, s[0:3], 0 idxen", 8), ("buffer_load_format_x v2, v1, s[8:11], 0 idxen", 8), ("s_endpgm", 4)]) vs = listing([("v_mov_b32_e32 v0, v1", 4), ("s_endpgm", 4)]) synthetic = [ ("tb_cs", "cs", cs, psv0([(9, 12), (3, 10), (6, 2)])), # counter UAV, typed buffer SRV, typed 2D UAV ("tb_ps", "ps", ps, psv0([(6, 10), (6, 10)])), # two typed buffer UAVs ("tb_vs", "vs", vs, psv0([(3, 10)], size=16)), # v0 records carry no kind ("tb_gs", "gs", None, psv0([(9, 12)], truncate=True)), # records past the end of the part ] index = [] for name, stage, text, psv in synthetic: with open(os.path.join(dx, name + ".dxbc"), "wb") as f: f.write(dxbc([("SFI0", struct.pack("