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. Dismiss Notice

Writing to PhysicsWorld in parallel job

Discussion in 'Physics for ECS' started by Lukas_Kastern, Apr 17, 2021.

  1. Lukas_Kastern

    Lukas_Kastern

    Joined:
    Aug 31, 2018
    Posts:
    97
    I'm currently trying to change my vehicle step system to run in parallel.

    Inside the IJobEntityBatch job I'm trying to ApplyImpulses on the PhysicsWorld.
    I can guarantee that I'm only writing to the RigidBodyIndex of the vehicle so I thought I could just disable the parallel for restriction and I would be good.

    But when adding the NativeDisableParallelForRestriction attribute to the PhysicsWorld I'm still getting an error: "InvalidOperationException: StepVehiclesJob.JobData.physicsWorld.CollisionWorld.EntityBodyIndexMap is not declared [ReadOnly] in a IJobParallelFor job"

    I would really like to not have to directly add the MotionDataArray, etc to my job so I can use the extension methods on the PhysicsWorld.

    Any idea what I'm missing or could try?
     
  2. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Just sharing first thoughts:
    - maybe try to trick the burst compiler by having a readonly PhysicsWorld and a writeable MotionDatas array?
    - When does your system update, have you linked it properly with jobs from existing physics systems (build, step, export physics world)?
     
    Lukas_Kastern likes this.
  3. Lukas_Kastern

    Lukas_Kastern

    Joined:
    Aug 31, 2018
    Posts:
    97
    My dependencies were already setup correct it was just the job debugger refusing to accept that I want to write to the motion data and velocity array in parallel.

    Thanks for the first suggestion.The following code works fine for me!

    Code (CSharp):
    1.         [BurstCompile( CompileSynchronously = true )]
    2.         struct StepVehiclesJob : IJobEntityBatch
    3.         {
    4.             [ReadOnly]
    5.             public PhysicsWorld PhysicsWorld;
    6.  
    7.             [NativeDisableContainerSafetyRestriction]
    8.             public NativeArray<MotionData> MotionDataArray;
    9.  
    10.             [NativeDisableContainerSafetyRestriction]
    11.             public NativeArray<MotionVelocity> MotionVelocities;
    12.  
    13.             public void Execute( ArchetypeChunk chunk, int batchIndex )
    14.             {
    15.                 PhysicsWorld.DynamicsWorld.m_MotionDatas = MotionDataArray;
    16.                 PhysicsWorld.DynamicsWorld.m_MotionVelocities = MotionVelocities;
    17.             }
    18.         }