Skip to content

Commit

Permalink
Added groups
Browse files Browse the repository at this point in the history
  • Loading branch information
denis authored and denis committed Dec 6, 2024
1 parent c267cbf commit dc15be4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 23 deletions.
25 changes: 18 additions & 7 deletions Engine/Shaders/TransparentPipeline/computeCopy.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ layout(std140, binding = 1) uniform MeshData {
mat4 projection;
};

layout(std140, binding = 4) uniform CameraData {
layout(std430, binding = 28) buffer CameraData {
float zNear;
float zFar;
float fovY;
Expand Down Expand Up @@ -168,31 +168,42 @@ bool isAABBInFrustum(mat4 viewProjection, AABB worldAABB) {

uniform bool initIndices = false;

layout(binding = 0) uniform atomic_uint counterSize;

void main() {
uint index = gl_GlobalInvocationID.x;

if (initIndices) {
indicesData[index].x = int(index);
}
else {
if (index == 0) {
atomicCounterExchange(counterSize, 0);
}

barrier();// Wait till all threads reach this point

int sortedIndex = indicesData[index].x;

// Copy data
instanceData[index] = instanceDataCopy[sortedIndex];
modelMatrices[index] = modelMatricesCopy[sortedIndex];
materialData[index] = materialDataCopy[sortedIndex];
status[index] = statusCopy[sortedIndex];

// Compute the view-projection matrix
mat4 viewProjection = projection * view;

// Transform the AABB to world space
AABB worldAABB = transformAABB(aabbData[sortedIndex], modelMatrices[index]);
AABB worldAABB = transformAABB(aabbData[sortedIndex], modelMatricesCopy[sortedIndex]);

// Perform frustum culling
if (!isAABBInFrustum(viewProjection, worldAABB)) {
status[index] = 0;
instanceData[index].instanceCount = 0;
}
else {
uint culledIdx = atomicCounterIncrement(counterSize);
// Copy data
instanceData[culledIdx] = instanceDataCopy[sortedIndex];
modelMatrices[culledIdx] = modelMatricesCopy[sortedIndex];
materialData[culledIdx] = materialDataCopy[sortedIndex];
status[culledIdx] = statusCopy[sortedIndex];
}
}
}
4 changes: 3 additions & 1 deletion Engine/include/SceneData/MeshIndirect.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ namespace Prisma
float aspect;
};

std::shared_ptr<Prisma::Ubo> m_uboCamera;
std::shared_ptr<Prisma::SSBO> m_ssboCamera;

unsigned int m_sizeAtomic;

public:
std::shared_ptr<VAO> vao();
Expand Down
8 changes: 4 additions & 4 deletions Engine/src/Containers/SSBO.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include "../../include/Containers/SSBO.h"
#include "../../include/Helpers/GarbageCollector.h"
#include <iostream>
#include <map>

static std::vector<unsigned int> usedId;
static std::map<unsigned int, bool> usedId;

Prisma::SSBO::SSBO(unsigned int ssbo)
{
auto find = std::find(usedId.begin(), usedId.end(), ssbo);
if (find != usedId.end())
if (usedId.find(ssbo) != usedId.end())
{
std::cerr << "SSBO ID " << ssbo << " ALREADY USED" << std::endl;
}
else
{
usedId.push_back(ssbo);
usedId[ssbo] = true;
glGenBuffers(1, &m_ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_ssbo);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, ssbo, m_ssbo);
Expand Down
10 changes: 6 additions & 4 deletions Engine/src/Containers/UBO.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
#include "../../include/Containers/Ubo.h"
#include "../../include/Helpers/GarbageCollector.h"
#include <iostream>
#include <map>

static std::vector<unsigned int> usedId;
static std::map<unsigned int, bool> usedId;

Prisma::Ubo::Ubo(unsigned int size, int ubo)
{
auto find = std::find(usedId.begin(), usedId.end(), ubo);
if (find != usedId.end())
if (usedId.find(ubo) != usedId.end())
{
std::cerr << "UBO ID " << ubo << " ALREADY USED" << std::endl;
}
else
{
usedId.push_back(ubo);
usedId[ubo] = true;
glGenBuffers(1, &m_ubo);
glBindBuffer(GL_UNIFORM_BUFFER, m_ubo);
glBufferData(GL_UNIFORM_BUFFER, size, nullptr, GL_DYNAMIC_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindBufferRange(GL_UNIFORM_BUFFER, ubo, m_ubo, 0, size);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
GarbageCollector::getInstance().add({GarbageCollector::GarbageType::BUFFER, m_ubo});
}
}

void Prisma::Ubo::modifyData(unsigned int offset, unsigned int size, void* data)
{
glBindBuffer(GL_UNIFORM_BUFFER, m_ubo);
glBindBufferRange(GL_UNIFORM_BUFFER, m_ubo, m_ubo, 0, size);
glBufferSubData(GL_UNIFORM_BUFFER, offset, size, data);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
19 changes: 13 additions & 6 deletions Engine/src/SceneData/MeshIndirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void Prisma::MeshIndirect::sort() const
data.zNear = camera->nearPlane();
data.fovY = glm::radians(camera->angle());
data.aspect = static_cast<float>(globalSettings.width) / static_cast<float>(globalSettings.height);
m_uboCamera->modifyData(0, sizeof(CameraData), &data);
m_ssboCamera->modifyData(0, sizeof(CameraData), &data);
m_shaderCopy->use();
m_shaderCopy->setBool(m_indicesCopyLocation, false);
m_shaderCopy->dispatchCompute({meshes.size(), 1, 1});
Expand Down Expand Up @@ -135,10 +135,11 @@ void Prisma::MeshIndirect::renderMeshes() const
{
m_vao->bind();
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, m_indirectDraw);
glBindBuffer(GL_PARAMETER_BUFFER_ARB, m_sizeAtomic);
glMemoryBarrier(GL_COMMAND_BARRIER_BIT);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, m_indirectSSBOId, m_indirectDraw);
glMultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, nullptr,
static_cast<GLuint>(Prisma::GlobalData::getInstance().currentGlobalScene()->meshes.
size()), 0);
glMultiDrawElementsIndirectCountARB(GL_TRIANGLES, GL_UNSIGNED_INT, NULL, 0, m_drawCommands.size(), 0);
glBindBuffer(GL_PARAMETER_BUFFER_ARB, 0);
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, 0);
}
}
Expand Down Expand Up @@ -408,7 +409,12 @@ Prisma::MeshIndirect::MeshIndirect()
m_vaoAnimation = std::make_shared<VAO>();
m_vboAnimation = std::make_shared<VBO>();
m_eboAnimation = std::make_shared<EBO>();

unsigned int numData = 0;
glGenBuffers(1, &m_sizeAtomic);
glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, m_sizeAtomic);
glBindBufferBase(GL_ATOMIC_COUNTER_BUFFER, 0, m_sizeAtomic);
glBufferData(GL_ATOMIC_COUNTER_BUFFER, sizeof(unsigned int), NULL, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, 0);
m_ssboModel = std::make_shared<SSBO>(1);
m_ssboMaterial = std::make_shared<SSBO>(0);

Expand All @@ -434,7 +440,8 @@ Prisma::MeshIndirect::MeshIndirect()
m_shaderCopy->use();
m_indicesCopyLocation = m_shaderCopy->getUniformPosition("initIndices");

m_uboCamera = std::make_shared<Prisma::Ubo>(sizeof(CameraData), 4);
m_ssboCamera = std::make_shared<Prisma::SSBO>(28);
m_ssboCamera->resize(sizeof(CameraData));
}

void Prisma::MeshIndirect::updateAnimation()
Expand Down
1 change: 0 additions & 1 deletion GUI/src/ImguiDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ Prisma::ImguiDebug::ImguiDebug() : m_lastFrameTime{glfwGetTime()}, m_fps{60.0f}
m_modelPos = m_shader->getUniformPosition("model");
m_scale = 0.72f;
m_translate = 1.0f - m_scale;
std::cout << Prisma::GlobalData::getInstance().currentGlobalScene()->camera->angle() << std::endl;
m_projection = glm::perspective(
glm::radians(Prisma::GlobalData::getInstance().currentGlobalScene()->camera->angle()),
static_cast<float>(settings.width) / static_cast<float>(settings.height),
Expand Down

0 comments on commit dc15be4

Please sign in to comment.