Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ShaderScriptableStripper.cs 53KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. using UnityEngine;
  2. using UnityEngine.Rendering.Universal;
  3. using UnityEngine.Rendering;
  4. using System.Diagnostics.CodeAnalysis;
  5. namespace UnityEditor.Rendering.Universal
  6. {
  7. /// <summary>
  8. /// Class used for Scriptable Shader keyword stripping in URP.
  9. /// </summary>
  10. internal class ShaderScriptableStripper : IShaderVariantStripper, IShaderVariantStripperScope
  11. {
  12. public bool active => UniversalRenderPipeline.asset != null;
  13. // Interfaces / Structs
  14. // Interface for info gathered when making builds. Used for
  15. // Scriptable Stripping and when testing the scriptable stripper.
  16. internal interface IShaderScriptableStrippingData
  17. {
  18. public ShaderFeatures shaderFeatures { get; set; }
  19. public VolumeFeatures volumeFeatures { get; set; }
  20. public bool isGLDevice { get; set; }
  21. public bool strip2DPasses { get; set; }
  22. public bool stripSoftShadowQualityLevels { get; set; }
  23. public bool stripDebugDisplayShaders { get; set; }
  24. public bool stripScreenCoordOverrideVariants { get; set; }
  25. public bool stripUnusedVariants { get; set; }
  26. public bool stripUnusedPostProcessingVariants { get; set; }
  27. public bool stripUnusedXRVariants { get; set; }
  28. public Shader shader { get; set; }
  29. public ShaderType shaderType { get; set; }
  30. public ShaderCompilerPlatform shaderCompilerPlatform { get; set; }
  31. public string passName { get; set; }
  32. public PassType passType { get; set; }
  33. public PassIdentifier passIdentifier { get; set; }
  34. public bool IsHDRDisplaySupportEnabled { get; set; }
  35. public bool IsHDRShaderVariantValid { get; set; }
  36. public bool IsShaderFeatureEnabled(ShaderFeatures feature);
  37. public bool IsVolumeFeatureEnabled(VolumeFeatures feature);
  38. public bool IsKeywordEnabled(LocalKeyword keyword);
  39. public bool PassHasKeyword(LocalKeyword keyword);
  40. }
  41. // Data containing all the info needed to compare
  42. // against the features gathered in ShaderBuildPreprocessor.cs
  43. internal struct StrippingData : IShaderScriptableStrippingData
  44. {
  45. public ShaderFeatures shaderFeatures { get; set; }
  46. public VolumeFeatures volumeFeatures { get; set; }
  47. public bool isGLDevice { get => variantData.shaderCompilerPlatform == ShaderCompilerPlatform.GLES3x || variantData.shaderCompilerPlatform == ShaderCompilerPlatform.OpenGLCore; set{} }
  48. public bool stripSoftShadowQualityLevels { get; set; }
  49. public bool strip2DPasses { get; set; }
  50. public bool stripDebugDisplayShaders { get; set; }
  51. public bool stripScreenCoordOverrideVariants { get; set; }
  52. public bool stripUnusedVariants { get; set; }
  53. public bool stripUnusedPostProcessingVariants { get; set; }
  54. public bool stripUnusedXRVariants { get; set; }
  55. public Shader shader { get; set; }
  56. public ShaderType shaderType { get => passData.shaderType; set{} }
  57. public ShaderCompilerPlatform shaderCompilerPlatform { get => variantData.shaderCompilerPlatform; set {} }
  58. public string passName { get => passData.passName; set {} }
  59. public PassType passType { get => passData.passType; set {} }
  60. public PassIdentifier passIdentifier { get => passData.pass; set {} }
  61. public bool IsHDRDisplaySupportEnabled { get; set; }
  62. public bool IsHDRShaderVariantValid { get => HDROutputUtils.IsShaderVariantValid(variantData.shaderKeywordSet, PlayerSettings.allowHDRDisplaySupport); set { } }
  63. public bool IsKeywordEnabled(LocalKeyword keyword)
  64. {
  65. return variantData.shaderKeywordSet.IsEnabled(keyword);
  66. }
  67. public bool IsShaderFeatureEnabled(ShaderFeatures feature)
  68. {
  69. return (shaderFeatures & feature) != 0;
  70. }
  71. public bool IsVolumeFeatureEnabled(VolumeFeatures feature)
  72. {
  73. return (volumeFeatures & feature) != 0;
  74. }
  75. public bool PassHasKeyword(LocalKeyword keyword)
  76. {
  77. return ShaderUtil.PassHasKeyword(shader, passData.pass, keyword, passData.shaderType, shaderCompilerPlatform);
  78. }
  79. public ShaderSnippetData passData { get; set; }
  80. public ShaderCompilerData variantData { get; set; }
  81. }
  82. // Shaders
  83. Shader m_BokehDepthOfField = Shader.Find("Hidden/Universal Render Pipeline/BokehDepthOfField");
  84. Shader m_GaussianDepthOfField = Shader.Find("Hidden/Universal Render Pipeline/GaussianDepthOfField");
  85. Shader m_CameraMotionBlur = Shader.Find("Hidden/Universal Render Pipeline/CameraMotionBlur");
  86. Shader m_PaniniProjection = Shader.Find("Hidden/Universal Render Pipeline/PaniniProjection");
  87. Shader m_Bloom = Shader.Find("Hidden/Universal Render Pipeline/Bloom");
  88. Shader m_TerrainLit = Shader.Find("Universal Render Pipeline/Terrain/Lit");
  89. Shader m_StencilDeferred = Shader.Find("Hidden/Universal Render Pipeline/StencilDeferred");
  90. Shader m_UberPostShader = Shader.Find("Hidden/Universal Render Pipeline/UberPost");
  91. Shader m_HDROutputBlitShader = Shader.Find("Hidden/Universal/BlitHDROverlay");
  92. Shader m_DataDrivenLensFlareShader = Shader.Find("Hidden/Universal Render Pipeline/LensFlareDataDriven");
  93. Shader m_ScreenSpaceLensFlareShader = Shader.Find("Hidden/Universal Render Pipeline/LensFlareScreenSpace");
  94. Shader m_XROcclusionMeshShader = Shader.Find("Hidden/Universal Render Pipeline/XR/XROcclusionMesh");
  95. Shader m_XRMirrorViewShader = Shader.Find("Hidden/Universal Render Pipeline/XR/XRMirrorView");
  96. Shader m_XRMotionVectorShader = Shader.Find("Hidden/Universal Render Pipeline/XR/XRMotionVector");
  97. // Pass names
  98. public static readonly string kPassNameUniversal2D = "Universal2D";
  99. public static readonly string kPassNameGBuffer = "GBuffer";
  100. public static readonly string kPassNameForwardLit = "ForwardLit";
  101. public static readonly string kPassNameDepthNormals = "DepthNormals";
  102. public static readonly string kPassNameXRMotionVectors = "XRMotionVectors";
  103. // Keywords
  104. LocalKeyword m_MainLightShadows;
  105. LocalKeyword m_MainLightShadowsCascades;
  106. LocalKeyword m_MainLightShadowsScreen;
  107. LocalKeyword m_AdditionalLightsVertex;
  108. LocalKeyword m_AdditionalLightsPixel;
  109. LocalKeyword m_AdditionalLightShadows;
  110. LocalKeyword m_ReflectionProbeBlending;
  111. LocalKeyword m_ReflectionProbeBoxProjection;
  112. LocalKeyword m_CastingPunctualLightShadow;
  113. LocalKeyword m_SoftShadows;
  114. LocalKeyword m_SoftShadowsLow;
  115. LocalKeyword m_SoftShadowsMedium;
  116. LocalKeyword m_SoftShadowsHigh;
  117. LocalKeyword m_MixedLightingSubtractive;
  118. LocalKeyword m_LightmapShadowMixing;
  119. LocalKeyword m_ShadowsShadowMask;
  120. LocalKeyword m_Lightmap;
  121. LocalKeyword m_DynamicLightmap;
  122. LocalKeyword m_DirectionalLightmap;
  123. LocalKeyword m_AlphaTestOn;
  124. LocalKeyword m_GbufferNormalsOct;
  125. LocalKeyword m_ScreenSpaceOcclusion;
  126. LocalKeyword m_UseFastSRGBLinearConversion;
  127. LocalKeyword m_LightLayers;
  128. LocalKeyword m_DecalLayers;
  129. LocalKeyword m_WriteRenderingLayers;
  130. LocalKeyword m_RenderPassEnabled;
  131. LocalKeyword m_DebugDisplay;
  132. LocalKeyword m_DBufferMRT1;
  133. LocalKeyword m_DBufferMRT2;
  134. LocalKeyword m_DBufferMRT3;
  135. LocalKeyword m_DecalNormalBlendLow;
  136. LocalKeyword m_DecalNormalBlendMedium;
  137. LocalKeyword m_DecalNormalBlendHigh;
  138. LocalKeyword m_ForwardPlus;
  139. LocalKeyword m_FoveatedRenderingNonUniformRaster;
  140. LocalKeyword m_EditorVisualization;
  141. LocalKeyword m_LODFadeCrossFade;
  142. LocalKeyword m_LightCookies;
  143. LocalKeyword m_LensDistortion;
  144. LocalKeyword m_ChromaticAberration;
  145. LocalKeyword m_BloomLQ;
  146. LocalKeyword m_BloomHQ;
  147. LocalKeyword m_BloomLQDirt;
  148. LocalKeyword m_BloomHQDirt;
  149. LocalKeyword m_HdrGrading;
  150. LocalKeyword m_ToneMapACES;
  151. LocalKeyword m_ToneMapNeutral;
  152. LocalKeyword m_FilmGrain;
  153. LocalKeyword m_ScreenCoordOverride;
  154. LocalKeyword m_ProbeVolumesL1;
  155. LocalKeyword m_ProbeVolumesL2;
  156. LocalKeyword m_EasuRcasAndHDRInput;
  157. LocalKeyword m_Gamma20AndHDRInput;
  158. LocalKeyword m_SHPerVertex;
  159. LocalKeyword m_SHMixed;
  160. private LocalKeyword TryGetLocalKeyword(Shader shader, string name)
  161. {
  162. return shader.keywordSpace.FindKeyword(name);
  163. }
  164. private void InitializeLocalShaderKeywords([DisallowNull] Shader shader)
  165. {
  166. m_MainLightShadows = TryGetLocalKeyword(shader, ShaderKeywordStrings.MainLightShadows);
  167. m_MainLightShadowsCascades = TryGetLocalKeyword(shader, ShaderKeywordStrings.MainLightShadowCascades);
  168. m_MainLightShadowsScreen = TryGetLocalKeyword(shader, ShaderKeywordStrings.MainLightShadowScreen);
  169. m_AdditionalLightsVertex = TryGetLocalKeyword(shader, ShaderKeywordStrings.AdditionalLightsVertex);
  170. m_AdditionalLightsPixel = TryGetLocalKeyword(shader, ShaderKeywordStrings.AdditionalLightsPixel);
  171. m_AdditionalLightShadows = TryGetLocalKeyword(shader, ShaderKeywordStrings.AdditionalLightShadows);
  172. m_ReflectionProbeBlending = TryGetLocalKeyword(shader, ShaderKeywordStrings.ReflectionProbeBlending);
  173. m_ReflectionProbeBoxProjection = TryGetLocalKeyword(shader, ShaderKeywordStrings.ReflectionProbeBoxProjection);
  174. m_CastingPunctualLightShadow = TryGetLocalKeyword(shader, ShaderKeywordStrings.CastingPunctualLightShadow);
  175. m_SoftShadows = TryGetLocalKeyword(shader, ShaderKeywordStrings.SoftShadows);
  176. m_SoftShadowsLow = TryGetLocalKeyword(shader, ShaderKeywordStrings.SoftShadowsLow);
  177. m_SoftShadowsMedium = TryGetLocalKeyword(shader, ShaderKeywordStrings.SoftShadowsMedium);
  178. m_SoftShadowsHigh = TryGetLocalKeyword(shader, ShaderKeywordStrings.SoftShadowsHigh);
  179. m_MixedLightingSubtractive = TryGetLocalKeyword(shader, ShaderKeywordStrings.MixedLightingSubtractive);
  180. m_LightmapShadowMixing = TryGetLocalKeyword(shader, ShaderKeywordStrings.LightmapShadowMixing);
  181. m_ShadowsShadowMask = TryGetLocalKeyword(shader, ShaderKeywordStrings.ShadowsShadowMask);
  182. m_Lightmap = TryGetLocalKeyword(shader, ShaderKeywordStrings.LIGHTMAP_ON);
  183. m_DynamicLightmap = TryGetLocalKeyword(shader, ShaderKeywordStrings.DYNAMICLIGHTMAP_ON);
  184. m_DirectionalLightmap = TryGetLocalKeyword(shader, ShaderKeywordStrings.DIRLIGHTMAP_COMBINED);
  185. m_AlphaTestOn = TryGetLocalKeyword(shader, ShaderKeywordStrings._ALPHATEST_ON);
  186. m_GbufferNormalsOct = TryGetLocalKeyword(shader, ShaderKeywordStrings._GBUFFER_NORMALS_OCT);
  187. m_ScreenSpaceOcclusion = TryGetLocalKeyword(shader, ShaderKeywordStrings.ScreenSpaceOcclusion);
  188. m_UseFastSRGBLinearConversion = TryGetLocalKeyword(shader, ShaderKeywordStrings.UseFastSRGBLinearConversion);
  189. m_LightLayers = TryGetLocalKeyword(shader, ShaderKeywordStrings.LightLayers);
  190. m_DecalLayers = TryGetLocalKeyword(shader, ShaderKeywordStrings.DecalLayers);
  191. m_WriteRenderingLayers = TryGetLocalKeyword(shader, ShaderKeywordStrings.WriteRenderingLayers);
  192. m_RenderPassEnabled = TryGetLocalKeyword(shader, ShaderKeywordStrings.RenderPassEnabled);
  193. m_DebugDisplay = TryGetLocalKeyword(shader, ShaderKeywordStrings.DEBUG_DISPLAY);
  194. m_DBufferMRT1 = TryGetLocalKeyword(shader, ShaderKeywordStrings.DBufferMRT1);
  195. m_DBufferMRT2 = TryGetLocalKeyword(shader, ShaderKeywordStrings.DBufferMRT2);
  196. m_DBufferMRT3 = TryGetLocalKeyword(shader, ShaderKeywordStrings.DBufferMRT3);
  197. m_DecalNormalBlendLow = TryGetLocalKeyword(shader, ShaderKeywordStrings.DecalNormalBlendLow);
  198. m_DecalNormalBlendMedium = TryGetLocalKeyword(shader, ShaderKeywordStrings.DecalNormalBlendMedium);
  199. m_DecalNormalBlendHigh = TryGetLocalKeyword(shader, ShaderKeywordStrings.DecalNormalBlendHigh);
  200. m_ForwardPlus = TryGetLocalKeyword(shader, ShaderKeywordStrings.ForwardPlus);
  201. m_FoveatedRenderingNonUniformRaster = TryGetLocalKeyword(shader, ShaderKeywordStrings.FoveatedRenderingNonUniformRaster);
  202. m_EditorVisualization = TryGetLocalKeyword(shader, ShaderKeywordStrings.EDITOR_VISUALIZATION);
  203. m_LODFadeCrossFade = TryGetLocalKeyword(shader, ShaderKeywordStrings.LOD_FADE_CROSSFADE);
  204. m_LightCookies = TryGetLocalKeyword(shader, ShaderKeywordStrings.LightCookies);
  205. m_ScreenCoordOverride = TryGetLocalKeyword(shader, ShaderKeywordStrings.SCREEN_COORD_OVERRIDE);
  206. m_ProbeVolumesL1 = TryGetLocalKeyword(shader, ShaderKeywordStrings.ProbeVolumeL1);
  207. m_ProbeVolumesL2 = TryGetLocalKeyword(shader, ShaderKeywordStrings.ProbeVolumeL2);
  208. m_EasuRcasAndHDRInput = TryGetLocalKeyword(shader, ShaderKeywordStrings.EasuRcasAndHDRInput);
  209. m_Gamma20AndHDRInput = TryGetLocalKeyword(shader, ShaderKeywordStrings.Gamma20AndHDRInput);
  210. // Post processing
  211. m_LensDistortion = TryGetLocalKeyword(shader, ShaderKeywordStrings.Distortion);
  212. m_ChromaticAberration = TryGetLocalKeyword(shader, ShaderKeywordStrings.ChromaticAberration);
  213. m_BloomLQ = TryGetLocalKeyword(shader, ShaderKeywordStrings.BloomLQ);
  214. m_BloomHQ = TryGetLocalKeyword(shader, ShaderKeywordStrings.BloomHQ);
  215. m_BloomLQDirt = TryGetLocalKeyword(shader, ShaderKeywordStrings.BloomLQDirt);
  216. m_BloomHQDirt = TryGetLocalKeyword(shader, ShaderKeywordStrings.BloomHQDirt);
  217. m_HdrGrading = TryGetLocalKeyword(shader, ShaderKeywordStrings.HDRGrading);
  218. m_ToneMapACES = TryGetLocalKeyword(shader, ShaderKeywordStrings.TonemapACES);
  219. m_ToneMapNeutral = TryGetLocalKeyword(shader, ShaderKeywordStrings.TonemapNeutral);
  220. m_FilmGrain = TryGetLocalKeyword(shader, ShaderKeywordStrings.FilmGrain);
  221. m_SHPerVertex = TryGetLocalKeyword(shader, ShaderKeywordStrings.EVALUATE_SH_VERTEX);
  222. m_SHMixed = TryGetLocalKeyword(shader, ShaderKeywordStrings.EVALUATE_SH_MIXED);
  223. }
  224. /*********************************************************
  225. Volume Features
  226. *********************************************************/
  227. internal bool StripVolumeFeatures_UberPostShader(ref IShaderScriptableStrippingData strippingData)
  228. {
  229. if (strippingData.shader != m_UberPostShader)
  230. return false;
  231. ShaderStripTool<VolumeFeatures> stripTool = new ShaderStripTool<VolumeFeatures>(strippingData.volumeFeatures, ref strippingData);
  232. if (stripTool.StripMultiCompileKeepOffVariant(m_LensDistortion, VolumeFeatures.LensDistortion))
  233. return true;
  234. if (stripTool.StripMultiCompileKeepOffVariant(m_ChromaticAberration, VolumeFeatures.ChromaticAberration))
  235. return true;
  236. if (stripTool.StripMultiCompileKeepOffVariant(m_BloomLQ, VolumeFeatures.BloomLQ))
  237. return true;
  238. if (stripTool.StripMultiCompileKeepOffVariant(m_BloomHQ, VolumeFeatures.BloomHQ))
  239. return true;
  240. if (stripTool.StripMultiCompileKeepOffVariant(m_BloomLQDirt, VolumeFeatures.BloomLQDirt))
  241. return true;
  242. if (stripTool.StripMultiCompileKeepOffVariant(m_BloomHQDirt, VolumeFeatures.BloomHQDirt))
  243. return true;
  244. if (stripTool.StripMultiCompileKeepOffVariant(m_ToneMapACES, VolumeFeatures.ToneMapping))
  245. return true;
  246. if (stripTool.StripMultiCompileKeepOffVariant(m_ToneMapNeutral, VolumeFeatures.ToneMapping))
  247. return true;
  248. if (stripTool.StripMultiCompileKeepOffVariant(m_FilmGrain, VolumeFeatures.FilmGrain))
  249. return true;
  250. return false;
  251. }
  252. internal bool StripVolumeFeatures_BokehDepthOfFieldShader(ref IShaderScriptableStrippingData strippingData)
  253. {
  254. if (strippingData.shader != m_BokehDepthOfField)
  255. return false;
  256. return !strippingData.IsVolumeFeatureEnabled(VolumeFeatures.DepthOfField);
  257. }
  258. internal bool StripVolumeFeatures_GaussianDepthOfFieldShader(ref IShaderScriptableStrippingData strippingData)
  259. {
  260. if (strippingData.shader != m_GaussianDepthOfField)
  261. return false;
  262. return !strippingData.IsVolumeFeatureEnabled(VolumeFeatures.DepthOfField);
  263. }
  264. internal bool StripVolumeFeatures_CameraMotionBlurShader(ref IShaderScriptableStrippingData strippingData)
  265. {
  266. if (strippingData.shader != m_CameraMotionBlur)
  267. return false;
  268. return !strippingData.IsVolumeFeatureEnabled(VolumeFeatures.CameraMotionBlur);
  269. }
  270. internal bool StripVolumeFeatures_PaniniProjectionShader(ref IShaderScriptableStrippingData strippingData)
  271. {
  272. if (strippingData.shader != m_PaniniProjection)
  273. return false;
  274. return !strippingData.IsVolumeFeatureEnabled(VolumeFeatures.PaniniProjection);
  275. }
  276. internal bool StripVolumeFeatures_BloomShader(ref IShaderScriptableStrippingData strippingData)
  277. {
  278. if (strippingData.shader != m_Bloom)
  279. return false;
  280. bool isBloomEnabled = strippingData.IsVolumeFeatureEnabled(VolumeFeatures.BloomHQ)
  281. || strippingData.IsVolumeFeatureEnabled(VolumeFeatures.BloomHQDirt)
  282. || strippingData.IsVolumeFeatureEnabled(VolumeFeatures.BloomLQ)
  283. || strippingData.IsVolumeFeatureEnabled(VolumeFeatures.BloomLQDirt);
  284. return !isBloomEnabled;
  285. }
  286. internal bool StripVolumeFeatures(VolumeFeatures features, ref IShaderScriptableStrippingData strippingData)
  287. {
  288. if (StripVolumeFeatures_UberPostShader(ref strippingData))
  289. return true;
  290. if (StripVolumeFeatures_BokehDepthOfFieldShader(ref strippingData))
  291. return true;
  292. if (StripVolumeFeatures_GaussianDepthOfFieldShader(ref strippingData))
  293. return true;
  294. if (StripVolumeFeatures_CameraMotionBlurShader(ref strippingData))
  295. return true;
  296. if (StripVolumeFeatures_PaniniProjectionShader(ref strippingData))
  297. return true;
  298. if (StripVolumeFeatures_BloomShader(ref strippingData))
  299. return true;
  300. return false;
  301. }
  302. /*********************************************************
  303. Unused Variants
  304. *********************************************************/
  305. internal bool StripUnusedFeatures_DebugDisplay(ref IShaderScriptableStrippingData strippingData)
  306. {
  307. return strippingData.stripDebugDisplayShaders && strippingData.IsKeywordEnabled(m_DebugDisplay);
  308. }
  309. internal bool StripUnusedFeatures_ScreenCoordOverride(ref IShaderScriptableStrippingData strippingData)
  310. {
  311. return strippingData.stripScreenCoordOverrideVariants && strippingData.IsKeywordEnabled(m_ScreenCoordOverride);
  312. }
  313. internal bool StripUnusedFeatures_PunctualLightShadows(ref IShaderScriptableStrippingData strippingData)
  314. {
  315. // Shadow caster punctual light strip
  316. if (strippingData.passType == PassType.ShadowCaster && strippingData.PassHasKeyword(m_CastingPunctualLightShadow))
  317. {
  318. bool mainLightShadowsDisabled =
  319. !strippingData.IsShaderFeatureEnabled(ShaderFeatures.MainLightShadows) &&
  320. !strippingData.IsShaderFeatureEnabled(ShaderFeatures.MainLightShadowsCascade) &&
  321. !strippingData.IsShaderFeatureEnabled(ShaderFeatures.ScreenSpaceShadows);
  322. if (mainLightShadowsDisabled && !strippingData.IsKeywordEnabled(m_CastingPunctualLightShadow))
  323. return true;
  324. if (!strippingData.IsShaderFeatureEnabled(ShaderFeatures.AdditionalLightShadows) && strippingData.IsKeywordEnabled(m_CastingPunctualLightShadow))
  325. return true;
  326. }
  327. return false;
  328. }
  329. internal bool StripUnusedFeatures_FoveatedRendering(ref IShaderScriptableStrippingData strippingData)
  330. {
  331. // Strip Foveated Rendering variants on all platforms (except PS5 and Metal)
  332. // TODO: add a way to communicate this requirement from the xr plugin directly
  333. #if ENABLE_VR && ENABLE_XR_MODULE
  334. if (strippingData.shaderCompilerPlatform != ShaderCompilerPlatform.PS5NGGC && strippingData.shaderCompilerPlatform != ShaderCompilerPlatform.Metal)
  335. #endif
  336. {
  337. if (strippingData.IsKeywordEnabled(m_FoveatedRenderingNonUniformRaster))
  338. return true;
  339. }
  340. return false;
  341. }
  342. internal bool StripUnusedFeatures_DeferredRendering(ref IShaderScriptableStrippingData strippingData)
  343. {
  344. // TODO: Test against lightMode tag instead.
  345. if (strippingData.passName == kPassNameGBuffer)
  346. {
  347. if (!strippingData.IsShaderFeatureEnabled(ShaderFeatures.DeferredShading))
  348. return true;
  349. }
  350. return false;
  351. }
  352. internal bool StripUnusedFeatures_MainLightShadows(ref IShaderScriptableStrippingData strippingData, ref ShaderStripTool<ShaderFeatures> stripTool)
  353. {
  354. // strip main light shadows, cascade and screen variants
  355. if (strippingData.IsShaderFeatureEnabled(ShaderFeatures.ShadowsKeepOffVariants))
  356. {
  357. if (stripTool.StripMultiCompileKeepOffVariant(
  358. m_MainLightShadows, ShaderFeatures.MainLightShadows,
  359. m_MainLightShadowsCascades, ShaderFeatures.MainLightShadowsCascade,
  360. m_MainLightShadowsScreen, ShaderFeatures.ScreenSpaceShadows))
  361. return true;
  362. }
  363. else
  364. {
  365. if (stripTool.StripMultiCompile(
  366. m_MainLightShadows, ShaderFeatures.MainLightShadows,
  367. m_MainLightShadowsCascades, ShaderFeatures.MainLightShadowsCascade,
  368. m_MainLightShadowsScreen, ShaderFeatures.ScreenSpaceShadows))
  369. return true;
  370. }
  371. return false;
  372. }
  373. internal bool StripUnusedFeatures_AdditionalLightShadows(ref IShaderScriptableStrippingData strippingData, ref ShaderStripTool<ShaderFeatures> stripTool)
  374. {
  375. // No additional light shadows
  376. if (strippingData.IsShaderFeatureEnabled(ShaderFeatures.ShadowsKeepOffVariants))
  377. {
  378. if (stripTool.StripMultiCompileKeepOffVariant(m_AdditionalLightShadows, ShaderFeatures.AdditionalLightShadows))
  379. return true;
  380. }
  381. else if (stripTool.StripMultiCompile(m_AdditionalLightShadows, ShaderFeatures.AdditionalLightShadows))
  382. return true;
  383. return false;
  384. }
  385. internal bool StripUnusedFeatures_MixedLighting(ref IShaderScriptableStrippingData strippingData)
  386. {
  387. // Strip here only if mixed lighting is disabled
  388. if (!strippingData.IsShaderFeatureEnabled(ShaderFeatures.MixedLighting))
  389. {
  390. if (strippingData.IsKeywordEnabled(m_MixedLightingSubtractive))
  391. return true;
  392. // No need to check here if actually used by scenes as this taken care by builtin stripper
  393. if (strippingData.IsKeywordEnabled(m_LightmapShadowMixing) || strippingData.IsKeywordEnabled(m_ShadowsShadowMask))
  394. return true;
  395. }
  396. return false;
  397. }
  398. internal bool StripUnusedFeatures_SoftShadows(ref ShaderStripTool<ShaderFeatures> stripTool)
  399. {
  400. // TODO: Strip off variants once we have global soft shadows option for forcing instead as support
  401. return stripTool.StripMultiCompileKeepOffVariant(m_SoftShadows, ShaderFeatures.SoftShadows);
  402. }
  403. internal bool StripUnusedFeatures_SoftShadowsQualityLevels(ref IShaderScriptableStrippingData strippingData, ref ShaderStripTool<ShaderFeatures> stripTool)
  404. {
  405. var forcedStrip = strippingData.stripSoftShadowQualityLevels &&
  406. (strippingData.IsShaderFeatureEnabled(ShaderFeatures.SoftShadowsLow) ||
  407. strippingData.IsShaderFeatureEnabled(ShaderFeatures.SoftShadowsMedium) ||
  408. strippingData.IsShaderFeatureEnabled(ShaderFeatures.SoftShadowsHigh));
  409. return forcedStrip || (stripTool.StripMultiCompileKeepOffVariant(
  410. m_SoftShadowsLow, ShaderFeatures.SoftShadowsLow,
  411. m_SoftShadowsMedium, ShaderFeatures.SoftShadowsMedium,
  412. m_SoftShadowsHigh, ShaderFeatures.SoftShadowsHigh));
  413. }
  414. internal bool StripUnusedFeatures_HDRGrading(ref ShaderStripTool<ShaderFeatures> stripTool)
  415. {
  416. return stripTool.StripMultiCompileKeepOffVariant(m_HdrGrading, ShaderFeatures.HdrGrading);
  417. }
  418. internal bool StripUnusedFeatures_UseFastSRGBLinearConversion(ref ShaderStripTool<ShaderFeatures> stripTool)
  419. {
  420. return stripTool.StripMultiCompile(m_UseFastSRGBLinearConversion, ShaderFeatures.UseFastSRGBLinearConversion);
  421. }
  422. internal bool StripUnusedFeatures_LightLayers(ref ShaderStripTool<ShaderFeatures> stripTool)
  423. {
  424. return stripTool.StripMultiCompile(m_LightLayers, ShaderFeatures.LightLayers);
  425. }
  426. internal bool StripUnusedFeatures_RenderPassEnabled(ref ShaderStripTool<ShaderFeatures> stripTool)
  427. {
  428. return stripTool.StripMultiCompileKeepOffVariant(m_RenderPassEnabled, ShaderFeatures.RenderPassEnabled);
  429. }
  430. internal bool StripUnusedFeatures_ReflectionProbes(ref ShaderStripTool<ShaderFeatures> stripTool)
  431. {
  432. // Reflection probes
  433. if (stripTool.StripMultiCompile(m_ReflectionProbeBlending, ShaderFeatures.ReflectionProbeBlending))
  434. return true;
  435. if (stripTool.StripMultiCompile(m_ReflectionProbeBoxProjection, ShaderFeatures.ReflectionProbeBoxProjection))
  436. return true;
  437. return false;
  438. }
  439. internal bool StripUnusedFeatures_ForwardPlus(ref ShaderStripTool<ShaderFeatures> stripTool)
  440. {
  441. return stripTool.StripMultiCompile(m_ForwardPlus, ShaderFeatures.ForwardPlus);
  442. }
  443. internal bool StripUnusedFeatures_SHAuto(ref IShaderScriptableStrippingData strippingData, ref ShaderStripTool<ShaderFeatures> stripTool)
  444. {
  445. // SH auto mode is per-vertex or per-pixel. Strip unused variants
  446. if (strippingData.IsShaderFeatureEnabled(ShaderFeatures.AutoSHMode))
  447. {
  448. if (strippingData.IsShaderFeatureEnabled(ShaderFeatures.AutoSHModePerVertex))
  449. {
  450. // Strip Mixed variant and Off(perPixel) variant
  451. if (stripTool.StripMultiCompile(m_SHMixed, ShaderFeatures.ExplicitSHMode, m_SHPerVertex, ShaderFeatures.AutoSHModePerVertex))
  452. return true;
  453. }
  454. else
  455. {
  456. // Strip Mixed variant and PerVertex variant
  457. if (stripTool.StripMultiCompileKeepOffVariant(m_SHPerVertex, ShaderFeatures.AutoSHModePerVertex, m_SHMixed, ShaderFeatures.ExplicitSHMode))
  458. return true;
  459. }
  460. }
  461. return false;
  462. }
  463. internal bool StripUnusedFeatures_AdditionalLights(ref IShaderScriptableStrippingData strippingData, ref ShaderStripTool<ShaderFeatures> stripTool)
  464. {
  465. // Forward Plus doesn't use Vertex or the Pixel Light variants.
  466. // It enables the Pixel keyword through a define.
  467. if (strippingData.IsShaderFeatureEnabled(ShaderFeatures.ForwardPlus))
  468. {
  469. if (strippingData.IsShaderFeatureEnabled(ShaderFeatures.AdditionalLightsVertex))
  470. return true;
  471. if (strippingData.IsShaderFeatureEnabled(ShaderFeatures.AdditionalLightsPixel))
  472. return true;
  473. }
  474. // Additional light are shaded per-vertex or per-pixel.
  475. if (strippingData.IsShaderFeatureEnabled(ShaderFeatures.AdditionalLightsKeepOffVariants))
  476. {
  477. if (stripTool.StripMultiCompileKeepOffVariant(m_AdditionalLightsVertex, ShaderFeatures.AdditionalLightsVertex, m_AdditionalLightsPixel, ShaderFeatures.AdditionalLightsPixel))
  478. return true;
  479. }
  480. else
  481. {
  482. if (stripTool.StripMultiCompile(m_AdditionalLightsVertex, ShaderFeatures.AdditionalLightsVertex, m_AdditionalLightsPixel, ShaderFeatures.AdditionalLightsPixel))
  483. return true;
  484. }
  485. return false;
  486. }
  487. internal bool StripUnusedFeatures_ScreenSpaceOcclusion(ref IShaderScriptableStrippingData strippingData, ref ShaderStripTool<ShaderFeatures> stripTool)
  488. {
  489. // Screen Space Occlusion
  490. if (strippingData.IsShaderFeatureEnabled(ShaderFeatures.ScreenSpaceOcclusionAfterOpaque))
  491. {
  492. // SSAO after opaque setting requires off variants
  493. if (stripTool.StripMultiCompileKeepOffVariant(m_ScreenSpaceOcclusion, ShaderFeatures.ScreenSpaceOcclusion))
  494. return true;
  495. }
  496. else
  497. {
  498. if (stripTool.StripMultiCompile(m_ScreenSpaceOcclusion, ShaderFeatures.ScreenSpaceOcclusion))
  499. return true;
  500. }
  501. return false;
  502. }
  503. internal bool StripUnusedFeatures_DecalsDbuffer(ref IShaderScriptableStrippingData strippingData, ref ShaderStripTool<ShaderFeatures> stripTool)
  504. {
  505. // DBuffer
  506. if (strippingData.isGLDevice)
  507. {
  508. // Decal DBuffer is not supported on gl
  509. if (strippingData.IsKeywordEnabled(m_DBufferMRT1) ||
  510. strippingData.IsKeywordEnabled(m_DBufferMRT2) ||
  511. strippingData.IsKeywordEnabled(m_DBufferMRT3))
  512. return true;
  513. }
  514. else
  515. {
  516. if (stripTool.StripMultiCompile(
  517. m_DBufferMRT1, ShaderFeatures.DBufferMRT1,
  518. m_DBufferMRT2, ShaderFeatures.DBufferMRT2,
  519. m_DBufferMRT3, ShaderFeatures.DBufferMRT3))
  520. return true;
  521. }
  522. return false;
  523. }
  524. internal bool StripUnusedFeatures_DecalsNormalBlend(ref IShaderScriptableStrippingData strippingData, ref ShaderStripTool<ShaderFeatures> stripTool)
  525. {
  526. // Decal Normal Blend
  527. if (stripTool.StripMultiCompile(
  528. m_DecalNormalBlendLow, ShaderFeatures.DecalNormalBlendLow,
  529. m_DecalNormalBlendMedium, ShaderFeatures.DecalNormalBlendMedium,
  530. m_DecalNormalBlendHigh, ShaderFeatures.DecalNormalBlendHigh))
  531. return true;
  532. return false;
  533. }
  534. internal bool StripUnusedFeatures_DecalLayers(ref IShaderScriptableStrippingData strippingData, ref ShaderStripTool<ShaderFeatures> stripTool)
  535. {
  536. // Rendering layers are not supported on gl
  537. if (strippingData.isGLDevice)
  538. {
  539. if (strippingData.IsKeywordEnabled(m_DecalLayers))
  540. return true;
  541. }
  542. else if (stripTool.StripMultiCompile(m_DecalLayers, ShaderFeatures.DecalLayers))
  543. return true;
  544. return false;
  545. }
  546. internal bool StripUnusedFeatures_WriteRenderingLayers(ref IShaderScriptableStrippingData strippingData, ref ShaderStripTool<ShaderFeatures> stripTool)
  547. {
  548. // Rendering layers are not supported on gl
  549. if (strippingData.isGLDevice)
  550. {
  551. if (strippingData.IsKeywordEnabled(m_WriteRenderingLayers))
  552. return true;
  553. }
  554. else
  555. {
  556. if (strippingData.passName == kPassNameDepthNormals)
  557. {
  558. if (stripTool.StripMultiCompile(m_WriteRenderingLayers, ShaderFeatures.DepthNormalPassRenderingLayers))
  559. return true;
  560. }
  561. if (strippingData.passName == kPassNameForwardLit)
  562. {
  563. if (stripTool.StripMultiCompile(m_WriteRenderingLayers, ShaderFeatures.OpaqueWriteRenderingLayers))
  564. return true;
  565. }
  566. if (strippingData.passName == kPassNameGBuffer)
  567. {
  568. if (stripTool.StripMultiCompile(m_WriteRenderingLayers, ShaderFeatures.GBufferWriteRenderingLayers))
  569. return true;
  570. }
  571. }
  572. return false;
  573. }
  574. internal bool StripUnusedFeatures_AccurateGbufferNormals(ref IShaderScriptableStrippingData strippingData, ref ShaderStripTool<ShaderFeatures> stripTool)
  575. {
  576. return stripTool.StripMultiCompile(m_GbufferNormalsOct, ShaderFeatures.AccurateGbufferNormals);
  577. }
  578. internal bool StripUnusedFeatures_LightCookies(ref IShaderScriptableStrippingData strippingData, ref ShaderStripTool<ShaderFeatures> stripTool)
  579. {
  580. return stripTool.StripMultiCompileKeepOffVariant(m_LightCookies, ShaderFeatures.LightCookies);
  581. }
  582. internal bool StripUnusedFeatures_ProbesVolumes(ref ShaderStripTool<ShaderFeatures> stripTool)
  583. {
  584. return stripTool.StripMultiCompileKeepOffVariant(m_ProbeVolumesL1, ShaderFeatures.ProbeVolumeL1, m_ProbeVolumesL2, ShaderFeatures.ProbeVolumeL2);
  585. }
  586. internal bool StripUnusedFeatures_DataDrivenLensFlare(ref IShaderScriptableStrippingData strippingData)
  587. {
  588. // If this is not the right shader, then skip
  589. if (strippingData.shader != m_DataDrivenLensFlareShader)
  590. return false;
  591. return !strippingData.IsShaderFeatureEnabled(ShaderFeatures.DataDrivenLensFlare);
  592. }
  593. internal bool StripUnusedFeatures_ScreenSpaceLensFlare(ref IShaderScriptableStrippingData strippingData)
  594. {
  595. // If this is not the right shader, then skip
  596. if (strippingData.shader != m_ScreenSpaceLensFlareShader)
  597. return false;
  598. return !strippingData.IsShaderFeatureEnabled(ShaderFeatures.ScreenSpaceLensFlare);
  599. }
  600. internal bool StripUnusedFeatures_XRMirrorView(ref IShaderScriptableStrippingData strippingData)
  601. {
  602. if (strippingData.shader != m_XRMirrorViewShader)
  603. return false;
  604. return strippingData.stripUnusedXRVariants;
  605. }
  606. internal bool StripUnusedFeatures_XROcclusionMesh(ref IShaderScriptableStrippingData strippingData)
  607. {
  608. if (strippingData.shader != m_XROcclusionMeshShader)
  609. return false;
  610. return strippingData.stripUnusedXRVariants;
  611. }
  612. internal bool StripUnusedFeatures_XRMotionVector(ref IShaderScriptableStrippingData strippingData)
  613. {
  614. if (strippingData.shader != m_XRMotionVectorShader)
  615. return false;
  616. return strippingData.stripUnusedXRVariants;
  617. }
  618. internal bool StripUnusedFeatures(ref IShaderScriptableStrippingData strippingData)
  619. {
  620. if (StripUnusedFeatures_DebugDisplay(ref strippingData))
  621. return true;
  622. if (StripUnusedFeatures_ScreenCoordOverride(ref strippingData))
  623. return true;
  624. if (StripUnusedFeatures_MixedLighting(ref strippingData))
  625. return true;
  626. if (StripUnusedFeatures_PunctualLightShadows(ref strippingData))
  627. return true;
  628. if (StripUnusedFeatures_FoveatedRendering(ref strippingData))
  629. return true;
  630. if (StripUnusedFeatures_DeferredRendering(ref strippingData))
  631. return true;
  632. if (StripUnusedFeatures_DataDrivenLensFlare(ref strippingData))
  633. return true;
  634. // Eventhough, it's a post process and a volume override, we put that here since it depend on a URP asset property.
  635. if (StripUnusedFeatures_ScreenSpaceLensFlare(ref strippingData))
  636. return true;
  637. ShaderStripTool<ShaderFeatures> stripTool = new ShaderStripTool<ShaderFeatures>(strippingData.shaderFeatures, ref strippingData);
  638. if (StripUnusedFeatures_MainLightShadows(ref strippingData, ref stripTool))
  639. return true;
  640. if (StripUnusedFeatures_AdditionalLightShadows(ref strippingData, ref stripTool))
  641. return true;
  642. if (StripUnusedFeatures_SoftShadows(ref stripTool))
  643. return true;
  644. if (StripUnusedFeatures_SoftShadowsQualityLevels(ref strippingData, ref stripTool))
  645. return true;
  646. if (StripUnusedFeatures_HDRGrading(ref stripTool))
  647. return true;
  648. if (StripUnusedFeatures_UseFastSRGBLinearConversion(ref stripTool))
  649. return true;
  650. if (StripUnusedFeatures_LightLayers(ref stripTool))
  651. return true;
  652. if (StripUnusedFeatures_RenderPassEnabled(ref stripTool))
  653. return true;
  654. if (StripUnusedFeatures_ReflectionProbes(ref stripTool))
  655. return true;
  656. if (StripUnusedFeatures_ForwardPlus(ref stripTool))
  657. return true;
  658. if (StripUnusedFeatures_AdditionalLights(ref strippingData, ref stripTool))
  659. return true;
  660. if (StripUnusedFeatures_SHAuto(ref strippingData, ref stripTool))
  661. return true;
  662. if (StripUnusedFeatures_ScreenSpaceOcclusion(ref strippingData, ref stripTool))
  663. return true;
  664. if (StripUnusedFeatures_DecalsDbuffer(ref strippingData, ref stripTool))
  665. return true;
  666. if (StripUnusedFeatures_DecalsNormalBlend(ref strippingData, ref stripTool))
  667. return true;
  668. if (StripUnusedFeatures_DecalLayers(ref strippingData, ref stripTool))
  669. return true;
  670. if (StripUnusedFeatures_WriteRenderingLayers(ref strippingData, ref stripTool))
  671. return true;
  672. if (StripUnusedFeatures_AccurateGbufferNormals(ref strippingData, ref stripTool))
  673. return true;
  674. if (stripTool.StripMultiCompileKeepOffVariant(m_LODFadeCrossFade, ShaderFeatures.LODCrossFade))
  675. return true;
  676. if (StripUnusedFeatures_LightCookies(ref strippingData, ref stripTool))
  677. return true;
  678. if (StripUnusedFeatures_ProbesVolumes(ref stripTool))
  679. return true;
  680. if (StripUnusedFeatures_XRMirrorView(ref strippingData))
  681. return true;
  682. if (StripUnusedFeatures_XROcclusionMesh(ref strippingData))
  683. return true;
  684. if (StripUnusedFeatures_XRMotionVector(ref strippingData))
  685. return true;
  686. return false;
  687. }
  688. /*********************************************************
  689. Unsupported Variants
  690. *********************************************************/
  691. internal bool StripUnsupportedVariants_DirectionalLightmap(ref IShaderScriptableStrippingData strippingData)
  692. {
  693. // We can strip variants that have directional lightmap enabled but not static nor dynamic lightmap.
  694. if (strippingData.IsKeywordEnabled(m_DirectionalLightmap)
  695. && !(strippingData.IsKeywordEnabled(m_Lightmap) || strippingData.IsKeywordEnabled(m_DynamicLightmap)))
  696. return true;
  697. return false;
  698. }
  699. internal bool StripUnsupportedVariants_EditorVisualization(ref IShaderScriptableStrippingData strippingData)
  700. {
  701. // Editor visualization is only used in scene view debug modes.
  702. if (strippingData.IsKeywordEnabled(m_EditorVisualization))
  703. return true;
  704. return false;
  705. }
  706. internal bool StripUnsupportedVariants(ref IShaderScriptableStrippingData strippingData)
  707. {
  708. // We can strip variants that have directional lightmap enabled but not static nor dynamic lightmap.
  709. if (StripUnsupportedVariants_DirectionalLightmap(ref strippingData))
  710. return true;
  711. if (StripUnsupportedVariants_EditorVisualization(ref strippingData))
  712. return true;
  713. return false;
  714. }
  715. /*********************************************************
  716. Invalid Variants
  717. *********************************************************/
  718. internal bool StripInvalidVariants_HDR(ref IShaderScriptableStrippingData strippingData)
  719. {
  720. // We do not need to strip out HDR output variants if HDR display is enabled.
  721. if (strippingData.IsHDRDisplaySupportEnabled)
  722. return false;
  723. // Shared keywords between URP and HDRP.
  724. if (!strippingData.IsHDRShaderVariantValid)
  725. return true;
  726. // HDR output shader variants specific to URP.
  727. if (strippingData.IsKeywordEnabled(m_EasuRcasAndHDRInput) || strippingData.IsKeywordEnabled(m_Gamma20AndHDRInput))
  728. return true;
  729. return false;
  730. }
  731. internal bool StripInvalidVariants_TerrainHoles(ref IShaderScriptableStrippingData strippingData)
  732. {
  733. if (strippingData.shader == m_TerrainLit)
  734. if (!strippingData.IsShaderFeatureEnabled(ShaderFeatures.TerrainHoles) && strippingData.IsKeywordEnabled(m_AlphaTestOn))
  735. return true;
  736. return false;
  737. }
  738. internal bool StripInvalidVariants_Shadows(ref IShaderScriptableStrippingData strippingData)
  739. {
  740. // Strip Additional Shadow variants if it's not set to PerPixel and not F+/Deferred
  741. bool areAdditionalShadowsEnabled = strippingData.IsKeywordEnabled(m_AdditionalLightShadows);
  742. bool hasShadowsOff = strippingData.IsShaderFeatureEnabled(ShaderFeatures.ShadowsKeepOffVariants);
  743. if (hasShadowsOff && areAdditionalShadowsEnabled)
  744. {
  745. bool isPerPixel = strippingData.IsKeywordEnabled(m_AdditionalLightsPixel);
  746. bool isForwardPlus = strippingData.IsKeywordEnabled(m_ForwardPlus);
  747. bool isDeferred = strippingData.IsShaderFeatureEnabled(ShaderFeatures.DeferredShading);
  748. if (!isPerPixel && !isForwardPlus && !isDeferred)
  749. return true;
  750. }
  751. // Strip Soft Shadows if shadows are disabled for both Main and Additional Lights...
  752. bool isMainShadowNoCascades = strippingData.IsKeywordEnabled(m_MainLightShadows);
  753. bool isMainShadowCascades = strippingData.IsKeywordEnabled(m_MainLightShadowsCascades);
  754. bool isMainShadowScreen = strippingData.IsKeywordEnabled(m_MainLightShadowsScreen);
  755. bool isMainShadow = isMainShadowNoCascades || isMainShadowCascades || isMainShadowScreen;
  756. bool isShadowVariant = isMainShadow || areAdditionalShadowsEnabled;
  757. if (!isShadowVariant && (strippingData.IsKeywordEnabled(m_SoftShadows) ||
  758. strippingData.IsKeywordEnabled(m_SoftShadowsLow) ||
  759. strippingData.IsKeywordEnabled(m_SoftShadowsMedium)
  760. || strippingData.IsKeywordEnabled(m_SoftShadowsHigh)))
  761. return true;
  762. return false;
  763. }
  764. internal bool StripInvalidVariants(ref IShaderScriptableStrippingData strippingData)
  765. {
  766. if (StripInvalidVariants_HDR(ref strippingData))
  767. return true;
  768. if (StripInvalidVariants_TerrainHoles(ref strippingData))
  769. return true;
  770. if (StripInvalidVariants_Shadows(ref strippingData))
  771. return true;
  772. return false;
  773. }
  774. /*********************************************************
  775. Unused Passes
  776. *********************************************************/
  777. internal bool StripUnusedPass_2D(ref IShaderScriptableStrippingData strippingData)
  778. {
  779. // Strip 2D Passes if there are no 2D renderers...
  780. if (strippingData.passName == kPassNameUniversal2D && strippingData.strip2DPasses)
  781. return true;
  782. return false;
  783. }
  784. internal bool StripUnusedPass_Meta(ref IShaderScriptableStrippingData strippingData)
  785. {
  786. // Meta pass is needed in the player for Enlighten Precomputed Realtime GI albedo and emission.
  787. if (strippingData.passType == PassType.Meta)
  788. {
  789. if (SupportedRenderingFeatures.active.enlighten == false
  790. || ((int)SupportedRenderingFeatures.active.lightmapBakeTypes | (int)LightmapBakeType.Realtime) == 0)
  791. return true;
  792. }
  793. return false;
  794. }
  795. internal bool StripUnusedPass_ShadowCaster(ref IShaderScriptableStrippingData strippingData)
  796. {
  797. if (strippingData.passType == PassType.ShadowCaster)
  798. {
  799. if ( !strippingData.IsShaderFeatureEnabled(ShaderFeatures.MainLightShadows)
  800. && !strippingData.IsShaderFeatureEnabled(ShaderFeatures.AdditionalLightShadows))
  801. return true;
  802. }
  803. return false;
  804. }
  805. internal bool StripUnusedPass_Decals(ref IShaderScriptableStrippingData strippingData)
  806. {
  807. // Do not strip GL passes as there are only screen space forward
  808. if (strippingData.isGLDevice)
  809. return false;
  810. // DBuffer
  811. if (strippingData.passName == DecalShaderPassNames.DBufferMesh
  812. || strippingData.passName == DecalShaderPassNames.DBufferProjector
  813. || strippingData.passName == DecalShaderPassNames.DecalMeshForwardEmissive
  814. || strippingData.passName == DecalShaderPassNames.DecalProjectorForwardEmissive)
  815. if (!strippingData.IsShaderFeatureEnabled(ShaderFeatures.DBufferMRT1) && !strippingData.IsShaderFeatureEnabled(ShaderFeatures.DBufferMRT2) && !strippingData.IsShaderFeatureEnabled(ShaderFeatures.DBufferMRT3))
  816. return true;
  817. // Decal Screen Space
  818. if (strippingData.passName == DecalShaderPassNames.DecalScreenSpaceMesh || strippingData.passName == DecalShaderPassNames.DecalScreenSpaceProjector)
  819. if (!strippingData.IsShaderFeatureEnabled(ShaderFeatures.DecalScreenSpace))
  820. return true;
  821. // Decal GBuffer
  822. if (strippingData.passName == DecalShaderPassNames.DecalGBufferMesh || strippingData.passName == DecalShaderPassNames.DecalGBufferProjector)
  823. if (!strippingData.IsShaderFeatureEnabled(ShaderFeatures.DecalGBuffer))
  824. return true;
  825. return false;
  826. }
  827. internal bool StripUnusedPass_XRMotionVectors(ref IShaderScriptableStrippingData strippingData)
  828. {
  829. // Strip XR MotionVector Passes if there is no XR
  830. if (strippingData.passName == kPassNameXRMotionVectors && strippingData.stripUnusedXRVariants)
  831. return true;
  832. return false;
  833. }
  834. internal bool StripUnusedPass(ref IShaderScriptableStrippingData strippingData)
  835. {
  836. if (StripUnusedPass_2D(ref strippingData))
  837. return true;
  838. if (StripUnusedPass_Meta(ref strippingData))
  839. return true;
  840. if (StripUnusedPass_ShadowCaster(ref strippingData))
  841. return true;
  842. if (StripUnusedPass_Decals(ref strippingData))
  843. return true;
  844. if (StripUnusedPass_XRMotionVectors(ref strippingData))
  845. return true;
  846. return false;
  847. }
  848. /*********************************************************
  849. Unused Shaders
  850. *********************************************************/
  851. internal bool StripUnusedShaders_Deferred(ref IShaderScriptableStrippingData strippingData)
  852. {
  853. if (!strippingData.stripUnusedVariants)
  854. return false;
  855. // Remove DeferredStencil if Deferred Rendering is not used
  856. if (strippingData.shader == m_StencilDeferred)
  857. if (!strippingData.IsShaderFeatureEnabled(ShaderFeatures.DeferredShading))
  858. return true;
  859. return false;
  860. }
  861. internal bool StripUnusedShaders_HDROutput(ref IShaderScriptableStrippingData strippingData)
  862. {
  863. if (!strippingData.stripUnusedVariants)
  864. return false;
  865. // Remove BlitHDROverlay if HDR output is not used
  866. if (strippingData.shader == m_HDROutputBlitShader)
  867. if (!PlayerSettings.allowHDRDisplaySupport)
  868. return true;
  869. return false;
  870. }
  871. internal bool StripUnusedShaders(ref IShaderScriptableStrippingData strippingData)
  872. {
  873. if (!strippingData.stripUnusedVariants)
  874. return false;
  875. // Remove DeferredStencil if Deferred Rendering is not used
  876. if (StripUnusedShaders_Deferred(ref strippingData))
  877. return true;
  878. // Remove BlitHDROverlay if HDR output is not used
  879. if (StripUnusedShaders_HDROutput(ref strippingData))
  880. return true;
  881. return false;
  882. }
  883. /*********************************************************
  884. Main Callbacks
  885. *********************************************************/
  886. public bool CanRemoveVariant([DisallowNull] Shader shader, ShaderSnippetData passData, ShaderCompilerData variantData)
  887. {
  888. IShaderScriptableStrippingData strippingData = new StrippingData()
  889. {
  890. volumeFeatures = ShaderBuildPreprocessor.volumeFeatures,
  891. stripSoftShadowQualityLevels = !ShaderBuildPreprocessor.s_UseSoftShadowQualityLevelKeywords,
  892. strip2DPasses = ShaderBuildPreprocessor.s_Strip2DPasses,
  893. stripDebugDisplayShaders = ShaderBuildPreprocessor.s_StripDebugDisplayShaders,
  894. stripScreenCoordOverrideVariants = ShaderBuildPreprocessor.s_StripScreenCoordOverrideVariants,
  895. stripUnusedVariants = ShaderBuildPreprocessor.s_StripUnusedVariants,
  896. stripUnusedPostProcessingVariants = ShaderBuildPreprocessor.s_StripUnusedPostProcessingVariants,
  897. stripUnusedXRVariants = ShaderBuildPreprocessor.s_StripXRVariants,
  898. IsHDRDisplaySupportEnabled = PlayerSettings.allowHDRDisplaySupport,
  899. shader = shader,
  900. passData = passData,
  901. variantData = variantData
  902. };
  903. // All feature sets need to have this variant unused to be stripped out.
  904. bool removeInput = true;
  905. for (var index = 0; index < ShaderBuildPreprocessor.supportedFeaturesList.Count; index++)
  906. {
  907. strippingData.shaderFeatures = ShaderBuildPreprocessor.supportedFeaturesList[index];
  908. if (StripUnusedShaders(ref strippingData))
  909. continue;
  910. if (StripUnusedPass(ref strippingData))
  911. continue;
  912. if (StripInvalidVariants(ref strippingData))
  913. continue;
  914. if (StripUnsupportedVariants(ref strippingData))
  915. continue;
  916. if (StripUnusedFeatures(ref strippingData))
  917. continue;
  918. removeInput = false;
  919. break;
  920. }
  921. // Check PostProcessing variants...
  922. if (!removeInput && strippingData.stripUnusedPostProcessingVariants)
  923. if (StripVolumeFeatures(ShaderBuildPreprocessor.volumeFeatures, ref strippingData))
  924. removeInput = true;
  925. return removeInput;
  926. }
  927. public void BeforeShaderStripping(Shader shader)
  928. {
  929. if (shader != null)
  930. InitializeLocalShaderKeywords(shader);
  931. }
  932. public void AfterShaderStripping(Shader shader)
  933. {
  934. }
  935. }
  936. }