BsVulkanSwapChain.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. VulkanImage* image;
  15. VkImageView view;
  16. VulkanSemaphore* 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. * 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. Returns the index of the image representing the current
  51. * back buffer.
  52. */
  53. UINT32 prepareForPresent();
  54. /** Returns information describing the current back buffer. */
  55. const SwapChainSurface& getBackBuffer() { return mSurfaces[mCurrentBackBufferIdx]; }
  56. /** Returns the number of available color surfaces. */
  57. UINT32 getNumColorSurfaces() const { return (UINT32)mSurfaces.size(); }
  58. /** Returns an image view representing the color surface at the specified index. */
  59. VkImageView getColorView(UINT32 index) const { return mSurfaces[index].view; }
  60. /** Returns an image view representing the depth-stencil buffer, if any. */
  61. VkImageView getDepthStencilView() const { return mDepthStencilView; }
  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. VkImageView mDepthStencilView = VK_NULL_HANDLE;
  74. UINT32 mCurrentSemaphoreIdx = 0;
  75. UINT32 mCurrentBackBufferIdx = 0;
  76. };
  77. /** @} */
  78. }