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

Closed
mint17 wants to merge 559 commits from upstream-pr/272 into main
mint17 commented 2026-11-27 13:16:24 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by mint17 on 2026-11-27T13:16:24Z.


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).
<!-- greyfinch-sync:TaynanGT/Vice-Tay:issue:5527276309 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/272). Originally posted by [mint17](https://github.com/mint17) on 2026-11-27T13:16:24Z. --- 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).
TaynanGT commented 2026-11-27 13:23:17 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-27T13:23:17Z.


@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.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7219415505 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/272#issuecomment-7219415505). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-27T13:23:17Z. --- @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.
Garu667 commented 2026-11-27 14:47:34 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by Garu667 on 2026-11-27T14:47:34Z.


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.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7221233910 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/272#issuecomment-7221233910). Originally posted by [Garu667](https://github.com/Garu667) on 2026-11-27T14:47:34Z. --- 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.
TaynanGT commented 2026-11-27 15:01:51 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-27T15:01:51Z.


@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.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7221535120 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/272#issuecomment-7221535120). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-27T15:01:51Z. --- @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.
mint17 commented 2026-11-28 04:00:11 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by mint17 on 2026-11-28T04:00:11Z.


@TaynanGT @Garu667 Thanks for the detailed RTX 3080 reproduction and for adding the VICTORY_BATCH_LIST_VIEWS toggle in d3ff310!

After auditing 3caeac1 line by line against the preparing / copies stall profile, all Rust state logic (Few, keep_ref, AddressHash root slots, host_signature, views.made) emits identical deferred work, leaving two concrete regressions — both fixed in e2880f7:

  1. Default VICTORY_BATCH_LIST_VIEWS to off (direct CopyDescriptorsSimple(1, ...)): ListCopyBatch::flush() was the only runtime multi-range ID3D12Device::CopyDescriptors call in the codebase, hit concurrently across 12 recording threads (VICTORY_THIN_THREADS=12 + VICTORY_D3D12_DRED=faults) against the shared shader-visible heap while Host::submit / refresh_spans / victory-heap-sync also call into ID3D12Device. Flipping the default to direct CopyDescriptorsSimple(1, ...) + immediate post-copy resource retirement keeps the VICTORY_BATCH_LIST_VIEWS=1 switch intact for A/B comparison while avoiding the stall by default.
  2. Eliminate FreshTable 2.6× bucket bloat: Inlining Few<ReadyReader, 4> grew FreshTable from 40 B → 144 B (inflating fresh_tables HashMap buckets from 64 B → 168 B). Keeping the compact 24 B ReadyReader inside Vec<ReadyReader> restores a 40 B FreshTable / ≤ 64 B single-cache-line bucket (pinned with a compile-time layout guard in fresh_table_fits_single_cache_line_bucket).

Re-verified cleanly: 58/58 targeted tests (default), 8/8 diagnostics tests, Clippy (default + --all-features), and xtask (11/11) all pass with zero warnings.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7233588531 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/272#issuecomment-7233588531). Originally posted by [mint17](https://github.com/mint17) on 2026-11-28T04:00:11Z. --- @TaynanGT @Garu667 Thanks for the detailed RTX 3080 reproduction and for adding the `VICTORY_BATCH_LIST_VIEWS` toggle in `d3ff310`! After auditing `3caeac1` line by line against the `preparing` / `copies` stall profile, all Rust state logic (`Few`, `keep_ref`, `AddressHash` root slots, `host_signature`, `views.made`) emits identical deferred work, leaving two concrete regressions — both fixed in `e2880f7`: 1. **Default `VICTORY_BATCH_LIST_VIEWS` to off (direct `CopyDescriptorsSimple(1, ...)`):** `ListCopyBatch::flush()` was the only runtime multi-range `ID3D12Device::CopyDescriptors` call in the codebase, hit concurrently across 12 recording threads (`VICTORY_THIN_THREADS=12` + `VICTORY_D3D12_DRED=faults`) against the shared shader-visible heap while `Host::submit` / `refresh_spans` / `victory-heap-sync` also call into `ID3D12Device`. Flipping the default to direct `CopyDescriptorsSimple(1, ...)` + immediate post-copy resource retirement keeps the `VICTORY_BATCH_LIST_VIEWS=1` switch intact for A/B comparison while avoiding the stall by default. 2. **Eliminate `FreshTable` 2.6× bucket bloat:** Inlining `Few<ReadyReader, 4>` grew `FreshTable` from 40 B → 144 B (inflating `fresh_tables` `HashMap` buckets from 64 B → 168 B). Keeping the compact 24 B `ReadyReader` inside `Vec<ReadyReader>` restores a 40 B `FreshTable` / ≤ 64 B single-cache-line bucket (pinned with a compile-time layout guard in `fresh_table_fits_single_cache_line_bucket`). Re-verified cleanly: 58/58 targeted tests (default), 8/8 diagnostics tests, Clippy (default + `--all-features`), and `xtask` (11/11) all pass with zero warnings.
TaynanGT commented 2026-11-29 11:23:58 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-29T11:23:58Z.


Thanks for isolating the two regressions. Keeping direct copies as the default and the batch toggle available for A/B makes sense. Please repeat the RTX 3080 workload on e2880f7 with both toggle states before we reconsider batching by default.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7255085044 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/272#issuecomment-7255085044). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-29T11:23:58Z. --- Thanks for isolating the two regressions. Keeping direct copies as the default and the batch toggle available for A/B makes sense. Please repeat the RTX 3080 workload on e2880f7 with both toggle states before we reconsider batching by default.
TaynanGT commented 2026-11-30 17:13:52 +00:00 (Migrated from github.com)

Imported from TaynanGT/Vice-Tay. Originally posted by TaynanGT on 2026-11-30T17:13:52Z.


main has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current main and open a new PR; I'll review it there.

<!-- greyfinch-sync:TaynanGT/Vice-Tay:comment:7285928259 --> Imported from [TaynanGT/Vice-Tay](https://github.com/TaynanGT/Vice-Tay/pull/272#issuecomment-7285928259). Originally posted by [TaynanGT](https://github.com/TaynanGT) on 2026-11-30T17:13:52Z. --- `main` has moved to a new line of the code and everything is now named Vice-Tay, so this PR no longer applies as it is (details in #468). If you'd like this change in, please rebuild it on the current `main` and open a new PR; I'll review it there.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
TaynanGT/Vice-Tay!272
No description provided.