Buffer.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /// Flush the buffer from the CPU caches. Call it to make the buffer memory available to the GPU.
  65. /// @param offset The starting offset.
  66. /// @param range The range to map or MAX_PTR_SIZE to map until the end.
  67. void flush(PtrSize offset, PtrSize range) const;
  68. /// Invalidate the buffer from the CPU caches. Call it to ready the buffer to see GPU updates.
  69. /// @param offset The starting offset.
  70. /// @param range The range to map or MAX_PTR_SIZE to map until the end.
  71. void invalidate(PtrSize offset, PtrSize range) const;
  72. /// Convenience map method.
  73. /// @param offset The starting offset.
  74. /// @param elementCount The number of T element sto map.
  75. /// @param access The access to the buffer.
  76. /// @return The array that was mapped.
  77. template<typename T>
  78. WeakArray<T, PtrSize> map(PtrSize offset, PtrSize elementCount, BufferMapAccessBit access)
  79. {
  80. return WeakArray<T, PtrSize>(static_cast<T*>(map(offset, sizeof(T) * elementCount, access)), elementCount);
  81. }
  82. /// Unmap the buffer.
  83. void unmap();
  84. /// Get the GPU adress of the buffer.
  85. U64 getGpuAddress() const
  86. {
  87. ANKI_ASSERT(m_gpuAddress);
  88. return m_gpuAddress;
  89. }
  90. protected:
  91. PtrSize m_size = 0;
  92. BufferUsageBit m_usage = BufferUsageBit::NONE;
  93. BufferMapAccessBit m_access = BufferMapAccessBit::NONE;
  94. U64 m_gpuAddress = 0;
  95. /// Construct.
  96. Buffer(GrManager* manager, CString name)
  97. : GrObject(manager, CLASS_TYPE, name)
  98. {
  99. }
  100. /// Destroy.
  101. ~Buffer()
  102. {
  103. }
  104. private:
  105. /// Allocate and initialize a new instance.
  106. static ANKI_USE_RESULT Buffer* newInstance(GrManager* manager, const BufferInitInfo& init);
  107. };
  108. /// @}
  109. } // end namespace anki