Mirror of Svelto.ECS because we're a fan of it
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

104 satır
4.0KB

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