sgltk 0.6
Simple OpenGL Tool Kit
Loading...
Searching...
No Matches
buffer.h
1#ifndef __BUFFER_H_
2#define __BUFFER_H_
3
4#include "app.h"
5
6namespace sgltk {
7
12class Buffer {
13 GLenum usage;
14 GLenum target;
15public:
19 GLuint buffer;
20
24 unsigned int size;
25
29 unsigned int num_elements;
30
34 Buffer(GLenum target = GL_ARRAY_BUFFER) {
35 this->target = target;
36 glGenBuffers(1, &buffer);
37 }
38
39 ~Buffer() {
40 glDeleteBuffers(1, &buffer);
41 }
42
48 void bind() {
49 glBindBuffer(target, buffer);
50 }
51
56 void bind(GLenum target) {
57 this->target = target;
58 glBindBuffer(target, buffer);
59 }
60
70 void bind(GLenum target, unsigned int index) {
71 this->target = target;
72 switch(target) {
73 case GL_ATOMIC_COUNTER_BUFFER:
74 case GL_TRANSFORM_FEEDBACK_BUFFER:
75 case GL_UNIFORM_BUFFER:
76 case GL_SHADER_STORAGE_BUFFER:
77 glBindBufferBase(target, index, buffer);
78 break;
79 default:
80 glBindBuffer(target, buffer);
81 break;
82 }
83 }
84
91 void unbind() {
92 glBindBuffer(target, 0);
93 }
94
101 template <typename T>
102 void create_empty(unsigned int num_elements, GLenum usage) {
103 this->usage = usage;
104 this->size = num_elements * sizeof(T);
105 bind();
106 glBufferData(target, size, nullptr, usage);
107 unbind();
108 }
109
116 template <typename T>
117 void load(const std::vector<T> &data, GLenum usage) {
118 this->usage = usage;
119 bind();
120 size = data.size() * sizeof(T);
121 num_elements = data.size();
122
123 glBufferData(target, size, data.data(), usage);
124 unbind();
125 }
126
134 template <typename T>
135 void load(unsigned int num_elements, const T *data, GLenum usage) {
136 this->usage = usage;
137 bind();
138 size = num_elements * sizeof(T);
139
140 glBufferData(target, size, data, usage);
141 unbind();
142 }
143
154 bool store(unsigned int offset, unsigned int size, void *storage) {
155 if(offset >= this->size)
156 return false;
157
158 if(size < this->size - offset)
159 return false;
160
161 if(!storage)
162 return false;
163
164 bind(GL_COPY_READ_BUFFER);
165 glGetBufferSubData(GL_COPY_READ_BUFFER, offset, size, storage);
166 unbind();
167
168 return true;
169 }
170
179 bool copy(Buffer& source, unsigned int read_offset,
180 unsigned int write_offset, unsigned int size) {
181
182 if(read_offset >= source.size ||
183 read_offset + size >= source.size)
184 return false;
185
186 if(write_offset >= this->size ||
187 write_offset + size >= this->size)
188 return false;
189
190 source.bind(GL_COPY_READ_BUFFER);
191 bind(GL_COPY_WRITE_BUFFER);
192 glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER,
193 read_offset, write_offset, size);
194 source.unbind();
195 unbind();
196
197 return true;
198 }
199
207 bool copy(Buffer *source, unsigned int read_offset,
208 unsigned int write_offset, unsigned int size) {
209
210 if(!source)
211 return false;
212
213 if(read_offset >= source->size ||
214 read_offset + size >= source->size)
215 return false;
216
217 if(write_offset >= this->size ||
218 write_offset + size >= this->size)
219 return false;
220
221 source->bind(GL_COPY_READ_BUFFER);
222 bind(GL_COPY_WRITE_BUFFER);
223 glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER,
224 read_offset, write_offset, size);
225 source->unbind();
226 unbind();
227
228 return true;
229 }
230
239 void *map(GLenum access) {
240 bind(GL_ARRAY_BUFFER);
241 void *ptr = glMapBuffer(target, access);
242 unbind();
243 return ptr;
244 }
245
250 void unmap() {
251 bind(GL_ARRAY_BUFFER);
252 glUnmapBuffer(target);
253 unbind();
254 }
255
260 template <typename T>
261 void replace_data(const std::vector<T> &data) {
262 bind();
263
264 unsigned int new_size = data.size() * sizeof(T);
265
266 if(size == new_size) {
267 //replace the buffer data without reallocation
268 glBufferSubData(target, 0, size, data.data());
269 } else {
270 //replace the buffer data with reallocation
271 size = new_size;
272 glBufferData(target, new_size, data.data(), usage);
273 }
274 unbind();
275 }
276
282 template <typename T>
283 void replace_data(const T *data, unsigned int number_elements) {
284 bind();
285
286 unsigned int new_size = number_elements * sizeof(T);
287
288 if(size == new_size) {
289 //replace the buffer data without reallocation
290 glBufferSubData(target, 0, size, data);
291 } else {
292 //replace the buffer data with reallocation
293 size = new_size;
294 glBufferData(target, new_size, data, usage);
295 }
296 unbind();
297 }
298
304 template <typename T>
305 bool replace_partial_data(unsigned int offset, const std::vector<T>& data) {
306
307 unsigned int data_size = data.size() * sizeof(T);
308
309 if(offset + data_size > size) {
310 return false;
311 }
312
313 bind();
314 glBufferSubData(target, offset, data_size, data.data());
315 unbind();
316 return true;
317 }
318
325 template <typename T>
326 bool replace_partial_data(unsigned int offset,
327 const T *data,
328 unsigned int number_elements) {
329
330 unsigned int data_size = number_elements * sizeof(T);
331
332 if(offset + data_size > size) {
333 return false;
334 }
335
336 bind();
337 glBufferSubData(target, offset, data_size, data);
338 unbind();
339 return true;
340 }
341
342};
343
344};
345
346#endif
Manages buffer objects.
Definition buffer.h:12
GLuint buffer
The name of the buffer object.
Definition buffer.h:19
void unbind()
Unbinds the buffer object from the target it was bound to.
Definition buffer.h:91
void load(unsigned int num_elements, const T *data, GLenum usage)
Loads data into the buffer.
Definition buffer.h:135
void create_empty(unsigned int num_elements, GLenum usage)
Creates an empty buffer.
Definition buffer.h:102
bool store(unsigned int offset, unsigned int size, void *storage)
Writes the contents of the buffer object into the storage.
Definition buffer.h:154
void bind()
Binds the buffer object to the target it was previously bound to or GL_ARRAY_BUFFER if the buffer has...
Definition buffer.h:48
bool replace_partial_data(unsigned int offset, const std::vector< T > &data)
Overwrites data in the buffer starting at the specified offset.
Definition buffer.h:305
void load(const std::vector< T > &data, GLenum usage)
Loads data into the buffer.
Definition buffer.h:117
void unmap()
Releases the mapping of a buffer object's data store into the client's address space.
Definition buffer.h:250
void replace_data(const std::vector< T > &data)
Overwrites all data in a vertex buffer.
Definition buffer.h:261
bool copy(Buffer *source, unsigned int read_offset, unsigned int write_offset, unsigned int size)
Copies data from another buffer object.
Definition buffer.h:207
void * map(GLenum access)
Maps all of a buffer object's data into the client's address space.
Definition buffer.h:239
void replace_data(const T *data, unsigned int number_elements)
Overwrites all data in the buffer.
Definition buffer.h:283
void bind(GLenum target)
Binds the buffer object to a target.
Definition buffer.h:56
bool copy(Buffer &source, unsigned int read_offset, unsigned int write_offset, unsigned int size)
Copies data from another buffer object.
Definition buffer.h:179
bool replace_partial_data(unsigned int offset, const T *data, unsigned int number_elements)
Overwrites data in the buffer starting at the specified offset.
Definition buffer.h:326
void bind(GLenum target, unsigned int index)
Binds the buffer object to an indexed buffer target.
Definition buffer.h:70
unsigned int size
Size of the buffer in bytes.
Definition buffer.h:24
Buffer(GLenum target=GL_ARRAY_BUFFER)
Definition buffer.h:34
unsigned int num_elements
Number of elements contained in the buffer.
Definition buffer.h:29