diff --git a/filament/backend/src/metal/MetalDriver.mm b/filament/backend/src/metal/MetalDriver.mm index c65922fa782..29a0f6fed90 100644 --- a/filament/backend/src/metal/MetalDriver.mm +++ b/filament/backend/src/metal/MetalDriver.mm @@ -821,17 +821,24 @@ } 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); } }