perf(agc): trap CPU reads of label readbacks on macOS instead of storing them #13499
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/13499"
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 Gonic0 on 2026-10-30T17:03:47Z.
What
Builds on #13442. Until it merges, this branch carries its commits; only
2cf5b41ec8and0fdd66d900are new here.StorageTexture::StoreReadbackBeforeLabelwrites back every pending linear or small storage image before each label, whether or not the CPU reads it. On macOS this PR traps CPU reads of such an image with #13442's write-watch read traps instead of storing it, so the store happens only when something reads or writes its memory.GuestWriteWatchTrapReadsover its exact range instead of its store. The alias check is a new libkernel export,DirectMemoryAliased_nid_postfix, which tells whether any of a range's physical memory is mapped by another direct mapping. If the trap cannot be set (unwatched memory, a host write in progress), the image is stored as before. #13442 makes a trap of an already trapped range a no-op, so the same range can be trapped at every label without the driver tracking it.GuestMemory::FlushGpuWritesover the trap's range on the write watch's resolver thread. That is the host-read hook every other reader of guest memory uses: it stores the pending results and waits until they are in memory. While a queued draw still holds reads of the range (DrawPipeline::HeldReads, which makes the store wait), it retries.writeBackLayers, soFlushPending, cache eviction (Flush),WriteBackand refreshes all do. A store that copies into the import on the GPU releases it once the copy is recorded, so a read that faults before then waits for the store. A store through the CPU releases it before its first access to guest memory, together with the traps of other images on the same pages; those images stay pending and are trapped again at the next label. A trapped image is stored whole.APS5_NO_LABEL_READ_TRAP=1keeps the store at the label. Linux and Windows are unchanged.Gaps, in the new entry of
docs/dev/debt/libSceAgcDriver.md:GpuMutexand touches a trapped image waits for the resolver forever;EFAULT;The page rule keeps the storing thread from faulting on a page another image's trap covers. If it did, the resolver would wait for that store while the store waits for the resolver.
Tested
Apple M5 Pro, macOS 27.2, MoltenVK from Vulkan SDK 1.4.363, x86-64 Release under Rosetta 2.
mainalone does not build on macOS, so the tests and the game ran in a tree with the open macOS port PRs, where these files are identical.agc_small_tiled_shadow_label(agc_driver_recorder_tests --small-tiled-shadow-label-only) gets a trap check on macOS. After the image is written again on the GPU:FlushPendingstores it and gives the page back;FlushPendingwhile the test holdsGpuMutex, which takes the image out of the pending registry and waits. The page is still without access; a read started then sees the stored results once the test releases the mutex.Two 8x8 linear images on one page (both architectures' page size):
FlushPendingwith the reasonidle, which takes the CPU path) completes, stores the second and gives the page back. The next label traps the first again, and a read sees its results.The test thread releases
GpuMutexaround the read, since the resolver needs it. The test passes with and withoutAPS5_NO_LABEL_READ_TRAP=1.Negative controls, each of which fails it:
writeBackLayers: "storing a trapped image did not release its trap";FlushPendingtakes the image out of the registry, as before: "a trapped image taken for a store gave back its pages before the store";FlushPendingwithout waiting: "a read of a trapped image did not see its pending results".The 82 CTest entries of
agc_driver_recorder_testspass, except one skip andagc_recorder_failed_check, which fails the same way withAPS5_NO_LABEL_READ_TRAP=1. The write-watch tests pass on x86-64 and arm64 (#13442). The whole-image store of a trapped image and the alias check are not covered by a test: these images have one layer, the recorder test's memory is not direct memory, andguest_memory_testsdoes not build on macOS (sceKernelMunmap).PPSA21564 (ASTRO BOT 01.007.000), the logos before the intro.
maindoes not build on macOS without the open port PRs, so the base is that tree with #13220 and #13442 (5b2b0eb50830), and the change is the same tree with this commit'sTexture.cpp(1f0553384a68). Captures withAPS5_TRACE_FRAMES=150:240and no other trace or profile switch, alternated base, change, base, change:5b2b0eb508301f0553384a685b2b0eb508301f0553384a68Summary lines of the second round. Base:
This PR:
Traces: base, this PR.
trace_digest.py, base (second round)frames 150:240 from present 150: 240 presents, frame p50 26.0 / p90 28.1 / p99 30.3 / max 55.4 ms, GPU busy 19.5 ms per present
Apple M5 Pro, MoltenVK 1.4.2, Darwin 27.2.0, 3840x2160, commit 5b2b0eb50830, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 73.1, Driver.Packet.memory_wait 46.9, batch 19.5, Driver.Packet.dispatch 15.3, Driver.Dispatch 13.6, VideoOut.Flip 6.6, Driver.WorkerWait 6.3, Vulkan.DispatchRecipe 5.7, Vulkan.DispatchRecipe.command_record 5.7, Driver.Present 5.5
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
trace_digest.py, this PR (second round)frames 150:240 from present 150: 240 presents, frame p50 20.6 / p90 22.6 / p99 25.1 / max 42.5 ms, GPU busy 11.9 ms per present
Apple M5 Pro, MoltenVK 1.4.2, Darwin 27.2.0, 3840x2160, commit 1f0553384a68, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 56.2, Driver.Packet.memory_wait 36.9, batch 11.9, Driver.Packet.dispatch 11.9, Driver.Dispatch 10.9, Vulkan.DispatchRecipe 6.7, Vulkan.DispatchRecipe.command_record 6.7, VideoOut.Flip 6.5, Driver.WorkerWait 6.3, Driver.Present 5.5
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Over whole 120 s runs, counting the frames dumped every 60 presents, the same comparison gives 44.0 and 44.3 FPS against 38.1 and 37.1 with
APS5_NO_LABEL_READ_TRAP=1. With0fdd66d900it gives 44.2 and 44.7 against 38.2, 37.8 and 37.8; one more run with the traps, the first after the rebuild, gave 22.3. The image is the same. In an earlier instrumented run of this design, no trap was resolved by a read in that scene: the emulator itself stored every trapped image, about 65 times a second, before any guest read.Validation: the 82 CTest entries of
agc_driver_recorder_testswithVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ANYPS5_NO_SHADER_CACHE=1andANYPS5_REQUIRE_VULKAN=1(ctest -V) on the base and on this PR report the same 152VUID-VkDeviceCreateInfo-pProperties-04451lines (the test device does not enableVK_KHR_portability_subseton MoltenVK, #10920) and no otherVUID-. Neither log reports the layer as not found.Not tested on a title that reads these images from the CPU.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T17:06:13Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The PR reports a performance improvement against its opt-out mode, but the repository requires captures from
mainand the change before that claim can be checked.What I ran
Reviewed head
a485eb515e3021a565d66222bfe3eceb883e933dagainst basea026562629da7bcc7af781639a11ab0647a524dd. The dependency stack preview reports a clean merge preview for this head against the current base. Environment: Linux x86-64, Debian 12, Python 3.11.2, GCC 12.2.0.Results
main/change summary lines, attached traces, ortrace_digest.pyoutputRaw output
The precheck's ABI-audit and test-quality helper steps were not executable in its isolated PR snapshot because those local helper scripts are absent there; these are not reported as repository failures.
Findings
Blocking
APS5_NO_LABEL_READ_TRAP=1, both on the PR stack. It does not provide captures frommainand the change, nor the required trace artifacts and digest. The repository rule requires those paired captures for a speed claim. After dependency #13442 is in the baseline tree, add summary lines from the same title, scene, and machine, attach both traces, and include thetools/trace_digest.pyoutput. Repository performance-report rule · Performance report formNon-blocking
Not verified
To get this to a good state
After #13442 is in the baseline tree, add the matched
mainand change captures and the required artifacts. I’ll re-check the updated head and its evidence.