FenceFactory.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include <AnKi/Gr/Vulkan/FenceFactory.h>
  6. namespace anki {
  7. void FenceFactory::destroy()
  8. {
  9. for(U32 i = 0; i < m_fenceCount; ++i)
  10. {
  11. m_alloc.deleteInstance(m_fences[i]);
  12. }
  13. m_fences.destroy(m_alloc);
  14. }
  15. MicroFence* FenceFactory::newFence()
  16. {
  17. MicroFence* out = nullptr;
  18. LockGuard<Mutex> lock(m_mtx);
  19. U32 count = m_fenceCount;
  20. while(count--)
  21. {
  22. VkResult status;
  23. ANKI_VK_CHECKF(status = vkGetFenceStatus(m_dev, m_fences[count]->getHandle()));
  24. if(status == VK_SUCCESS)
  25. {
  26. out = m_fences[count];
  27. ANKI_VK_CHECKF(vkResetFences(m_dev, 1, &m_fences[count]->getHandle()));
  28. // Pop it
  29. for(U32 i = count; i < m_fenceCount - 1; ++i)
  30. {
  31. m_fences[i] = m_fences[i + 1];
  32. }
  33. --m_fenceCount;
  34. break;
  35. }
  36. else if(status != VK_NOT_READY)
  37. {
  38. ANKI_ASSERT(0);
  39. }
  40. }
  41. if(out == nullptr)
  42. {
  43. // Create a new one
  44. out = m_alloc.newInstance<MicroFence>(this);
  45. }
  46. ANKI_ASSERT(out->m_refcount.getNonAtomically() == 0);
  47. return out;
  48. }
  49. void FenceFactory::deleteFence(MicroFence* fence)
  50. {
  51. ANKI_ASSERT(fence);
  52. LockGuard<Mutex> lock(m_mtx);
  53. if(m_fences.getSize() <= m_fenceCount)
  54. {
  55. // Grow storage
  56. m_fences.resize(m_alloc, max<U32>(1, m_fences.getSize() * 2));
  57. }
  58. m_fences[m_fenceCount] = fence;
  59. ++m_fenceCount;
  60. }
  61. } // end namespace anki