Search Unity

Question Burstable lambda support for jobs

Discussion in 'DOTS Dev Blitz Day 2022 - Q&A' started by Deleted User, Dec 7, 2022.

  1. Deleted User

    Deleted User

    Guest

    Any plans that the lambda IL weaving in
    Entities.ForEach
    could be generalized to bursted jobs?

    For example, using them as special delegate types in jobs:

    Code (CSharp):
    1. public delegate void BurstableFunc<TArg0, TResult>(in TArg0 arg0, ref TResult result);
    2.  
    3. [BurstCompile]
    4. public struct FilterJob : IJob
    5. {
    6.   public BurstableFunc<int, bool> Predicate;
    7.   public NativeArray<int> Input;
    8.   public NativeList<int> Output;
    9.  
    10.   public void Execute()
    11.   {
    12.     for (var i = 0; i < Input.Length; i++)
    13.     {
    14.       if (Predicate.Invoke(Input[i]))
    15.       {
    16.         Output.Add(Input[i]);
    17.       }
    18.     }
    19.   }
    20. }
    21.  
    22. var input = new NativeArray<int>(5, Allocator.TempJob);
    23. for (var i = 0; i < input.Length; i++)
    24.   input[i] = i;
    25.  
    26. var output = new NativeList<int>(5, Allocator.TempJob);
    27.  
    28. new FilterJob
    29. {
    30.   Input = input,
    31.   Predicate = (in x, ref isGreaterThanTwo) => isGreaterThanTwo = x > 2,
    32.   Output = output
    33. }.Schedule().Complete();