Search Unity

Iterating over DynamicBuffer elements in a Job

Discussion in 'Data Oriented Technology Stack' started by PublicEnumE, Mar 13, 2019.

  1. PublicEnumE

    PublicEnumE

    Joined:
    Feb 3, 2019
    Posts:
    173
    Hi folks!

    Right now, a lot of my data lives in DynamicBuffers. Several of my systems iterate over each element of those buffers, to do work on each one.

    When it comes to Jobifying this code, I'm wondering if there's a cleaner approach than what I'm doing right now:

    Code (CSharp):
    1. public class MyBufferElementDataSystem : JobComponentSystem
    2. {
    3.     private MyBarrier myBarrier;
    4.  
    5.     [RequireComponentTag(typeof(MyBufferElementData))]
    6.     private struct Job : IJobProcessComponentDataWithEntity<MyComponentData>
    7.     {
    8.         public EntityCommandBuffer.Concurrent entityCommandBuffer;
    9.  
    10.         [NativeDisableParallelForRestriction]
    11.         public BufferFromEntity<MyBufferElementData> bufferFromEntity;
    12.  
    13.         public void Execute(Entity entity, int index, [ReadOnly] ref MyComponentData c0)
    14.         {
    15.             DynamicBuffer<MyBufferElementData> buffer = bufferFromEntity[entity];
    16.  
    17.             for (int i = 0; i < buffer.Length; i++)
    18.             {
    19.                 MyBufferElementData myBufferElementData = buffer[i];
    20.  
    21.                 // logic goes here
    22.  
    23.                 buffer[i] = myBufferElementData;
    24.             }
    25.         }
    26.     }
    27.  
    28.     protected override void OnCreateManager()
    29.     {
    30.         myBarrier = World.Active.GetExistingManager<MyBarrier>();
    31.     }
    32.  
    33.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    34.     {
    35.         Job job = new Job()
    36.         {
    37.             entityCommandBuffer = myBarrier.CreateCommandBuffer().ToConcurrent(),
    38.             bufferFromEntity = GetBufferFromEntity<MyBufferElementData>()
    39.         };
    40.  
    41.         JobHandle jobHandle = job.Schedule(this, inputDeps);
    42.  
    43.         myBarrier.AddJobHandleForProducer(jobHandle);
    44.  
    45.         return jobHandle;
    46.     }
    47. }
    Is this pattern the simplest way to iterate over DynamicBuffer elements in a job right now? It's really not that much, but maybe one of your smart, beautiful people knows better. :)

    Many thanks, and cheers!
     
  2. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    132
    I currently use this framework (more or less) for dynamic buffer iteration.
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    6,964
    It is one of basics formats, to get buffer data into a system. As well as other data, which uou pass through update.

    Any system will require similar boiler plate anyway. Don't think there is anything much smaller, other than tweaking here, or there.
    It may be a bit verbose at first glance, but it doesn't mean, is less performant.

    Btw, you don't need barrier in this example, unless you plan to do something with it.

    But mind, there may be something new, for which I may don't know. So is worth to listening others opinions too.
     
unityunity