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

Closed
opened 2026-11-26 20:20:49 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1281
Author: @Moriz82
Created: 11/24/2026
Status: ✅ Merged
Merged: 11/26/2026
Merged by: @aslan2142

Base: main ← Head: split/tools-library-compute-by-hash


📝 Commits (1)

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

📊 Changes

3 files changed (+138 additions, -6 deletions)

View changed files

📝 docs/shader_library.md (+7 -2)
📝 tests/test_shader_library.py (+123 -0)
📝 tools/shader_library.py (+8 -4)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1281 **Author:** [@Moriz82](https://github.com/Moriz82) **Created:** 11/24/2026 **Status:** ✅ Merged **Merged:** 11/26/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `split/tools-library-compute-by-hash` --- ### 📝 Commits (1) - [`570db1d`](https://github.com/ubiquitarianism/leonida/commit/570db1d58f67a3b899cc60f53055eeb86fc8ac3d) shader_library: keep each state of a compute program, select by code hash (TOOLS-LIBRARY-COMPUTE-BY-HASH) ### 📊 Changes **3 files changed** (+138 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `docs/shader_library.md` (+7 -2) 📝 `tests/test_shader_library.py` (+123 -0) 📝 `tools/shader_library.py` (+8 -4) </details> ### 📄 Description **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://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7144662266)). 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:20:49 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1314
No description provided.