Merge pull request #6941 from ameerj/swapchain-srgb
vk_swapchain: Prefer linear swapchain format when presenting sRGB images
This commit is contained in:
		| @@ -358,7 +358,7 @@ void VKBlitScreen::CreateDescriptorPool() { | ||||
| void VKBlitScreen::CreateRenderPass() { | ||||
|     const VkAttachmentDescription color_attachment{ | ||||
|         .flags = 0, | ||||
|         .format = swapchain.GetImageFormat(), | ||||
|         .format = swapchain.GetImageViewFormat(), | ||||
|         .samples = VK_SAMPLE_COUNT_1_BIT, | ||||
|         .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR, | ||||
|         .storeOp = VK_ATTACHMENT_STORE_OP_STORE, | ||||
|   | ||||
| @@ -20,16 +20,15 @@ namespace Vulkan { | ||||
|  | ||||
| namespace { | ||||
|  | ||||
| VkSurfaceFormatKHR ChooseSwapSurfaceFormat(vk::Span<VkSurfaceFormatKHR> formats, bool srgb) { | ||||
| VkSurfaceFormatKHR ChooseSwapSurfaceFormat(vk::Span<VkSurfaceFormatKHR> formats) { | ||||
|     if (formats.size() == 1 && formats[0].format == VK_FORMAT_UNDEFINED) { | ||||
|         VkSurfaceFormatKHR format; | ||||
|         format.format = VK_FORMAT_B8G8R8A8_UNORM; | ||||
|         format.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; | ||||
|         return format; | ||||
|     } | ||||
|     const auto& found = std::find_if(formats.begin(), formats.end(), [srgb](const auto& format) { | ||||
|         const auto request_format = srgb ? VK_FORMAT_B8G8R8A8_SRGB : VK_FORMAT_B8G8R8A8_UNORM; | ||||
|         return format.format == request_format && | ||||
|     const auto& found = std::find_if(formats.begin(), formats.end(), [](const auto& format) { | ||||
|         return format.format == VK_FORMAT_B8G8R8A8_UNORM && | ||||
|                format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; | ||||
|     }); | ||||
|     return found != formats.end() ? *found : formats[0]; | ||||
| @@ -143,7 +142,7 @@ void VKSwapchain::CreateSwapchain(const VkSurfaceCapabilitiesKHR& capabilities, | ||||
|     const auto formats{physical_device.GetSurfaceFormatsKHR(surface)}; | ||||
|     const auto present_modes{physical_device.GetSurfacePresentModesKHR(surface)}; | ||||
|  | ||||
|     const VkSurfaceFormatKHR surface_format{ChooseSwapSurfaceFormat(formats, srgb)}; | ||||
|     const VkSurfaceFormatKHR surface_format{ChooseSwapSurfaceFormat(formats)}; | ||||
|     const VkPresentModeKHR present_mode{ChooseSwapPresentMode(present_modes)}; | ||||
|  | ||||
|     u32 requested_image_count{capabilities.minImageCount + 1}; | ||||
| @@ -178,6 +177,17 @@ void VKSwapchain::CreateSwapchain(const VkSurfaceCapabilitiesKHR& capabilities, | ||||
|         swapchain_ci.queueFamilyIndexCount = static_cast<u32>(queue_indices.size()); | ||||
|         swapchain_ci.pQueueFamilyIndices = queue_indices.data(); | ||||
|     } | ||||
|     static constexpr std::array view_formats{VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_B8G8R8A8_SRGB}; | ||||
|     VkImageFormatListCreateInfo format_list{ | ||||
|         .sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO_KHR, | ||||
|         .pNext = nullptr, | ||||
|         .viewFormatCount = static_cast<u32>(view_formats.size()), | ||||
|         .pViewFormats = view_formats.data(), | ||||
|     }; | ||||
|     if (device.IsKhrSwapchainMutableFormatEnabled()) { | ||||
|         format_list.pNext = std::exchange(swapchain_ci.pNext, &format_list); | ||||
|         swapchain_ci.flags |= VK_SWAPCHAIN_CREATE_MUTABLE_FORMAT_BIT_KHR; | ||||
|     } | ||||
|     // Request the size again to reduce the possibility of a TOCTOU race condition. | ||||
|     const auto updated_capabilities = physical_device.GetSurfaceCapabilitiesKHR(surface); | ||||
|     swapchain_ci.imageExtent = ChooseSwapExtent(updated_capabilities, width, height); | ||||
| @@ -189,7 +199,7 @@ void VKSwapchain::CreateSwapchain(const VkSurfaceCapabilitiesKHR& capabilities, | ||||
|  | ||||
|     images = swapchain.GetImages(); | ||||
|     image_count = static_cast<u32>(images.size()); | ||||
|     image_format = surface_format.format; | ||||
|     image_view_format = srgb ? VK_FORMAT_B8G8R8A8_SRGB : VK_FORMAT_B8G8R8A8_UNORM; | ||||
| } | ||||
|  | ||||
| void VKSwapchain::CreateSemaphores() { | ||||
| @@ -205,7 +215,7 @@ void VKSwapchain::CreateImageViews() { | ||||
|         .flags = 0, | ||||
|         .image = {}, | ||||
|         .viewType = VK_IMAGE_VIEW_TYPE_2D, | ||||
|         .format = image_format, | ||||
|         .format = image_view_format, | ||||
|         .components = | ||||
|             { | ||||
|                 .r = VK_COMPONENT_SWIZZLE_IDENTITY, | ||||
|   | ||||
| @@ -68,8 +68,8 @@ public: | ||||
|         return *image_views[index]; | ||||
|     } | ||||
|  | ||||
|     VkFormat GetImageFormat() const { | ||||
|         return image_format; | ||||
|     VkFormat GetImageViewFormat() const { | ||||
|         return image_view_format; | ||||
|     } | ||||
|  | ||||
|     VkSemaphore CurrentPresentSemaphore() const { | ||||
| @@ -100,7 +100,7 @@ private: | ||||
|     u32 image_index{}; | ||||
|     u32 frame_index{}; | ||||
|  | ||||
|     VkFormat image_format{}; | ||||
|     VkFormat image_view_format{}; | ||||
|     VkExtent2D extent{}; | ||||
|  | ||||
|     bool current_srgb{}; | ||||
|   | ||||
| @@ -839,6 +839,8 @@ std::vector<const char*> Device::LoadExtensions(bool requires_surface) { | ||||
|     bool has_khr_shader_float16_int8{}; | ||||
|     bool has_khr_workgroup_memory_explicit_layout{}; | ||||
|     bool has_khr_pipeline_executable_properties{}; | ||||
|     bool has_khr_image_format_list{}; | ||||
|     bool has_khr_swapchain_mutable_format{}; | ||||
|     bool has_ext_subgroup_size_control{}; | ||||
|     bool has_ext_transform_feedback{}; | ||||
|     bool has_ext_custom_border_color{}; | ||||
| @@ -888,6 +890,9 @@ std::vector<const char*> Device::LoadExtensions(bool requires_surface) { | ||||
|         test(has_ext_shader_atomic_int64, VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME, false); | ||||
|         test(has_khr_workgroup_memory_explicit_layout, | ||||
|              VK_KHR_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_EXTENSION_NAME, false); | ||||
|         test(has_khr_image_format_list, VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME, false); | ||||
|         test(has_khr_swapchain_mutable_format, VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME, | ||||
|              false); | ||||
|         test(has_ext_line_rasterization, VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME, false); | ||||
|         if (Settings::values.enable_nsight_aftermath) { | ||||
|             test(nv_device_diagnostics_config, VK_NV_DEVICE_DIAGNOSTICS_CONFIG_EXTENSION_NAME, | ||||
| @@ -1066,6 +1071,11 @@ std::vector<const char*> Device::LoadExtensions(bool requires_surface) { | ||||
|             khr_pipeline_executable_properties = true; | ||||
|         } | ||||
|     } | ||||
|     if (has_khr_image_format_list && has_khr_swapchain_mutable_format) { | ||||
|         extensions.push_back(VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME); | ||||
|         extensions.push_back(VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME); | ||||
|         khr_swapchain_mutable_format = true; | ||||
|     } | ||||
|     if (khr_push_descriptor) { | ||||
|         VkPhysicalDevicePushDescriptorPropertiesKHR push_descriptor; | ||||
|         push_descriptor.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR; | ||||
|   | ||||
| @@ -224,6 +224,11 @@ public: | ||||
|         return khr_pipeline_executable_properties; | ||||
|     } | ||||
|  | ||||
|     /// Returns true if VK_KHR_swapchain_mutable_format is enabled. | ||||
|     bool IsKhrSwapchainMutableFormatEnabled() const { | ||||
|         return khr_swapchain_mutable_format; | ||||
|     } | ||||
|  | ||||
|     /// Returns true if the device supports VK_KHR_workgroup_memory_explicit_layout. | ||||
|     bool IsKhrWorkgroupMemoryExplicitLayoutSupported() const { | ||||
|         return khr_workgroup_memory_explicit_layout; | ||||
| @@ -390,6 +395,7 @@ private: | ||||
|     bool khr_workgroup_memory_explicit_layout{}; ///< Support for explicit workgroup layouts. | ||||
|     bool khr_push_descriptor{};                  ///< Support for VK_KHR_push_descritor. | ||||
|     bool khr_pipeline_executable_properties{};   ///< Support for executable properties. | ||||
|     bool khr_swapchain_mutable_format{};         ///< Support for VK_KHR_swapchain_mutable_format. | ||||
|     bool ext_index_type_uint8{};                 ///< Support for VK_EXT_index_type_uint8. | ||||
|     bool ext_sampler_filter_minmax{};            ///< Support for VK_EXT_sampler_filter_minmax. | ||||
|     bool ext_depth_range_unrestricted{};         ///< Support for VK_EXT_depth_range_unrestricted. | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Fernando S
					Fernando S