Repository navigation
GPU: copy into title memory without asking for its region first - #322
Conversation
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>
|
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. |
|
Tested
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). |
f7bfa64 to
b58d198
Compare
…ion first Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Small documentation suggestion for |
|
Windows RTX 4070 Ti A/B for Medians of the logged samples below 2 FPS, one run per build:
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
left a comment
There was a problem hiding this comment.
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:
WriteProcessMemorytemporarily makes read-only pages writable, but a kernel copy throughReadProcessMemoryinto 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.rscopy-back, runswatch::writing(start + at, n), which opens the guard, right beforewrite_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_mappedaskstexture::committedonly after a failed copy, so the common all-mapped case skips theVirtualQuery. The retry path still stops at the first unmapped title page instead of faulting.
AI-assisted (Claude Code).
…sking for its region first Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
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
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. |
…s region first
|
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).
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. |
…s region first
…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).
…s region first
What
Two copies into title memory ask the host about the destination before every copy:
copy_mappedwith a title destination (a list's buffer copies) first checks that the whole destination is committed, withVirtualQuery(texture::committed).write_mapped(the copy-back of what shaders wrote) usesWriteProcessMemory, 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 (
ReadProcessMemoryon 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 withWriteProcessMemoryon 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:
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):VirtualQueryunder these two calls was 47 of 462 samples before and 0 of 189 after. TheVirtualQueryleft in that profile is the one inwatch::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.main(five runs out of five) as with this change.No game assets or derived data are included.
AI-Assisted