2
0

BsVulkanCommandBufferManager.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "BsCommandBufferManager.h"
  6. #include "BsVulkanCommandBuffer.h"
  7. namespace bs
  8. {
  9. /** @addtogroup Vulkan
  10. * @{
  11. */
  12. /** Wrapper around a command buffer used specifically for transfer operations. */
  13. class VulkanTransferBuffer
  14. {
  15. public:
  16. VulkanTransferBuffer();
  17. VulkanTransferBuffer(VulkanDevice* device, GpuQueueType type, UINT32 queueIdx);
  18. ~VulkanTransferBuffer();
  19. /**
  20. * OR's the provided sync mask with the internal sync mask. The sync mask determines on which queues should
  21. * the buffer wait on before executing. Sync mask is reset after a flush. See CommandSyncMask on how to generate
  22. * a sync mask.
  23. */
  24. void appendMask(UINT32 syncMask) { mSyncMask |= syncMask; }
  25. /** Resets the sync mask. */
  26. void clearMask() { mSyncMask = 0; }
  27. /**
  28. * Issues a pipeline barrier on the provided buffer. See vkCmdPipelineBarrier in Vulkan spec. for usage
  29. * information.
  30. */
  31. void memoryBarrier(VkBuffer buffer, VkAccessFlags srcAccessFlags, VkAccessFlags dstAccessFlags,
  32. VkPipelineStageFlags srcStage, VkPipelineStageFlags dstStage);
  33. /**
  34. * Issues a pipeline barrier on the provided image, changing its layout. See vkCmdPipelineBarrier in Vulkan spec.
  35. * for usage information.
  36. */
  37. void setLayout(VkImage image, VkAccessFlags srcAccessFlags, VkAccessFlags dstAccessFlags,
  38. VkImageLayout oldLayout, VkImageLayout newLayout, const VkImageSubresourceRange& range);
  39. /**
  40. * Submits the command buffer on the queue.
  41. *
  42. * @param[in] wait If true, the caller thread will wait until all device operations on the command buffer's
  43. * queue complete.
  44. */
  45. void flush(bool wait);
  46. /** Returns the internal command buffer. */
  47. VulkanCmdBuffer* getCB() const { return mCB; }
  48. private:
  49. friend class VulkanCommandBufferManager;
  50. /** Allocates a new internal command buffer. */
  51. void allocate();
  52. VulkanDevice* mDevice;
  53. GpuQueueType mType;
  54. UINT32 mQueueIdx;
  55. VulkanQueue* mQueue;
  56. UINT32 mQueueMask;
  57. VulkanCmdBuffer* mCB;
  58. UINT32 mSyncMask;
  59. };
  60. /**
  61. * Handles creation of Vulkan command buffers. See CommandBuffer.
  62. *
  63. * @note Core thread only.
  64. */
  65. class VulkanCommandBufferManager : public CommandBufferManager
  66. {
  67. public:
  68. VulkanCommandBufferManager(const VulkanRenderAPI& rapi);
  69. ~VulkanCommandBufferManager();
  70. /** @copydoc CommandBufferManager::createInternal() */
  71. SPtr<CommandBuffer> createInternal(GpuQueueType type, UINT32 deviceIdx = 0, UINT32 queueIdx = 0,
  72. bool secondary = false) override;
  73. /** Notifies the manager that this buffer was just submitted to the queue for execution. */
  74. void setActiveBuffer(GpuQueueType type, UINT32 deviceIdx, UINT32 queueIdx, VulkanCmdBuffer* buffer);
  75. /**
  76. * Returns a set of command buffer semaphores depending on the provided sync mask.
  77. *
  78. * @param[in] deviceIdx Index of the device to get the semaphores for.
  79. * @param[in] syncMask Mask that has a bit enabled for each command buffer to retrieve the semaphore for.
  80. * If the command buffer is not currently executing, semaphore won't be returned.
  81. * @param[out] semaphores List containing all the required semaphores. Semaphores are tightly packed at the
  82. * beginning of the array. Must be able to hold at least BS_MAX_COMMAND_BUFFERS entries.
  83. * @param[out] count Number of semaphores provided in the @p semaphores array.
  84. */
  85. void getSyncSemaphores(UINT32 deviceIdx, UINT32 syncMask, VkSemaphore* semaphores, UINT32& count);
  86. /**
  87. * Checks if any of the active command buffers finished executing on the device and updates their states
  88. * accordingly.
  89. */
  90. void refreshStates(UINT32 deviceIdx);
  91. /**
  92. * Returns an command buffer that can be used for executing transfer operations on the specified queue.
  93. * Transfer buffers are automatically flushed (submitted) whenever a new (normal) command buffer is about to
  94. * execute.
  95. */
  96. VulkanTransferBuffer* getTransferBuffer(UINT32 deviceIdx, GpuQueueType type, UINT32 queueIdx);
  97. /** Submits all transfer command buffers, ensuring all queued transfer operations get executed. */
  98. void flushTransferBuffers(UINT32 deviceIdx);
  99. private:
  100. /** Contains command buffers specific to one device. */
  101. struct PerDeviceData
  102. {
  103. VulkanCmdBuffer* activeBuffers[BS_MAX_UNIQUE_QUEUES];
  104. VulkanTransferBuffer transferBuffers[GQT_COUNT][BS_MAX_QUEUES_PER_TYPE];
  105. };
  106. const VulkanRenderAPI& mRapi;
  107. PerDeviceData* mDeviceData;
  108. UINT32 mNumDevices;
  109. };
  110. /** Provides easy access to the VulkanCommandBufferManager. */
  111. VulkanCommandBufferManager& gVulkanCBManager();
  112. /** @} */
  113. }