Search Unity

Resolved Broadphase:PrepareStaticBodyDataJob as a dependency

Discussion in 'Physics for ECS' started by hreintke, Nov 29, 2022.

  1. hreintke

    hreintke

    Joined:
    Jan 6, 2022
    Posts:
    20
    Hi,
    I starting with raycasts using the example from https://docs.unity3d.com/Packages/com.unity.physics@1.0/manual/collision-queries.html

    New to DOTS/ECS/Physics I never worked with older versions so my knowledge/experience is only entities 1.0 and physivs 1.0

    Implementing the raycast example in an ISYSTEM system I came to the following.

    Code (CSharp):
    1. [BurstCompile]
    2. public struct RaycastJob : IJobParallelFor
    3. {
    4.     [ReadOnly] public CollisionWorld world;
    5.     [ReadOnly] public NativeArray<RaycastInput> inputs;
    6.     public NativeArray<RaycastHit> results;
    7.     [BurstCompile]
    8.     public unsafe void Execute(int index)
    9.     {
    10.         RaycastHit hit;
    11.         world.CastRay(inputs[index], out hit);
    12.         results[index] = hit;
    13.     }
    14. }
    15. [UpdateInGroup(typeof(AfterPhysicsSystemGroup))]    // <=== Don't know if this is correct
    16. [BurstCompile]
    17. partial struct RayCastSystem : ISystem
    18. {
    19.     private CollisionWorld world;
    20.     private NativeArray<RaycastInput> inputs;
    21.     private NativeArray<RaycastHit> results;
    22.     [BurstCompile]
    23.     public void OnCreate(ref SystemState state)
    24.     {
    25.     }
    26.     [BurstCompile]
    27.     public void OnDestroy(ref SystemState state)
    28.     {
    29.     }
    30.     [BurstCompile]
    31.     public void OnUpdate(ref SystemState state)
    32.     {
    33.         world = SystemAPI.GetSingleton<PhysicsWorldSingleton>().CollisionWorld;
    34.         inputs = new NativeArray<RaycastInput>(1, Allocator.TempJob);
    35.         results = new NativeArray<RaycastHit>(1, Allocator.TempJob);
    36.         inputs[0] = new RaycastInput()
    37.         {
    38.             Start = float3.zero,
    39.             End = new float3(1f, 1f, 1f),
    40.             Filter = new CollisionFilter()
    41.             {
    42.                 BelongsTo = ~0u,
    43.                 CollidesWith = ~0u, // all 1s, so all layers, collide with everything
    44.                 GroupIndex = 0
    45.             }
    46.         };
    47.         JobHandle rcj = new RaycastJob
    48.         {
    49.             inputs = inputs,
    50.             results = results,
    51.             world = world
    52.         }.Schedule(inputs.Length, 4);
    53.         rcj.Complete();
    54.     }
    55. }
    Running with this system I get the error :

    Code (CSharp):
    1. InvalidOperationException: The previously scheduled job Broadphase:PrepareStaticBodyDataJob writes to the UNKNOWN_OBJECT_TYPE PrepareStaticBodyDataJob.FiltersOut. You are trying to schedule a new job RaycastJob, which reads from the same UNKNOWN_OBJECT_TYPE (via RaycastJob.world.Broadphase.m_StaticTree.BodyFilters). To guarantee safety, you must include Broadphase:PrepareStaticBodyDataJob as a dependency of the newly scheduled job.
    I have seen a thread on this but that is on older versions and the solutions from that I cannot get working in the 1.0 versions.


    Anyone had the same/similar issue and know a solution ?
     
  2. JustAWitness

    JustAWitness

    Joined:
    Jan 27, 2015
    Posts:
    5
    Hi,

    Try putting this as your first line in OnUpdate

    Code (CSharp):
    1. state.EntityManager.CompleteDependencyBeforeRO<PhysicsWorldSingleton>();
     
  3. hreintke

    hreintke

    Joined:
    Jan 6, 2022
    Posts:
    20
    Yes, That works, thanks.

    A I also found that starting the job with :
    Code (CSharp):
    1.         state.Dependency = new RaycastJob
    2.         {
    3.             inputs = inputs,
    4.             results = results,
    5.             world = world
    6.  
    7.         }.Schedule(inputs.Length, 4, state.Dependency);
    Checking actual cast results now.

    (And learning more on dependencies between systems :))