vkback: compact tape Op, cache active tape, pool TapeData, atomic batch completion #200

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

@⁠Mhyster on GitHub · 2026-12-02 07:39 UTC · original
Mhyster:opt/vkback-tape-compact-and-pool → main · +243 −58 · 1 files · 1 commits · open

What

Reduces renderer CPU and allocator overhead in the parallel command-buffer tape (greenfield/vkback/src/tape.rs):

  1. Shrink size_of::<Op>() to <= 48 bytes (down from ~112–120 bytes):
    • Boxes rare/heavy variants (Barrier, Barrier2, CopyBuffer, CopyBufferToImage, CopyImageToBuffer, CopyImage, ResolveImage, ClearColor, ClearDepth, ClearAttachments, multi-binding BindVertexBuffers) behind 8-byte Box<...> payloads.
    • Packs the single-binding BindVertexBuffer variant into 32 bytes (falling back to boxed BindVertexBuffers only if first > u16::MAX or stride > u32::MAX) and adds inline Viewport(vk::Viewport) / Scissor(vk::Rect2D) variants so single-viewport and single-scissor commands do zero heap allocation.
  2. Active-tape fast path in Store:
    • Keeps the most recently touched (id, TapeData) directly in StoreInner::active, avoiding a HashMap hash + lookup on consecutive commands recorded into the same virtual command buffer.
  3. Recycle TapeData buffers across frames (Store::pool):
    • Replay workers clear and return replayed TapeData (ops and bytes Vecs) to a bounded shared pool (capped at 64 tapes, dropping oversized buffers > 65,536 ops or > 4 MiB), so Store::open reuses pre-warmed capacities instead of re-allocating and growing on every command list.
  4. Lock-free Batch completion counter:
    • Replaces Batch::left: Mutex<usize> with AtomicUsize + done_mu: Mutex<()> so only the final worker acquiring fetch_sub(1) == 1 locks and notifies cv, and Batch::wait returns immediately with an Acquire load when workers have already finished.

Pure host-side CPU optimization; recorded Vulkan commands and submission order are unchanged.

The reference

  • No separate vkd3d-proton counterpart: tape.rs is vkback's own parallel command-buffer recording layer (VKB_REC_THREADS).

Evidence

  • Unit test added in greenfield/vkback/src/tape.rs (op_is_compact_and_tapes_recycle_across_batches): asserts size_of::<Op>() <= 48, verifies active-tape switching across interleaved open/push/push_bytes calls, verifies Batch::wait with the AtomicUsize counter, and verifies cleared TapeData buffers return to Store::pool and are reused by Store::open.
  • Existing tape::tests (tape_keeps_order_and_values, op_without_tape_is_counted, parallel_replay_keeps_submission_order, real_drops_virtual) all pass.
  • Local CI checks passed (Linux x86_64):
    • cargo test -p victory-shader -p victory-formats -p victory-core -p xtask --locked
    • (cd greenfield && cargo test -p umem -p frame -p vkback --lib --locked && cargo test -p vkback --test policy --locked) (169 passed, 3 ignored; 6/6 policy tests)
    • cargo clippy --all-targets --locked -- -D warnings on root native crates, greenfield, greenfield/wine, and greenfield/vkback/translate.
  • Not run: live game / AMD GPU A/B frame timing (frames.py) and Vulkan validation run (house scene) could not be run in this session (no live game run performed); cargo xwin clippy runs in GitHub Actions CI.

Checklist (docs/AGENTS.md)

  • One topic (rule 31); docs/TASKS.md not edited, the fixed item named above (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 07:39 UTC · [original](https://github.com/vktory6/VKtory/pull/200) `Mhyster:opt/vkback-tape-compact-and-pool` → `main` · +243 −58 · 1 files · 1 commits · open ## What Reduces renderer CPU and allocator overhead in the parallel command-buffer tape (`greenfield/vkback/src/tape.rs`): 1. **Shrink `size_of::<Op>()` to `<= 48` bytes (down from ~112–120 bytes)**: - Boxes rare/heavy variants (`Barrier`, `Barrier2`, `CopyBuffer`, `CopyBufferToImage`, `CopyImageToBuffer`, `CopyImage`, `ResolveImage`, `ClearColor`, `ClearDepth`, `ClearAttachments`, multi-binding `BindVertexBuffers`) behind 8-byte `Box<...>` payloads. - Packs the single-binding `BindVertexBuffer` variant into 32 bytes (falling back to boxed `BindVertexBuffers` only if `first > u16::MAX` or `stride > u32::MAX`) and adds inline `Viewport(vk::Viewport)` / `Scissor(vk::Rect2D)` variants so single-viewport and single-scissor commands do zero heap allocation. 2. **Active-tape fast path in `Store`**: - Keeps the most recently touched `(id, TapeData)` directly in `StoreInner::active`, avoiding a `HashMap` hash + lookup on consecutive commands recorded into the same virtual command buffer. 3. **Recycle `TapeData` buffers across frames (`Store::pool`)**: - Replay workers clear and return replayed `TapeData` (`ops` and `bytes` `Vec`s) to a bounded shared pool (capped at 64 tapes, dropping oversized buffers `> 65,536` ops or `> 4 MiB`), so `Store::open` reuses pre-warmed capacities instead of re-allocating and growing on every command list. 4. **Lock-free `Batch` completion counter**: - Replaces `Batch::left: Mutex<usize>` with `AtomicUsize` + `done_mu: Mutex<()>` so only the final worker acquiring `fetch_sub(1) == 1` locks and notifies `cv`, and `Batch::wait` returns immediately with an `Acquire` load when workers have already finished. Pure host-side CPU optimization; recorded Vulkan commands and submission order are unchanged. ## The reference - No separate vkd3d-proton counterpart: `tape.rs` is vkback's own parallel command-buffer recording layer (`VKB_REC_THREADS`). ## Evidence - Unit test added in `greenfield/vkback/src/tape.rs` (`op_is_compact_and_tapes_recycle_across_batches`): asserts `size_of::<Op>() <= 48`, verifies active-tape switching across interleaved `open`/`push`/`push_bytes` calls, verifies `Batch::wait` with the `AtomicUsize` counter, and verifies cleared `TapeData` buffers return to `Store::pool` and are reused by `Store::open`. - Existing `tape::tests` (`tape_keeps_order_and_values`, `op_without_tape_is_counted`, `parallel_replay_keeps_submission_order`, `real_drops_virtual`) all pass. - Local CI checks passed (Linux x86_64): - `cargo test -p victory-shader -p victory-formats -p victory-core -p xtask --locked` - `(cd greenfield && cargo test -p umem -p frame -p vkback --lib --locked && cargo test -p vkback --test policy --locked)` (169 passed, 3 ignored; 6/6 policy tests) - `cargo clippy --all-targets --locked -- -D warnings` on root native crates, `greenfield`, `greenfield/wine`, and `greenfield/vkback/translate`. - **Not run**: live game / AMD GPU A/B frame timing (`frames.py`) and Vulkan validation run (house scene) could not be run in this session (no live game run performed); `cargo xwin clippy` runs in GitHub Actions CI. ## Checklist (docs/AGENTS.md) - [x] One topic (rule 31); `docs/TASKS.md` not edited, the fixed item named above (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 07:40:10 +00:00
Author
Owner

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

Thanks, but these are four topics (rule 31): boxing the rare Op variants, the active-tape cache, the TapeData pool, and the atomic batch counter. Please split them and measure each with A B B A.

One thing to check in the tape cache: command buffers alternate (clamp prologue vs body), so a map remove and insert per switch may make it slower, not faster. The atomic counter's wakeup is correct (the notifier takes done_mu).

**@⁠vktory6** on GitHub · 2026-12-02 15:28 UTC · [original](https://github.com/vktory6/VKtory/pull/200#issuecomment-7350392884) Thanks, but these are four topics (rule 31): boxing the rare `Op` variants, the active-tape cache, the `TapeData` pool, and the atomic batch counter. Please split them and measure each with A B B A. One thing to check in the tape cache: command buffers alternate (clamp prologue vs body), so a map remove and insert per switch may make it slower, not faster. The atomic counter's wakeup is correct (the notifier takes `done_mu`).
This pull request has changes conflicting with the target branch.
  • greenfield/vkback/src/tape.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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