Search Unity

Construct TransformAccess manually for IJobParallelFor

Discussion in 'Data Oriented Technology Stack' started by Solovykh, Feb 12, 2020.

  1. Solovykh

    Solovykh

    Joined:
    Aug 28, 2017
    Posts:
    59
    I'm trying to run a job on a bunch of transforms, so I looked into using IJobParallelForTransform. However, all my transforms have the same root, and I don't want to change that due to project setup, so my job runs all in one thread.

    So, to take advantage of multiple threads I've moved my job to IJobParallelFor. However, I still want to do a bunch operations to my transforms without having to maintain some struct representations of them on the job, and then copying back the struct representations into the actual transforms when the job was done.

    So, I was wondering if it's possible to get a TransformAccess from a TransformAccessArray inside a job?

    looking at IJobParallelForExtensions.cs it seems like i might be able to use reflection to get access to
    TransformAccessArray.GetSortedTransformAccess and then use that somehow?
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    2,516
    I may be totally wrong on this, but I think the issue is that Unity needs to maintain consistent state of both local and world space transforms and so having a parent be manipulated on a different thread from a child could be problematic. It would be cool if there was some API to get a specialized TransformAccessArray of the descendants (not including the root which would be locked) as this is a pretty common use case for anyone using MonoBehaviours and jobs.

    I do the trick of maintaining struct representations and copying in the few cases where I need to, and it has been fast enough.
     
  3. Solovykh

    Solovykh

    Joined:
    Aug 28, 2017
    Posts:
    59
    Yeah, unfortunately copying take a while for me.

    TransformAccessArray.GetSortedTransformAccess returns a TransformAccess* but unfortunately TransformAccessArray.GetSortedTransformAccess throws an error that says "PrepareTransformAccessArray must be called before calling GetSortedTransformAccessPtr" which is a method we can't get to :(
     
  4. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,311
    If you want to update transforms via IJobParallelForTransform in parallel, only choice is split the roots. IJobParallelForTransform is I'm fairly certain not just a custom job type, it has specific hooks into the engine which is probably why it's in UnityEngine.CoreModule.

    Copying data can be done performantly but how depends on the details. And whether the work required is worth it. But it's absolutely doable although some refactoring might be in order.
     
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    2,516
    That surprises me. How long is a "while" compared to the job doing the transform manipulations? Are you doing the copy using Burst? Is it just one root or are you doing this to multiple different roots where the copies could be done in parallel to each other?

    This surprises me because I usually can't tolerate more than 10000 GameObjects in a game but doing the copy for this takes well under a millisecond.
     
unityunity