Files
Seele/res/shaders/terrain/CBTCompute.slang
T

187 lines
5.8 KiB
Plaintext
Raw Normal View History

2024-10-15 21:35:52 +02:00
import update_utils;
2024-10-07 20:14:00 +02:00
import Parameters;
import CBT;
2024-10-15 21:35:52 +02:00
static const uint32_t WORKGROUP_SIZE = 64;
2024-10-07 20:14:00 +02:00
[numthreads(1, 1, 1)]
2024-10-15 21:35:52 +02:00
void Reset()
2024-10-07 20:14:00 +02:00
{
2024-10-15 21:35:52 +02:00
ResetBuffers();
2024-10-07 20:14:00 +02:00
}
[numthreads(WORKGROUP_SIZE, 1, 1)]
2024-10-15 21:35:52 +02:00
void Classify(uint currentID : SV_DispatchThreadID)
2024-10-07 20:14:00 +02:00
{
2024-10-15 21:35:52 +02:00
// This thread doesn't have any work to do, we're done
if (currentID >= pParams.indirectDrawBuffer[9])
2024-10-07 20:14:00 +02:00
return;
2024-10-15 21:35:52 +02:00
// Operate the indirection
currentID = pParams.indexedBisectorBuffer[currentID];
// Read the current geometry data
2024-10-07 20:14:00 +02:00
BisectorGeometry bis;
2024-10-15 21:35:52 +02:00
bis.p[0] = pParams.currentVertexBuffer[3 * currentID].xyz;
bis.p[1] = pParams.currentVertexBuffer[3 * currentID + 1].xyz;
bis.p[2] = pParams.currentVertexBuffer[3 * currentID + 2].xyz;
bis.p[3] = pParams.currentVertexBuffer[3 * pParams.geometry.totalNumElements + currentID].xyz;
2024-10-07 20:14:00 +02:00
2024-10-15 21:35:52 +02:00
// Classify the element
ClassifyElement(currentID, bis, pParams.geometry.totalNumElements, pParams.geometry.baseDepth);
2024-10-07 20:14:00 +02:00
}
[numthreads(WORKGROUP_SIZE, 1, 1)]
2024-10-15 21:35:52 +02:00
void Split(uint dispatchID : SV_DispatchThreadID)
2024-10-07 20:14:00 +02:00
{
2024-10-15 21:35:52 +02:00
if (dispatchID >= pParams.classificationBuffer[SPLIT_COUNTER])
2024-10-07 20:14:00 +02:00
return;
2024-10-15 21:35:52 +02:00
// Grab the real elementID
2024-10-07 20:14:00 +02:00
uint currentID = pParams.classificationBuffer[CLASSIFY_COUNTER_OFFSET + dispatchID];
2024-10-15 21:35:52 +02:00
// Split the element
SplitElement(currentID, pParams.geometry.baseDepth);
2024-10-07 20:14:00 +02:00
}
[numthreads(1, 1, 1)]
2024-10-15 21:35:52 +02:00
void PrepareIndirect(uint currentID : SV_DispatchThreadID)
2024-10-07 20:14:00 +02:00
{
pParams.indirectDispatchBuffer[currentID * 3 + 0] = (pParams.allocateBuffer[currentID] + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE;
pParams.indirectDispatchBuffer[currentID * 3 + 1] = 1;
pParams.indirectDispatchBuffer[currentID * 3 + 2] = 1;
}
[numthreads(WORKGROUP_SIZE, 1, 1)]
2024-10-15 21:35:52 +02:00
void Allocate(uint groupIndex : SV_GroupIndex, uint dispatchID : SV_DispatchThreadID)
2024-10-07 20:14:00 +02:00
{
2024-10-15 21:35:52 +02:00
// Load the CBT to the LDS
2024-10-07 20:14:00 +02:00
load_buffer_to_shared_memory(groupIndex);
2024-10-15 21:35:52 +02:00
// If this element doesn't need to be processed, we're done
if (dispatchID >= pParams.allocateBuffer[0])
2024-10-07 20:14:00 +02:00
return;
2024-10-15 21:35:52 +02:00
// Allocate the required bits
AllocateElement(pParams.allocateBuffer[1 + dispatchID]);
2024-10-07 20:14:00 +02:00
}
[numthreads(WORKGROUP_SIZE, 1, 1)]
2024-10-15 21:35:52 +02:00
void Bisect(uint groupIndex : SV_GroupIndex, uint dispatchID : SV_DispatchThreadID)
2024-10-07 20:14:00 +02:00
{
2024-10-15 21:35:52 +02:00
// If this element doesn't need to be processed, we're done
if (dispatchID >= pParams.allocateBuffer[0])
2024-10-07 20:14:00 +02:00
return;
2024-10-15 21:35:52 +02:00
// Operation the bisection of this element
BisectElement(pParams.allocateBuffer[1 + dispatchID]);
2024-10-07 20:14:00 +02:00
}
[numthreads(WORKGROUP_SIZE, 1, 1)]
2024-10-15 21:35:52 +02:00
void PropagateBisect(uint dispatchID : SV_DispatchThreadID)
2024-10-07 20:14:00 +02:00
{
2024-10-15 21:35:52 +02:00
// If this element doesn't need to be processed, we're done
if (dispatchID >= pParams.propagateBuffer[0])
2024-10-07 20:14:00 +02:00
return;
2024-10-15 21:35:52 +02:00
PropagateBisectElement(pParams.propagateBuffer[2 + dispatchID]);
2024-10-07 20:14:00 +02:00
}
[numthreads(WORKGROUP_SIZE, 1, 1)]
2024-10-15 21:35:52 +02:00
void PrepareSimplify(uint dispatchID : SV_DispatchThreadID)
2024-10-07 20:14:00 +02:00
{
2024-10-15 21:35:52 +02:00
// If this element doesn't need to be processed, we're done
if (dispatchID >= pParams.classificationBuffer[SIMPLIFY_COUNTER])
2024-10-07 20:14:00 +02:00
return;
2024-10-15 21:35:52 +02:00
// Grab the real elementID
2024-10-07 20:14:00 +02:00
uint currentID = pParams.classificationBuffer[CLASSIFY_COUNTER_OFFSET + pParams.geometry.totalNumElements + dispatchID];
2024-10-15 21:35:52 +02:00
// Simplify an element
PrepareSimplifyElement(currentID);
2024-10-07 20:14:00 +02:00
}
[numthreads(WORKGROUP_SIZE, 1, 1)]
2024-10-15 21:35:52 +02:00
void Simplify(uint currentID : SV_DispatchThreadID)
2024-10-07 20:14:00 +02:00
{
2024-10-15 21:35:52 +02:00
// This thread doesn't have any work to do, we're done
if (currentID >= pParams.simplifyBuffer[0])
2024-10-07 20:14:00 +02:00
return;
2024-10-15 21:35:52 +02:00
// Simplify an element
SimplifyElement(pParams.simplifyBuffer[1 + currentID]);
}
2024-10-07 20:14:00 +02:00
2024-10-15 21:35:52 +02:00
[numthreads(WORKGROUP_SIZE, 1, 1)]
void PropagateSimplify(uint dispatchID : SV_DispatchThreadID)
{
// If this element doesn't need to be processed, we're done
if (dispatchID >= pParams.propagateBuffer[1])
return;
2024-10-07 20:14:00 +02:00
2024-10-15 21:35:52 +02:00
PropagateElementSimplify(pParams.propagateBuffer[2 + dispatchID]);
2024-10-07 20:14:00 +02:00
}
[numthreads(WORKGROUP_SIZE, 1, 1)]
2024-10-15 21:35:52 +02:00
void ReducePrePass(uint dispatchThreadID : SV_DispatchThreadID)
2024-10-07 20:14:00 +02:00
{
reduce_prepass(dispatchThreadID);
}
[numthreads(WORKGROUP_SIZE, 1, 1)]
2024-10-15 21:35:52 +02:00
void ReduceFirstPass(uint dispatchThreadID : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex)
2024-10-07 20:14:00 +02:00
{
2024-10-15 21:35:52 +02:00
// Reduce Mid Pass
reduce_first_pass(dispatchThreadID, groupIndex);
2024-10-07 20:14:00 +02:00
}
[numthreads(WORKGROUP_SIZE, 1, 1)]
2024-10-15 21:35:52 +02:00
void ReduceSecondPass(uint groupIndex : SV_GroupIndex)
2024-10-07 20:14:00 +02:00
{
2024-10-15 21:35:52 +02:00
// Find the ith bit set to one
2024-10-07 20:14:00 +02:00
reduce_second_pass(groupIndex);
}
[numthreads(WORKGROUP_SIZE, 1, 1)]
2024-10-15 21:35:52 +02:00
void BisectorIndexation(uint currentID : SV_DispatchThreadID)
2024-10-07 20:14:00 +02:00
{
2024-10-15 21:35:52 +02:00
// This thread doesn't have any work to do, we're done
if (currentID >= pParams.geometry.totalNumElements)
2024-10-07 20:14:00 +02:00
return;
2024-10-15 21:35:52 +02:00
// Indexate this bisector
BisectorElementIndexation(currentID);
2024-10-07 20:14:00 +02:00
}
[numthreads(1, 1, 1)]
2024-10-15 21:35:52 +02:00
void PrepareBisectorIndirect(uint currentID : SV_DispatchThreadID)
2024-10-07 20:14:00 +02:00
{
2024-10-15 21:35:52 +02:00
// Indirect dispatch for each bisector
2024-10-07 20:14:00 +02:00
pParams.indirectDispatchBuffer[0] = (pParams.indirectDrawBuffer[0] / 3 + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE;
pParams.indirectDispatchBuffer[1] = 1;
pParams.indirectDispatchBuffer[2] = 1;
2024-10-15 21:35:52 +02:00
// Indirect dispatch for each vertex to process (3 per bisector + 1 for the parent)
2024-10-07 20:14:00 +02:00
pParams.indirectDispatchBuffer[3] = (pParams.indirectDrawBuffer[0] * 4 / 3 + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE;
pParams.indirectDispatchBuffer[4] = 1;
pParams.indirectDispatchBuffer[5] = 1;
2024-10-15 21:35:52 +02:00
// Indirect dispatch for each modified vertex to process
2024-10-07 20:14:00 +02:00
pParams.indirectDispatchBuffer[6] = (pParams.indirectDrawBuffer[8] + WORKGROUP_SIZE - 1) / WORKGROUP_SIZE;
pParams.indirectDispatchBuffer[7] = 1;
pParams.indirectDispatchBuffer[8] = 1;
2024-10-15 21:35:52 +02:00
// Explicit counter of the number of bisectors
2024-10-07 20:14:00 +02:00
pParams.indirectDrawBuffer[9] = pParams.indirectDrawBuffer[0] / 3;
}
[numthreads(WORKGROUP_SIZE, 1, 1)]
2024-10-15 21:35:52 +02:00
void Validate(uint currentID : SV_DispatchThreadID)
2024-10-07 20:14:00 +02:00
{
2024-10-15 21:35:52 +02:00
// This thread doesn't have any work to do, we're done
if (currentID >= pParams.geometry.totalNumElements)
2024-10-07 20:14:00 +02:00
return;
2024-10-15 21:35:52 +02:00
ValidateBisector(currentID);
2024-10-07 20:14:00 +02:00
}