FenceFactory.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. namespace anki
  8. {
  9. // Forward
  10. class FenceFactory;
  11. /// @addtogroup vulkan
  12. /// @{
  13. /// Fence wrapper over VkFence.
  14. class MicroFence
  15. {
  16. friend class FenceFactory;
  17. friend class MicroFencePtrDeleter;
  18. public:
  19. MicroFence(FenceFactory* f);
  20. MicroFence(const MicroFence&) = delete; // Non-copyable
  21. ~MicroFence();
  22. MicroFence& operator=(const MicroFence&) = delete; // Non-copyable
  23. const VkFence& getHandle() const
  24. {
  25. ANKI_ASSERT(m_handle);
  26. return m_handle;
  27. }
  28. Atomic<U32>& getRefcount()
  29. {
  30. return m_refcount;
  31. }
  32. GrAllocator<U8> getAllocator() const;
  33. void wait()
  34. {
  35. const Bool timeout = !clientWait(MAX_SECOND);
  36. if(ANKI_UNLIKELY(timeout))
  37. {
  38. ANKI_VK_LOGF("Waiting for a fence timed out");
  39. }
  40. }
  41. Bool clientWait(Second seconds);
  42. Bool done() const;
  43. private:
  44. VkFence m_handle = VK_NULL_HANDLE;
  45. Atomic<U32> m_refcount = {0};
  46. FenceFactory* m_factory = nullptr;
  47. };
  48. /// Deleter for FencePtr.
  49. class MicroFencePtrDeleter
  50. {
  51. public:
  52. void operator()(MicroFence* f);
  53. };
  54. /// Fence smart pointer.
  55. using MicroFencePtr = IntrusivePtr<MicroFence, MicroFencePtrDeleter>;
  56. /// A factory of fences.
  57. class FenceFactory
  58. {
  59. friend class MicroFence;
  60. friend class MicroFencePtrDeleter;
  61. public:
  62. FenceFactory()
  63. {
  64. }
  65. ~FenceFactory()
  66. {
  67. }
  68. void init(GrAllocator<U8> alloc, VkDevice dev)
  69. {
  70. ANKI_ASSERT(dev);
  71. m_alloc = alloc;
  72. m_dev = dev;
  73. }
  74. void destroy();
  75. /// Create a new fence pointer.
  76. MicroFencePtr newInstance()
  77. {
  78. return MicroFencePtr(newFence());
  79. }
  80. private:
  81. GrAllocator<U8> m_alloc;
  82. VkDevice m_dev = VK_NULL_HANDLE;
  83. DynamicArray<MicroFence*> m_fences;
  84. U32 m_fenceCount = 0;
  85. Mutex m_mtx;
  86. MicroFence* newFence();
  87. void deleteFence(MicroFence* fence);
  88. };
  89. /// @}
  90. } // end namespace anki
  91. #include <AnKi/Gr/Vulkan/FenceFactory.inl.h>