BsVulkanCommandBuffer.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #include "BsVulkanResource.h"
  8. namespace BansheeEngine
  9. {
  10. /** @addtogroup Vulkan
  11. * @{
  12. */
  13. class VulkanCmdBuffer;
  14. #define BS_MAX_VULKAN_CB_PER_QUEUE_FAMILY BS_MAX_QUEUES_PER_TYPE * 32
  15. /** Pool that allocates and distributes Vulkan command buffers. */
  16. class VulkanCmdBufferPool
  17. {
  18. public:
  19. VulkanCmdBufferPool(VulkanDevice& device);
  20. ~VulkanCmdBufferPool();
  21. /**
  22. * Attempts to find a free command buffer, or creates a new one if not found. Caller must guarantee the provided
  23. * queue family is valid.
  24. */
  25. VulkanCmdBuffer* getBuffer(UINT32 queueFamily, bool secondary);
  26. private:
  27. /** Command buffer pool and related information. */
  28. struct PoolInfo
  29. {
  30. VkCommandPool pool = VK_NULL_HANDLE;
  31. VulkanCmdBuffer* buffers[BS_MAX_VULKAN_CB_PER_QUEUE_FAMILY];
  32. UINT32 queueFamily = -1;
  33. };
  34. /** Creates a new command buffer. */
  35. VulkanCmdBuffer* createBuffer(UINT32 queueFamily, bool secondary);
  36. VulkanDevice& mDevice;
  37. UnorderedMap<UINT32, PoolInfo> mPools;
  38. UINT32 mNextId;
  39. };
  40. /**
  41. * Represents a direct wrapper over an internal Vulkan command buffer. This is unlike VulkanCommandBuffer which is a
  42. * higher level class, and it allows for re-use by internally using multiple low-level command buffers.
  43. */
  44. class VulkanCmdBuffer
  45. {
  46. /** Possible states a command buffer can be in. */
  47. enum class State
  48. {
  49. /** Buffer is ready to be re-used. */
  50. Ready,
  51. /** Buffer is currently recording commands, but isn't recording a render pass. */
  52. Recording,
  53. /** Buffer is currently recording render pass commands. */
  54. RecordingRenderPass,
  55. /** Buffer is done recording but hasn't been submitted. */
  56. RecordingDone,
  57. /** Buffer is done recording and is currently submitted on a queue. */
  58. Submitted
  59. };
  60. public:
  61. VulkanCmdBuffer(VulkanDevice& device, UINT32 id, VkCommandPool pool, UINT32 queueFamily, bool secondary);
  62. ~VulkanCmdBuffer();
  63. /** Returns an unique identifier of this command buffer. */
  64. UINT32 getId() const { return mId; }
  65. /** Returns the index of the queue family this command buffer is executing on. */
  66. UINT32 getQueueFamily() const { return mQueueFamily; }
  67. /** Returns the index of the device this command buffer will execute on. */
  68. UINT32 getDeviceIdx() const;
  69. /** Makes the command buffer ready to start recording commands. */
  70. void begin();
  71. /** Ends command buffer command recording (as started with begin()). */
  72. void end();
  73. /** Begins render pass recording. Must be called within begin()/end() calls. */
  74. void beginRenderPass();
  75. /** Ends render pass recording (as started with beginRenderPass(). */
  76. void endRenderPass();
  77. /** Returns the handle to the internal Vulkan command buffer wrapped by this object. */
  78. VkCommandBuffer getHandle() const { return mCmdBuffer; }
  79. /** Returns a fence that can be used for tracking when the command buffer is done executing. */
  80. VkFence getFence() const { return mFence; }
  81. /**
  82. * Returns a semaphore that may be used for synchronizing execution between command buffers executing on different
  83. * queues.
  84. */
  85. VkSemaphore getSemaphore() const { return mSemaphore; }
  86. /** Returns true if the command buffer is currently being processed by the device. */
  87. bool isSubmitted() const { return mState == State::Submitted; }
  88. /** Returns true if the command buffer is ready to be submitted to a queue. */
  89. bool isReadyForSubmit() const { return mState == State::RecordingDone; }
  90. /** Returns a counter that gets incremented whenever the command buffer is done executing. */
  91. UINT32 getFenceCounter() const { return mFenceCounter; }
  92. /** Checks the internal fence and changes command buffer state if done executing. */
  93. void refreshFenceStatus();
  94. /**
  95. * Lets the command buffer know that the provided resource has been queued on it, and will be used by the
  96. * device when the command buffer is submitted.
  97. */
  98. void registerResource(VulkanResource* res, VulkanUseFlags flags);
  99. /**
  100. * Lets the command buffer know that the provided GPU params object has been bound to it, and its resources
  101. * and descriptors will be used by the device when the command buffer is submitted.
  102. */
  103. void registerGpuParams(const SPtr<VulkanGpuParams>& params);
  104. private:
  105. friend class VulkanCmdBufferPool;
  106. friend class VulkanCommandBuffer;
  107. /** Information about a resource currently queued for use on the command buffer. */
  108. struct ResourceInfo
  109. {
  110. VulkanUseFlags flags;
  111. };
  112. /**
  113. * Called just before the buffer has been submitted to the queue.
  114. *
  115. * @param[out] transitionInfo Contains barriers that transition resources to appropriate queues families
  116. * and/or transition image layouts. Caller should issue relevant pipeline barriers
  117. * according to this structure, before submitting the command buffer.
  118. */
  119. void prepareForSubmit(UnorderedMap<UINT32, TransitionInfo>& transitionInfo);
  120. /** Called after the buffer has been submitted to the queue. */
  121. void notifySubmit();
  122. UINT32 mId;
  123. UINT32 mQueueFamily;
  124. State mState;
  125. VulkanDevice& mDevice;
  126. VkCommandPool mPool;
  127. VkCommandBuffer mCmdBuffer;
  128. VkFence mFence;
  129. VkSemaphore mSemaphore;
  130. UINT32 mFenceCounter;
  131. UnorderedMap<VulkanResource*, ResourceInfo> mResources;
  132. UnorderedSet<SPtr<VulkanGpuParams>> mBoundParams;
  133. };
  134. /** CommandBuffer implementation for Vulkan. */
  135. class VulkanCommandBuffer : public CommandBuffer
  136. {
  137. public:
  138. /**
  139. * Submits the command buffer for execution.
  140. *
  141. * @param[in] syncMask Mask that controls which other command buffers does this command buffer depend upon
  142. * (if any). See description of @p syncMask parameter in RenderAPICore::executeCommands().
  143. */
  144. void submit(UINT32 syncMask);
  145. /** Checks if the submitted buffer finished executing, and updates state if it has. */
  146. void refreshSubmitStatus();
  147. /**
  148. * Returns the internal command buffer.
  149. *
  150. * @note This buffer will change after a submit() call.
  151. */
  152. VulkanCmdBuffer* getInternal() const { return mBuffer; }
  153. private:
  154. friend class VulkanCommandBufferManager;
  155. VulkanCommandBuffer(VulkanDevice& device, UINT32 id, GpuQueueType type, UINT32 deviceIdx, UINT32 queueIdx,
  156. bool secondary);
  157. /**
  158. * Tasks the command buffer to find a new internal command buffer. Call this after the command buffer has been
  159. * submitted to a queue (it's not allowed to be used until the queue is done with it).
  160. */
  161. void acquireNewBuffer();
  162. VulkanCmdBuffer* mBuffer;
  163. VulkanCmdBuffer* mSubmittedBuffer;
  164. VulkanDevice& mDevice;
  165. VulkanQueue* mQueue;
  166. UINT32 mIdMask;
  167. VkSemaphore mSemaphoresTemp[BS_MAX_COMMAND_BUFFERS];
  168. UnorderedMap<UINT32, TransitionInfo> mTransitionInfoTemp;
  169. };
  170. /** @} */
  171. }