Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

NativeQueue and Burst

Discussion in 'Burst' started by benzy54n, Dec 24, 2019.

  1. benzy54n

    benzy54n

    Joined:
    Dec 21, 2013
    Posts:
    34
    Does Burst not work with NativeQueue?

    Code (CSharp):
    1. D:\UnityDevelopment\MapGenerator\Library\PackageCache\com.unity.collections@0.1.1-preview\Unity.Collections\NativeQueue.cs(97,17): error: The managed class type `Unity.Collections.NativeQueueBlockPoolData*` is not supported. Loading from a non-readonly static field `Unity.Collections.NativeQueueBlockPool.data` is not supported by burst
    2. at Unity.Collections.NativeQueueBlockPool.get_QueueBlockPool() (at D:\UnityDevelopment\MapGenerator\Library\PackageCache\com.unity.collections@0.1.1-preview\Unity.Collections\NativeQueue.cs:97)
    3. at Unity.Collections.NativeQueue`1<System.Int32>..ctor(Unity.Collections.NativeQueue`1<int>* this, Unity.Collections.Allocator label) (at D:\UnityDevelopment\MapGenerator\Library\PackageCache\com.unity.collections@0.1.1-preview\Unity.Collections\NativeQueue.cs:306)
    4. at ContinentDetection.Execute(ContinentDetection* this) (at D:\UnityDevelopment\MapGenerator\Assets\Scripts\InitializationData\ContinentDetection.cs:156)
    5. at Unity.Jobs.IJobExtensions.JobStruct`1<ContinentDetection>.Execute(ref ContinentDetection data, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, ref Unity.Jobs.LowLevel.Unsafe.JobRanges ranges, int jobIndex) (at C:\buildslave\unity\build\Runtime\Jobs\Managed\IJob.cs:30)
    6.  
    7.  
    8. While compiling job: System.Void Unity.Jobs.IJobExtensions/JobStruct`1<ContinentDetection>::Execute(T&,System.IntPtr,System.IntPtr,Unity.Jobs.LowLevel.Unsafe.JobRanges&,System.Int32)
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Allocation NativeQueue inside Burst - no. You only can pass NativeQueue in to job as field allocated outside.
     
    Graep likes this.
  3. joshrs926

    joshrs926

    Joined:
    Jan 31, 2021
    Posts:
    111
    I just tried to do the same thing and got the same error message and read this post. So I made my own queue for use in bursted jobs. I've only lightly tested it so far. Please let me know if you try it and discover any bugs so I can fix them for my own projects too.

    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Collections.LowLevel.Unsafe;
    3. using Unity.Jobs;
    4. public struct Queue<T> where T : unmanaged
    5. {
    6.     UnsafeList<T> list;
    7.     int front;
    8.     int back;
    9.     public int Count => list.Length;
    10.     public int Capacity => list.Capacity;
    11.     public bool IsCreated => list.IsCreated;
    12.     public bool IsEmpty => list.Length <= 0;
    13.     public bool IsFull => list.Length >= list.Capacity;
    14.     public Queue(Allocator alloc, int initialCapacity = 1)
    15.     {
    16.         list = new UnsafeList<T>(initialCapacity, alloc);
    17.         front = 0;
    18.         back = -1;
    19.     }
    20.     public void Enqueue(in T value)
    21.     {
    22.         if (IsFull)
    23.         {
    24.             DoubleCapacityAndCopyValues();
    25.         }
    26.         back = (back + 1) % Capacity;
    27.         list[back] = value;
    28.         list.Length++;
    29.     }
    30.     public bool TryDequeue(out T value)
    31.     {
    32.         if (IsEmpty)
    33.         {
    34.             value = default;
    35.             return false;
    36.         }
    37.         int index = front;
    38.         front = (front + 1) % Capacity;
    39.         list.Length--;
    40.         value = list[index];
    41.         return true;
    42.     }
    43.     public bool TryPeek(out T value)
    44.     {
    45.         if (IsEmpty)
    46.         {
    47.             value = default;
    48.             return false;
    49.         }
    50.         value = list[front];
    51.         return true;
    52.     }
    53.     void DoubleCapacityAndCopyValues()
    54.     {
    55.         int oldCapacity = Capacity;
    56.         list.SetCapacity(oldCapacity + 1);
    57.         for (int i = 0; i < front; i++)
    58.         {
    59.             list[oldCapacity + i] = list[i];
    60.         }
    61.         back = front + oldCapacity - 1;
    62.     }
    63.     public void Dispose()
    64.     {
    65.         list.Dispose();
    66.     }
    67.     public JobHandle Dispose(JobHandle jobHandle)
    68.     {
    69.         return list.Dispose(jobHandle);
    70.     }
    71. }