#if UNITY_ECS
#if !UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP_RUNTIME_WORLD && !UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP
#error SveltoOnDOTS required the user to take over the DOTS world control and explicitly create it. UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP must be defined
#endif
using System;
using Svelto.Common;
using Svelto.DataStructures;
using Svelto.ECS.Schedulers;
using Unity.Entities;
using Unity.Jobs;
namespace Svelto.ECS.SveltoOnDOTS
{
///
/// SveltoDOTS ECSEntitiesSubmissionGroup extends the _submissionScheduler responsibility to integrate the
/// submission of Svelto entities with the submission of DOTS ECS entities using DOTSOperationsForSvelto.
/// As there is just one submissionScheduler per enginesRoot, there should be only one SveltoDOTS
/// ECSEntitiesSubmissionGroup.
/// initialise DOTS ECS/Svelto systems/engines that handles DOTS ECS entities structural changes.
/// Flow:
/// Complete all the jobs used as input dependencies (this is a sync point)
/// Svelto entities are submitted
/// Svelto Add and remove callback are called
/// ISveltoOnDOTSStructuralEngine can use DOTSOperationsForSvelto in their add/remove/moove callbacks
///
[DisableAutoCreation]
public sealed partial class SveltoOnDOTSEntitiesSubmissionGroup: SystemBase, IQueryingEntitiesEngine, ISveltoOnDOTSSubmission
{
public SveltoOnDOTSEntitiesSubmissionGroup(SimpleEntitiesSubmissionScheduler submissionScheduler)
{
_submissionScheduler = submissionScheduler;
_submissionEngines = new FasterList();
}
public EntitiesDB entitiesDB { get; set; }
public void Ready() { }
public void SubmitEntities(JobHandle jobHandle)
{
if (_submissionScheduler.paused == true || World.EntityManager == default)
return;
using (var profiler = new PlatformProfiler("SveltoDOTSEntitiesSubmissionGroup"))
{
using (profiler.Sample("Complete All Pending Jobs"))
{
jobHandle.Complete(); //sync-point
#if UNITY_ECS_100
EntityManager.CompleteAllTrackedJobs();
#else
EntityManager.CompleteAllJobs();
#endif
}
//Submit Svelto Entities, calls Add/Remove/MoveTo that can be used by the DOTS ECSSubmissionEngines
_submissionScheduler.SubmitEntities();
foreach (var engine in _submissionEngines)
engine.OnPostSubmission();
_dotsOperationsForSvelto.Complete();
}
}
public void Add(ISveltoOnDOTSStructuralEngine engine)
{
_submissionEngines.Add(engine);
if (World != null)
{
engine.DOTSOperations = _dotsOperationsForSvelto;
engine.OnOperationsReady();
}
}
protected override void OnCreate()
{
unsafe
{
_jobHandle = (JobHandle*) MemoryUtilities.NativeAlloc((uint)MemoryUtilities.SizeOf(), Allocator.Persistent);
_dotsOperationsForSvelto = new DOTSOperationsForSvelto(World.EntityManager, _jobHandle);
//initialise engines field while world was null
foreach (var engine in _submissionEngines)
{
engine.DOTSOperations = _dotsOperationsForSvelto;
engine.OnOperationsReady();
}
}
}
protected override void OnDestroy()
{
unsafe
{
base.OnDestroy();
MemoryUtilities.NativeFree((IntPtr)_jobHandle, Allocator.Persistent);
}
}
protected override void OnUpdate()
{
throw new NotSupportedException("if this is called something broke the original design");
}
readonly FasterList _submissionEngines;
readonly SimpleEntitiesSubmissionScheduler _submissionScheduler;
DOTSOperationsForSvelto _dotsOperationsForSvelto;
unsafe JobHandle* _jobHandle;
}
}
#endif