vkback: pre-decode placed texture shapes and shrink placed_ids key to 12 bytes #176

Open
github-bot wants to merge 1 commit from github-bot/gh-176 into main AGit
Owner

@⁠Mhyster on GitHub · 2026-12-02 00:13 UTC · original
Mhyster:opt/predecode-placed-textures → main · +251 −28 · 4 files · 1 commits · open

What

Overlap note: Touches greenfield/vkback/src/placed.rs, slotwrite.rs, and lib.rs, which are also touched in separate hunks by open PRs #147 (placed.rs, lib.rs) and #152 (slotwrite.rs, lib.rs).

Pre-decodes placed texture shapes once per Call::ViewShape and replaces 64-byte (u64, [u32; 14]) SipHash placed_ids lookups with lossless 12-byte (u64, u32) FxMap lookups:

  1. Pre-decoded PlacedShape (greenfield/frame/src/views.rs):
    • Computes filler-stripped key_words (words [1] and [13] zeroed, matching victory-gpu's without_filler), ResourceDesc::from_words, first_mip (victory-gpu's resource::first_mip), and filled (flags & 7 == 0) once when a shape arrives in ViewTable::apply (Call::ViewShape) or ViewTable::parse.
    • Attaches meta: Option<&PlacedShape> on ViewRef::Placed (populated only for real shapes, None for gap fillers and table-entry View::Placed), keeping PartialEq semantics identical to View::as_ref().
  2. Bijective 12-byte PlacedIdTable (greenfield/vkback/src/placed.rs, slotwrite.rs, lib.rs):
    • Interns every distinct filler-stripped [u32; 14] to a unique monotonic u32 ID (desc_ids: FxMap<[u32; 14], u32>, never a lossy hash), caches shape_index -> desc_id in O(1) (Vec<Option<u32>>), and keys ids by 12-byte (address: u64, desc_id: u32) in FxMap.
    • Switches placed_keys from HashMap to FxMap.
    • All lookups are pure in-memory O(1)/FxMap operations on the renderer thread — no file access, locks, or blocking (rule 11).

The reference

  • crates/victory-gpu:
    • descriptor/views.rs (ViewTable::shapes.push, line 165): shapes are append-only and immutable per shape number, which guarantees shape_to_desc[shape_index] never goes stale once populated for a valid ViewShape.
    • resource.rs (without_filler, first_mip): stack-filler words [1] and [13] are zeroed so two views of the same texture differing only in filler map to one texture ID; first_mip applies to shader views of 2D textures (dimension == 4) only.
    • Guards kept: unfilled gap entries in ViewTable::shapes carry valid = false (meta = None), falling back cleanly to on-demand stripping/decoding; View::Placed table entries share the same desc_ids interner so both paths resolve identical (address, key_words) to the exact same texture ID.

Evidence

  • Machine: Linux (CachyOS), Ryzen 5 9600X + AMD RX 9070 XT (Mesa RADV 26.3.1).
  • Collision-freedom proof (placed::tests::placed_id_table_never_aliases_distinct_textures):
    • Exercises production PlacedIdTable directly (rule 27): verifies filler words [1] and [13] normalize to the same ID across indexed (Some(shape)) and unindexed (None) paths; verifies 64 distinct addresses with identical descriptions produce 64 distinct IDs; verifies flipping every single bit of all 12 non-filler words (12 × 32 = 384 mutations) at the same address produces 384 strictly unique IDs; verifies remove deletes only the superseded texture and issues a fresh monotonic ID on re-insert.
  • Pre-decode proof (frame::views::tests::placed_shape_predecodes_filler_desc_mip_and_gap_validity):
    • Verifies gap shapes produce meta == None while valid shapes match ResourceDesc::from_words, without_filler, first_mip, and filled bit-for-bit.
  • Offline tests and CI checks run and passing:
    • cargo test -p victory-shader -p victory-formats -p victory-core -p xtask --locked (all passed).
    • (cd greenfield && cargo test -p umem -p frame -p vkback --lib --locked && cargo test -p vkback --test policy --locked) (14 frame, 22 umem, 169 vkback, 6 policy passed; lib.rs shrank by 2 lines).
    • Clippy with --locked -- -D warnings: native root crates, greenfield, greenfield/wine, and greenfield/vkback/translate all clean.
  • Checks NOT run in this PR (live game runs were paused because another agent was actively using the game/GPU on this machine):
    • Boat scene A B B A performance measurement (VKB_LAT + greenfield/tools/frec/frames.py).
    • House scene ~60 s Vulkan validation run (VK_LAYER_KHRONOS_validation).
    • Before/after screenshots.
    • PE-side cargo xwin clippy (CI will verify; this change touches only greenfield/{frame,vkback}).

Checklist (docs/AGENTS.md)

  • One topic (rule 31); docs/TASKS.md not edited (rule 29)
  • "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen (it drops the maintainers' commits)
  • No fallback or second path for a case that can't happen, in title data or in our own setup (rule 33); unexpected input fails loudly (rule 9)
  • Constants come from the console, the hardware or the title's data, not tuned by eye or ear (rule 9b)
  • Every switch is listed, read in one place, and a bad value stops the start (rule 2)
  • Queues and other externally synchronized Vulkan objects are used under their lock (rule 35)
  • Every unsafe block has a // SAFETY: comment; no raw pointer around the borrow checker (rule 36)
  • Clippy clean; tests call the production code; TRANSLATOR_VERSION bumped if the translator's output changes (rule 27)
  • Comments say what is true now and name functions, not line numbers (rule 3)
  • Nothing from the title, and no pointers to where non-free data can be found (hard constraints)
**@⁠Mhyster** on GitHub · 2026-12-02 00:13 UTC · [original](https://github.com/vktory6/VKtory/pull/176) `Mhyster:opt/predecode-placed-textures` → `main` · +251 −28 · 4 files · 1 commits · open ## What > **Overlap note:** Touches `greenfield/vkback/src/placed.rs`, `slotwrite.rs`, and `lib.rs`, which are also touched in separate hunks by open PRs #147 (`placed.rs`, `lib.rs`) and #152 (`slotwrite.rs`, `lib.rs`). Pre-decodes placed texture shapes once per `Call::ViewShape` and replaces 64-byte `(u64, [u32; 14])` SipHash `placed_ids` lookups with lossless 12-byte `(u64, u32)` `FxMap` lookups: 1. **Pre-decoded `PlacedShape` (`greenfield/frame/src/views.rs`)**: - Computes filler-stripped `key_words` (words `[1]` and `[13]` zeroed, matching `victory-gpu`'s `without_filler`), `ResourceDesc::from_words`, `first_mip` (`victory-gpu`'s `resource::first_mip`), and `filled` (`flags & 7 == 0`) once when a shape arrives in `ViewTable::apply` (`Call::ViewShape`) or `ViewTable::parse`. - Attaches `meta: Option<&PlacedShape>` on `ViewRef::Placed` (populated only for real shapes, `None` for gap fillers and table-entry `View::Placed`), keeping `PartialEq` semantics identical to `View::as_ref()`. 2. **Bijective 12-byte `PlacedIdTable` (`greenfield/vkback/src/placed.rs`, `slotwrite.rs`, `lib.rs`)**: - Interns every distinct filler-stripped `[u32; 14]` to a unique monotonic `u32` ID (`desc_ids: FxMap<[u32; 14], u32>`, never a lossy hash), caches `shape_index -> desc_id` in O(1) (`Vec<Option<u32>>`), and keys `ids` by 12-byte `(address: u64, desc_id: u32)` in `FxMap`. - Switches `placed_keys` from `HashMap` to `FxMap`. - All lookups are pure in-memory O(1)/`FxMap` operations on the renderer thread — no file access, locks, or blocking (rule 11). ## The reference - **`crates/victory-gpu`**: - `descriptor/views.rs` (`ViewTable::shapes.push`, line 165): shapes are append-only and immutable per shape number, which guarantees `shape_to_desc[shape_index]` never goes stale once populated for a valid `ViewShape`. - `resource.rs` (`without_filler`, `first_mip`): stack-filler words `[1]` and `[13]` are zeroed so two views of the same texture differing only in filler map to one texture ID; `first_mip` applies to shader views of 2D textures (`dimension == 4`) only. - Guards kept: unfilled gap entries in `ViewTable::shapes` carry `valid = false` (`meta = None`), falling back cleanly to on-demand stripping/decoding; `View::Placed` table entries share the same `desc_ids` interner so both paths resolve identical `(address, key_words)` to the exact same texture ID. ## Evidence - Machine: Linux (CachyOS), Ryzen 5 9600X + AMD RX 9070 XT (Mesa RADV 26.3.1). - **Collision-freedom proof (`placed::tests::placed_id_table_never_aliases_distinct_textures`)**: - Exercises production `PlacedIdTable` directly (rule 27): verifies filler words `[1]` and `[13]` normalize to the same ID across indexed (`Some(shape)`) and unindexed (`None`) paths; verifies 64 distinct addresses with identical descriptions produce 64 distinct IDs; verifies flipping every single bit of all 12 non-filler words (`12 × 32 = 384` mutations) at the same address produces 384 strictly unique IDs; verifies `remove` deletes only the superseded texture and issues a fresh monotonic ID on re-insert. - **Pre-decode proof (`frame::views::tests::placed_shape_predecodes_filler_desc_mip_and_gap_validity`)**: - Verifies gap shapes produce `meta == None` while valid shapes match `ResourceDesc::from_words`, `without_filler`, `first_mip`, and `filled` bit-for-bit. - Offline tests and CI checks run and passing: - `cargo test -p victory-shader -p victory-formats -p victory-core -p xtask --locked` (all passed). - `(cd greenfield && cargo test -p umem -p frame -p vkback --lib --locked && cargo test -p vkback --test policy --locked)` (14 `frame`, 22 `umem`, 169 `vkback`, 6 `policy` passed; `lib.rs` shrank by 2 lines). - Clippy with `--locked -- -D warnings`: native root crates, `greenfield`, `greenfield/wine`, and `greenfield/vkback/translate` all clean. - **Checks NOT run in this PR** (live game runs were paused because another agent was actively using the game/GPU on this machine): - Boat scene A B B A performance measurement (`VKB_LAT` + `greenfield/tools/frec/frames.py`). - House scene ~60 s Vulkan validation run (`VK_LAYER_KHRONOS_validation`). - Before/after screenshots. - PE-side `cargo xwin clippy` (CI will verify; this change touches only `greenfield/{frame,vkback}`). ## Checklist (docs/AGENTS.md) - [x] One topic (rule 31); `docs/TASKS.md` not edited (rule 29) - [x] "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen (it drops the maintainers' commits) - [x] No fallback or second path for a case that can't happen, in title data or in our own setup (rule 33); unexpected input fails loudly (rule 9) - [x] Constants come from the console, the hardware or the title's data, not tuned by eye or ear (rule 9b) - [x] Every switch is listed, read in one place, and a bad value stops the start (rule 2) - [x] Queues and other externally synchronized Vulkan objects are used under their lock (rule 35) - [x] Every `unsafe` block has a `// SAFETY:` comment; no raw pointer around the borrow checker (rule 36) - [x] Clippy clean; tests call the production code; `TRANSLATOR_VERSION` bumped if the translator's output changes (rule 27) - [x] Comments say what is true now and name functions, not line numbers (rule 3) - [x] Nothing from the title, and no pointers to where non-free data can be found (hard constraints)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 00:15:46 +00:00
Author
Owner

@⁠vktory6 on GitHub · 2026-12-02 15:28 UTC · original

Thanks, the 12-byte (addr, desc_id) key and pre-decoded shapes look correct. Three things before it can merge:

  • first_mip / filled / the filler strip now live in two places, PlacedShape::from_shape and the fallback in slotwrite. Please put them in one helper (rule 33).
  • The shape_to_desc cache assumes a shape number is never redefined. The PE side is append-only today, but ViewTable::apply accepts an overwrite silently. Please make overwriting a valid shape fail loudly (rules 6/9).
  • No measurement yet. A plain FxMap swap might get most of the gain with much less code, so an A B B A boat run should decide which (rule 8).
**@⁠vktory6** on GitHub · 2026-12-02 15:28 UTC · [original](https://github.com/vktory6/VKtory/pull/176#issuecomment-7350386086) Thanks, the 12-byte `(addr, desc_id)` key and pre-decoded shapes look correct. Three things before it can merge: - `first_mip` / `filled` / the filler strip now live in two places, `PlacedShape::from_shape` and the fallback in slotwrite. Please put them in one helper (rule 33). - The `shape_to_desc` cache assumes a shape number is never redefined. The PE side is append-only today, but `ViewTable::apply` accepts an overwrite silently. Please make overwriting a valid shape fail loudly (rules 6/9). - No measurement yet. A plain FxMap swap might get most of the gain with much less code, so an A B B A boat run should decide which (rule 8).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/176/head:github-bot/gh-176'
git switch 'github-bot/gh-176'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-176'
git switch 'github-bot/gh-176'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-176'
git switch 'github-bot/gh-176'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-176'
git switch 'main'
git merge --squash 'github-bot/gh-176'
git switch 'main'
git merge --ff-only 'github-bot/gh-176'
git switch 'main'
git merge 'github-bot/gh-176'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.