Trying to add some optimization. Need to remove an intersection test now
This commit is contained in:
@ -28,6 +28,7 @@ public class VoxelRaycastGpuManager
|
||||
int groupsX;
|
||||
|
||||
int maxRaycastPerIteration;
|
||||
int maxIterations = 3;
|
||||
|
||||
int threadsY = 8;
|
||||
int maxGroupsY = 65535;
|
||||
@ -59,25 +60,28 @@ public class VoxelRaycastGpuManager
|
||||
|
||||
public VoxelRaycastGPU.BatchData[] Raycast(in VoxelRaycastGPU.BatchData[] batchData, int datasLenght)
|
||||
{
|
||||
int iteration = 0;
|
||||
int currentCount = datasLenght;
|
||||
int previousCount = datasLenght;
|
||||
int totalCastNumber = 0;
|
||||
|
||||
Stopwatch sw = Stopwatch.StartNew();
|
||||
|
||||
int iteration = 0;
|
||||
int currentCount = batchData.Length;
|
||||
int previousCount = currentCount;
|
||||
|
||||
datasBuffer.SetCounterValue(0);
|
||||
datasBuffer.SetData(batchData, 0, 0, currentCount);
|
||||
|
||||
while (iteration < 5 && currentCount > 0)
|
||||
while (iteration < maxIterations && currentCount > 0)
|
||||
{
|
||||
totalCastNumber += currentCount;
|
||||
previousCount = currentCount;
|
||||
|
||||
hitBuffer.SetCounterValue(0);
|
||||
datasBuffer.SetCounterValue(0);
|
||||
|
||||
raycastShader.SetBuffer(kernel, "batchDatas", datasBuffer );
|
||||
raycastShader.SetBuffer(kernel, "hits", hitBuffer);
|
||||
|
||||
/**
|
||||
Stopwatch sw = Stopwatch.StartNew();
|
||||
*/
|
||||
|
||||
for (int y = 0; y < currentCount; y += threadsY * maxGroupsY)
|
||||
{
|
||||
int remaining = currentCount - y;
|
||||
@ -86,70 +90,49 @@ public class VoxelRaycastGpuManager
|
||||
raycastShader.Dispatch(kernel, groupsX, dispatchGroupsY, 1);
|
||||
}
|
||||
|
||||
|
||||
ComputeBuffer.CopyCount(hitBuffer, countBuffer, 0);
|
||||
int[] countArr = new int[1];
|
||||
countBuffer.GetData(countArr);
|
||||
currentCount = countArr[0];
|
||||
|
||||
|
||||
/**
|
||||
VoxelRaycastGPU.BatchData[] hits = new VoxelRaycastGPU.BatchData[currentCount];
|
||||
hitBuffer.GetData(hits, 0, 0, currentCount);
|
||||
for( int i = 0; i < hits.Length; i++ )
|
||||
{
|
||||
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
||||
sphere.transform.position = hits[i].origin;
|
||||
sphere.transform.localScale = Vector3.one * 0.5f;
|
||||
}
|
||||
*/
|
||||
sw.Stop();
|
||||
|
||||
VoxelRaycastGPU.BatchData[] hits = new VoxelRaycastGPU.BatchData[currentCount];
|
||||
hitBuffer.GetData(hits, 0, 0, currentCount);
|
||||
for( int i = 0; i < hits.Length; i++ )
|
||||
{
|
||||
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
||||
sphere.transform.position = hits[i].origin;
|
||||
sphere.transform.localScale = Vector3.one * 0.5f;
|
||||
}
|
||||
|
||||
/**
|
||||
sw.Stop();
|
||||
UnityEngine.Debug.Log($"Dispatch done in {sw.Elapsed.TotalMilliseconds}ms for {previousCount*raysPerBatch} casts retrieving {currentCount} hits");
|
||||
*/
|
||||
sw.Start();
|
||||
|
||||
iteration++;
|
||||
|
||||
if (currentCount > 0 && iteration < 5)
|
||||
if (currentCount > 0 && iteration < maxIterations )
|
||||
{
|
||||
datasBuffer = hitBuffer;
|
||||
/**
|
||||
if (currentCount * raysPerBatch > maxRaycastPerIteration && iteration < 5)
|
||||
{
|
||||
sw = Stopwatch.StartNew();
|
||||
currentCount = Clustering( maxRaycastPerIteration / raysPerBatch);
|
||||
sw.Stop();
|
||||
|
||||
UnityEngine.Debug.Log($"Clustering done in {sw.Elapsed.TotalMilliseconds}ms for {currentCount} casts");
|
||||
|
||||
VoxelRaycastGPU.BatchData[] hits = new VoxelRaycastGPU.BatchData[currentCount];
|
||||
datasBuffer.GetData(hits, 0, 0, currentCount);
|
||||
|
||||
for (int i = 0; i < currentCount; i++)
|
||||
{
|
||||
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
||||
sphere.transform.position = hits[i].origin;
|
||||
sphere.transform.localScale = Vector3.one * 0.5f;
|
||||
}
|
||||
}
|
||||
*/
|
||||
(datasBuffer,hitBuffer) = (hitBuffer,datasBuffer);
|
||||
}
|
||||
|
||||
}
|
||||
sw.Stop();
|
||||
|
||||
VoxelRaycastGPU.BatchData[] result = new VoxelRaycastGPU.BatchData[previousCount];
|
||||
hitBuffer.GetData(result, 0, 0, previousCount);
|
||||
|
||||
|
||||
UnityEngine.Debug.Log($"Raycast done in {sw.Elapsed.TotalMilliseconds}ms for a total of {totalCastNumber} raycasts");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Init(int nbRaysPerBatch, in VoxelRaycastGPU.Ray[] rays)
|
||||
public void Init(int nbRaysPerBatch, in VoxelRaycastGPU.Ray[] rays, int maxIterations)
|
||||
{
|
||||
maxRaycastPerIteration = 1000000;
|
||||
raysPerBatch = nbRaysPerBatch;
|
||||
|
||||
this.maxIterations = maxIterations;
|
||||
|
||||
countBuffer = new ComputeBuffer(1, sizeof(int), ComputeBufferType.Raw);
|
||||
|
||||
// Flatten octree
|
||||
@ -157,7 +140,7 @@ public class VoxelRaycastGpuManager
|
||||
int nodeStride = Marshal.SizeOf(typeof(LinearNode)); // should be 64
|
||||
|
||||
hitBuffer = new ComputeBuffer(maxRaycastPerIteration * raysPerBatch, batchDataClassSize, ComputeBufferType.Append);
|
||||
datasBuffer = new ComputeBuffer(maxRaycastPerIteration, batchDataClassSize, ComputeBufferType.Default);
|
||||
datasBuffer = new ComputeBuffer(maxRaycastPerIteration, batchDataClassSize, ComputeBufferType.Append);
|
||||
|
||||
rayBuffer = new ComputeBuffer(rays.Length, Marshal.SizeOf(typeof(VoxelRaycastGPU.Ray)), ComputeBufferType.Default);
|
||||
rayBuffer.SetData(rays, 0, 0, rays.Length);
|
||||
|
||||
Reference in New Issue
Block a user