VkQueryFactory.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #pragma once
  6. #include <AnKi/Gr/Vulkan/VkCommon.h>
  7. #include <AnKi/Util/BitSet.h>
  8. #include <AnKi/Util/List.h>
  9. namespace anki {
  10. // Forward
  11. class QueryFactoryChunk;
  12. /// @addtogroup vulkan
  13. /// @{
  14. /// The return handle of a query allocation.
  15. class MicroQuery
  16. {
  17. friend class QueryFactory;
  18. public:
  19. VkQueryPool getQueryPool() const
  20. {
  21. ANKI_ASSERT(m_pool != VK_NULL_HANDLE);
  22. return m_pool;
  23. }
  24. /// Get the index of the query inside the query pool.
  25. U32 getQueryIndex() const
  26. {
  27. ANKI_ASSERT(m_queryIndex != kMaxU32);
  28. return m_queryIndex;
  29. }
  30. explicit operator Bool() const
  31. {
  32. return m_pool != VK_NULL_HANDLE;
  33. }
  34. private:
  35. VkQueryPool m_pool = VK_NULL_HANDLE;
  36. U32 m_queryIndex = kMaxU32;
  37. QueryFactoryChunk* m_chunk = nullptr;
  38. };
  39. /// An allocation chunk.
  40. class QueryFactoryChunk : public IntrusiveListEnabled<QueryFactoryChunk>
  41. {
  42. friend class QueryFactory;
  43. private:
  44. VkQueryPool m_pool = VK_NULL_HANDLE;
  45. BitSet<kMaxQueriesPerQueryChunk> m_allocatedMask = {false};
  46. U32 m_subAllocationCount = 0;
  47. };
  48. /// Batch allocator of queries.
  49. class QueryFactory
  50. {
  51. public:
  52. QueryFactory(VkQueryType poolType, VkQueryPipelineStatisticFlags pplineStatisticsFlags = 0)
  53. {
  54. m_poolType = poolType;
  55. m_pplineStatisticsFlags = pplineStatisticsFlags;
  56. }
  57. QueryFactory(const QueryFactory&) = delete; // Non-copyable
  58. ~QueryFactory();
  59. QueryFactory& operator=(const QueryFactory&) = delete; // Non-copyable
  60. /// @note It's thread-safe.
  61. Error newQuery(MicroQuery& handle);
  62. /// @note It's thread-safe.
  63. void deleteQuery(MicroQuery& handle);
  64. private:
  65. using Chunk = QueryFactoryChunk;
  66. IntrusiveList<Chunk> m_chunks;
  67. Mutex m_mtx;
  68. VkQueryType m_poolType = VK_QUERY_TYPE_MAX_ENUM;
  69. VkQueryPipelineStatisticFlags m_pplineStatisticsFlags = 0;
  70. };
  71. class OcclusionQueryFactory : public QueryFactory, public MakeSingleton<OcclusionQueryFactory>
  72. {
  73. public:
  74. OcclusionQueryFactory()
  75. : QueryFactory(VK_QUERY_TYPE_OCCLUSION)
  76. {
  77. }
  78. };
  79. class TimestampQueryFactory : public QueryFactory, public MakeSingleton<TimestampQueryFactory>
  80. {
  81. public:
  82. TimestampQueryFactory()
  83. : QueryFactory(VK_QUERY_TYPE_TIMESTAMP)
  84. {
  85. }
  86. };
  87. class PrimitivesPassedClippingFactory : public QueryFactory, public MakeSingleton<PrimitivesPassedClippingFactory>
  88. {
  89. public:
  90. PrimitivesPassedClippingFactory()
  91. : QueryFactory(VK_QUERY_TYPE_PIPELINE_STATISTICS, VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT)
  92. {
  93. }
  94. };
  95. /// @}
  96. } // end namespace anki