GpuVisibleTransientMemoryPool.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright (C) 2009-2023, 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/Core/Common.h>
  7. #include <AnKi/Gr/Utils/StackGpuMemoryPool.h>
  8. #include <AnKi/Gr/GrManager.h>
  9. namespace anki {
  10. /// @addtogroup core
  11. /// @{
  12. /// @memberof GpuVisibleTransientMemoryPool
  13. class GpuVisibleTransientMemoryAllocation
  14. {
  15. public:
  16. Buffer* m_buffer = nullptr;
  17. PtrSize m_offset = kMaxPtrSize;
  18. PtrSize m_size = 0;
  19. };
  20. /// GPU only transient memory. Used for temporary allocations. Allocations will get reset after each frame.
  21. class GpuVisibleTransientMemoryPool : public MakeSingleton<GpuVisibleTransientMemoryPool>
  22. {
  23. template<typename>
  24. friend class MakeSingleton;
  25. public:
  26. GpuVisibleTransientMemoryAllocation allocate(PtrSize size)
  27. {
  28. GpuVisibleTransientMemoryAllocation out;
  29. m_pool.allocate(size, out.m_offset, out.m_buffer);
  30. out.m_size = size;
  31. return out;
  32. }
  33. void endFrame()
  34. {
  35. if(m_frame == 0)
  36. {
  37. m_pool.reset();
  38. }
  39. m_frame = (m_frame + 1) % kMaxFramesInFlight;
  40. }
  41. private:
  42. StackGpuMemoryPool m_pool;
  43. U32 m_frame = 0;
  44. GpuVisibleTransientMemoryPool()
  45. {
  46. U32 alignment = GrManager::getSingleton().getDeviceCapabilities().m_uniformBufferBindOffsetAlignment;
  47. alignment = max(alignment, GrManager::getSingleton().getDeviceCapabilities().m_storageBufferBindOffsetAlignment);
  48. alignment = max(alignment, GrManager::getSingleton().getDeviceCapabilities().m_sbtRecordAlignment);
  49. const BufferUsageBit buffUsage =
  50. BufferUsageBit::kAllUniform | BufferUsageBit::kAllStorage | BufferUsageBit::kIndirectDraw | BufferUsageBit::kVertex;
  51. m_pool.init(10_MB, 2.0, 0, alignment, buffUsage, BufferMapAccessBit::kNone, true, "GpuVisibleTransientMemoryPool");
  52. }
  53. ~GpuVisibleTransientMemoryPool() = default;
  54. };
  55. /// @}
  56. } // end namespace anki