CommandBufferFactory.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Copyright (C) 2009-2022, 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::GENERAL_WORK) ^ !!(flags & CommandBufferFlag::COMPUTE_WORK));
  12. if(!(flags & CommandBufferFlag::GENERAL_WORK) && queueFamilies[VulkanQueueType::COMPUTE] != MAX_U32)
  13. {
  14. return VulkanQueueType::COMPUTE;
  15. }
  16. else
  17. {
  18. ANKI_ASSERT(queueFamilies[VulkanQueueType::GENERAL] != MAX_U32);
  19. return VulkanQueueType::GENERAL;
  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(VK_COMMAND_BUFFER_RESET);
  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_fastAlloc);
  41. }
  42. m_fastAlloc.getMemoryPool().reset();
  43. }
  44. Error CommandBufferThreadAllocator::init()
  45. {
  46. for(VulkanQueueType qtype : EnumIterable<VulkanQueueType>())
  47. {
  48. if(m_factory->m_queueFamilies[qtype] == MAX_U32)
  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_alloc);
  66. }
  67. }
  68. }
  69. return Error::NONE;
  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::COMPUTE_WORK) ^ !!(cmdbFlags & CommandBufferFlag::GENERAL_WORK));
  95. const Bool secondLevel = !!(cmdbFlags & CommandBufferFlag::SECOND_LEVEL);
  96. const Bool smallBatch = !!(cmdbFlags & CommandBufferFlag::SMALL_BATCH);
  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(ANKI_UNLIKELY(out == nullptr))
  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(VK_COMMAND_BUFFER_CREATE, 1);
  109. VkCommandBuffer cmdb;
  110. ANKI_VK_CHECK(vkAllocateCommandBuffers(m_factory->m_dev, &ci, &cmdb));
  111. MicroCommandBuffer* newCmdb = getAllocator().newInstance<MicroCommandBuffer>(this);
  112. newCmdb->m_fastAlloc =
  113. StackAllocator<U8>(m_factory->m_alloc.getMemoryPool().getAllocationCallback(),
  114. m_factory->m_alloc.getMemoryPool().getAllocationCallbackUserData(), 256_KB, 2.0f);
  115. newCmdb->m_handle = cmdb;
  116. newCmdb->m_flags = cmdbFlags;
  117. newCmdb->m_queue = queue;
  118. out = newCmdb;
  119. m_factory->m_createdCmdBufferCount.fetchAdd(1);
  120. }
  121. else
  122. {
  123. for([[maybe_unused]] GrObjectType type : EnumIterable<GrObjectType>())
  124. {
  125. ANKI_ASSERT(out->m_objectRefs[type].getSize() == 0);
  126. }
  127. }
  128. ANKI_ASSERT(out && out->m_refcount.load() == 0);
  129. ANKI_ASSERT(out->m_flags == cmdbFlags);
  130. outPtr.reset(out);
  131. return Error::NONE;
  132. }
  133. void CommandBufferThreadAllocator::deleteCommandBuffer(MicroCommandBuffer* ptr)
  134. {
  135. ANKI_ASSERT(ptr);
  136. const Bool secondLevel = !!(ptr->m_flags & CommandBufferFlag::SECOND_LEVEL);
  137. const Bool smallBatch = !!(ptr->m_flags & CommandBufferFlag::SMALL_BATCH);
  138. m_recyclers[secondLevel][smallBatch][ptr->m_queue].recycle(ptr);
  139. }
  140. Error CommandBufferFactory::init(GrAllocator<U8> alloc, VkDevice dev, const VulkanQueueFamilies& queueFamilies)
  141. {
  142. ANKI_ASSERT(dev);
  143. m_alloc = alloc;
  144. m_dev = dev;
  145. m_queueFamilies = queueFamilies;
  146. return Error::NONE;
  147. }
  148. void CommandBufferFactory::destroy()
  149. {
  150. // First trim the caches for all recyclers. This will release the primaries and populate the recyclers of
  151. // secondaries
  152. for(CommandBufferThreadAllocator* talloc : m_threadAllocs)
  153. {
  154. for(U32 secondLevel = 0; secondLevel < 2; ++secondLevel)
  155. {
  156. for(U32 smallBatch = 0; smallBatch < 2; ++smallBatch)
  157. {
  158. for(VulkanQueueType queue : EnumIterable<VulkanQueueType>())
  159. {
  160. talloc->m_recyclers[secondLevel][smallBatch][queue].trimCache();
  161. }
  162. }
  163. }
  164. }
  165. for(CommandBufferThreadAllocator* talloc : m_threadAllocs)
  166. {
  167. talloc->destroy();
  168. m_alloc.deleteInstance(talloc);
  169. }
  170. m_threadAllocs.destroy(m_alloc);
  171. }
  172. Error CommandBufferFactory::newCommandBuffer(ThreadId tid, CommandBufferFlag cmdbFlags, MicroCommandBufferPtr& ptr)
  173. {
  174. CommandBufferThreadAllocator* alloc = nullptr;
  175. // Get the thread allocator
  176. {
  177. class Comp
  178. {
  179. public:
  180. Bool operator()(const CommandBufferThreadAllocator* a, ThreadId tid) const
  181. {
  182. return a->m_tid < tid;
  183. }
  184. Bool operator()(ThreadId tid, const CommandBufferThreadAllocator* a) const
  185. {
  186. return tid < a->m_tid;
  187. }
  188. };
  189. // Find using binary search
  190. {
  191. RLockGuard<RWMutex> lock(m_threadAllocMtx);
  192. auto it = binarySearch(m_threadAllocs.getBegin(), m_threadAllocs.getEnd(), tid, Comp());
  193. alloc = (it != m_threadAllocs.getEnd()) ? (*it) : nullptr;
  194. }
  195. if(ANKI_UNLIKELY(alloc == nullptr))
  196. {
  197. WLockGuard<RWMutex> lock(m_threadAllocMtx);
  198. // Check again
  199. auto it = binarySearch(m_threadAllocs.getBegin(), m_threadAllocs.getEnd(), tid, Comp());
  200. alloc = (it != m_threadAllocs.getEnd()) ? (*it) : nullptr;
  201. if(alloc == nullptr)
  202. {
  203. alloc = m_alloc.newInstance<CommandBufferThreadAllocator>(this, tid);
  204. m_threadAllocs.resize(m_alloc, m_threadAllocs.getSize() + 1);
  205. m_threadAllocs[m_threadAllocs.getSize() - 1] = alloc;
  206. // Sort for fast find
  207. std::sort(m_threadAllocs.getBegin(), m_threadAllocs.getEnd(),
  208. [](const CommandBufferThreadAllocator* a, const CommandBufferThreadAllocator* b) {
  209. return a->m_tid < b->m_tid;
  210. });
  211. ANKI_CHECK(alloc->init());
  212. }
  213. }
  214. }
  215. ANKI_ASSERT(alloc);
  216. ANKI_ASSERT(alloc->m_tid == tid);
  217. ANKI_CHECK(alloc->newCommandBuffer(cmdbFlags, ptr));
  218. return Error::NONE;
  219. }
  220. } // end namespace anki