Skip to content

GPU: copy-backs compare a block at a time (stacked on #2242) - #2280

Open
wbkepler wants to merge 2 commits into
Marvisak:mainfrom
wbkepler:gpu/copy-back-block-compare
Open

wbkepler wants to merge 2 commits into
Marvisak:mainfrom
wbkepler:gpu/copy-back-block-compare

Conversation

@wbkepler

Copy link
Copy Markdown

Stacked on #2242 (based on gpu/copy-back-gpu-bytes-only; the first commit here is #2242's). Only the last commit is new: differing in shadow.rs, the function #2242 added.

Why

In a world frame, the CPU time on the submitting thread goes to preparing (~210 ms) and copying back (~120–150 ms), and the GPU waits for both. I added throwaway timers (diagnostics build, one live run) to break down the copy-back. Almost all of it is the copy-back of pages that shaders mark as written through views, about 2,250 pages per frame:

copy-back of marked pages, ms/frame before
read title page (alloc + kernel read) 12.4
find changed bytes (differing) + take title's others 70
write runs (open guard, kernel writes, ~15k spans) 28
re-guard (taken) / re-read 8.6 / 4.1
total 131

differing compared 8 bytes per step through a slice comparison, which is a call each time: 8,192 calls per 64 KiB page, about 30 µs per page.

The change

Equal 1 KiB blocks are skipped whole. Inside a block that differs, equal 32-byte lines are skipped, and only lines that differ are walked byte by byte. The runs it returns are the same as before. A new test checks it against a byte-by-byte oracle, with runs across line and block edges, a last byte, partial blocks, and a gpu(i) filter.

Measured (Windows 11, NVIDIA RTX 5090, world, steady state, diagnostics build with the timers)

ms/frame before after
differing + take others 67–79 (avg ~71) 56–60 (avg ~58)
copy-back of marked pages 127–152 (avg ~134) 115–122 (avg ~117)
fps 1.2–1.3 1.3–1.4

On its own, differing now costs next to nothing. I measured that offline on a real host copy mapping with 2,048 pages and about 12 changed words per page. What is left in that step is memory traffic: reading the 64 KiB title page and the 64 KiB host copy, each about 8 µs per page on this host and slower in the world. Going further would need finer marks than 64 KiB pages, and that is out of scope here.

Tested

  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo clippy -p victory --features diagnostics --all-targets -- -D warnings: clean.
  • cargo xtask policy: clean.
  • cargo test -p victory-gpu: 143 passed and 2 failed. The same 2 fail on the unmodified base on this host: dynamic_slots_are_held_until_released and lists_share_a_table_and_copies_keep_their_views. Both hit the 1M view-heap cap on NVIDIA.
  • One live world run with the change on top of an integration build, as above. No device removal.

🤖 Generated with Claude Code

wbkepler and others added 2 commits November 25, 2026 09:35
After a submission, a page shaders wrote goes back into title memory.
For marked pages (views) and pages written at computed addresses that
was the whole page: read title memory, compare, then write the copy's
64 KiB over it. The title's threads write the same pages meanwhile (its
GPU heap holds CPU records next to what shaders write), and a write that
landed after the read was undone by the copy's older bytes. The page was
then taken with those bytes, so the copy kept them for good, and a later
copy-back of the page put them back over the title's again.

Now only the runs of bytes the shaders changed are written (runs less
than 256 bytes apart as one span, with the title's bytes between them
as read: a kernel copy per run cost over 1 ms per page). The page is
armed while it is read, so a title write that lands during the read
saves the bytes from before it and the shaders' bytes are told apart
from the title's. After the copy-back the page is taken first and the
copy reads it again, so a title write before the guard is back is in
the copy and one after it faults. The snapshot path writes only the
bytes the GPU changed as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Finding the bytes the GPU changed in a copy-back compared 8 bytes per
step, a call each: ~30 us per 64 KiB page, the largest part of the
copy-back of the pages shaders mark (~2,250 per world frame). Equal
blocks of 1 KiB are skipped whole, then 32-byte lines inside a block that
differs, bytes only in a line that differs; the runs found are the same.

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

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy-backs compare a block at a time (1 KiB blocks, 32-byte lines inside differing blocks) — 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. Perf follow-up to #2242: ~30 µs/page → the 8-byte scan cost ~70 ms/world frame; the two-level skip looks right. Review together with #2242.

@DesertGamer

Copy link
Copy Markdown

VIctory V2: community PRs, merged and tested

Open PRs sitting in the queue? V2 pulls them into one build, screens each one (binaries, build scripts, hooks, new deps) and runs tests after every batch.

Where it stands today: launches, runs the menu at 30 fps, plays the intro video, reaches the start screen and loads the world (~50 s). Entering the world is the current blocker: rendering fails with a GPU "device removed" error (tracked in issue #1).

⚠️ Entering the world on a real GPU can hang it and make Windows reset the driver. Try it at your own risk.

Not playable yet, but every batch of merged fixes moves it closer. If you have a PR that's stuck, or a GPU to test on, I'd love your help:
https://DesertGamer/VIctory-V2

Unofficial community fork of Marvisak/VIctory, GPL-3.0.

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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

Clean block-at-a-time copy-back comparison in shadow.rs. Looks good.

AI-assisted (Gemini 4 Neon).

@Calvix2k

Copy link
Copy Markdown
Contributor

Verified the stack on Windows 10 19045 with an RX 7800 XT (RDNA3), head 7ef96fa (both commits, so the block comparison runs on top of #2242's byte-only copy-back):

No number for the copy-back time itself: I did not run the game with your timers, so the ms/frame table stays yours. If you want a second machine's breakdown, say which timers to keep and I will run one world frame on this GPU.

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.

5 participants