Search Unity

Deallocate native array - multiple jobs

Discussion in 'Entity Component System' started by Spy-Shifty, Sep 25, 2018.

  1. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    I've the following case:

    I create a RaycastCommand job and I wan't to use the result in multiple other jobs.
    After all jobs have finished the NativeArray's should be dealocated by the jobs.

    The thing is, I don't want to call jobHandle.Complete() to wait for all jobs and to dealocate the native arrays manually.

    Is there a way to use DeallocateOnJobCompletion then all jobs have been finished?

    The workaround would be to create a RaycastCommandJob and therefore NativeArrays of commands and results for each job which works with the RaycastCommand...

    Code snipped:
    Code (CSharp):
    1.  
    2.  
    3.     struct JobA : IJob {
    4.         [DeallocateOnJobCompletion] [ReadOnly] public NativeArray<RaycastCommand> RaycastCommands;
    5.         [DeallocateOnJobCompletion] [ReadOnly] public NativeArray<RaycastHit> RaycastHits;
    6.  
    7.         public void Execute() {
    8.             // do something
    9.         }
    10.     }
    11.  
    12.     struct JobB : IJob {
    13.         [DeallocateOnJobCompletion] [ReadOnly] public NativeArray<RaycastCommand> RaycastCommands;
    14.         [DeallocateOnJobCompletion] [ReadOnly] public NativeArray<RaycastHit> RaycastHits;
    15.  
    16.         public void Execute() {
    17.             // do something
    18.         }
    19.     }
    20.  
    21. protected override JobHandle OnUpdate(JobHandle inputDeps) {
    22.         if (!Input.GetMouseButtonDown(0)) {
    23.             return inputDeps;
    24.         }
    25.  
    26.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    27.         NativeArray<RaycastCommand> raycastCommands = new NativeArray<RaycastCommand>(1, Allocator.TempJob);
    28.         NativeArray<RaycastHit> raycastHits = new NativeArray<RaycastHit>(1, Allocator.TempJob);
    29.         raycastCommands[0] = new RaycastCommand(ray.origin, ray.direction, maxHits: 1);
    30.         inputDeps = RaycastCommand.ScheduleBatch(raycastCommands, raycastHits, 1, inputDeps);
    31.      
    32.         var jobA = new JobA { RaycastCommands = raycastCommands, RaycastHits = raycastHits };
    33.         var jobB = new JobB { RaycastCommands = raycastCommands, RaycastHits = raycastHits };
    34.         inputDeps = jobA.Schedule(inputDeps);
    35.         inputDeps = jobB.Schedule(inputDeps);
    36.         return inputDeps;
    37.     }
     
  2. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    157
    I'm not sure if this would be the best idea... but you could create a job that does nothing except deallocate those arrays. Combine all your other jobs in to one job handle and pass that do this job as its dependency.
     
  3. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Sometimes it's so simple...:D

    Thank you
     
  4. TNY_O_O

    TNY_O_O

    Joined:
    Dec 9, 2018
    Posts:
    1
    You could try [DeallocateOnJobCompletion] as a clarification like:

    Code (CSharp):
    1. public struct MyJob : IJob
    2. {
    3.     [DeallocateOnJobCompletion]
    4.     public NativeArray<byte> data;
    5.     {...}
    6. }
    But there is an issue here saying this may not work sometimes...