mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 18:58:37 +00:00
Merges main at1154976d4into the integration branch at273b3bdf9(merge baseece4ba5b3). 42 files conflicted; this commit is the resolution only. Build and test repairs follow in their own commits. Rule of resolution: where both sides added different things, both stay; where both made the same change in two forms, main's reviewed form stays and whatever #984's form did in addition is carried into it; where the two changed the same code for different purposes, both purposes are kept. Xbox proxy code (runtime/shims/d3d12_xs_device.cpp, d3d12_xs_device.h, d3d12_xs.cpp, d3d12_xs_alloc_size.h, d3d12_xs_desc_clean.h, registry.cpp): - d3d12_xs_device.cpp is #984's file with main's differing pieces ported in: env_slot and the configurable device routes, XboxQueueSlotRoutes, PresentationHandler / set_presentation_handler, the console caller rule for device slots 61 and 74 (#383, #385), route_xbox_device_slot, slot 81 as GetFrameStatisticsX, create_host_device, the console-form rule for slots 25 and 56, the console list IID of #359, and the forms of queue slot 18 (#403) and list slot 139 (#396). - Kept from #984: the list proxy with handle translation, queue slots 16/17 as Signal/Wait, the fence's completed-value mirror, the shared FrameTokenSource, PresentX through queue_present_x when no handler is attached (slot 30, or 32 on a queue detected as the newer layout), slots 27/53/55/57/70/71/133, the wider pc_resource_desc, S_OK for queue slots past the PC table. - Dropped: #984's GetFrameEventsX and LEONIDA_XS_FRAME_EVENTS_SLOT, its second-word rule for slot 61, its undetected-layout rule for queue slot 18, its list IID mapping to GraphicsCommandList6; main's copy of the base list proxy, its null-view slot 70 and its refusing slot 133. Proxy tests (runtime/test/xs_proxy_mock_test.cpp, device_proxy_test.cpp, queue_proxy_test.cpp): - Checks of both sides, each run once; main's forms of the render-target size and layout checks with #984's extra cases added; main's --env-slots-74 and #984's --env-slots-70. - Adapted to the merged code: the queue tail answers S_OK, slot 73 answers an unknown IID with the list proxy, the queue slot 18 case installs a console caller. Dropped: #984's slot 81 GetFrameEventsX cases. D3D12/Vulkan layer (runtime/shims/d3d12_com.cpp, d3d12_vulkan_device.cpp, runtime/src/command_list.h, frame_pacer.h, shader_library.h, vulkan_backend.h, vulkan_replay.h, runtime/test/command_list_test.cpp, d3d12_close_parity_test.cpp, d3d12_execute_indirect_validation_test.cpp, replay_texture_barrier_test.cpp, vk_pipeline_check.cpp, vk_test_device.h): - Resource::create_texture: #984's title images (deferred placed, pending streamed, title pool) and main's ordinary textures (#1413 shared blocks, #1364 staging fallback) are exclusive branches chosen by the texture's origin, so both pools run on disjoint sets of textures. - frame_pacer.h is main's pacer (#402) with #984's FrameTokenSource as the token counter. - main's forms kept for ExecuteIndirect (#1066, #377), the barrier into LAYOUT_UNDEFINED (#1055), the B4G4R4A4 stand-in, per-face stencil masks, the unaligned tile staging (#528) and the device-lost flag (#980). - shader_library.h: main's block inflate; #984's `bulk` parameter now defaults to true so main's callers keep it. - Dropped: #984's B8G8R8A8 stand-in for B4G4R4A4 targets, its refusal of separate stencil masks, its zero-count refusal in ExecuteIndirect, its wider UNDEFINED -> UNDEFINED release and the test case for it. Other shims (runtime/shims/dstorage.cpp, file_system.cpp, gameinput.cpp, xgameruntime_offline.cpp, runtime/test/xgameruntime_offline_test.cpp, xmem_test.cpp): - ReadFile and DirectStorage reads: #984's pinned kernel_writes read, then main's touch_pages retry on ERROR_NOACCESS. main's rt::fs::read_file is folded into #984's read_file. - gameinput: main's strict LEONIDA_INPUT_SCRIPT parser with #984's rsup/rsdown entries. - Speech synthesis: main's one silent voice and handle checks; #984's accept-anything form dropped. Build, tools, docs (runtime/CMakeLists.txt, runtime/cmake/translator_deps.cmake, scripts/build_runtime.ps1, make_game.py, tools/rdna2spirv.py, shader_disasm.py, shader_ir.py, shader_library.py, runtime/test/shaders/cs_imgload_tfe.asm, cs_imgload_d16_tfe.asm, docs/README.md, boot.md, d3d12_vulkan.md, runtime/README.md): - Unions of the test, target and source lists; spirv_loop_guard.py added to translator_deps.cmake. - rdna2spirv.py: main's DPP compare rule (#957), SMEM vcc base (#647) and widen_wave64_masks (#1021); #984's restore_skipped_instructions and restore_dot4_inline_sel. #984's duplicate widen_wave64_valu_masks dropped. - shader_disasm.py and shader_ir.py: main's structure with #984's one-line-per-dword llvm-mc input and wave flags. - shader_library.py: main's structure with #984's --compute-hashes and per-state compute variants. One file that did not conflict is changed here: tools/rdna2_decode.py. Git kept both sides' decoders for a VOPC compare with a DPP operand, and #984's narrower one shadowed main's; it is main's file again. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
596 lines
30 KiB
Python
596 lines
30 KiB
Python
"""Unit tests for tools/shader_library.py (the pipeline library in the zlib part of shaders.rpf).
|
|
|
|
python -I tests/test_shader_library.py
|
|
|
|
Zero game data: every library and archive is built here from synthetic bytes.
|
|
"""
|
|
import contextlib
|
|
import hashlib
|
|
import io
|
|
import json
|
|
import os
|
|
import struct
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
import zlib
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
sys.path.insert(0, os.path.join(ROOT, "tools"))
|
|
|
|
import shader_library as sl # noqa: E402
|
|
import shader_meta # noqa: E402
|
|
|
|
ENDPGM = struct.pack("<I", 0xBF810000)
|
|
CODE_END = struct.pack("<I", 0xBF9F0000)
|
|
V_MOV = struct.pack("<I", 0x7E000280)
|
|
NONE = 0xFFFFFFFF
|
|
|
|
|
|
def prologue(tag):
|
|
"""Three 12-byte VOP3 opcode-0x180 words with literals: the shape every library program starts with."""
|
|
out = b""
|
|
for lo in (tag & 0xFFFF, (tag >> 16) & 0xFFFF, 0):
|
|
out += struct.pack("<III", 0xD5800000 | lo, 0x03FE0100, 0x00000AC0)
|
|
return out
|
|
|
|
|
|
def prog(tag, body=2):
|
|
code = prologue(tag) + V_MOV * body + ENDPGM
|
|
return code + CODE_END * ((-len(code)) % 256 // 4)
|
|
|
|
|
|
def kind0(threads=(8, 8, 1), rsrc2=0xB8A):
|
|
x, y, z = threads
|
|
return struct.pack("<7I", 0x4005CF00, 0x6C090102, 0, x | y << 16, z, 0x402C0343, rsrc2)
|
|
|
|
|
|
def layout_blob(kind, digest, records=1):
|
|
"""A resource-layout blob: head (16 bytes for a graphics stage, 24 for compute) ending in a 0x40-tagged word,
|
|
32-byte 0x41/0x42 records, then 0x43, the 16-byte digest and 12 bytes of padding."""
|
|
head = struct.pack("<I", kind) + bytes(sl.STAGE_HEAD[kind] - 8) + struct.pack("<I", 0x00030440)
|
|
recs = b"".join(struct.pack("<I", 0x41 + (r & 1)) + bytes(16) + struct.pack("<I", 0x43) + bytes(8)
|
|
for r in range(records)) # a 0x43 inside a record must not end the walk
|
|
return head + recs + struct.pack("<I", 0x43) + digest + bytes(12)
|
|
|
|
|
|
def dxbc(digest, extra=b""):
|
|
"""A DXBC container with a HASH part (flags + 16-byte digest) and one more part."""
|
|
parts = [(b"HASH", struct.pack("<I", 0) + digest), (b"SFI0", extra or bytes(8))]
|
|
off = 32 + 4 * len(parts)
|
|
table, body = b"", b""
|
|
for tag, data in parts:
|
|
table += struct.pack("<I", off + len(body))
|
|
body += tag + struct.pack("<I", len(data)) + data
|
|
total = off + len(body)
|
|
return b"DXBC" + bytes(16) + struct.pack("<III", 1, total, len(parts)) + table + body
|
|
|
|
|
|
D_PS, D_VS, D_MS, D_CS = (bytes([n]) * 16 for n in (1, 2, 3, 4))
|
|
|
|
|
|
def build(graphics, compute, blobs, programs, name=b"0x00000000", version=b"1.0"):
|
|
"""graphics: [(state, layout, {slot: (program, blob)})]; compute: [(state, blob, program, layout)]."""
|
|
head = bytearray(sl.HEADER)
|
|
head[:4] = sl.MAGIC
|
|
head[4:4 + len(name)] = name
|
|
head[0x24:0x24 + len(version)] = version
|
|
struct.pack_into("<Q", head, 0x48, 0x0123456789ABCDEF)
|
|
blob_data, blob_tab = b"", b""
|
|
for x in blobs:
|
|
blob_tab += struct.pack("<II", len(blob_data), len(x))
|
|
blob_data += x
|
|
code, prog_tab = b"", b""
|
|
for x in programs:
|
|
code += b"\0" * ((-len(code)) % 256)
|
|
prog_tab += struct.pack("<II", len(code), len(x))
|
|
code += x
|
|
struct.pack_into("<6I", head, 0x50, len(graphics), len(compute), len(blobs), len(programs), len(blob_data),
|
|
len(code))
|
|
gfx = b""
|
|
for i, (state, layout, stages) in enumerate(graphics):
|
|
pairs = []
|
|
for slot in range(7):
|
|
pairs += list(stages.get(slot, (NONE, NONE)))
|
|
gfx += struct.pack("<QII", 0x1000 + i, state, layout) + bytes(40) + struct.pack("<14I", *pairs)
|
|
cs = b"".join(struct.pack("<QIIII", 0x2000 + i, *c) for i, c in enumerate(compute))
|
|
return bytes(head) + gfx + cs + blob_tab + prog_tab + blob_data + code
|
|
|
|
|
|
def sample():
|
|
blobs = [kind0(), b"\x01" * 20, layout_blob(3, D_CS, 3), b"\x03" * 388, b"POSITION\0",
|
|
layout_blob(2, D_PS), layout_blob(2, D_VS, 2), layout_blob(2, D_MS, 0)]
|
|
programs = [prog(0x11112222), prog(0x33334444, 5), prog(0x55556666), prog(0x77778888, 3)]
|
|
graphics = [(3, 4, {0: (0, 5), 1: (1, 6)}), # VS + PS
|
|
(3, 4, {0: (0, 5), 2: (2, 7)})] # MS + PS, the PS shared
|
|
compute = [(0, 1, 3, 2)]
|
|
return build(graphics, compute, blobs, programs)
|
|
|
|
|
|
def archive(lib, junk=b"\x78\x9c not a stream " * 50):
|
|
"""An archive-like file: unrelated bytes (with false zlib headers), then the library as 64 KiB zlib blocks with
|
|
small gaps between them, as in shaders.rpf."""
|
|
out = bytearray(b"9FPR" + bytes(60) + junk)
|
|
for k in range(0, len(lib), sl.BLOCK):
|
|
out += zlib.compress(lib[k:k + sl.BLOCK], 9)
|
|
out += b"\0" * (k // sl.BLOCK % 13)
|
|
return bytes(out)
|
|
|
|
|
|
class Parse(unittest.TestCase):
|
|
def test_header_and_tables(self):
|
|
b = sample()
|
|
lib = sl.parse(b)
|
|
h = lib["header"]
|
|
self.assertEqual((h["graphics"], h["compute"], h["blobs"], h["programs"]), (2, 1, 8, 4))
|
|
self.assertEqual(h["name"], "0x00000000")
|
|
self.assertEqual(lib["layout"]["end"], len(b))
|
|
g0, g1 = lib["graphics"]
|
|
self.assertEqual(g0["stages"], {"ps": {"program": 0, "blob": 5}, "vs": {"program": 1, "blob": 6}})
|
|
self.assertEqual(set(g1["stages"]), {"ps", "ms"})
|
|
self.assertEqual(lib["compute"][0]["program"], 3)
|
|
self.assertEqual({k: sorted(v) for k, v in lib["stage"].items()},
|
|
{0: ["ps"], 1: ["vs"], 2: ["ms"], 3: ["cs"]})
|
|
self.assertEqual(sl.program(b, lib, 1)[:36], prologue(0x33334444))
|
|
self.assertEqual(sl.blob(b, lib, 4), b"POSITION\0")
|
|
self.assertEqual(lib["digest"], {0: {D_PS}, 1: {D_VS}, 2: {D_MS}, 3: {D_CS}})
|
|
|
|
def test_stage_digest(self):
|
|
self.assertEqual(sl.stage_digest(layout_blob(2, D_PS, 4)), D_PS)
|
|
self.assertEqual(sl.stage_digest(layout_blob(3, D_CS, 0)), D_CS)
|
|
self.assertIsNone(sl.stage_digest(b""))
|
|
self.assertIsNone(sl.stage_digest(layout_blob(2, D_PS)[:-20])) # cut before the digest ends
|
|
bad = bytearray(layout_blob(2, D_PS))
|
|
bad[12] = 0x3F # head not closed by a 0x40 word
|
|
self.assertIsNone(sl.stage_digest(bytes(bad)))
|
|
bad = bytearray(layout_blob(3, D_CS, 1))
|
|
bad[24] = 0x44 # unknown record tag
|
|
self.assertIsNone(sl.stage_digest(bytes(bad)))
|
|
self.assertIsNone(sl.stage_digest(b"\x05" + layout_blob(2, D_PS)[1:])) # unknown kind
|
|
|
|
def test_rejects(self):
|
|
b = sample()
|
|
with self.assertRaises(sl.LibraryError):
|
|
sl.parse(b"XX" + b[2:])
|
|
with self.assertRaises(sl.LibraryError):
|
|
sl.parse(b + b"\0") # size must match the header exactly
|
|
bad = bytearray(b) # a stage naming a program past the table
|
|
struct.pack_into("<I", bad, sl.HEADER + 56, 99)
|
|
with self.assertRaises(sl.LibraryError):
|
|
sl.parse(bytes(bad))
|
|
bad = bytearray(b) # a program entry ending past the code section
|
|
lay = sl.layout(sl.header(b))
|
|
struct.pack_into("<I", bad, lay["program_table"] + 4, 1 << 20)
|
|
with self.assertRaises(sl.LibraryError):
|
|
sl.parse(bytes(bad))
|
|
|
|
def test_compute_with_missing_program_or_blob(self):
|
|
# A compute record may name no program or no layout blob (0xFFFFFFFF, "a missing stage is program
|
|
# 0xFFFFFFFF"): it must not crash parse(), take a "cs" stage, or link a digest to a missing program.
|
|
blobs = [kind0(), layout_blob(3, D_CS)]
|
|
programs = [prog(0x11112222)]
|
|
b = build([], [(0, 0, NONE, NONE), (0, NONE, 0, NONE)], blobs, programs)
|
|
lib = sl.parse(b)
|
|
self.assertEqual([c["program"] for c in lib["compute"]], [None, 0])
|
|
self.assertEqual(lib["compute"][0]["layout"], None)
|
|
self.assertEqual(lib["stage"], {0: {"cs"}})
|
|
self.assertEqual(lib["digest"], {})
|
|
|
|
def test_find_in_archive(self):
|
|
lib = sample() + b""
|
|
# make it span several 64 KiB blocks
|
|
big = build([], [(0, 0, 0, 0)], [kind0()], [prog(1, 40000)])
|
|
for b in (lib, big):
|
|
self.assertEqual(sl.find_library(archive(b)), b)
|
|
with self.assertRaises(sl.LibraryError):
|
|
sl.find_library(archive(big)[:-200]) # the last block cut off: size no longer matches
|
|
with self.assertRaises(sl.LibraryError):
|
|
sl.find_library(b"9FPR" + bytes(4096))
|
|
|
|
|
|
class Main(unittest.TestCase):
|
|
def run_main(self, *args):
|
|
out = io.StringIO()
|
|
with contextlib.redirect_stdout(out):
|
|
rc = sl.main(list(args))
|
|
return rc, out.getvalue()
|
|
|
|
def assert_save_overlap(self, src, out, save, *args):
|
|
err = io.StringIO()
|
|
with contextlib.redirect_stderr(err), self.assertRaises(SystemExit) as caught:
|
|
self.run_main(src, "--out", out, "--save", save, *args)
|
|
self.assertEqual(caught.exception.code, 2)
|
|
self.assertIn("--save overlaps an --out path", err.getvalue())
|
|
self.assertIn(save, err.getvalue())
|
|
|
|
def test_save_cannot_create_an_extraction_output(self):
|
|
for parts in ((), ("index.json",), ("library.json",), ("fspx",),
|
|
("fspx", "lib_ps_00000.code"), ("fspx", "lib_ps_00000.json"),
|
|
("fspx", "lib_cs_00003.state"), ("fspx", "lib_ps_00000.state"),
|
|
("disasm",), ("disasm", "lib_ps_00000.s")):
|
|
with self.subTest(parts=parts), tempfile.TemporaryDirectory() as tmp:
|
|
src = os.path.join(tmp, "shaders.rpf")
|
|
with open(src, "wb") as f:
|
|
f.write(archive(sample()))
|
|
out = os.path.join(tmp, "out")
|
|
args = ("--disasm",) if parts and parts[0] == "disasm" else ()
|
|
self.assert_save_overlap(src, out, os.path.join(out, *parts), *args)
|
|
self.assertEqual(os.listdir(tmp), ["shaders.rpf"])
|
|
|
|
def test_save_overlap_leaves_existing_outputs_intact(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
src = os.path.join(tmp, "lib.bin")
|
|
with open(src, "wb") as f:
|
|
f.write(sample())
|
|
out = os.path.join(tmp, "out")
|
|
fspx = os.path.join(out, "fspx")
|
|
os.makedirs(fspx)
|
|
index = os.path.join(out, "index.json")
|
|
code = os.path.join(fspx, "lib_ps_00000.code")
|
|
for path, contents in ((index, b"old index"), (code, b"old code")):
|
|
with open(path, "wb") as f:
|
|
f.write(contents)
|
|
|
|
aliases = [index, os.path.join("out", "fspx", "..", ".", "index.json"), code]
|
|
case_alias = os.path.join(out, "INDEX.JSON")
|
|
if os.path.exists(case_alias):
|
|
aliases.append(case_alias)
|
|
for save in aliases:
|
|
with self.subTest(save=save), contextlib.chdir(tmp):
|
|
self.assert_save_overlap(src, out, save)
|
|
self.assertEqual(sorted(os.listdir(out)), ["fspx", "index.json"])
|
|
self.assertEqual(os.listdir(fspx), ["lib_ps_00000.code"])
|
|
with open(index, "rb") as f:
|
|
self.assertEqual(f.read(), b"old index")
|
|
with open(code, "rb") as f:
|
|
self.assertEqual(f.read(), b"old code")
|
|
|
|
def test_save_overlap_through_symlink_or_hardlink(self):
|
|
for link in (os.symlink, os.link):
|
|
with self.subTest(link=link.__name__), tempfile.TemporaryDirectory() as tmp:
|
|
src = os.path.join(tmp, "lib.bin")
|
|
with open(src, "wb") as f:
|
|
f.write(sample())
|
|
out = os.path.join(tmp, "out")
|
|
os.mkdir(out)
|
|
index = os.path.join(out, "index.json")
|
|
with open(index, "wb") as f:
|
|
f.write(b"old index")
|
|
save = os.path.join(tmp, "alias")
|
|
try:
|
|
link(index, save)
|
|
except (OSError, NotImplementedError) as e:
|
|
self.skipTest(f"{link.__name__} unavailable: {e}")
|
|
self.assert_save_overlap(src, out, save)
|
|
self.assertEqual(os.listdir(out), ["index.json"])
|
|
for path in (index, save):
|
|
with open(path, "rb") as f:
|
|
self.assertEqual(f.read(), b"old index")
|
|
|
|
def test_save_cannot_alias_an_out_ancestor_directory(self):
|
|
for precreate in (False, True):
|
|
with self.subTest(precreate=precreate), tempfile.TemporaryDirectory() as tmp:
|
|
src = os.path.join(tmp, "lib.bin")
|
|
with open(src, "wb") as f:
|
|
f.write(sample())
|
|
parent = os.path.join(tmp, "parent")
|
|
if precreate:
|
|
with open(parent, "wb") as f:
|
|
f.write(b"sentinel")
|
|
out = os.path.join(parent, "nested")
|
|
self.assert_save_overlap(src, out, parent)
|
|
if precreate:
|
|
with open(parent, "rb") as f:
|
|
self.assertEqual(f.read(), b"sentinel")
|
|
else:
|
|
self.assertEqual(os.listdir(tmp), ["lib.bin"])
|
|
|
|
def test_save_can_write_noncolliding_file_inside_fresh_out_tree(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
src = os.path.join(tmp, "lib.bin")
|
|
with open(src, "wb") as f:
|
|
f.write(sample())
|
|
out = os.path.join(tmp, "out")
|
|
save = os.path.join(out, "fspx", "raw.bin")
|
|
rc, _ = self.run_main(src, "--out", out, "--save", save)
|
|
self.assertEqual(rc, 0)
|
|
with open(save, "rb") as f:
|
|
self.assertEqual(f.read(), sample())
|
|
self.assertTrue(os.path.isfile(os.path.join(out, "index.json")))
|
|
|
|
def test_save_can_use_a_filtered_program_path(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
src = os.path.join(tmp, "lib.bin")
|
|
with open(src, "wb") as f:
|
|
f.write(sample())
|
|
out = os.path.join(tmp, "out")
|
|
fspx = os.path.join(out, "fspx")
|
|
os.makedirs(fspx)
|
|
save = os.path.join(fspx, "lib_ps_00000.code")
|
|
rc, _ = self.run_main(src, "--out", out, "--stages", "vs", "--save", save)
|
|
self.assertEqual(rc, 0)
|
|
with open(save, "rb") as f:
|
|
self.assertEqual(f.read(), sample())
|
|
self.assertEqual(sorted(os.listdir(fspx)), ["lib_ps_00000.code", "lib_vs_00001.code", "lib_vs_00001.json"])
|
|
|
|
def test_unpack_layout(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
src = os.path.join(tmp, "shaders.rpf")
|
|
with open(src, "wb") as f:
|
|
f.write(archive(sample()))
|
|
out = os.path.join(tmp, "out")
|
|
other = os.path.join(tmp, "other")
|
|
os.mkdir(other)
|
|
save = os.path.join(other, "saved.json")
|
|
rc, text = self.run_main(src, "--out", out, "--save", save)
|
|
self.assertEqual(rc, 0)
|
|
self.assertIn("2 graphics + 1 compute pipelines, 4 programs; 4 written", text)
|
|
with open(save, "rb") as f:
|
|
self.assertEqual(f.read(), sample())
|
|
with open(os.path.join(out, "index.json"), encoding="utf-8") as f:
|
|
index = json.load(f)
|
|
self.assertEqual([e["file"] for e in index],
|
|
["lib_ps_00000.dxbc", "lib_vs_00001.dxbc", "lib_ms_00002.dxbc", "lib_cs_00003.dxbc"])
|
|
cs = index[3]
|
|
self.assertEqual((cs["stage"], cs["threads"], cs["wave32"]), ("cs", [8, 8, 1], False))
|
|
with open(os.path.join(out, "fspx", "lib_cs_00003.json"), encoding="utf-8") as f:
|
|
meta = json.load(f)
|
|
self.assertEqual((meta["rsrc2"], meta["user_sgprs"], meta["code"]["start"]), (0xB8A, 5, 36))
|
|
self.assertTrue(meta["code"]["prologue"])
|
|
with open(os.path.join(out, "fspx", "lib_vs_00001.code"), "rb") as f:
|
|
code = f.read()
|
|
self.assertEqual(code[36:36 + 20], V_MOV * 5)
|
|
self.assertEqual(meta["code"]["end"], 36 + 3 * 4 + 4)
|
|
with open(os.path.join(out, "library.json"), encoding="utf-8") as f:
|
|
summary = json.load(f)
|
|
self.assertEqual(summary["graphics"][1]["stages"], {"ps": "lib_ps_00000", "ms": "lib_ms_00002"})
|
|
self.assertEqual(summary["compute"][0]["program"], "lib_cs_00003")
|
|
# The compute program gets its record's state blob as kind 0, which the run time finds a slot-67
|
|
# pipeline by (fspx_state_sha256); a graphics program gets none.
|
|
with open(os.path.join(out, "fspx", "lib_cs_00003.state"), "rb") as f:
|
|
self.assertEqual(f.read(), kind0())
|
|
self.assertFalse(os.path.exists(os.path.join(out, "fspx", "lib_vs_00001.state")))
|
|
disasm_file = os.path.join(out, "disasm", "lib_cs_00003.s")
|
|
self.assertEqual(shader_meta.fspx_state_sha256_file(disasm_file, "lib_cs_00003"),
|
|
hashlib.sha256(kind0()).hexdigest())
|
|
self.assertEqual(shader_meta.fspx_code_sha256_file(disasm_file, "lib_cs_00003"),
|
|
hashlib.sha256(sl.program(sample(), sl.parse(sample()), 3)).hexdigest())
|
|
|
|
def test_stage_filter_and_saved_input(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
src = os.path.join(tmp, "lib.bin")
|
|
with open(src, "wb") as f:
|
|
f.write(sample())
|
|
out = os.path.join(tmp, "out")
|
|
rc, text = self.run_main(src, "--out", out, "--stages", "cs,vs")
|
|
self.assertEqual(rc, 0)
|
|
self.assertEqual(sorted(os.listdir(os.path.join(out, "fspx"))),
|
|
["lib_cs_00003.code", "lib_cs_00003.json", "lib_cs_00003.state", "lib_vs_00001.code",
|
|
"lib_vs_00001.json"])
|
|
|
|
def test_compute_missing_state_removes_stale_state_and_skips_none_blob(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = os.path.join(tmp, "out")
|
|
os.makedirs(os.path.join(out, "fspx"))
|
|
stale = os.path.join(out, "fspx", "lib_cs_00000.state")
|
|
with open(stale, "wb") as f:
|
|
f.write(b"stale state")
|
|
src = os.path.join(tmp, "lib.bin")
|
|
# Compute record with NONE state (0xFFFFFFFF) must not crash or keep a stale .state sidecar.
|
|
with open(src, "wb") as f:
|
|
f.write(build([], [(NONE, NONE, 0, NONE)], [], [prog(0x11112222)]))
|
|
rc, _ = self.run_main(src, "--out", out, "--stages", "cs")
|
|
self.assertEqual(rc, 0)
|
|
self.assertFalse(os.path.exists(stale))
|
|
disasm_file = os.path.join(out, "disasm", "lib_cs_00000.s")
|
|
self.assertIsNone(shader_meta.fspx_state_sha256_file(disasm_file, "lib_cs_00000"))
|
|
meta = shader_meta.collect(disasm_file, "lib_cs_00000", [[]], [[]])
|
|
self.assertIn("fspx_code_sha256", meta)
|
|
self.assertNotIn("fspx_state_sha256", meta)
|
|
|
|
def test_same_code_with_distinct_states_gets_distinct_state_keys(self):
|
|
code = prog(0x44556677)
|
|
s_a, s_b = kind0(threads=(32, 1, 1)), kind0(threads=(64, 1, 1))
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
src = os.path.join(tmp, "lib.bin")
|
|
with open(src, "wb") as f:
|
|
f.write(build([], [(0, NONE, 0, NONE), (1, NONE, 1, NONE)], [s_a, s_b], [code, code]))
|
|
out = os.path.join(tmp, "out")
|
|
rc, _ = self.run_main(src, "--out", out, "--stages", "cs")
|
|
self.assertEqual(rc, 0)
|
|
metas = [
|
|
shader_meta.collect(os.path.join(out, "disasm", f"lib_cs_{i:05d}.s"), f"lib_cs_{i:05d}", [[]], [[]])
|
|
for i in range(2)
|
|
]
|
|
self.assertEqual({m["fspx_code_sha256"] for m in metas}, {hashlib.sha256(code).hexdigest()})
|
|
self.assertEqual([m["fspx_state_sha256"] for m in metas],
|
|
[hashlib.sha256(s_a).hexdigest(), hashlib.sha256(s_b).hexdigest()])
|
|
|
|
def test_compute_hash_filter_keeps_distinct_states(self):
|
|
first, second = kind0(), kind0(threads=(32, 1, 1))
|
|
code = prog(3)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
src = os.path.join(tmp, "lib.bin")
|
|
with open(src, "wb") as f:
|
|
f.write(build([], [(i, NONE, 0, NONE) for i in range(3)] + [(0, NONE, 1, NONE)],
|
|
[first, second, first], [code, prog(4)]))
|
|
hashes = os.path.join(tmp, "hashes.txt")
|
|
with open(hashes, "w", encoding="utf-8", newline="\n") as f:
|
|
f.write(hashlib.sha256(code).hexdigest() + "\n")
|
|
out = os.path.join(tmp, "out")
|
|
rc, _ = self.run_main(src, "--out", out, "--compute-hashes", hashes)
|
|
self.assertEqual(rc, 0)
|
|
names = ["lib_cs_00000", "lib_cs_00000_state_1"]
|
|
with open(os.path.join(out, "index.json"), encoding="utf-8") as f:
|
|
self.assertEqual([e["file"] for e in json.load(f)], [n + ".dxbc" for n in names])
|
|
for name, state in zip(names, (first, second)):
|
|
with open(os.path.join(out, "fspx", name + ".state"), "rb") as f:
|
|
self.assertEqual(f.read(), state)
|
|
with open(os.path.join(out, "library.json"), encoding="utf-8") as f:
|
|
self.assertEqual([e["program"] for e in json.load(f)["compute"][:3]], [*names, names[0]])
|
|
|
|
def test_carve_names_sources_and_copies_containers(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
carve = os.path.join(tmp, "carve")
|
|
os.makedirs(carve)
|
|
index = []
|
|
for name, d in (("PS_A", D_PS), ("PS_A~1", D_PS), ("CS_B", D_CS), ("VS_C", D_VS)):
|
|
with open(os.path.join(carve, name + ".dxbc"), "wb") as f:
|
|
f.write(dxbc(d))
|
|
index.append({"name": name, "file": name + ".dxbc"})
|
|
index.append({"name": "gone", "file": "gone.dxbc"}) # listed but missing: skipped
|
|
with open(os.path.join(carve, "index.json"), "w", encoding="utf-8", newline="\n") as f:
|
|
json.dump(index, f)
|
|
src = os.path.join(tmp, "lib.bin")
|
|
with open(src, "wb") as f:
|
|
f.write(sample())
|
|
out = os.path.join(tmp, "out")
|
|
self.assert_save_overlap(src, out, os.path.join(out, "lib_vs_00001.dxbc"), "--carve", carve)
|
|
self.assertFalse(os.path.exists(out))
|
|
os.mkdir(out)
|
|
save = os.path.join(out, "lib_ms_00002.dxbc")
|
|
rc, text = self.run_main(src, "--out", out, "--carve", carve, "--save", save)
|
|
self.assertEqual(rc, 0)
|
|
self.assertIn("3 of 4 named by a carved shader's HASH digest, 1 not in the carve", text)
|
|
with open(os.path.join(out, "index.json"), encoding="utf-8") as f:
|
|
by = {e["file"]: e for e in json.load(f)}
|
|
self.assertEqual(by["lib_ps_00000.dxbc"]["source"], ["PS_A", "PS_A~1"])
|
|
self.assertEqual(by["lib_cs_00003.dxbc"]["source"], ["CS_B"])
|
|
self.assertEqual(by["lib_ms_00002.dxbc"]["source"], [])
|
|
with open(os.path.join(out, "lib_vs_00001.dxbc"), "rb") as f:
|
|
self.assertEqual(f.read(), dxbc(D_VS))
|
|
with open(save, "rb") as f:
|
|
self.assertEqual(f.read(), sample())
|
|
with open(os.path.join(out, "fspx", "lib_ps_00000.json"), encoding="utf-8") as f:
|
|
self.assertEqual(json.load(f)["digest"], [D_PS.hex()])
|
|
|
|
def test_translator_reads_compute_sidecar(self):
|
|
import rdna2spirv
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
src = os.path.join(tmp, "lib.bin")
|
|
with open(src, "wb") as f:
|
|
f.write(sample())
|
|
out = os.path.join(tmp, "out")
|
|
self.run_main(src, "--out", out, "--stages", "cs")
|
|
s_path = os.path.join(out, "disasm", "lib_cs_00003.s")
|
|
self.assertEqual(rdna2spirv.cs_dispatch_state(s_path, "lib_cs_00003"), (0xB8A, 8, 8, 1))
|
|
self.assertEqual(rdna2spirv.native_blob(s_path, "lib_cs_00003")[:36], prologue(0x77778888))
|
|
|
|
def test_translator_rejects_pixel_program_without_container(self):
|
|
"""A pixel program whose source isn't in the carve has no <name>.dxbc for its input signature: Unsupported for
|
|
that shader, not a FileNotFoundError that stops the run."""
|
|
import rdna2spirv
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
src = os.path.join(tmp, "lib.bin")
|
|
with open(src, "wb") as f:
|
|
f.write(sample())
|
|
out = os.path.join(tmp, "out")
|
|
self.run_main(src, "--out", out, "--stages", "ps")
|
|
os.makedirs(os.path.join(out, "disasm"))
|
|
s_path = os.path.join(out, "disasm", "lib_ps_00000.s")
|
|
with open(s_path, "w", encoding="utf-8", newline="\n") as f:
|
|
f.write("; lib_ps_00000 code 0x24..0x30 (fspx), gfx1013, 0 invalid words\n\ts_endpgm\n")
|
|
rdna2spirv._STAGES = None # stage_of caches the first index.json it reads
|
|
try:
|
|
with self.assertRaisesRegex(rdna2spirv.Unsupported, "no shader container"):
|
|
rdna2spirv.translate(s_path)
|
|
finally:
|
|
rdna2spirv._STAGES = None
|
|
|
|
def test_carve_reports_corrupt_and_unreadable_containers(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
carve = os.path.join(tmp, "carve")
|
|
os.makedirs(carve)
|
|
with open(os.path.join(carve, "PS_A.dxbc"), "wb") as f:
|
|
f.write(dxbc(D_PS))
|
|
with open(os.path.join(carve, "VS_C.dxbc"), "wb") as f:
|
|
f.write(dxbc(D_VS)[:20]) # truncated header
|
|
with open(os.path.join(carve, "MS_D.dxbc"), "wb") as f:
|
|
f.write(dxbc(D_MS)[:-4]) # truncated part payload
|
|
os.makedirs(os.path.join(carve, "CS_B.dxbc")) # exists on disk, unreadable OSError
|
|
index = [
|
|
{"name": "PS_A", "file": "PS_A.dxbc"},
|
|
{"name": "gone", "file": "gone.dxbc"}, # missing: skipped silently
|
|
{"name": "VS_C", "file": "VS_C.dxbc"},
|
|
{"name": "MS_D", "file": "MS_D.dxbc"},
|
|
{"name": "CS_B", "file": "CS_B.dxbc"},
|
|
]
|
|
with open(os.path.join(carve, "index.json"), "w", encoding="utf-8", newline="\n") as f:
|
|
json.dump(index, f)
|
|
src = os.path.join(tmp, "lib.bin")
|
|
with open(src, "wb") as f:
|
|
f.write(sample())
|
|
out = os.path.join(tmp, "out")
|
|
stdout, stderr = io.StringIO(), io.StringIO()
|
|
with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
|
|
rc = sl.main([src, "--out", out, "--carve", carve])
|
|
self.assertEqual(rc, 0)
|
|
self.assertIn("1 of 4 named by a carved shader's HASH digest, 3 not in the carve, "
|
|
"3 unreadable/corrupt containers", stdout.getvalue())
|
|
err_lines = stderr.getvalue().splitlines()
|
|
self.assertEqual(len(err_lines), 3)
|
|
self.assertEqual(err_lines[0], "carve: VS_C.dxbc: truncated DXBC header")
|
|
self.assertEqual(err_lines[1], "carve: MS_D.dxbc: truncated DXBC part payload")
|
|
self.assertTrue(err_lines[2].startswith("carve: CS_B.dxbc: ") and len(err_lines[2]) > len("carve: CS_B.dxbc: "))
|
|
self.assertNotIn("gone.dxbc", stderr.getvalue())
|
|
self.assertTrue(os.path.isfile(os.path.join(out, "lib_ps_00000.dxbc")))
|
|
|
|
def test_carve_missing_only_stays_silent(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
carve = os.path.join(tmp, "carve")
|
|
os.makedirs(carve)
|
|
with open(os.path.join(carve, "PS_A.dxbc"), "wb") as f:
|
|
f.write(dxbc(D_PS))
|
|
index = [
|
|
{"name": "PS_A", "file": "PS_A.dxbc"},
|
|
{"name": "gone", "file": "gone.dxbc"},
|
|
]
|
|
with open(os.path.join(carve, "index.json"), "w", encoding="utf-8", newline="\n") as f:
|
|
json.dump(index, f)
|
|
src = os.path.join(tmp, "lib.bin")
|
|
with open(src, "wb") as f:
|
|
f.write(sample())
|
|
out = os.path.join(tmp, "out")
|
|
stdout, stderr = io.StringIO(), io.StringIO()
|
|
with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
|
|
rc = sl.main([src, "--out", out, "--carve", carve])
|
|
self.assertEqual(rc, 0)
|
|
self.assertEqual(stderr.getvalue(), "")
|
|
self.assertIn("1 of 4 named by a carved shader's HASH digest, 3 not in the carve\n", stdout.getvalue())
|
|
self.assertNotIn("unreadable/corrupt", stdout.getvalue())
|
|
|
|
def test_carve_single_corrupt_container_and_direct_api(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
carve = os.path.join(tmp, "carve")
|
|
os.makedirs(carve)
|
|
with open(os.path.join(carve, "PS_A.dxbc"), "wb") as f:
|
|
f.write(dxbc(D_PS))
|
|
with open(os.path.join(carve, "ZERO.dxbc"), "wb") as f:
|
|
f.write(bytes(64)) # valid length/count=0, wrong magic
|
|
index = [
|
|
{"name": "PS_A", "file": "PS_A.dxbc"},
|
|
{"name": "ZERO", "file": "ZERO.dxbc"},
|
|
]
|
|
with open(os.path.join(carve, "index.json"), "w", encoding="utf-8", newline="\n") as f:
|
|
json.dump(index, f)
|
|
direct_err = io.StringIO()
|
|
with contextlib.redirect_stderr(direct_err):
|
|
digests = sl.carve_digests(carve)
|
|
self.assertEqual(digests, {D_PS: [("PS_A", os.path.join(carve, "PS_A.dxbc"))]})
|
|
self.assertEqual(direct_err.getvalue().splitlines(), ["carve: ZERO.dxbc: not a DXBC container"])
|
|
src = os.path.join(tmp, "lib.bin")
|
|
with open(src, "wb") as f:
|
|
f.write(sample())
|
|
out = os.path.join(tmp, "out")
|
|
stdout, stderr = io.StringIO(), io.StringIO()
|
|
with contextlib.redirect_stdout(stdout), contextlib.redirect_stderr(stderr):
|
|
rc = sl.main([src, "--out", out, "--carve", carve])
|
|
self.assertEqual(rc, 0)
|
|
self.assertIn("1 of 4 named by a carved shader's HASH digest, 3 not in the carve, "
|
|
"1 unreadable/corrupt container\n", stdout.getvalue())
|
|
self.assertEqual(stderr.getvalue().splitlines(), ["carve: ZERO.dxbc: not a DXBC container"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|