Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Burst Job support delegates

Discussion in 'Data Oriented Technology Stack' started by xman7c7, Jan 18, 2019.

  1. xman7c7

    xman7c7

    Joined:
    Oct 28, 2016
    Posts:
    28
    In newer version of burst package 0.2.4-preview.41 i have found that burst now support delegates here.
    You need to manually add BURST_FEATURE_FUNCTION_POINTER if you want to create pointer to delegate function.

    Anyway i have little problem with it, struct which holding pointer to delegate dont have attribute [NativeDisableUnsafePtrRestriction] so i have add it and it works.
    Code (CSharp):
    1. public struct FunctionPointer<T>
    2. {
    3.     [NativeDisableUnsafePtrRestriction] private readonly IntPtr _ptr;
    4.  
    5.     public FunctionPointer(IntPtr ptr)
    6.     {
    7.         _ptr = ptr;
    8.     }
    9.  
    10.     public T Invoke => (T) (object) Marshal.GetDelegateForFunctionPointer(_ptr, typeof(T));
    11. }
    Code (CSharp):
    1. public struct SomeData : IComponentData
    2. {
    3.     public int Value;
    4. }
    5.  
    6. public delegate int CustomFunction(int value);
    7.  
    8. [BurstCompile]
    9. public struct ChangeValue : IJobChunk
    10. {
    11.     public ArchetypeChunkComponentType<SomeData> SomeDataType;
    12.     public FunctionPointer<CustomFunction> CustomFunction;
    13.     public int Value;
    14.  
    15.     public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
    16.     {
    17.         NativeArray<SomeData> someDatas = chunk.GetNativeArray(SomeDataType);
    18.         for (int i = 0; i < chunk.Count; i++)
    19.         {
    20.             var someData = someDatas[i];
    21.             someData.Value = CustomFunction.Invoke(Value);
    22.             someDatas[i] = someData;
    23.         }
    24.     }
    25. }
    26.  
    27. public class UpdateSystem : ComponentSystem
    28. {
    29.     private ComponentGroup _group;
    30.     private int _number;
    31.  
    32.     protected override void OnCreateManager()
    33.     {
    34.         base.OnCreateManager();
    35.         EntityManager.CreateEntity(typeof(SomeData));
    36.         _group = GetComponentGroup(typeof(SomeData));
    37.     }
    38.  
    39.     private int CustomFunctionImplementation(int value)
    40.     {
    41.         return value + 1;
    42.     }
    43.    
    44.     protected override void OnUpdate()
    45.     {
    46.         new ChangeValue
    47.         {
    48.             SomeDataType = GetArchetypeChunkComponentType<SomeData>(),
    49. //                CustomFunction = BurstCompiler.CompileFunctionPointer((CustomFunction) CustomFunctionImplementation),
    50.             CustomFunction = new FunctionPointer<CustomFunction>(
    51.                 Marshal.GetFunctionPointerForDelegate((CustomFunction) CustomFunctionImplementation)
    52.             ),
    53.             Value = _number++
    54.         }.Schedule(_group).Complete();
    55.     }
    56. }