gpu/draw: batch ListViews descriptor copies and remove per-draw allocations #272

Open
github-bot wants to merge 2 commits from github-bot/gh-272 into main AGit
Owner

@⁠mint17 on GitHub · 2026-11-27 13:16 UTC · original
mint17:perf/draw-zero-alloc-batched-listviews → main · +1310 −187 · 2 files · 2 commits · open

Eliminates per-draw heap allocations, SipHash overhead, COM refcount churn, and per-descriptor CopyDescriptorsSimple calls on the hot submission path in crates/victory-gpu/src/draw.rs and crates/victory-gpu/src/descriptor/host.rs (zero merge conflict with #234, #245, or #257).

Changes

  • Batched ListViews CopyDescriptors (ListCopyBatch + HostHeap::make_listed_into): Collapses up to 64 primary + twin descriptor copies in State::tables into a single CopyDescriptors call (keeping CopyDescriptorsSimple(1, ...) for single copies), uses a single views.made.get(&words) lookup on hit, flushes on Drop and before recording resets, and buffers displaced COM resources inline ([Option; 64]) so retire_resource strictly runs after CopyDescriptors overwrites the shader-visible descriptor slots.
  • Pointer-equality HostHeap::keep_ref: Compares raw COM pointers under self.kept[index] before cloning/retiring on make_listed_into, raw_at, raw_uav_at, constant_at, and constant_raw_at.
  • Shutdown-safe zero-refcount host_signature TLS cache: Stores leaked &'static ID3D12CommandSignature references in static MADE and caches plain 'static references in thread_local! via try_with() — eliminating COM AddRef/Release per ExecuteIndirect while running zero COM calls in TLS destructors at teardown.
  • Stack-inline per-draw state (Few<T, N> & 24-byte ReadyReader): Extends Few with clear(), inline/spill-preserving dedup(), and PartialEq/Eq/Debug; replaces heap Vecs in State::host_targets, set_targets scratch buffers, State::tables selected indices, and FreshTable::ready (packed into a 24-byte Copy ReadyReader).
  • Fast AddressHash & zero-atomic root-slot hits: Switches root_srv_slots / root_uav_slots to AddressHash and borrows Option<&Arc<Shadow>> on hits in root_uav and root_srv_table.

Release Microbenchmark (cargo test --release --locked -p victory-gpu --lib draw_batch_and_alloc_bench -- --ignored --nocapture)

Regime Baseline New (ListCopyBatch / Zero-Alloc) Speedup
Live D3D12 48-desc ListViews copy (20,000 tables) 10,593.3 ns/table (220.7 ns/desc) 5,763.7 ns/table (120.1 ns/desc) 1.84×
Per-draw state (250,000 draws: Vec + SipHash + Arc::clone vs Few + ReadyReader + AddressHash) 100.6 ns/draw 57.0 ns/draw 1.77×

Verification

  • cargo test --locked -p victory-gpu --lib -- zero_alloc_tests descriptor::host::tests draw::record_hash_tests -- --test-threads=1 (56 passed, 0 failed — includes 70-slot ListCopyBatch boundary + len == 63 pair-flush + deferred-retirement tests, keep_ref identity/retirement test, 4-thread host_signature test, and Few / ReadyReader tests).
  • cargo test --locked -p victory-gpu --lib --features diagnostics -- zero_alloc_tests -- --test-threads=1 (6 passed, 0 failed).
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings (default + --all-features) and cargo test --locked -p xtask (11 passed, 0 failed).
  • Verified with synthetic tests, live D3D12 host tests, and microbenchmarks only (no title files locally).
**@⁠mint17** on GitHub · 2026-11-27 13:16 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/272) `mint17:perf/draw-zero-alloc-batched-listviews` → `main` · +1310 −187 · 2 files · 2 commits · open Eliminates per-draw heap allocations, SipHash overhead, COM refcount churn, and per-descriptor `CopyDescriptorsSimple` calls on the hot submission path in `crates/victory-gpu/src/draw.rs` and `crates/victory-gpu/src/descriptor/host.rs` (zero merge conflict with #234, #245, or #257). ### Changes - **Batched `ListViews` `CopyDescriptors` (`ListCopyBatch` + `HostHeap::make_listed_into`):** Collapses up to 64 primary + twin descriptor copies in `State::tables` into a single `CopyDescriptors` call (keeping `CopyDescriptorsSimple(1, ...)` for single copies), uses a single `views.made.get(&words)` lookup on hit, flushes on `Drop` and before recording resets, and buffers displaced COM resources inline ([Option<ID3D12Resource>; 64]) so `retire_resource` strictly runs **after** `CopyDescriptors` overwrites the shader-visible descriptor slots. - **Pointer-equality `HostHeap::keep_ref`:** Compares raw COM pointers under `self.kept[index]` before cloning/retiring on `make_listed_into`, `raw_at`, `raw_uav_at`, `constant_at`, and `constant_raw_at`. - **Shutdown-safe zero-refcount `host_signature` TLS cache:** Stores leaked `&'static ID3D12CommandSignature` references in `static MADE` and caches plain 'static references in `thread_local!` via `try_with()` — eliminating COM `AddRef`/`Release` per `ExecuteIndirect` while running zero COM calls in TLS destructors at teardown. - **Stack-inline per-draw state (`Few<T, N>` & 24-byte `ReadyReader`):** Extends `Few` with `clear()`, inline/spill-preserving `dedup()`, and `PartialEq`/`Eq`/`Debug`; replaces heap `Vec`s in `State::host_targets`, `set_targets` scratch buffers, `State::tables` `selected` indices, and `FreshTable::ready` (packed into a 24-byte `Copy` `ReadyReader`). - **Fast `AddressHash` & zero-atomic root-slot hits:** Switches `root_srv_slots` / `root_uav_slots` to `AddressHash` and borrows `Option<&Arc<Shadow>>` on hits in `root_uav` and `root_srv_table`. ### Release Microbenchmark (`cargo test --release --locked -p victory-gpu --lib draw_batch_and_alloc_bench -- --ignored --nocapture`) | Regime | Baseline | New (`ListCopyBatch` / Zero-Alloc) | Speedup | | --- | ---: | ---: | ---: | | Live D3D12 48-desc `ListViews` copy (20,000 tables) | 10,593.3 ns/table (220.7 ns/desc) | 5,763.7 ns/table (120.1 ns/desc) | **1.84×** | | Per-draw state (250,000 draws: `Vec` + SipHash + `Arc::clone` vs `Few` + `ReadyReader` + `AddressHash`) | 100.6 ns/draw | 57.0 ns/draw | **1.77×** | ### Verification - `cargo test --locked -p victory-gpu --lib -- zero_alloc_tests descriptor::host::tests draw::record_hash_tests -- --test-threads=1` (56 passed, 0 failed — includes 70-slot `ListCopyBatch` boundary + `len == 63` pair-flush + deferred-retirement tests, `keep_ref` identity/retirement test, 4-thread `host_signature` test, and `Few` / `ReadyReader` tests). - `cargo test --locked -p victory-gpu --lib --features diagnostics -- zero_alloc_tests -- --test-threads=1` (6 passed, 0 failed). - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` (default + `--all-features`) and `cargo test --locked -p xtask` (11 passed, 0 failed). - Verified with synthetic tests, live D3D12 host tests, and microbenchmarks only (no title files locally).
Eliminate per-draw heap allocations, SipHash overhead, COM refcount churn,
and per-descriptor CopyDescriptorsSimple calls on the hot submission path
in crates/victory-gpu/src/draw.rs and crates/victory-gpu/src/descriptor/host.rs:

- Add stack-buffered ListCopyBatch<'a> (64-slot dst/src handle arrays,
  inline deferred COM retirement drained strictly after CopyDescriptors,
  !Send PhantomData, Drop safety flush, and diagnostics written-slot
  stamping) and HostHeap::make_listed_into so State::tables collapses up
  to 64 ListViews descriptor copies into a single CopyDescriptors call
  while preserving the post-descriptor retirement invariant.
- Add HostHeap::keep_ref / swap_kept_ref pointer-equality fast path to
  skip redundant COM AddRef/retire churn on make_listed_into, raw_at,
  raw_uav_at, constant_at, and constant_raw_at.
- Cache &'static ID3D12CommandSignature references in thread-local storage
  via try_with() (backed by leaked static MADE entries), eliminating COM
  AddRef/Release per ExecuteIndirect without running COM Release in TLS
  destructors at process teardown.
- Extend Few<T, N> with clear(), inline/spill-preserving dedup(), and
  PartialEq/Eq/Debug; convert State::host_targets, set_targets scratch
  buffers, State::tables selected indices, and 24-byte ReadyReader entries
  in FreshTable::ready from heap Vecs to stack-inline Few buffers.
- Switch root_srv_slots and root_uav_slots to AddressHash and borrow
  Option<&Arc<Shadow>> on root_uav / root_srv_table hits.
- Add unit tests, D3D12 ListCopyBatch boundary/deferred-retirement tests,
  multi-thread host_signature tests, and an opt-in release microbenchmark
  (1.84x faster 48-desc ListViews copies, 1.77x faster per-draw state).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 13:23:44 +00:00
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 13:23 UTC · original

@⁠mint17 The 48-descriptor comparison isolates the batching gain. The boundary I’ll check is that displaced COM resources stay alive until the destination slots are overwritten and any in-flight use completes. Keep the per-draw allocation result scoped to the microbenchmark.

**@⁠TaynanGT** on GitHub · 2026-11-27 13:23 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/272#issuecomment-7219415505) @⁠mint17 The 48-descriptor comparison isolates the batching gain. The boundary I’ll check is that displaced COM resources stay alive until the destination slots are overwritten and any in-flight use completes. Keep the per-draw allocation result scoped to the microbenchmark.
Author
Owner

@⁠Garu667 on GitHub · 2026-11-27 14:47 UTC · original

Tested on an RTX 3080 10 GB (Series S, -Machine auto, boat mission autosave, warm shader cache). Same executable source except for this PR:

Run Build fps median (last 24 lines) preparing ms (median) fps lines in ~8 min
159 main e45051a 6.9 81 18+
161 main + #272 0.5 482 11

With this PR the game stalls for seconds at a time: frames of up to 36 s (0.0 fps, 35992.8 ms/frame (22054.4 preparing: copies 7537.6 ms ...)). There's no crash and no card loss, and no pipelines are being made (warm cache). A combined build with #284 and #294 (run 160) stalled the same way. #272 alone reproduces it, so it looks like this one. Happy to rerun with a switch if there is one to turn the batching off.

**@⁠Garu667** on GitHub · 2026-11-27 14:47 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/272#issuecomment-7221233910) Tested on an **RTX 3080 10 GB** (Series S, `-Machine auto`, boat mission autosave, warm shader cache). Same executable source except for this PR: | Run | Build | fps median (last 24 lines) | preparing ms (median) | fps lines in ~8 min | |---|---|---|---|---| | 159 | `main` e45051a | 6.9 | 81 | 18+ | | 161 | `main` + #272 | 0.5 | 482 | 11 | With this PR the game stalls for seconds at a time: frames of up to **36 s** (`0.0 fps, 35992.8 ms/frame (22054.4 preparing: copies 7537.6 ms ...)`). There's no crash and no card loss, and no pipelines are being made (warm cache). A combined build with #284 and #294 (run 160) stalled the same way. #272 alone reproduces it, so it looks like this one. Happy to rerun with a switch if there is one to turn the batching off.
Use VICTORY_BATCH_LIST_VIEWS=0 to flush each view with CopyDescriptorsSimple before releasing displaced resources. Exercise direct and batched copies in the descriptor regression.
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 15:01 UTC · original

@⁠Garu667 The switch is in d3ff310. -More @{VICTORY_BATCH_LIST_VIEWS='0'} flushes each view with individual descriptor copies. Compare it with 1 on the same executable, boat save and warm shader cache. Median frame time, the longest stall and the preparing/copies breakdown are enough to tell whether the batching is responsible.

**@⁠TaynanGT** on GitHub · 2026-11-27 15:01 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/272#issuecomment-7221535120) @⁠Garu667 The switch is in `d3ff310`. `-More @{VICTORY_BATCH_LIST_VIEWS='0'}` flushes each view with individual descriptor copies. Compare it with `1` on the same executable, boat save and warm shader cache. Median frame time, the longest stall and the preparing/copies breakdown are enough to tell whether the batching is responsible.
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/272/head:github-bot/gh-272'
git switch 'github-bot/gh-272'

Merge

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