QueryFactory.cpp 2.2 KB

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