mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 16:58:37 +00:00
docs/d3d12_vulkan.md §5.2 leaves the push-constant shape open and says to decide after measuring how many
real PSOs have two code-base stages; row 14 of the docs/boot.md checklist asks the same thing, and
tools/feature_scan.py's header says the pairing is in the executable, not in shaders.rpf. It is in shaders.rpf:
the pipeline library names a program per stage slot, and a stage's resource-layout blob ends with the HASH
digest of the container its code came from, so a stage joins to that program's `uses_code_base` in the
translation index through the container shader_carve.py carved.
The new tool reads the library, as an archive or as a saved library, joins
PSO -> stage slot -> program -> digest -> carved container -> spirv/index.json row, and prints counts only:
graphics PSOs by the number of code-base stages, those stages, the stage slots, the per-stage program counts
and the records it could not resolve. --out writes the same counts as JSON and refuses to name one of its own
inputs.
Measured on Xbox 1.0.2.1 (library 0x65F41EDD 930.048.020, 25,471 graphics records): no PSO has two code-base
stages. 25,027 have none and 444 exactly one - 343 MS, 60 PS, 41 VS - in a corpus of 13,383 VS+PS, 10,726
MS+PS, 1,336 MS, 24 VS and 2 VS+PS+slot 6; 21 records stay unknown because a stage's program is not in the
carve output. Its per-stage program counts (38 VS, 46 PS, 260 MS, 263 CS) are lower than the corpus's own
54 / 93 / 266 / 263: the library holds a second compiled form of 6,522 carved shaders and a program can serve
two slots.
So one 144-byte block with one code base at dwords 34-35 is enough for this package, and the runtime's refusal
of a pair where both stages read it can stand. docs/d3d12_vulkan.md §5.2 and its question 14, docs/boot.md
row 14 and docs/shader_library.md §4 and §6 say so; the stale pairing line in feature_scan.py's header is
corrected, and shader_library.py's header now says slot 6 is used by the 2 records that also have VS and PS.
tests/test_pso_code_base_pairs.py builds the library, the carve folder and both translation-index shapes from
bytes: 9 tests, no game data. The tool's __main__ block sets errors="replace" on stdout like every other tool,
which tests/test_tool_stdout_encoding.py checks. Also run: compileall, scripts/check_no_game_data.py,
test_shader_library.py (21) and test_text_write_line_endings.py (3). Whole-suite comparison, `python -I
scripts/run_semantic_tests.py tests -j 4` on Windows 11: plain 89d7dfcf5 reports 522 files, 8,099 tests, 42 files
failed; with this commit 523 files, 8,108 tests, 42 files failed - the two lists differ only by
test_run_wine_tests and test_build_batch_launch, which both pass alone on this machine and fail under -j 4 on a
loaded box. Those 42 are unchanged by this commit: 30 are pytest-based files that `python -I` cannot import and 6
more are script-style files the runner collects nothing from (ci.yml:87 runs both kinds elsewhere), plus the
Windows-only failures in test_world_names, test_bink_names, test_opus_tags, test_ci_registration and
test_mesh_alloc_single_writer. All of them are filed as findings on #14.
Claimed in the coordination issue as SHADER-CODEBASE-PAIRS (issue #14, comment 7146787349). AI-assisted.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
180 lines
8.7 KiB
Python
180 lines
8.7 KiB
Python
"""Unit tests for tools/pso_code_base_pairs.py (how many real PSOs pair two code-base stages).
|
|
|
|
python -I tests/test_pso_code_base_pairs.py
|
|
|
|
Zero game data: the pipeline library, the carved containers and the translation index are all built here from
|
|
synthetic bytes, using the builders in tests/test_shader_library.py.
|
|
"""
|
|
import contextlib
|
|
import io
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
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"))
|
|
|
|
import pso_code_base_pairs as pairs # noqa: E402
|
|
import shader_library as sl # noqa: E402
|
|
import test_shader_library as synth # noqa: E402
|
|
|
|
TOOL = Path(ROOT) / "tools" / "pso_code_base_pairs.py"
|
|
|
|
|
|
def library():
|
|
"""Three graphics PSOs and one compute record: VS+PS with both stages on a code base, MS+PS with only the
|
|
pixel stage on one, and an MS-only record whose program cannot be resolved."""
|
|
blobs = [synth.layout_blob(2, synth.D_PS), synth.layout_blob(2, synth.D_VS),
|
|
synth.layout_blob(2, synth.D_MS), synth.layout_blob(3, synth.D_CS), b"\x05" * 8]
|
|
programs = [synth.prog(tag) for tag in (0x11112222, 0x33334444, 0x55556666, 0x77778888)]
|
|
graphics = [(4, 4, {0: (0, 0), 1: (1, 1)}), # pixel and vertex, both on a code base
|
|
(4, 4, {0: (0, 0), 2: (2, 2)}), # mesh without, pixel with
|
|
(4, 4, {2: (3, 3)})] # the mesh program of the compute record too
|
|
return synth.build(graphics, [(4, 4, 3, 3)], blobs, programs)
|
|
|
|
|
|
def corpus(root: str, spirv_shape: str = "map"):
|
|
"""<work>/shaders: a carve folder with one container per digest and a translation index over those containers.
|
|
The mesh container says no code base; the compute container is missing from both sides."""
|
|
shaders = Path(root) / "shaders"
|
|
dxbc = shaders / "dxbc"
|
|
dxbc.mkdir(parents=True)
|
|
entries = []
|
|
for stem, digest, code_base in (("PS_ONE", synth.D_PS, True), ("VS_ONE", synth.D_VS, True),
|
|
("MS_ONE", synth.D_MS, False)):
|
|
(dxbc / f"{stem}.dxbc").write_bytes(synth.dxbc(digest))
|
|
entries.append({"file": f"{stem}.dxbc", "name": f"synthetic {stem.lower()}"})
|
|
(dxbc / "index.json").write_text(json.dumps(entries), encoding="utf-8")
|
|
spirv = shaders / "spirv"
|
|
spirv.mkdir()
|
|
rows = {"PS_ONE": {"uses_code_base": True}, "VS_ONE": {"uses_code_base": True},
|
|
"MS_ONE": {"uses_code_base": False}}
|
|
index = rows if spirv_shape == "map" else [dict(row, file=f"{stem}.dxbc") for stem, row in rows.items()]
|
|
(spirv / "index.json").write_text(json.dumps(index), encoding="utf-8")
|
|
return str(shaders)
|
|
|
|
|
|
class Join(unittest.TestCase):
|
|
def setUp(self):
|
|
self._tmp = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self._tmp.cleanup)
|
|
self.shaders = corpus(self._tmp.name)
|
|
self.lib = sl.parse(library())
|
|
self.code_base = pairs.read_spirv(os.path.join(self.shaders, "spirv", "index.json"))
|
|
self.digests = shader_carve_digests(os.path.join(self.shaders, "dxbc"))
|
|
|
|
def report(self):
|
|
return pairs.count_records(self.lib, pairs.code_base_flags(self.lib, self.digests, self.code_base))
|
|
|
|
def test_two_code_base_stages_are_counted_by_stage_set(self):
|
|
r = self.report()
|
|
self.assertEqual(r["stage_slots"], {"ps+vs": 1, "ms+ps": 1, "ms": 1})
|
|
self.assertEqual(r["psos_by_code_base_stages"], {"2": 1, "1": 1, "0": 1})
|
|
self.assertEqual(r["psos_with_two_code_base_stages"], {"ps+vs": 1})
|
|
|
|
def test_unresolved_stages_are_reported_separately(self):
|
|
# Program 3's layout digest is D_CS and no carved container carries it, so neither of the two records
|
|
# that name it can say whether that stage uses a code base.
|
|
r = self.report()
|
|
self.assertEqual(r["psos_unresolved"], {"ms": 1})
|
|
self.assertEqual(r["compute_records_by_code_base"], {"None": 1})
|
|
self.assertEqual(r["psos_by_code_base_stages"], {"2": 1, "1": 1, "0": 1})
|
|
|
|
def test_per_stage_program_counts_match_feature_scan(self):
|
|
r = self.report()
|
|
# Program 3 is the mesh stage of the last record and the compute record's program, so it is counted once,
|
|
# under the pair of stages it serves.
|
|
self.assertEqual(r["programs"], {"ps": 1, "vs": 1, "ms": 1, "cs+ms": 1})
|
|
self.assertEqual(r["programs_with_code_base"], {"ps": 1, "vs": 1})
|
|
self.assertEqual(r["psos_with_code_base_by_stage"], {"ps": 2, "vs": 1})
|
|
|
|
def test_a_container_carved_twice_counts_as_a_code_base_once(self):
|
|
dxbc = Path(self.shaders) / "dxbc"
|
|
(dxbc / "PS_TWICE.dxbc").write_bytes(synth.dxbc(synth.D_PS))
|
|
entries = json.loads((dxbc / "index.json").read_text(encoding="utf-8"))
|
|
entries.append({"file": "PS_TWICE.dxbc", "name": "synthetic ps again"})
|
|
(dxbc / "index.json").write_text(json.dumps(entries), encoding="utf-8")
|
|
index_path = Path(self.shaders) / "spirv" / "index.json"
|
|
rows = json.loads(index_path.read_text(encoding="utf-8"))
|
|
rows["PS_TWICE"] = {"uses_code_base": False} # the other copy says no
|
|
index_path.write_text(json.dumps(rows), encoding="utf-8")
|
|
self.digests = shader_carve_digests(str(dxbc))
|
|
self.code_base = pairs.read_spirv(str(index_path))
|
|
self.assertEqual(self.report()["psos_with_two_code_base_stages"], {"ps+vs": 1})
|
|
|
|
def test_list_shaped_translation_index_joins_by_file(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
shaders = corpus(tmp, spirv_shape="list")
|
|
code_base = pairs.read_spirv(os.path.join(shaders, "spirv", "index.json"))
|
|
self.assertEqual(code_base, {"PS_ONE": True, "VS_ONE": True, "MS_ONE": False})
|
|
flags = pairs.code_base_flags(self.lib, self.digests, code_base)
|
|
self.assertEqual(pairs.count_records(self.lib, flags)["psos_with_two_code_base_stages"], {"ps+vs": 1})
|
|
|
|
|
|
def shader_carve_digests(dxbc: str):
|
|
"""digest -> containers, over a carve folder, with the helper's own error lines captured."""
|
|
with contextlib.redirect_stderr(io.StringIO()):
|
|
return sl.carve_digests(dxbc, errors=[])
|
|
|
|
|
|
class CommandLine(unittest.TestCase):
|
|
def setUp(self):
|
|
self._tmp = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self._tmp.cleanup)
|
|
self.root = self._tmp.name
|
|
self.shaders = corpus(self.root)
|
|
|
|
def run_tool(self, library_path, *extra):
|
|
out = os.path.join(self.root, "pairs.json")
|
|
proc = subprocess.run([sys.executable, "-I", str(TOOL), self.shaders, "--library", library_path,
|
|
*extra, "--out", out], capture_output=True, text=True)
|
|
return proc, (json.loads(Path(out).read_text(encoding="utf-8")) if os.path.exists(out) else None)
|
|
|
|
def test_library_blob_and_archive_are_both_accepted(self):
|
|
lib = library()
|
|
raw = os.path.join(self.root, "library.bin")
|
|
Path(raw).write_bytes(lib)
|
|
packed = os.path.join(self.root, "shaders.rpf")
|
|
Path(packed).write_bytes(synth.archive(lib))
|
|
for path in (raw, packed):
|
|
with self.subTest(path=os.path.basename(path)):
|
|
proc, report = self.run_tool(path)
|
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
|
self.assertEqual(report["psos_with_two_code_base_stages"], {"ps+vs": 1})
|
|
self.assertIn("stage slots per graphics PSO", proc.stdout)
|
|
|
|
def test_report_names_no_shader(self):
|
|
raw = os.path.join(self.root, "library.bin")
|
|
Path(raw).write_bytes(library())
|
|
proc, report = self.run_tool(raw)
|
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
|
written = proc.stdout + json.dumps(report)
|
|
for name in ("PS_ONE", "VS_ONE", "MS_ONE", "ps one"):
|
|
self.assertNotIn(name, written)
|
|
|
|
def test_out_naming_an_input_is_refused(self):
|
|
raw = os.path.join(self.root, "library.bin")
|
|
Path(raw).write_bytes(library())
|
|
input_path = os.path.join(self.shaders, "spirv", "index.json")
|
|
proc = subprocess.run([sys.executable, "-I", str(TOOL), self.shaders, "--library", raw,
|
|
"--out", input_path], capture_output=True, text=True)
|
|
self.assertEqual(proc.returncode, 2)
|
|
self.assertIn("is an input", proc.stderr)
|
|
|
|
def test_missing_work_folder(self):
|
|
raw = os.path.join(self.root, "library.bin")
|
|
Path(raw).write_bytes(library())
|
|
proc = subprocess.run([sys.executable, "-I", str(TOOL), os.path.join(self.root, "nope"),
|
|
"--library", raw], capture_output=True, text=True)
|
|
self.assertEqual(proc.returncode, 2)
|
|
self.assertIn("does not exist", proc.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|