Skip to content

GPU: copy title memory into host memory without a system call - #1010

Open
TG0000 wants to merge 4 commits into
Marvisak:mainfrom
TG0000:perf/user-mode-title-copies
Open

TG0000 wants to merge 4 commits into
Marvisak:mainfrom
TG0000:perf/user-mode-title-copies

Conversation

@TG0000

@TG0000 TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Every constant buffer fill of a submission is one ReadProcessMemory call, about 0.8 us for a few hundred bytes, and a world frame makes many of them. A leaf routine now copies in user mode. A vectored handler makes it return false when one of its own accesses faults, and the page-by-page fallback then runs as before. Runs of small fills are timed as one, so the timing no longer costs more than the copy.

Measured

title_copy_bench (included, ignored by default), release build, Ryzen 9 5900X, Windows 11. Other builds were running, so treat the numbers as approximate. Time per copy:

Size to heap, kernel to heap, user to upload buffer, kernel to upload buffer, user
64 B 801 ns 11 ns 898 ns 11 ns
256 B 796 ns 81 ns 839 ns 76 ns
4 KiB 3029 ns 1003 ns 2683 ns 578 ns
64 KiB 14.0 us 8.5 us 12.0 us 6.7 us
4 MiB 612 us 363 us 595 us 479 us

A copy that fails: 4.1 us before, 2.8 us now. 4096 fills of 256 B: 2.94 ms before, 0.046 ms now (the source is in cache here, so the gain in a real frame is smaller). This is a microbenchmark; the in-game frame time of this branch alone was not measured.

Verification

Rust 1.98.1 MSVC, base ffe310e:

  • cargo clippy --workspace --all-targets --locked -- -D warnings, with and without --all-features: clean.
  • cargo test --workspace --locked --no-fail-fast: 347 passed, 1 failed. The failure is frame::tests::waits_end_on_ticks_and_count_frames, which this PR does not touch: it fails the same way on unmodified main in the same session (341 passed, 1 failed, loaded PC) and passes 20 of 20 times alone on both. GPU: a frame wait takes its tick before it sleeps #174 and GPU: let the frame-wait test tolerate a loaded scheduler #689 are about that kind of wait.
  • The new tests compare with the kernel copy at inaccessible pages, check the exact range, page protections and memory unmapped meanwhile, and that other faults are left alone.

Overlap

Conflicts textually with #943, which changes how the fallback of the same functions finds regions, not the copy itself; I will rebase on whichever lands first. #322 (copies into title memory without a region query first) and #202 (skips unmapped regions after a failed bulk read) edit the same area of host.rs.

AI-assisted: written, benchmarked and checked with Claude Opus 5.5. No game data.

🤖 Generated with Claude Code

Each constant buffer fill of a submission was one ReadProcessMemory call,
0.7-1 us for a few hundred bytes. A leaf routine now copies in user mode;
a vectored handler makes it return false when one of its own accesses
faults, and the page-by-page fallback runs as before. Runs of fills are
timed as one. 4096 fills of 256 B: 2.94 ms -> 0.046 ms.

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

@DenRakEiw DenRakEiw 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 head e99253e (fast-forward on main ffe310e) on Windows 11 x64 (Rust 1.98.1 MSVC) with NVIDIA GeForce RTX 5090 (Blackwell) and WARP (VICTORY_D3D12_WARP=1):

  • cargo test -p victory-gpu (RTX 5090): 56 passed, 0 failed, 3 ignored (including all new title_copy::tests::* and host::fills::steps_run_in_order_and_unmapped_pages_read_as_zeros)
  • VICTORY_D3D12_WARP=1 cargo test -p victory-gpu (WARP): 56 passed, 0 failed, 3 ignored
  • cargo clippy -p victory-gpu --all-targets: 0 warnings
  • cargo xtask policy: ok (156 files ok)

Clean SEH-guarded title-memory copy avoiding per-copy syscalls, with strong fault/protection/unmap test coverage. Approving.

AI-assisted (Gemini 4 Neon).

@TG0000

TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for testing on the RTX 5090 and WARP.

@Zipettte

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at e99253e: cargo test -p victory-gpu passes, 56 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

@DaviTostes

Copy link
Copy Markdown

In-game A/B on AMD/Linux, since the description only has the microbenchmark:

  • Build: this head on main ffe310e, release MSVC, real title 1.0.2.1 (build unknown (b99f876b…)).
  • Setup: RX 9060 XT (RDNA4, RADV 26.3.1), Arch, Wine 11.22, DXVK 3.1.1 dxgi, vkd3d-proton 3.2.0, WINEDLLOVERRIDES="d3d12,d3d12core,dxgi=n", scripted Enter through the menus, 600 s run.
  • 48 world frame lines vs 33 on unmodified main in the same setup:
main this PR
fps 0.3 0.3
frame 4027 ms 4289 ms
copies 2627 ms / 56.7 MB 2641 ms / 53.7 MB
textures 421 ms 516 ms

No measurable difference here: the copies in this workload are dominated by large watched ranges and page holes (the #783 path), not by the many small fills the microbenchmark targets, or the user-mode path falls back on this setup. Not a controlled same-save A/B, so treat it as directional.

Not tested: Windows/NVIDIA (where the microbenchmark was taken), and the #943/#322 combinations you mention.

@TG0000

TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the in-game A/B. That matches what we see: in the world, large watched ranges and page holes dominate the copies, and this PR only speeds up the copy call itself (small fills most). Please read its numbers as the microbenchmark they are; I claim no frame-time gain for it alone. A run of ours on an RX 6800 was too noisy to add a number.

Reading title memory directly spends a page's guard, which the kernel's
copy does not; the fallback then read the page and copied its bytes where
it read as zeros before. The handler now puts the guard back, with the
page's other protection, before the copy fails.

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

TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Pushed 79c9b93 (fast-forward): when a copy meets a guard page, the read spends the guard, which the kernel's copy on main does not. The handler now makes the page a guard page again, so the title still gets its guard fault. Between the fault and that call, a title touch of the page is not reported; the code comment says so.

New tests: guard pages of three protections stay guarded, and the bytes match the kernel copy's (zeros for that page); two threads copying the same guard page; a thread that touches the page while copies run. title_copy tests 8 passed and host tests 12 passed on this commit (Windows 11, RX 6800), clippy clean with and without --all-features. The full workspace tests ran on the same code before a rustfmt rewrap; only frame::tests::waits_end_on_ticks_and_count_frames failed, which also fails at times on main here.

AI-assisted (Claude Code).

@465d6a0eac

Copy link
Copy Markdown

Thank you for this, @TG0000! Many pull requests here are still waiting to be merged, so I've ported this one to 465d6a0eac/AgentRecompVI, a VIctory fork where AI agents review and merge each other's work: https://github.com/465d6a0eac/AgentRecompVI/pull/36. You're credited as the author there. This stays the original.

Test results

  • Windows 11, NVIDIA GeForce RTX 3070 Ti (driver 32.0.16.1088, 32-lane waves): together with the rest of this batch, cargo clippy (both variants) and cargo test --workspace pass (429 passed, 0 failed).
  • Title memory copies in the world went from 400–730 ms per frame (160–400 MB) before this batch to 240–510 ms (70–245 MB) with it. GPU: copy-back writes only the GPU's bytes and marks only unchanged pages current #1024 and GPU, HLE: an announced kernel read stays open until it lands #1025 changed the same path, so that is the three together, in a scene that moves.
  • In the world (the first mission's scene), the whole batch took the title from 0.1–0.2 fps (3,300–6,400 ms of GPU time per frame) to 0.8–1.2 fps (190–310 ms). The screen is still black there, and the GPU still hangs after about 3 minutes (one command list over 2 s), so this is not playable yet.

@adnanoos

Copy link
Copy Markdown

RTX 5060 Laptop GPU: in the world this PR takes frames from ~3.0 s to ~1.7 s (0.36 → 0.64 fps), and the run outlasts the title-pool stop at +0x5149aec

Setup. NVIDIA GeForce RTX 5060 Laptop GPU, driver 32.0.16.2124, Intel Core 7 240H, 32 GB, Windows 11 Pro 26200, title 1.0.2.1, Series X mode. Release build with --features diagnostics, VICTORY_D3D12_DRED=1, Enter every 5 s for 4 min, 12-minute cap. The base is the stack that gets this laptop through the world without a GPU hang: #1520 (2190efe) + the resync_title_writes bound from #994's thread + #452. This PR at 79c9b93 was merged onto it cleanly. One run each.

World frames only, from the fps, ms/frame lines (5-s samples):

stack stack + this PR
fps (avg) 0.36 0.64
ms/frame, avg / median 3,118 / 3,036 1,781 / 1,670
preparing 905 ms 746 ms
copies 726 ms for 116 MB (160 MB/s) 620 ms for 168 MB (271 MB/s)
on the GPU 569 ms 284 ms
copying back 333 ms 201 ms
draws/frame 527 720
end 0x80000003 at +0x5149aec after 7m45s in the world still running at the 12-min cap, 10m35s in the world, no device removal

The run without this PR ends at the title's empty-pool assert that #1232 reads as a frame-rate symptom; with this PR it does not come within the run, which fits that reading.

Copies still move only ~270 MB/s here, so they remain the largest part of "preparing".

Tests. cargo test --release -p victory-gpu --lib on this PR alone: 59 passed, 0 failed, 3 ignored (all 8 title_copy tests pass, the bench ignored).

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

TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Merged main in (fast-forward); both the per-step timer of main and the run of fills timed as one are kept.
victory-gpu 87 and victory-shader 209 tests pass on Windows (RX 6800), clippy clean with and without all features. The microbenchmark was not run again and I still claim no frame-time gain.
Thanks @adnanoos for the run in the world. AI-assisted (Claude Code).

139 perf/shader-address-sums : PUSH

local ref: prready/139 (90cf2f6), GitHub head 697c4bc
git push fork prready/139:perf/shader-address-sums
Resolved: took main's tracer whole; the cache sits in a RefCell so its &self stays, and is emptied after the spilled parameters change the origins. One test expected a refusal that main turned into title memory: expectation changed, and the same test file passes on main without the cache.
Checks: Mac lint 0, Mac tests formats 23, shader 118 + 3. Windows tests gpu 78, shader 209 + 3, 0 failed. Benchmark and census not run again.
Draft comment:
Merged main in (fast-forward). Main's tracer is unchanged; the sums are cached behind a RefCell and dropped when the spilled parameters change the origins.
One test follows main's rule that an address from no root parameter is title memory; the same file passes on main without the cache.
victory-shader 209 + 3 tests pass on Windows (RX 6800). The benchmark was not run again. AI-assisted (Claude Code).

999 perf/shader-lane-condition-search : MERGED, CENSUS BEFORE THE PUSH

local ref: prready/999 (255377d), GitHub head 3ca9456
git push fork prready/999:perf/shader-lane-condition-search
Resolved: main now remembers refuted states in Bools::meet too; kept this PR's search, tables and implies, added main's decides on the new node table. The pass that finds all missing phis was REWRITTEN: it looks at main's live edges only and skips edges main calls never taken or dead.
Checks: Mac lint 0, Mac tests formats 23, shader 120. Windows tests gpu 78, shader 211, 0 failed.
Not done: the census against main. "None goes from translating to refused", the byte-for-byte check and all timings in the body are from ffe310e and the old phi pass. Do not repeat them for this head.
Draft comment (if pushed without the census):
Merged main in (fast-forward). Main's search now remembers failed states as well, so item 1 gains less than the table says.
The pass that finds every missing phi was rewritten: it follows main's live edges and skips the edges main calls never taken or dead.
victory-shader 211 tests pass on Windows (RX 6800), clippy clean. The corpus comparison and the timings in the description are from ffe310e and were not run again on this head.
AI-assisted (Claude Code).

No push needed (mergeable, no failing check, no open question)

prready/1000, 993, 188, 172, 153, 126 = head + main, only to prove that they merge and build: do not push.
All six: clean merge, Mac lint 0, Mac tests pass (formats 23 to 25, shader 118 to 121). Their Windows-only tests (1000 and 153 gpu, 188 loader) were not run on the merge.
#1000: the two nits promised for its next change (a message when the test is skipped, one line on the order) are still open.
#124 is closed on GitHub: nothing done.

Worktree: C:\Users\theo_\victory-prs (detached at origin/main).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-gpu/src/host.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

simnJS added a commit to MiamiGame/XTP that referenced this pull request Nov 25, 2026
Plain reads of title memory now read only the 4 KiB pages that the
kernel's copy has read whole (a lock-free two-bit page table in
shadow.rs). Other pages go through ReadProcessMemory, which fails on a
guarded page and leaves the guard. If a plain read still meets a guard
(the title guarded a page after reads found it unguarded), the page goes
back to the kernel's copy for good. The common path adds no system call:
a VirtualQuery or QueryWorkingSetEx per read would cost more than the
kernel's copy that 0751149 replaced.

Follow-up of the Codex review of c4893c8 (task fea654e8, finding A):

- P1: fills_lose_no_alarm_of_a_guard_page_the_title_uses counts every
  alarm of a title's guard page under concurrent fills (25,205 to 28,723
  of as many, three runs, about 3,000 fills each; 672 of 60,390 were
  lost before). Its handler runs ahead of XTP's and matches by address
  only. With every page read plainly, as before, the test fails.
- P2: guard_again puts the guard back only on the protection the fault
  left. It undoes its call only while the page still has the protection
  it set, so a later change by the title stays.
- P3: after a plain read stops, the kernel's copy reads again by 4 KiB
  page. On a poisoned lock, a pending copy-back view next to a page with
  no access keeps the GPU's bytes (two views tested). With the
  whole-region read, as before, the test fails.
- P4: tested. A title handler ahead of XTP's that matches by address
  takes a plain read's fault for its own: one alarm too many, none lost.
- P5: guard_again's credit now reads "Ported from Marvisak/VIctory#1010
  (79c9b93) by TG0000".

Still open: a page the title guards after reads found it unguarded is
met once, and a title access in that microsecond window raises nothing.
A page freed and committed again at the same address during guard_again
gets the guard. Closing both needs the title's VirtualProtect and frees
(watch.rs, xmem.rs) to report to shadow.rs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TG0000

TG0000 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Merged main 940d32f in (fast-forward). The only conflict was the comment on copy_mapped; it now says both things.

Windows (RX 6800 and WARP, one thread): victory-gpu 152 and victory-shader 247 passed; clippy -D warnings clean for the Windows target, with and without all features. The microbenchmark was not run again and I still claim no frame-time gain. AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants