Skip to content

Commit

Permalink
Added group
Browse files Browse the repository at this point in the history
  • Loading branch information
denis authored and denis committed Nov 15, 2024
1 parent bb907a1 commit f1a86fa
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 38 deletions.
63 changes: 46 additions & 17 deletions Engine/Shaders/TransparentPipeline/compute.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ layout(std430, binding = 21) buffer MaterialCopy

layout(std430, binding = 23) buffer IndicesData
{
uint indicesData[];
ivec4 indicesData[];
};

layout(std430, binding = 24) buffer Status
Expand All @@ -92,30 +92,59 @@ void main() {
// Sort using indices to avoid modifying the copy buffers
uint size = statusCopy.length();
// Initialize indices
for (uint i = 0; i < size; i++) {
indicesData[i] = i;
for (int i = 0; i < size; i++) {
indicesData[i].x = i;
}

// Perform bubble sort on indices based on depth from copy buffers
for (uint i = 0; i < size - 1; i++) {
for (uint j = 0; j < size - 1 - i; j++) {
// Calculate depths using the copy buffers
float depthA = calculateDepth(modelMatricesCopy[indicesData[j]]);
float depthB = calculateDepth(modelMatricesCopy[indicesData[j + 1]]);

// Sort in descending order (farthest first) for transparency blending
if (depthA < depthB) {
// Swap indices instead of modifying the copy buffer data
uint temp = indicesData[j];
indicesData[j] = indicesData[j + 1];
indicesData[j + 1] = temp;
uint bitMask = 1u;
int maxBits = 32; // Assuming 32-bit floats represented as integers

// Perform sorting bit-by-bit
for (int bit = 0; bit < maxBits; bit++) {
int zeroCount = 0;

// Create a count of zeros for the current bit position
for (int i = 0; i < size; i++) {
float depth = calculateDepth(modelMatricesCopy[indicesData[i].x]);
int intDepth = floatBitsToInt(depth);
if ((intDepth & bitMask) == 0u) {
zeroCount++;
}
}

// Compute the output indices
int zeroIndex = 0;
int oneIndex = zeroCount;
for (int i = 0; i < size; i++) {
float depth = calculateDepth(modelMatricesCopy[indicesData[i].x]);
int intDepth = floatBitsToInt(depth);
if ((intDepth & bitMask) == 0u) {
indicesData[zeroIndex++].y = indicesData[i].x;
}
else {
indicesData[oneIndex++].y = indicesData[i].x;
}
}

// Copy sorted data back to indicesData
for (int i = 0; i < size; i++) {
indicesData[i].x = indicesData[i].y;
}

// Move to the next bit
bitMask <<= 1u;
}

// Reverse the array to ensure descending order (largest first)
for (int i = 0; i < size / 2; i++) {
int temp = indicesData[i].x;
indicesData[i].x = indicesData[size - 1 - i].x;
indicesData[size - 1 - i].x = temp;
}

// Write sorted data from copy buffers to main buffers
for (uint i = 0; i < size; i++) {
uint sortedIndex = indicesData[i];
int sortedIndex = indicesData[i].x;
instanceData[i] = instanceDataCopy[sortedIndex];
modelMatrices[i] = modelMatricesCopy[sortedIndex];
materialData[i] = materialDataCopy[sortedIndex];
Expand Down
34 changes: 15 additions & 19 deletions Engine/Shaders/TransparentPipeline/computeHideShow.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,21 @@ uniform bool transparent;

void main() {
uint index = gl_GlobalInvocationID.x;
if (index == 0) {
for (int i = 0; i < materialData.length(); i++) {
if (status[i] > 0) {
if (transparent) {
if (materialData[i].transparent==1) {
instanceData[i].instanceCount = 1;
}
else {
instanceData[i].instanceCount = 0;
}
}
else {
if (materialData[i].transparent==0) {
instanceData[i].instanceCount = 1;
}
else {
instanceData[i].instanceCount = 0;
}
}
if (status[index] > 0) {
if (transparent) {
if (materialData[index].transparent==1) {
instanceData[index].instanceCount = 1;
}
else {
instanceData[index].instanceCount = 0;
}
}
else {
if (materialData[index].transparent==0) {
instanceData[index].instanceCount = 1;
}
else {
instanceData[index].instanceCount = 0;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Engine/src/Pipelines/PipelineDeferredForward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,6 @@ void Prisma::PipelineDeferredForward::showTransparencies(bool show)
{
m_shaderCompute->use();
m_shaderCompute->setBool(m_transparentLocation, show);
m_shaderCompute->dispatchCompute({1, 1, 1});
m_shaderCompute->dispatchCompute({Prisma::GlobalData::getInstance().currentGlobalScene()->meshes.size(), 1, 1});
m_shaderCompute->wait(GL_COMMAND_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT);
}
2 changes: 1 addition & 1 deletion Engine/src/SceneData/MeshIndirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ void Prisma::MeshIndirect::updateSize()
m_ssboMaterialCopy->resize(sizeof(MaterialData) * (m_materialData.size()));
m_ssboMaterialCopy->modifyData(0, sizeof(MaterialData) * m_materialData.size(), m_materialData.data());

m_ssboIndices->resize(sizeof(unsigned int) * meshes.size());
m_ssboIndices->resize(sizeof(glm::ivec4) * meshes.size());

std::vector<unsigned int> status;
for (const auto& mesh : meshes)
Expand Down

0 comments on commit f1a86fa

Please sign in to comment.