Files
mandy/tests/semantic/test_export_valid_mask.py
Vonsek cfdc3bab0d fix(shader): preserve channels across partial color exports
An EXP component mask can write different channels of the same MRT across multiple instructions. Retain disabled components from that target so a later export does not erase color that an earlier instruction wrote.
2026-11-22 17:45:32 +01:00

173 lines
8.4 KiB
Python

"""The PS valid mask: a pixel is discarded by EXEC at its last executed `exp ... vm`, not by EXEC at the end.
RDNA2 ISA reference guide (30-November-2020), 11.1 and 11.2.1: with VM set EXEC is the valid mask, the final one
sent is used, and an export to NULL may carry only the mask. Not measured on hardware; docs/design.md, EXEC.
Hand-written programs, no tools needed.
"""
from pathlib import Path
import unittest
import support
import rdna2spirv
import shader_ir
from spirv_mini_interp import run_fragment
from spirv_wave_interp import run_wave
ONE = 0x3F800000
SEED = [f"v_mov_b32_e32 v2, 0x{ONE:x}", "v_mov_b32_e32 v1, 1"]
OFF = ["v_cmpx_eq_u32_e32 0, v1"]
ON = ["s_mov_b64 exec, -1"]
EXP = ["exp mrt0 v2, v2, v2, v2 done vm"]
EXP_NO_VM = ["exp mrt0 v2, v2, v2, v2"]
EXP_MRT1 = ["exp mrt1 v2, v2, v2, v2"]
NULL = ["exp null off, off, off, off done vm"]
END = ["s_endpgm"]
KILLED = {}
MRT0 = {"%out_mrt0": (ONE,) * 4}
def sized(lines):
"""The lines with a placeholder encoding of the right size: branch offsets are counted in dwords of it."""
out = []
for line in lines:
size = 8 if line.startswith("exp ") or "0x3f800000" in line else 4
out.append(f"{line} ; encoding: [" + ",".join(["0x00"] * size) + "]")
return out
def translate(lines):
return support.translate_lines("ps_valid_mask", sized(lines))
def dwords(lines):
return sum(2 if line.startswith("exp ") or "0x3f800000" in line else 1 for line in lines)
class StraightLine(unittest.TestCase):
def check(self, lines, want):
self.assertEqual(run_fragment(translate(SEED + lines + END)), want)
def test_disjoint_color_exports_accumulate_per_target(self) -> None:
fixture = Path(__file__).with_name("fixtures") / "partial_color_exports.asm"
shader = support.translate_lines("ps_partial_exports", fixture.read_text(encoding="ascii").splitlines())
self.assertEqual(run_fragment(shader), {
"%out_mrt0": (0x3F800000, 0x40000000, 0x40400000, 0x40800000),
"%out_mrt1": (0x40A00000, 0x40C00000, 0x40E00000, 0x41000000),
})
def test_first_partial_color_export_has_zero_unwritten_channels(self) -> None:
lines = ["exp mrt0 off, v2, off, v2 done vm"]
shader = translate(SEED + lines + END)
self.assertEqual(run_fragment(shader), {"%out_mrt0": (0, ONE, 0, ONE)})
# The interpreter defaults unwritten outputs to zero. Vulkan needs an explicit initializer here.
self.assertIn("%initial_mrt = OpConstantNull %v4float", shader)
self.assertIn("%out_mrt0 = OpVariable %_ptr_Output_v4 Output %initial_mrt", shader)
def test_partial_export_obeys_exec_and_replaces_enabled_channels(self) -> None:
lines = ["v_mov_b32_e32 v3, 0x40000000", "exp mrt0 v2, v2, v2, v2", "s_waitcnt expcnt(0)"] + OFF
lines += ["exp mrt0 off, v3, v3, off", "s_waitcnt expcnt(0)"] + ON
lines += ["exp mrt0 off, off, v3, off done vm"]
self.check(lines, {"%out_mrt0": (ONE, ONE, 0x40000000, ONE)})
def test_exec_unchanged_after_the_export(self):
self.check(EXP, MRT0)
self.check(OFF + EXP, KILLED)
def test_off_at_the_export_and_back_on_is_killed(self):
self.check(OFF + EXP + ON, KILLED)
def test_on_at_the_export_and_off_afterwards_is_kept(self):
self.check(EXP + OFF, MRT0)
self.check(EXP + ["s_mov_b64 exec, 0"], MRT0)
def test_null_export_carries_the_mask_too(self):
self.check(EXP_NO_VM + OFF + NULL + ON, KILLED)
self.check(EXP_NO_VM + NULL + OFF, MRT0)
def test_compressed_export_carries_the_mask_too(self):
compr = ["exp mrt0 v2, v2, v2, v2 done compr vm"]
self.check(OFF + compr + ON, KILLED)
self.assertEqual(list(run_fragment(translate(SEED + compr + OFF + END))), ["%out_mrt0"])
def test_depth_export_carries_the_mask_too(self):
depth = ["exp mrtz v2, off, off, off done vm"]
self.check(EXP_NO_VM + OFF + depth + ON, KILLED)
self.assertEqual(run_fragment(translate(SEED + EXP_NO_VM + depth + OFF + END))["%out_mrt0"], (ONE,) * 4)
def test_the_last_vm_export_wins(self):
first = ["exp mrt0 v2, v2, v2, v2 vm"]
self.check(first + OFF + NULL, KILLED)
self.check(first + OFF + ON + NULL + OFF, MRT0)
def test_a_later_export_without_vm_does_not_move_the_mask(self):
self.check(EXP + OFF + EXP_MRT1, {**MRT0, "%out_mrt1": (0,) * 4}) # 0: the interpreter's unwritten output
def test_without_a_vm_export_exec_at_the_end_decides(self):
self.check(EXP_NO_VM + OFF, KILLED)
self.check(EXP_NO_VM + OFF + ON, MRT0)
class Branches(unittest.TestCase):
def skip_over(self, taken, skipped):
"""SCC = `taken`, then a branch over `skipped`."""
return [f"s_cmp_eq_u32 0, {0 if taken else 1}", f"s_cbranch_scc1 {dwords(skipped)}"] + skipped
def test_exec_write_in_a_later_block(self):
for taken in (False, True):
body = EXP + self.skip_over(taken, OFF)
self.assertEqual(run_fragment(translate(SEED + body + END)), MRT0, taken)
body = OFF + EXP + self.skip_over(taken, ON)
self.assertEqual(run_fragment(translate(SEED + body + END)), KILLED, taken)
def test_exec_write_several_blocks_later(self):
far = self.skip_over(False, ["v_mov_b32_e32 v3, 0"])
body = EXP + far + self.skip_over(False, OFF)
self.assertEqual(run_fragment(translate(SEED + body + END)), MRT0)
body = OFF + EXP + far + self.skip_over(False, ON)
self.assertEqual(run_fragment(translate(SEED + body + END)), KILLED)
def test_a_path_around_the_vm_export_uses_exec_at_the_end(self):
# The branch skips the export: that path has no valid mask, so EXEC at the end decides, as before.
body = EXP_NO_VM + self.skip_over(True, NULL) + OFF
self.assertEqual(run_fragment(translate(SEED + body + END)), KILLED)
body = EXP_NO_VM + self.skip_over(False, NULL) + OFF
self.assertEqual(run_fragment(translate(SEED + body + END)), MRT0)
def test_exec_write_earlier_in_a_loop(self):
# Two rounds. EXEC is written above the export, so only the back edge puts that write after it.
head = SEED + EXP_NO_VM + ["s_mov_b32 s4, 0"]
loop = ON + ["s_cmp_eq_u32 s4, 0", "s_cbranch_scc1 1"] + OFF + NULL + ["s_add_u32 s4, s4, 1",
"s_cmp_lt_u32 s4, 2"]
back = [f"s_cbranch_scc1 {65536 - dwords(loop) - 1}"]
text = translate(head + loop + back + ON + END)
self.assertEqual(run_wave(text, wave=32)["outputs"], [None] * 32) # off at the second round's export
loop = ON + ["s_cmp_eq_u32 s4, 1", "s_cbranch_scc1 1"] + OFF + NULL + ["s_add_u32 s4, s4, 1",
"s_cmp_lt_u32 s4, 2"]
text = translate(head + loop + back + OFF + END)
self.assertNotIn(None, run_wave(text, wave=32)["outputs"]) # on at the second round's export
class OnlyWhereNeeded(unittest.TestCase):
def test_no_latch_when_exec_cannot_change_after_the_export(self):
for body in (EXP, OFF + EXP, OFF + ON + EXP, EXP_NO_VM + OFF, OFF + EXP + ["s_cmp_eq_u64 exec, 0"],
OFF + EXP + ["v_mov_b32_e32 v3, 0"]):
self.assertNotIn("%vm_mask", translate(SEED + body + END), body)
def test_code_after_endpgm_does_not_count(self):
self.assertNotIn("%vm_mask", translate(SEED + OFF + EXP + END + ON))
def test_every_form_that_writes_exec_is_seen(self):
for line in ("v_cmpx_eq_u32_e32 0, v1", "v_cmpx_lt_f32_e64 v1, v2", "v_cmpx_class_f32_e32 v1, v2",
"v_cmpx_eq_u64 v[2:3], v[4:5]", "s_and_saveexec_b64 s[4:5], vcc",
"s_or_saveexec_b32 s4, vcc_lo", "s_andn2_wrexec_b64 s[4:5], s[6:7]", "s_mov_b64 exec, -1",
"s_mov_b32 exec_lo, -1", "s_mov_b32 exec_hi, 0", "s_wqm_b64 exec, exec",
"s_and_b64 exec, exec, s[4:5]", "v_cmp_eq_u32_e64 exec, 0, v1"):
self.assertTrue(rdna2spirv.writes_exec(shader_ir.parse_line("\t" + line)), line)
for line in ("s_cmp_eq_u64 exec, 0", "s_bitcmp1_b32 exec_lo, 3", "s_mov_b64 s[4:5], exec",
"exp mrt0 v0, v1, v2, v3 done vm", "v_cndmask_b32_e64 v0, v1, v2, exec"):
self.assertFalse(rdna2spirv.writes_exec(shader_ir.parse_line("\t" + line)), line)
if __name__ == "__main__":
unittest.main()