BufferObject.h 3.5 KB

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