From f781ee720a9432417084a104495d3ab7820458f9 Mon Sep 17 00:00:00 2001 From: Bill Hollings Date: Wed, 5 May 2021 16:37:16 -0400 Subject: [PATCH] MVKImagePlane::getMTLTexture() protect against crash when image has no device memory bound. --- Docs/Whats_New.md | 1 + MoltenVK/MoltenVK/GPUObjects/MVKImage.mm | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Docs/Whats_New.md b/Docs/Whats_New.md index da7173d7..b0d90a50 100644 --- a/Docs/Whats_New.md +++ b/Docs/Whats_New.md @@ -25,6 +25,7 @@ Released TBD - Improve cache hits when matching `SPIRVToMSLConversionConfiguration` structs to each other to find a cached shader, by only considering resources from the current shader stage. - Rename `kMVKShaderStageMax` to `kMVKShaderStageCount`. +- Protect against crash when retrieving `MTLTexture` when `VkImage` has no `VkDeviceMemory` bound. - Fix internal reference from `SPIRV_CROSS_NAMESPACE_OVERRIDE` to `SPIRV_CROSS_NAMESPACE`. diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm b/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm index 4c2af5c9..435d78ed 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm @@ -44,6 +44,7 @@ id MVKImagePlane::getMTLTexture() { MTLTextureDescriptor* mtlTexDesc = newMTLTextureDescriptor(); // temp retain MVKImageMemoryBinding* memoryBinding = getMemoryBinding(); + MVKDeviceMemory* dvcMem = memoryBinding->_deviceMemory; if (_image->_ioSurface) { _mtlTexture = [_image->getMTLDevice() @@ -55,19 +56,19 @@ id MVKImagePlane::getMTLTexture() { newTextureWithDescriptor: mtlTexDesc offset: memoryBinding->_mtlTexelBufferOffset + _subresources[0].layout.offset bytesPerRow: _subresources[0].layout.rowPitch]; - } else if (memoryBinding->_deviceMemory->getMTLHeap() && !_image->getIsDepthStencil()) { + } else if (dvcMem && dvcMem->getMTLHeap() && !_image->getIsDepthStencil()) { // Metal support for depth/stencil from heaps is flaky - _mtlTexture = [memoryBinding->_deviceMemory->getMTLHeap() + _mtlTexture = [dvcMem->getMTLHeap() newTextureWithDescriptor: mtlTexDesc offset: memoryBinding->getDeviceMemoryOffset() + _subresources[0].layout.offset]; if (_image->_isAliasable) { [_mtlTexture makeAliasable]; } - } else if (_image->_isAliasable && memoryBinding->_deviceMemory->isDedicatedAllocation() && - !contains(memoryBinding->_deviceMemory->_imageMemoryBindings, memoryBinding)) { + } else if (_image->_isAliasable && dvcMem && dvcMem->isDedicatedAllocation() && + !contains(dvcMem->_imageMemoryBindings, memoryBinding)) { // This is a dedicated allocation, but it belongs to another aliasable image. // In this case, use the MTLTexture from the memory's dedicated image. // We know the other image must be aliasable, or I couldn't have been bound // to its memory: the memory object wouldn't allow it. - _mtlTexture = [memoryBinding->_deviceMemory->_imageMemoryBindings[0]->_image->getMTLTexture(_planeIndex, mtlTexDesc.pixelFormat) retain]; + _mtlTexture = [dvcMem->_imageMemoryBindings[0]->_image->getMTLTexture(_planeIndex, mtlTexDesc.pixelFormat) retain]; } else { _mtlTexture = [_image->getMTLDevice() newTextureWithDescriptor: mtlTexDesc]; }