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

Open
github-bot wants to merge 1 commit from github-bot/gh-3053 into main AGit
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-26 05:35 UTC · original
0xkn0wh3r3:xmem/write-watched-graphics-blocks → main · +67 −1 · 1 files · 1 commits · open

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 #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.
**@⁠0xkn0wh3r3** on GitHub · 2026-11-26 05:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/3053) `0xkn0wh3r3:xmem/write-watched-graphics-blocks` → `main` · +67 −1 · 1 files · 1 commits · open 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 #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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:27:37 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 06:18 UTC · original · edited 2026-11-26 07:20 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-26 06:18 UTC · [original](https://github.com/Marvisak/VIctory/pull/3053#issuecomment-7184301292) · edited 2026-11-26 07:20 UTC <!-- victory-triage v1 head=3069e4ff9f568bcb0c97c46a40346a8a0a066830 verdict=needs-work at=2026-11-26T07:20:26Z --> 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](https://claude.ai/code)_
Author
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-26 06:43 UTC · original

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** on GitHub · 2026-11-26 06:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/3053#issuecomment-7184719948) 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.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 05:45 UTC · original · approved these changes

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 05:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/3053#pullrequestreview-6069120712) · **approved these changes** 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.
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-26 06:01 UTC · original · approved these changes

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.

**@⁠kotekompotek** on GitHub · 2026-11-26 06:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/3053#pullrequestreview-6069248837) · **approved these changes** 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.
Author
Owner

@⁠Jackster on GitHub · 2026-11-26 16:24 UTC · original

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.

**@⁠Jackster** on GitHub · 2026-11-26 16:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/3053#issuecomment-7197614213) 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.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3053/head:github-bot/gh-3053'
git switch 'github-bot/gh-3053'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3053'
git switch 'github-bot/gh-3053'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3053'
git switch 'github-bot/gh-3053'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3053'
git switch 'main'
git merge --squash 'github-bot/gh-3053'
git switch 'main'
git merge --ff-only 'github-bot/gh-3053'
git switch 'main'
git merge 'github-bot/gh-3053'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3053
No description provided.