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.

Question How to determin when an IJobEntity derived class has completed execution after ScheduleParallel()?

Discussion in 'Entity Component System' started by norlesh, Mar 16, 2023.

  1. norlesh

    norlesh

    Joined:
    Feb 10, 2019
    Posts:
    5
    I am running an IJobEntity based job that updates components for every entity using job.ScheduleParallel() however I have a second job to run after this that depends on reading from ALL the newly updated component values; without a JobHandle from the first job I'm stuck scratching my head on how to coordinate this.
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,722
    You need the JobHandle. Why do you not have it?
     
  3. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    89
    Thanks to the codegen IJobEntity dependencies are managed automatically using SystemBase/SystemState Dependency member.

    For example this:
    Code (CSharp):
    1.  
    2. public partial class PlayerControllerSystem: SystemBase
    3. {
    4.     [BurstCompile]
    5.     partial struct ProcessInputJob: IJobEntity
    6.     {
    7.         void Execute(in AnimatorControllerParameterIndexTableComponent paramIndexTable, ref DynamicBuffer<AnimatorControllerParameterComponent> allParams)
    8.         {...}
    9.     }
    10.  
    11.     protected override void OnUpdate()
    12.     {
    13.         var processInputJob = new ProcessInputJob()
    14.         {...};
    15.  
    16.         processInputJob.ScheduleParallel();
    17.     }
    18.  
    19.  
    Will be codegenerated into this:
    Code (CSharp):
    1.  
    2.     [global::System.Runtime.CompilerServices.CompilerGenerated]
    3.     public partial class PlayerControllerSystem : Unity.Entities.SystemBase
    4.     {
    5.         [Unity.Entities.DOTSCompilerPatchedMethod("OnUpdate")]
    6.         void __OnUpdate_1817F1CB()
    7.         {
    8.             ...
    9.             #line 170 ".../Samples/Scripts/PlayerControllerSystem.cs"
    10.             var processInputJob = new ProcessInputJob() {...};
    11.             #line 181 "../Samples/Scripts/PlayerControllerSystem.cs"
    12.             this.CheckedStateRef.Dependency = __ScheduleViaJobChunkExtension_0(processInputJob, __query_1126464610_0, this.CheckedStateRef.Dependency, ref this.CheckedStateRef);
    13.         }
    14.  
    As you can see, job scheduling is performed by using 'Dependency' as dependency, and assigning to the same 'Dependency' member as new JobHandle. This informaiton gives us the knowldge that sequential IJobEntity executions will lead to sequential job dependency. This is not always performant and desirable comparing to manual job dependency handling.

    Of course you can use it in own code too.
     
    norlesh likes this.
  4. norlesh

    norlesh

    Joined:
    Feb 10, 2019
    Posts:
    5
    Thanks, how did you access the codgen output for future reference?
     
  5. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    89
    Define 'DOTS_OUTPUT_SOURCEGEN_FILES' script define symbol in Project Settings -> Player preferences, and look into 'Temp/GeneratedCode' directory.
     
  6. azmi_unity

    azmi_unity

    Joined:
    Dec 13, 2020
    Posts:
    54
    Are dependencies handled automatically for IJob and IJobParallelFor as well?
     
  7. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,722
    No. You need to assign them manually.
     
    azmi_unity likes this.