GpuReadbackMemoryPool.h 2.4 KB

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