From e49a69e61700f76c4fbeaae7f49b889bac6c7b6e Mon Sep 17 00:00:00 2001 From: sean Date: Fri, 29 Dec 2023 01:49:51 +0100 Subject: [PATCH] Add: KHR_format_feature_flags2 --- Docs/MoltenVK_Runtime_UserGuide.md | 1 + MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm | 22 ++++++++++++++++--- MoltenVK/MoltenVK/GPUObjects/MVKImage.mm | 2 +- .../MoltenVK/GPUObjects/MVKPixelFormats.h | 9 ++++---- .../MoltenVK/GPUObjects/MVKPixelFormats.mm | 22 +++++++++++++++---- MoltenVK/MoltenVK/Layers/MVKExtensions.def | 1 + 6 files changed, 45 insertions(+), 12 deletions(-) diff --git a/Docs/MoltenVK_Runtime_UserGuide.md b/Docs/MoltenVK_Runtime_UserGuide.md index 36329b5a..92929b17 100644 --- a/Docs/MoltenVK_Runtime_UserGuide.md +++ b/Docs/MoltenVK_Runtime_UserGuide.md @@ -323,6 +323,7 @@ In addition to core *Vulkan* functionality, **MoltenVK** also supports the foll - `VK_KHR_device_group_creation` - `VK_KHR_driver_properties` - `VK_KHR_dynamic_rendering` +- `VK_KHR_format_feature_flags2` - `VK_KHR_fragment_shader_barycentric` - *Requires Metal 2.2 on Mac or Metal 2.3 on iOS.* - `VK_KHR_get_memory_requirements2` diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm index a47b4e62..e022dde9 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm @@ -888,6 +888,22 @@ void MVKPhysicalDevice::getFormatProperties(VkFormat format, VkFormatProperties* void MVKPhysicalDevice::getFormatProperties(VkFormat format, VkFormatProperties2KHR* pFormatProperties) { pFormatProperties->sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2_KHR; + + for (auto* next = (VkBaseOutStructure*)pFormatProperties->pNext; next; next = next->pNext) { + switch (next->sType) { + case VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3_KHR: { + auto* properties3 = (VkFormatProperties3*)next; + auto& properties = _pixelFormats.getVkFormatProperties3(format); + properties3->linearTilingFeatures = properties.linearTilingFeatures; + properties3->optimalTilingFeatures = properties.optimalTilingFeatures; + properties3->bufferFeatures = properties.bufferFeatures; + break; + } + default: + break; + } + } + getFormatProperties(format, &pFormatProperties->formatProperties); } @@ -2530,7 +2546,7 @@ void MVKPhysicalDevice::initLimits() { } else { alignment = [_mtlDevice minimumLinearTextureAlignmentForPixelFormat: mtlFmt]; } - VkFormatProperties& props = _pixelFormats.getVkFormatProperties(vk); + VkFormatProperties3& props = _pixelFormats.getVkFormatProperties3(vk); // For uncompressed formats, this is the size of a single texel. // Note that no implementations of Metal support compressed formats // in a linear texture (including texture buffers). It's likely that even @@ -2558,11 +2574,11 @@ void MVKPhysicalDevice::initLimits() { break; } } - if (mvkAreAllFlagsEnabled(props.bufferFeatures, VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT)) { + if (mvkAreAllFlagsEnabled(props.bufferFeatures, VK_FORMAT_FEATURE_2_UNIFORM_TEXEL_BUFFER_BIT)) { maxUniform = max(maxUniform, uint32_t(alignment)); if (alignment > texelSize) { singleTexelUniform = false; } } - if (mvkAreAllFlagsEnabled(props.bufferFeatures, VK_FORMAT_FEATURE_STORAGE_TEXEL_BUFFER_BIT)) { + if (mvkAreAllFlagsEnabled(props.bufferFeatures, VK_FORMAT_FEATURE_2_STORAGE_TEXEL_BUFFER_BIT)) { maxStorage = max(maxStorage, uint32_t(alignment)); if (alignment > texelSize) { singleTexelStorage = false; } } diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm b/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm index 61bb96b1..d9d6a8bb 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKImage.mm @@ -931,7 +931,7 @@ MVKImage::MVKImage(MVKDevice* device, const VkImageCreateInfo* pCreateInfo) : MV _is3DCompressed = (getImageType() == VK_IMAGE_TYPE_3D) && (pixFmts->getFormatType(pCreateInfo->format) == kMVKFormatCompressed) && !_device->_pMetalFeatures->native3DCompressedTextures; _isDepthStencilAttachment = (mvkAreAllFlagsEnabled(pCreateInfo->usage, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) || - mvkAreAllFlagsEnabled(pixFmts->getVkFormatProperties(pCreateInfo->format).optimalTilingFeatures, VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)); + mvkAreAllFlagsEnabled(pixFmts->getVkFormatProperties3(pCreateInfo->format).optimalTilingFeatures, VK_FORMAT_FEATURE_2_DEPTH_STENCIL_ATTACHMENT_BIT)); _canSupportMTLTextureView = !_isDepthStencilAttachment || _device->_pMetalFeatures->stencilViews; _rowByteAlignment = _isLinear || _isLinearForAtomics ? _device->getVkFormatTexelBufferAlignment(pCreateInfo->format, this) : mvkEnsurePowerOfTwo(pixFmts->getBytesPerBlock(pCreateInfo->format)); diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKPixelFormats.h b/MoltenVK/MoltenVK/GPUObjects/MVKPixelFormats.h index 0e1ecd74..bbc71fa5 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKPixelFormats.h +++ b/MoltenVK/MoltenVK/GPUObjects/MVKPixelFormats.h @@ -147,7 +147,7 @@ typedef struct MVKVkFormatDesc { VkExtent2D blockTexelSize; uint32_t bytesPerBlock; MVKFormatType formatType; - VkFormatProperties properties; + VkFormatProperties3 properties; VkComponentMapping componentMapping; const char* name; bool hasReportedSubstitution; @@ -344,7 +344,8 @@ public: MTLTextureSwizzleChannels getMTLTextureSwizzleChannels(VkFormat vkFormat); /** Returns the default properties for the specified Vulkan format. */ - VkFormatProperties& getVkFormatProperties(VkFormat vkFormat); + VkFormatProperties getVkFormatProperties(VkFormat format); + VkFormatProperties3& getVkFormatProperties3(VkFormat vkFormat); /** Returns the Metal format capabilities supported by the specified Vulkan format, without substitution. */ MVKMTLFmtCaps getCapabilities(VkFormat vkFormat, bool isExtended = false); @@ -404,8 +405,8 @@ public: * Vulkan VkFormat as used as a vertex attribute format. */ MTLVertexFormat getMTLVertexFormat(VkFormat vkFormat); - - + + static VkFormatFeatureFlags convertFormatPropertiesFlagBits(VkFormatFeatureFlags2 flags); #pragma mark Construction MVKPixelFormats(MVKPhysicalDevice* physicalDevice = nullptr); diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKPixelFormats.mm b/MoltenVK/MoltenVK/GPUObjects/MVKPixelFormats.mm index ce60ba3e..e6004b51 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKPixelFormats.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKPixelFormats.mm @@ -420,10 +420,19 @@ MTLTextureSwizzleChannels MVKPixelFormats::getMTLTextureSwizzleChannels(VkFormat return mvkMTLTextureSwizzleChannelsFromVkComponentMapping(getVkComponentMapping(vkFormat)); } -VkFormatProperties& MVKPixelFormats::getVkFormatProperties(VkFormat vkFormat) { +VkFormatProperties3& MVKPixelFormats::getVkFormatProperties3(VkFormat vkFormat) { return getVkFormatDesc(vkFormat).properties; } +VkFormatProperties MVKPixelFormats::getVkFormatProperties(VkFormat vkFormat) { + auto& properties = getVkFormatProperties3(vkFormat); + VkFormatProperties ret; + ret.linearTilingFeatures = MVKPixelFormats::convertFormatPropertiesFlagBits(properties.linearTilingFeatures); + ret.optimalTilingFeatures = MVKPixelFormats::convertFormatPropertiesFlagBits(properties.optimalTilingFeatures); + ret.bufferFeatures = MVKPixelFormats::convertFormatPropertiesFlagBits(properties.bufferFeatures); + return ret; +} + MVKMTLFmtCaps MVKPixelFormats::getCapabilities(VkFormat vkFormat, bool isExtended) { return getCapabilities(getVkFormatDesc(vkFormat).mtlPixelFormat, isExtended); } @@ -460,7 +469,7 @@ const char* MVKPixelFormats::getName(MTLVertexFormat mtlFormat) { } void MVKPixelFormats::enumerateSupportedFormats(VkFormatProperties properties, bool any, std::function func) { - static const auto areFeaturesSupported = [any](uint32_t a, uint32_t b) { + static const auto areFeaturesSupported = [any](VkFlags64 a, VkFlags64 b) { if (b == 0) return true; if (any) return mvkIsAnyFlagEnabled(a, b); @@ -792,6 +801,11 @@ MVKMTLFormatDesc& MVKPixelFormats::getMTLVertexFormatDesc(MTLVertexFormat mtlFor return _mtlVertexFormatDescriptions[fmtIdx]; } +VkFormatFeatureFlags MVKPixelFormats::convertFormatPropertiesFlagBits(VkFormatFeatureFlags2 flags) { + // Truncate to 32-bits and just return. All current values are identical. + return static_cast(flags); +} + #pragma mark Construction @@ -811,7 +825,7 @@ MVKPixelFormats::MVKPixelFormats(MVKPhysicalDevice* physicalDevice) : _physicalD #define addVkFormatDescFull(VK_FMT, MTL_FMT, MTL_FMT_ALT, MTL_VTX_FMT, MTL_VTX_FMT_ALT, CSPC, CSCB, BLK_W, BLK_H, BLK_BYTE_CNT, MVK_FMT_TYPE, SWIZ_R, SWIZ_G, SWIZ_B, SWIZ_A) \ MVKAssert(fmtIdx < _vkFormatCount, "Attempting to describe %d VkFormats, but only have space for %d. Increase the value of _vkFormatCount", fmtIdx + 1, _vkFormatCount); \ _vkFormatDescriptions[fmtIdx++] = { VK_FORMAT_ ##VK_FMT, MTLPixelFormat ##MTL_FMT, MTLPixelFormat ##MTL_FMT_ALT, MTLVertexFormat ##MTL_VTX_FMT, MTLVertexFormat ##MTL_VTX_FMT_ALT, \ - CSPC, CSCB, { BLK_W, BLK_H }, BLK_BYTE_CNT, kMVKFormat ##MVK_FMT_TYPE, { 0, 0, 0 }, \ + CSPC, CSCB, { BLK_W, BLK_H }, BLK_BYTE_CNT, kMVKFormat ##MVK_FMT_TYPE, { VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3, nullptr, 0, 0, 0 }, \ { VK_COMPONENT_SWIZZLE_ ##SWIZ_R, VK_COMPONENT_SWIZZLE_ ##SWIZ_G, VK_COMPONENT_SWIZZLE_ ##SWIZ_B, VK_COMPONENT_SWIZZLE_ ##SWIZ_A }, \ "VK_FORMAT_" #VK_FMT, false } @@ -2119,7 +2133,7 @@ void MVKPixelFormats::setFormatProperties(MVKVkFormatDesc& vkDesc) { mvkEnableFlags(VK_FEATS, kMVKVkFormatFeatureFlags ##TYPE ##CAP); \ } - VkFormatProperties& vkProps = vkDesc.properties; + VkFormatProperties3& vkProps = vkDesc.properties; MVKMTLFmtCaps mtlPixFmtCaps = getMTLPixelFormatDesc(vkDesc.mtlPixelFormat).mtlFmtCaps; vkProps.optimalTilingFeatures = kMVKVkFormatFeatureFlagsTexNone; vkProps.linearTilingFeatures = kMVKVkFormatFeatureFlagsTexNone; diff --git a/MoltenVK/MoltenVK/Layers/MVKExtensions.def b/MoltenVK/MoltenVK/Layers/MVKExtensions.def index c9917b79..bbda1dcf 100644 --- a/MoltenVK/MoltenVK/Layers/MVKExtensions.def +++ b/MoltenVK/MoltenVK/Layers/MVKExtensions.def @@ -63,6 +63,7 @@ MVK_EXTENSION(KHR_external_memory_capabilities, KHR_EXTERNAL_MEMORY_CAPABI MVK_EXTENSION(KHR_external_semaphore, KHR_EXTERNAL_SEMAPHORE, DEVICE, 10.11, 8.0, 1.0) MVK_EXTENSION(KHR_external_semaphore_capabilities, KHR_EXTERNAL_SEMAPHORE_CAPABILITIES, INSTANCE, 10.11, 8.0, 1.0) MVK_EXTENSION(KHR_fragment_shader_barycentric, KHR_FRAGMENT_SHADER_BARYCENTRIC, DEVICE, 10.15, 14.0, 1.0) +MVK_EXTENSION(KHR_format_feature_flags2, KHR_FORMAT_FEATURE_FLAGS_2, DEVICE, 10.11, 8.0, 1.0) MVK_EXTENSION(KHR_get_memory_requirements2, KHR_GET_MEMORY_REQUIREMENTS_2, DEVICE, 10.11, 8.0, 1.0) MVK_EXTENSION(KHR_get_physical_device_properties2, KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2, INSTANCE, 10.11, 8.0, 1.0) MVK_EXTENSION(KHR_get_surface_capabilities2, KHR_GET_SURFACE_CAPABILITIES_2, INSTANCE, 10.11, 8.0, 1.0)