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.

SveltoUECSEntitiesSubmissionGroup.cs 3.1KB

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