GLW
Computer Graphics Structures library
UniformBuffer.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef GLW_UNIFORM_BUFFER_H
7 #define GLW_UNIFORM_BUFFER_H
8 
9 #include <cstddef>
10 #include <vector>
11 #include <memory>
12 #include <glbinding/gl/gl.h>
13 #include <glm/detail/func_common.hpp>
14 #include <Utils/Ulog.h>
15 
16 namespace glw
17 {
18 
24  template<typename T>
26  {
27  public:
28 
30  UniformBuffer();
31 
34 
36  uint32_t Size();
37 
39  void Resize(const uint32_t size);
40 
46  void Reset(const std::vector<T> &data);
47 
49  void Bind(uint32_t index);
50 
55  void SetData(const std::vector<T> &data, const uint32_t position = 0u);
56 
61  void SetData(const T &data, const uint32_t position = 0);
62 
63  private:
64  uint32_t buffer_ = 0u;
65  uint32_t actual_size_ = 0u;
66  static uint32_t GenerateBufferName();
67  };
68 
69  template <typename T>
70  using PUniformBuffer = std::shared_ptr<UniformBuffer<T>>;
71 
72 
73  template<typename T>
74  void glw::UniformBuffer<T>::Resize(const uint32_t new_size)
75  {
76  if (buffer_ == 0 || actual_size_ == new_size)
77  {
78  return;
79  }
80 
81  gl::glBindBuffer(gl::GLenum::GL_UNIFORM_BUFFER, buffer_);
82 
83  if (actual_size_ == 0u)
84  {
85  gl::glBufferData(gl::GLenum::GL_UNIFORM_BUFFER, new_size * sizeof(T), nullptr, gl::GLenum::GL_STATIC_DRAW);
86  gl::glBindBuffer(gl::GLenum::GL_UNIFORM_BUFFER, 0);
87  actual_size_ = new_size;
88  return;
89  }
90 
91  const gl::GLuint new_buffer = GenerateBufferName();
92 
93  gl::glBindBuffer(gl::GLenum::GL_COPY_WRITE_BUFFER, new_buffer);
94  gl::glBufferData(gl::GLenum::GL_COPY_WRITE_BUFFER, new_size * sizeof(T), nullptr, gl::GLenum::GL_STATIC_DRAW);
95 
96  gl::glCopyBufferSubData(gl::GLenum::GL_UNIFORM_BUFFER, gl::GLenum::GL_COPY_WRITE_BUFFER, 0, 0, (glm::min)(actual_size_, new_size));
97 
98  gl::glBindBuffer(gl::GLenum::GL_COPY_WRITE_BUFFER, 0);
99  gl::glBindBuffer(gl::GLenum::GL_UNIFORM_BUFFER, 0);
100 
101  gl::glDeleteBuffers(1, &buffer_);
102  buffer_ = new_buffer;
103  actual_size_ = new_size;
104  }
105 
106  template<typename T>
107  void glw::UniformBuffer<T>::Reset(const std::vector<T> &data)
108  {
109  if (buffer_ == 0)
110  {
111  return;
112  }
113 
114  actual_size_ = static_cast<uint32_t>(data.size());
115  gl::glBindBuffer(gl::GLenum::GL_UNIFORM_BUFFER, buffer_);
116  gl::glBufferData(gl::GLenum::GL_UNIFORM_BUFFER, actual_size_ * sizeof(T), nullptr, gl::GLenum::GL_STATIC_DRAW);
117 
118  if (!data.empty() && data.size() == actual_size_)
119  {
120  SetData(data, 0);
121  }
122  gl::glBindBuffer(gl::GLenum::GL_UNIFORM_BUFFER, 0);
123  }
124 
125  template<typename T>
127  : buffer_(GenerateBufferName())
128  {
129  }
130 
131  template<typename T>
133  {
134  gl::glDeleteBuffers(1, &buffer_);
135  }
136 
137  template<typename T>
139  {
140  return actual_size_;
141  }
142 
143  template<typename T>
144  void UniformBuffer<T>::Bind(uint32_t index)
145  {
146  gl::glBindBufferBase(gl::GLenum::GL_UNIFORM_BUFFER, index, buffer_);
147  }
148 
149  template<typename T>
150  void UniformBuffer<T>::SetData(const std::vector<T> &data, const uint32_t position)
151  {
152  if (buffer_ == 0)
153  {
154  return;
155  }
156  if (data.size() + position <= actual_size_)
157  {
158  gl::glBindBuffer(gl::GLenum::GL_UNIFORM_BUFFER, buffer_);
159  gl::glBufferSubData(gl::GLenum::GL_UNIFORM_BUFFER, position * sizeof(T), data.size() * sizeof(T), &data.front());
160  gl::glBindBuffer(gl::GLenum::GL_UNIFORM_BUFFER, 0);
161  }
162  else
163  {
164  ulog::Fail("Can not set data to uniform buffer at position " + std::to_string(position)
165  + ". Out of current memory block range.", "glw::UniformBuffer::SetData");
166  }
167  }
168 
169  template<typename T>
170  void glw::UniformBuffer<T>::SetData(const T &data, const uint32_t position)
171  {
172  if (buffer_ == 0)
173  {
174  return;
175  }
176  if (position + 1 <= actual_size_)
177  {
178  gl::glBindBuffer(gl::GLenum::GL_UNIFORM_BUFFER, buffer_);
179  gl::glBufferSubData(gl::GLenum::GL_UNIFORM_BUFFER, position * sizeof(T), sizeof(T), &data);
180  gl::glBindBuffer(gl::GLenum::GL_UNIFORM_BUFFER, 0);
181  }
182  else
183  {
184  ulog::Fail("Can not set data to uniform buffer at position " + std::to_string(position)
185  + ". Out of current memory block range.", "glw::UniformBuffer::SetData");
186  }
187  }
188 
189 
190  template<typename T>
192  {
193  uint32_t name = 0;
194  try
195  {
196  gl::glGenBuffers(1, &name);
197  if (name == 0)
198  {
199  ulog::Fail("Could not generate OpenGL uniform buffer.", "glw::UniformBuffer::ctor");
200  }
201  }
202  catch (...)
203  {
204  ulog::Crash("Uniform buffer generation have crashed. Does OpenGL context exist?", "glw::UniformBuffer::ctor");
205  }
206  return name;
207  }
208 
209 }
210 
211 #endif /* !GLW_UNIFORM_BUFFER_H */
OpenGL wrapper library namespace. Provides simplified interface for using OpenGL features.
Definition: API.h:17
void Bind(uint32_t index)
Bind this buffer onto specified shader location.
Definition: UniformBuffer.h:144
UniformBuffer()
Register new buffer to video memory.
Definition: UniformBuffer.h:126
~UniformBuffer()
Free buffer from video memory.
Definition: UniformBuffer.h:132
Object to store global variables, arrays or structures and manipulate them. Type of elements inside t...
Definition: UniformBuffer.h:25
uint32_t Size()
Get number of elements in currently allocated buffer.
Definition: UniformBuffer.h:138
void Reset(const std::vector< T > &data)
Definition: UniformBuffer.h:107
void SetData(const std::vector< T > &data, const uint32_t position=0u)
Definition: UniformBuffer.h:150
void Resize(const uint32_t size)
Allocate new buffer with specified number of elements, copies old buffer data to newly created one...
Definition: UniformBuffer.h:74