BsVulkanSwapChain.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsVulkanSwapChain.h"
  4. #include "BsVulkanRenderAPI.h"
  5. #include "BsVulkanDevice.h"
  6. namespace BansheeEngine
  7. {
  8. VulkanSwapChain::~VulkanSwapChain()
  9. {
  10. clear(mSwapChain);
  11. }
  12. void VulkanSwapChain::rebuild(const SPtr<VulkanDevice>& device, VkSurfaceKHR surface, UINT32 width, UINT32 height,
  13. bool vsync, VkFormat colorFormat, VkColorSpaceKHR colorSpace, bool createDepth, VkFormat depthFormat)
  14. {
  15. mDevice = device;
  16. VkResult result;
  17. VkPhysicalDevice physicalDevice = device->getPhysical();
  18. // Determine swap chain dimensions
  19. VkSurfaceCapabilitiesKHR surfaceCaps;
  20. result = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &surfaceCaps);
  21. assert(result == VK_SUCCESS);
  22. VkExtent2D swapchainExtent;
  23. // If width/height is 0xFFFFFFFF, we can manually specify width, height
  24. if (surfaceCaps.currentExtent.width == (uint32_t)-1 || surfaceCaps.currentExtent.height == (uint32_t)-1)
  25. {
  26. swapchainExtent.width = width;
  27. swapchainExtent.height = height;
  28. }
  29. else // Otherwise we must use the size we're given
  30. swapchainExtent = surfaceCaps.currentExtent;
  31. mWidth = swapchainExtent.width;
  32. mHeight = swapchainExtent.height;
  33. // Find present mode
  34. uint32_t numPresentModes;
  35. result = vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &numPresentModes, nullptr);
  36. assert(result == VK_SUCCESS);
  37. assert(numPresentModes > 0);
  38. VkPresentModeKHR* presentModes = bs_stack_alloc<VkPresentModeKHR>(numPresentModes);
  39. result = vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, surface, &numPresentModes, presentModes);
  40. assert(result == VK_SUCCESS);
  41. VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR;
  42. if(!vsync)
  43. {
  44. for (UINT32 i = 0; i < numPresentModes; i++)
  45. {
  46. if (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)
  47. {
  48. presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
  49. break;
  50. }
  51. if (presentMode == VK_PRESENT_MODE_FIFO_RELAXED_KHR)
  52. presentMode = VK_PRESENT_MODE_FIFO_RELAXED_KHR;
  53. }
  54. }
  55. else
  56. {
  57. for (UINT32 i = 0; i < numPresentModes; i++)
  58. {
  59. if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR)
  60. {
  61. presentMode = VK_PRESENT_MODE_MAILBOX_KHR;
  62. break;
  63. }
  64. }
  65. }
  66. bs_stack_free(presentModes);
  67. uint32_t numImages = std::min(surfaceCaps.minImageCount + BS_NUM_BACK_BUFFERS, surfaceCaps.maxImageCount);
  68. VkSurfaceTransformFlagsKHR transform;
  69. if (surfaceCaps.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR)
  70. transform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
  71. else
  72. transform = surfaceCaps.currentTransform;
  73. VkSwapchainCreateInfoKHR swapChainCI;
  74. swapChainCI.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
  75. swapChainCI.pNext = nullptr;
  76. swapChainCI.flags = 0;
  77. swapChainCI.surface = surface;
  78. swapChainCI.minImageCount = numImages;
  79. swapChainCI.imageFormat = colorFormat;
  80. swapChainCI.imageColorSpace = colorSpace;
  81. swapChainCI.imageExtent = { swapchainExtent.width, swapchainExtent.height };
  82. swapChainCI.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
  83. swapChainCI.preTransform = (VkSurfaceTransformFlagBitsKHR)transform;
  84. swapChainCI.imageArrayLayers = 1;
  85. swapChainCI.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
  86. swapChainCI.queueFamilyIndexCount = 0;
  87. swapChainCI.pQueueFamilyIndices = NULL;
  88. swapChainCI.presentMode = presentMode;
  89. swapChainCI.oldSwapchain = mSwapChain;
  90. swapChainCI.clipped = VK_TRUE;
  91. swapChainCI.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
  92. VkSwapchainKHR oldSwapChain = mSwapChain;
  93. VkDevice logicalDevice = device->getLogical();
  94. result = vkCreateSwapchainKHR(logicalDevice, &swapChainCI, gVulkanAllocator, &mSwapChain);
  95. assert(result == VK_SUCCESS);
  96. clear(oldSwapChain);
  97. result = vkGetSwapchainImagesKHR(logicalDevice, mSwapChain, &numImages, nullptr);
  98. assert(result == VK_SUCCESS);
  99. // Get the swap chain images
  100. VkImage* images = bs_stack_alloc<VkImage>(numImages);
  101. result = vkGetSwapchainImagesKHR(logicalDevice, mSwapChain, &numImages, images);
  102. assert(result == VK_SUCCESS);
  103. mSurfaces.resize(numImages);
  104. for (UINT32 i = 0; i < numImages; i++)
  105. {
  106. VkImageViewCreateInfo colorViewCI;
  107. colorViewCI.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  108. colorViewCI.pNext = nullptr;
  109. colorViewCI.flags = 0;
  110. colorViewCI.viewType = VK_IMAGE_VIEW_TYPE_2D;
  111. colorViewCI.image = images[i];
  112. colorViewCI.format = colorFormat;
  113. colorViewCI.components = {
  114. VK_COMPONENT_SWIZZLE_R,
  115. VK_COMPONENT_SWIZZLE_G,
  116. VK_COMPONENT_SWIZZLE_B,
  117. VK_COMPONENT_SWIZZLE_A
  118. };
  119. colorViewCI.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  120. colorViewCI.subresourceRange.baseMipLevel = 0;
  121. colorViewCI.subresourceRange.levelCount = 1;
  122. colorViewCI.subresourceRange.baseArrayLayer = 0;
  123. colorViewCI.subresourceRange.layerCount = 1;
  124. mSurfaces[i].acquired = false;
  125. mSurfaces[i].image = images[i];
  126. result = vkCreateImageView(logicalDevice, &colorViewCI, gVulkanAllocator, &mSurfaces[i].view);
  127. assert(result == VK_SUCCESS);
  128. VkSemaphoreCreateInfo semaphoreCI;
  129. semaphoreCI.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
  130. semaphoreCI.pNext = nullptr;
  131. semaphoreCI.flags = 0;
  132. result = vkCreateSemaphore(logicalDevice, &semaphoreCI, gVulkanAllocator, &mSurfaces[i].sync);
  133. assert(result == VK_SUCCESS);
  134. }
  135. bs_stack_free(images);
  136. // Create depth stencil image
  137. if (createDepth)
  138. {
  139. VkImageCreateInfo depthStencilImageCI;
  140. depthStencilImageCI.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
  141. depthStencilImageCI.pNext = nullptr;
  142. depthStencilImageCI.flags = 0;
  143. depthStencilImageCI.imageType = VK_IMAGE_TYPE_2D;
  144. depthStencilImageCI.format = depthFormat;
  145. depthStencilImageCI.extent = { width, height, 1 };
  146. depthStencilImageCI.mipLevels = 1;
  147. depthStencilImageCI.arrayLayers = 1;
  148. depthStencilImageCI.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  149. depthStencilImageCI.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  150. depthStencilImageCI.samples = VK_SAMPLE_COUNT_1_BIT;
  151. depthStencilImageCI.tiling = VK_IMAGE_TILING_OPTIMAL;
  152. depthStencilImageCI.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
  153. depthStencilImageCI.pQueueFamilyIndices = nullptr;
  154. depthStencilImageCI.queueFamilyIndexCount = 0;
  155. result = vkCreateImage(logicalDevice, &depthStencilImageCI, gVulkanAllocator, &mDepthStencilImage);
  156. assert(result == VK_SUCCESS);
  157. mDepthStencilMemory = mDevice->allocateMemory(mDepthStencilImage, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
  158. VkImageViewCreateInfo depthStencilViewCI;
  159. depthStencilViewCI.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  160. depthStencilViewCI.pNext = nullptr;
  161. depthStencilViewCI.flags = 0;
  162. depthStencilViewCI.viewType = VK_IMAGE_VIEW_TYPE_2D;
  163. depthStencilViewCI.image = mDepthStencilImage;
  164. depthStencilViewCI.format = depthFormat;
  165. depthStencilViewCI.components = {
  166. VK_COMPONENT_SWIZZLE_R,
  167. VK_COMPONENT_SWIZZLE_G,
  168. VK_COMPONENT_SWIZZLE_B,
  169. VK_COMPONENT_SWIZZLE_A
  170. };
  171. depthStencilViewCI.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
  172. depthStencilViewCI.subresourceRange.baseMipLevel = 0;
  173. depthStencilViewCI.subresourceRange.levelCount = 1;
  174. depthStencilViewCI.subresourceRange.baseArrayLayer = 0;
  175. depthStencilViewCI.subresourceRange.layerCount = 1;
  176. result = vkCreateImageView(logicalDevice, &depthStencilViewCI, gVulkanAllocator, &mDepthStencilView);
  177. assert(result == VK_SUCCESS);
  178. }
  179. else
  180. {
  181. mDepthStencilImage = VK_NULL_HANDLE;
  182. mDepthStencilView = VK_NULL_HANDLE;
  183. mDepthStencilMemory = VK_NULL_HANDLE;
  184. }
  185. // Create a framebuffer for each swap chain buffer
  186. UINT32 numFramebuffers = (UINT32)mSurfaces.size();
  187. for (UINT32 i = 0; i < numFramebuffers; i++)
  188. {
  189. VULKAN_FRAMEBUFFER_DESC& desc = mSurfaces[i].framebufferDesc;
  190. desc.width = getWidth();
  191. desc.height = getHeight();
  192. desc.layers = 1;
  193. desc.numSamples = 1;
  194. desc.offscreen = false;
  195. desc.color[0].format = colorFormat;
  196. desc.color[0].view = mSurfaces[i].view;
  197. desc.depth.format = depthFormat;
  198. desc.depth.view = mDepthStencilView;
  199. mSurfaces[i].framebuffer = bs_new<VulkanFramebuffer>(device, desc);
  200. }
  201. }
  202. void VulkanSwapChain::present(VkQueue queue, VkSemaphore* semaphores, UINT32 numSemaphores)
  203. {
  204. assert(mSurfaces[mCurrentBackBufferIdx].acquired && "Attempting to present an unacquired back buffer.");
  205. mSurfaces[mCurrentBackBufferIdx].acquired = false;
  206. VkPresentInfoKHR presentInfo;
  207. presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
  208. presentInfo.pNext = nullptr;
  209. presentInfo.swapchainCount = 1;
  210. presentInfo.pSwapchains = &mSwapChain;
  211. presentInfo.pImageIndices = &mCurrentBackBufferIdx;
  212. presentInfo.pResults = nullptr;
  213. // Wait before presenting, if required
  214. if (numSemaphores > 0)
  215. {
  216. presentInfo.pWaitSemaphores = semaphores;
  217. presentInfo.waitSemaphoreCount = numSemaphores;
  218. }
  219. else
  220. {
  221. presentInfo.pWaitSemaphores = nullptr;
  222. presentInfo.waitSemaphoreCount = 0;
  223. }
  224. VkResult result = vkQueuePresentKHR(queue, &presentInfo);
  225. assert(result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR);
  226. }
  227. void VulkanSwapChain::acquireBackBuffer()
  228. {
  229. uint32_t imageIndex;
  230. VkResult result = vkAcquireNextImageKHR(mDevice->getLogical(), mSwapChain, UINT64_MAX,
  231. mSurfaces[mCurrentSemaphoreIdx].sync, VK_NULL_HANDLE, &imageIndex);
  232. assert(result == VK_SUCCESS || result == VK_SUBOPTIMAL_KHR);
  233. // In case surfaces aren't being distributed in round-robin fashion the image and semaphore indices might not match,
  234. // in which case just move the semaphores
  235. if(imageIndex != mCurrentSemaphoreIdx)
  236. std::swap(mSurfaces[mCurrentSemaphoreIdx].sync, mSurfaces[imageIndex].sync);
  237. mCurrentSemaphoreIdx = (mCurrentSemaphoreIdx + 1) % mSurfaces.size();
  238. assert(!mSurfaces[imageIndex].acquired && "Same swap chain surface being acquired twice in a row without present().");
  239. mSurfaces[imageIndex].acquired = true;
  240. mCurrentBackBufferIdx = imageIndex;
  241. }
  242. void VulkanSwapChain::clear(VkSwapchainKHR swapChain)
  243. {
  244. VkDevice logicalDevice = mDevice->getLogical();
  245. if (swapChain != VK_NULL_HANDLE)
  246. {
  247. for (auto& surface : mSurfaces)
  248. {
  249. bs_delete(surface.framebuffer);
  250. vkDestroySemaphore(logicalDevice, surface.sync, gVulkanAllocator);
  251. vkDestroyImageView(logicalDevice, surface.view, gVulkanAllocator);
  252. }
  253. vkDestroySwapchainKHR(logicalDevice, swapChain, gVulkanAllocator);
  254. }
  255. if (mDepthStencilImage != VK_NULL_HANDLE)
  256. {
  257. vkDestroyImageView(logicalDevice, mDepthStencilView, gVulkanAllocator);
  258. vkDestroyImage(logicalDevice, mDepthStencilImage, gVulkanAllocator);
  259. mDevice->freeMemory(mDepthStencilMemory);
  260. mDepthStencilImage = VK_NULL_HANDLE;
  261. }
  262. }
  263. }