FenceFactory.h 1.8 KB

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