FenceFactory.h 1.9 KB

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