\ GPU: copy-backs put back only the bytes the GPU changed\ #2240

Open
wbkepler wants to merge 1 commit from upstream-pr/2242 into main
wbkepler commented 2026-11-25 08:36:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-25T08:36:00Z.


What's wrong

After a submission, each 64 KiB page that shaders marked as written (through views, copy_back_marked) or wrote at computed addresses (copy_back_page) goes back into title memory. Both paths read title memory, compared it with the host copy and then wrote the copy's whole page over title memory. The title's threads keep writing the same pages meanwhile: its GPU heap holds CPU-side records next to buffers that shaders write. Two races follow:

  1. A title write that lands after the copy-back has checked the page (changed) and before its write_mapped is overwritten by the copy's older bytes. A write during the read itself looks like a shader's byte (title memory differs from the copy) and is reverted too.
  2. The page is then taken with the bytes the copy-back had, so a title write between the read and the guard coming back is lost from the copy for good. The next copy-back of that page sees the copy differ from title memory and puts the stale bytes back over the title's record. This can happen much later.

The snapshot path (copy_back) merged correctly byte by byte, but it also wrote the merged 64 KiB piece from a buffer it read earlier, so it had race 1 as well.

On the host below, the title crashed with an access violation about 155 s into the world. It read an address computed from a per-object index, and that address lies deep in an unmapped gap of the address space. Reconstructing the address from the title's own minidump fits an index that held the float 1.0 in place of an index, with the array it points into in physical-page memory that a host copy guards (read-only pages). The world copies back about 700 marked pages every 5 s. I did not catch the bad write in the act, so this cause is inferred, not proven.

The fix (crates/victory-gpu/src/shadow.rs)

  • Copy-backs write only the runs of bytes the shaders changed (differing + write_runs). Runs less than 256 bytes apart go out as one span, and the bytes between them hold title memory as the copy-back read it. A kernel copy per run cost over 1 ms per page in the world, where shaders change many scattered words of a page. Title bytes farther than that from the shaders' bytes are never written.
  • copy_back_marked and copy_back_page share put_back. It arms the page while it reads title memory, so a title write that lands during the read saves the page's bytes from before (the existing guard save). The shaders' bytes can then still be told apart from the title's, and the page is unarmed before our own write.
  • After a copy-back, a whole page is taken first and then read again into the copy (take_and_read). A title write before the guard is back is in the copy. A write after it faults and marks the page changed. The copy never takes the page with bytes read earlier.
  • copy_back (snapshot path) writes only the bytes the GPU changed, through the same helpers, and takes and re-reads its whole pages the same way.

Cost: one more 64 KiB read per page that goes back, and one kernel copy per span instead of one per page.

Shortcut: inside a span, a title write that lands between the copy-back's read and its write is still undone. That window is microseconds, and the write has to fall within 256 bytes of bytes the shaders changed.

Tests

New test shadow::tests::a_copy_back_puts_back_only_the_bytes_the_gpu_changed. A test-only hook makes a title write right after the copy-back has read the page, on a page marked through views and on a page written at computed addresses. The test checks that the shader's byte and the title's byte both end up in title memory, and that the copy holds the title's byte. With the previous whole-page write, the title's byte would be reverted at that point.

The existing shadow tests (marked_pages_go_back_by_the_bytes_the_gpu_changed, title_writes_under_a_busy_range_are_copied, snapshots_cover_the_declared_ranges_and_writes_go_back_once) still pass unchanged.

Gate on Windows 11, NVIDIA RTX 5090:

  • cargo clippy -p victory-gpu --all-targets -- -D warnings and cargo clippy -p victory --features diagnostics --all-targets -- -D warnings: clean.
  • cargo test -p victory-gpu: 142 passed, 2 failed. The two failures are unrelated to this change: descriptor::host::tests::dynamic_slots_are_held_until_released and memory::tests::lists_share_a_table_and_copies_keep_their_views both assert the view heap's size or layout, and this host caps shader-visible view heaps at 1M descriptors.
  • cargo xtask policy: ok.

Live

Windows 11, NVIDIA RTX 5090, a private integration build plus this fix (an earlier revision that wrote each run separately, without the 256-byte spans):

  • Without the fix: one run crashed with the access violation after about 155 s in the world. Two more runs ended with DEVICE_HUNG after about 210–220 s from start.
  • With the fix: the run went the full 300 s with no access violation and no device removal. The world loaded late in that run (from about +150 s), so this covers only about 150 s in the world.

That run alone does not prove the crash is gone.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:5487912632 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2242). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-25T08:36:00Z. --- ### What's wrong After a submission, each 64 KiB page that shaders marked as written (through views, `copy_back_marked`) or wrote at computed addresses (`copy_back_page`) goes back into title memory. Both paths read title memory, compared it with the host copy and then wrote the copy's **whole page** over title memory. The title's threads keep writing the same pages meanwhile: its GPU heap holds CPU-side records next to buffers that shaders write. Two races follow: 1. A title write that lands after the copy-back has checked the page (`changed`) and before its `write_mapped` is overwritten by the copy's older bytes. A write during the read itself looks like a shader's byte (title memory differs from the copy) and is reverted too. 2. The page is then `taken` with the bytes the copy-back had, so a title write between the read and the guard coming back is lost from the copy for good. The next copy-back of that page sees the copy differ from title memory and puts the stale bytes back over the title's record. This can happen much later. The snapshot path (`copy_back`) merged correctly byte by byte, but it also wrote the merged 64 KiB piece from a buffer it read earlier, so it had race 1 as well. On the host below, the title crashed with an access violation about 155 s into the world. It read an address computed from a per-object index, and that address lies deep in an unmapped gap of the address space. Reconstructing the address from the title's own minidump fits an index that held the float 1.0 in place of an index, with the array it points into in physical-page memory that a host copy guards (read-only pages). The world copies back about 700 marked pages every 5 s. I did not catch the bad write in the act, so this cause is inferred, not proven. ### The fix (`crates/victory-gpu/src/shadow.rs`) - Copy-backs write only the **runs of bytes the shaders changed** (`differing` + `write_runs`). Runs less than 256 bytes apart go out as one span, and the bytes between them hold title memory as the copy-back read it. A kernel copy per run cost over 1 ms per page in the world, where shaders change many scattered words of a page. Title bytes farther than that from the shaders' bytes are never written. - `copy_back_marked` and `copy_back_page` share `put_back`. It arms the page while it reads title memory, so a title write that lands during the read saves the page's bytes from before (the existing guard save). The shaders' bytes can then still be told apart from the title's, and the page is unarmed before our own write. - After a copy-back, a whole page is **taken first and then read again into the copy** (`take_and_read`). A title write before the guard is back is in the copy. A write after it faults and marks the page changed. The copy never takes the page with bytes read earlier. - `copy_back` (snapshot path) writes only the bytes the GPU changed, through the same helpers, and takes and re-reads its whole pages the same way. Cost: one more 64 KiB read per page that goes back, and one kernel copy per span instead of one per page. Shortcut: inside a span, a title write that lands between the copy-back's read and its write is still undone. That window is microseconds, and the write has to fall within 256 bytes of bytes the shaders changed. ### Tests New test `shadow::tests::a_copy_back_puts_back_only_the_bytes_the_gpu_changed`. A test-only hook makes a title write right after the copy-back has read the page, on a page marked through views and on a page written at computed addresses. The test checks that the shader's byte and the title's byte both end up in title memory, and that the copy holds the title's byte. With the previous whole-page write, the title's byte would be reverted at that point. The existing shadow tests (`marked_pages_go_back_by_the_bytes_the_gpu_changed`, `title_writes_under_a_busy_range_are_copied`, `snapshots_cover_the_declared_ranges_and_writes_go_back_once`) still pass unchanged. Gate on Windows 11, NVIDIA RTX 5090: - `cargo clippy -p victory-gpu --all-targets -- -D warnings` and `cargo clippy -p victory --features diagnostics --all-targets -- -D warnings`: clean. - `cargo test -p victory-gpu`: 142 passed, 2 failed. The two failures are unrelated to this change: `descriptor::host::tests::dynamic_slots_are_held_until_released` and `memory::tests::lists_share_a_table_and_copies_keep_their_views` both assert the view heap's size or layout, and this host caps shader-visible view heaps at 1M descriptors. - `cargo xtask policy`: ok. ### Live Windows 11, NVIDIA RTX 5090, a private integration build plus this fix (an earlier revision that wrote each run separately, without the 256-byte spans): - **Without the fix:** one run crashed with the access violation after about 155 s in the world. Two more runs ended with `DEVICE_HUNG` after about 210–220 s from start. - **With the fix:** the run went the full 300 s with no access violation and no device removal. The world loaded late in that run (from about +150 s), so this covers only about 150 s in the world. That run alone does not prove the crash is gone. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
DenRakEiw commented 2026-11-25 08:42:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T08:42:31Z.

Review: APPROVED


Verified locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-gpu --lib shadow:: — 5 passed, 0 failed
  • cargo clippy -p victory-gpu --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean run-based copy-back writing only bytes actually changed by the GPU without clobbering concurrent CPU writes. Looks great.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6054307957 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2242#pullrequestreview-6054307957). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T08:42:31Z. **Review:** APPROVED --- Verified locally merged onto `origin/main` (`940d32f`, Windows 11 x64, Rust 1.98.1 MSVC): - `cargo test -p victory-gpu --lib shadow::` — **5 passed, 0 failed** - `cargo clippy -p victory-gpu --all-targets` — 0 warnings - `cargo xtask policy` — ok (`183 files ok`) Clean run-based copy-back writing only bytes actually changed by the GPU without clobbering concurrent CPU writes. Looks great. AI-assisted (Gemini 4 Neon).
catminou4 commented 2026-11-25 08:48:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T08:48:36Z.

Review: COMMENTED


Copy-backs put back only the bytes the GPU changed: differing computes changed runs word-at-a-time, SPAN_GAP=256 merges nearby runs. Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1. Notable perf fix (whole-page copy-back cost >1 ms/page in-world) and avoids clobbering concurrent title writes — the motivation is well documented.

<!-- greyfinch-sync:Marvisak/VIctory:review:6054381798 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2242#pullrequestreview-6054381798). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T08:48:36Z. **Review:** COMMENTED --- Copy-backs put back only the bytes the GPU changed: `differing` computes changed runs word-at-a-time, SPAN_GAP=256 merges nearby runs. Static review only: victory-gpu/victory-hle are Windows-only (D3D12/windows-sys); not compiled or tested on this macOS arm64 host. rustc 1.98.1. Notable perf fix (whole-page copy-back cost >1 ms/page in-world) and avoids clobbering concurrent title writes — the motivation is well documented.
MamaLeVrai commented 2026-11-25 10:07:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by MamaLeVrai on 2026-11-25T09:13:30Z.


Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present, so the hardware-only shader checks run. I merged this PR's head 09595753 into main 940d32f in a throwaway tree (no conflict). cargo clippy --workspace --all-targets --locked -- -D warnings: clean; the same with --all-features: clean. cargo test --workspace --locked: 702 passed, 0 failed, 10 ignored, two runs (main alone on the same machine: 701 passed, 0 failed, 10 ignored). The title was not run, so this says nothing about in-game effect. No game data.

AI-assisted (Claude Code); no game data.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158042236 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2242#issuecomment-7158042236). Originally posted by [MamaLeVrai](https://github.com/MamaLeVrai) on 2026-11-25T09:13:30Z. --- Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present, so the hardware-only shader checks run. I merged this PR's head `09595753` into `main` `940d32f` in a throwaway tree (no conflict). `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean; the same with `--all-features`: clean. `cargo test --workspace --locked`: 702 passed, 0 failed, 10 ignored, two runs (`main` alone on the same machine: 701 passed, 0 failed, 10 ignored). The title was not run, so this says nothing about in-game effect. No game data. AI-assisted (Claude Code); no game data.
Calvix2k commented 2026-11-25 10:07:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Calvix2k on 2026-11-25T09:44:29Z.


Independent run of the base commit's behaviour on Windows 10 19045 / RX 7800 XT, as part of testing #2280 (which is stacked on this): shadow:: 6/6 and the whole victory-gpu --features diagnostics suite 147 passed, 1 failed (the known Windows 10 ID3D12Device10 placed-view test, #1999/#2251). The byte-only copy-back is exercised by those tests on this host without a regression.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7158737172 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2242#issuecomment-7158737172). Originally posted by [Calvix2k](https://github.com/Calvix2k) on 2026-11-25T09:44:29Z. --- Independent run of the base commit's behaviour on Windows 10 19045 / RX 7800 XT, as part of testing #2280 (which is stacked on this): `shadow::` **6/6** and the whole `victory-gpu --features diagnostics` suite **147 passed, 1 failed** (the known Windows 10 `ID3D12Device10` placed-view test, #1999/#2251). The byte-only copy-back is exercised by those tests on this host without a regression.
alexodee commented 2026-11-25 12:03:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by alexodee on 2026-11-25T11:09:14Z.


In-game numbers for spans against one kernel write per run. They come from a fork and not from this branch, so they say something about the idea and nothing about this PR's code.

The fork (fxceless/victory-gta6) has the per-run write (after #2009) in its snapshot copy-back since its 8dd30ab, through NtWriteVirtualMemory. Locally I join runs at most 4096 bytes apart. Medians over gameplay report lines:

differing pieces a frame (64 KiB at most) putting them back frame
older fork tree, one write per stretch 609 13.4 ms 421 ms
one write per run 688 632 ms 1,109 ms
runs up to 4096 bytes apart in one write 680-685 37.3 ms 462-467 ms

On a newer fork tree (more PRs, 925-954 pieces a frame), same executable and switches: 54.3 ms with 4096 and 50.8 ms with 256 (45 and 35 report lines, one launch each, so equal within noise). The smaller gap, as in this PR, cost nothing extra here, and it keeps the race window smaller.

Per-run writes were about 0.9 ms a piece, close to the "over 1 ms per page" in this PR's description. The same idea is open for the fork with a gap of 64 (https://github.com/fxceless/victory-gta6/pull/857, measured under Wine).

Setup: RTX 3070 Ti Laptop 8 GB (driver 596.36), i7-12700H, 16 GB RAM, Windows 11, diagnostics build, Series X profile. One machine, one run per row. The per-run and spans rows come from the same executable, but each spans run differs by one more switch, so this is not a strict A/B.

AI-assisted (Claude Code). No game data.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7160663809 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2242#issuecomment-7160663809). Originally posted by [alexodee](https://github.com/alexodee) on 2026-11-25T11:09:14Z. --- In-game numbers for spans against one kernel write per run. They come from a fork and not from this branch, so they say something about the idea and nothing about this PR's code. The fork (fxceless/victory-gta6) has the per-run write (after #2009) in its snapshot copy-back since its `8dd30ab`, through `NtWriteVirtualMemory`. Locally I join runs at most 4096 bytes apart. Medians over gameplay report lines: | | differing pieces a frame (64 KiB at most) | putting them back | frame | |---|---:|---:|---:| | older fork tree, one write per stretch | 609 | 13.4 ms | 421 ms | | one write per run | 688 | 632 ms | 1,109 ms | | runs up to 4096 bytes apart in one write | 680-685 | 37.3 ms | 462-467 ms | On a newer fork tree (more PRs, 925-954 pieces a frame), same executable and switches: 54.3 ms with 4096 and **50.8 ms with 256** (45 and 35 report lines, one launch each, so equal within noise). The smaller gap, as in this PR, cost nothing extra here, and it keeps the race window smaller. Per-run writes were about 0.9 ms a piece, close to the "over 1 ms per page" in this PR's description. The same idea is open for the fork with a gap of 64 (https://github.com/fxceless/victory-gta6/pull/857, measured under Wine). Setup: RTX 3070 Ti Laptop 8 GB (driver 596.36), i7-12700H, 16 GB RAM, Windows 11, diagnostics build, Series X profile. One machine, one run per row. The per-run and spans rows come from the same executable, but each spans run differs by one more switch, so this is not a strict A/B. AI-assisted (Claude Code). No game data.
r4tedx commented 2026-11-25 13:03:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by r4tedx on 2026-11-25T12:59:37Z.


In-game A/B of this PR together with #2280 (stacked), on an RTX 5070 12 GB, Windows 11, Series X profile, diagnostics build. Local title data was used for the in-game measurement; only log lines, sizes and timings are reported here.

Both builds come from the same tree (our play tree on main 940d32f, fork branch gpu-run/layer2 533217b7) with the same shader cache, DRED off, no captures, and 6 minutes each with the same scripted input. There were 0 device events in either. The numbers are medians of the copy-back report lines over the world part of each run.

Build Written back a frame Time Per MB Copy-back line
with #2242 + #2280 (+ a word-at-a-time compare of ours, see below) 68.1 MB 144.8 ms 2.13 ms/MB 188 ms
the same tree with #2280 and #2242 reverted 75.6 MB 59.7 ms 0.79 ms/MB 68 ms

Two earlier runs agree: 0.91 ms/MB before these two PRs went into the tree, and 2.22 ms/MB after.

What this says, both ways:

  • The PRs fix a real race: title writes made while a copy-back runs are no longer lost. Nothing in these runs argues against that.
  • On this NVIDIA machine they cost about 2.5x per MB written back, roughly +120 ms of copy-back a frame at ~70 MB/frame.
  • The compare is not the cost. Our commit eb034c41 (fork branch perf/copy-back-word-runs, on #2280's head) tests a differing line 8 bytes at a time, and it is in the first row: no gain. What's left is the per-span kernel writes (write_runs → host::write_mapped) and the second 64 KiB read per page in take_and_read. We're benchmarking those separately (synthetic, WARP) and will post the breakdown.
  • Caveats: the two runs were not in the same scene (3,659 against 2,437 draws a frame in the world), so per-MB is the comparable figure, not the totals. Frame time came out about equal (830 against 844 ms). One run per build.

alexodee's numbers above point the same way: per-run writes were the cost there, and joining runs brought it back down.

AI-assisted

<!-- greyfinch-sync:Marvisak/VIctory:comment:7163139752 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2242#issuecomment-7163139752). Originally posted by [r4tedx](https://github.com/r4tedx) on 2026-11-25T12:59:37Z. --- In-game A/B of this PR together with #2280 (stacked), on an RTX 5070 12 GB, Windows 11, Series X profile, diagnostics build. Local title data was used for the in-game measurement; only log lines, sizes and timings are reported here. Both builds come from the same tree (our play tree on `main` 940d32f, fork branch `gpu-run/layer2` 533217b7) with the same shader cache, DRED off, no captures, and 6 minutes each with the same scripted input. There were 0 device events in either. The numbers are medians of the copy-back report lines over the world part of each run. | Build | Written back a frame | Time | Per MB | Copy-back line | |---|---:|---:|---:|---:| | with #2242 + #2280 (+ a word-at-a-time compare of ours, see below) | 68.1 MB | 144.8 ms | 2.13 ms/MB | 188 ms | | the same tree with #2280 and #2242 reverted | 75.6 MB | 59.7 ms | 0.79 ms/MB | 68 ms | Two earlier runs agree: 0.91 ms/MB before these two PRs went into the tree, and 2.22 ms/MB after. What this says, both ways: - The PRs fix a real race: title writes made while a copy-back runs are no longer lost. Nothing in these runs argues against that. - On this NVIDIA machine they cost about 2.5x per MB written back, roughly +120 ms of copy-back a frame at ~70 MB/frame. - The compare is not the cost. Our commit `eb034c41` (fork branch `perf/copy-back-word-runs`, on #2280's head) tests a differing line 8 bytes at a time, and it is in the first row: no gain. What's left is the per-span kernel writes (`write_runs` → `host::write_mapped`) and the second 64 KiB read per page in `take_and_read`. We're benchmarking those separately (synthetic, WARP) and will post the breakdown. - Caveats: the two runs were not in the same scene (3,659 against 2,437 draws a frame in the world), so per-MB is the comparable figure, not the totals. Frame time came out about equal (830 against 844 ms). One run per build. alexodee's numbers above point the same way: per-run writes were the cost there, and joining runs brought it back down. AI-assisted
Marvisak commented 2026-11-25 14:07:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T13:31:41Z.


Automated triage for the maintainers.

This pull request no longer merges cleanly into main (conflicts in crates/victory-gpu/src/shadow.rs). It will be reviewed once it is rebased onto the current main. The earlier verdict (merge-candidate) was for an older state of main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7163905988 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2242#issuecomment-7163905988). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T13:31:41Z. --- <!-- victory-triage v1 head=09595753c1e3f4365a6fa2554099ac40e4274685 verdict=needs-rebase at=2026-11-25T16:19:00Z --> Automated triage for the maintainers. This pull request no longer merges cleanly into `main` (conflicts in `crates/victory-gpu/src/shadow.rs`). It will be reviewed once it is rebased onto the current `main`. The earlier verdict (merge-candidate) was for an older state of `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
r4tedx commented 2026-11-25 15:04:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by r4tedx on 2026-11-25T14:30:31Z.


Correction to my in-game A/B above (https://github.com/Marvisak/VIctory/pull/2242#issuecomment-7163139752): the second build did not revert this PR. Our revert commit removed only #2280's block compare; this PR's put_back, differing, write_runs and take_and_read were still in. So, per MB written back by marked-page copy-backs (RTX 5070, our play build, local title data used for the in-game measurement; only sizes and timings reported):

Build Per MB
neither PR (an earlier run) 0.91 ms/MB
this PR alone 0.79 ms/MB
this PR + #2280 2.22 ms/MB
this PR + #2280 + a word-at-a-time compare of ours 2.13 ms/MB

In game, then, this PR alone cost about the same per MB as before it. The extra cost came in with #2280. I attributed it to this PR's per-span writes, which these runs don't support. Sorry for the noise. The runs were in different scenes, one run each, so treat per-MB as the comparable figure. The synthetic breakdown is going on #2280.

The point from triage about SPAN_GAP (a title write that lands in a merged gap after the read is undone) stands on its own, and isn't affected by this.

AI-assisted

<!-- greyfinch-sync:Marvisak/VIctory:comment:7165385283 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2242#issuecomment-7165385283). Originally posted by [r4tedx](https://github.com/r4tedx) on 2026-11-25T14:30:31Z. --- Correction to my in-game A/B above (https://github.com/Marvisak/VIctory/pull/2242#issuecomment-7163139752): the second build did **not** revert this PR. Our revert commit removed only #2280's block compare; this PR's `put_back`, `differing`, `write_runs` and `take_and_read` were still in. So, per MB written back by marked-page copy-backs (RTX 5070, our play build, local title data used for the in-game measurement; only sizes and timings reported): | Build | Per MB | |---|---:| | neither PR (an earlier run) | 0.91 ms/MB | | **this PR alone** | **0.79 ms/MB** | | this PR + #2280 | 2.22 ms/MB | | this PR + #2280 + a word-at-a-time compare of ours | 2.13 ms/MB | In game, then, this PR alone cost about the same per MB as before it. The extra cost came in with #2280. I attributed it to this PR's per-span writes, which these runs don't support. Sorry for the noise. The runs were in different scenes, one run each, so treat per-MB as the comparable figure. The synthetic breakdown is going on #2280. The point from triage about `SPAN_GAP` (a title write that lands in a merged gap after the read is undone) stands on its own, and isn't affected by this. AI-assisted
r4tedx commented 2026-11-25 15:04:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by r4tedx on 2026-11-25T14:31:37Z.


Correcting my correction above (https://github.com/Marvisak/VIctory/pull/2242#issuecomment-7165385283): it was wrong. I checked the wrong commit. The build behind the 0.79 ms/MB figure was made from a tree with both this PR and #2280 reverted: its head reverts this PR's merge, on top of the #2280 revert, and the exe was built after that commit. So my first comment's reading stands:

Build Per MB
neither PR 0.79 ms/MB (and 0.91 in an earlier run)
this PR + #2280 2.22 ms/MB
this PR + #2280 + our word-at-a-time compare 2.13 ms/MB

The ~2.5x comes with the two PRs together. How it splits between them is not measured in game. A run with this PR alone is planned, and I'll post it here when it's done. Sorry for the noise.

AI-assisted

<!-- greyfinch-sync:Marvisak/VIctory:comment:7165413544 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2242#issuecomment-7165413544). Originally posted by [r4tedx](https://github.com/r4tedx) on 2026-11-25T14:31:37Z. --- Correcting my correction above (https://github.com/Marvisak/VIctory/pull/2242#issuecomment-7165385283): it was wrong. I checked the wrong commit. The build behind the 0.79 ms/MB figure was made from a tree with **both** this PR and #2280 reverted: its head reverts this PR's merge, on top of the #2280 revert, and the exe was built after that commit. So my first comment's reading stands: | Build | Per MB | |---|---:| | neither PR | 0.79 ms/MB (and 0.91 in an earlier run) | | this PR + #2280 | 2.22 ms/MB | | this PR + #2280 + our word-at-a-time compare | 2.13 ms/MB | The ~2.5x comes with the two PRs together. How it splits between them is **not measured** in game. A run with this PR alone is planned, and I'll post it here when it's done. Sorry for the noise. AI-assisted
r4tedx commented 2026-11-25 16:09:06 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by r4tedx on 2026-11-25T15:10:35Z.


Data for the triage point about SPAN_GAP. Synthetic: WARP, release build, 1,216 marked 64 KiB pages, µs per page for the whole copy-back, mean of 2 rounds; rounds differ by about 10-25 %. The title was not used in these tests.

GPU writes before this PR this PR (gap 256) this PR, gap 0 plain-store spans, gap 256 plain-store spans, gap 0
whole page 27.0 94.8 103.9 56.4 48.3
a word per 64 B 27.0 48.8 509.0 65.2 78.3
a word per 512 B 27.6 88.8 99.6 47.3 44.9
a word per 4 KiB 27.4 40.8 39.0 39.0 41.0
every other byte 27.5 445.5 15,791 384.8 789.7
  • Kernel copy per span: each span costs about 0.4 µs (61-67 µs per page at 128 spans). With the kernel copy, writing exact runs (gap 0) is too slow: 509 µs per page at one word per 64 B.
  • Plain stores: in our fork branch r4tedx/VIctory perf/copy-back-spans, spans are written with plain stores. A store that faults (page unmapped meanwhile, or read-only to the title) is resumed past by the guard handler, and the kernel copy writes the rest. That brings 128 spans to about 13 µs, and exact runs cost little more than gap 256. So with plain stores, gap 0 is affordable and closes the race where a title write between two close runs is undone. The branch has a test for that (a_title_write_between_close_runs_stays).
  • Caveat: the plain-store columns also carry a fork-only change to differing (8 bytes at a time inside a differing line).

The in-game split between this PR and #2280 isn't measured yet; a run with this PR alone is planned.

AI-assisted

<!-- greyfinch-sync:Marvisak/VIctory:comment:7166423468 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2242#issuecomment-7166423468). Originally posted by [r4tedx](https://github.com/r4tedx) on 2026-11-25T15:10:35Z. --- Data for the triage point about `SPAN_GAP`. Synthetic: WARP, release build, 1,216 marked 64 KiB pages, µs per page for the whole copy-back, mean of 2 rounds; rounds differ by about 10-25 %. The title was not used in these tests. | GPU writes | before this PR | this PR (gap 256) | this PR, gap 0 | plain-store spans, gap 256 | plain-store spans, gap 0 | |---|--:|--:|--:|--:|--:| | whole page | 27.0 | 94.8 | 103.9 | 56.4 | 48.3 | | a word per 64 B | 27.0 | 48.8 | **509.0** | 65.2 | 78.3 | | a word per 512 B | 27.6 | 88.8 | 99.6 | 47.3 | 44.9 | | a word per 4 KiB | 27.4 | 40.8 | 39.0 | 39.0 | 41.0 | | every other byte | 27.5 | 445.5 | **15,791** | 384.8 | 789.7 | - **Kernel copy per span:** each span costs about 0.4 µs (61-67 µs per page at 128 spans). With the kernel copy, writing exact runs (gap 0) is too slow: 509 µs per page at one word per 64 B. - **Plain stores:** in our fork branch r4tedx/VIctory `perf/copy-back-spans`, spans are written with plain stores. A store that faults (page unmapped meanwhile, or read-only to the title) is resumed past by the guard handler, and the kernel copy writes the rest. That brings 128 spans to about 13 µs, and exact runs cost little more than gap 256. So with plain stores, gap 0 is affordable and closes the race where a title write between two close runs is undone. The branch has a test for that (`a_title_write_between_close_runs_stays`). - **Caveat:** the plain-store columns also carry a fork-only change to `differing` (8 bytes at a time inside a differing line). The in-game split between this PR and #2280 isn't measured yet; a run with this PR alone is planned. AI-assisted
r4tedx commented 2026-11-25 16:09:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by r4tedx on 2026-11-25T16:02:33Z.


An in-game number for this PR alone, which settles the split I said was unmeasured. RTX 5070, our play build, local title data used for the in-game measurement; only copy-back sizes and timings are reported.

I checked that the measured build's tree has this PR's put_back / write_runs / take_and_read / SPAN_GAP, and none of #2280's block/line compare.

Build Copy-back per MB written back
neither PR 0.79 ms/MB
this PR + #2280 2.22 ms/MB
this PR alone 2.51 ms/MB

So in game the extra cost comes with this PR itself; #2280 adds little or nothing on top.

Caveat: the "alone" build is a later, larger tree (it also has #1962 and other changes) in a different scene, 10 min, one run. Read the per-MB figure as a rough comparison.

That fits the synthetic breakdown I posted above: about 0.4 µs per span for the kernel copy, so 61-67 µs per page at 128 spans. Our fork branch perf/copy-back-spans writes spans with plain stores and keeps the kernel copy as the fallback when a store faults. We'll run it in game next, rebased onto this PR alone, and post the number here.

AI-assisted

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167541007 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2242#issuecomment-7167541007). Originally posted by [r4tedx](https://github.com/r4tedx) on 2026-11-25T16:02:33Z. --- An in-game number for this PR alone, which settles the split I said was unmeasured. RTX 5070, our play build, local title data used for the in-game measurement; only copy-back sizes and timings are reported. I checked that the measured build's tree has this PR's `put_back` / `write_runs` / `take_and_read` / `SPAN_GAP`, and none of #2280's block/line compare. | Build | Copy-back per MB written back | |---|---:| | neither PR | 0.79 ms/MB | | this PR + #2280 | 2.22 ms/MB | | **this PR alone** | **2.51 ms/MB** | So in game the extra cost comes with this PR itself; #2280 adds little or nothing on top. **Caveat:** the "alone" build is a later, larger tree (it also has #1962 and other changes) in a different scene, 10 min, one run. Read the per-MB figure as a rough comparison. That fits the synthetic breakdown I posted above: about 0.4 µs per span for the kernel copy, so 61-67 µs per page at 128 spans. Our fork branch `perf/copy-back-spans` writes spans with plain stores and keeps the kernel copy as the fallback when a store faults. We'll run it in game next, rebased onto this PR alone, and post the number here. AI-assisted
latentdev commented 2026-11-26 17:36:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by latentdev on 2026-11-26T17:36:04Z.


One more case in the same function: the "kept the title's" shortcut loses a ping-ponged exposure result (RTX 4090, in game)

This is the other side of the races described here: shaders' bytes that never reach title memory.

copy_back_marked has the documented shortcut that a marked page the title also wrote, without the page being armed, keeps the title's bytes whole. Our copy statistics show it costing 1–3 pages a frame in the world (kept the title's). One of those pages holds a pass result the title reads back the next frame:

  • avgLuminanceCompute:ComputeLuxCompressionInfo (441d01cd75dc5d5d) writes a 16-byte [x, 1/x, 8, 0.125] through a UAV buffer view.
  • It alternates frame by frame between two title buffers.
  • I traced six consecutive gameplay frames, with title memory read when each dispatch was recorded:
    • one buffer never changed: [43.4062, 0.023038, 8, 0.125] every time it was bound;
    • the other evolved: 64.76 → 61.22 → 55.91.

The first buffer's page is evidently one the title writes too, so every frame lost the shaders' result there. Its stale value went back into the copy at the next refresh.

Local fix that brings it back. In Shadow::gpu_writes, views of up to 4096 bytes take the snapshot path even when their writes are marked: let marked = marked && len > 4096;. The snapshot comparison is byte-precise, so the title's other bytes on that page stay. With it, the frozen buffer updates (25.7 → 128.7 → 33.6 → 167.4 across its uses in the same scene). Now and then either buffer still repeats a value, which I haven't run down. I haven't measured the snapshots' cost.

Caveat. The presented picture in that scene still alternates between very dark and very bright frames. The exposure values now swing several-fold from frame to frame. At ~1–2 fps the title's adaptation is probably unstable on its own. So this fixes the lost writes, not the flicker.

The same comparison would apply to any small marked result the title reads back from a page it also writes (counters, indirect counts, parameters). Your byte-run copy-back may make the threshold unnecessary if it can also cover unarmed pages the title wrote.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7199301467 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2242#issuecomment-7199301467). Originally posted by [latentdev](https://github.com/latentdev) on 2026-11-26T17:36:04Z. --- ### One more case in the same function: the "kept the title's" shortcut loses a ping-ponged exposure result (RTX 4090, in game) This is the other side of the races described here: shaders' bytes that never reach title memory. `copy_back_marked` has the documented shortcut that a marked page the title also wrote, without the page being armed, keeps the title's bytes whole. Our copy statistics show it costing 1–3 pages a frame in the world (`kept the title's`). One of those pages holds a pass result the title reads back the next frame: - `avgLuminanceCompute:ComputeLuxCompressionInfo` (`441d01cd75dc5d5d`) writes a 16-byte `[x, 1/x, 8, 0.125]` through a UAV buffer view. - It alternates frame by frame between two title buffers. - I traced six consecutive gameplay frames, with title memory read when each dispatch was recorded: - one buffer never changed: `[43.4062, 0.023038, 8, 0.125]` every time it was bound; - the other evolved: 64.76 → 61.22 → 55.91. The first buffer's page is evidently one the title writes too, so every frame lost the shaders' result there. Its stale value went back into the copy at the next refresh. **Local fix that brings it back.** In `Shadow::gpu_writes`, views of up to 4096 bytes take the snapshot path even when their writes are marked: `let marked = marked && len > 4096;`. The snapshot comparison is byte-precise, so the title's other bytes on that page stay. With it, the frozen buffer updates (25.7 → 128.7 → 33.6 → 167.4 across its uses in the same scene). Now and then either buffer still repeats a value, which I haven't run down. I haven't measured the snapshots' cost. **Caveat.** The presented picture in that scene still alternates between very dark and very bright frames. The exposure values now swing several-fold from frame to frame. At ~1–2 fps the title's adaptation is probably unstable on its own. So this fixes the lost writes, not the flicker. The same comparison would apply to any small marked result the title reads back from a page it also writes (counters, indirect counts, parameters). Your byte-run copy-back may make the threshold unnecessary if it can also cover unarmed pages the title wrote.
latentdev commented 2026-11-26 19:13:35 +00:00 (Migrated from github.com)

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


Correction to my caveat above: the flicker wasn't the title's exposure adaptation being unstable at low frame rates.

The lost-writes finding about copy_back_marked's "kept the title's" shortcut stands on its own.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7201335225 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2242#issuecomment-7201335225). Originally posted by [latentdev](https://github.com/latentdev) on 2026-11-26T19:13:35Z. --- Correction to my caveat above: the flicker wasn't the title's exposure adaptation being unstable at low frame rates. - The fxceless fork, slowed to about 2 fps on the same card, holds the shot steady. - In our tree the black frames were whole frames whose final output stayed black while the tonemapped scene was lit. - They went away when #2121 was updated (details: https://github.com/Marvisak/VIctory/pull/2121#issuecomment-7201331624). The lost-writes finding about `copy_back_marked`'s "kept the title's" shortcut stands on its own.
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/2242:upstream-pr/2242'
git switch 'upstream-pr/2242'

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