GPU: marked-page copy-backs skip work that changes nothing (stacked on #2280) #2407

Open
wbkepler wants to merge 3 commits from upstream-pr/2409 into main
wbkepler commented 2026-11-25 13:07:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by wbkepler on 2026-11-25T13:07:08Z.


Stacked on #2280 (gpu/copy-back-block-compare, itself on #2242); only the last commit is new. It does not need #2175: the marked-page path here uses the views' bitmap in shadow.rs, not the page-table bitmap #2175 moved.

What

After a submission, every 64 KiB page shaders marked as written through views (copy_back_marks → Shadow::copy_back_marked) is read from title memory, compared with the copy, and taken again. In the world that is about 2,150 pages per frame, and about a third of them hold no byte that changed. Four changes, all in crates/victory-gpu/src/shadow.rs:

  • No copy of bytes that are already equal. With no bytes saved by the guard, differing runs over every byte where the copy and title memory differ, so outside the runs the copy already holds title memory's bytes. take_others (a 64 KiB write per page) now runs only when saved bytes exist.
  • No second take of an untouched page. If neither our copy-back nor the title wrote the page (a title write marks it changed), it is still taken and guarded and the copy matches title memory, so it is no longer taken and read back in full.
  • One reused read buffer (thread-local, one page) instead of a new zeroed Vec per page.
  • The bitmap scan skips zero words instead of writing back every word.

Behaviour stays the same. The saved-bytes path, pages the title wrote, and pages the copy only partly holds all go through the same code as before.

Measured (Windows 11, NVIDIA RTX 5090)

Same 300 s world run before and after, on an integration build with the GPU-upload copies switched on. Figures are medians of the 5 s frame lines from 150 s on (n=26 each):

before after
copying back 142.3 ms 116.2 ms
marked pages (copystat) ~2,140 in ~133 ms ~2,145 in ~109 ms
on the GPU 83.7 ms 85.4 ms
frame 466.7 ms (2.1 fps) 455.6 ms (2.2 fps)

No device removal, no access violations, and captured world frames look the same as before.

Not done: finer write marks

I also tried marking 4 KiB blocks instead of 64 KiB pages (shader shift 12, a 2^24-bit bitmap, and a copy-back that reads and compares only the marked blocks of each page). Shaders mark about 28,400 of the ~34,400 blocks in the marked pages (83%). About 16,100 of those blocks actually change, and a third of the marked pages are written with identical bytes. So the compare barely shrinks. GPU time went up about 7 ms (91.4 vs 83.7) from the extra atomics, and copy-back came out at 124.7 ms, worse than this PR's 116 ms. I left it out.

Tests: cargo test -p victory-gpu passes, plus the new a_marked_page_the_shaders_left_alone_stays_taken. Two tests also fail on the base on this host, unrelated to this change: dynamic_slots_are_held_until_released and lists_share_a_table_and_copies_keep_their_views. The view heap has 1M descriptors on NVIDIA, below HEAP_VIEWS. Clippy (victory-gpu, victory --features diagnostics) with -D warnings and cargo xtask policy both pass.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6605177754 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2409). Originally posted by [wbkepler](https://github.com/wbkepler) on 2026-11-25T13:07:08Z. --- Stacked on #2280 (`gpu/copy-back-block-compare`, itself on #2242); only the last commit is new. It does not need #2175: the marked-page path here uses the views' bitmap in `shadow.rs`, not the page-table bitmap #2175 moved. ## What After a submission, every 64 KiB page shaders marked as written through views (`copy_back_marks` → `Shadow::copy_back_marked`) is read from title memory, compared with the copy, and taken again. In the world that is about 2,150 pages per frame, and about a third of them hold no byte that changed. Four changes, all in `crates/victory-gpu/src/shadow.rs`: - **No copy of bytes that are already equal.** With no bytes saved by the guard, `differing` runs over every byte where the copy and title memory differ, so outside the runs the copy already holds title memory's bytes. `take_others` (a 64 KiB write per page) now runs only when saved bytes exist. - **No second take of an untouched page.** If neither our copy-back nor the title wrote the page (a title write marks it changed), it is still taken and guarded and the copy matches title memory, so it is no longer taken and read back in full. - **One reused read buffer** (thread-local, one page) instead of a new zeroed `Vec` per page. - **The bitmap scan skips zero words** instead of writing back every word. Behaviour stays the same. The saved-bytes path, pages the title wrote, and pages the copy only partly holds all go through the same code as before. ## Measured (Windows 11, NVIDIA RTX 5090) Same 300 s world run before and after, on an integration build with the GPU-upload copies switched on. Figures are medians of the 5 s frame lines from 150 s on (n=26 each): | | before | after | |---|---|---| | copying back | 142.3 ms | 116.2 ms | | marked pages (copystat) | ~2,140 in ~133 ms | ~2,145 in ~109 ms | | on the GPU | 83.7 ms | 85.4 ms | | frame | 466.7 ms (2.1 fps) | 455.6 ms (2.2 fps) | No device removal, no access violations, and captured world frames look the same as before. ## Not done: finer write marks I also tried marking 4 KiB blocks instead of 64 KiB pages (shader shift 12, a 2^24-bit bitmap, and a copy-back that reads and compares only the marked blocks of each page). Shaders mark about 28,400 of the ~34,400 blocks in the marked pages (83%). About 16,100 of those blocks actually change, and a third of the marked pages are written with identical bytes. So the compare barely shrinks. GPU time went up about 7 ms (91.4 vs 83.7) from the extra atomics, and copy-back came out at 124.7 ms, worse than this PR's 116 ms. I left it out. Tests: `cargo test -p victory-gpu` passes, plus the new `a_marked_page_the_shaders_left_alone_stays_taken`. Two tests also fail on the base on this host, unrelated to this change: `dynamic_slots_are_held_until_released` and `lists_share_a_table_and_copies_keep_their_views`. The view heap has 1M descriptors on NVIDIA, below `HEAP_VIEWS`. Clippy (`victory-gpu`, `victory --features diagnostics`) with `-D warnings` and `cargo xtask policy` both pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
catminou4 commented 2026-11-25 14:06:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T13:19:27Z.

Review: COMMENTED


Marked-page copy-backs skip work that changes nothing — but note this re-adds the same BLOCK/LINE differing as #2280 (stacked on #2242). 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. If this supersedes the #2242/#2280 stack, say so; otherwise they conflict in shadow.rs.

<!-- greyfinch-sync:Marvisak/VIctory:review:6058345828 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2409#pullrequestreview-6058345828). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T13:19:27Z. **Review:** COMMENTED --- Marked-page copy-backs skip work that changes nothing — but note this re-adds the same BLOCK/LINE `differing` as #2280 (stacked on #2242). 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. If this supersedes the #2242/#2280 stack, say so; otherwise they conflict in shadow.rs.
DenRakEiw commented 2026-11-25 15:03:29 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T14:17:38Z.

Review: APPROVED


Verified against origin/main (940d32f) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP:

  • Skipping no-op marked-page copy-backs cleanly preserves unchanged pages and passes all shadow tests on RTX 5090 and WARP.
  • cargo test -p victory-gpu shadow::: 7/7 passed on RTX 5090, 7/7 passed on WARP (including differing_matches_a_byte_wise_comparison, a_copy_back_puts_back_only_the_bytes_the_gpu_changed, and a_marked_page_the_shaders_left_alone_stays_taken).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6059296228 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2409#pullrequestreview-6059296228). Originally posted by [DenRakEiw](https://github.com/DenRakEiw) on 2026-11-25T14:17:38Z. **Review:** APPROVED --- Verified against `origin/main` (`940d32f`) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP: - Skipping no-op marked-page copy-backs cleanly preserves unchanged pages and passes all shadow tests on RTX 5090 and WARP. - `cargo test -p victory-gpu shadow::`: 7/7 passed on RTX 5090, 7/7 passed on WARP (including `differing_matches_a_byte_wise_comparison`, `a_copy_back_puts_back_only_the_bytes_the_gpu_changed`, and `a_marked_page_the_shaders_left_alone_stays_taken`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: 0 warnings. - `cargo xtask policy`: 183 files ok. AI-assisted (Gemini 4 Neon).
Marvisak commented 2026-11-25 16:09:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15: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 (needs-work) was for an older state of main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7166981555 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2409#issuecomment-7166981555). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:31:41Z. --- <!-- victory-triage v1 head=b6e75cf03d83cba7bc4edf7d2849c69d0241ab94 verdict=needs-rebase at=2026-11-25T16:04: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 (needs-work) was for an older state of `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
kotekompotek commented 2026-11-25 17:05:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T16:55:34Z.


Reviewed the rewrite against the invariants the marked-page path needs — it holds up:

  • arm(from,to) before the read + unarm before the write is the right ordering: a title write landing during the armed read gets its old bytes saved, so differing(host,title, |i| host[i] != before[i]) correctly identifies "bytes the GPU wrote" rather than "bytes that differ now" — the write-then-read race that whole-page put-back got wrong.
  • write_runs (span merge at 256 B) + take_others/take_and_read preserve the post-write copy state — including the re-read that picks up title writes racing the put-back, which is what #2437 does for copy_back_marks.
  • Skipping no-diff pages avoids both the write_mapped and the re-take — consistent with the measured ~1/3 unchanged rate and the reported ~24 ms/frame win at ~2,150 pages.

Two triage notes:

  1. writing(at, merged.len()) suppresses the watch across the whole range while only spans are written — a racing title write in a gap would be read as ours. Harmless for correctness (the re-read picks it up) but it can skip a changed mark; same exposure as before, now slightly narrower.
  2. This restructures the same functions #2434's prepare/commit split and #2474's partial-refresh merge touch — landing order will need a rebase pass in one direction or the other. The saved-vs-gpu distinction is the part worth keeping from this set.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7168738531 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2409#issuecomment-7168738531). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T16:55:34Z. --- Reviewed the rewrite against the invariants the marked-page path needs — it holds up: - `arm(from,to)` before the read + `unarm` before the write is the right ordering: a title write landing during the armed read gets its old bytes *saved*, so `differing(host,title, |i| host[i] != before[i])` correctly identifies "bytes the GPU wrote" rather than "bytes that differ now" — the write-then-read race that whole-page put-back got wrong. - `write_runs` (span merge at 256 B) + `take_others`/`take_and_read` preserve the post-write copy state — including the re-read that picks up title writes racing the put-back, which is what #2437 does for `copy_back_marks`. - Skipping no-diff pages avoids both the `write_mapped` and the re-take — consistent with the measured ~1/3 unchanged rate and the reported ~24 ms/frame win at ~2,150 pages. Two triage notes: 1. `writing(at, merged.len())` suppresses the watch across the *whole* range while only spans are written — a racing title write in a gap would be read as ours. Harmless for correctness (the re-read picks it up) but it can skip a `changed` mark; same exposure as before, now slightly narrower. 2. This restructures the same functions #2434's prepare/commit split and #2474's partial-refresh merge touch — landing order will need a rebase pass in one direction or the other. The `saved`-vs-`gpu` distinction is the part worth keeping from this set.
Machad3x commented 2026-11-25 19:06:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Machad3x on 2026-11-25T18:26:42Z.


Setup: RTX 3080 Ti (driver 621.24), Windows 11, a local stack of open PRs merged with main 67e8ae1.

b6e75cf has one conflict on a tree with two written-mark bitmaps (the title
memory one apart): there only the skip of empty mark words is needed in the
scan. victory-gpu tests pass and an 8-minute game run had no device removal.

Written with Claude Code; tested locally.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7170915734 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2409#issuecomment-7170915734). Originally posted by [Machad3x](https://github.com/Machad3x) on 2026-11-25T18:26:42Z. --- Setup: RTX 3080 Ti (driver 621.24), Windows 11, a local stack of open PRs merged with main 67e8ae1. b6e75cf has one conflict on a tree with two written-mark bitmaps (the title memory one apart): there only the skip of empty mark words is needed in the scan. `victory-gpu` tests pass and an 8-minute game run had no device removal. Written with Claude Code; tested locally.
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/2409:upstream-pr/2409'
git switch 'upstream-pr/2409'

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