Attempt at buffer consistency/safety and added preprocessor GL debug macros

This commit is contained in:
NaifBanana 2024-10-23 00:39:30 -05:00
parent ec8009ef86
commit 5a029929bf
5 changed files with 61 additions and 34 deletions

8
Buffers.cpp Normal file
View File

@ -0,0 +1,8 @@
#include <vector>
#include "glad/glad.h"
#include "Buffers.h"
namespace NB::BufferBindingTracker {
std::map<GLenum, unsigned int> _bound_buffers{};
}

View File

@ -83,8 +83,10 @@ std::vector<VertexAttributePointer> DrawBuffer::getLayout() const {
}
void DrawBuffer::draw() const {
_elmt_data.bind();
_shader.use();
glBindVertexArray(_VAO);
glDrawElements(_gl_primitive_type, _elmt_data.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
const Shader DrawBuffer::shader() const {
@ -167,11 +169,12 @@ void DrawBuffer::generateVertexBuffer(std::vector<unsigned char> vert_data) {
}
// TODO: Make it not static draw all the time
_vert_data.setData(vert_data);
glGenVertexArrays(1, &_VAO);
glBindVertexArray(_VAO);
_vert_data.bind();
_vert_data.setData(vert_data);
_elmt_data.bind();
for (GLuint i = 0; i < num_attrs; ++i) {
glVertexAttribPointer(
i,
@ -183,10 +186,11 @@ void DrawBuffer::generateVertexBuffer(std::vector<unsigned char> vert_data) {
);
glEnableVertexAttribArray(i);
}
_elmt_data.bind();
glBindVertexArray(0);
}
void DrawBuffer::loadElementBuffer(const std::vector<unsigned int>& elements, unsigned int offset, GLenum gl_primitive_type) {
glBindVertexArray(_VAO);
if (!_elmt_data.isInitialized()) {
std::vector<unsigned int> load_data(offset);
load_data.insert(load_data.end(), elements.begin(), elements.end());
@ -197,15 +201,19 @@ void DrawBuffer::loadElementBuffer(const std::vector<unsigned int>& elements, un
}
_elmt_data.loadData(elements, offset);
}
glBindVertexArray(0);
}
void DrawBuffer::loadVertexBuffer(const std::vector<unsigned char>& data, unsigned int offset) {
glBindVertexArray(_VAO);
if (!_vert_data.isInitialized()) {
std::vector<unsigned char> load_data(offset);
load_data.insert(load_data.end(), data.begin(), data.end());
generateVertexBuffer(load_data);
} else {
_vert_data.loadData(data, offset);
}}
}
glBindVertexArray(0);
}
}

9
grid.cpp Normal file
View File

@ -0,0 +1,9 @@
#include "Draw.h"
namespace NB{
class DrawGrid : DrawBuffer {
};
}

View File

@ -3,6 +3,7 @@
#define _NB_BUFFER
#include <exception>
#include <map>
#include <string>
#include <vector>
@ -10,6 +11,13 @@
#include <GLFW/glfw3.h>
#include "Shader.h"
static unsigned int __NB_GL_DEBUG_ERROR_CODE__;
#define __NB_GL_DEBUG_THROW__(cmd, when) while(__NB_GL_DEBUG_ERROR_CODE__=glGetError()){\
std::cout << "[GL ERROR]: " << __NB_GL_DEBUG_ERROR_CODE__;\
std::cout << " " << when << " " << cmd << "\n";\
}
#define __NB_GL_DEBUG__(cmd) __NB_GL_DEBUG_THROW__(#cmd, "before"); cmd; __NB_GL_DEBUG_THROW__(#cmd, "after");
namespace NB{
class BufferWarning : public std::runtime_error {
@ -17,6 +25,12 @@ public:
BufferWarning(const std::string& msg) : std::runtime_error(msg) {}
};
static unsigned int getBound(GLenum buffer) {
int ret;
__NB_GL_DEBUG__(glGetIntegerv(buffer, &ret));
return ret;
}
template<typename T>
class BufferManager {
public:
@ -50,9 +64,8 @@ public:
bool keepLocal() const { return _keep_local; }
std::vector<T> getDataDirect() const {
std::vector<T> ret(_size);
glBindBuffer(_buffer_type, _id);
bind();
glGetBufferSubData(_buffer_type, 0, sizeof(T)*_size, ret.data());
glBindBuffer(_buffer_type, 0);
return ret;
}
@ -63,8 +76,8 @@ public:
return getDataDirect();
}
}
unsigned int bind() const { glBindBuffer(_buffer_type, _id); return _id; }
bool isBound() const { return (getBound(_buffer_type) == _id); }
unsigned int bind() const { glBindBuffer(_buffer_type, _id); return _id;}
void unbind() const { glBindBuffer(_buffer_type, 0); }
GLenum usageType(GLenum usage) { _usage=usage; return usageType(); }
bool keepLocal(bool keep_local) {
@ -84,12 +97,11 @@ public:
if ( newData.size()+offset > _max_size ) {
throw BufferWarning("Attempting to overflow buffer of capacity " + std::to_string(_max_size) + ".");
}
glBindBuffer(_buffer_type, _id);
bind();
glBufferSubData(_buffer_type, offset*sizeof(T), sizeof(T)*newData.size(), newData.data());
if(_keep_local) {
memcpy(_data.data()+offset*sizeof(T), newData.data(), sizeof(T)*newData.size());
}
glBindBuffer(_buffer_type, 0);
}
void setData(const std::vector<T>& set_data) {
if (_keep_local) {_data = set_data;}
@ -97,7 +109,7 @@ public:
glGenBuffers(1, &_id);
_initialized = true;
}
glBindBuffer(_buffer_type, _id);
bind();
if (set_data.size() <= _max_size) {
glBufferSubData(_buffer_type, 0, sizeof(T)*_size, _data.data());
_size = _data.size();
@ -106,16 +118,14 @@ public:
_max_size = _size;
glBufferData(_buffer_type, sizeof(T)*_max_size, _data.data(), _usage);
}
glBindBuffer(_buffer_type, 0);
}
unsigned int resize(unsigned int n) {
if (_keep_local) { _data.resize(n); }
if (n > _max_size) {
_max_size = n;
if (_initialized) {
glBindBuffer(_buffer_type, _id);
bind();
glBufferData(_buffer_type, sizeof(T)*_max_size, _data.data(), _usage);
glBindBuffer(_buffer_type, 0);
}
}
_size = n;
@ -126,9 +136,8 @@ public:
if (_keep_local) {
_data.shrink_to_fit();
_size = _data.size();
glBindBuffer(_buffer_type, _id);
bind();
glBufferData(_buffer_type, _size*sizeof(T), _data.data(),_usage);
glBindBuffer(_buffer_type, 0);
_max_size = _size;
} else {
_data = getDataDirect();
@ -139,12 +148,6 @@ public:
return _size;
}
static unsigned int getBound(GLenum buffer) {
unsigned int ret;
glGetIntegerv(buffer, &ret);
return ret;
}
private:
const GLenum _buffer_type;
@ -153,7 +156,6 @@ private:
unsigned int _id, _size, _max_size;
bool _initialized;
bool _keep_local;
};

View File

@ -22,23 +22,23 @@ int main() {
{2, GL_FLOAT, false},
{3, GL_FLOAT, false}
};
NB::DrawBuffer myBuffer(attrs, myShader);
myBuffer.generateElementBuffer({0, 1, 2}, GL_TRIANGLES);
__NB_GL_DEBUG__(NB::DrawBuffer myBuffer(attrs, myShader));
__NB_GL_DEBUG__(myBuffer.generateElementBuffer({0, 1, 2, 0, 2, 3}, GL_TRIANGLES));
std::vector<float> data_float = {
-0.5, -0.5, 1.0, 0.0, 0.0,
0.5, -0.5, 0.0, 1.0, 0.0,
0.0, 0.5, 0.0, 0.0, 1.0
0.5, 0.5, 0.0, 0.0, 1.0,
-0.5, 0.5, 1.0, 0.0, 1.0
};
myBuffer.generateVertexBuffer(NB::rawDataVector(data_float));
__NB_GL_DEBUG__(myBuffer.generateVertexBuffer(NB::rawDataVector(data_float)));
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//glProvokingVertex(GL_FIRST_VERTEX_CONVENTION);
std::cout << "elmt buf to " << NB::getBound(GL_ELEMENT_ARRAY_BUFFER_BINDING) << "\n";
std::cout << "vert buf to " << NB::getBound(GL_VERTEX_ARRAY_BINDING) << "\n";
// Window loop
GLenum err;
while(!glfwWindowShouldClose(window)) {
while(err=glGetError()) {
std::cout << "[GL ERROR] " << err << "\n";
}
__NB_GL_DEBUG_THROW__("nothing", "during");
// Input checking
processInput(window);
@ -46,7 +46,7 @@ int main() {
myShader.use();
glClearColor(0.2f, 0.4f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
myBuffer.draw();
__NB_GL_DEBUG__(myBuffer.draw());
// Events and buffer swap
glfwPollEvents();