DescriptorSet.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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/DescriptorSet.h>
  6. #include <AnKi/Gr/Buffer.h>
  7. #include <AnKi/Gr/Vulkan/BufferImpl.h>
  8. #include <AnKi/Util/List.h>
  9. #include <AnKi/Util/HashMap.h>
  10. #include <AnKi/Util/Tracer.h>
  11. #include <algorithm>
  12. namespace anki {
  13. thread_local DescriptorSetFactory::ThreadLocal* DescriptorSetFactory::m_threadLocal = nullptr;
  14. /// Wraps a global descriptor set that is used to store bindless textures.
  15. class DescriptorSetFactory::BindlessDescriptorSet
  16. {
  17. public:
  18. ~BindlessDescriptorSet();
  19. Error init(const GrAllocator<U8>& alloc, VkDevice dev, const U32 bindlessTextureCount, U32 bindlessImageCount);
  20. /// Bind a sampled image.
  21. /// @note It's thread-safe.
  22. U32 bindTexture(const VkImageView view, const VkImageLayout layout);
  23. /// Bind a storage image.
  24. /// @note It's thread-safe.
  25. U32 bindImage(const VkImageView view);
  26. /// @note It's thread-safe.
  27. void unbindTexture(U32 idx)
  28. {
  29. unbindCommon(idx, m_freeTexIndices, m_freeTexIndexCount);
  30. }
  31. /// @note It's thread-safe.
  32. void unbindImage(U32 idx)
  33. {
  34. unbindCommon(idx, m_freeImgIndices, m_freeImgIndexCount);
  35. }
  36. DescriptorSet getDescriptorSet() const
  37. {
  38. ANKI_ASSERT(m_dset);
  39. DescriptorSet out;
  40. out.m_handle = m_dset;
  41. return out;
  42. }
  43. VkDescriptorSetLayout getDescriptorSetLayout() const
  44. {
  45. ANKI_ASSERT(m_layout);
  46. return m_layout;
  47. }
  48. private:
  49. GrAllocator<U8> m_alloc;
  50. VkDevice m_dev = VK_NULL_HANDLE;
  51. VkDescriptorSetLayout m_layout = VK_NULL_HANDLE;
  52. VkDescriptorPool m_pool = VK_NULL_HANDLE;
  53. VkDescriptorSet m_dset = VK_NULL_HANDLE;
  54. Mutex m_mtx;
  55. DynamicArray<U16> m_freeTexIndices;
  56. DynamicArray<U16> m_freeImgIndices;
  57. U16 m_freeTexIndexCount ANKI_DEBUG_CODE(= MAX_U16);
  58. U16 m_freeImgIndexCount ANKI_DEBUG_CODE(= MAX_U16);
  59. void unbindCommon(U32 idx, DynamicArray<U16>& freeIndices, U16& freeIndexCount);
  60. };
  61. /// Descriptor set internal class.
  62. class DS : public IntrusiveListEnabled<DS>
  63. {
  64. public:
  65. VkDescriptorSet m_handle = {};
  66. U64 m_lastFrameUsed = MAX_U64;
  67. U64 m_hash;
  68. };
  69. /// Per thread allocator.
  70. class DescriptorSetFactory::DSAllocator
  71. {
  72. public:
  73. DSAllocator(const DSAllocator&) = delete; // Non-copyable
  74. DSAllocator& operator=(const DSAllocator&) = delete; // Non-copyable
  75. DSAllocator(const DSLayoutCacheEntry* layout)
  76. : m_layoutEntry(layout)
  77. {
  78. ANKI_ASSERT(m_layoutEntry);
  79. }
  80. ~DSAllocator();
  81. ANKI_USE_RESULT Error init();
  82. ANKI_USE_RESULT Error createNewPool();
  83. ANKI_USE_RESULT Error getOrCreateSet(U64 hash,
  84. const Array<AnyBindingExtended, MAX_BINDINGS_PER_DESCRIPTOR_SET>& bindings,
  85. StackAllocator<U8>& tmpAlloc, const DS*& out)
  86. {
  87. out = tryFindSet(hash);
  88. if(out == nullptr)
  89. {
  90. ANKI_CHECK(newSet(hash, bindings, tmpAlloc, out));
  91. }
  92. return Error::NONE;
  93. }
  94. private:
  95. const DSLayoutCacheEntry* m_layoutEntry; ///< Know your father.
  96. DynamicArray<VkDescriptorPool> m_pools;
  97. U32 m_lastPoolDSCount = 0;
  98. U32 m_lastPoolFreeDSCount = 0;
  99. IntrusiveList<DS> m_list; ///< At the left of the list are the least used sets.
  100. HashMap<U64, DS*> m_hashmap;
  101. ANKI_USE_RESULT const DS* tryFindSet(U64 hash);
  102. ANKI_USE_RESULT Error newSet(U64 hash, const Array<AnyBindingExtended, MAX_BINDINGS_PER_DESCRIPTOR_SET>& bindings,
  103. StackAllocator<U8>& tmpAlloc, const DS*& out);
  104. void writeSet(const Array<AnyBindingExtended, MAX_BINDINGS_PER_DESCRIPTOR_SET>& bindings, const DS& set,
  105. StackAllocator<U8>& tmpAlloc);
  106. };
  107. class alignas(ANKI_CACHE_LINE_SIZE) DescriptorSetFactory::ThreadLocal
  108. {
  109. public:
  110. DynamicArray<DSAllocator*> m_allocators;
  111. };
  112. /// Cache entry. It's built around a specific descriptor set layout.
  113. class DSLayoutCacheEntry
  114. {
  115. public:
  116. DescriptorSetFactory* m_factory;
  117. U64 m_hash = 0; ///< Layout hash.
  118. VkDescriptorSetLayout m_layoutHandle = {};
  119. BitSet<MAX_BINDINGS_PER_DESCRIPTOR_SET, U32> m_activeBindings = {false};
  120. Array<U32, MAX_BINDINGS_PER_DESCRIPTOR_SET> m_bindingArraySize = {};
  121. Array<DescriptorType, MAX_BINDINGS_PER_DESCRIPTOR_SET> m_bindingType = {};
  122. U32 m_minBinding = MAX_U32;
  123. U32 m_maxBinding = 0;
  124. U32 m_index = 0; ///< Index in DescriptorSetFactory::m_caches
  125. // Cache the create info
  126. Array<VkDescriptorPoolSize, U(DescriptorType::COUNT)> m_poolSizesCreateInf = {};
  127. VkDescriptorPoolCreateInfo m_poolCreateInf = {};
  128. DSLayoutCacheEntry(DescriptorSetFactory* factory, U32 index)
  129. : m_factory(factory)
  130. , m_index(index)
  131. {
  132. }
  133. ~DSLayoutCacheEntry();
  134. ANKI_USE_RESULT Error init(const DescriptorBinding* bindings, U32 bindingCount, U64 hash);
  135. /// @note Thread-safe.
  136. ANKI_USE_RESULT Error getOrCreateDSAllocator(DescriptorSetFactory::DSAllocator*& alloc);
  137. };
  138. DescriptorSetFactory::BindlessDescriptorSet::~BindlessDescriptorSet()
  139. {
  140. ANKI_ASSERT(m_freeTexIndexCount == m_freeTexIndices.getSize() && "Forgot to unbind some textures");
  141. ANKI_ASSERT(m_freeImgIndexCount == m_freeImgIndices.getSize() && "Forgot to unbind some images");
  142. if(m_pool)
  143. {
  144. vkDestroyDescriptorPool(m_dev, m_pool, nullptr);
  145. m_pool = VK_NULL_HANDLE;
  146. m_dset = VK_NULL_HANDLE;
  147. }
  148. if(m_layout)
  149. {
  150. vkDestroyDescriptorSetLayout(m_dev, m_layout, nullptr);
  151. m_layout = VK_NULL_HANDLE;
  152. }
  153. m_freeImgIndices.destroy(m_alloc);
  154. m_freeTexIndices.destroy(m_alloc);
  155. }
  156. Error DescriptorSetFactory::BindlessDescriptorSet::init(const GrAllocator<U8>& alloc, VkDevice dev,
  157. U32 bindlessTextureCount, U32 bindlessImageCount)
  158. {
  159. ANKI_ASSERT(dev);
  160. m_alloc = alloc;
  161. m_dev = dev;
  162. // Create the layout
  163. {
  164. Array<VkDescriptorSetLayoutBinding, 2> bindings = {};
  165. bindings[0].binding = 0;
  166. bindings[0].stageFlags = VK_SHADER_STAGE_ALL;
  167. bindings[0].descriptorCount = bindlessTextureCount;
  168. bindings[0].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
  169. bindings[1].binding = 1;
  170. bindings[1].stageFlags = VK_SHADER_STAGE_ALL;
  171. bindings[1].descriptorCount = bindlessImageCount;
  172. bindings[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
  173. Array<VkDescriptorBindingFlagsEXT, 2> bindingFlags = {};
  174. bindingFlags[0] = VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT
  175. | VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT
  176. | VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT;
  177. bindingFlags[1] = bindingFlags[0];
  178. VkDescriptorSetLayoutBindingFlagsCreateInfoEXT extraInfos = {};
  179. extraInfos.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO_EXT;
  180. extraInfos.bindingCount = bindingFlags.getSize();
  181. extraInfos.pBindingFlags = &bindingFlags[0];
  182. VkDescriptorSetLayoutCreateInfo ci = {};
  183. ci.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
  184. ci.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT;
  185. ci.bindingCount = bindings.getSize();
  186. ci.pBindings = &bindings[0];
  187. ci.pNext = &extraInfos;
  188. ANKI_VK_CHECK(vkCreateDescriptorSetLayout(m_dev, &ci, nullptr, &m_layout));
  189. }
  190. // Create the pool
  191. {
  192. Array<VkDescriptorPoolSize, 2> sizes = {};
  193. sizes[0].type = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
  194. sizes[0].descriptorCount = bindlessTextureCount;
  195. sizes[1].type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
  196. sizes[1].descriptorCount = bindlessImageCount;
  197. VkDescriptorPoolCreateInfo ci = {};
  198. ci.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
  199. ci.maxSets = 1;
  200. ci.poolSizeCount = sizes.getSize();
  201. ci.pPoolSizes = &sizes[0];
  202. ci.flags = VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT_EXT;
  203. ANKI_VK_CHECK(vkCreateDescriptorPool(m_dev, &ci, nullptr, &m_pool));
  204. }
  205. // Create the descriptor set
  206. {
  207. VkDescriptorSetAllocateInfo ci = {};
  208. ci.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  209. ci.descriptorPool = m_pool;
  210. ci.descriptorSetCount = 1;
  211. ci.pSetLayouts = &m_layout;
  212. ANKI_VK_CHECK(vkAllocateDescriptorSets(m_dev, &ci, &m_dset));
  213. }
  214. // Init the free arrays
  215. {
  216. m_freeTexIndices.create(m_alloc, bindlessTextureCount);
  217. m_freeTexIndexCount = U16(m_freeTexIndices.getSize());
  218. for(U32 i = 0; i < m_freeTexIndices.getSize(); ++i)
  219. {
  220. m_freeTexIndices[i] = U16(m_freeTexIndices.getSize() - i - 1);
  221. }
  222. m_freeImgIndices.create(m_alloc, bindlessImageCount);
  223. m_freeImgIndexCount = U16(m_freeImgIndices.getSize());
  224. for(U32 i = 0; i < m_freeImgIndices.getSize(); ++i)
  225. {
  226. m_freeImgIndices[i] = U16(m_freeImgIndices.getSize() - i - 1);
  227. }
  228. }
  229. return Error::NONE;
  230. }
  231. U32 DescriptorSetFactory::BindlessDescriptorSet::bindTexture(const VkImageView view, const VkImageLayout layout)
  232. {
  233. ANKI_ASSERT(layout == VK_IMAGE_LAYOUT_GENERAL || layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
  234. ANKI_ASSERT(view);
  235. LockGuard<Mutex> lock(m_mtx);
  236. ANKI_ASSERT(m_freeTexIndexCount > 0 && "Out of indices");
  237. // Pop the index
  238. --m_freeTexIndexCount;
  239. const U16 idx = m_freeTexIndices[m_freeTexIndexCount];
  240. ANKI_ASSERT(idx < m_freeTexIndices.getSize());
  241. // Update the set
  242. VkDescriptorImageInfo imageInf = {};
  243. imageInf.imageView = view;
  244. imageInf.imageLayout = layout;
  245. VkWriteDescriptorSet write = {};
  246. write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  247. write.pNext = nullptr;
  248. write.dstSet = m_dset;
  249. write.dstBinding = 0;
  250. write.descriptorCount = 1;
  251. write.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
  252. write.dstArrayElement = idx;
  253. write.pImageInfo = &imageInf;
  254. vkUpdateDescriptorSets(m_dev, 1, &write, 0, nullptr);
  255. return idx;
  256. }
  257. U32 DescriptorSetFactory::BindlessDescriptorSet::bindImage(const VkImageView view)
  258. {
  259. ANKI_ASSERT(view);
  260. LockGuard<Mutex> lock(m_mtx);
  261. ANKI_ASSERT(m_freeImgIndexCount > 0 && "Out of indices");
  262. // Get the index
  263. --m_freeImgIndexCount;
  264. const U32 idx = m_freeImgIndices[m_freeImgIndexCount];
  265. ANKI_ASSERT(idx < m_freeImgIndices.getSize());
  266. // Update the set
  267. VkDescriptorImageInfo imageInf = {};
  268. imageInf.imageView = view;
  269. imageInf.imageLayout = VK_IMAGE_LAYOUT_GENERAL; // Storage images are always in general.
  270. VkWriteDescriptorSet write = {};
  271. write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  272. write.pNext = nullptr;
  273. write.dstSet = m_dset;
  274. write.dstBinding = 1;
  275. write.descriptorCount = 1;
  276. write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
  277. write.dstArrayElement = idx;
  278. write.pImageInfo = &imageInf;
  279. vkUpdateDescriptorSets(m_dev, 1, &write, 0, nullptr);
  280. return idx;
  281. }
  282. void DescriptorSetFactory::BindlessDescriptorSet::unbindCommon(U32 idx, DynamicArray<U16>& freeIndices,
  283. U16& freeIndexCount)
  284. {
  285. LockGuard<Mutex> lock(m_mtx);
  286. ANKI_ASSERT(idx < freeIndices.getSize());
  287. ANKI_ASSERT(freeIndexCount < freeIndices.getSize());
  288. freeIndices[freeIndexCount] = U16(idx);
  289. ++freeIndexCount;
  290. // Sort the free indices to minimize fragmentation
  291. std::sort(&freeIndices[0], &freeIndices[0] + freeIndexCount, std::greater<U16>());
  292. // Make sure there are no duplicates
  293. for(U32 i = 1; i < freeIndexCount; ++i)
  294. {
  295. ANKI_ASSERT(freeIndices[i] != freeIndices[i - 1]);
  296. }
  297. }
  298. DescriptorSetFactory::DSAllocator::~DSAllocator()
  299. {
  300. auto alloc = m_layoutEntry->m_factory->m_alloc;
  301. while(!m_list.isEmpty())
  302. {
  303. DS* ds = &m_list.getFront();
  304. m_list.popFront();
  305. alloc.deleteInstance(ds);
  306. }
  307. for(VkDescriptorPool pool : m_pools)
  308. {
  309. vkDestroyDescriptorPool(m_layoutEntry->m_factory->m_dev, pool, nullptr);
  310. }
  311. m_pools.destroy(alloc);
  312. m_hashmap.destroy(alloc);
  313. }
  314. Error DescriptorSetFactory::DSAllocator::init()
  315. {
  316. ANKI_CHECK(createNewPool());
  317. return Error::NONE;
  318. }
  319. Error DescriptorSetFactory::DSAllocator::createNewPool()
  320. {
  321. m_lastPoolDSCount = (m_lastPoolDSCount != 0) ? U32(F32(m_lastPoolDSCount) * DESCRIPTOR_POOL_SIZE_SCALE)
  322. : DESCRIPTOR_POOL_INITIAL_SIZE;
  323. m_lastPoolFreeDSCount = m_lastPoolDSCount;
  324. // Set the create info
  325. Array<VkDescriptorPoolSize, U(DescriptorType::COUNT)> poolSizes;
  326. memcpy(&poolSizes[0], &m_layoutEntry->m_poolSizesCreateInf[0],
  327. sizeof(poolSizes[0]) * m_layoutEntry->m_poolCreateInf.poolSizeCount);
  328. for(U i = 0; i < m_layoutEntry->m_poolCreateInf.poolSizeCount; ++i)
  329. {
  330. poolSizes[i].descriptorCount *= m_lastPoolDSCount;
  331. ANKI_ASSERT(poolSizes[i].descriptorCount > 0);
  332. }
  333. VkDescriptorPoolCreateInfo ci = m_layoutEntry->m_poolCreateInf;
  334. ci.pPoolSizes = &poolSizes[0];
  335. ci.maxSets = m_lastPoolDSCount;
  336. // Create
  337. VkDescriptorPool pool;
  338. ANKI_VK_CHECK(vkCreateDescriptorPool(m_layoutEntry->m_factory->m_dev, &ci, nullptr, &pool));
  339. ANKI_TRACE_INC_COUNTER(VK_DESCRIPTOR_POOL_CREATE, 1);
  340. // Push back
  341. m_pools.resize(m_layoutEntry->m_factory->m_alloc, m_pools.getSize() + 1);
  342. m_pools[m_pools.getSize() - 1] = pool;
  343. return Error::NONE;
  344. }
  345. const DS* DescriptorSetFactory::DSAllocator::tryFindSet(U64 hash)
  346. {
  347. ANKI_ASSERT(hash > 0);
  348. auto it = m_hashmap.find(hash);
  349. if(it == m_hashmap.getEnd())
  350. {
  351. return nullptr;
  352. }
  353. else
  354. {
  355. DS* ds = *it;
  356. // Remove from the list and place at the end of the list
  357. m_list.erase(ds);
  358. m_list.pushBack(ds);
  359. ds->m_lastFrameUsed = m_layoutEntry->m_factory->m_frameCount;
  360. return ds;
  361. }
  362. }
  363. Error DescriptorSetFactory::DSAllocator::newSet(
  364. U64 hash, const Array<AnyBindingExtended, MAX_BINDINGS_PER_DESCRIPTOR_SET>& bindings, StackAllocator<U8>& tmpAlloc,
  365. const DS*& out_)
  366. {
  367. DS* out = nullptr;
  368. // First try to see if there are unused to recycle
  369. const U64 crntFrame = m_layoutEntry->m_factory->m_frameCount;
  370. auto it = m_list.getBegin();
  371. const auto end = m_list.getEnd();
  372. while(it != end)
  373. {
  374. DS* set = &(*it);
  375. U64 frameDiff = crntFrame - set->m_lastFrameUsed;
  376. if(frameDiff > DESCRIPTOR_FRAME_BUFFERING)
  377. {
  378. // Found something, recycle
  379. auto it2 = m_hashmap.find(set->m_hash);
  380. ANKI_ASSERT(it2 != m_hashmap.getEnd());
  381. m_hashmap.erase(m_layoutEntry->m_factory->m_alloc, it2);
  382. m_list.erase(set);
  383. m_list.pushBack(set);
  384. m_hashmap.emplace(m_layoutEntry->m_factory->m_alloc, hash, set);
  385. out = set;
  386. break;
  387. }
  388. ++it;
  389. }
  390. if(out == nullptr)
  391. {
  392. // Need to allocate one
  393. if(m_lastPoolFreeDSCount == 0)
  394. {
  395. // Can't allocate one from the current pool, create new
  396. ANKI_CHECK(createNewPool());
  397. }
  398. --m_lastPoolFreeDSCount;
  399. VkDescriptorSetAllocateInfo ci = {};
  400. ci.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
  401. ci.descriptorPool = m_pools.getBack();
  402. ci.pSetLayouts = &m_layoutEntry->m_layoutHandle;
  403. ci.descriptorSetCount = 1;
  404. VkDescriptorSet handle;
  405. VkResult rez = vkAllocateDescriptorSets(m_layoutEntry->m_factory->m_dev, &ci, &handle);
  406. (void)rez;
  407. ANKI_ASSERT(rez == VK_SUCCESS && "That allocation can't fail");
  408. ANKI_TRACE_INC_COUNTER(VK_DESCRIPTOR_SET_CREATE, 1);
  409. out = m_layoutEntry->m_factory->m_alloc.newInstance<DS>();
  410. out->m_handle = handle;
  411. m_hashmap.emplace(m_layoutEntry->m_factory->m_alloc, hash, out);
  412. m_list.pushBack(out);
  413. }
  414. ANKI_ASSERT(out);
  415. out->m_lastFrameUsed = crntFrame;
  416. out->m_hash = hash;
  417. // Finally, write it
  418. writeSet(bindings, *out, tmpAlloc);
  419. out_ = out;
  420. return Error::NONE;
  421. }
  422. void DescriptorSetFactory::DSAllocator::writeSet(
  423. const Array<AnyBindingExtended, MAX_BINDINGS_PER_DESCRIPTOR_SET>& bindings, const DS& set,
  424. StackAllocator<U8>& tmpAlloc)
  425. {
  426. DynamicArrayAuto<VkWriteDescriptorSet> writeInfos(tmpAlloc);
  427. DynamicArrayAuto<VkDescriptorImageInfo> texInfos(tmpAlloc);
  428. DynamicArrayAuto<VkDescriptorBufferInfo> buffInfos(tmpAlloc);
  429. DynamicArrayAuto<VkWriteDescriptorSetAccelerationStructureKHR> asInfos(tmpAlloc);
  430. DynamicArrayAuto<VkBufferView> bufferViews(tmpAlloc);
  431. // First pass: Populate the VkDescriptorImageInfo and VkDescriptorBufferInfo
  432. for(U bindingIdx = m_layoutEntry->m_minBinding; bindingIdx <= m_layoutEntry->m_maxBinding; ++bindingIdx)
  433. {
  434. if(m_layoutEntry->m_activeBindings.get(bindingIdx))
  435. {
  436. for(U arrIdx = 0; arrIdx < m_layoutEntry->m_bindingArraySize[bindingIdx]; ++arrIdx)
  437. {
  438. ANKI_ASSERT(bindings[bindingIdx].m_arraySize >= m_layoutEntry->m_bindingArraySize[bindingIdx]);
  439. const AnyBinding& b = (bindings[bindingIdx].m_arraySize == 1) ? bindings[bindingIdx].m_single
  440. : bindings[bindingIdx].m_array[arrIdx];
  441. switch(b.m_type)
  442. {
  443. case DescriptorType::COMBINED_TEXTURE_SAMPLER:
  444. {
  445. VkDescriptorImageInfo& info = *texInfos.emplaceBack();
  446. info.sampler = b.m_texAndSampler.m_samplerHandle;
  447. info.imageView = b.m_texAndSampler.m_imgViewHandle;
  448. info.imageLayout = b.m_texAndSampler.m_layout;
  449. break;
  450. }
  451. case DescriptorType::TEXTURE:
  452. {
  453. VkDescriptorImageInfo& info = *texInfos.emplaceBack();
  454. info.sampler = VK_NULL_HANDLE;
  455. info.imageView = b.m_tex.m_imgViewHandle;
  456. info.imageLayout = b.m_tex.m_layout;
  457. break;
  458. }
  459. case DescriptorType::SAMPLER:
  460. {
  461. VkDescriptorImageInfo& info = *texInfos.emplaceBack();
  462. info.sampler = b.m_sampler.m_samplerHandle;
  463. info.imageView = VK_NULL_HANDLE;
  464. info.imageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  465. break;
  466. }
  467. case DescriptorType::UNIFORM_BUFFER:
  468. case DescriptorType::STORAGE_BUFFER:
  469. {
  470. VkDescriptorBufferInfo& info = *buffInfos.emplaceBack();
  471. info.buffer = b.m_buff.m_buffHandle;
  472. info.offset = 0;
  473. info.range = (b.m_buff.m_range == MAX_PTR_SIZE) ? VK_WHOLE_SIZE : b.m_buff.m_range;
  474. break;
  475. }
  476. case DescriptorType::READ_TEXTURE_BUFFER:
  477. case DescriptorType::READ_WRITE_TEXTURE_BUFFER:
  478. {
  479. VkBufferView& view = *bufferViews.emplaceBack();
  480. view = b.m_textureBuffer.m_buffView;
  481. break;
  482. }
  483. case DescriptorType::IMAGE:
  484. {
  485. VkDescriptorImageInfo& info = *texInfos.emplaceBack();
  486. info.sampler = VK_NULL_HANDLE;
  487. info.imageView = b.m_image.m_imgViewHandle;
  488. info.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
  489. break;
  490. }
  491. case DescriptorType::ACCELERATION_STRUCTURE:
  492. {
  493. VkWriteDescriptorSetAccelerationStructureKHR& info = *asInfos.emplaceBack();
  494. info.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR;
  495. info.pNext = nullptr;
  496. info.accelerationStructureCount = 1;
  497. info.pAccelerationStructures = &b.m_accelerationStructure.m_accelerationStructureHandle;
  498. break;
  499. }
  500. default:
  501. ANKI_ASSERT(0);
  502. }
  503. }
  504. }
  505. }
  506. // Second pass: Populate the VkWriteDescriptorSet with VkDescriptorImageInfo and VkDescriptorBufferInfo
  507. U32 texCounter = 0;
  508. U32 buffCounter = 0;
  509. U32 asCounter = 0;
  510. U32 buffViewsCounter = 0;
  511. VkWriteDescriptorSet writeTemplate = {};
  512. writeTemplate.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
  513. writeTemplate.pNext = nullptr;
  514. writeTemplate.dstSet = set.m_handle;
  515. writeTemplate.descriptorCount = 1;
  516. for(U32 bindingIdx = m_layoutEntry->m_minBinding; bindingIdx <= m_layoutEntry->m_maxBinding; ++bindingIdx)
  517. {
  518. if(m_layoutEntry->m_activeBindings.get(bindingIdx))
  519. {
  520. for(U32 arrIdx = 0; arrIdx < m_layoutEntry->m_bindingArraySize[bindingIdx]; ++arrIdx)
  521. {
  522. const AnyBinding& b = (bindings[bindingIdx].m_arraySize == 1) ? bindings[bindingIdx].m_single
  523. : bindings[bindingIdx].m_array[arrIdx];
  524. VkWriteDescriptorSet& writeInfo = *writeInfos.emplaceBack(writeTemplate);
  525. writeInfo.descriptorType = convertDescriptorType(b.m_type);
  526. writeInfo.dstArrayElement = arrIdx;
  527. writeInfo.dstBinding = bindingIdx;
  528. switch(b.m_type)
  529. {
  530. case DescriptorType::COMBINED_TEXTURE_SAMPLER:
  531. case DescriptorType::TEXTURE:
  532. case DescriptorType::SAMPLER:
  533. case DescriptorType::IMAGE:
  534. writeInfo.pImageInfo = &texInfos[texCounter++];
  535. break;
  536. case DescriptorType::UNIFORM_BUFFER:
  537. case DescriptorType::STORAGE_BUFFER:
  538. writeInfo.pBufferInfo = &buffInfos[buffCounter++];
  539. break;
  540. case DescriptorType::READ_TEXTURE_BUFFER:
  541. case DescriptorType::READ_WRITE_TEXTURE_BUFFER:
  542. writeInfo.pTexelBufferView = &bufferViews[buffViewsCounter++];
  543. break;
  544. case DescriptorType::ACCELERATION_STRUCTURE:
  545. writeInfo.pNext = &asInfos[asCounter++];
  546. break;
  547. default:
  548. ANKI_ASSERT(0);
  549. }
  550. }
  551. }
  552. }
  553. // Write
  554. vkUpdateDescriptorSets(m_layoutEntry->m_factory->m_dev, writeInfos.getSize(),
  555. (writeInfos.getSize() > 0) ? &writeInfos[0] : nullptr, 0, nullptr);
  556. }
  557. DSLayoutCacheEntry::~DSLayoutCacheEntry()
  558. {
  559. auto alloc = m_factory->m_alloc;
  560. if(m_layoutHandle)
  561. {
  562. vkDestroyDescriptorSetLayout(m_factory->m_dev, m_layoutHandle, nullptr);
  563. }
  564. }
  565. Error DSLayoutCacheEntry::init(const DescriptorBinding* bindings, U32 bindingCount, U64 hash)
  566. {
  567. ANKI_ASSERT(bindings);
  568. ANKI_ASSERT(hash > 0);
  569. m_hash = hash;
  570. // Create the VK layout
  571. Array<VkDescriptorSetLayoutBinding, MAX_BINDINGS_PER_DESCRIPTOR_SET> vkBindings;
  572. VkDescriptorSetLayoutCreateInfo ci = {};
  573. ci.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
  574. for(U i = 0; i < bindingCount; ++i)
  575. {
  576. VkDescriptorSetLayoutBinding& vk = vkBindings[i];
  577. const DescriptorBinding& ak = bindings[i];
  578. vk.binding = ak.m_binding;
  579. vk.descriptorCount = ak.m_arraySize;
  580. vk.descriptorType = convertDescriptorType(ak.m_type);
  581. vk.pImmutableSamplers = nullptr;
  582. vk.stageFlags = convertShaderTypeBit(ak.m_stageMask);
  583. ANKI_ASSERT(m_activeBindings.get(ak.m_binding) == false);
  584. m_activeBindings.set(ak.m_binding);
  585. m_bindingType[ak.m_binding] = ak.m_type;
  586. m_bindingArraySize[ak.m_binding] = ak.m_arraySize;
  587. m_minBinding = min<U32>(m_minBinding, ak.m_binding);
  588. m_maxBinding = max<U32>(m_maxBinding, ak.m_binding);
  589. }
  590. ci.bindingCount = bindingCount;
  591. ci.pBindings = &vkBindings[0];
  592. ANKI_VK_CHECK(vkCreateDescriptorSetLayout(m_factory->m_dev, &ci, nullptr, &m_layoutHandle));
  593. // Create the pool info
  594. U32 poolSizeCount = 0;
  595. for(U i = 0; i < bindingCount; ++i)
  596. {
  597. U j;
  598. for(j = 0; j < poolSizeCount; ++j)
  599. {
  600. if(m_poolSizesCreateInf[j].type == convertDescriptorType(bindings[i].m_type))
  601. {
  602. m_poolSizesCreateInf[j].descriptorCount += bindings[i].m_arraySize;
  603. break;
  604. }
  605. }
  606. if(j == poolSizeCount)
  607. {
  608. m_poolSizesCreateInf[poolSizeCount].type = convertDescriptorType(bindings[i].m_type);
  609. m_poolSizesCreateInf[poolSizeCount].descriptorCount = bindings[i].m_arraySize;
  610. ++poolSizeCount;
  611. }
  612. }
  613. if(poolSizeCount == 0)
  614. {
  615. // If the poolSizeCount it means that the DS layout has 0 descriptors. Since the pool sizes can't be zero put
  616. // something in them
  617. m_poolSizesCreateInf[0].type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
  618. m_poolSizesCreateInf[0].descriptorCount = 1;
  619. ++poolSizeCount;
  620. }
  621. ANKI_ASSERT(poolSizeCount > 0);
  622. m_poolCreateInf.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
  623. m_poolCreateInf.poolSizeCount = poolSizeCount;
  624. return Error::NONE;
  625. }
  626. Error DSLayoutCacheEntry::getOrCreateDSAllocator(DescriptorSetFactory::DSAllocator*& alloc)
  627. {
  628. alloc = nullptr;
  629. // Get or create thread-local
  630. DescriptorSetFactory::ThreadLocal* threadLocal = DescriptorSetFactory::m_threadLocal;
  631. if(ANKI_UNLIKELY(threadLocal == nullptr))
  632. {
  633. threadLocal = m_factory->m_alloc.newInstance<DescriptorSetFactory::ThreadLocal>();
  634. DescriptorSetFactory::m_threadLocal = threadLocal;
  635. LockGuard<Mutex> lock(m_factory->m_allThreadLocalsMtx);
  636. m_factory->m_allThreadLocals.emplaceBack(m_factory->m_alloc, threadLocal);
  637. }
  638. // Get or create the allocator
  639. if(ANKI_UNLIKELY(m_index >= threadLocal->m_allocators.getSize()))
  640. {
  641. threadLocal->m_allocators.resize(m_factory->m_alloc, m_index + 1, nullptr);
  642. alloc = m_factory->m_alloc.newInstance<DescriptorSetFactory::DSAllocator>(this);
  643. ANKI_CHECK(alloc->init());
  644. threadLocal->m_allocators[m_index] = alloc;
  645. }
  646. else if(ANKI_UNLIKELY(threadLocal->m_allocators[m_index] == nullptr))
  647. {
  648. alloc = m_factory->m_alloc.newInstance<DescriptorSetFactory::DSAllocator>(this);
  649. ANKI_CHECK(alloc->init());
  650. threadLocal->m_allocators[m_index] = alloc;
  651. }
  652. else
  653. {
  654. alloc = threadLocal->m_allocators[m_index];
  655. }
  656. ANKI_ASSERT(alloc);
  657. return Error::NONE;
  658. }
  659. void DescriptorSetState::flush(U64& hash, Array<PtrSize, MAX_BINDINGS_PER_DESCRIPTOR_SET>& dynamicOffsets,
  660. U32& dynamicOffsetCount, Bool& bindlessDSet)
  661. {
  662. // Set some values
  663. hash = 0;
  664. dynamicOffsetCount = 0;
  665. bindlessDSet = false;
  666. // There is a chance where the bindless set is bound but the actual shaders have an empty DS layout (maybe because
  667. // the dead code elimination eliminated the bindless set). In that case we can't bind the bindless DS. We have to
  668. // treat it as regular set
  669. ANKI_ASSERT(!(m_layout.m_entry == nullptr && !m_bindlessDSetBound)
  670. && "DS layout points to bindless but no bindless is bound");
  671. const Bool reallyBindless = m_bindlessDSetBound && m_layout.m_entry == nullptr;
  672. if(!reallyBindless)
  673. {
  674. // Get cache entry
  675. ANKI_ASSERT(m_layout.m_entry);
  676. const DSLayoutCacheEntry& entry = *m_layout.m_entry;
  677. // Early out if nothing happened
  678. const Bool anyActiveBindingDirty = !!(entry.m_activeBindings & m_dirtyBindings);
  679. if(!anyActiveBindingDirty && !m_layoutDirty)
  680. {
  681. return;
  682. }
  683. Bool dynamicOffsetsDirty = false;
  684. // Compute the hash
  685. Array<U64, MAX_BINDINGS_PER_DESCRIPTOR_SET * 2 * 2> toHash;
  686. U toHashCount = 0;
  687. const U minBinding = entry.m_minBinding;
  688. const U maxBinding = entry.m_maxBinding;
  689. for(U i = minBinding; i <= maxBinding; ++i)
  690. {
  691. if(entry.m_activeBindings.get(i))
  692. {
  693. ANKI_ASSERT(m_bindingSet.get(i) && "Forgot to bind");
  694. ANKI_ASSERT(m_bindings[i].m_arraySize >= entry.m_bindingArraySize[i] && "Bound less");
  695. const Bool crntBindingDirty = m_dirtyBindings.get(i);
  696. m_dirtyBindings.unset(i);
  697. for(U arrIdx = 0; arrIdx < entry.m_bindingArraySize[i]; ++arrIdx)
  698. {
  699. ANKI_ASSERT(arrIdx < m_bindings[i].m_arraySize);
  700. if(arrIdx > 1)
  701. {
  702. ANKI_ASSERT(m_bindings[i].m_array[arrIdx].m_type == m_bindings[i].m_array[arrIdx - 1].m_type);
  703. }
  704. const AnyBinding& anyBinding =
  705. (m_bindings[i].m_arraySize == 1) ? m_bindings[i].m_single : m_bindings[i].m_array[arrIdx];
  706. ANKI_ASSERT(anyBinding.m_uuids[0] != 0 && "Forgot to bind");
  707. toHash[toHashCount++] = anyBinding.m_uuids[0];
  708. switch(entry.m_bindingType[i])
  709. {
  710. case DescriptorType::COMBINED_TEXTURE_SAMPLER:
  711. ANKI_ASSERT(anyBinding.m_type == DescriptorType::COMBINED_TEXTURE_SAMPLER
  712. && "Have bound the wrong type");
  713. toHash[toHashCount++] = anyBinding.m_uuids[1];
  714. toHash[toHashCount++] = U64(anyBinding.m_texAndSampler.m_layout);
  715. break;
  716. case DescriptorType::TEXTURE:
  717. ANKI_ASSERT(anyBinding.m_type == DescriptorType::TEXTURE && "Have bound the wrong type");
  718. toHash[toHashCount++] = U64(anyBinding.m_tex.m_layout);
  719. break;
  720. case DescriptorType::SAMPLER:
  721. ANKI_ASSERT(anyBinding.m_type == DescriptorType::SAMPLER && "Have bound the wrong type");
  722. break;
  723. case DescriptorType::UNIFORM_BUFFER:
  724. ANKI_ASSERT(anyBinding.m_type == DescriptorType::UNIFORM_BUFFER && "Have bound the wrong type");
  725. toHash[toHashCount++] = anyBinding.m_buff.m_range;
  726. dynamicOffsets[dynamicOffsetCount++] = anyBinding.m_buff.m_offset;
  727. dynamicOffsetsDirty = dynamicOffsetsDirty || crntBindingDirty;
  728. break;
  729. case DescriptorType::STORAGE_BUFFER:
  730. ANKI_ASSERT(anyBinding.m_type == DescriptorType::STORAGE_BUFFER && "Have bound the wrong type");
  731. toHash[toHashCount++] = anyBinding.m_buff.m_range;
  732. dynamicOffsets[dynamicOffsetCount++] = anyBinding.m_buff.m_offset;
  733. dynamicOffsetsDirty = dynamicOffsetsDirty || crntBindingDirty;
  734. break;
  735. case DescriptorType::READ_TEXTURE_BUFFER:
  736. ANKI_ASSERT(anyBinding.m_type == DescriptorType::READ_TEXTURE_BUFFER
  737. && "Have bound the wrong type");
  738. toHash[toHashCount++] = anyBinding.m_uuids[1];
  739. break;
  740. case DescriptorType::READ_WRITE_TEXTURE_BUFFER:
  741. ANKI_ASSERT(anyBinding.m_type == DescriptorType::READ_WRITE_TEXTURE_BUFFER
  742. && "Have bound the wrong type");
  743. toHash[toHashCount++] = anyBinding.m_uuids[1];
  744. break;
  745. case DescriptorType::IMAGE:
  746. ANKI_ASSERT(anyBinding.m_type == DescriptorType::IMAGE && "Have bound the wrong type");
  747. break;
  748. case DescriptorType::ACCELERATION_STRUCTURE:
  749. ANKI_ASSERT(anyBinding.m_type == DescriptorType::ACCELERATION_STRUCTURE
  750. && "Have bound the wrong type");
  751. break;
  752. default:
  753. ANKI_ASSERT(0);
  754. }
  755. }
  756. }
  757. }
  758. const U64 newHash = computeHash(&toHash[0], toHashCount * sizeof(U64));
  759. if(newHash != m_lastHash || dynamicOffsetsDirty || m_layoutDirty)
  760. {
  761. // DS needs rebind
  762. m_lastHash = newHash;
  763. hash = newHash;
  764. }
  765. else
  766. {
  767. // All clean, keep hash equal to 0
  768. }
  769. m_layoutDirty = false;
  770. }
  771. else
  772. {
  773. // Custom set
  774. if(!m_bindlessDSetDirty && !m_layoutDirty)
  775. {
  776. return;
  777. }
  778. bindlessDSet = true;
  779. hash = 1;
  780. m_bindlessDSetDirty = false;
  781. m_layoutDirty = false;
  782. }
  783. }
  784. DescriptorSetFactory::~DescriptorSetFactory()
  785. {
  786. }
  787. Error DescriptorSetFactory::init(const GrAllocator<U8>& alloc, VkDevice dev, U32 bindlessTextureCount,
  788. U32 bindlessImageCount)
  789. {
  790. m_alloc = alloc;
  791. m_dev = dev;
  792. m_bindless = m_alloc.newInstance<BindlessDescriptorSet>();
  793. ANKI_CHECK(m_bindless->init(alloc, dev, bindlessTextureCount, bindlessImageCount));
  794. m_bindlessTextureCount = bindlessTextureCount;
  795. m_bindlessImageCount = bindlessImageCount;
  796. return Error::NONE;
  797. }
  798. void DescriptorSetFactory::destroy()
  799. {
  800. for(ThreadLocal* threadLocal : m_allThreadLocals)
  801. {
  802. for(DSAllocator* alloc : threadLocal->m_allocators)
  803. {
  804. m_alloc.deleteInstance(alloc);
  805. }
  806. threadLocal->m_allocators.destroy(m_alloc);
  807. m_alloc.deleteInstance(threadLocal);
  808. }
  809. m_allThreadLocals.destroy(m_alloc);
  810. for(DSLayoutCacheEntry* l : m_caches)
  811. {
  812. m_alloc.deleteInstance(l);
  813. }
  814. m_caches.destroy(m_alloc);
  815. if(m_bindless)
  816. {
  817. m_alloc.deleteInstance(m_bindless);
  818. }
  819. }
  820. Error DescriptorSetFactory::newDescriptorSetLayout(const DescriptorSetLayoutInitInfo& init, DescriptorSetLayout& layout)
  821. {
  822. // Compute the hash for the layout
  823. Array<DescriptorBinding, MAX_BINDINGS_PER_DESCRIPTOR_SET> bindings;
  824. const U32 bindingCount = init.m_bindings.getSize();
  825. U64 hash;
  826. if(init.m_bindings.getSize() > 0)
  827. {
  828. memcpy(bindings.getBegin(), init.m_bindings.getBegin(), init.m_bindings.getSizeInBytes());
  829. std::sort(bindings.getBegin(), bindings.getBegin() + bindingCount,
  830. [](const DescriptorBinding& a, const DescriptorBinding& b) {
  831. return a.m_binding < b.m_binding;
  832. });
  833. hash = computeHash(&bindings[0], init.m_bindings.getSizeInBytes());
  834. ANKI_ASSERT(hash != 1);
  835. }
  836. else
  837. {
  838. hash = 1;
  839. }
  840. // Identify if the DS is the bindless one. It is if there is at least one binding that matches the criteria
  841. Bool isBindless = false;
  842. if(bindingCount > 0)
  843. {
  844. isBindless = true;
  845. for(U32 i = 0; i < bindingCount; ++i)
  846. {
  847. const DescriptorBinding& binding = bindings[i];
  848. if(binding.m_binding == 0 && binding.m_type == DescriptorType::TEXTURE
  849. && binding.m_arraySize == m_bindlessTextureCount)
  850. {
  851. // All good
  852. }
  853. else if(binding.m_binding == 1 && binding.m_type == DescriptorType::IMAGE
  854. && binding.m_arraySize == m_bindlessImageCount)
  855. {
  856. // All good
  857. }
  858. else
  859. {
  860. isBindless = false;
  861. }
  862. }
  863. }
  864. // Find or create the cache entry
  865. if(isBindless)
  866. {
  867. layout.m_handle = m_bindless->getDescriptorSetLayout();
  868. layout.m_entry = nullptr;
  869. }
  870. else
  871. {
  872. LockGuard<SpinLock> lock(m_cachesMtx);
  873. DSLayoutCacheEntry* cache = nullptr;
  874. U count = 0;
  875. for(DSLayoutCacheEntry* it : m_caches)
  876. {
  877. if(it->m_hash == hash)
  878. {
  879. cache = it;
  880. break;
  881. }
  882. ++count;
  883. }
  884. if(cache == nullptr)
  885. {
  886. cache = m_alloc.newInstance<DSLayoutCacheEntry>(this, m_caches.getSize());
  887. ANKI_CHECK(cache->init(bindings.getBegin(), bindingCount, hash));
  888. m_caches.emplaceBack(m_alloc, cache);
  889. }
  890. // Set the layout
  891. layout.m_handle = cache->m_layoutHandle;
  892. layout.m_entry = cache;
  893. }
  894. return Error::NONE;
  895. }
  896. Error DescriptorSetFactory::newDescriptorSet(ThreadId tid, StackAllocator<U8>& tmpAlloc, DescriptorSetState& state,
  897. DescriptorSet& set, Bool& dirty,
  898. Array<PtrSize, MAX_BINDINGS_PER_DESCRIPTOR_SET>& dynamicOffsets,
  899. U32& dynamicOffsetCount)
  900. {
  901. ANKI_TRACE_SCOPED_EVENT(VK_DESCRIPTOR_SET_GET_OR_CREATE);
  902. U64 hash;
  903. Bool bindlessDSet;
  904. state.flush(hash, dynamicOffsets, dynamicOffsetCount, bindlessDSet);
  905. if(hash == 0)
  906. {
  907. dirty = false;
  908. return Error::NONE;
  909. }
  910. else
  911. {
  912. dirty = true;
  913. if(!bindlessDSet)
  914. {
  915. DescriptorSetLayout layout = state.m_layout;
  916. DSLayoutCacheEntry& entry = *layout.m_entry;
  917. // Get thread allocator
  918. DSAllocator* alloc;
  919. ANKI_CHECK(entry.getOrCreateDSAllocator(alloc));
  920. // Finally, allocate
  921. const DS* s;
  922. ANKI_CHECK(alloc->getOrCreateSet(hash, state.m_bindings, tmpAlloc, s));
  923. set.m_handle = s->m_handle;
  924. ANKI_ASSERT(set.m_handle != VK_NULL_HANDLE);
  925. }
  926. else
  927. {
  928. set = m_bindless->getDescriptorSet();
  929. }
  930. }
  931. return Error::NONE;
  932. }
  933. U32 DescriptorSetFactory::bindBindlessTexture(const VkImageView view, const VkImageLayout layout)
  934. {
  935. ANKI_ASSERT(m_bindless);
  936. return m_bindless->bindTexture(view, layout);
  937. }
  938. U32 DescriptorSetFactory::bindBindlessImage(const VkImageView view)
  939. {
  940. ANKI_ASSERT(m_bindless);
  941. return m_bindless->bindImage(view);
  942. }
  943. void DescriptorSetFactory::unbindBindlessTexture(U32 idx)
  944. {
  945. ANKI_ASSERT(m_bindless);
  946. m_bindless->unbindTexture(idx);
  947. }
  948. void DescriptorSetFactory::unbindBindlessImage(U32 idx)
  949. {
  950. ANKI_ASSERT(m_bindless);
  951. m_bindless->unbindImage(idx);
  952. }
  953. } // end namespace anki