FrameGpuAllocator.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/Common.h>
  7. namespace anki
  8. {
  9. /// @addtogroup graphics
  10. /// @{
  11. /// Manages pre-allocated GPU memory for per frame usage.
  12. class FrameGpuAllocator
  13. {
  14. friend class DynamicMemorySerializeCommand;
  15. public:
  16. FrameGpuAllocator()
  17. {
  18. }
  19. FrameGpuAllocator(const FrameGpuAllocator&) = delete; // Non-copyable
  20. ~FrameGpuAllocator()
  21. {
  22. }
  23. FrameGpuAllocator& operator=(const FrameGpuAllocator&) = delete; // Non-copyable
  24. /// Initialize with pre-allocated always mapped memory.
  25. /// @param size The size of the GPU buffer.
  26. /// @param alignment The working alignment.
  27. /// @param maxAllocationSize The size in @a allocate cannot exceed maxAllocationSize.
  28. void init(PtrSize size, U32 alignment, PtrSize maxAllocationSize = MAX_PTR_SIZE);
  29. /// Allocate memory for a dynamic buffer.
  30. ANKI_USE_RESULT Error allocate(PtrSize size, PtrSize& outOffset);
  31. /// Call this at the end of the frame.
  32. /// @return The bytes that were not used. Used for statistics.
  33. PtrSize endFrame();
  34. #if ANKI_ENABLE_TRACE
  35. /// Call this before endFrame.
  36. PtrSize getUnallocatedMemorySize() const;
  37. #endif
  38. private:
  39. PtrSize m_size = 0; ///< The full size of the buffer.
  40. U32 m_alignment = 0; ///< Always work in that alignment.
  41. PtrSize m_maxAllocationSize = 0; ///< For debugging.
  42. Atomic<PtrSize> m_offset = {0};
  43. #if ANKI_ENABLE_TRACE
  44. Atomic<PtrSize> m_lastAllocatedSize = {0}; ///< For tracing.
  45. #endif
  46. U64 m_frame = 0;
  47. Bool isCreated() const
  48. {
  49. return m_size > 0;
  50. }
  51. };
  52. /// @}
  53. } // end namespace anki