MVKSwapchainImage always retrieve MTLTexture directly from CAMetalDrawable.
diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKImage.h b/MoltenVK/MoltenVK/GPUObjects/MVKImage.h
index c8c1c8b..c44f478 100644
--- a/MoltenVK/MoltenVK/GPUObjects/MVKImage.h
+++ b/MoltenVK/MoltenVK/GPUObjects/MVKImage.h
@@ -147,7 +147,7 @@
#pragma mark Metal
/** Returns the Metal texture underlying this image. */
- id<MTLTexture> getMTLTexture();
+ virtual id<MTLTexture> getMTLTexture();
/** Returns a Metal texture that interprets the pixels in the specified format. */
id<MTLTexture> getMTLTexture(MTLPixelFormat mtlPixFmt);
@@ -235,7 +235,7 @@
bool validateUseTexelBuffer();
void initSubresources(const VkImageCreateInfo* pCreateInfo);
void initSubresourceLayout(MVKImageSubresource& imgSubRez);
- virtual id<MTLTexture> newMTLTexture();
+ id<MTLTexture> newMTLTexture();
void releaseMTLTexture();
void releaseIOSurface();
MTLTextureDescriptor* newMTLTextureDescriptor();
@@ -300,6 +300,9 @@
#pragma mark Metal
+ /** Returns the Metal texture used by the CAMetalDrawable underlying this image. */
+ id<MTLTexture> getMTLTexture() override;
+
/**
* Presents the contained drawable to the OS, releases the Metal drawable and its
* texture back to the Metal layer's pool, and makes the image memory available for new use.
@@ -324,7 +327,6 @@
protected:
friend MVKSwapchain;
- id<MTLTexture> newMTLTexture() override;
id<CAMetalDrawable> getCAMetalDrawable();
void releaseMetalDrawable();
MVKSwapchainImageAvailability getAvailability();
diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm b/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm
index 8a16ef2..65637b5 100644
--- a/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm
+++ b/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm
@@ -298,34 +298,6 @@
return mtlTex;
}
-VkResult MVKImage::setMTLTexture(id<MTLTexture> mtlTexture) {
- lock_guard<mutex> lock(_lock);
- releaseMTLTexture();
- releaseIOSurface();
-
- _mtlTexture = [mtlTexture retain]; // retained
-
- _mtlPixelFormat = _mtlTexture.pixelFormat;
- _mtlTextureType = _mtlTexture.textureType;
- _extent.width = uint32_t(_mtlTexture.width);
- _extent.height = uint32_t(_mtlTexture.height);
- _extent.depth = uint32_t(_mtlTexture.depth);
- _mipLevels = uint32_t(_mtlTexture.mipmapLevelCount);
- _samples = mvkVkSampleCountFlagBitsFromSampleCount(_mtlTexture.sampleCount);
- _arrayLayers = uint32_t(_mtlTexture.arrayLength);
- _usage = getPixelFormats()->getVkImageUsageFlagsFromMTLTextureUsage(_mtlTexture.usage, _mtlPixelFormat);
-
- if (_device->_pMetalFeatures->ioSurfaces) {
- _ioSurface = mtlTexture.iosurface;
- CFRetain(_ioSurface);
- }
-
- return VK_SUCCESS;
-}
-
-// Creates and returns a retained Metal texture suitable for use in this instance.
-// This implementation creates a new MTLTexture from a MTLTextureDescriptor and possible IOSurface.
-// Subclasses may override this function to create the MTLTexture in a different manner.
id<MTLTexture> MVKImage::newMTLTexture() {
id<MTLTexture> mtlTex = nil;
MTLTextureDescriptor* mtlTexDesc = newMTLTextureDescriptor(); // temp retain
@@ -348,6 +320,32 @@
return mtlTex;
}
+VkResult MVKImage::setMTLTexture(id<MTLTexture> mtlTexture) {
+ lock_guard<mutex> lock(_lock);
+
+ releaseMTLTexture();
+ releaseIOSurface();
+
+ _mtlTexture = [mtlTexture retain]; // retained
+
+ _mtlPixelFormat = mtlTexture.pixelFormat;
+ _mtlTextureType = mtlTexture.textureType;
+ _extent.width = uint32_t(mtlTexture.width);
+ _extent.height = uint32_t(mtlTexture.height);
+ _extent.depth = uint32_t(mtlTexture.depth);
+ _mipLevels = uint32_t(mtlTexture.mipmapLevelCount);
+ _samples = mvkVkSampleCountFlagBitsFromSampleCount(mtlTexture.sampleCount);
+ _arrayLayers = uint32_t(mtlTexture.arrayLength);
+ _usage = getPixelFormats()->getVkImageUsageFlagsFromMTLTextureUsage(mtlTexture.usage, _mtlPixelFormat);
+
+ if (_device->_pMetalFeatures->ioSurfaces) {
+ _ioSurface = mtlTexture.iosurface;
+ CFRetain(_ioSurface);
+ }
+
+ return VK_SUCCESS;
+}
+
// Removes and releases the MTLTexture object, and all associated texture views
void MVKImage::releaseMTLTexture() {
[_mtlTexture release];
@@ -929,11 +927,8 @@
#pragma mark Metal
-// Creates and returns a retained Metal texture suitable for use in this instance.
-// This implementation retrieves a MTLTexture from the CAMetalDrawable.
-id<MTLTexture> MVKSwapchainImage::newMTLTexture() {
- return [[getCAMetalDrawable() texture] retain];
-}
+// Overridden to always retrieve the MTLTexture directly from the CAMetalDrawable.
+id<MTLTexture> MVKSwapchainImage::getMTLTexture() { return [getCAMetalDrawable() texture]; }
id<CAMetalDrawable> MVKSwapchainImage::getCAMetalDrawable() {
while ( !_mtlDrawable ) {
@@ -941,7 +936,7 @@
uint64_t startTime = _device->getPerformanceTimestamp();
_mtlDrawable = [_swapchain->_mtlLayer.nextDrawable retain];
- if ( !_mtlDrawable ) { MVKLogError("CAMetalDrawable could not be acquired after %.6f ms.", mvkGetElapsedMilliseconds(startTime)); }
+ if ( !_mtlDrawable ) { MVKLogError("CAMetalDrawable could not be acquired."); }
_device->addActivityPerformance(_device->_performanceStatistics.queue.nextCAMetalDrawable, startTime);
}