I try to optimize pefrormance using IJobParallelFor in Unity code: Unfortunatelly I met error likie this:
System.IndexOutOfRangeException: Index {0} is out of restricted IJobParallelFor range [{1}...{2}] in ReadWriteBuffer.
I tried to use
[NativeDisableParallelForRestriction]
and
[NativeDisableContainerSafetyRestriction]
but without effect
[BurstCompile(CompileSynchronously = true)]
public struct DilationJob : IJobParallelFor
{
[ReadOnly]
public NativeArray<Color32> colorsArray;
public NativeArray<int> voxelToColor;
public int kernelSize;
public NativeArray<int> neighboursArray;
public int cubeNumber;
public void Execute(int index)
{
int dimx = 512;
int dimy = 512;
//int[] neighboursArray = new int[kernelSize * kernelSize * kernelSize];
int listIndex = 0;
for (int i = -1; i < kernelSize - 1; i++)
{
for (int j = -1; j < kernelSize - 1; j++)
{
for (int k = -1; k < kernelSize - 1; k++)
{
int neigbourIndex = (i + 1) * (j + 1) * kernelSize + (j + 1) * kernelSize + (k + 1);
if (neigbourIndex < 0)
{
neigbourIndex = 0;
}
neighboursArray[neigbourIndex] =
index + k * dimx * dimy + j * dimx + i;
if (neighboursArray[neigbourIndex] < colorsArray.Length && neighboursArray[neigbourIndex] >= 0 &&
colorsArray[neighboursArray[neigbourIndex]].b == 255)
{
voxelToColor[listIndex] = index;
listIndex++;
}
}
}
}
}
}