Compare commits

...

2 Commits

8 changed files with 222 additions and 67 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();
@ -71,69 +77,55 @@ namespace blt::gfx
struct IntDefaultedToMinusOne
{
GLint i = -1;
inline explicit operator bool() const
{
return i != -1;
}
inline operator int() const
{
return i;
}
};
std::unordered_map<std::string, IntDefaultedToMinusOne> uniformVars;
GLuint programID = 0;
inline GLint getUniformLocation(const std::string& name)
{
if (uniformVars[name].i != -1)
return uniformVars[name].i;
int loc = glGetUniformLocation(programID, name.c_str());
uniformVars[name].i = loc;
return loc;
}
IntDefaultedToMinusOne getUniformLocation(const std::string& name);
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()
{
glUniform1i(getUniformLocation(name), (int) value);
glUseProgram(0);
return *this;
}
inline void setInt(const std::string& name, int value)
{
glUniform1i(getUniformLocation(name), value);
}
shader_base_t& setBool(const std::string& name, bool value);
inline void setFloat(const std::string& name, float value)
{
glUniform1f(getUniformLocation(name), value);
}
shader_base_t& setInt(const std::string& name, int value);
inline void setMatrix(const std::string& name, blt::mat4x4& matrix)
{
glUniformMatrix4fv(getUniformLocation(name), 1, GL_FALSE, matrix.ptr());
}
shader_base_t& setFloat(const std::string& name, float value);
inline void setVec3(const std::string& name, const blt::vec3& vec)
{
glUniform3f(getUniformLocation(name), vec.x(), vec.y(), vec.z());
}
shader_base_t& setMatrix(const std::string& name, blt::mat4x4& matrix);
inline void setVec4(const std::string& name, const blt::vec4& vec)
{
glUniform4f(getUniformLocation(name), vec.x(), vec.y(), vec.z(), vec.w());
}
shader_base_t& setVec2(const std::string& name, const blt::vec2& vec);
inline void setVec2(const std::string& name, float x, float y)
{
glUniform2f(getUniformLocation(name), x, y);
}
shader_base_t& setVec3(const std::string& name, const blt::vec3& vec);
inline void setVec3(const std::string& name, float x, float y, float z)
{
glUniform3f(getUniformLocation(name), x, y, z);
}
shader_base_t& setVec4(const std::string& name, const blt::vec4& vec);
inline void setVec4(const std::string& name, float x, float y, float z, float w)
{
glUniform4f(getUniformLocation(name), x, y, z, w);
}
shader_base_t& setVec2(const std::string& name, float x, float y);
shader_base_t& setVec3(const std::string& name, float x, float y, float z);
shader_base_t& setVec4(const std::string& name, float x, float y, float z, float w);
};
/**

View File

@ -22,11 +22,14 @@
namespace blt::gfx
{
static std::string removeEmptyFirstLines(const std::string& string){
static std::string removeEmptyFirstLines(const std::string& string)
{
auto lines = blt::string::split(string, "\n");
std::string new_source_string;
for (const auto& line : lines) {
if (!line.empty() && !blt::string::contains(line, "\"")) {
for (const auto& line : lines)
{
if (!line.empty() && !blt::string::contains(line, "\""))
{
new_source_string += line;
new_source_string += "\n";
}
@ -34,7 +37,8 @@ namespace blt::gfx
return new_source_string;
}
unsigned int shader_t::createShader(const std::string& source, int type) {
unsigned int shader_t::createShader(const std::string& source, int type)
{
const char* shader_code = source.c_str();
// creates a Shader
unsigned int shaderID = glCreateShader(type);
@ -49,7 +53,8 @@ namespace blt::gfx
// the TODO: maybe find a way of lexing the output to give suggestions about fixing the error? default error messages can be unhelpful at times.
GLint success;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &success);
if (!success) {
if (!success)
{
int log_length = 0;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &log_length);
@ -68,15 +73,18 @@ namespace blt::gfx
return shaderID;
}
shader_t::shader_t(const std::string& vertex, const std::string& fragment, bool load_as_string) {
shader_t::shader_t(const std::string& vertex, const std::string& fragment, bool load_as_string)
{
// load shader sources
std::string vertex_source = vertex;
std::string fragment_source = fragment;
if (!load_as_string){
if (!load_as_string)
{
// BLT provides a recursive file loader for glsl shaders. It's pretty much just a recursive function looking for include statements.
vertex_source = blt::fs::loadShaderFile(vertex);
fragment_source = blt::fs::loadShaderFile(fragment);
} else {
} else
{
vertex_source = removeEmptyFirstLines(vertex_source);
fragment_source = removeEmptyFirstLines(fragment_source);
}
@ -95,7 +103,8 @@ namespace blt::gfx
GLint success;
glGetProgramiv(programID, GL_LINK_STATUS, &success);
if (!success) {
if (!success)
{
int log_length = 0;
glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &log_length);
@ -118,12 +127,14 @@ namespace blt::gfx
glUseProgram(0);
}
void shader_t::bindAttribute(int attribute, const std::string &name) const {
void shader_t::bindAttribute(int attribute, const std::string& name) const
{
bind();
glBindAttribLocation(programID, attribute, name.c_str());
}
void shader_t::setUniformBlockLocation(const std::string &name, int location) const {
void shader_t::setUniformBlockLocation(const std::string& name, int location) const
{
bind();
auto blockID = glGetUniformBlockIndex(programID, name.c_str());
if (blockID != GL_INVALID_INDEX)
@ -132,7 +143,8 @@ namespace blt::gfx
BLT_WARN("Unable to find uniform buffer '%s'. Did you forget to declare it?", name.c_str());
}
shader_t::~shader_t() {
shader_t::~shader_t()
{
glUseProgram(0);
// shader was moved
if (programID <= 0)
@ -149,7 +161,8 @@ namespace blt::gfx
glDeleteProgram(programID);
}
shader_t::shader_t(shader_t&& move) noexcept {
shader_t::shader_t(shader_t&& move) noexcept
{
// the move constructor doesn't need to construct a new shader but it does need to ensure all old variables are moved over
programID = move.programID;
vertexShaderID = move.vertexShaderID;
@ -160,33 +173,151 @@ 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);
}
shader_base_t& shader_base_t::setBool(const std::string& name, bool value)
{
if (auto i = getUniformLocation(name))
glUniform1i(i.i, (int) value);
else
BLT_WARN("Unable to find boolean uniform variable %s", name.c_str());
return *this;
}
shader_base_t& shader_base_t::setInt(const std::string& name, int value)
{
if (auto i = getUniformLocation(name))
glUniform1i(i.i, value);
else
BLT_WARN("Unable to find integer uniform variable %s", name.c_str());
return *this;
}
shader_base_t& shader_base_t::setFloat(const std::string& name, float value)
{
if (auto i = getUniformLocation(name))
glUniform1f(i.i, value);
else
BLT_WARN("Unable to find float uniform variable %s", name.c_str());
return *this;
}
shader_base_t& shader_base_t::setMatrix(const std::string& name, mat4x4& matrix)
{
if (auto i = getUniformLocation(name))
glUniformMatrix4fv(i.i, 1, GL_FALSE, matrix.ptr());
else
BLT_WARN("Unable to find matrix uniform variable %s", name.c_str());
return *this;
}
shader_base_t& shader_base_t::setVec2(const std::string& name, const vec2& vec)
{
if (auto i = getUniformLocation(name))
glUniform2f(i.i, vec.x(), vec.y());
else
BLT_WARN("Unable to find vec3 uniform variable %s", name.c_str());
return *this;
}
shader_base_t& shader_base_t::setVec3(const std::string& name, const vec3& vec)
{
if (auto i = getUniformLocation(name))
glUniform3f(i.i, vec.x(), vec.y(), vec.z());
else
BLT_WARN("Unable to find vec3 uniform variable %s", name.c_str());
return *this;
}
shader_base_t& shader_base_t::setVec4(const std::string& name, const vec4& vec)
{
if (auto i = getUniformLocation(name))
glUniform4f(i.i, vec.x(), vec.y(), vec.z(), vec.w());
else
BLT_WARN("Unable to find vec4 uniform variable %s", name.c_str());
return *this;
}
shader_base_t& shader_base_t::setVec2(const std::string& name, float x, float y)
{
if (auto i = getUniformLocation(name))
glUniform2f(i.i, x, y);
else
BLT_WARN("Unable to find vec2 uniform variable %s", name.c_str());
return *this;
}
shader_base_t& shader_base_t::setVec3(const std::string& name, float x, float y, float z)
{
if (auto i = getUniformLocation(name))
glUniform3f(i.i, x, y, z);
else
BLT_WARN("Unable to find vec3 uniform variable %s", name.c_str());
return *this;
}
shader_base_t& shader_base_t::setVec4(const std::string& name, float x, float y, float z, float w)
{
if (auto i = getUniformLocation(name))
glUniform4f(i.i, x, y, z, w);
else
BLT_WARN("Unable to find vec4 uniform variable %s", name.c_str());
return *this;
}
shader_base_t::IntDefaultedToMinusOne shader_base_t::getUniformLocation(const std::string& name)
{
if (uniformVars[name])
return uniformVars[name];
int loc = glGetUniformLocation(programID, name.c_str());
auto& v = uniformVars[name];
v.i = loc;
return v;
}
}