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 });
}
}
}