Search Unity

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

Discussion in 'Burst' started by Matt_De_Boss_Developer, Nov 18, 2018.

  1. Matt_De_Boss_Developer

    Matt_De_Boss_Developer

    Joined:
    Oct 17, 2014
    Posts:
    46
    Code (CSharp):
    1. NativeArray<float> vals = new NativeArray<float>(8, Allocator.Temp);
    2.         vals[0] = _voxels[xi + yi * SizeX + zi * SizeX * SizeY];
    3.         vals[1] = _voxels[(xi + 1) + yi * SizeX + zi * SizeX * SizeY];
    4.         vals[2] = _voxels[(xi + 1) + (yi + 1) * SizeX + zi * SizeX * SizeY];
    5.         vals[3] = _voxels[xi + (yi + 1) * SizeX + zi * SizeX * SizeY];
    6.  
    7.         vals[4] = _voxels[xi + yi * SizeX + (zi + 1) * SizeX * SizeY];
    8.         vals[5] = _voxels[(xi + 1) + yi * SizeX + (zi + 1) * SizeX * SizeY];
    9.         vals[6] = _voxels[(xi + 1) + (yi + 1) * SizeX + (zi + 1) * SizeX * SizeY];
    10.         vals[7] = _voxels[xi + (yi + 1) * SizeX + (zi + 1) * SizeX * SizeY];
    11.  


    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)
     
    Antypodish likes this.
  2. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    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.
     
  3. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    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.
     
  4. Matt_De_Boss_Developer

    Matt_De_Boss_Developer

    Joined:
    Oct 17, 2014
    Posts:
    46
    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.
     
  5. Razmot

    Razmot

    Joined:
    Apr 27, 2013
    Posts:
    346
    you can allocate a big array in onUpdate

    Code (CSharp):
    1. var megaVertexBuffer = new NativeArray<short>(buffer_stride * _group.Length, Allocator.TempJob);
    2.    
    3.  var surfJob = new SurfaceJobP
    4.         {
    5.        ...
    6.             megaVertexBuffer = megaVertexBuffer
    7.  
    Define it in your job like that

    Code (CSharp):
    1.  
    2.   private struct SurfaceJobP : IJobProcessComponentDataWithEntity<VoxelChunk>
    3.     {
    4.         [DeallocateOnJobCompletion] [NativeDisableParallelForRestriction] internal NativeArray<short> megaVertexBuffer;
    5.  

    And use it like that :

    Code (CSharp):
    1.  
    2.  public void Execute(Entity e, int i, ref VoxelChunk vox)
    3.         {        
    4.             var vertexBuffer = megaVertexBuffer.Slice(i * buffer_stride, buffer_stride);
    5.  
    That works fine in burst, and DeallocateOnJobCompletion is smart enough.