BsVulkanCommandBuffer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 LVulkanCommandBuffer;
  13. #define BS_MAX_VULKAN_COMMAND_BUFFERS_PER_QUEUE 32
  14. /** Pool that allocates and distributes Vulkan command buffers. */
  15. class CommandBufferPool
  16. {
  17. public:
  18. CommandBufferPool(VulkanDevice& device);
  19. ~CommandBufferPool();
  20. /** Attempts to find a free command buffer, or creates a new one if not found. */
  21. LVulkanCommandBuffer* getBuffer(CommandBufferType type, UINT32 queueIdx, bool secondary);
  22. private:
  23. /** Creates a new command buffer. */
  24. LVulkanCommandBuffer* 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. LVulkanCommandBuffer* 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 LVulkanCommandBuffer
  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. LVulkanCommandBuffer(VulkanDevice& device, VkCommandPool pool, bool secondary);
  53. ~LVulkanCommandBuffer();
  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 a counter that gets incremented whenever the command buffer is done executing. */
  72. UINT32 getFenceCounter() const { return mFenceCounter; }
  73. /** Checks the internal fence and changes command buffer state if done executing. */
  74. void refreshFenceStatus();
  75. private:
  76. friend class CommandBufferPool;
  77. State mState;
  78. VulkanDevice& mDevice;
  79. VkCommandPool mPool;
  80. VkCommandBuffer mCmdBuffer;
  81. VkFence mFence;
  82. VkSemaphore mSemaphore;
  83. UINT32 mFenceCounter;
  84. };
  85. /** CommandBuffer implementation for Vulkan. */
  86. class VulkanCommandBuffer : public CommandBuffer
  87. {
  88. public:
  89. /**
  90. * Returns the handle to the internal command buffer. This is a lower-level command buffer that more directly
  91. * maps to Vulkan's command buffers.
  92. */
  93. LVulkanCommandBuffer& getBuffer() const { return *mBuffer; }
  94. /**
  95. * Tasks the command buffer to find a new internal command buffer. Call this after the command buffer has been
  96. * submitted to a queue (it's not allowed to be used until the queue is done with it).
  97. */
  98. void acquireNewBuffer();
  99. private:
  100. friend class VulkanCommandBufferManager;
  101. VulkanCommandBuffer(VulkanDevice& device, UINT32 id, CommandBufferType type, UINT32 queueIdx,
  102. bool secondary);
  103. LVulkanCommandBuffer* mBuffer;
  104. VulkanDevice& mDevice;
  105. };
  106. /** @} */
  107. }