From 0c2d23e55cd487986336fa33c6d005dbfd789c18 Mon Sep 17 00:00:00 2001 From: Ben Doherty Date: Fri, 1 Nov 2024 14:16:54 -0700 Subject: [PATCH] Metal: unbind SwapChain upon destruction (#8244) --- filament/backend/src/metal/MetalDriver.mm | 29 ++++++++++++++--------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/filament/backend/src/metal/MetalDriver.mm b/filament/backend/src/metal/MetalDriver.mm index c65922fa78..29a0f6fed9 100644 --- a/filament/backend/src/metal/MetalDriver.mm +++ b/filament/backend/src/metal/MetalDriver.mm @@ -821,17 +821,24 @@ void MetalDriver::destroyRenderTarget(Handle rth) { } void MetalDriver::destroySwapChain(Handle sch) { - if (sch) { - auto* swapChain = handle_cast(sch); - // If the SwapChain is a pixel buffer, we need to wait for the current command buffer to - // complete before destroying it. This is because pixel buffer SwapChains hold a - // MetalExternalImage that could still being rendered into. - if (UTILS_UNLIKELY(swapChain->isPixelBuffer())) { - executeAfterCurrentCommandBufferCompletes( - [this, sch]() mutable { destruct_handle(sch); }); - } else { - destruct_handle(sch); - } + if (UTILS_UNLIKELY(!sch)) { + return; + } + auto* swapChain = handle_cast(sch); + if (mContext->currentDrawSwapChain == swapChain) { + mContext->currentDrawSwapChain = nullptr; + } + if (mContext->currentReadSwapChain == swapChain) { + mContext->currentReadSwapChain = nullptr; + } + // If the SwapChain is a pixel buffer, we need to wait for the current command buffer to + // complete before destroying it. This is because pixel buffer SwapChains hold a + // MetalExternalImage that could still being rendered into. + if (UTILS_UNLIKELY(swapChain->isPixelBuffer())) { + executeAfterCurrentCommandBufferCompletes( + [this, sch]() mutable { destruct_handle(sch); }); + } else { + destruct_handle(sch); } }