BsVulkanCommandBuffer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "BsCommandBuffer.h"
  6. #include "BsVulkanRenderAPI.h"
  7. namespace BansheeEngine
  8. {
  9. /** @addtogroup Vulkan
  10. * @{
  11. */
  12. class VulkanCmdBuffer;
  13. #define BS_MAX_VULKAN_COMMAND_BUFFERS_PER_QUEUE 32
  14. /** Pool that allocates and distributes Vulkan command buffers. */
  15. class VulkanCmdBufferPool
  16. {
  17. public:
  18. VulkanCmdBufferPool(VulkanDevice& device);
  19. ~VulkanCmdBufferPool();
  20. /** Attempts to find a free command buffer, or creates a new one if not found. */
  21. VulkanCmdBuffer* getBuffer(CommandBufferType type, UINT32 queueIdx, bool secondary);
  22. private:
  23. /** Creates a new command buffer. */
  24. VulkanCmdBuffer* createBuffer(VulkanQueueType type, bool secondary);
  25. /** Returns a Vulkan command pool for the specified queue type. */
  26. VkCommandPool getPool(VulkanQueueType type);
  27. VulkanDevice& mDevice;
  28. VkCommandPool mPools[VQT_COUNT];
  29. VulkanCmdBuffer* mBuffers[VQT_COUNT][BS_MAX_QUEUES_PER_TYPE][BS_MAX_VULKAN_COMMAND_BUFFERS_PER_QUEUE];
  30. };
  31. /**
  32. * Represents a direct wrapper over an internal Vulkan command buffer. This is unlike VulkanCommandBuffer which is a
  33. * higher level class, and it allows for re-use by internally using multiple low-level command buffers.
  34. */
  35. class VulkanCmdBuffer
  36. {
  37. /** Possible states a command buffer can be in. */
  38. enum class State
  39. {
  40. /** Buffer is ready to be re-used. */
  41. Ready,
  42. /** Buffer is currently recording commands, but isn't recording a render pass. */
  43. Recording,
  44. /** Buffer is currently recording render pass commands. */
  45. RecordingRenderPass,
  46. /** Buffer is done recording but hasn't been submitted. */
  47. RecordingDone,
  48. /** Buffer is done recording and is currently submitted on a queue. */
  49. Submitted
  50. };
  51. public:
  52. VulkanCmdBuffer(VulkanDevice& device, VkCommandPool pool, bool secondary);
  53. ~VulkanCmdBuffer();
  54. /** Makes the command buffer ready to start recording commands. */
  55. void begin();
  56. /** Ends command buffer command recording (as started with begin()). */
  57. void end();
  58. /** Begins render pass recording. Must be called within begin()/end() calls. */
  59. void beginRenderPass();
  60. /** Ends render pass recording (as started with beginRenderPass(). */
  61. void endRenderPass();
  62. /** Returns the handle to the internal Vulkan command buffer wrapped by this object. */
  63. VkCommandBuffer getHandle() const { return mCmdBuffer; }
  64. /** Returns a fence that can be used for tracking when the command buffer is done executing. */
  65. VkFence getFence() const { return mFence; }
  66. /**
  67. * Returns a semaphore that may be used for synchronizing execution between command buffers executing on different
  68. * queues.
  69. */
  70. VkSemaphore getSemaphore() const { return mSemaphore; }
  71. /** Returns true if the command buffer is currently being processed by the device. */
  72. bool isSubmitted() const { return mState == State::Submitted; }
  73. /** Returns true if the command buffer is ready to be submitted to a queue. */
  74. bool isReadyForSubmit() const { return mState == State::RecordingDone; }
  75. /** Returns a counter that gets incremented whenever the command buffer is done executing. */
  76. UINT32 getFenceCounter() const { return mFenceCounter; }
  77. /** Checks the internal fence and changes command buffer state if done executing. */
  78. void refreshFenceStatus();
  79. private:
  80. friend class VulkanCmdBufferPool;
  81. friend class VulkanCommandBuffer;
  82. State mState;
  83. VulkanDevice& mDevice;
  84. VkCommandPool mPool;
  85. VkCommandBuffer mCmdBuffer;
  86. VkFence mFence;
  87. VkSemaphore mSemaphore;
  88. UINT32 mFenceCounter;
  89. };
  90. /** CommandBuffer implementation for Vulkan. */
  91. class VulkanCommandBuffer : public CommandBuffer
  92. {
  93. public:
  94. /**
  95. * Submits the command buffer for execution.
  96. *
  97. * @param[in] syncMask Mask that controls which other command buffers does this command buffer depend upon
  98. * (if any). See description of @p syncMask parameter in RenderAPICore::executeCommands().
  99. */
  100. void submit(UINT32 syncMask);
  101. /** Checks if the submitted buffer finished executing, and updates state if it has. */
  102. void refreshSubmitStatus();
  103. private:
  104. friend class VulkanCommandBufferManager;
  105. VulkanCommandBuffer(VulkanDevice& device, UINT32 id, CommandBufferType type, UINT32 deviceIdx, UINT32 queueIdx,
  106. bool secondary);
  107. /**
  108. * Tasks the command buffer to find a new internal command buffer. Call this after the command buffer has been
  109. * submitted to a queue (it's not allowed to be used until the queue is done with it).
  110. */
  111. void acquireNewBuffer();
  112. VulkanCmdBuffer* mBuffer;
  113. VulkanCmdBuffer* mSubmittedBuffer;
  114. VulkanDevice& mDevice;
  115. VulkanQueue* mQueue;
  116. VkSemaphore mSemaphoresTemp[BS_MAX_COMMAND_BUFFERS];
  117. };
  118. /** @} */
  119. }