vk_swapchain.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_device.h"
  13. #include "video_core/renderer_vulkan/vk_resource_manager.h"
  14. #include "video_core/renderer_vulkan/vk_swapchain.h"
  15. #include "video_core/renderer_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 VKDevice& device)
  51. : surface{surface}, device{device} {}
  52. VKSwapchain::~VKSwapchain() = default;
  53. void VKSwapchain::Create(u32 width, u32 height, bool srgb) {
  54. const auto physical_device = device.GetPhysical();
  55. const auto capabilities{physical_device.GetSurfaceCapabilitiesKHR(surface)};
  56. if (capabilities.maxImageExtent.width == 0 || capabilities.maxImageExtent.height == 0) {
  57. return;
  58. }
  59. device.GetLogical().WaitIdle();
  60. Destroy();
  61. CreateSwapchain(capabilities, width, height, srgb);
  62. CreateSemaphores();
  63. CreateImageViews();
  64. fences.resize(image_count, nullptr);
  65. }
  66. void VKSwapchain::AcquireNextImage() {
  67. device.GetLogical().AcquireNextImageKHR(*swapchain, std::numeric_limits<u64>::max(),
  68. *present_semaphores[frame_index], {}, &image_index);
  69. if (auto& fence = fences[image_index]; fence) {
  70. fence->Wait();
  71. fence->Release();
  72. fence = nullptr;
  73. }
  74. }
  75. bool VKSwapchain::Present(VkSemaphore render_semaphore, VKFence& fence) {
  76. const VkSemaphore present_semaphore{*present_semaphores[frame_index]};
  77. const std::array<VkSemaphore, 2> semaphores{present_semaphore, render_semaphore};
  78. const auto present_queue{device.GetPresentQueue()};
  79. bool recreated = false;
  80. VkPresentInfoKHR present_info;
  81. present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
  82. present_info.pNext = nullptr;
  83. present_info.waitSemaphoreCount = render_semaphore ? 2U : 1U;
  84. present_info.pWaitSemaphores = semaphores.data();
  85. present_info.swapchainCount = 1;
  86. present_info.pSwapchains = swapchain.address();
  87. present_info.pImageIndices = &image_index;
  88. present_info.pResults = nullptr;
  89. switch (const VkResult result = present_queue.Present(present_info)) {
  90. case VK_SUCCESS:
  91. break;
  92. case VK_SUBOPTIMAL_KHR:
  93. LOG_DEBUG(Render_Vulkan, "Suboptimal swapchain");
  94. break;
  95. case VK_ERROR_OUT_OF_DATE_KHR:
  96. if (current_width > 0 && current_height > 0) {
  97. Create(current_width, current_height, current_srgb);
  98. recreated = true;
  99. }
  100. break;
  101. default:
  102. LOG_CRITICAL(Render_Vulkan, "Failed to present with error {}", vk::ToString(result));
  103. break;
  104. }
  105. ASSERT(fences[image_index] == nullptr);
  106. fences[image_index] = &fence;
  107. frame_index = (frame_index + 1) % static_cast<u32>(image_count);
  108. return recreated;
  109. }
  110. bool VKSwapchain::HasFramebufferChanged(const Layout::FramebufferLayout& framebuffer) const {
  111. // TODO(Rodrigo): Handle framebuffer pixel format changes
  112. return framebuffer.width != current_width || framebuffer.height != current_height;
  113. }
  114. void VKSwapchain::CreateSwapchain(const VkSurfaceCapabilitiesKHR& capabilities, u32 width,
  115. u32 height, bool srgb) {
  116. const auto physical_device{device.GetPhysical()};
  117. const auto formats{physical_device.GetSurfaceFormatsKHR(surface)};
  118. const auto present_modes{physical_device.GetSurfacePresentModesKHR(surface)};
  119. const VkSurfaceFormatKHR surface_format{ChooseSwapSurfaceFormat(formats, srgb)};
  120. const VkPresentModeKHR present_mode{ChooseSwapPresentMode(present_modes)};
  121. u32 requested_image_count{capabilities.minImageCount + 1};
  122. if (capabilities.maxImageCount > 0 && requested_image_count > capabilities.maxImageCount) {
  123. requested_image_count = capabilities.maxImageCount;
  124. }
  125. VkSwapchainCreateInfoKHR swapchain_ci;
  126. swapchain_ci.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
  127. swapchain_ci.pNext = nullptr;
  128. swapchain_ci.flags = 0;
  129. swapchain_ci.surface = surface;
  130. swapchain_ci.minImageCount = requested_image_count;
  131. swapchain_ci.imageFormat = surface_format.format;
  132. swapchain_ci.imageColorSpace = surface_format.colorSpace;
  133. swapchain_ci.imageArrayLayers = 1;
  134. swapchain_ci.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
  135. swapchain_ci.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
  136. swapchain_ci.queueFamilyIndexCount = 0;
  137. swapchain_ci.pQueueFamilyIndices = nullptr;
  138. swapchain_ci.preTransform = capabilities.currentTransform;
  139. swapchain_ci.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
  140. swapchain_ci.presentMode = present_mode;
  141. swapchain_ci.clipped = VK_FALSE;
  142. swapchain_ci.oldSwapchain = nullptr;
  143. const u32 graphics_family{device.GetGraphicsFamily()};
  144. const u32 present_family{device.GetPresentFamily()};
  145. const std::array<u32, 2> queue_indices{graphics_family, present_family};
  146. if (graphics_family != present_family) {
  147. swapchain_ci.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
  148. swapchain_ci.queueFamilyIndexCount = static_cast<u32>(queue_indices.size());
  149. swapchain_ci.pQueueFamilyIndices = queue_indices.data();
  150. } else {
  151. swapchain_ci.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
  152. }
  153. // Request the size again to reduce the possibility of a TOCTOU race condition.
  154. const auto updated_capabilities = physical_device.GetSurfaceCapabilitiesKHR(surface);
  155. swapchain_ci.imageExtent = ChooseSwapExtent(updated_capabilities, width, height);
  156. // Don't add code within this and the swapchain creation.
  157. swapchain = device.GetLogical().CreateSwapchainKHR(swapchain_ci);
  158. extent = swapchain_ci.imageExtent;
  159. current_width = extent.width;
  160. current_height = extent.height;
  161. current_srgb = srgb;
  162. images = swapchain.GetImages();
  163. image_count = static_cast<u32>(images.size());
  164. image_format = surface_format.format;
  165. }
  166. void VKSwapchain::CreateSemaphores() {
  167. present_semaphores.resize(image_count);
  168. std::generate(present_semaphores.begin(), present_semaphores.end(),
  169. [this] { return device.GetLogical().CreateSemaphore(); });
  170. }
  171. void VKSwapchain::CreateImageViews() {
  172. VkImageViewCreateInfo ci;
  173. ci.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  174. ci.pNext = nullptr;
  175. ci.flags = 0;
  176. // ci.image
  177. ci.viewType = VK_IMAGE_VIEW_TYPE_2D;
  178. ci.format = image_format;
  179. ci.components = {VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY,
  180. VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY};
  181. ci.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  182. ci.subresourceRange.baseMipLevel = 0;
  183. ci.subresourceRange.levelCount = 1;
  184. ci.subresourceRange.baseArrayLayer = 0;
  185. ci.subresourceRange.layerCount = 1;
  186. image_views.resize(image_count);
  187. for (std::size_t i = 0; i < image_count; i++) {
  188. ci.image = images[i];
  189. image_views[i] = device.GetLogical().CreateImageView(ci);
  190. }
  191. }
  192. void VKSwapchain::Destroy() {
  193. frame_index = 0;
  194. present_semaphores.clear();
  195. framebuffers.clear();
  196. image_views.clear();
  197. swapchain.reset();
  198. }
  199. } // namespace Vulkan