BufferObject.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #ifndef ANKI_GL_BUFFER_OBJECT_H
  2. #define ANKI_GL_BUFFER_OBJECT_H
  3. #include "anki/gl/Ogl.h"
  4. #include "anki/util/Assert.h"
  5. #include "anki/util/NonCopyable.h"
  6. #include "anki/util/StdTypes.h"
  7. namespace anki {
  8. /// @addtogroup OpenGL
  9. /// @{
  10. /// A wrapper for OpenGL buffer objects (vertex arrays, texture buffers etc)
  11. /// to prevent us from making idiotic errors
  12. class BufferObject: public NonCopyable
  13. {
  14. public:
  15. /// @name Constructors/Destructor
  16. /// @{
  17. /// Default
  18. BufferObject()
  19. {}
  20. /// Move
  21. BufferObject(BufferObject&& b)
  22. : glId(b.glId), target(b.target), usage(b.usage),
  23. sizeInBytes(b.sizeInBytes)
  24. #if ANKI_DEBUG
  25. , mapped(b.mapped)
  26. #endif
  27. {
  28. b.glId = 0;
  29. }
  30. /// @see create
  31. BufferObject(GLenum target, U32 sizeInBytes,
  32. const void* dataPtr, GLenum usage)
  33. {
  34. create(target, sizeInBytes, dataPtr, usage);
  35. }
  36. /// It deletes the BO
  37. ~BufferObject();
  38. /// @}
  39. /// @name Accessors
  40. /// @{
  41. GLuint getGlId() const
  42. {
  43. ANKI_ASSERT(isCreated());
  44. return glId;
  45. }
  46. GLenum getBufferTarget() const
  47. {
  48. ANKI_ASSERT(isCreated());
  49. return target;
  50. }
  51. GLenum getBufferUsage() const
  52. {
  53. ANKI_ASSERT(isCreated());
  54. return usage;
  55. }
  56. U32 getSizeInBytes() const
  57. {
  58. ANKI_ASSERT(isCreated());
  59. return sizeInBytes;
  60. }
  61. /// @}
  62. /// Bind BO
  63. void bind() const
  64. {
  65. ANKI_ASSERT(isCreated());
  66. glBindBuffer(target, glId);
  67. }
  68. /// Unbind BO
  69. void unbind() const
  70. {
  71. ANKI_ASSERT(isCreated());
  72. glBindBuffer(target, 0);
  73. }
  74. /// Creates a new BO with the given parameters and checks if everything
  75. /// went OK. Throws exception if fails
  76. /// @param target Depends on the BO
  77. /// @param sizeInBytes The size of the buffer that we will allocate in bytes
  78. /// @param dataPtr Points to the data buffer to copy to the VGA memory.
  79. /// Put NULL if you want just to allocate memory
  80. /// @param usage It should be: GL_STREAM_DRAW or GL_STATIC_DRAW or
  81. /// GL_DYNAMIC_DRAW only!!!!!!!!!
  82. /// @exception Exception
  83. void create(GLenum target, U32 sizeInBytes, const void* dataPtr,
  84. GLenum usage);
  85. /// Delete the BO
  86. void destroy()
  87. {
  88. ANKI_ASSERT(isCreated());
  89. unbind();
  90. glDeleteBuffers(1, &glId);
  91. glId = 0;
  92. }
  93. /// Write data to buffer. This means that maps the BO to local memory,
  94. /// writes the local memory and unmaps it. Throws exception if the
  95. /// given size and the BO size are not equal. It throws an exception if
  96. /// the usage is GL_STATIC_DRAW
  97. /// @param[in] buff The buffer to copy to BO
  98. void write(void* buff)
  99. {
  100. write(buff, 0, sizeInBytes);
  101. }
  102. /// The same as the other write but it maps only a subset of the data
  103. /// @param[in] buff The buffer to copy to BO
  104. /// @param[in] offset The offset
  105. /// @param[in] size The size in bytes we want to write
  106. void write(void* buff, U32 offset, U32 size);
  107. /// Map part of the buffer
  108. void* map(U32 offset, U32 length, GLuint flags);
  109. /// Map the entire buffer
  110. void* map(GLuint flags)
  111. {
  112. return map(0, sizeInBytes, flags);
  113. }
  114. /// Unmap buffer
  115. void unmap();
  116. /// If create() is run successfully this returns true
  117. Bool isCreated() const
  118. {
  119. return glId != 0;
  120. }
  121. /// Set the binding for this buffer
  122. void setBinding(GLuint binding) const
  123. {
  124. ANKI_ASSERT(target == GL_TRANSFORM_FEEDBACK_BUFFER
  125. || target == GL_UNIFORM_BUFFER);
  126. bind();
  127. glBindBufferBase(target, binding, glId);
  128. }
  129. private:
  130. GLuint glId = 0; ///< The OpenGL id of the BO
  131. /// Used in glBindBuffer(target, glId) and its for easy access so we
  132. /// wont have to query the GL driver. Its the type of the buffer eg
  133. /// GL_TEXTURE_BUFFER or GL_ELEMENT_ARRAY_BUFFER etc
  134. GLenum target;
  135. GLenum usage; ///< GL_STREAM_DRAW or GL_STATIC_DRAW or GL_DYNAMIC_DRAW
  136. U32 sizeInBytes; ///< The size of the buffer
  137. #if ANKI_DEBUG
  138. Bool mapped = false; ///< Only in debug
  139. #endif
  140. };
  141. /// @}
  142. } // end namespace anki
  143. #endif