暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ShaderStripTool.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4. using System.Runtime.CompilerServices;
  5. using IShaderScriptableStrippingData = UnityEditor.Rendering.Universal.ShaderScriptableStripper.IShaderScriptableStrippingData;
  6. namespace UnityEditor.Rendering.Universal
  7. {
  8. /// <summary>
  9. /// Struct used to determine whether keyword variants can be stripped from builds
  10. /// </summary>
  11. /// <typeparam name="T">The Shader Features used for verifying against the keywords</typeparam>
  12. internal struct ShaderStripTool<T> where T : Enum
  13. {
  14. T m_Features;
  15. private IShaderScriptableStrippingData m_StrippingData;
  16. public ShaderStripTool(T features, ref IShaderScriptableStrippingData strippingData)
  17. {
  18. m_Features = features;
  19. m_StrippingData = strippingData;
  20. }
  21. public bool StripMultiCompileKeepOffVariant(in LocalKeyword kw, T feature, in LocalKeyword kw2, T feature2, in LocalKeyword kw3, T feature3)
  22. {
  23. if (StripMultiCompileKeepOffVariant(kw, feature))
  24. return true;
  25. if (StripMultiCompileKeepOffVariant(kw2, feature2))
  26. return true;
  27. if (StripMultiCompileKeepOffVariant(kw3, feature3))
  28. return true;
  29. return false;
  30. }
  31. public bool StripMultiCompile(in LocalKeyword kw, T feature, in LocalKeyword kw2, T feature2, in LocalKeyword kw3, T feature3)
  32. {
  33. if (StripMultiCompileKeepOffVariant(kw, feature, kw2, feature2, kw3, feature3))
  34. return true;
  35. // To strip out the OFF variant, it needs to check if
  36. // * Strip unused variants has been enabled
  37. // * ALL THREE keywords are present in that pass
  38. // * ALL THREE keywords are disabled in the keyword set
  39. // * One one of the keywords is enabled in the feature set gathered in ShaderBuildPreprocessor
  40. if (m_StrippingData.stripUnusedVariants)
  41. {
  42. bool containsKeywords = ContainsKeyword(kw) && ContainsKeyword(kw2) && ContainsKeyword(kw3);
  43. bool keywordsDisabled = !m_StrippingData.IsKeywordEnabled(kw) && !m_StrippingData.IsKeywordEnabled(kw2) && !m_StrippingData.IsKeywordEnabled(kw3);
  44. bool hasAnyFeatureEnabled = m_Features.HasFlag(feature) || m_Features.HasFlag(feature2) || m_Features.HasFlag(feature3);
  45. if (containsKeywords && keywordsDisabled && hasAnyFeatureEnabled)
  46. return true;
  47. }
  48. return false;
  49. }
  50. public bool StripMultiCompileKeepOffVariant(in LocalKeyword kw, T feature, in LocalKeyword kw2, T feature2)
  51. {
  52. if (StripMultiCompileKeepOffVariant(kw, feature))
  53. return true;
  54. if (StripMultiCompileKeepOffVariant(kw2, feature2))
  55. return true;
  56. return false;
  57. }
  58. public bool StripMultiCompile(in LocalKeyword kw, T feature, in LocalKeyword kw2, T feature2)
  59. {
  60. if (StripMultiCompileKeepOffVariant(kw, feature, kw2, feature2))
  61. return true;
  62. // To strip out the OFF variant, it needs to check if
  63. // * Strip unused variants has been enabled
  64. // * BOTH keywords are present in that pass
  65. // * BOTH keywords are disabled in the keyword set
  66. // * One one of the keywords is enabled in the feature set gathered in ShaderBuildPreprocessor
  67. if (m_StrippingData.stripUnusedVariants)
  68. {
  69. bool containsKeywords = ContainsKeyword(kw) && ContainsKeyword(kw2);
  70. bool keywordsDisabled = !m_StrippingData.IsKeywordEnabled(kw) && !m_StrippingData.IsKeywordEnabled(kw2);
  71. bool hasAnyFeatureEnabled = m_Features.HasFlag(feature) || m_Features.HasFlag(feature2);
  72. if (containsKeywords && keywordsDisabled && hasAnyFeatureEnabled)
  73. return true;
  74. }
  75. return false;
  76. }
  77. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  78. public bool StripMultiCompileKeepOffVariant(in LocalKeyword kw, T feature)
  79. {
  80. return !m_Features.HasFlag(feature) && m_StrippingData.IsKeywordEnabled(kw);
  81. }
  82. public bool StripMultiCompile(in LocalKeyword kw, T feature)
  83. {
  84. // Same as Strip and Keep OFF variant
  85. if (!m_Features.HasFlag(feature))
  86. {
  87. if (m_StrippingData.IsKeywordEnabled(kw))
  88. return true;
  89. }
  90. // To strip out the OFF variant, it needs to check if
  91. // * Strip unused variants has been enabled
  92. // * The keyword is present in that pass
  93. // * The keyword is disabled in the keyword set
  94. // * The keyword is enabled in the feature set gathered in ShaderBuildPreprocessor (Checked in the HasFlag check above)
  95. else if (m_StrippingData.stripUnusedVariants)
  96. {
  97. if (!m_StrippingData.IsKeywordEnabled(kw) && ContainsKeyword(kw))
  98. return true;
  99. }
  100. return false;
  101. }
  102. internal bool ContainsKeyword(in LocalKeyword kw)
  103. {
  104. return m_StrippingData.PassHasKeyword(kw);
  105. }
  106. }
  107. }