Skip to content

GPU: use VEH-guarded in-process copy for copy_mapped and write_mapped (#118) - #154

Open
BitWuehler wants to merge 1 commit into
fxceless:mainfrom
BitWuehler:wine-direct-copy
Open

BitWuehler wants to merge 1 commit into
fxceless:mainfrom
BitWuehler:wine-direct-copy

Conversation

@BitWuehler

@BitWuehler BitWuehler commented Nov 24, 2026 •

Copy link
Copy Markdown

Summary

Rebased onto fxceless/victory-fork main (c36971b), extending crates/victory-gpu/src/plaincopy.rs for:

  1. PAGE_GUARD (STATUS_GUARD_PAGE_VIOLATION / 0x80000001) handling & re-arming (rearm_guard) on both reads and writes, preventing unhandled guard-page exceptions and restoring PAGE_GUARD via VirtualQuery + VirtualProtect(..., info.Protect | PAGE_GUARD) before failing back to the kernel path.
  2. Opt-in guarded plain-store writes (VICTORY_COPY_PLAIN_WRITES=1 or VICTORY_DIRECT_COPY=1, off by default) for host::copy_mapped (title_dst) and host::write_mapped via victory_write_guarded + shared watch::handle_write_fault(at).

Why this shape on top of c36971b

  • main (c36971b) already landed plaincopy.rs (VICTORY_COPY_PLAIN=1) for reads (copy_mapped non-title_dst & read_into) while leaving < 256-byte probes and writes on the kernel path. Keeping main's read design and LEAST = 256 leaves native Windows defaults and picture behavior untouched.
  • Keeping guarded writes opt-in (VICTORY_COPY_PLAIN_WRITES=1, off by default) avoids changing Windows write behavior (where @larelgit measured NtWriteVirtualMemory winning on freshly reset MEM_WRITE_WATCH pages) while still allowing Wine/Proton users to bypass NtWriteVirtualMemory wineserver IPC when desired.
  • Handling EXCEPTION_GUARD_PAGE (0x80000001) in plaincopy::on_fault and calling rearm_guard before exiting via victory_copy_guarded_fault fixes the crash mode identified by @iurysf, @advxrsary, and @larelgit whenever a copied source/target page carries PAGE_GUARD.

Changed files

  • crates/victory-gpu/src/watch.rs: extracted pub(crate) fn handle_write_fault(at: usize) -> bool from on_write so watch::on_write and plaincopy::on_fault share the same write-watch guard-opening path.
  • crates/victory-gpu/src/plaincopy.rs: added victory_write_guarded (rep movsb with Win64 SEH unwind directives), writes_on(), rearm_guard(at), WRITE == 1 write-fault handling in on_fault, write(to, from, len), write counters in line(frames), and unit tests guard_page_fails_copy_and_stays_guarded and guarded_write_skips_unmapped_and_readonly_and_marks_write_watch.
  • crates/victory-gpu/src/host.rs: routed copy_mapped (title_dst) and write_mapped (direct_writes() && crate::plaincopy::writes_on()) through crate::plaincopy::write.

Verification (82dbea5 on c36971b)

  • cargo xtask policy: 185 files ok
  • cargo clippy -p victory-gpu --all-targets --target x86_64-pc-windows-msvc: 0 warnings in modified files (plaincopy.rs, watch.rs, host.rs; 7/8 pre-existing upstream warnings in untouched modules).
  • Unit tests under GE-Proton11-14 (vkd3d-proton, VICTORY_COPY_PLAIN=1 VICTORY_COPY_PLAIN_WRITES=1):
    • plaincopy::tests::: 6 passed; 0 failed (including guard_page_fails_copy_and_stays_guarded and guarded_write_skips_unmapped_and_readonly_and_marks_write_watch)
    • watch::tests:: --skip drvwatch --test-threads=1: 11 passed; 0 failed (drvwatch::tests::the_system_takes_the_subscription skipped due to Wine EvtSubscribe stub)
    • host::tests:: --test-threads=1: 22 passed; 0 failed
    • shadow::: 30 passed; 0 failed
    • texture::: 2 passed; 0 failed

(Prepared and verified with AI assistance.)

@BitWuehler

Copy link
Copy Markdown
Author

In-world follow-up on CachyOS Linux (Ryzen 7 9800X3D, RTX 5080 620.73.04, GE-Proton11-14, release profile, Series X files, 463.9 s prior run vs 345.1 s new run):

Metric (world intervals, draws >= 100) Baseline 4e17478 (RELAY=2, no #154, n=50) With #154 + LATE_WAIT=3 + WIDE_REFRESH=1 + WIDE_MERGE=1 (no RELAY=2, n=38)
copying back median 185.50 ms (61.70 MB) 19.85 ms (67.30 MB)
copying back mean 284.74 ms (60.21 MB) 38.71 ms (63.75 MB)
At ~6,800 draws/frame 179.9 ms (53.2 MB, 6,813 draws) 12.7–19.1 ms (49.3–55.6 MB, 6,730–6,873 draws)
gpu median 121.55 ms 67.55 ms
tex_ms median 166.55 ms (34.80 MB) 596.55 ms (50.60 MB, up to 210.5 ms waiting for remaps & 141.9 ms watch/guards)
fps median (world peak) 1.30 fps (2.0 fps) 0.85 fps (2.0 fps; 1.6 fps at 6,873 draws)

Caveat on isolating #154: Beyond adding #154, the second run also removed VICTORY_RELAY=2 (see #6 / #89, which previously caused ~103 ms card-fetch relay waits per frame) and enabled VICTORY_WIDE_MERGE=1 + VICTORY_WIDE_REFRESH=1. So the ~9x drop in copying back combines dropping RELAY=2 and #154 + WIDE_MERGE=1. Stability was clean across 345.1 s (rc = 0, 0 device removals, 0 Xids), and PlanePass warnings from #127 also dropped from 2 to 0.

(AI-assisted analysis from session logs; numbers directly extracted from the two runs.)

@iurysf

iurysf commented Nov 25, 2026

Copy link
Copy Markdown

We merged this PR into our test branch and measured it on an RTX 4070, paired within one run (VICTORY_DIRECT_COPY turned off and on by turns every ~20 s in the steady world, 4 pairs): the frame moved by +3.7 ms (pairs +6.6, −3.4, +0.8, +16.8), so no gain we could see on Windows.

One of the runs also ended at boot (~52 s, before the world) with an unhandled 0x80000001 (STATUS_GUARD_PAGE_VIOLATION) inside victory_try_copy_op. Symbolized stack, innermost last:

victory_gpu::shadow::workers (wide refresh worker)
victory_gpu::host::Batch::flush (parallel refresh job)
victory_gpu::shadow::Shadow::update
victory_gpu::shadow::Shadow::update_free
victory_gpu::host::copy_mapped
victory_try_copy_op            <- 0x80000001

on_fault takes EXCEPTION_ACCESS_VIOLATION and EXCEPTION_IN_PAGE_ERROR only. A read through a page protected with PAGE_GUARD raises STATUS_GUARD_PAGE_VIOLATION instead, which goes to the unhandled filter. It also clears the guard bit on that page, which the title may rely on (stack growth or its own probes). ReadProcessMemory fails on such a page cleanly and leaves the guard in place.

A possible fix: in on_fault, treat STATUS_GUARD_PAGE_VIOLATION inside [victory_try_copy_op, victory_try_copy_end) like an access violation (so the caller falls back to the kernel copy), and put the guard back on the page with VirtualProtect(..., old | PAGE_GUARD) before resuming, since the fault has already consumed it. It happened once in five runs of that build, so it is rare.

AI-assisted.

@LeonardoS11

Copy link
Copy Markdown

On an RTX 3080 Ti this PR changes the picture: two alpha/decal textures in the prologue's first room never arrive. Details, in case it helps.

Setup — Windows 11, RTX 3080 Ti 12 GB, 64 GB RAM, diag build with diagnostics; main at c96b4f2 with only this PR's commit cherry-picked (no conflict), no environment switches; one checkpoint in the prologue (the room with the window onto the yard), the game window's contents saved at 245 / 275 / 295 s.

Result, 3 runs each

chain-link mesh in the window print on the back of the character's shirt room walls
plain c96b4f2 there, 3 of 3 there, 3 of 3 missing in 2 of 3 (the known "walls missing in some launches")
c96b4f2 + this PR missing, 3 of 3 missing, 3 of 3 (shirt plain dark) missing in 2 of 3

So the walls are main's own intermittent defect, but the mesh and the shirt print follow this PR. The character's hair also came out in coarse blocks in two of the three runs with the PR.

A guess at the cause, not verified: the handler is installed first (AddVectoredExceptionHandler(1, …)) and turns any access violation inside the copy into "failed". If part of the title's memory is only made readable by a handler of the runtime on first touch (physical-page views, reservations committed on demand), ours sees the fault before it and the page counts as unmapped, so a texture picked by index is passed over and not uploaded. The per-frame line did not show it, though: "passed over unmapped" was 5,216 with the PR and 9,431 without, so the count alone does not explain it.

VICTORY_DIRECT_COPY=0 on a build that also had other PRs did not give a clean picture, but that build carried #158, which breaks lighting on its own (I will report that there once confirmed), so that test does not separate the two. I can rerun this PR alone with VICTORY_DIRECT_COPY=0 if that is useful.

The speed side is real here too (this PR was in the stack whose frame time I reported on #175); I am only flagging the picture.

(Runs and screenshots by me on the hardware above; comparison and this note written with Claude Code.)

@larelgit

Copy link
Copy Markdown

Measurements of the same idea on Windows, with the read side alone, in case they help with the two open questions here (no gain on Windows, and the window mesh / shirt print that go missing).

Setup. RTX 5080 16 GB (driver 610.88), Ryzen 7 7800X3D, 32 GB, Windows 11 Pro 22631, diag build, rustc 1.98.1. Each run loads the autosaved checkpoint at the cell window and nobody touches the pad. Frame time is the spacing of the recording per frame lines over 260-355 s of the run (median of 15-18 intervals); the other columns are means of the frame and card lines over the same window. The scene is judged from screenshots at 300 and 350 s. One run at a time, alternating.

The variant differs from this PR in three ways (branch: https://github.com/larelgit/victory-fork/tree/perf/user-copies, two commits on 310c6eb):

  • It copies only out of title memory with plain loads (copy_mapped into host memory, read_into). Copies into title memory (write_mapped, Step::Copy) stay with the kernel.
  • After a fault the whole range is read again through ReadProcessMemory, so what an unmapped page does to the result is what it was before. The one-byte readability probe in texture.rs stays on the kernel, since it mostly looks at pages that are not there.
  • The handler also takes STATUS_GUARD_PAGE_VIOLATION and puts the page's guard back before returning, as @iurysf suggested above (unit-tested with a guard page in the middle of a copy).

Reads only, on 310c6eb with its defaults (no switches; VICTORY_USER_COPIES=0 is the kernel path, same binary):

Copies out of title memory Runs ms/frame Preparing copy_mapped bulk read On the GPU Submitting thread waiting for the card
plain loads 168, 165, 161, 161, 170 median 165 51.0-52.5 ms 6.3-6.6 ms 93.5-103.7 ms 93-102 ms
kernel (=0) 165, 167, 169, 166, 168 median 167 60.4-63.6 ms 11.1-11.5 ms 87.4-92.7 ms 87-92 ms

(A first run of the new binary, translating its shaders, is left out: 179 ms with plain loads.) The copies get about 9.5 ms a frame cheaper, and the submitting thread then waits for the card about as much longer: the card is about 80 % busy and sets the frame, so on this machine with these defaults the frame time does not move (2 ms, inside the spread). 8-10 copies a frame fault and go to the kernel, the same count as the kernel path's own fallbacks.

Picture. The chain-link mesh in the window and the print on the back of the shirt are there in all 11 runs, the 6 with plain loads included (the first one too). The cell walls were drawn in 2 of the 6 runs with plain loads and 1 of the 5 with the kernel (main's own intermittent loss). So reading with plain loads does not reproduce the loss @LeonardoS11 saw with this PR; if it follows this PR, the write side (write_mapped / Step::Copy through try_copy) or the readability probe would be the parts to look at.

Why the write side is left out of the variant. Title memory is write-watched, and each asking resets the watch, after which the first store to every page faults. The kernel takes those faults for less than user mode does: a 64 KiB piece into freshly reset write-watched pages took 11.4 µs through NtWriteVirtualMemory and 13.1 µs with a plain copy on this machine (without the reset, the plain copy is the faster one). In the game, plain stores into title memory as well ran at 188 ms a frame against 177-181 with plain loads only (a803c87 with VICTORY_LATE_WAIT=3 VICTORY_WIDE_BACK=1 and card buffers, no relay; there the submitting thread still set the frame and reads alone gave 177-180 against 193-196 with the kernel). Under Wine the balance is different (the 24x write figure above), so this is about Windows only.

(Runs, measurements and this note by Claude Code with Claude Opus 5.5 on the hardware above, at the account owner's request.)

@advxrsary

Copy link
Copy Markdown

I measured this pull request on native Windows. The processor and the card are the same as in your Proton test.

Setup

Item Value
Machine RTX 5080 16 GB (driver 621.24), Ryzen 7 9800X3D, 32 GB RAM, Windows 11
Trees main at 9376fd6, and the same tree with this branch merged. The merge had no conflicts.
Build diagnostics, Series X profile, the defaults of main and VICTORY_WAVE_WIDE=0
Scene the first room, camera at rest, 4,700 to 5,150 draws a frame
Runs 5 minutes each, with a warm shader cache and a warm pipeline library

Result

Each number is the median of the frame lines of one run in that scene.

For each frame main, 4 runs with this branch, 2 runs
preparing 71.8, 71.3, 71.3, 69.5 ms 63.5, 58.6 ms
copies (a part of preparing) 39.1, 39.5, 39.1, 38.4 ms 33.2, 31.3 ms
submitting thread, kernel time in submissions 41.8, 40.0, 39.1, 40.6 ms 21.4, 17.8 ms
submitting thread, user time in submissions 41.8, 38.5, 41.6, 40.0 ms 48.8, 49.4 ms
frame 224, 222, 226, 225 ms 232, 220 ms
frames a second 4.5, 4.5, 4.4, 4.5 4.3, 4.5

The kernel time of the submitting thread decreases by half, and preparing decreases by about 10 ms a frame. The log shows 0 errors and no device loss in the two runs.

The frame rate does not change on main. On this machine main waits for the card for 113 to 124 ms a frame, so the frame is not limited by the processor. I expect a gain in frame rate only where the card is not the limit.

Two notes

  • A local variant of the same idea gave the same result in three more runs: preparing 58.1, 53.2 and 56.3 ms.
  • I did not see this case in the runs, but it is possible. If a copied range has a page with PAGE_GUARD (for example the guard page of a thread stack), rep movsb raises STATUS_GUARD_PAGE_VIOLATION. on_fault does not handle this code, and the system removes the guard from the page. The local variant makes the copy fail in this case and sets the guard on the page again.

I made the measurements with an AI assistant (Claude Code), on my own machine. This comment has no game data.

@oeirughrepuiohg

Copy link
Copy Markdown

Measured this pull request on Linux/Proton, where the submitting thread (not the card) sets the frame, so the saving in preparing shows up as frame rate.

Setup: RTX 4070 Ti, Linux, Proton Experimental with vkd3d-proton, Series X files. Our tree is the fork at 2119b0e plus a few open pull requests (this one included). Relay stage 1, VICTORY_INDIRECT_WINDOW=1, VICTORY_BAKE_TEXTURES_MS=0. Same exe for every run, first gameplay room, 5 minutes per run after the world appears (the third default run: 2 minutes), warm caches.

VICTORY_DIRECT_COPY=0 (2 runs) default, this PR on (3 runs)
frames a second 6.68, 6.69 7.17, 7.25, 7.66
median frame 147, 147 ms 137, 137, 128 ms
preparing (frame line) 102.2, 103.7 ms 81.6, 88.6, 84.0 ms
copies (part of preparing) 60.7, 61.4 ms 51.1, 55.8, 51.5 ms

Preparing goes down by about 15-20 ms a frame. Here the card waits on the submitting thread, so the frame rate goes up by about 8-15%. The off and on runs were interleaved (off, on, off, on). The sampled screenshots look the same in both settings: intro art, cutscene faces, the first room. No device loss, no new errors.

This fits @advxrsary's native Windows numbers: preparing goes down the same way there, but the frame does not change because the card is the limit on that machine.


Measured and written with AI assistance (Claude Code, Claude Opus 5.5).

@larelgit

Copy link
Copy Markdown

The case @advxrsary described above does happen, on main at f85101b: this branch merged onto it without conflicts stopped the title in 4 of 4 runs, between 64 and 99 s (still in the loading phase, before the world), with an unhandled STATUS_GUARD_PAGE_VIOLATION (0x80000001). Three of the runs had the README's new switches on (VICTORY_ASYNC_SUBMIT=1 VICTORY_QUEUE2=1 VICTORY_INDIRECT_WINDOW=1 VICTORY_TEXTURE_NO_WAIT=1), one had the defaults, so it is not the switches.

The faulting stack, symbolized with the build's PDB, is the same in all four (the last frames differ only between a dispatch and a mesh dispatch):

victory_try_copy_op
victory_gpu::host::read_into
victory_gpu::texture::readable
victory_gpu::texture::mapped_bytes
victory_gpu::draw::State::heap_textures
victory_gpu::draw::State::bind
victory_gpu::draw::State::dispatch  (or dispatch_mesh)

So a texture's bytes in title memory include a page the title asked for with PAGE_GUARD; xmem passes that flag through to the host (HOST_PROTECTION is 0x1ff), the in-process rep movsb trips it, and on_fault does not take that code. The kernel read main uses does not stop the process there. main at f85101b without this branch ran 14 runs of 300 to 370 s on the same machine and checkpoint in the same hours with no such stop.

A fix along the lines of the local variant @advxrsary mentions would cover it: treat STATUS_GUARD_PAGE_VIOLATION like the access violation (fail the copy and fall back to the kernel read), and put PAGE_GUARD back on the page, since the fault has already taken it off and the title expects to see its own guard fault.

Machine: RTX 5080, Ryzen 7 7800X3D, 32 GB, Windows 11; diagnostics build.

(Runs and this note by Claude Code with Claude Opus 5.5, at the account owner's request.)

@BitWuehler

Copy link
Copy Markdown
Author

Rebased and updated PR #154 (82dbea5) onto c36971b (plaincopy.rs), addressing every issue raised above:

  1. STATUS_GUARD_PAGE_VIOLATION (0x80000001) / PAGE_GUARD preservation (@iurysf, @advxrsary, @larelgit):
    plaincopy::on_fault now handles EXCEPTION_GUARD_PAGE for both victory_copy_guarded_at and victory_write_guarded_at, calls rearm_guard(at) (VirtualQuery + VirtualProtect(..., info.Protect | PAGE_GUARD)) to restore the consumed guard bit, and fails the plain copy cleanly so the caller falls back to the kernel path without triggering an unhandled guard-page fault. Verified with plaincopy::tests::guard_page_fails_copy_and_stays_guarded.
  2. Reads-only default + LEAST = 256 kept from main (c36971b) (@LeonardoS11, @larelgit):
    plaincopy::copy keeps main's read-side design (LEAST = 256, small readability probes stay on ReadProcessMemory, and all switches stay off by default on Windows), so Windows default behavior and copy_mapped read visuals remain untouched.
  3. Opt-in guarded write path (VICTORY_COPY_PLAIN_WRITES=1 / VICTORY_DIRECT_COPY=1, off by default) (@larelgit, @oeirughrepuiohg):
    On Windows, @larelgit measured NtWriteVirtualMemory outperforming user-mode writes on freshly reset MEM_WRITE_WATCH pages (11.4 us vs 13.1 us per 64 KiB), whereas under Wine/Proton avoiding NtWriteVirtualMemory wineserver IPC helps (@oeirughrepuiohg's +8–15% frame-rate measurement). Keeping writes behind opt-in VICTORY_COPY_PLAIN_WRITES=1 leaves Windows defaults unchanged while preserving the Wine/Proton fast path (victory_write_guarded + shared watch::handle_write_fault).

Verification on c36971b + 82dbea5 (GE-Proton11-14, synthetic/unit suite only):

  • cargo xtask policy: 185 files ok
  • cargo clippy -p victory-gpu --all-targets --target x86_64-pc-windows-msvc: 0 warnings in modified files (plaincopy.rs, watch.rs, host.rs).
  • plaincopy::tests::: 6 passed; 0 failed (VICTORY_COPY_PLAIN=1 VICTORY_COPY_PLAIN_WRITES=1)
  • watch::tests:: --skip drvwatch --test-threads=1: 11 passed; 0 failed
  • host::tests:: --test-threads=1: 22 passed; 0 failed
  • shadow::: 30 passed; 0 failed
  • texture::: 2 passed; 0 failed

AI-assisted (prepared and unit-tested locally; no application/asset data referenced).

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.

6 participants