diff --git a/base/VulkanDevice.hpp b/base/VulkanDevice.hpp index 76c6655d..a6d8dbae 100644 --- a/base/VulkanDevice.hpp +++ b/base/VulkanDevice.hpp @@ -567,5 +567,36 @@ namespace vks return (std::find(supportedExtensions.begin(), supportedExtensions.end(), extension) != supportedExtensions.end()); } + /** + * Select the best-fit depth format for this device from a list of possible depth (and stencil) formats + * + * @param checkSamplingSupport Check if the format can be sampled from (e.g. for shader reads) + * + * @return The depth format that best fits for the current device + * + * @throw Throws an exception if no depth format fits the requirements + */ + VkFormat getSupportedDepthFormat(bool checkSamplingSupport) + { + // All depth formats may be optional, so we need to find a suitable depth format to use + 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 formatProperties; + vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProperties); + // Format must support depth stencil attachment for optimal tiling + if (formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) + { + if (checkSamplingSupport) { + if (!(formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) { + continue; + } + } + return format; + } + } + throw std::runtime_error("Could not find a matching depth format"); + } + }; } diff --git a/examples/shadowmappingcascade/shadowmappingcascade.cpp b/examples/shadowmappingcascade/shadowmappingcascade.cpp index 96e6ee55..d9b9d6d6 100644 --- a/examples/shadowmappingcascade/shadowmappingcascade.cpp +++ b/examples/shadowmappingcascade/shadowmappingcascade.cpp @@ -262,8 +262,7 @@ public: */ void prepareDepthPass() { - VkFormat depthFormat; - vks::tools::getSupportedDepthFormat(physicalDevice, &depthFormat); + VkFormat depthFormat = vulkanDevice->getSupportedDepthFormat(true); /* Depth map renderpass