GPU: stop copying game memory to the card and back every frame (opt-in): 75% less copy-back, half the buffer uploads #772
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!772
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-772"
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?
@sirrus98 on GitHub · 2026-11-25 07:13 UTC · original
sirrus98:perf/tier3-stage2→main· +6944 −143 · 18 files · 18 commits · openSummary
Every frame, victory copies the game's GPU memory out of game RAM into host copies, uploads them to the card, hashes them, and after the card is done copies what shaders wrote back into game RAM. This PR stops most of that copying.
VICTORY_INPLACE=3). Each region of game memory the GPU can reach is opened once as a D3D12 heap over the game's own pages (OpenExistingHeapFromAddress1). Buffers and windows then point straight at the game's bytes: no copy, no refresh, no copy-back.VICTORY_INPLACE_POOL=1). The game's physical page pool is made of committed sections, opened the same way.VICTORY_INPLACE=4). Memory the card reads every frame (big buffers, windows in use for several frames) gets a copy in video memory. A GPU copy brings it up to date from game RAM before the frame's lists run. Afterwards, a GPU compute pass writes back only the bytes shaders changed, leaving any bytes the CPU wrote untouched. No CPU copy, hash or read-back is involved.Everything is opt-in. Without these switches nothing changes.
Results
Measured on an RTX 4090, release build, 120 s in the game world after a warm-up. Each column is one run of this branch with the switches shown;
mainis8dd30abwith the same measurement switches (VICTORY_CARD_TIME=1 VICTORY_INPLACE_PROBE=2).Per frame:
INPLACE=3INPLACE=3POOL=1INPLACE=4POOL=1With hot memory in VRAM, the card holds about 107 such copies (875 MB). Per frame, the GPU brings 21 MB up to date from game RAM, compares 556 MB and writes 39 MB back.
What this shows
main, identical runs on this machine vary by up to ±15% (a "D3D Background Thread" uses about 3.8 cores in every run,mainincluded), and these are single runs. On the previousmain(4e17478), with no such load, the full set ran at 234.6 ms against 240.1 ms with these switches off.Fixes and findings along the way
OpenExistingHeapFromFileMapping) keeps its commit until the PC restarts: 256 MB per test, every time. Sections are now opened through a view of their own, by address. The system's commit stays flat across runs.VICTORY_INPLACE_MIN_KB, 1024 by default). Card atomics into tiny buffers left in game RAM have to cross the bus each time. Three once-a-frame compute passes went from 1–6 ms to 19–67 ms of card time. With the floor, the game's command lists went from 222–244 ms back to 100–129 ms of card time.Switches
All off unless set.
VICTORY_INPLACE=3VICTORY_INPLACE=4VICTORY_INPLACE_POOL=1VICTORY_INPLACE_MIN_KBVICTORY_RESIDENT_*ONE_OWNER,PARTIAL,HOT_FRAMES,NO_MERGE, …)VICTORY_ARENA_EARLY=1,VICTORY_EARLY_SIGNAL=1VICTORY_CARD_TIME=1,VICTORY_INPLACE_PROBE=2,VICTORY_DRAW_PROBES=1,VICTORY_PROBE_PIPELINESvictory inplace-probeVerification
cargo check --workspace --all-targets --locked). The branch merges intomain(a26859f) without conflicts.cargo clippy --workspace --all-targets --locked -- -D warnings, with and without--all-features, reports only the warningsmainalready has (draw.rssort_unstable_by,indirect.rsdepth_written,resource.rsplaced_filler_twins). None is in this PR's code.cargo test --workspace --locked:victory-gpu: 165 passed, 10 ignored, run with--test-threads=1. In parallel, one existing in-place test fails now and then, because the tests share the process-wide table of the game's views. It passes alone and serially.victory-shader: 213 passed, 2 failed (emit::rect::tests::the_rectangle_stage_is_a_geometry_shader,rectangles_are_drawn_whole). Both fail the same way on plainmain; this PR doesn't touch that crate.main.main's CI fails at the Clippy step on its last commits (the three warnings above), and the twovictory-shadertests fail onmaintoo. This PR changes neither, so it stays focused.main, all without a device loss. Measured on8dd30ab, then rebased ontoa26859f(one upstream commit, descriptor records mirrored).Risks
VICTORY_INPLACE_MB(4096) and the in-place budget.ID3D12Device13and heap tier 2. Where opening is refused (for example Wine, or memory from victory's own heap), memory falls back to host copies, and the log line says why.Related work
Searched: every open and closed PR here and on upstream VIctory, plus GitHub code. None opens memory in place over committed ranges, keeps hot memory in VRAM, or writes back on the GPU. These overlap in part:
inplace.rs; happy to rebase on whichever lands first.VICTORY_INPLACE_POOLand the leak fix here.Reviewing
17 commits; best read one at a time:
🤖 Generated with Claude Code
victory inplace-probe; HLE: count the title's frees and protects of graphics memory ed1d824a7cThe tier-3 design (title memory as the GPU's own, one opened heap per region) rests on driver limits nobody had measured on the card. This adds the design's probes (inplace_probes.rs), run on a queue and memory of their own, and a way to run them without the title: victory inplace-probe [--small] [--tiles] [--debug-layer] [--mb N] and `cargo test -p victory-gpu -- --ignored probe_on_hardware`. In game, VICTORY_INPLACE_PROBE=1 adds the small ones (P2-P6, R9) to the first probe, now on a thread of its own; =3 also the ones that lock or commit gigabytes (P1, P2's 4 GiB part, P7). Measured on the RTX 4090 (debug layer on, no errors except where said): - P1: 40 bits of GPU VA per resource and per process. Parts of 2 GiB, 4095 MiB and 4 GiB - 64 KiB open (343/684/672 ms) with a buffer over all of it, written at its far end by copy and shader. 4 GiB and 6 GiB are refused by the runtime ("must point to a memory region that is 4GiB or less"). Committed sections of 2 GiB and 4095 MiB open (89/177 ms); 6 GiB is refused ("hFileMapping must be 4GiB or less"). - P2: opening private memory locks it: 1 GiB 173 ms +1,024 MB working set, 4095 MiB 695 ms +4,096 MB; no commit added; the heap goes in 16/68 ms. A committed section's pages count in the working set when opened (no more when the GPU reads them). - P3: InterlockedOr/And from a compute shader into a part and a section are exact, a byte replaced by And+Or keeps the dword's other bytes, and a CPU writing byte 0 of the same dwords all along keeps its last byte (32 of 32) while the GPU's bits all land. - P4: with a part opened, write watching still reports CPU writes exactly ([1, 5, 9] -> [1, 5, 9]) and reports none of the GPU's (copy and shader: []). - P5: 512 parts of 64 KiB with a buffer each: 93 ms, none refused, gone in 41 ms. Two overlapping parts: written through one, UAV barrier, read through the other in one list: right, and the CPU sees it. - P6: a buffer in an opened heap works as vertex and index views, root CBV, typed SRV and raw UAV in a table, ExecuteIndirect arguments and count, CopyTextureRegion both ways, ResolveQueryData. One buffer used as copy source then destination in one list needs an explicit transition (implicit promotion is once per list: risk R8). - P7: commit headroom 63,990 MB; a 6 GiB SEC_RESERVE section adds 0 MB, SEC_COMMIT adds 6,156 MB (whole or as six 1 GiB sections, both made at once); a 6 GiB section cannot be opened as one heap. - R9: on pages of an opened part VirtualProtect read-only, MEM_DECOMMIT and MEM_RELEASE fail with error 87, also while only a buffer placed in the heap is alive; MEM_RESET and an unchanged protection succeed. - tiles (--tiles, a device of its own): a reserved buffer's tile mapped onto a heap in the card's memory works; mapped into an opened heap the runtime removes the device (0x887a0001), as on the software adapter. R9 (count only): with VICTORY_INPLACE_PROBE, VICTORY_INPLACE or VICTORY_MEMORY_CALLS=1 the title's kernel32 VirtualFree, VirtualProtect and VirtualAlloc(MEM_RESET) go through wrappers in xmem.rs that call the host's functions unchanged and count (and log, first 16 and every power of two) the calls that touch memory XMemVirtualAlloc reserved. Without the switches the imports go to the host as before. Tests: victory-gpu 111 passed, 2 ignored (probe_on_hardware, and the one ignored before); victory-hle 96 passed. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SZC5U8SHMXUK5wS4AS2KxTIn run inplace3-t3 (VICTORY_INPLACE=3) no part ever opened: the title commits its 3.4 GB private region at 0xd00000000 in steps, with holes and pages of other protections, and the arena asked for one part over the whole region ("not private memory committed whole and read-write"); that one refusal then turned down every range of the region. The pool run (inplace3-pool-t3) worked: 8 sections open, 6.78 GiB locked, 6,011 windows (575 MB) and 80 title buffers in place, 2,018 MB of view memory placed, median frame 233.8 ms against 285.5 ms for its control (-18%). Parts come from runs of committed pages. `runs_of` asks Windows for the runs of committed read-write private pages of a reported region, one VirtualQuery per run. The runs are asked when first needed, and again when the region changes or an opening finds the pages changed. A range is in place when it lies in one run. Its part is the lowest of the run's parts on the 2 GiB grid from the run's start (`grid_part`, at most 4 GiB - 64 KiB). An open part that holds the range is always taken first. So when the title commits more and two runs join, ranges over the join open a new, longer part beside the older ones, which stay for what was placed in them until it is let go. A refusal is kept per part, not per region, and a part refused because its pages changed is not kept at all. The title's own kernel32 VirtualAlloc(MEM_COMMIT) in memory that XMemVirtualAlloc reserved is reported too (xmem's wrapper). Remaps. The pool run let 9,755 in-place windows go in three minutes because the title remapped their pages. A placement in a section's arena is now only marked when its pages are remapped. When it is next asked for, it stays if its range is still the same run of the same section at the same offset (`still_good`, `inplace::still_run`); otherwise it is let go and placed again. Both outcomes are counted on the arenas line (REMAPS: kept / placed again). Placements in private parts, and the title's own frees and protects, still let go at once. Why a range is turned down is now counted by kind (arena::Why): - view memory not one run of a committed section; - not memory the runtime reported committed; - spans pages not committed; - spans another protection; - longer than a part of its run; - over the budget; - refused by the host; - its arena still being opened. The counts are given on the arenas line, so the census can be checked against what placement does. Off path: `host::in_place_writes` returns before any pass when VICTORY_INPLACE is off, a closed section tells the arenas nothing below level 3, and xmem's release of 32-bit memory asks nothing (no lock, no VirtualQuery) unless the wrappers' switches are on. Everything else was already behind a switch read once. Tests: - arena::tests::a_range_takes_the_lowest_part_of_its_run_that_holds_it (the grid; regions joined and cut). - arena::tests::warp_runs_of_committed_pages_are_parts_of_their_own. A reservation is committed in two runs with a hole: a buffer in either run is placed in its run's part, and one over the hole is turned down. After the hole is committed and reported, a range over it gets a part of the joined run beside the older two. A range over a read-only page is turned down as another protection. The GPU reads the title's bytes through each part. - shadow::remap_tests::warp_a_placement_in_a_section_outlives_a_remapping_ to_the_same_pages. - arena_on_hardware passes on the RTX 4090. victory-gpu 119 passed, 3 ignored; victory-hle 96 passed. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SZC5U8SHMXUK5wS4AS2KxTIn run inplace3-pool the private arenas opened (2 parts, 3.29 GiB locked, 287 MB placed), but 1,320 placements were turned down "its arena still being opened": a part is opened at its first GPU use, and a part of 2.2 GB takes about 365 ms to open on the arenas' thread. Meanwhile every buffer in it gets a copy, and keeps it. With the switch (off by default; with arenas, VICTORY_INPLACE=3 or more), a commit of graphics memory the runtime reports (title_memory_committed) is asked of the arenas' thread at once (Arenas::ask_early), so its part is usually open before the GPU first uses it: - the parts are cut from the commit itself, every 2 GiB from its start and up to 4 GiB - 64 KiB each, as a run is cut (grid_part), so commits made in steps never open parts over each other; a range over two commits gets the part of its run at its first use, as before; - commits of VICTORY_ARENA_SYNC_MB (64) or less are left to their first use, which opens them on the asking thread (about 11 ms); - the budget (VICTORY_INPLACE_MB) is held from the ask on; over it, nothing is asked and the first use decides, as before; - commits reported before the host is up wait for the first range resolved (the host is not made for them); - a range in a part asked early that is still opening is turned down as opening, and residents wait for it (resolve_waiting), instead of asking for a second, overlapping part of the same run; - title_memory_released waits (five seconds at most) for a part asked early over the range that is still opening: while it opens its pages are locked, and the title's decommit or protect would fail (R9). Without the switch nothing is asked early, the set of parts asked early stays empty, and resolving and releasing are as before. Each ask is logged: "arena: committed graphics memory asked of the arenas' thread as the runtime reported it (VICTORY_ARENA_EARLY=1)" start=.. mb=.. followed by the opening line as before. The effect shows in the existing turned-down counts ("its arena still being opened"). Tests: victory-gpu 130 passed, 4 ignored (was 129); victory-hle 96 + 1. - arena::tests::a_commit_asked_early_is_opened_in_parts_of_its_own (new): a 5 GiB commit is asked as two parts (0, 4 GiB - 64 KiB) and (2 GiB, 3 GiB) with their bytes held; a range held already, a small commit and one over the budget are not asked; a range in an opening part is turned down as opening without a new ask; a release over it sees it; landed or released, it is no longer waited for and its bytes are given back. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SZC5U8SHMXUK5wS4AS2KxTWith VICTORY_INPLACE=4 three compute passes that run once a frame took 1-6 ms of card time in the control and 19-67 ms with Tier 3 (timing runs with VICTORY_GPU_TIMING; 5cf00dc3 3.3 -> 67 ms, cb6adcc6 5.6 -> 41.6 ms, 0a5ecfee 1.2 -> 19 ms). Their code is the same; what changed is where they write: their buffer atomics go to tiny buffers (52 B, 480 B, 56 KB) that were copies in system memory in the control and in place, in the title's memory, with Tier 3. Shaders reach memory in place across the bus. Title buffers smaller than VICTORY_INPLACE_MIN_KB (1024 by default, 0 for no floor) now stay copies, as without VICTORY_INPLACE. Windows are not affected. Such buffers are 42 MB in all (3,376 of them, 3,242 under 64 KiB), so copying them costs little. Measured (RTX 4090, release, the same exe, the switches equal but this one; inplace4 with VICTORY_CARD_TIME=1, 120 s after the world): floor off floor 1024 KiB control (Tier 3 off) frame median 495.0 ms 234.6 ms 240.1 ms average fps 1.69 4.30 4.33 title lists, card time 222-244 ms 100-129 ms ~111-115 ms The frame-rate line gets a placement census: title buffers and windows apart, count and MB in a resident, in place (and of those under 64 KiB), card copy and system copy; and the buffers the floor kept as copies. Tests: inplace::min_tests (the default, 0 and bad settings).A title buffer shares a resident whose pages hold it (the previous commit). But when an inner big buffer is used before the one around it, the outer one got a resident of its own beside the inner one. Their bytes stayed in step only between submissions (cross_mark), and the card's memory held the overlap twice. Now a big buffer whose pages no resident holds, but which overlaps residents, gets one resident over all of them (resident::make_over), if that lies in one arena within the budget; else one over its own pages, as before. The new resident takes their places in the registry, and lookup lets go of the title buffers whose shadows are in a replaced resident, so their next ask finds the new one. A replaced resident stays alive for the lists and descriptor slots that still hold it, and keeps receiving the title's pages and the other copies' writes until they let go. resident::find now returns Found { resident, bias, replaced }. Tests: victory-gpu 127 passed, 4 ignored (was 126); victory-hle 96 + 1. - resident::tests::warp_overlapping_residents_become_one (new). Residents over pages 1 and 5 are made first. A buffer over pages 0..3 replaces the one over page 1, and its pages find the new resident. One over 2..6 joins the two remaining into 0..6. A write through the new one marks the replaced one's page. Thirteen runs of the whole victory-gpu suite in a row passed. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SZC5U8SHMXUK5wS4AS2KxT@Kanthon1209 on GitHub · 2026-11-25 07:16 UTC · original
UP NB
@alpz1000 on GitHub · 2026-11-25 17:40 UTC · original
Independent Windows 11 build 26200 result on RTX 3060 12 GiB (610.88), i5-8400 and 32 GiB RAM. I integrated
70302bainto a local465b94f-based Series S build with the same display, range-lock and idle-cache changes in both runs. The shortinplace-probeconfirmed GPU reads and writes through the private allocation and section-backed views. I kept the 2 GiB Windows commit-headroom guard enabled.VICTORY_INPLACE=4 VICTORY_INPLACE_POOL=1 VICTORY_INPLACE_MB=2048 VICTORY_RESIDENT_MB=1024: the guard ended the launch after 188 s during loading. Near that point the arena census showed 1.42 GiB locked, and 24 residents used 121 MiB plus 121 MiB of comparison buffers.VICTORY_INPLACE_MB=1024 VICTORY_RESIDENT_MB=768: reached the same gameplay checkpoint. The arena held one 1 GiB section. After warm-up, the resident line showed seven buffers (12 MiB plus comparison buffers) and no resident merge work in the sampled steady intervals. A 240-frame window at roughly 4,500 draws averaged 5.21 FPS (P90 216 ms). The comparable local build without this PR averaged 4.92 FPS over 250 frames (P90 225 ms). There was one launch per setting, and the steady FPS difference is too small to attribute to resident work.The smaller budget let the run finish loading on this configuration. The low number of active residents in the steady checkpoint is the main difference from the 4090 results above. AI-assisted with OpenAI Codex.
@UwUCammie on GitHub · 2026-11-26 00:52 UTC · original
The bounded physical import test passed both directions on an RX 7900 XT with Wine 11.22 and RADV 26.3.1. In the 230-second title run, level 4 with the pool enabled imported 10,093 windows and had 9.99 GiB locked at the last report. It was still showing a white transition screen at the late check. I can’t assign a gameplay FPS to that run. The same executable with both options off reached the character and courtyard (356 accepted presents in 73.82 s, 4.82 FPS). No device removal was reported. GPU fixture · Build and measurement window
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.