diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..0f3559e --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +BLT_WITH_GRAPHICS \ No newline at end of file diff --git a/.idea/BLT-With-Graphics-Template.iml b/.idea/BLT-With-Graphics-Template.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/BLT-With-Graphics-Template.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..7d22b79 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..0b95b24 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/include/blt/gfx/shader.h b/include/blt/gfx/shader.h index 9e44c03..de77028 100644 --- a/include/blt/gfx/shader.h +++ b/include/blt/gfx/shader.h @@ -30,26 +30,32 @@ namespace blt::gfx class uniform_buffer { private: - GLuint uboID; + GLuint uboID = 0; size_t size_; GLuint location_; public: explicit uniform_buffer(size_t size, GLuint location = 0); - uniform_buffer(void* data, size_t size, GLuint location = 0); + + uniform_buffer(void* data, size_t size, GLuint location = 0); /** * Resizes the internal UBO * @param newSize new size for the UBO */ uniform_buffer& resize(size_t newSize); + /** * Uploads data to the UBO. This can be an arbitrary locations and does not need to be the whole UBO. */ uniform_buffer& upload(void* data, size_t size, size_t offset = 0); - /** - * Binds the UBO to a binding location - */ - uniform_buffer& bind(GLuint location); + + uniform_buffer& bind(); + + inline uniform_buffer& unbind() + { + glBindBuffer(GL_UNIFORM_BUFFER, 0); + return *this; + } ~uniform_buffer(); @@ -85,14 +91,22 @@ namespace blt::gfx } public: - inline void bind() const + inline const shader_base_t& bind() const { glUseProgram(programID); + return *this; } - inline void setBool(const std::string& name, bool value) + inline shader_base_t& unbind() + { + glUseProgram(0); + return *this; + } + + inline shader_base_t& setBool(const std::string& name, bool value) { glUniform1i(getUniformLocation(name), (int) value); + return *this; } inline void setInt(const std::string& name, int value) diff --git a/src/blt/gfx/shader.cpp b/src/blt/gfx/shader.cpp index 50b3edb..bfd3126 100644 --- a/src/blt/gfx/shader.cpp +++ b/src/blt/gfx/shader.cpp @@ -160,33 +160,51 @@ namespace blt::gfx move.programID = -1; } - uniform_buffer::uniform_buffer(size_t size, GLuint location) + uniform_buffer::uniform_buffer(size_t size, GLuint location): size_(size), location_(location) { - + glGenBuffers(1, &uboID); + bind(); + glBufferData(GL_UNIFORM_BUFFER, static_cast(size), nullptr, GL_DYNAMIC_READ); + unbind(); + + glBindBufferBase(GL_UNIFORM_BUFFER, location, uboID); } - uniform_buffer::uniform_buffer(void* data, size_t size, GLuint location) + uniform_buffer::uniform_buffer(void* data, size_t size, GLuint location): size_(size), location_(location) { - + glGenBuffers(1, &uboID); + bind(); + glBufferData(GL_UNIFORM_BUFFER, static_cast(size), data, GL_DYNAMIC_READ); + unbind(); + + glBindBufferBase(GL_UNIFORM_BUFFER, location, uboID); } uniform_buffer& uniform_buffer::resize(size_t newSize) { + bind(); + size_ = newSize; + glBufferData(GL_UNIFORM_BUFFER, static_cast(size_), nullptr, GL_DYNAMIC_READ); + unbind(); return *this; } uniform_buffer& uniform_buffer::upload(void* data, size_t size, size_t offset) { + bind(); + glBufferSubData(GL_UNIFORM_BUFFER, static_cast(offset), static_cast(size), data); + unbind(); return *this; } - uniform_buffer& uniform_buffer::bind(GLuint location) + uniform_buffer& uniform_buffer::bind() { + glBindBuffer(GL_UNIFORM_BUFFER, uboID); return *this; } uniform_buffer::~uniform_buffer() { - + glDeleteBuffers(1, &uboID); } }