BsVulkanSwapChain.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsVulkanPrerequisites.h"
  5. #include "BsVulkanFramebuffer.h"
  6. namespace bs
  7. {
  8. /** @addtogroup Vulkan
  9. * @{
  10. */
  11. /** Description of a single swap chain surface. */
  12. struct SwapChainSurface
  13. {
  14. VkImage image;
  15. VkImageView view;
  16. VkSemaphore sync;
  17. bool acquired;
  18. VulkanFramebuffer* framebuffer;
  19. VULKAN_FRAMEBUFFER_DESC framebufferDesc;
  20. };
  21. /** Vulkan swap chain containing two or more buffers for rendering and presenting onto the screen. */
  22. class VulkanSwapChain : INonCopyable
  23. {
  24. public:
  25. ~VulkanSwapChain();
  26. /**
  27. * Rebuilds the swap chain with the provided properties. Destroys any previously existing swap chain. Caller must
  28. * ensure the swap chain is not used at the device when this is called.
  29. */
  30. void rebuild(const SPtr<VulkanDevice>& device, VkSurfaceKHR surface, UINT32 width, UINT32 height, bool vsync,
  31. VkFormat colorFormat, VkColorSpaceKHR colorSpace, bool createDepth, VkFormat depthFormat);
  32. /**
  33. * Returns the actual width of the swap chain, in pixels. This might differ from the requested size in case it
  34. * wasn't supported.
  35. */
  36. UINT32 getWidth() const { return mWidth; }
  37. /**
  38. * Returns the actual height of the swap chain, in pixels. This might differ from the requested size in case it
  39. * wasn't supported.
  40. */
  41. UINT32 getHeight() const { return mHeight; }
  42. /**
  43. * Presents the back buffer to the output device, swapping the buffers.
  44. *
  45. * @param[in] queue Queue on which to queue the present operation. Must support present operations.
  46. * @param[in] semaphores Optional semaphores to wait on before presenting the queue.
  47. * @param[in] numSemaphores Number of semaphores in the @p semaphores array.
  48. */
  49. void present(VkQueue queue, VkSemaphore* semaphores, UINT32 numSemaphores);
  50. /**
  51. * Acquires a new back buffer image. Caller can retrieve the surface by calling getBackBuffer(). Caller must wait
  52. * on the semaphore provided by the surface before rendering to it.
  53. *
  54. * @note Must only be called once in-between present() calls, or before the first present() call.
  55. */
  56. void acquireBackBuffer();
  57. /** Returns information describing the current back buffer. */
  58. const SwapChainSurface& getBackBuffer() { return mSurfaces[mCurrentBackBufferIdx]; }
  59. /** Returns the number of available color surfaces. */
  60. UINT32 getNumColorSurfaces() const { return (UINT32)mSurfaces.size(); }
  61. /** Returns an image view representing the color surface at the specified index. */
  62. VkImageView getColorView(UINT32 index) const { return mSurfaces[index].view; }
  63. /** Returns an image view representing the depth-stencil buffer, if any. */
  64. VkImageView getDepthStencilView() const { return mDepthStencilView; }
  65. private:
  66. /** Destroys current swap chain and depth stencil image (if any). */
  67. void clear(VkSwapchainKHR swapChain);
  68. SPtr<VulkanDevice> mDevice;
  69. VkSwapchainKHR mSwapChain = VK_NULL_HANDLE;
  70. UINT32 mWidth = 0;
  71. UINT32 mHeight = 0;
  72. Vector<SwapChainSurface> mSurfaces;
  73. VkImage mDepthStencilImage = VK_NULL_HANDLE;
  74. VkImageView mDepthStencilView = VK_NULL_HANDLE;
  75. VkDeviceMemory mDepthStencilMemory = nullptr;
  76. UINT32 mCurrentSemaphoreIdx = 0;
  77. UINT32 mCurrentBackBufferIdx = 0;
  78. };
  79. /** @} */
  80. }