Search Unity

Jobs with struct with methods

Discussion in 'Entity Component System' started by vincismurf, Jan 31, 2018.

  1. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    I have modified the example found in the Job API page

    Code (csharp):
    1.  
    2. public struct testStruct
    3. {
    4.     public void doSomething(NativeArray<Vector3> velocity, NativeArray<Vector3> position, float deltaTime)
    5.     {
    6.         // Move the positions based on delta time and velocity
    7.         for (var i = 0; i < position.Length; i++)
    8.             position[i] = position[i] + velocity[i] * deltaTime;
    9.     }
    10.  
    11. }
    12. class ApplyVelocitySample : MonoBehaviour
    13. {
    14.     struct VelocityJob : IJob
    15.     {
    16.         // Jobs declare all data that will be accessed in the job
    17.         // By declaring it as read only, multiple jobs are allowed to access the data in parallel
    18.         [ReadOnly]
    19.         public NativeArray<Vector3> velocity;
    20.  
    21.         // By default containers are assumed to be read & write
    22.         public NativeArray<Vector3> position;
    23.  
    24.         // Delta time must be copied to the job since jobs generally don't have concept of a frame.
    25.         // The main thread waits for the job on the same frame or the next frame, but the job should
    26.         // perform work in a deterministic and independent way when running on worker threads.
    27.         public float deltaTime;
    28.         public testStruct bob;
    29.         // The code actually running on the job
    30.         public void Execute()
    31.         {
    32.             bob.doSomething(velocity, position, deltaTime);
    33.         }
    34.     }
    35.  
    36.     private testStruct fred;
    37.     public void Start()
    38.     {
    39.         fred = new testStruct();
    40.     }
    41.     public void Update()
    42.     {
    43.         var position = new NativeArray<Vector3>(500, Allocator.Persistent);
    44.  
    45.         var velocity = new NativeArray<Vector3>(500, Allocator.Persistent);
    46.         for (var i = 0; i < velocity.Length; i++)
    47.             velocity[i] = new Vector3(0, 10, 0);
    48.  
    49.        
    50.         // Initialize the job data
    51.         var job = new VelocityJob()
    52.         {
    53.             deltaTime = Time.deltaTime,
    54.             position = position,
    55.             velocity = velocity,
    56.             bob = fred
    57.         };
    58.         //job.bob = fred;
    59.         // Schedule the job, returns the JobHandle which can be waited upon later on
    60.         JobHandle jobHandle = job.Schedule();
    61.  
    62.         // Ensure the job has completed
    63.         // It is not recommended to Complete a job immediately,
    64.         // since that gives you no actual parallelism.
    65.         // You optimally want to schedule a job early in a frame and then wait for it later in the frame.
    66.         jobHandle.Complete();
    67.  
    68.         Debug.Log(job.position[0]);
    69.  
    70.         // Native arrays must be disposed manually
    71.         position.Dispose();
    72.         velocity.Dispose();
    73.  
    74.     }
    75. }
    76.  

    Essentially I moved the logic from the VelocityJob.Execute to a method within a struct in VelocityJob.

    The reason is ultimately want pass in unique AI planners to each job, so they can solve plans on each thread.

    My question being, is this OK to do? It compiles and gives the same result. But is it still leveraging the compactness linear arrangement for high performance access?
     
  2. Krajca

    Krajca

    Joined:
    May 6, 2014
    Posts:
    347
    It should be good, but also you should wait for ECS and do it with it.