Buffer.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/GrObject.h>
  7. #include <AnKi/Util/WeakArray.h>
  8. namespace anki
  9. {
  10. /// @addtogroup graphics
  11. /// @{
  12. /// Buffer init info.
  13. /// @memberof Buffer
  14. class BufferInitInfo : public GrBaseInitInfo
  15. {
  16. public:
  17. PtrSize m_size = 0;
  18. BufferUsageBit m_usage = BufferUsageBit::NONE;
  19. BufferMapAccessBit m_mapAccess = BufferMapAccessBit::NONE;
  20. BufferInitInfo(CString name = {})
  21. : GrBaseInitInfo(name)
  22. {
  23. }
  24. BufferInitInfo(PtrSize size, BufferUsageBit usage, BufferMapAccessBit mapAccess, CString name = {})
  25. : GrBaseInitInfo(name)
  26. , m_size(size)
  27. , m_usage(usage)
  28. , m_mapAccess(mapAccess)
  29. {
  30. }
  31. Bool isValid() const
  32. {
  33. return m_size && !!m_usage;
  34. }
  35. };
  36. /// GPU buffer.
  37. class Buffer : public GrObject
  38. {
  39. ANKI_GR_OBJECT
  40. public:
  41. static constexpr GrObjectType CLASS_TYPE = GrObjectType::BUFFER;
  42. /// Return the size of the buffer.
  43. PtrSize getSize() const
  44. {
  45. ANKI_ASSERT(m_size > 0);
  46. return m_size;
  47. }
  48. /// Return the BufferUsageBit of the Buffer.
  49. BufferUsageBit getBufferUsage() const
  50. {
  51. ANKI_ASSERT(!!m_usage);
  52. return m_usage;
  53. }
  54. /// Return the BufferMapAccessBit of the Buffer.
  55. BufferMapAccessBit getMapAccess() const
  56. {
  57. return m_access;
  58. }
  59. /// Map the buffer.
  60. /// @param offset The starting offset.
  61. /// @param range The range to map or MAX_PTR_SIZE to map until the end.
  62. /// @param access The access to the buffer.
  63. void* map(PtrSize offset, PtrSize range, BufferMapAccessBit access);
  64. /// Convenience map method.
  65. /// @param offset The starting offset.
  66. /// @param elementCount The number of T element sto map.
  67. /// @param access The access to the buffer.
  68. /// @return The array that was mapped.
  69. template<typename T>
  70. WeakArray<T, PtrSize> map(PtrSize offset, PtrSize elementCount, BufferMapAccessBit access)
  71. {
  72. return WeakArray<T, PtrSize>(static_cast<T*>(map(offset, sizeof(T) * elementCount, access)), elementCount);
  73. }
  74. /// Unmap the buffer.
  75. void unmap();
  76. /// Get the GPU adress of the buffer.
  77. U64 getGpuAddress() const
  78. {
  79. ANKI_ASSERT(m_gpuAddress);
  80. return m_gpuAddress;
  81. }
  82. protected:
  83. PtrSize m_size = 0;
  84. BufferUsageBit m_usage = BufferUsageBit::NONE;
  85. BufferMapAccessBit m_access = BufferMapAccessBit::NONE;
  86. U64 m_gpuAddress = 0;
  87. /// Construct.
  88. Buffer(GrManager* manager, CString name)
  89. : GrObject(manager, CLASS_TYPE, name)
  90. {
  91. }
  92. /// Destroy.
  93. ~Buffer()
  94. {
  95. }
  96. private:
  97. /// Allocate and initialize a new instance.
  98. static ANKI_USE_RESULT Buffer* newInstance(GrManager* manager, const BufferInitInfo& init);
  99. };
  100. /// @}
  101. } // end namespace anki