GPU: eliminate redundant per-draw/dispatch D3D12 state calls, cache ID3D12GraphicsCommandList6, and remove double shadow::find #2217

Closed
github-bot wants to merge 1 commit from gh-pr/2217/head into gh-pr/2217/base
Owner

@⁠RVZO6 on GitHub · 2026-11-25 08:06 UTC · original
RVZO6:rvzo/draw-state-dedup → main · +777 −153 · 3 files · 1 commits · closed without merging

Every draw, mesh dispatch, and compute dispatch currently re-issues redundant D3D12 command-list state calls even when the host command list already has the exact same state bound:

  1. State::bind() re-emits all root CBVs, SRVs, UAVs, and 32-bit constants (including p.marks, VIEW_STRIDES, view_addresses, and memory tables) on every draw/dispatch even when the root signature and values are unchanged.
  2. State::prepare_for() re-scans all vertex buffers and calls IASetVertexBuffers / IASetIndexBuffer on every draw even when VB/IB bindings, p.slots, and HostList deferred copies have not changed.
  3. list.rs forwards IASetPrimitiveTopology, RSSetViewports, RSSetScissorRects, OMSetBlendFactor, and OMSetStencilRef straight onto the host command list without deduplication.
  4. State::dispatch_mesh() calls list.list.cast::<ID3D12GraphicsCommandList6>() (QueryInterface) on every mesh dispatch.
  5. readable() calls shadow::find() twice for View::Constant even though len <= s.left(address) guarantees the first lookup already covers [address, address + len).

Claimed on #1633 (https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7155911614).

The change

  • crates/victory-gpu/src/host.rs:
    • Cache list6: Option<ID3D12GraphicsCommandList6> once on HostList in Host::new_list().
    • Add monotonic copy_epoch: AtomicU64 + HostList::copy_epoch(), bumped in defer_copy, defer, defer_gpu_write, and reset.
  • crates/victory-gpu/src/draw.rs & crates/victory-gpu/src/list.rs:
    • Add a pure, unit-testable HostStateCache in draw.rs:
      • Deduplicates graphics and compute root CBV/SRV/UAV/32-bit-constant bindings (bound_roots: [Vec<BoundParam>; 2]) while keeping all shadow::refresh, shadow::gpu_writes, memory_map, and map.precise side effects intact. Clears affected slots on root-signature switch, descriptor-heap switch, compute rebind (take_rebind() / execute_indirect), host_state_lost(), and reset(). Validates p.marks up front and binds after for stage so depth-twin compute rebinds during tables() cannot leave compute mark bindings unset.
      • Tracks vb_dirty, ib_dirty, bound_vb_slots, bound_vb_epoch, and bound_ib_epoch against list.copy_epoch(), invalidating on VB/IB mutations, rectangle-list scratch expansion, p.slots expansion, copy_epoch advances, host_state_lost(), and reset().
      • Deduplicates topology, viewports (bitwise f32::to_bits), scissors, blend_factor (bitwise f32::to_bits, normalizing None to [1.0; 4] per D3D12), and stencil_ref routed through list.rs with_state.
    • Switch State::dispatch_mesh() to use cached list.list6.as_ref().
    • Remove the redundant second shadow::find(address, len) in readable() for View::Constant.
    • Add 3 unit tests: host_state_cache_dedups_root_params_and_invalidates_on_switches, host_state_cache_tracks_vb_ib_slots_mutations_and_copy_epoch, and host_state_cache_dedups_fixed_function_state_bitwise.

Not a duplicate of

No open PR or CLAIMS.md entry deduplicates per-draw/dispatch root parameters, VB/IB binds, or fixed-function D3D12 state, caches ID3D12GraphicsCommandList6 on HostList, or removes the double shadow::find in readable().

Tested

  • Fork windows-latest MSVC + Windows WARP SM 6.6 CI run 45572159221 (Clippy clean, Clippy, diagnostics build clean, Tests: 715 passed; 0 failed; 10 ignored, victory-gpu: 157 passed; 0 failed; 2 ignored + 1 passed, xtask: 6 passed; 0 failed):
    • draw::tests::host_state_cache_dedups_root_params_and_invalidates_on_switches ... ok
    • draw::tests::host_state_cache_tracks_vb_ib_slots_mutations_and_copy_epoch ... ok
    • draw::tests::host_state_cache_dedups_fixed_function_state_bitwise ... ok

Not run: the title; any hardware GPU. The title was not used in these tests.

AI-assisted: T3 Code (Antigravity, gemini-4-neon)

**@⁠RVZO6** on GitHub · 2026-11-25 08:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/2217) `RVZO6:rvzo/draw-state-dedup` → `main` · +777 −153 · 3 files · 1 commits · closed without merging Every draw, mesh dispatch, and compute dispatch currently re-issues redundant D3D12 command-list state calls even when the host command list already has the exact same state bound: 1. `State::bind()` re-emits all root CBVs, SRVs, UAVs, and 32-bit constants (including `p.marks`, `VIEW_STRIDES`, `view_addresses`, and `memory` tables) on every draw/dispatch even when the root signature and values are unchanged. 2. `State::prepare_for()` re-scans all vertex buffers and calls `IASetVertexBuffers` / `IASetIndexBuffer` on every draw even when VB/IB bindings, `p.slots`, and `HostList` deferred copies have not changed. 3. `list.rs` forwards `IASetPrimitiveTopology`, `RSSetViewports`, `RSSetScissorRects`, `OMSetBlendFactor`, and `OMSetStencilRef` straight onto the host command list without deduplication. 4. `State::dispatch_mesh()` calls `list.list.cast::<ID3D12GraphicsCommandList6>()` (`QueryInterface`) on every mesh dispatch. 5. `readable()` calls `shadow::find()` twice for `View::Constant` even though `len <= s.left(address)` guarantees the first lookup already covers `[address, address + len)`. Claimed on #1633 (`https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7155911614`). ## The change - `crates/victory-gpu/src/host.rs`: - Cache `list6: Option<ID3D12GraphicsCommandList6>` once on `HostList` in `Host::new_list()`. - Add monotonic `copy_epoch: AtomicU64` + `HostList::copy_epoch()`, bumped in `defer_copy`, `defer`, `defer_gpu_write`, and `reset`. - `crates/victory-gpu/src/draw.rs` & `crates/victory-gpu/src/list.rs`: - Add a pure, unit-testable `HostStateCache` in `draw.rs`: - Deduplicates graphics and compute root CBV/SRV/UAV/32-bit-constant bindings (`bound_roots: [Vec<BoundParam>; 2]`) while keeping all `shadow::refresh`, `shadow::gpu_writes`, `memory_map`, and `map.precise` side effects intact. Clears affected slots on root-signature switch, descriptor-heap switch, compute rebind (`take_rebind()` / `execute_indirect`), `host_state_lost()`, and `reset()`. Validates `p.marks` up front and binds after `for stage` so depth-twin compute rebinds during `tables()` cannot leave compute mark bindings unset. - Tracks `vb_dirty`, `ib_dirty`, `bound_vb_slots`, `bound_vb_epoch`, and `bound_ib_epoch` against `list.copy_epoch()`, invalidating on VB/IB mutations, rectangle-list scratch expansion, `p.slots` expansion, `copy_epoch` advances, `host_state_lost()`, and `reset()`. - Deduplicates `topology`, `viewports` (bitwise `f32::to_bits`), `scissors`, `blend_factor` (bitwise `f32::to_bits`, normalizing `None` to `[1.0; 4]` per D3D12), and `stencil_ref` routed through `list.rs` `with_state`. - Switch `State::dispatch_mesh()` to use cached `list.list6.as_ref()`. - Remove the redundant second `shadow::find(address, len)` in `readable()` for `View::Constant`. - Add 3 unit tests: `host_state_cache_dedups_root_params_and_invalidates_on_switches`, `host_state_cache_tracks_vb_ib_slots_mutations_and_copy_epoch`, and `host_state_cache_dedups_fixed_function_state_bitwise`. ## Not a duplicate of No open PR or `CLAIMS.md` entry deduplicates per-draw/dispatch root parameters, VB/IB binds, or fixed-function D3D12 state, caches `ID3D12GraphicsCommandList6` on `HostList`, or removes the double `shadow::find` in `readable()`. ## Tested - Fork `windows-latest` MSVC + Windows WARP SM 6.6 CI run [`45572159221`](https://github.com/RVZO6/VIctory/actions/runs/45572159221) (`Clippy` clean, `Clippy, diagnostics build` clean, `Tests`: **`715 passed; 0 failed; 10 ignored`**, `victory-gpu`: **`157 passed; 0 failed; 2 ignored`** + `1 passed`, `xtask`: **`6 passed; 0 failed`**): - `draw::tests::host_state_cache_dedups_root_params_and_invalidates_on_switches ... ok` - `draw::tests::host_state_cache_tracks_vb_ib_slots_mutations_and_copy_epoch ... ok` - `draw::tests::host_state_cache_dedups_fixed_function_state_bitwise ... ok` **Not run:** the title; any hardware GPU. The title was not used in these tests. AI-assisted: T3 Code (Antigravity, gemini-4-neon)
Cache ID3D12GraphicsCommandList6 once on HostList for DispatchMesh, add a pure unit-testable HostStateCache in draw.rs (deduplicating root parameters, VB/IB binds via HostList::copy_epoch, and bitwise fixed-function state routed from list.rs), and remove the redundant second shadow::find on View::Constant in readable().

Assisted-by: T3 Code (Antigravity, gemini-4-neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:58:46 +00:00
github-bot closed this pull request 2026-11-26 04:58:46 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 13:30 UTC · original · edited 2026-11-25 16:04 UTC

Automated triage for the maintainers.

This pull request no longer merges cleanly into main (conflicts in crates/victory-gpu/src/draw.rs, crates/victory-gpu/src/host.rs, crates/victory-gpu/src/list.rs). It will be reviewed once it is rebased onto the current main. The earlier verdict (needs-work) was for an older state of main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 13:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/2217#issuecomment-7163878586) · edited 2026-11-25 16:04 UTC <!-- victory-triage v1 head=86e18a71f47ac6323623f99e9f7ad8feb634461e verdict=needs-rebase at=2026-11-25T16:16:00Z --> Automated triage for the maintainers. This pull request no longer merges cleanly into `main` (conflicts in `crates/victory-gpu/src/draw.rs`, `crates/victory-gpu/src/host.rs`, `crates/victory-gpu/src/list.rs`). It will be reviewed once it is rebased onto the current `main`. The earlier verdict (needs-work) was for an older state of `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠RVZO6 on GitHub · 2026-11-25 17:14 UTC · original

Closing to keep the queue clean: draw.rs/host.rs moved significantly on 13f91ca and I do not have a title run to profile per-draw D3D12 state savings.

**@⁠RVZO6** on GitHub · 2026-11-25 17:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/2217#issuecomment-7169202330) Closing to keep the queue clean: `draw.rs`/`host.rs` moved significantly on `13f91ca` and I do not have a title run to profile per-draw D3D12 state savings.

Pull request closed

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!2217
No description provided.