GPU: batch resource barriers, reuse draw scratch buffers, range-lock copy_simple, and pool list uploads #1382

Open
wbkepler wants to merge 1 commit from upstream-pr/1382 into main
wbkepler commented 2026-11-24 17:40:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T17:40:06Z.


Summary

Rebased cleanly onto upstream/main (ffe310e):

  • Batches HostList ResourceBarrier calls (coalescing same-resource transitions A->B->C into A->C, cancelling A->B->A round-trips, and deduplicating consecutive global UAV barriers) and flushes once before Draw/Dispatch/Copy/Resolve/Clear/ExecuteIndirect/Close.
  • Reuses scratch Vecs across draws and resets in draw::State::bind and State::tables, and replaces small heap Vecs in set_targets/SetDescriptorHeaps with fixed stack arrays.
  • Range-locks descriptor::copy_simple via mirror_range and views_at_batch so SHADER_HEAPS, pending, VIEWS, made, and kept are acquired once per copy range instead of per slot.
  • Pools HostList::upload allocations onto 512-byte-aligned Scratch upload chunks (plus a small capped pool for oversized >4 MiB uploads) reused across HostList::reset().
  • Adds GPU/WARP tests covering batched barrier coalescing, range-locked copy_simple parity, and pooled upload reuse across resets.

Verification

  • cargo clippy -p victory-gpu --all-targets & -p victory --features diagnostics --all-targets — passed
  • cargo test -p victory-gpu — passed
  • cargo xtask policy — passed
<!-- greyfinch-sync:Marvisak/VIctory:issue:6587818095 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1382). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T17:40:06Z. --- ## Summary Rebased cleanly onto `upstream/main` (`ffe310e`): - Batches `HostList` `ResourceBarrier` calls (coalescing same-resource transitions `A->B->C` into `A->C`, cancelling `A->B->A` round-trips, and deduplicating consecutive global UAV barriers) and flushes once before `Draw`/`Dispatch`/`Copy`/`Resolve`/`Clear`/`ExecuteIndirect`/`Close`. - Reuses scratch `Vec`s across draws and resets in `draw::State::bind` and `State::tables`, and replaces small heap `Vec`s in `set_targets`/`SetDescriptorHeaps` with fixed stack arrays. - Range-locks `descriptor::copy_simple` via `mirror_range` and `views_at_batch` so `SHADER_HEAPS`, `pending`, `VIEWS`, `made`, and `kept` are acquired once per copy range instead of per slot. - Pools `HostList::upload` allocations onto 512-byte-aligned `Scratch` upload chunks (plus a small capped pool for oversized `>4 MiB` uploads) reused across `HostList::reset()`. - Adds GPU/WARP tests covering batched barrier coalescing, range-locked `copy_simple` parity, and pooled upload reuse across resets. ## Verification - `cargo clippy -p victory-gpu --all-targets` & `-p victory --features diagnostics --all-targets` — passed - `cargo test -p victory-gpu` — passed - `cargo xtask policy` — passed
catminou4 commented 2026-11-24 21:02:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T20:49:53Z.

Review: COMMENTED


Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; this is source review of a large diff plus the PR's own Windows tests.

Resource barriers are batched (queue_barrier/queue_transition/flush_barriers with pending_barrier_count), draw scratch buffers are reused, and copy ranges are range-locked (mirror_range/views_at_batch + copy_simple_range_lock_matches_single_slot_mirroring test comparing batched vs single-slot mirroring). Batching barriers changes ordering only if flush points are right — the mirror-equivalence test is the key evidence; please make sure flush_barriers is called at every point the old code submitted. Windows-only.

<!-- greyfinch-sync:Marvisak/VIctory:review:6047171793 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1382#pullrequestreview-6047171793). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T20:49:53Z. **Review:** COMMENTED --- Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; this is source review of a large diff plus the PR's own Windows tests. Resource barriers are batched (queue_barrier/queue_transition/flush_barriers with pending_barrier_count), draw scratch buffers are reused, and copy ranges are range-locked (mirror_range/views_at_batch + copy_simple_range_lock_matches_single_slot_mirroring test comparing batched vs single-slot mirroring). Batching barriers changes ordering only if flush points are right — the mirror-equivalence test is the key evidence; please make sure flush_barriers is called at every point the old code submitted. Windows-only.
wbkepler commented 2026-11-24 23:05:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T22:02:56Z.


Rebased onto 70564ae as bf8a9b2 (squashed to one commit; conflicts resolved by re-applying each part on top of upstream's new code).

Kept, adapted to upstream

  • Barrier queue: ensure_state, title barriers and barrier groups queue per host list and go out in one ResourceBarrier before the next command. Every path that records a command or its own barrier flushes first. That covers draws, dispatches, mesh and indirect work (before the clamp and before done), copies, resolves, RTV/DSV/UAV clears, texture uploads and alias copies, twin::route (so queued transitions land before the depth/twin copy), external::host_texture/record, and the diagnostics read-backs. close() flushes as well. Folding no longer touches split barriers. A cancelled UAV -> X -> UAV now becomes a UAV barrier instead of disappearing, so back-to-back shader writes stay ordered.
  • Pooled HostList::upload: unchanged in intent. It hands out 512-byte aligned scratch slices and keeps up to 2 big buffers across resets.
  • Draw scratch: only the user-data buffer, the fixed render-target array and the fixed heap array in upstream's new bind_heaps (memory heap first, as upstream orders it).

Dropped as superseded

  • The tables() scratch rewrite. Upstream no longer makes every constant-buffer table fresh, so fresh/made stay empty Vec::new() (no allocation) on the common path.
  • The duplicated per-slot logic in HostHeap::mirror_range. Upstream's HostHeap::mirror changed a lot (stride table, viewed, placed textures, buffer views made on write), so a copy of it would drift. copy_simple now looks the heap up and takes pending once per range, then calls upstream's mirror per slot. Slots past the heap still go one by one.
  • .rev() in shader_slot (not needed).

Verification: clippy -D warnings (victory-gpu, and victory with diagnostics), cargo test -p victory-gpu and cargo xtask policy all pass. New tests: barrier folding/flush on the device, upload pooling across resets, and per-slot mirroring by copy_simple. Two tests fail identically on plain upstream/main on this host: dynamic_slots_are_held_until_released and lists_share_a_table_and_copies_keep_their_views.

Interaction with #1331: #1331 already conflicts with upstream/main itself, in the same 2 places, and this PR adds no new conflict region. The resolution:

  1. In State::bind, keep upstream's take_rebind + memory_heap + self.bind_heaps(list, memory_heap).
  2. Move #1331's idea into bind_heaps: build the host heap array first, then compare and store the host heaps' as_raw() identities in host_heaps instead of the title addresses in want. This needs room for the memory heap's identity too.
  3. Merge #1331's test into upstream's existing #[cfg(test)] mod tests at the end of draw.rs.

AI-assisted (Claude Code); checked on Windows 11 / RTX 5090.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7145732366 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1382#issuecomment-7145732366). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T22:02:56Z. --- Rebased onto `70564ae` as `bf8a9b2` (squashed to one commit; conflicts resolved by re-applying each part on top of upstream's new code). **Kept, adapted to upstream** - **Barrier queue:** `ensure_state`, title barriers and barrier groups queue per host list and go out in one `ResourceBarrier` before the next command. Every path that records a command or its own barrier flushes first. That covers draws, dispatches, mesh and indirect work (before the clamp and before `done`), copies, resolves, RTV/DSV/UAV clears, texture uploads and alias copies, `twin::route` (so queued transitions land before the depth/twin copy), `external::host_texture`/`record`, and the diagnostics read-backs. `close()` flushes as well. Folding no longer touches split barriers. A cancelled `UAV -> X -> UAV` now becomes a UAV barrier instead of disappearing, so back-to-back shader writes stay ordered. - **Pooled `HostList::upload`:** unchanged in intent. It hands out 512-byte aligned scratch slices and keeps up to 2 big buffers across resets. - **Draw scratch:** only the user-data buffer, the fixed render-target array and the fixed heap array in upstream's new `bind_heaps` (memory heap first, as upstream orders it). **Dropped as superseded** - The `tables()` scratch rewrite. Upstream no longer makes every constant-buffer table fresh, so `fresh`/`made` stay empty `Vec::new()` (no allocation) on the common path. - The duplicated per-slot logic in `HostHeap::mirror_range`. Upstream's `HostHeap::mirror` changed a lot (stride table, `viewed`, placed textures, buffer views made on write), so a copy of it would drift. `copy_simple` now looks the heap up and takes `pending` once per range, then calls upstream's `mirror` per slot. Slots past the heap still go one by one. - `.rev()` in `shader_slot` (not needed). **Verification:** clippy `-D warnings` (victory-gpu, and victory with diagnostics), `cargo test -p victory-gpu` and `cargo xtask policy` all pass. New tests: barrier folding/flush on the device, upload pooling across resets, and per-slot mirroring by `copy_simple`. Two tests fail identically on plain `upstream/main` on this host: `dynamic_slots_are_held_until_released` and `lists_share_a_table_and_copies_keep_their_views`. **Interaction with #1331:** #1331 already conflicts with `upstream/main` itself, in the same 2 places, and this PR adds no new conflict region. The resolution: 1. In `State::bind`, keep upstream's `take_rebind` + `memory_heap` + `self.bind_heaps(list, memory_heap)`. 2. Move #1331's idea into `bind_heaps`: build the host heap array first, then compare and store the host heaps' `as_raw()` identities in `host_heaps` instead of the title addresses in `want`. This needs room for the memory heap's identity too. 3. Merge #1331's test into upstream's existing `#[cfg(test)] mod tests` at the end of `draw.rs`. _AI-assisted (Claude Code); checked on Windows 11 / RTX 5090._
Marvisak commented 2026-11-25 02:06:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:24:46Z.


Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-gpu/src/host.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149518515 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1382#issuecomment-7149518515). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:24:46Z. --- <!-- victory-triage v1 head=bf8a9b23ec9c008e477c0e529b8b13539526720d verdict=needs-rebase at=2026-11-25T01:24:44Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-gpu/src/host.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1382:upstream-pr/1382'
git switch 'upstream-pr/1382'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1382'
git switch 'upstream-pr/1382'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1382'
git switch 'upstream-pr/1382'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1382'
git switch 'main'
git merge --squash 'upstream-pr/1382'
git switch 'main'
git merge --ff-only 'upstream-pr/1382'
git switch 'main'
git merge 'upstream-pr/1382'
git push origin 'main'
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
Marvisak/VIctory!1382
No description provided.