mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 12:38:38 +00:00
336 lines
16 KiB
Python
336 lines
16 KiB
Python
"""Integer SV_Target (UINT/SINT) unpatched v_cvt_pkrtz_f16_f32 -> raw uvec4/ivec4 export.
|
|
|
|
Xbox pixel shaders ship with a 6-dword patchable export slot per MRT carrying the FP16_ABGR default
|
|
(`v_cvt_pkrtz_f16_f32` + `exp mrtK ... compr`); at PSO creation the Xbox driver patches integer RTV slots
|
|
to FSSM template #9 (`SPI_SHADER_32_ABGR`: raw 32-bit channels masked by OSG1's component mask). Without
|
|
unpatching, translating the FP16 default destroys bit-packed 32-bit integers via FP16 RTZ conversion and
|
|
emits a float vec4 output violating VUID-VkGraphicsPipelineCreateInfo-renderPass-06055.
|
|
|
|
Hand-built OSG1 containers and RDNA2 listings, no game data.
|
|
|
|
python -I tests/semantic/test_int_mrt_export.py -v
|
|
"""
|
|
import os
|
|
import re
|
|
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
|
|
|
|
D3D_NAME_TARGET = 64
|
|
CTYPE_UINT = 1
|
|
CTYPE_SINT = 2
|
|
CTYPE_FLOAT = 3
|
|
|
|
|
|
def osg1_dxbc(specs):
|
|
"""Synthetic DXBC container with one OSG1 chunk.
|
|
|
|
`specs` is a list of `(sem_idx, ctype, mask)` for `SV_Target<sem_idx>`.
|
|
"""
|
|
payload = bytearray(struct.pack("<II", len(specs), 8))
|
|
for sem_idx, ctype, mask in specs:
|
|
elem = bytearray(32)
|
|
struct.pack_into("<6I", elem, 0, 0, 0, sem_idx, D3D_NAME_TARGET, ctype, sem_idx)
|
|
elem[24], elem[25] = mask & 0xF, mask & 0xF
|
|
payload += elem
|
|
part = b"OSG1" + struct.pack("<I", len(payload)) + bytes(payload)
|
|
part += bytes((-len(part)) % 4)
|
|
header = b"DXBC" + bytes(16) + struct.pack("<III", 1, 0, 1)
|
|
return header + struct.pack("<I", len(header) + 8) + bytes(4) + part
|
|
|
|
|
|
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)
|
|
res = rdna2spirv.assemble_spirv(sa, sv)
|
|
testcase.assertEqual(res.returncode, 0, f"{label} spirv-as/val failed:\n{res.stderr}\n{res.stdout}")
|
|
sv_py = os.path.join(tmp, f"{label}_py.spv")
|
|
with open(sv_py, "wb") as f:
|
|
f.write(spirv_asm.assemble(asm, target_env="vulkan1.2"))
|
|
r2 = subprocess.run(["spirv-val", "--target-env", "vulkan1.2", sv_py], capture_output=True, text=True)
|
|
testcase.assertEqual(r2.returncode, 0, f"{label} spirv_asm+val failed:\n{r2.stderr}\n{r2.stdout}")
|
|
|
|
|
|
def output_scalar_type(spvasm, var_name):
|
|
"""Return the scalar SPIR-V type ('%float', '%uint', '%int') of an Output vec4 variable."""
|
|
m = re.search(rf"{re.escape(var_name)} = OpVariable (%\w+) Output", spvasm)
|
|
assert m, f"{var_name} not declared in:\n{spvasm}"
|
|
ptr_ty = m.group(1)
|
|
m2 = re.search(rf"{re.escape(ptr_ty)} = OpTypePointer Output (%\w+)", spvasm)
|
|
assert m2, f"pointer type {ptr_ty} not declared"
|
|
vec_ty = m2.group(1)
|
|
m3 = re.search(rf"{re.escape(vec_ty)} = OpTypeVector (%\w+) 4", spvasm)
|
|
assert m3, f"vector type {vec_ty} not declared"
|
|
return m3.group(1)
|
|
|
|
|
|
class IntMrtExportTests(unittest.TestCase):
|
|
def test_r32_uint_unpatched_pkrtz_preserves_exact_bits(self):
|
|
"""R32_UINT (mask=0x1) with s_nop between pkrtz and compr exp preserves arbitrary 32-bit uint words."""
|
|
dxbc = osg1_dxbc([(0, CTYPE_UINT, 0x1)])
|
|
for val in (0x477FE000, 0x3F800001, 0xDEADBEEF, 0x00000001, 0xFFFFFFFF):
|
|
with self.subTest(val=hex(val)):
|
|
lines = [
|
|
f"v_mov_b32_e32 v7, {val:#x}",
|
|
"s_nop 0",
|
|
"s_nop 0",
|
|
"v_cvt_pkrtz_f16_f32 v4, v7, v7",
|
|
"s_nop 0",
|
|
"exp mrt0 v4, v4, off, off done compr vm",
|
|
"s_endpgm",
|
|
]
|
|
asm = support.translate_lines("ps_r32_uint", lines, dxbc=dxbc)
|
|
self.assertEqual(output_scalar_type(asm, "%out_mrt0"), "%uint")
|
|
self.assertNotIn("UnpackHalf2x16", asm)
|
|
self.assertIn("%initial_mrt_u = OpConstantNull", asm)
|
|
validate_v12(self, asm, "r32_uint")
|
|
out = support.run_fragment(asm)["%out_mrt0"]
|
|
self.assertEqual(out, (val, 0, 0, 0))
|
|
|
|
def test_r32_uint_adjacent_pkrtz_without_nop(self):
|
|
"""R32_UINT (mask=0x1) with pkrtz immediately before compr exp (no intervening s_nop)."""
|
|
dxbc = osg1_dxbc([(0, CTYPE_UINT, 0x1)])
|
|
lines = [
|
|
"v_mov_b32_e32 v7, 0x3f800001",
|
|
"v_cvt_pkrtz_f16_f32 v4, v7, v7",
|
|
"exp mrt0 v4, v4, off, off done compr vm",
|
|
"s_endpgm",
|
|
]
|
|
asm = support.translate_lines("ps_r32_uint_adj", lines, dxbc=dxbc)
|
|
self.assertEqual(output_scalar_type(asm, "%out_mrt0"), "%uint")
|
|
self.assertNotIn("UnpackHalf2x16", asm)
|
|
validate_v12(self, asm, "r32_uint_adj")
|
|
self.assertEqual(support.run_fragment(asm)["%out_mrt0"], (0x3F800001, 0, 0, 0))
|
|
|
|
def test_rg32_uint_mask_0x3(self):
|
|
"""RG32_UINT (mask=0x3) exports both low channels verbatim and zeroes z/w."""
|
|
dxbc = osg1_dxbc([(0, CTYPE_UINT, 0x3)])
|
|
lines = [
|
|
"v_mov_b32_e32 v7, 0x12345678",
|
|
"v_mov_b32_e32 v8, 0x9abcdef0",
|
|
"v_cvt_pkrtz_f16_f32 v4, v7, v8",
|
|
"s_nop 0",
|
|
"exp mrt0 v4, v4, off, off done compr vm",
|
|
"s_endpgm",
|
|
]
|
|
asm = support.translate_lines("ps_rg32_uint", lines, dxbc=dxbc)
|
|
self.assertEqual(output_scalar_type(asm, "%out_mrt0"), "%uint")
|
|
self.assertNotIn("UnpackHalf2x16", asm)
|
|
validate_v12(self, asm, "rg32_uint")
|
|
self.assertEqual(support.run_fragment(asm)["%out_mrt0"], (0x12345678, 0x9ABCDEF0, 0, 0))
|
|
|
|
def test_rgba32_uint_two_pkrtz_all_four_channels(self):
|
|
"""RGBA32_UINT (mask=0xf) with two pkrtz instructions exports all 4 raw 32-bit uint channels."""
|
|
dxbc = osg1_dxbc([(0, CTYPE_UINT, 0xF)])
|
|
lines = [
|
|
"v_mov_b32_e32 v7, 0x477fe000",
|
|
"v_mov_b32_e32 v8, 0x3f800001",
|
|
"v_mov_b32_e32 v9, 0x00000007",
|
|
"v_mov_b32_e32 v10, 0x80000001",
|
|
"s_nop 0",
|
|
"s_nop 0",
|
|
"v_cvt_pkrtz_f16_f32 v4, v7, v8",
|
|
"v_cvt_pkrtz_f16_f32 v5, v9, v10",
|
|
"exp mrt0 v4, v4, v5, v5 done compr vm",
|
|
"s_endpgm",
|
|
]
|
|
asm = support.translate_lines("ps_rgba32_uint", lines, dxbc=dxbc)
|
|
self.assertEqual(output_scalar_type(asm, "%out_mrt0"), "%uint")
|
|
self.assertNotIn("UnpackHalf2x16", asm)
|
|
self.assertNotIn("%initial_mrt_u", asm)
|
|
validate_v12(self, asm, "rgba32_uint")
|
|
self.assertEqual(
|
|
support.run_fragment(asm)["%out_mrt0"],
|
|
(0x477FE000, 0x3F800001, 0x00000007, 0x80000001),
|
|
)
|
|
|
|
def test_r32_sint_preserves_exact_signed_bits(self):
|
|
"""R32_SINT (ctype=2, mask=0x1) declares %v4int output and preserves signed 32-bit words."""
|
|
dxbc = osg1_dxbc([(0, CTYPE_SINT, 0x1)])
|
|
for val in (0xFFFFFFFF, 0xFFED2979, 0x80000000, 0x7FFFFFFF, 0x00000001):
|
|
with self.subTest(val=hex(val)):
|
|
lines = [
|
|
f"v_mov_b32_e32 v7, {val:#x}",
|
|
"v_cvt_pkrtz_f16_f32 v4, v7, v7",
|
|
"s_nop 0",
|
|
"exp mrt0 v4, v4, off, off done compr vm",
|
|
"s_endpgm",
|
|
]
|
|
asm = support.translate_lines("ps_r32_sint", lines, dxbc=dxbc)
|
|
self.assertEqual(output_scalar_type(asm, "%out_mrt0"), "%int")
|
|
self.assertNotIn("UnpackHalf2x16", asm)
|
|
self.assertIn("%initial_mrt_i = OpConstantNull", asm)
|
|
validate_v12(self, asm, "r32_sint")
|
|
self.assertEqual(support.run_fragment(asm)["%out_mrt0"], (val, 0, 0, 0))
|
|
|
|
def test_mixed_float_mrt0_and_uint_mrt1(self):
|
|
"""Mixed FLOAT mrt0 + UINT mrt1 keeps mrt0 as %v4float (compressed f16) and unpatches mrt1 to %v4uint."""
|
|
dxbc = osg1_dxbc([(0, CTYPE_FLOAT, 0xF), (1, CTYPE_UINT, 0x1)])
|
|
lines = [
|
|
"v_mov_b32_e32 v0, 0.5",
|
|
"v_mov_b32_e32 v1, 1.0",
|
|
"v_mov_b32_e32 v2, 2.0",
|
|
"v_mov_b32_e32 v3, 4.0",
|
|
"v_mov_b32_e32 v7, 0x477fe000",
|
|
"v_cvt_pkrtz_f16_f32 v4, v0, v1",
|
|
"v_cvt_pkrtz_f16_f32 v5, v2, v3",
|
|
"exp mrt0 v4, v4, v5, v5 compr vm",
|
|
"s_nop 0",
|
|
"s_nop 0",
|
|
"v_cvt_pkrtz_f16_f32 v6, v7, v7",
|
|
"s_nop 0",
|
|
"exp mrt1 v6, v6, off, off done compr vm",
|
|
"s_endpgm",
|
|
]
|
|
asm = support.translate_lines("ps_mixed_float_uint", lines, dxbc=dxbc)
|
|
self.assertEqual(output_scalar_type(asm, "%out_mrt0"), "%float")
|
|
self.assertEqual(output_scalar_type(asm, "%out_mrt1"), "%uint")
|
|
self.assertIn("UnpackHalf2x16", asm) # mrt0 float compr still unpacked
|
|
validate_v12(self, asm, "mixed_float_uint")
|
|
out = support.run_fragment(asm)
|
|
self.assertEqual(
|
|
[struct.unpack("<f", struct.pack("<I", w))[0] for w in out["%out_mrt0"]],
|
|
[0.5, 1.0, 2.0, 4.0],
|
|
)
|
|
self.assertEqual(out["%out_mrt1"], (0x477FE000, 0, 0, 0))
|
|
|
|
def test_uncompressed_uint_export_passes_through(self):
|
|
"""Uncompressed `exp mrt0 v0, v1, v2, v3` on UINT exports all 4 channels directly as %v4uint."""
|
|
dxbc = osg1_dxbc([(0, CTYPE_UINT, 0xF)])
|
|
lines = [
|
|
"v_mov_b32_e32 v0, 0x11111111",
|
|
"v_mov_b32_e32 v1, 0x22222222",
|
|
"v_mov_b32_e32 v2, 0x33333333",
|
|
"v_mov_b32_e32 v3, 0x44444444",
|
|
"exp mrt0 v0, v1, v2, v3 done vm",
|
|
"s_endpgm",
|
|
]
|
|
asm = support.translate_lines("ps_uncompr_uint", lines, dxbc=dxbc)
|
|
self.assertEqual(output_scalar_type(asm, "%out_mrt0"), "%uint")
|
|
validate_v12(self, asm, "uncompr_uint")
|
|
self.assertEqual(
|
|
support.run_fragment(asm)["%out_mrt0"],
|
|
(0x11111111, 0x22222222, 0x33333333, 0x44444444),
|
|
)
|
|
|
|
def test_unmatched_compressed_int_export_is_refused(self):
|
|
"""Compressed integer export without matching pkrtz epilogue raises Unsupported."""
|
|
dxbc = osg1_dxbc([(0, CTYPE_UINT, 0x1)])
|
|
lines = [
|
|
"v_mov_b32_e32 v4, 0x3c003c00",
|
|
"exp mrt0 v4, v4, off, off done compr vm",
|
|
"s_endpgm",
|
|
]
|
|
with self.assertRaises(rdna2spirv.Unsupported) as cm:
|
|
support.translate_lines("ps_bad_int_compr", lines, dxbc=dxbc)
|
|
self.assertIn("compressed integer color export", str(cm.exception))
|
|
|
|
def test_branch_targeting_bypassed_pkrtz_preserves_pc(self):
|
|
"""Replacing bypassed pkrtz with s_nop keeps its PC/encoding so branches to it remain valid."""
|
|
dxbc = osg1_dxbc([(0, CTYPE_UINT, 0x1)])
|
|
# Hand-assembled offsets: s_branch +1 skips the 4-byte v_mov at 0x0c and lands on pkrtz at 0x10.
|
|
listing = (
|
|
"; ps_branch_int_mrt hand-written test program\n"
|
|
"\tv_mov_b32_e32 v7, 0x3f800001\t; encoding: [0xff,0x02,0x0e,0x7e,0x01,0x00,0x80,0x3f]\n"
|
|
"\ts_branch 1\t; encoding: [0x01,0x00,0x82,0xbf]\n"
|
|
"\tv_mov_b32_e32 v7, 0\t; encoding: [0x80,0x02,0x0e,0x7e]\n"
|
|
"\tv_cvt_pkrtz_f16_f32 v4, v7, v7\t; encoding: [0x04,0x00,0x68,0xd7,0x07,0x0f,0x02,0x00]\n"
|
|
"\ts_nop 0\t; encoding: [0x00,0x00,0x80,0xbf]\n"
|
|
"\texp mrt0 v4, v4, off, off done compr vm\t; encoding: [0x03,0x1c,0x00,0xf8,0x04,0x04,0x00,0x00]\n"
|
|
"\ts_endpgm\t; encoding: [0x00,0x00,0x81,0xbf]\n"
|
|
).encode("utf-8")
|
|
asm = support.translate_listing("ps_branch_int_mrt", listing, dxbc=dxbc)
|
|
self.assertEqual(output_scalar_type(asm, "%out_mrt0"), "%uint")
|
|
validate_v12(self, asm, "branch_int_mrt")
|
|
self.assertEqual(support.run_fragment(asm)["%out_mrt0"], (0x3F800001, 0, 0, 0))
|
|
|
|
def test_int_mrt_with_typed_image_preserves_v4uint_st_dedupe(self):
|
|
"""Integer MRT combined with typed image store keeps %v4uint_st dedupe naming and passes spirv-val."""
|
|
dxbc = osg1_dxbc([(0, CTYPE_UINT, 0x1)])
|
|
lines = [
|
|
"v_mov_b32_e32 v0, 0",
|
|
"v_mov_b32_e32 v1, 0",
|
|
"v_mov_b32_e32 v7, 0x477fe000",
|
|
"image_store v7, v[0:1], s[4:11] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm",
|
|
"v_cvt_pkrtz_f16_f32 v4, v7, v7",
|
|
"s_nop 0",
|
|
"exp mrt0 v4, v4, off, off done compr vm",
|
|
"s_endpgm",
|
|
]
|
|
asm = support.translate_lines("ps_int_mrt_with_img_store", lines, dxbc=dxbc)
|
|
self.assertIn("%v4uint_st = OpTypeVector %uint 4", asm)
|
|
self.assertEqual(output_scalar_type(asm, "%out_mrt0"), "%uint")
|
|
validate_v12(self, asm, "int_mrt_with_img_store")
|
|
|
|
def test_mid_sequence_branch_target_is_refused(self):
|
|
"""A branch landing strictly inside (min(nop_idxs), idx] must be rejected, not silently miscompiled."""
|
|
dxbc = osg1_dxbc([(0, CTYPE_UINT, 0x1)])
|
|
# s_branch +2 at 0x10 (nxt=0x14, target=0x1c) jumps over the 8-byte pkrtz directly onto exp mrt0!
|
|
listing = (
|
|
"; ps_mid_branch_int_mrt hand-written test program\n"
|
|
"\tv_mov_b32_e32 v7, 1\t; encoding: [0x81,0x02,0x0e,0x7e]\n"
|
|
"\tv_mov_b32_e32 v8, 2\t; encoding: [0x82,0x02,0x10,0x7e]\n"
|
|
"\tv_cvt_pkrtz_f16_f32 v4, v7, v7\t; encoding: [0x04,0x00,0x68,0xd7,0x07,0x0f,0x02,0x00]\n"
|
|
"\ts_branch 2\t; encoding: [0x02,0x00,0x82,0xbf]\n"
|
|
"\tv_cvt_pkrtz_f16_f32 v4, v8, v8\t; encoding: [0x04,0x00,0x68,0xd7,0x08,0x11,0x02,0x00]\n"
|
|
"\texp mrt0 v4, v4, off, off done compr vm\t; encoding: [0x03,0x1c,0x00,0xf8,0x04,0x04,0x00,0x00]\n"
|
|
"\ts_endpgm\t; encoding: [0x00,0x00,0x81,0xbf]\n"
|
|
).encode("utf-8")
|
|
with self.assertRaises(rdna2spirv.Unsupported) as cm:
|
|
support.translate_listing("ps_mid_branch_int_mrt", listing, dxbc=dxbc)
|
|
self.assertIn("compressed integer color export", str(cm.exception))
|
|
|
|
def test_overlapping_pkrtz_destinations_are_refused(self):
|
|
"""2-pkrtz sequence where r0 == r1 (second pkrtz overwrites r0) must be rejected."""
|
|
dxbc = osg1_dxbc([(0, CTYPE_UINT, 0xF)])
|
|
lines = [
|
|
"v_mov_b32_e32 v7, 1",
|
|
"v_mov_b32_e32 v8, 2",
|
|
"v_mov_b32_e32 v9, 3",
|
|
"v_mov_b32_e32 v10, 4",
|
|
"v_cvt_pkrtz_f16_f32 v4, v7, v8",
|
|
"v_cvt_pkrtz_f16_f32 v4, v9, v10",
|
|
"exp mrt0 v4, v4, v4, v4 done compr vm",
|
|
"s_endpgm",
|
|
]
|
|
with self.assertRaises(rdna2spirv.Unsupported) as cm:
|
|
support.translate_lines("ps_overlap_pkrtz_dst", lines, dxbc=dxbc)
|
|
self.assertIn("compressed integer color export", str(cm.exception))
|
|
|
|
def test_sdwa_dpp_and_sext_pkrtz_are_refused(self):
|
|
"""Non-plain pkrtz encodings (_sdwa/_dpp) or input modifiers (sext/neg/abs) must be rejected."""
|
|
dxbc = osg1_dxbc([(0, CTYPE_UINT, 0x1)])
|
|
for ins in (
|
|
"v_cvt_pkrtz_f16_f32_sdwa v4, sext(v7), v7",
|
|
"v_cvt_pkrtz_f16_f32 v4, -v7, v7",
|
|
"v_cvt_pkrtz_f16_f32 v4, |v7|, v7",
|
|
):
|
|
with self.subTest(ins=ins), self.assertRaises(rdna2spirv.Unsupported) as cm:
|
|
support.translate_lines(
|
|
"ps_mod_pkrtz",
|
|
["v_mov_b32_e32 v7, 1", ins, "exp mrt0 v4, v4, off, off done compr vm", "s_endpgm"],
|
|
dxbc=dxbc,
|
|
)
|
|
self.assertIn("compressed integer color export", str(cm.exception))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|