"""The pipeline state list: the back end's records (gpu/vk/vk_draw.cpp StateRec, StateRec2) and the tools that merge and ship the lists agree. A list made by one build is read by another (that is its point): its record size and magic are a file format. nx2/pipeline_states.py merges lists into work/pipeline_states.bin, each pipeline once, and nx2/make_game.py puts that beside the game. Version 1 lists are converted to version 2 when merged with one (or with --v2): each Vulkan format back to an NVN format (V1_NVN, checked against vk_formats.cpp, nvn_api.h and the Vulkan headers when given), a mesh record made with the mesh fallback numbered as the native one. Synthetic data only. python -I -B tests/test_pipeline_states.py [--headers ] """ import argparse import importlib.util import os import re import struct import subprocess import sys import tempfile from pathlib import Path RUN_ALL_ARGS = ["--headers", "{vulkan_headers}"] ROOT = Path(__file__).resolve().parents[1] def load(name, path): spec = importlib.util.spec_from_file_location(name, path) mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod def check_v1_table(tool, headers): """V1_NVN: each Vulkan format back to an NVN format vk_format (gpu/vk/vk_formats.cpp) makes it, by the numbers in nvn_api.h and the Vulkan headers (when given: RUN_ALL_ARGS)""" formats = (ROOT / "gpu" / "vk" / "vk_formats.cpp").read_text(encoding="utf-8") nvn_h = (ROOT / "gpu" / "render" / "nvn_api.h").read_text(encoding="utf-8") nvn = {m.group(1): int(m.group(2), 0) for m in re.finditer(r"(NVN_FORMAT_\w+)\s*=\s*(0x[0-9a-fA-F]+|\d+)", nvn_h)} to_vk = {nvn[n]: v for n, v in re.findall(r"case (NVN_FORMAT_\w+): return \{(VK_FORMAT_\w+),", formats)} core = (Path(headers) / "vulkan" / "vulkan_core.h").read_text(encoding="utf-8") if headers else None for vk_value, nvn_value in tool.V1_NVN.items(): name = to_vk.get(nvn_value) if not name or "BLOCK" in name: raise SystemExit("V1_NVN: NVN %#x is no target format in vk_formats.cpp" % nvn_value) if core: value = int(re.search(r"\b%s = (\d+)," % name, core).group(1)) if value != vk_value: raise SystemExit("V1_NVN: %d is not %s (%d)" % (vk_value, name, value)) targets = {v for v in to_vk.values() if "BLOCK" not in v} if len(tool.V1_NVN) != len(targets): raise SystemExit("V1_NVN has %d formats, vk_formats.cpp %d a target can be" % (len(tool.V1_NVN), len(targets))) def check_versions(tool): """Version 2 records; a version 1 list converted (formats back to NVN, a fallback mesh record numbered as the native one); a merge of both versions; a compute record (another build's kind) read and kept""" def v1(colors, depth, mesh=0, topo=3, task=0, answers=()): r = bytearray(tool.SIZE) struct.pack_into("<4Q", r, 0, 0x11, 0x22, task, 0) struct.pack_into(" v2: formats %s, depth %#x" % (f["color"], f["depth"])) if tool.v1_to_v2(v1([1000156000], 0)) is not None: # (a format no target can be: not converted) raise SystemExit("v1 -> v2: a format with no NVN twin was converted") # a mesh record (no task) made with the fallback: fragment answers at stage 2 (after the generated vertex stage), points frag_fb, frag = (2 << 28) | (1 << 12) | 2, (1 << 28) | (1 << 12) | 2 mesh_ans = (0 << 28) | 1 fb = v2_fields(tool.v1_to_v2(v1([37], 0, mesh=1, topo=0, answers=(mesh_ans, frag_fb)))) native = v2_fields(tool.v1_to_v2(v1([37], 0, mesh=1, topo=3, answers=(mesh_ans, frag)))) if fb != native or fb["topo"] != 3 or fb["answers"] != (mesh_ans, frag): raise SystemExit("v1 -> v2: a fallback mesh record is not the native one: %s / %s" % (fb, native)) if tool.v1_to_v2(v1([37], 0, mesh=1, topo=0)) is not None: # (the fallback's, its answers left out) raise SystemExit("v1 -> v2: a fallback record without answers was converted") # a compute program's record (a later build's kind): sound, kept by the merge, no point list; one holding more is not sound comp = tool.compute_record(0x77) if tool.record_faults(comp) or tool.is_point(comp) or not tool.record_faults(comp[:40] + b"\x01" + comp[41:]): raise SystemExit("a compute record: %s" % tool.record_faults(comp)) if tool.record_faults(a) or tool.record_faults(a[:-8] + struct.pack("<2I", 7, 0)) != ["kind 7"]: raise SystemExit("a version 2 record's kind: not checked") tool_py = str(ROOT / "nx2" / "pipeline_states.py") with tempfile.TemporaryDirectory() as d: p1, p2, out = (os.path.join(d, n) for n in ("v1.bin", "v2.bin", "out.bin")) open(p1, "wb").write(tool.MAGIC + struct.pack("