Skip to content

Commit

Permalink
Merge pull request #150 from uPiscium/develop
Browse files Browse the repository at this point in the history
Change SSBO interface to UBO interface.
  • Loading branch information
uPiscium authored Oct 2, 2024
2 parents 22ac00e + 1f1eceb commit ff7d437
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 36 deletions.
10 changes: 10 additions & 0 deletions impls/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,14 @@ void ShaderStorageBuffer::Allocate(Ulong const &size,
mSize = size;
this->Unbind();
}

void ShaderStorageBuffer::Bind(Shader &shader, Str const &name) const {
shader.BindStorageBlock(name, (Uint)mSSBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, (Uint)mSSBO, mSSBO);
}

void ShaderStorageBuffer::Bind(ComputeKernel &shader, Str const &name) const {
shader.BindStorageBlock(name, (Uint)mSSBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, (Uint)mSSBO, mSSBO);
}
} // namespace TerreateGraphics::Core
19 changes: 0 additions & 19 deletions impls/compute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,6 @@ void ComputeKernel::SetMat4(Str const &name, mat4 const &value) const {
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");
Expand Down
7 changes: 4 additions & 3 deletions includes/buffer.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#ifndef __TERREATE_GRAPHICS_BUFFER_HPP__
#define __TERREATE_GRAPHICS_BUFFER_HPP__

#include "compute.hpp"
#include "defines.hpp"
#include "exceptions.hpp"
#include "globj.hpp"
#include "shader.hpp"

namespace TerreateGraphics::Core {
using namespace TerreateGraphics::Defines;
using namespace TerreateGraphics::Compute;
using namespace TerreateGraphics::GL;

struct AttributeData {
Expand Down Expand Up @@ -356,9 +358,8 @@ class ShaderStorageBuffer : public TerreateObjectBase {

void Allocate(Ulong const &size,
BufferUsage const &usage = BufferUsage::STATIC_DRAW);
void BindBase(Uint const &index) const {
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, index, mSSBO);
}
void Bind(Shader &shader, Str const &name) const;
void Bind(ComputeKernel &kernel, Str const &name) const;
};
} // namespace TerreateGraphics::Core

Expand Down
22 changes: 12 additions & 10 deletions includes/compute.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#ifndef __TERREATE_GRAPHICS_KERNEL_HPP__
#define __TERREATE_GRAPHICS_KERNEL_HPP__

#include "buffer.hpp"
// #include "buffer.hpp"
#include "defines.hpp"
#include "globj.hpp"

namespace TerreateGraphics::Compute {
using namespace TerreateGraphics::Defines;
using namespace TerreateGraphics::Core;
// using namespace TerreateGraphics::Core;
using namespace TerreateGraphics::GL;
using namespace TerreateCore::Math;

Expand All @@ -17,8 +17,6 @@ class ComputeKernel final : public TerreateObjectBase {
Bool mLinked = false;
GLObject mKernelID = GLObject();
Str mKernelSource = "";
Map<Uint, ShaderStorageBuffer> mSSBOMap;
Map<Uint, Uint> mSSBOBindingMap;

public:
/*
Expand Down Expand Up @@ -143,18 +141,22 @@ class ComputeKernel final : public TerreateObjectBase {
*/
void SetMat4(Str const &name, mat4 const &value) const;

/*
* @brief: Add shader storage block.
* @param: name: name of storage block
* @param: binding: binding point of storage block
*/
void AddStorage(ShaderStorageBuffer const &ssbo, Str const &name);
/*
* @brief: Add kernel source.
* @param: source: source code to add
*/
void AddKernelSource(Str const &source) { mKernelSource += source; }

/*
* @brief: This function binds storage block index to binding point.
* @param: name: name of storage block
* @param: binding: binding point
*/
void BindStorageBlock(Str const &name, Uint const &binding) const {
glShaderStorageBlockBinding(mKernelID, this->GetStorageBlockIndex(name),
binding);
}

/*
* @brief: Compile shader.
*/
Expand Down
18 changes: 18 additions & 0 deletions includes/shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ class Shader final : public TerreateObjectBase {
unsigned GetUniformBlockIndex(Str const &name) const {
return glGetUniformBlockIndex(mShaderID, name.c_str());
}
/*
* @brief: Getter for shader storage block index.
* @param: name: name of storage block
* @return: storage block index
*/
unsigned GetStorageBlockIndex(Str const &name) const {
return glGetProgramResourceIndex(mShaderID, GL_SHADER_STORAGE_BLOCK,
name.c_str());
}

/*
* @brief: Setter for shader Bool uniform.
Expand Down Expand Up @@ -249,6 +258,15 @@ class Shader final : public TerreateObjectBase {
void BindUniformBlock(Str const &name, Uint const &binding) const {
glUniformBlockBinding(mShaderID, this->GetUniformBlockIndex(name), binding);
}
/*
* @brief: This function binds storage block index to binding point.
* @param: name: name of storage block
* @param: binding: binding point
*/
void BindStorageBlock(Str const &name, Uint const &binding) const {
glShaderStorageBlockBinding(mShaderID, this->GetStorageBlockIndex(name),
binding);
}
/*
* @brief: This function swiches blending on or off.
* @param: value: true to turn on, false to turn off
Expand Down
10 changes: 6 additions & 4 deletions tests/TGTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ void TestCompute() {
kernel.Compile();
kernel.Link();

kernel.AddStorage(input, "InputBuffer");
kernel.AddStorage(output, "OutputBuffer");
input.Bind(kernel, "InputBuffer");
output.Bind(kernel, "OutputBuffer");

kernel.SetFloat("scaleFactor", 2.0f);
kernel.Dispatch(10, 1, 1);
Expand All @@ -329,8 +329,8 @@ void TestCompute() {
kernel2.Compile();
kernel2.Link();

kernel2.AddStorage(input2, "InputBuffer");
kernel2.AddStorage(output2, "OutputBuffer");
input2.Bind(kernel2, "InputBuffer");
output2.Bind(kernel2, "OutputBuffer");

kernel2.SetFloat("scaleFactor", 3.0f);
kernel2.Dispatch(10, 1, 1);
Expand Down Expand Up @@ -364,6 +364,8 @@ int main() {
app.CharCallback(window, codepoint);
});

// TestCompute();

while (window) {
window.Frame([&app](Window *window) { app.OnFrame(window); });
}
Expand Down

0 comments on commit ff7d437

Please sign in to comment.