BsVulkanSwapChain.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 { namespace ct
  7. {
  8. /** @addtogroup Vulkan
  9. * @{
  10. */
  11. /** Description of a single swap chain surface. */
  12. struct SwapChainSurface
  13. {
  14. VulkanImage* image;
  15. VkImageView view;
  16. VulkanSemaphore* sync;
  17. bool acquired;
  18. bool needsWait;
  19. VulkanFramebuffer* framebuffer;
  20. VULKAN_FRAMEBUFFER_DESC framebufferDesc;
  21. };
  22. /** Vulkan swap chain containing two or more buffers for rendering and presenting onto the screen. */
  23. class VulkanSwapChain : INonCopyable
  24. {
  25. public:
  26. ~VulkanSwapChain();
  27. /**
  28. * Rebuilds the swap chain with the provided properties. Destroys any previously existing swap chain. Caller must
  29. * ensure the swap chain is not used at the device when this is called.
  30. */
  31. void rebuild(const SPtr<VulkanDevice>& device, VkSurfaceKHR surface, UINT32 width, UINT32 height, bool vsync,
  32. VkFormat colorFormat, VkColorSpaceKHR colorSpace, bool createDepth, VkFormat depthFormat);
  33. /**
  34. * Returns the actual width of the swap chain, in pixels. This might differ from the requested size in case it
  35. * wasn't supported.
  36. */
  37. UINT32 getWidth() const { return mWidth; }
  38. /**
  39. * Returns the actual height of the swap chain, in pixels. This might differ from the requested size in case it
  40. * wasn't supported.
  41. */
  42. UINT32 getHeight() const { return mHeight; }
  43. /**
  44. * Acquires a new back buffer image. Caller can retrieve the surface by calling getBackBuffer(). Caller must wait
  45. * on the semaphore provided by the surface before rendering to it.
  46. *
  47. * @note Must only be called once in-between present() calls, or before the first present() call.
  48. */
  49. void acquireBackBuffer();
  50. /**
  51. * Prepares the swap chain for the present operation.
  52. *
  53. * @param[out] backBufferIdx Index of the image representing the current back buffer.
  54. * @return True if there is anything to present, false otherwise.
  55. */
  56. bool prepareForPresent(UINT32& backBufferIdx);
  57. /** Notifies the chain that the semaphore waiting for the back buffer to become available is being waited on. */
  58. void notifyBackBufferWaitIssued();
  59. /** Returns information describing the current back buffer. */
  60. const SwapChainSurface& getBackBuffer() { return mSurfaces[mCurrentBackBufferIdx]; }
  61. /** Returns the number of available color surfaces. */
  62. UINT32 getNumColorSurfaces() const { return (UINT32)mSurfaces.size(); }
  63. /** Returns an image view representing the color surface at the specified index. */
  64. VkImageView getColorView(UINT32 index) const { return mSurfaces[index].view; }
  65. /** Returns an image view representing the depth-stencil buffer, if any. */
  66. VkImageView getDepthStencilView() const { return mDepthStencilView; }
  67. /** Returns the internal swap chain handle. */
  68. VkSwapchainKHR getHandle() const { return mSwapChain; }
  69. private:
  70. /** Destroys current swap chain and depth stencil image (if any). */
  71. void clear(VkSwapchainKHR swapChain);
  72. SPtr<VulkanDevice> mDevice;
  73. VkSwapchainKHR mSwapChain = VK_NULL_HANDLE;
  74. UINT32 mWidth = 0;
  75. UINT32 mHeight = 0;
  76. Vector<SwapChainSurface> mSurfaces;
  77. VulkanImage* mDepthStencilImage = nullptr;
  78. VkImageView mDepthStencilView = VK_NULL_HANDLE;
  79. UINT32 mCurrentSemaphoreIdx = 0;
  80. UINT32 mCurrentBackBufferIdx = 0;
  81. };
  82. /** @} */
  83. }}