Shader optimizations

This commit is contained in:
2025-10-23 07:55:29 +02:00
parent 4ef1b04156
commit 40a6d832bc
3 changed files with 133 additions and 121 deletions

View File

@ -52,6 +52,8 @@ struct StackEntry
int nodeIndex; int nodeIndex;
float3 center; float3 center;
float halfSize; float halfSize;
float entry;
float exit;
}; };
// Buffers // Buffers
@ -167,6 +169,9 @@ void CSMain(uint3 id : SV_DispatchThreadID)
root.nodeIndex = rootIndex; root.nodeIndex = rootIndex;
root.center = rootCenter; root.center = rootCenter;
root.halfSize = rootHalfSize; root.halfSize = rootHalfSize;
if (IntersectAABB_fast(rootCenter, rootHalfSize, b.origin, r.direction, invDir, root.entry, root.exit))
{
stack[sp++] = root; stack[sp++] = root;
bool hasHit = false; bool hasHit = false;
@ -180,17 +185,14 @@ void CSMain(uint3 id : SV_DispatchThreadID)
LinearNode n = nodes[e.nodeIndex]; 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 // prune with current best
if (tEntry >= outHit.maxDistance) continue; if (e.entry >= outHit.maxDistance) continue;
if (n.isLeaf == 1u) if (n.isLeaf == 1u)
{ {
if (n.isOccupied == 1u) if (n.isOccupied == 1u)
{ {
float tHit = max(tEntry, 0.0); float tHit = max(e.entry, 0.0);
if (tHit < outHit.maxDistance) if (tHit < outHit.maxDistance)
{ {
// found a closer hit — commit minimal info, defer heavy ops // found a closer hit — commit minimal info, defer heavy ops
@ -202,12 +204,13 @@ void CSMain(uint3 id : SV_DispatchThreadID)
continue; 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; uint childMask = n.childMask;
// small local arrays // small local arrays
float childT[8];
int childIdx[8]; int childIdx[8];
float3 childCenter[8]; float3 childCenter[8];
float childEntry[8];
float childExit[8];
int childCount = 0; int childCount = 0;
@ -228,14 +231,14 @@ void CSMain(uint3 id : SV_DispatchThreadID)
float3 cCenter = e.center + offsetVec; float3 cCenter = e.center + offsetVec;
// pretest intersection with child AABB to get tEntry // pretest intersection with child AABB to get tEntry
float ctEntry, ctExit; if (!IntersectAABB_fast(cCenter, childHalf, b.origin, r.direction, invDir, e.entry, e.exit)) continue;
if (!IntersectAABB_fast(cCenter, childHalf, b.origin, r.direction, invDir, ctEntry, ctExit)) continue; if (e.entry >= outHit.maxDistance) continue; // prune child if already farther than best hit
if (ctEntry >= outHit.maxDistance) continue; // prune child if already farther than best hit
// store for near-first push // store for near-first push
childT[childCount] = ctEntry;
childIdx[childCount] = cIndex; childIdx[childCount] = cIndex;
childCenter[childCount] = cCenter; childCenter[childCount] = cCenter;
childEntry[childCount] = e.entry;
childExit[childCount] = e.exit;
// temporarily store center and half? we recompute on push // temporarily store center and half? we recompute on push
childCount++; childCount++;
} }
@ -243,30 +246,36 @@ void CSMain(uint3 id : SV_DispatchThreadID)
// sort children by childT ascending (insertion sort on at most 8 elements) // sort children by childT ascending (insertion sort on at most 8 elements)
for (int a = 1; a < childCount; ++a) for (int a = 1; a < childCount; ++a)
{ {
float keyT = childT[a];
int keyIdx = childIdx[a]; int keyIdx = childIdx[a];
float3 keyCenter = childCenter[a]; float3 keyCenter = childCenter[a];
float keyEntry = childEntry[a];
float keyExit = childExit[a];
int j = a - 1; int j = a - 1;
while (j >= 0 && childT[j] > keyT) { while (j >= 0 && childEntry[j] > keyEntry) {
childT[j+1] = childT[j];
childIdx[j+1] = childIdx[j]; childIdx[j+1] = childIdx[j];
childCenter[j+1] = childCenter[j]; childCenter[j+1] = childCenter[j];
childEntry[j+1] = childEntry[j];
childExit[j+1] = childExit[j];
j--; j--;
} }
childT[j+1] = keyT;
childIdx[j+1] = keyIdx; childIdx[j+1] = keyIdx;
childCenter[j+1] = keyCenter; 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 // push children in reverse order (so the nearest is popped first) if stack has room
for (int c = childCount - 1; c >= 0; --c) for (int c = childCount - 1; c >= 0; --c)
{ {
// push // push
childEntry.nodeIndex = childIdx[c]; nextChildEntry.nodeIndex = childIdx[c];
childEntry.center = childCenter[c]; nextChildEntry.center = childCenter[c];
childEntry.halfSize = childHalf; nextChildEntry.halfSize = childHalf;
stack[sp++] = childEntry; nextChildEntry.entry = childEntry[c];
nextChildEntry.exit = childExit[c];
stack[sp++] = nextChildEntry;
} }
} }
@ -289,3 +298,4 @@ void CSMain(uint3 id : SV_DispatchThreadID)
} }
} }
} }
}

View File

@ -24,7 +24,7 @@ public class Player : MonoBehaviour
VoxelRaycastGPU.Ray[] rays = new VoxelRaycastGPU.Ray[rayCount]; VoxelRaycastGPU.Ray[] rays = new VoxelRaycastGPU.Ray[rayCount];
FillRaysArray(rays); FillRaysArray(rays);
voxelManager.gpuRayCaster.Init(rayCount, rays, 3); voxelManager.gpuRayCaster.Init(rayCount, rays, 5);
} }
void Cast( ref VoxelRaycastGPU.BatchData[] batchData, int batchCount, int iIteration ) void Cast( ref VoxelRaycastGPU.BatchData[] batchData, int batchCount, int iIteration )

View File

@ -95,6 +95,7 @@ public class VoxelRaycastGpuManager
countBuffer.GetData(countArr); countBuffer.GetData(countArr);
currentCount = countArr[0]; currentCount = countArr[0];
/**
sw.Stop(); sw.Stop();
VoxelRaycastGPU.BatchData[] hits = new VoxelRaycastGPU.BatchData[currentCount]; VoxelRaycastGPU.BatchData[] hits = new VoxelRaycastGPU.BatchData[currentCount];
@ -107,6 +108,7 @@ public class VoxelRaycastGpuManager
} }
sw.Start(); sw.Start();
*/
iteration++; iteration++;