gpu: keep the narrow barriers' scratch per thread and ask for CommandList7 once #2856

Merged
shiman1901 merged 4 commits from upstream-pr/2856 into main 2026-11-30 14:12:01 +00:00
shiman1901 commented 2026-11-29 18:14:45 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by shiman1901 on 2026-11-29T18:14:45Z.


Related to #799

What it changes

The narrow-barrier replay (VICTORY_NARROW_BARRIERS, merged in #799) runs 653–816 times a frame on the recording thread. On each call it queried ID3D12GraphicsCommandList7 via .cast::<>() (QueryInterface), allocated four fresh Vecs plus a barrier-group Vec, and collected overlapping host copies for buffer barriers into a fresh Vec<Arc<Shadow>>. On CPU-bound setups (reported in #799 by @POMXARK on Ryzen 7 5700X + RTX 5080: victory-submit 59.3 -> 70.9 ms, steps 28.3 -> 36.5 ms), that adds ~800–1,000 QueryInterface calls and ~3,000 heap allocations per frame on top of the barriers themselves.

This keeps all emitted barriers, ordering, dedup keys, and global-UAV fallbacks identical while removing the per-call overhead:

  • HostList::list7 (host.rs, host/place.rs, list.rs): queries ID3D12GraphicsCommandList7 once at HostList creation (list.cast::<ID3D12GraphicsCommandList7>().ok()) and reuses it across all four hot call sites (list.rs legacy narrow path + replay_narrow_barrier_groups, and host/place.rs buffer_barriers + wait_for_all, which now take &HostList directly from self).
  • BarScratch (list.rs): keeps the replay vectors in a thread-local RefCell<BarScratch> taken via an RAII Guard. Because D3D12_BUFFER_BARRIER::pResource is a ManuallyDrop, BarScratch::clear() (called in both Guard::drop and BarScratch::drop) drops every pResource before clearing while preserving vector capacity — leak-free on early returns and panics.
  • shadow::for_each_overlapping (shadow.rs): walks BUFFERS and LOOSE under their read locks via callback without allocating Vec<Arc<Shadow>>. Buffer barriers clone only &ID3D12Resource (AddRef) into scratch.kept inside the read locks, release the locks, then call HostList::keep(&res) (no host-list lock ever taken under SHADOWS/LOOSE) and move res via kept.drain(..) straight into D3D12_BUFFER_BARRIER::pResource (avoiding a second AddRef/Release). peek_overlapping is now a 3-line wrapper over for_each_overlapping.

No new unsafe beyond the existing ManuallyDrop::drop.

How it was tested

  • GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): No local live title run on this machine — static CPU-overhead optimization targeting @POMXARK's CPU-bound profile from #799 (Ryzen 7 5700X + RTX 5080, 653–816 replays/frame). CC @Czer0xx @POMXARK for a live A/B re-check once CI is green.
  • Before: N/A (see #799 comment by @POMXARK: victory-submit 59.3 -> 70.9 ms, steps 28.3 -> 36.5 ms)
  • After: N/A (pending live A/B)
  • For shader changes, the wave sizes tested (32 and/or 64 lanes): N/A (no shader changes)
  • cargo test passed for the crates it touches: verified via Windows CI workflow (added 4 GPU-less unit tests: list::bar_scratch_tests::the_scratch_is_emptied_and_keeps_its_capacity, list::bar_scratch_tests::a_panic_empties_the_scratch_too, shadow::overlapping_tests::an_address_with_no_copy_names_none, shadow::overlapping_tests::a_range_past_the_address_space_is_refused)

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. Related: #799
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5553637146 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2856). Originally posted by [shiman1901](https://github.com/shiman1901) on 2026-11-29T18:14:45Z. --- Related to #799 ## What it changes The narrow-barrier replay (`VICTORY_NARROW_BARRIERS`, merged in #799) runs 653–816 times a frame on the recording thread. On each call it queried `ID3D12GraphicsCommandList7` via `.cast::<>()` (`QueryInterface`), allocated four fresh `Vec`s plus a barrier-group `Vec`, and collected overlapping host copies for buffer barriers into a fresh `Vec<Arc<Shadow>>`. On CPU-bound setups (reported in #799 by @POMXARK on Ryzen 7 5700X + RTX 5080: `victory-submit` 59.3 -> 70.9 ms, `steps` 28.3 -> 36.5 ms), that adds ~800–1,000 `QueryInterface` calls and ~3,000 heap allocations per frame on top of the barriers themselves. This keeps all emitted barriers, ordering, dedup keys, and global-UAV fallbacks identical while removing the per-call overhead: - **`HostList::list7` (`host.rs`, `host/place.rs`, `list.rs`):** queries `ID3D12GraphicsCommandList7` once at `HostList` creation (`list.cast::<ID3D12GraphicsCommandList7>().ok()`) and reuses it across all four hot call sites (`list.rs` legacy narrow path + `replay_narrow_barrier_groups`, and `host/place.rs` `buffer_barriers` + `wait_for_all`, which now take `&HostList` directly from `self`). - **`BarScratch` (`list.rs`):** keeps the replay vectors in a thread-local `RefCell<BarScratch>` taken via an RAII `Guard`. Because `D3D12_BUFFER_BARRIER::pResource` is a `ManuallyDrop`, `BarScratch::clear()` (called in both `Guard::drop` and `BarScratch::drop`) drops every `pResource` before clearing while preserving vector capacity — leak-free on early returns and panics. - **`shadow::for_each_overlapping` (`shadow.rs`):** walks `BUFFERS` and `LOOSE` under their read locks via callback without allocating `Vec<Arc<Shadow>>`. Buffer barriers clone only `&ID3D12Resource` (`AddRef`) into `scratch.kept` inside the read locks, release the locks, then call `HostList::keep(&res)` (no host-list lock ever taken under `SHADOWS`/`LOOSE`) and move `res` via `kept.drain(..)` straight into `D3D12_BUFFER_BARRIER::pResource` (avoiding a second `AddRef`/`Release`). `peek_overlapping` is now a 3-line wrapper over `for_each_overlapping`. No new `unsafe` beyond the existing `ManuallyDrop::drop`. ## How it was tested - GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): No local live title run on this machine — static CPU-overhead optimization targeting @POMXARK's CPU-bound profile from #799 (`Ryzen 7 5700X + RTX 5080`, 653–816 replays/frame). CC @Czer0xx @POMXARK for a live A/B re-check once CI is green. - Before: N/A (see #799 comment by @POMXARK: `victory-submit` 59.3 -> 70.9 ms, `steps` 28.3 -> 36.5 ms) - After: N/A (pending live A/B) - For shader changes, the wave sizes tested (32 and/or 64 lanes): N/A (no shader changes) - `cargo test` passed for the crates it touches: verified via Windows CI workflow (added 4 GPU-less unit tests: `list::bar_scratch_tests::the_scratch_is_emptied_and_keeps_its_capacity`, `list::bar_scratch_tests::a_panic_empties_the_scratch_too`, `shadow::overlapping_tests::an_address_with_no_copy_names_none`, `shadow::overlapping_tests::a_range_past_the_address_space_is_refused`) ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: #799
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
trustno1-coder/VIctory-NVIDIA!2856
No description provided.