vk_swapchain.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 auto present_queue{device.GetPresentQueue()};
  94. const VkPresentInfoKHR present_info{
  95. .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
  96. .pNext = nullptr,
  97. .waitSemaphoreCount = render_semaphore ? 1U : 0U,
  98. .pWaitSemaphores = &render_semaphore,
  99. .swapchainCount = 1,
  100. .pSwapchains = swapchain.address(),
  101. .pImageIndices = &image_index,
  102. .pResults = nullptr,
  103. };
  104. switch (const VkResult result = present_queue.Present(present_info)) {
  105. case VK_SUCCESS:
  106. break;
  107. case VK_SUBOPTIMAL_KHR:
  108. LOG_DEBUG(Render_Vulkan, "Suboptimal swapchain");
  109. break;
  110. case VK_ERROR_OUT_OF_DATE_KHR:
  111. is_outdated = true;
  112. break;
  113. default:
  114. LOG_CRITICAL(Render_Vulkan, "Failed to present with error {}", vk::ToString(result));
  115. break;
  116. }
  117. ++frame_index;
  118. if (frame_index >= image_count) {
  119. frame_index = 0;
  120. }
  121. }
  122. void VKSwapchain::CreateSwapchain(const VkSurfaceCapabilitiesKHR& capabilities, u32 width,
  123. u32 height, bool srgb) {
  124. const auto physical_device{device.GetPhysical()};
  125. const auto formats{physical_device.GetSurfaceFormatsKHR(surface)};
  126. const auto present_modes{physical_device.GetSurfacePresentModesKHR(surface)};
  127. const VkSurfaceFormatKHR surface_format{ChooseSwapSurfaceFormat(formats, srgb)};
  128. const VkPresentModeKHR present_mode{ChooseSwapPresentMode(present_modes)};
  129. u32 requested_image_count{capabilities.minImageCount + 1};
  130. if (capabilities.maxImageCount > 0 && requested_image_count > capabilities.maxImageCount) {
  131. requested_image_count = capabilities.maxImageCount;
  132. }
  133. VkSwapchainCreateInfoKHR swapchain_ci{
  134. .sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
  135. .pNext = nullptr,
  136. .flags = 0,
  137. .surface = surface,
  138. .minImageCount = requested_image_count,
  139. .imageFormat = surface_format.format,
  140. .imageColorSpace = surface_format.colorSpace,
  141. .imageExtent = {},
  142. .imageArrayLayers = 1,
  143. .imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
  144. .imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
  145. .queueFamilyIndexCount = 0,
  146. .pQueueFamilyIndices = nullptr,
  147. .preTransform = capabilities.currentTransform,
  148. .compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
  149. .presentMode = present_mode,
  150. .clipped = VK_FALSE,
  151. .oldSwapchain = nullptr,
  152. };
  153. const u32 graphics_family{device.GetGraphicsFamily()};
  154. const u32 present_family{device.GetPresentFamily()};
  155. const std::array<u32, 2> queue_indices{graphics_family, present_family};
  156. if (graphics_family != present_family) {
  157. swapchain_ci.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
  158. swapchain_ci.queueFamilyIndexCount = static_cast<u32>(queue_indices.size());
  159. swapchain_ci.pQueueFamilyIndices = queue_indices.data();
  160. }
  161. // Request the size again to reduce the possibility of a TOCTOU race condition.
  162. const auto updated_capabilities = physical_device.GetSurfaceCapabilitiesKHR(surface);
  163. swapchain_ci.imageExtent = ChooseSwapExtent(updated_capabilities, width, height);
  164. // Don't add code within this and the swapchain creation.
  165. swapchain = device.GetLogical().CreateSwapchainKHR(swapchain_ci);
  166. extent = swapchain_ci.imageExtent;
  167. current_srgb = srgb;
  168. images = swapchain.GetImages();
  169. image_count = static_cast<u32>(images.size());
  170. image_format = surface_format.format;
  171. }
  172. void VKSwapchain::CreateSemaphores() {
  173. present_semaphores.resize(image_count);
  174. std::ranges::generate(present_semaphores,
  175. [this] { return device.GetLogical().CreateSemaphore(); });
  176. }
  177. void VKSwapchain::CreateImageViews() {
  178. VkImageViewCreateInfo ci{
  179. .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
  180. .pNext = nullptr,
  181. .flags = 0,
  182. .image = {},
  183. .viewType = VK_IMAGE_VIEW_TYPE_2D,
  184. .format = image_format,
  185. .components =
  186. {
  187. .r = VK_COMPONENT_SWIZZLE_IDENTITY,
  188. .g = VK_COMPONENT_SWIZZLE_IDENTITY,
  189. .b = VK_COMPONENT_SWIZZLE_IDENTITY,
  190. .a = VK_COMPONENT_SWIZZLE_IDENTITY,
  191. },
  192. .subresourceRange =
  193. {
  194. .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
  195. .baseMipLevel = 0,
  196. .levelCount = 1,
  197. .baseArrayLayer = 0,
  198. .layerCount = 1,
  199. },
  200. };
  201. image_views.resize(image_count);
  202. for (std::size_t i = 0; i < image_count; i++) {
  203. ci.image = images[i];
  204. image_views[i] = device.GetLogical().CreateImageView(ci);
  205. }
  206. }
  207. void VKSwapchain::Destroy() {
  208. frame_index = 0;
  209. present_semaphores.clear();
  210. framebuffers.clear();
  211. image_views.clear();
  212. swapchain.reset();
  213. }
  214. } // namespace Vulkan