public ShaderVariantsInfo(Shader sh, PassType pass, string[] words) { int wordsLength = 0; if (words != null) { wordsLength = words.Length; } this.shader = sh; this.passType = pass; this.keywords = words; this.keywordInfos = new List <ShaderKeyword>(wordsLength); for (int i = 0; i < wordsLength; ++i) { if (string.IsNullOrEmpty(words[i])) { continue; } ShaderKeyword shKeyword = new ShaderKeyword(sh, words[i]); keywordInfos.Add(shKeyword); } keywordsForCheck = new List <string>(); foreach (var keywordInfo in keywordInfos) { if (!string.IsNullOrEmpty(ShaderKeyword.GetKeywordName(sh, keywordInfo)) && ShaderKeyword.GetKeywordType(sh, keywordInfo) != ShaderKeywordType.BuiltinDefault) { keywordsForCheck.Add(ShaderKeyword.GetKeywordName(sh, keywordInfo)); } } keywordsForCheck.Sort(); }
//判断字符串是那种关键字 public static ShaderKeywordType GetKeywordType(string key) { try { ShaderKeyword shaderKeyword = new ShaderKeyword(key); return(shaderKeyword.GetKeywordType()); } catch { return(ShaderKeywordType.None); } }
private List <string> Convert(Shader shader, ShaderKeyword[] keywords) { List <string> converted = new List <string>(keywords.Length); for (int i = 0; i < keywords.Length; ++i) { if (!string.IsNullOrEmpty(ShaderKeyword.GetKeywordName(shader, keywords[i])) && ShaderKeyword.GetKeywordType(shader, keywords[i]) != ShaderKeywordType.BuiltinDefault) { converted.Add(ShaderKeyword.GetKeywordName(shader, keywords[i])); } } converted.Sort(); return(converted); }
private void AppendShaderInfo(StringBuilder sb, Shader shader, ShaderSnippetData snippet, ShaderCompilerData compilerData) { if (sb.Length == 0) { sb.Append("Shader:" + shader.name).Append("\n"); sb.Append("ShaderType:").Append(snippet.shaderType).Append("\n"). Append("PassName:").Append(snippet.passName).Append("\n"). Append("PassType:").Append(snippet.passType).Append("\n\n"); } var keywords = compilerData.shaderKeywordSet.GetShaderKeywords(); var sortKeywords = new ShaderKeyword[keywords.Length]; for (int i = 0; i < keywords.Length; ++i) { sortKeywords[i] = keywords[i]; } System.Array.Sort(sortKeywords, new SortShaderKeyword(shader)); sb.Append(" Keyword:"); foreach (var keyword in sortKeywords) { #if UNITY_2019_3_OR_NEWER sb.Append(ShaderKeyword.GetKeywordName(shader, keyword)).Append(" "); #else sb.Append(keyword.GetKeywordName()).Append(" "); #endif } sb.Append("\n KeywordType:"); foreach (var keyword in sortKeywords) { #if UNITY_2019_3_OR_NEWER sb.Append(ShaderKeyword.GetKeywordType(shader, keyword)).Append(" "); #else sb.Append(keyword.GetKeywordType()).Append(" "); #endif } #if UNITY_2019_3_OR_NEWER sb.Append("\n IsLocalkeyword:"); foreach (var keyword in sortKeywords) { sb.Append(ShaderKeyword.IsKeywordLocal(keyword)).Append(" "); } #endif sb.Append("\n").Append("\n"); }
public ShaderVariantsInfo(Shader sh, PassType pass, string[] words) { this.shader = sh; this.passType = pass; this.keywords = words; this.keywordInfos = new List <ShaderKeyword>(words.Length); for (int i = 0; i < words.Length; ++i) { if (string.IsNullOrEmpty(words[i]) || sh == null) { continue; } #if UNITY_2019 || UNITY_2019_3_OR_NEWER ShaderKeyword shKeyword = new ShaderKeyword(sh, words[i]); #else ShaderKeyword shKeyword = new ShaderKeyword(words[i]); #endif keywordInfos.Add(shKeyword); } keywordsForCheck = new List <string>(); foreach (var keywordInfo in keywordInfos) { #if UNITY_2019_3_OR_NEWER if (!string.IsNullOrEmpty(ShaderKeyword.GetKeywordName(sh, keywordInfo)) && ShaderKeyword.GetKeywordType(sh, keywordInfo) != ShaderKeywordType.BuiltinAutoStripped) { keywordsForCheck.Add(ShaderKeyword.GetKeywordName(sh, keywordInfo)); } #else if (!string.IsNullOrEmpty(keywordInfo.GetKeywordName()) && keywordInfo.GetKeywordType() != ShaderKeywordType.BuiltinAutoStripped) { keywordsForCheck.Add(keywordInfo.GetKeywordName()); } #endif } keywordsForCheck.Sort(); }
private List <string> Convert(Shader shader, ShaderKeyword[] keywords) { List <string> converted = new List <string>(keywords.Length); for (int i = 0; i < keywords.Length; ++i) { #if UNITY_2019_3_OR_NEWER if (!string.IsNullOrEmpty(ShaderKeyword.GetKeywordName(shader, keywords[i])) && ShaderKeyword.GetKeywordType(shader, keywords[i]) != ShaderKeywordType.BuiltinAutoStripped) { converted.Add(ShaderKeyword.GetKeywordName(shader, keywords[i])); } #else if (!string.IsNullOrEmpty(keywords[i].GetKeywordName()) && keywords[i].GetKeywordType() != ShaderKeywordType.BuiltinAutoStripped) { converted.Add(keywords[i].GetKeywordName()); } #endif } converted.Sort(); return(converted); }
/// <summary> /// Strips shader variants from the underwater shader based on what features are enabled on the ocean material. /// </summary> public void ProcessUnderwaterShader(Shader shader, IList <ShaderCompilerData> data) { // This should not happen. There should always be at least one variant. if (data.Count == 0) { return; } #if CREST_DEBUG var shaderVariantCount = data.Count; var shaderVarientStrippedCount = 0; #endif // Collect all shader keywords. var unusedShaderKeywords = new HashSet <ShaderKeyword>(); for (int i = 0; i < data.Count; i++) { // Each ShaderCompilerData is a variant which is a combination of keywords. Since each list will be // different, simply getting a list of all keywords is not possible. This also appears to be the only // way to get a list of keywords without trying to extract them from shader property names. Lastly, // shader_feature will be returned only if they are enabled. unusedShaderKeywords.UnionWith(data[i].shaderKeywordSet.GetShaderKeywords()); } // Get used shader keywords so we can exclude them. var usedShaderKeywords = new List <ShaderKeyword>(); foreach (var shaderKeyword in unusedShaderKeywords) { // Do not handle built-in shader keywords. if (ShaderKeyword.GetKeywordType(shader, shaderKeyword) != ShaderKeywordType.UserDefined) { usedShaderKeywords.Add(shaderKeyword); continue; } // GetKeywordName will work for both global and local keywords. var shaderKeywordName = ShaderKeyword.GetKeywordName(shader, shaderKeyword); // These keywords will not be on ocean material. if (shaderKeywordName.Contains("_MENISCUS") || shaderKeywordName.Contains("_FULL_SCREEN_EFFECT")) { usedShaderKeywords.Add(shaderKeyword); continue; } // TODO: Strip this once post-processing is more unified. if (shaderKeywordName.Contains("_DEBUG_VIEW_OCEAN_MASK")) { usedShaderKeywords.Add(shaderKeyword); continue; } foreach (var oceanMaterial in _oceanMaterials) { if (oceanMaterial.IsKeywordEnabled(shaderKeywordName)) { usedShaderKeywords.Add(shaderKeyword); break; } } } // Exclude used keywords to obtain list of unused keywords. unusedShaderKeywords.ExceptWith(usedShaderKeywords); for (int index = 0; index < data.Count; index++) { foreach (var unusedShaderKeyword in unusedShaderKeywords) { // IsEnabled means this variant uses this keyword and we can strip it. if (data[index].shaderKeywordSet.IsEnabled(unusedShaderKeyword)) { data.RemoveAt(index--); #if CREST_DEBUG shaderVarientStrippedCount++; #endif break; } } } #if CREST_DEBUG this.shaderVarientStrippedCount += shaderVarientStrippedCount; Debug.Log($"Crest: {shaderVarientStrippedCount} shader variants stripped of {shaderVariantCount} from {shader.name}."); #endif }
public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList <ShaderCompilerData> data) { int newVariantsForThisShader = 0; //The real variant count newVariantsForThisShader += data.Count; //Go through all the variants for (int i = 0; i < data.Count; ++i) { ShaderKeyword[] sk = data[i].shaderKeywordSet.GetShaderKeywords(); //The default variant if (sk.Length == 0) { CompiledShaderVariant scv_default = new CompiledShaderVariant(); //scv.id = id; scv_default.shaderName = shader.name; scv_default.passName = "" + snippet.passName; scv_default.passType = "" + snippet.passType.ToString(); scv_default.shaderType = "" + snippet.shaderType.ToString(); scv_default.graphicsTier = "--"; scv_default.shaderCompilerPlatform = "--"; scv_default.shaderKeywordName = "No Keyword / All Off"; scv_default.shaderKeywordType = "--"; scv_default.shaderKeywordIndex = "-1"; scv_default.isShaderKeywordValid = "--"; scv_default.isShaderKeywordEnabled = "--"; SVL.variantlist.Add(scv_default); SVL.compiledTotalCount++; } for (int k = 0; k < sk.Length; ++k) { CompiledShaderVariant scv = new CompiledShaderVariant(); //scv.id = id; scv.shaderName = shader.name; scv.passName = "" + snippet.passName; scv.passType = "" + snippet.passType.ToString(); scv.shaderType = "" + snippet.shaderType.ToString(); scv.graphicsTier = "" + data[i].graphicsTier; scv.shaderCompilerPlatform = "" + data[i].shaderCompilerPlatform; //scv.shaderRequirements = ""+data[i].shaderRequirements; //scv.platformKeywordName = ""+data[i].platformKeywordSet.ToString(); //scv.isplatformKeywordEnabled = ""+data[i].platformKeywordSet.IsEnabled(BuiltinShaderDefine.SHADER_API_DESKTOP); bool isLocal = ShaderKeyword.IsKeywordLocal(sk[k]); scv.shaderKeywordName = (isLocal? "[Local] " : "[Global] ") + ShaderKeyword.GetKeywordName(shader, sk[k]); //sk[k].GetKeywordName(); scv.shaderKeywordType = "" + ShaderKeyword.GetKeywordType(shader, sk[k]); //""+sk[k].GetKeywordType().ToString(); scv.shaderKeywordIndex = "" + sk[k].index; scv.isShaderKeywordValid = "" + sk[k].IsValid(); scv.isShaderKeywordEnabled = "" + data[i].shaderKeywordSet.IsEnabled(sk[k]); SVL.variantlist.Add(scv); SVL.compiledTotalCount++; //Just to verify API is correct string globalShaderKeywordName = ShaderKeyword.GetGlobalKeywordName(sk[k]); if (!isLocal && globalShaderKeywordName != ShaderKeyword.GetKeywordName(shader, sk[k])) { Debug.LogError("Bug. ShaderKeyword.GetGlobalKeywordName() and ShaderKeyword.GetKeywordName() is wrong"); } ShaderKeywordType globalShaderKeywordType = ShaderKeyword.GetGlobalKeywordType(sk[k]); if (!isLocal && globalShaderKeywordType != ShaderKeyword.GetKeywordType(shader, sk[k])) { Debug.LogError("Bug. ShaderKeyword.GetGlobalKeywordType() and ShaderKeyword.GetKeywordType() is wrong"); } } } //Add to shader list int compiledShaderId = SVL.shaderlist.FindIndex(o => o.name == shader.name); if (compiledShaderId == -1) { CompiledShader newCompiledShader = new CompiledShader(); newCompiledShader.name = shader.name; newCompiledShader.guiEnabled = false; newCompiledShader.noOfVariantsForThisShader = 0; SVL.shaderlist.Add(newCompiledShader); compiledShaderId = SVL.shaderlist.Count - 1; } //Add variant count to shader CompiledShader compiledShader = SVL.shaderlist[compiledShaderId]; compiledShader.noOfVariantsForThisShader += newVariantsForThisShader; SVL.shaderlist[compiledShaderId] = compiledShader; //Add to total count SVL.variantTotalCount += newVariantsForThisShader; }
public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList <ShaderCompilerData> shaderVariants) { if (!ShaderVariantsStripperConfigure.Configure.useStripper) { return; } int inputShaderVariantCount = shaderVariants.Count; string prefix = "VARIANT: " + shader.name + " ("; if (snippet.passName.Length > 0) { prefix += snippet.passName + ", "; } prefix += snippet.shaderType.ToString() + ") "; if (ShaderVariantsStripperConfigure.Configure.enableLog) { if (shaderSets.Add(shader.name)) { File.AppendAllText(allshaderFile, shader.name + "\n"); } } for (int i = 0; i < shaderVariants.Count; ++i) { string log = prefix; log += shaderVariants[i].shaderCompilerPlatform.ToString() + " "; log += shaderVariants[i].graphicsTier.ToString() + " "; if (ShaderVariantsStripperConfigure.Configure.enableLog) { ShaderKeyword[] keywords = shaderVariants[i].shaderKeywordSet.GetShaderKeywords(); for (int labelIndex = 0; labelIndex < keywords.Count(); ++labelIndex) { ShaderKeyword akey = keywords[labelIndex]; bool isUserDefined = akey.GetKeywordType() == ShaderKeywordType.UserDefined; bool isBuiltinDefault = akey.GetKeywordType() == ShaderKeywordType.BuiltinDefault; bool isBuiltinExtra = akey.GetKeywordType() == ShaderKeywordType.BuiltinExtra; bool isBuiltinAutoStripped = akey.GetKeywordType() == ShaderKeywordType.BuiltinAutoStripped; #if UNITY_2018_3_OR_NEWER string keyWordName = akey.GetKeywordName(); #else string keyWordName = akey.GetName(); #endif log += keyWordName + " "; { if (keySets.Add(keyWordName)) { File.AppendAllText(keywordFile, keyWordName + "\n"); } } if (isBuiltinDefault) { if (!buildInKeys.Contains(keyWordName)) { buildInKeys.Add(keyWordName); File.AppendAllText(buildInFile, keyWordName + "\n"); } } if (isBuiltinExtra) { if (!buildInExtraKeys.Contains(keyWordName)) { buildInExtraKeys.Add(keyWordName); File.AppendAllText(buildExtraFile, keyWordName + "\n"); } } if (isBuiltinAutoStripped) { if (!buildInAutoStripKeys.Contains(keyWordName)) { buildInAutoStripKeys.Add(keyWordName); File.AppendAllText(buildInAutoStripFile, keyWordName + "\n"); } } if (isUserDefined) { if (!userDefineKeys.Contains(keyWordName)) { userDefineKeys.Add(keyWordName); File.AppendAllText(userDefineFile, keyWordName + "\n"); } } } } bool keepVariant = true; if (ShaderVariantsStripperConfigure.Configure.useWhitelist) { keepVariant = KeepVariantByWhitelist(shader, snippet, shaderVariants[i]); } else { keepVariant = KeepVariantByConfigure(shader, snippet, shaderVariants[i]); } if (ShaderVariantsStripperConfigure.Configure.enableLog) { //填写编译结果 ShaderCompileReport.Report.AddCompileInfo(shader.name, snippet, shaderVariants[i], !keepVariant); } if (!keepVariant) { shaderVariants.RemoveAt(i); --i; } if (ShaderVariantsStripperConfigure.Configure.enableLog) { File.AppendAllText(logFile, log + "\n"); } } if (ShaderVariantsStripperConfigure.Configure.enableLog) { float percentage = (float)shaderVariants.Count / (float)inputShaderVariantCount * 100f; string logresult = "STRIPPING(" + shader.name + " " + snippet.passName + " " + snippet.shaderType.ToString() + ") = Kept / Total = " + shaderVariants.Count + " / " + inputShaderVariantCount + " = " + percentage + "% of the generated shader variants remain in the player data"; //Debug.Log(BText(logresult)); File.AppendAllText(logFile, logresult + "\n"); } }