Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Question Burstable lambda support for jobs

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

  1. jasonboukheir3

    jasonboukheir3

    Joined:
    Apr 13, 2022
    Posts:
    81
    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();