"""Describe the root-signature / user-data side of the graphics PSO records in a PS\\0\\x09 cache. Each graphics PSO record (0x70 bytes) names a stage record at +8 and an input-layout record at +0xC. The stage record names the per-stage chunk records that carry the user data. A chunk record (0x41/0x42/0x43 list) is decoded here as: - 0x41 entry: kind, offset (w2), key (w3), count (w4), and w5 split into `a` (bits 16..31), `b` (bits 8..15) and element size (bits 0..7). For 0x300/0x302/0x303 the tool reads `b` as the register space, `a` as the base register and w4 as the count. For 0x401 it reads `a` as the number of 32-bit constants. The 0x2 kind is a sampler entry with no slot. - 0x42 entry: the slot word splits as 0x00SS_IIRR. SS is read as the descriptor-table / root-parameter index, II as the D3D12_DESCRIPTOR_RANGE_TYPE order (0 SRV, 1 UAV, 2 CBV, 3 sampler) and RR as a width (1, or 2 for the extra slots). w4 is reported as found. - 0x43 entry: a 16-byte value, not printed. The 32-dword root-argument budget is computed as `constants + 2 * tables` (the layer's default mapping of two dwords per descriptor table) and alternatively as `constants + 1 * tables` (the one-dword-per-table model measured on the console shaders, see docs/d3d12_vulkan.md 3.5). A record fits when the total is at most 32. The names and readings above are inferences from measured shapes, not a decoded driver format; the tool prints the raw bytes it used next to each field so the reading can be checked. It never prints a hash or a path. Usage: python -I psocache_usersig.py [--entry N] [--limit N] [--json] [--quiet] --entry N prints one graphics PSO (0-based). --limit N samples the first N graphics PSOs (default: all). Exit code: 0 on success, 2 when the cache cannot be read or the inspector reports a disagreement. """ import argparse import collections import json import mmap import os import struct import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import psocache_inspect as pi # noqa: E402 KIND_NAMES = { 0x300: "SRV", 0x302: "CBV", 0x303: "UAV", 0x2: "Sampler", 0x401: "constants", 0x100: "unknown-0x100", } DESCRIPTOR_KINDS = (0x300, 0x302, 0x303) RANGE_NAMES = {0: "SRV", 1: "UAV", 2: "CBV", 3: "Sampler"} STAGE_REFS = (0x4, 0x2C, 0x78, 0x90, 0x150, 0x1A4) CONTRACT_DWORDS = 32 def describe_chunk(w): """One decoded chunk as a JSON-safe dict.""" if w[0] == 0x41: kind = w[1] item = { "chunk": "0x41", "tag": "0x%03x" % kind, "name": KIND_NAMES.get(kind, "unknown"), "offset": w[2], "key": w[3], "count": w[4], "a": w[5] >> 16, "b": (w[5] >> 8) & 0xFF, "size": w[5] & 0xFF, } if kind in DESCRIPTOR_KINDS: item["space"] = item["b"] item["base_register"] = item["a"] elif kind == 0x401: item["constant_count"] = item["a"] return item if w[0] == 0x42: return { "chunk": "0x42", "tag": "0x42", "slot": "0x%06x" % w[2], "SS": (w[2] >> 16) & 0xFF, "II": (w[2] >> 8) & 0xFF, "RR": w[2] & 0xFF, "key": w[3], "w4": w[4], } return {"tag": "0x%x" % w[0]} def describe_record(index, chunks): """Decode one chunk record and compute its 32-dword cost under both table models.""" entries = [] descriptors = [] slots = [] hashes = 0 for w in chunks: if w[0] == 0x41: entries.append(describe_chunk(w)) if w[1] in DESCRIPTOR_KINDS: descriptors.append((w, len(entries) - 1)) elif w[0] == 0x42: slots.append(w) elif w[0] == 0x43: hashes += 1 pairs = [] for i, (w, entry_index) in enumerate(descriptors): slot = describe_chunk(slots[i]) if i < len(slots) else None pairs.append({"entry": entries[entry_index], "slot": slot}) extras = [describe_chunk(w) for w in slots[len(descriptors) :]] tables = sorted({(w[2] >> 16) & 0xFF for w in slots}) constants = sum(w[5] >> 16 for w in chunks if w[0] == 0x41 and w[1] == 0x401) cost_two = constants + 2 * len(tables) cost_one = constants + len(tables) kinds = collections.Counter(e["name"] for e in entries) return { "record": index, "chunks": len(chunks), "hashes": hashes, "entries": entries, "pairs": pairs, "extras": extras, "tables": tables, "constants": constants, "cost_two_per_table": cost_two, "cost_one_per_table": cost_one, "fits_two_per_table": cost_two <= CONTRACT_DWORDS, "fits_one_per_table": cost_one <= CONTRACT_DWORDS, "kinds": dict(kinds), } def stage_record_refs(m, meta_off, meta_index, chunk_records, stage): """(stage offset, chunk record index) for every stage field that names a chunk record, deduplicated.""" if type(stage) is not int or not 0 <= stage < len(meta_index): return [] a, b = meta_index[stage] out = [] seen = set() for p in STAGE_REFS: if a >= 0 and p + 4 <= b and meta_off + a + p + 4 <= len(m): value = pi.u32(m, meta_off + a + p) if chunk_records.get(value) is not None and value not in seen: seen.add(value) out.append((p, value)) return out def describe_pso(m, region, meta_off, meta_index, chunk_records, index): """Decode one graphics PSO and the chunk records its stage record names.""" if type(index) is not int or index < 0: raise ValueError("PSO index must be a nonnegative integer") offset = region + index * pi.GRAPHICS_STRIDE if offset < 0 or offset + pi.GRAPHICS_STRIDE > len(m): raise ValueError( "graphics PSO %d at 0x%x does not fit in %d-byte cache" % (index, offset, len(m)) ) stage = pi.u32(m, offset + 8) layout = pi.u32(m, offset + 0xC) refs = stage_record_refs(m, meta_off, meta_index, chunk_records, stage) records = [describe_record(record, chunk_records[record]) for _, record in refs] fits_two = all(r["fits_two_per_table"] for r in records) fits_one = all(r["fits_one_per_table"] for r in records) return { "pso": index, "offset": offset, "stage": stage, "input_layout": layout, "records": [ { "field": "0x%x" % p, "index": record, "size": meta_index[record][1], "decode": decode, } for (p, record), decode in zip(refs, records) ], "fits_two_per_table": fits_two, "fits_one_per_table": fits_one, } def _load_meta(m, report): """Unpack meta_index and chunk_records after checking that `m` still covers the inspected size.""" expected_size = report["size"] if len(m) < expected_size: raise ValueError( "cache shrank during read (%d bytes, expected at least %d)" % (len(m), expected_size) ) counts = report["counts"] meta_off = report["regions"]["meta_blob"]["offset"] meta_region = report["regions"]["meta_index"] meta_index = [ struct.unpack_from("