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.

EntityCommandBuffer.Playback

Discussion in 'Project Tiny' started by Djo_krd, Oct 3, 2020.

  1. Djo_krd

    Djo_krd

    Joined:
    Sep 6, 2017
    Posts:
    31
    What is the difference between using EntityCommandBuffer.Playback and direct call of EntityManager (for example EntityManager.AddComponent)? What advantage does Playback have? It working in job, or maby it support Burst compile?

    Here is example of TinyGems project:
    Code (CSharp):
    1. private void SpawnPlayArea()
    2.         {
    3.             var cmdBuffer = new EntityCommandBuffer(Allocator.Temp);
    4.             var cellManager = GetSingleton<CellManager>();
    5.  
    6.             for (var x = 0; x < cellManager.MaxCol; x++)
    7.             {
    8.                 for(var y = 0; y < cellManager.MaxRow; y++)
    9.                 {
    10.                     var backgroundEntity = cmdBuffer.Instantiate(cellManager.CellBackground);
    11.  
    12.                     cmdBuffer.AddComponent<CellBackground>(backgroundEntity);
    13.                  
    14.                     cmdBuffer.SetComponent(backgroundEntity, new Translation
    15.                     {
    16.                         Value = new float3(x, y, 0)
    17.                     });
    18.                 }
    19.             }
    20.          
    21.             cmdBuffer.Playback(EntityManager);
    22.         }
    My question is - what advantage gives using this lines
    Code (CSharp):
    1. var cmdBuffer = new EntityCommandBuffer(Allocator.Temp);
    2. ...
    3. cmdBuffer.Playback(EntityManager);
    instead of using regular EntityManager.AddComponent... ?
     
  2. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    848
    If you call
    EntityManager.AddComponent
    per entity per component, you tell Unity that you would like to perform this command instantly on the line. If you instead use an EntityCommandBuffer and queue up your structural changes, Unity will batch these structural changes, giving a slight performance boost.

    You can read a little bit more about it here.
     
  3. Djo_krd

    Djo_krd

    Joined:
    Sep 6, 2017
    Posts:
    31
    Ok, thanx.
    But this queue executes immediately in main thread without burst, am i right?
     
  4. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    668
    Djo_krd likes this.
  5. Ted_Wikman

    Ted_Wikman

    Unity Technologies

    Joined:
    Oct 7, 2019
    Posts:
    848
    I looked into this a bit more, and @brunocoimbra is correct that both ECB and EntityManager have their AddComponent actions bursted.

    When you use ECB, the actions will be deferred to a later time. This allows any other code that might execute in the loop to not be stopped by the structural changes. However, if your loop only contains structural changes and you want the structural changes immediately on the line, using ECB will cost a little bit more than EntityManager due to the fact that Unity both record the actions, and play them back.

    The general advice is to use ECB in jobs running on worker-threads, and play these ECBs back on pre-existing sync points. This will give you the least main thread impact.