Search Unity

Most efficient/correct way of editing large DynamicBuffer Attached to entity

Discussion in 'Entity Component System' started by Matt_De_Boss_Developer, May 22, 2019.

  1. Matt_De_Boss_Developer

    Matt_De_Boss_Developer

    Joined:
    Oct 17, 2014
    Posts:
    46
    So at the current moment I have an entity with a DynamicBuffer of floats that is of size 4913 (so 19,652‬ bytes). I have started to break ground in coding a JobComponentSystem that fills the buffer associated with each entity with raw Simplex Noise from the Mathematics library. I just wanted to make sure that I was writing things in the most efficient/correct way so that I don't move forward with something that straight up doesn't work. In addition, the job seems to be running way too fast for something like this (it fills the entire buffer in .5ms, which is stupid fast). Also, is there some sort of way to add I Job Paralle lFor to this? Code is below:
    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Jobs;
    3. using UnityEngine.Jobs;
    4. using Unity.Burst;
    5. using Unity.Collections;
    6. using UnityEngine;
    7.  
    8. using static Unity.Mathematics.math;
    9. using Unity.Mathematics;
    10.  
    11. public class VoxelGenerationSystem : JobComponentSystem
    12. {
    13.         protected override void OnCreate()
    14.         {
    15.         }
    16.         protected override JobHandle OnUpdate(JobHandle inputDeps)
    17.         {
    18.             var DestroyInitialVoxelDataComponentJob = new GenerateVoxelData
    19.             {
    20.                 voxelData = GetBufferFromEntity<VoxelData>(false)
    21.             }.Schedule(this);
    22.  
    23.             //DestroyInitialVoxelDataComponentJob.Complete();
    24.  
    25.  
    26.             return DestroyInitialVoxelDataComponentJob;
    27.         }
    28.  
    29.         [BurstCompile]
    30.         struct GenerateVoxelData : IJobForEachWithEntity<ChunkStatus>
    31.         {
    32.  
    33.             [NativeDisableParallelForRestriction]
    34.             public BufferFromEntity<VoxelData> voxelData;
    35.          
    36.  
    37.             public void Execute(Entity entity, int index, ref ChunkStatus c0)
    38.             {
    39.                 c0.shouldGenerateVoxelData = false;
    40.                 DynamicBuffer<VoxelData> voxelDataBuffer = voxelData[entity];
    41.  
    42.                 c0.SizeX = 17;
    43.                 c0.SizeY = 17;
    44.                 c0.SizeZ = 17;
    45.  
    46.  
    47.             for (int i = 0; i < voxelDataBuffer.Length; i++)
    48.                 {
    49.                     int iz = i / ((c0.SizeX - 0) * (c0.SizeY - 0));
    50.                     int ib = i - (((c0.SizeX - 0) * (c0.SizeY - 0)) * iz);
    51.                     int iy = ib / (c0.SizeX - 0);
    52.                     int ix = ib % (c0.SizeX - 0);
    53.  
    54.                     float z = iz;
    55.                     float y = iy;
    56.                     float x = ix;
    57.  
    58.  
    59.  
    60.                     VoxelData value = voxelDataBuffer[i];
    61.                     value.value = noise.snoise(float3(x,y,z));
    62.                     voxelDataBuffer[i] = value;
    63.                 }
    64.  
    65.             }
    66.         }
    67.  
    68.  
    69.      
    70.  
    71.  
    72.  
    73.  
    74. }
     
    Last edited: May 22, 2019
  2. o1o1o1o1o2

    o1o1o1o1o2

    Joined:
    May 22, 2014
    Posts:
    34
    Did you check if voxelDataBuffer.Length return zero?