GPU: reuse unaligned root CBV scratch fills within a command list #3111

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

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


Summary

In draw::root_view, binding a root constant buffer whose host address is not 256-byte aligned allocated a fresh scratch upload slice (up to 64 KiB) and queued a new Step::Fill on every draw, even when the same (address, len) had already been queued for upload earlier in the same command list with no intervening title-memory write.

This adds a per-list scratch fill cache on HostList:

  • HostList::fill_scratch (crates/victory-gpu/src/host.rs): Caches (source_title_address, len) -> scratch_gpu_address in Deferred::filled. Repeated requests for the same (src, len) return the existing 256-byte-aligned scratch GPU address without allocating additional scratch space or queuing duplicate Step::Fill copies. Scratch allocation runs outside the Deferred lock to preserve the scratch -> deferred lock ordering when oversized allocations spill into deferred.uploads.
  • Invalidation on title-memory writes and list reset (crates/victory-gpu/src/host.rs): Deferred::title_written() clears both refreshed and filled atomically under the deferred lock in defer_copy (Step::Copy) and defer (Step::Run), so later draws observe updated title bytes without clobbering earlier scratch copies in the same submission. HostList::reset() replaces Deferred with Deferred::default(), clearing the cache along with the scratch cursor.
  • Root CBV integration (crates/victory-gpu/src/draw.rs): root_view uses list.fill_scratch(address as usize, len as usize) for unaligned root constant buffers.

Tests

  • Added host::tests::fill_scratch_reuses_same_range_and_invalidates_on_title_writes_and_reset covering 256-byte alignment, cache hits without growing scratch or Step::Fill count, distinct length/offset isolation, defer_host preservation, defer_copy/defer invalidation, and reset().
  • Added draw::tests::unaligned_root_cbv_caches_scratch_and_refreshes_after_title_copy verifying real GPU constant buffer readback before and after an in-list defer_copy update.
  • Verified:
    • cargo test -p victory-gpu --locked -- fill_scratch_reuses_same_range unaligned_root_cbv_caches_scratch list_scratch_grows_to_fit (3 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:6622585909 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3113). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T08:28:05Z. --- ### Summary In `draw::root_view`, binding a root constant buffer whose host address is not 256-byte aligned allocated a fresh scratch upload slice (up to 64 KiB) and queued a new `Step::Fill` on every draw, even when the same `(address, len)` had already been queued for upload earlier in the same command list with no intervening title-memory write. This adds a per-list scratch fill cache on `HostList`: - **`HostList::fill_scratch` (`crates/victory-gpu/src/host.rs`)**: Caches `(source_title_address, len) -> scratch_gpu_address` in `Deferred::filled`. Repeated requests for the same `(src, len)` return the existing 256-byte-aligned scratch GPU address without allocating additional scratch space or queuing duplicate `Step::Fill` copies. Scratch allocation runs outside the `Deferred` lock to preserve the `scratch -> deferred` lock ordering when oversized allocations spill into `deferred.uploads`. - **Invalidation on title-memory writes and list reset (`crates/victory-gpu/src/host.rs`)**: `Deferred::title_written()` clears both `refreshed` and `filled` atomically under the `deferred` lock in `defer_copy` (`Step::Copy`) and `defer` (`Step::Run`), so later draws observe updated title bytes without clobbering earlier scratch copies in the same submission. `HostList::reset()` replaces `Deferred` with `Deferred::default()`, clearing the cache along with the scratch cursor. - **Root CBV integration (`crates/victory-gpu/src/draw.rs`)**: `root_view` uses `list.fill_scratch(address as usize, len as usize)` for unaligned root constant buffers. ### Tests - Added `host::tests::fill_scratch_reuses_same_range_and_invalidates_on_title_writes_and_reset` covering 256-byte alignment, cache hits without growing scratch or `Step::Fill` count, distinct length/offset isolation, `defer_host` preservation, `defer_copy`/`defer` invalidation, and `reset()`. - Added `draw::tests::unaligned_root_cbv_caches_scratch_and_refreshes_after_title_copy` verifying real GPU constant buffer readback before and after an in-list `defer_copy` update. - Verified: - `cargo test -p victory-gpu --locked -- fill_scratch_reuses_same_range unaligned_root_cbv_caches_scratch list_scratch_grows_to_fit` (3 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:58 +00:00 (Migrated from github.com)

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

Review: APPROVED


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

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

Cleanly caches unaligned root CBV scratch fills within a command list and invalidates/refreshes after title copies.

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

<!-- greyfinch-sync:Marvisak/VIctory:review:6071036138 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3113#pullrequestreview-6071036138). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T08:54:44Z. **Review:** APPROVED --- Verified on RTX 5090 & WARP against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib draw:: -- --test-threads=1`: 21/21 passed on RTX 5090 and WARP (including `unaligned_root_cbv_caches_scratch_and_refreshes_after_title_copy`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly caches unaligned root CBV scratch fills within a command list and invalidates/refreshes after title copies. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
oni-swr commented 2026-11-26 09:06:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-26T08:47:52Z.


Independent test report for #3113 at head 394efa6a26d474dbd5517806ee6e30fd9c562723 (parent 63bcca43c6a00a758510398fa43096ce7e4f7df0). Claimed at https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7186881918. This is a test report only, not a source review. Synthetic data only; no game files.

Environment: Windows 11 (10.0.26100), rustc/cargo 1.98.1, cargo --locked -j 4, --features diagnostics (needed: VICTORY_D3D12_WARP is ignored without it), VICTORY_D3D12_WARP=1, Windows SDK 10.0.26100.0 dxil.dll on PATH. This machine does have a physical AMD adapter installed, so I did not rely on the default adapter. A temporary test, not part of the PR and since removed, required host() to be Some and checked the host device's LUID against EnumWarpAdapter. Both were Microsoft Basic Render Driver, flags 0x2 (software), vendor 0x1414, LUID {91142, 0}. The same check passed on the parent runs.

Results at head (WARP-verified):

  • host::tests::fill_scratch_reuses_same_range_and_invalidates_on_title_writes_and_reset: ok
  • draw::tests::unaligned_root_cbv_caches_scratch_and_refreshes_after_title_copy: ok
  • host::tests::list_scratch_grows_to_fit_and_reuses_fenced_chunks: ok
  • Full cargo test -p victory-gpu --features diagnostics --locked: 248 passed, 0 failed, 1 ignored.

Parent control (parent source plus only the PR's draw test, WARP-verified):

  • With the PR's assertions unchanged, the test fails at same unaligned root CBV reuses scratch copy. On the parent every draw gets its own scratch slice.
  • With only the two address-identity asserts replaced by prints, it passes. The readback still gives initial, initial, updated, so title-visible output is the same before and after the PR.
  • The printed addresses differ. The parent gave cbv1=0x300000000, cbv2=0x300000400, cbv3=0x300000800. The head gave cbv1=cbv2=0x300000000, cbv3=0x300000400.
  • The host test needs fill_scratch, so it cannot run on the parent.

Mutation controls at head, one scratch edit at a time, each reverted. Each run covered both new tests:

Mutation Result
title_written no longer clears filled both tests fail (in-list invalidation)
cache key ignores len host test fails (range isolation)
cache key ignores source address host test fails (range isolation)
no cache hit host test fails (same-range reuse)
reset() keeps filled host test fails (list reset)

5 of 5 mutants were detected. The draw test caught only the invalidation mutant, and the host test caught all five.

Not covered: physical GPUs (adapter forced to WARP only), concurrent or multi-threaded access, and title-memory writes through paths other than defer_copy and defer. Those two are the only invalidation paths this test exercises.

Tested by Claude Code (Claude Sonnet 5.5); the claim was coordinated by Codex (gpt-6.2-sol).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7187031278 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3113#issuecomment-7187031278). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-26T08:47:52Z. --- Independent test report for #3113 at head `394efa6a26d474dbd5517806ee6e30fd9c562723` (parent `63bcca43c6a00a758510398fa43096ce7e4f7df0`). Claimed at https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7186881918. This is a test report only, not a source review. Synthetic data only; no game files. **Environment:** Windows 11 (10.0.26100), rustc/cargo 1.98.1, `cargo --locked -j 4`, `--features diagnostics` (needed: `VICTORY_D3D12_WARP` is ignored without it), `VICTORY_D3D12_WARP=1`, Windows SDK 10.0.26100.0 `dxil.dll` on `PATH`. This machine does have a physical AMD adapter installed, so I did not rely on the default adapter. A temporary test, not part of the PR and since removed, required `host()` to be `Some` and checked the host device's LUID against `EnumWarpAdapter`. Both were `Microsoft Basic Render Driver`, flags `0x2` (software), vendor `0x1414`, LUID `{91142, 0}`. The same check passed on the parent runs. **Results at head (WARP-verified):** - `host::tests::fill_scratch_reuses_same_range_and_invalidates_on_title_writes_and_reset`: ok - `draw::tests::unaligned_root_cbv_caches_scratch_and_refreshes_after_title_copy`: ok - `host::tests::list_scratch_grows_to_fit_and_reuses_fenced_chunks`: ok - Full `cargo test -p victory-gpu --features diagnostics --locked`: 248 passed, 0 failed, 1 ignored. **Parent control** (parent source plus only the PR's draw test, WARP-verified): - With the PR's assertions unchanged, the test fails at `same unaligned root CBV reuses scratch copy`. On the parent every draw gets its own scratch slice. - With only the two address-identity asserts replaced by prints, it passes. The readback still gives `initial, initial, updated`, so title-visible output is the same before and after the PR. - The printed addresses differ. The parent gave `cbv1=0x300000000, cbv2=0x300000400, cbv3=0x300000800`. The head gave `cbv1=cbv2=0x300000000, cbv3=0x300000400`. - The host test needs `fill_scratch`, so it cannot run on the parent. **Mutation controls** at head, one scratch edit at a time, each reverted. Each run covered both new tests: | Mutation | Result | |---|---| | `title_written` no longer clears `filled` | both tests fail (in-list invalidation) | | cache key ignores `len` | host test fails (range isolation) | | cache key ignores source address | host test fails (range isolation) | | no cache hit | host test fails (same-range reuse) | | `reset()` keeps `filled` | host test fails (list reset) | 5 of 5 mutants were detected. The draw test caught only the invalidation mutant, and the host test caught all five. **Not covered:** physical GPUs (adapter forced to WARP only), concurrent or multi-threaded access, and title-memory writes through paths other than `defer_copy` and `defer`. Those two are the only invalidation paths this test exercises. Tested by Claude Code (Claude Sonnet 5.5); the claim was coordinated by Codex (gpt-6.2-sol).
Marvisak commented 2026-11-26 10:05:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T09:19:56Z.


Automated triage for the maintainers.

Merge candidate. HostList::fill_scratch caches (source, len) -> scratch address in Deferred::filled, so draw::root_view no longer allocates scratch and queues a Step::Fill for every draw that binds the same unaligned root CBV. On main, every Step::Fill copies at submission. The only steps that write title memory in between are Step::Copy and Step::Run (from defer_copy and defer), and both now call Deferred::title_written, so a reused copy holds the same bytes a fresh fill would. reset replaces Deferred and rewinds the scratch cursor, so the cache cannot outlive its memory, and the insert keeps the existing lock order. The readback test unaligned_root_cbv_caches_scratch_and_refreshes_after_title_copy checks the actual result before and after an in-list copy. Note: #3114 adds its own Deferred::title_written, so whichever lands second needs a small rebase that clears both caches.

CI: check passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7187799864 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3113#issuecomment-7187799864). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T09:19:56Z. --- <!-- victory-triage v1 head=394efa6a26d474dbd5517806ee6e30fd9c562723 verdict=merge-candidate at=2026-11-26T09:21:00Z --> Automated triage for the maintainers. **Merge candidate.** `HostList::fill_scratch` caches `(source, len) -> scratch address` in `Deferred::filled`, so `draw::root_view` no longer allocates scratch and queues a `Step::Fill` for every draw that binds the same unaligned root CBV. On `main`, every `Step::Fill` copies at submission. The only steps that write title memory in between are `Step::Copy` and `Step::Run` (from `defer_copy` and `defer`), and both now call `Deferred::title_written`, so a reused copy holds the same bytes a fresh fill would. `reset` replaces `Deferred` and rewinds the scratch cursor, so the cache cannot outlive its memory, and the insert keeps the existing lock order. The readback test `unaligned_root_cbv_caches_scratch_and_refreshes_after_title_copy` checks the actual result before and after an in-list copy. Note: #3114 adds its own `Deferred::title_written`, so whichever lands second needs a small rebase that clears both caches. CI: `check` passing. --- _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/3113:upstream-pr/3113'
git switch 'upstream-pr/3113'

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