BsVulkanSwapChain.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. VulkanSemaphore* sync;
  16. bool acquired;
  17. bool needsWait;
  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. * Acquires a new back buffer image. Caller can retrieve the surface by calling getBackBuffer(). Caller must wait
  44. * on the semaphore provided by the surface before rendering to it.
  45. *
  46. * @note Must only be called once in-between present() calls, or before the first present() call.
  47. */
  48. void acquireBackBuffer();
  49. /**
  50. * Prepares the swap chain for the present operation.
  51. *
  52. * @param[out] backBufferIdx Index of the image representing the current back buffer.
  53. * @return True if there is anything to present, false otherwise.
  54. */
  55. bool prepareForPresent(UINT32& backBufferIdx);
  56. /** Notifies the chain that the semaphore waiting for the back buffer to become available is being waited on. */
  57. void notifyBackBufferWaitIssued();
  58. /** Returns information describing the current back buffer. */
  59. const SwapChainSurface& getBackBuffer() { return mSurfaces[mCurrentBackBufferIdx]; }
  60. /** Returns the number of available color surfaces. */
  61. UINT32 getNumColorSurfaces() const { return (UINT32)mSurfaces.size(); }
  62. /** Returns the internal swap chain handle. */
  63. VkSwapchainKHR getHandle() const { return mSwapChain; }
  64. private:
  65. /** Destroys current swap chain and depth stencil image (if any). */
  66. void clear(VkSwapchainKHR swapChain);
  67. SPtr<VulkanDevice> mDevice;
  68. VkSwapchainKHR mSwapChain = VK_NULL_HANDLE;
  69. UINT32 mWidth = 0;
  70. UINT32 mHeight = 0;
  71. Vector<SwapChainSurface> mSurfaces;
  72. VulkanImage* mDepthStencilImage = nullptr;
  73. UINT32 mCurrentSemaphoreIdx = 0;
  74. UINT32 mCurrentBackBufferIdx = 0;
  75. };
  76. /** @} */
  77. }}