From 1f33a311ae8e833e1dcd53949e7b1f0d38045552 Mon Sep 17 00:00:00 2001 From: Chip Davis Date: Mon, 10 Sep 2018 14:54:50 -0500 Subject: [PATCH 1/3] Support views of the stencil aspect of depth/stencil images. This requires Metal 1.2 (for the `MTLPixelFormatX24_Stencil8` and `MTLPixelFormatX32_Stencil8` formats). Since Vulkan doesn't actually have a corresponding format for that, add a new Metal feature for this. Bump extension spec version. --- MoltenVK/MoltenVK/API/vk_mvk_moltenvk.h | 3 ++- MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm | 2 ++ MoltenVK/MoltenVK/GPUObjects/MVKImage.mm | 20 ++++++++++++++++++-- MoltenVK/MoltenVK/Vulkan/mvk_datatypes.mm | 2 +- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/MoltenVK/MoltenVK/API/vk_mvk_moltenvk.h b/MoltenVK/MoltenVK/API/vk_mvk_moltenvk.h index a089adf7..d0c167a2 100644 --- a/MoltenVK/MoltenVK/API/vk_mvk_moltenvk.h +++ b/MoltenVK/MoltenVK/API/vk_mvk_moltenvk.h @@ -54,7 +54,7 @@ extern "C" { #define MVK_VERSION MVK_MAKE_VERSION(MVK_VERSION_MAJOR, MVK_VERSION_MINOR, MVK_VERSION_PATCH) -#define VK_MVK_MOLTENVK_SPEC_VERSION 7 +#define VK_MVK_MOLTENVK_SPEC_VERSION 8 #define VK_MVK_MOLTENVK_EXTENSION_NAME "VK_MVK_moltenvk" /** @@ -94,6 +94,7 @@ typedef struct { VkBool32 depthClipMode; /**< If true, depth clipping and depth clamping per the VkGraphicsPipelineCreateInfo::VkPipelineRasterizationStateCreateInfo::depthClampEnable flag is supported. */ VkBool32 layeredRendering; /**< If true, layered rendering to multiple cube or texture array layers is supported. */ VkBool32 presentModeImmediate; /**< If true, immediate surface present mode (VK_PRESENT_MODE_IMMEDIATE_KHR), allowing a swapchain image to be presented immediately, without waiting for the vertical sync period of the display, is supported. */ + VkBool32 stencilViews; /**< If true, stencil aspect views are supported through the MTLPixelFormatX24_Stencil8 and MTLPixelFormatX32_Stencil8 formats. */ uint32_t maxTextureDimension; /**< The maximum size of each texture dimension (width, height, or depth). */ uint32_t maxPerStageBufferCount; /**< The total number of per-stage Metal buffers available for shader uniform content and attributes. */ uint32_t maxPerStageTextureCount; /**< The total number of per-stage Metal textures available for shader uniform content. */ diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm index 63da27e8..7f07f1cd 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm @@ -423,6 +423,7 @@ void MVKPhysicalDevice::initMetalFeatures() { if ( [_mtlDevice supportsFeatureSet: MTLFeatureSet_iOS_GPUFamily1_v3] ) { _metalFeatures.mslVersion = SPIRVToMSLConverterOptions::makeMSLVersion(1, 2); _metalFeatures.shaderSpecialization = true; + _metalFeatures.stencilViews = true; } if ( [_mtlDevice supportsFeatureSet: MTLFeatureSet_iOS_GPUFamily1_v4] ) { _metalFeatures.mslVersion = SPIRVToMSLConverterOptions::makeMSLVersion(2); @@ -454,6 +455,7 @@ void MVKPhysicalDevice::initMetalFeatures() { _metalFeatures.mslVersion = SPIRVToMSLConverterOptions::makeMSLVersion(1, 2); _metalFeatures.dynamicMTLBuffers = true; _metalFeatures.shaderSpecialization = true; + _metalFeatures.stencilViews = true; _metalFeatures.maxMTLBufferSize = (1 * GIBI); } diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm b/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm index 3fa0f96f..96f24e13 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm @@ -464,12 +464,12 @@ MVKImage::MVKImage(MVKDevice* device, const VkImageCreateInfo* pCreateInfo) : MV _isDepthStencilAttachment = (mvkAreFlagsEnabled(pCreateInfo->usage, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) || mvkAreFlagsEnabled(mvkVkFormatProperties(pCreateInfo->format).optimalTilingFeatures, VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)); - _canSupportMTLTextureView = !_isDepthStencilAttachment; + _canSupportMTLTextureView = !_isDepthStencilAttachment || _device->_pMetalFeatures->stencilViews; _hasExpectedTexelSize = (mvkMTLPixelFormatBytesPerBlock(_mtlPixelFormat) == mvkVkFormatBytesPerBlock(pCreateInfo->format)); _isLinear = validateLinear(pCreateInfo); _usesTexelBuffer = false; - // Calc _byteCount after _mtlTexture & _byteAlignment + // Calc _byteCount after _mtlTexture & _byteAlignment for (uint32_t mipLvl = 0; mipLvl < _mipLevels; mipLvl++) { _byteCount += getBytesPerLayer(mipLvl) * _extent.depth * _arrayLayers; } @@ -718,6 +718,22 @@ MTLPixelFormat MVKImageView::getSwizzledMTLPixelFormat(VkFormat format, VkCompon } break; + case MTLPixelFormatDepth32Float_Stencil8: + if (_subresourceRange.aspectMask == VK_IMAGE_ASPECT_STENCIL_BIT && + matchesSwizzle(components, {VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_MAX_ENUM, VK_COMPONENT_SWIZZLE_MAX_ENUM, VK_COMPONENT_SWIZZLE_MAX_ENUM} ) ) { + return MTLPixelFormatX32_Stencil8; + } + break; + +#if MVK_MACOS + case MTLPixelFormatDepth24Unorm_Stencil8: + if (_subresourceRange.aspectMask == VK_IMAGE_ASPECT_STENCIL_BIT && + matchesSwizzle(components, {VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_MAX_ENUM, VK_COMPONENT_SWIZZLE_MAX_ENUM, VK_COMPONENT_SWIZZLE_MAX_ENUM} ) ) { + return MTLPixelFormatX24_Stencil8; + } + break; +#endif + default: break; } diff --git a/MoltenVK/MoltenVK/Vulkan/mvk_datatypes.mm b/MoltenVK/MoltenVK/Vulkan/mvk_datatypes.mm index 3e67654a..76b02599 100644 --- a/MoltenVK/MoltenVK/Vulkan/mvk_datatypes.mm +++ b/MoltenVK/MoltenVK/Vulkan/mvk_datatypes.mm @@ -759,7 +759,7 @@ MVK_PUBLIC_SYMBOL MTLTextureUsage mvkMTLTextureUsageFromVkImageUsageFlags(VkImag } if ( mvkAreFlagsEnabled(vkImageUsageFlags, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ) { mvkEnableFlag(mtlUsage, MTLTextureUsageRenderTarget); - mvkDisableFlag(mtlUsage, MTLTextureUsagePixelFormatView); // Clears bit. Do this last. + mvkEnableFlag(mtlUsage, MTLTextureUsagePixelFormatView); } return mtlUsage; From 6947fdf57ee91ec015b28e08f8c98d5f0ed2dd06 Mon Sep 17 00:00:00 2001 From: Chip Davis Date: Tue, 11 Sep 2018 11:33:16 -0500 Subject: [PATCH 2/3] Only set MTLTextureUsagePixelFormatView on depth/stencil views... ...and only if the device supports stencil views. --- MoltenVK/MoltenVK/GPUObjects/MVKImage.h | 3 +++ MoltenVK/MoltenVK/GPUObjects/MVKImage.mm | 15 ++++++++++++++- MoltenVK/MoltenVK/Vulkan/mvk_datatypes.mm | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKImage.h b/MoltenVK/MoltenVK/GPUObjects/MVKImage.h index 48e0e536..9c83a487 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKImage.h +++ b/MoltenVK/MoltenVK/GPUObjects/MVKImage.h @@ -168,6 +168,9 @@ public: /** Returns the Metal texture type of this image. */ inline MTLTextureType getMTLTextureType() { return _mtlTextureType; } + /** Returns the Metal texture usage of this image. */ + MTLTextureUsage getMTLTextureUsage(); + /** * Returns whether the Metal texel size is the same as the Vulkan texel size. * diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm b/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm index 96f24e13..e8d914d2 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm @@ -327,6 +327,19 @@ VkResult MVKImage::useIOSurface(IOSurfaceRef ioSurface) { return VK_SUCCESS; } +MTLTextureUsage MVKImage::getMTLTextureUsage() { + MTLTextureUsage usage = mvkMTLTextureUsageFromVkImageUsageFlags(_usage); + // If this is a depth/stencil texture, and the device supports it, tell + // Metal we may create texture views of this, too. + if ((_mtlPixelFormat == MTLPixelFormatDepth32Float_Stencil8 +#if MVK_MACOS + || _mtlPixelFormat == MTLPixelFormatDepth24Unorm_Stencil8 +#endif + ) && _device->_pMetalFeatures->stencilViews) + mvkEnableFlag(usage, MTLTextureUsagePixelFormatView); + return usage; +} + // Returns an autoreleased Metal texture descriptor constructed from the properties of this image. MTLTextureDescriptor* MVKImage::getMTLTextureDescriptor() { MTLTextureDescriptor* mtlTexDesc = [[MTLTextureDescriptor alloc] init]; @@ -338,7 +351,7 @@ MTLTextureDescriptor* MVKImage::getMTLTextureDescriptor() { mtlTexDesc.mipmapLevelCount = _mipLevels; mtlTexDesc.sampleCount = mvkSampleCountFromVkSampleCountFlagBits(_samples); mtlTexDesc.arrayLength = _arrayLayers; - mtlTexDesc.usageMVK = mvkMTLTextureUsageFromVkImageUsageFlags(_usage); + mtlTexDesc.usageMVK = getMTLTextureUsage(); mtlTexDesc.storageModeMVK = getMTLStorageMode(); mtlTexDesc.cpuCacheMode = getMTLCPUCacheMode(); diff --git a/MoltenVK/MoltenVK/Vulkan/mvk_datatypes.mm b/MoltenVK/MoltenVK/Vulkan/mvk_datatypes.mm index 76b02599..3e67654a 100644 --- a/MoltenVK/MoltenVK/Vulkan/mvk_datatypes.mm +++ b/MoltenVK/MoltenVK/Vulkan/mvk_datatypes.mm @@ -759,7 +759,7 @@ MVK_PUBLIC_SYMBOL MTLTextureUsage mvkMTLTextureUsageFromVkImageUsageFlags(VkImag } if ( mvkAreFlagsEnabled(vkImageUsageFlags, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) ) { mvkEnableFlag(mtlUsage, MTLTextureUsageRenderTarget); - mvkEnableFlag(mtlUsage, MTLTextureUsagePixelFormatView); + mvkDisableFlag(mtlUsage, MTLTextureUsagePixelFormatView); // Clears bit. Do this last. } return mtlUsage; From 9fa8e8c57cf7176e73e6d46f5112d59c345956e4 Mon Sep 17 00:00:00 2001 From: Chip Davis Date: Tue, 11 Sep 2018 12:15:28 -0500 Subject: [PATCH 3/3] Bump extension spec version again after merge. --- MoltenVK/MoltenVK/API/vk_mvk_moltenvk.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MoltenVK/MoltenVK/API/vk_mvk_moltenvk.h b/MoltenVK/MoltenVK/API/vk_mvk_moltenvk.h index b0c85c11..72bd9a18 100644 --- a/MoltenVK/MoltenVK/API/vk_mvk_moltenvk.h +++ b/MoltenVK/MoltenVK/API/vk_mvk_moltenvk.h @@ -54,7 +54,7 @@ extern "C" { #define MVK_VERSION MVK_MAKE_VERSION(MVK_VERSION_MAJOR, MVK_VERSION_MINOR, MVK_VERSION_PATCH) -#define VK_MVK_MOLTENVK_SPEC_VERSION 8 +#define VK_MVK_MOLTENVK_SPEC_VERSION 9 #define VK_MVK_MOLTENVK_EXTENSION_NAME "VK_MVK_moltenvk" /**