BsVulkanCommandBuffer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. #include "BsVulkanGpuPipelineState.h"
  9. namespace BansheeEngine
  10. {
  11. class VulkanImage;
  12. /** @addtogroup Vulkan
  13. * @{
  14. */
  15. class VulkanCmdBuffer;
  16. #define BS_MAX_VULKAN_CB_PER_QUEUE_FAMILY BS_MAX_QUEUES_PER_TYPE * 32
  17. /** Pool that allocates and distributes Vulkan command buffers. */
  18. class VulkanCmdBufferPool
  19. {
  20. public:
  21. VulkanCmdBufferPool(VulkanDevice& device);
  22. ~VulkanCmdBufferPool();
  23. /**
  24. * Attempts to find a free command buffer, or creates a new one if not found. Caller must guarantee the provided
  25. * queue family is valid.
  26. */
  27. VulkanCmdBuffer* getBuffer(UINT32 queueFamily, bool secondary);
  28. private:
  29. /** Command buffer pool and related information. */
  30. struct PoolInfo
  31. {
  32. VkCommandPool pool = VK_NULL_HANDLE;
  33. VulkanCmdBuffer* buffers[BS_MAX_VULKAN_CB_PER_QUEUE_FAMILY];
  34. UINT32 queueFamily = -1;
  35. };
  36. /** Creates a new command buffer. */
  37. VulkanCmdBuffer* createBuffer(UINT32 queueFamily, bool secondary);
  38. VulkanDevice& mDevice;
  39. UnorderedMap<UINT32, PoolInfo> mPools;
  40. UINT32 mNextId;
  41. };
  42. /**
  43. * Represents a direct wrapper over an internal Vulkan command buffer. This is unlike VulkanCommandBuffer which is a
  44. * higher level class, and it allows for re-use by internally using multiple low-level command buffers.
  45. */
  46. class VulkanCmdBuffer
  47. {
  48. /** Possible states a command buffer can be in. */
  49. enum class State
  50. {
  51. /** Buffer is ready to be re-used. */
  52. Ready,
  53. /** Buffer is currently recording commands, but isn't recording a render pass. */
  54. Recording,
  55. /** Buffer is currently recording render pass commands. */
  56. RecordingRenderPass,
  57. /** Buffer is done recording but hasn't been submitted. */
  58. RecordingDone,
  59. /** Buffer is done recording and is currently submitted on a queue. */
  60. Submitted
  61. };
  62. public:
  63. VulkanCmdBuffer(VulkanDevice& device, UINT32 id, VkCommandPool pool, UINT32 queueFamily, bool secondary);
  64. ~VulkanCmdBuffer();
  65. /** Returns an unique identifier of this command buffer. */
  66. UINT32 getId() const { return mId; }
  67. /** Returns the index of the queue family this command buffer is executing on. */
  68. UINT32 getQueueFamily() const { return mQueueFamily; }
  69. /** Returns the index of the device this command buffer will execute on. */
  70. UINT32 getDeviceIdx() const;
  71. /** Makes the command buffer ready to start recording commands. */
  72. void begin();
  73. /** Ends command buffer command recording (as started with begin()). */
  74. void end();
  75. /** Begins render pass recording. Must be called within begin()/end() calls. */
  76. void beginRenderPass();
  77. /** Ends render pass recording (as started with beginRenderPass(). */
  78. void endRenderPass();
  79. /**
  80. * Submits the command buffer for execution.
  81. *
  82. * @param[in] queue Queue to submit the command buffer on.
  83. * @param[in] queueIdx Index of the queue the command buffer was submitted on. Note that this may be different
  84. * from the actual VulkanQueue index since multiple command buffer queue indices can map
  85. * to the same queue.
  86. * @param[in] syncMask Mask that controls which other command buffers does this command buffer depend upon
  87. * (if any). See description of @p syncMask parameter in RenderAPICore::executeCommands().
  88. */
  89. void submit(VulkanQueue* queue, UINT32 queueIdx, UINT32 syncMask);
  90. /** Returns the handle to the internal Vulkan command buffer wrapped by this object. */
  91. VkCommandBuffer getHandle() const { return mCmdBuffer; }
  92. /** Returns a fence that can be used for tracking when the command buffer is done executing. */
  93. VkFence getFence() const { return mFence; }
  94. /**
  95. * Returns a semaphore that may be used for synchronizing execution between command buffers executing on different
  96. * queues.
  97. */
  98. VkSemaphore getSemaphore() const { return mSemaphore; }
  99. /** Returns true if the command buffer is currently being processed by the device. */
  100. bool isSubmitted() const { return mState == State::Submitted; }
  101. /** Returns true if the command buffer is ready to be submitted to a queue. */
  102. bool isReadyForSubmit() const { return mState == State::RecordingDone; }
  103. /** Returns true if the command buffer is currently recording a render pass. */
  104. bool isInRenderPass() const { return mState == State::RecordingRenderPass; }
  105. /** Returns a counter that gets incremented whenever the command buffer is done executing. */
  106. UINT32 getFenceCounter() const { return mFenceCounter; }
  107. /** Checks the internal fence and changes command buffer state if done executing. */
  108. void refreshFenceStatus();
  109. /**
  110. * Lets the command buffer know that the provided resource has been queued on it, and will be used by the
  111. * device when the command buffer is submitted. If a resource is an image or a buffer use the more specific
  112. * registerResource() overload.
  113. */
  114. void registerResource(VulkanResource* res, VulkanUseFlags flags);
  115. /**
  116. * Lets the command buffer know that the provided image resource has been queued on it, and will be used by the
  117. * device when the command buffer is submitted. If a resource is an image or a buffer use the more specific
  118. * registerResource() overload.
  119. */
  120. void registerResource(VulkanImage* res, VkAccessFlags accessFlags, VkImageLayout layout,
  121. const VkImageSubresourceRange& range, VulkanUseFlags flags);
  122. /**
  123. * Lets the command buffer know that the provided image resource has been queued on it, and will be used by the
  124. * device when the command buffer is submitted. If a resource is an image or a buffer use the more specific
  125. * registerResource() overload.
  126. */
  127. void registerResource(VulkanBuffer* res, VkAccessFlags accessFlags, VulkanUseFlags flags);
  128. /************************************************************************/
  129. /* COMMANDS */
  130. /************************************************************************/
  131. /**
  132. * Assigns a render target the the command buffer. This render target's framebuffer and render pass will be used
  133. * when beginRenderPass() is called. Command buffer must not be currently recording a render pass.
  134. */
  135. void setRenderTarget(const SPtr<RenderTargetCore>& rt, bool readOnlyDepthStencil);
  136. /** Assigns a pipeline state to use for subsequent draw commands. */
  137. void setPipelineState(const SPtr<GraphicsPipelineStateCore>& state);
  138. /** Assigns a pipeline state to use for subsequent dispatch commands. */
  139. void setPipelineState(const SPtr<ComputePipelineStateCore>& state);
  140. /** Sets the current viewport which determine to which portion of the render target to render to. */
  141. void setViewport(const Rect2& area);
  142. /**
  143. * Sets the scissor rectangle area which determines in which area if the viewport are the fragments allowed to be
  144. * generated. Only relevant if enabled on the pipeline state.
  145. */
  146. void setScissorRect(const Rect2I& area);
  147. /** Sets a stencil reference value that will be used for comparisons in stencil operations, if enabled. */
  148. void setStencilRef(UINT32 value);
  149. /** Changes how are primitives interpreted as during rendering. */
  150. void setDrawOp(DrawOperationType drawOp);
  151. /** Sets one or multiple vertex buffers that will be used for subsequent draw() or drawIndexed() calls. */
  152. void setVertexBuffers(UINT32 index, SPtr<VertexBufferCore>* buffers, UINT32 numBuffers);
  153. /** Sets an index buffer that will be used for subsequent drawIndexed() calls. */
  154. void setIndexBuffer(const SPtr<IndexBufferCore>& buffer);
  155. /** Sets a declaration that determines how are vertex buffer contents interpreted. */
  156. void setVertexDeclaration(const SPtr<VertexDeclarationCore>& decl);
  157. /** Executes a draw command using the currently bound graphics pipeline, vertex buffer and render target. */
  158. void draw(UINT32 vertexOffset, UINT32 vertexCount, UINT32 instanceCount);
  159. /** Executes a draw command using the currently bound graphics pipeline, index & vertex buffer and render target. */
  160. void drawIndexed(UINT32 startIndex, UINT32 indexCount, UINT32 vertexOffset, UINT32 instanceCount);
  161. /** Executes a dispatch command using the currently bound compute pipeline. */
  162. void dispatch(UINT32 numGroupsX, UINT32 numGroupsY, UINT32 numGroupsZ);
  163. private:
  164. friend class VulkanCmdBufferPool;
  165. friend class VulkanCommandBuffer;
  166. /** Contains information about a single Vulkan resource bound/used on this command buffer. */
  167. struct ResourceUseHandle
  168. {
  169. bool used;
  170. VulkanUseFlags flags;
  171. };
  172. /** Contains information about a single Vulkan buffer resource bound/used on this command buffer. */
  173. struct BufferInfo
  174. {
  175. VkAccessFlags accessFlags;
  176. ResourceUseHandle useHandle;
  177. };
  178. /** Contains information about a single Vulkan image resource bound/used on this command buffer. */
  179. struct ImageInfo
  180. {
  181. VkAccessFlags accessFlags;
  182. VkImageLayout layout;
  183. VkImageSubresourceRange range;
  184. ResourceUseHandle useHandle;
  185. };
  186. /** Checks if all the prerequisites for rendering have been made (e.g. render target and pipeline state are set. */
  187. bool isReadyForRender();
  188. /** Binds the current graphics pipeline to the command buffer. */
  189. void bindGraphicsPipeline();
  190. /** Binds any dynamic states to the pipeline, as required.
  191. *
  192. * @param[in] forceAll If true all states will be bound. If false only states marked as dirty will be bound.
  193. */
  194. void bindDynamicStates(bool forceAll);
  195. UINT32 mId;
  196. UINT32 mQueueFamily;
  197. State mState;
  198. VulkanDevice& mDevice;
  199. VkCommandPool mPool;
  200. VkCommandBuffer mCmdBuffer;
  201. VkFence mFence;
  202. VkSemaphore mSemaphore;
  203. UINT32 mFenceCounter;
  204. VulkanFramebuffer* mFramebuffer;
  205. VkSemaphore mPresentSemaphore;
  206. UINT32 mRenderTargetWidth;
  207. UINT32 mRenderTargetHeight;
  208. bool mRenderTargetDepthReadOnly;
  209. UnorderedMap<VulkanResource*, ResourceUseHandle> mResources;
  210. UnorderedMap<VulkanResource*, ImageInfo> mImages;
  211. UnorderedMap<VulkanResource*, BufferInfo> mBuffers;
  212. UINT32 mGlobalQueueIdx;
  213. SPtr<VulkanGraphicsPipelineStateCore> mGraphicsPipeline;
  214. SPtr<VulkanComputePipelineStateCore> mComputePipeline;
  215. SPtr<VertexDeclarationCore> mVertexDecl;
  216. Rect2 mViewport;
  217. Rect2I mScissor;
  218. UINT32 mStencilRef;
  219. DrawOperationType mDrawOp;
  220. bool mGfxPipelineRequiresBind : 1;
  221. bool mCmpPipelineRequiresBind : 1;
  222. bool mViewportRequiresBind : 1;
  223. bool mStencilRefRequiresBind : 1;
  224. bool mScissorRequiresBind : 1;
  225. VkSemaphore mSemaphoresTemp[BS_MAX_UNIQUE_QUEUES + 1]; // +1 for present semaphore
  226. VkBuffer mVertexBuffersTemp[BS_MAX_BOUND_VERTEX_BUFFERS];
  227. VkDeviceSize mVertexBufferOffsetsTemp[BS_MAX_BOUND_VERTEX_BUFFERS];
  228. UnorderedMap<UINT32, TransitionInfo> mTransitionInfoTemp;
  229. };
  230. /** CommandBuffer implementation for Vulkan. */
  231. class VulkanCommandBuffer : public CommandBuffer
  232. {
  233. public:
  234. /**
  235. * Submits the command buffer for execution.
  236. *
  237. * @param[in] syncMask Mask that controls which other command buffers does this command buffer depend upon
  238. * (if any). See description of @p syncMask parameter in RenderAPICore::executeCommands().
  239. */
  240. void submit(UINT32 syncMask);
  241. /**
  242. * Returns the internal command buffer.
  243. *
  244. * @note This buffer will change after a submit() call.
  245. */
  246. VulkanCmdBuffer* getInternal() const { return mBuffer; }
  247. private:
  248. friend class VulkanCommandBufferManager;
  249. VulkanCommandBuffer(VulkanDevice& device, GpuQueueType type, UINT32 deviceIdx, UINT32 queueIdx,
  250. bool secondary);
  251. /**
  252. * Tasks the command buffer to find a new internal command buffer. Call this after the command buffer has been
  253. * submitted to a queue (it's not allowed to be used until the queue is done with it).
  254. */
  255. void acquireNewBuffer();
  256. VulkanCmdBuffer* mBuffer;
  257. VulkanDevice& mDevice;
  258. VulkanQueue* mQueue;
  259. UINT32 mIdMask;
  260. VkSemaphore mSemaphoresTemp[BS_MAX_UNIQUE_QUEUES];
  261. UnorderedMap<UINT32, TransitionInfo> mTransitionInfoTemp;
  262. };
  263. /** @} */
  264. }