Search Unity

DynamicBuffer Multithread

Discussion in 'Entity Component System' started by xman7c7, Sep 29, 2018.

  1. xman7c7

    xman7c7

    Joined:
    Oct 28, 2016
    Posts:
    28
    Hi,

    is it somehow possible to use DynamicBuffer in Multithread context e.g IJobParallelFor, but parallel schedule? Not one depend on previously Completed

    Not like this
    Code (CSharp):
    1. protected override JobHandle OnUpdate(JobHandle inputDeps)
    2. {
    3.     for (int i = 0; i < chunk.Length; i++)
    4.     {
    5.         inputDeps = new MyJob(...).Schedule(size, batchCount, inputDeps);
    6.     }
    7.  
    8.     return inputDeps;
    9. }

    Code (CSharp):
    1. public struct Triangles : IBufferElementData
    2. {
    3.     public int Value;
    4. }
    5.  
    6. public struct Vertices : IBufferElementData
    7. {
    8.     public Vector3 Value;
    9. }

    Code (CSharp):
    1. public struct MyJob : IJobParallelFor
    2. {
    3.     [NativeDisableParallelForRestriction] public DynamicBuffer<Vector3> _vertices;
    4.     [NativeDisableParallelForRestriction] public DynamicBuffer<int> _triangles;
    5.     [ReadOnly] public MeshData _meshData;
    6.     [ReadOnly] public Vector2 Offset;
    7.  
    8.     public MyJob(DynamicBuffer<Vector3> vertices, DynamicBuffer<int> triangles, MeshData meshData, Vector2 offset)
    9.     {
    10.         _vertices = vertices;
    11.         _triangles = triangles;
    12.         _meshData = meshData;
    13.         Offset = offset;
    14.     }
    15.  
    16.     public void Execute(int index)
    17.     {
    18.         var position = Position(index, _meshData.Size);
    19.  
    20.         _vertices[index] = Vertex(_meshData.StepSize, position, Offset, _meshData.NoiseSettings);
    21.         if (position.x < _meshData.Size && position.y < _meshData.Size)
    22.         {
    23.             var t = 6 * (index - position.y);
    24.             _triangles[t] = index;
    25.             _triangles[t + 1] = index + _meshData.Size + 1;
    26.             _triangles[t + 2] = index + 1;
    27.             _triangles[t + 3] = index + 1;
    28.             _triangles[t + 4] = index + _meshData.Size + 1;
    29.             _triangles[t + 5] = index + _meshData.Size + 2;
    30.         }
    31.     }
    32.  
    33.     private Vector2Int Position(int index, int size)
    34.     {
    35.         return new Vector2Int(
    36.             index % (size + 1),
    37.             index / (size + 1)
    38.         );
    39.     }
    40.  
    41.     private Vector3 Vertex(float stepSize, Vector2Int position, Vector2 offset, NoiseSettings noiseSettings)
    42.     {
    43.         var point = Common.MiddlePosition(position.x, position.y, stepSize, offset);
    44.         var noiseValue = Common.NoiseValue(point, noiseSettings);
    45.         return new Vector3(position.x * stepSize - 0.5f, noiseValue, position.y * stepSize - 0.5f);
    46.     }
    47. }

    Code (CSharp):
    1. public sealed class MeshBuilderSystem : JobComponentSystem
    2. {
    3.     private struct Data
    4.     {
    5.         public readonly int Length;
    6.         public ComponentDataArray<Position> Position;
    7.         public BufferArray<Triangles> Triangles;
    8.         public BufferArray<Vertices> Vertices;
    9.     }
    10.  
    11.     [Inject] private Data _data;
    12.  
    13.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    14.     {
    15.         var meshData = new MeshData(
    16.             _terrainSettings.Resolution,
    17.             _terrainSettings.NoiseSettings
    18.         );
    19.  
    20.         var jobHandles = new NativeArray<JobHandle>(_terrainSettings.ChunkCount, Allocator.Temp);
    21.         for (int i = 0; i < _data.Length; i++)
    22.         {
    23.             _data.Vertices[i].ResizeUninitialized(
    24.                 (_terrainSettings.Resolution + 1) * (_terrainSettings.Resolution + 1)
    25.             );
    26.             _data.Triangles[i].ResizeUninitialized(
    27.                 _terrainSettings.Resolution * _terrainSettings.Resolution * 6
    28.             );
    29.  
    30.             var offset = _data.Position[i].Value;
    31.  
    32.             jobHandles[i] = new MyJob(
    33.                 _data.Vertices[i].Reinterpret<Vector3>(),
    34.                 _data.Triangles[i].Reinterpret<int>(),
    35.                 meshData,
    36.                 new Vector2(offset.x, offset.z)
    37.             ).Schedule(
    38.                 (_terrainSettings.Resolution + 1) * (_terrainSettings.Resolution + 1),
    39.                 64,
    40.                 inputDeps
    41.             );
    42.         }
    43.  
    44.         return JobHandle.CombineDependencies(jobHandles);
    45.     }
    46. }

    Thanks
     
  2. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
  3. shoroiusagi

    shoroiusagi

    Joined:
    Nov 24, 2019
    Posts:
    2
    You link is 404.
     
  4. Deleted User

    Deleted User

    Guest

    Do some extra search based on the link's path? Use WaybackMachine to see the cached version of the page?
     
  5. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,853
    To get an answer to a question with a broken link when you are already frustrated enough about your question or problem to bring it to a forum of your peers does not contribute to factors that would lessen the lessen the blood pressure. A little respect for your peer group seems in order if one presents what is supposed to be an answer. As well..outdated links to code for DOTS is a guaranteed road to hell paved with good intentions. That said I sure am learning one hell of a great deal of the instrinsics of compiling and multithreading in relation to performance hanging out here. The science nerd puzzle kid inside me is fascinated.
     
    zanius likes this.