fix(agc): destroy buffers and memory on MoltenVK only while the graphics queue is idle #10062
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10062"
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 imellag on 2026-10-23T07:44:10Z.
What
On MoltenVK, buffers and device memory that the AGC driver releases are now destroyed only while the graphics queue is idle. This works around KhronosGroup/MoltenVK#2876 (open; MoltenVK 1.4.2 and
main), which loses the device on Stray (PPSA02100) about 40 to 100 s into the game.MoltenVK adds every
VkBuffercreated withSHADER_DEVICE_ADDRESSusage to a device-wide list atvkCreateBuffer, andMVKDevice::encodeGPUAddressableBufferspasses the whole list touseResourcesin every command buffer that binds a shader using device addresses. Its command buffers do not retain resources. So:kIOGPUCommandBufferCallbackErrorInvalidResource; underMTL_DEBUG_LAYER=1,The following Metal object is being destroyed while still required to be alive by the command buffer);MTLBuffer, and Metal validation assertsresource (at index: N) should not be nilinuseResources.The only safe moment is when no command buffer that was encoded while the buffer existed is pending: the queue is idle. The change:
Graphics/src/DeferredDestruction.cpp: on MoltenVK the graphics context gets a device resolver that returns queuing versions ofvkDestroyBufferandvkFreeMemoryand forwards every other name. Everything that destroys through the context (the buffer pool,Buffer,DeviceBuffer, guest buffer memory imports, unit shadows) is covered without touching those call sites.Recorder::SubmitandCommandBatch::Submitdrain the queue before theirvkQueueSubmit, under the GPU mutex that every graphics-queue submission holds, once 512 objects wait or the oldest has waited 100 ms:vkQueueWaitIdle, then the buffers, then the memory.vkQueueWaitIdlealso waits for MoltenVK's asynchronous encoding, so nothing can still read the list.vkDestroyDevice.VK_DRIVER_ID_MOLTENVKonly; other drivers keep destroying at once.APS5_NO_DEFERRED_DESTRUCTION=1turns it off (row indocs/dev/ENVIRONMENT.md). The debt entry states the cost: each drain stalls the CPU until the GPU is idle, and memory released under pressure only returns at the next drain. It also says to remove this once a MoltenVK release fixes #2876.Tested
agc_deferred_destructiontest: destroys and frees are queued, other names pass through, a drain below both bounds does nothing, a drain past the age bound or at 512 objects waits for the queue and then destroys buffers before memory, a failed queue wait throws and keeps the objects, teardown destroys the rest without a queue wait, and destroys after teardown or on another device run at once. With the queueing disabled (deferalways returning false) it fails 6 of its checks.ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation,ctest -V). lavapipe is not MoltenVK, so this checks that other drivers are unchanged:mainef0d8ae3b54bee82086fagc_buffer_pool(and its two small-device variants),agc_command_batch_failure,agc_driver_graphics,agc_driver_pooled_buffer_rangeagc_deferred_destructionmainwas not found but was requested/VUID-maindoes not build on macOS yet):agc_deferred_destructionandagc_buffer_poolpass. Stray, pressing cross into the game,MTL_DEBUG_LAYER=1:MVKMetalComputeCommandEncoderState::prepareComputeDispatchwithin 40 to 100 s; Metal names a 12-byte buffer destroyed fromBuffer::Bufferwhen the pool rebinds reused memory. With only the per-fence deferral described above, it names buffers and imported guest memory destroyed on the release thread after their own batch's fence.getMTLBuffer()is nil) touseResources:vkCreateBufferadds it to the list beforevkBindBufferMemory. AnyPS5 creates buffers on threads without the GPU mutex, so this cannot be closed from here; the local MoltenVK fix skips nil buffers inencodeGPUAddressableBuffers. I will report it on #2876.trace_digest.pypair (a separate UE4 asset-loading failure after the first checkpoint, timing-dependent, is being investigated).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)