GPU: use VEH-guarded in-process copy for copy_mapped and write_mapped (#118) #154
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/fxceless--victory-fork!154
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-154"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@BitWuehler on GitHub · 2026-11-24 21:56 UTC · original
BitWuehler:wine-direct-copy→main· +295 −44 · 3 files · 1 commits · openSummary
Rebased onto
fxceless/victory-forkmain(c36971b), extendingcrates/victory-gpu/src/plaincopy.rsfor:PAGE_GUARD(STATUS_GUARD_PAGE_VIOLATION/0x80000001) handling & re-arming (rearm_guard) on both reads and writes, preventing unhandled guard-page exceptions and restoringPAGE_GUARDviaVirtualQuery+VirtualProtect(..., info.Protect | PAGE_GUARD)before failing back to the kernel path.VICTORY_COPY_PLAIN_WRITES=1orVICTORY_DIRECT_COPY=1, off by default) forhost::copy_mapped(title_dst) andhost::write_mappedviavictory_write_guarded+ sharedwatch::handle_write_fault(at).Why this shape on top of
c36971bmain(c36971b) already landedplaincopy.rs(VICTORY_COPY_PLAIN=1) for reads (copy_mappednon-title_dst&read_into) while leaving< 256-byte probes and writes on the kernel path. Keepingmain's read design andLEAST = 256leaves native Windows defaults and picture behavior untouched.VICTORY_COPY_PLAIN_WRITES=1, off by default) avoids changing Windows write behavior (where @larelgit measuredNtWriteVirtualMemorywinning on freshly resetMEM_WRITE_WATCHpages) while still allowing Wine/Proton users to bypassNtWriteVirtualMemorywineserverIPC when desired.EXCEPTION_GUARD_PAGE(0x80000001) inplaincopy::on_faultand callingrearm_guardbefore exiting viavictory_copy_guarded_faultfixes the crash mode identified by @iurysf, @advxrsary, and @larelgit whenever a copied source/target page carriesPAGE_GUARD.Changed files
crates/victory-gpu/src/watch.rs: extractedpub(crate) fn handle_write_fault(at: usize) -> boolfromon_writesowatch::on_writeandplaincopy::on_faultshare the same write-watch guard-opening path.crates/victory-gpu/src/plaincopy.rs: addedvictory_write_guarded(rep movsbwith Win64 SEH unwind directives),writes_on(),rearm_guard(at),WRITE == 1write-fault handling inon_fault,write(to, from, len), write counters inline(frames), and unit testsguard_page_fails_copy_and_stays_guardedandguarded_write_skips_unmapped_and_readonly_and_marks_write_watch.crates/victory-gpu/src/host.rs: routedcopy_mapped(title_dst) andwrite_mapped(direct_writes() && crate::plaincopy::writes_on()) throughcrate::plaincopy::write.Verification (
82dbea5onc36971b)cargo xtask policy:185 files okcargo clippy -p victory-gpu --all-targets --target x86_64-pc-windows-msvc:0warnings in modified files (plaincopy.rs,watch.rs,host.rs; 7/8 pre-existing upstream warnings in untouched modules).GE-Proton11-14(vkd3d-proton,VICTORY_COPY_PLAIN=1 VICTORY_COPY_PLAIN_WRITES=1):plaincopy::tests:::6 passed; 0 failed(includingguard_page_fails_copy_and_stays_guardedandguarded_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_subscriptionskipped due to WineEvtSubscribestub)host::tests:: --test-threads=1:22 passed; 0 failedshadow:::30 passed; 0 failedtexture:::2 passed; 0 failed(Prepared and verified with AI assistance.)
@BitWuehler on GitHub · 2026-11-24 22:12 UTC · original
In-world follow-up on CachyOS Linux (
Ryzen 7 9800X3D,RTX 5080620.73.04,GE-Proton11-14, release profile, Series X files,463.9 sprior run vs345.1 snew run):draws >= 100)4e17478(RELAY=2, no#154,n=50)#154+LATE_WAIT=3+WIDE_REFRESH=1+WIDE_MERGE=1(noRELAY=2,n=38)copying backmedian185.50 ms(61.70 MB)19.85 ms(67.30 MB)copying backmean284.74 ms(60.21 MB)38.71 ms(63.75 MB)6,800draws/frame179.9 ms(53.2 MB,6,813draws)12.7–19.1 ms(49.3–55.6 MB,6,730–6,873draws)gpumedian121.55 ms67.55 mstex_msmedian166.55 ms(34.80 MB)596.55 ms(50.60 MB, up to210.5 mswaiting for remaps &141.9 mswatch/guards)fpsmedian (world peak)1.30 fps(2.0 fps)0.85 fps(2.0 fps;1.6 fpsat6,873draws)Caveat on isolating
#154: Beyond adding#154, the second run also removedVICTORY_RELAY=2(see #6 / #89, which previously caused ~103 ms card-fetch relay waits per frame) and enabledVICTORY_WIDE_MERGE=1+VICTORY_WIDE_REFRESH=1. So the~9xdrop incopying backcombines droppingRELAY=2and#154+WIDE_MERGE=1. Stability was clean across345.1 s(rc = 0, 0 device removals, 0 Xids), andPlanePasswarnings from #127 also dropped from2to0.(AI-assisted analysis from session logs; numbers directly extracted from the two runs.)
@iurysf on GitHub · 2026-11-25 00:14 UTC · original
We merged this PR into our test branch and measured it on an RTX 4070, paired within one run (
VICTORY_DIRECT_COPYturned 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) insidevictory_try_copy_op. Symbolized stack, innermost last:on_faulttakesEXCEPTION_ACCESS_VIOLATIONandEXCEPTION_IN_PAGE_ERRORonly. A read through a page protected withPAGE_GUARDraisesSTATUS_GUARD_PAGE_VIOLATIONinstead, 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).ReadProcessMemoryfails on such a page cleanly and leaves the guard in place.A possible fix: in
on_fault, treatSTATUS_GUARD_PAGE_VIOLATIONinside[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 withVirtualProtect(..., 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 on GitHub · 2026-11-25 03:19 UTC · original
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,
diagbuild withdiagnostics;mainatc96b4f2with 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
c96b4f2c96b4f2+ this PRSo 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=0on 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 withVICTORY_DIRECT_COPY=0if 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 on GitHub · 2026-11-25 04:45 UTC · original
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 framelines 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):copy_mappedinto host memory,read_into). Copies into title memory (write_mapped,Step::Copy) stay with the kernel.ReadProcessMemory, so what an unmapped page does to the result is what it was before. The one-byte readability probe intexture.rsstays on the kernel, since it mostly looks at pages that are not there.STATUS_GUARD_PAGE_VIOLATIONand 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
310c6ebwith its defaults (no switches;VICTORY_USER_COPIES=0is the kernel path, same binary):copy_mappedbulk read=0)(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::Copythroughtry_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
NtWriteVirtualMemoryand 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 (a803c87withVICTORY_LATE_WAIT=3 VICTORY_WIDE_BACK=1and 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 on GitHub · 2026-11-25 10:02 UTC · original
I measured this pull request on native Windows. The processor and the card are the same as in your Proton test.
Setup
mainat9376fd6, and the same tree with this branch merged. The merge had no conflicts.mainandVICTORY_WAVE_WIDE=0Result
Each number is the median of the frame lines of one run in that scene.
main, 4 runsThe 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 machinemainwaits 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
PAGE_GUARD(for example the guard page of a thread stack),rep movsbraisesSTATUS_GUARD_PAGE_VIOLATION.on_faultdoes 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 on GitHub · 2026-11-25 12:08 UTC · original
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
2119b0eplus 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)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 on GitHub · 2026-11-25 12:57 UTC · original
The case @advxrsary described above does happen, on
mainatf85101b: 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 unhandledSTATUS_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):
So a texture's bytes in title memory include a page the title asked for with
PAGE_GUARD;xmempasses that flag through to the host (HOST_PROTECTIONis 0x1ff), the in-processrep movsbtrips it, andon_faultdoes not take that code. The kernel readmainuses does not stop the process there.mainatf85101bwithout 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_VIOLATIONlike the access violation (fail the copy and fall back to the kernel read), and putPAGE_GUARDback 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 on GitHub · 2026-11-25 21:41 UTC · original
Rebased and updated PR #154 (
82dbea5) ontoc36971b(plaincopy.rs), addressing every issue raised above:STATUS_GUARD_PAGE_VIOLATION(0x80000001) /PAGE_GUARDpreservation (@iurysf, @advxrsary, @larelgit):plaincopy::on_faultnow handlesEXCEPTION_GUARD_PAGEfor bothvictory_copy_guarded_atandvictory_write_guarded_at, callsrearm_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 withplaincopy::tests::guard_page_fails_copy_and_stays_guarded.LEAST = 256kept frommain(c36971b) (@LeonardoS11, @larelgit):plaincopy::copykeepsmain's read-side design (LEAST = 256, small readability probes stay onReadProcessMemory, and all switches stay off by default on Windows), so Windows default behavior andcopy_mappedread visuals remain untouched.VICTORY_COPY_PLAIN_WRITES=1/VICTORY_DIRECT_COPY=1, off by default) (@larelgit, @oeirughrepuiohg):On Windows, @larelgit measured
NtWriteVirtualMemoryoutperforming user-mode writes on freshly resetMEM_WRITE_WATCHpages (11.4 usvs13.1 usper 64 KiB), whereas under Wine/Proton avoidingNtWriteVirtualMemorywineserverIPC helps (@oeirughrepuiohg's +8–15% frame-rate measurement). Keeping writes behind opt-inVICTORY_COPY_PLAIN_WRITES=1leaves Windows defaults unchanged while preserving the Wine/Proton fast path (victory_write_guarded+ sharedwatch::handle_write_fault).Verification on
c36971b+82dbea5(GE-Proton11-14, synthetic/unit suite only):cargo xtask policy:185 files okcargo clippy -p victory-gpu --all-targets --target x86_64-pc-windows-msvc:0warnings 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 failedhost::tests:: --test-threads=1:22 passed; 0 failedshadow:::30 passed; 0 failedtexture:::2 passed; 0 failedAI-assisted (prepared and unit-tested locally; no application/asset data referenced).
82dbea5333to3d0b43fb61@BitWuehler on GitHub · 2026-11-26 14:40 UTC · original
Rebased onto
main(a20809d, commit3d0b43f) to resolve theplaincopy.rsconflict withcopy_short/VICTORY_FILLS_PLAIN:main'scopy_short/COUNT[5]path alongside opt-inVICTORY_COPY_PLAIN_WRITES=1(COUNT[6..9]) andPAGE_GUARDre-arming.GE-Proton11-14-x86_64(VICTORY_COPY_PLAIN=1 VICTORY_COPY_PLAIN_WRITES=1):plaincopy::tests::: 7 passed; 0 failed (includinga_short_copy_is_whole_or_ends_without_a_fault,guard_page_fails_copy_and_stays_guarded, andguarded_write_skips_unmapped_and_readonly_and_marks_write_watch).watch::tests::: 16 passed; 1 failed (holes_are_known_by_their_bits_only_when_noted_for_good, hitting Wine's unimplementedNtQueryVirtualMemory info_class=7).Note: Rebase and unit-test verification were AI-assisted; no application/asset data referenced.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.