QueryFactory.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #pragma once
  6. #include <AnKi/Gr/Vulkan/Common.h>
  7. #include <AnKi/Util/BitSet.h>
  8. #include <AnKi/Util/List.h>
  9. namespace anki
  10. {
  11. // Forward
  12. class QueryFactoryChunk;
  13. /// @addtogroup vulkan
  14. /// @{
  15. const U MAX_SUB_ALLOCATIONS_PER_QUERY_CHUNK = 64;
  16. /// The return handle of a query allocation.
  17. class MicroQuery
  18. {
  19. friend class QueryFactory;
  20. public:
  21. VkQueryPool getQueryPool() const
  22. {
  23. ANKI_ASSERT(m_pool != VK_NULL_HANDLE);
  24. return m_pool;
  25. }
  26. /// Get the index of the query inside the query pool.
  27. U32 getQueryIndex() const
  28. {
  29. ANKI_ASSERT(m_queryIndex != MAX_U32);
  30. return m_queryIndex;
  31. }
  32. explicit operator Bool() const
  33. {
  34. return m_pool != VK_NULL_HANDLE;
  35. }
  36. private:
  37. VkQueryPool m_pool = VK_NULL_HANDLE;
  38. U32 m_queryIndex = MAX_U32;
  39. QueryFactoryChunk* m_chunk = nullptr;
  40. };
  41. /// An allocation chunk.
  42. class QueryFactoryChunk : public IntrusiveListEnabled<QueryFactoryChunk>
  43. {
  44. friend class QueryFactory;
  45. private:
  46. VkQueryPool m_pool = VK_NULL_HANDLE;
  47. BitSet<MAX_SUB_ALLOCATIONS_PER_QUERY_CHUNK> m_allocatedMask = {false};
  48. U32 m_subAllocationCount = 0;
  49. };
  50. /// Batch allocator of queries.
  51. class QueryFactory
  52. {
  53. public:
  54. QueryFactory()
  55. {
  56. }
  57. QueryFactory(const QueryFactory&) = delete; // Non-copyable
  58. ~QueryFactory();
  59. QueryFactory& operator=(const QueryFactory&) = delete; // Non-copyable
  60. void init(GrAllocator<U8> alloc, VkDevice dev, VkQueryType poolType)
  61. {
  62. m_alloc = alloc;
  63. m_dev = dev;
  64. m_poolType = poolType;
  65. }
  66. /// @note It's thread-safe.
  67. ANKI_USE_RESULT Error newQuery(MicroQuery& handle);
  68. /// @note It's thread-safe.
  69. void deleteQuery(MicroQuery& handle);
  70. private:
  71. using Chunk = QueryFactoryChunk;
  72. GrAllocator<U8> m_alloc;
  73. VkDevice m_dev;
  74. IntrusiveList<Chunk> m_chunks;
  75. Mutex m_mtx;
  76. VkQueryType m_poolType = VK_QUERY_TYPE_MAX_ENUM;
  77. };
  78. /// @}
  79. } // end namespace anki