mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 13:08:37 +00:00
196 lines
8.6 KiB
Python
196 lines
8.6 KiB
Python
"""Fix 4E (translator half): 64-bit 2D `image_atomic_*` on Set 0 Binding 29 (`R64ui`).
|
|
|
|
2D 64-bit image atomics (`dmask:0x3` on 2-VGPR standard ops, `dmask:0xf` on 4-VGPR `image_atomic_cmpswap`)
|
|
emit `OpCapability Int64`, `OpCapability Int64Atomics`, `OpCapability Int64ImageEXT`,
|
|
`OpExtension "SPV_EXT_shader_image_int64"`, `%simages64` at `DescriptorSet 0, Binding 29`
|
|
(`OpTypeImage %ulong 2D 0 0 0 2 R64ui`), pack `(lo, hi)` -> `%ulong`, run the 64-bit atomic via
|
|
`OpImageTexelPointer %_ptr_Image_ulong`, and unpack `old` back to `(lo, hi)` when `glc` is set.
|
|
32-bit `image_atomic_cmpswap` (`dmask:0x3`, 2 VGPRs) stays 32-bit on `%simages` / `%simages3d`.
|
|
|
|
Hand-written listings, no game data.
|
|
|
|
python -I tests/semantic/test_image_atomic_int64.py -v
|
|
"""
|
|
import os
|
|
import shutil
|
|
import struct
|
|
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 cs_fssm(group_size=64):
|
|
b = bytearray(0x148)
|
|
b[0xD4] = 0x20 # wave32
|
|
struct.pack_into("<4I", b, 0x138, 0x8088, group_size, 1, 1)
|
|
return bytes(b)
|
|
|
|
|
|
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 ImageAtomicInt64Tests(unittest.TestCase):
|
|
def test_2d_umax_64bit_without_psb_and_with_glc(self):
|
|
"""Pure-image PS (no PSB) with 64-bit image_atomic_umax emits Int64/Int64Atomics/Int64ImageEXT on Binding 29."""
|
|
for glc in (False, True):
|
|
with self.subTest(glc=glc):
|
|
mod = " glc" if glc else ""
|
|
lines = [
|
|
"v_mov_b32_e32 v2, 10",
|
|
"v_mov_b32_e32 v3, 20",
|
|
"v_mov_b32_e32 v4, 0x12345678",
|
|
"v_mov_b32_e32 v5, 0x9abcdef0",
|
|
f"image_atomic_umax v[4:5], v[2:3], s[8:15] dmask:0x3 dim:SQ_RSRC_IMG_2D unorm{mod}",
|
|
"exp mrt0 v4, v5, v4, v5 done vm",
|
|
"s_endpgm",
|
|
]
|
|
asm = support.translate_lines("ps_img_atomic64", lines)
|
|
meta = rdna2spirv.shader_meta(rdna2spirv.translate.last_gen, asm)
|
|
self.assertIn("OpCapability Int64", asm)
|
|
self.assertIn("OpCapability Int64Atomics", asm)
|
|
self.assertIn("OpCapability Int64ImageEXT", asm)
|
|
self.assertIn('OpExtension "SPV_EXT_shader_image_int64"', asm)
|
|
self.assertIn("%simg64 = OpTypeImage %ulong 2D 0 0 0 2 R64ui", asm)
|
|
self.assertIn("OpDecorate %simages64 DescriptorSet 0", asm)
|
|
self.assertIn("OpDecorate %simages64 Binding 29", asm)
|
|
self.assertNotIn("Binding 4", asm)
|
|
self.assertIn("OpImageTexelPointer %_ptr_Image_ulong", asm)
|
|
self.assertIn("OpAtomicUMax %ulong", asm)
|
|
self.assertIn("shaderImageInt64Atomics", meta["features"])
|
|
self.assertIn(29, meta["bindings"])
|
|
if glc:
|
|
self.assertIn("OpShiftRightLogical %ulong", asm)
|
|
self.assertEqual(asm.count("OpUConvert %uint"), 2)
|
|
else:
|
|
self.assertNotIn("OpShiftRightLogical %ulong", asm)
|
|
validate_v12(self, asm, f"ps_umax64_glc_{glc}")
|
|
|
|
def test_cs_64bit_umax_with_psb_does_not_duplicate_ulong(self):
|
|
"""CS using both PSB (buffer_load) and 64-bit image_atomic_umax declares %ulong once and validates."""
|
|
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_dwordx2 v[4:5], v1, s[4:7], 0 offen",
|
|
"s_waitcnt vmcnt(0)",
|
|
"v_mov_b32_e32 v2, 1",
|
|
"v_mov_b32_e32 v3, 2",
|
|
"image_atomic_umax v[4:5], v[2:3], s[8:15] dmask:0x3 dim:SQ_RSRC_IMG_2D unorm",
|
|
"s_endpgm",
|
|
]
|
|
asm = support.translate_lines("cs_img_atomic64_psb", lines, fssm=cs_fssm())
|
|
self.assertEqual(asm.count("OpTypeInt 64 0"), 1)
|
|
self.assertEqual(asm.count("OpCapability Int64\n"), 1)
|
|
self.assertIn("OpAtomicUMax %ulong", asm)
|
|
validate_v12(self, asm, "cs_umax64_psb")
|
|
|
|
def test_all_64bit_atomic_opcodes_and_cmpswap_validate(self):
|
|
std_ops = [
|
|
("image_atomic_swap", "OpAtomicExchange"),
|
|
("image_atomic_add", "OpAtomicIAdd"),
|
|
("image_atomic_sub", "OpAtomicISub"),
|
|
("image_atomic_smin", "OpAtomicSMin"),
|
|
("image_atomic_umin", "OpAtomicUMin"),
|
|
("image_atomic_smax", "OpAtomicSMax"),
|
|
("image_atomic_umax", "OpAtomicUMax"),
|
|
("image_atomic_and", "OpAtomicAnd"),
|
|
("image_atomic_or", "OpAtomicOr"),
|
|
("image_atomic_xor", "OpAtomicXor"),
|
|
]
|
|
for op, spv_op in std_ops:
|
|
with self.subTest(op=op):
|
|
lines = [
|
|
"v_mov_b32_e32 v2, 0",
|
|
"v_mov_b32_e32 v3, 0",
|
|
"v_mov_b32_e32 v4, 1",
|
|
"v_mov_b32_e32 v5, 2",
|
|
f"{op} v[4:5], v[2:3], s[8:15] dmask:0x3 dim:SQ_RSRC_IMG_2D unorm glc",
|
|
"exp mrt0 v4, v5, v4, v5 done vm",
|
|
"s_endpgm",
|
|
]
|
|
asm = support.translate_lines(f"ps_{op}_64", lines)
|
|
self.assertIn(f"{spv_op} %ulong", asm)
|
|
validate_v12(self, asm, f"{op}_64")
|
|
|
|
# 64-bit cmpswap uses dmask:0xf and 4 VGPRs (v[4:5] = data64, v[6:7] = cmp64).
|
|
lines = [
|
|
"v_mov_b32_e32 v2, 0",
|
|
"v_mov_b32_e32 v3, 0",
|
|
"v_mov_b32_e32 v4, 1",
|
|
"v_mov_b32_e32 v5, 2",
|
|
"v_mov_b32_e32 v6, 3",
|
|
"v_mov_b32_e32 v7, 4",
|
|
"image_atomic_cmpswap v[4:7], v[2:3], s[8:15] dmask:0xf dim:SQ_RSRC_IMG_2D unorm glc",
|
|
"exp mrt0 v4, v5, v4, v5 done vm",
|
|
"s_endpgm",
|
|
]
|
|
asm = support.translate_lines("ps_cmpswap_64", lines)
|
|
self.assertIn("OpAtomicCompareExchange %ulong", asm)
|
|
validate_v12(self, asm, "cmpswap_64")
|
|
|
|
def test_32bit_cmpswap_dmask_0x3_stays_32bit(self):
|
|
"""32-bit image_atomic_cmpswap uses dmask:0x3 (2 VGPRs) and MUST stay 32-bit on Binding 4 / Binding 7."""
|
|
for dim, addr, binding in (("SQ_RSRC_IMG_2D", "v[2:3]", 4), ("SQ_RSRC_IMG_3D", "v[2:4]", 7)):
|
|
with self.subTest(dim=dim):
|
|
lines = [
|
|
"v_mov_b32_e32 v2, 0",
|
|
"v_mov_b32_e32 v3, 0",
|
|
"v_mov_b32_e32 v4, 0",
|
|
"v_mov_b32_e32 v6, 1",
|
|
"v_mov_b32_e32 v7, 2",
|
|
f"image_atomic_cmpswap v[6:7], {addr}, s[8:15] dmask:0x3 dim:{dim} unorm glc",
|
|
"exp mrt0 v6, v6, v6, v6 done vm",
|
|
"s_endpgm",
|
|
]
|
|
asm = support.translate_lines("ps_cmpswap_32", lines)
|
|
self.assertIn("OpAtomicCompareExchange %uint", asm)
|
|
self.assertIn(f"Binding {binding}", asm)
|
|
self.assertNotIn("Int64ImageEXT", asm)
|
|
self.assertNotIn("Binding 29", asm)
|
|
validate_v12(self, asm, f"cmpswap32_{dim}")
|
|
|
|
def test_64bit_3d_image_atomic_is_refused(self):
|
|
lines = [
|
|
"v_mov_b32_e32 v2, 0",
|
|
"v_mov_b32_e32 v3, 0",
|
|
"v_mov_b32_e32 v4, 0",
|
|
"v_mov_b32_e32 v6, 1",
|
|
"v_mov_b32_e32 v7, 2",
|
|
"image_atomic_umax v[6:7], v[2:4], s[8:15] dmask:0x3 dim:SQ_RSRC_IMG_3D unorm",
|
|
"exp mrt0 v6, v6, v6, v6 done vm",
|
|
"s_endpgm",
|
|
]
|
|
with self.assertRaises(rdna2spirv.Unsupported) as cm:
|
|
support.translate_lines("ps_umax64_3d", lines)
|
|
self.assertIn("64-bit 3D", str(cm.exception))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|