From 1a686efc197a23229e7c764e9f5085f7500cf570 Mon Sep 17 00:00:00 2001 From: Bill Hollings Date: Tue, 30 Jun 2020 15:28:52 -0400 Subject: [PATCH] MVKPhysicalDevice::getSurfaceFormats() reduce fragility of surface format definitions. --- MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm | 38 ++++++++++------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm index 947b7d46..f5e70b46 100644 --- a/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm +++ b/MoltenVK/MoltenVK/GPUObjects/MVKDevice.mm @@ -615,21 +615,22 @@ VkResult MVKPhysicalDevice::getSurfaceFormats(MVKSurface* surface, CAMetalLayer* mtlLayer = surface->getCAMetalLayer(); if ( !mtlLayer ) { return surface->getConfigurationResult(); } - const MTLPixelFormat mtlFormats[] = { - MTLPixelFormatBGRA8Unorm, - MTLPixelFormatBGRA8Unorm_sRGB, - MTLPixelFormatRGBA16Float, +#define addSurfFmt(FMT) { if (_pixelFormats.isSupported(FMT)) { mtlFormats.push_back(FMT); } } + + MVKSmallVector mtlFormats; + addSurfFmt(MTLPixelFormatBGRA8Unorm); + addSurfFmt(MTLPixelFormatBGRA8Unorm_sRGB); + addSurfFmt(MTLPixelFormatRGBA16Float); #if MVK_MACOS - MTLPixelFormatRGB10A2Unorm, - MTLPixelFormatBGR10A2Unorm, + addSurfFmt(MTLPixelFormatRGB10A2Unorm); + addSurfFmt(MTLPixelFormatBGR10A2Unorm); #endif #if MVK_IOS_OR_TVOS - MTLPixelFormatBGRA10_XR, - MTLPixelFormatBGRA10_XR_sRGB, - MTLPixelFormatBGR10_XR, - MTLPixelFormatBGR10_XR_sRGB, + addSurfFmt(MTLPixelFormatBGRA10_XR); + addSurfFmt(MTLPixelFormatBGRA10_XR_sRGB); + addSurfFmt(MTLPixelFormatBGR10_XR); + addSurfFmt(MTLPixelFormatBGR10_XR_sRGB); #endif - }; MVKSmallVector colorSpaces; colorSpaces.push_back(VK_COLOR_SPACE_SRGB_NONLINEAR_KHR); @@ -678,25 +679,18 @@ VkResult MVKPhysicalDevice::getSurfaceFormats(MVKSurface* surface, #endif } - uint mtlFmtsCnt = sizeof(mtlFormats) / sizeof(MTLPixelFormat); -#if MVK_MACOS - if ( !_pixelFormats.isSupported(MTLPixelFormatBGR10A2Unorm) ) { mtlFmtsCnt--; } -#endif -#if MVK_IOS_OR_TVOS - if ( !_pixelFormats.isSupported(MTLPixelFormatBGRA10_XR) ) { mtlFmtsCnt -= 4; } -#endif - - const uint vkFmtsCnt = mtlFmtsCnt * (uint)colorSpaces.size(); + size_t mtlFmtsCnt = mtlFormats.size(); + size_t vkFmtsCnt = mtlFmtsCnt * colorSpaces.size(); // If properties aren't actually being requested yet, simply update the returned count if ( !pSurfaceFormats ) { - *pCount = vkFmtsCnt; + *pCount = (uint32_t)vkFmtsCnt; return VK_SUCCESS; } // Determine how many results we'll return, and return that number VkResult result = (*pCount >= vkFmtsCnt) ? VK_SUCCESS : VK_INCOMPLETE; - *pCount = min(*pCount, vkFmtsCnt); + *pCount = min(*pCount, (uint32_t)vkFmtsCnt); // Now populate the supplied array for (uint csIdx = 0, idx = 0; idx < *pCount && csIdx < colorSpaces.size(); csIdx++) {