GPU: cache redundant render target binds on host command lists #3105

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3107 into main
fuzeofficlal commented 2026-11-26 08:18:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T08:18:06Z.


Summary

draw::State::set_targets previously allocated new host RTV/DSV descriptors from the global CPU descriptor rings and recorded OMSetRenderTargets on every call, even when the title repeatedly bound the exact same render targets and depth stencil view on the same command list.

This adds a per-list host target binding cache in draw::State, matching the existing host_pipeline / host_roots / host_heaps caches:

  • Resolved view key (crates/victory-gpu/src/descriptor.rs, crates/victory-gpu/src/draw.rs): RtvRecord::rtv_words and RtvRecord::dsv_words expose the exact 24-byte host descriptor words passed to CreateRenderTargetView / CreateDepthStencilView (after view_format / pc_format normalization). Each cached entry stores the resolved ID3D12Resource and Option<[u32; 6]> description words, keeping the cached COM references alive while bound so a destroyed resource's raw pointer cannot be reused (ABA), and detecting in-place descriptor overwrites at the same title handle.
  • Zero-allocation hit path with full state fidelity (crates/victory-gpu/src/draw.rs): State::set_targets still updates fixed.targets (reusing its Vec capacity), self.targets, self.depth, and diagnostic capture::note_target calls on every invocation. When the resolved host textures and view descriptions match host_targets.bound, it clears its scratch vector and skips host.rtv, host.dsv, and OMSetRenderTargets.
  • Complete invalidation: host_targets.bound starts as None (so an initial empty bind is not skipped) and is cleared on State::reset (command list Reset / ResetX) and State::host_state_lost (segment cut onto a new HostList, and external::record after host middleware records directly into the list).

Tests

  • Added draw::tests::redundant_set_targets_reuses_binding_and_invalidates_on_any_change covering initial bind, repeated hits, distinct handles to the same texture/desc, target swaps, in-place RTV mip and DSV flag overwrites at the same handle, empty binds, host_state_lost, cut(), and reset().
  • Added draw::tests::cached_render_targets_render_expected_pixels_across_redundant_binds_and_cut performing real D3D12 draws and pixel readbacks across redundant binds, target switches, in-place handle retargeting, and segment cut().
  • Verified:
    • cargo test -p victory-gpu --locked -- redundant_set_targets_reuses_binding cached_render_targets_render_expected_pixels (2 passed on RTX 5090)
    • VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --features diagnostics --locked (248 passed, 0 failed)
    • cargo clippy --workspace --all-targets --locked -- -D warnings
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
    • cargo test -p xtask --locked policy::tests::repository_passes_policy
<!-- greyfinch-sync:Marvisak/VIctory:issue:6622430209 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3107). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T08:18:06Z. --- ### Summary `draw::State::set_targets` previously allocated new host RTV/DSV descriptors from the global CPU descriptor rings and recorded `OMSetRenderTargets` on every call, even when the title repeatedly bound the exact same render targets and depth stencil view on the same command list. This adds a per-list host target binding cache in `draw::State`, matching the existing `host_pipeline` / `host_roots` / `host_heaps` caches: - **Resolved view key (`crates/victory-gpu/src/descriptor.rs`, `crates/victory-gpu/src/draw.rs`)**: `RtvRecord::rtv_words` and `RtvRecord::dsv_words` expose the exact 24-byte host descriptor words passed to `CreateRenderTargetView` / `CreateDepthStencilView` (after `view_format` / `pc_format` normalization). Each cached entry stores the resolved `ID3D12Resource` and `Option<[u32; 6]>` description words, keeping the cached COM references alive while bound so a destroyed resource's raw pointer cannot be reused (ABA), and detecting in-place descriptor overwrites at the same title handle. - **Zero-allocation hit path with full state fidelity (`crates/victory-gpu/src/draw.rs`)**: `State::set_targets` still updates `fixed.targets` (reusing its `Vec` capacity), `self.targets`, `self.depth`, and diagnostic `capture::note_target` calls on every invocation. When the resolved host textures and view descriptions match `host_targets.bound`, it clears its scratch vector and skips `host.rtv`, `host.dsv`, and `OMSetRenderTargets`. - **Complete invalidation**: `host_targets.bound` starts as `None` (so an initial empty bind is not skipped) and is cleared on `State::reset` (command list `Reset` / `ResetX`) and `State::host_state_lost` (segment `cut` onto a new `HostList`, and `external::record` after host middleware records directly into the list). ### Tests - Added `draw::tests::redundant_set_targets_reuses_binding_and_invalidates_on_any_change` covering initial bind, repeated hits, distinct handles to the same texture/desc, target swaps, in-place RTV mip and DSV flag overwrites at the same handle, empty binds, `host_state_lost`, `cut()`, and `reset()`. - Added `draw::tests::cached_render_targets_render_expected_pixels_across_redundant_binds_and_cut` performing real D3D12 draws and pixel readbacks across redundant binds, target switches, in-place handle retargeting, and segment `cut()`. - Verified: - `cargo test -p victory-gpu --locked -- redundant_set_targets_reuses_binding cached_render_targets_render_expected_pixels` (2 passed on RTX 5090) - `VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --features diagnostics --locked` (248 passed, 0 failed) - `cargo clippy --workspace --all-targets --locked -- -D warnings` - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` - `cargo test -p xtask --locked policy::tests::repository_passes_policy`
Dukestar69 commented 2026-11-26 09:05:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T08:28:49Z.

Review: APPROVED


Verified on RTX 5090 & WARP against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 22/22 passed on RTX 5090 and WARP (including cached_render_targets_render_expected_pixels_across_redundant_binds_and_cut and redundant_set_targets_reuses_binding_and_invalidates_on_any_change).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly caches redundant render target binds on host command lists and invalidates on any target/depth/cut change.

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

<!-- greyfinch-sync:Marvisak/VIctory:review:6070699361 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3107#pullrequestreview-6070699361). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T08:28:49Z. **Review:** APPROVED --- Verified on RTX 5090 & WARP against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib draw:: -- --test-threads=1`: 22/22 passed on RTX 5090 and WARP (including `cached_render_targets_render_expected_pixels_across_redundant_binds_and_cut` and `redundant_set_targets_reuses_binding_and_invalidates_on_any_change`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly caches redundant render target binds on host command lists and invalidates on any target/depth/cut change. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Marvisak commented 2026-11-26 09:06:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T08:32:16Z.


Automated triage for the maintainers.

Verdict: needs work. CI check passes.

This PR caches the host render target binding in draw::State::set_targets (new HostTargets / BoundTarget). It skips host.rtv / host.dsv and OMSetRenderTargets when the resolved host textures and view words are unchanged. The cache is cleared in State::reset and State::host_state_lost. By my reading the invalidation is complete. I found no other OMSetRenderTargets on title lists in main besides the one in set_targets, and holding the ID3D12Resource avoids pointer reuse.

What it needs:

  • A measured benefit. This is a performance change with no numbers. Please show from a run of the title how often redundant binds occur and what they cost: CPU time in set_targets, or descriptor ring pressure from host.rtv / host.dsv. Without that, the extra state and 540 lines (mostly tests) are hard to justify.
  • It touches the same code in set_targets as #3109 by the same author. Once one of them lands, the other will need a rebase.

With measurements from the title showing a real cost, this would be reviewed for merge.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7186713953 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3107#issuecomment-7186713953). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T08:32:16Z. --- <!-- victory-triage v1 head=119a6d9faa03aa3d6fb70a2fdc55aa21a98527f9 verdict=needs-work at=2026-11-26T08:31:06Z --> Automated triage for the maintainers. **Verdict: needs work.** CI `check` passes. This PR caches the host render target binding in `draw::State::set_targets` (new `HostTargets` / `BoundTarget`). It skips `host.rtv` / `host.dsv` and `OMSetRenderTargets` when the resolved host textures and view words are unchanged. The cache is cleared in `State::reset` and `State::host_state_lost`. By my reading the invalidation is complete. I found no other `OMSetRenderTargets` on title lists in main besides the one in `set_targets`, and holding the `ID3D12Resource` avoids pointer reuse. What it needs: - **A measured benefit.** This is a performance change with no numbers. Please show from a run of the title how often redundant binds occur and what they cost: CPU time in `set_targets`, or descriptor ring pressure from `host.rtv` / `host.dsv`. Without that, the extra state and 540 lines (mostly tests) are hard to justify. - It touches the same code in `set_targets` as #3109 by the same author. Once one of them lands, the other will need a rebase. With measurements from the title showing a real cost, this would be reviewed for merge. --- _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/3107:upstream-pr/3107'
git switch 'upstream-pr/3107'

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/3107'
git switch 'upstream-pr/3107'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3107'
git switch 'upstream-pr/3107'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3107'
git switch 'main'
git merge --squash 'upstream-pr/3107'
git switch 'main'
git merge --ff-only 'upstream-pr/3107'
git switch 'main'
git merge 'upstream-pr/3107'
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!3105
No description provided.