GpuReadbackMemoryPool.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (C) 2009-present, 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/SegregatedListsGpuMemoryPool.h>
  8. namespace anki {
  9. /// @addtogroup core
  10. /// @{
  11. /// @memberof GpuReadbackMemoryPool
  12. class GpuReadbackMemoryAllocation
  13. {
  14. friend class GpuReadbackMemoryPool;
  15. public:
  16. GpuReadbackMemoryAllocation() = default;
  17. GpuReadbackMemoryAllocation(const GpuReadbackMemoryAllocation&) = delete;
  18. GpuReadbackMemoryAllocation(GpuReadbackMemoryAllocation&& b)
  19. {
  20. *this = std::move(b);
  21. }
  22. ~GpuReadbackMemoryAllocation();
  23. GpuReadbackMemoryAllocation& operator=(const GpuReadbackMemoryAllocation&) = delete;
  24. GpuReadbackMemoryAllocation& operator=(GpuReadbackMemoryAllocation&& b)
  25. {
  26. ANKI_ASSERT(!isValid() && "Forgot to delete");
  27. m_token = b.m_token;
  28. b.m_token = {};
  29. m_buffer = b.m_buffer;
  30. m_mappedMemory = b.m_mappedMemory;
  31. return *this;
  32. }
  33. Bool isValid() const
  34. {
  35. return m_token.m_offset != kMaxPtrSize;
  36. }
  37. /// Get offset in the Unified Geometry Buffer buffer.
  38. U32 getOffset() const
  39. {
  40. ANKI_ASSERT(isValid());
  41. return U32(m_token.m_offset);
  42. }
  43. U32 getAllocatedSize() const
  44. {
  45. ANKI_ASSERT(isValid());
  46. return U32(m_token.m_size);
  47. }
  48. Buffer& getBuffer() const
  49. {
  50. ANKI_ASSERT(isValid());
  51. return *m_buffer;
  52. }
  53. const void* getMappedMemory() const
  54. {
  55. ANKI_ASSERT(isValid());
  56. return m_mappedMemory;
  57. }
  58. private:
  59. SegregatedListsGpuMemoryPoolToken m_token;
  60. Buffer* m_buffer = nullptr;
  61. void* m_mappedMemory = nullptr;
  62. };
  63. class GpuReadbackMemoryPool : public MakeSingleton<GpuReadbackMemoryPool>
  64. {
  65. template<typename>
  66. friend class MakeSingleton;
  67. public:
  68. GpuReadbackMemoryAllocation allocate(PtrSize size);
  69. void deferredFree(GpuReadbackMemoryAllocation& allocation);
  70. void endFrame();
  71. private:
  72. SegregatedListsGpuMemoryPool m_pool;
  73. U32 m_alignment = 0;
  74. GpuReadbackMemoryPool();
  75. ~GpuReadbackMemoryPool();
  76. };
  77. inline GpuReadbackMemoryAllocation::~GpuReadbackMemoryAllocation()
  78. {
  79. GpuReadbackMemoryPool::getSingleton().deferredFree(*this);
  80. }
  81. /// @}
  82. } // end namespace anki