Skip to content

feat(gpu): add opt-in mach_vm_remap zero-copy title buffer aliasing - #207

Merged
pragyaangaur merged 1 commit into
pragyaangaur:mainfrom
PlutoDeveloper:feat/macos-alias-buffers
Nov 26, 2026
Merged

pragyaangaur merged 1 commit into
pragyaangaur:mainfrom
PlutoDeveloper:feat/macos-alias-buffers

Conversation

@PlutoDeveloper

Copy link
Copy Markdown
Contributor

Summary

Implements opt-in zero-copy title buffer aliasing on Apple Silicon + D3DMetal using raw mach_msg2_trap / mach_vm_remap (VM_FLAGS_FIXED | VM_FLAGS_OVERWRITE, copy = FALSE), based on the probe and approach documented by @Ahmet-Dedeler in #204.

No game files were used for any of this.

What changed

  • crates/victory-gpu/src/alias.rs:
    • Added mach module calling mach_task_self, mach_reply_port (cached in OnceLock to avoid leaking ports), and mach_vm_remap (msgh_id = 4813) via mach_msg2_trap with MACH64_SEND_KOBJECT_CALL on x86_64.
    • Added enabled() gated behind VICTORY_ALIAS_BUFFERS=1 on Rosetta + non-WARP, plus a one-time supported() probe that verifies mach_vm_remap onto a write-back L0 shared_buffer and checks whether fresh buffers are zero-initialized (falling back cleanly to shadow copies if unsupported).
    • Added alias_region(base, len, preserve_contents) and unalias_region(base) plus 6 synthetic tests covering CPU↔GPU coherence, overlapping texture tracker notification, widened RGB32 view refresh, VirtualQuery + VirtualProtect write-guard VEH, unalias_region + VirtualFree, and switch gating.
  • crates/victory-gpu/src/shadow.rs:
    • Added Shadow::new_aliased and an ALIASED region registry wired into find, all, and near.
    • Added zero-copy fast paths in clean, update, writing, copy_back_or_not, copy_back_marked, and copy_back_page while preserving widened RGB32 view refreshes and notifying overlapping texture trackers.
  • crates/victory-gpu/src/watch.rs: Added notify_written to mark overlapping texture/alias trackers when GPU shaders write aliased title memory.
  • crates/victory-gpu/src/bindless.rs & crates/victory-gpu/src/memory.rs: Skip clean shadows (!shadow.clean()) before queuing refreshes.
  • crates/victory-gpu/src/lib.rs & crates/victory-hle/src/modules/xmem.rs: Exported alias_buffers, alias_region, unalias_region and hooked XMemVirtualAlloc / XMemFreeDefault when VICTORY_ALIAS_BUFFERS=1.

Testing

Tested on Apple M4 (16 GB), macOS 27.2.1, CrossOver 26.2 + D3DMetal 3.0 (no game files used):

  • cargo xtask policy: 244 files ok
  • cargo clippy --target x86_64-pc-windows-gnullvm -p victory-gpu -p victory-hle --all-targets: 0 warnings in touched files
  • cargo test --workspace (native): 519 passed, 0 failed, 17 ignored
  • tools/macos/test-wine.sh -p victory-gpu --lib -- --test-threads=1: 305 passed, 0 failed, 9 ignored (all 6 new alias::tests passed)
  • VICTORY_ALIAS_BUFFERS=1 tools/macos/test-wine.sh -p victory-hle --lib xmem -- --test-threads=1: 16 passed, 0 failed, 0 ignored

Mentioning #204 without closing words so testers with a game dump can validate in-game under VICTORY_ALIAS_BUFFERS=1 first.

Copilot AI balanced review requested due to automatic review settings November 26, 2026 03:24

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@derz92

derz92 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

A first in-game data point for VICTORY_ALIAS_BUFFERS=1, since the docs say it has only synthetic tests so far. Setup: M5 Max, macOS 26, D3DMetal 4.0 beta 3, Series X profile, fault tracking on. The base was current main (including #278 and #275, both off) plus #280, #262 and #282.

The aliasing came up:

zero-copy title buffer aliasing active (mach_vm_remap) zeroed=true
large title buffer aliased zero-copy base=0x143800000 len=1228931072
large title buffer aliased zero-copy base=0xe9ee00000 len=2305818624
large title buffer aliased zero-copy base=0x100400000 len=590348288

But the run never got past the loading artwork:

  • Slow frames: loading-screen frames (11 draws) took 10.6 to 11.1 s each, with 2.0 to 5.0 s of that on the GPU. Without the switch, the same screen takes about 5 ms on the GPU.
  • Then a stall: no frame was presented for 5 minutes, and I stopped it. No host GPU hang and no exception were logged.

The same build without the switch reaches the world in about 6 minutes. So GPU reads through the remapped pages may be very slow under D3DMetal (uncached, or faulting per page?). I'm happy to rerun with any extra logging you'd like.

🤖 Generated with Claude Code

@Ahmet-Dedeler

Copy link
Copy Markdown

@derz92 thanks for running it in game, that's the test this needed.

My best guess is the guards. On main, a GPU write into aliased memory goes through watch::notify_written, which calls changed(), and changed() ends with open_guarded. So with fault tracking on, every aliased block the GPU writes gets its protection opened, and the next take guards it again: two Wine protection calls per run of blocks, every frame, plus the faults when the title touches them again. With ~3.4 GB aliased and the GPU writing all over it, that can add up fast.

The version I have that works in game does it differently. A GPU write into aliased memory only marks the blocks as written in the trackers: no guard opened, no bytes saved (the write already happened, there's no "before" to keep), and the CPU side keeps its protection, so the title's own writes are still seen. That's the same idea as Marvisak/VIctory@29decb3 (watch::gpu_wrote), and it was a big difference for me.

Cheap way to check before changing anything: a diagnostics run with the switch on that counts protection changes per frame. If they jump with VICTORY_ALIAS_BUFFERS=1, it's this.

@derz92

derz92 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

A correction to my run above. That build included #279, and I later found that #279 alone stops this title on the loading artwork here: three runs froze with it, including one with no experimental switches, and runs with it reverted reach the world (details on #279). So the loading freeze I reported is most likely #279, not aliasing. The 10-11 s loading-screen frames with 2-5 s on the GPU are the only part I can't attribute yet.

I'm rerunning VICTORY_ALIAS_BUFFERS=1 now on main with #279 reverted, same Mac, and will post the result here and on #322.

🤖 Generated with Claude Code

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants