"""Value tests for RDNA2 8/16-bit MUBUF loads and stores, run through spirv_mini_interp on the CPU. buffer_load_{u,s}byte / {u,s}short zero- or sign-extend to 32 bits; the _d16 loads write one 16-bit half of vdata (the _hi forms the high half; ubyte / sbyte extended to 16 bits) and keep the other; buffer_store_byte / short write the low 8 / 16 bits of vdata (the _d16_hi forms its high half) and leave the neighbouring bytes alone. The V# is raw (OOB_SELECT 3): an access is in range when offset + its size <= num_records; out of range a load gives 0 and a store writes nothing. Every byte offset in a dword is tried, so a 16-bit access at byte 3 crosses into the next dword. Standard library only; no GPU, no game files. """ import os import struct import sys import tempfile import unittest import warnings HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.dirname(os.path.dirname(HERE)) sys.dont_write_bytecode = True sys.path.insert(0, os.environ.get("LEONIDA_TOOLS_DIR") or os.path.join(ROOT, "tools")) sys.path.insert(0, HERE) import rdna2spirv # noqa: E402 import spirv_mini_interp as si # noqa: E402 M32 = 0xFFFFFFFF STUB_DXBC = b"DXBC" + bytes(24) + struct.pack(" RECORDS: v = 0 else: v = int.from_bytes(buf[byte:byte + n], "little") if signed and v >> (8 * n - 1): v -= 1 << (8 * n) if half is None: return v & M32 return (KEEP & ~(0xFFFF << (16 * half)) | (v & 0xFFFF) << (16 * half)) & M32 def load(op, byte): body = [f"v_mov_b32_e32 v1, {BASE + byte:#x}", f"v_mov_b32_e32 v5, {KEEP:#x}", f"{op} v5, v1, s[8:11], 0 offen", "s_waitcnt vmcnt(0)", f"v_mov_b32_e32 v6, {OUT:#x}", "buffer_store_dword v5, v6, s[0:3], 0 offen"] return run(body)[OUT] class BufferSubDword(unittest.TestCase): def test_stores_keep_neighbouring_bytes(self): data = 0xF1E2D3C4 for op, (n, shift) in STORES.items(): for byte in range(0, 8): # byte 3 and 7: a short crosses into the next dword with self.subTest(op=op, byte=byte): mem = run([f"v_mov_b32_e32 v1, {BASE + byte:#x}", f"v_mov_b32_e32 v2, {data:#x}", f"{op} v2, v1, s[8:11], 0 offen"]) want = seed_bytes() want[byte:byte + n] = ((data >> shift) & ((1 << 8 * n) - 1)).to_bytes(n, "little") self.assertEqual([mem[BASE + 4 * k] for k in range(3)], words(want)) def test_loads(self): for op in LOADS: for byte in range(0, 8): with self.subTest(op=op, byte=byte): self.assertEqual(load(op, byte), load_ref(op, byte)) def test_bounds_cover_the_access_size(self): # num_records = BASE + 12: the last byte is in range for a byte load, a short at BASE + 11 is not. for op, byte in (("buffer_load_ubyte", 11), ("buffer_load_ushort", 10), ("buffer_load_ushort", 11), ("buffer_load_sbyte_d16_hi", 12), ("buffer_load_short_d16", 11)): with self.subTest(op=op, byte=byte): self.assertEqual(load(op, byte), load_ref(op, byte)) # a store out of range writes nothing mem = run([f"v_mov_b32_e32 v1, {BASE + 11:#x}", "v_mov_b32_e32 v2, 0x7777", "buffer_store_short v2, v1, s[8:11], 0 offen"]) self.assertEqual([mem[BASE + 4 * k] for k in range(3)], SEED) def test_inactive_lane_does_nothing(self): body = ["v_cmpx_eq_u32_e32 1, v0", f"v_mov_b32_e32 v1, {BASE + 1:#x}", "v_add_nc_u32_e32 v2, 0x70, v0", "buffer_store_byte v2, v1, s[8:11], 0 offen"] mem = run(body, invocations=2) self.assertEqual(mem[BASE], (SEED[0] & ~0xFF00) | 0x71 << 8) def test_rejected_forms(self): for line in ("buffer_load_ubyte v[4:5], v1, s[8:11], 0 offen", "buffer_store_short v1, v2, s[8:11], 0 offen lds"): with self.subTest(line=line): with self.assertRaises(rdna2spirv.Unsupported): translate(["v_mov_b32_e32 v1, 0", line]) def test_first_read_analysis_counts_the_kept_half(self): from shader_ir import parse_line self.assertEqual(rdna2spirv.read_before_write_vgprs( [parse_line("\tbuffer_load_short_d16_hi v5, v1, s[8:11], 0 offen")]), {1, 5}) self.assertEqual(rdna2spirv.read_before_write_vgprs( [parse_line("\tbuffer_load_ushort v5, v1, s[8:11], 0 offen")]), {1}) if __name__ == "__main__": unittest.main()