Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
5.7KB

  1. #if UNITY_ECS
  2. using Svelto.Common;
  3. using Svelto.ECS.Schedulers;
  4. using Unity.Entities;
  5. using Unity.Jobs;
  6. namespace Svelto.ECS.SveltoOnDOTS
  7. {
  8. /// <summary>
  9. /// This is a high level class to abstract the complexity of creating a Svelto ECS application that interacts
  10. /// with DOTS ECS.
  11. /// This is a JobifiedEngine and as such it expect to be ticked. Normally it must be executed in a
  12. /// SortedEnginesGroup as step that happens after the Svelto jobified engines run.
  13. ///
  14. /// The flow should be:
  15. /// Svelto (GameLogic) Engines Run first
  16. /// Then this Engine runs, which causes:
  17. /// Jobs to be completed (it's a sync point)
  18. /// Synchronizations engines to be executed (Svelto to DOTS ECS)
  19. /// Submission of Entities to be executed
  20. /// Svelto Add/Remove callbacks to be called
  21. /// ISveltoOnDOTSStructuralEngine to be executed
  22. /// DOTS ECS engines to executed
  23. /// Synchronizations engines to be executed (DOTS ECS To Svelto)
  24. /// </summary>
  25. [Sequenced(nameof(JobifiedSveltoEngines.SveltoOnDOTS))]
  26. public class SveltoOnDOTSEnginesGroup: IJobifiedEngine
  27. {
  28. public SveltoOnDOTSEnginesGroup(EnginesRoot enginesRoot)
  29. {
  30. DBC.ECS.Check.Require(
  31. enginesRoot.scheduler is SimpleEntitiesSubmissionScheduler
  32. , "The Engines root must use a EntitiesSubmissionScheduler scheduler implementation");
  33. CreateUnityECSWorldForSvelto(enginesRoot.scheduler as SimpleEntitiesSubmissionScheduler, enginesRoot);
  34. }
  35. /// <summary>
  36. /// for the user to add pure DOTS ECS SystemBase/ISystem systems to the DOTS ECS world
  37. /// </summary>
  38. public World world { get; private set; }
  39. public JobHandle Execute(JobHandle inputDeps)
  40. {
  41. //this is a sync point, there won't be pending jobs after this
  42. _sveltoDotsEntitiesSubmissionGroup.SubmitEntities(inputDeps);
  43. //Mixed explicit job dependency and internal automatic ECS dependency system
  44. //Write in to DOTS ECS entities so the DOTS ECS dependencies react on the components touched
  45. var handle = _syncSveltoToDotsGroup.Execute(default);
  46. //As long as pure DOTS ECS systems do not use external containers (like native arrays and so) the Unity
  47. //automatic dependencies system will guarantee that there won't be race conditions
  48. world.Update();
  49. //this svelto group of DOTS ECS SystemBase systems
  50. return _syncDotsToSveltoGroup.Execute(handle);
  51. }
  52. public string name => nameof(SveltoOnDOTSEnginesGroup);
  53. public void AddSveltoToDOTSSyncEngine(SyncSveltoToDOTSEngine engine)
  54. {
  55. //it's a Svelto Engine/DOTS ECS SystemBase so it must be added in the DOTS ECS world AND svelto enginesRoot
  56. world.AddSystemManaged(engine);
  57. _enginesRoot.AddEngine(engine);
  58. _syncSveltoToDotsGroup.Add(engine);
  59. }
  60. public void AddDOTSToSveltoSyncEngine(SyncDOTSToSveltoEngine engine)
  61. {
  62. //it's a Svelto Engine/DOTS ECS SystemBase so it must be added in the DOTS ECS world AND svelto enginesRoot
  63. world.AddSystemManaged(engine);
  64. _enginesRoot.AddEngine(engine);
  65. _syncDotsToSveltoGroup.Add(engine);
  66. }
  67. public void AddSveltoOnDOTSSubmissionEngine(ISveltoOnDOTSStructuralEngine submissionEngine)
  68. {
  69. _sveltoDotsEntitiesSubmissionGroup.Add(submissionEngine);
  70. if (submissionEngine is IEngine enginesRootEngine)
  71. _enginesRoot.AddEngine(enginesRootEngine);
  72. }
  73. public void Dispose()
  74. {
  75. world.Dispose();
  76. }
  77. void CreateUnityECSWorldForSvelto(SimpleEntitiesSubmissionScheduler scheduler, EnginesRoot enginesRoot)
  78. {
  79. world = new World("Svelto<>DOTS world");
  80. var systems = DefaultWorldInitialization.GetAllSystems(WorldSystemFilterFlags.Default);
  81. DefaultWorldInitialization.AddSystemsToRootLevelSystemGroups(world, systems);
  82. World.DefaultGameObjectInjectionWorld = world;
  83. //This is the DOTS ECS group that takes care of all the DOTS ECS systems that creates entities
  84. //it also submits Svelto entities through the scheduler
  85. var defaultSveltoOnDotsHandleLifeTimeEngine = new SveltoOnDOTSHandleLifeTimeEngine<DOTSEntityComponent>();
  86. _sveltoDotsEntitiesSubmissionGroup = new SveltoOnDOTSEntitiesSubmissionGroup(scheduler);
  87. enginesRoot.AddEngine(defaultSveltoOnDotsHandleLifeTimeEngine);
  88. _sveltoDotsEntitiesSubmissionGroup.Add(defaultSveltoOnDotsHandleLifeTimeEngine);
  89. enginesRoot.AddEngine(_sveltoDotsEntitiesSubmissionGroup);
  90. world.AddSystemManaged(_sveltoDotsEntitiesSubmissionGroup);
  91. //This is the group that handles the DOTS ECS sync systems that copy the svelto entities values to DOTS ECS entities
  92. _syncSveltoToDotsGroup = new SyncSveltoToDOTSGroup();
  93. enginesRoot.AddEngine(_syncSveltoToDotsGroup);
  94. //This is the group that handles the DOTS ECS sync systems that copy the DOTS ECS entities values to svelto entities
  95. _syncDotsToSveltoGroup = new SyncDOTSToSveltoGroup();
  96. enginesRoot.AddEngine(_syncDotsToSveltoGroup);
  97. enginesRoot.AddEngine(this);
  98. _enginesRoot = enginesRoot;
  99. }
  100. EnginesRoot _enginesRoot;
  101. SveltoOnDOTSEntitiesSubmissionGroup _sveltoDotsEntitiesSubmissionGroup;
  102. SyncSveltoToDOTSGroup _syncSveltoToDotsGroup;
  103. SyncDOTSToSveltoGroup _syncDotsToSveltoGroup;
  104. }
  105. }
  106. #endif