6 #ifndef GLW_UNIFORM_BUFFER_H 7 #define GLW_UNIFORM_BUFFER_H 12 #include <glbinding/gl/gl.h> 13 #include <glm/detail/func_common.hpp> 14 #include <Utils/Ulog.h> 39 void Resize(
const uint32_t size);
46 void Reset(
const std::vector<T> &data);
49 void Bind(uint32_t index);
55 void SetData(
const std::vector<T> &data,
const uint32_t position = 0u);
61 void SetData(
const T &data,
const uint32_t position = 0);
64 uint32_t buffer_ = 0u;
65 uint32_t actual_size_ = 0u;
66 static uint32_t GenerateBufferName();
70 using PUniformBuffer = std::shared_ptr<UniformBuffer<T>>;
76 if (buffer_ == 0 || actual_size_ == new_size)
81 gl::glBindBuffer(gl::GLenum::GL_UNIFORM_BUFFER, buffer_);
83 if (actual_size_ == 0u)
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;
91 const gl::GLuint new_buffer = GenerateBufferName();
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);
96 gl::glCopyBufferSubData(gl::GLenum::GL_UNIFORM_BUFFER, gl::GLenum::GL_COPY_WRITE_BUFFER, 0, 0, (glm::min)(actual_size_, new_size));
98 gl::glBindBuffer(gl::GLenum::GL_COPY_WRITE_BUFFER, 0);
99 gl::glBindBuffer(gl::GLenum::GL_UNIFORM_BUFFER, 0);
101 gl::glDeleteBuffers(1, &buffer_);
102 buffer_ = new_buffer;
103 actual_size_ = new_size;
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);
118 if (!data.empty() && data.size() == actual_size_)
122 gl::glBindBuffer(gl::GLenum::GL_UNIFORM_BUFFER, 0);
127 : buffer_(GenerateBufferName())
134 gl::glDeleteBuffers(1, &buffer_);
146 gl::glBindBufferBase(gl::GLenum::GL_UNIFORM_BUFFER, index, buffer_);
156 if (data.size() + position <= actual_size_)
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);
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");
176 if (position + 1 <= actual_size_)
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);
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");
196 gl::glGenBuffers(1, &name);
199 ulog::Fail(
"Could not generate OpenGL uniform buffer.",
"glw::UniformBuffer::ctor");
204 ulog::Crash(
"Uniform buffer generation have crashed. Does OpenGL context exist?",
"glw::UniformBuffer::ctor");
OpenGL wrapper library namespace. Provides simplified interface for using OpenGL features.
Definition: API.h:17