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.

137 lines
5.1KB

  1. #if UNITY_ECS
  2. using System;
  3. using System.Collections;
  4. using Svelto.Common;
  5. using Svelto.DataStructures;
  6. using Svelto.ECS.Schedulers;
  7. using Unity.Entities;
  8. using Unity.Jobs;
  9. namespace Svelto.ECS.Extensions.Unity
  10. {
  11. /// <summary>
  12. /// Group of UECS/Svelto SystemBase engines that creates UECS entities.
  13. /// Svelto entities are submitted
  14. /// Svelto Add and remove callback are called
  15. /// OnUpdate of the systems are called
  16. /// finally the UECS command buffer is flushed
  17. /// Note: I cannot use Unity ComponentSystemGroups nor I can rely on the SystemBase Dependency field to
  18. /// solve external dependencies. External dependencies are tracked, but only linked to the UECS components operations
  19. /// With Dependency I cannot guarantee that an external container is used before previous jobs working on it are completed
  20. /// </summary>
  21. public sealed class SveltoUECSEntitiesSubmissionGroup
  22. {
  23. public SveltoUECSEntitiesSubmissionGroup(SimpleEntitiesSubmissionScheduler submissionScheduler, World UECSWorld)
  24. {
  25. _submissionScheduler = submissionScheduler;
  26. _ECBSystem = UECSWorld.CreateSystem<SubmissionEntitiesCommandBufferSystem>();
  27. _engines = new FasterList<SubmissionEngine>();
  28. }
  29. public void SubmitEntities(JobHandle jobHandle)
  30. {
  31. JobHandle RefHelper()
  32. {
  33. //execute submission engines and complete jobs because of this I don't need to do _ECBSystem.AddJobHandleForProducer(Dependency);
  34. using (var profiler = new PlatformProfiler("SveltoUECSEntitiesSubmissionGroup"))
  35. {
  36. for (var index = 0; index < _engines.count; index++)
  37. {
  38. ref var engine = ref _engines[index];
  39. using (profiler.Sample(engine.name))
  40. {
  41. jobHandle = engine.Execute(jobHandle);
  42. }
  43. }
  44. }
  45. return jobHandle;
  46. }
  47. if (_submissionScheduler.paused)
  48. return;
  49. jobHandle.Complete();
  50. //prepare the entity command buffer to be used by the registered engines
  51. var entityCommandBuffer = _ECBSystem.CreateCommandBuffer();
  52. foreach (var system in _engines)
  53. {
  54. system.ECB = entityCommandBuffer;
  55. }
  56. //Submit Svelto Entities, calls Add/Remove/MoveTo that can be used by the IUECSSubmissionEngines
  57. _submissionScheduler.SubmitEntities();
  58. jobHandle = RefHelper();
  59. //Sync Point as we must be sure that jobs that create/swap/remove entities are done
  60. jobHandle.Complete();
  61. //flush command buffer
  62. _ECBSystem.Update();
  63. }
  64. public IEnumerator SubmitEntities(JobHandle jobHandle, uint maxEntities)
  65. {
  66. JobHandle RefHelper()
  67. {
  68. //execute submission engines and complete jobs because of this I don't need to do _ECBSystem.AddJobHandleForProducer(Dependency);
  69. using (var profiler = new PlatformProfiler("SveltoUECSEntitiesSubmissionGroup"))
  70. {
  71. for (var index = 0; index < _engines.count; index++)
  72. {
  73. ref var engine = ref _engines[index];
  74. using (profiler.Sample(engine.name))
  75. {
  76. jobHandle = engine.Execute(jobHandle);
  77. }
  78. }
  79. }
  80. return jobHandle;
  81. }
  82. if (_submissionScheduler.paused)
  83. yield break;
  84. jobHandle.Complete();
  85. //prepare the entity command buffer to be used by the registered engines
  86. var entityCommandBuffer = _ECBSystem.CreateCommandBuffer();
  87. foreach (var system in _engines)
  88. {
  89. system.ECB = entityCommandBuffer;
  90. }
  91. //Submit Svelto Entities, calls Add/Remove/MoveTo that can be used by the IUECSSubmissionEngines
  92. var submitEntitiesAsync = _submissionScheduler.SubmitEntitiesAsync(maxEntities);
  93. while (submitEntitiesAsync.MoveNext())
  94. yield return null;
  95. jobHandle = RefHelper();
  96. //Sync Point as we must be sure that jobs that create/swap/remove entities are done
  97. jobHandle.Complete();
  98. //flush command buffer
  99. _ECBSystem.Update();
  100. }
  101. public void Add(SubmissionEngine engine)
  102. {
  103. _ECBSystem.World.AddSystem(engine);
  104. _engines.Add(engine);
  105. }
  106. readonly SimpleEntitiesSubmissionScheduler _submissionScheduler;
  107. readonly SubmissionEntitiesCommandBufferSystem _ECBSystem;
  108. readonly FasterList<SubmissionEngine> _engines;
  109. [DisableAutoCreation]
  110. class SubmissionEntitiesCommandBufferSystem : EntityCommandBufferSystem { }
  111. }
  112. }
  113. #endif