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

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

@⁠borosf on GitHub · 2026-11-26 21:28 UTC · original
borosf:vibe/mirror-draw-batch-842ca5 → main · +221 −38 · 2 files · 2 commits · open

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

  • cargo test -p victory-gpu --lib --locked -- --test-threads=1: 231 passed, 2 failed = only the known B580 baseline failures (#1744/#1829), incl. new descriptor::host::tests::mirror_draw_matches_mirror_and_batches_the_locks
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings and --all-features: clean
  • fmt gate: host.rs 67=67, draw.rs 84=84 hunk parity vs origin/main; cargo xtask policy: 191 files ok

No game data used; synthetic tests only.

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

**@⁠borosf** on GitHub · 2026-11-26 21:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/3391) `borosf:vibe/mirror-draw-batch-842ca5` → `main` · +221 −38 · 2 files · 2 commits · open ## 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 - `cargo test -p victory-gpu --lib --locked -- --test-threads=1`: 231 passed, 2 failed = only the known B580 baseline failures (#1744/#1829), incl. new `descriptor::host::tests::mirror_draw_matches_mirror_and_batches_the_locks` - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` and `--all-features`: clean - fmt gate: host.rs 67=67, draw.rs 84=84 hunk parity vs origin/main; `cargo xtask policy`: 191 files ok 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 <[email protected]>
Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 01:06:22 +00:00
Author
Owner

@⁠AurumCarlos on GitHub · 2026-11-26 21:38 UTC · original · reviewed

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.

**@⁠AurumCarlos** on GitHub · 2026-11-26 21:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/3391#pullrequestreview-6080737742) · **reviewed** 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.
Author
Owner

@⁠borosf on GitHub · 2026-11-26 21:40 UTC · original

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

**@⁠borosf** on GitHub · 2026-11-26 21:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/3391#issuecomment-7203950294) 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. **#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. **#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 #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.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 21:51 UTC · original · requested changes

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 21:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/3391#pullrequestreview-6080875260) · **requested changes** 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`.
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-27 01:28 UTC · original · approved these changes

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

**@⁠Dukestar69** on GitHub · 2026-11-27 01:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/3391#pullrequestreview-6082280588) · **approved these changes** 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).
This pull request can be merged automatically.
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/3391/head:github-bot/gh-3391'
git switch 'github-bot/gh-3391'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3391'
git switch 'github-bot/gh-3391'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3391'
git switch 'github-bot/gh-3391'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3391'
git switch 'main'
git merge --squash 'github-bot/gh-3391'
git switch 'main'
git merge --ff-only 'github-bot/gh-3391'
git switch 'main'
git merge 'github-bot/gh-3391'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3391
No description provided.