"""Fix 4D: `image_store` with upper-channel / non-contiguous `dmask` preserves unset channels via RMW. Non-contiguous or upper-channel write masks (`dmask:0x8`, `0x9`, `0xd`, `0x2`, `0x4`, etc.) update only the enabled channels of a multi-channel UAV. Previously `image_store` padded unset channels with 0, clobbering `.rgb` on `dmask:0x8` writes. Now `image_store` emits `OpImageRead` + `OpVectorShuffle` + `OpImageWrite` (requiring `StorageImageReadWithoutFormat`) on partial masks while leaving contiguous masks (`0x1`, `0x3`, `0x7`, `0xf`) write-only. Hand-written listings, no game data. python -I tests/semantic/test_image_store_partial_rmw.py -v """ import os import re import shutil import subprocess import sys import tempfile import unittest HERE = os.path.dirname(os.path.abspath(__file__)) if HERE not in sys.path: sys.path.insert(0, HERE) import support # noqa: E402 from support import rdna2spirv # noqa: E402 import spirv_asm # noqa: E402 def validate_v12(testcase, asm, label): if not shutil.which("spirv-val"): return with tempfile.TemporaryDirectory() as tmp: if shutil.which("spirv-as"): sa = os.path.join(tmp, f"{label}.spvasm") sv = os.path.join(tmp, f"{label}.spv") with open(sa, "w", encoding="utf-8") as f: f.write(asm) r = rdna2spirv.assemble_spirv(sa, sv) testcase.assertEqual(r.returncode, 0, f"{label} spirv-as/val failed:\n{r.stderr}\n{r.stdout}") spv_py = os.path.join(tmp, f"{label}_py.spv") with open(spv_py, "wb") as f: f.write(spirv_asm.assemble(asm, target_env="vulkan1.2")) r2 = subprocess.run(["spirv-val", "--target-env", "vulkan1.2", spv_py], capture_output=True, text=True) testcase.assertEqual(r2.returncode, 0, f"{label} spirv_asm+val failed:\n{r2.stderr}\n{r2.stdout}") class ImageStorePartialRmwTests(unittest.TestCase): def test_non_contiguous_dmasks_emit_rmw_shuffle_and_validate(self): cases = [ ("0x8", "SQ_RSRC_IMG_2D", "v1", "v[2:3]", "unorm d16", "4 5 6 3"), ("0x8", "SQ_RSRC_IMG_2D", "v1", "v[2:3]", "unorm", "4 5 6 3"), ("0x2", "SQ_RSRC_IMG_2D", "v1", "v[2:3]", "unorm", "4 1 6 7"), ("0x4", "SQ_RSRC_IMG_2D", "v1", "v[2:3]", "unorm d16", "4 5 2 7"), ("0x9", "SQ_RSRC_IMG_3D", "v[0:1]", "v[2:4]", "unorm", "0 5 6 3"), ("0x9", "SQ_RSRC_IMG_3D", "v0", "v[2:4]", "unorm d16", "0 5 6 3"), ("0xd", "SQ_RSRC_IMG_2D_ARRAY", "v[0:2]", "v[3:5]", "unorm", "0 5 2 3"), ] for dmask, dim, vdata, vaddr, mods, shuf in cases: with self.subTest(dmask=dmask, dim=dim, mods=mods): lines = [ "v_mov_b32_e32 v0, 1.0", "v_mov_b32_e32 v1, 0.5", "v_mov_b32_e32 v2, 0", "v_mov_b32_e32 v3, 0", "v_mov_b32_e32 v4, 0", "v_mov_b32_e32 v5, 0", f"image_store {vdata}, {vaddr}, s[8:15] dmask:{dmask} dim:{dim} {mods}", "exp mrt0 v0, v0, v0, v0 done vm", "s_endpgm", ] asm = support.translate_lines("ps_img_rmw", lines) meta = rdna2spirv.shader_meta(rdna2spirv.translate.last_gen, asm) self.assertIn("OpCapability StorageImageReadWithoutFormat", asm) self.assertIn("shaderStorageImageReadWithoutFormat", meta["features"]) # All 3 typed branches (float, uint, sint) must read-modify-write with the exact shuffle mask. self.assertEqual(asm.count(" = OpImageRead "), 3) shuffles = re.findall(r"OpVectorShuffle (%\w+) %\w+ %\w+ ([0-7 ]+)", asm) self.assertEqual(len(shuffles), 3) for _ty, got_shuf in shuffles: self.assertEqual(got_shuf.strip(), shuf) validate_v12(self, asm, f"rmw_{dmask}_{dim}") def test_contiguous_dmasks_stay_write_only(self): for dmask, vdata in (("0x1", "v0"), ("0x3", "v[0:1]"), ("0x7", "v[0:2]"), ("0xf", "v[0:3]")): with self.subTest(dmask=dmask): lines = [ "v_mov_b32_e32 v0, 1.0", "v_mov_b32_e32 v1, 1.0", "v_mov_b32_e32 v2, 1.0", "v_mov_b32_e32 v3, 1.0", "v_mov_b32_e32 v4, 0", "v_mov_b32_e32 v5, 0", f"image_store {vdata}, v[4:5], s[8:15] dmask:{dmask} dim:SQ_RSRC_IMG_2D unorm", "exp mrt0 v0, v0, v0, v0 done vm", "s_endpgm", ] asm = support.translate_lines("ps_img_contig", lines) self.assertNotIn("OpCapability StorageImageReadWithoutFormat", asm) self.assertNotIn("OpImageRead", asm) self.assertNotIn("OpVectorShuffle", asm) validate_v12(self, asm, f"contig_{dmask}") def test_partial_rmw_glc_under_vmm_annotates_both_read_and_write(self): lines = [ "s_mov_b32 s4, 0x1000", "s_mov_b32 s5, 0", "s_mov_b32 s6, 0x100", "s_mov_b32 s7, 0x30016fac", "v_mov_b32_e32 v1, 0", "buffer_load_dword v0, v1, s[4:7], 0 offen glc", "s_waitcnt vmcnt(0)", "v_mov_b32_e32 v2, 0", "v_mov_b32_e32 v3, 0", "image_store v0, v[2:3], s[8:15] dmask:0x8 dim:SQ_RSRC_IMG_2D unorm glc", "exp mrt0 v0, v0, v0, v0 done vm", "s_endpgm", ] asm = support.translate_lines("ps_img_rmw_vmm", lines) self.assertIn("OpMemoryModel PhysicalStorageBuffer64 Vulkan", asm) self.assertNotIn(" Coherent", asm) self.assertEqual(asm.count("MakeTexelVisible|NonPrivateTexel %c1"), 3) self.assertEqual(asm.count("MakeTexelAvailable|NonPrivateTexel %c1"), 3) validate_v12(self, asm, "rmw_vmm_glc") if __name__ == "__main__": unittest.main()