Search Unity

Consecutive jobs waiting on main thread command buffer execution before running

Discussion in 'Entity Component System' started by Init33, Jul 13, 2019.

  1. Init33

    Init33

    Joined:
    Aug 30, 2017
    Posts:
    67
    Hi, I have a general question about job execution order.

    Say I have a system with 3 jobs. Each job has some EntityQuery data injected in, and based on logic, will create/destroy entities via a concurrent commandbuffer.
    The jobs are scheduled such that the first job is the input dependency for the second, and the second is the input for the third etc...

    Is there a way to make the first job run, execute its command buffer on the main thread, get a new entityqueries based on those changes which the second job now runs on... and same again for the third job?

    Basically the way I have things in this system structured, my second job depends on the changes the first job makes via command buffer to be executed before the second job runs.

    I'm willing to accept this is just a bad design philosophy and I need to re-approach how I do this but was curious as to whether I had any control over the way this was done.

    Thanks for the help.
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Sure, you can just complete the job and run the command buffer.

    Code (CSharp):
    1. var cb1 = new EntityCommandBuffer();
    2. var job1 = new Job1() { Buffer = cb1; }.Schedule(this, handle);
    3. job2.Complete();
    4. cb1.Playback(this.EntityManager);
    5.  
    6. var cb2 = new EntityCommandBuffer();
    7. var job2 = new Job2() { Buffer = cb2; }.Schedule(this);
    8. job2.Complete();
    9. cb2.Playback(this.EntityManager);
    You shouldn't really do this, you are significantly reducing your parallelism/throughput, but you can.
     
    Init33 likes this.
  3. Init33

    Init33

    Joined:
    Aug 30, 2017
    Posts:
    67

    Thanks for the reply. Makes sense. I don't mean to say that I think this is ideal but just handy tool to know exists if i'm stuck on structuring a problem.
     
  4. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    This is not the best parallel algorithm in the world but sometimes people do these things with valid reasons. For example you might want to remove all lag from your bullet instantiations so the job which create bullets can run before every other job and then its command buffer gets executed and then rest of your simulation runs and places each bullet at its initial position and (among doing other things) and then your rendering happens.

    Or do run all of your simulation in parallel and then execute the command buffers but have a last job in your simulation which puts all bullets in their initial positions before presentation kicks in which is harder to code and has more exceptions but might gives you a few milliseconds which you might really need.

    It's important to keep in mind that ECS is created so default is linear access and as much parallelism as possible but you will diverge from it for valid algorithmic and experience related reasons. It's performance by default and not performance at the cost of correctness even when it is not a good trade off.