shader_library: keep each state of a compute program, select by code hash (TOOLS-LIBRARY-COMPUTE-BY-HASH) #1281

Manually merged
github merged 1 commit from gh-pr/1281/head into gh-pr/1281/base 2026-11-26 09:45:44 +00:00
Owner

@⁠Moriz82 on GitHub · 2026-11-24 21:14 UTC · original
split/tools-library-compute-by-hash → main · +138 −6 · 3 files · 1 commits · merged 2026-11-26 03:08 UTC

Rebased onto bfef489c at 570db1d5. Main already supports selecting by hash and writing distinct state variants. This diff omits the redundant stateless output when a program also has stateful records, improves the diagnostics and retains the output and overlap tests. All 25 shader-library tests passed.

Claimed in the coordination issue as: TOOLS-LIBRARY-COMPUTE-BY-HASH (claim).

Part of the split of #984 into small pull requests.

Depends on: none
Overlaps: #984 is the parent integration. #583 and #998 (both open) change one line inside the loop this pull request rewrites in tools/shader_library.py ("prologue": start == fspx_unpack.PROLOGUE_SIZE becomes start > 0), and #998 also adds compute=True to the disasm.disasm(...) call below it. Neither is repeated here: the integration branch carried the start > 0 line together with this change, and it is left as main has it. Merged three-way (each PR's diff applied to the base, then merged into this branch), #583 and #998 each conflict in tools/shader_library.py at that one place, because the line they change is next to a line this change removes; the resolution keeps their line. #1021 merges cleanly.

What and why

Two changes to tools/shader_library.py, the tool that unpacks the pipeline library of shaders.rpf.

1. Each state of a compute program gets its entry. The run time finds a library compute program by two keys, the SHA-256 of its code and of its record's state blob (docs/fspx.md, "Build and run-time lookup"), and make_game.py already allows "the same kind-6 bytes with different kind-0 entries" in the translation index. The tool, however, kept one state per program:

cs_state = {c["program"]: c["state"] for c in lib["compute"] if ...}

so when two compute records used one program with different state blobs, the last record's blob was written as fspx/<name>.state and the other pair of keys had no entry at all: no .state, no listing, nothing for the translator to build.

Now every distinct state is written. The first one met keeps the program's name; each further one is written as <name>_state_<blob index> with its own .code, .json, .state, listing, carved container and index.json entry. Records whose blobs hold the same bytes share an entry, a record without a state names the program itself, and library.json names the entry each record means. A program with a single state is written exactly as before. That is every program of the measured library: docs/shader_library.md counts one distinct state-blob index per compute program in 1.0.2.1 (2,684 programs, 2,693 records), so this is a latent fault there and not one that was seen to lose a pipeline. No retail library was read for this pull request.

2. --compute-hashes FILE. Writes only the compute programs whose code has one of the SHA-256 digests listed in FILE (whitespace-separated, any case): the fspx_code_sha256 of the translation index, which is also the key under which the run time looks a program up. Every state such a program is used with is written. It replaces --stages (argparse refuses both together) and needs --out. A file that cannot be read, is empty or holds a word that is not 64 hex digits is a usage error, and a digest that no compute program has is an error; both are reported before any output is created. The digest is the one the run time prints for a program it has no translation for ([rt] d3d12: no translated compute shader for FSPX kind-6 ... sha256 <digest>, runtime/shims/d3d12_vulkan_device.cpp), so the programs a run reported can be unpacked and disassembled without unpacking the whole library.

library.json keeps both whole pipeline tables whatever was selected, as with --stages.

Tested

Linux (CachyOS, kernel 7.3.5), Python 3.14. One commit on 6293c1c4f.

  • python3 -I tests/test_shader_library.py: 24 tests pass (21 before, 3 new). The new ones use one synthetic library: a compute program with three state blobs, two of them the same bytes, and one record without a state; a second compute program that uses one of those blobs; a pixel program.
    • test_program_with_two_states_gets_an_entry_for_each (--stages cs): three entries with .code, .json and .state each; the two entries of the first program have the same code key and each its own state key (shader_meta.collect); library.json names the right entry for all five records; --save onto the further state's file is refused before anything is written.
    • test_compute_hashes_select_programs_by_their_code: a file with the digest in upper case between blank lines selects the first program with both states and nothing of the second; library.json keeps both tables.
    • test_compute_hashes_are_checked_before_anything_is_written: --stages together with the option, the option without --out, an empty file, a blank one, 63 digits, a second word that is no digest, a 0x prefix and a missing file are usage errors; an unknown digest and the pixel program's digest are errors; no output folder exists afterwards.
  • The same file with the base commit's tool (an export of 6293c1c4f with only the test file replaced): all 3 new tests fail. On that library the base writes two entries and two .state files: the first program has no entry with its second state, and library.json gives that record the same name as the others. The option does not exist there.
  • python3 -I tests/test_library_shaders_stage.py (17 tests, the make_game.py step that runs this tool) and tests/test_fspx_shaders_stage.py (24 tests): pass.
  • python3 -I tests/test_docs_cli_flags.py (4 tests, the flags the docs name exist): passes.
  • The CI loop as ci.yml runs it (compileall, every tests/test_*.py outside tests/semantic, the semantic suite, the tracked-file guard), on the committed tree: one failing file, tests/test_imhex_patterns.py, which fails on the base commit too and is the only one there. Semantic suite: 470 files, 6,168 tests, 0 files failed, the same counts as the base.
  • python3 -I scripts/check_no_game_data.py: passes.

Not run: a retail shaders.rpf (so no count of programs with more than one state beyond the figure already in the docs), the make_game.py library step on real data, llvm-mc on the new entries' listings (the new tests do not use --disasm), native Windows or macOS, the runtime build (no C++ or build file changes).

GPUs tested: none (a file unpacker).

AI-assisted: yes (Claude Code). The numbers above come from runs on the machine named.

Checklist

  • Based on upstream main 6293c1c4f, one commit.
  • One topic: which compute programs the tool writes, and under which names.
  • No game data, keys or decryption; the library in the tests is built by hand.
  • No translator rule changed, so no row in docs/design.md; the option and the naming are in docs/shader_library.md.

🤖 Generated with Claude Code

**@⁠Moriz82** on GitHub · 2026-11-24 21:14 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1281) `split/tools-library-compute-by-hash` → `main` · +138 −6 · 3 files · 1 commits · merged 2026-11-26 03:08 UTC **Rebased onto `bfef489c` at `570db1d5`.** Main already supports selecting by hash and writing distinct state variants. This diff omits the redundant stateless output when a program also has stateful records, improves the diagnostics and retains the output and overlap tests. All 25 shader-library tests passed. Claimed in the coordination issue as: TOOLS-LIBRARY-COMPUTE-BY-HASH ([claim](https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14)). Part of the split of #984 into small pull requests. Depends on: none Overlaps: #984 is the parent integration. #583 and #998 (both open) change one line inside the loop this pull request rewrites in `tools/shader_library.py` (`"prologue": start == fspx_unpack.PROLOGUE_SIZE` becomes `start > 0`), and #998 also adds `compute=True` to the `disasm.disasm(...)` call below it. Neither is repeated here: the integration branch carried the `start > 0` line together with this change, and it is left as main has it. Merged three-way (each PR's diff applied to the base, then merged into this branch), #583 and #998 each conflict in `tools/shader_library.py` at that one place, because the line they change is next to a line this change removes; the resolution keeps their line. #1021 merges cleanly. ### What and why Two changes to `tools/shader_library.py`, the tool that unpacks the pipeline library of `shaders.rpf`. **1. Each state of a compute program gets its entry.** The run time finds a library compute program by two keys, the SHA-256 of its code and of its record's state blob (`docs/fspx.md`, "Build and run-time lookup"), and `make_game.py` already allows "the same kind-6 bytes with different kind-0 entries" in the translation index. The tool, however, kept one state per program: ```python cs_state = {c["program"]: c["state"] for c in lib["compute"] if ...} ``` so when two compute records used one program with different state blobs, the last record's blob was written as `fspx/<name>.state` and the other pair of keys had no entry at all: no `.state`, no listing, nothing for the translator to build. Now every distinct state is written. The first one met keeps the program's name; each further one is written as `<name>_state_<blob index>` with its own `.code`, `.json`, `.state`, listing, carved container and `index.json` entry. Records whose blobs hold the same bytes share an entry, a record without a state names the program itself, and `library.json` names the entry each record means. A program with a single state is written exactly as before. That is every program of the measured library: `docs/shader_library.md` counts one distinct state-blob index per compute program in 1.0.2.1 (2,684 programs, 2,693 records), so this is a latent fault there and not one that was seen to lose a pipeline. **No retail library was read for this pull request.** **2. `--compute-hashes FILE`.** Writes only the compute programs whose code has one of the SHA-256 digests listed in `FILE` (whitespace-separated, any case): the `fspx_code_sha256` of the translation index, which is also the key under which the run time looks a program up. Every state such a program is used with is written. It replaces `--stages` (argparse refuses both together) and needs `--out`. A file that cannot be read, is empty or holds a word that is not 64 hex digits is a usage error, and a digest that no compute program has is an error; both are reported before any output is created. The digest is the one the run time prints for a program it has no translation for (`[rt] d3d12: no translated compute shader for FSPX kind-6 ... sha256 <digest>`, `runtime/shims/d3d12_vulkan_device.cpp`), so the programs a run reported can be unpacked and disassembled without unpacking the whole library. `library.json` keeps both whole pipeline tables whatever was selected, as with `--stages`. ### Tested Linux (CachyOS, kernel 7.3.5), Python 3.14. One commit on `6293c1c4f`. - `python3 -I tests/test_shader_library.py`: 24 tests pass (21 before, 3 new). The new ones use one synthetic library: a compute program with three state blobs, two of them the same bytes, and one record without a state; a second compute program that uses one of those blobs; a pixel program. - `test_program_with_two_states_gets_an_entry_for_each` (`--stages cs`): three entries with `.code`, `.json` and `.state` each; the two entries of the first program have the same code key and each its own state key (`shader_meta.collect`); `library.json` names the right entry for all five records; `--save` onto the further state's file is refused before anything is written. - `test_compute_hashes_select_programs_by_their_code`: a file with the digest in upper case between blank lines selects the first program with both states and nothing of the second; `library.json` keeps both tables. - `test_compute_hashes_are_checked_before_anything_is_written`: `--stages` together with the option, the option without `--out`, an empty file, a blank one, 63 digits, a second word that is no digest, a `0x` prefix and a missing file are usage errors; an unknown digest and the pixel program's digest are errors; no output folder exists afterwards. - The same file with the base commit's tool (an export of `6293c1c4f` with only the test file replaced): **all 3 new tests fail**. On that library the base writes two entries and two `.state` files: the first program has no entry with its second state, and `library.json` gives that record the same name as the others. The option does not exist there. - `python3 -I tests/test_library_shaders_stage.py` (17 tests, the `make_game.py` step that runs this tool) and `tests/test_fspx_shaders_stage.py` (24 tests): pass. - `python3 -I tests/test_docs_cli_flags.py` (4 tests, the flags the docs name exist): passes. - The CI loop as `ci.yml` runs it (compileall, every `tests/test_*.py` outside `tests/semantic`, the semantic suite, the tracked-file guard), on the committed tree: one failing file, `tests/test_imhex_patterns.py`, which fails on the base commit too and is the only one there. Semantic suite: 470 files, 6,168 tests, 0 files failed, the same counts as the base. - `python3 -I scripts/check_no_game_data.py`: passes. Not run: a retail `shaders.rpf` (so no count of programs with more than one state beyond the figure already in the docs), the `make_game.py` library step on real data, llvm-mc on the new entries' listings (the new tests do not use `--disasm`), native Windows or macOS, the runtime build (no C++ or build file changes). GPUs tested: none (a file unpacker). AI-assisted: yes (Claude Code). The numbers above come from runs on the machine named. ### Checklist - [x] Based on upstream main `6293c1c4f`, one commit. - [x] One topic: which compute programs the tool writes, and under which names. - [x] No game data, keys or decryption; the library in the tests is built by hand. - [x] No translator rule changed, so no row in `docs/design.md`; the option and the naming are in `docs/shader_library.md`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The run time finds a pipeline-library compute program by the SHA-256 of
its code and of its record's state blob. shader_library.py kept one
state per program, the last record's, so when two compute records used
one program with different state blobs only one of the two pairs got an
entry and a .state file.

Each distinct state of a program is now written: the first under the
program's name, each further one as <name>_state_<blob index>, with its
own .code, .json, .state, listing and index entry. library.json names
the entry of each record. A program with one state, the case of every
program in the measured 1.0.2.1 library, is written exactly as before.

--compute-hashes FILE writes only the compute programs whose code has
one of the SHA-256 digests listed in FILE, with every state they are
used with. It cannot be combined with --stages, and a digest no compute
program has is an error before anything is written.

tests/test_shader_library.py gets three tests on a synthetic library;
all three fail on the base. The standard-library tests and the
translator suite add no failing file. No retail library was read for
this change.

AI-assisted (Claude Code).

Co-authored-by: Plaaasma <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:45:44 +00:00
github manually merged commit 570db1d58f into gh-pr/1281/base 2026-11-26 09:45:44 +00:00
Author
Owner

@⁠PierreEbele on GitHub · 2026-11-25 14:50 UTC · original

Reviewed 570db1d: runs of tests/test_shader_library.py on the PR (25/25 pass) and on main with the PR's test file copied in (the 3 new tests fail there, the 22 others pass), plus tests/test_library_shaders_stage.py on both trees (17 OK, 1 skip: no spirv-as/spirv-val here). Probes on synthetic libraries (stateless-first mixed program, blob dedup, stateless-only, --compute-hashes edges) found no bug: outputs stay self-consistent, stateless-only extraction is byte-identical to main, and an empty --compute-hashes filename now errors instead of silently ignoring the filter. One note: main already keeps every distinct state and per-record names since #984, so the real delta here is the stateless-record alias and the --compute-hashes polish, not the message's premise. AI-assisted review.

Recommendation: Merge

**@⁠PierreEbele** on GitHub · 2026-11-25 14:50 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1281#issuecomment-7165892005) Reviewed 570db1d: runs of tests/test_shader_library.py on the PR (25/25 pass) and on main with the PR's test file copied in (the 3 new tests fail there, the 22 others pass), plus tests/test_library_shaders_stage.py on both trees (17 OK, 1 skip: no spirv-as/spirv-val here). Probes on synthetic libraries (stateless-first mixed program, blob dedup, stateless-only, --compute-hashes edges) found no bug: outputs stay self-consistent, stateless-only extraction is byte-identical to main, and an empty --compute-hashes filename now errors instead of silently ignoring the filter. One note: main already keeps every distinct state and per-record names since #984, so the real delta here is the stateless-record alias and the --compute-hashes polish, not the message's premise. AI-assisted review. **Recommendation:** Merge
This discussion has been locked. Commenting is limited to contributors.
No description provided.