VkQueryFactory.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (C) 2009-present, 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/VkQueryFactory.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 < kMaxQueriesPerQueryChunk)
  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 = newInstance<Chunk>(GrMemoryPool::getSingleton());
  40. VkQueryPoolCreateInfo ci = {};
  41. ci.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO;
  42. ci.queryType = m_poolType;
  43. ci.queryCount = kMaxQueriesPerQueryChunk;
  44. ci.pipelineStatistics = m_pplineStatisticsFlags;
  45. ANKI_VK_CHECK(vkCreateQueryPool(getVkDevice(), &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 < kMaxQueriesPerQueryChunk; ++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. vkResetQueryPool(getVkDevice(), chunk->m_pool, handle.m_queryIndex, 1);
  60. break;
  61. }
  62. }
  63. ANKI_ASSERT(!!handle);
  64. return Error::kNone;
  65. }
  66. void QueryFactory::deleteQuery(MicroQuery& handle)
  67. {
  68. ANKI_ASSERT(handle.m_pool && handle.m_queryIndex != kMaxU32 && handle.m_chunk);
  69. LockGuard<Mutex> lock(m_mtx);
  70. Chunk* chunk = handle.m_chunk;
  71. ANKI_ASSERT(chunk->m_subAllocationCount > 0);
  72. --chunk->m_subAllocationCount;
  73. if(chunk->m_subAllocationCount == 0)
  74. {
  75. // Delete the chunk
  76. ANKI_ASSERT(chunk->m_allocatedMask.getAnySet());
  77. vkDestroyQueryPool(getVkDevice(), chunk->m_pool, nullptr);
  78. m_chunks.erase(chunk);
  79. deleteInstance(GrMemoryPool::getSingleton(), chunk);
  80. }
  81. else
  82. {
  83. ANKI_ASSERT(chunk->m_allocatedMask.get(handle.m_queryIndex));
  84. chunk->m_allocatedMask.unset(handle.m_queryIndex);
  85. }
  86. handle = {};
  87. }
  88. } // end namespace anki