Cannot create a NativeArray that is Burst Compatible even with the Temp Allocator

NativeArray<float> vals = new NativeArray<float>(8, Allocator.Temp);
        vals[0] = _voxels[xi + yi * SizeX + zi * SizeX * SizeY];
        vals[1] = _voxels[(xi + 1) + yi * SizeX + zi * SizeX * SizeY];
        vals[2] = _voxels[(xi + 1) + (yi + 1) * SizeX + zi * SizeX * SizeY];
        vals[3] = _voxels[xi + (yi + 1) * SizeX + zi * SizeX * SizeY];

        vals[4] = _voxels[xi + yi * SizeX + (zi + 1) * SizeX * SizeY];
        vals[5] = _voxels[(xi + 1) + yi * SizeX + (zi + 1) * SizeX * SizeY];
        vals[6] = _voxels[(xi + 1) + (yi + 1) * SizeX + (zi + 1) * SizeX * SizeY];
        vals[7] = _voxels[xi + (yi + 1) * SizeX + (zi + 1) * SizeX * SizeY];

Is there nay sort of work around for this? I thought that in Unity 2018.3, that this would be fixed? (I am running 2019 ALPHA but I tested it with the Unity 2018.3 BETA too)

1 Like

Are you trying to run the above code in a Job? If so, allocating a native array isn’t supported in jobs, it must be done on the main thread. Everything else should work fine in a job.

UT should have put “Coming Soon” section in the docs. It will be massively helpful for both design decision and learning.

  • Allocate in-job
  • Blittable bool
  • Burst ECB
    etc.

I am indeed. The only thing is… I NEED to be able to do this in job code since I am currently working on a Marching Cubes implementation, however, I could probably avoid the need to make this array by going straight to getting values from the _voxel array. I can do the same for corners. Then, I can actually use the pre existing vertices buffer as a placeholder for my values that would normally be stored into the verlist NativeArray.

you can allocate a big array in onUpdate

var megaVertexBuffer = new NativeArray<short>(buffer_stride * _group.Length, Allocator.TempJob);
   
 var surfJob = new SurfaceJobP
        {
       ...
            megaVertexBuffer = megaVertexBuffer

Define it in your job like that

  private struct SurfaceJobP : IJobProcessComponentDataWithEntity<VoxelChunk>
    {
        [DeallocateOnJobCompletion] [NativeDisableParallelForRestriction] internal NativeArray<short> megaVertexBuffer;

And use it like that :

 public void Execute(Entity e, int i, ref VoxelChunk vox)
        {        
            var vertexBuffer = megaVertexBuffer.Slice(i * buffer_stride, buffer_stride);

That works fine in burst, and DeallocateOnJobCompletion is smart enough.