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

Closed
github-bot wants to merge 1 commit from gh-pr/436/head into gh-pr/436/base
Owner

@⁠desplmfao on GitHub · 2026-11-24 03:21 UTC · original
desplmfao:fix/shadow-edge-pages → main · +109 −0 · 1 files · 1 commits · closed without merging

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)
  • Copies per frame drop by more than half; frame time does not change. What remains is the per-range work: about 940,000 ranges per frame, each with its own write-watch query (3.9–4.9 s of a frame), which is a follow-up.
  • Unwatched copies are higher with the change in both runs that had it on (1.6–4 GB and 4.7–5.1 GB). Not explained yet; I am finding which memory fails the write-watch query and why it happens more often here.
  • The run with the change on ended in a GPU hang, the one with it off did not. One run each; this machine hangs in the world without this change too (two of three runs earlier the same day, #71), so this says nothing either way yet.
  • On main alone this is not measured. #400 reports 25–29 GB requested per world frame there, mostly unchanged; how much of that this saves is open.
  • #185: its refresh of directly indexed SRVs is what asks for the many small ranges this change is about.
  • #440 / #435: record each GPU write range once per submission. This change checks a buffer's pending write ranges on every widened range, so a shorter pending list makes that check cheaper.
  • #464: unmapped guarded view blocks were re-copied every submission; the same kind of waste for view memory as the edge pages here are for watched memory.
  • #497: keeps the snapshot of what shaders may write per page (Shadow::writing, copy_back); same file, different functions.
  • #400: measurements of the copy path on main (Windows, RTX 5070 Ti), including how much of each world frame goes to refreshes.

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

**@⁠desplmfao** on GitHub · 2026-11-24 03:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/436) `desplmfao:fix/shadow-edge-pages` → `main` · +109 −0 · 1 files · 1 commits · closed without merging **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) | - Copies per frame drop by more than half; frame time does not change. What remains is the per-range work: about 940,000 ranges per frame, each with its own write-watch query (3.9–4.9 s of a frame), which is a follow-up. - Unwatched copies are higher with the change in both runs that had it on (1.6–4 GB and 4.7–5.1 GB). Not explained yet; I am finding which memory fails the write-watch query and why it happens more often here. - The run with the change on ended in a GPU hang, the one with it off did not. One run each; this machine hangs in the world without this change too (two of three runs earlier the same day, #71), so this says nothing either way yet. - On `main` alone this is not measured. #400 reports 25–29 GB requested per world frame there, mostly unchanged; how much of that this saves is open. ## Related - #185: its refresh of directly indexed SRVs is what asks for the many small ranges this change is about. - #440 / #435: record each GPU write range once per submission. This change checks a buffer's pending write ranges on every widened range, so a shorter pending list makes that check cheaper. - #464: unmapped guarded view blocks were re-copied every submission; the same kind of waste for view memory as the edge pages here are for watched memory. - #497: keeps the snapshot of what shaders may write per page (`Shadow::writing`, `copy_back`); same file, different functions. - #400: measurements of the copy path on `main` (Windows, RTX 5070 Ti), including how much of each world frame goes to refreshes. ## 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](https://claude.com/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 #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 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:03:54 +00:00
github-bot closed this pull request 2026-11-26 04:03:54 +00:00
Author
Owner

@⁠desplmfao on GitHub · 2026-11-24 05:47 UTC · original

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).

**@⁠desplmfao** on GitHub · 2026-11-24 05:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/436#issuecomment-7123280962) 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).
Author
Owner

@⁠alexdeliu on GitHub · 2026-11-24 17:23 UTC · original

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

**@⁠alexdeliu** on GitHub · 2026-11-24 17:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/436#issuecomment-7139132831) 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. <sub>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</sub>

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!436
No description provided.