BsVulkanCommandBuffer.h 5.6 KB

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