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.

163 line
6.5KB

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