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.

215 line
7.8KB

  1. #if UNITY_ECS
  2. //#if !UNITY_ECS_050
  3. #define SLOW_SVELTO_ECB //Using EntityManager directly is much faster than using ECB because of the shared components
  4. //#endif
  5. using System;
  6. using System.Runtime.CompilerServices;
  7. using Unity.Entities;
  8. namespace Svelto.ECS.SveltoOnDOTS
  9. {
  10. public readonly struct EntityCommandBufferForSvelto
  11. {
  12. internal EntityCommandBufferForSvelto(EntityCommandBuffer value, EntityManager manager)
  13. {
  14. _ECB = value;
  15. _EManager = manager;
  16. }
  17. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  18. public Entity CreatePureDOTSEntity(EntityArchetype jointArchetype)
  19. {
  20. #if SLOW_SVELTO_ECB
  21. return _EManager.CreateEntity(jointArchetype);
  22. #else
  23. return _ECB.CreateEntity(jointArchetype);
  24. #endif
  25. }
  26. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  27. public void SetComponent<T>(Entity e, in T component) where T : struct, IComponentData
  28. {
  29. #if SLOW_SVELTO_ECB
  30. _EManager.SetComponentData<T>(e, component);
  31. #else
  32. _ECB.SetComponent(e, component);
  33. #endif
  34. }
  35. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  36. public void SetSharedComponent<T>(Entity e, in T component) where T : struct, ISharedComponentData
  37. {
  38. #if SLOW_SVELTO_ECB
  39. _EManager.SetSharedComponentData<T>(e, component);
  40. #else
  41. _ECB.SetSharedComponent(e, component);
  42. #endif
  43. }
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. ///Not ready for prime time with BURST yet, maybe with DOTS 1.0
  46. public static Entity CreateDOTSEntityOnSvelto(int sortKey, EntityCommandBuffer.ParallelWriter writer,
  47. Entity entityComponentPrefabEntity, EGID egid, bool mustHandleDOTSComponent)
  48. {
  49. #if !SLOW_SVELTO_ECB
  50. Entity dotsEntity = writer.Instantiate(sortKey, entityComponentPrefabEntity);
  51. //SharedComponentData can be used to group the DOTS ECS entities exactly like the Svelto ones
  52. writer.AddSharedComponent(sortKey, dotsEntity, new DOTSSveltoGroupID(egid.groupID));
  53. writer.AddComponent(sortKey, dotsEntity, new DOTSSveltoEGID(egid));
  54. if (mustHandleDOTSComponent)
  55. writer.AddSharedComponent(sortKey, dotsEntity, new DOTSEntityToSetup(egid.groupID));
  56. return dotsEntity;
  57. #endif
  58. throw new NotSupportedException();
  59. }
  60. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  61. internal Entity CreateDOTSEntityOnSvelto(Entity entityComponentPrefabEntity, EGID egid,
  62. bool mustHandleDOTSComponent)
  63. {
  64. #if SLOW_SVELTO_ECB
  65. Entity dotsEntity = _EManager.Instantiate(entityComponentPrefabEntity);
  66. //SharedComponentData can be used to group the DOTS ECS entities exactly like the Svelto ones
  67. _EManager.AddSharedComponentData(dotsEntity, new DOTSSveltoGroupID(egid.groupID));
  68. _EManager.AddComponentData(dotsEntity, new DOTSSveltoEGID(egid));
  69. if (mustHandleDOTSComponent)
  70. _EManager.AddSharedComponentData(dotsEntity, new DOTSEntityToSetup(egid.groupID));
  71. #else
  72. Entity dotsEntity = _ECB.Instantiate(entityComponentPrefabEntity);
  73. //SharedComponentData can be used to group the DOTS ECS entities exactly like the Svelto ones
  74. _ECB.AddSharedComponent(dotsEntity, new DOTSSveltoGroupID(egid.groupID));
  75. _ECB.AddComponent(dotsEntity, new DOTSSveltoEGID(egid));
  76. if (mustHandleDOTSComponent)
  77. _ECB.AddSharedComponent(dotsEntity, new DOTSEntityToSetup(egid.groupID));
  78. #endif
  79. return dotsEntity;
  80. }
  81. /// <summary>
  82. /// This method assumes that the Svelto entity with EGID egid has also dotsEntityComponent
  83. /// among the descriptors
  84. /// </summary>
  85. /// <param name="archetype"></param>
  86. /// <param name="egid"></param>
  87. /// <returns></returns>
  88. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  89. internal Entity CreateDOTSEntityOnSvelto(EntityArchetype archetype, EGID egid, bool mustHandleDOTSComponent)
  90. {
  91. #if SLOW_SVELTO_ECB
  92. Entity dotsEntity = _EManager.CreateEntity(archetype);
  93. //SharedComponentData can be used to group the DOTS ECS entities exactly like the Svelto ones
  94. _EManager.AddSharedComponentData(dotsEntity, new DOTSSveltoGroupID(egid.groupID));
  95. _EManager.AddComponentData(dotsEntity, new DOTSSveltoEGID(egid));
  96. if (mustHandleDOTSComponent)
  97. _EManager.AddSharedComponentData(dotsEntity, new DOTSEntityToSetup(egid.groupID));
  98. #else
  99. Entity dotsEntity = _ECB.CreateEntity(archetype);
  100. //SharedComponentData can be used to group the DOTS ECS entities exactly like the Svelto ones
  101. _ECB.AddSharedComponent(dotsEntity, new DOTSSveltoGroupID(egid.groupID));
  102. _ECB.AddComponent(dotsEntity, new DOTSSveltoEGID(egid));
  103. if (mustHandleDOTSComponent)
  104. _ECB.AddSharedComponent(dotsEntity, new DOTSEntityToSetup(egid.groupID));
  105. #endif
  106. return dotsEntity;
  107. }
  108. /// <summary>
  109. /// in this case the user decided to create a DOTS entity that is self managed and not managed
  110. /// by the framework
  111. /// </summary>
  112. /// <param name="archetype"></param>
  113. /// <param name="wireEgid"></param>
  114. /// <returns></returns>
  115. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  116. internal Entity CreateDOTSEntityUnmanaged(EntityArchetype archetype)
  117. {
  118. #if SLOW_SVELTO_ECB
  119. return _EManager.CreateEntity(archetype);
  120. #else
  121. return _ECB.CreateEntity(archetype);
  122. #endif
  123. }
  124. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  125. public void DestroyEntity(Entity e)
  126. {
  127. #if SLOW_SVELTO_ECB
  128. _EManager.DestroyEntity(e);
  129. #else
  130. _ECB.DestroyEntity(e);
  131. #endif
  132. }
  133. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  134. public void RemoveComponent<T>(Entity dotsEntity)
  135. {
  136. #if SLOW_SVELTO_ECB
  137. _EManager.RemoveComponent<T>(dotsEntity);
  138. #else
  139. _ECB.RemoveComponent<T>(dotsEntity);
  140. #endif
  141. }
  142. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  143. public void AddComponent<T>(Entity dotsEntity) where T : struct, IComponentData
  144. {
  145. #if SLOW_SVELTO_ECB
  146. _EManager.AddComponent<T>(dotsEntity);
  147. #else
  148. _ECB.AddComponent<T>(dotsEntity);
  149. #endif
  150. }
  151. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  152. public void AddComponent<T>(Entity dotsEntity, in T component) where T : struct, IComponentData
  153. {
  154. #if SLOW_SVELTO_ECB
  155. _EManager.AddComponentData(dotsEntity, component);
  156. #else
  157. _ECB.AddComponent(dotsEntity, component);
  158. #endif
  159. }
  160. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  161. public void AddSharedComponent<T>(Entity dotsEntity, in T component) where T : struct, ISharedComponentData
  162. {
  163. #if SLOW_SVELTO_ECB
  164. _EManager.AddSharedComponentData(dotsEntity, component);
  165. #else
  166. _ECB.AddSharedComponent(dotsEntity, component);
  167. #endif
  168. }
  169. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  170. public void AddBuffer<T>(Entity dotsEntity) where T : struct, IBufferElementData
  171. {
  172. #if SLOW_SVELTO_ECB
  173. _EManager.AddBuffer<T>(dotsEntity);
  174. #else
  175. _ECB.AddBuffer<T>(dotsEntity);
  176. #endif
  177. }
  178. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  179. public EntityCommandBuffer.ParallelWriter AsParallelWriter()
  180. {
  181. #if SLOW_SVELTO_ECB
  182. throw new System.Exception();
  183. #else
  184. return _ECB.AsParallelWriter();
  185. #endif
  186. }
  187. readonly EntityCommandBuffer _ECB;
  188. readonly EntityManager _EManager;
  189. }
  190. }
  191. #endif