Skip to content

xmem: large graphics blocks get write-watched memory of their own - #3053

Open
0xkn0wh3r3 wants to merge 1 commit into
Marvisak:mainfrom
0xkn0wh3r3:xmem/write-watched-graphics-blocks
Open

0xkn0wh3r3 wants to merge 1 commit into
Marvisak:mainfrom
0xkn0wh3r3:xmem/write-watched-graphics-blocks

Conversation

@0xkn0wh3r3

Copy link
Copy Markdown
Contributor

For a title without allocation hooks, XMemAllocDefault serves every block from the host heap, graphics memory included. Committed resources' memory (allocator 128, from victory_gpu::resource) therefore lands in private memory the GPU side can't write-watch, so its host copies are refreshed whole at every submission that uses them. Decision 0007 is to copy only the pages written since the last copy, which XMemVirtualAlloc's write-watched reservations make possible.

Changes

  • Graphics blocks of 64 KiB and up, aligned to 64 KiB at most, now get a reservation of their own from XMemVirtualAlloc, write-watched like the runtime's memory. XMemFreeDefault releases them.
  • Smaller blocks, larger alignments and other allocators' blocks still come from the host heap.
  • Titles with allocation hooks (Grand Theft Auto VI) don't go through this path and are unaffected.

Testing

  • New test large_graphics_blocks_are_write_watched. It checks the reservation is write-watched and 64 KiB aligned, that it's released on free, and that small and non-graphics blocks stay on the heap.
  • All 15 xmem tests pass: cross-compiled for Windows in release and run under Wine and CrossOver on macOS. Not run on Windows.
  • Measured with the conformance demo from Tests: the conformance title's GPU frame, cargo conformance and cargo demo (with a stress scale), on Windows and macOS #3021, a title without hooks (under CrossOver, 720p, on the fork's demo). The refresh phase fell 80–98% a submission: from 0.697 to 0.014 ms when a 14.7 MB compute output buffer is in use, and from 0.058 to 0.009 ms with 129 small submissions a frame.
  • Trade-off: copy-back of buffers the GPU writes goes through the tracked path page by page, so it rose (0.888 to 1.402 ms a submission in the scale-1 case). Net CPU time per submission still fell at every scale but one, where it was even. At 129 submissions a frame, the frame went from 54.9 to 50.4 ms.

Without the title's allocation hooks, XMemAllocDefault served every block
from the host heap, graphics memory included: committed resources' memory
(allocator 128, victory_gpu::resource) was private memory the GPU side
cannot watch, so their host copies were refreshed whole at every
submission that used them (decision 0007 copies only the pages the title
wrote since the last copy, which XMemVirtualAlloc's write-watched
reservations make possible). Graphics blocks of 64 KiB and up, aligned to
64 KiB at most, now get a reservation of their own from XMemVirtualAlloc,
as the runtime's memory is; XMemFreeDefault releases them. Other blocks
come from the host heap as before. Titles with hooks are unaffected.

large_graphics_blocks_are_write_watched checks the reservation, its
alignment and release, and that small and non-graphics blocks stay on
the heap.
0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 26, 2026
- `cargo demo` is a synthwave drive into the sunset: a neon car, the
  camera following, spinning shapes along the highway (the stress scale),
  and it reports where each frame's time in VIctory goes, by call and by
  submission phase (victory_gpu::submission_totals).
- Read-mark scans stop at the end of the view slots in use, dynamic slots
  counted (as Marvisak#3052 on main).
- Large graphics blocks get write-watched memory of their own, so their
  host copies refresh only the pages written (as Marvisak#3053 on main).

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed head 3069e4ff line-by-line (CI green):

  • graphics_block routes D3D_ALLOCATOR (128) allocations of >= 64 KiB with align <= 64 KiB to XMemVirtualAlloc(..., MEM_RESERVE | MEM_COMMIT, GRAPHICS, PAGE_READWRITE), which sets MEM_WRITE_WATCH and satisfies <= 64 KiB alignment via Windows allocation granularity.
  • Lock/free ordering is sound: WATCHED.insert runs before returning the address, and WATCHED.remove runs before VirtualFree(address, 0, MEM_RELEASE), avoiding any address-reuse ABA race. ALLOC_32BIT still takes precedence before graphics_block, and xmem.rs has no XMemSizeDefault that would read a missing block header.
  • large_graphics_blocks_are_write_watched verifies GetWriteWatch, 64 KiB alignment, MEM_FREE after release, and that smaller or non-graphics allocations stay on the host heap.

Minor non-blocking note: XMemFreeDefault acquires WATCHED on every free; an AtomicUsize count fast path when empty would avoid lock traffic on non-graphics frees.

@kotekompotek kotekompotek 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.

Verified: gating on allocator id 128 + size ≥64KiB + alignment ≤64KiB confines the new path to committed-resource blocks that fit a reservation; everything else keeps the heap path. WATCHED membership decides MEM_RELEASE so mixed frees can't misfree, and the write-watch flag means the GPU side now copies only written pages rather than the whole block per submission — the real win. The test verifies GetWriteWatch, 64KiB alignment, and MEM_FREE after release.

Marvisak commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Thanks, agreed. This stays as is until #3021 lands and the net effect can be measured there.


Generated by Claude Code

@0xkn0wh3r3

Copy link
Copy Markdown
Contributor Author

Fair. The title this project runs takes its allocation hooks, so it never reaches this path, and the only numbers come from #3021's demo, where copy-back per submission went up. Without a case on main that benefits, I'll leave this waiting for #3021. Then it can be measured there with the copy-back cost included, or closed if that doesn't come out ahead. If it goes in, the WATCHED lookup in XMemFreeDefault will only run for allocator-128 attributes, so other frees skip the lock.

0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 26, 2026
…ebased onto upstream; its reworded test note)
@Jackster

Copy link
Copy Markdown

Windows 11, 63bcca4 + this PR:

  • cargo test -p victory-hle xmem::: 15 passed, 0 failed, including large_graphics_blocks_are_write_watched.
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.

Synthetic only. AI-assisted.

lfmalta96 pushed a commit to lfmalta96/VIctory-9060XT16 that referenced this pull request Nov 27, 2026
Without the title's allocation hooks, XMemAllocDefault served every block
from the host heap, graphics memory included: committed resources' memory
(allocator 128, victory_gpu::resource) was private memory the GPU side
cannot watch, so their host copies were refreshed whole at every
submission that used them (decision 0007 copies only the pages the title
wrote since the last copy, which XMemVirtualAlloc's write-watched
reservations make possible). Graphics blocks of 64 KiB and up, aligned to
64 KiB at most, now get a reservation of their own from XMemVirtualAlloc,
as the runtime's memory is; XMemFreeDefault releases them. Other blocks
come from the host heap as before. Titles with hooks are unaffected.

large_graphics_blocks_are_write_watched checks the reservation, its
alignment and release, and that small and non-graphics blocks stay on
the heap.

Port of Marvisak/VIctory#3053 (commit 3069e4ff).
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.

5 participants