Skip to content

GPU: copy into title memory without asking for its region first - #322

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
jsthope:gpu/title-copies-without-region-queries
Nov 25, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
jsthope:gpu/title-copies-without-region-queries

Conversation

@jsthope

@jsthope jsthope commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What

Two copies into title memory ask the host about the destination before every copy:

  • copy_mapped with a title destination (a list's buffer copies) first checks that the whole destination is committed, with VirtualQuery (texture::committed).
  • write_mapped (the copy-back of what shaders wrote) uses WriteProcessMemory, which asks for the destination's region before it writes, for every 64 KiB piece.

copy_mapped's comment already says what such a query costs when it was asked about the source ("about 2 ms per copy in the title's big pools"): it walks the page tables to the end of the region with the same state, whatever the size of the copy. In the world the destinations were private pools of about 600 MiB.

Change

Both use the kernel copy the other copies already go through (ReadProcessMemory on our own process). It fails on a destination page that is not mapped writable as it does on a source page it cannot read, without faulting.

  • copy_mapped: the copy runs first. A title destination is asked for its region only once its copy has failed, which keeps the page retries (they write their zeros themselves) off unmapped memory.
  • write_mapped: the same pieces as before, written by the kernel copy.

One file, crates/victory-gpu/src/host.rs.

Behaviour

  • write_mapped: unchanged. Mapped pages are written, unmapped and read-only pages are not. Compared with WriteProcessMemory on reserved, partly committed and read-only memory: same result, same partial copy.
  • copy_mapped: one difference. A title destination that was not all mapped used to be left alone. Now the bytes before its first unmapped page may arrive, because the kernel copy stops at that page; nothing is written from that page on. In the run below none of the 3,274 title copies had such a destination, and none failed.

Numbers

World (first mission's intro); i7-13650HX, RTX 4060 Laptop, Windows 11, diagnostics build. Temporary counters, not in this PR, timed the old and the new call on the same copies of one run, 41 frames of about 1.15 s each:

per frame before after
title copies 80 copies, 0.45 MB 59.5 ms (the check alone) 0.56 ms (the whole copy)
copy-back 110 pieces, 4.9 MB 38.6 ms 0.55 ms

That is 98 ms of a frame (8.6 %) down to about 1 ms.

Stacks of the submitting thread sampled at 10 Hz (VICTORY_STACKS): VirtualQuery under these two calls was 47 of 462 samples before and 0 of 189 after. The VirtualQuery left in that profile is the one in watch::guard, which #119 addresses. This merges cleanly with #119, #202 and #124.

Synthetic, same machine: writing a 64 KiB piece into a committed view of 64 MiB / 512 MiB / 2 GiB takes 118 µs / 1.1 ms / 8.0 ms with WriteProcessMemory, and 6 µs with the kernel copy whatever the view.

Tested

  • cargo test -p victory-gpu: 45 passed, 1 ignored, also with --features diagnostics. Two new tests run both copies against reserved memory with a committed block on each side of a hole (no GPU, no game data).
  • cargo clippy -p victory-gpu --all-targets: no new warning. cargo xtask policy: ok.
  • Two runs through the menus and loading into the world with the change, on Windows 11 and NVIDIA. On this machine every world run ends with the host GPU hanging after 20 to 80 s, on main (five runs out of five) as with this change.
  • Not tested under Wine.

No game assets or derived data are included.

AI-Assisted

Two copies into title memory asked the host about the destination before
every copy. copy_mapped's own comment tells what that costs: asking
VirtualQuery about the source took about 2 ms per copy in the title's big
pools, because the query walks the page tables to the end of the region
with the same state, whatever the size of the copy.

- A list's copy into title memory checked that the whole destination was
  committed first, with VirtualQuery. The destinations were private pools
  of about 600 MiB.
- The copy-back of what shaders wrote used WriteProcessMemory, which asks
  for the destination's region before it writes: the same walk for every
  64 KiB piece.

Both now rely on the kernel copy the other copies already use
(ReadProcessMemory on our own process). It fails on a destination page
that is not mapped writable as it does on a source page it cannot read,
without faulting. A title destination is asked for its region only after
its copy has failed, which keeps the page retries (they write their zeros
themselves) off unmapped memory. write_mapped does what it did: mapped
pages are written, unmapped and read-only ones are not.

One difference: a title destination that was not all mapped used to be
left alone, and the bytes before its first unmapped page may now arrive
(the kernel copy stops at that page). No title copy of the run below had
such a destination, and none failed.

Measured in the world (first mission's intro; i7-13650HX, RTX 4060
Laptop, diagnostics build), with the old and the new call timed on the
same copies of one run, 41 frames of about 1.15 s each:

  title copies, 80 per frame (0.45 MB): the check alone 59.5 ms per
  frame; the whole copy now 0.56 ms
  copy-back, 110 pieces per frame (4.9 MB): 38.6 ms per frame; now
  0.55 ms

That is 98 ms of a frame (8.6 %) down to about 1 ms. In stacks of the
submitting thread sampled at 10 Hz, VirtualQuery under these two calls
was 47 of 462 samples before and 0 of 189 after.

Two tests run both copies against reserved memory with a committed block
on each side of a hole (no GPU, no game data).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@ifBars

ifBars commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

On Windows 10 I compared this with Causeway's previous destination-region query on the same buffers. Median time for a 64 KiB write-back into a touched, committed 64 MiB mapping fell from 205 to 3.86 µs. On a 512 MiB mapping it fell from 4,890 to 3.75 µs. A sparse write spanning a reserved 64 KiB hole went the other way, from 27.6 to 81.9 µs.

Our adaptation also exercises a read-only destination with an unaligned source. The fallback stops the title copy at the inaccessible page and preserves the unmapped-hole behavior on write-back. These are synthetic CPU measurements. We haven't measured story frame time on this change.

muhammed-aksoy pushed a commit to muhammed-aksoy/VIctory that referenced this pull request Nov 24, 2026
@tcurtsinger

tcurtsinger commented Nov 24, 2026 •

Copy link
Copy Markdown

Tested f7bfa64 against d2a4bae on Windows 11 build 26300, Ryzen 9 9950X3D. Each value is microseconds per 64 KiB piece copied by the production function into a fully committed, pre-touched pool.

Destination pool Write-back, main → PR Title-to-title copy, main → PR
64 MiB mapped view 119.55 → 2.61 119.66 → 2.61
512 MiB mapped view 1,044.09 → 2.59 1,022.95 → 2.60
512 MiB private allocation 745.52 → 2.65 746.73 → 2.67

Four fresh processes per revision ran in interleaved order. In each process a batch used 32 disjoint destinations, with three warm-up batches and nine timed batches. The table shows medians of the four process medians. We changed the input pattern each batch, verified every copied byte and checked a sentinel on either side. The two title-copy tests in this PR also passed on this machine.

We have since run the user-controlled intro on the RTX 4080 with this PR. The local build also included #87's reporting fix and the free-slot diagnostics on #251. The menu held 30 FPS. The single detailed report above 1,000 draws per frame registered 0.8 FPS at 3,795 draws, including 111.7 ms in the whole copy-back phase for 10.7 MB. The window later reported 433.9 ms per frame (about 2.3 FPS) at 1,925 draws. The game exited after a GPU hang 64.3 seconds after launch. Peak process memory was 22.85 GiB private and 12.84 GiB resident.

The previous build logged four busy samples at 1,205–2,248 draws with 48.5–68.5 ms of copy-back time. The workloads differed, and the phase timer includes both comparing snapshots and writing changed bytes. This single run did not establish a change in intro FPS attributable to this PR. Previous native run.

AI-assisted testing (Codex).

@jsthope
jsthope force-pushed the gpu/title-copies-without-region-queries branch from f7bfa64 to b58d198 Compare November 24, 2026 02:57
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…ion first

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

Copy link
Copy Markdown

Small documentation suggestion for copy_mapped: could its doc comment explicitly mention that a failed copy may already have modified the destination prefix? The test with the unmapped middle block checks that the third block stays untouched and leaves the first block unspecified. read() already mentions possible partial copies, so this would make the same contract clear at the caller-facing helper. Reviewed the diff with OpenAI Codex (GPT-6 family). No Windows test run.

@elamriz

elamriz commented Nov 24, 2026 •

Copy link
Copy Markdown

Windows RTX 4070 Ti A/B for b58d198. I applied #322 to the same local 6aa72a6 + #454 integration used for the baseline. The title watchdog was enabled for both runs. Both used the hardware adapter (driver 621.14), D3D12 Debug and DRED, a fresh cache, the same scripted input, and the same offline AppContainer setup. Both reached the 300-second harness limit without a device removal or unhandled exception.

Medians of the logged samples below 2 FPS, one run per build:

Build Samples Frame Copy-back GPU
Baseline 27 9,166.7 ms 7,769.6 ms 729.0 ms
+ #322 43 1,566.7 ms 663.6 ms 394.3 ms

This run agrees in direction with #400, with a larger effect on this setup. The reported workload varies, and I have not verified visual playability or run-to-run variance. AI-assisted testing and reporting. No raw logs or title files attached.

Cold-cache repeat with a few local frame captures: 40 logged samples below 2 FPS, median 1,366.4 ms per frame, 340.3 ms copying back, and 361.8 ms on the GPU. The run reached the same 300-second limit without device removal or an unhandled exception. The captures show loading art fading to a black frame. Later black-frame warnings continue while draws are submitted. 3D gameplay remains unverified.

@mercuryVM mercuryVM 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.

Tested b58d198 on Windows 11, RTX 5070, MSVC: cargo test -p victory-gpu -- --test-threads=1 45 passed, including the new title_copies tests (a holed destination, unaligned ranges, a read-only block). Clippy's 1 warning is the existing one in host.rs.

  • Behaviour change, checked: WriteProcessMemory temporarily makes read-only pages writable, but a kernel copy through ReadProcessMemory into our own buffer respects protection and fails on them. So copy-back no longer writes guarded (read-only) title blocks by itself. The only caller, shadow.rs copy-back, runs watch::writing(start + at, n), which opens the guard, right before write_mapped, so guarded blocks are still written. Pages the title itself keeps read-only are now left alone instead of overwritten, which is more correct.
  • copy_mapped asks texture::committed only after a failed copy, so the common all-mapped case skips the VirtualQuery. The retry path still stops at the first unmapped title page instead of faulting.

AI-assisted (Claude Code).

fornorharry pushed a commit to NitricAcidDev/VIctory-Omega-Fork that referenced this pull request Nov 24, 2026
@Daniel333313131

Copy link
Copy Markdown

The AMD side of the #400 measurement for this PR. As on the RTX 5070 Ti, copying back clearly drops. Whole frames are not clearly shorter.

Setup: AMD Radeon RX 7900 XTX 24 GB, driver 32.0.32029.2010; Ryzen 7 9800X3D, 32 GB; Windows 11 10.0.26300. Diag build of this PR's head b58d198 merged alone onto main ffe310e. Each run used its own empty VICTORY_CACHE_DIR, VICTORY_D3D12_DRED=1, and VICTORY_KEYS set to Enter every 3 s from 8 s to 47 s. Each run was stopped after 180 s. The method and the main numbers are in #400 (comment).

run frame copies on the GPU copying back device removed
main 1 972 410 ms 178 155 143 s
main 2 873 391 ms 179 135 170 s
main 3 679 267 ms 157 109 76 s
#322 1 674 207 ms 204 52 75 s
#322 2 801 307 ms 140 65 103 s
#322 3 1065 378 ms 213 70 75 s

AI use: Claude Code (Claude Opus 5.5) merged and built the commits, ran the title, summarised the log lines and wrote this comment. The numbers come from the runs above.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
@Daniel333313131

Copy link
Copy Markdown

Follow-up to my comment above: the same test with #431 merged too. #431 keeps the title in the world for longer on this GPU. With both PRs, whole frames are now shorter than with #431 alone.

Setup: as above (RX 7900 XTX, driver 32.0.32029.2010, Ryzen 7 9800X3D, diag build). main ffe310e with #431 23ced70 and this PR b58d198 merged. Each run used its own empty VICTORY_CACHE_DIR, VICTORY_D3D12_DRED=1, and VICTORY_KEYS set to Enter every 3 s from 8 s to 47 s. Each run was stopped after 180 s.

run world lines frame copies on the GPU copying back device removed
main 1 / 2 / 3 16 / 20 / 4 972 / 873 / 679 410 / 391 / 267 178 / 179 / 157 155 / 135 / 109 143 / 170 / 76 s
#431 1 / 2 / 3 21 / 22 / 23 1037 / 936 / 929 441 / 403 / 411 217 / 185 / 170 124 / 126 / 134 169 s / none / none
#431 + #322 1 / 2 / 3 22 / 22 / 23 807 / 814 / 747 367 / 390 / 369 211 / 177 / 143 68 / 67 / 67 none / none / none

AI use: Claude Code (Claude Opus 5.5) merged and built the commits, ran the title, summarised the log lines and wrote this comment. The numbers come from the runs above.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
KainanP added a commit to KainanP/VIctory that referenced this pull request Nov 24, 2026
…y unchanged pages current) onto Marvisak#346 + Marvisak#334 + integ/next

Taken instead of Marvisak#693 (shadow buffers in DEFAULT VRAM with staging
readback): both rewrite how shaders' writes reach title memory, on
incompatible models (Marvisak#1024 stores from the CPU-mapped host copy; Marvisak#693 has
no CPU mapping and merges from readback snapshots), so only one can land.
Marvisak#693 has unfixed in-title regressions on its last head's predecessor and no
title run since: about 2x slower frames and 5-7x GPU time on an RX 6700 XT
(Marvisak#693 (comment)), GPU
page faults inside freed `shadow readback/upload staging` on RX 6950 XT and
RX 6700 XT (Marvisak#693 (comment)),
and a changes-requested review (mercuryVM). Its last commit (1c94839) fixes
the review's failure paths but not the performance loss, and was not run in
the title. Marvisak#1024 has no negative report; its tests pass on RTX 4090/Wine,
RTX 5070 Ti, RX 7700 XT, RTX 5090/Wine, and a title run with it (and Marvisak#472,
Marvisak#322) had shorter frames and less copy-back time (AgneyaK).

Conflicts, crates/victory-gpu/src/shadow.rs:
1. `Shadow::copy_back`: the stack (Marvisak#124, TG0000) skips pages already found
   unchanged against this submission's snapshot (`UnchangedPages`) and
   allocates the title buffer lazily, by PAGE; Marvisak#1024 writes back only the
   runs of bytes the GPU changed (`write_runs`) and marks a synced page
   current only if title memory still equals the copy after the final
   write-watch query. Independent: both kept. The loop keeps the stack's
   `unchanged.differs` guard and lazy buffer, with Marvisak#1024's run collection
   and `write_runs` inside; the final check takes the (by then allocated)
   lazy buffer for its page re-reads.
2. Tests: the stack's `mod tests` and Marvisak#1024's `mod copy_back_tests` at the
   same place. The shared text holds the closing `);`, `}` and `}`, so the
   stack's module is closed in the hunk and Marvisak#1024's module follows as its
   own `#[cfg(test)]` item (not nested).

No other non-textual fix needed (cargo check --all-targets --all-features).
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
@Marvisak
Marvisak merged commit a9d6f17 into Marvisak:main Nov 25, 2026
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.

10 participants