GPU: avoid quadratic shadow snapshotting in Shadow::writing #3318

Open
danielibabet wants to merge 3 commits from upstream-pr/3318 into main
danielibabet commented 2026-11-26 17:28:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by danielibabet on 2026-11-26T17:28:12Z.


Summary

  • Make Shadow::writing insert only uncovered gaps via partition_point instead of draining and re-copying already-snapshotted bytes on every overlapping GpuWrite (O(N^2) -> O(N) per frame).
  • Reuse pooled(PIECE) / give_back for the title readback scratch buffer in Shadow::copy_back_or_not and iterate across adjacent snapshot segments covering [from, to) without needing contiguous merged vectors.
  • Add unit test overlapping_snapshots_never_recopy_and_cross_segment_copy_back verifying disjoint gap insertion and multi-segment / cross-64 KiB writeback merging.

Motivation & Measurements

With VICTORY_GPU_TIMING=1 on RTX 4070 (12 GB, i9-14900KF, Windows 11), frames with repeated overlapping UAV/GPU writes to large buffers (e.g. a 64 MiB buffer) were re-snapshotting 626–918 MB/frame in Shadow::writing.

  • preparing CPU time dropped from ~120–145 ms/frame down to 82.5–84.1 ms/frame (~40 ms/frame saved at 5,185 draws/frame).

Verification

  • cargo test -p victory-gpu --locked -- --test-threads=1 --skip dynamic_slots_are_held_until_released --skip lists_share_a_table_and_copies_keep_their_views: 230 passed; 0 failed; 1 ignored; 2 filtered out
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: 0 warnings
  • cargo run -p xtask --locked -- policy: asset policy: 191 files ok
<!-- greyfinch-sync:Marvisak/VIctory:issue:5514524203 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3318). Originally posted by [danielibabet](https://github.com/danielibabet) on 2026-11-26T17:28:12Z. --- ### Summary - Make `Shadow::writing` insert only uncovered gaps via `partition_point` instead of draining and re-copying already-snapshotted bytes on every overlapping `GpuWrite` (`O(N^2)` -> `O(N)` per frame). - Reuse `pooled(PIECE)` / `give_back` for the title readback scratch buffer in `Shadow::copy_back_or_not` and iterate across adjacent snapshot segments covering `[from, to)` without needing contiguous merged vectors. - Add unit test `overlapping_snapshots_never_recopy_and_cross_segment_copy_back` verifying disjoint gap insertion and multi-segment / cross-64 KiB writeback merging. ### Motivation & Measurements With `VICTORY_GPU_TIMING=1` on RTX 4070 (12 GB, i9-14900KF, Windows 11), frames with repeated overlapping UAV/GPU writes to large buffers (e.g. a 64 MiB buffer) were re-snapshotting 626–918 MB/frame in `Shadow::writing`. - `preparing` CPU time dropped from ~120–145 ms/frame down to **82.5–84.1 ms/frame** (~40 ms/frame saved at 5,185 draws/frame). ### Verification - `cargo test -p victory-gpu --locked -- --test-threads=1 --skip dynamic_slots_are_held_until_released --skip lists_share_a_table_and_copies_keep_their_views`: `230 passed; 0 failed; 1 ignored; 2 filtered out` - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: 0 warnings - `cargo run -p xtask --locked -- policy`: `asset policy: 191 files ok`
AurumCarlos commented 2026-11-26 17:48:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-26T17:48:10Z.

Review: COMMENTED


Same problem, same function as #3211 (yumaste, open, approved), so the two will conflict; worth linking it and agreeing which one goes forward. On this version:

  • The "compared once per submission" rule no longer holds for fragmented pieces. n is now also clipped to seg_end, so a 64 KiB piece that spans two snapshot segments (exactly what gap insertion produces) is compared in parts with n < PIECE, and compared.insert never runs for it. A later overlapping declared range then compares and writes back the same piece again. #3211 keeps the rule by assembling a fragmented piece's old bytes (changed_piece) before comparing.
  • The new test only checks that bytes aren't re-copied and that one copy_back lands; it doesn't cover a second overlapping copy_back on a piece split across segments, which is where this differs from main.
<!-- greyfinch-sync:Marvisak/VIctory:review:6078601473 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3318#pullrequestreview-6078601473). Originally posted by [AurumCarlos](https://github.com/AurumCarlos) on 2026-11-26T17:48:10Z. **Review:** COMMENTED --- Same problem, same function as #3211 (yumaste, open, approved), so the two will conflict; worth linking it and agreeing which one goes forward. On this version: - **The "compared once per submission" rule no longer holds for fragmented pieces.** `n` is now also clipped to `seg_end`, so a 64 KiB piece that spans two snapshot segments (exactly what gap insertion produces) is compared in parts with `n < PIECE`, and `compared.insert` never runs for it. A later overlapping declared range then compares and writes back the same piece again. #3211 keeps the rule by assembling a fragmented piece's old bytes (`changed_piece`) before comparing. - The new test only checks that bytes aren't re-copied and that one `copy_back` lands; it doesn't cover a second overlapping `copy_back` on a piece split across segments, which is where this differs from `main`.
Dukestar69 commented 2026-11-26 17:50:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T17:50:30Z.

Review: APPROVED


Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090):

  • Snapshotting only uncovered gaps in Shadow::writing (instead of draining and re-copying already-snapshotted segments) and iterating across segments in copy_back_or_not cleanly eliminates quadratic snapshot copies while preserving cross-segment copy-back semantics.
  • cargo test -p victory-gpu --lib shadow:: -- --test-threads=1 (RTX 5090): 11 passed, 0 failed (including overlapping_snapshots_never_recopy_and_cross_segment_copy_back).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

<!-- greyfinch-sync:Marvisak/VIctory:review:6078623006 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3318#pullrequestreview-6078623006). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T17:50:30Z. **Review:** APPROVED --- Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090): - Snapshotting only uncovered gaps in `Shadow::writing` (instead of draining and re-copying already-snapshotted segments) and iterating across segments in `copy_back_or_not` cleanly eliminates quadratic snapshot copies while preserving cross-segment copy-back semantics. - `cargo test -p victory-gpu --lib shadow:: -- --test-threads=1` (RTX 5090): 11 passed, 0 failed (including `overlapping_snapshots_never_recopy_and_cross_segment_copy_back`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
oni-swr commented 2026-11-26 17:55:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-26T17:55:55Z.


Independent verification of PR head e6324bc9a0d41ca2975c190f4a2791689116d871 against control main 63bcca43c6a00a758510398fa43096ce7e4f7df0 (merge base is main itself, so it merges cleanly).

Environment: Windows 11 x64, CPU/WARP only (VICTORY_WARP=1, VICTORY_D3D12_WARP=1, Windows SDK 10.0.26100.0 x64 dxil.dll on PATH, -- --test-threads=1). No physical GPU was used and no game files were involved. -j 4, separate target dir.

Diff read in full (crates/victory-gpu/src/shadow.rs only): writing now walks the segment list and snapshots only the uncovered gaps instead of merging and re-copying overlapped segments. copy_back now walks the segments across 64 KiB pieces, skips uncovered holes, and uses a pooled buffer that it gives back. A new test checks the segments stay sorted and disjoint, that total bytes equal the range (no re-copy), and that GPU writes merge without clobbering title writes. I found no bug. The diagnostics counters now record only newly copied bytes, which is a deliberate behaviour change. Note: the new test returns early when no host GPU copy is available, so on a machine without one it passes without exercising anything. It did run here on WARP.

Results (PR head / main control):

  • cargo test -p victory-gpu --lib shadow: 12 passed, 0 failed, 0 ignored / 11 passed, 0 failed, 0 ignored
  • cargo test -p victory-gpu --lib: PR 231 passed, 1 failed, 1 ignored. Control 230 passed, 1 failed, 1 ignored.
    • The one failure is the same on both: frame::tests::waits_end_on_ticks_and_count_frames (frame.rs:231). It fails on unmodified main in this environment, so it is not caused by this PR (it matches the TEST-FRAME-WAITS item in #1633).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean
  • cargo run -p xtask -- policy: asset policy: 191 files ok

AI disclosure: this verification was run by Claude Code (Claude Sonnet 5.5).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7199859565 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3318#issuecomment-7199859565). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-26T17:55:55Z. --- Independent verification of PR head `e6324bc9a0d41ca2975c190f4a2791689116d871` against control `main` `63bcca43c6a00a758510398fa43096ce7e4f7df0` (merge base is main itself, so it merges cleanly). **Environment:** Windows 11 x64, CPU/WARP only (`VICTORY_WARP=1`, `VICTORY_D3D12_WARP=1`, Windows SDK 10.0.26100.0 x64 `dxil.dll` on PATH, `-- --test-threads=1`). No physical GPU was used and no game files were involved. `-j 4`, separate target dir. **Diff read in full** (`crates/victory-gpu/src/shadow.rs` only): `writing` now walks the segment list and snapshots only the uncovered gaps instead of merging and re-copying overlapped segments. `copy_back` now walks the segments across 64 KiB pieces, skips uncovered holes, and uses a pooled buffer that it gives back. A new test checks the segments stay sorted and disjoint, that total bytes equal the range (no re-copy), and that GPU writes merge without clobbering title writes. I found no bug. The diagnostics counters now record only newly copied bytes, which is a deliberate behaviour change. Note: the new test returns early when no host GPU copy is available, so on a machine without one it passes without exercising anything. It did run here on WARP. **Results (PR head / main control):** - `cargo test -p victory-gpu --lib shadow`: 12 passed, 0 failed, 0 ignored / 11 passed, 0 failed, 0 ignored - `cargo test -p victory-gpu --lib`: PR 231 passed, 1 failed, 1 ignored. Control 230 passed, 1 failed, 1 ignored. - The one failure is the same on both: `frame::tests::waits_end_on_ticks_and_count_frames` (frame.rs:231). It fails on unmodified main in this environment, so it is not caused by this PR (it matches the `TEST-FRAME-WAITS` item in #1633). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean - `cargo run -p xtask -- policy`: `asset policy: 191 files ok` AI disclosure: this verification was run by Claude Code (Claude Sonnet 5.5).
danielibabet commented 2026-11-26 18:19:19 +00:00 (Migrated from github.com)

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


Good catch @oni-swr! Updated \Shadow::copy_back_or_not\ so \compared.insert\ runs once per whole 64 KiB piece before walking the segments inside that piece, and extended \overlapping_snapshots_never_recopy_and_cross_segment_copy_back\ to verify a second overlapping \copy_back\ skips a split 64 KiB piece. Re-ran \ ictory-gpu\ tests (230 passed, 0 failed; 2 skipped), strict clippy, and \xtask policy\ (191 files ok).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7200321201 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3318#issuecomment-7200321201). Originally posted by [danielibabet](https://github.com/danielibabet) on 2026-11-26T18:19:19Z. --- Good catch @oni-swr! Updated \Shadow::copy_back_or_not\ so \compared.insert\ runs once per whole 64 KiB piece before walking the segments inside that piece, and extended \overlapping_snapshots_never_recopy_and_cross_segment_copy_back\ to verify a second overlapping \copy_back\ skips a split 64 KiB piece. Re-ran \ ictory-gpu\ tests (230 passed, 0 failed; 2 skipped), strict clippy, and \xtask policy\ (191 files ok).
borosf commented 2026-11-26 19:03:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by borosf on 2026-11-26T19:03:35Z.


Independent local verification on Windows 11 x64, Intel Arc B580 12 GB (12 cores), at this PR's head cb7347b:

cargo test -p victory-gpu --lib --locked --no-fail-fast -- --test-threads=1
cargo clippy -p victory-gpu --all-targets --locked -- -D warnings
cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings

Result: victory-gpu lib: 2 known baseline failures only; the new shadow::tests::overlapping_snapshots_never_recopy_and_cross_segment_copy_back test passed.

Both clippy runs exit 0 (-D warnings, both feature sets). The B580 baseline failures pristine main also hits are: victory-gpu descriptor::host::tests::dynamic_slots_are_held_until_released + memory::tests::lists_share_a_table_and_copies_keep_their_views (#1744/#1829); victory-shader emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros + emit::valu_tests::fspx_programs_run_with_the_hardwares_dot_products. No PR regressions.

Synthetic tests only; no game data used. Full log kept locally. — borosf

<!-- greyfinch-sync:Marvisak/VIctory:comment:7201151042 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3318#issuecomment-7201151042). Originally posted by [borosf](https://github.com/borosf) on 2026-11-26T19:03:35Z. --- Independent local verification on Windows 11 x64, Intel Arc B580 12 GB (12 cores), at this PR's head `cb7347b`: ``` cargo test -p victory-gpu --lib --locked --no-fail-fast -- --test-threads=1 cargo clippy -p victory-gpu --all-targets --locked -- -D warnings cargo clippy -p victory-gpu --all-targets --all-features --locked -- -D warnings ``` Result: victory-gpu lib: 2 known baseline failures only; the new shadow::tests::overlapping_snapshots_never_recopy_and_cross_segment_copy_back test passed. Both clippy runs exit 0 (-D warnings, both feature sets). The B580 baseline failures pristine main also hits are: victory-gpu descriptor::host::tests::dynamic_slots_are_held_until_released + memory::tests::lists_share_a_table_and_copies_keep_their_views (#1744/#1829); victory-shader emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros + emit::valu_tests::fspx_programs_run_with_the_hardwares_dot_products. No PR regressions. Synthetic tests only; no game data used. Full log kept locally. — borosf
danielibabet commented 2026-11-26 19:06:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by danielibabet on 2026-11-26T19:06:10Z.


Thanks @borosf for verifying on Intel Arc B580!

<!-- greyfinch-sync:Marvisak/VIctory:comment:7201197247 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3318#issuecomment-7201197247). Originally posted by [danielibabet](https://github.com/danielibabet) on 2026-11-26T19:06:10Z. --- Thanks @borosf for verifying on Intel Arc B580!
Desamodeth commented 2026-11-26 21:00:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Desamodeth on 2026-11-26T21:00:14Z.

Review: COMMENTED


On cb7347b8 the number of gaps matters even when the total snapshot is small. For 2,048 adjacent 32-byte writes in one shadow, the last keep_writes alone took 1.09–1.18 ms. It returned 2,048 buffers. Each give_back sorts and scans the growing pool under its mutex, and the next submission scans the pool again on checkout. With #3211 the same final call returned one capture buffer.

CPU time for capture plus retirement, with the same pool kept warm across submissions:

Adjacent writes to one shadow main 63bcca43 This PR #3211 ebd01cdd
2,048 × 32 B (64 KiB) 0.64–0.66 ms 2.05–2.16 ms 0.101–0.102 ms
128 × 32 KiB (4 MiB) 8.46–8.55 ms 0.085–0.087 ms 0.075–0.077 ms

To reproduce, seed each pool with two touched buffers the size of the full span. For each submission call writing(base + i * width, width, false) in ascending order, then keep_writes for each range in the same order. Time the final return separately. Replacing keep_writes with copy_back on unchanged bytes preserved the small-range result (2.72–2.77 ms here, 1.35–1.40 ms on main, 0.77–0.86 ms on #3211). One 64 KiB declaration took about 1 µs across all three on the keep path.

I’d favor #3211’s single capture buffer if these changes are consolidated. Both PRs deliver the large-range gain. The single buffer also avoids a separate pool operation per small gap.

Windows x64, Ryzen 7 9800X3D, rustc 1.98.1 -C opt-level=3. Isolated production snapshot, pending-set and pool code, one thread, synthetic host bytes. Medians of 11 interleaved warmed samples in each of two opposite-order runs. Checked captured contents, coverage and empty pending/snapshot state after retirement. The in-game distribution of range sizes and the effect on FPS were not measured. AI-assisted with Codex.

<!-- greyfinch-sync:Marvisak/VIctory:review:6080427225 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3318#pullrequestreview-6080427225). Originally posted by [Desamodeth](https://github.com/Desamodeth) on 2026-11-26T21:00:14Z. **Review:** COMMENTED --- On `cb7347b8` the number of gaps matters even when the total snapshot is small. For 2,048 adjacent 32-byte writes in one shadow, the **last** `keep_writes` alone took 1.09–1.18 ms. It returned 2,048 buffers. Each `give_back` sorts and scans the growing pool under its mutex, and the next submission scans the pool again on checkout. With #3211 the same final call returned one capture buffer. CPU time for capture **plus** retirement, with the same pool kept warm across submissions: | Adjacent writes to one shadow | main `63bcca43` | This PR | #3211 `ebd01cdd` | | --- | ---: | ---: | ---: | | 2,048 × 32 B (64 KiB) | 0.64–0.66 ms | 2.05–2.16 ms | 0.101–0.102 ms | | 128 × 32 KiB (4 MiB) | 8.46–8.55 ms | 0.085–0.087 ms | 0.075–0.077 ms | To reproduce, seed each pool with two touched buffers the size of the full span. For each submission call `writing(base + i * width, width, false)` in ascending order, then `keep_writes` for each range in the same order. Time the final return separately. Replacing `keep_writes` with `copy_back` on unchanged bytes preserved the small-range result (2.72–2.77 ms here, 1.35–1.40 ms on main, 0.77–0.86 ms on #3211). One 64 KiB declaration took about 1 µs across all three on the keep path. I’d favor #3211’s single capture buffer if these changes are consolidated. Both PRs deliver the large-range gain. The single buffer also avoids a separate pool operation per small gap. Windows x64, Ryzen 7 9800X3D, rustc 1.98.1 `-C opt-level=3`. Isolated production snapshot, pending-set and pool code, one thread, synthetic host bytes. Medians of 11 interleaved warmed samples in each of two opposite-order runs. Checked captured contents, coverage and empty pending/snapshot state after retirement. The in-game distribution of range sizes and the effect on FPS were not measured. AI-assisted with Codex.
danielibabet commented 2026-11-26 21:19:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by danielibabet on 2026-11-26T21:19:16Z.


Thanks for the detailed head-to-head benchmark against #3211 @Desamodeth! That 2,048 × 32 B gap case makes the trade-off very clear: keeping one capture buffer like #3211 avoids the per-gap pool \give_back\ / checkout overhead on fragmented small writes while matching the large-range speedup (~0.08 ms vs ~8.5 ms). If maintainers prefer consolidating onto #3211's single-buffer approach, that makes complete sense.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7203611442 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3318#issuecomment-7203611442). Originally posted by [danielibabet](https://github.com/danielibabet) on 2026-11-26T21:19:16Z. --- Thanks for the detailed head-to-head benchmark against #3211 @Desamodeth! That 2,048 × 32 B gap case makes the trade-off very clear: keeping one capture buffer like #3211 avoids the per-gap pool \give_back\ / checkout overhead on fragmented small writes while matching the large-range speedup (~0.08 ms vs ~8.5 ms). If maintainers prefer consolidating onto #3211's single-buffer approach, that makes complete sense.
fuzeofficlal commented 2026-11-26 22:00:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T22:00:33Z.

Review: CHANGES_REQUESTED


Good fix in cb7347b8 for keeping compared.insert at the 64 KiB piece level, and the existing 10 shadow::tests pass on RTX 5090 and WARP. However, fragmenting a 64 KiB page across multiple snapshot segments introduces a silent data-loss bug in Shadow::copy_back_or_not's synced / t.taken(&whole) tracking:

Blocking: partial-page merge marks the whole 64 KiB page clean, permanently dropping concurrent title writes in untouched sibling segments

  • crates/victory-gpu/src/shadow.rs:437-479

On main, copy_back_or_not compared a whole 64 KiB piece at once: if any byte differed (host != old), merge_writeback copied all concurrent title writes across the entire 64 KiB page into self.data, making it safe to call t.taken(&whole) afterward.

In cb7347b8, overlapping Shadow::writing calls split a single 64 KiB page into disjoint segments (e.g., [0, 32 KiB) and [32 KiB, 64 KiB)). If the GPU writes to segment 0 (+0x10 = 0xaa) while a title thread concurrently writes to segment 1 (+0x9000 = 0xbbbbbbbb):

  1. Segment 0 sees host != old, merges [0, 32 KiB), and pushes (start + at) / PAGE into synced (shadow.rs:466).
  2. Segment 1 sees host == old and skips read_into / merge_writeback, leaving self.data[0x9000] at its stale pre-submission value 0x00000000.
  3. t.taken(&whole) (shadow.rs:479) still marks the entire 64 KiB page clean (copy.clean() == true), so subsequent Shadow::update calls return 0 immediately and the title's write at +0x9000 never reaches the host copy.

Live reproduction (copy.update(p, PAGE); copy.writing(p, PAGE / 2, false); copy.writing(p, PAGE, false); copy.write_host(p + 0x10, &[0xaa, 0, 0, 0]); *(p + 0x9000) = 0xbbbbbbbb; copy.copy_back(p, PAGE); copy.update(p, PAGE);):

  • origin/main (63bcca43): written=65536 clean_after=true next_update_bytes=0 title=[0x000000aa,0xbbbbbbbb] host=[0x000000aa,0xbbbbbbbb]
  • PR #3318 (cb7347b8, RTX 5090 & WARP): written=32768 clean_after=true next_update_bytes=0 title=[0x000000aa,0xbbbbbbbb] host=[0x000000aa,0x00000000]

Fix: Either track piece_synced += n across the inner segment loop and only push (start + piece_start) / PAGE into synced when piece_len == PIECE && piece_synced == PIECE (verified on RTX 5090: clean_after=false, next_update_bytes=65536, host=[0x000000aa,0xbbbbbbbb], 11/11 shadow::tests pass), or — if you want to avoid re-refreshing the page on the next submission when the title did not write — scan the piece's segments first and merge every segment in the piece whenever any segment in that piece has host != old.

Minor note (non-blocking)

  • crates/victory-gpu/src/shadow.rs:418-426: gating if back && from < to && !segments.is_empty() avoids checking out and zeroing a 64 KiB pooled(PIECE) scratch buffer on marked-write ranges that have no snapshot segments.
<!-- greyfinch-sync:Marvisak/VIctory:review:6080937966 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3318#pullrequestreview-6080937966). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T22:00:33Z. **Review:** CHANGES_REQUESTED --- Good fix in `cb7347b8` for keeping `compared.insert` at the 64 KiB piece level, and the existing 10 `shadow::tests` pass on RTX 5090 and WARP. However, fragmenting a 64 KiB page across multiple snapshot segments introduces a silent data-loss bug in `Shadow::copy_back_or_not`'s `synced` / `t.taken(&whole)` tracking: ### Blocking: partial-page merge marks the whole 64 KiB page clean, permanently dropping concurrent title writes in untouched sibling segments - `crates/victory-gpu/src/shadow.rs:437-479` On `main`, `copy_back_or_not` compared a whole 64 KiB piece at once: if any byte differed (`host != old`), `merge_writeback` copied all concurrent title writes across the entire 64 KiB page into `self.data`, making it safe to call `t.taken(&whole)` afterward. In `cb7347b8`, overlapping `Shadow::writing` calls split a single 64 KiB page into disjoint segments (e.g., `[0, 32 KiB)` and `[32 KiB, 64 KiB)`). If the GPU writes to segment 0 (`+0x10 = 0xaa`) while a title thread concurrently writes to segment 1 (`+0x9000 = 0xbbbbbbbb`): 1. Segment 0 sees `host != old`, merges `[0, 32 KiB)`, and pushes `(start + at) / PAGE` into `synced` (`shadow.rs:466`). 2. Segment 1 sees `host == old` and skips `read_into` / `merge_writeback`, leaving `self.data[0x9000]` at its stale pre-submission value `0x00000000`. 3. `t.taken(&whole)` (`shadow.rs:479`) still marks the entire 64 KiB page clean (`copy.clean() == true`), so subsequent `Shadow::update` calls return `0` immediately and the title's write at `+0x9000` never reaches the host copy. Live reproduction (`copy.update(p, PAGE); copy.writing(p, PAGE / 2, false); copy.writing(p, PAGE, false); copy.write_host(p + 0x10, &[0xaa, 0, 0, 0]); *(p + 0x9000) = 0xbbbbbbbb; copy.copy_back(p, PAGE); copy.update(p, PAGE);`): - `origin/main` (`63bcca43`): `written=65536 clean_after=true next_update_bytes=0 title=[0x000000aa,0xbbbbbbbb] host=[0x000000aa,0xbbbbbbbb]` - PR `#3318` (`cb7347b8`, RTX 5090 & WARP): `written=32768 clean_after=true next_update_bytes=0 title=[0x000000aa,0xbbbbbbbb] host=[0x000000aa,0x00000000]` **Fix**: Either track `piece_synced += n` across the inner segment loop and only push `(start + piece_start) / PAGE` into `synced` when `piece_len == PIECE && piece_synced == PIECE` (verified on RTX 5090: `clean_after=false`, `next_update_bytes=65536`, `host=[0x000000aa,0xbbbbbbbb]`, 11/11 `shadow::tests` pass), or — if you want to avoid re-refreshing the page on the next submission when the title did not write — scan the piece's segments first and merge every segment in the piece whenever any segment in that piece has `host != old`. ### Minor note (non-blocking) - `crates/victory-gpu/src/shadow.rs:418-426`: gating `if back && from < to && !segments.is_empty()` avoids checking out and zeroing a 64 KiB `pooled(PIECE)` scratch buffer on marked-write ranges that have no snapshot segments.
danielibabet commented 2026-11-26 22:03:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by danielibabet on 2026-11-26T22:03:01Z.


Good catch @fuzeofficlal! Fixed both in 8b284dd:

  1. Full-piece merge on dirty 64 KiB piece (Shadow::copy_back_or_not): when any snapshot segment inside a piece_start..piece_end differs from its snapshot (host != old), copy_back_or_not now runs read_into + merge_writeback across every segment in that piece (so concurrent title writes in untouched sibling segments are pulled into self.data) and only adds (start + piece_start) / PAGE to synced when piece_len == PIECE && piece_synced == PIECE.
  2. Non-blocking note: gated the pooled(PIECE) checkout on !segments.is_empty().
  3. Added regression test partial_page_merge_preserves_concurrent_title_writes_across_sibling_segments matching your exact repro.

Verified on Windows 11 x64 (RTX 4070) with VICTORY_D3D12_DEBUG=1: shadow:: 12/12 passed, cargo clippy -p victory-gpu --all-targets -- -D warnings clean, cargo xtask policy ok (191 files).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7204307740 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3318#issuecomment-7204307740). Originally posted by [danielibabet](https://github.com/danielibabet) on 2026-11-26T22:03:01Z. --- Good catch @fuzeofficlal! Fixed both in `8b284dd`: 1. **Full-piece merge on dirty 64 KiB piece (`Shadow::copy_back_or_not`)**: when any snapshot segment inside a `piece_start..piece_end` differs from its snapshot (`host != old`), `copy_back_or_not` now runs `read_into` + `merge_writeback` across **every** segment in that piece (so concurrent title writes in untouched sibling segments are pulled into `self.data`) and only adds `(start + piece_start) / PAGE` to `synced` when `piece_len == PIECE && piece_synced == PIECE`. 2. **Non-blocking note**: gated the `pooled(PIECE)` checkout on `!segments.is_empty()`. 3. Added regression test `partial_page_merge_preserves_concurrent_title_writes_across_sibling_segments` matching your exact repro. Verified on Windows 11 x64 (RTX 4070) with `VICTORY_D3D12_DEBUG=1`: `shadow::` 12/12 passed, `cargo clippy -p victory-gpu --all-targets -- -D warnings` clean, `cargo xtask policy` ok (191 files).
fuzeofficlal commented 2026-11-26 22:44:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T22:44:16Z.

Review: APPROVED


Verified the updated head (8b284dd2) merged cleanly onto main (63bcca43) — the partial-page t.taken(&whole) bug from the previous review is cleanly fixed.

Verification

  • cargo xtask policy: 191 files ok; cargo clippy --locked -p victory-gpu --all-targets -- -D warnings (both default and --features diagnostics): 0 warnings.
  • shadow::tests:: (including the new partial_page_merge_preserves_concurrent_title_writes_across_sibling_segments regression test at crates/victory-gpu/src/shadow.rs:1780) passed on both a discrete NVIDIA RTX 5090 and WARP with --features diagnostics and VICTORY_D3D12_DEBUG=1.
  • Confirmed shadow.rs:477-521 now tracks piece_synced per 64 KiB page and only includes a page in t.taken(&whole) (shadow.rs:521) when piece_len == PIECE && piece_synced == PIECE (shadow.rs:509).
  • Ran 4 additional adversarial readback/merge probes on both RTX 5090 and WARP (VICTORY_D3D12_DEBUG=1):
    1. 3-segment split 64 KiB page ([0, 16K), [16K, 48K), [48K, 64K)) with GPU writes in the middle segment and concurrent title writes in both outer segments — all 3 segments merged cleanly without losing title writes.
    2. Uncovered [16K, 48K) gap (piece_synced == 32K != 64K) — page remains dirty (!copy.clean()) and subsequent update preserves concurrent title writes inside the gap alongside GPU writes.
    3. 3-page (192 KiB) cross-boundary multi-segment span with concurrent title writes across all 3 pages and GPU diffs on pages 0 and 2 — all writes preserved.
    4. Zero-GPU-diff split page with concurrent title write — remains dirty and preserves the title write.

LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6081219534 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3318#pullrequestreview-6081219534). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T22:44:16Z. **Review:** APPROVED --- Verified the updated head (`8b284dd2`) merged cleanly onto `main` (`63bcca43`) — the partial-page `t.taken(&whole)` bug from the previous review is cleanly fixed. ### Verification - `cargo xtask policy`: 191 files ok; `cargo clippy --locked -p victory-gpu --all-targets -- -D warnings` (both default and `--features diagnostics`): 0 warnings. - `shadow::tests::` (including the new `partial_page_merge_preserves_concurrent_title_writes_across_sibling_segments` regression test at `crates/victory-gpu/src/shadow.rs:1780`) passed on both a discrete NVIDIA RTX 5090 and WARP with `--features diagnostics` and `VICTORY_D3D12_DEBUG=1`. - Confirmed `shadow.rs:477-521` now tracks `piece_synced` per 64 KiB page and only includes a page in `t.taken(&whole)` (`shadow.rs:521`) when `piece_len == PIECE && piece_synced == PIECE` (`shadow.rs:509`). - Ran 4 additional adversarial readback/merge probes on both RTX 5090 and WARP (`VICTORY_D3D12_DEBUG=1`): 1. 3-segment split 64 KiB page (`[0, 16K)`, `[16K, 48K)`, `[48K, 64K)`) with GPU writes in the middle segment and concurrent title writes in both outer segments — all 3 segments merged cleanly without losing title writes. 2. Uncovered `[16K, 48K)` gap (`piece_synced == 32K != 64K`) — page remains dirty (`!copy.clean()`) and subsequent `update` preserves concurrent title writes inside the gap alongside GPU writes. 3. 3-page (192 KiB) cross-boundary multi-segment span with concurrent title writes across all 3 pages and GPU diffs on pages 0 and 2 — all writes preserved. 4. Zero-GPU-diff split page with concurrent title write — remains dirty and preserves the title write. LGTM.
neonbyte1 commented 2026-11-26 23:00:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by neonbyte1 on 2026-11-26T23:00:35Z.


In-game on an RTX 5090, Linux, GE-Proton11-14 Wine with vkd3d-proton, GTA VI's opening yard. Untimed 10-minute runs, medians of the last 30 frame-rate reports, VICTORY_GPU_OVERLAP=1.

ms/frame (min) preparing copying back
my base (main + #2958, #3120, #2309, #2461, …) 553 / 563 (527 / 534) 63 48 / 49
base + this PR 575 (542) 64 50
base + #3312 386 (369) 85 52
base + #3312 + this PR 372 (359) 81 49

On my base, preparing doesn't change, so there's nothing for it to remove at this draw count. On top of #3312, whose VRAM copies make preparing heavier, it takes about 14 ms off the frame. These are single runs; the median and minimum agree, but the gain is within this scene's run-to-run variation, so treat it as small but positive.

AI-assisted (Claude); measured on my machine, no game data shared.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7205174794 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3318#issuecomment-7205174794). Originally posted by [neonbyte1](https://github.com/neonbyte1) on 2026-11-26T23:00:35Z. --- In-game on an RTX 5090, Linux, GE-Proton11-14 Wine with vkd3d-proton, GTA VI's opening yard. Untimed 10-minute runs, medians of the last 30 frame-rate reports, `VICTORY_GPU_OVERLAP=1`. | | ms/frame (min) | preparing | copying back | |---|---|---|---| | my base (`main` + #2958, #3120, #2309, #2461, …) | 553 / 563 (527 / 534) | 63 | 48 / 49 | | base + this PR | 575 (542) | 64 | 50 | | base + #3312 | 386 (369) | 85 | 52 | | base + #3312 + this PR | **372** (359) | **81** | 49 | On my base, preparing doesn't change, so there's nothing for it to remove at this draw count. On top of #3312, whose VRAM copies make preparing heavier, it takes about 14 ms off the frame. These are single runs; the median and minimum agree, but the gain is within this scene's run-to-run variation, so treat it as small but positive. _AI-assisted (Claude); measured on my machine, no game data shared._
upipibi commented 2026-11-27 22:10:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-27T22:10:34Z.


Run at head 8b284dd2 (base main 63bcca4), Windows 11 with an RTX 3070 (driver 616.56). cargo test -p victory-gpu --lib shadow::tests::, 3 runs: all of the existing shadow tests and this PR's overlapping_snapshots_never_recopy_and_cross_segment_copy_back pass.

Two things for the merge order with the open copy-back fixes:

  • This PR does not fix the stale-write case those PRs fix. I added two regression tests to it locally: #3031's overlapping_copy_backs_do_not_clobber_newer_cpu_writes and #3412's partial_snapshot_after_a_full_page_preserves_a_new_cpu_store. Both fail here 3 of 3, as they do on main: two declared ranges of one submission overlap, and the second copy-back writes the stale snapshot byte over a newer CPU store. That is expected, since this PR doesn't aim at it. It is also not made worse.
  • It conflicts with #3031 (triage "merge-candidate") in Shadow::copy_back_or_not: three hunks plus the tests. #3031's fix is one line, old.copy_from_slice(host) after the merge. In this PR's per-segment loop old is an immutable slice of before (let old = &before[probe_at - taken..probe_at - taken + n]), so whichever of the two goes in second has to re-place that step inside the new loop, not just rebase it.

AI-assisted (Claude Code, Claude Opus 5.5 ran the tests and the merge).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7229094203 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3318#issuecomment-7229094203). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-27T22:10:34Z. --- Run at head `8b284dd2` (base `main` `63bcca4`), Windows 11 with an RTX 3070 (driver 616.56). `cargo test -p victory-gpu --lib shadow::tests::`, 3 runs: all of the existing `shadow` tests and this PR's `overlapping_snapshots_never_recopy_and_cross_segment_copy_back` pass. Two things for the merge order with the open copy-back fixes: - This PR does not fix the stale-write case those PRs fix. I added two regression tests to it locally: #3031's `overlapping_copy_backs_do_not_clobber_newer_cpu_writes` and #3412's `partial_snapshot_after_a_full_page_preserves_a_new_cpu_store`. Both fail here 3 of 3, as they do on `main`: two declared ranges of one submission overlap, and the second copy-back writes the stale snapshot byte over a newer CPU store. That is expected, since this PR doesn't aim at it. It is also not made worse. - It conflicts with #3031 (triage "merge-candidate") in `Shadow::copy_back_or_not`: three hunks plus the tests. #3031's fix is one line, `old.copy_from_slice(host)` after the merge. In this PR's per-segment loop `old` is an immutable slice of `before` (`let old = &before[probe_at - taken..probe_at - taken + n]`), so whichever of the two goes in second has to re-place that step inside the new loop, not just rebase it. AI-assisted (Claude Code, Claude Opus 5.5 ran the tests and the merge).
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/3318:upstream-pr/3318'
git switch 'upstream-pr/3318'

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