Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
bufferobject.hpp
Go to the documentation of this file.
1 
23 #ifndef __BUFFEROBJECT__
24 #define __BUFFEROBJECT__
25 
26 #include "tucano/shader.hpp"
27 #include "tucano/texture.hpp"
28 #include <Eigen/Dense>
29 #include <vector>
30 #include <iostream>
31 #include <limits>
32 #include <GL/glew.h>
33 
34 namespace Tucano
35 {
36 
43 template <class T> class BufferObject {
44 
45 protected:
46 
50  virtual void create(void)
51  {
52  // declare and generate a buffer object name
53  glGenBuffers(1, &buffer_id);
54  bind();
55  // define its initial storage capacity
56  glBufferData(buffer_type, sizeof(T) * size, NULL, GL_DYNAMIC_DRAW);
57  unbind();
58  }
59 
61  GLuint buffer_id;
62 
64  GLenum buffer_type;
65 
67  int size;
68 
71 
72 public:
73 
79  BufferObject (int s, GLenum buftype) : buffer_id(0), buffer_type(buftype), size(s)
80  {
81  create();
82  clear();
83  }
84 
88  virtual ~BufferObject (void)
89  {}
90 
95  GLuint getBufferID (void) {
96  return buffer_id;
97  }
98 
102  virtual void bind (void)
103  {
104  glBindBuffer(buffer_type, buffer_id);
105  }
106 
111  void bindBase (int index)
112  {
113  binding_point = index;
114  glBindBufferBase(buffer_type, binding_point, buffer_id);
115  }
116 
120  void unbindBase (void) {
121  glBindBufferBase(buffer_type, binding_point, 0);
122  binding_point = -1;
123  }
124 
128  virtual void unbind(void)
129  {
130  glBindBuffer(buffer_type, 0);
131  }
132 
136  virtual void clear (void)
137  {
138  // declare a pointer to hold the values in the buffer
139  T *buffer_data;
140  bind();
141  // map the buffer, userCounters will point to the buffers data
142 
143  buffer_data = (T*)glMapBufferRange(buffer_type,
144  0 ,
145  sizeof(T) * size,
146  GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_UNSYNCHRONIZED_BIT
147  );
148 
149  // set the memory to zeros, resetting the values in the buffer
150  memset(buffer_data, 0, sizeof(T) * size );
151  // unmap the buffer
152  glUnmapBuffer(buffer_type);
153  unbind();
154 
155  }
156 
163  virtual void readBuffer (T **return_values)
164  {
165  T *values = new T[size];
166  T *buffer_data;
167  bind();
168  // again we map the buffer to userCounters, but this time for read-only access
169  buffer_data = (T*)glMapBufferRange(buffer_type,
170  0,
171  sizeof(T) * size,
172  GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_UNSYNCHRONIZED_BIT
173  );
174 
175  // copy the values to other variables because...
176  for (int i = 0; i < size; ++i) {
177  values[i] = buffer_data[i];
178  }
179  *return_values = values;
180 
181  // ... as soon as we unmap the buffer the pointer buffer becomes invalid.
182  glUnmapBuffer(buffer_type);
183  unbind();
184  }
185 
189  void printBuffer (void) {
190  T *values;
191  readBuffer(&values);
192  for (int i = 0; i < size; ++i) {
193  cout << i << " : " << values[i] << endl;
194  }
195  delete [] values;
196  }
197 
202  int getSize (void)
203  {
204  return size;
205  }
206 
207 
208 
209 };
210 
214 class AtomicBuffer : public BufferObject <GLuint>
215 {
216 
217 public:
222  AtomicBuffer (int s) : BufferObject<GLuint>(s, GL_ATOMIC_COUNTER_BUFFER) {}
223 };
224 
228 class ShaderStorageBufferFloat: public BufferObject <GLfloat>
229 {
230 
231 public:
236  ShaderStorageBufferFloat (int s) : BufferObject<GLfloat>(s, GL_SHADER_STORAGE_BUFFER) {}
237 };
238 
243 {
244 
245 public:
250  ShaderStorageBufferInt (int s) : BufferObject<GLint>(s, GL_SHADER_STORAGE_BUFFER) {}
251 };
252 
253 }
254 
255 #endif
BufferObject(int s, GLenum buftype)
Definition: bufferobject.hpp:79
int binding_point
Binding point for this buffer.
Definition: bufferobject.hpp:70
An Atomic Buffer object (inherited from BufferObject).
Definition: bufferobject.hpp:214
The buffer object of thype ShaderStorageBuffer with Float elements.
Definition: bufferobject.hpp:228
Definition: bufferobject.hpp:34
virtual ~BufferObject(void)
Default Destructor.
Definition: bufferobject.hpp:88
ShaderStorageBufferInt(int s)
Integer Shader Storage Buffer constructor.
Definition: bufferobject.hpp:250
void unbindBase(void)
Unbinds buffer from binding point.
Definition: bufferobject.hpp:120
virtual void clear(void)
Clears all values (sets to zero).
Definition: bufferobject.hpp:136
virtual void readBuffer(T **return_values)
Reads a GPU buffer and stores it in a CPU array.
Definition: bufferobject.hpp:163
virtual void unbind(void)
Unbinds the buffer object.
Definition: bufferobject.hpp:128
The buffer object of thype ShaderStorageBuffer with Integer elements.
Definition: bufferobject.hpp:242
virtual void bind(void)
Binds buffer object.
Definition: bufferobject.hpp:102
ShaderStorageBufferFloat(int s)
Float Shader Storage Buffer constructor.
Definition: bufferobject.hpp:236
void bindBase(int index)
Binds buffer to a specific binding point.
Definition: bufferobject.hpp:111
GLuint buffer_id
The handle of the Buffer Object.
Definition: bufferobject.hpp:61
GLuint getBufferID(void)
Returns The id of the buffer (handle).
Definition: bufferobject.hpp:95
virtual void create(void)
Creates the Buffer Object.
Definition: bufferobject.hpp:50
GLenum buffer_type
Type of buffer.
Definition: bufferobject.hpp:64
int getSize(void)
Returns the size of the buffer, number of elements.
Definition: bufferobject.hpp:202
A buffer object (i.e. ShaderStorageBuffer).
Definition: bufferobject.hpp:43
AtomicBuffer(int s)
Atomic Buffer constructor.
Definition: bufferobject.hpp:222
void printBuffer(void)
Prints the content of a GPU. Usually used for debugging.
Definition: bufferobject.hpp:189
int size
Buffer dimension (number of elements).
Definition: bufferobject.hpp:67