"""RDNA2 (gfx1036) buffer format store semantics (SHADER-364): 1. UNORM/SNORM format stores round the exact product clamp(x) * scale once (ties away from zero), matching native gfx1036 (wave32 and wave64, MUBUF and MTBUF): e.g. UNORM16(0.3) == 19661 (not 19660). 2. 2-component MUBUF buffer_store_format_* stores infer COMP_SWAP from V# DST_SEL_X/Y/Z: XY01 -> (x, y), YX01 -> (y, x), XYZW -> (x, w) (so _xy writes (x, 0)), YXZW/ZYXW -> (w, x); MTBUF tbuffer_store_format_* ignores DST_SEL and always stores (x, y). """ from __future__ import annotations import os import shutil import struct import subprocess 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 buffer_store_probe as probe # noqa: E402 import rdna2spirv # noqa: E402 import spirv_mini_interp as si # noqa: E402 BASE = 0x1000 STUB_DXBC = b"DXBC" + bytes(24) + struct.pack(" (x, y) for both _xy and _xyzw self.assertEqual(_run_store("buffer_store_format_xy", 62, (4, 5, 0, 1), vals), (0x11111111, 0x22222222)) self.assertEqual(_run_store("buffer_store_format_xyzw", 62, (4, 5, 0, 1), vals), (0x11111111, 0x22222222)) # YX01 -> (y, x) self.assertEqual(_run_store("buffer_store_format_xyzw", 62, (5, 4, 0, 1), vals), (0x22222222, 0x11111111)) # XYZW -> (x, w) for _xyzw and (x, 0) for _xy self.assertEqual(_run_store("buffer_store_format_xyzw", 62, (4, 5, 6, 7), vals), (0x11111111, 0x44444444)) self.assertEqual(_run_store("buffer_store_format_xy", 62, (4, 5, 6, 7), vals), (0x11111111, 0x00000000)) # YXZW -> (w, x) self.assertEqual(_run_store("buffer_store_format_xyzw", 62, (5, 4, 6, 7), vals), (0x44444444, 0x11111111)) def test_mubuf_2comp_packed_and_norm_formats(self): # 8_8_UINT (18): XY01 -> 0xdead2211, XYZW xyzw -> 0xdead4411, XYZW xy -> 0xdead0011 w0, _ = _run_store("buffer_store_format_xyzw", 18, (4, 5, 0, 1), (0x11, 0x22, 0x33, 0x44)) self.assertEqual(w0, 0xDEAD2211) w0, _ = _run_store("buffer_store_format_xyzw", 18, (4, 5, 6, 7), (0x11, 0x22, 0x33, 0x44)) self.assertEqual(w0, 0xDEAD4411) w0, _ = _run_store("buffer_store_format_xy", 18, (4, 5, 6, 7), (0x11, 0x22, 0x33, 0x44)) self.assertEqual(w0, 0xDEAD0011) # 16_16_UNORM (23): (0.25, 0.5, 0.75, 1.0) flt = tuple(probe.f32_to_u32(x) for x in (0.25, 0.5, 0.75, 1.0)) w0, _ = _run_store("buffer_store_format_xyzw", 23, (4, 5, 0, 1), flt) self.assertEqual(w0, 0x80004000) w0, _ = _run_store("buffer_store_format_xyzw", 23, (4, 5, 6, 7), flt) self.assertEqual(w0, 0xFFFF4000) # d16_xyzw and d16_xy on 16_16_UINT (27) d16_vals = (0x22221111, 0x44443333) w0, _ = _run_store("buffer_store_format_d16_xyzw", 27, (4, 5, 0, 1), d16_vals, d16=True) self.assertEqual(w0, 0x22221111) w0, _ = _run_store("buffer_store_format_d16_xyzw", 27, (4, 5, 6, 7), d16_vals, d16=True) self.assertEqual(w0, 0x44441111) w0, _ = _run_store("buffer_store_format_d16_xy", 27, (4, 5, 6, 7), d16_vals, d16=True) self.assertEqual(w0, 0x00001111) def test_mtbuf_2comp_ignores_dst_sel(self): vals = (0x11111111, 0x22222222, 0x33333333, 0x44444444) self.assertEqual(_run_store("tbuffer_store_format_xyzw", 62, (4, 5, 6, 7), vals, fixed_fmt=True), (0x11111111, 0x22222222)) self.assertEqual(_run_store("tbuffer_store_format_xyzw", 62, (5, 4, 6, 7), vals, fixed_fmt=True), (0x11111111, 0x22222222)) if __name__ == "__main__": unittest.main()