Skip to content

tools: extract, inspect, decode and map the PS\0\x09 PSO/shader cache - #4358

Open
YaBulochka wants to merge 4 commits into
ItsNotRexed:mainfrom
YaBulochka:feat/psocache-inspect
Open

YaBulochka wants to merge 4 commits into
ItsNotRexed:mainfrom
YaBulochka:feat/psocache-inspect

Conversation

@YaBulochka

@YaBulochka YaBulochka commented Nov 23, 2026 •

Copy link
Copy Markdown

Adds the tools and the measured documentation for the game's PSO/shader cache (PS\0\x09): an inspector, a reproducible extractor, a meta-chunk decoder, a graphics user-signature describer, and docs/pso_cache.md. The cache sits compressed inside an RPF9 archive; the decompressed file is 275,540,780 bytes and every declared region sums to the file size with zero slack.

Verified from the real cache

  • magic, the 0x80 header, graphics PSO count × 0x70 (25,471), compute count × 0x18 (2,693), meta index count × 8 (54,576), shader index count × 8 (21,522), then meta blob then code blob;
  • the shader index holds (offset, size) pairs into the code blob: 4,529 entries contain two or more programs and one contains none; 21,463 entries end in s_code_end, so it is alignment filler and 26,092 s_endpgm is not 21,522 records;
  • the meta blob is not a stream of chunk lists: only 20,318 of 54,576 records are 0x41/0x42/0x43 lists (3,349 empty), the other 30,909 are other record kinds.

Meta chunks

Every list record is all 0x41 chunks, then all 0x42, then exactly one 0x43, so a record belongs to one stage. 0x41 carries kind, an 8-byte-aligned offset, key, count, a packed register triple and a size of 2/4/8 bytes. 0x42 is a slot word 0x00SS_IIRR; where the counts line up, II is 0 for 0x300 (SRV) in 23,047 of 23,049 chunks, 1 for 0x303 (UAV) in 4,144 of 4,146, and 2 for all 6,100 0x302 (CBV), matching D3D12_DESCRIPTOR_RANGE_TYPE order. 0x2 sampler chunks and 0x401 never get a slot, consistent with static samplers not consuming user-data dwords. A record is not the 32-dword user-data block: offsets reach 0x820 and only 49.5% of 0x41 chunks fit in 128 bytes (5,581 of 20,318 records entirely).

Graphics user signature

tools/psocache_usersig.py reads a graphics PSO's chunk records and describes the user-data set: one distinct SS per descriptor table, one (SS, II) per range, one 0x401 per 32-bit-constants parameter, with element sizes 2/4/8 and the root-argument cost of the record. On the real cache every one of the 25,471 graphics PSOs fits the 32-dword contract under both cost models (15 dwords with 2 per table); the graphics records carry SRV 90,500, UAV 182,958, CBV 102,562 and 32-bit constants 90,500, and no sampler chunks. The doc records each field with a confidence, and states what is still ambiguous: SS versus register space, the table order, key/count, 0x100, the extra slots with RR=2, whether a table costs 1 or 2 dwords, and whether 0x43 is the shader hash.

Tools

  • tools/psocache_inspect.py: validates the magic, bounds every region, checks meta-index continuity and shader-index bounds, histograms chunk types and code markers, --meta/--entry/--limit decode the chunk records.
  • tools/psocache_extract.py: finds the cache in an RPF9 archive deterministically, inflates the stream chain (bounded by --budget/--max-streams), writes only to the system tmp, and validates the result with the inspector. On the real archive it stops at the first match in ~4.5 s and produces a byte-identical file on repeat.
  • tools/psocache_usersig.py: the graphics user-signature describer above.
  • tests/test_psocache_inspect.py (14), tests/test_psocache_extract.py (7) and tests/test_psocache_usersig.py (7) build synthetic caches and archives; no game data.

pytest on the three test files: 28 passed. Docs tests and scripts/check_no_game_data.py are clean; docs/pso_cache.md has no game bytes, dump file names or paths.

tools/psocache_inspect.py reads a cache that starts with the PS\0\x09 magic and
checks it against the 0x80 header, 0x70 graphics record, 0x18 compute record and
8-byte index layout: it bounds every region, checks that the meta index covers
the meta blob and the shader index stays inside the code blob, parses the meta
chunk lists (0x41/0x42/0x43) and counts s_endpgm and s_code_end in the code
blob. A file that disagrees is printed region by region and the in-bounds checks
still run; the exit code is 0 for the model, 1 for a disagreement, 2 for input
that is not a cache.

tests/test_psocache_inspect.py builds a synthetic cache (a 0x80 header, two
graphics records, one compute record, two index entries, chunk records and two
programs) and covers the model, an out-of-range record reference, a truncated
file, a bad magic and a short file.
docs/pso_cache.md records the PS\0\x09 blob: the 0x80 header fields and which
of their meanings are unproven, the 0x70 and 0x18 record regions, the two
8-byte index tables, the meta chunk records (0x41/0x42/0x43, the six 0x41
second-dword values and the 0x42 slot-shaped values), the s_endpgm and
s_code_end markers and why their counts differ from the shader count. It also
lists what is still unknown and how tools/psocache_inspect.py checks a carved
copy. docs/README.md gets one row for the page.
@LocranKing

Copy link
Copy Markdown
Contributor

Tested 78fe8991 on Windows 11 Build 26200 (RTX 3060 Ti / driver 621.01 / Python 3.12): pass.

  • python -m compileall -q tools tests scripts docs: clean
  • python make_game.py --help: clean
  • python -I tests/test_psocache_inspect.py: 10/10 tests passed (0.92s)
  • python -I tests/test_docs_links.py: 6/6 tests passed
  • python -I tests/test_docs_repo_paths.py: 2/2 tests passed
  • git diff --check: clean

Verified layout verification and bounds inspection logic against the documented 0x80 header / 0x70 / 0x18 record structures using synthetic test fixtures.

AI-assisted. No game files.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran this at 78fe8991 on Windows 11: python -I tests/test_psocache_inspect.py -> 10 tests, OK; compileall and git diff --check clean.

The tool and docs describe the measured PS\0x09 cache format with synthetic images only. No blocking issue.

tools/psocache_extract.py walks an RPF9 archive's stream table in order,
inflates each stream to learn its extent and stops at the first stream whose
output starts with the cache magic. It joins the following contiguous streams
until the header's declared total is reached, writes the file inside the
temporary directory and validates it with psocache_inspect. A --budget and
--max-streams bound the search; a folder is scanned in sorted order and the
report carries the archive index and the archive-internal stream offset only.

psocache_inspect.py gains --meta (with --entry N and --limit N): it reports
the chunk-record order, the per-kind 0x41 fields, the 0x42 slot bytes and the
0x41-kind to 0x42-middle-byte correlation over the records whose counts match.
The 0x43 value is never printed.

docs/pso_cache.md records the decoded shape with a confidence per claim and
the remaining unknowns; docs/README.md names the new tool.

tests/test_psocache_extract.py builds synthetic RPF9 archives and covers the
contiguous split, folder scan, budget, stream limit, truncated chain and an
output path outside the temporary directory. The inspect test now covers the
meta decode, an entry listing and the CLI flags.
@YaBulochka YaBulochka changed the title tools: inspect the PS\0\x09 PSO/shader cache, with the measured format tools: extract, inspect and decode the PS\0\x09 PSO/shader cache Nov 23, 2026
@CuteBooty

Copy link
Copy Markdown

At bb360f84, I checked the four positional exceptions to the proposed descriptor-to-slot class mapping on Xbox build 1.0.2.1. There were 4,849 records with equal, nonempty descriptor and slot lists, covering 33,295 positions. Comparing by position reproduced four mismatches across two records.

Within each of those two records, the predicted and observed counts match for every class. A pairing that preserves class therefore exists in both records. Slot order could account for all four exceptions. The intended descriptor-to-slot bindings remain unknown. This check covers only equal-length lists.

Method: map descriptor kinds 0x300, 0x303, 0x302 to 0, 1, 2 as proposed, then count each class separately per record on both sides. Six synthetic controls passed, including a case where two inventory errors cancel if pooled across records. The upstream test_psocache_inspect.py and test_psocache_extract.py suites passed 21/21. Windows 11, Ryzen 5 3600, CPython 3.14.10, CPU only. AI-assisted with Codex and Claude.

@CuteBooty

Copy link
Copy Markdown

I also checked the 15,068 records with more 0x42 slots than mapped descriptors on Xbox 1.0.2.1 at bb360f84. In 12 records, even a class-preserving rearrangement leaves some predicted descriptors without a slot of their class. Across those records, the shortfall totals 27 (24 of class 1 and 3 of class 2).

For this check I matched as many descriptors and slots of each class as possible within each record, using the three proposed kind→class mappings. Of 337,783 observed slots and 313,360 predictions, 313,333 match by class. The remaining slots comprise 15,617 in classes already predicted for that record, 149 in classes 0–2 absent from its predictions, and 8,684 in class 3. The net surplus reconciles to 24,423 after subtracting the 27 missing predictions. These counts describe class inventories. The roles of the extra slots remain open.

Tested the per-record counting method on seven synthetic records, including simultaneous excess and deficit, zero predicted descriptors, and an out-of-vocabulary class. Windows 11, Ryzen 5 3600, CPython 3.14.10, CPU only. AI-assisted with Codex and Claude.

tools/psocache_usersig.py resolves a graphics PSO to the chunk records its
stage record names, decodes each 0x41 entry (kind, offset, key, count,
space/base/size) and each 0x42 slot word (SS, II, RR), and computes the
32-dword cost as constants + 2 tables and as constants + 1 table. --limit N
summarises the first N graphics PSOs, --entry N prints one, --json dumps the
same report. It reuses the cache parser from psocache_inspect and never
prints a hash.

docs/pso_cache.md maps the graphics records to descriptor tables, ranges and
32-bit constants with a confidence per claim, gives the measured fit numbers
and sketches how a CreateGraphicsPipelineState path would consume them.

tests/test_psocache_usersig.py builds a two-PSO cache that covers SRV, UAV,
CBV, Sampler, constants, a record that does not fit 32 dwords and extra
slots, and checks the summary, the entry listing, --limit, --json and the
error paths.
@YaBulochka YaBulochka changed the title tools: extract, inspect and decode the PS\0\x09 PSO/shader cache tools: extract, inspect, decode and map the PS\0\x09 PSO/shader cache Nov 23, 2026
@CuteBooty

Copy link
Copy Markdown

Checked psocache_usersig at 16dc3fb7 on Xbox Series build 1.0.2.1. All 25,471 graphics PSOs fit the 32-dword limit when each referenced chunk record is evaluated separately. Each PSO resolved 3–5 distinct records. The largest individual record cost was 13 dwords at one dword per table, or 15 at two.

I also calculated what the same records would cost under two hypothetical combined budgets. Both sum constants across distinct referenced records.

Table accounting across records Fits at 1 dword/table Maximum Fits at 2 dwords/table Maximum
Charge each record’s tables separately 0 / 25,471 65 0 / 25,471 75
Charge each distinct SS label once across records 0 / 25,471 58 0 / 25,471 61

This makes the scope of the documentation’s fit claim explicit: every referenced record fits individually. Determining whether the runtime shares constants, tables, or budgets across stages requires separate binding evidence. Three synthetic controls passed, including repeated references and shared SS labels. All 28 upstream cache tests passed. Windows 11, Ryzen 5 3600, CPython 3.14.10, CPU only. AI-assisted with Codex and Claude.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants