Search Unity

What is Barrier?

Discussion in 'Data Oriented Technology Stack' started by Antypodish, Aug 2, 2018.

  1. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,197
    Can anyone explain, or point into reading reference, what is ECS barrier?
    Many thanks.
     
  2. dartriminis

    dartriminis

    Joined:
    Feb 3, 2017
    Posts:
    154
    A barrier is a sync point. It holds one or many EntityCommandBuffers that other jobs can use. It will then playback these buffers when the barrier gets updated. You'll need to use a barrier when updating entities (adding/removing components, creating/destroying entities) from jobs.

    See more here.
     
    Antypodish likes this.
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,197
    Thx for an explanation and a reference link.
    I have been on that page before, but perhaps was too early for me, to grasp it.
     
  4. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    77
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,197
    I read once, to get some light on my understanding.
    But definitely I need to re read at least once or twice more.
    Nice to see examples.
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,197
    To the above reference, I would like to appoint also the tutorial, which I followed recetly, to help me understand Barriers and ECS + Job System in practice.
    Unity - using the Job System with ECS



    However, it feels for me a bit vague, official references are worth to be mentioned too
    ECS features in detail
    https://github.com/Unity-Technologi...ocumentation/content/ecs_in_detail.md#barrier
     
    Vacummus likes this.
  7. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    573
    Wondering if someone could help me find a good example for creating a barrier system that deletes components. (most links in the thread are now 404s). Not sure if there is something special about them.

    Here is what im thinking:

    I have some components VertexComponent and TriangleComponent that is created via subscense live linking at runtime.

    Several system use this data but only need to use it once ( building a mesh set some relationships for pathfinding). I thought it might be a good idea to run a barrier system after all my system are done with these components and remove the these components. This way these system will only run once when adjusting my live link game objects.

    Im I in the ballpark here or is this a bad idea.
    edit : added my attempt

    Code (CSharp):
    1.  
    2. using Unity.Collections;
    3. using Unity.Entities;
    4. using Unity.Jobs;
    5.  
    6.  
    7. namespace NDA.HexWorld
    8. {
    9.     public class HexSphereBuildBarrierSystem : JobComponentSystem
    10.     {
    11.  
    12.         #region FLIELDS --------------------------------------------------------
    13.  
    14.         [ReadOnly] private EntityManager em;
    15.         [ReadOnly] private EntityQuery eqVertexComp;
    16.         [ReadOnly] private EntityQuery eqTriangleComp;
    17.  
    18.         #endregion
    19.  
    20.         #region METHODS --------------------------------------------------------
    21.  
    22.         /// <summary>
    23.         /// Constructor for system.
    24.         /// </summary>
    25.         protected override void OnCreate()
    26.         {
    27.             base.OnCreate();
    28.  
    29.             //
    30.             // Create EntityManager
    31.             em = World.Active.EntityManager;
    32.          
    33.             //
    34.             // Add system
    35.             World.Active.GetOrCreateSystem<BeginSimulationEntityCommandBufferSystem>();
    36.          
    37.             //
    38.             // Create Entity EntityQuery
    39.             eqVertexComp = em.CreateEntityQuery(ComponentType.ReadOnly<VertexComponent>());
    40.             eqTriangleComp = em.CreateEntityQuery(ComponentType.ReadOnly<TriangleComponent>());
    41.  
    42.         }
    43.  
    44.         #endregion
    45.  
    46.         #region MAIN THREAD ------------------------------------------------------
    47.  
    48.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    49.         {
    50.             //
    51.             // Remove hex vertex and triangle components so the system doen not run jobs
    52.             EntityManager.RemoveComponent(eqVertexComp, typeof(VertexComponent));
    53.             EntityManager.RemoveComponent(eqTriangleComp, typeof(TriangleComponent));
    54.             return inputDeps;
    55.         }
    56.  
    57.         #endregion
    58.     }
    59. }
    and my other systems use [UpdateBefore(typeof(HexSphereBuildBarrierSystem))]
     
    Last edited: Aug 13, 2019
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    5,197
    Well classical (U2018) inject barrier become obsolete in U2019.

    https://docs.unity3d.com/Packages/com.unity.entities@0.0/manual/entity_command_buffer.html
    Not the best link, but gives you good starting point.

    Then look into defaultWorld.cs for some ideas.

    Probably for best performance, it would be better to have empty component as tag(s), which indicates that your data is removed/added. This way you don't need move entity between chunks, when adding/removing component with data.

    You basically wan EntityCommandBuffer ecb.
    And similarly as Entity Manager, you can manipulate components. ecb.AddComponentData for example.
     
  9. francois85

    francois85

    Joined:
    Aug 11, 2015
    Posts:
    573
    Ok ill check it out , thanks for the reply