diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index 8163be12..dc3f910e 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -522,24 +522,8 @@ void VulkanExampleBase::initVulkan(bool enableValidation) // Get the graphics queue vkGetDeviceQueue(device, graphicsQueueIndex, 0, &queue); - // Find supported depth format - // We prefer 24 bits of depth and 8 bits of stencil, but that may not be supported by all implementations - std::vector depthFormats = { VK_FORMAT_D24_UNORM_S8_UINT, VK_FORMAT_D16_UNORM_S8_UINT, VK_FORMAT_D16_UNORM }; - bool depthFormatFound = false; - for (auto& format : depthFormats) - { - VkFormatProperties formatProps; - vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProps); - // Format must support depth stencil attachment for optimal tiling - if (formatProps.optimalTilingFeatures && VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) - { - depthFormat = format; - depthFormatFound = true; - break; - } - } - - assert(depthFormatFound); + VkBool32 validDepthFormat = vkTools::getSupportedDepthFormat(physicalDevice, &depthFormat); + assert(validDepthFormat); swapChain.init(instance, physicalDevice, device); } diff --git a/base/vulkantools.cpp b/base/vulkantools.cpp index 3b7d809e..9a8de81a 100644 --- a/base/vulkantools.cpp +++ b/base/vulkantools.cpp @@ -74,6 +74,33 @@ namespace vkTools } } + VkBool32 getSupportedDepthFormat(VkPhysicalDevice physicalDevice, VkFormat *depthFormat) + { + // Since all depth formats may be optional, we need to find a suitable depth format to use + // Start with the highest precision packed format + std::vector depthFormats = { + VK_FORMAT_D32_SFLOAT_S8_UINT, + VK_FORMAT_D32_SFLOAT, + VK_FORMAT_D24_UNORM_S8_UINT, + VK_FORMAT_D16_UNORM_S8_UINT, + VK_FORMAT_D16_UNORM + }; + + for (auto& format : depthFormats) + { + VkFormatProperties formatProps; + vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProps); + // Format must support depth stencil attachment for optimal tiling + if (formatProps.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) + { + *depthFormat = format; + return true; + } + } + + return false; + } + // Create an image memory barrier for changing the layout of // an image and put it into an active command buffer // See chapter 11.4 "Image Layout" for details diff --git a/base/vulkantools.h b/base/vulkantools.h index ed37ce25..859cc33f 100644 --- a/base/vulkantools.h +++ b/base/vulkantools.h @@ -37,6 +37,10 @@ namespace vkTools VkBool32 checkDeviceExtensionPresent(VkPhysicalDevice physicalDevice, const char* extensionName); // Return string representation of a vulkan error string std::string errorString(VkResult errorCode); + + // Selected a suitable supported depth format starting with 32 bit down to 16 bit + // Returns false if none of the depth formats in the list is supported by the device + VkBool32 getSupportedDepthFormat(VkPhysicalDevice physicalDevice, VkFormat *depthFormat); // Put an image memory barrier for setting an image layout into the given command buffer void setImageLayout(