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.

115 lines
4.3KB

  1. #if UNITY_ECS
  2. #if !UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP_RUNTIME_WORLD && !UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP
  3. #error SveltoOnDOTS required the user to take over the DOTS world control and explicitly create it. UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP must be defined
  4. #endif
  5. using System;
  6. using Svelto.Common;
  7. using Svelto.DataStructures;
  8. using Svelto.ECS.Schedulers;
  9. using Unity.Entities;
  10. using Unity.Jobs;
  11. namespace Svelto.ECS.SveltoOnDOTS
  12. {
  13. /// <summary>
  14. /// SveltoDOTS ECSEntitiesSubmissionGroup extends the _submissionScheduler responsibility to integrate the
  15. /// submission of Svelto entities with the submission of DOTS ECS entities using DOTSOperationsForSvelto.
  16. /// As there is just one submissionScheduler per enginesRoot, there should be only one SveltoDOTS
  17. /// ECSEntitiesSubmissionGroup.
  18. /// initialise DOTS ECS/Svelto systems/engines that handles DOTS ECS entities structural changes.
  19. /// Flow:
  20. /// Complete all the jobs used as input dependencies (this is a sync point)
  21. /// Svelto entities are submitted
  22. /// Svelto Add and remove callback are called
  23. /// ISveltoOnDOTSStructuralEngine can use DOTSOperationsForSvelto in their add/remove/moove callbacks
  24. /// </summary>
  25. [DisableAutoCreation]
  26. public sealed partial class SveltoOnDOTSEntitiesSubmissionGroup: SystemBase, IQueryingEntitiesEngine, ISveltoOnDOTSSubmission
  27. {
  28. public SveltoOnDOTSEntitiesSubmissionGroup(EntitiesSubmissionScheduler submissionScheduler)
  29. {
  30. _submissionScheduler = submissionScheduler;
  31. _structuralEngines = new FasterList<ISveltoOnDOTSStructuralEngine>();
  32. }
  33. public EntitiesDB entitiesDB { get; set; }
  34. public void Ready() { }
  35. public void SubmitEntities(JobHandle jobHandle)
  36. {
  37. if (_submissionScheduler.paused == true || World.EntityManager == default)
  38. return;
  39. using (var profiler = new PlatformProfiler("SveltoDOTSEntitiesSubmissionGroup"))
  40. {
  41. using (profiler.Sample("Complete All Pending Jobs"))
  42. {
  43. jobHandle.Complete(); //sync-point
  44. #if UNITY_ECS_100
  45. EntityManager.CompleteAllTrackedJobs();
  46. #else
  47. EntityManager.CompleteAllJobs();
  48. #endif
  49. }
  50. //Submit Svelto Entities, calls Add/Remove/MoveTo that can be used by the DOTS ECSSubmissionEngines
  51. _submissionScheduler.SubmitEntities();
  52. foreach (var engine in _structuralEngines)
  53. engine.OnPostSubmission();
  54. _dotsOperationsForSvelto.Complete();
  55. }
  56. }
  57. public void Add(ISveltoOnDOTSStructuralEngine engine)
  58. {
  59. _structuralEngines.Add(engine);
  60. if (_isReady == true)
  61. {
  62. engine.DOTSOperations = _dotsOperationsForSvelto;
  63. engine.OnOperationsReady();
  64. }
  65. }
  66. protected override void OnCreate()
  67. {
  68. unsafe
  69. {
  70. _jobHandle = (JobHandle*) MemoryUtilities.NativeAlloc((uint)MemoryUtilities.SizeOf<JobHandle>(), Allocator.Persistent);
  71. _dotsOperationsForSvelto = new DOTSOperationsForSvelto(World.EntityManager, _jobHandle);
  72. _isReady = true;
  73. //initialise engines field while world was null
  74. foreach (var engine in _structuralEngines)
  75. {
  76. engine.DOTSOperations = _dotsOperationsForSvelto;
  77. engine.OnOperationsReady();
  78. }
  79. }
  80. }
  81. protected override void OnDestroy()
  82. {
  83. unsafe
  84. {
  85. base.OnDestroy();
  86. MemoryUtilities.NativeFree((IntPtr)_jobHandle, Allocator.Persistent);
  87. }
  88. }
  89. protected override void OnUpdate()
  90. {
  91. throw new NotSupportedException("if this is called something broke the original design");
  92. }
  93. readonly FasterList<ISveltoOnDOTSStructuralEngine> _structuralEngines;
  94. readonly EntitiesSubmissionScheduler _submissionScheduler;
  95. DOTSOperationsForSvelto _dotsOperationsForSvelto;
  96. bool _isReady;
  97. unsafe JobHandle* _jobHandle;
  98. }
  99. }
  100. #endif