CommandBufferFactory.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. const U32 count = m_threadAlloc->m_factory->m_createdCmdBufferCount.fetchSub(1);
  30. ANKI_ASSERT(count > 0);
  31. (void)count;
  32. }
  33. }
  34. void MicroCommandBuffer::reset()
  35. {
  36. ANKI_TRACE_SCOPED_EVENT(VK_COMMAND_BUFFER_RESET);
  37. ANKI_ASSERT(m_refcount.load() == 0);
  38. ANKI_ASSERT(!m_fence.isCreated());
  39. for(GrObjectType type : EnumIterable<GrObjectType>())
  40. {
  41. m_objectRefs[type].destroy(m_fastAlloc);
  42. }
  43. m_fastAlloc.getMemoryPool().reset();
  44. }
  45. Error CommandBufferThreadAllocator::init()
  46. {
  47. for(VulkanQueueType qtype : EnumIterable<VulkanQueueType>())
  48. {
  49. if(m_factory->m_queueFamilies[qtype] == MAX_U32)
  50. {
  51. continue;
  52. }
  53. VkCommandPoolCreateInfo ci = {};
  54. ci.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  55. ci.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
  56. ci.queueFamilyIndex = m_factory->m_queueFamilies[qtype];
  57. ANKI_VK_CHECK(vkCreateCommandPool(m_factory->m_dev, &ci, nullptr, &m_pools[qtype]));
  58. }
  59. for(U32 secondLevel = 0; secondLevel < 2; ++secondLevel)
  60. {
  61. for(U32 smallBatch = 0; smallBatch < 2; ++smallBatch)
  62. {
  63. for(VulkanQueueType queue : EnumIterable<VulkanQueueType>())
  64. {
  65. MicroObjectRecycler<MicroCommandBuffer>& recycler = m_recyclers[secondLevel][smallBatch][queue];
  66. recycler.init(m_factory->m_alloc);
  67. }
  68. }
  69. }
  70. return Error::NONE;
  71. }
  72. void CommandBufferThreadAllocator::destroy()
  73. {
  74. for(U32 secondLevel = 0; secondLevel < 2; ++secondLevel)
  75. {
  76. for(U32 smallBatch = 0; smallBatch < 2; ++smallBatch)
  77. {
  78. for(VulkanQueueType queue : EnumIterable<VulkanQueueType>())
  79. {
  80. m_recyclers[secondLevel][smallBatch][queue].destroy();
  81. }
  82. }
  83. }
  84. for(VkCommandPool& pool : m_pools)
  85. {
  86. if(pool)
  87. {
  88. vkDestroyCommandPool(m_factory->m_dev, pool, nullptr);
  89. pool = VK_NULL_HANDLE;
  90. }
  91. }
  92. }
  93. Error CommandBufferThreadAllocator::newCommandBuffer(CommandBufferFlag cmdbFlags, MicroCommandBufferPtr& outPtr)
  94. {
  95. ANKI_ASSERT(!!(cmdbFlags & CommandBufferFlag::COMPUTE_WORK) ^ !!(cmdbFlags & CommandBufferFlag::GENERAL_WORK));
  96. const Bool secondLevel = !!(cmdbFlags & CommandBufferFlag::SECOND_LEVEL);
  97. const Bool smallBatch = !!(cmdbFlags & CommandBufferFlag::SMALL_BATCH);
  98. const VulkanQueueType queue = getQueueTypeFromCommandBufferFlags(cmdbFlags, m_factory->m_queueFamilies);
  99. MicroObjectRecycler<MicroCommandBuffer>& recycler = m_recyclers[secondLevel][smallBatch][queue];
  100. MicroCommandBuffer* out = recycler.findToReuse();
  101. if(ANKI_UNLIKELY(out == nullptr))
  102. {
  103. // Create a new one
  104. VkCommandBufferAllocateInfo ci = {};
  105. ci.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  106. ci.commandPool = m_pools[queue];
  107. ci.level = (secondLevel) ? VK_COMMAND_BUFFER_LEVEL_SECONDARY : VK_COMMAND_BUFFER_LEVEL_PRIMARY;
  108. ci.commandBufferCount = 1;
  109. ANKI_TRACE_INC_COUNTER(VK_COMMAND_BUFFER_CREATE, 1);
  110. VkCommandBuffer cmdb;
  111. ANKI_VK_CHECK(vkAllocateCommandBuffers(m_factory->m_dev, &ci, &cmdb));
  112. MicroCommandBuffer* newCmdb = getAllocator().newInstance<MicroCommandBuffer>(this);
  113. newCmdb->m_fastAlloc =
  114. StackAllocator<U8>(m_factory->m_alloc.getMemoryPool().getAllocationCallback(),
  115. m_factory->m_alloc.getMemoryPool().getAllocationCallbackUserData(), 256_KB, 2.0f);
  116. newCmdb->m_handle = cmdb;
  117. newCmdb->m_flags = cmdbFlags;
  118. newCmdb->m_queue = queue;
  119. out = newCmdb;
  120. m_factory->m_createdCmdBufferCount.fetchAdd(1);
  121. }
  122. else
  123. {
  124. for(GrObjectType type : EnumIterable<GrObjectType>())
  125. {
  126. (void)type;
  127. ANKI_ASSERT(out->m_objectRefs[type].getSize() == 0);
  128. }
  129. }
  130. ANKI_ASSERT(out && out->m_refcount.load() == 0);
  131. ANKI_ASSERT(out->m_flags == cmdbFlags);
  132. outPtr.reset(out);
  133. return Error::NONE;
  134. }
  135. void CommandBufferThreadAllocator::deleteCommandBuffer(MicroCommandBuffer* ptr)
  136. {
  137. ANKI_ASSERT(ptr);
  138. const Bool secondLevel = !!(ptr->m_flags & CommandBufferFlag::SECOND_LEVEL);
  139. const Bool smallBatch = !!(ptr->m_flags & CommandBufferFlag::SMALL_BATCH);
  140. m_recyclers[secondLevel][smallBatch][ptr->m_queue].recycle(ptr);
  141. }
  142. Error CommandBufferFactory::init(GrAllocator<U8> alloc, VkDevice dev, const VulkanQueueFamilies& queueFamilies)
  143. {
  144. ANKI_ASSERT(dev);
  145. m_alloc = alloc;
  146. m_dev = dev;
  147. m_queueFamilies = queueFamilies;
  148. return Error::NONE;
  149. }
  150. void CommandBufferFactory::destroy()
  151. {
  152. // First trim the caches for all recyclers. This will release the primaries and populate the recyclers of
  153. // secondaries
  154. for(CommandBufferThreadAllocator* talloc : m_threadAllocs)
  155. {
  156. for(U32 secondLevel = 0; secondLevel < 2; ++secondLevel)
  157. {
  158. for(U32 smallBatch = 0; smallBatch < 2; ++smallBatch)
  159. {
  160. for(VulkanQueueType queue : EnumIterable<VulkanQueueType>())
  161. {
  162. talloc->m_recyclers[secondLevel][smallBatch][queue].trimCache();
  163. }
  164. }
  165. }
  166. }
  167. for(CommandBufferThreadAllocator* talloc : m_threadAllocs)
  168. {
  169. talloc->destroy();
  170. m_alloc.deleteInstance(talloc);
  171. }
  172. m_threadAllocs.destroy(m_alloc);
  173. }
  174. Error CommandBufferFactory::newCommandBuffer(ThreadId tid, CommandBufferFlag cmdbFlags, MicroCommandBufferPtr& ptr)
  175. {
  176. CommandBufferThreadAllocator* alloc = nullptr;
  177. // Get the thread allocator
  178. {
  179. class Comp
  180. {
  181. public:
  182. Bool operator()(const CommandBufferThreadAllocator* a, ThreadId tid) const
  183. {
  184. return a->m_tid < tid;
  185. }
  186. Bool operator()(ThreadId tid, const CommandBufferThreadAllocator* a) const
  187. {
  188. return tid < a->m_tid;
  189. }
  190. };
  191. // Find using binary search
  192. {
  193. RLockGuard<RWMutex> lock(m_threadAllocMtx);
  194. auto it = binarySearch(m_threadAllocs.getBegin(), m_threadAllocs.getEnd(), tid, Comp());
  195. alloc = (it != m_threadAllocs.getEnd()) ? (*it) : nullptr;
  196. }
  197. if(ANKI_UNLIKELY(alloc == nullptr))
  198. {
  199. WLockGuard<RWMutex> lock(m_threadAllocMtx);
  200. // Check again
  201. auto it = binarySearch(m_threadAllocs.getBegin(), m_threadAllocs.getEnd(), tid, Comp());
  202. alloc = (it != m_threadAllocs.getEnd()) ? (*it) : nullptr;
  203. if(alloc == nullptr)
  204. {
  205. alloc = m_alloc.newInstance<CommandBufferThreadAllocator>(this, tid);
  206. m_threadAllocs.resize(m_alloc, m_threadAllocs.getSize() + 1);
  207. m_threadAllocs[m_threadAllocs.getSize() - 1] = alloc;
  208. // Sort for fast find
  209. std::sort(m_threadAllocs.getBegin(), m_threadAllocs.getEnd(),
  210. [](const CommandBufferThreadAllocator* a, const CommandBufferThreadAllocator* b) {
  211. return a->m_tid < b->m_tid;
  212. });
  213. ANKI_CHECK(alloc->init());
  214. }
  215. }
  216. }
  217. ANKI_ASSERT(alloc);
  218. ANKI_ASSERT(alloc->m_tid == tid);
  219. ANKI_CHECK(alloc->newCommandBuffer(cmdbFlags, ptr));
  220. return Error::NONE;
  221. }
  222. } // end namespace anki