#Writing to EntityCommandBuffer.ParallelWriter from multiple jobs

1 messages · Page 1 of 1 (latest)

limpid sparrow
#

Hey all. I'm trying to write to an EntityCommandBuffer.ParallelWriter from multiple IJobParallelFor jobs. From my point of view this should work, since each parallel for job is listed as a dependency for the other parallel for jobs. However, the safety system keeps complaining. Anyone got any ideas?

Here's my repro code:

    public struct MyComponent : IComponentData
    {
        public int Value;
    }

    [UpdateInGroup(typeof(PhysicsStreamingSystemGroup))]
    public partial struct MyTestSystem : ISystem
    {
        public BufferLookup<EntitiesInBatch> mEntitiesInBatchLookup;

        public void OnCreate(ref SystemState state)
        {
            mEntitiesInBatchLookup = state.GetBufferLookup<EntitiesInBatch>(true);
        }

        public void OnUpdate(ref SystemState state)
        {
            mEntitiesInBatchLookup.Update(ref state);

            var ecb = SystemAPI.GetSingleton<EndInitializationEntityCommandBufferSystem.Singleton>().CreateCommandBuffer(state.WorldUnmanaged);

            var handle = state.Dependency;
            foreach (var (_, batch) in SystemAPI.Query<BatchTag>().WithEntityAccess())
            {
                var entities = mEntitiesInBatchLookup[batch];
                handle = new MyParallelJob
                {
                    Entities = entities,
                    ECB = ecb.AsParallelWriter(),
                }
                .Schedule(entities.Length, 1, handle);
            }

            handle.Complete();
        }

        public struct MyParallelJob
            : IJobParallelFor
        {
            [ReadOnly]
            public DynamicBuffer<EntitiesInBatch> Entities;

            [WriteOnly]
            public EntityCommandBuffer.ParallelWriter ECB;

            public void Execute(int index)
            {
                ECB.AddComponent(index, Entities[index].Value, new MyComponent { Value = index });
            }
        }
    }
cinder rose
#

Can you post the error

#

I think this is a main thread false positive from memory

#

If you store the ecb parallel writer outside the loop once instead of constantly remaking it, I suspect it'll work

#

On phone so can' check source but if I recall, AsParallelWriter has a safety check in it which triggers if you've passed ecb to a job already.
Hence if you store
var ecbpw = ecb.AsParallelWriter();
outside loop and just pass that to each job it'll be fine