diff --git a/attachments/32_ecosystem_utilities.cpp b/attachments/32_ecosystem_utilities.cpp index a0a4c2d2..16352b3e 100644 --- a/attachments/32_ecosystem_utilities.cpp +++ b/attachments/32_ecosystem_utilities.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #if defined(__INTELLISENSE__) || !defined(USE_CPP20_MODULES) # include @@ -137,6 +138,7 @@ class HelloTriangleApplication vk::raii::Image depthImage = nullptr; vk::raii::DeviceMemory depthImageMemory = nullptr; vk::raii::ImageView depthImageView = nullptr; + vk::ImageAspectFlags depthImageAspect = vk::ImageAspectFlagBits::eDepth; uint32_t mipLevels = 0; vk::raii::Image textureImage = nullptr; @@ -811,9 +813,14 @@ class HelloTriangleApplication void createDepthResources() { vk::Format depthFormat = findDepthFormat(); + depthImageAspect = vk::ImageAspectFlagBits::eDepth; + if (vk::hasStencilComponent(depthFormat)) + { + depthImageAspect |= vk::ImageAspectFlagBits::eStencil; + } createImage(swapChainExtent.width, swapChainExtent.height, 1, msaaSamples, depthFormat, vk::ImageTiling::eOptimal, vk::ImageUsageFlagBits::eDepthStencilAttachment, vk::MemoryPropertyFlagBits::eDeviceLocal, depthImage, depthImageMemory); - depthImageView = createImageView(depthImage, depthFormat, vk::ImageAspectFlagBits::eDepth, 1); + depthImageView = createImageView(depthImage, depthFormat, depthImageAspect, 1); } vk::Format findSupportedFormat(const std::vector &candidates, vk::ImageTiling tiling, vk::FormatFeatureFlags features) const @@ -843,11 +850,6 @@ class HelloTriangleApplication vk::FormatFeatureFlagBits::eDepthStencilAttachment); } - static bool hasStencilComponent(vk::Format format) - { - return format == vk::Format::eD32SfloatS8Uint || format == vk::Format::eD24UnormS8Uint; - } - void createTextureImage() { int texWidth, texHeight, texChannels; @@ -1376,7 +1378,7 @@ class HelloTriangleApplication .oldLayout = vk::ImageLayout::eUndefined, .newLayout = vk::ImageLayout::eDepthStencilAttachmentOptimal, .image = *depthImage, - .subresourceRange = {vk::ImageAspectFlagBits::eDepth, 0, 1, 0, 1}}; + .subresourceRange = {depthImageAspect, 0, 1, 0, 1}}; vk::ImageMemoryBarrier2 swapchainBarrier{ .srcStageMask = vk::PipelineStageFlagBits2::eColorAttachmentOutput, @@ -1416,7 +1418,7 @@ class HelloTriangleApplication .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .image = *depthImage, - .subresourceRange = {vk::ImageAspectFlagBits::eDepth, 0, 1, 0, 1}}; + .subresourceRange = {depthImageAspect, 0, 1, 0, 1}}; vk::ImageMemoryBarrier swapchainBarrier{ .srcAccessMask = vk::AccessFlagBits::eNone,