Add stencil format require toggle

Fixes #1031
This commit is contained in:
Sascha Willems 2023-05-09 18:03:51 +02:00
parent 0afa517a68
commit 3c9aca3fcb
5 changed files with 48 additions and 15 deletions

View file

@ -82,7 +82,7 @@ namespace vks
{ {
// Since all depth formats may be optional, we need to find a suitable depth format to use // Since all depth formats may be optional, we need to find a suitable depth format to use
// Start with the highest precision packed format // Start with the highest precision packed format
std::vector<VkFormat> depthFormats = { std::vector<VkFormat> formatList = {
VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D32_SFLOAT_S8_UINT,
VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT,
VK_FORMAT_D24_UNORM_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT,
@ -90,11 +90,10 @@ namespace vks
VK_FORMAT_D16_UNORM VK_FORMAT_D16_UNORM
}; };
for (auto& format : depthFormats) for (auto& format : formatList)
{ {
VkFormatProperties formatProps; VkFormatProperties formatProps;
vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProps); vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProps);
// Format must support depth stencil attachment for optimal tiling
if (formatProps.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) if (formatProps.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
{ {
*depthFormat = format; *depthFormat = format;
@ -105,6 +104,29 @@ namespace vks
return false; return false;
} }
VkBool32 getSupportedDepthStencilFormat(VkPhysicalDevice physicalDevice, VkFormat* depthStencilFormat)
{
std::vector<VkFormat> formatList = {
VK_FORMAT_D32_SFLOAT_S8_UINT,
VK_FORMAT_D24_UNORM_S8_UINT,
VK_FORMAT_D16_UNORM_S8_UINT,
};
for (auto& format : formatList)
{
VkFormatProperties formatProps;
vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProps);
if (formatProps.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)
{
*depthStencilFormat = format;
return true;
}
}
return false;
}
VkBool32 formatHasStencil(VkFormat format) VkBool32 formatHasStencil(VkFormat format)
{ {
std::vector<VkFormat> stencilFormats = { std::vector<VkFormat> stencilFormats = {

View file

@ -1,7 +1,7 @@
/* /*
* Assorted Vulkan helper functions * Assorted Vulkan helper functions
* *
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de * Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de
* *
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/ */
@ -78,6 +78,8 @@ namespace vks
// Selected a suitable supported depth format starting with 32 bit down to 16 bit // 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 // Returns false if none of the depth formats in the list is supported by the device
VkBool32 getSupportedDepthFormat(VkPhysicalDevice physicalDevice, VkFormat *depthFormat); VkBool32 getSupportedDepthFormat(VkPhysicalDevice physicalDevice, VkFormat *depthFormat);
// Same as getSupportedDepthFormat but will only select formats that also have stencil
VkBool32 getSupportedDepthStencilFormat(VkPhysicalDevice physicalDevice, VkFormat* depthStencilFormat);
// Returns tru a given format support LINEAR filtering // Returns tru a given format support LINEAR filtering
VkBool32 formatIsFilterable(VkPhysicalDevice physicalDevice, VkFormat format, VkImageTiling tiling); VkBool32 formatIsFilterable(VkPhysicalDevice physicalDevice, VkFormat format, VkImageTiling tiling);

View file

@ -1,7 +1,7 @@
/* /*
* Vulkan Example base class * Vulkan Example base class
* *
* Copyright (C) by Sascha Willems - www.saschawillems.de * Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de
* *
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/ */
@ -1043,9 +1043,15 @@ bool VulkanExampleBase::initVulkan()
// Get a graphics queue from the device // Get a graphics queue from the device
vkGetDeviceQueue(device, vulkanDevice->queueFamilyIndices.graphics, 0, &queue); vkGetDeviceQueue(device, vulkanDevice->queueFamilyIndices.graphics, 0, &queue);
// Find a suitable depth format // Find a suitable depth and/or stencil format
VkBool32 validDepthFormat = vks::tools::getSupportedDepthFormat(physicalDevice, &depthFormat); VkBool32 validFormat{ false };
assert(validDepthFormat); // Sample that make use of stencil will require a depth + stencil format, so we select from a different list
if (requiresStencil) {
validFormat = vks::tools::getSupportedDepthStencilFormat(physicalDevice, &depthFormat);
} else {
validFormat = vks::tools::getSupportedDepthFormat(physicalDevice, &depthFormat);
}
assert(validFormat);
swapChain.connect(instance, physicalDevice, device); swapChain.connect(instance, physicalDevice, device);

View file

@ -1,7 +1,7 @@
/* /*
* Vulkan Example base class * Vulkan Example base class
* *
* Copyright (C) by Sascha Willems - www.saschawillems.de * Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de
* *
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/ */
@ -154,6 +154,7 @@ protected:
VkSemaphore renderComplete; VkSemaphore renderComplete;
} semaphores; } semaphores;
std::vector<VkFence> waitFences; std::vector<VkFence> waitFences;
bool requiresStencil{ false };
public: public:
bool prepared = false; bool prepared = false;
bool resized = false; bool resized = false;

View file

@ -1,7 +1,7 @@
/* /*
* Vulkan Example - Rendering outlines using the stencil buffer * Vulkan Example - Rendering outlines using the stencil buffer
* *
* Copyright (C) 2016-2017 by Sascha Willems - www.saschawillems.de * Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de
* *
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/ */
@ -43,6 +43,8 @@ public:
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 512.0f); camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 512.0f);
camera.setRotation(glm::vec3(2.5f, -35.0f, 0.0f)); camera.setRotation(glm::vec3(2.5f, -35.0f, 0.0f));
camera.setTranslation(glm::vec3(0.0f, 0.0f, -2.0f)); camera.setTranslation(glm::vec3(0.0f, 0.0f, -2.0f));
// This samples requires a format that supports depth AND stencil, which will be handled by the base class
requiresStencil = true;
} }
~VulkanExample() ~VulkanExample()