Compare commits

..

No commits in common. "d6c05fa236b1ee53d50349a28d57ee96381d64d3" and "febfe200a9b8ecf98318429263e167e97a31698f" have entirely different histories.

8 changed files with 67 additions and 222 deletions

8
.idea/.gitignore vendored
View File

@ -1,8 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -1 +0,0 @@
BLT_WITH_GRAPHICS

View File

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

View File

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

View File

@ -1,8 +0,0 @@
<?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>

View File

@ -1,9 +0,0 @@
<?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,12 +30,11 @@ namespace blt::gfx
class uniform_buffer
{
private:
GLuint uboID = 0;
GLuint uboID;
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);
/**
@ -43,19 +42,14 @@ namespace blt::gfx
* @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);
uniform_buffer& bind();
inline uniform_buffer& unbind()
{
glBindBuffer(GL_UNIFORM_BUFFER, 0);
return *this;
}
/**
* Binds the UBO to a binding location
*/
uniform_buffer& bind(GLuint location);
~uniform_buffer();
@ -77,55 +71,69 @@ 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;
IntDefaultedToMinusOne getUniformLocation(const std::string& name);
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;
}
public:
inline const shader_base_t& bind() const
inline void bind() const
{
glUseProgram(programID);
return *this;
}
inline shader_base_t& unbind()
inline void setBool(const std::string& name, bool value)
{
glUseProgram(0);
return *this;
glUniform1i(getUniformLocation(name), (int) value);
}
shader_base_t& setBool(const std::string& name, bool value);
inline void setInt(const std::string& name, int value)
{
glUniform1i(getUniformLocation(name), value);
}
shader_base_t& setInt(const std::string& name, int value);
inline void setFloat(const std::string& name, float value)
{
glUniform1f(getUniformLocation(name), value);
}
shader_base_t& setFloat(const std::string& name, float value);
inline void setMatrix(const std::string& name, blt::mat4x4& matrix)
{
glUniformMatrix4fv(getUniformLocation(name), 1, GL_FALSE, matrix.ptr());
}
shader_base_t& setMatrix(const std::string& name, blt::mat4x4& matrix);
inline void setVec3(const std::string& name, const blt::vec3& vec)
{
glUniform3f(getUniformLocation(name), vec.x(), vec.y(), vec.z());
}
shader_base_t& setVec2(const std::string& name, const blt::vec2& vec);
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& setVec3(const std::string& name, const blt::vec3& vec);
inline void setVec2(const std::string& name, float x, float y)
{
glUniform2f(getUniformLocation(name), x, y);
}
shader_base_t& setVec4(const std::string& name, const blt::vec4& vec);
inline void setVec3(const std::string& name, float x, float y, float z)
{
glUniform3f(getUniformLocation(name), x, y, z);
}
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);
inline void setVec4(const std::string& name, float x, float y, float z, float w)
{
glUniform4f(getUniformLocation(name), x, y, z, w);
}
};
/**

View File

@ -22,14 +22,11 @@
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";
}
@ -37,8 +34,7 @@ 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);
@ -53,8 +49,7 @@ 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);
@ -73,18 +68,15 @@ 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);
}
@ -103,8 +95,7 @@ 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);
@ -127,14 +118,12 @@ 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)
@ -143,8 +132,7 @@ 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)
@ -161,8 +149,7 @@ 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;
@ -173,151 +160,33 @@ namespace blt::gfx
move.programID = -1;
}
uniform_buffer::uniform_buffer(size_t size, GLuint location): size_(size), location_(location)
uniform_buffer::uniform_buffer(size_t size, GLuint 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): size_(size), location_(location)
uniform_buffer::uniform_buffer(void* data, size_t size, GLuint 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()
uniform_buffer& uniform_buffer::bind(GLuint location)
{
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;
}
}