SemaphoreFactory.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/FenceFactory.h>
  7. #include <AnKi/Gr/Vulkan/MicroObjectRecycler.h>
  8. namespace anki {
  9. // Forward
  10. class SemaphoreFactory;
  11. /// @addtogroup vulkan
  12. /// @{
  13. /// Simple semaphore wrapper.
  14. class MicroSemaphore
  15. {
  16. friend class SemaphoreFactory;
  17. friend class MicroSemaphorePtrDeleter;
  18. template<typename, typename>
  19. friend class GenericPoolAllocator;
  20. public:
  21. MicroSemaphore(const MicroSemaphore&) = delete; // Non-copyable
  22. MicroSemaphore& operator=(const MicroSemaphore&) = delete; // Non-copyable
  23. const VkSemaphore& getHandle() const
  24. {
  25. ANKI_ASSERT(m_handle);
  26. return m_handle;
  27. }
  28. GrAllocator<U8> getAllocator() const;
  29. Atomic<U32>& getRefcount()
  30. {
  31. return m_refcount;
  32. }
  33. MicroFencePtr& getFence()
  34. {
  35. return m_fence;
  36. }
  37. void setFence(MicroFencePtr& fence)
  38. {
  39. m_fence = fence;
  40. }
  41. Bool clientWait(Second seconds);
  42. Bool isTimeline() const
  43. {
  44. return m_isTimeline;
  45. }
  46. /// Get the value of the semaphore after a signal.
  47. /// @note It's thread safe.
  48. U64 getNextSemaphoreValue()
  49. {
  50. ANKI_ASSERT(m_isTimeline);
  51. return m_timelineValue.fetchAdd(1) + 1;
  52. }
  53. /// Get the value of the semaphore to wait on.
  54. /// @note It's thread safe.
  55. U64 getSemaphoreValue() const
  56. {
  57. ANKI_ASSERT(m_isTimeline);
  58. return m_timelineValue.load();
  59. }
  60. private:
  61. VkSemaphore m_handle = VK_NULL_HANDLE;
  62. Atomic<U32> m_refcount = {0};
  63. SemaphoreFactory* m_factory = nullptr;
  64. /// Fence to find out when it's safe to reuse this semaphore.
  65. MicroFencePtr m_fence;
  66. Atomic<U64> m_timelineValue = {0};
  67. Bool m_isTimeline = false;
  68. MicroSemaphore(SemaphoreFactory* f, MicroFencePtr fence, Bool isTimeline);
  69. ~MicroSemaphore();
  70. };
  71. /// MicroSemaphorePtr deleter.
  72. class MicroSemaphorePtrDeleter
  73. {
  74. public:
  75. void operator()(MicroSemaphore* s);
  76. };
  77. /// MicroSemaphore smart pointer.
  78. using MicroSemaphorePtr = IntrusivePtr<MicroSemaphore, MicroSemaphorePtrDeleter>;
  79. /// Factory of semaphores.
  80. class SemaphoreFactory
  81. {
  82. friend class MicroSemaphore;
  83. friend class MicroSemaphorePtrDeleter;
  84. public:
  85. void init(GrAllocator<U8> alloc, VkDevice dev)
  86. {
  87. ANKI_ASSERT(dev);
  88. m_alloc = alloc;
  89. m_dev = dev;
  90. m_binaryRecycler.init(alloc);
  91. m_timelineRecycler.init(alloc);
  92. }
  93. void destroy()
  94. {
  95. m_binaryRecycler.destroy();
  96. m_timelineRecycler.destroy();
  97. }
  98. MicroSemaphorePtr newInstance(MicroFencePtr fence, Bool isTimeline);
  99. private:
  100. GrAllocator<U8> m_alloc;
  101. VkDevice m_dev = VK_NULL_HANDLE;
  102. MicroObjectRecycler<MicroSemaphore> m_binaryRecycler;
  103. MicroObjectRecycler<MicroSemaphore> m_timelineRecycler;
  104. };
  105. /// @}
  106. } // end namespace anki
  107. #include <AnKi/Gr/Vulkan/SemaphoreFactory.inl.h>