BsVulkanSwapChain.h 3.0 KB

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