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

228 lines
7.0 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
}
2024-10-21 21:40:48 +02:00
[numthreads(WORKGROUP_SIZE, 1, 1)]
2024-10-21 16:49:58 +02:00
void Classify(uint dispatchID : SV_DispatchThreadID)
2024-10-07 20:14:00 +02:00
{
2024-10-21 21:40:48 +02:00
//if(dispatchID > 0)
// return;
//for(uint i = 0; i < pParams.indirectDrawBuffer[9]; ++i)
//{
2024-10-21 16:49:58 +02:00
// This thread doesn't have any work to do, we're done
2024-10-21 21:40:48 +02:00
if (dispatchID >= pParams.indirectDrawBuffer[9])
return;
2024-10-07 20:14:00 +02:00
2024-10-21 21:40:48 +02:00
// Operate the indirection
uint currentID = pParams.indexedBisectorBuffer[dispatchID];
2024-10-15 21:35:52 +02:00
2024-10-21 21:40:48 +02:00
// Read the current geometry data
BisectorGeometry bis;
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-21 21:40:48 +02:00
// Classify the element
ClassifyElement(currentID, bis, pParams.geometry.totalNumElements, pParams.geometry.baseDepth);
2024-10-07 20:14:00 +02:00
}
2024-10-21 21:40:48 +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-21 21:40:48 +02:00
//if(dispatchID > 0)
// return;
//for(dispatchID = 0; dispatchID < pParams.classificationBuffer[SPLIT_COUNTER]; ++dispatchID)
//{
if (dispatchID >= pParams.classificationBuffer[SPLIT_COUNTER])
2024-10-07 20:14:00 +02:00
return;
2024-10-21 21:40:48 +02:00
// Grab the real elementID
uint currentID = pParams.classificationBuffer[CLASSIFY_COUNTER_OFFSET + dispatchID];
2024-10-07 20:14:00 +02:00
2024-10-21 21:40:48 +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;
}
2024-10-21 21:40:48 +02:00
[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-21 21:40:48 +02:00
//if(dispatchID > 0)
// return;
//
//for(uint i = 0; i < 64; ++i)
//{
2024-10-21 16:49:58 +02:00
// Load the CBT to the LDS
2024-10-21 21:40:48 +02:00
load_buffer_to_shared_memory(groupIndex);
2024-10-21 16:49:58 +02:00
// If this element doesn't need to be processed, we're done
2024-10-21 21:40:48 +02:00
if (dispatchID >= pParams.allocateBuffer[0])
return;
//for(uint i = 0; i < pParams.allocateBuffer[0]; ++i)
//{
2024-10-21 16:49:58 +02:00
// Allocate the required bits
2024-10-27 14:41:23 +01:00
AllocateElement(pParams.allocateBuffer[1 + dispatchID], dispatchID);
2024-10-21 21:40:48 +02:00
//}
2024-10-07 20:14:00 +02:00
}
2024-10-21 21:40:48 +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-21 21:40:48 +02:00
//if(dispatchID > 0)
2024-10-21 16:49:58 +02:00
// return;
2024-10-21 21:40:48 +02:00
// If this element doesn't need to be processed, we're done
if (dispatchID >= pParams.allocateBuffer[0])
return;
//for(uint i = 0; i < pParams.allocateBuffer[0]; ++i)
//{
2024-10-21 16:49:58 +02:00
// Operation the bisection of this element
2024-10-21 21:40:48 +02:00
BisectElement(pParams.allocateBuffer[1 + dispatchID]);
//}
2024-10-07 20:14:00 +02:00
}
2024-10-21 21:40:48 +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-21 21:40:48 +02:00
//if(dispatchID > 0)
2024-10-21 16:49:58 +02:00
// return;
2024-10-21 21:40:48 +02:00
// If this element doesn't need to be processed, we're done
if (dispatchID >= pParams.propagateBuffer[0])
return;
//for(uint i = 0; i < pParams.propagateBuffer[0]; ++i)
//{
PropagateBisectElement(pParams.propagateBuffer[2 + dispatchID]);
//}
2024-10-07 20:14:00 +02:00
}
2024-10-21 21:40:48 +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-21 21:40:48 +02:00
//if(dispatchID > 0)
2024-10-21 16:49:58 +02:00
// return;
2024-10-21 21:40:48 +02:00
// If this element doesn't need to be processed, we're done
if (dispatchID >= pParams.classificationBuffer[SIMPLIFY_COUNTER])
return;
//for(uint i = 0; i < pParams.classificationBuffer[SIMPLIFY_COUNTER]; ++i)
//{
// Grab the real elementID
uint currentID = pParams.classificationBuffer[CLASSIFY_COUNTER_OFFSET + pParams.geometry.totalNumElements + dispatchID];
2024-10-07 20:14:00 +02:00
2024-10-21 21:40:48 +02:00
// Simplify an element
PrepareSimplifyElement(currentID);
//}
2024-10-07 20:14:00 +02:00
}
2024-10-21 21:40:48 +02:00
[numthreads(WORKGROUP_SIZE, 1, 1)]
2024-10-21 16:49:58 +02:00
void Simplify(uint dispatchID : SV_DispatchThreadID)
2024-10-07 20:14:00 +02:00
{
2024-10-21 21:40:48 +02:00
//if(dispatchID > 0)
2024-10-21 16:49:58 +02:00
// return;
2024-10-21 21:40:48 +02:00
// This thread doesn't have any work to do, we're done
if (dispatchID >= pParams.simplifyBuffer[0])
return;
//for(uint i = 0; i < pParams.simplifyBuffer[0]; ++i)
//{
// Simplify an element
SimplifyElement(pParams.simplifyBuffer[1 + dispatchID]);
//}
2024-10-15 21:35:52 +02:00
}
2024-10-07 20:14:00 +02:00
2024-10-21 21:40:48 +02:00
[numthreads(WORKGROUP_SIZE, 1, 1)]
2024-10-15 21:35:52 +02:00
void PropagateSimplify(uint dispatchID : SV_DispatchThreadID)
{
2024-10-21 21:40:48 +02:00
//if(dispatchID > 0)
2024-10-21 16:49:58 +02:00
// return;
2024-10-21 21:40:48 +02:00
// If this element doesn't need to be processed, we're done
if (dispatchID >= pParams.propagateBuffer[1])
return;
//for(uint i = 0; i < pParams.propagateBuffer[1]; ++i)
//{
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);
}
2024-10-21 21:40:48 +02:00
[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-21 21:40:48 +02:00
//if(currentID > 0)
2024-10-21 16:49:58 +02:00
// return;
2024-10-21 21:40:48 +02:00
// This thread doesn't have any work to do, we're done
if (currentID >= pParams.geometry.totalNumElements)
return;
2024-10-07 20:14:00 +02:00
2024-10-21 21:40:48 +02:00
//for(uint i = 0; i < pParams.geometry.totalNumElements; ++i)
//{
// 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
}