diff --git a/agdk/common/base_game_framework/include/display_manager.h b/agdk/common/base_game_framework/include/display_manager.h index c94c94d8..771923d5 100644 --- a/agdk/common/base_game_framework/include/display_manager.h +++ b/agdk/common/base_game_framework/include/display_manager.h @@ -375,7 +375,10 @@ class DisplayManager { const std::vector& display_swap_intervals; /** @brief The minimum number of frame images configurable by the display swapchain */ const uint32_t min_swapchain_frame_count; - /** @brief The maximum number of frame images configurable by the display swapchain */ + /** + * @brief The maximum number of frame images configurable by the display swapchain. + * 0 means there is no upper limit (Vulkan `maxImageCount` of 0). + */ const uint32_t max_swapchain_frame_count; /** @brief A bitmask of `SwapchainPresentMode` values of supported present modes */ const uint32_t swapchain_present_modes; 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 964c1649..f2f60e13 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 @@ -18,6 +18,7 @@ #include "debug_manager.h" #include "graphics_api_vulkan_utils.h" #include "platform_util_vulkan.h" +#include #include namespace base_game_framework { @@ -80,7 +81,8 @@ GraphicsAPIVulkan::GraphicsAPIVulkan() pretransform_flags_(VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR), surface_capabilities_{}, allocator_(VK_NULL_HANDLE), - use_physical_device_properties2_(false) { + use_physical_device_properties2_(false), + validation_layers_active_(false) { } GraphicsAPIVulkan::~GraphicsAPIVulkan() { @@ -170,6 +172,10 @@ void GraphicsAPIVulkan::QueryCapabilities() { void GraphicsAPIVulkan::QueryDeviceCapabilities(VkPhysicalDevice physical_device) { // If we got this far, we at least support a Vulkan 1.0 device feature_flags_ = DisplayManager::kVulkan_1_0_Support; + enabled_device_extensions_.clear(); + supported_float16_int8_features_ = {}; + supported_16bit_storage_features_ = {}; + api_features_.SetFeatureBits(0); api_features_.SetActiveAPIVersion(VK_API_VERSION_1_0); if (!use_physical_device_properties2_) { @@ -192,16 +198,6 @@ void GraphicsAPIVulkan::QueryDeviceCapabilities(VkPhysicalDevice physical_device // Optional extensions we may query const bool has_driver_properties = VulkanAPIUtils::HasDeviceExtension(available_extensions, VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME); - const bool has_shader_float16_int8 = VulkanAPIUtils::HasDeviceExtension(available_extensions, - VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME); - if (has_shader_float16_int8) { - enabled_device_extensions_.push_back(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME); - } - const bool has_16bit_storage = VulkanAPIUtils::HasDeviceExtension(available_extensions, - VK_KHR_16BIT_STORAGE_EXTENSION_NAME); - if (has_16bit_storage) { - enabled_device_extensions_.push_back(VK_KHR_16BIT_STORAGE_EXTENSION_NAME); - } // Retrieve device properties, including driver properties if available VkPhysicalDeviceProperties2KHR device_properties{}; @@ -228,6 +224,32 @@ void GraphicsAPIVulkan::QueryDeviceCapabilities(VkPhysicalDevice physical_device driver_id_ = device_driver_properties.driverID; } + // The API version decides whether the reduced precision features are core or need extensions + const uint32_t api_version = PlatformUtilVulkan::GetVulkanApiVersion(); + DetermineAPILevel(api_version, device_properties.properties.apiVersion); + const uint32_t active_api_version = api_features_.GetActiveAPIVersion(); + + // 16-bit storage is core in Vulkan 1.1. On 1.0 it needs VK_KHR_16bit_storage, which itself + // requires VK_KHR_storage_buffer_storage_class. + bool use_16bit_storage = true; + std::vector storage_16bit_extensions; + if (active_api_version < VK_API_VERSION_1_1) { + storage_16bit_extensions = {VK_KHR_16BIT_STORAGE_EXTENSION_NAME, + VK_KHR_STORAGE_BUFFER_STORAGE_CLASS_EXTENSION_NAME}; + for (const char *extension : storage_16bit_extensions) { + use_16bit_storage &= VulkanAPIUtils::HasDeviceExtension(available_extensions, extension); + } + } + // Float16/Int8 shader math is core in Vulkan 1.2, before that it needs + // VK_KHR_shader_float16_int8. + bool use_float16_int8 = true; + std::vector float16_int8_extensions; + if (active_api_version < VK_API_VERSION_1_2) { + float16_int8_extensions = {VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME}; + use_float16_int8 = VulkanAPIUtils::HasDeviceExtension(available_extensions, + VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME); + } + // Retrieve device features, including optional extensions on reduced precision formats VkPhysicalDeviceFeatures2KHR device_features{}; device_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR; @@ -238,14 +260,16 @@ void GraphicsAPIVulkan::QueryDeviceCapabilities(VkPhysicalDevice physical_device device_16_bit_storage_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES_KHR; // Chain the features structures - if (has_16bit_storage) { - device_features.pNext = &device_16_bit_storage_features; - if (has_shader_float16_int8) { - device_16_bit_storage_features.pNext = &shader_float_16_int_8_features; - } - } else if (has_shader_float16_int8) { - device_features.pNext = &shader_float_16_int_8_features; + void *feature_chain = nullptr; + if (use_float16_int8) { + shader_float_16_int_8_features.pNext = feature_chain; + feature_chain = &shader_float_16_int_8_features; } + if (use_16bit_storage) { + device_16_bit_storage_features.pNext = feature_chain; + feature_chain = &device_16_bit_storage_features; + } + device_features.pNext = feature_chain; if (vkGetPhysicalDeviceFeatures2 != nullptr) { vkGetPhysicalDeviceFeatures2(physical_device, &device_features); @@ -263,16 +287,45 @@ void GraphicsAPIVulkan::QueryDeviceCapabilities(VkPhysicalDevice physical_device api_features_.SetGraphicsFeature(GraphicsAPIFeatures::kGraphicsFeature_Wide_Lines); } - const uint32_t api_version = PlatformUtilVulkan::GetVulkanApiVersion(); - DetermineAPILevel(api_version, device_properties.properties.apiVersion); - // Determine if the device has ETC2 texture support if (device_features.features.textureCompressionETC2) { feature_flags_ |= DisplayManager::kVulkan_ETC2_Support; } + // Ignore the feature structs entirely if they report no supported features, so + // CreateDevice doesn't chain them or enable their extensions for nothing + shader_float_16_int_8_features.pNext = nullptr; + device_16_bit_storage_features.pNext = nullptr; + if (!use_float16_int8 || !(shader_float_16_int_8_features.shaderFloat16 || + shader_float_16_int_8_features.shaderInt8)) { + shader_float_16_int_8_features = {}; + use_float16_int8 = false; + } + if (!use_16bit_storage || !(device_16_bit_storage_features.storageBuffer16BitAccess || + device_16_bit_storage_features.uniformAndStorageBuffer16BitAccess || + device_16_bit_storage_features.storagePushConstant16 || + device_16_bit_storage_features.storageInputOutput16)) { + device_16_bit_storage_features = {}; + use_16bit_storage = false; + } + DetermineNumericSupport(device_features.features.shaderInt16, shader_float_16_int_8_features, device_16_bit_storage_features); + + // Keep the supported feature bits (and any extensions they need) so CreateDevice can enable + // what we advertise + if (use_float16_int8) { + supported_float16_int8_features_ = shader_float_16_int_8_features; + enabled_device_extensions_.insert(enabled_device_extensions_.end(), + float16_int8_extensions.begin(), + float16_int8_extensions.end()); + } + if (use_16bit_storage) { + supported_16bit_storage_features_ = device_16_bit_storage_features; + enabled_device_extensions_.insert(enabled_device_extensions_.end(), + storage_16bit_extensions.begin(), + storage_16bit_extensions.end()); + } // TODO: Android Baseline Profile detection } @@ -387,13 +440,17 @@ bool GraphicsAPIVulkan::DisplayFormatExists(const DisplayManager::DisplayFormat } bool GraphicsAPIVulkan::InitializeGraphicsAPI() { - if (!PlatformUtilVulkan::GetValidationLayersAvailable()) { - // Disable request for validation layers if they aren't available - GraphicsAPIVulkan::enable_validation_layers_ = false; - DebugManager::Log(DebugManager::kLog_Channel_Default, - DebugManager::kLog_Level_Error, - BGM_CLASS_TAG, - "Vulkan validation layers were requested but unavailable!"); + // Use validation layers for this API session only if requested and available; don't + // overwrite the caller's request, so a later session checks availability again + validation_layers_active_ = false; + if (GraphicsAPIVulkan::enable_validation_layers_) { + validation_layers_active_ = PlatformUtilVulkan::GetValidationLayersAvailable(); + if (!validation_layers_active_) { + DebugManager::Log(DebugManager::kLog_Channel_Default, + DebugManager::kLog_Level_Error, + BGM_CLASS_TAG, + "Vulkan validation layers were requested but unavailable!"); + } } CreateInstance(false); if (vk_instance_ != VK_NULL_HANDLE) { @@ -401,6 +458,9 @@ bool GraphicsAPIVulkan::InitializeGraphicsAPI() { if (vk_surface_ != VK_NULL_HANDLE) { vk_physical_device_ = GetPhysicalDevice(); if (vk_physical_device_ != VK_NULL_HANDLE) { + // QueryCapabilities used a temporary instance, so query the capabilities again on + // this instance's physical device; these decide what CreateDevice enables + QueryDeviceCapabilities(vk_physical_device_); QueueFamilyIndices queue_indices = VulkanAPIUtils::FindQueueFamilies( vk_physical_device_, vk_surface_); if (CreateDevice(false, queue_indices)) { @@ -418,19 +478,21 @@ bool GraphicsAPIVulkan::InitializeGraphicsAPI() { allocator_info.pVulkanFunctions = &vulkan_functions; VkResult vma_result = vmaCreateAllocator(&allocator_info, &allocator_); - if (vma_result != VK_SUCCESS) { - DebugManager::Log(DebugManager::kLog_Channel_Default, - DebugManager::kLog_Level_Error, - BGM_CLASS_TAG, - "vmaCreateAllocator failed: %d", vma_result); - return false; + if (vma_result == VK_SUCCESS) { + api_status_ = kGraphicsAPI_Active; + return true; } - api_status_ = kGraphicsAPI_Active; - return true; + allocator_ = VK_NULL_HANDLE; + DebugManager::Log(DebugManager::kLog_Channel_Default, + DebugManager::kLog_Level_Error, + BGM_CLASS_TAG, + "vmaCreateAllocator failed: %d", vma_result); } } } } + // Release whatever was created before the failure + ShutdownGraphicsAPI(); return false; } @@ -454,7 +516,10 @@ void GraphicsAPIVulkan::ShutdownGraphicsAPI() { debug_messenger_ = VK_NULL_HANDLE; } + // The physical device handle belongs to the instance + vk_physical_device_ = VK_NULL_HANDLE; DestroyInstance(); + validation_layers_active_ = false; api_status_ = kGraphicsAPI_AvailabilityReady; } @@ -520,8 +585,9 @@ DisplayManager::InitSwapchainResult GraphicsAPIVulkan::InitSwapchain( return DisplayManager::kInit_Swapchain_Failure; } - if (!(swapchain_frame_count >= swapchain_min_frames_ && - swapchain_frame_count <= swapchain_max_frames_)) { + // A maxImageCount of 0 means the surface has no upper limit on image count + if (swapchain_frame_count < swapchain_min_frames_ || + (swapchain_max_frames_ != 0 && swapchain_frame_count > swapchain_max_frames_)) { DebugManager::Log(DebugManager::kLog_Channel_Default, DebugManager::kLog_Level_Error, BGM_CLASS_TAG, @@ -665,6 +731,7 @@ DisplayManager::SwapchainFrameHandle GraphicsAPIVulkan::PresentCurrentSwapchainF bool GraphicsAPIVulkan::GetGraphicsAPIResourcesVk(GraphicsAPIResourcesVk &api_resources_vk) { api_resources_vk.device = vk_device_; api_resources_vk.instance = vk_instance_; + api_resources_vk.physical_device = vk_physical_device_; api_resources_vk.render_queue = vk_graphics_queue_; api_resources_vk.allocator = allocator_; api_resources_vk.graphics_queue_index = graphics_queue_index_; @@ -775,7 +842,7 @@ bool GraphicsAPIVulkan::CreateDevice(bool is_preflight_check, const QueueFamilyIndices &queue_indices) { // Skip validation layers if we are just creating a preflight device to query // device extensions and capabilities - const bool enable_validation = !is_preflight_check && enable_validation_layers_; + const bool enable_validation = !is_preflight_check && validation_layers_active_; graphics_queue_index_ = queue_indices.graphics_family.value(); present_queue_index_ = queue_indices.present_family.value(); @@ -810,31 +877,62 @@ bool GraphicsAPIVulkan::CreateDevice(bool is_preflight_check, device_features.wideLines = VK_TRUE; } - const std::vector required_device_extensions = + std::vector device_extensions = PlatformUtilVulkan::GetRequiredDeviceExtensions(vk_physical_device_); VkDeviceCreateInfo device_create_info{}; device_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; + + // The real device enables the optional extensions QueryDeviceCapabilities found, plus the + // supported features of each, so features advertised through api_features_ (F16/I8 math, + // 16-bit storage) are actually usable. The preflight device only needs the basics. + VkPhysicalDeviceShaderFloat16Int8FeaturesKHR float16_int8_features{}; + VkPhysicalDevice16BitStorageFeaturesKHR storage_16bit_features{}; + if (!is_preflight_check) { + for (const char *extension : enabled_device_extensions_) { + bool already_enabled = false; + for (const char *enabled : device_extensions) { + if (strcmp(enabled, extension) == 0) { + already_enabled = true; + break; + } + } + if (!already_enabled) { + device_extensions.push_back(extension); + } + } + void *feature_chain = nullptr; + if (supported_float16_int8_features_.sType != 0) { + float16_int8_features = supported_float16_int8_features_; + float16_int8_features.pNext = feature_chain; + feature_chain = &float16_int8_features; + } + if (supported_16bit_storage_features_.sType != 0) { + storage_16bit_features = supported_16bit_storage_features_; + storage_16bit_features.pNext = feature_chain; + feature_chain = &storage_16bit_features; + } + device_create_info.pNext = feature_chain; + } + device_create_info.queueCreateInfoCount = static_cast(queue_create_infos.size()); device_create_info.pQueueCreateInfos = queue_create_infos.data(); device_create_info.pEnabledFeatures = &device_features; - device_create_info.enabledExtensionCount = - static_cast(required_device_extensions.size()); - device_create_info.ppEnabledExtensionNames = required_device_extensions.data(); - - std::vector validation_layers = PlatformUtilVulkan::GetValidationLayers(); - if (enable_validation) { - device_create_info.enabledLayerCount = - static_cast(validation_layers.size()); - device_create_info.ppEnabledLayerNames = validation_layers.data(); - } else { - device_create_info.enabledLayerCount = 0; - } - - VulkanAPIUtils::CheckVkResult( + device_create_info.enabledExtensionCount = static_cast(device_extensions.size()); + device_create_info.ppEnabledExtensionNames = device_extensions.data(); + // Device layers are deprecated and must not be set + // (VUID-VkDeviceCreateInfo-enabledLayerCount-12384); the instance layers enabled in + // CreateInstance validate device calls too + device_create_info.enabledLayerCount = 0; + device_create_info.ppEnabledLayerNames = nullptr; + + if (!VulkanAPIUtils::CheckVkResult( vkCreateDevice(vk_physical_device_, &device_create_info, nullptr, &vk_device_), - "vkCreateDevice"); + "vkCreateDevice")) { + vk_device_ = VK_NULL_HANDLE; + return false; + } vkGetDeviceQueue(vk_device_, queue_indices.graphics_family.value(), 0, &vk_graphics_queue_); vkGetDeviceQueue(vk_device_, queue_indices.present_family.value(), 0, &vk_present_queue_); @@ -863,7 +961,7 @@ void GraphicsAPIVulkan::DestroyDevice() { void GraphicsAPIVulkan::CreateInstance(bool is_preflight_check) { // Skip validation layers if we are just creating a preflight instance to query // device extensions and capabilities - const bool request_validation = !is_preflight_check && enable_validation_layers_; + const bool request_validation = !is_preflight_check && validation_layers_active_; const std::vector instance_extensions = PlatformUtilVulkan::GetRequiredInstanceExtensions(request_validation, @@ -923,7 +1021,8 @@ bool GraphicsAPIVulkan::CreateSwapchain(const QueueFamilyIndices &queue_indices) uint32_t queue_family_indices[] = {queue_indices.graphics_family.value(), queue_indices.present_family.value()}; swapchain_info_.swapchain_color_format_ = - VulkanAPIUtils::GetVkPixelFormat(swapchain_format_.display_pixel_format); + 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); @@ -1017,7 +1116,7 @@ void GraphicsAPIVulkan::CreateSwapchainImages() { swapchain_info_.swapchain_image_views_.resize(swapchain_info_.swapchain_image_count_); VkImageView *base_view = swapchain_info_.swapchain_image_views_.data(); const VkFormat pixel_format = VulkanAPIUtils::GetVkPixelFormat( - swapchain_format_.display_pixel_format); + swapchain_format_.display_pixel_format, swapchain_format_.display_color_space); for (size_t i = 0; i < swapchain_info_.swapchain_image_count_; ++i) { VkImageViewCreateInfo create_info{}; 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 e11d5b5e..434bd90e 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 @@ -150,7 +150,11 @@ class GraphicsAPIVulkan : public GraphicsAPIBase { std::vector display_resolutions_; std::vector swap_intervals_; + // Optional device extensions found by QueryDeviceCapabilities, enabled on the real device + // in CreateDevice along with the matching supported features below. std::vector enabled_device_extensions_; + VkPhysicalDeviceShaderFloat16Int8FeaturesKHR supported_float16_int8_features_{}; + VkPhysicalDevice16BitStorageFeaturesKHR supported_16bit_storage_features_{}; GraphicsAPIStatus api_status_; GraphicsAPIFeatures api_features_; @@ -193,6 +197,9 @@ class GraphicsAPIVulkan : public GraphicsAPIBase { // Extension bools bool use_physical_device_properties2_; + // Validation layers are in use by the active instance/device: requested via + // SetValidationLayersEnabled and available when the API was initialized + bool validation_layers_active_; static constexpr const char *BGM_CLASS_TAG = "BGF::GraphicsAPIVulkan"; static bool enable_validation_layers_; 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 8037edee..b0e59a9a 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 @@ -22,15 +22,22 @@ namespace base_game_framework { -static constexpr VkFormat kColorFormatTableVk[DisplayManager::kDisplay_Pixel_Format_Count] = { - VK_FORMAT_R8G8B8A8_SRGB, // kDisplay_Pixel_Format_RGBA8 - VK_FORMAT_B8G8R8A8_SRGB, // kDisplay_Pixel_Format_BGRA8 - VK_FORMAT_R16G16B16A16_SFLOAT, // kDisplay_Pixel_Format_RGBA_16F - VK_FORMAT_R32G32B32A32_SFLOAT, // kDisplay_Pixel_Format_RGBA_32F - VK_FORMAT_R4G4B4A4_UNORM_PACK16, // kDisplay_Pixel_Format_RGBA4 - VK_FORMAT_R5G5B5A1_UNORM_PACK16, // kDisplay_Pixel_Format_RGBA5551 - VK_FORMAT_R5G6B5_UNORM_PACK16, // kDisplay_Pixel_Format_RGBA565 - VK_FORMAT_A2B10G10R10_UNORM_PACK32 // kDisplay_Pixel_Format_ABGR2_10_10_10 +// Color formats by pixel format and color space. As with GLES, kDisplay_Color_Space_SRGB means +// the hardware sRGB-encodes fragment writes (an _SRGB VkFormat), and kDisplay_Color_Space_Linear +// means values are written as-is. Only the 8-bit formats have an _SRGB variant. +struct ColorFormatVk { + VkFormat linear; + VkFormat srgb; +}; +static constexpr ColorFormatVk kColorFormatTableVk[DisplayManager::kDisplay_Pixel_Format_Count] = { + {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_SRGB}, // kDisplay_Pixel_Format_RGBA8 + {VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_B8G8R8A8_SRGB}, // kDisplay_Pixel_Format_BGRA8 + {VK_FORMAT_R16G16B16A16_SFLOAT, VK_FORMAT_UNDEFINED}, // kDisplay_Pixel_Format_RGBA_16F + {VK_FORMAT_R32G32B32A32_SFLOAT, VK_FORMAT_UNDEFINED}, // kDisplay_Pixel_Format_RGBA_32F + {VK_FORMAT_R4G4B4A4_UNORM_PACK16, VK_FORMAT_UNDEFINED}, // kDisplay_Pixel_Format_RGBA4 + {VK_FORMAT_R5G5B5A1_UNORM_PACK16, VK_FORMAT_UNDEFINED}, // kDisplay_Pixel_Format_RGBA5551 + {VK_FORMAT_R5G6B5_UNORM_PACK16, VK_FORMAT_UNDEFINED}, // kDisplay_Pixel_Format_RGB565 + {VK_FORMAT_A2B10G10R10_UNORM_PACK32, VK_FORMAT_UNDEFINED} // kDisplay_Pixel_Format_ABGR2_10_10_10 }; static constexpr VkFormat kDepthFormatTableVk[DisplayManager::kDisplay_Depth_Format_Count] = { @@ -172,49 +179,36 @@ void VulkanAPIUtils::GetDisplayFormats(VkPhysicalDevice physical_device, VkSurfa vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface, &surface_format_count, surface_formats.data()); - std::vector pixel_formats; - DisplayManager::DisplayFormat current_format; - // Only supporting SRGB for display formats right now - current_format.display_color_space = DisplayManager::kDisplay_Color_Space_SRGB; - - for (const VkSurfaceFormatKHR &format : surface_formats) { - if (format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { - switch (format.format) { - case VK_FORMAT_R8G8B8A8_UNORM: - case VK_FORMAT_R8G8B8A8_SRGB:pixel_formats.push_back( - DisplayManager::kDisplay_Pixel_Format_RGBA8); - break; - case VK_FORMAT_B8G8R8A8_UNORM: - case VK_FORMAT_B8G8R8A8_SRGB:pixel_formats.push_back( - DisplayManager::kDisplay_Pixel_Format_BGRA8); - break; - case VK_FORMAT_R16G16B16A16_SFLOAT:pixel_formats.push_back( - DisplayManager::kDisplay_Pixel_Format_RGBA_16F); - break; - case VK_FORMAT_R32G32B32A32_SFLOAT:pixel_formats.push_back( - DisplayManager::kDisplay_Pixel_Format_RGBA_32F); - break; - case VK_FORMAT_R4G4B4A4_UNORM_PACK16:pixel_formats.push_back( - DisplayManager::kDisplay_Pixel_Format_RGBA4); - break; - case VK_FORMAT_R5G5B5A1_UNORM_PACK16:pixel_formats.push_back( - DisplayManager::kDisplay_Pixel_Format_RGBA5551); - break; - case VK_FORMAT_R5G6B5_UNORM_PACK16:pixel_formats.push_back( - DisplayManager::kDisplay_Pixel_Format_RGB565); - break; - case VK_FORMAT_A2B10G10R10_UNORM_PACK32:pixel_formats.push_back( - DisplayManager::kDisplay_Pixel_Format_ABGR2_10_10_10); + // Offer a pixel format/color space pair only if the surface supports the exact VkFormat + // that GetVkPixelFormat maps it to (e.g. RGBA8 sRGB is VK_FORMAT_R8G8B8A8_SRGB, so a surface + // that only reports R8G8B8A8_UNORM offers RGBA8 linear but not RGBA8 sRGB). Each pair is + // listed once, even if the surface reports a format more than once. + static constexpr DisplayManager::DisplayColorSpace kColorSpaces[] = { + DisplayManager::kDisplay_Color_Space_Linear, DisplayManager::kDisplay_Color_Space_SRGB}; + std::vector color_formats; + for (int32_t i = 0; i < DisplayManager::kDisplay_Pixel_Format_Count; ++i) { + const DisplayManager::DisplayPixelFormat pixel_format = + static_cast(i); + for (const DisplayManager::DisplayColorSpace color_space : kColorSpaces) { + const VkFormat vk_format = GetVkPixelFormat(pixel_format, color_space); + if (vk_format == VK_FORMAT_UNDEFINED) { + continue; + } + for (const VkSurfaceFormatKHR &format : surface_formats) { + if (format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR && format.format == vk_format) { + DisplayManager::DisplayFormat color_format; + color_format.display_pixel_format = pixel_format; + color_format.display_color_space = color_space; + color_formats.push_back(color_format); break; - default:break; + } } } } // Build all the permutations of depth/stencil with each color format - display_formats.reserve(pixel_formats.size() * depth_stencil_formats.size()); - for (const DisplayManager::DisplayPixelFormat pixel_format : pixel_formats) { - current_format.display_pixel_format = pixel_format; + display_formats.reserve(color_formats.size() * depth_stencil_formats.size()); + for (DisplayManager::DisplayFormat current_format : color_formats) { for (const DisplayManager::DisplayFormat &depth_stencil_format : depth_stencil_formats) { current_format.display_depth_format = depth_stencil_format.display_depth_format; current_format.display_stencil_format = depth_stencil_format.display_stencil_format; @@ -287,9 +281,11 @@ VkFormat VulkanAPIUtils::GetVkDepthFormat(const DisplayManager::DisplayDepthForm return VK_FORMAT_UNDEFINED; } -VkFormat VulkanAPIUtils::GetVkPixelFormat(const DisplayManager::DisplayPixelFormat pixel_format) { - if (pixel_format >= 0) { - return kColorFormatTableVk[pixel_format]; +VkFormat VulkanAPIUtils::GetVkPixelFormat(const DisplayManager::DisplayPixelFormat pixel_format, + const DisplayManager::DisplayColorSpace color_space) { + if (pixel_format >= 0 && pixel_format < DisplayManager::kDisplay_Pixel_Format_Count) { + return (color_space == DisplayManager::kDisplay_Color_Space_SRGB) ? + kColorFormatTableVk[pixel_format].srgb : kColorFormatTableVk[pixel_format].linear; } return VK_FORMAT_UNDEFINED; } @@ -303,13 +299,11 @@ VkFormat VulkanAPIUtils::GetVkStencilFormat( } VkColorSpaceKHR VulkanAPIUtils::GetVkColorSpace(const DisplayManager::DisplayColorSpace color_space) { - VkColorSpaceKHR vk_color_space = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; - switch (color_space) { - case DisplayManager::kDisplay_Color_Space_SRGB: - // Don't actually have a real linear format here? - case DisplayManager::kDisplay_Color_Space_Linear:break; - } - return vk_color_space; + // DisplayColorSpace selects whether the swapchain VkFormat sRGB-encodes writes (see + // GetVkPixelFormat). Either way the presentation engine interprets the stored values as + // sRGB-encoded, so the presentation color space is always SRGB_NONLINEAR. + (void)color_space; + return VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; } bool VulkanAPIUtils::HasDeviceExtension( 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 5a6b51aa..62939732 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 @@ -39,7 +39,9 @@ class VulkanAPIUtils { static const char *GetMessageTypeString(VkDebugUtilsMessageTypeFlagsEXT s); static bool GetUsePhysicalDeviceProperties2(); static VkFormat GetVkDepthFormat(const DisplayManager::DisplayDepthFormat depth_format); - static VkFormat GetVkPixelFormat(const DisplayManager::DisplayPixelFormat pixel_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,