Skip to content

GPU: copy a shadow's edge pages whole so they are not copied again - #436

Closed
desplmfao wants to merge 1 commit into
Marvisak:mainfrom
desplmfao:fix/shadow-edge-pages
Closed

desplmfao wants to merge 1 commit into
Marvisak:mainfrom
desplmfao:fix/shadow-edge-pages

Conversation

@desplmfao

@desplmfao desplmfao commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Status (2026-11-24): not needed if #185 lands as it is now. #185 was reworked after this PR was opened; its copy_stale keeps exact partial-page coverage, which does what this PR does more precisely, and the two conflict in copy_stale. Measured (world frames, median): old #185 6.1 s, old #185 + this and #462, #515, #600 4.3 s, new #185 alone 2.9 s. This PR only still helps main without #185. Details in the comments below; the rest of this description is as written before.

GPU hangs: not tied to this change. Xid 109 (CTX SWITCH TIMEOUT) ended runs both with and without it, 3–4 min after start, as it does on this machine without any of these changes (#71). Last pair, same build, #436 and #462 switched off and on together: off hung about 4 min after start; on had not hung when stopped at 4.5 min. Too few runs to say whether either makes it come sooner or later. With #493 (+#336) added, four runs with this change on went 7.5–10 min without a hang, so the hangs were most likely what #493 fixes (reads past the end of root SRVs); see the comment there.

Same pair, last frames in the world: off 5.0–6.2 s per frame (862,000–929,000 direct SRV ranges), on with #436 and #462 2.4–4.2 s (280,000–290,000 ranges).

Unwatched copies: most of them came from write-watch questions over ranges spanning several allocations, which the kernel refuses as a whole; that is #515. With #436, #462, #515 and #464/#514 together, unwatched copies fell to 1–81 MB per world frame, and the median world frame from 6.0 s (none of these) to 3.0 s; what is left is bound by CPU time per range rather than bytes.

Problem

Shadow::copy_stale copies a 64 KiB page that is only partly inside the requested range in part, and never marks it as copied, so every later range in the same page copies it again. With few, large ranges that costs little. With many small, unaligned ranges in the same pages it dominates: #185 refreshes directly indexed SRVs before each submission (close to a million ranges per world frame), and there it came to about 11 GB of copies per frame.

Change

copy_stale widens the range to its first and last pages' bounds (within the buffer) when no pending range (shaders of a submission may be writing there) overlaps what that adds. The page is then marked and copied again only after the title writes to it. Next to a pending range the old behaviour stays.

Measured

RTX 5080, Linux (proton-cachyos, vkd3d-proton 3.2.0), Series X 1.0.2.1. Diagnostics build of main d2a4bae + #185 + #259, #177, #151 (needed to reach the world here), with this change on and off in the same binary (a local switch), one 7 min run each, Enter into story mode at about 35 s, numbers from the first 2–3 min in the world:

per world frame off on
pages copied again because only partly in range 11,100–12,000 MB 0
watched copies 11,700–12,500 MB 940–1,450 MB
unwatched copies (untracked_filled_mb) 1,600–2,300 MB 4,700–5,100 MB
copies in total 13,500–14,300 MB 5,700–6,300 MB
frame time 7.6–11 s 7.8–11.6 s
GPU hang / crash none in 7 min Xid 109 (CTX SWITCH TIMEOUT) about 3 min into the world, then the title's watchdog crash (without #493; see the note above)

Related

Tests

Two tests in shadow.rs: small ranges in one page copy it once (and again after a title write), and edge pages stay cut next to pending writes (the copy's GPU-written bytes are kept). Both fail without the change (16 bytes copied instead of 65,536, and 16 instead of 32). victory-gpu tests under Wine: the same 4 unrelated tests fail with and without it (watch::tests::written_pages_are_reported_to_every_asker, resource::tests::buffer_data_reaches_the_host_texture, indirect::tests::argument_clamp_builds, pipeline::library::tests::a_rejected_blob_gives_an_empty_library).

AI-assisted (Claude Code).

🤖 Generated with Claude Code

A page only partly inside a requested range was copied in part and never
marked as copied, so every later range in the same page copied it again.
With many small, unaligned ranges in the same pages (directly indexed SRVs
in Marvisak#185, close to a million per world frame) this came to about 11 GB of
copies per frame.

Widen each range to its first and last pages' bounds (within the buffer)
when no pending range overlaps what that adds, so the page is marked and
copied again only after the title writes to it. Next to a pending range
the old behaviour stays.

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

Copy link
Copy Markdown
Contributor Author

Update: #185 has since been reworked (8 more commits, 04:04–04:57 UTC, after this PR was opened). Its copy_stale now keeps exact partial-page coverage (whole / partial), which solves the same repeated copy of partly covered pages more precisely than this PR's whole-page widening, and it conflicts with this PR in copy_stale.

Measured on the same machine and input (RTX 5080, Linux / proton-cachyos, world frames, median over the same window after world entry, with #493 in all three):

old #185 old #185 + #436, #462, #515, #600 new #185 alone
frame time 6.1 s 4.3 s 2.9 s
watched bytes copied 11.4 GB 1.2 GB 0.05 GB
bytes copied whole 3.7 GB 1.3 GB 0.07 GB

So if #185 lands as it is now, this PR is not needed and I'd close it. It only still helps main without #185 (any refresh that requests many small ranges in the same pages). Leaving that call to you.

AI-assisted (Claude Code).

@alexdeliu

Copy link
Copy Markdown
Contributor

Merged onto main @ ffe310eb (PR head e914ed45), the PR's own test fails, the same on a retry:

shadow::tests::small_ranges_in_one_page_copy_it_once
panicked at crates\victory-gpu\src\shadow.rs:579:13: assertion `left == right` failed
  left: 65536
 right: 0

This is not GPU-specific, so CI should show it too on the next push.

Checked with CI's steps (both clippy runs with -D warnings, cargo test --workspace) on Windows 11, AMD RX 9070. Every open PR: https://gist.github.com/alexdeliu/ef9ef0ebc1776ff104b5aa8eaea18e87

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.

3 participants