QueryFactory.h 1.9 KB

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