GpuVisibleTransientMemoryPool.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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/StackGpuMemoryPool.h>
  8. #include <AnKi/Gr/GrManager.h>
  9. #include <AnKi/Gr/Buffer.h>
  10. namespace anki {
  11. /// @addtogroup core
  12. /// @{
  13. /// GPU only transient memory. Used for temporary allocations. Allocations will get reset after each frame.
  14. class GpuVisibleTransientMemoryPool : public MakeSingleton<GpuVisibleTransientMemoryPool>
  15. {
  16. template<typename>
  17. friend class MakeSingleton;
  18. public:
  19. BufferView allocate(PtrSize size, PtrSize alignment = 0)
  20. {
  21. alignment = (alignment == 0) ? m_alignment : alignment;
  22. PtrSize offset;
  23. Buffer* buffer;
  24. m_pool.allocate(size, alignment, offset, buffer);
  25. return BufferView(buffer, offset, size);
  26. }
  27. template<typename T>
  28. BufferView allocateStructuredBuffer(U32 count)
  29. {
  30. return allocateStructuredBuffer(count, sizeof(T));
  31. }
  32. BufferView allocateStructuredBuffer(U32 count, U32 structureSize)
  33. {
  34. return allocate(PtrSize(structureSize * count), (m_structuredBufferAlignment == kMaxU32) ? structureSize : m_structuredBufferAlignment);
  35. }
  36. void endFrame();
  37. private:
  38. StackGpuMemoryPool m_pool;
  39. U32 m_alignment = 0;
  40. U32 m_frame = 0;
  41. U32 m_structuredBufferAlignment = 0;
  42. GpuVisibleTransientMemoryPool()
  43. {
  44. m_structuredBufferAlignment = (GrManager::getSingleton().getDeviceCapabilities().m_structuredBufferNaturalAlignment)
  45. ? kMaxU32
  46. : GrManager::getSingleton().getDeviceCapabilities().m_storageBufferBindOffsetAlignment;
  47. m_alignment = GrManager::getSingleton().getDeviceCapabilities().m_uniformBufferBindOffsetAlignment;
  48. m_alignment = max(m_alignment, GrManager::getSingleton().getDeviceCapabilities().m_storageBufferBindOffsetAlignment);
  49. m_alignment = max(m_alignment, GrManager::getSingleton().getDeviceCapabilities().m_sbtRecordAlignment);
  50. m_alignment = max(m_alignment, GrManager::getSingleton().getDeviceCapabilities().m_accelerationStructureBuildScratchOffsetAlignment);
  51. BufferUsageBit buffUsage = BufferUsageBit::kAllConstant | BufferUsageBit::kAllUav | BufferUsageBit::kAllSrv | BufferUsageBit::kIndirectDraw
  52. | BufferUsageBit::kIndirectCompute | BufferUsageBit::kVertex | BufferUsageBit::kAllCopy;
  53. if(GrManager::getSingleton().getDeviceCapabilities().m_rayTracingEnabled)
  54. {
  55. buffUsage |= (BufferUsageBit::kAccelerationStructureBuildScratch | BufferUsageBit::kAccelerationStructureBuild);
  56. }
  57. m_pool.init(10_MB, 2.0, 0, buffUsage, BufferMapAccessBit::kNone, true, "GpuVisibleTransientMemoryPool");
  58. }
  59. ~GpuVisibleTransientMemoryPool() = default;
  60. };
  61. /// @}
  62. } // end namespace anki