-
Notifications
You must be signed in to change notification settings - Fork 3
/
UniformBufferObject.hpp
executable file
·192 lines (160 loc) · 6.5 KB
/
UniformBufferObject.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/** @file UniformBufferObject.hpp
* @brief Helper functions to work with Uniform Buffer Objects
* @author Dr. Jeffrey Paone
*
* @copyright MIT License Copyright (c) 2021 Dr. Jeffrey Paone
*
* These functions, classes, and constants help minimize common
* code that needs to be written.
*/
#ifndef CSCI441_UNIFORM_BUFFER_OBJECT_HPP
#define CSCI441_UNIFORM_BUFFER_OBJECT_HPP
#include "ShaderProgram.hpp"
#ifdef CSCI441_USE_GLEW
#include <GL/glew.h>
#else
#include <glad/gl.h>
#endif
#include <cstdio>
#include <string>
#include <vector>
//******************************************************************************
namespace CSCI441 {
/**
* @class UniformBufferObject
* @brief Storage of UBO related data
*/
class [[maybe_unused]] UniformBufferObject final {
public:
/**
* @note must use `UniformBufferObject(const char*, std::initializer_list<const char*>
*/
UniformBufferObject() = delete;
/**
* @brief Initializes the UniformBufferObject object
* @param UNIFORM_BLOCK_NAME name of the uniform block
* @param uniformNamesList list of names that makeup the uniform block components
*/
[[maybe_unused]] UniformBufferObject(const char* UNIFORM_BLOCK_NAME, std::initializer_list<const char*> uniformNamesList);
/**
* @brief Deletes the UBO from the GPU and frees all memory on the CPU
*/
~UniformBufferObject();
/**
* @brief do not allow UBOs to be copied
*/
UniformBufferObject(const UniformBufferObject&) = delete;
/**
* @brief do not allow UBOs to be copied
*/
UniformBufferObject& operator=(const UniformBufferObject&) = delete;
/**
* @brief creates the UBO and allocates memory on both the CPU & GPU. binds the UBO and the
* uniform block for the provided ShaderProgram to the same binding point.
* @param shaderProgram ShaderProgram object that utilizes the uniformBlock
* @param bindingPoint Binding point to bind the UBO and ShaderProgram Uniform Block to
*/
[[maybe_unused]] void setupWithShaderProgram( ShaderProgram *shaderProgram, GLuint bindingPoint );
/**
* @brief copies the value pointed to by addr to the corresponding location within the UBO as denoted by the offset
* @param offset UBO offset to copy value to
* @param addr starting address of source to copy from
* @param len length of buffer to copy
*/
[[maybe_unused]] void copyToOffset( unsigned int offset, void* addr, size_t len );
/**
* @brief copies the value pointed to by addr to the corresponding location within the UBO as denoted by the uniform name
* @param UNIFORM_NAME name of the uniform within the block to copy value to
* @param addr starting address of source to copy from
* @param len length of buffer to copy
*/
[[maybe_unused]] void copyToBuffer( const char* UNIFORM_NAME, void* addr, size_t len );
/**
* @brief binds UBO object to UBO buffer
*/
void bindBuffer() const;
/**
* @brief transfers UBO data to UBO buffer
*/
[[maybe_unused]] void bufferSubData() const;
private:
char* _blockName;
std::vector<char*> _uniformNames;
GLint _blockSize;
GLubyte* _buffer;
GLuint _numUniforms;
GLuint* _uniformIndices;
GLint* _uniformOffsets;
GLuint _ubod;
GLuint _bindingPoint;
};
}
//******************************************************************************
[[maybe_unused]]
inline CSCI441::UniformBufferObject::UniformBufferObject(const char* UNIFORM_BLOCK_NAME, std::initializer_list<const char*> uniformNamesList) {
_blockName = (char*)UNIFORM_BLOCK_NAME;
_numUniforms = uniformNamesList.size();
for(const auto &uniformName : uniformNamesList ) {
char* un = (char*)malloc(strlen(uniformName) * sizeof(char));
strcpy(un, uniformName);
_uniformNames.push_back(un);
}
_uniformIndices = (GLuint*)malloc(_numUniforms * sizeof(GLuint));
_uniformOffsets = (GLint*)malloc(_numUniforms * sizeof(GLint));
_blockSize = 0;
_buffer = nullptr;
_bindingPoint = 0;
_ubod = 0;
}
inline CSCI441::UniformBufferObject::~UniformBufferObject() {
glDeleteBuffers(1, &_ubod);
for(GLuint i = 0; i < _numUniforms; i++) {
free(_uniformNames[i]);
}
free(_uniformIndices);
free(_uniformOffsets);
free(_buffer);
}
[[maybe_unused]]
inline void CSCI441::UniformBufferObject::setupWithShaderProgram( ShaderProgram *shaderProgram, GLuint bindingPoint ) {
_blockSize = shaderProgram->getUniformBlockSize( _blockName );
_buffer = (GLubyte*)malloc( _blockSize );
glGetUniformIndices(shaderProgram->getShaderProgramHandle(), _numUniforms, &_uniformNames[0], _uniformIndices);
glGetActiveUniformsiv(shaderProgram->getShaderProgramHandle(), _numUniforms, _uniformIndices, GL_UNIFORM_OFFSET, _uniformOffsets);
glGenBuffers(1, &_ubod);
bindBuffer();
glBufferData(GL_UNIFORM_BUFFER, _blockSize, nullptr, GL_DYNAMIC_DRAW);
_bindingPoint = bindingPoint;
glBindBufferBase(GL_UNIFORM_BUFFER, _bindingPoint, _ubod);
shaderProgram->setUniformBlockBinding(_blockName, _bindingPoint);
}
[[maybe_unused]]
inline void CSCI441::UniformBufferObject::copyToOffset( unsigned int offset, void* addr, size_t len ) {
if(offset < _numUniforms) {
memcpy(_buffer + _uniformOffsets[offset], addr, len);
} else {
fprintf(stderr, "[ERROR]: Offset %d exceeds size of Uniform Block %s which is %d\n", offset, _blockName, _numUniforms);
}
}
[[maybe_unused]]
inline void CSCI441::UniformBufferObject::copyToBuffer( const char* UNIFORM_NAME, void* addr, size_t len ) {
bool found = false;
for(GLuint i = 0; i < _numUniforms; i++) {
if( strcmp(_uniformNames[i], UNIFORM_NAME) == 0 ) {
memcpy( _buffer + _uniformOffsets[i], addr, len );
found = true;
break;
}
}
if(!found) {
fprintf(stderr, "[ERROR]: Uniform Name \"%s\" not found within Uniform Block \"%s\"\n", UNIFORM_NAME, _blockName);
}
}
inline void CSCI441::UniformBufferObject::bindBuffer() const {
glBindBuffer(GL_UNIFORM_BUFFER, _ubod);
}
[[maybe_unused]]
inline void CSCI441::UniformBufferObject::bufferSubData() const {
glBufferSubData(GL_UNIFORM_BUFFER, 0, _blockSize, _buffer);
}
#endif //CSCI441_UNIFORM_BUFFER_OBJECT_HPP