metal: Tie drawable's MTLTexture lifetime to MTLCommandBuffer

By default we define QRHI_METAL_COMMAND_BUFFERS_WITH_UNRETAINED_REFERENCES,
which means we create MTLCommandBuffer via commandBufferWithUnretainedReferences.

In this case, if Metal API validation diagnostics is enabled in Xcode,
the texture is released before the command buffer is done with it, so
we manually ensure the lifetime of the texture extends until the command
buffer is complete.

Pick-to: 6.7
Change-Id: I9b9efa96b4a004f43e0b72144aafc4b440c7fbb4
Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io>
bb10
Tor Arne Vestbø 2024-04-18 12:44:58 +02:00
parent da508c2c24
commit 94e83aa7d7
1 changed files with 10 additions and 0 deletions

View File

@ -2402,6 +2402,16 @@ QRhi::FrameOpResult QRhiMetal::endFrame(QRhiSwapChain *swapChain, QRhi::EndFrame
dispatch_semaphore_signal(swapChainD->d->sem[thisFrameSlot]);
}];
#ifdef QRHI_METAL_COMMAND_BUFFERS_WITH_UNRETAINED_REFERENCES
// When Metal API validation diagnostics is enabled in Xcode the texture is
// released before the command buffer is done with it. Manually keep it alive
// to work around this.
id<MTLTexture> drawableTexture = [swapChainD->d->curDrawable.texture retain];
[swapChainD->cbWrapper.d->cb addCompletedHandler:^(id<MTLCommandBuffer>) {
[drawableTexture release];
}];
#endif
const bool needsPresent = !flags.testFlag(QRhi::SkipPresent);
const bool presentsWithTransaction = swapChainD->d->layer.presentsWithTransaction;
if (!presentsWithTransaction && needsPresent) {