MotionDispatcher.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using UnityEngine;
  4. using LitMotion.Collections;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. #endif
  8. namespace LitMotion
  9. {
  10. /// <summary>
  11. /// Motion dispatcher.
  12. /// </summary>
  13. public static class MotionDispatcher
  14. {
  15. static class StorageCache<TValue, TOptions, TAdapter>
  16. where TValue : unmanaged
  17. where TOptions : unmanaged, IMotionOptions
  18. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  19. {
  20. public static MotionStorage<TValue, TOptions, TAdapter> initialization;
  21. public static MotionStorage<TValue, TOptions, TAdapter> earlyUpdate;
  22. public static MotionStorage<TValue, TOptions, TAdapter> fixedUpdate;
  23. public static MotionStorage<TValue, TOptions, TAdapter> preUpdate;
  24. public static MotionStorage<TValue, TOptions, TAdapter> update;
  25. public static MotionStorage<TValue, TOptions, TAdapter> preLateUpdate;
  26. public static MotionStorage<TValue, TOptions, TAdapter> postLateUpdate;
  27. public static MotionStorage<TValue, TOptions, TAdapter> timeUpdate;
  28. public static MotionStorage<TValue, TOptions, TAdapter> GetOrCreate(PlayerLoopTiming playerLoopTiming)
  29. {
  30. return playerLoopTiming switch
  31. {
  32. PlayerLoopTiming.Initialization => CreateIfNull(ref initialization),
  33. PlayerLoopTiming.EarlyUpdate => CreateIfNull(ref earlyUpdate),
  34. PlayerLoopTiming.FixedUpdate => CreateIfNull(ref fixedUpdate),
  35. PlayerLoopTiming.PreUpdate => CreateIfNull(ref preUpdate),
  36. PlayerLoopTiming.Update => CreateIfNull(ref update),
  37. PlayerLoopTiming.PreLateUpdate => CreateIfNull(ref preLateUpdate),
  38. PlayerLoopTiming.PostLateUpdate => CreateIfNull(ref postLateUpdate),
  39. PlayerLoopTiming.TimeUpdate => CreateIfNull(ref timeUpdate),
  40. _ => null,
  41. };
  42. }
  43. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  44. static MotionStorage<TValue, TOptions, TAdapter> CreateIfNull(ref MotionStorage<TValue, TOptions, TAdapter> storage)
  45. {
  46. if (storage == null)
  47. {
  48. storage = new MotionStorage<TValue, TOptions, TAdapter>(MotionStorageManager.CurrentStorageId);
  49. MotionStorageManager.AddStorage(storage);
  50. }
  51. return storage;
  52. }
  53. }
  54. static class RunnerCache<TValue, TOptions, TAdapter>
  55. where TValue : unmanaged
  56. where TOptions : unmanaged, IMotionOptions
  57. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  58. {
  59. public static UpdateRunner<TValue, TOptions, TAdapter> initialization;
  60. public static UpdateRunner<TValue, TOptions, TAdapter> earlyUpdate;
  61. public static UpdateRunner<TValue, TOptions, TAdapter> fixedUpdate;
  62. public static UpdateRunner<TValue, TOptions, TAdapter> preUpdate;
  63. public static UpdateRunner<TValue, TOptions, TAdapter> update;
  64. public static UpdateRunner<TValue, TOptions, TAdapter> preLateUpdate;
  65. public static UpdateRunner<TValue, TOptions, TAdapter> postLateUpdate;
  66. public static UpdateRunner<TValue, TOptions, TAdapter> timeUpdate;
  67. public static (UpdateRunner<TValue, TOptions, TAdapter> runner, bool isCreated) GetOrCreate(PlayerLoopTiming playerLoopTiming, MotionStorage<TValue, TOptions, TAdapter> storage)
  68. {
  69. return playerLoopTiming switch
  70. {
  71. PlayerLoopTiming.Initialization => CreateIfNull(playerLoopTiming, ref initialization, storage),
  72. PlayerLoopTiming.EarlyUpdate => CreateIfNull(playerLoopTiming, ref earlyUpdate, storage),
  73. PlayerLoopTiming.FixedUpdate => CreateIfNull(playerLoopTiming, ref fixedUpdate, storage),
  74. PlayerLoopTiming.PreUpdate => CreateIfNull(playerLoopTiming, ref preUpdate, storage),
  75. PlayerLoopTiming.Update => CreateIfNull(playerLoopTiming, ref update, storage),
  76. PlayerLoopTiming.PreLateUpdate => CreateIfNull(playerLoopTiming, ref preLateUpdate, storage),
  77. PlayerLoopTiming.PostLateUpdate => CreateIfNull(playerLoopTiming, ref postLateUpdate, storage),
  78. PlayerLoopTiming.TimeUpdate => CreateIfNull(playerLoopTiming, ref timeUpdate, storage),
  79. _ => default,
  80. };
  81. }
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. static (UpdateRunner<TValue, TOptions, TAdapter>, bool) CreateIfNull(PlayerLoopTiming playerLoopTiming, ref UpdateRunner<TValue, TOptions, TAdapter> runner, MotionStorage<TValue, TOptions, TAdapter> storage)
  84. {
  85. if (runner == null)
  86. {
  87. if (playerLoopTiming == PlayerLoopTiming.FixedUpdate)
  88. {
  89. runner = new UpdateRunner<TValue, TOptions, TAdapter>(storage, Time.fixedTimeAsDouble, Time.fixedUnscaledTimeAsDouble, Time.realtimeSinceStartupAsDouble);
  90. }
  91. else
  92. {
  93. runner = new UpdateRunner<TValue, TOptions, TAdapter>(storage, Time.timeAsDouble, Time.unscaledTimeAsDouble, Time.realtimeSinceStartupAsDouble);
  94. }
  95. GetRunnerList(playerLoopTiming).Add(runner);
  96. return (runner, true);
  97. }
  98. return (runner, false);
  99. }
  100. }
  101. static FastListCore<IUpdateRunner> initializationRunners;
  102. static FastListCore<IUpdateRunner> earlyUpdateRunners;
  103. static FastListCore<IUpdateRunner> fixedUpdateRunners;
  104. static FastListCore<IUpdateRunner> preUpdateRunners;
  105. static FastListCore<IUpdateRunner> updateRunners;
  106. static FastListCore<IUpdateRunner> preLateUpdateRunners;
  107. static FastListCore<IUpdateRunner> postLateUpdateRunners;
  108. static FastListCore<IUpdateRunner> timeUpdateRunners;
  109. internal static FastListCore<IUpdateRunner> EmptyList = FastListCore<IUpdateRunner>.Empty;
  110. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  111. static ref FastListCore<IUpdateRunner> GetRunnerList(PlayerLoopTiming playerLoopTiming)
  112. {
  113. // FastListCore<T> must be passed as ref
  114. switch (playerLoopTiming)
  115. {
  116. default:
  117. return ref EmptyList;
  118. case PlayerLoopTiming.Initialization:
  119. return ref initializationRunners;
  120. case PlayerLoopTiming.EarlyUpdate:
  121. return ref earlyUpdateRunners;
  122. case PlayerLoopTiming.FixedUpdate:
  123. return ref fixedUpdateRunners;
  124. case PlayerLoopTiming.PreUpdate:
  125. return ref preUpdateRunners;
  126. case PlayerLoopTiming.Update:
  127. return ref updateRunners;
  128. case PlayerLoopTiming.PreLateUpdate:
  129. return ref preLateUpdateRunners;
  130. case PlayerLoopTiming.PostLateUpdate:
  131. return ref postLateUpdateRunners;
  132. case PlayerLoopTiming.TimeUpdate:
  133. return ref timeUpdateRunners;
  134. };
  135. }
  136. static Action<Exception> unhandledException = DefaultUnhandledExceptionHandler;
  137. static readonly PlayerLoopTiming[] playerLoopTimings = (PlayerLoopTiming[])Enum.GetValues(typeof(PlayerLoopTiming));
  138. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  139. static void Init()
  140. {
  141. Clear();
  142. }
  143. /// <summary>
  144. /// Set handling of unhandled exceptions.
  145. /// </summary>
  146. public static void RegisterUnhandledExceptionHandler(Action<Exception> unhandledExceptionHandler)
  147. {
  148. unhandledException = unhandledExceptionHandler;
  149. }
  150. /// <summary>
  151. /// Get handling of unhandled exceptions.
  152. /// </summary>
  153. public static Action<Exception> GetUnhandledExceptionHandler()
  154. {
  155. return unhandledException;
  156. }
  157. static void DefaultUnhandledExceptionHandler(Exception exception)
  158. {
  159. Debug.LogException(exception);
  160. }
  161. /// <summary>
  162. /// Cancel all motions.
  163. /// </summary>
  164. public static void Clear()
  165. {
  166. foreach (var playerLoopTiming in playerLoopTimings)
  167. {
  168. var span = GetRunnerList(playerLoopTiming).AsSpan();
  169. for (int i = 0; i < span.Length; i++)
  170. {
  171. span[i].Reset();
  172. }
  173. }
  174. }
  175. /// <summary>
  176. /// Ensures the storage capacity until it reaches at least `capacity`.
  177. /// </summary>
  178. /// <param name="capacity">The minimum capacity to ensure.</param>
  179. public static void EnsureStorageCapacity<TValue, TOptions, TAdapter>(int capacity)
  180. where TValue : unmanaged
  181. where TOptions : unmanaged, IMotionOptions
  182. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  183. {
  184. foreach (var playerLoopTiming in playerLoopTimings)
  185. {
  186. StorageCache<TValue, TOptions, TAdapter>.GetOrCreate(playerLoopTiming).EnsureCapacity(capacity);
  187. }
  188. #if UNITY_EDITOR
  189. EditorMotionDispatcher.EnsureStorageCapacity<TValue, TOptions, TAdapter>(capacity);
  190. #endif
  191. }
  192. internal static MotionHandle Schedule<TValue, TOptions, TAdapter>(in MotionData<TValue, TOptions> data, in MotionCallbackData callbackData, PlayerLoopTiming playerLoopTiming)
  193. where TValue : unmanaged
  194. where TOptions : unmanaged, IMotionOptions
  195. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  196. {
  197. var storage = StorageCache<TValue, TOptions, TAdapter>.GetOrCreate(playerLoopTiming);
  198. RunnerCache<TValue, TOptions, TAdapter>.GetOrCreate(playerLoopTiming, storage);
  199. var (EntryIndex, Version) = storage.Append(data, callbackData);
  200. return new MotionHandle()
  201. {
  202. StorageId = storage.StorageId,
  203. Index = EntryIndex,
  204. Version = Version
  205. };
  206. }
  207. internal static void Update(PlayerLoopTiming playerLoopTiming)
  208. {
  209. #if UNITY_EDITOR
  210. if (!EditorApplication.isPlaying) return;
  211. #endif
  212. var span = GetRunnerList(playerLoopTiming).AsSpan();
  213. if (playerLoopTiming == PlayerLoopTiming.FixedUpdate)
  214. {
  215. for (int i = 0; i < span.Length; i++) span[i].Update(Time.fixedTimeAsDouble, Time.fixedUnscaledTimeAsDouble, Time.realtimeSinceStartupAsDouble);
  216. }
  217. else
  218. {
  219. for (int i = 0; i < span.Length; i++) span[i].Update(Time.timeAsDouble, Time.unscaledTimeAsDouble, Time.realtimeSinceStartupAsDouble);
  220. }
  221. }
  222. }
  223. #if UNITY_EDITOR
  224. internal static class EditorMotionDispatcher
  225. {
  226. static class Cache<TValue, TOptions, TAdapter>
  227. where TValue : unmanaged
  228. where TOptions : unmanaged, IMotionOptions
  229. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  230. {
  231. static MotionStorage<TValue, TOptions, TAdapter> storage;
  232. static UpdateRunner<TValue, TOptions, TAdapter> updateRunner;
  233. public static MotionStorage<TValue, TOptions, TAdapter> GetOrCreateStorage()
  234. {
  235. if (storage == null)
  236. {
  237. storage = new MotionStorage<TValue, TOptions, TAdapter>(MotionStorageManager.CurrentStorageId);
  238. MotionStorageManager.AddStorage(storage);
  239. }
  240. return storage;
  241. }
  242. public static void InitUpdateRunner()
  243. {
  244. if (updateRunner == null)
  245. {
  246. var time = EditorApplication.timeSinceStartup;
  247. updateRunner = new UpdateRunner<TValue, TOptions, TAdapter>(storage, time, time, time);
  248. updateRunners.Add(updateRunner);
  249. }
  250. }
  251. }
  252. static FastListCore<IUpdateRunner> updateRunners;
  253. public static MotionHandle Schedule<TValue, TOptions, TAdapter>(in MotionData<TValue, TOptions> data, in MotionCallbackData callbackData)
  254. where TValue : unmanaged
  255. where TOptions : unmanaged, IMotionOptions
  256. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  257. {
  258. var storage = Cache<TValue, TOptions, TAdapter>.GetOrCreateStorage();
  259. Cache<TValue, TOptions, TAdapter>.InitUpdateRunner();
  260. var (EntryIndex, Version) = storage.Append(data, callbackData);
  261. return new MotionHandle()
  262. {
  263. StorageId = storage.StorageId,
  264. Index = EntryIndex,
  265. Version = Version
  266. };
  267. }
  268. public static void EnsureStorageCapacity<TValue, TOptions, TAdapter>(int capacity)
  269. where TValue : unmanaged
  270. where TOptions : unmanaged, IMotionOptions
  271. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  272. {
  273. Cache<TValue, TOptions, TAdapter>.GetOrCreateStorage().EnsureCapacity(capacity);
  274. }
  275. [InitializeOnLoadMethod]
  276. static void Init()
  277. {
  278. EditorApplication.update += Update;
  279. }
  280. static void Update()
  281. {
  282. var span = updateRunners.AsSpan();
  283. for (int i = 0; i < span.Length; i++)
  284. {
  285. span[i].Update(EditorApplication.timeSinceStartup, EditorApplication.timeSinceStartup, Time.realtimeSinceStartupAsDouble);
  286. }
  287. }
  288. }
  289. #endif
  290. }