BsVulkanCommandBufferManager.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 { namespace ct
  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. * Issues one or multiple pipeline barrier on the provided image, changing the layout of its subresources.
  41. * Automatically determines original layout for individual sub-resources, groups the pipeline barriers and issues
  42. * them.
  43. */
  44. void setLayout(VulkanImage* image, const VkImageSubresourceRange& range, VkAccessFlags newAccessMask,
  45. VkImageLayout newLayout);
  46. /**
  47. * Submits the command buffer on the queue.
  48. *
  49. * @param[in] wait If true, the caller thread will wait until all device operations on the command buffer's
  50. * queue complete.
  51. */
  52. void flush(bool wait);
  53. /** Returns the internal command buffer. */
  54. VulkanCmdBuffer* getCB() const { return mCB; }
  55. private:
  56. friend class VulkanCommandBufferManager;
  57. /** Allocates a new internal command buffer. */
  58. void allocate();
  59. VulkanDevice* mDevice;
  60. GpuQueueType mType;
  61. UINT32 mQueueIdx;
  62. VulkanQueue* mQueue;
  63. UINT32 mQueueMask;
  64. VulkanCmdBuffer* mCB;
  65. UINT32 mSyncMask;
  66. Vector<VkImageMemoryBarrier> mBarriersTemp;
  67. };
  68. /**
  69. * Handles creation of Vulkan command buffers. See CommandBuffer.
  70. *
  71. * @note Core thread only.
  72. */
  73. class VulkanCommandBufferManager : public CommandBufferManager
  74. {
  75. public:
  76. VulkanCommandBufferManager(const VulkanRenderAPI& rapi);
  77. ~VulkanCommandBufferManager();
  78. /** @copydoc CommandBufferManager::createInternal() */
  79. SPtr<CommandBuffer> createInternal(GpuQueueType type, UINT32 deviceIdx = 0, UINT32 queueIdx = 0,
  80. bool secondary = false) override;
  81. /**
  82. * Returns a set of command buffer semaphores depending on the provided sync mask.
  83. *
  84. * @param[in] deviceIdx Index of the device to get the semaphores for.
  85. * @param[in] syncMask Mask that has a bit enabled for each command buffer to retrieve the semaphore for.
  86. * If the command buffer is not currently executing, semaphore won't be returned.
  87. * @param[out] semaphores List containing all the required semaphores. Semaphores are tightly packed at the
  88. * beginning of the array. Must be able to hold at least BS_MAX_UNIQUE_QUEUES entries.
  89. * @param[out] count Number of semaphores provided in the @p semaphores array.
  90. */
  91. void getSyncSemaphores(UINT32 deviceIdx, UINT32 syncMask, VulkanSemaphore** semaphores, UINT32& count);
  92. /**
  93. * Checks if any of the active command buffers finished executing on the device and updates their states
  94. * accordingly.
  95. */
  96. void refreshStates(UINT32 deviceIdx);
  97. /**
  98. * Returns an command buffer that can be used for executing transfer operations on the specified queue.
  99. * Transfer buffers are automatically flushed (submitted) whenever a new (normal) command buffer is about to
  100. * execute.
  101. */
  102. VulkanTransferBuffer* getTransferBuffer(UINT32 deviceIdx, GpuQueueType type, UINT32 queueIdx);
  103. /** Submits all transfer command buffers, ensuring all queued transfer operations get executed. */
  104. void flushTransferBuffers(UINT32 deviceIdx);
  105. private:
  106. /** Contains command buffers specific to one device. */
  107. struct PerDeviceData
  108. {
  109. VulkanTransferBuffer transferBuffers[GQT_COUNT][BS_MAX_QUEUES_PER_TYPE];
  110. };
  111. const VulkanRenderAPI& mRapi;
  112. PerDeviceData* mDeviceData;
  113. UINT32 mNumDevices;
  114. };
  115. /** Provides easy access to the VulkanCommandBufferManager. */
  116. VulkanCommandBufferManager& gVulkanCBManager();
  117. /** @} */
  118. }}