123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
-
- namespace UnityEditor.Rendering.Universal
- {
- /// <summary>
- /// Filter for the list of converters used in batch mode.
- /// </summary>
- /// <seealso cref="Converters.RunInBatchMode(UnityEditor.Rendering.Universal.ConverterContainerId, List{UnityEditor.Rendering.Universal.ConverterId}, UnityEditor.Rendering.Universal.ConverterFilter)"/>.)
- public enum ConverterFilter
- {
- /// <summary>
- /// Use this to include converters matching the filter.
- /// </summary>
- Inclusive,
-
- /// <summary>
- /// Use this to exclude converters matching the filter.
- /// </summary>
- Exclusive
- }
-
- /// <summary>
- /// The container to run in batch mode.
- /// </summary>
- /// <seealso cref="Converters.RunInBatchMode(UnityEditor.Rendering.Universal.ConverterContainerId)"/>.)
- public enum ConverterContainerId
- {
- /// <summary>
- /// Use this for Built-in to URP converter.
- /// </summary>
- BuiltInToURP,
-
- /// <summary>
- /// Use this for Built-in to 2D (URP) converter.
- /// </summary>
- BuiltInToURP2D,
-
- /// <summary>
- /// Use this to upgrade 2D (URP) assets.
- /// </summary>
- UpgradeURP2DAssets,
- }
-
- /// <summary>
- /// The converter to run in batch mode.
- /// </summary>
- /// <seealso cref="Converters.RunInBatchMode(UnityEditor.Rendering.Universal.ConverterContainerId, List{UnityEditor.Rendering.Universal.ConverterId}, UnityEditor.Rendering.Universal.ConverterFilter)"/>.)
- public enum ConverterId
- {
- /// <summary>
- /// Use this for the material converters.
- /// </summary>
- Material,
-
- /// <summary>
- /// Use this for the render settings converters.
- /// </summary>
- RenderSettings,
-
- /// <summary>
- /// Use this for the animation clip converters.
- /// </summary>
- AnimationClip,
-
- /// <summary>
- /// Use this for readonly material converters.
- /// </summary>
- ReadonlyMaterial,
-
- /// <summary>
- /// Use this for post processing V2 converters.
- /// </summary>
- PPv2,
-
- /// <summary>
- /// Use this for parametric to freeform light converters.
- /// </summary>
- ParametricToFreeformLight,
- }
-
- /// <summary>
- /// Class for the converter framework.
- /// </summary>
- public static class Converters
- {
- static Type GetContainerType(ConverterContainerId containerName)
- {
- switch (containerName)
- {
- case ConverterContainerId.BuiltInToURP:
- return typeof(BuiltInToURPConverterContainer);
- case ConverterContainerId.BuiltInToURP2D:
- return typeof(BuiltInToURP2DConverterContainer);
- case ConverterContainerId.UpgradeURP2DAssets:
- return typeof(UpgradeURP2DAssetsContainer);
- }
-
- return null;
- }
-
- static Type GetConverterType(ConverterId converterName)
- {
- switch (converterName)
- {
- case ConverterId.Material:
- return typeof(UniversalRenderPipelineMaterialUpgrader);
- case ConverterId.RenderSettings:
- return typeof(RenderSettingsConverter);
- case ConverterId.AnimationClip:
- return typeof(AnimationClipConverter);
- case ConverterId.ReadonlyMaterial:
- return typeof(ReadonlyMaterialConverter);
- #if PPV2_EXISTS
- case ConverterId.PPv2:
- return typeof(PPv2Converter);
- #endif
- case ConverterId.ParametricToFreeformLight:
- return typeof(ParametricToFreeformLightUpgrader);
- }
-
- return null;
- }
-
- /// <summary>
- /// Call this method to run all the converters in a specific container in batch mode.
- /// </summary>
- /// <param name="containerName">The name of the container which will be batched. All Converters in this Container will run if prerequisites are met.</param>
- public static void RunInBatchMode(ConverterContainerId containerName)
- {
- Type typeName = GetContainerType(containerName);
- if (typeName != null)
- {
- RunInBatchMode(typeName);
- }
- }
-
- /// <summary>
- /// Call this method to run a specific list of converters in a specific container in batch mode.
- /// </summary>
- /// <param name="containerName">The name of the container which will be batched.</param>
- /// <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>
- /// <param name="converterFilter">The enum that decide if the list of converters will be included or excluded when batching.</param>
- public static void RunInBatchMode(ConverterContainerId containerName, List<ConverterId> converterList, ConverterFilter converterFilter)
- {
- Type containerType = GetContainerType(containerName);
- List<Type> converterTypes = new List<Type>(converterList.Count);
- foreach (ConverterId typeName in converterList)
- {
- var converterType = GetConverterType(typeName);
- if (containerType != null && !converterTypes.Contains(converterType))
- {
- converterTypes.Add(converterType);
- }
- }
-
- if (containerType != null && converterTypes.Any())
- {
- RunInBatchMode(containerType, converterTypes, converterFilter);
- }
- }
-
- internal static void RunInBatchMode(Type containerName, List<Type> converterList, ConverterFilter converterFilter)
- {
- Debug.Log($"Converter Batch Mode: {containerName}");
- var container = (RenderPipelineConverterContainer)Activator.CreateInstance(containerName);
- List<RenderPipelineConverter> converters = GetConvertersInContainer(container);
-
- List<RenderPipelineConverter> convertersToBatch = new List<RenderPipelineConverter>(converters.Count);
- // This is just a temp to deal with the Include and Exclude enum
- List<RenderPipelineConverter> tempConvertersToBatch = new List<RenderPipelineConverter>(converters.Count);
-
- if (converterFilter == ConverterFilter.Inclusive)
- {
- foreach (RenderPipelineConverter converter in converters)
- {
- if (converterList.Contains(converter.GetType()))
- {
- tempConvertersToBatch.Add(converter);
- }
- }
- }
- else if (converterFilter == ConverterFilter.Exclusive)
- {
- tempConvertersToBatch = converters;
- foreach (RenderPipelineConverter converter in converters)
- {
- if (converterList.Contains(converter.GetType()))
- {
- tempConvertersToBatch.Remove(converter);
- }
- }
- }
-
- convertersToBatch = tempConvertersToBatch;
- BatchConverters(convertersToBatch);
- }
-
- /// <summary>
- /// The method that will be run when converting the assets in batch mode.
- /// </summary>
- /// <param name="containerName">The name of the container which will be batched.</param>
- internal static void RunInBatchMode(Type containerName)
- {
- List<RenderPipelineConverter> converters = new List<RenderPipelineConverter>();
- var containers = TypeCache.GetTypesDerivedFrom<RenderPipelineConverterContainer>();
- foreach (var containerType in containers)
- {
- if (containerType == containerName)
- {
- var container = (RenderPipelineConverterContainer)Activator.CreateInstance(containerType);
- converters = GetConvertersInContainer(container);
- }
- }
-
- BatchConverters(converters);
- }
-
- internal static void BatchConverters(List<RenderPipelineConverter> converters)
- {
- // This need to be sorted by Priority property
- converters = converters.OrderBy(o => o.priority).ToList();
-
- foreach (RenderPipelineConverter converter in converters)
- {
- List<ConverterItemDescriptor> converterItemInfos = new List<ConverterItemDescriptor>();
- var initCtx = new InitializeConverterContext { items = converterItemInfos };
- initCtx.isBatchMode = true;
- converter.OnInitialize(initCtx, () => { });
-
- converter.OnPreRun();
- for (int i = 0; i < initCtx.items.Count; i++)
- {
- var item = new ConverterItemInfo()
- {
- index = i,
- descriptor = initCtx.items[i],
- };
- var ctx = new RunItemContext(item);
- ctx.isBatchMode = true;
- converter.OnRun(ref ctx);
-
- string converterStatus = ctx.didFail ? $"Fail\nInfo: {ctx.info}" : "Pass";
- Debug.Log($"Name: {ctx.item.descriptor.name}\nConverter Status: {converterStatus}");
- }
-
- converter.OnPostRun();
-
- AssetDatabase.SaveAssets();
- }
- }
-
- internal static List<RenderPipelineConverter> GetConvertersInContainer(RenderPipelineConverterContainer container)
- {
- List<RenderPipelineConverter> listOfConverters = new List<RenderPipelineConverter>();
- var converterList = TypeCache.GetTypesDerivedFrom<RenderPipelineConverter>();
-
- for (int i = 0; i < converterList.Count; ++i)
- {
- // Iterate over the converters that are used by the current container
- RenderPipelineConverter conv = (RenderPipelineConverter)Activator.CreateInstance(converterList[i]);
- if (conv.container == container.GetType())
- {
- listOfConverters.Add(conv);
- }
- }
-
- return listOfConverters;
- }
- }
- }
|