From 9d37332111762dfa0cd9a63b636a9c06b6c6dcf8 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Tue, 5 Sep 2023 20:00:18 +1000 Subject: [PATCH] VulkanDevice: Prefer 8-bit formats for swap chain --- src/util/vulkan_swap_chain.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/util/vulkan_swap_chain.cpp b/src/util/vulkan_swap_chain.cpp index 4ef90fa93..efcb96807 100644 --- a/src/util/vulkan_swap_chain.cpp +++ b/src/util/vulkan_swap_chain.cpp @@ -209,19 +209,26 @@ std::optional VulkanSwapChain::SelectSurfaceFormat(VkSurface Assert(res == VK_SUCCESS); // If there is a single undefined surface format, the device doesn't care, so we'll just use RGBA - if (surface_formats[0].format == VK_FORMAT_UNDEFINED) + const auto has_format = [&surface_formats](VkFormat fmt) { + return std::any_of(surface_formats.begin(), surface_formats.end(), [fmt](const VkSurfaceFormatKHR& sf) { + return (sf.format == fmt || GetLinearFormat(sf.format) == fmt); + }); + }; + if (has_format(VK_FORMAT_UNDEFINED)) return VkSurfaceFormatKHR{VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}; - // Try to find a suitable format. - for (const VkSurfaceFormatKHR& surface_format : surface_formats) + // Prefer 8-bit formats. + for (VkFormat format : {VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R5G6B5_UNORM_PACK16, + VK_FORMAT_R5G5B5A1_UNORM_PACK16}) { - // Some drivers seem to return a SRGB format here (Intel Mesa). - // This results in gamma correction when presenting to the screen, which we don't want. - // Use a linear format instead, if this is the case. - return VkSurfaceFormatKHR{GetLinearFormat(surface_format.format), VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}; + if (has_format(format)) + return VkSurfaceFormatKHR{format, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}; } - Log_ErrorPrintf("Failed to find a suitable format for swap chain buffers."); + Log_ErrorPrintf("Failed to find a suitable format for swap chain buffers. Available formats were:"); + for (const VkSurfaceFormatKHR& sf : surface_formats) + Log_ErrorPrintf(" %u", static_cast(sf.format)); + return std::nullopt; }