"""Narrow integer typed-buffer formats (8/16-bit UINT and SINT) in tools/rdna2spirv.py, against words measured natively. Every expected word below was read back from an AMD Radeon RDNA2 iGPU (gfx1036, Ryzen 9 9950X3D, Windows AMD driver, through tools/hw_oracle with AMD clang 23 from rocm-sdk-core 10.0.0), in wave32 and wave64 alike, with the same V# and instruction the translated shader gets here: - loads: `buffer_load_format_xyzw` and `_d16_xyzw` from four source dwords (SOURCE) with DST_SEL XYZW and X011. UINT zero-extends and SINT sign-extends each channel; missing channels repeat the format's own (c mod n); DST_SEL 1 is integer 1; d16 packs each result as a 16-bit integer. - stores: `buffer_store_format_x/xy/xyzw` of four value sets into a buffer filled with 0xDEAD0000, with the V# selectors the runtime gives each format (gfx10_descriptors.h: X001, XY01, XYZW). UINT saturates to the channel (unsigned), SINT clamps the signed 32-bit value to [-2^(w-1), 2^(w-1) - 1]; a 1-2 byte element keeps the rest of its dword. `buffer_store_format_d16_xyzw` (D16_STORES) reads each 16-bit half as a signed integer for SINT and clamps it the same way, so 0xff80 stores -128 into 8_SINT. Formats 27, 60 and 69 (16_16_UINT, 8_8_8_8_UINT, 16_16_16_16_UINT) were decoded before; they are the control. """ from __future__ import annotations 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 STUB_DXBC = b"DXBC" + bytes(24) + struct.pack(" tuple: """Translate a pixel shader whose V# (s[0:3], raw, 256 bytes) points at BASE with this format and selectors.""" with tempfile.TemporaryDirectory() as tmp: os.mkdir(os.path.join(tmp, "disasm")) src = os.path.join(tmp, "disasm", "ps_narrow_int.s") with open(os.path.join(tmp, "ps_narrow_int.dxbc"), "wb") as f: f.write(STUB_DXBC) with open(src, "w", encoding="utf-8") as f: f.write("; test_buffer_format_narrow_int\n" f"\ts_mov_b32 s0, {BASE:#x}\n\ts_mov_b32 s1, 0\n\ts_mov_b32 s2, 0x100\n" f"\ts_mov_b32 s3, {fmt << 12 | dst_sel | 1 << 24 | 3 << 28:#x}\n\tv_mov_b32 v1, 0\n" + body) with warnings.catch_warnings(): warnings.simplefilter("ignore", ResourceWarning) spv = rdna2spirv.translate(src) return si.run_fragment(spv, memory=memory)["%out_mrt0"] def load(fmt: int, dst_sel: int, d16: bool = False) -> tuple: regs, dst = ("v[2:3]", "v2, v3, v2, v3") if d16 else ("v[2:5]", "v2, v3, v4, v5") body = (f"\tbuffer_load_format_{'d16_' if d16 else ''}xyzw {regs}, v1, s[0:3], 0 offen\n" f"\ts_waitcnt vmcnt(0)\n\texp mrt0 {dst} done vm\n\ts_endpgm\n") return run(fmt, body, dst_sel, {BASE + 4 * i: w for i, w in enumerate(SOURCE)}) def store(fmt: int, values: tuple, n: int, channels: int) -> tuple: memory = {BASE: 0xDEAD0000, BASE + 4: 0xDEAD0000} movs = "".join(f"\tv_mov_b32 v{2 + i}, {v:#x}\n" for i, v in enumerate(values[:n])) regs = "v2" if n == 1 else f"v[2:{1 + n}]" body = (movs + f"\tbuffer_store_format_{'xyzw'[:n]} {regs}, v1, s[0:3], 0 offen\n" "\texp mrt0 v2, v2, v2, v2 done vm\n\ts_endpgm\n") run(fmt, body, RUNTIME_SEL[channels], memory) return memory[BASE], memory[BASE + 4] def store_d16(fmt: int, pair: tuple, channels: int) -> tuple: """buffer_store_format_d16_xyzw of two registers (x | y << 16, z | w << 16): each half is a 16-bit integer, signed for SINT, then saturated or clamped like a full-width store.""" memory = {BASE: 0xDEAD0000, BASE + 4: 0xDEAD0000} body = (f"\tv_mov_b32 v2, {pair[0]:#x}\n\tv_mov_b32 v3, {pair[1]:#x}\n" "\tbuffer_store_format_d16_xyzw v[2:3], v1, s[0:3], 0 offen\n\texp mrt0 v2, v2, v2, v2 done vm\n\ts_endpgm\n") run(fmt, body, RUNTIME_SEL[channels], memory) return memory[BASE], memory[BASE + 4] class NarrowIntegerLoads(unittest.TestCase): def test_measured_words(self): for fmt, (xyzw, x011, d16) in LOADS.items(): with self.subTest(fmt=fmt): self.assertEqual(load(fmt, SEL_XYZW), xyzw, "DST_SEL XYZW") self.assertEqual(load(fmt, SEL_X011), x011, "DST_SEL X011: 0 and integer 1") self.assertEqual(load(fmt, SEL_XYZW, d16=True)[:2], d16, "d16") class NarrowIntegerStores(unittest.TestCase): def test_measured_words(self): for (fmt, values, n), want in STORES.items(): with self.subTest(fmt=fmt, values=values, n=n): self.assertEqual(store(fmt, VALUES[values], n, CHANNELS[fmt]), want) def test_measured_d16_words(self): # A d16 SINT half is sign-extended before the clamp: 0xff80 stores -128 (0x80) into 8_SINT, not 127. for (fmt, pair), want in D16_STORES.items(): with self.subTest(fmt=fmt, pair=pair): self.assertEqual(store_d16(fmt, D16_VALUES[pair], CHANNELS[fmt]), want) if __name__ == "__main__": unittest.main()