CommandBufferFactory.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Gr/Vulkan/CommandBufferFactory.h>
  6. #include <AnKi/Util/Tracer.h>
  7. namespace anki {
  8. static VulkanQueueType getQueueTypeFromCommandBufferFlags(CommandBufferFlag flags,
  9. const VulkanQueueFamilies& queueFamilies)
  10. {
  11. ANKI_ASSERT(!!(flags & CommandBufferFlag::kGeneralWork) ^ !!(flags & CommandBufferFlag::kComputeWork));
  12. if(!(flags & CommandBufferFlag::kGeneralWork) && queueFamilies[VulkanQueueType::kCompute] != kMaxU32)
  13. {
  14. return VulkanQueueType::kCompute;
  15. }
  16. else
  17. {
  18. ANKI_ASSERT(queueFamilies[VulkanQueueType::kGeneral] != kMaxU32);
  19. return VulkanQueueType::kGeneral;
  20. }
  21. }
  22. MicroCommandBuffer::~MicroCommandBuffer()
  23. {
  24. reset();
  25. if(m_handle)
  26. {
  27. vkFreeCommandBuffers(m_threadAlloc->m_factory->m_dev, m_threadAlloc->m_pools[m_queue], 1, &m_handle);
  28. m_handle = {};
  29. [[maybe_unused]] const U32 count = m_threadAlloc->m_factory->m_createdCmdBufferCount.fetchSub(1);
  30. ANKI_ASSERT(count > 0);
  31. }
  32. }
  33. void MicroCommandBuffer::reset()
  34. {
  35. ANKI_TRACE_SCOPED_EVENT(VkCommandBufferReset);
  36. ANKI_ASSERT(m_refcount.load() == 0);
  37. ANKI_ASSERT(!m_fence.isCreated());
  38. for(GrObjectType type : EnumIterable<GrObjectType>())
  39. {
  40. m_objectRefs[type].destroy(m_fastPool);
  41. }
  42. m_fastPool.reset();
  43. }
  44. Error CommandBufferThreadAllocator::init()
  45. {
  46. for(VulkanQueueType qtype : EnumIterable<VulkanQueueType>())
  47. {
  48. if(m_factory->m_queueFamilies[qtype] == kMaxU32)
  49. {
  50. continue;
  51. }
  52. VkCommandPoolCreateInfo ci = {};
  53. ci.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  54. ci.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
  55. ci.queueFamilyIndex = m_factory->m_queueFamilies[qtype];
  56. ANKI_VK_CHECK(vkCreateCommandPool(m_factory->m_dev, &ci, nullptr, &m_pools[qtype]));
  57. }
  58. for(U32 secondLevel = 0; secondLevel < 2; ++secondLevel)
  59. {
  60. for(U32 smallBatch = 0; smallBatch < 2; ++smallBatch)
  61. {
  62. for(VulkanQueueType queue : EnumIterable<VulkanQueueType>())
  63. {
  64. MicroObjectRecycler<MicroCommandBuffer>& recycler = m_recyclers[secondLevel][smallBatch][queue];
  65. recycler.init(m_factory->m_pool);
  66. }
  67. }
  68. }
  69. return Error::kNone;
  70. }
  71. void CommandBufferThreadAllocator::destroy()
  72. {
  73. for(U32 secondLevel = 0; secondLevel < 2; ++secondLevel)
  74. {
  75. for(U32 smallBatch = 0; smallBatch < 2; ++smallBatch)
  76. {
  77. for(VulkanQueueType queue : EnumIterable<VulkanQueueType>())
  78. {
  79. m_recyclers[secondLevel][smallBatch][queue].destroy();
  80. }
  81. }
  82. }
  83. for(VkCommandPool& pool : m_pools)
  84. {
  85. if(pool)
  86. {
  87. vkDestroyCommandPool(m_factory->m_dev, pool, nullptr);
  88. pool = VK_NULL_HANDLE;
  89. }
  90. }
  91. }
  92. Error CommandBufferThreadAllocator::newCommandBuffer(CommandBufferFlag cmdbFlags, MicroCommandBufferPtr& outPtr)
  93. {
  94. ANKI_ASSERT(!!(cmdbFlags & CommandBufferFlag::kComputeWork) ^ !!(cmdbFlags & CommandBufferFlag::kGeneralWork));
  95. const Bool secondLevel = !!(cmdbFlags & CommandBufferFlag::kSecondLevel);
  96. const Bool smallBatch = !!(cmdbFlags & CommandBufferFlag::kSmallBatch);
  97. const VulkanQueueType queue = getQueueTypeFromCommandBufferFlags(cmdbFlags, m_factory->m_queueFamilies);
  98. MicroObjectRecycler<MicroCommandBuffer>& recycler = m_recyclers[secondLevel][smallBatch][queue];
  99. MicroCommandBuffer* out = recycler.findToReuse();
  100. if(out == nullptr) [[unlikely]]
  101. {
  102. // Create a new one
  103. VkCommandBufferAllocateInfo ci = {};
  104. ci.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  105. ci.commandPool = m_pools[queue];
  106. ci.level = (secondLevel) ? VK_COMMAND_BUFFER_LEVEL_SECONDARY : VK_COMMAND_BUFFER_LEVEL_PRIMARY;
  107. ci.commandBufferCount = 1;
  108. ANKI_TRACE_INC_COUNTER(VkCommandBufferCreate, 1);
  109. VkCommandBuffer cmdb;
  110. ANKI_VK_CHECK(vkAllocateCommandBuffers(m_factory->m_dev, &ci, &cmdb));
  111. MicroCommandBuffer* newCmdb = newInstance<MicroCommandBuffer>(getMemoryPool(), this);
  112. newCmdb->m_fastPool.init(m_factory->m_pool->getAllocationCallback(),
  113. m_factory->m_pool->getAllocationCallbackUserData(), 256_KB, 2.0f);
  114. newCmdb->m_handle = cmdb;
  115. newCmdb->m_flags = cmdbFlags;
  116. newCmdb->m_queue = queue;
  117. out = newCmdb;
  118. m_factory->m_createdCmdBufferCount.fetchAdd(1);
  119. }
  120. else
  121. {
  122. for([[maybe_unused]] GrObjectType type : EnumIterable<GrObjectType>())
  123. {
  124. ANKI_ASSERT(out->m_objectRefs[type].getSize() == 0);
  125. }
  126. }
  127. ANKI_ASSERT(out && out->m_refcount.load() == 0);
  128. ANKI_ASSERT(out->m_flags == cmdbFlags);
  129. outPtr.reset(out);
  130. return Error::kNone;
  131. }
  132. void CommandBufferThreadAllocator::deleteCommandBuffer(MicroCommandBuffer* ptr)
  133. {
  134. ANKI_ASSERT(ptr);
  135. const Bool secondLevel = !!(ptr->m_flags & CommandBufferFlag::kSecondLevel);
  136. const Bool smallBatch = !!(ptr->m_flags & CommandBufferFlag::kSmallBatch);
  137. m_recyclers[secondLevel][smallBatch][ptr->m_queue].recycle(ptr);
  138. }
  139. Error CommandBufferFactory::init(HeapMemoryPool* pool, VkDevice dev, const VulkanQueueFamilies& queueFamilies)
  140. {
  141. ANKI_ASSERT(pool && dev);
  142. m_pool = pool;
  143. m_dev = dev;
  144. m_queueFamilies = queueFamilies;
  145. return Error::kNone;
  146. }
  147. void CommandBufferFactory::destroy()
  148. {
  149. // First trim the caches for all recyclers. This will release the primaries and populate the recyclers of
  150. // secondaries
  151. for(CommandBufferThreadAllocator* talloc : m_threadAllocs)
  152. {
  153. for(U32 secondLevel = 0; secondLevel < 2; ++secondLevel)
  154. {
  155. for(U32 smallBatch = 0; smallBatch < 2; ++smallBatch)
  156. {
  157. for(VulkanQueueType queue : EnumIterable<VulkanQueueType>())
  158. {
  159. talloc->m_recyclers[secondLevel][smallBatch][queue].trimCache();
  160. }
  161. }
  162. }
  163. }
  164. for(CommandBufferThreadAllocator* talloc : m_threadAllocs)
  165. {
  166. talloc->destroy();
  167. deleteInstance(*m_pool, talloc);
  168. }
  169. m_threadAllocs.destroy(*m_pool);
  170. }
  171. Error CommandBufferFactory::newCommandBuffer(ThreadId tid, CommandBufferFlag cmdbFlags, MicroCommandBufferPtr& ptr)
  172. {
  173. CommandBufferThreadAllocator* alloc = nullptr;
  174. // Get the thread allocator
  175. {
  176. class Comp
  177. {
  178. public:
  179. Bool operator()(const CommandBufferThreadAllocator* a, ThreadId tid) const
  180. {
  181. return a->m_tid < tid;
  182. }
  183. Bool operator()(ThreadId tid, const CommandBufferThreadAllocator* a) const
  184. {
  185. return tid < a->m_tid;
  186. }
  187. };
  188. // Find using binary search
  189. {
  190. RLockGuard<RWMutex> lock(m_threadAllocMtx);
  191. auto it = binarySearch(m_threadAllocs.getBegin(), m_threadAllocs.getEnd(), tid, Comp());
  192. alloc = (it != m_threadAllocs.getEnd()) ? (*it) : nullptr;
  193. }
  194. if(alloc == nullptr) [[unlikely]]
  195. {
  196. WLockGuard<RWMutex> lock(m_threadAllocMtx);
  197. // Check again
  198. auto it = binarySearch(m_threadAllocs.getBegin(), m_threadAllocs.getEnd(), tid, Comp());
  199. alloc = (it != m_threadAllocs.getEnd()) ? (*it) : nullptr;
  200. if(alloc == nullptr)
  201. {
  202. alloc = newInstance<CommandBufferThreadAllocator>(*m_pool, this, tid);
  203. m_threadAllocs.resize(*m_pool, m_threadAllocs.getSize() + 1);
  204. m_threadAllocs[m_threadAllocs.getSize() - 1] = alloc;
  205. // Sort for fast find
  206. std::sort(m_threadAllocs.getBegin(), m_threadAllocs.getEnd(),
  207. [](const CommandBufferThreadAllocator* a, const CommandBufferThreadAllocator* b) {
  208. return a->m_tid < b->m_tid;
  209. });
  210. ANKI_CHECK(alloc->init());
  211. }
  212. }
  213. }
  214. ANKI_ASSERT(alloc);
  215. ANKI_ASSERT(alloc->m_tid == tid);
  216. ANKI_CHECK(alloc->newCommandBuffer(cmdbFlags, ptr));
  217. return Error::kNone;
  218. }
  219. } // end namespace anki