Skip to content

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

Merged
pablogonz12 merged 2 commits into
mainfrom
int/vic-copyback
Nov 25, 2026
Merged

pablogonz12 merged 2 commits into
mainfrom
int/vic-copyback

Conversation

@pablogonz12

@pablogonz12 pablogonz12 commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Supersedes #198 (that branch had a same-worktree mistake in it: it carried the DirectStorage commit too; this one is
the copy-back change alone, on today's main).

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 a
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 — with Co-authored-by: trailers 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.
  • Second commit (CodeRabbit on GPU: a copy-back keeps the snapshot in step, so a later range takes no older bytes #198): the snapshot's bytes are read in a scope of their own per piece and no shared
    slice stays live across merge_back's write.

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): 211 passed, 2 ignored (the existing
    benchmarks), 0 skipped: no device.
  • cargo test -p victory-shader (Linux): 194 passed, 2 ignored.
  • cargo xtask policy: 255 files ok; the game-name grep is empty.

Off switch

None: this only stops a wrong write on the CPU copy-back path.

Not tested in a game run.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where GPU copy-back operations could overwrite newer title data with stale values. Subsequent updates now preserve those later changes.

pablogonz12 and others added 2 commits November 25, 2026 23:30
…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>
(cherry picked from commit 69ca2a8)
…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>
(cherry picked from commit 98d563d)
@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: GPU copy-back snapshot tracking

Layer / File(s) Summary
Merged-byte updates
crates/victory-gpu/src/shadow.rs
merge_back combines GPU-written bytes with unchanged title bytes, then writes the merged result to title memory, the copy, and the snapshot. A regression test checks snapshot tracking across repeated copy-backs.
Snapshot-based copy-back
crates/victory-gpu/src/shadow.rs, CREDITS.md
copy_back_with compares requested ranges with the snapshot, narrows each differing piece, and calls merge_back. CREDITS.md describes the copy-back implementation and credits the related design concept.

Possibly related PRs

Suggested reviewers: fxceless, fever308

Change: Bug fix

Merge Risk: Minimal · up to 54035

This change keeps the copy-back snapshot in step with merged bytes, so a later copy-back no longer overwrites newer title writes with older data. The supplied evidence shows no remaining merge-blocking risk. The change was not tested in a game run.

Security architecture risk: Minimal · up to 54035

The change prevents older data from overwriting newer writes without expanding who can initiate a write or where it can go. The reviewed paths did not show a new loss of recovery behavior.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • observed — The changed merge operates on a shadow's declared GPU-write spans. The added write is to the shadow's internal snapshot; the title-memory write still uses the same destination and first-to-last differing span as the base.

Trust Boundaries and Controls

  • observed — The entrypoint still excludes placed shadows, clips the requested range to the shadow, and uses the existing mapped-memory writer for title memory. The writer can skip pages and does not return write status to copy-back.

Resilience and Maintainability Implications

  • inferred — If a write is skipped after the merge, the updated snapshot cannot be used as proof of delivery. Repeating the comparison against that same snapshot would no longer find the missed byte, but the inspected callers coalesce overlaps and did not show a same-snapshot retry to lose.
Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 2
  • Waived: $0.50 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

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: 2fc643f3-1a66-4b87-999f-6b37ef30120c
Commits

Reviewing files that changed from the base of the PR and between 13b8b51 and 54035af.

Files selected for processing: 2

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

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

@pablogonz12
pablogonz12 merged commit 3cdf505 into main Nov 25, 2026
4 checks passed
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