add uniform buffer functions in shader.cpp

main
Brett 2023-12-29 12:19:45 -05:00
parent febfe200a9
commit f94ecb2e1e
8 changed files with 78 additions and 14 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -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

1
.idea/.name Normal file
View File

@ -0,0 +1 @@
BLT_WITH_GRAPHICS

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/BLT-With-Graphics-Template.iml" filepath="$PROJECT_DIR$/.idea/BLT-With-Graphics-Template.iml" />
</modules>
</component>
</project>

9
.idea/vcs.xml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$/libraries/BLT" vcs="Git" />
<mapping directory="$PROJECT_DIR$/libraries/BLT/libraries/parallel-hashmap" vcs="Git" />
<mapping directory="$PROJECT_DIR$/libraries/imgui" vcs="Git" />
</component>
</project>

View File

@ -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)

View File

@ -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<GLsizeiptr>(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<GLsizeiptr>(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<GLsizeiptr>(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<GLsizeiptr>(offset), static_cast<GLsizeiptr>(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);
}
}