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

Open
github-bot wants to merge 1 commit from github-bot/gh-3113 into main AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 08:28 UTC · original
fuzeofficlal:perf/gpu-cache-unaligned-root-cbv-scratch → main · +252 −7 · 2 files · 1 commits · open

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
**@⁠fuzeofficlal** on GitHub · 2026-11-26 08:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/3113) `fuzeofficlal:perf/gpu-cache-unaligned-root-cbv-scratch` → `main` · +252 −7 · 2 files · 1 commits · open ### 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`
Cache (source_address, len) -> scratch GPU address in HostList's Deferred::filled via HostList::fill_scratch so repeated binds of the same unaligned root constant buffer within a command list avoid redundant scratch allocations and Step::Fill uploads. Allocate scratch outside the Deferred lock to avoid lock-order inversion with oversized scratch uploads, invalidate cached fills alongside refreshes in Deferred::title_written() on defer_copy and defer, and clear them automatically on HostList::reset.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:34:09 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 08:47 UTC · original

Independent test report for #3113 at head 394efa6a26d474dbd5517806ee6e30fd9c562723 (parent 63bcca43c6a00a758510398fa43096ce7e4f7df0). Claimed at #1633. 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).

**@⁠oni-swr** on GitHub · 2026-11-26 08:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/3113#issuecomment-7187031278) Independent test report for #3113 at head `394efa6a26d474dbd5517806ee6e30fd9c562723` (parent `63bcca43c6a00a758510398fa43096ce7e4f7df0`). Claimed at https://git.gtahub.st/github/Marvisak--VIctory/issues/1633. 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).
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 08:54 UTC · original · approved these changes

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

**@⁠Dukestar69** on GitHub · 2026-11-26 08:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/3113#pullrequestreview-6071036138) · **approved these changes** 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).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 09:19 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-26 09:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/3113#issuecomment-7187799864) <!-- 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 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/3113/head:github-bot/gh-3113'
git switch 'github-bot/gh-3113'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3113'
git switch 'github-bot/gh-3113'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3113'
git switch 'github-bot/gh-3113'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3113'
git switch 'main'
git merge --squash 'github-bot/gh-3113'
git switch 'main'
git merge --ff-only 'github-bot/gh-3113'
git switch 'main'
git merge 'github-bot/gh-3113'
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!3113
No description provided.