vk_swapchain.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include <limits>
  7. #include <vector>
  8. #include "common/assert.h"
  9. #include "common/logging/log.h"
  10. #include "core/core.h"
  11. #include "core/frontend/framebuffer_layout.h"
  12. #include "video_core/renderer_vulkan/vk_scheduler.h"
  13. #include "video_core/renderer_vulkan/vk_swapchain.h"
  14. #include "video_core/vulkan_common/vulkan_device.h"
  15. #include "video_core/vulkan_common/vulkan_wrapper.h"
  16. namespace Vulkan {
  17. namespace {
  18. VkSurfaceFormatKHR ChooseSwapSurfaceFormat(vk::Span<VkSurfaceFormatKHR> formats, bool srgb) {
  19. if (formats.size() == 1 && formats[0].format == VK_FORMAT_UNDEFINED) {
  20. VkSurfaceFormatKHR format;
  21. format.format = VK_FORMAT_B8G8R8A8_UNORM;
  22. format.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
  23. return format;
  24. }
  25. const auto& found = std::find_if(formats.begin(), formats.end(), [srgb](const auto& format) {
  26. const auto request_format = srgb ? VK_FORMAT_B8G8R8A8_SRGB : VK_FORMAT_B8G8R8A8_UNORM;
  27. return format.format == request_format &&
  28. format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
  29. });
  30. return found != formats.end() ? *found : formats[0];
  31. }
  32. VkPresentModeKHR ChooseSwapPresentMode(vk::Span<VkPresentModeKHR> modes) {
  33. // Mailbox doesn't lock the application like fifo (vsync), prefer it
  34. const auto found = std::find(modes.begin(), modes.end(), VK_PRESENT_MODE_MAILBOX_KHR);
  35. return found != modes.end() ? *found : VK_PRESENT_MODE_FIFO_KHR;
  36. }
  37. VkExtent2D ChooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, u32 width, u32 height) {
  38. constexpr auto undefined_size{std::numeric_limits<u32>::max()};
  39. if (capabilities.currentExtent.width != undefined_size) {
  40. return capabilities.currentExtent;
  41. }
  42. VkExtent2D extent;
  43. extent.width = std::max(capabilities.minImageExtent.width,
  44. std::min(capabilities.maxImageExtent.width, width));
  45. extent.height = std::max(capabilities.minImageExtent.height,
  46. std::min(capabilities.maxImageExtent.height, height));
  47. return extent;
  48. }
  49. } // Anonymous namespace
  50. VKSwapchain::VKSwapchain(VkSurfaceKHR surface_, const Device& device_, VKScheduler& scheduler_,
  51. u32 width, u32 height, bool srgb)
  52. : surface{surface_}, device{device_}, scheduler{scheduler_} {
  53. Create(width, height, srgb);
  54. }
  55. VKSwapchain::~VKSwapchain() = default;
  56. void VKSwapchain::Create(u32 width, u32 height, bool srgb) {
  57. is_outdated = false;
  58. is_suboptimal = false;
  59. const auto physical_device = device.GetPhysical();
  60. const auto capabilities{physical_device.GetSurfaceCapabilitiesKHR(surface)};
  61. if (capabilities.maxImageExtent.width == 0 || capabilities.maxImageExtent.height == 0) {
  62. return;
  63. }
  64. device.GetLogical().WaitIdle();
  65. Destroy();
  66. CreateSwapchain(capabilities, width, height, srgb);
  67. CreateSemaphores();
  68. CreateImageViews();
  69. resource_ticks.clear();
  70. resource_ticks.resize(image_count);
  71. }
  72. void VKSwapchain::AcquireNextImage() {
  73. const VkResult result = device.GetLogical().AcquireNextImageKHR(
  74. *swapchain, std::numeric_limits<u64>::max(), *present_semaphores[frame_index],
  75. VK_NULL_HANDLE, &image_index);
  76. switch (result) {
  77. case VK_SUCCESS:
  78. break;
  79. case VK_SUBOPTIMAL_KHR:
  80. is_suboptimal = true;
  81. break;
  82. case VK_ERROR_OUT_OF_DATE_KHR:
  83. is_outdated = true;
  84. break;
  85. default:
  86. LOG_ERROR(Render_Vulkan, "vkAcquireNextImageKHR returned {}", vk::ToString(result));
  87. break;
  88. }
  89. scheduler.Wait(resource_ticks[image_index]);
  90. resource_ticks[image_index] = scheduler.CurrentTick();
  91. }
  92. void VKSwapchain::Present(VkSemaphore render_semaphore) {
  93. const VkSemaphore present_semaphore{*present_semaphores[frame_index]};
  94. const std::array<VkSemaphore, 2> semaphores{present_semaphore, render_semaphore};
  95. const auto present_queue{device.GetPresentQueue()};
  96. const VkPresentInfoKHR present_info{
  97. .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
  98. .pNext = nullptr,
  99. .waitSemaphoreCount = render_semaphore ? 2U : 1U,
  100. .pWaitSemaphores = semaphores.data(),
  101. .swapchainCount = 1,
  102. .pSwapchains = swapchain.address(),
  103. .pImageIndices = &image_index,
  104. .pResults = nullptr,
  105. };
  106. switch (const VkResult result = present_queue.Present(present_info)) {
  107. case VK_SUCCESS:
  108. break;
  109. case VK_SUBOPTIMAL_KHR:
  110. LOG_DEBUG(Render_Vulkan, "Suboptimal swapchain");
  111. break;
  112. case VK_ERROR_OUT_OF_DATE_KHR:
  113. is_outdated = true;
  114. break;
  115. default:
  116. LOG_CRITICAL(Render_Vulkan, "Failed to present with error {}", vk::ToString(result));
  117. break;
  118. }
  119. ++frame_index;
  120. if (frame_index >= image_count) {
  121. frame_index = 0;
  122. }
  123. }
  124. void VKSwapchain::CreateSwapchain(const VkSurfaceCapabilitiesKHR& capabilities, u32 width,
  125. u32 height, bool srgb) {
  126. const auto physical_device{device.GetPhysical()};
  127. const auto formats{physical_device.GetSurfaceFormatsKHR(surface)};
  128. const auto present_modes{physical_device.GetSurfacePresentModesKHR(surface)};
  129. const VkSurfaceFormatKHR surface_format{ChooseSwapSurfaceFormat(formats, srgb)};
  130. const VkPresentModeKHR present_mode{ChooseSwapPresentMode(present_modes)};
  131. u32 requested_image_count{capabilities.minImageCount + 1};
  132. if (capabilities.maxImageCount > 0 && requested_image_count > capabilities.maxImageCount) {
  133. requested_image_count = capabilities.maxImageCount;
  134. }
  135. VkSwapchainCreateInfoKHR swapchain_ci{
  136. .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
  137. .pNext = nullptr,
  138. .flags = 0,
  139. .surface = surface,
  140. .minImageCount = requested_image_count,
  141. .imageFormat = surface_format.format,
  142. .imageColorSpace = surface_format.colorSpace,
  143. .imageExtent = {},
  144. .imageArrayLayers = 1,
  145. .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
  146. .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
  147. .queueFamilyIndexCount = 0,
  148. .pQueueFamilyIndices = nullptr,
  149. .preTransform = capabilities.currentTransform,
  150. .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
  151. .presentMode = present_mode,
  152. .clipped = VK_FALSE,
  153. .oldSwapchain = nullptr,
  154. };
  155. const u32 graphics_family{device.GetGraphicsFamily()};
  156. const u32 present_family{device.GetPresentFamily()};
  157. const std::array<u32, 2> queue_indices{graphics_family, present_family};
  158. if (graphics_family != present_family) {
  159. swapchain_ci.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
  160. swapchain_ci.queueFamilyIndexCount = static_cast<u32>(queue_indices.size());
  161. swapchain_ci.pQueueFamilyIndices = queue_indices.data();
  162. }
  163. // Request the size again to reduce the possibility of a TOCTOU race condition.
  164. const auto updated_capabilities = physical_device.GetSurfaceCapabilitiesKHR(surface);
  165. swapchain_ci.imageExtent = ChooseSwapExtent(updated_capabilities, width, height);
  166. // Don't add code within this and the swapchain creation.
  167. swapchain = device.GetLogical().CreateSwapchainKHR(swapchain_ci);
  168. extent = swapchain_ci.imageExtent;
  169. current_srgb = srgb;
  170. images = swapchain.GetImages();
  171. image_count = static_cast<u32>(images.size());
  172. image_format = surface_format.format;
  173. }
  174. void VKSwapchain::CreateSemaphores() {
  175. present_semaphores.resize(image_count);
  176. std::ranges::generate(present_semaphores,
  177. [this] { return device.GetLogical().CreateSemaphore(); });
  178. }
  179. void VKSwapchain::CreateImageViews() {
  180. VkImageViewCreateInfo ci{
  181. .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
  182. .pNext = nullptr,
  183. .flags = 0,
  184. .image = {},
  185. .viewType = VK_IMAGE_VIEW_TYPE_2D,
  186. .format = image_format,
  187. .components =
  188. {
  189. .r = VK_COMPONENT_SWIZZLE_IDENTITY,
  190. .g = VK_COMPONENT_SWIZZLE_IDENTITY,
  191. .b = VK_COMPONENT_SWIZZLE_IDENTITY,
  192. .a = VK_COMPONENT_SWIZZLE_IDENTITY,
  193. },
  194. .subresourceRange =
  195. {
  196. .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
  197. .baseMipLevel = 0,
  198. .levelCount = 1,
  199. .baseArrayLayer = 0,
  200. .layerCount = 1,
  201. },
  202. };
  203. image_views.resize(image_count);
  204. for (std::size_t i = 0; i < image_count; i++) {
  205. ci.image = images[i];
  206. image_views[i] = device.GetLogical().CreateImageView(ci);
  207. }
  208. }
  209. void VKSwapchain::Destroy() {
  210. frame_index = 0;
  211. present_semaphores.clear();
  212. framebuffers.clear();
  213. image_views.clear();
  214. swapchain.reset();
  215. }
  216. } // namespace Vulkan