"""tools/sampler_source_census.py on hand-written listings and containers (no game files, no GPU).""" import contextlib import io import json import os import struct 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")) import sampler_source_census as ssc # noqa: E402 TOOL = os.path.join(ROOT, "tools", "sampler_source_census.py") SFI0_SMP = 1 << 26 SAMPLE = ("image_sample v[0:3], v[4:5], s[20:27], s[28:31] dmask:0xf dim:SQ_RSRC_IMG_2D", 8) END = ("s_endpgm", 4) def enc(n): """A placeholder `; encoding:` of n bytes: the census only needs the length (for code offsets).""" return "; encoding: [" + ",".join(["0x00"] * n) + "]" def listing(name, lines): """lines: (text, bytes) pairs -> a tab-indented listing as tools/shader_disasm.py writes it.""" end = 0x100 + sum(n for _, n in lines) body = "".join(f"\t{text:<40}{enc(n)}\n" for text, n in lines) return f"; {name}.fssm code 0x100..0x{end:x}\n{body}" def dxbc(flags): """A DXBC container holding only an SFI0 part with the given feature flags.""" sfi0 = b"SFI0" + struct.pack("/shaders/dxbc folder: carve index, containers, FSSM headers and disasm listings.""" def __init__(self): self.tmp = tempfile.TemporaryDirectory() self.root = self.tmp.name self.disasm = os.path.join(self.root, "disasm") os.makedirs(self.disasm) os.makedirs(os.path.join(self.root, "fssm")) self.index = [] def add(self, name, stage, lines, flags=0, rsrc2=None, text=None, blob=None): self.index.append({"file": name + ".dxbc", "stage": stage}) if flags is not None: with open(os.path.join(self.root, name + ".dxbc"), "wb") as f: f.write(dxbc(flags)) with open(os.path.join(self.disasm, name + ".s"), "w", encoding="utf-8", newline="\n") as f: f.write(listing(name, lines) if text is None else text) if rsrc2 is not None or blob is not None: with open(os.path.join(self.root, "fssm", name + ".fssm"), "wb") as f: f.write(blob if blob is not None else bytes(0x138) + struct.pack("