Debian Codegraph @

Sources/nCine/Graphics/RHI/GL/GLBufferObject.h

raw
1 #pragma once
2
3 #include "GLHashMap.h"
4 #include "../RhiTypes.h"
5
6 #include <Containers/StringView.h>
7
8 using namespace Death::Containers;
9
10 namespace nCine
11 {
12 class RenderVaoPool;
13 }
14
15 namespace nCine::RHI::GL
16 {
17 /**
18 @brief Wraps an OpenGL buffer object
19
20 Manages the lifetime of a single OpenGL buffer object bound to a given target (e.g., a VBO, IBO
21 or UBO). Binding is cached per target so that redundant `glBindBuffer()` calls are skipped, and
22 indexed binding state (base/range) is tracked for uniform buffers. Provides data upload, immutable
23 storage allocation and persistent mapping.
24 */
25 class GLBufferObject
26 {
27 friend class nCine::RenderVaoPool;
28
29 public:
30 explicit GLBufferObject(GLenum target);
31 /** @brief Creates a buffer object for the specified backend-neutral binding target */
32 explicit GLBufferObject(BufferTarget target)
33 : GLBufferObject(static_cast<GLenum>(target)) {}
34 ~GLBufferObject();
35
36 /** @brief Returns the OpenGL handle of the buffer object */
37 inline GLuint GetGLHandle() const {
38 return _glHandle;
39 }
40 /** @brief Returns the target this buffer is bound to (e.g., `GL_ARRAY_BUFFER`) */
41 inline GLenum GetTarget() const {
42 return _target;
43 }
44 /** @brief Returns the size in bytes of the data store, as last set by @ref BufferData() or @ref BufferStorage() */
45 inline GLsizeiptr GetSize() const {
46 return _size;
47 }
48
49 /**
50 * @brief Binds the buffer to its target
51 *
52 * @return `true` if a `glBindBuffer()` call was issued, `false` if it was already bound
53 */
54 bool Bind() const;
55 /**
56 * @brief Unbinds any buffer from this object's target
57 *
58 * @return `true` if a `glBindBuffer()` call was issued, `false` if nothing was bound
59 */
60 bool Unbind() const;
61
62 /** @brief Creates and initializes the data store with the given size, optional data and usage hint */
63 void BufferData(GLsizeiptr size, const GLvoid* data, GLenum usage);
64 /** @brief Creates and initializes the data store with a backend-neutral usage hint */
65 inline void BufferData(GLsizeiptr size, const GLvoid* data, BufferUsage usage) {
66 BufferData(size, data, static_cast<GLenum>(usage));
67 }
68 /** @brief Updates a subset of the data store starting at the given byte offset */
69 void BufferSubData(GLintptr offset, GLsizeiptr size, const GLvoid* data);
70 #if defined(RHI_GL_PROFILE_CORE) && !defined(DEATH_TARGET_EMSCRIPTEN) && !(defined(DEATH_TARGET_APPLE) && defined(DEATH_TARGET_ARM))
71 /** @brief Allocates an immutable data store with the given size, optional data and storage flags */
72 void BufferStorage(GLsizeiptr size, const GLvoid* data, GLbitfield flags);
73 /** @brief Allocates an immutable data store with backend-neutral storage flags */
74 inline void BufferStorage(GLsizeiptr size, const GLvoid* data, MapFlags flags) {
75 BufferStorage(size, data, static_cast<GLbitfield>(flags));
76 }
77 #endif
78
79 /** @brief Binds the whole buffer to a binding point index of the (uniform) buffer target */
80 void BindBufferBase(GLuint index);
81 /** @brief Binds a byte range of the buffer to a binding point index of the (uniform) buffer target */
82 void BindBufferRange(GLuint index, GLintptr offset, GLsizei ptrsize);
83 /** @brief Maps a byte range of the buffer into client memory and returns the pointer */
84 void* MapBufferRange(GLintptr offset, GLsizeiptr length, GLbitfield access);
85 /** @brief Maps a byte range of the buffer with backend-neutral access flags */
86 inline void* MapBufferRange(GLintptr offset, GLsizeiptr length, MapFlags access) {
87 return MapBufferRange(offset, length, static_cast<GLbitfield>(access));
88 }
89 /** @brief Flushes a byte range of the currently mapped buffer */
90 void FlushMappedBufferRange(GLintptr offset, GLsizeiptr length);
91 /**
92 * @brief Unmaps the previously mapped buffer
93 *
94 * @return `GL_FALSE` if the buffer contents became corrupt while mapped
95 */
96 GLboolean Unmap();
97 #if defined(RHI_GL_PROFILE_CORE) || GL_ES_VERSION_3_2
98 /** @brief Attaches the buffer storage to the active texture as a texture buffer with the given internal format */
99 void TexBuffer(GLenum internalformat);
100 #endif
101
102 /** @brief Sets an OpenGL object label for the buffer, for debugging */
103 void SetObjectLabel(StringView label);
104
105 /** @brief Drops the cached bindings so the next binds re-apply (external code changed them) */
106 static void InvalidateCachedBindings() {
107 _boundBuffers.SetAll(~0u);
108 for (std::int32_t i = 0; i < MaxIndexBufferRange; i++) {
109 _boundIndexBase[i] = ~0u;
110 _boundBufferRange[i].glHandle = ~0u;
111 }
112 }
113
114 private:
115 static class GLHashMap<GLBufferObjectMappingFunc::Size, GLBufferObjectMappingFunc> _boundBuffers;
116
117 #ifndef DOXYGEN_GENERATING_OUTPUT
118 // Doxygen 1.12.0 outputs also private structs/unions even if it shouldn't
119 struct BufferRange
120 {
121 BufferRange()
122 : glHandle(0), offset(0), ptrsize(0) {}
123
124 GLuint glHandle;
125 GLintptr offset;
126 GLsizei ptrsize;
127 };
128 #endif
129
130 GLuint _glHandle;
131 GLenum _target;
132 GLsizeiptr _size;
133 bool _mapped;
134
135 static const std::int32_t MaxIndexBufferRange = 128;
136 /** @brief Handle bound at each base binding point index, or 0 if none */
137 static GLuint _boundIndexBase[MaxIndexBufferRange];
138 /** @brief Handle, offset and size bound at each ranged binding point index */
139 static BufferRange _boundBufferRange[MaxIndexBufferRange];
140
141 /** @brief Deleted copy constructor */
142 GLBufferObject(const GLBufferObject&) = delete;
143 /** @brief Deleted assignment operator */
144 GLBufferObject& operator=(const GLBufferObject&) = delete;
145
146 static bool BindHandle(GLenum target, GLuint glHandle);
147
148 static GLuint GetBoundHandle(GLenum target);
149 inline static void SetBoundHandle(GLenum target, GLuint glHandle) {
150 _boundBuffers[target] = glHandle;
151 }
152 };
153
154 }