2
0

QueryFactory.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/QueryFactory.h>
  6. namespace anki
  7. {
  8. QueryFactory::~QueryFactory()
  9. {
  10. if(!m_chunks.isEmpty())
  11. {
  12. ANKI_VK_LOGW("Forgot the delete some queries");
  13. }
  14. }
  15. Error QueryFactory::newQuery(MicroQuery& handle)
  16. {
  17. ANKI_ASSERT(!handle);
  18. LockGuard<Mutex> lock(m_mtx);
  19. // Find a not-full chunk
  20. Chunk* chunk = nullptr;
  21. for(Chunk& c : m_chunks)
  22. {
  23. if(c.m_subAllocationCount < MAX_SUB_ALLOCATIONS_PER_QUERY_CHUNK)
  24. {
  25. // Found one
  26. if(chunk == nullptr)
  27. {
  28. chunk = &c;
  29. }
  30. else if(c.m_subAllocationCount > chunk->m_subAllocationCount)
  31. {
  32. // To decrease fragmentation use the most full chunk
  33. chunk = &c;
  34. }
  35. }
  36. }
  37. if(chunk == nullptr)
  38. {
  39. // Create new chunk
  40. chunk = m_alloc.newInstance<Chunk>();
  41. VkQueryPoolCreateInfo ci = {};
  42. ci.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
  43. ci.queryType = m_poolType;
  44. ci.queryCount = MAX_SUB_ALLOCATIONS_PER_QUERY_CHUNK;
  45. ANKI_VK_CHECK(vkCreateQueryPool(m_dev, &ci, nullptr, &chunk->m_pool));
  46. m_chunks.pushBack(chunk);
  47. }
  48. ANKI_ASSERT(chunk);
  49. // Allocate from chunk
  50. for(U32 i = 0; i < MAX_SUB_ALLOCATIONS_PER_QUERY_CHUNK; ++i)
  51. {
  52. if(chunk->m_allocatedMask.get(i) == 0)
  53. {
  54. chunk->m_allocatedMask.set(i);
  55. ++chunk->m_subAllocationCount;
  56. handle.m_pool = chunk->m_pool;
  57. handle.m_queryIndex = i;
  58. handle.m_chunk = chunk;
  59. break;
  60. }
  61. }
  62. ANKI_ASSERT(!!handle);
  63. return Error::NONE;
  64. }
  65. void QueryFactory::deleteQuery(MicroQuery& handle)
  66. {
  67. ANKI_ASSERT(handle.m_pool && handle.m_queryIndex != MAX_U32 && handle.m_chunk);
  68. LockGuard<Mutex> lock(m_mtx);
  69. Chunk* chunk = handle.m_chunk;
  70. ANKI_ASSERT(chunk->m_subAllocationCount > 0);
  71. --chunk->m_subAllocationCount;
  72. if(chunk->m_subAllocationCount == 0)
  73. {
  74. // Delete the chunk
  75. ANKI_ASSERT(chunk->m_allocatedMask.getAny());
  76. vkDestroyQueryPool(m_dev, chunk->m_pool, nullptr);
  77. m_chunks.erase(chunk);
  78. m_alloc.deleteInstance(chunk);
  79. }
  80. else
  81. {
  82. ANKI_ASSERT(chunk->m_allocatedMask.get(handle.m_queryIndex));
  83. chunk->m_allocatedMask.unset(handle.m_queryIndex);
  84. }
  85. handle = {};
  86. }
  87. } // end namespace anki