fix(agc): fall back to device-local heap mirrors #8016
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8016"
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 PayloadAnim on 2026-10-19T17:45:54Z.
What
When the host-visible heap-mirror allocation fails after the existing reclaim and retry path, retry the same contiguous shader BDA in device-local memory. A bounded 16 MiB host staging buffer handles initial fill, write-watch refresh, and GPU write-back with queue-ordered copies and fence waits. The allocation-success path is unchanged.
Tested
Windows, MinGW GCC 15.2: directly compiled GuestBufferMemory.cpp and Resources.cpp after rebasing. The conventions check and git diff --check passed. No GPU runtime test was run; the full Ninja target did not complete in this environment. The PR overlap script could not run because gh is unavailable; a targeted GitHub search found no matching open heap-mirror PR.
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)Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-21T00:14:55Z.
@xxLnolan Thanks for taking on #7047. A few things before this can go in:
mirror->context = &contextkeeps a pointer to theContextthatacquireMirrorreceived by reference, which is the callingGuestBufferMemory's own member. Mirrors live in the sharedMirrors()cache and are used later through*mirror.contextincompareBlocks,takeHeapReferencesandWriteBack, so that pointer can outlive its owner. Please store a copy of theContext, asBufferdoes.Buffer's default memory, host-visible and coherent, which is the heap whose exhaustion triggers this fallback. The refresh path also allocates a fresh 16 MiB one each time. Please allocate one staging buffer up front (or before falling back), and handle the case where even that fails.CommandBatch(context, false)andSubmitAndWait(false)skip submitting and reaping the active recorder. Could you explain in the description why the readbacks intakeHeapReferencesandWriteBackstill see every earlier GPU write, and why the refresh copy may run ahead of recorded but unsubmitted work?Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-21T11:35:50Z.
@xxLnolan thanks for the update. On
b220f80ce8:min(16 MiB, mirror bytes), or share one staging buffer per context?Mirrors(). What guarantees that two transfers on the same mirror (refresh,takeHeapReferences,WriteBack) never run at the same time? If it'sGuestMemory::GpuMutex(), a short comment attransferHeapMirrorsaying so would help.takeHeapReferencesandWriteBacksee every earlier GPU write whenSubmitAndWait(false)skips the active recorder, and why the refresh copy may run ahead of recorded but unsubmitted work.Imported from boykopovar/AnyPS5. Originally posted by PayloadAnim on 2026-10-21T15:28:09Z.
Thanks for the review. I’ve updated the patch:
The staging buffer is now sized to min(16 MiB, mirror bytes).
Heap mirror transfers now require GuestMemory::GpuMutex(), enforced with a runtime check. The repository instructions prohibit adding code comments, so I used the check to make the synchronization requirement explicit.
Transfer batches now submit the active recorder before submitting the copy batch. Both use the same queue, and the copy batch waits on its fence. This orders prior recorded work before refreshes and readbacks; SubmitAndWait(false) skips reaping recorder callbacks, not queue ordering.
I tested the change in a game, and it works. I haven’t added the requested automated test covering fill, refresh, write-back, and a sparse mirror, or tested on a Radeon 780M.
Imported from boykopovar/AnyPS5. Originally posted by simnJS on 2026-10-21T16:28:17Z.
NVIDIA, Windows. GeForce RTX 4080 SUPER, driver 616.92 (32.0.16.1692), Windows 11 Pro 26200, Ryzen 9 7950X3D, WinLibs GCC 15.2.0 r7, Release.
Head
c83a53887merged intomaincecb540a6(clean merge), and the head alone.The PR adds no test, so I picked these existing ones:
agc_driver_graphics. Its BDA resource part (tests/BdaResources.cpp) builds heap mirrors on mock Vulkan. It covers fill, refresh after CPU writes, writable and sparse write-back, and theAPS5_HEAP_MIRROR_MIBbudget. It also covers a heap mirror whose host-visible allocation the mock refuses, which is thecatchbranch this PR changes.agc_space_pending_writes,agc_storage_written_buffer_order,agc_driver_vertex_target_range_copiedandagc_driver_vertex_target_range_imported. On the GPU they buildGuestBufferMemoryover registered allocations and write back through it, and theirStorageTextureuploads and write-backs go throughCommandBatch. They do not show whether a heap mirror was built.ANYPS5_GPU=NVIDIA ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1,ctest --test-dir build -R "^(agc_driver_graphics|agc_space_pending_writes|agc_storage_written_buffer_order|agc_driver_vertex_target_range_copied|agc_driver_vertex_target_range_imported)$" --output-on-failure --timeout 120 --repeat until-fail:3 -V.agc_driver_graphicswith the PR was repeated with--repeat until-pass:3.The four recorder tests print
Vulkan device: NVIDIA GeForce RTX 4080 SUPER.agc_driver_graphicsuses mock Vulkan and prints no device line. No test skipped.Resources.hpp,Resources.cpp,GuestBufferMemory.cppagc_driver_graphicsa refused heap mirror was allocated againagc_space_pending_writesagc_storage_written_buffer_orderagc_driver_vertex_target_range_copiedagc_driver_vertex_target_range_importedThe head alone (
c83a53887, not merged) failsagc_driver_graphicsthe same way, 0 of 3 (0.46, 0.38, 0.39 s):a refused heap mirror was allocated again. So the failure is not from the merge with currentmain.The failing check is
tests/BdaResources.cpp:2487. After the mock refuses the heap mirror's host-visible allocation (limitMemory), the test requires exactly one allocation attempt for that build. The check before it passed: the build failed with an error containing the mirror range andVulkan result -2. The attempt count did not match. Nousing device-local memory after host allocation failedline was printed in that run. The test stops at that check, so with the PR the sparse heap mirror tests after it did not run.The device-local fallback was not reached on this GPU. No run printed the fallback line, and
docs/dev/ENVIRONMENT.mdlists no variable that makes the host-visible allocation fail.APS5_HEAP_MIRROR_MIBrefuses withpast the budgetbefore the allocation. Not tested: a run with an exhausted host-visible heap, or on the #7047 device.Khronos validation layer not run: the Vulkan SDK is not installed on this machine.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-22T01:04:27Z.
@PayloadAnim thanks, the delta covers the review points: the staging buffer is
min(16 MiB, mirror bytes), transfers checkGuestMemory::GpuMutex(), andCommandBatch(context, true)puts the active recorder's work ahead of each transfer on the same queue.One thing blocks it now: the change fails one of
main's existing tests. simnJS's Windows run above shows it.agc_driver_graphicsfails 0 of 3 with the PR, both merged intomainand on the head alone, and passes 3 of 3 withmain's three files. The check istests/BdaResources.cpp:2576:That test pins
main's behaviour: when the mock refuses a heap mirror's host-visible allocation (limitMemory), the build fails after exactly one attempt. With the fallback, the same refusal now goes on to allocate the staging buffer and the device-local buffer. In the mock both are refused too, so the build still fails with -2, but after more than one attempt. These heap-mirror tests only run on Windows (#ifdef _WIN32; on Linux the section prints "heap mirrors not tested"), so a Linux run won't show it. Windows CI will.The same mock is also the way to get the test that's still missing. The fallback only runs after a real allocation failure, so CI can't reach it any other way. Could you:
BdaTestAccessfor that is fine.That test is the evidence for this PR, since nobody can force the path on a real GPU.
Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T02:55:34Z.
@PayloadAnim a data point for this PR from #9514's thread: parl0v ran Sword of the Sea (PPSA25344) 01.510.000 on Windows 10, with an RTX 4060 8 GB and 16 GB of RAM (comment). Once #9514 gets the New Game load past the copy cap, it stops on
heap mirror of 0x32af9e0000+0x100000: vkAllocateMemory buffer ... heap 1 7704/7448 MiB; heap mirrors hold 2858 MiB, andmainwithAPS5_ADDRESS_COPY_MAX_MIB=1024stops on the same heap. That's a host-visible heap mirror allocation failing after the pool's reclaim, the case this PR's fallback retries in device-local memory. I've asked parl0v whether they'd try #9514 with this PR.Pull request closed