QueryFactory.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 : public NonCopyable
  52. {
  53. public:
  54. QueryFactory()
  55. {
  56. }
  57. ~QueryFactory();
  58. void init(GrAllocator<U8> alloc, VkDevice dev, VkQueryType poolType)
  59. {
  60. m_alloc = alloc;
  61. m_dev = dev;
  62. m_poolType = poolType;
  63. }
  64. /// @note It's thread-safe.
  65. ANKI_USE_RESULT Error newQuery(MicroQuery& handle);
  66. /// @note It's thread-safe.
  67. void deleteQuery(MicroQuery& handle);
  68. private:
  69. using Chunk = QueryFactoryChunk;
  70. GrAllocator<U8> m_alloc;
  71. VkDevice m_dev;
  72. IntrusiveList<Chunk> m_chunks;
  73. Mutex m_mtx;
  74. VkQueryType m_poolType = VK_QUERY_TYPE_MAX_ENUM;
  75. };
  76. /// @}
  77. } // end namespace anki