Shader optimizations
This commit is contained in:
@ -52,6 +52,8 @@ struct StackEntry
|
||||
int nodeIndex;
|
||||
float3 center;
|
||||
float halfSize;
|
||||
float entry;
|
||||
float exit;
|
||||
};
|
||||
|
||||
// Buffers
|
||||
@ -167,6 +169,9 @@ void CSMain(uint3 id : SV_DispatchThreadID)
|
||||
root.nodeIndex = rootIndex;
|
||||
root.center = rootCenter;
|
||||
root.halfSize = rootHalfSize;
|
||||
|
||||
if (IntersectAABB_fast(rootCenter, rootHalfSize, b.origin, r.direction, invDir, root.entry, root.exit))
|
||||
{
|
||||
stack[sp++] = root;
|
||||
|
||||
bool hasHit = false;
|
||||
@ -180,17 +185,14 @@ void CSMain(uint3 id : SV_DispatchThreadID)
|
||||
|
||||
LinearNode n = nodes[e.nodeIndex];
|
||||
|
||||
float tEntry, tExit;
|
||||
if (!IntersectAABB_fast(e.center, e.halfSize, b.origin, r.direction, invDir, tEntry, tExit)) continue;
|
||||
|
||||
// prune with current best
|
||||
if (tEntry >= outHit.maxDistance) continue;
|
||||
if (e.entry >= outHit.maxDistance) continue;
|
||||
|
||||
if (n.isLeaf == 1u)
|
||||
{
|
||||
if (n.isOccupied == 1u)
|
||||
{
|
||||
float tHit = max(tEntry, 0.0);
|
||||
float tHit = max(e.entry, 0.0);
|
||||
if (tHit < outHit.maxDistance)
|
||||
{
|
||||
// found a closer hit — commit minimal info, defer heavy ops
|
||||
@ -202,12 +204,13 @@ void CSMain(uint3 id : SV_DispatchThreadID)
|
||||
continue;
|
||||
}
|
||||
|
||||
// Non-leaf: gather children that intersect and their tEntry (small array)
|
||||
// Non-leaf: gather children that intersect and their entry (small array)
|
||||
uint childMask = n.childMask;
|
||||
// small local arrays
|
||||
float childT[8];
|
||||
int childIdx[8];
|
||||
float3 childCenter[8];
|
||||
float childEntry[8];
|
||||
float childExit[8];
|
||||
|
||||
int childCount = 0;
|
||||
|
||||
@ -228,14 +231,14 @@ void CSMain(uint3 id : SV_DispatchThreadID)
|
||||
float3 cCenter = e.center + offsetVec;
|
||||
|
||||
// pretest intersection with child AABB to get tEntry
|
||||
float ctEntry, ctExit;
|
||||
if (!IntersectAABB_fast(cCenter, childHalf, b.origin, r.direction, invDir, ctEntry, ctExit)) continue;
|
||||
if (ctEntry >= outHit.maxDistance) continue; // prune child if already farther than best hit
|
||||
if (!IntersectAABB_fast(cCenter, childHalf, b.origin, r.direction, invDir, e.entry, e.exit)) continue;
|
||||
if (e.entry >= outHit.maxDistance) continue; // prune child if already farther than best hit
|
||||
|
||||
// store for near-first push
|
||||
childT[childCount] = ctEntry;
|
||||
childIdx[childCount] = cIndex;
|
||||
childCenter[childCount] = cCenter;
|
||||
childEntry[childCount] = e.entry;
|
||||
childExit[childCount] = e.exit;
|
||||
// temporarily store center and half? we recompute on push
|
||||
childCount++;
|
||||
}
|
||||
@ -243,30 +246,36 @@ void CSMain(uint3 id : SV_DispatchThreadID)
|
||||
// sort children by childT ascending (insertion sort on at most 8 elements)
|
||||
for (int a = 1; a < childCount; ++a)
|
||||
{
|
||||
float keyT = childT[a];
|
||||
int keyIdx = childIdx[a];
|
||||
float3 keyCenter = childCenter[a];
|
||||
float keyEntry = childEntry[a];
|
||||
float keyExit = childExit[a];
|
||||
|
||||
int j = a - 1;
|
||||
while (j >= 0 && childT[j] > keyT) {
|
||||
childT[j+1] = childT[j];
|
||||
while (j >= 0 && childEntry[j] > keyEntry) {
|
||||
childIdx[j+1] = childIdx[j];
|
||||
childCenter[j+1] = childCenter[j];
|
||||
childEntry[j+1] = childEntry[j];
|
||||
childExit[j+1] = childExit[j];
|
||||
j--;
|
||||
}
|
||||
childT[j+1] = keyT;
|
||||
childIdx[j+1] = keyIdx;
|
||||
childCenter[j+1] = keyCenter;
|
||||
childEntry[j+1] = keyEntry;
|
||||
childExit[j+1] = keyExit;
|
||||
}
|
||||
|
||||
StackEntry childEntry;
|
||||
StackEntry nextChildEntry;
|
||||
// push children in reverse order (so the nearest is popped first) if stack has room
|
||||
for (int c = childCount - 1; c >= 0; --c)
|
||||
{
|
||||
// push
|
||||
childEntry.nodeIndex = childIdx[c];
|
||||
childEntry.center = childCenter[c];
|
||||
childEntry.halfSize = childHalf;
|
||||
stack[sp++] = childEntry;
|
||||
nextChildEntry.nodeIndex = childIdx[c];
|
||||
nextChildEntry.center = childCenter[c];
|
||||
nextChildEntry.halfSize = childHalf;
|
||||
nextChildEntry.entry = childEntry[c];
|
||||
nextChildEntry.exit = childExit[c];
|
||||
stack[sp++] = nextChildEntry;
|
||||
}
|
||||
}
|
||||
|
||||
@ -289,3 +298,4 @@ void CSMain(uint3 id : SV_DispatchThreadID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ public class Player : MonoBehaviour
|
||||
|
||||
VoxelRaycastGPU.Ray[] rays = new VoxelRaycastGPU.Ray[rayCount];
|
||||
FillRaysArray(rays);
|
||||
voxelManager.gpuRayCaster.Init(rayCount, rays, 3);
|
||||
voxelManager.gpuRayCaster.Init(rayCount, rays, 5);
|
||||
}
|
||||
|
||||
void Cast( ref VoxelRaycastGPU.BatchData[] batchData, int batchCount, int iIteration )
|
||||
|
||||
@ -95,6 +95,7 @@ public class VoxelRaycastGpuManager
|
||||
countBuffer.GetData(countArr);
|
||||
currentCount = countArr[0];
|
||||
|
||||
/**
|
||||
sw.Stop();
|
||||
|
||||
VoxelRaycastGPU.BatchData[] hits = new VoxelRaycastGPU.BatchData[currentCount];
|
||||
@ -107,6 +108,7 @@ public class VoxelRaycastGpuManager
|
||||
}
|
||||
|
||||
sw.Start();
|
||||
*/
|
||||
|
||||
iteration++;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user