Skip to content

Commit

Permalink
Merge pull request #148 from uPiscium/develop
Browse files Browse the repository at this point in the history
develop
  • Loading branch information
uPiscium authored Sep 24, 2024
2 parents 10de924 + aa7cfb3 commit 22ac00e
Show file tree
Hide file tree
Showing 20 changed files with 1,542 additions and 188 deletions.
2 changes: 2 additions & 0 deletions impls/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ function(Build)
${PROJECT_NAME} STATIC
TerreateGraphics.cpp
buffer.cpp
compute.cpp
event.cpp
font.cpp
gl.cpp
globj.cpp
joystick.cpp
screen.cpp
shader.cpp
text.cpp
Expand Down
2 changes: 2 additions & 0 deletions impls/TerreateGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ void Initialize() {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_DOUBLEBUFFER, GL_TRUE);

glfwSetJoystickCallback(JoystickCallback);

GLFW_INITIALIZED = true;
}

Expand Down
34 changes: 33 additions & 1 deletion impls/buffer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "../includes/buffer.hpp"
#include "../includes/exceptions.hpp"

namespace TerreateGraphics::Core {
using namespace TerreateGraphics::Defines;
Expand Down Expand Up @@ -112,6 +111,7 @@ void Buffer::LoadData(Shader &shader, Vec<Float> const &raw,
shader.Link();
mBuffers.push_back(buffer);
}

void Buffer::LoadData(Shader &shader, BufferDataConstructor const &bdc,
BufferUsage const &usage) {
Map<Str, AttributeData> const &attributes = bdc.GetAttributes();
Expand Down Expand Up @@ -213,4 +213,36 @@ void Buffer::Draw(DrawMode const &mode, Ulong const &count) const {
this->Unbind();
}

UniformBuffer::~UniformBuffer() {
if (mUBO.Count() <= 1) {
glDeleteBuffers(1, mUBO);
mUBO.Delete();
}
}

void UniformBuffer::Allocate(Ulong const &size, BufferUsage const &usage) {
this->Bind();
glBufferData(GL_UNIFORM_BUFFER, size, nullptr, (GLenum)usage);
this->Unbind();
}

void UniformBuffer::Bind(Shader const &shader, Str const &name) const {
shader.BindUniformBlock(name, (Uint)mUBO);
glBindBufferBase(GL_UNIFORM_BUFFER, (Uint)mUBO, mUBO);
}

ShaderStorageBuffer::~ShaderStorageBuffer() {
if (mSSBO.Count() <= 1) {
glDeleteBuffers(1, mSSBO);
mSSBO.Delete();
}
}

void ShaderStorageBuffer::Allocate(Ulong const &size,
BufferUsage const &usage) {
this->Bind();
glBufferData(GL_SHADER_STORAGE_BUFFER, size, nullptr, (GLenum)usage);
mSize = size;
this->Unbind();
}
} // namespace TerreateGraphics::Core
215 changes: 215 additions & 0 deletions impls/compute.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
#include "../includes/compute.hpp"
#include "../includes/exceptions.hpp"

namespace TerreateGraphics::Compute {
Str GetShaderLog(Uint const &id) {
Int status;
glGetShaderiv(id, GL_COMPILE_STATUS, &status);
if (status) {
return "";
}

char log[1024];
glGetShaderInfoLog(id, 1024, NULL, log);
return log;
}

void CheckCompileStatus(Uint const &id, Str const &name) {
Str log = GetShaderLog(id);
if (log != "") {
log = name + " shader log: " + log;
throw Exceptions::ShaderError(log);
}
}

Str GetProgramLog(Uint const &id) {
Int status;
glGetProgramiv(id, GL_LINK_STATUS, &status);

if (status) {
return "";
}

char log[1024];
glGetProgramInfoLog(id, 1024, NULL, log);
return log;
}

void CheckLinkStatus(Uint const &id) {
Str log = GetProgramLog(id);

if (log != "") {
log = "Shader program log: " + log;
throw Exceptions::ShaderError(log);
}
}

ComputeKernel::ComputeKernel() {
mKernelID = glCreateProgram();
mCompiled = false;
}

ComputeKernel::~ComputeKernel() {
if (mKernelID.Count() <= 1) {
glDeleteProgram(mKernelID);
mKernelID.Delete();
}
}

void ComputeKernel::SetBool(Str const &name, Bool const &value) const {
glUseProgram(mKernelID);
glUniform1i(glGetUniformLocation(mKernelID, name.c_str()), value);
glUseProgram(0);
}

void ComputeKernel::SetBools(Str const &name, Bool const *values,
Uint const &count) const {
glUseProgram(mKernelID);
glUniform1iv(glGetUniformLocation(mKernelID, name.c_str()), count,
reinterpret_cast<Int const *>(values));
glUseProgram(0);
}

void ComputeKernel::SetInt(Str const &name, Int const &value) const {
glUseProgram(mKernelID);
glUniform1i(glGetUniformLocation(mKernelID, name.c_str()), value);
glUseProgram(0);
}

void ComputeKernel::SetInts(Str const &name, Int const *values,
Uint const &count) const {
glUseProgram(mKernelID);
glUniform1iv(glGetUniformLocation(mKernelID, name.c_str()), count, values);
glUseProgram(0);
}

void ComputeKernel::SetInts(Str const &name, Vec<Int> values) const {
glUseProgram(mKernelID);
glUniform1iv(glGetUniformLocation(mKernelID, name.c_str()), values.size(),
values.data());
glUseProgram(0);
}

void ComputeKernel::SetFloat(Str const &name, Float const &value) const {
glUseProgram(mKernelID);
glUniform1f(glGetUniformLocation(mKernelID, name.c_str()), value);
glUseProgram(0);
}

void ComputeKernel::SetFloats(Str const &name, Float const *values,
Uint const &count) const {
glUseProgram(mKernelID);
glUniform1fv(glGetUniformLocation(mKernelID, name.c_str()), count, values);
glUseProgram(0);
}

void ComputeKernel::SetFloats(Str const &name, Vec<Float> values) const {
glUseProgram(mKernelID);
glUniform1fv(glGetUniformLocation(mKernelID, name.c_str()), values.size(),
values.data());
glUseProgram(0);
}

void ComputeKernel::SetVec2(Str const &name, vec2 const &value) const {
glUseProgram(mKernelID);
glUniform2f(glGetUniformLocation(mKernelID, name.c_str()), value.x, value.y);
glUseProgram(0);
}

void ComputeKernel::SetVec3(Str const &name, vec3 const &value) const {
glUseProgram(mKernelID);
glUniform3f(glGetUniformLocation(mKernelID, name.c_str()), value.x, value.y,
value.z);
glUseProgram(0);
}

void ComputeKernel::SetVec4(Str const &name, vec4 const &value) const {
glUseProgram(mKernelID);
glUniform4f(glGetUniformLocation(mKernelID, name.c_str()), value.x, value.y,
value.z, value.w);
glUseProgram(0);
}

void ComputeKernel::SetMat2(Str const &name, mat2 const &value) const {
glUseProgram(mKernelID);
glUniformMatrix2fv(glGetUniformLocation(mKernelID, name.c_str()), 1, GL_FALSE,
&value[0][0]);
glUseProgram(0);
}

void ComputeKernel::SetMat3(Str const &name, mat3 const &value) const {
glUseProgram(mKernelID);
glUniformMatrix3fv(glGetUniformLocation(mKernelID, name.c_str()), 1, GL_FALSE,
&value[0][0]);
glUseProgram(0);
}

void ComputeKernel::SetMat4(Str const &name, mat4 const &value) const {
glUseProgram(mKernelID);
glUniformMatrix4fv(glGetUniformLocation(mKernelID, name.c_str()), 1, GL_FALSE,
&value[0][0]);
glUseProgram(0);
}

void ComputeKernel::AddStorage(ShaderStorageBuffer const &ssbo,
Str const &name) {
if (!mLinked) {
throw Exceptions::ShaderError("Kernel is not linked");
return;
}

Uint index = this->GetStorageBlockIndex(name);

if (mSSBOBindingMap.find(index) == mSSBOBindingMap.end()) {
Uint newID = mSSBOBindingMap.size();
glShaderStorageBlockBinding(mKernelID, index, newID);
mSSBOBindingMap.insert({index, newID});
mSSBOMap.insert({index, ssbo});
}

ssbo.BindBase(mSSBOBindingMap.at(index));
}

void ComputeKernel::Compile() {
if (mKernelSource == "") {
throw Exceptions::ShaderError("Compute kernel source is empty");
return;
}

ID kernelID = 0;
kernelID = glCreateShader(GL_COMPUTE_SHADER);
char const *kernelSource = mKernelSource.c_str();
glShaderSource(kernelID, 1, &kernelSource, nullptr);
glCompileShader(kernelID);
CheckCompileStatus(kernelID, "Compute");

glAttachShader(mKernelID, kernelID);
glDeleteShader(kernelID);

mCompiled = true;
}

void ComputeKernel::Link() {
if (!mCompiled) {
throw Exceptions::ShaderError("Shader is not compiled");
return;
}

glLinkProgram(mKernelID);
CheckLinkStatus(mKernelID);
mLinked = true;
}

void ComputeKernel::Dispatch(Uint const &x, Uint const &y,
Uint const &z) const {
if (!mLinked) {
throw Exceptions::ShaderError("Kernel is not linked");
return;
}

glUseProgram(mKernelID);
glDispatchCompute(x, y, z);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
glUseProgram(0);
}
} // namespace TerreateGraphics::Compute
Loading

0 comments on commit 22ac00e

Please sign in to comment.