MVKGPUCapture: Make sure the MTLCaptureScope has only one reference.
For some reason, when we create the `MTLCaptureScope` with capture
enabled, in addition to the reference from object creation, Metal
retains the object for no apparent reason in the base `MTLCaptureScope`
initializer. This causes us to leak the capture scope--and with it, the
queue. Let's hope Apple isn't doing anything with that extra reference.
diff --git a/MoltenVK/MoltenVK/OS/MVKGPUCapture.mm b/MoltenVK/MoltenVK/OS/MVKGPUCapture.mm
index bcffe97..d72a792 100644
--- a/MoltenVK/MoltenVK/OS/MVKGPUCapture.mm
+++ b/MoltenVK/MoltenVK/OS/MVKGPUCapture.mm
@@ -61,6 +61,14 @@
if (mvkOSVersionIsAtLeast(kMinOSVersionMTLCaptureScope)) {
_mtlCaptureScope = [[MTLCaptureManager sharedCaptureManager] newCaptureScopeWithCommandQueue: _mtlQueue]; // retained
_mtlCaptureScope.label = @(_queue->getName().c_str());
+ // Due to a retain bug in Metal when the capture layer is installed, capture scopes
+ // can have too many references on them. Release the excess references so the scope--
+ // and the command queue--aren't leaked. This is a horrible kludge that depends on
+ // Apple not taking internal references to capture scopes, but without it, we could
+ // get hung up waiting for a new queue, because the old queues are still outstanding.
+ while (_mtlCaptureScope.retainCount > 1) {
+ [_mtlCaptureScope release];
+ }
}
}