Skip to content

GPU: a copy-back keeps the snapshot in step, so a later range takes no older bytes - #198

Closed
pablogonz12 wants to merge 3 commits into
mainfrom
int/vic-snapshot
Closed

pablogonz12 wants to merge 3 commits into
mainfrom
int/vic-snapshot

Conversation

@pablogonz12

@pablogonz12 pablogonz12 commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

What

A copy-back puts what the shaders wrote into title memory and lets the copy take the title's own bytes
(Shadow::copy_back_with). The snapshot it compares against — the copy's bytes before the submission — kept
only the GPU's bytes, so the bytes the copy had just taken from title memory looked like the GPU's to a later
copy-back of a range that overlaps the first one (the world declares overlapping views of its big heap
dozens of times per frame): it wrote the copy's older bytes back over a title write made after the first
copy-back. Same family as the stale-bytes-over-live-data failures we chase in streaming and geometry.

Now the bytes a copy-back merges into the copy go into the snapshot too: merge_back, one pass over the
differing span (no second copy of it — this runs over the differing bytes of every declared range). A later
range finds them unchanged, takes the title's current bytes and writes nothing back. merge_back is
merge_piece's rule with the copy stored into as well; merge_piece keeps not storing into its source (the
card may be writing it for a later flight).

The same shape is already right on the card-merge path (Shadow::card_merge) and in relay_merge, which
update the snapshot they hold — this brings the plain CPU path in line.

Not changed: relay_merge still lets its snapshot follow only the GPU's bytes (relay is off by default and its
merge reads the copy without writing it).

Credit: Marvisak/VIctory #2513 (Calvix2k, GPL-3.0).
Reimplemented — theirs changes copy_back_marked, which this tree has no equivalent of (one copy_back
merges into the copy and keeps one snapshot per submission) — with a Co-authored-by: trailer and one
CREDITS.md line.

Tests

shadow::merge_tests::a_copy_back_keeps_its_snapshot_in_step: upstream #2436's shape in one piece — a shader
byte, a title write beside it, the copy-back, then the title writing both again: both stay. Plus the same
piece with the snapshot left behind, which puts the older byte back.

Gates

  • cargo xwin clippy --target x86_64-pc-windows-msvc --workspace --all-targets -- -D warnings: clean.
  • victory-gpu on the RADV card (VICTORY_TEST_ADAPTER=hw, native dxgi): 207 passed, 2 ignored (the existing
    benchmark and watch benchmarks), 0 skipped: no device.
  • cargo test -p victory-shader (Linux): 187 passed, 2 ignored.
  • cargo xtask policy: 244 files ok; the game-name grep is empty.

Off switch

None: this only stops a wrong write on the CPU copy-back path (no new work per frame beyond the one extra
store per merged byte).

Not tested in a game run.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed copy-back behavior so GPU-written and title-memory data stay consistent across repeated updates. Later copy-backs now account for the latest title-memory changes.
  • Behavior Changes
    • Staging buffer requests now accept only supported capacities by default; unsupported sizes are rejected without changing the existing capacity. Set VICTORY_DSTORAGE_STAGING_CHECK=0 to accept other sizes.

…o older bytes

A copy-back puts what the shaders wrote into title memory and lets the copy
take the title's own bytes. The snapshot it compares against (the copy's
bytes before the submission) kept only the GPU's bytes, so the bytes the copy
had just taken from title memory looked like the GPU's to a later copy-back
of a range that overlaps: it put the copy's older bytes back over a title
write made after the first copy-back.

Now the bytes a copy-back merges into the copy go into the snapshot too
(`merge_back`, one pass over the differing span, no second copy of it): a
later range finds them unchanged, takes the title's current bytes and writes
nothing back. `merge_back` is `merge_piece`'s rule with the copy stored into
as well; `merge_piece` keeps not storing into its source (the card may be
writing it for a later flight). The same shape is already right on the
card-merge path and in `relay_merge`, which update the snapshot they hold.

What is not changed: `relay_merge`'s own snapshot still follows only the
GPU's bytes (relay is off by default and its merge reads the copy without
writing it).

Tests: shadow::merge_tests::a_copy_back_keeps_its_snapshot_in_step — the
upstream #2436 shape in one piece (a shader byte, a title write beside it,
the copy-back, then the title writing both again: both stay), and the same
piece with the snapshot left behind, which puts the older byte back. Gates:
clippy clean (workspace, all targets); victory-gpu 207 passed, 2 ignored (the
existing benchmark and watch benchmarks), 0 "no device" skips on the RADV
card; victory-shader 187 passed, 2 ignored. Behaviour only on the CPU
copy-back path, no switch. Not tested in a game run.

Co-authored-by: Calvix2k <calvinhebben1@gmail.com>
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

No actionable comments were generated in the recent review.

Walkthrough

Changes: Copy-back snapshot synchronization

Layer / File(s) Summary
Merge bytes into title memory, copy, and snapshot
crates/victory-gpu/src/shadow.rs
merge_back writes merged bytes to title memory, the copy, and the snapshot.
Integrate snapshot updates into copy-back
crates/victory-gpu/src/shadow.rs, CREDITS.md
copy_back_with narrows each differing piece and calls merge_back. A regression test checks that later title-memory writes are preserved across copy-backs. The design note credits the related reference idea and distinguishes this implementation from copy_back_marked.

DirectStorage staging capacity validation

Layer / File(s) Summary
Validate and apply staging capacities
crates/victory-hle/src/modules/dstorage.rs, CREDITS.md
Staging requests accept supported capacities, or any capacity when validation is disabled. Unsupported requests return E_INVALIDARG without changing the scheduler capacity. Tests cover accepted and rejected sizes.

Suggested reviewers: fxceless, fever308

Change: Bug fix

Security architecture risk: Moderate · up to 69ca2

The change prevents a later copy-back from overwriting newer application data with stale bytes. However, it can also treat a failed write as complete, and it introduces a memory-safety issue when copying back multiple pieces of a range. The affected path is in-process; the change does not add a new entry point or expand what memory it can access.

Retained concerns

  • Medium · security · observed: Copy-back creates a shared slice over the full snapshot range, mutates part of that range through a mutable slice, then reuses the full shared slice to index later pieces. Even when the pieces are disjoint, the shared slice covers the mutated bytes. The mutex does not satisfy Rust's reference requirements, so this introduces undefined behavior on the multi-piece path.
  • Medium · reliability · observed: The merge advances the snapshot before the title write, and the destination writer discards final page-level failures. If a write fails or only partly completes, a later copy-back using the same snapshot can see the affected bytes as already synchronized and skip them. The base retained that difference, although it did not guarantee a later retry.
Security review details

Security Blast Radius

  • observed — The change operates on declared ranges of an existing GPU shadow. The same host callers reach the same in-process title-memory writer; the added write targets the shadow's own snapshot, not a new external destination.

Resilience and Maintainability Implications

  • observed — A failed title read skips the merge without changing the copy or snapshot. Write failure is different: the host receives no success status and proceeds to normal completion, which can clear pending ranges and release the snapshot.
- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e7a7a64e-748d-4ff6-a879-5ac0e70e0950
Commits

Reviewing files that changed from the base of the PR and between 0e31559 and 69ca2a8.

Files selected for processing: 2

  • CREDITS.md
  • crates/victory-gpu/src/shadow.rs

Limit details: You’ve used all 10 included reviews currently available.

…and no others

The factory's SetStagingBufferSize copied whatever size the title asked for
into the scheduler. The console has the staging capacities the current
release documents - zero, for a title whose queues all come from memory, and
16, 20, 24, 28 and 32 MiB - and nothing between or past them.

Now a size outside that list is refused with E_INVALIDARG and the capacity
the scheduler has is kept, logged once. What the console answers is not
documented in the interface (`E_INVALIDARG` is what this module answers for
out-of-range values elsewhere, not a verified code). `VICTORY_DSTORAGE_STAGING_
CHECK=0` takes any size and copies it, as before.

Our title asks for 32 MiB, which is in the list.

Tests: dstorage::tests::staging_buffer_sizes_the_console_has_none_of_are_
refused (each documented size, sizes inside, between and past them, u32::MAX;
then through the factory method, refused with the scheduler's capacity
unchanged). Gates: clippy clean; victory-hle 98 passed, 3 failed on the RADV
card, the same three failures as on hub/main in the same Wine prefix
(lifecycle keyboard/window and the loopback net check need a display and
network that the test prefix has not). Not tested in a game run.

Co-authored-by: TheGrayOrange <357638192+TheGrayOrange@users.noreply.github.com>
…itten

The merge wrote the snapshot through a slice while a shared slice of the
same bytes (the whole declared range) was still live, which Rust's aliasing
rules forbid even though the mutex keeps other threads out.

Now the range-wide shared slice lives only for the comparison that finds the
differing pieces, and each piece's bytes are read in a scope of their own
that ends before `merge_back` writes them: no borrow of the snapshot is kept
across the merge.

Found by CodeRabbit on #198. Gates: clippy clean; victory-gpu 207 passed, 2
ignored, 0 "no device" skips on the RADV card; cargo xtask policy 244 files
ok.

Co-authored-by: Calvix2k <calvinhebben1@gmail.com>
@pablogonz12

Copy link
Copy Markdown
Owner Author

Closed as superseded: this branch was built in a worktree that already held the DirectStorage commit, so it carried an unrelated change. The copy-back change alone is #214 (int/vic-copyback), including the fix for the aliasing finding (snapshot bytes read per piece, in their own scope).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant