Skip to content

GPU: copy-back puts back only what shaders wrote, in two more cases - #261

Open
Maraco91 wants to merge 1 commit into
Marvisak:mainfrom
Maraco91:gpu/copy-back-keeps-newer-title-bytes
Open

Maraco91 wants to merge 1 commit into
Marvisak:mainfrom
Maraco91:gpu/copy-back-keeps-newer-title-bytes

Conversation

@Maraco91

Copy link
Copy Markdown
Contributor

What

Shadow::copy_back can still write an older byte of the host copy over a byte the title wrote while the GPU ran: the failure its own comment describes ("putting back the copy's older bytes there broke the title's own records"), through two paths that are left.

copy_back takes every byte of a declared range that differs from the snapshot (before) for a shader's write, and puts it into title memory unless the title's byte already equals it. So anything else that makes the copy differ from the snapshot goes back as if a shader had written it.

1. A range declared inside a gap the snapshot already spans. There is one snapshot per copy and submission, grown to span every declared range, so it also holds the gaps between them, as the copy was when the snapshot grew over them. If a later range of the same submission lies in such a gap, its refresh runs after that and brings the title's current bytes into the copy; writing then finds the snapshot already spans the range and returns. For every byte that refresh changed, copy ≠ snapshot. If the title writes that byte again before the copy-back, the refresh's value is written over it.

update(A) writing(A)   update(C) writing(C)      snapshot = [A .. gap B .. C], B as last copied
update(B)                                        copy[B] = the title's bytes now (byte k: 0 -> 1)
writing(B)                                       returns: already spanned; snapshot[k] is still 0
   ... GPU runs; the title sets byte k to 2 ...
copy_back(B)                                     copy 1 != snapshot 0, title 2 != 1  =>  title byte k = 1

The world declares views of its 174 MB heap dozens of times per submission, in no particular order, so ranges inside earlier gaps are the normal case there.

2. Two declared ranges that overlap. For each 64 KiB piece it handles, copy_back leaves the copy holding the title's bytes (*h = *t), but the snapshot kept the old ones. A second range over the same piece (host.rs only folds identical ranges) then sees the bytes the first pass took from the title as shader writes, and puts them back over whatever the title wrote between the two passes.

The fix, in shadow.rs only:

  • writing works out the parts of the range that no earlier range of the submission declared (new helper outside). A refresh leaves declared ranges alone, so only those parts can have been refreshed since the snapshot took them; where the snapshot already held them, they are taken again from the copy. No shader has written yet at that point: every list's deferred steps run before the submission executes.
  • copy_back gives the snapshot the title's bytes along with the copy (*o = *t), so a piece that was handled no longer differs.

Cost: a byte is copied a second time only if it was first captured as part of a gap and declared later, at most once per submission; ranges declared again, and ranges that extend the snapshot, cost what they did. The body of writing moved under an if, hence the size of the diff.

I have not run a title (I have no game files), so I cannot say which of its symptoms this accounts for; what it does is intermittent corruption of title memory in buffers shaders write.

Tested

Windows 11 Pro 10.0.26200, NVIDIA RTX 3080 Ti (driver 32.0.16.1656), rustc 1.98.1 (x86_64-pc-windows-msvc), no game data.

shadow.rs had no tests. Two new ones, on a real host buffer (Host::shared_buffer; they return early without a host GPU, like the crate's other device tests) over three pages of plain heap memory standing in for title memory (not write-watched, so refreshes copy whole ranges). "The title writes" and "a shader writes" are single-byte writes into that memory and into the mapped copy.

  • a_range_between_earlier_ones_keeps_the_titles_newer_bytes: the sequence in the diagram. Checks the title's newer byte survives; controls: a byte the title wrote before the refresh and not after keeps its value, and the two bytes "the shader" wrote (one in an outer range, one in the late range) do go back to title memory; the snapshot is released at the end.
  • overlapping_ranges_keep_the_titles_newer_bytes: [a, a+1 page) and [a, a+2 pages) declared, a shader's byte and a title's byte in the first page, the title's byte written again between the two copy-backs. Checks the newer byte survives and the shader's byte went back.

Results:

Not tested: a title run; frame time in the world.

AI-assisted (Claude Code); the tests above were run on the machine described.

🤖 Generated with Claude Code

After a submission, copy_back writes into title memory the bytes of a
declared range that differ from the snapshot taken before the GPU ran,
unless the title's byte already equals the copy's. That is meant to be
what shaders wrote and nothing else: the title's threads write the same
memory while the GPU runs. Two things made other bytes differ from the
snapshot, and the copy's older value then went back over a byte the
title had written since:

- The snapshot spans the gaps between the ranges declared so far, taken
  from the copy as it was then. A range declared later inside such a gap
  was refreshed after that, so every byte its refresh brought in looked
  like a shader's write. writing now takes the parts of a range no
  earlier range declared again, where the snapshot already held them.
- copy_back leaves the copy holding the title's bytes for each piece it
  handled, but the snapshot kept the old ones, so a second declared range
  overlapping the first saw those bytes as a shader's. The snapshot now
  takes them too.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@jeremygold02

Copy link
Copy Markdown
Contributor

Tested current PR head a2243adc60d86e1540989447981a8e8e5c27f2fe.

  • cargo test --locked -p victory-gpu --lib shadow -- --test-threads=1: 2 passed, 44 filtered out. a_range_between_earlier_ones_keeps_the_titles_newer_bytes declares outer pages before the middle page, after an earlier snapshot already spans that gap; it checks that refreshes and copy-backs keep the title's newer middle-page bytes while returning shader-written bytes. overlapping_ranges_keep_the_titles_newer_bytes checks the same protection when two declared ranges overlap and the title updates a byte between their copy-backs (copy-back tests).

The fixtures use a host shared buffer and return early if no host GPU is available, so the result is conditional on that resource. They do not run a title.

@Vaximous

Copy link
Copy Markdown
Contributor

Supporting data for this PR's direction. On main (and on my local branch before the fix), copy-back reads a 64 KiB piece of title memory, merges the GPU's bytes in and writes the whole piece back. The title keeps CPU-side records inside its GPU heap, so any title write elsewhere in that piece between the read and the write is undone. In GTA VI that showed up as a title crash: a hash-map value pointer the title had just stored reverted to null, and several worker threads faulted on it at once.

Writing back only the runs of bytes the shader changed stopped it: about 1,100 s of world time across 5 runs with no recurrence.

One performance note: one WriteProcessMemory-style call per run cost 1.3-2.2 s/frame in the world on my machine (88% of the submit thread in ZwReadVirtualMemory). Batching helps. When a piece has more than 8 runs, I store them as plain writes into the page just read, still leaving the gap bytes untouched. That brought copy-back back to ~200 ms/frame.

Xeltax pushed a commit to Xeltax/VIcky that referenced this pull request Nov 24, 2026
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
The tester tree (upstream main ffe310e plus its PRs, update 8; see its
INTEGRATION.md) ran 11-25 minutes in the world on an RTX 5070 without a
device removal. Where both sides carry the same PR (the wave64 stack
#223/#233/#320 with #439 and #180, #454, #336, #259, #331 and others), the
tester tree's newer head is taken; our own work and the PRs only we
merged are kept.

Conflicts, file by file:

- descriptor/host.rs: the tester layout (`spare`, the guard slot after the
  dynamic slots, `free_table`, the capped sampler heap). A capped view
  heap keeps `capped_reserve()` dynamic slots, now 2^18 by default (the
  tester measurement, and our 91fed158), still `VICTORY_DYNAMIC_SLOTS` in
  diagnostics builds; uncapped hosts keep our 2^19. Our slot statistics
  (TAKEN/PEAK/SHORT), the bindless plan cache (`CommittedRegions`,
  `timed_valid_title_address`, `timed_find`, `title_bounds`) and the
  revision triple are kept beside the tester's `guard_slot`.
- Buffer states: the tester tree's buffer-state tracker (#334: every list
  moves host copies of title memory between GENERIC_READ and
  UNORDERED_ACCESS and back to common before it closes) and depth twins
  (#469) are taken. #40's enhanced/legacy indirect barriers (the indirect
  prelude, `use_indirect_buffers`, the title buffer handoff mirrored in
  `host_barrier`) cannot share buffers with that tracker and are dropped;
  #40's argument checks (`byte_len`, exact spans, overflow-safe clamp
  shader, aligned counts) stay. `indirect::bound` and #282's
  `rectangles` go through `buffer_state`, and `indirect::execute` (#334)
  replaces `done`. #178's DXC fallback compiles both of our passes.
  Our segments (#387: `close` ends the segment, `close_native` closes a
  native list) carry the tracker: each native list returns its buffers
  to common and its twins to their depth targets, and each prelude also
  holds our texture uploads.
- shadow.rs: our write flow (`queue_write`, one `snapshot` after the
  batch's refreshes, `finish_writes`, the unchanged-page cache) with
  #261's copy-back that also gives the snapshot the title's bytes; its
  tests run through our flow.
- pipeline/make.rs: #275's per-frame budget and per-pipeline wake pair,
  with our compute waits (a dispatch waits for its pipeline, up to 20 s).
- pipeline/library.rs: the tester version (#347, #275's saver thread);
  our concurrent-write test kept. shader_cache.rs: our byte-returning
  cache API with #358's "translated, only not kept".
- draw.rs: #493's root SRV tables; where no table can stand in, our
  drop of a draw whose root view is unset or outside title buffers. Our
  refusal of a table offset past the capped heap stays.
- resource.rs: `HostTexture` has both our `staged` generation and the
  depth twin.
- Shaders: the tester's lower.rs for the wave64 stack, #160's image
  sampling (it covers our #73 and #136 sample forms) and #453/#587; kept
  from our side: #136's preference for an image or buffer use of a
  descriptor read as data and its `s_buffer_` arm, #422's nearest-even
  narrowing (also for #169's d16 loads and #453's interpolation), #452's
  spin-wait limit, #391's LDS limit, #302's spilled root arguments (now in
  `NativeProgram::translation_ir`), #136's PSV0 interpolation beside the
  tester's row harmonisation. split_tests.rs, lane_read_loop_tests.rs and
  docs/wave64-on-wave32.md are the tester versions (the same PRs at newer
  heads).
- Tests: #483's heap guard dwords filled in our GPU tests of #200
  (v_div_scale) and #437 (scalar loads from a raw view), which read
  through the guard slot otherwise; #334's debug-layer test passes an
  offset to our `indirect::bound`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

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

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


Generated by Claude Code

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants