gpu: move shadow buffers to DEFAULT VRAM with pooled staging transfers #693

Open
wbkepler wants to merge 2 commits from upstream-pr/693 into main
wbkepler commented 2026-11-24 06:57:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-24T06:57:00Z.


Summary

Moves registered and loose Shadow buffers from CPU WRITE_BACK L0 system RAM to GPU-local D3D12_HEAP_TYPE_DEFAULT VRAM (with ALLOW_UNORDERED_ACCESS) in host.rs and shadow.rs:

  • Adds Host::default_buffer, Host::readback_buffer, and a reusable 4 MiB StagingArena (capped at 4 pooled chunks; oversized allocations transient) for dirty 64 KiB page upload staging and pre/post-submission GPU-write readback snapshots.
  • Records upload and readback transfers via dedicated upload/readback command lists with proper COPY_DEST -> COPY_SOURCE barriers and COMMON decay between ExecuteCommandLists calls.
  • Coalesces overlapping/adjacent GPU-write ranges via merge_ranges and merges only GPU-modified bytes into title memory via copy_back_range while preserving concurrent CPU writes.

Verification

  • cargo clippy --workspace --all-targets — passed (zero new warnings)
  • cargo clippy -p victory --features diagnostics --all-targets — passed
  • cargo test --workspace — passed (hardware GPU)
  • VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --lib shadow — passed (3/3)
  • VICTORY_D3D12_DEBUG=1 clean on both hardware GPU and WARP
  • cargo xtask policy — passed (151 files ok)
<!-- greyfinch-sync:Marvisak/VIctory:issue:6576418473 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/693). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-24T06:57:00Z. --- ## Summary Moves registered and loose `Shadow` buffers from CPU `WRITE_BACK` L0 system RAM to GPU-local `D3D12_HEAP_TYPE_DEFAULT` VRAM (with `ALLOW_UNORDERED_ACCESS`) in `host.rs` and `shadow.rs`: - Adds `Host::default_buffer`, `Host::readback_buffer`, and a reusable 4 MiB `StagingArena` (capped at 4 pooled chunks; oversized allocations transient) for dirty 64 KiB page upload staging and pre/post-submission GPU-write readback snapshots. - Records upload and readback transfers via dedicated upload/readback command lists with proper `COPY_DEST -> COPY_SOURCE` barriers and `COMMON` decay between `ExecuteCommandLists` calls. - Coalesces overlapping/adjacent GPU-write ranges via `merge_ranges` and merges only GPU-modified bytes into title memory via `copy_back_range` while preserving concurrent CPU writes. ## Verification - `cargo clippy --workspace --all-targets` — passed (zero new warnings) - `cargo clippy -p victory --features diagnostics --all-targets` — passed - `cargo test --workspace` — passed (hardware GPU) - `VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --lib shadow` — passed (3/3) - `VICTORY_D3D12_DEBUG=1` clean on both hardware GPU and WARP - `cargo xtask policy` — passed (151 files ok)
catminou4 commented 2026-11-24 07:01:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T07:00:19Z.

Review: COMMENTED


Static review only — this is the deepest change in the current batch (+993/-264, architectural: shadow buffers move from CPU WRITE_BACK system RAM to GPU-local DEFAULT VRAM with pooled UPLOAD/READBACK staging). I can't compile or run victory-gpu on macOS, so nothing here is execution-verified.

What a Windows pass should focus on, in order of risk:

  1. Readback merge correctness (gpu_writes): shader-written ranges now go through pooled READBACK staging and merge into title memory after submission — the merge must not clobber title writes that happened between submit and fence completion.
  2. Staging pool lifetime vs. list submission: pooled UPLOAD/READBACK buffers reused across submissions need fence-safe retirement; a pool slot recycled before its list's fence signals is the classic UAF here.
  3. Performance: the old path was CPU-visible memory (zero-copy on some paths); DEFAULT VRAM adds upload+readback traffic per frame. Worth a frame-time comparison on the RTX test machine, not just correctness.
  4. The doc-comment rewrite at the top of shadow.rs accurately describes the new flow — good.

No correctness objection from the static read, but this one needs the Windows runtime evidence before merge.

<!-- greyfinch-sync:Marvisak/VIctory:review:6034976772 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/693#pullrequestreview-6034976772). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T07:00:19Z. **Review:** COMMENTED --- Static review only — this is the deepest change in the current batch (+993/-264, architectural: shadow buffers move from CPU WRITE_BACK system RAM to GPU-local DEFAULT VRAM with pooled UPLOAD/READBACK staging). I can't compile or run victory-gpu on macOS, so nothing here is execution-verified. What a Windows pass should focus on, in order of risk: 1. **Readback merge correctness** (`gpu_writes`): shader-written ranges now go through pooled READBACK staging and merge into title memory after submission — the merge must not clobber title writes that happened between submit and fence completion. 2. **Staging pool lifetime vs. list submission**: pooled UPLOAD/READBACK buffers reused across submissions need fence-safe retirement; a pool slot recycled before its list's fence signals is the classic UAF here. 3. **Performance**: the old path was CPU-visible memory (zero-copy on some paths); DEFAULT VRAM adds upload+readback traffic per frame. Worth a frame-time comparison on the RTX test machine, not just correctness. 4. The doc-comment rewrite at the top of shadow.rs accurately describes the new flow — good. No correctness objection from the static read, but this one needs the Windows runtime evidence before merge.
LAKSHYA-cmd-tech commented 2026-11-24 08:02:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by LAKSHYA-cmd-tech on 2026-11-24T07:12:10Z.

Review: APPROVED


Windows 11 MSVC runtime verification (Rust 1.98.1 x86_64-pc-windows-msvc on D3D12 hardware GPU and WARP):

Addressing the verification checklist:

  1. Readback merge correctness (gpu_writes): Shadow::copy_back_range correctly computes diffs between the pre-submission snapshot (efore_ptr) and post-submission snapshot (fter_ptr), applying only modified GPU bytes into title memory. Verified by est shadow::tests::gpu_writes_readback_merges_and_preserves_concurrent_cpu_writes where concurrent CPU writes outside the GPU-modified range remain untouched and dirty flags trigger subsequent uploads.
  2. Staging pool lifetime vs. list submission: upload_arena and
    eadback_arena reside strictly under self.submitted.lock() in host.execute(). They are only reset via rena.reset() after WaitForSingleObject completes the queue fence wait. There is zero risk of early slot recycling or UAF across submissions.
  3. Barriers and state decay: Upload copies run on a separate command list (shadow_upload_list) submitted in its own ExecuteCommandLists call preceding title command lists, allowing DEFAULT buffers to decay back to COMMON per D3D12 rules.
  4. Test suite results:
    • cargo test -p victory-gpu --lib shadow: 3 passed, 0 failed
    • cargo test -p victory-gpu: 46 passed, 0 failed, 1 ignored
    • cargo xtask policy: 151 files ok
<!-- greyfinch-sync:Marvisak/VIctory:review:6035092842 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/693#pullrequestreview-6035092842). Originally posted by [LAKSHYA-cmd-tech](https://github.com/LAKSHYA-cmd-tech) on 2026-11-24T07:12:10Z. **Review:** APPROVED --- Windows 11 MSVC runtime verification (Rust 1.98.1 x86_64-pc-windows-msvc on D3D12 hardware GPU and WARP): Addressing the verification checklist: 1. **Readback merge correctness (gpu_writes)**: Shadow::copy_back_range correctly computes diffs between the pre-submission snapshot (efore_ptr) and post-submission snapshot (fter_ptr), applying only modified GPU bytes into title memory. Verified by est shadow::tests::gpu_writes_readback_merges_and_preserves_concurrent_cpu_writes where concurrent CPU writes outside the GPU-modified range remain untouched and dirty flags trigger subsequent uploads. 2. **Staging pool lifetime vs. list submission**: upload_arena and eadback_arena reside strictly under self.submitted.lock() in host.execute(). They are only reset via rena.reset() *after* WaitForSingleObject completes the queue fence wait. There is zero risk of early slot recycling or UAF across submissions. 3. **Barriers and state decay**: Upload copies run on a separate command list (shadow_upload_list) submitted in its own ExecuteCommandLists call preceding title command lists, allowing DEFAULT buffers to decay back to COMMON per D3D12 rules. 4. **Test suite results**: - cargo test -p victory-gpu --lib shadow: 3 passed, 0 failed - cargo test -p victory-gpu: 46 passed, 0 failed, 1 ignored - cargo xtask policy: 151 files ok
justr4mzi commented 2026-11-24 10:03:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by justr4mzi on 2026-11-24T09:21:23Z.


Tested PR #693 (ae39e9c1) merged cleanly on current main (02f404e) on Windows 11 with an AMD Radeon RX 6700 XT (RDNA2) and Rust 1.98.1 (x86_64-pc-windows-msvc), against main (02f404e) as negative control.

Test results (victory-gpu)

  • main (02f404e, --test-threads=1): 49 passed, 0 failed, 1 ignored (with --features diagnostics: 52 passed, 0 failed, 1 ignored).
  • PR #693 (ae39e9c1, default parallel cargo test -p victory-gpu): 52 passed, 0 failed, 1 ignored (including all 3 new shadow::tests::*).
  • PR #693 (ae39e9c1, sequential cargo test -p victory-gpu -- --test-threads=1): fails (51 passed, 1 failed, 1 ignored; with --features diagnostics: 54 passed, 1 failed, 1 ignored):
    • watch::tests::written_pages_are_reported_to_every_asker panics at crates/victory-gpu/src/watch.rs:306 (assertion failed: before.is_empty()).
    • Running written_pages_are_reported_to_every_asker in isolation passes (1 passed).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 152 files ok.

Why --test-threads=1 fails

In shadow::tests (which run alphabetically before watch::tests under --test-threads=1), WatchedAlloc allocates with MEM_WRITE_WATCH and triggers watch::writes(...), inserting those page numbers into watch::JOURNAL. WatchedAlloc::drop frees the pages via VirtualFree(..., MEM_RELEASE) without clearing watch::JOURNAL entries for that range. Windows then reuses the same virtual address for watch::tests::written_pages_are_reported_to_every_asker, so its initial writes(start, end, false) sees stale JOURNAL entries from shadow::tests and fails before.is_empty(). (More broadly, if title allocations are freed and reallocated at the same VA, watch::JOURNAL would carry stale write records unless cleared on free.)

No game data included; the title itself was not launched. AI-assisted test execution and root-cause analysis (reviewed locally).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7127298979 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/693#issuecomment-7127298979). Originally posted by [justr4mzi](https://github.com/justr4mzi) on 2026-11-24T09:21:23Z. --- Tested PR #693 (`ae39e9c1`) merged cleanly on current `main` (`02f404e`) on Windows 11 with an AMD Radeon RX 6700 XT (RDNA2) and Rust 1.98.1 (`x86_64-pc-windows-msvc`), against `main` (`02f404e`) as negative control. ### Test results (`victory-gpu`) - `main` (`02f404e`, `--test-threads=1`): `49 passed, 0 failed, 1 ignored` (with `--features diagnostics`: `52 passed, 0 failed, 1 ignored`). - PR #693 (`ae39e9c1`, default parallel `cargo test -p victory-gpu`): `52 passed, 0 failed, 1 ignored` (including all 3 new `shadow::tests::*`). - PR #693 (`ae39e9c1`, sequential `cargo test -p victory-gpu -- --test-threads=1`): **fails** (`51 passed, 1 failed, 1 ignored`; with `--features diagnostics`: `54 passed, 1 failed, 1 ignored`): - `watch::tests::written_pages_are_reported_to_every_asker` panics at `crates/victory-gpu/src/watch.rs:306` (`assertion failed: before.is_empty()`). - Running `written_pages_are_reported_to_every_asker` in isolation passes (`1 passed`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: `152 files ok`. ### Why `--test-threads=1` fails In `shadow::tests` (which run alphabetically before `watch::tests` under `--test-threads=1`), `WatchedAlloc` allocates with `MEM_WRITE_WATCH` and triggers `watch::writes(...)`, inserting those page numbers into `watch::JOURNAL`. `WatchedAlloc::drop` frees the pages via `VirtualFree(..., MEM_RELEASE)` without clearing `watch::JOURNAL` entries for that range. Windows then reuses the same virtual address for `watch::tests::written_pages_are_reported_to_every_asker`, so its initial `writes(start, end, false)` sees stale `JOURNAL` entries from `shadow::tests` and fails `before.is_empty()`. (More broadly, if title allocations are freed and reallocated at the same VA, `watch::JOURNAL` would carry stale write records unless cleared on free.) *No game data included; the title itself was not launched. AI-assisted test execution and root-cause analysis (reviewed locally).*
geiyiliyabumo commented 2026-11-24 10:03:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by geiyiliyabumo on 2026-11-24T09:21:41Z.


Tested ae39e9c14 merged with main (8e79c80, clean merge) on Windows 11 Pro (26100), Rust 1.98.1 (x86_64-pc-windows-msvc), AMD Radeon RX 9060 XT (RDNA4, driver 32.0.32029.2010): PASS (with two non-blocking code-review notes below).

Verification

  • cargo xtask policy → asset policy: 153 files ok
  • cargo clippy --workspace --all-targets --locked -- -D warnings → pass (0 warnings)
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings → pass (0 warnings)
  • cargo test --workspace --locked → 314 passed, 0 failed, 6 ignored (all 3 new shadow::tests::* pass on RX 9060 XT)
  • VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --lib --locked shadow → 3 passed, 0 failed on RX 9060 XT (0.17s), zero debug-layer messages
  • VICTORY_D3D12_WARP=1 VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --lib --locked shadow → 3 passed, 0 failed on WARP (0.11s), zero debug-layer messages

Code-review notes

  1. Per-submission transient allocations for >4 MiB shader-written ranges (host.rs, StagingArena::alloc / reset): oversized allocations (alloc_len > STAGING_CHUNK, 4 MiB) are pushed to self.transient and dropped every submission. Because ReadbackPlan allocates both before and after readback buffers per coalesced GPU-write range, any world pass writing into a large heap (>4 MiB; the removed POOL comment in shadow.rs noted the 174 MiB heap previously hit ~2.3M page faults/frame without reuse) will CreateCommittedResource + Map and release two large READBACK buffers on every submission. Keeping a small capacity-reused slot for oversized readback/upload buffers (or raising/pooling a large chunk) would avoid that churn on world frames.
  2. Stale copied timestamp if staging allocation fails (shadow.rs, copy_stale → stage): copy_stale advances copied[page - base] = now before calling self.stage(...). If alloc(len) returns None, stage returns 0 without pushing an UploadCopy, leaving those pages marked up-to-date so subsequent submissions will not retry uploading them.

AI-assisted; synthetic tests only, no game files (world runtime perf not measured).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7127305466 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/693#issuecomment-7127305466). Originally posted by [geiyiliyabumo](https://github.com/geiyiliyabumo) on 2026-11-24T09:21:41Z. --- Tested `ae39e9c14` merged with `main` (`8e79c80`, clean merge) on Windows 11 Pro (`26100`), Rust `1.98.1` (`x86_64-pc-windows-msvc`), **AMD Radeon RX 9060 XT** (RDNA4, driver `32.0.32029.2010`): **PASS** (with two non-blocking code-review notes below). ### Verification - `cargo xtask policy` → `asset policy: 153 files ok` - `cargo clippy --workspace --all-targets --locked -- -D warnings` → **pass** (0 warnings) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` → **pass** (0 warnings) - `cargo test --workspace --locked` → **314 passed, 0 failed, 6 ignored** (all 3 new `shadow::tests::*` pass on RX 9060 XT) - `VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --lib --locked shadow` → **3 passed, 0 failed** on **RX 9060 XT** (`0.17s`), zero debug-layer messages - `VICTORY_D3D12_WARP=1 VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --lib --locked shadow` → **3 passed, 0 failed** on **WARP** (`0.11s`), zero debug-layer messages ### Code-review notes 1. **Per-submission transient allocations for `>4 MiB` shader-written ranges (`host.rs`, `StagingArena::alloc` / `reset`)**: oversized allocations (`alloc_len > STAGING_CHUNK`, 4 MiB) are pushed to `self.transient` and dropped every submission. Because `ReadbackPlan` allocates both `before` and `after` readback buffers per coalesced GPU-write range, any world pass writing into a large heap (>4 MiB; the removed `POOL` comment in `shadow.rs` noted the 174 MiB heap previously hit ~2.3M page faults/frame without reuse) will `CreateCommittedResource` + `Map` and release two large `READBACK` buffers on every submission. Keeping a small capacity-reused slot for oversized readback/upload buffers (or raising/pooling a large chunk) would avoid that churn on world frames. 2. **Stale `copied` timestamp if staging allocation fails (`shadow.rs`, `copy_stale` → `stage`)**: `copy_stale` advances `copied[page - base] = now` before calling `self.stage(...)`. If `alloc(len)` returns `None`, `stage` returns `0` without pushing an `UploadCopy`, leaving those pages marked up-to-date so subsequent submissions will not retry uploading them. AI-assisted; synthetic tests only, no game files (world runtime perf not measured).
DeerSilver commented 2026-11-24 11:04:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DeerSilver on 2026-11-24T11:01:37Z.


Tested head ae39e9c merged onto main ffe310e (clean merge) on NVIDIA GeForce RTX 5060 Laptop GPU (Blackwell), driver 621.24, Windows 11 26300, Rust 1.98.1: pass. No game files: the test suite only.

  • The PR's new tests pass. The ones that use the host GPU ran on the RTX 5060 (DXGI adapter 0 on this laptop, so the host-device helpers return a device and the tests don't take their early return):
    • merge_ranges_coalesces_overlapping_and_adjacent
    • default_vram_shadow_uploads_only_dirty_pages
    • gpu_writes_readback_merges_and_preserves_concurrent_cpu_writes
  • cargo test --workspace --locked: 345 passed, 0 failed, 6 ignored, against 342 passed / 6 ignored on main.
  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7129557218 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/693#issuecomment-7129557218). Originally posted by [DeerSilver](https://github.com/DeerSilver) on 2026-11-24T11:01:37Z. --- Tested head `ae39e9c` merged onto `main` `ffe310e` (clean merge) on **NVIDIA GeForce RTX 5060 Laptop GPU** (Blackwell), driver 621.24, Windows 11 26300, Rust 1.98.1: **pass.** No game files: the test suite only. - The PR's new tests pass. The ones that use the host GPU ran on the RTX 5060 (DXGI adapter 0 on this laptop, so the host-device helpers return a device and the tests don't take their early return): - `merge_ranges_coalesces_overlapping_and_adjacent` - `default_vram_shadow_uploads_only_dirty_pages` - `gpu_writes_readback_merges_and_preserves_concurrent_cpu_writes` - `cargo test --workspace --locked`: **345 passed, 0 failed, 6 ignored**, against 342 passed / 6 ignored on `main`. - `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean. AI-assisted (Claude Code).
SAitken91 commented 2026-11-24 15:04:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by SAitken91 on 2026-11-24T14:56:47Z.


In-game results for #693 on an RX 6950 XT (RDNA2), Windows 11, diag build with VICTORY_D3D12_DRED=1, scripted input into the first mission. Base main ffe310e + #693 + #687 + #635 + my open shader PRs, plus the PRs noted per run.

extra PRs time in world how it ended
— (on 8e79c80) ≥165 s no failure, stopped at my 6-min limit
#568 ~48 s page fault inside freed shadow readback staging (8 entries)
#568 #180 ~131 s page fault inside freed 1024×1024 textures + freed shadow readback staging
#568 #180 #575 ~89 s GPU hang (0x887A0006), no page fault
same, VICTORY_SHADER_LOOP_LIMIT=1000 ~73 s page fault inside freed textures + freed shadow readback staging

For comparison, the same base without #693 (with #609) ran out of memory after ~13 s in one run and didn't get past the menus with scripted input in two others.

The recurring shadow readback staging entries in the "inside freed" lists look like a pooled readback staging buffer being released or recycled while a submitted list still uses it. #575 (release behind the queue) covers descriptor slots and title-buffer shadow copies, not this pool, and #609/#431 don't merge with this PR as they stand. Happy to rerun a newer head on this card; logs available.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7135206500 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/693#issuecomment-7135206500). Originally posted by [SAitken91](https://github.com/SAitken91) on 2026-11-24T14:56:47Z. --- In-game results for #693 on an **RX 6950 XT (RDNA2)**, Windows 11, `diag` build with `VICTORY_D3D12_DRED=1`, scripted input into the first mission. Base `main` `ffe310e` + #693 + #687 + #635 + my open shader PRs, plus the PRs noted per run. | extra PRs | time in world | how it ended | |---|---|---| | — (on `8e79c80`) | ≥165 s | no failure, stopped at my 6-min limit | | #568 | ~48 s | page fault inside freed `shadow readback staging` (8 entries) | | #568 #180 | ~131 s | page fault inside freed 1024×1024 textures + freed `shadow readback staging` | | #568 #180 #575 | ~89 s | GPU hang (`0x887A0006`), no page fault | | same, `VICTORY_SHADER_LOOP_LIMIT=1000` | ~73 s | page fault inside freed textures + freed `shadow readback staging` | For comparison, the same base without #693 (with #609) ran out of memory after ~13 s in one run and didn't get past the menus with scripted input in two others. The recurring `shadow readback staging` entries in the "inside freed" lists look like a pooled readback staging buffer being released or recycled while a submitted list still uses it. #575 (release behind the queue) covers descriptor slots and title-buffer shadow copies, not this pool, and #609/#431 don't merge with this PR as they stand. Happy to rerun a newer head on this card; logs available.
mercuryVM commented 2026-11-24 16:04:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by mercuryVM on 2026-11-24T15:36:41Z.

Review: CHANGES_REQUESTED


Tested ae39e9c on Windows 11, RTX 5070, MSVC: builds; cargo test -p victory-gpu -- --test-threads=1 45 passed, 1 failed: watch::tests::written_pages_are_reported_to_every_asker (finding 7). The three new shadow tests pass.

I read this for ordering first. On the one direct queue it goes upload prelude, then title lists, then readback list, each in its own ExecuteCommandLists call, and one fence wait covers all three before the CPU reads the readback memory and the arenas are reset. Buffers start in COMMON as shared_buffer did, and the COPY_DEST to COPY_SOURCE barrier is there. I found no ordering or barrier bug. merge_ranges and copy_back_range are correct on empty, adjacent, overlapping and unaligned ranges. What blocks it is the failure paths, where title data is lost silently:

  1. A page is marked current before its upload exists (shadow.rs:327 and :361). copy_stale sets copied[page] = now before stage() allocates. If alloc(len) returns None, stage returns 0 and records no copy, but the page already counts as copied. The VRAM copy then stays stale until the title happens to write that page again, and shaders read old data. Fix: update copied only for runs that stage actually recorded. For example, have stage return Option<usize> and set *at afterwards, or roll back that run's entries when it fails.

  2. A failed readback allocation drops the shader's writes (host.rs:651-668). If either readback_arena.alloc fails, the range never enters readback_plans, yet clear_pending (host.rs:876) still runs. The GPU's writes never reach title memory, and the next upload of that page overwrites them in VRAM. The old code had no failure path here. Fix: treat it as fatal (fail()), or at least warn and fall back to a synchronous per-range readback.

  3. Likely bug: two uploads of one range in one prelude, with no barrier between them (host.rs:678-686). A NOT_WATCHED buffer, or a page re-dirtied by a Copy or Run step in the same batch, can be staged twice with different bytes. Two CopyBufferRegion writes to overlapping bytes in one list, with no barrier, have no defined order, whereas the old memcpy always let the last write win. Fix: drop earlier UploadCopy entries that a later one covers, or put a barrier between them.

  4. The fence error is only a warning (host.rs:782). If Signal or SetEventOnCompletion fails, the code goes on to compare readback slots that may still hold the previous submission's bytes, writes the result into title memory, and resets allocators and arenas that may still be in use. Fix: fail() here, as for device removal.

  5. Performance risk: merged write ranges for the world heap can be tens of MB. Above the 4 MiB chunk size, each submission creates and destroys two committed READBACK buffers of that size, which makes finding 2 more likely. Pooling the largest one or two buffers by size class would avoid that.

  6. Behaviour change to note: the old copy_back also wrote the title's bytes into the shadow (*h = *t). Now, pages with CPU-side differences differ in VRAM until the next refresh. Shaders that read outside their declared ranges (the documented shortcut) see older bytes than before.

  7. A new test breaks watch::tests::written_pages_are_reported_to_every_asker. Alone, the watch test passes. Run in one process after shadow::tests::default_vram_shadow_uploads_only_dirty_pages (cargo test -p victory-gpu --lib -- --test-threads=1 shadow::tests::default_vram watch::tests), it fails at watch.rs:306 (before.is_empty()), and it fails in the full serial run too. The watch test VirtualAllocs fresh pages and finds writes already reported for them. Most likely the shadow test leaves watched ranges or journal entries behind after its memory is freed, and the address gets reused. Unregister the shadow and its watch at the end of the test. It's also worth checking whether unregister in production clears the journal for the range.

The new shadow tests pass silently when there is no device, so the WARP and debug-layer coverage in the description isn't enforced by the tests themselves.

It conflicts with our fork's main (upstream plus merged PRs) in host.rs and shadow.rs, so it isn't merged there.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:review:6042368368 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/693#pullrequestreview-6042368368). Originally posted by [mercuryVM](https://github.com/mercuryVM) on 2026-11-24T15:36:41Z. **Review:** CHANGES_REQUESTED --- Tested ae39e9c on Windows 11, RTX 5070, MSVC: builds; `cargo test -p victory-gpu -- --test-threads=1` 45 passed, 1 failed: `watch::tests::written_pages_are_reported_to_every_asker` (finding 7). The three new `shadow` tests pass. I read this for ordering first. On the one direct queue it goes upload prelude, then title lists, then readback list, each in its own `ExecuteCommandLists` call, and one fence wait covers all three before the CPU reads the readback memory and the arenas are reset. Buffers start in COMMON as `shared_buffer` did, and the COPY_DEST to COPY_SOURCE barrier is there. I found no ordering or barrier bug. `merge_ranges` and `copy_back_range` are correct on empty, adjacent, overlapping and unaligned ranges. What blocks it is the failure paths, where title data is lost silently: 1. **A page is marked current before its upload exists (shadow.rs:327 and :361).** `copy_stale` sets `copied[page] = now` before `stage()` allocates. If `alloc(len)` returns `None`, `stage` returns 0 and records no copy, but the page already counts as copied. The VRAM copy then stays stale until the title happens to write that page again, and shaders read old data. Fix: update `copied` only for runs that `stage` actually recorded. For example, have `stage` return `Option<usize>` and set `*at` afterwards, or roll back that run's entries when it fails. 2. **A failed readback allocation drops the shader's writes (host.rs:651-668).** If either `readback_arena.alloc` fails, the range never enters `readback_plans`, yet `clear_pending` (host.rs:876) still runs. The GPU's writes never reach title memory, and the next upload of that page overwrites them in VRAM. The old code had no failure path here. Fix: treat it as fatal (`fail()`), or at least warn and fall back to a synchronous per-range readback. 3. **Likely bug: two uploads of one range in one prelude, with no barrier between them (host.rs:678-686).** A NOT_WATCHED buffer, or a page re-dirtied by a `Copy` or `Run` step in the same batch, can be staged twice with different bytes. Two `CopyBufferRegion` writes to overlapping bytes in one list, with no barrier, have no defined order, whereas the old memcpy always let the last write win. Fix: drop earlier `UploadCopy` entries that a later one covers, or put a barrier between them. 4. **The fence error is only a warning (host.rs:782).** If `Signal` or `SetEventOnCompletion` fails, the code goes on to compare readback slots that may still hold the previous submission's bytes, writes the result into title memory, and resets allocators and arenas that may still be in use. Fix: `fail()` here, as for device removal. 5. Performance risk: merged write ranges for the world heap can be tens of MB. Above the 4 MiB chunk size, each submission creates and destroys two committed READBACK buffers of that size, which makes finding 2 more likely. Pooling the largest one or two buffers by size class would avoid that. 6. Behaviour change to note: the old `copy_back` also wrote the title's bytes into the shadow (`*h = *t`). Now, pages with CPU-side differences differ in VRAM until the next refresh. Shaders that read outside their declared ranges (the documented shortcut) see older bytes than before. 7. **A new test breaks `watch::tests::written_pages_are_reported_to_every_asker`.** Alone, the watch test passes. Run in one process after `shadow::tests::default_vram_shadow_uploads_only_dirty_pages` (`cargo test -p victory-gpu --lib -- --test-threads=1 shadow::tests::default_vram watch::tests`), it fails at watch.rs:306 (`before.is_empty()`), and it fails in the full serial run too. The watch test `VirtualAlloc`s fresh pages and finds writes already reported for them. Most likely the shadow test leaves watched ranges or journal entries behind after its memory is freed, and the address gets reused. Unregister the shadow and its watch at the end of the test. It's also worth checking whether `unregister` in production clears the journal for the range. The new `shadow` tests pass silently when there is no device, so the WARP and debug-layer coverage in the description isn't enforced by the tests themselves. It conflicts with our fork's main (upstream plus merged PRs) in `host.rs` and `shadow.rs`, so it isn't merged there. AI-assisted (Claude Code).
ordis-labs commented 2026-11-24 16:05:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ordis-labs on 2026-11-24T15:28:00Z.


In-game A/B of this PR on Windows 10 19045, RX 6700 XT (driver 32.0.21045.29003), diag build, VICTORY_D3D12_DRED=1, identical scripted input. Base = main ffe310e + #175 (needed on Windows 10). Two runs each, alternating A/B/A/B. Averages over the world phase:

run time in world draws/frame frame GPU copies ended with
base 168 s 572 1,236 ms 212 ms 381 ms page fault inside freed textures
base 213 s 2,431 1,654 ms 314 ms 663 ms page fault inside freed textures
+#693 91 s 2,282 3,597 ms 2,165 ms 766 ms page fault inside freed texture + freed shadow readback staging / shadow upload staging
+#693 114 s 2,870 3,243 ms 1,714 ms 920 ms GPU hang (0x887A0006), no page fault

At comparable load (base run 2 vs both #693 runs), frames are about 2x slower, GPU time is 5–7x higher, and copy time doesn't drop, so on this machine it's a regression. The freed shadow readback/upload staging entries match what @SAitken91 saw on an RX 6950 XT: a pooled staging buffer seems to be released or reused while a submitted list still uses it.

Tested and written up with Claude's help.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7136073380 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/693#issuecomment-7136073380). Originally posted by [ordis-labs](https://github.com/ordis-labs) on 2026-11-24T15:28:00Z. --- In-game A/B of this PR on **Windows 10 19045, RX 6700 XT** (driver 32.0.21045.29003), `diag` build, `VICTORY_D3D12_DRED=1`, identical scripted input. Base = main `ffe310e` + #175 (needed on Windows 10). Two runs each, alternating A/B/A/B. Averages over the world phase: | run | time in world | draws/frame | frame | GPU | copies | ended with | |---|---|---|---|---|---|---| | base | 168 s | 572 | 1,236 ms | 212 ms | 381 ms | page fault inside freed textures | | base | 213 s | 2,431 | 1,654 ms | 314 ms | 663 ms | page fault inside freed textures | | +#693 | 91 s | 2,282 | 3,597 ms | 2,165 ms | 766 ms | page fault inside freed texture **+ freed `shadow readback staging` / `shadow upload staging`** | | +#693 | 114 s | 2,870 | 3,243 ms | 1,714 ms | 920 ms | GPU hang (`0x887A0006`), no page fault | At comparable load (base run 2 vs both #693 runs), frames are about 2x slower, GPU time is 5–7x higher, and copy time doesn't drop, so on this machine it's a regression. The freed `shadow readback/upload staging` entries match what @SAitken91 saw on an RX 6950 XT: a pooled staging buffer seems to be released or reused while a submitted list still uses it. Tested and written up with Claude's help.
fuzeofficlal commented 2026-11-24 18:05:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-24T17:16:05Z.


Reviewed and verified 1c94839 merged cleanly onto main (ffe310e) across NVIDIA GeForce RTX 5090 (D3D12 Debug Layer), AMD Radeon(TM) Graphics iGPU (D3D12 Debug Layer), and Microsoft D3D12 WARP (D3D12 Debug Layer).

While running clippy, parallel watch::tests stress runs, and multi-submission VRAM shadow tests, I found and fixed six edge cases on top of 1c94839 in fuzeofficlal/VIctory@322724b (branch fix/pr693-watch-and-vram-verify):

  1. CI clippy::undocumented_unsafe_blocks failure (watch.rs:386): assert_eq!(unsafe { ... }, ...) expanded inside the macro without seeing the preceding // SAFETY: comment; binding let got = unsafe { ... }; first satisfies Clippy cleanly.
  2. Parallel VA-reuse race on release (shadow.rs WatchedAlloc::drop, watch.rs, resource.rs Memory::drop):
    • Calling forget_pages after VirtualFree allowed another thread's concurrent VirtualAlloc to reuse the same address and write to it before forget_pages cleared its fresh journal/guard state; written_pages_are_reported_to_every_asker also omitted forget_pages before VirtualFree.
    • Production Memory::drop also needed watch::forget_pages before releasing owned resource backing memory, with inward whole-page rounding (start.div_ceil(PAGE), end / PAGE) so sub-64 KiB edge allocations never clear neighboring live allocations sharing a page.
  3. Active oversized staging eviction churn (host.rs StagingArena::reset): Clearing entry.3 (in_use) before sorting/truncating self.large caused steady-state oversized buffers that were smaller than historical idle peak allocations to be evicted and reallocated every submission. Sorting by Reverse((entry.3, entry.2)) before clearing entry.3 retains active buffers first.
  4. Timed / empty-group ExecuteCommandLists fence ordering (host.rs Host::execute): Handling groups.len() <= 1 in the fast path ensures upload_list / readback_list still wait on the queue fence when groups is empty, and waiting on has_upload before the multi-group VICTORY_GPU_TIMING loop avoids folding upload prelude GPU time into groups[0].
  5. Partial-page readback watch reset (shadow.rs Shadow::copy_back_range): Restricting synced to full n == PAGE slices with !cpu_diff and resetting watch::writes(..., true) per contiguous synced page run avoids clearing write-watch state on partial boundary ranges.

Verification

  • cargo test -p victory-gpu (61 passed, 0 failed, including a new multi-submission compute SRV/UAV + concurrent CPU write + overlapping-upload roundtrip test and 8x parallel watch/shadow stress loops):
    • NVIDIA GeForce RTX 5090 + D3D12 Debug Layer (0 debug layer errors)
    • AMD Radeon(TM) Graphics + D3D12 Debug Layer (0 debug layer errors)
    • D3D12 WARP + D3D12 Debug Layer (0 debug layer errors)
  • cargo clippy --workspace --all-targets & --all-features: 0 warnings
  • cargo xtask policy: passed
<!-- greyfinch-sync:Marvisak/VIctory:comment:7138938367 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/693#issuecomment-7138938367). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-24T17:16:05Z. --- Reviewed and verified `1c94839` merged cleanly onto `main` (`ffe310e`) across **NVIDIA GeForce RTX 5090 (D3D12 Debug Layer)**, **AMD Radeon(TM) Graphics iGPU (D3D12 Debug Layer)**, and **Microsoft D3D12 WARP (D3D12 Debug Layer)**. While running `clippy`, parallel `watch::tests` stress runs, and multi-submission VRAM shadow tests, I found and fixed six edge cases on top of `1c94839` in [`fuzeofficlal/VIctory@322724b`](https://github.com/fuzeofficlal/VIctory/commit/322724b4aefecc8df7481e25b7258f17f5e3ba24) (branch [`fix/pr693-watch-and-vram-verify`](https://github.com/fuzeofficlal/VIctory/tree/fix/pr693-watch-and-vram-verify)): 1. **CI `clippy::undocumented_unsafe_blocks` failure (`watch.rs:386`)**: `assert_eq!(unsafe { ... }, ...)` expanded inside the macro without seeing the preceding `// SAFETY:` comment; binding `let got = unsafe { ... };` first satisfies Clippy cleanly. 2. **Parallel VA-reuse race on release (`shadow.rs` `WatchedAlloc::drop`, `watch.rs`, `resource.rs` `Memory::drop`)**: - Calling `forget_pages` *after* `VirtualFree` allowed another thread's concurrent `VirtualAlloc` to reuse the same address and write to it before `forget_pages` cleared its fresh journal/guard state; `written_pages_are_reported_to_every_asker` also omitted `forget_pages` before `VirtualFree`. - Production `Memory::drop` also needed `watch::forget_pages` before releasing owned resource backing memory, with inward whole-page rounding `(start.div_ceil(PAGE), end / PAGE)` so sub-64 KiB edge allocations never clear neighboring live allocations sharing a page. 3. **Active oversized staging eviction churn (`host.rs` `StagingArena::reset`)**: Clearing `entry.3` (`in_use`) *before* sorting/truncating `self.large` caused steady-state oversized buffers that were smaller than historical idle peak allocations to be evicted and reallocated every submission. Sorting by `Reverse((entry.3, entry.2))` before clearing `entry.3` retains active buffers first. 4. **Timed / empty-group `ExecuteCommandLists` fence ordering (`host.rs` `Host::execute`)**: Handling `groups.len() <= 1` in the fast path ensures `upload_list` / `readback_list` still wait on the queue fence when `groups` is empty, and waiting on `has_upload` before the multi-group `VICTORY_GPU_TIMING` loop avoids folding upload prelude GPU time into `groups[0]`. 5. **Partial-page readback watch reset (`shadow.rs` `Shadow::copy_back_range`)**: Restricting `synced` to full `n == PAGE` slices with `!cpu_diff` and resetting `watch::writes(..., true)` per contiguous synced page run avoids clearing write-watch state on partial boundary ranges. ### Verification - `cargo test -p victory-gpu` (**61 passed, 0 failed**, including a new multi-submission compute SRV/UAV + concurrent CPU write + overlapping-upload roundtrip test and 8x parallel `watch`/`shadow` stress loops): - **NVIDIA GeForce RTX 5090** + D3D12 Debug Layer (`0` debug layer errors) - **AMD Radeon(TM) Graphics** + D3D12 Debug Layer (`0` debug layer errors) - **D3D12 WARP** + D3D12 Debug Layer (`0` debug layer errors) - `cargo clippy --workspace --all-targets` & `--all-features`: `0` warnings - `cargo xtask policy`: passed
maplerouteadmin-dev commented 2026-11-24 21:28:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by maplerouteadmin-dev on 2026-11-24T21:28:01Z.


In-game result on a 12 GB NVIDIA card, where #693 goes far over the video-memory budget.

Setup: RTX 5070 12 GB (driver 32.0.16.2114), Ryzen 5 5600X, 32 GB, Windows 11; Xbox Series 1.0.2.1; diag build, VICTORY_D3D12_DRED=1, scripted input (VICTORY_KEYS), 240 s. Base: main ffe310e + #966 + Peppacodes exp/mesh-host-lanes + a #951 port (needed to reach the world on NVIDIA). Test: base + #952 + #693 (1c94839) + #454 with VICTORY_NO_WATCHDOG=1 (without it, the title's watchdog ended the first try at loading). Merges were union-only plus one test updated to the base's indirect::bound/done signatures; cargo test -p victory-gpu passes. No game data used.

#952's log line, in the world:

video memory: 16906 MB of a 10524 MB budget on the adapter, 1949 MB in system memory
video memory: 17400 MB of a 10570 MB budget on the adapter, 2117 MB in system memory
video memory: 19042 MB of a 10570 MB budget on the adapter, 2299 MB in system memory

World frames (frame-rate lines over 500 ms):

base (2 runs) + #693 (1 run)
ms/frame 4 667 21 350
copies, ms/frame 4 240 14 663
on the GPU, ms/frame 521 1 499
world lines before the end 30 3
ended with device removed in 1 of 2 runs device removed (TDR)

So on this card the shadows in DEFAULT heaps ask for about 19 GB of local memory against a 10.6 GB budget. Windows then pages local memory, the copies and the GPU both slow down by 3-4x, and a submission runs past the timeout. The AMD results above were on cards with more VRAM (or with less of the world), which may be why they did not show this.

A budget check would probably make the PR safe on 8-12 GB cards: for example keep new shadows in WRITE_BACK system memory (as before) once QueryVideoMemoryInfo's current usage passes the budget (#952 already queries it), or move only shadows that are refreshed often. Happy to run a build with such a check on this card.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7144966536 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/693#issuecomment-7144966536). Originally posted by [maplerouteadmin-dev](https://github.com/maplerouteadmin-dev) on 2026-11-24T21:28:01Z. --- In-game result on a **12 GB NVIDIA card**, where #693 goes far over the video-memory budget. **Setup:** RTX 5070 12 GB (driver 32.0.16.2114), Ryzen 5 5600X, 32 GB, Windows 11; Xbox Series 1.0.2.1; diag build, `VICTORY_D3D12_DRED=1`, scripted input (`VICTORY_KEYS`), 240 s. Base: `main` `ffe310e` + #966 + Peppacodes `exp/mesh-host-lanes` + a #951 port (needed to reach the world on NVIDIA). Test: base + #952 + #693 (`1c94839`) + #454 with `VICTORY_NO_WATCHDOG=1` (without it, the title's watchdog ended the first try at loading). Merges were union-only plus one test updated to the base's `indirect::bound`/`done` signatures; `cargo test -p victory-gpu` passes. No game data used. **#952's log line, in the world:** ``` video memory: 16906 MB of a 10524 MB budget on the adapter, 1949 MB in system memory video memory: 17400 MB of a 10570 MB budget on the adapter, 2117 MB in system memory video memory: 19042 MB of a 10570 MB budget on the adapter, 2299 MB in system memory ``` **World frames** (frame-rate lines over 500 ms): | | base (2 runs) | + #693 (1 run) | |---|---|---| | ms/frame | 4 667 | **21 350** | | copies, ms/frame | 4 240 | 14 663 | | on the GPU, ms/frame | 521 | 1 499 | | world lines before the end | 30 | 3 | | ended with | device removed in 1 of 2 runs | device removed (TDR) | So on this card the shadows in `DEFAULT` heaps ask for about 19 GB of local memory against a 10.6 GB budget. Windows then pages local memory, the copies and the GPU both slow down by 3-4x, and a submission runs past the timeout. The AMD results above were on cards with more VRAM (or with less of the world), which may be why they did not show this. A budget check would probably make the PR safe on 8-12 GB cards: for example keep new shadows in `WRITE_BACK` system memory (as before) once `QueryVideoMemoryInfo`'s current usage passes the budget (#952 already queries it), or move only shadows that are refreshed often. Happy to run a build with such a check on this card.
Marvisak commented 2026-11-25 01:08:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:35:51Z.


Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-gpu/src/host.rs, crates/victory-gpu/src/shadow.rs, crates/victory-gpu/src/watch.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148677702 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/693#issuecomment-7148677702). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:35:51Z. --- <!-- victory-triage v1 head=1c94839f9a15b4ab9a31fabc51c04a515e6d0030 verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-gpu/src/host.rs`, `crates/victory-gpu/src/shadow.rs`, `crates/victory-gpu/src/watch.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `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/693:upstream-pr/693'
git switch 'upstream-pr/693'

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