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

Open
0xkn0wh3r3 wants to merge 1 commit from upstream-pr/3053 into main
0xkn0wh3r3 commented 2026-11-26 05:35:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-26T05:35:27Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:5504624678 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3053). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-26T05:35:27Z. --- 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.
fuzeofficlal commented 2026-11-26 05:45:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T05:45:14Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6069120712 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3053#pullrequestreview-6069120712). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T05:45:14Z. **Review:** APPROVED --- 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 commented 2026-11-26 06:01:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T06:01:24Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6069248837 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3053#pullrequestreview-6069248837). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-26T06:01:24Z. **Review:** APPROVED --- 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 2026-11-26 07:01:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T06:18:31Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7184301292 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3053#issuecomment-7184301292). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T06:18:31Z. --- <!-- 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)_
0xkn0wh3r3 commented 2026-11-26 07:01:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-26T06:43:36Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7184719948 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3053#issuecomment-7184719948). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-26T06:43:36Z. --- 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.
Jackster commented 2026-11-26 16:24:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T16:24:53Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7197614213 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3053#issuecomment-7197614213). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T16:24:53Z. --- 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.
djasdh commented 2026-11-28 18:09:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by djasdh on 2026-11-28T18:09:46Z.


A data point on the "titles with hooks are unaffected" note, from GTA VI.

Setup: RX 6900 XT (RADV, Mesa 26.3.1), Linux, Wine 11.0, 1920x1080, Series S profile.

This build of the title has no __XMemAllocationHooks__: it reaches XMemAllocDefault, so its committed resources' memory (allocator 128) lands on the host heap. One 21 MB buffer (0x1412200 bytes) was copied whole twice a frame:

  • before: copied whole 2.0x/frame, 42.09 MB/frame; traffic: 21.05 MB up / 21.04 MB fetched
  • after a write-watched reservation for the same blocks: copied whole (memory not watched) 0 ranges; traffic: 0.00 MB up / 21.04 MB fetched; frame copies 109.8 -> 84.5 MB

The fetched side is unchanged (the shaders do write it); only the redundant host-to-card copy goes. So with empty hooks this title does take the path, and the win is on a real frame, not only the demo.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7243578005 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3053#issuecomment-7243578005). Originally posted by [djasdh](https://github.com/djasdh) on 2026-11-28T18:09:46Z. --- A data point on the "titles with hooks are unaffected" note, from GTA VI. Setup: RX 6900 XT (RADV, Mesa 26.3.1), Linux, Wine 11.0, 1920x1080, Series S profile. This build of the title has no `__XMemAllocationHooks__`: it reaches `XMemAllocDefault`, so its committed resources' memory (allocator 128) lands on the host heap. One 21 MB buffer (`0x1412200` bytes) was copied whole twice a frame: - before: `copied whole 2.0x/frame, 42.09 MB/frame`; `traffic: 21.05 MB up / 21.04 MB fetched` - after a write-watched reservation for the same blocks: `copied whole (memory not watched)` 0 ranges; `traffic: 0.00 MB up / 21.04 MB fetched`; frame copies 109.8 -> 84.5 MB The fetched side is unchanged (the shaders do write it); only the redundant host-to-card copy goes. So with empty hooks this title does take the path, and the win is on a real frame, not only the demo.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3053:upstream-pr/3053'
git switch 'upstream-pr/3053'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3053'
git switch 'upstream-pr/3053'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3053'
git switch 'upstream-pr/3053'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3053'
git switch 'main'
git merge --squash 'upstream-pr/3053'
git switch 'main'
git merge --ff-only 'upstream-pr/3053'
git switch 'main'
git merge 'upstream-pr/3053'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!3051
No description provided.