FenceFactory.cpp 1.4 KB

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