Skip to content

Instantly share code, notes, and snippets.

@justonia
Last active May 9, 2019 21:16
Show Gist options
  • Save justonia/7934ae5b5079e4227c03c20ab045f47b to your computer and use it in GitHub Desktop.
Save justonia/7934ae5b5079e4227c03c20ab045f47b to your computer and use it in GitHub Desktop.
public struct Buffer : IBufferElementData
{
int x;
}
public struct Data : IComponentData
{
int y;
}
public class SystemA : JobComponentSystem
{
public struct AJob : IJobForEachWithEntity<Data>
{
public BufferFromEntity<Buffer> buffers;
public void Execute(Entity entity, int index, ref Data data)
{
var buffer = buffers[entity];
// do some work, mutate buffer
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
return (new AJob{
buffers = GetBufferFromEntity<Buffer>(isReadOnly:false),
}).Schedule(this, inputDeps);
}
}
[UpdateAfter(typeof(SystemA))]
public class SystemB : JobComponentSystem
{
public struct BJob : IJobParallelFor
{
[ReadOnly] public NativeArray<Entity> entities;
[ReadOnly] public BufferFromEntity<Buffer> buffers;
public void Execute(int index)
{
var entity = entities[index];
var buffer = buffers[entity];
// do some work
}
}
private EntityQuery query;
protected override void OnCreate()
{
query = GetEntityQuery(
ComponentType.ReadOnly<Data>(),
ComponentType.ReadOnly<Buffer>()
);
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var entities = query.ToEntityArray(Allocator.TempJob, out var entitiesHandle);
var buffers = GetBufferFromEntity<Buffer>(isReadOnly:true);
// Does the ECS safety system understand that this job has a dependency on a read-only
// BufferFromEntity<Buffer>, despite it being a job that is not a IJobForEach or IJobChunk?
return (new BJob{
entities = entities,
buffers = buffers,
}).Schedule(entities.Length, 1, JobHandle.CombineDependencies(inputDeps entitiesHandle));
}
}
[UpdateAfter(typeof(SystemB))]
public class SystemC : JobComponentSystem
{
public struct CJob : IJobForEachWithEntity<Data>
{
public BufferFromEntity<Buffer> buffers;
public void Execute(Entity entity, int index, ref Data data)
{
var buffer = buffers[entity];
// do some work, mutate buffer
}
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
// Does the safety system know that it must wait for SystemB.BJob to finish before
// this is allowed to run?
return (new CJob{
buffers = GetBufferFromEntity<Buffer>(isReadOnly:false),
}).Schedule(this, inputDeps);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment