Search Unity

IJobParallelForTransform - transform.position's sync-back

Discussion in 'Entity Component System' started by Liviuss, Jun 27, 2018.

  1. Liviuss

    Liviuss

    Joined:
    Apr 2, 2014
    Posts:
    101
    Hi,

    I have a job dependency where i do:
    1. MoveJob - move transforms
    2. RotateJob - rotate transforms
    3. RangeCheckJob - check distances

    All jobs are scheduled with dependency 3-2-1 and use the same generic data struct as input to read/write to.
    MoveJob write to the struct the position after movement is applied.
    RangeCheckJob use positions to do range check.

    After job.Complete() is called, i use transform.position's from data struct to draw lines(with LineRenderer) between moved objects.

    If i schedule the job within the same frame (Start on Update -> Complete in LateUpdate) positions of the line points coincide with moved objects positions.

    If i schedule the job wit a 1 frame delay (Start/Complete in LateUpdate) the positions of object on screen and positions of the lines are different.

    Anyone have ideas about how to sync-back positions or it's a bug?


    Thanks
     
    Last edited: Jun 27, 2018
  2. LazyGameDevZA

    LazyGameDevZA

    Joined:
    Nov 10, 2016
    Posts:
    143
    Is it possible to provide a test project as example? It's a important to remember that the one frame delay should have an effect on this. Try checking where the objects are and keeping that position in mind on the next frame. That might coincide with the lines as expected.
     
  3. Liviuss

    Liviuss

    Joined:
    Apr 2, 2014
    Posts:
    101
    I still don't understand the impact of the delay.
    My lifecycle is simple:

    void LateUpdate(){
    1. CompleteJobs()

    2. ProcessDataFromJobs()
    -> DrawLines (Get Positions Of Moved Objects and Draw Lines between them)


    3. ScheduleJobs()
    -> Movement
    -> Rotations
    -> Collisions
    }

    And i expect that in LateUpdate, right before rendering, every processed transform.position will be in sync with what i get from datas from jobs. Instead, transforms continue to update their positions because of P.3. ScheduleJobs() (that's my thoughts)
    so when rendering is done, objects are drawn with new positions. That's my problem! i don't know those new positions!
    Or maybe i miss something.
     
    Last edited: Jun 27, 2018
  4. LazyGameDevZA

    LazyGameDevZA

    Joined:
    Nov 10, 2016
    Posts:
    143
    I'm not sure that I'm following exactly with what is happening. A sample project that demonstrates this will be much easier to diagnose.
     
  5. Liviuss

    Liviuss

    Joined:
    Apr 2, 2014
    Posts:
    101
  6. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    You should call Schedule() then Complete() and not Complete() then Schedule() as it currently is?
     
  7. Liviuss

    Liviuss

    Joined:
    Apr 2, 2014
    Posts:
    101
    It's a 1 frame delay job.
     
  8. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    Fundamentally all jobs scheduled are completed at the end of frame and cannot be delayed (they parallel with other jobs in the same frame) so what you get here is the correct behaviour :

    1. You call complete first but the job is already completed from the last frame anyways. This complete is just to unlock the native array. You get the position of the last computation. The line use this position.
    2. You schedule a new ParallelForTransform job.
    3. The frame ends then that job is automatically completed (without unlocking the native array)
    4. The new computed position updates the sphere automatically because TransformAccessArray linked with the Transform. So in the end 1. and 4. is not the same position.

    At the end of frame, if you want the old position (1.) drawn to screen while the object actually all have new position (4.), you should not use the transform system as it is designed draw things that match the current value. You would have to draw manually with Graphics.Draw___ and use the old data.
     
  9. Liviuss

    Liviuss

    Joined:
    Apr 2, 2014
    Posts:
    101
    Just deleted Complete() from the code and yeah, seems that there is a hard sync point that complete the job. To bad there is nothing about this in https://github.com/Unity-Technologi...ob/master/Documentation/content/job_system.md

    Thanks for the help.
     
    Last edited: Jun 28, 2018