Debian Codegraph @

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

raw
1 #include "GLBufferObject.h"
2 #include "GLDebug.h"
3 #include "../../../../Main.h"
4 #include "../../../tracy_opengl.h"
5
6 namespace nCine::RHI::GL
7 {
8 GLHashMap<GLBufferObjectMappingFunc::Size, GLBufferObjectMappingFunc> GLBufferObject::_boundBuffers;
9 GLuint GLBufferObject::_boundIndexBase[MaxIndexBufferRange];
10 GLBufferObject::BufferRange GLBufferObject::_boundBufferRange[MaxIndexBufferRange];
11
12 GLBufferObject::GLBufferObject(GLenum target)
13 : _glHandle(0), _target(target), _size(0), _mapped(false)
14 {
15 glGenBuffers(1, &_glHandle);
16 GL_LOG_ERRORS();
17 }
18
19 GLBufferObject::~GLBufferObject()
20 {
21 if (_boundBuffers[_target] == _glHandle)
22 Unbind();
23
24 // Scrub the indexed-binding caches, or a new buffer recycling this GL handle would be
25 // considered already bound and the actual glBindBufferBase()/glBindBufferRange() skipped
26 for (std::int32_t i = 0; i < MaxIndexBufferRange; i++) {
27 if (_boundIndexBase[i] == _glHandle) {
28 _boundIndexBase[i] = 0;
29 }
30 if (_boundBufferRange[i].glHandle == _glHandle) {
31 _boundBufferRange[i].glHandle = 0;
32 _boundBufferRange[i].offset = 0;
33 _boundBufferRange[i].ptrsize = 0;
34 }
35 }
36
37 glDeleteBuffers(1, &_glHandle);
38 GL_LOG_ERRORS();
39 }
40
41 bool GLBufferObject::Bind() const
42 {
43 if (_boundBuffers[_target] != _glHandle) {
44 glBindBuffer(_target, _glHandle);
45 GL_LOG_ERRORS();
46 _boundBuffers[_target] = _glHandle;
47 return true;
48 }
49 return false;
50 }
51
52 bool GLBufferObject::Unbind() const
53 {
54 if (_boundBuffers[_target] != 0) {
55 glBindBuffer(_target, 0);
56 GL_LOG_ERRORS();
57 _boundBuffers[_target] = 0;
58 return true;
59 }
60 return false;
61 }
62
63 void GLBufferObject::BufferData(GLsizeiptr size, const GLvoid* data, GLenum usage)
64 {
65 TracyGpuZone("glBufferData");
66 Bind();
67 glBufferData(_target, size, data, usage);
68 GL_LOG_ERRORS();
69 _size = size;
70 }
71
72 void GLBufferObject::BufferSubData(GLintptr offset, GLsizeiptr size, const GLvoid* data)
73 {
74 TracyGpuZone("glBufferSubData");
75 Bind();
76 glBufferSubData(_target, offset, size, data);
77 GL_LOG_ERRORS();
78 }
79
80 #if defined(RHI_GL_PROFILE_CORE) && !defined(DEATH_TARGET_EMSCRIPTEN) && !(defined(DEATH_TARGET_APPLE) && defined(DEATH_TARGET_ARM))
81 void GLBufferObject::BufferStorage(GLsizeiptr size, const GLvoid* data, GLbitfield flags)
82 {
83 TracyGpuZone("glBufferStorage");
84 Bind();
85 glBufferStorage(_target, size, data, flags);
86 GL_LOG_ERRORS();
87 _size = size;
88 }
89 #endif
90
91 void GLBufferObject::BindBufferBase(GLuint index)
92 {
93 #if defined(RHI_GL_PROFILE_ES2)
94 // Indexed uniform-buffer bindings (glBindBufferBase) are ES 3.0; nothing binds a UBO on the OpenGL|ES 2.0
95 // profile - the block members are pushed as loose uniforms and CommitUniformBlocks() is a no-op
96 static_cast<void>(index);
97 #else
98 DEATH_ASSERT(_target == GL_UNIFORM_BUFFER);
99 DEATH_ASSERT(index < MaxIndexBufferRange);
100
101 if (index >= MaxIndexBufferRange) {
102 glBindBufferBase(_target, index, _glHandle);
103 } else if (_boundIndexBase[index] != _glHandle) {
104 _boundBufferRange[index].glHandle = -1;
105 _boundBufferRange[index].offset = 0;
106 _boundBufferRange[index].ptrsize = 0;
107 _boundIndexBase[index] = _glHandle;
108 glBindBufferBase(_target, index, _glHandle);
109 }
110 GL_LOG_ERRORS();
111 #endif
112 }
113
114 void GLBufferObject::BindBufferRange(GLuint index, GLintptr offset, GLsizei ptrsize)
115 {
116 #if defined(RHI_GL_PROFILE_ES2)
117 // glBindBufferRange is ES 3.0; see BindBufferBase() above - no UBO is ever bound on this profile
118 static_cast<void>(index);
119 static_cast<void>(offset);
120 static_cast<void>(ptrsize);
121 #else
122 DEATH_ASSERT(_target == GL_UNIFORM_BUFFER);
123 DEATH_ASSERT(index < MaxIndexBufferRange);
124
125 if (index >= MaxIndexBufferRange) {
126 glBindBufferRange(_target, index, _glHandle, offset, ptrsize);
127 } else if (_boundBufferRange[index].glHandle != _glHandle ||
128 _boundBufferRange[index].offset != offset ||
129 _boundBufferRange[index].ptrsize != ptrsize) {
130 _boundIndexBase[index] = -1;
131 _boundBufferRange[index].glHandle = _glHandle;
132 _boundBufferRange[index].offset = offset;
133 _boundBufferRange[index].ptrsize = ptrsize;
134 glBindBufferRange(_target, index, _glHandle, offset, ptrsize);
135 }
136 GL_LOG_ERRORS();
137 #endif
138 }
139
140 void* GLBufferObject::MapBufferRange(GLintptr offset, GLsizeiptr length, GLbitfield access)
141 {
142 #if defined(RHI_GL_PROFILE_ES2)
143 // Buffer mapping (glMapBufferRange) is ES 3.0; the ES2 profile forces useBufferMapping=false and streams
144 // via glBufferSubData instead, so this is never reached
145 static_cast<void>(offset);
146 static_cast<void>(length);
147 static_cast<void>(access);
148 return nullptr;
149 #else
150 FATAL_ASSERT(_mapped == false);
151 _mapped = true;
152 Bind();
153 void* result = glMapBufferRange(_target, offset, length, access);
154 GL_LOG_ERRORS();
155 return result;
156 #endif
157 }
158
159 void GLBufferObject::FlushMappedBufferRange(GLintptr offset, GLsizeiptr length)
160 {
161 #if defined(RHI_GL_PROFILE_ES2)
162 // glFlushMappedBufferRange is ES 3.0; unreached on this profile (see MapBufferRange above)
163 static_cast<void>(offset);
164 static_cast<void>(length);
165 #else
166 FATAL_ASSERT(_mapped == true);
167 Bind();
168 glFlushMappedBufferRange(_target, offset, length);
169 GL_LOG_ERRORS();
170 #endif
171 }
172
173 GLboolean GLBufferObject::Unmap()
174 {
175 #if defined(RHI_GL_PROFILE_ES2)
176 // glUnmapBuffer's ES 3.0 signature is unreached on this profile (see MapBufferRange above)
177 return GL_TRUE;
178 #else
179 FATAL_ASSERT(_mapped == true);
180 _mapped = false;
181 Bind();
182 GLboolean result = glUnmapBuffer(_target);
183 GL_LOG_ERRORS();
184 return result;
185 #endif
186 }
187
188 #if defined(RHI_GL_PROFILE_CORE) || GL_ES_VERSION_3_2
189 void GLBufferObject::TexBuffer(GLenum internalformat)
190 {
191 FATAL_ASSERT(_target == GL_TEXTURE_BUFFER);
192 glTexBuffer(GL_TEXTURE_BUFFER, internalformat, _glHandle);
193 GL_LOG_ERRORS();
194 }
195 #endif
196
197 void GLBufferObject::SetObjectLabel(StringView label)
198 {
199 GLDebug::SetObjectLabel(GLDebug::LabelTypes::Buffer, _glHandle, label);
200 }
201
202 bool GLBufferObject::BindHandle(GLenum target, GLuint glHandle)
203 {
204 if (_boundBuffers[target] != glHandle) {
205 glBindBuffer(target, glHandle);
206 GL_LOG_ERRORS();
207 _boundBuffers[target] = glHandle;
208 return true;
209 }
210 return false;
211 }
212
213 GLuint GLBufferObject::GetBoundHandle(GLenum target)
214 {
215 return _boundBuffers[target];
216 }
217 }