fix(agc): make host imports over writable pages on macOS #5101
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5101"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Zheorgyan on 2026-10-13T11:40:55Z.
What
Metal keeps the protection a page had when it was imported. a host import made while the write watch has the guest pages read-only drops every GPU store into it, also after the protection is lifted, so render targets and storage images written back through an import kept their old contents on macOS.
ImportWatchednow opens the range withGuestArena::HostWritefor the import call only, and the watch protects it again after, so the import stays watched. the pages the open marks as written are compared with the copy taken before the import, so an import that stores nothing is not taken for a CPU storethe approach and the standalone Metal repro are mrzacarias's, see the comment below. other hosts are unchanged
Tested
macos-portatb7eb6b6b, the same open around the import. theagcGPU tests with-j1 --timeout 30, without the flip, display andlds_atomics64tests: 23 fail before, 13 after. fixed aresrgb8_color_target,packed_unorm_color_target,swap_alt_blend,alpha_to_coverage,dual_source_blend,array_layer_target,storage_one_slice_alias,depth_compare_copy,cross_workgroup_flagandagc_unchanged_cpu_stamps, none added.dcc_keys_first_writestill fails there with "an unchanged pending image was uploaded again": that branch predates the import window compare from #3874 that this relies on, and I could not runmainitself on macOSlibSceAgcDriverbuilds, the change is macOS onlytools/check_conventions.pypassesChecklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by mrzacarias on 2026-10-13T14:09:27Z.
Same here on a different setup: M1 Pro, macOS 26.4.1, native arm64 through the FEXCore runner, MoltenVK 1.4.2. It is Metal, not Rosetta: a standalone Metal program gives the same result built as arm64 and as x86-64.
PROT_READpages, then fill: the fill is lost with the command buffer completed (status 4), and it stays lost aftermprotect(PROT_READ | PROT_WRITE).PROT_READandPROT_READ | PROT_WRITEbefore each fill: every fill lands, including while the pages are read-only.So only the protection at the moment of the import matters, which leaves room to keep imports watched instead of comparing them: open the range for the import call only and let the watch protect it again after. That is what I run locally in
ImportWatched:With it, Balatro, God of War Sons of Sparta, Vampire Crawlers and Rogue Prince of Persia render and keep imported ranges watched. On an earlier branch with its own mprotect watch, God of War without the open stalled at about 0.4 FPS, the guest waiting on labels the GPU never wrote. I have not run the
agctests with this variant, so I can't say it fixes the same 10.Repro (
clang -fobjc-arc -framework Metal -framework Foundation repro.m):Imported from boykopovar/AnyPS5. Originally posted by Zheorgyan on 2026-10-13T19:10:35Z.
@mrzacarias thanks, that is better, the imports stay watched. I will run the
agcset on the M1 Pro with it and switch this PR if the same tests passImported from boykopovar/AnyPS5. Originally posted by Zheorgyan on 2026-10-13T20:10:10Z.
@mrzacarias switched to your open around the import, with you as co-author. on the M1 Pro with
macos-portatb7eb6b6bit fixes the same tests plusagc_unchanged_cpu_stamps.dcc_keys_first_writestill fails there because that branch has no import window compare from #3874 yet, onmainthe unchanged pages should be filtered outImported from boykopovar/AnyPS5. Originally posted by krishalivaibhav on 2026-10-13T20:28:02Z.
Additional independent M2 reproduction of the protected-import ordering problem, using our older local macOS integration (base
197ffac97203plus unpublished macOS runtime patches), x86-64 under Rosetta and MoltenVK. This is not a build/test of the exact #5101 head.I added a small diagnostic using the actual host-pointer import helper. It fills a 16 KiB imported buffer with
0x02020202, records a transfer-write → host-read barrier, waits for the real fence, and checks every byte, counting incorrect 4 KiB regions. Both submissions completed successfully:I then adapted the ordering to our older
ImportWatchedAPI: collect/stamp pending CPU writes under the tracker lock, remove write-watch protection, then invoke the Vulkan import callback. The control now uses that actual production tracker path rather than unregistering protection directly. It passed three consecutive runs, each withfailure=none,result=0,pages=4,wrongGpuWritePages=0. Watch counts in the unwatched control are explicitly unmeasured.This independently supports the ordering diagnosis on M2. It does not establish an FPS improvement, stable game sessions, or validation of all changes in this PR. I have not opened a duplicate upstream implementation PR.
AI-assisted: yes (Codex; low-reasoning subagent implementation, parent review and execution).
Imported from boykopovar/AnyPS5. Originally posted by yripper on 2026-10-14T01:25:22Z.
Data point from macOS 26 on an M1 Pro (Rosetta 2, MoltenVK), God of War: Sons of Sparta (PPSA28997): I hit the same Metal refusal. A single write-watched page inside a label range made the no-copy import fail with
VK_ERROR_OUT_OF_DEVICE_MEMORY, so the range was copied and every label store drained the GPU, about 900 drains a second. Making the import succeed took the title screen at 1080p from 19 to 24 FPS (GPU idle per frame 13.7 → 2.3 ms, GPU mutex waits 928 → 28 ms per 10 s). I fixed it one level higher, by releasing the watched pages before the import, but your approach inImportWatchedis more precise, so I'll drop mine and test this PR on my branch.Imported from boykopovar/AnyPS5. Originally posted by yripper on 2026-10-14T22:19:44Z.
Follow-up: I merged
mainwith this PR and dropped my workaround. On PPSA28997 (M1 Pro, Rosetta 2, MoltenVK) the label ranges now import, with 0 device drains per packet and 0 labels left unimported in a 10 s window, the same result my workaround gave. Thanks!