From 3b8b8e2e890d1dff529fdeed9119887a77247197 Mon Sep 17 00:00:00 2001 From: Nate Trost Date: Mon, 16 Nov 2026 18:40:52 -0600 Subject: [PATCH] BGF: Fix Vulkan depth/stencil swapchain formats and image views Fix depth/stencil handling of Vulkan swapchains in base_game_framework: - CreateSwapchain picked the depth/stencil VkFormat from the depth format alone (GetVkDepthFormat) and ignored display_stencil_format, so the advertised stencil combinations were never created: D32F + S8 created D32_SFLOAT (no stencil), D16 + S8 created D16_UNORM, and stencil-only S8 created no depth/stencil buffer at all. The VkFormat now comes from the (depth, stencil) pair. GetDepthStencilFormats and the new GetVkDepthStencilFormat share one table, so the offered combinations and the created formats always match. The unused GetVkStencilFormat and the depth-only table are removed. GetDepthStencilFormats also clears its output first. - Depth/stencil image views always used VK_IMAGE_ASPECT_DEPTH_BIT. They now use the format's aspects: depth, stencil (S8_UINT), or both for combined formats. - Each swapchain image has its own depth/stencil image, but views were picked by a separate counter advanced on every present rather than by the acquired image index. vkAcquireNextImageKHR does not hand out images in a fixed order, so two frames in flight could share a depth/stencil image. Renderers that cache framebuffers per color view would also pair a color image with the wrong depth image for good. The depth/stencil view now uses the acquired image index, and the separate counter is removed. - GetSwapchainFrameResourcesVk indexed the depth/stencil views unconditionally. That vector is empty when the swapchain has no depth/stencil buffer, so the read was out of bounds. It now returns VK_NULL_HANDLE in that case. - CreateSwapchainImages resets the acquired image index and state, since the new image set may be smaller than the old one. - CreateSwapchainImages reports failure. A failed vmaCreateImage or vkCreateImageView used to be logged and ignored, so it went on to create a view of a null image, and InitSwapchain still reported success. InitSwapchain now destroys the swapchain and fails if image creation or Swappy activation fails, instead of leaving a swapchain that GetSwapchainValid reports as valid. RecreateSwapchain stops if swapchain or image creation fails, instead of continuing with a null swapchain. DestroySwapchainImages skips handles that were never created and clears swapchain_images_. - The depth/stencil create-info structs are value-initialized, so the file builds with -Wall -Wextra (no -Wmissing-field-initializers warnings). Tested on a Pixel 10 Pro with agdktunnel and Vulkan Validation Layers 1.4.363.1, using a temporary test hook: - For every depth/stencil combination the device offers, InitSwapchain succeeds, GetSwapchainFrameResourcesVk returns the expected format, and the swapchain is invalid after ShutdownSwapchain: none/none -> UNDEFINED with a null view (previously an out-of-bounds read), D24S8 -> D24_UNORM_S8_UINT, S8 -> S8_UINT, D16 -> D16_UNORM, D32F -> D32_SFLOAT, D32F+S8 -> D32_SFLOAT_S8_UINT (was D32_SFLOAT). D16_UNORM_S8_UINT is not supported on this device, so it is not offered. - While rendering, each of the 5 swapchain images always gets its own depth/stencil view. - agdktunnel renders correctly with its default D24S8 (including after background/resume) and with D32F+S8. - No new validation errors. The two errors already on main (finding #23, PR 12) are unchanged. Not exercised: the image creation failure paths (no allocation failure could be forced). Rendering with no depth/stencil buffer still crashes, in simple_renderer: RenderPassVk always declares a depth attachment, even with an UNDEFINED format. This is tracked for PR 10. --- .../src/vulkan/graphics_api_vulkan.cpp | 157 ++++++++++++------ .../src/vulkan/graphics_api_vulkan.h | 9 +- .../src/vulkan/graphics_api_vulkan_utils.cpp | 120 ++++++------- .../src/vulkan/graphics_api_vulkan_utils.h | 7 +- 4 files changed, 164 insertions(+), 129 deletions(-) diff --git a/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan.cpp b/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan.cpp index f2f60e13..2ad47814 100644 --- a/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan.cpp +++ b/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan.cpp @@ -630,7 +630,10 @@ DisplayManager::InitSwapchainResult GraphicsAPIVulkan::InitSwapchain( return DisplayManager::kInit_Swapchain_Failure; } - CreateSwapchainImages(); + if (!CreateSwapchainImages()) { + DestroySwapchain(); + return DisplayManager::kInit_Swapchain_Failure; + } if (!PlatformUtilVulkan::ActivateSwapchain(vk_physical_device_, vk_device_, @@ -638,6 +641,9 @@ DisplayManager::InitSwapchainResult GraphicsAPIVulkan::InitSwapchain( vk_present_queue_, present_queue_index_, swapchain_interval_)) { + // Don't leave a half initialized swapchain that GetSwapchainValid reports as valid + PlatformUtilVulkan::DeactivateSwapchain(vk_device_, vk_swapchain_); + DestroySwapchain(); return DisplayManager::kInit_Swapchain_Failure; } return DisplayManager::kInit_Swapchain_Success; @@ -715,9 +721,6 @@ DisplayManager::SwapchainFrameHandle GraphicsAPIVulkan::PresentCurrentSwapchainF swapchain_info_.swapchain_current_frame_index_ = (swapchain_info_.swapchain_current_frame_index_ + 1) % swapchain_info_.swapchain_frame_count_; - swapchain_info_.swapchain_current_depth_stencil_frame_index_ = - (swapchain_info_.swapchain_current_depth_stencil_frame_index_ + 1) % - swapchain_info_.swapchain_image_count_; } else { DebugManager::Log(DebugManager::kLog_Channel_Default, DebugManager::kLog_Level_Error, @@ -786,9 +789,15 @@ bool GraphicsAPIVulkan::GetSwapchainFrameResourcesVk( frame_render_completion_semaphore_[swapchain_info_.swapchain_current_frame_index_]; frame_resources.swapchain_color_image_view = swapchain_info_.swapchain_image_views_[swapchain_info_.swapchain_current_image_index_]; + // Each swapchain image has its own depth/stencil image, so a depth/stencil image is never + // shared by two frames in flight. None exist if the swapchain format has no depth or + // stencil. + const uint32_t image_index = swapchain_info_.swapchain_current_image_index_; + const std::vector &depth_stencil_views = + swapchain_info_.swapchain_depth_stencil_image_views_; frame_resources.swapchain_depth_stencil_image_view = - swapchain_info_.swapchain_depth_stencil_image_views_[ - swapchain_info_.swapchain_current_depth_stencil_frame_index_]; + (image_index < depth_stencil_views.size()) ? depth_stencil_views[image_index] : + VK_NULL_HANDLE; frame_resources.swapchain = vk_swapchain_; frame_resources.swapchain_color_format = swapchain_info_.swapchain_color_format_; frame_resources.swapchain_depth_stencil_format = @@ -1024,7 +1033,8 @@ bool GraphicsAPIVulkan::CreateSwapchain(const QueueFamilyIndices &queue_indices) VulkanAPIUtils::GetVkPixelFormat(swapchain_format_.display_pixel_format, swapchain_format_.display_color_space); swapchain_info_.swapchain_depth_stencil_format_ = - VulkanAPIUtils::GetVkDepthFormat(swapchain_format_.display_depth_format); + VulkanAPIUtils::GetVkDepthStencilFormat(swapchain_format_.display_depth_format, + swapchain_format_.display_stencil_format); VkSwapchainCreateInfoKHR swapchain_create_info{}; swapchain_create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; @@ -1080,9 +1090,17 @@ void GraphicsAPIVulkan::RecreateSwapchain() { DebugManager::kLog_Level_Error, BGM_CLASS_TAG, "CreateSwapchain in RecreateSwapchain failed!"); + return; } - CreateSwapchainImages(); + if (!CreateSwapchainImages()) { + DebugManager::Log(DebugManager::kLog_Channel_Default, + DebugManager::kLog_Level_Error, + BGM_CLASS_TAG, + "CreateSwapchainImages in RecreateSwapchain failed!"); + DestroySwapchain(); + return; + } if (!PlatformUtilVulkan::ActivateSwapchain(vk_physical_device_, vk_device_, @@ -1103,22 +1121,35 @@ void GraphicsAPIVulkan::DestroySwapchain() { vk_swapchain_ = VK_NULL_HANDLE; } -void GraphicsAPIVulkan::CreateSwapchainImages() { +bool GraphicsAPIVulkan::CreateSwapchainImages() { + // A new set of images: no image is acquired yet, and the image count may differ from the + // previous swapchain, so don't keep an index into the old image set + swapchain_info_.swapchain_current_image_index_ = 0; + swapchain_acquired_current_frame_image_ = false; + // Get the images and create the image views and framebuffers - vkGetSwapchainImagesKHR(vk_device_, - vk_swapchain_, - &swapchain_info_.swapchain_image_count_, - nullptr); - swapchain_info_.swapchain_images_.resize(swapchain_info_.swapchain_image_count_); - vkGetSwapchainImagesKHR(vk_device_, vk_swapchain_, &swapchain_info_.swapchain_image_count_, - swapchain_info_.swapchain_images_.data()); - - swapchain_info_.swapchain_image_views_.resize(swapchain_info_.swapchain_image_count_); - VkImageView *base_view = swapchain_info_.swapchain_image_views_.data(); + uint32_t image_count = 0; + if (!VulkanAPIUtils::CheckVkResult( + vkGetSwapchainImagesKHR(vk_device_, vk_swapchain_, &image_count, nullptr), + "vkGetSwapchainImagesKHR")) { + return false; + } + swapchain_info_.swapchain_images_.resize(image_count); + if (!VulkanAPIUtils::CheckVkResult( + vkGetSwapchainImagesKHR(vk_device_, vk_swapchain_, &image_count, + swapchain_info_.swapchain_images_.data()), + "vkGetSwapchainImagesKHR")) { + swapchain_info_.swapchain_images_.clear(); + return false; + } + swapchain_info_.swapchain_image_count_ = image_count; + + // Views/images that fail to create stay VK_NULL_HANDLE, which DestroySwapchainImages skips + swapchain_info_.swapchain_image_views_.resize(image_count, VK_NULL_HANDLE); const VkFormat pixel_format = VulkanAPIUtils::GetVkPixelFormat( swapchain_format_.display_pixel_format, swapchain_format_.display_color_space); - for (size_t i = 0; i < swapchain_info_.swapchain_image_count_; ++i) { + for (size_t i = 0; i < image_count; ++i) { VkImageViewCreateInfo create_info{}; create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; create_info.image = swapchain_info_.swapchain_images_[i]; @@ -1133,26 +1164,25 @@ void GraphicsAPIVulkan::CreateSwapchainImages() { create_info.subresourceRange.levelCount = 1; create_info.subresourceRange.baseArrayLayer = 0; create_info.subresourceRange.layerCount = 1; - VulkanAPIUtils::CheckVkResult( - vkCreateImageView(vk_device_, &create_info, nullptr, &base_view[i]), - "vkCreateImageView"); + if (!VulkanAPIUtils::CheckVkResult( + vkCreateImageView(vk_device_, &create_info, nullptr, + &swapchain_info_.swapchain_image_views_[i]), + "vkCreateImageView")) { + swapchain_info_.swapchain_image_views_[i] = VK_NULL_HANDLE; + DestroySwapchainImages(); + return false; + } } if (swapchain_info_.swapchain_depth_stencil_format_ != VK_FORMAT_UNDEFINED) { - // Create the image/imageviews for the depth/stencil buffer - swapchain_info_.swapchain_depth_stencil_images_.resize( - swapchain_info_.swapchain_image_count_); - swapchain_info_.swapchain_depth_stencil_image_views_.resize( - swapchain_info_.swapchain_image_count_); - swapchain_info_.swapchain_depth_stencil_allocs_.resize( - swapchain_info_.swapchain_image_count_); - - VkImage *swap_images = swapchain_info_.swapchain_depth_stencil_images_.data(); - VkImageView *swap_image_views = swapchain_info_.swapchain_depth_stencil_image_views_.data(); - VmaAllocation *swap_allocs = swapchain_info_.swapchain_depth_stencil_allocs_.data(); - - for (size_t i = 0; i < swapchain_info_.swapchain_image_count_; ++i) { - VkImageCreateInfo ds_image_info = {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO}; + // Create one depth/stencil image/imageview per swapchain image, used with that image + swapchain_info_.swapchain_depth_stencil_images_.resize(image_count, VK_NULL_HANDLE); + swapchain_info_.swapchain_depth_stencil_image_views_.resize(image_count, VK_NULL_HANDLE); + swapchain_info_.swapchain_depth_stencil_allocs_.resize(image_count, VK_NULL_HANDLE); + + for (size_t i = 0; i < image_count; ++i) { + VkImageCreateInfo ds_image_info{}; + ds_image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; ds_image_info.imageType = VK_IMAGE_TYPE_2D; if (pretransform_flags_ & VK_SURFACE_TRANSFORM_ROTATE_90_BIT_KHR || pretransform_flags_ & VK_SURFACE_TRANSFORM_ROTATE_270_BIT_KHR) { @@ -1177,50 +1207,67 @@ void GraphicsAPIVulkan::CreateSwapchainImages() { VmaAllocationCreateInfo ds_image_alloc_create_info = {}; ds_image_alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO; - VulkanAPIUtils::CheckVkResult( - vmaCreateImage(allocator_, &ds_image_info, - &ds_image_alloc_create_info, - &swap_images[i], &swap_allocs[i], nullptr), - "vmaCreateImage swap depth/stencil"); + if (!VulkanAPIUtils::CheckVkResult( + vmaCreateImage(allocator_, &ds_image_info, &ds_image_alloc_create_info, + &swapchain_info_.swapchain_depth_stencil_images_[i], + &swapchain_info_.swapchain_depth_stencil_allocs_[i], nullptr), + "vmaCreateImage swap depth/stencil")) { + swapchain_info_.swapchain_depth_stencil_images_[i] = VK_NULL_HANDLE; + swapchain_info_.swapchain_depth_stencil_allocs_[i] = VK_NULL_HANDLE; + DestroySwapchainImages(); + return false; + } - VkImageViewCreateInfo ds_image_view_info = {VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO}; - ds_image_view_info.image = swap_images[i]; + VkImageViewCreateInfo ds_image_view_info{}; + ds_image_view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; + ds_image_view_info.image = swapchain_info_.swapchain_depth_stencil_images_[i]; ds_image_view_info.viewType = VK_IMAGE_VIEW_TYPE_2D; ds_image_view_info.format = swapchain_info_.swapchain_depth_stencil_format_; - ds_image_view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; + ds_image_view_info.subresourceRange.aspectMask = + VulkanAPIUtils::GetVkDepthStencilAspectMask( + swapchain_info_.swapchain_depth_stencil_format_); ds_image_view_info.subresourceRange.baseMipLevel = 0; ds_image_view_info.subresourceRange.levelCount = 1; ds_image_view_info.subresourceRange.baseArrayLayer = 0; ds_image_view_info.subresourceRange.layerCount = 1; - VulkanAPIUtils::CheckVkResult(vkCreateImageView( - vk_device_, &ds_image_view_info, nullptr, &swap_image_views[i]), - "vkCreateImageView swap depth/stencil"); + if (!VulkanAPIUtils::CheckVkResult( + vkCreateImageView(vk_device_, &ds_image_view_info, nullptr, + &swapchain_info_.swapchain_depth_stencil_image_views_[i]), + "vkCreateImageView swap depth/stencil")) { + swapchain_info_.swapchain_depth_stencil_image_views_[i] = VK_NULL_HANDLE; + DestroySwapchainImages(); + return false; + } } } + return true; } void GraphicsAPIVulkan::DestroySwapchainImages() { - if (!swapchain_info_.swapchain_depth_stencil_image_views_.empty()) { - for (const VkImageView &view : swapchain_info_.swapchain_depth_stencil_image_views_) { + // Safe after a partial CreateSwapchainImages: anything not created is VK_NULL_HANDLE + for (const VkImageView view : swapchain_info_.swapchain_depth_stencil_image_views_) { + if (view != VK_NULL_HANDLE) { vkDestroyImageView(vk_device_, view, nullptr); } } - if (!swapchain_info_.swapchain_depth_stencil_images_.empty()) { - for (size_t i = 0; i < swapchain_info_.swapchain_depth_stencil_images_.size(); ++i) { + for (size_t i = 0; i < swapchain_info_.swapchain_depth_stencil_images_.size(); ++i) { + if (swapchain_info_.swapchain_depth_stencil_images_[i] != VK_NULL_HANDLE) { vmaDestroyImage(allocator_, swapchain_info_.swapchain_depth_stencil_images_[i], swapchain_info_.swapchain_depth_stencil_allocs_[i]); } } - if (!swapchain_info_.swapchain_image_views_.empty()) { - for (size_t i = 0; i < swapchain_info_.swapchain_image_count_; ++i) { - vkDestroyImageView(vk_device_, swapchain_info_.swapchain_image_views_[i], nullptr); + for (const VkImageView view : swapchain_info_.swapchain_image_views_) { + if (view != VK_NULL_HANDLE) { + vkDestroyImageView(vk_device_, view, nullptr); } } + // The swapchain images themselves are owned by the swapchain + swapchain_info_.swapchain_images_.clear(); swapchain_info_.swapchain_image_views_.clear(); swapchain_info_.swapchain_depth_stencil_images_.clear(); swapchain_info_.swapchain_depth_stencil_image_views_.clear(); diff --git a/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan.h b/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan.h index 434bd90e..682d48d3 100644 --- a/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan.h +++ b/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan.h @@ -49,12 +49,9 @@ class GraphicsAPIVulkan : public GraphicsAPIBase { // This is the index of the current in-flight frame: // i.e. 0/1 for double buffer, or 0/1/2 for triple buffer uint32_t swapchain_current_frame_index_ = 0; - // This is the index of the current in-flight depth/stencil frame, - // this index won't match the color buffer since we don't have to - // keep the depth/stencil buffer for presentation - uint32_t swapchain_current_depth_stencil_frame_index_ = 0; // This is the index of the current active swapchain image, - // which may be greater than the number of max in-flight frames + // which may be greater than the number of max in-flight frames. + // Each swapchain image has its own depth/stencil image, also selected by this index. uint32_t swapchain_current_image_index_ = 0; // Number of max in-flight frames uint32_t swapchain_frame_count_ = 0; @@ -127,7 +124,7 @@ class GraphicsAPIVulkan : public GraphicsAPIBase { bool CreateSwapchain(const QueueFamilyIndices &queue_indices); void RecreateSwapchain(); void DestroySwapchain(); - void CreateSwapchainImages(); + bool CreateSwapchainImages(); void DestroySwapchainImages(); void CreateSynchronizationObjects(); diff --git a/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan_utils.cpp b/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan_utils.cpp index b0e59a9a..b2357ff1 100644 --- a/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan_utils.cpp +++ b/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan_utils.cpp @@ -40,16 +40,27 @@ static constexpr ColorFormatVk kColorFormatTableVk[DisplayManager::kDisplay_Pixe {VK_FORMAT_A2B10G10R10_UNORM_PACK32, VK_FORMAT_UNDEFINED} // kDisplay_Pixel_Format_ABGR2_10_10_10 }; -static constexpr VkFormat kDepthFormatTableVk[DisplayManager::kDisplay_Depth_Format_Count] = { - VK_FORMAT_D24_UNORM_S8_UINT, // kDisplay_Depth_Format_D24S8_Packed - VK_FORMAT_D16_UNORM, // kDisplay_Depth_Format_16U - VK_FORMAT_UNDEFINED, // kDisplay_Depth_Format_16F - VK_FORMAT_D32_SFLOAT // kDisplay_Depth_Format_32F +// Depth/stencil combinations and the VkFormat each maps to. GetDepthStencilFormats offers +// the combinations the device supports, and GetVkDepthStencilFormat resolves a selected one, +// so both always agree. kDisplay_Depth_Format_16F has no Vulkan format and is never offered. +struct DepthStencilFormatVk { + DisplayManager::DisplayDepthFormat depth; + DisplayManager::DisplayStencilFormat stencil; + VkFormat format; }; - -static constexpr VkFormat kStencilFormatTableVk[DisplayManager::kDisplay_Stencil_Format_Count] = { - VK_FORMAT_D24_UNORM_S8_UINT, // kDisplay_Stencil_Format_D24S8_Packed - VK_FORMAT_S8_UINT // kDisplay_Stencil_Format_S8 +static constexpr DepthStencilFormatVk kDepthStencilFormatTableVk[] = { + {DisplayManager::kDisplay_Depth_Format_D24S8_Packed, + DisplayManager::kDisplay_Stencil_Format_D24S8_Packed, VK_FORMAT_D24_UNORM_S8_UINT}, + {DisplayManager::kDisplay_Depth_Format_None, + DisplayManager::kDisplay_Stencil_Format_S8, VK_FORMAT_S8_UINT}, + {DisplayManager::kDisplay_Depth_Format_16U, + DisplayManager::kDisplay_Stencil_Format_None, VK_FORMAT_D16_UNORM}, + {DisplayManager::kDisplay_Depth_Format_16U, + DisplayManager::kDisplay_Stencil_Format_S8, VK_FORMAT_D16_UNORM_S8_UINT}, + {DisplayManager::kDisplay_Depth_Format_32F, + DisplayManager::kDisplay_Stencil_Format_None, VK_FORMAT_D32_SFLOAT}, + {DisplayManager::kDisplay_Depth_Format_32F, + DisplayManager::kDisplay_Stencil_Format_S8, VK_FORMAT_D32_SFLOAT_S8_UINT} }; bool VulkanAPIUtils::CheckVkResult(const VkResult result, const char *message) { @@ -113,57 +124,20 @@ bool VulkanAPIUtils::GetImageFormatSupported(VkPhysicalDevice physical_device, void VulkanAPIUtils::GetDepthStencilFormats(VkPhysicalDevice physical_device, std::vector &depth_stencil_formats) { + depth_stencil_formats.clear(); // Color format and color space are placeholder for our depth/stencil permutations DisplayManager::DisplayFormat format; // Always have none/none available for depth/stencil depth_stencil_formats.push_back(format); - if (VulkanAPIUtils::GetImageFormatSupported(physical_device, VK_FORMAT_D24_UNORM_S8_UINT, - VK_IMAGE_TILING_OPTIMAL, - VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) { - format.display_depth_format = DisplayManager::kDisplay_Depth_Format_D24S8_Packed; - format.display_stencil_format = DisplayManager::kDisplay_Stencil_Format_D24S8_Packed; - depth_stencil_formats.push_back(format); - } - - if (VulkanAPIUtils::GetImageFormatSupported(physical_device, VK_FORMAT_S8_UINT, - VK_IMAGE_TILING_OPTIMAL, - VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) { - format.display_depth_format = DisplayManager::kDisplay_Depth_Format_None; - format.display_stencil_format = DisplayManager::kDisplay_Stencil_Format_S8; - depth_stencil_formats.push_back(format); - } - - if (VulkanAPIUtils::GetImageFormatSupported(physical_device, VK_FORMAT_D16_UNORM, - VK_IMAGE_TILING_OPTIMAL, - VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) { - format.display_depth_format = DisplayManager::kDisplay_Depth_Format_16U; - format.display_stencil_format = DisplayManager::kDisplay_Stencil_Format_None; - depth_stencil_formats.push_back(format); - } - - if (VulkanAPIUtils::GetImageFormatSupported(physical_device, VK_FORMAT_D16_UNORM_S8_UINT, - VK_IMAGE_TILING_OPTIMAL, - VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) { - format.display_depth_format = DisplayManager::kDisplay_Depth_Format_16U; - format.display_stencil_format = DisplayManager::kDisplay_Stencil_Format_S8; - depth_stencil_formats.push_back(format); - } - - if (VulkanAPIUtils::GetImageFormatSupported(physical_device, VK_FORMAT_D32_SFLOAT, - VK_IMAGE_TILING_OPTIMAL, - VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) { - format.display_depth_format = DisplayManager::kDisplay_Depth_Format_32F; - format.display_stencil_format = DisplayManager::kDisplay_Stencil_Format_None; - depth_stencil_formats.push_back(format); - } - - if (VulkanAPIUtils::GetImageFormatSupported(physical_device, VK_FORMAT_D32_SFLOAT_S8_UINT, - VK_IMAGE_TILING_OPTIMAL, - VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) { - format.display_depth_format = DisplayManager::kDisplay_Depth_Format_32F; - format.display_stencil_format = DisplayManager::kDisplay_Stencil_Format_S8; - depth_stencil_formats.push_back(format); + for (const DepthStencilFormatVk &entry : kDepthStencilFormatTableVk) { + if (VulkanAPIUtils::GetImageFormatSupported(physical_device, entry.format, + VK_IMAGE_TILING_OPTIMAL, + VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT)) { + format.display_depth_format = entry.depth; + format.display_stencil_format = entry.stencil; + depth_stencil_formats.push_back(format); + } } } @@ -274,13 +248,35 @@ bool VulkanAPIUtils::GetUsePhysicalDeviceProperties2() { return true; } -VkFormat VulkanAPIUtils::GetVkDepthFormat(const DisplayManager::DisplayDepthFormat depth_format) { - if (depth_format >= 0) { - return kDepthFormatTableVk[depth_format]; +VkFormat VulkanAPIUtils::GetVkDepthStencilFormat( + const DisplayManager::DisplayDepthFormat depth_format, + const DisplayManager::DisplayStencilFormat stencil_format) { + for (const DepthStencilFormatVk &entry : kDepthStencilFormatTableVk) { + if (entry.depth == depth_format && entry.stencil == stencil_format) { + return entry.format; + } } + // None/none, or a combination with no Vulkan format return VK_FORMAT_UNDEFINED; } +VkImageAspectFlags VulkanAPIUtils::GetVkDepthStencilAspectMask(const VkFormat format) { + switch (format) { + case VK_FORMAT_D16_UNORM: + case VK_FORMAT_X8_D24_UNORM_PACK32: + case VK_FORMAT_D32_SFLOAT: + return VK_IMAGE_ASPECT_DEPTH_BIT; + case VK_FORMAT_S8_UINT: + return VK_IMAGE_ASPECT_STENCIL_BIT; + case VK_FORMAT_D16_UNORM_S8_UINT: + case VK_FORMAT_D24_UNORM_S8_UINT: + case VK_FORMAT_D32_SFLOAT_S8_UINT: + return VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; + default: + return 0; + } +} + VkFormat VulkanAPIUtils::GetVkPixelFormat(const DisplayManager::DisplayPixelFormat pixel_format, const DisplayManager::DisplayColorSpace color_space) { if (pixel_format >= 0 && pixel_format < DisplayManager::kDisplay_Pixel_Format_Count) { @@ -290,14 +286,6 @@ VkFormat VulkanAPIUtils::GetVkPixelFormat(const DisplayManager::DisplayPixelForm return VK_FORMAT_UNDEFINED; } -VkFormat VulkanAPIUtils::GetVkStencilFormat( - const DisplayManager::DisplayStencilFormat stencil_format) { - if (stencil_format >= 0) { - return kStencilFormatTableVk[stencil_format]; - } - return VK_FORMAT_UNDEFINED; -} - VkColorSpaceKHR VulkanAPIUtils::GetVkColorSpace(const DisplayManager::DisplayColorSpace color_space) { // DisplayColorSpace selects whether the swapchain VkFormat sRGB-encodes writes (see // GetVkPixelFormat). Either way the presentation engine interprets the stored values as diff --git a/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan_utils.h b/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan_utils.h index 62939732..266a0649 100644 --- a/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan_utils.h +++ b/agdk/common/base_game_framework/src/vulkan/graphics_api_vulkan_utils.h @@ -38,11 +38,14 @@ class VulkanAPIUtils { static const char *GetMessageSeverityString(VkDebugUtilsMessageSeverityFlagBitsEXT s); static const char *GetMessageTypeString(VkDebugUtilsMessageTypeFlagsEXT s); static bool GetUsePhysicalDeviceProperties2(); - static VkFormat GetVkDepthFormat(const DisplayManager::DisplayDepthFormat depth_format); + // Returns VK_FORMAT_UNDEFINED for none/none or a combination with no Vulkan format + static VkFormat GetVkDepthStencilFormat(const DisplayManager::DisplayDepthFormat depth_format, + const DisplayManager::DisplayStencilFormat stencil_format); + // Depth and/or stencil aspect bits of a depth/stencil format, 0 for other formats + static VkImageAspectFlags GetVkDepthStencilAspectMask(const VkFormat format); // Returns VK_FORMAT_UNDEFINED if the pixel format has no variant in the color space static VkFormat GetVkPixelFormat(const DisplayManager::DisplayPixelFormat pixel_format, const DisplayManager::DisplayColorSpace color_space); - static VkFormat GetVkStencilFormat(const DisplayManager::DisplayStencilFormat stencil_format); static VkColorSpaceKHR GetVkColorSpace(const DisplayManager::DisplayColorSpace color_space); static bool HasDeviceExtension(const std::vector &available_extensions, const char *extension_name);