2
0

CommandBufferFactory.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // Copyright (C) 2009-2021, 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. void MicroCommandBuffer::destroy()
  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. }
  30. }
  31. void MicroCommandBuffer::reset()
  32. {
  33. ANKI_TRACE_SCOPED_EVENT(VK_COMMAND_BUFFER_RESET);
  34. ANKI_ASSERT(m_refcount.load() == 0);
  35. ANKI_ASSERT(!m_fence.isCreated() || m_fence->done());
  36. for(GrObjectType type : EnumIterable<GrObjectType>())
  37. {
  38. m_objectRefs[type].destroy(m_fastAlloc);
  39. }
  40. m_fastAlloc.getMemoryPool().reset();
  41. m_fence = {};
  42. }
  43. Error CommandBufferThreadAllocator::init()
  44. {
  45. for(VulkanQueueType qtype : EnumIterable<VulkanQueueType>())
  46. {
  47. if(m_factory->m_queueFamilies[qtype] == MAX_U32)
  48. {
  49. continue;
  50. }
  51. VkCommandPoolCreateInfo ci = {};
  52. ci.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  53. ci.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
  54. ci.queueFamilyIndex = m_factory->m_queueFamilies[qtype];
  55. ANKI_VK_CHECK(vkCreateCommandPool(m_factory->m_dev, &ci, nullptr, &m_pools[qtype]));
  56. }
  57. return Error::NONE;
  58. }
  59. void CommandBufferThreadAllocator::destroyList(IntrusiveList<MicroCommandBuffer>& list)
  60. {
  61. while(!list.isEmpty())
  62. {
  63. MicroCommandBuffer* ptr = list.popFront();
  64. ptr->destroy();
  65. getAllocator().deleteInstance(ptr);
  66. #if ANKI_EXTRA_CHECKS
  67. m_createdCmdbs.fetchSub(1);
  68. #endif
  69. }
  70. }
  71. void CommandBufferThreadAllocator::destroyLists()
  72. {
  73. for(U i = 0; i < 2; ++i)
  74. {
  75. for(U j = 0; j < 2; ++j)
  76. {
  77. for(VulkanQueueType qtype : EnumIterable<VulkanQueueType>())
  78. {
  79. CmdbType& type = m_types[i][j][qtype];
  80. destroyList(type.m_deletedCmdbs);
  81. destroyList(type.m_readyCmdbs);
  82. destroyList(type.m_inUseCmdbs);
  83. }
  84. }
  85. }
  86. }
  87. void CommandBufferThreadAllocator::destroy()
  88. {
  89. for(VkCommandPool& pool : m_pools)
  90. {
  91. if(pool)
  92. {
  93. vkDestroyCommandPool(m_factory->m_dev, pool, nullptr);
  94. pool = VK_NULL_HANDLE;
  95. }
  96. }
  97. ANKI_ASSERT(m_createdCmdbs.load() == 0 && "Someone still holds references to command buffers");
  98. }
  99. Error CommandBufferThreadAllocator::newCommandBuffer(CommandBufferFlag cmdbFlags, MicroCommandBufferPtr& outPtr,
  100. Bool& createdNew)
  101. {
  102. ANKI_ASSERT(!!(cmdbFlags & CommandBufferFlag::COMPUTE_WORK) ^ !!(cmdbFlags & CommandBufferFlag::GENERAL_WORK));
  103. createdNew = false;
  104. const Bool secondLevel = !!(cmdbFlags & CommandBufferFlag::SECOND_LEVEL);
  105. const Bool smallBatch = !!(cmdbFlags & CommandBufferFlag::SMALL_BATCH);
  106. const VulkanQueueType queue = getQueueTypeFromCommandBufferFlags(cmdbFlags, m_factory->m_queueFamilies);
  107. CmdbType& type = m_types[secondLevel][smallBatch][queue];
  108. // Move the deleted to (possibly) in-use or ready
  109. {
  110. LockGuard<Mutex> lock(type.m_deletedMtx);
  111. while(!type.m_deletedCmdbs.isEmpty())
  112. {
  113. MicroCommandBuffer* ptr = type.m_deletedCmdbs.popFront();
  114. if(secondLevel)
  115. {
  116. type.m_readyCmdbs.pushFront(ptr);
  117. ptr->reset();
  118. }
  119. else
  120. {
  121. type.m_inUseCmdbs.pushFront(ptr);
  122. }
  123. }
  124. }
  125. // Reset the in-use command buffers and try to get one available
  126. MicroCommandBuffer* out = nullptr;
  127. if(!secondLevel)
  128. {
  129. // Primary
  130. // Try to reuse a ready buffer
  131. if(!type.m_readyCmdbs.isEmpty())
  132. {
  133. out = type.m_readyCmdbs.popFront();
  134. }
  135. // Do a sweep and move in-use buffers to ready
  136. IntrusiveList<MicroCommandBuffer> inUseCmdbs; // Push to temporary because we are iterating
  137. while(!type.m_inUseCmdbs.isEmpty())
  138. {
  139. MicroCommandBuffer* inUseCmdb = type.m_inUseCmdbs.popFront();
  140. if(!inUseCmdb->m_fence.isCreated() || inUseCmdb->m_fence->done())
  141. {
  142. // It's ready
  143. if(out)
  144. {
  145. type.m_readyCmdbs.pushFront(inUseCmdb);
  146. inUseCmdb->reset();
  147. }
  148. else
  149. {
  150. out = inUseCmdb;
  151. }
  152. }
  153. else
  154. {
  155. inUseCmdbs.pushBack(inUseCmdb);
  156. }
  157. }
  158. ANKI_ASSERT(type.m_inUseCmdbs.isEmpty());
  159. type.m_inUseCmdbs = std::move(inUseCmdbs);
  160. }
  161. else
  162. {
  163. // Secondary
  164. ANKI_ASSERT(type.m_inUseCmdbs.isEmpty());
  165. if(!type.m_readyCmdbs.isEmpty())
  166. {
  167. out = type.m_readyCmdbs.popFront();
  168. }
  169. }
  170. if(ANKI_UNLIKELY(out == nullptr))
  171. {
  172. // Create a new one
  173. VkCommandBufferAllocateInfo ci = {};
  174. ci.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  175. ci.commandPool = m_pools[queue];
  176. ci.level = (secondLevel) ? VK_COMMAND_BUFFER_LEVEL_SECONDARY : VK_COMMAND_BUFFER_LEVEL_PRIMARY;
  177. ci.commandBufferCount = 1;
  178. ANKI_TRACE_INC_COUNTER(VK_COMMAND_BUFFER_CREATE, 1);
  179. VkCommandBuffer cmdb;
  180. ANKI_VK_CHECK(vkAllocateCommandBuffers(m_factory->m_dev, &ci, &cmdb));
  181. MicroCommandBuffer* newCmdb = getAllocator().newInstance<MicroCommandBuffer>(this);
  182. #if ANKI_EXTRA_CHECKS
  183. m_createdCmdbs.fetchAdd(1);
  184. #endif
  185. newCmdb->m_fastAlloc =
  186. StackAllocator<U8>(m_factory->m_alloc.getMemoryPool().getAllocationCallback(),
  187. m_factory->m_alloc.getMemoryPool().getAllocationCallbackUserData(), 256_KB, 2.0f);
  188. newCmdb->m_handle = cmdb;
  189. newCmdb->m_flags = cmdbFlags;
  190. newCmdb->m_queue = queue;
  191. out = newCmdb;
  192. createdNew = true;
  193. }
  194. else
  195. {
  196. out->reset();
  197. }
  198. ANKI_ASSERT(out && out->m_refcount.load() == 0);
  199. ANKI_ASSERT(out->m_flags == cmdbFlags);
  200. outPtr.reset(out);
  201. return Error::NONE;
  202. }
  203. void CommandBufferThreadAllocator::deleteCommandBuffer(MicroCommandBuffer* ptr)
  204. {
  205. ANKI_ASSERT(ptr);
  206. const Bool secondLevel = !!(ptr->m_flags & CommandBufferFlag::SECOND_LEVEL);
  207. const Bool smallBatch = !!(ptr->m_flags & CommandBufferFlag::SMALL_BATCH);
  208. CmdbType& type = m_types[secondLevel][smallBatch][ptr->m_queue];
  209. LockGuard<Mutex> lock(type.m_deletedMtx);
  210. type.m_deletedCmdbs.pushBack(ptr);
  211. }
  212. Error CommandBufferFactory::init(GrAllocator<U8> alloc, VkDevice dev, const VulkanQueueFamilies& queueFamilies)
  213. {
  214. ANKI_ASSERT(dev);
  215. m_alloc = alloc;
  216. m_dev = dev;
  217. m_queueFamilies = queueFamilies;
  218. return Error::NONE;
  219. }
  220. void CommandBufferFactory::destroy()
  221. {
  222. // Run 2 times because destroyLists() populates other allocators' lists
  223. for(U i = 0; i < 2; ++i)
  224. {
  225. for(CommandBufferThreadAllocator* alloc : m_threadAllocs)
  226. {
  227. alloc->destroyLists();
  228. }
  229. }
  230. for(CommandBufferThreadAllocator* talloc : m_threadAllocs)
  231. {
  232. talloc->destroy();
  233. m_alloc.deleteInstance(talloc);
  234. }
  235. m_threadAllocs.destroy(m_alloc);
  236. }
  237. Error CommandBufferFactory::newCommandBuffer(ThreadId tid, CommandBufferFlag cmdbFlags, MicroCommandBufferPtr& ptr)
  238. {
  239. CommandBufferThreadAllocator* alloc = nullptr;
  240. // Get the thread allocator
  241. {
  242. class Comp
  243. {
  244. public:
  245. Bool operator()(const CommandBufferThreadAllocator* a, ThreadId tid) const
  246. {
  247. return a->m_tid < tid;
  248. }
  249. Bool operator()(ThreadId tid, const CommandBufferThreadAllocator* a) const
  250. {
  251. return tid < a->m_tid;
  252. }
  253. };
  254. // Find using binary search
  255. {
  256. RLockGuard<RWMutex> lock(m_threadAllocMtx);
  257. auto it = binarySearch(m_threadAllocs.getBegin(), m_threadAllocs.getEnd(), tid, Comp());
  258. alloc = (it != m_threadAllocs.getEnd()) ? (*it) : nullptr;
  259. }
  260. if(ANKI_UNLIKELY(alloc == nullptr))
  261. {
  262. WLockGuard<RWMutex> lock(m_threadAllocMtx);
  263. // Check again
  264. auto it = binarySearch(m_threadAllocs.getBegin(), m_threadAllocs.getEnd(), tid, Comp());
  265. alloc = (it != m_threadAllocs.getEnd()) ? (*it) : nullptr;
  266. if(alloc == nullptr)
  267. {
  268. alloc = m_alloc.newInstance<CommandBufferThreadAllocator>(this, tid);
  269. m_threadAllocs.resize(m_alloc, m_threadAllocs.getSize() + 1);
  270. m_threadAllocs[m_threadAllocs.getSize() - 1] = alloc;
  271. // Sort for fast find
  272. std::sort(m_threadAllocs.getBegin(), m_threadAllocs.getEnd(),
  273. [](const CommandBufferThreadAllocator* a, const CommandBufferThreadAllocator* b) {
  274. return a->m_tid < b->m_tid;
  275. });
  276. ANKI_CHECK(alloc->init());
  277. }
  278. }
  279. }
  280. ANKI_ASSERT(alloc);
  281. ANKI_ASSERT(alloc->m_tid == tid);
  282. Bool createdNew;
  283. ANKI_CHECK(alloc->newCommandBuffer(cmdbFlags, ptr, createdNew));
  284. if(createdNew)
  285. {
  286. m_createdCmdBufferCount.fetchAdd(1);
  287. }
  288. return Error::NONE;
  289. }
  290. } // end namespace anki