vk_swapchain.cpp 8.7 KB

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