| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoBackends/Vulkan/VKSwapChain.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <cstdint> |
| 8 | |
| 9 | #include "Common/Assert.h" |
| 10 | #include "Common/Contains.h" |
| 11 | #include "Common/Logging/Log.h" |
| 12 | #include "Common/MsgHandler.h" |
| 13 | |
| 14 | #include "VideoBackends/Vulkan/CommandBufferManager.h" |
| 15 | #include "VideoBackends/Vulkan/ObjectCache.h" |
| 16 | #include "VideoBackends/Vulkan/VKTexture.h" |
| 17 | #include "VideoBackends/Vulkan/VulkanContext.h" |
| 18 | #include "VideoCommon/Present.h" |
| 19 | |
| 20 | #if defined(VK_USE_PLATFORM_XLIB_KHR) |
| 21 | #include <X11/Xlib.h> |
| 22 | #endif |
| 23 | |
| 24 | namespace Vulkan |
| 25 | { |
| 26 | SwapChain::SwapChain(const WindowSystemInfo& wsi, VkSurfaceKHR surface, bool vsync) |
| 27 | : m_wsi(wsi), m_surface(surface), m_vsync_enabled(vsync), |
| 28 | m_fullscreen_supported(g_vulkan_context->SupportsExclusiveFullscreen(wsi, surface)) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | SwapChain::~SwapChain() |
| 33 | { |
| 34 | DestroySwapChainImages(); |
| 35 | DestroySwapChain(); |
| 36 | DestroySurface(); |
| 37 | } |
| 38 | |
| 39 | VkSurfaceKHR SwapChain::CreateVulkanSurface(VkInstance instance, const WindowSystemInfo& wsi) |
| 40 | { |
| 41 | #if defined(VK_USE_PLATFORM_WIN32_KHR) |
| 42 | if (wsi.type == WindowSystemType::Windows) |
| 43 | { |
| 44 | VkWin32SurfaceCreateInfoKHR surface_create_info = { |
| 45 | VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR, // VkStructureType sType |
| 46 | nullptr, // const void* pNext |
| 47 | 0, // VkWin32SurfaceCreateFlagsKHR flags |
| 48 | nullptr, // HINSTANCE hinstance |
| 49 | static_cast<HWND>(wsi.render_surface) // HWND hwnd |
| 50 | }; |
| 51 | |
| 52 | VkSurfaceKHR surface; |
| 53 | VkResult res = vkCreateWin32SurfaceKHR(instance, &surface_create_info, nullptr, &surface); |
| 54 | if (res != VK_SUCCESS) |
| 55 | { |
| 56 | LOG_VULKAN_ERROR(res, "vkCreateWin32SurfaceKHR failed: "); |
| 57 | return VK_NULL_HANDLE; |
| 58 | } |
| 59 | |
| 60 | return surface; |
| 61 | } |
| 62 | #endif |
| 63 | |
| 64 | #if defined(VK_USE_PLATFORM_XLIB_KHR) |
| 65 | if (wsi.type == WindowSystemType::X11) |
| 66 | { |
| 67 | VkXlibSurfaceCreateInfoKHR surface_create_info = { |
| 68 | VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR, // VkStructureType sType |
| 69 | nullptr, // const void* pNext |
| 70 | 0, // VkXlibSurfaceCreateFlagsKHR flags |
| 71 | static_cast<Display*>(wsi.display_connection), // Display* dpy |
| 72 | reinterpret_cast<Window>(wsi.render_surface) // Window window |
| 73 | }; |
| 74 | |
| 75 | VkSurfaceKHR surface; |
| 76 | VkResult res = vkCreateXlibSurfaceKHR(instance, &surface_create_info, nullptr, &surface); |
| 77 | if (res != VK_SUCCESS) |
| 78 | { |
| 79 | LOG_VULKAN_ERROR(res, "vkCreateXlibSurfaceKHR failed: "); |
| 80 | return VK_NULL_HANDLE; |
| 81 | } |
| 82 | |
| 83 | return surface; |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | #if defined(VK_USE_PLATFORM_ANDROID_KHR) |
| 88 | if (wsi.type == WindowSystemType::Android) |
| 89 | { |
| 90 | VkAndroidSurfaceCreateInfoKHR surface_create_info = { |
| 91 | VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR, // VkStructureType sType |
| 92 | nullptr, // const void* pNext |
| 93 | 0, // VkAndroidSurfaceCreateFlagsKHR flags |
| 94 | reinterpret_cast<ANativeWindow*>(wsi.render_surface) // ANativeWindow* window |
| 95 | }; |
| 96 | |
| 97 | VkSurfaceKHR surface; |
| 98 | VkResult res = vkCreateAndroidSurfaceKHR(instance, &surface_create_info, nullptr, &surface); |
| 99 | if (res != VK_SUCCESS) |
| 100 | { |
| 101 | LOG_VULKAN_ERROR(res, "vkCreateAndroidSurfaceKHR failed: "); |
| 102 | return VK_NULL_HANDLE; |
| 103 | } |
| 104 | |
| 105 | return surface; |
| 106 | } |
| 107 | #endif |
| 108 | |
| 109 | #if defined(VK_USE_PLATFORM_METAL_EXT) |
| 110 | if (wsi.type == WindowSystemType::MacOS) |
| 111 | { |
| 112 | VkMetalSurfaceCreateInfoEXT surface_create_info = { |
| 113 | VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT, nullptr, 0, |
| 114 | static_cast<const CAMetalLayer*>(wsi.render_surface)}; |
| 115 | |
| 116 | VkSurfaceKHR surface; |
| 117 | VkResult res = vkCreateMetalSurfaceEXT(instance, &surface_create_info, nullptr, &surface); |
| 118 | if (res != VK_SUCCESS) |
| 119 | { |
| 120 | LOG_VULKAN_ERROR(res, "vkCreateMetalSurfaceEXT failed: "); |
| 121 | return VK_NULL_HANDLE; |
| 122 | } |
| 123 | |
| 124 | return surface; |
| 125 | } |
| 126 | #endif |
| 127 | |
| 128 | return VK_NULL_HANDLE; |
| 129 | } |
| 130 | |
| 131 | std::unique_ptr<SwapChain> SwapChain::Create(const WindowSystemInfo& wsi, VkSurfaceKHR surface, |
| 132 | bool vsync) |
| 133 | { |
| 134 | std::unique_ptr<SwapChain> swap_chain = std::make_unique<SwapChain>(wsi, surface, vsync); |
| 135 | if (!swap_chain->CreateSwapChain() || !swap_chain->SetupSwapChainImages()) |
| 136 | return nullptr; |
| 137 | |
| 138 | return swap_chain; |
| 139 | } |
| 140 | |
| 141 | bool SwapChain::SelectSurfaceFormat() |
| 142 | { |
| 143 | u32 format_count; |
| 144 | VkResult res = vkGetPhysicalDeviceSurfaceFormatsKHR(g_vulkan_context->GetPhysicalDevice(), |
| 145 | m_surface, &format_count, nullptr); |
| 146 | if (res != VK_SUCCESS || format_count == 0) |
| 147 | { |
| 148 | LOG_VULKAN_ERROR(res, "vkGetPhysicalDeviceSurfaceFormatsKHR failed: "); |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | std::vector<VkSurfaceFormatKHR> surface_formats(format_count); |
| 153 | res = vkGetPhysicalDeviceSurfaceFormatsKHR(g_vulkan_context->GetPhysicalDevice(), m_surface, |
| 154 | &format_count, surface_formats.data()); |
| 155 | ASSERT(res == VK_SUCCESS); |
| 156 | |
| 157 | // If there is a single undefined surface format, the device doesn't care, so we'll just use RGBA8 |
| 158 | if (surface_formats[0].format == VK_FORMAT_UNDEFINED) |
| 159 | { |
| 160 | m_surface_format.format = VK_FORMAT_R8G8B8A8_UNORM; |
| 161 | m_surface_format.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | const VkSurfaceFormatKHR* surface_format_RGBA8 = nullptr; |
| 166 | const VkSurfaceFormatKHR* surface_format_BGRA8 = nullptr; |
| 167 | const VkSurfaceFormatKHR* surface_format_RGB10_A2 = nullptr; |
| 168 | const VkSurfaceFormatKHR* surface_format_RGBA16F_scRGB = nullptr; |
| 169 | |
| 170 | // Try to find all suitable formats. |
| 171 | for (const VkSurfaceFormatKHR& surface_format : surface_formats) |
| 172 | { |
| 173 | // Some drivers seem to return a RGBA8 SRGB format here (Intel Mesa). |
| 174 | // Some other drivers return both a RGBA8 SRGB and UNORM formats (Nvidia). |
| 175 | // This results in gamma correction when presenting to the screen, which we don't want, |
| 176 | // because we already apply gamma ourselves, and we might not use sRGB gamma. |
| 177 | // Force using a linear format instead, if this is the case. |
| 178 | VkFormat format = VKTexture::GetLinearFormat(surface_format.format); |
| 179 | if (surface_format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) |
| 180 | { |
| 181 | if (format == VK_FORMAT_R8G8B8A8_UNORM) |
| 182 | surface_format_RGBA8 = &surface_format; |
| 183 | else if (format == VK_FORMAT_B8G8R8A8_UNORM) |
| 184 | surface_format_BGRA8 = &surface_format; |
| 185 | else if (format == VK_FORMAT_A2B10G10R10_UNORM_PACK32) |
| 186 | surface_format_RGB10_A2 = &surface_format; |
| 187 | } |
| 188 | else if (format == VK_FORMAT_R16G16B16A16_SFLOAT && |
| 189 | surface_format.colorSpace == VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT) |
| 190 | { |
| 191 | surface_format_RGBA16F_scRGB = &surface_format; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | const VkSurfaceFormatKHR* surface_format = nullptr; |
| 196 | |
| 197 | // Pick the best format. |
| 198 | // "g_ActiveConfig" might not have been been updated yet. |
| 199 | if (g_Config.bHDR && surface_format_RGBA16F_scRGB) |
| 200 | surface_format = surface_format_RGBA16F_scRGB; |
| 201 | else if (surface_format_RGB10_A2) |
| 202 | surface_format = surface_format_RGB10_A2; |
| 203 | else if (surface_format_RGBA8) |
| 204 | surface_format = surface_format_RGBA8; |
| 205 | else if (surface_format_BGRA8) |
| 206 | surface_format = surface_format_BGRA8; |
| 207 | |
| 208 | if (surface_format) |
| 209 | { |
| 210 | const VkFormat format = VKTexture::GetLinearFormat(surface_format->format); |
| 211 | if (format == VK_FORMAT_R8G8B8A8_UNORM) |
| 212 | m_texture_format = AbstractTextureFormat::RGBA8; |
| 213 | else if (format == VK_FORMAT_B8G8R8A8_UNORM) |
| 214 | m_texture_format = AbstractTextureFormat::BGRA8; |
| 215 | else if (format == VK_FORMAT_A2B10G10R10_UNORM_PACK32) |
| 216 | m_texture_format = AbstractTextureFormat::RGB10_A2; |
| 217 | else if (format == VK_FORMAT_R16G16B16A16_SFLOAT) |
| 218 | m_texture_format = AbstractTextureFormat::RGBA16F; |
| 219 | |
| 220 | m_surface_format.format = format; |
| 221 | m_surface_format.colorSpace = surface_format->colorSpace; |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | PanicAlertFmt("Failed to find a suitable format for swap chain buffers."); |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | bool SwapChain::SelectPresentMode() |
| 230 | { |
| 231 | VkResult res; |
| 232 | u32 mode_count; |
| 233 | res = vkGetPhysicalDeviceSurfacePresentModesKHR(g_vulkan_context->GetPhysicalDevice(), m_surface, |
| 234 | &mode_count, nullptr); |
| 235 | if (res != VK_SUCCESS || mode_count == 0) |
| 236 | { |
| 237 | LOG_VULKAN_ERROR(res, "vkGetPhysicalDeviceSurfaceFormatsKHR failed: "); |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | std::vector<VkPresentModeKHR> present_modes(mode_count); |
| 242 | res = vkGetPhysicalDeviceSurfacePresentModesKHR(g_vulkan_context->GetPhysicalDevice(), m_surface, |
| 243 | &mode_count, present_modes.data()); |
| 244 | ASSERT(res == VK_SUCCESS); |
| 245 | |
| 246 | // If vsync is enabled, use VK_PRESENT_MODE_FIFO_KHR. |
| 247 | // This check should not fail with conforming drivers, as the FIFO present mode is mandated by |
| 248 | // the specification (VK_KHR_swapchain). In case it isn't though, fall through to any other mode. |
| 249 | if (m_vsync_enabled && Common::Contains(present_modes, VK_PRESENT_MODE_FIFO_KHR)) |
| 250 | { |
| 251 | m_present_mode = VK_PRESENT_MODE_FIFO_KHR; |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | // Prefer screen-tearing, if possible, for lowest latency. |
| 256 | if (Common::Contains(present_modes, VK_PRESENT_MODE_IMMEDIATE_KHR)) |
| 257 | { |
| 258 | m_present_mode = VK_PRESENT_MODE_IMMEDIATE_KHR; |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | // Use optimized-vsync above vsync. |
| 263 | if (Common::Contains(present_modes, VK_PRESENT_MODE_MAILBOX_KHR)) |
| 264 | { |
| 265 | m_present_mode = VK_PRESENT_MODE_MAILBOX_KHR; |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | // Fall back to whatever is available. |
| 270 | m_present_mode = present_modes[0]; |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | bool SwapChain::CreateSwapChain() |
| 275 | { |
| 276 | // Look up surface properties to determine image count and dimensions |
| 277 | VkSurfaceCapabilitiesKHR surface_capabilities; |
| 278 | VkResult res = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(g_vulkan_context->GetPhysicalDevice(), |
| 279 | m_surface, &surface_capabilities); |
| 280 | if (res != VK_SUCCESS) |
| 281 | { |
| 282 | LOG_VULKAN_ERROR(res, "vkGetPhysicalDeviceSurfaceCapabilitiesKHR failed: "); |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | // Select swap chain format and present mode |
| 287 | if (!SelectSurfaceFormat() || !SelectPresentMode()) |
| 288 | return false; |
| 289 | |
| 290 | // Select number of images in swap chain, we prefer one buffer in the background to work on |
| 291 | uint32_t image_count = surface_capabilities.minImageCount + 1; |
| 292 | |
| 293 | // maxImageCount can be zero, in which case there isn't an upper limit on the number of buffers. |
| 294 | if (surface_capabilities.maxImageCount > 0) |
| 295 | image_count = std::min(image_count, surface_capabilities.maxImageCount); |
| 296 | |
| 297 | // Determine the dimensions of the swap chain. Values of -1 indicate the size we specify here |
| 298 | // determines window size? |
| 299 | VkExtent2D size = surface_capabilities.currentExtent; |
| 300 | if (size.width == UINT32_MAX) |
| 301 | { |
| 302 | size.width = std::max(g_presenter->GetBackbufferWidth(), 1); |
| 303 | size.height = std::max(g_presenter->GetBackbufferHeight(), 1); |
| 304 | } |
| 305 | size.width = std::clamp(size.width, surface_capabilities.minImageExtent.width, |
| 306 | surface_capabilities.maxImageExtent.width); |
| 307 | size.height = std::clamp(size.height, surface_capabilities.minImageExtent.height, |
| 308 | surface_capabilities.maxImageExtent.height); |
| 309 | |
| 310 | // Prefer identity transform if possible |
| 311 | VkSurfaceTransformFlagBitsKHR transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
| 312 | if (!(surface_capabilities.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR)) |
| 313 | transform = surface_capabilities.currentTransform; |
| 314 | |
| 315 | // Select swap chain flags, we only need a colour attachment |
| 316 | VkImageUsageFlags image_usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 317 | if (!(surface_capabilities.supportedUsageFlags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)) |
| 318 | { |
| 319 | ERROR_LOG_FMT(VIDEO, "Vulkan: Swap chain does not support usage as color attachment"); |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | // Select the number of image layers for Quad-Buffered stereoscopy |
| 324 | uint32_t image_layers = g_ActiveConfig.stereo_mode == StereoMode::QuadBuffer ? 2 : 1; |
| 325 | |
| 326 | // Store the old/current swap chain when recreating for resize |
| 327 | VkSwapchainKHR old_swap_chain = m_swap_chain; |
| 328 | m_swap_chain = VK_NULL_HANDLE; |
| 329 | |
| 330 | // Now we can actually create the swap chain |
| 331 | VkSwapchainCreateInfoKHR swap_chain_info = {VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, |
| 332 | nullptr, |
| 333 | 0, |
| 334 | m_surface, |
| 335 | image_count, |
| 336 | m_surface_format.format, |
| 337 | m_surface_format.colorSpace, |
| 338 | size, |
| 339 | image_layers, |
| 340 | image_usage, |
| 341 | VK_SHARING_MODE_EXCLUSIVE, |
| 342 | 0, |
| 343 | nullptr, |
| 344 | transform, |
| 345 | VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, |
| 346 | m_present_mode, |
| 347 | VK_TRUE, |
| 348 | old_swap_chain}; |
| 349 | std::array<uint32_t, 2> indices = {{ |
| 350 | g_vulkan_context->GetGraphicsQueueFamilyIndex(), |
| 351 | g_vulkan_context->GetPresentQueueFamilyIndex(), |
| 352 | }}; |
| 353 | if (g_vulkan_context->GetGraphicsQueueFamilyIndex() != |
| 354 | g_vulkan_context->GetPresentQueueFamilyIndex()) |
| 355 | { |
| 356 | swap_chain_info.imageSharingMode = VK_SHARING_MODE_CONCURRENT; |
| 357 | swap_chain_info.queueFamilyIndexCount = 2; |
| 358 | swap_chain_info.pQueueFamilyIndices = indices.data(); |
| 359 | } |
| 360 | |
| 361 | #ifdef SUPPORTS_VULKAN_EXCLUSIVE_FULLSCREEN |
| 362 | if (m_fullscreen_supported) |
| 363 | { |
| 364 | VkSurfaceFullScreenExclusiveInfoEXT fullscreen_support = {}; |
| 365 | swap_chain_info.pNext = &fullscreen_support; |
| 366 | fullscreen_support.sType = VK_STRUCTURE_TYPE_SURFACE_FULL_SCREEN_EXCLUSIVE_INFO_EXT; |
| 367 | fullscreen_support.fullScreenExclusive = VK_FULL_SCREEN_EXCLUSIVE_APPLICATION_CONTROLLED_EXT; |
| 368 | |
| 369 | auto platform_info = g_vulkan_context->GetPlatformExclusiveFullscreenInfo(m_wsi); |
| 370 | fullscreen_support.pNext = &platform_info; |
| 371 | |
| 372 | res = vkCreateSwapchainKHR(g_vulkan_context->GetDevice(), &swap_chain_info, nullptr, |
| 373 | &m_swap_chain); |
| 374 | if (res != VK_SUCCESS) |
| 375 | { |
| 376 | // Try without exclusive fullscreen. |
| 377 | WARN_LOG_FMT(VIDEO, "Failed to create exclusive fullscreen swapchain, trying without."); |
| 378 | swap_chain_info.pNext = nullptr; |
| 379 | g_backend_info.bSupportsExclusiveFullscreen = false; |
| 380 | m_fullscreen_supported = false; |
| 381 | } |
| 382 | } |
| 383 | #endif |
| 384 | |
| 385 | if (m_swap_chain == VK_NULL_HANDLE) |
| 386 | { |
| 387 | res = vkCreateSwapchainKHR(g_vulkan_context->GetDevice(), &swap_chain_info, nullptr, |
| 388 | &m_swap_chain); |
| 389 | } |
| 390 | if (res != VK_SUCCESS) |
| 391 | { |
| 392 | LOG_VULKAN_ERROR(res, "vkCreateSwapchainKHR failed: "); |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | // Now destroy the old swap chain, since it's been recreated. |
| 397 | // We can do this immediately since all work should have been completed before calling resize. |
| 398 | if (old_swap_chain != VK_NULL_HANDLE) |
| 399 | vkDestroySwapchainKHR(g_vulkan_context->GetDevice(), old_swap_chain, nullptr); |
| 400 | |
| 401 | m_width = size.width; |
| 402 | m_height = size.height; |
| 403 | m_layers = image_layers; |
| 404 | return true; |
| 405 | } |
| 406 | |
| 407 | bool SwapChain::SetupSwapChainImages() |
| 408 | { |
| 409 | ASSERT(m_swap_chain_images.empty()); |
| 410 | |
| 411 | uint32_t image_count; |
| 412 | VkResult res = |
| 413 | vkGetSwapchainImagesKHR(g_vulkan_context->GetDevice(), m_swap_chain, &image_count, nullptr); |
| 414 | if (res != VK_SUCCESS) |
| 415 | { |
| 416 | LOG_VULKAN_ERROR(res, "vkGetSwapchainImagesKHR failed: "); |
| 417 | return false; |
| 418 | } |
| 419 | |
| 420 | std::vector<VkImage> images(image_count); |
| 421 | res = vkGetSwapchainImagesKHR(g_vulkan_context->GetDevice(), m_swap_chain, &image_count, |
| 422 | images.data()); |
| 423 | ASSERT(res == VK_SUCCESS); |
| 424 | |
| 425 | const TextureConfig texture_config( |
| 426 | TextureConfig(m_width, m_height, 1, m_layers, 1, m_texture_format, |
| 427 | AbstractTextureFlag_RenderTarget, AbstractTextureType::Texture_2DArray)); |
| 428 | const VkRenderPass load_render_pass = g_object_cache->GetRenderPass( |
| 429 | m_surface_format.format, VK_FORMAT_UNDEFINED, 1, VK_ATTACHMENT_LOAD_OP_LOAD); |
| 430 | const VkRenderPass clear_render_pass = g_object_cache->GetRenderPass( |
| 431 | m_surface_format.format, VK_FORMAT_UNDEFINED, 1, VK_ATTACHMENT_LOAD_OP_CLEAR); |
| 432 | if (load_render_pass == VK_NULL_HANDLE || clear_render_pass == VK_NULL_HANDLE) |
| 433 | { |
| 434 | PanicAlertFmt("Failed to get swap chain render passes."); |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | m_swap_chain_images.reserve(image_count); |
| 439 | for (uint32_t i = 0; i < image_count; i++) |
| 440 | { |
| 441 | SwapChainImage image; |
| 442 | image.image = images[i]; |
| 443 | |
| 444 | // Create texture object, which creates a view of the backbuffer |
| 445 | image.texture = |
| 446 | VKTexture::CreateAdopted(texture_config, image.image, |
| 447 | m_layers > 1 ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D, |
| 448 | VK_IMAGE_LAYOUT_UNDEFINED); |
| 449 | if (!image.texture) |
| 450 | return false; |
| 451 | |
| 452 | image.framebuffer = VKFramebuffer::Create(image.texture.get(), nullptr, {}); |
| 453 | if (!image.framebuffer) |
| 454 | { |
| 455 | image.texture.reset(); |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | m_swap_chain_images.emplace_back(std::move(image)); |
| 460 | } |
| 461 | |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | void SwapChain::DestroySwapChainImages() |
| 466 | { |
| 467 | for (auto& it : m_swap_chain_images) |
| 468 | { |
| 469 | // Images themselves are cleaned up by the swap chain object |
| 470 | it.framebuffer.reset(); |
| 471 | it.texture.reset(); |
| 472 | } |
| 473 | m_swap_chain_images.clear(); |
| 474 | } |
| 475 | |
| 476 | void SwapChain::DestroySwapChain() |
| 477 | { |
| 478 | if (m_swap_chain == VK_NULL_HANDLE) |
| 479 | return; |
| 480 | |
| 481 | // Release exclusive fullscreen before destroying. |
| 482 | if (m_current_fullscreen_state) |
| 483 | SetFullscreenState(false); |
| 484 | |
| 485 | vkDestroySwapchainKHR(g_vulkan_context->GetDevice(), m_swap_chain, nullptr); |
| 486 | m_swap_chain = VK_NULL_HANDLE; |
| 487 | } |
| 488 | |
| 489 | VkResult SwapChain::AcquireNextImage() |
| 490 | { |
| 491 | VkResult res = vkAcquireNextImageKHR(g_vulkan_context->GetDevice(), m_swap_chain, UINT64_MAX, |
| 492 | g_command_buffer_mgr->GetCurrentCommandBufferSemaphore(), |
| 493 | VK_NULL_HANDLE, &m_current_swap_chain_image_index); |
| 494 | m_current_swap_chain_image_is_valid = res >= 0; |
| 495 | if (IsCurrentImageValid()) |
| 496 | g_command_buffer_mgr->MarkCurrentCommandBufferSemaphoreUsed(); |
| 497 | if (res != VK_SUCCESS && res != VK_ERROR_OUT_OF_DATE_KHR && res != VK_SUBOPTIMAL_KHR) |
| 498 | LOG_VULKAN_ERROR(res, "vkAcquireNextImageKHR failed: "); |
| 499 | |
| 500 | return res; |
| 501 | } |
| 502 | |
| 503 | bool SwapChain::ResizeSwapChain() |
| 504 | { |
| 505 | DestroySwapChainImages(); |
| 506 | if (!CreateSwapChain() || !SetupSwapChainImages()) |
| 507 | { |
| 508 | PanicAlertFmt("Failed to re-configure swap chain images, this is fatal (for now)"); |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | return true; |
| 513 | } |
| 514 | |
| 515 | bool SwapChain::RecreateSwapChain() |
| 516 | { |
| 517 | DestroySwapChainImages(); |
| 518 | DestroySwapChain(); |
| 519 | if (!CreateSwapChain() || !SetupSwapChainImages()) |
| 520 | { |
| 521 | PanicAlertFmt("Failed to re-configure swap chain images, this is fatal (for now)"); |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | return true; |
| 526 | } |
| 527 | |
| 528 | bool SwapChain::SetVSync(bool enabled) |
| 529 | { |
| 530 | if (m_vsync_enabled == enabled) |
| 531 | return true; |
| 532 | |
| 533 | // Recreate the swap chain with the new present mode. |
| 534 | m_vsync_enabled = enabled; |
| 535 | return RecreateSwapChain(); |
| 536 | } |
| 537 | |
| 538 | bool SwapChain::SetFullscreenState(bool state) |
| 539 | { |
| 540 | #ifdef SUPPORTS_VULKAN_EXCLUSIVE_FULLSCREEN |
| 541 | if (m_current_fullscreen_state == state) |
| 542 | return true; |
| 543 | |
| 544 | if (state) |
| 545 | { |
| 546 | VkResult res = vkAcquireFullScreenExclusiveModeEXT(g_vulkan_context->GetDevice(), m_swap_chain); |
| 547 | if (res != VK_SUCCESS) |
| 548 | { |
| 549 | LOG_VULKAN_ERROR(res, "vkAcquireFullScreenExclusiveModeEXT failed:"); |
| 550 | return false; |
| 551 | } |
| 552 | |
| 553 | INFO_LOG_FMT(VIDEO, "Exclusive fullscreen acquired."); |
| 554 | } |
| 555 | else |
| 556 | { |
| 557 | VkResult res = vkReleaseFullScreenExclusiveModeEXT(g_vulkan_context->GetDevice(), m_swap_chain); |
| 558 | if (res != VK_SUCCESS) |
| 559 | LOG_VULKAN_ERROR(res, "vkReleaseFullScreenExclusiveModeEXT failed:"); |
| 560 | |
| 561 | INFO_LOG_FMT(VIDEO, "Exclusive fullscreen released."); |
| 562 | } |
| 563 | |
| 564 | m_current_fullscreen_state = state; |
| 565 | return true; |
| 566 | #else |
| 567 | return false; |
| 568 | #endif |
| 569 | } |
| 570 | |
| 571 | bool SwapChain::RecreateSurface(void* native_handle) |
| 572 | { |
| 573 | // Destroy the old swap chain, images, and surface. |
| 574 | DestroySwapChainImages(); |
| 575 | DestroySwapChain(); |
| 576 | DestroySurface(); |
| 577 | |
| 578 | // Re-create the surface with the new native handle |
| 579 | m_wsi.render_surface = native_handle; |
| 580 | m_surface = CreateVulkanSurface(g_vulkan_context->GetVulkanInstance(), m_wsi); |
| 581 | if (m_surface == VK_NULL_HANDLE) |
| 582 | return false; |
| 583 | |
| 584 | // The validation layers get angry at us if we don't call this before creating the swapchain. |
| 585 | VkBool32 present_supported = VK_TRUE; |
| 586 | VkResult res = vkGetPhysicalDeviceSurfaceSupportKHR( |
| 587 | g_vulkan_context->GetPhysicalDevice(), g_vulkan_context->GetPresentQueueFamilyIndex(), |
| 588 | m_surface, &present_supported); |
| 589 | if (res != VK_SUCCESS) |
| 590 | { |
| 591 | LOG_VULKAN_ERROR(res, "vkGetPhysicalDeviceSurfaceSupportKHR failed: "); |
| 592 | return false; |
| 593 | } |
| 594 | if (!present_supported) |
| 595 | { |
| 596 | PanicAlertFmt("Recreated surface does not support presenting."); |
| 597 | return false; |
| 598 | } |
| 599 | |
| 600 | // Update exclusive fullscreen support (unlikely to change). |
| 601 | m_fullscreen_supported = g_vulkan_context->SupportsExclusiveFullscreen(m_wsi, m_surface); |
| 602 | g_backend_info.bSupportsExclusiveFullscreen = m_fullscreen_supported; |
| 603 | m_current_fullscreen_state = false; |
| 604 | m_next_fullscreen_state = false; |
| 605 | |
| 606 | // Finally re-create the swap chain |
| 607 | if (!CreateSwapChain() || !SetupSwapChainImages()) |
| 608 | return false; |
| 609 | |
| 610 | return true; |
| 611 | } |
| 612 | |
| 613 | void SwapChain::DestroySurface() |
| 614 | { |
| 615 | vkDestroySurfaceKHR(g_vulkan_context->GetVulkanInstance(), m_surface, nullptr); |
| 616 | m_surface = VK_NULL_HANDLE; |
| 617 | } |
| 618 | } // namespace Vulkan |