FrameGpuAllocator.h 1.6 KB

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