VkFenceFactory.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (C) 2009-present, 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/VkCommon.h>
  7. #include <AnKi/Gr/BackendCommon/MicroFenceFactory.h>
  8. #include <AnKi/Util/Tracer.h>
  9. namespace anki {
  10. /// @addtogroup vulkan
  11. /// @{
  12. /// Fence wrapper over VkFence.
  13. class MicroFenceImpl
  14. {
  15. public:
  16. VkFence m_handle = VK_NULL_HANDLE;
  17. ~MicroFenceImpl()
  18. {
  19. ANKI_ASSERT(!m_handle);
  20. }
  21. operator Bool() const
  22. {
  23. return m_handle != 0;
  24. }
  25. Bool clientWait(Second seconds)
  26. {
  27. ANKI_ASSERT(m_handle);
  28. const F64 nsf = 1e+9 * seconds;
  29. const U64 ns = U64(nsf);
  30. VkResult res;
  31. ANKI_VK_CHECKF(res = vkWaitForFences(getVkDevice(), 1, &m_handle, true, ns));
  32. return res != VK_TIMEOUT;
  33. }
  34. Bool signaled()
  35. {
  36. ANKI_ASSERT(m_handle);
  37. VkResult status;
  38. ANKI_VK_CHECKF(status = vkGetFenceStatus(getVkDevice(), m_handle));
  39. return status == VK_SUCCESS;
  40. }
  41. void create()
  42. {
  43. ANKI_ASSERT(!m_handle);
  44. VkFenceCreateInfo ci = {};
  45. ci.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
  46. ANKI_VK_CHECKF(vkCreateFence(getVkDevice(), &ci, nullptr, &m_handle));
  47. }
  48. void destroy()
  49. {
  50. ANKI_ASSERT(m_handle);
  51. vkDestroyFence(getVkDevice(), m_handle, nullptr);
  52. m_handle = 0;
  53. }
  54. void reset()
  55. {
  56. ANKI_ASSERT(m_handle);
  57. ANKI_VK_CHECKF(vkResetFences(getVkDevice(), 1, &m_handle));
  58. }
  59. void setName(CString name) const;
  60. };
  61. using VulkanMicroFence = MicroFence<MicroFenceImpl>;
  62. /// Deleter for FencePtr.
  63. class MicroFencePtrDeleter
  64. {
  65. public:
  66. void operator()(VulkanMicroFence* fence);
  67. };
  68. /// Fence smart pointer.
  69. using MicroFencePtr = IntrusivePtr<VulkanMicroFence, MicroFencePtrDeleter>;
  70. /// A factory of fences.
  71. class FenceFactory : public MakeSingleton<FenceFactory>
  72. {
  73. friend class MicroFencePtrDeleter;
  74. public:
  75. /// Create a new fence pointer.
  76. MicroFencePtr newInstance(CString name = "unnamed")
  77. {
  78. return MicroFencePtr(m_factory.newFence(name));
  79. }
  80. private:
  81. MicroFenceFactory<MicroFenceImpl> m_factory;
  82. void deleteFence(VulkanMicroFence* fence)
  83. {
  84. m_factory.releaseFence(fence);
  85. }
  86. };
  87. inline void MicroFencePtrDeleter::operator()(VulkanMicroFence* fence)
  88. {
  89. ANKI_ASSERT(fence);
  90. FenceFactory::getSingleton().deleteFence(fence);
  91. }
  92. /// @}
  93. } // end namespace anki