GlobeEngine
Buffer.h
Go to the documentation of this file.
1 
12 #ifndef GlobeEngine_Buffer_h
13 #define GlobeEngine_Buffer_h
14 
15 #include "OpenGL_Includes.h"
16 
17 namespace ge {
18  template <int TYPE> class Buffer
19  {
20  public:
21  Buffer() : bufferHandle(0) {
22  isImmutable = false;
23  }
24 
25  ~Buffer() {
26  clear();
27  }
28 
29  void clear()
30  {
31  if (this->bufferHandle != 0) {
32  glBindBuffer(TYPE, this->bufferHandle);
33  if (!this->isImmutable) {
34  glBufferData(TYPE, 0, nullptr, GL_STATIC_DRAW);
35  }
36  glDeleteBuffers( 1, &bufferHandle );
37  glBindBuffer(TYPE, 0); // should be done automatically after delete
38  bufferHandle = 0;
39  }
40  }
41 
43  bool res = false;
44  if (this->bufferHandle == 0) {
45  glGenBuffers(1, &this->bufferHandle);
46  res = true;
47  }
48  this->bindBuffer();
49  return res;
50  }
51 
52  void copyToBufferCustom(GLsizeiptr _size, const GLvoid* _data, GLenum _access = GL_STATIC_DRAW) {
53  this->generateHandle();
54  glBufferData(TYPE, _size, _data, _access);
55  }
56 
57  template <typename T>
58  void copyToBufferByData(GLuint _count, const T* _data, GLenum _access = GL_STATIC_DRAW) {
59  this->generateHandle();
60  glBufferData(TYPE, _count * sizeof(T), _data, _access);
61  }
62 
63  void copyToBufferUInt(GLuint _size, const GLvoid* _data, GLenum _access = GL_STATIC_DRAW) {
64  this->generateHandle();
65  glBufferData(TYPE, _size * sizeof(GLuint), _data, _access);
66  }
67 
68 #ifndef GENGINE_GL_ES
69  template <typename T>
70  T* mapBuffer(GLuint _count,
71  GLenum _usage = GL_STATIC_DRAW, GLbitfield _mode = GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT) {
72  this->generateHandle();
73  glBufferData(TYPE, _count * sizeof(T), nullptr, _usage);
74  return reinterpret_cast<T*>(glMapBufferRange(TYPE, 0, _count * sizeof(T), _mode));
75  }
76 
77 #ifndef GENGINE_GL_BELOW_410
78  template <typename T>
79  T* mapBufferStorage(GLuint _count,
80  GLbitfield _storageMode = GL_MAP_WRITE_BIT,
81  GLbitfield _mode = GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT) {
82  if (this->generateHandle()){
83  // darf nur einmal aufgerufen werden auf ein buffer objekt
84  glBufferStorage(TYPE, _count * sizeof(T), nullptr, _storageMode);
85  this->isImmutable = true;
86  }
87  return reinterpret_cast<T*>(glMapBufferRange(TYPE, 0, _count * sizeof(T), _mode));
88  }
89 #endif
90 
91  void unmapBuffer() {
92  glUnmapBuffer(TYPE);
93  }
94 #endif
95 
96  void bindBuffer() {
97  glBindBuffer(TYPE, this->bufferHandle);
98  };
99 
100  void unbindBuffer(){
101  glBindBuffer(TYPE, 0);
102  }
103 
104  GLuint getBufferHandle(unsigned int _layerid){
105  return this->bufferHandle;
106  }
107 
108  private:
109  GLuint bufferHandle;
110  /* it this buffer was allocated with a BufferStorage command */
111  bool isImmutable;
112  };
113 
116 #ifndef GENGINE_GL_ES
118  typedef Buffer<GL_PIXEL_UNPACK_BUFFER> PixelUnpackBuffer; // Texture data upload
119  typedef Buffer<GL_PIXEL_PACK_BUFFER> PixelPackBuffer; // Texture data download from graphics card
120 #endif
121 
122 #ifndef GENGINE_GL_BELOW_410
125 #endif
126 
127 
128 
129 }
130 #endif
Buffer< GL_ELEMENT_ARRAY_BUFFER > IndexBuffer
Definition: Buffer.h:115
Buffer< GL_PIXEL_PACK_BUFFER > PixelPackBuffer
Definition: Buffer.h:119
void copyToBufferByData(GLuint _count, const T *_data, GLenum _access=GL_STATIC_DRAW)
Definition: Buffer.h:58
Buffer< GL_SHADER_STORAGE_BUFFER > ShaderStorageBuffer
Definition: Buffer.h:124
void clear()
Definition: Buffer.h:29
Buffer< GL_UNIFORM_BUFFER > UniformBuffer
Definition: Buffer.h:117
T * mapBufferStorage(GLuint _count, GLbitfield _storageMode=GL_MAP_WRITE_BIT, GLbitfield _mode=GL_MAP_WRITE_BIT|GL_MAP_INVALIDATE_BUFFER_BIT)
Definition: Buffer.h:79
bool generateHandle()
Definition: Buffer.h:42
void copyToBufferUInt(GLuint _size, const GLvoid *_data, GLenum _access=GL_STATIC_DRAW)
Definition: Buffer.h:63
Definition: Buffer.h:18
Buffer()
Definition: Buffer.h:21
Buffer< GL_DRAW_INDIRECT_BUFFER > DrawIndirectBuffer
Definition: Buffer.h:123
void bindBuffer()
Definition: Buffer.h:96
~Buffer()
Definition: Buffer.h:25
T * mapBuffer(GLuint _count, GLenum _usage=GL_STATIC_DRAW, GLbitfield _mode=GL_MAP_WRITE_BIT|GL_MAP_INVALIDATE_BUFFER_BIT)
Definition: Buffer.h:70
Buffer< GL_ARRAY_BUFFER > VertexBuffer
Definition: Buffer.h:114
Definition: AvalancheTrainingSimulationEngine.h:28
Buffer< GL_PIXEL_UNPACK_BUFFER > PixelUnpackBuffer
Definition: Buffer.h:118
void unmapBuffer()
Definition: Buffer.h:91
void copyToBufferCustom(GLsizeiptr _size, const GLvoid *_data, GLenum _access=GL_STATIC_DRAW)
Definition: Buffer.h:52
void unbindBuffer()
Definition: Buffer.h:100
GLuint getBufferHandle(unsigned int _layerid)
Definition: Buffer.h:104