Skip to content

gpu: batch descriptor-heap mirroring per draw (HostHeap::mirror_draw) - #3391

Closed
borosf wants to merge 2 commits into
Marvisak:mainfrom
borosf:vibe/mirror-draw-batch-842ca5
Closed

borosf wants to merge 2 commits into
Marvisak:mainfrom
borosf:vibe/mirror-draw-batch-842ca5

Conversation

@borosf

@borosf borosf commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

What

draw.rs::tables() mirrored every non-fresh descriptor-table entry with a separate HostHeap::mirror call — one global VIEWS lock plus one made/kept lock pair per entry. During a world frame the recording thread contends on VIEWS with every view write on the other title threads.

HostHeap::mirror_draw(&[(u32, u64)]) -> Vec<Option<View>> takes one VIEWS lock, reads every slot once, and one made/kept lock per heap per draw. Stale/remake-needed slots fall through to the full mirror(..., true) path, so semantics are unchanged. tables() groups a draw's entries by host heap and mirrors each heap with one call.

Measured (Intel Arc B580, Windows 11, synthetic test, 256 table entries per draw)

  • under VIEWS contention (title thread writing views concurrently): per-slot 755 µs vs mirror_draw 547 µs (~1.4x)
  • uncontended: 585 vs 627 µs (clone cost dominates; no lock win — reported for honesty)

Verification

No game data used; synthetic tests only.

claim: mirror_draw batching per draw (posted on #1633, comment 7203605528)

tables() mirrored every non-fresh descriptor-table entry with a separate
HostHeap::mirror call, taking the global VIEWS lock plus the heap's made/kept
locks once per entry. During a world frame the recording thread contends on
VIEWS with every view write on the other title threads, so those per-entry
lock round-trips dominate. mirror_draw takes one VIEWS lock, reads every slot
once, and one made/kept lock per heap per draw; stale or remake slots still
fall through to the full mirror path. draw.rs groups a draw's table entries
by host heap and mirrors each heap with a single call.

Measured in the new test on Intel Arc B580 (Windows 11), 256 table entries:
per-slot 755 us vs mirror_draw 547 us under VIEWS contention (~1.4x);
uncontended 585 vs 627 us (clone cost dominates, no lock win). Synthetic
tests only; no game data.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
… known parallel-suite flake, NEW-307/NEW-20)

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>

@AurumCarlos AurumCarlos left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This overlaps with #3275 (KainanP, open), which takes the same draw-path VIEWS lookups off the registry lock entirely (generation-checked, lock-free reads; with #2821's RwLock and #3274 as companions). mirror_draw goes the other way: it holds VIEWS once per draw and reads entries directly. If #3275 lands first, this brings the lock back onto the draw path. If this lands first, #3275 has to redo mirror_draw. Worth agreeing with KainanP on one approach in #2586.

Also, your own numbers show the uncontended case getting slower (585 → 627 µs). Without #3275 that may be the right trade, but with it the contended win goes away while the clone cost stays.

@borosf

borosf commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Good catch on #3275, and thank you for the honest read of the uncontended number.

You're right that the two overlap in direction: #3275 makes the draw path's view lookups lock-free (generation-checked reads), and mirror_draw instead takes VIEWS once per heap per draw. If #3275 lands, the contended win in this PR is gone — what remains is the per-heap made/kept batching, which is a different pair of locks, and the 585 → 627 µs uncontended cost of the extra Vec and clone.

So I'd rather not have this compete with #3275. Three ways to settle it, in the order I'd pick them:

  1. GPU: draws find the views their tables hold without the view registry's lock #3275 first, and this closed as superseded. KainanP's change removes the lock instead of paying it once; that is the better answer for the draw path, and mine only wins against the lock while the lock is still there.
  2. GPU: draws find the views their tables hold without the view registry's lock #3275 first, then this rebased on it if the made/kept batching still measures a win once VIEWS is out of the picture (I'd re-measure before asking for review).
  3. This first as an interim only if GPU: draws find the views their tables hold without the view registry's lock #3275 is not landing soon — in which case the numbers as posted are the whole case, uncontended regression included.

I'll ask in #2586 which of the three the maintainer and KainanP want, and I won't push this further until there is an answer. If it's 1, closing it is not a problem from my side — the finding is worth more than the commit: the draw path's per-entry VIEWS round-trips are ~1.4x of the mirror call under contention on this card, and that is what #3275 removes properly.

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this on RTX 5090 and WARP with VICTORY_D3D12_DEBUG=1 (35/35 descriptor::host::tests + draw::tests passed on RTX 5090 excluding the known 1M-cap test, 36/36 on WARP; 256-entry timing: 375.8 µs -> 293.7 µs contended / 296.6 µs -> 291.2 µs uncontended on RTX 5090, 322.5 µs -> 303.0 µs contended / 304.3 µs -> 301.7 µs uncontended on WARP).

Independently of the coordination with #3275, there are four technical defects that need fixing if this PR moves forward:

1. Heap buffer overflow (out-of-bounds write) in the new test

  • crates/victory-gpu/src/descriptor/host.rs:1965, 2008-2013

mirror_draw_matches_mirror_and_batches_the_locks creates a descriptor heap of N = 256 entries (descriptor::create_heap allocates Memory::zeroed(256 * 32, 64) = 8,192 bytes, valid indices 0..256), and then spawns a contender thread writing at let at = title_heap.start + u64::from(N) * 32; (offset 8,192 — index 256, one slot past the end of the allocation). Every create_placed_srv(..., at) call writes 12 bytes past the end of the allocated heap buffer (and also misses SHADER_HEAPS's (start..end) check in descriptor::mirror). The test heap needs N + 1 slots.

2. mirror_draw misses its fast path on 100% of samplers, null/unkept buffer views, and empty slots

  • crates/victory-gpu/src/descriptor/host.rs:869, 877

Two issues here:

  • HostHeap::mirror guards needs_remake_kept behind is_texture(&view) (host.rs:758), but mirror_draw (host.rs:869) omits is_texture(v). Because samplers (View::Sampler) and null/refused buffer views never populate self.kept (kept[index] == None), needs_remake_kept returns true for every single one of them (verified live on both RTX 5090 and WARP: is_texture=false kept_is_none=true needs_remake_kept=true), forcing every sampler and null buffer view to miss the fast path and re-enter self.mirror(index, handle, true) — paying both the batched locks and the per-slot locks.
  • For empty/unwritten slots (view.is_none() and made[index] == 0), out[at] remains None, so if out[at].is_none() (host.rs:877) cannot distinguish a cached None hit from a fast-path miss and still calls self.mirror(index, handle, true) on every empty slot. Tracking resolved entries separately (e.g., Vec<bool> or Vec<Option<Option<View>>>) and checking !is_texture(v) || !needs_remake_kept(...) fixes both.

3. self.kept is now held across crate::resource::host_copy(r)

  • crates/victory-gpu/src/descriptor/host.rs:761-762, 853, 1267-1287

On main, HostHeap::mirror copied held out of self.kept and dropped the self.kept lock before calling crate::resource::host_copy(r). With needs_remake_kept(&kept, ...), both mirror and mirror_draw hold self.made and self.kept across resource::host_copy(r), which acquires resource.host and can perform D3D12 host texture creation, widening lock contention against textures_at/evict/keep and deepening lock nesting. Extracting only the Option<*mut c_void> raw pointer under self.kept (or checking host_copy after releasing self.kept) avoids holding self.kept across resource creation.

4. Minor: table handle lookup in draw.rs:1278-1279

State::table_entry previously looked up self.heap_of(handle); tables() now uses unchecked table + u64::from(u.table_offset_dwords) * 4 and self.heap_of(table). Using table.checked_add(...) and self.heap_of(handle) keeps overflow safety and matches main.

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — verified on Windows (RTX 5090):

  • cargo test -p victory-gpu --lib descriptor:: passed (22/22, including mirror_draw_matches_mirror_and_batches_the_locks, skipping the pre-existing origin/main RTX 5090 failure)
  • cargo test -p victory-gpu --lib draw:: (20/20) and bindless:: (3/3) passed on RTX 5090
  • cargo clippy -p victory-gpu --all-targets -- -D warnings passed with 0 warnings
  • cargo xtask policy passed (191 files ok)

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

@borosf

borosf commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Closing as superseded, as flagged in @AurumCarlos's review: #3275 (generation-checked reads, no registry lock on the draw path) plus #3405 removes the cost this PR pays once per heap per draw, and #3249 already covers the target-transition clones. Landing this first would only put the VIEWS lock back on a path KainanP is clearing.

What was measured stays on the record: 256 table entries, Intel Arc B580 — per-entry mirror 755 µs vs one batched call 547 µs (~1.4x) under VIEWS contention, 585 vs 627 µs uncontended (the batching's own Vec and clone cost is a small loss there). That number is the case for #3275, not against it.

@borosf borosf closed this Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants