설명 없음
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.

Converters.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace UnityEditor.Rendering.Universal
  6. {
  7. /// <summary>
  8. /// Filter for the list of converters used in batch mode.
  9. /// </summary>
  10. /// <seealso cref="Converters.RunInBatchMode(UnityEditor.Rendering.Universal.ConverterContainerId, List{UnityEditor.Rendering.Universal.ConverterId}, UnityEditor.Rendering.Universal.ConverterFilter)"/>.)
  11. public enum ConverterFilter
  12. {
  13. /// <summary>
  14. /// Use this to include converters matching the filter.
  15. /// </summary>
  16. Inclusive,
  17. /// <summary>
  18. /// Use this to exclude converters matching the filter.
  19. /// </summary>
  20. Exclusive
  21. }
  22. /// <summary>
  23. /// The container to run in batch mode.
  24. /// </summary>
  25. /// <seealso cref="Converters.RunInBatchMode(UnityEditor.Rendering.Universal.ConverterContainerId)"/>.)
  26. public enum ConverterContainerId
  27. {
  28. /// <summary>
  29. /// Use this for Built-in to URP converter.
  30. /// </summary>
  31. BuiltInToURP,
  32. /// <summary>
  33. /// Use this for Built-in to 2D (URP) converter.
  34. /// </summary>
  35. BuiltInToURP2D,
  36. /// <summary>
  37. /// Use this to upgrade 2D (URP) assets.
  38. /// </summary>
  39. UpgradeURP2DAssets,
  40. }
  41. /// <summary>
  42. /// The converter to run in batch mode.
  43. /// </summary>
  44. /// <seealso cref="Converters.RunInBatchMode(UnityEditor.Rendering.Universal.ConverterContainerId, List{UnityEditor.Rendering.Universal.ConverterId}, UnityEditor.Rendering.Universal.ConverterFilter)"/>.)
  45. public enum ConverterId
  46. {
  47. /// <summary>
  48. /// Use this for the material converters.
  49. /// </summary>
  50. Material,
  51. /// <summary>
  52. /// Use this for the render settings converters.
  53. /// </summary>
  54. RenderSettings,
  55. /// <summary>
  56. /// Use this for the animation clip converters.
  57. /// </summary>
  58. AnimationClip,
  59. /// <summary>
  60. /// Use this for readonly material converters.
  61. /// </summary>
  62. ReadonlyMaterial,
  63. /// <summary>
  64. /// Use this for post processing V2 converters.
  65. /// </summary>
  66. PPv2,
  67. /// <summary>
  68. /// Use this for parametric to freeform light converters.
  69. /// </summary>
  70. ParametricToFreeformLight,
  71. }
  72. /// <summary>
  73. /// Class for the converter framework.
  74. /// </summary>
  75. public static class Converters
  76. {
  77. static Type GetContainerType(ConverterContainerId containerName)
  78. {
  79. switch (containerName)
  80. {
  81. case ConverterContainerId.BuiltInToURP:
  82. return typeof(BuiltInToURPConverterContainer);
  83. case ConverterContainerId.BuiltInToURP2D:
  84. return typeof(BuiltInToURP2DConverterContainer);
  85. case ConverterContainerId.UpgradeURP2DAssets:
  86. return typeof(UpgradeURP2DAssetsContainer);
  87. }
  88. return null;
  89. }
  90. static Type GetConverterType(ConverterId converterName)
  91. {
  92. switch (converterName)
  93. {
  94. case ConverterId.Material:
  95. return typeof(UniversalRenderPipelineMaterialUpgrader);
  96. case ConverterId.RenderSettings:
  97. return typeof(RenderSettingsConverter);
  98. case ConverterId.AnimationClip:
  99. return typeof(AnimationClipConverter);
  100. case ConverterId.ReadonlyMaterial:
  101. return typeof(ReadonlyMaterialConverter);
  102. #if PPV2_EXISTS
  103. case ConverterId.PPv2:
  104. return typeof(PPv2Converter);
  105. #endif
  106. case ConverterId.ParametricToFreeformLight:
  107. return typeof(ParametricToFreeformLightUpgrader);
  108. }
  109. return null;
  110. }
  111. /// <summary>
  112. /// Call this method to run all the converters in a specific container in batch mode.
  113. /// </summary>
  114. /// <param name="containerName">The name of the container which will be batched. All Converters in this Container will run if prerequisites are met.</param>
  115. public static void RunInBatchMode(ConverterContainerId containerName)
  116. {
  117. Type typeName = GetContainerType(containerName);
  118. if (typeName != null)
  119. {
  120. RunInBatchMode(typeName);
  121. }
  122. }
  123. /// <summary>
  124. /// Call this method to run a specific list of converters in a specific container in batch mode.
  125. /// </summary>
  126. /// <param name="containerName">The name of the container which will be batched.</param>
  127. /// <param name="converterList">The list of converters that will be either included or excluded from batching. These converters need to be part of the passed in container for them to run.</param>
  128. /// <param name="converterFilter">The enum that decide if the list of converters will be included or excluded when batching.</param>
  129. public static void RunInBatchMode(ConverterContainerId containerName, List<ConverterId> converterList, ConverterFilter converterFilter)
  130. {
  131. Type containerType = GetContainerType(containerName);
  132. List<Type> converterTypes = new List<Type>(converterList.Count);
  133. foreach (ConverterId typeName in converterList)
  134. {
  135. var converterType = GetConverterType(typeName);
  136. if (containerType != null && !converterTypes.Contains(converterType))
  137. {
  138. converterTypes.Add(converterType);
  139. }
  140. }
  141. if (containerType != null && converterTypes.Any())
  142. {
  143. RunInBatchMode(containerType, converterTypes, converterFilter);
  144. }
  145. }
  146. internal static void RunInBatchMode(Type containerName, List<Type> converterList, ConverterFilter converterFilter)
  147. {
  148. Debug.Log($"Converter Batch Mode: {containerName}");
  149. var container = (RenderPipelineConverterContainer)Activator.CreateInstance(containerName);
  150. List<RenderPipelineConverter> converters = GetConvertersInContainer(container);
  151. List<RenderPipelineConverter> convertersToBatch = new List<RenderPipelineConverter>(converters.Count);
  152. // This is just a temp to deal with the Include and Exclude enum
  153. List<RenderPipelineConverter> tempConvertersToBatch = new List<RenderPipelineConverter>(converters.Count);
  154. if (converterFilter == ConverterFilter.Inclusive)
  155. {
  156. foreach (RenderPipelineConverter converter in converters)
  157. {
  158. if (converterList.Contains(converter.GetType()))
  159. {
  160. tempConvertersToBatch.Add(converter);
  161. }
  162. }
  163. }
  164. else if (converterFilter == ConverterFilter.Exclusive)
  165. {
  166. tempConvertersToBatch = converters;
  167. foreach (RenderPipelineConverter converter in converters)
  168. {
  169. if (converterList.Contains(converter.GetType()))
  170. {
  171. tempConvertersToBatch.Remove(converter);
  172. }
  173. }
  174. }
  175. convertersToBatch = tempConvertersToBatch;
  176. BatchConverters(convertersToBatch);
  177. }
  178. /// <summary>
  179. /// The method that will be run when converting the assets in batch mode.
  180. /// </summary>
  181. /// <param name="containerName">The name of the container which will be batched.</param>
  182. internal static void RunInBatchMode(Type containerName)
  183. {
  184. List<RenderPipelineConverter> converters = new List<RenderPipelineConverter>();
  185. var containers = TypeCache.GetTypesDerivedFrom<RenderPipelineConverterContainer>();
  186. foreach (var containerType in containers)
  187. {
  188. if (containerType == containerName)
  189. {
  190. var container = (RenderPipelineConverterContainer)Activator.CreateInstance(containerType);
  191. converters = GetConvertersInContainer(container);
  192. }
  193. }
  194. BatchConverters(converters);
  195. }
  196. internal static void BatchConverters(List<RenderPipelineConverter> converters)
  197. {
  198. // This need to be sorted by Priority property
  199. converters = converters.OrderBy(o => o.priority).ToList();
  200. foreach (RenderPipelineConverter converter in converters)
  201. {
  202. List<ConverterItemDescriptor> converterItemInfos = new List<ConverterItemDescriptor>();
  203. var initCtx = new InitializeConverterContext { items = converterItemInfos };
  204. initCtx.isBatchMode = true;
  205. converter.OnInitialize(initCtx, () => { });
  206. converter.OnPreRun();
  207. for (int i = 0; i < initCtx.items.Count; i++)
  208. {
  209. var item = new ConverterItemInfo()
  210. {
  211. index = i,
  212. descriptor = initCtx.items[i],
  213. };
  214. var ctx = new RunItemContext(item);
  215. ctx.isBatchMode = true;
  216. converter.OnRun(ref ctx);
  217. string converterStatus = ctx.didFail ? $"Fail\nInfo: {ctx.info}" : "Pass";
  218. Debug.Log($"Name: {ctx.item.descriptor.name}\nConverter Status: {converterStatus}");
  219. }
  220. converter.OnPostRun();
  221. AssetDatabase.SaveAssets();
  222. }
  223. }
  224. internal static List<RenderPipelineConverter> GetConvertersInContainer(RenderPipelineConverterContainer container)
  225. {
  226. List<RenderPipelineConverter> listOfConverters = new List<RenderPipelineConverter>();
  227. var converterList = TypeCache.GetTypesDerivedFrom<RenderPipelineConverter>();
  228. for (int i = 0; i < converterList.Count; ++i)
  229. {
  230. // Iterate over the converters that are used by the current container
  231. RenderPipelineConverter conv = (RenderPipelineConverter)Activator.CreateInstance(converterList[i]);
  232. if (conv.container == container.GetType())
  233. {
  234. listOfConverters.Add(conv);
  235. }
  236. }
  237. return listOfConverters;
  238. }
  239. }
  240. }