Search Unity

Needing explanation of JobScheduleParameters and CreateJobReflectionData

Discussion in 'Entity Component System' started by sebas77, Oct 1, 2019.

  1. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,643
    I like to understand the code I am working with, and I can't figure out what's going on with the following code:

    internal static unsafe JobHandle ScheduleImpl<T>(this T jobData, ISimulation simulation, ref PhysicsWorld world, JobHandle inputDeps)
    where T : struct, ITriggerEventsJob
    {
    if (simulation.Type == SimulationType.UnityPhysics)
    {
    var data = new TriggerEventJobData<T>
    {
    UserJobData = jobData,
    EventReader = ((Simulation)simulation).TriggerEvents,
    Bodies = world.Bodies
    };

    // Ensure the input dependencies include the end-of-simulation job, so events will have been generated
    inputDeps = JobHandle.CombineDependencies(inputDeps, simulation.FinalSimulationJobHandle);

    var parameters = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref data),
    TriggerEventJobProcess<T>.Initialize(),
    inputDeps, ScheduleMode.Batched);
    return JobsUtility.Schedule(ref parameters);
    }
    return inputDeps;
    }

    internal unsafe struct TriggerEventJobData<T> where T : struct
    {
    public T UserJobData;
    [NativeDisableContainerSafetyRestriction] public LowLevel.TriggerEvents EventReader;
    // Disable aliasing restriction in case T has a NativeSlice of PhysicsWorld.Bodies
    [ReadOnly, NativeDisableContainerSafetyRestriction] public NativeSlice<RigidBody> Bodies;
    }

    internal struct TriggerEventJobProcess<T> where T : struct, ITriggerEventsJob
    {
    static IntPtr jobReflectionData;

    public static IntPtr Initialize()
    {
    if (jobReflectionData == IntPtr.Zero)
    {
    jobReflectionData = JobsUtility.CreateJobReflectionData(typeof(TriggerEventJobData<T>),
    typeof(T), JobType.Single, (ExecuteJobFunction)Execute);
    }
    return jobReflectionData;
    }


    public delegate void ExecuteJobFunction(ref TriggerEventJobData<T> jobData, IntPtr additionalData,
    IntPtr bufferRangePatchData, ref JobRanges ranges, int jobIndex);

    public unsafe static void Execute(ref TriggerEventJobData<T> jobData, IntPtr additionalData,
    IntPtr bufferRangePatchData, ref JobRanges ranges, int jobIndex)
    {
    foreach (LowLevel.TriggerEvent eventData in jobData.EventReader)
    {
    jobData.UserJobData.Execute(new TriggerEvent
    {
    EventData = eventData,
    Entities = new EntityPair
    {
    EntityA = jobData.Bodies[eventData.BodyIndices.BodyAIndex].Entity,
    EntityB = jobData.Bodies[eventData.BodyIndices.BodyBIndex].Entity
    }
    });
    }
    }
    }

    It looks to me that the red part and the orange part are a mysterious pattern that is meant to be followed but not understood.

    Red part: a pointer to data in the stack is passed, I assume a copy happens inside somewhere, not sure with what mechanism

    Orange part: the static IntPtr is nowhere used, but present

    Do you know what's going on and if so why?
     
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
  3. sebas77

    sebas77

    Joined:
    Nov 4, 2011
    Posts:
    1,643
    I really wonder why google doesn't index these pages