Files
mandy/tests/test_rdna2spirv_pixelinput.py
SuarezPM fdf4d29955 Translator: use the FSSM pixel-input table in the PS input path (SHADER-15)
AI-assisted (fixer lane, orchestrated). tools/fssm_header.py already decoded the pixel-input table; rdna2spirv.py ignored it.

- PS translate() reads the fssm/<name>.fssm sidecar (same convention as CS) and declares every table position 0..count-1 as a fragment input at that Location, including positions the code never reads.
- A 32-bit v_interp read (p1/p2/mov) of a 16-bit-flagged position (flag bit 4) is rounded f32->f16->f32; *_f16-opcode reads already narrow and are unchanged; reads past the table still declare; no sidecar (or empty table) is byte-identical to the old path.
- shader_meta() records ps_interpolants (count) and ps_fp16_interpolants (flagged positions).
- docs/design.md: contract-table row with corpus measurements and the fixture confirmation (synthetic + mini_interp, no GPU).

Verified: tests/test_rdna2spirv_pixelinput.py 9 passed (synthetic FSSM blobs + hand-written listings, empty DXBC; no llvm-mc/spirv-tools/GPU/game data); full suite 1066 passed + 366 subtests on the dev branch; compileall + make_game --help green. Claimed as SHADER-15 in #156 before starting.
2026-11-21 19:15:52 -03:00

222 lines
9.4 KiB
Python

import pytest
"""SHADER-15: the translator uses the FSSM pixel-input table (interpolant count, fp16 mask).
All blobs and shaders here are built by hand; no game data is used, and nothing needs
llvm-mc, spirv-as/val or a GPU: the .s listings are written directly in the disassembler
listing format, and values are checked with tests/semantic/spirv_mini_interp.py.
Rule under test (tools/rdna2spirv.py, PS input path):
* When a sidecar fssm/<name>.fssm next to the disasm directory holds a non-empty
pixel-input table (tools/fssm_header.py), every table position 0..count-1 is declared
as a fragment input at that Location, including positions the code never reads (the
table overcounts the code's distinct v_interp attrs in 775 of 12,101 corpus PS).
Position in the table is the v_interp attr number, not the record's slot byte.
* A 32-bit v_interp read (p1/p2/mov) of a position flagged 16-bit (flag bit 4) sees the
value rounded to f16 (round to nearest, and back to f32). Reads through the *_f16
opcodes already narrow to f16 and are unchanged.
* Without a sidecar, or with a sidecar holding no table, the output is byte-identical
to the old code-driven path (no new declarations, no quantization, no new metadata).
"""
import gc
import os
import struct
import sys
import tempfile
import warnings
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tools"))
sys.path.insert(0, os.path.join(ROOT, "tests", "semantic"))
import rdna2spirv # noqa: E402
from spirv_mini_interp import run_fragment # noqa: E402
# An empty DXBC container (no ISG1): pixel shaders that only read barycentrics through
# v_interp need no input signature.
STUB_DXBC = b"DXBC" + bytes(24) + struct.pack("<I", 0) + bytes(4)
FP16_FLAG = 0x10
def fssm_blob(records):
"""A synthetic FSSM header holding `records` = [(semantic, mask, index, slot, flags)]."""
data = bytearray(max(0x140, 0x138 + 8 * len(records)))
data[0:4] = b"FSSM"
for i, (semantic, mask, index, slot, flags) in enumerate(records):
struct.pack_into("<8B", data, 0x138 + 8 * i, 6, semantic, mask, index, 0, 0, slot, flags)
return bytes(data)
def write_case(tmp_path, name, body, fssm=None):
"""Write a synthetic shader case: disasm/<name>.s, <name>.dxbc and optionally fssm/<name>.fssm."""
disasm = tmp_path / "disasm"
disasm.mkdir(exist_ok=True)
(tmp_path / f"{name}.dxbc").write_bytes(STUB_DXBC)
if fssm is not None:
(tmp_path / "fssm").mkdir(exist_ok=True)
(tmp_path / "fssm" / f"{name}.fssm").write_bytes(fssm)
src = disasm / f"{name}.s"
src.write_text(f"; {name} synthetic\n" + body)
return str(src)
def translate(path):
with warnings.catch_warnings():
warnings.simplefilter("ignore", ResourceWarning) # translate() leaves its .dxbc handle to the GC
result = rdna2spirv.translate(path)
gc.collect()
return result
def mrt0_words(spvasm, **run_options):
out = run_fragment(spvasm, **run_options)
assert list(out) == ["%out_mrt0"], f"expected exactly one output, got {sorted(out)}"
return tuple(out["%out_mrt0"])
def f16_roundtrip_bits(word):
"""Independent oracle: f32 -> f16 (round to nearest even) -> f32, as bit patterns."""
as_float = struct.unpack("<f", struct.pack("<I", word))[0]
half = struct.unpack("<H", struct.pack("<e", as_float))[0]
back = struct.unpack("<e", struct.pack("<H", half))[0]
return struct.unpack("<I", struct.pack("<f", back))[0]
READ_ATTR0 = (
"\tv_interp_p1_f32_e32 v2, v0, attr0.x\n"
"\tv_interp_p2_f32_e32 v2, v1, attr0.x\n"
"\texp mrt0 v2, v2, v2, v2 done vm\n"
"\ts_endpgm\n"
)
READ_ATTR1 = (
"\tv_interp_p1_f32_e32 v2, v0, attr1.x\n"
"\tv_interp_p2_f32_e32 v2, v1, attr1.x\n"
"\texp mrt0 v2, v2, v2, v2 done vm\n"
"\ts_endpgm\n"
)
READ_ATTR0_THEN_ATTR1 = (
"\tv_interp_p1_f32_e32 v2, v0, attr0.x\n"
"\tv_interp_p2_f32_e32 v2, v1, attr0.x\n"
"\tv_interp_p1_f32_e32 v3, v0, attr1.y\n"
"\tv_interp_p2_f32_e32 v3, v1, attr1.y\n"
"\texp mrt0 v2, v3, v2, v3 done vm\n"
"\ts_endpgm\n"
)
MOV_ATTR1 = (
"\tv_interp_mov_f32_e32 v2, p0, attr1.x\n"
"\texp mrt0 v2, v2, v2, v2 done vm\n"
"\ts_endpgm\n"
)
def test_table_predeclares_unread_interpolants(tmp_path):
"""Positions the code never reads are still declared (Location = table position)."""
table = fssm_blob([(4, 0xF, 0, 0, 0), (4, 0xF, 1, 1, 0), (4, 0xF, 2, 2, 0)])
spv = translate(write_case(tmp_path, "ps_pix_table", READ_ATTR0, fssm=table))
lines = [line.strip() for line in spv.splitlines()]
for n in (0, 1, 2):
assert f"OpDecorate %in_attr{n} Location {n}" in lines
assert f"%in_attr{n} = OpVariable %_ptr_Input_v4 Input" in lines
assert "OpDecorate %in_attr0 Flat" not in lines # read through p1/p2: interpolated
assert "OpDecorate %in_attr1 Flat" in lines # never read: Flat and unused
assert "OpDecorate %in_attr2 Flat" in lines
def test_table_position_not_slot_is_the_attr_number(tmp_path):
"""The record's position is the v_interp attr number even when the slot bytes disagree."""
table = fssm_blob([(4, 0xF, 0, 7, 0), (4, 0xF, 1, 3, FP16_FLAG)])
spv = translate(write_case(tmp_path, "ps_pix_slots", READ_ATTR1, fssm=table))
assert "PackHalf2x16" in spv # attr1 = table position 1 = flagged, whatever its slot says
def test_fp16_read_rounds_to_nearest(tmp_path):
"""A 32-bit v_interp read of a flagged position sees the f16-rounded value."""
table = fssm_blob([(4, 0xF, 0, 0, 0), (4, 0xF, 1, 1, FP16_FLAG)])
spv = translate(write_case(tmp_path, "ps_pix_fp16", READ_ATTR1, fssm=table))
assert "PackHalf2x16" in spv
f32_one_point_one = struct.unpack("<I", struct.pack("<f", 1.1))[0]
want = f16_roundtrip_bits(f32_one_point_one)
assert want != f32_one_point_one # the fixture value is not f16-exact, so the check is meaningful
got = mrt0_words(spv, inputs={"%in_attr1": (1.1, 0.0, 0.0, 0.0)})
assert got == (want,) * 4
def test_fp16_mov_read_also_rounds(tmp_path):
"""v_interp_mov_f32 of a flagged position rounds too, and stays Flat."""
table = fssm_blob([(4, 0xF, 0, 0, 0), (4, 0xF, 1, 1, FP16_FLAG)])
spv = translate(write_case(tmp_path, "ps_pix_fmov", MOV_ATTR1, fssm=table))
lines = [line.strip() for line in spv.splitlines()]
assert "OpDecorate %in_attr1 Flat" in lines # mov-only: Flat, as before
f32_one_point_one = struct.unpack("<I", struct.pack("<f", 1.1))[0]
got = mrt0_words(spv, inputs={"%in_attr1": (1.1, 0.0, 0.0, 0.0)})
assert got == (f16_roundtrip_bits(f32_one_point_one),) * 4
def test_no_table_leaves_values_and_declarations_unchanged(tmp_path):
"""Control: without a sidecar there is no quantization and only code-read inputs exist."""
src = write_case(tmp_path, "ps_pix_ctrl", READ_ATTR1)
spv = translate(src)
assert "PackHalf2x16" not in spv
assert "%in_attr0 = OpVariable" not in spv
assert "%in_attr2 = OpVariable" not in spv
f32_one_point_one = struct.unpack("<I", struct.pack("<f", 1.1))[0]
got = mrt0_words(spv, inputs={"%in_attr1": (1.1, 0.0, 0.0, 0.0)})
assert got == (f32_one_point_one,) * 4 # exact f32, not rounded
def test_empty_table_is_the_old_path(tmp_path):
"""Control: a sidecar whose first byte is not 6 holds no table and changes nothing."""
import pathlib
src = write_case(tmp_path, "ps_pix_empty", READ_ATTR1, fssm=b"FSSM" + bytes(0x140 - 4))
with tempfile.TemporaryDirectory() as other:
plain = translate(write_case(pathlib.Path(other), "ps_pix_empty", READ_ATTR1))
assert translate(src) == plain
def test_agreeing_shader_is_byte_identical_with_table(tmp_path):
"""A shader reading exactly the table's attrs with no fp16 flags translates as before."""
table = fssm_blob([(4, 0xF, 0, 0, 0), (4, 0xF, 1, 1, 0)])
with_table = translate(write_case(tmp_path, "ps_pix_same", READ_ATTR0_THEN_ATTR1, fssm=table))
import pathlib
with tempfile.TemporaryDirectory() as other:
other = pathlib.Path(other)
without_table = translate(write_case(other, "ps_pix_same", READ_ATTR0_THEN_ATTR1))
assert with_table == without_table
def test_out_of_table_read_still_declares(tmp_path):
"""A read past the table declares an input instead of failing (no new rejections)."""
table = fssm_blob([(4, 0xF, 0, 0, 0), (4, 0xF, 1, 1, 0)])
body = (
"\tv_interp_p1_f32_e32 v2, v0, attr5.x\n"
"\tv_interp_p2_f32_e32 v2, v1, attr5.x\n"
"\texp mrt0 v2, v2, v2, v2 done vm\n"
"\ts_endpgm\n"
)
spv = translate(write_case(tmp_path, "ps_pix_beyond", body, fssm=table))
assert "OpDecorate %in_attr5 Location 5" in [line.strip() for line in spv.splitlines()]
def test_metadata_records_table(tmp_path):
"""shader_meta carries the interpolant count and the flagged positions; absent without a table."""
table = fssm_blob([(4, 0xF, 0, 0, 0), (4, 0xF, 1, 1, FP16_FLAG), (4, 0xF, 2, 2, 0)])
spv = translate(write_case(tmp_path, "ps_pix_meta", READ_ATTR0, fssm=table))
meta = rdna2spirv.shader_meta(rdna2spirv.translate.last_gen, spv)
assert meta["ps_interpolants"] == 3
assert meta["ps_fp16_interpolants"] == [1]
import pathlib
with tempfile.TemporaryDirectory() as other:
other = pathlib.Path(other)
spv_plain = translate(write_case(other, "ps_pix_meta", READ_ATTR0))
meta_plain = rdna2spirv.shader_meta(rdna2spirv.translate.last_gen, spv_plain)
assert "ps_interpolants" not in meta_plain
assert "ps_fp16_interpolants" not in meta_plain