CSCI441 OpenGL Library 5.9.0
CS@Mines CSCI441 Computer Graphics Course Library
Loading...
Searching...
No Matches
UniformBufferObject.hpp
Go to the documentation of this file.
1
11#ifndef CSCI441_UNIFORM_BUFFER_OBJECT_HPP
12#define CSCI441_UNIFORM_BUFFER_OBJECT_HPP
13
14#include "ShaderProgram.hpp"
15
16#ifdef CSCI441_USE_GLEW
17 #include <GL/glew.h>
18#else
19 #include <glad/gl.h>
20#endif
21
22#include <cstdio>
23#include <string>
24#include <vector>
25
26//******************************************************************************
27
28namespace CSCI441 {
29
34 class [[maybe_unused]] UniformBufferObject final {
35 public:
40
46 [[maybe_unused]] UniformBufferObject(const char* UNIFORM_BLOCK_NAME, std::initializer_list<const char*> uniformNamesList);
51
60
67 [[maybe_unused]] void setupWithShaderProgram( ShaderProgram *shaderProgram, GLuint bindingPoint );
68
75 [[maybe_unused]] void copyToOffset( unsigned int offset, void* addr, size_t len );
76
83 [[maybe_unused]] void copyToBuffer( const char* UNIFORM_NAME, void* addr, size_t len );
84
88 void bindBuffer() const;
89
93 [[maybe_unused]] void bufferSubData() const;
94
95 private:
96 char* _blockName;
97 std::vector<char*> _uniformNames;
98 GLint _blockSize;
99 GLubyte* _buffer;
100 GLuint _numUniforms;
101 GLuint* _uniformIndices;
102 GLint* _uniformOffsets;
103 GLuint _ubod;
104 GLuint _bindingPoint;
105 };
106}
107
108//******************************************************************************
109
110[[maybe_unused]]
111inline CSCI441::UniformBufferObject::UniformBufferObject(const char* UNIFORM_BLOCK_NAME, std::initializer_list<const char*> uniformNamesList) {
112 _blockName = (char*)UNIFORM_BLOCK_NAME;
113
114 _numUniforms = uniformNamesList.size();
115
116 for(const auto &uniformName : uniformNamesList ) {
117 char* un = (char*)malloc(strlen(uniformName) * sizeof(char));
118 strcpy(un, uniformName);
119 _uniformNames.push_back(un);
120 }
121
122 _uniformIndices = (GLuint*)malloc(_numUniforms * sizeof(GLuint));
123 _uniformOffsets = (GLint*)malloc(_numUniforms * sizeof(GLint));
124
125 _blockSize = 0;
126 _buffer = nullptr;
127 _bindingPoint = 0;
128 _ubod = 0;
129}
130
132 glDeleteBuffers(1, &_ubod);
133
134 for(GLuint i = 0; i < _numUniforms; i++) {
135 free(_uniformNames[i]);
136 }
137 free(_uniformIndices);
138 free(_uniformOffsets);
139 free(_buffer);
140}
141
142[[maybe_unused]]
143inline void CSCI441::UniformBufferObject::setupWithShaderProgram( ShaderProgram *shaderProgram, GLuint bindingPoint ) {
144 _blockSize = shaderProgram->getUniformBlockSize( _blockName );
145 _buffer = (GLubyte*)malloc( _blockSize );
146
147 glGetUniformIndices(shaderProgram->getShaderProgramHandle(), _numUniforms, &_uniformNames[0], _uniformIndices);
148 glGetActiveUniformsiv(shaderProgram->getShaderProgramHandle(), _numUniforms, _uniformIndices, GL_UNIFORM_OFFSET, _uniformOffsets);
149
150 glGenBuffers(1, &_ubod);
151 bindBuffer();
152 glBufferData(GL_UNIFORM_BUFFER, _blockSize, nullptr, GL_DYNAMIC_DRAW);
153
154 _bindingPoint = bindingPoint;
155 glBindBufferBase(GL_UNIFORM_BUFFER, _bindingPoint, _ubod);
156 shaderProgram->setUniformBlockBinding(_blockName, _bindingPoint);
157}
158
159[[maybe_unused]]
160inline void CSCI441::UniformBufferObject::copyToOffset( unsigned int offset, void* addr, size_t len ) {
161 if(offset < _numUniforms) {
162 memcpy(_buffer + _uniformOffsets[offset], addr, len);
163 } else {
164 fprintf(stderr, "[ERROR]: Offset %d exceeds size of Uniform Block %s which is %d\n", offset, _blockName, _numUniforms);
165 }
166}
167
168[[maybe_unused]]
169inline void CSCI441::UniformBufferObject::copyToBuffer( const char* UNIFORM_NAME, void* addr, size_t len ) {
170 bool found = false;
171 for(GLuint i = 0; i < _numUniforms; i++) {
172 if( strcmp(_uniformNames[i], UNIFORM_NAME) == 0 ) {
173 memcpy( _buffer + _uniformOffsets[i], addr, len );
174 found = true;
175 break;
176 }
177 }
178 if(!found) {
179 fprintf(stderr, "[ERROR]: Uniform Name \"%s\" not found within Uniform Block \"%s\"\n", UNIFORM_NAME, _blockName);
180 }
181}
182
184 glBindBuffer(GL_UNIFORM_BUFFER, _ubod);
185}
186
187[[maybe_unused]]
189 glBufferSubData(GL_UNIFORM_BUFFER, 0, _blockSize, _buffer);
190}
191
192#endif //CSCI441_UNIFORM_BUFFER_OBJECT_HPP
Class to work with OpenGL 4.0+ Shaders.
Handles registration and compilation of Shaders.
Definition: ShaderProgram.hpp:35
virtual GLuint getShaderProgramHandle() const final
Returns the handle for this shader program.
Definition: ShaderProgram.hpp:1417
virtual void setUniformBlockBinding(const char *uniformBlockName, GLuint binding) const final
Set the binding point for the given uniform block in this shader program.
Definition: ShaderProgram.hpp:1293
virtual GLint getUniformBlockSize(const char *uniformBlockName) const final
Returns the size of the given uniform block in this shader program.
Definition: ShaderProgram.hpp:1242
Storage of UBO related data.
Definition: UniformBufferObject.hpp:34
void bindBuffer() const
binds UBO object to UBO buffer
Definition: UniformBufferObject.hpp:183
~UniformBufferObject()
Deletes the UBO from the GPU and frees all memory on the CPU.
Definition: UniformBufferObject.hpp:131
UniformBufferObject & operator=(const UniformBufferObject &)=delete
do not allow UBOs to be copied
void copyToOffset(unsigned int offset, void *addr, size_t len)
copies the value pointed to by addr to the corresponding location within the UBO as denoted by the of...
Definition: UniformBufferObject.hpp:160
void copyToBuffer(const char *UNIFORM_NAME, void *addr, size_t len)
copies the value pointed to by addr to the corresponding location within the UBO as denoted by the un...
Definition: UniformBufferObject.hpp:169
UniformBufferObject(const UniformBufferObject &)=delete
do not allow UBOs to be copied
void setupWithShaderProgram(ShaderProgram *shaderProgram, GLuint bindingPoint)
creates the UBO and allocates memory on both the CPU & GPU. binds the UBO and the uniform block for t...
Definition: UniformBufferObject.hpp:143
void bufferSubData() const
transfers UBO data to UBO buffer
Definition: UniformBufferObject.hpp:188
CSCI441 Helper Functions for OpenGL.
Definition: ArcballCam.hpp:17