Attempt at buffer consistency/safety and added preprocessor GL debug macros
This commit is contained in:
parent
ec8009ef86
commit
5a029929bf
8
Buffers.cpp
Normal file
8
Buffers.cpp
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "glad/glad.h"
|
||||||
|
#include "Buffers.h"
|
||||||
|
|
||||||
|
namespace NB::BufferBindingTracker {
|
||||||
|
std::map<GLenum, unsigned int> _bound_buffers{};
|
||||||
|
}
|
||||||
18
Draw.cpp
18
Draw.cpp
@ -83,8 +83,10 @@ std::vector<VertexAttributePointer> DrawBuffer::getLayout() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DrawBuffer::draw() const {
|
void DrawBuffer::draw() const {
|
||||||
_elmt_data.bind();
|
_shader.use();
|
||||||
|
glBindVertexArray(_VAO);
|
||||||
glDrawElements(_gl_primitive_type, _elmt_data.size(), GL_UNSIGNED_INT, 0);
|
glDrawElements(_gl_primitive_type, _elmt_data.size(), GL_UNSIGNED_INT, 0);
|
||||||
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
const Shader DrawBuffer::shader() const {
|
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
|
// TODO: Make it not static draw all the time
|
||||||
_vert_data.setData(vert_data);
|
|
||||||
|
|
||||||
glGenVertexArrays(1, &_VAO);
|
glGenVertexArrays(1, &_VAO);
|
||||||
glBindVertexArray(_VAO);
|
glBindVertexArray(_VAO);
|
||||||
_vert_data.bind();
|
_vert_data.setData(vert_data);
|
||||||
|
_elmt_data.bind();
|
||||||
|
|
||||||
for (GLuint i = 0; i < num_attrs; ++i) {
|
for (GLuint i = 0; i < num_attrs; ++i) {
|
||||||
glVertexAttribPointer(
|
glVertexAttribPointer(
|
||||||
i,
|
i,
|
||||||
@ -183,10 +186,11 @@ void DrawBuffer::generateVertexBuffer(std::vector<unsigned char> vert_data) {
|
|||||||
);
|
);
|
||||||
glEnableVertexAttribArray(i);
|
glEnableVertexAttribArray(i);
|
||||||
}
|
}
|
||||||
_elmt_data.bind();
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawBuffer::loadElementBuffer(const std::vector<unsigned int>& elements, unsigned int offset, GLenum gl_primitive_type) {
|
void DrawBuffer::loadElementBuffer(const std::vector<unsigned int>& elements, unsigned int offset, GLenum gl_primitive_type) {
|
||||||
|
glBindVertexArray(_VAO);
|
||||||
if (!_elmt_data.isInitialized()) {
|
if (!_elmt_data.isInitialized()) {
|
||||||
std::vector<unsigned int> load_data(offset);
|
std::vector<unsigned int> load_data(offset);
|
||||||
load_data.insert(load_data.end(), elements.begin(), elements.end());
|
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);
|
_elmt_data.loadData(elements, offset);
|
||||||
}
|
}
|
||||||
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DrawBuffer::loadVertexBuffer(const std::vector<unsigned char>& data, unsigned int offset) {
|
void DrawBuffer::loadVertexBuffer(const std::vector<unsigned char>& data, unsigned int offset) {
|
||||||
|
glBindVertexArray(_VAO);
|
||||||
if (!_vert_data.isInitialized()) {
|
if (!_vert_data.isInitialized()) {
|
||||||
std::vector<unsigned char> load_data(offset);
|
std::vector<unsigned char> load_data(offset);
|
||||||
load_data.insert(load_data.end(), data.begin(), data.end());
|
load_data.insert(load_data.end(), data.begin(), data.end());
|
||||||
generateVertexBuffer(load_data);
|
generateVertexBuffer(load_data);
|
||||||
} else {
|
} else {
|
||||||
_vert_data.loadData(data, offset);
|
_vert_data.loadData(data, offset);
|
||||||
}}
|
}
|
||||||
|
glBindVertexArray(0);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
9
grid.cpp
Normal file
9
grid.cpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#include "Draw.h"
|
||||||
|
|
||||||
|
namespace NB{
|
||||||
|
|
||||||
|
class DrawGrid : DrawBuffer {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
@ -3,6 +3,7 @@
|
|||||||
#define _NB_BUFFER
|
#define _NB_BUFFER
|
||||||
|
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -10,6 +11,13 @@
|
|||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include "Shader.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{
|
namespace NB{
|
||||||
|
|
||||||
class BufferWarning : public std::runtime_error {
|
class BufferWarning : public std::runtime_error {
|
||||||
@ -17,6 +25,12 @@ public:
|
|||||||
BufferWarning(const std::string& msg) : std::runtime_error(msg) {}
|
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>
|
template<typename T>
|
||||||
class BufferManager {
|
class BufferManager {
|
||||||
public:
|
public:
|
||||||
@ -50,9 +64,8 @@ public:
|
|||||||
bool keepLocal() const { return _keep_local; }
|
bool keepLocal() const { return _keep_local; }
|
||||||
std::vector<T> getDataDirect() const {
|
std::vector<T> getDataDirect() const {
|
||||||
std::vector<T> ret(_size);
|
std::vector<T> ret(_size);
|
||||||
glBindBuffer(_buffer_type, _id);
|
bind();
|
||||||
glGetBufferSubData(_buffer_type, 0, sizeof(T)*_size, ret.data());
|
glGetBufferSubData(_buffer_type, 0, sizeof(T)*_size, ret.data());
|
||||||
glBindBuffer(_buffer_type, 0);
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -64,7 +77,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsigned int bind() const { glBindBuffer(_buffer_type, _id); return _id;}
|
unsigned int bind() const { glBindBuffer(_buffer_type, _id); return _id;}
|
||||||
bool isBound() const { return (getBound(_buffer_type) == _id); }
|
void unbind() const { glBindBuffer(_buffer_type, 0); }
|
||||||
|
|
||||||
GLenum usageType(GLenum usage) { _usage=usage; return usageType(); }
|
GLenum usageType(GLenum usage) { _usage=usage; return usageType(); }
|
||||||
bool keepLocal(bool keep_local) {
|
bool keepLocal(bool keep_local) {
|
||||||
@ -84,12 +97,11 @@ public:
|
|||||||
if ( newData.size()+offset > _max_size ) {
|
if ( newData.size()+offset > _max_size ) {
|
||||||
throw BufferWarning("Attempting to overflow buffer of capacity " + std::to_string(_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());
|
glBufferSubData(_buffer_type, offset*sizeof(T), sizeof(T)*newData.size(), newData.data());
|
||||||
if(_keep_local) {
|
if(_keep_local) {
|
||||||
memcpy(_data.data()+offset*sizeof(T), newData.data(), sizeof(T)*newData.size());
|
memcpy(_data.data()+offset*sizeof(T), newData.data(), sizeof(T)*newData.size());
|
||||||
}
|
}
|
||||||
glBindBuffer(_buffer_type, 0);
|
|
||||||
}
|
}
|
||||||
void setData(const std::vector<T>& set_data) {
|
void setData(const std::vector<T>& set_data) {
|
||||||
if (_keep_local) {_data = set_data;}
|
if (_keep_local) {_data = set_data;}
|
||||||
@ -97,7 +109,7 @@ public:
|
|||||||
glGenBuffers(1, &_id);
|
glGenBuffers(1, &_id);
|
||||||
_initialized = true;
|
_initialized = true;
|
||||||
}
|
}
|
||||||
glBindBuffer(_buffer_type, _id);
|
bind();
|
||||||
if (set_data.size() <= _max_size) {
|
if (set_data.size() <= _max_size) {
|
||||||
glBufferSubData(_buffer_type, 0, sizeof(T)*_size, _data.data());
|
glBufferSubData(_buffer_type, 0, sizeof(T)*_size, _data.data());
|
||||||
_size = _data.size();
|
_size = _data.size();
|
||||||
@ -106,16 +118,14 @@ public:
|
|||||||
_max_size = _size;
|
_max_size = _size;
|
||||||
glBufferData(_buffer_type, sizeof(T)*_max_size, _data.data(), _usage);
|
glBufferData(_buffer_type, sizeof(T)*_max_size, _data.data(), _usage);
|
||||||
}
|
}
|
||||||
glBindBuffer(_buffer_type, 0);
|
|
||||||
}
|
}
|
||||||
unsigned int resize(unsigned int n) {
|
unsigned int resize(unsigned int n) {
|
||||||
if (_keep_local) { _data.resize(n); }
|
if (_keep_local) { _data.resize(n); }
|
||||||
if (n > _max_size) {
|
if (n > _max_size) {
|
||||||
_max_size = n;
|
_max_size = n;
|
||||||
if (_initialized) {
|
if (_initialized) {
|
||||||
glBindBuffer(_buffer_type, _id);
|
bind();
|
||||||
glBufferData(_buffer_type, sizeof(T)*_max_size, _data.data(), _usage);
|
glBufferData(_buffer_type, sizeof(T)*_max_size, _data.data(), _usage);
|
||||||
glBindBuffer(_buffer_type, 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_size = n;
|
_size = n;
|
||||||
@ -126,9 +136,8 @@ public:
|
|||||||
if (_keep_local) {
|
if (_keep_local) {
|
||||||
_data.shrink_to_fit();
|
_data.shrink_to_fit();
|
||||||
_size = _data.size();
|
_size = _data.size();
|
||||||
glBindBuffer(_buffer_type, _id);
|
bind();
|
||||||
glBufferData(_buffer_type, _size*sizeof(T), _data.data(),_usage);
|
glBufferData(_buffer_type, _size*sizeof(T), _data.data(),_usage);
|
||||||
glBindBuffer(_buffer_type, 0);
|
|
||||||
_max_size = _size;
|
_max_size = _size;
|
||||||
} else {
|
} else {
|
||||||
_data = getDataDirect();
|
_data = getDataDirect();
|
||||||
@ -139,12 +148,6 @@ public:
|
|||||||
return _size;
|
return _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int getBound(GLenum buffer) {
|
|
||||||
unsigned int ret;
|
|
||||||
glGetIntegerv(buffer, &ret);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const GLenum _buffer_type;
|
const GLenum _buffer_type;
|
||||||
|
|
||||||
@ -153,7 +156,6 @@ private:
|
|||||||
unsigned int _id, _size, _max_size;
|
unsigned int _id, _size, _max_size;
|
||||||
bool _initialized;
|
bool _initialized;
|
||||||
bool _keep_local;
|
bool _keep_local;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
20
main.cpp
20
main.cpp
@ -22,23 +22,23 @@ int main() {
|
|||||||
{2, GL_FLOAT, false},
|
{2, GL_FLOAT, false},
|
||||||
{3, GL_FLOAT, false}
|
{3, GL_FLOAT, false}
|
||||||
};
|
};
|
||||||
NB::DrawBuffer myBuffer(attrs, myShader);
|
__NB_GL_DEBUG__(NB::DrawBuffer myBuffer(attrs, myShader));
|
||||||
myBuffer.generateElementBuffer({0, 1, 2}, GL_TRIANGLES);
|
__NB_GL_DEBUG__(myBuffer.generateElementBuffer({0, 1, 2, 0, 2, 3}, GL_TRIANGLES));
|
||||||
std::vector<float> data_float = {
|
std::vector<float> data_float = {
|
||||||
-0.5, -0.5, 1.0, 0.0, 0.0,
|
-0.5, -0.5, 1.0, 0.0, 0.0,
|
||||||
0.5, -0.5, 0.0, 1.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
|
// Window loop
|
||||||
GLenum err;
|
GLenum err;
|
||||||
while(!glfwWindowShouldClose(window)) {
|
while(!glfwWindowShouldClose(window)) {
|
||||||
while(err=glGetError()) {
|
__NB_GL_DEBUG_THROW__("nothing", "during");
|
||||||
std::cout << "[GL ERROR] " << err << "\n";
|
|
||||||
}
|
|
||||||
// Input checking
|
// Input checking
|
||||||
processInput(window);
|
processInput(window);
|
||||||
|
|
||||||
@ -46,7 +46,7 @@ int main() {
|
|||||||
myShader.use();
|
myShader.use();
|
||||||
glClearColor(0.2f, 0.4f, 0.3f, 1.0f);
|
glClearColor(0.2f, 0.4f, 0.3f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
|
||||||
myBuffer.draw();
|
__NB_GL_DEBUG__(myBuffer.draw());
|
||||||
|
|
||||||
// Events and buffer swap
|
// Events and buffer swap
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user