Buffer.h 3.2 KB

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