From 36c3c1da49b8c2423352bc93285455306d070a79 Mon Sep 17 00:00:00 2001 From: MingWai Chan Date: Wed, 4 May 2022 16:06:57 +0200 Subject: [PATCH 1/3] fix for 2021.2.19f1 --- .../ShaderVariantTool_BuildProcess.cs | 71 +++++++++++++------ .../ShaderVariantTool_ComputeProcess.cs | 2 +- .../ShaderVariantTool_Helper.cs | 13 +++- .../ShaderVariantTool_ShaderProcess.cs | 2 +- 4 files changed, 65 insertions(+), 23 deletions(-) diff --git a/Assets/Editor/ShaderVariantTool/ShaderVariantTool_BuildProcess.cs b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_BuildProcess.cs index 5687272..ca169a9 100644 --- a/Assets/Editor/ShaderVariantTool/ShaderVariantTool_BuildProcess.cs +++ b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_BuildProcess.cs @@ -53,7 +53,7 @@ public void OnPostprocessBuild(BuildReport report) //Because of the new incremental build changes, Development build won't trigger OnShaderProcess - 1338940 if(SVL.shaderlist.Count == 0) { - Debug.LogError("No shader or variant are logged. Please turn ON Delete PlayerCache Before Build checkbox in Window > ShaderVariantTool."); + Debug.LogError("No shader or variant are logged. Please go to Window > ShaderVariantTool > turn ON Delete PlayerCache Before Build checkbox."); } //Calculate shader count by type @@ -161,6 +161,9 @@ public void OnPostprocessBuild(BuildReport report) //CleanUp outputRows.Clear(); + //Let user know the tool has successfully done it's job + Debug.Log("Build is done and ShaderVariantTool has done gathering data. Find the details on Windows > ShaderVariantTool, or look at the generated CSV reort at: "+ShaderVariantTool.savedFile); + SVL.buildProcessStarted = false; } @@ -219,45 +222,72 @@ private void ReadShaderCompileInfo() string programName = Helper.ExtractString(currentLine, passName+"\" (",""); programName = programName.Replace("\"","").Replace(")",""); - //total variant + //skip the line bla bla bla prepared currentLine = sr.ReadLine(); - string totalVariant = Helper.ExtractString(currentLine, "" , " variants," ); - totalVariant = totalVariant.Replace(" ",""); - int totalVariantInt = int.Parse(totalVariant); + if (currentLine.Contains("prepared") && !currentLine.Contains("variants, prepared")) + { + currentLine = sr.ReadLine(); + } //remaining variant & stripping time - currentLine = sr.ReadLine(); - string strippingTime = Helper.ExtractString(currentLine, "finished in " , " seconds. " ); - string remainingVariant = Helper.ExtractString(currentLine, " seconds. " , " variants left" ); + string remainingVariant = Helper.ExtractString(currentLine, "" , " / " ); int remainingVariantInt = int.Parse(remainingVariant); + //total variant + string totalVariant = Helper.ExtractString(currentLine, "/ " , " variants" ); + totalVariant = totalVariant.Replace(" ",""); + int totalVariantInt = int.Parse(totalVariant); + + //stripping time + string strippingTime = Helper.ExtractString(currentLine, "variants left after stripping, processed in " , " seconds" ); + //jump to line of compilation time if(remainingVariantInt > 0) { currentLine = sr.ReadLine(); - while (!currentLine.Contains("finished in ")) + while (!currentLine.Contains("finished in ") || currentLine.Contains("variants ready")) { currentLine = sr.ReadLine(); } } //compilation time and compiled variant count (time is faster if there are cached variants) + string remainingText = currentLine; + string startString = ""; + string endString = ""; string compileTime = "0.00"; string compiledVariants = "0"; string localCache = "0"; string remoteCache = "0"; if(remainingVariantInt > 0) { - compileTime = Helper.ExtractString(currentLine, "finished in " , " seconds. " ); - compiledVariants = Helper.ExtractString(currentLine, ", compiled " , " variants" ); - localCache = Helper.ExtractString(currentLine, "Local cache hits " , ", remote" ); - remoteCache = Helper.ExtractString(currentLine, "remote cache hits " , ", compiled" ); - - //Changes for the latest PR - localCache = Helper.ExtractString(localCache, "" , "(" ); + //Compile time + startString = "finished in "; + endString = " seconds. "; + compileTime = Helper.ExtractString(remainingText,startString,endString,false); + compileTime = compileTime.Replace(" ",""); + remainingText = Helper.GetRemainingString(remainingText,endString); + + //Local cache hit + startString = "Local cache hits "; + endString = " ("; + localCache = Helper.ExtractString(remainingText,startString,endString,false); localCache = localCache.Replace(" ",""); - remoteCache = Helper.ExtractString(remoteCache, "" , "(" ); + remainingText = Helper.GetRemainingString(remainingText,endString); + + //Remote cache hit + startString = "remote cache hits "; + endString = " ("; + remoteCache = Helper.ExtractString(remainingText,startString,endString,false); remoteCache = remoteCache.Replace(" ",""); + remainingText = Helper.GetRemainingString(remainingText,endString); + + //Compiled variants + startString = "), compiled "; + endString = " variants ("; + compiledVariants = Helper.ExtractString(remainingText,startString,endString,false); + compiledVariants = compiledVariants.Replace(" ",""); + remainingText = Helper.GetRemainingString(remainingText,endString); } int compiledVariantsInt = int.Parse(compiledVariants); int localCacheInt = int.Parse(localCache); @@ -300,12 +330,13 @@ private void ReadShaderCompileInfo() "Tool counted there are "+SVL.variantFromShader+" shader variants in build, "+ "but Editor Log counted "+variantCountinBuild+". Please contact @mingwai on slack."); } - if( SVL.variantInCache + SVL.variantCompiledCount != variantCountinBuild) + int variantCacheAndCompiledSum = SVL.variantInCache + SVL.variantCompiledCount; + if( variantCacheAndCompiledSum != variantCountinBuild) { Debug.LogError("ShaderVariantTool error. "+ "The sum of "+SVL.variantInCache+" variants in cache + "+ - SVL.variantCompiledCount+" variants compiled is not equal to the accumulated sum "+ - variantCountinBuild+" variants. Please contact @mingwai on slack."); + SVL.variantCompiledCount+" variants compiled "+variantCacheAndCompiledSum+" variants is not equal to the accumulated sum "+ + variantCountinBuild+" variants. Please contact @mingwai on slack. This could be related to exisiting known issue: Case 1389276"); } } } diff --git a/Assets/Editor/ShaderVariantTool/ShaderVariantTool_ComputeProcess.cs b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_ComputeProcess.cs index bbc569e..88d26e4 100644 --- a/Assets/Editor/ShaderVariantTool/ShaderVariantTool_ComputeProcess.cs +++ b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_ComputeProcess.cs @@ -67,7 +67,7 @@ public void OnProcessComputeShader(ComputeShader shader, string kernelName, ILis bool isLocal = ShaderKeyword.IsKeywordLocal(sk[k]); LocalKeyword lkey = new LocalKeyword(shader,sk[k].name); - scv.shaderKeywordName = ( isLocal? "[Local] " : "[Global] " ) + sk[k].name; //sk[k].GetKeywordName(); + scv.shaderKeywordName = ( isLocal? "[Local]" : "[Global]" ) + sk[k].name; //sk[k].GetKeywordName(); scv.shaderKeywordType = isLocal? "--" : ShaderKeyword.GetGlobalKeywordType(sk[k]).ToString(); //""+sk[k].GetKeywordType().ToString(); scv.shaderKeywordIndex = sk[k].index.ToString(); scv.isShaderKeywordValid = sk[k].IsValid().ToString(); diff --git a/Assets/Editor/ShaderVariantTool/ShaderVariantTool_Helper.cs b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_Helper.cs index 9163396..4f94b10 100644 --- a/Assets/Editor/ShaderVariantTool/ShaderVariantTool_Helper.cs +++ b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_Helper.cs @@ -29,7 +29,13 @@ public static string TimeFormatString (double timeInSeconds) return timeString; } - public static string ExtractString(string line, string from, string to) + public static string GetRemainingString(string line, string from) + { + int index = line.IndexOf(from) + from.Length; + return line.Substring( index , line.Length - index); + } + + public static string ExtractString(string line, string from, string to, bool takeLastIndexOfTo = true) { int pFrom = 0; if(from != "") @@ -42,6 +48,11 @@ public static string ExtractString(string line, string from, string to) if(to != "") { int index = line.LastIndexOf(to); + if(!takeLastIndexOfTo) + { + index = line.IndexOf(to); + } + if(index >= 0) pTo = index; } diff --git a/Assets/Editor/ShaderVariantTool/ShaderVariantTool_ShaderProcess.cs b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_ShaderProcess.cs index e52a503..d7e2e19 100644 --- a/Assets/Editor/ShaderVariantTool/ShaderVariantTool_ShaderProcess.cs +++ b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_ShaderProcess.cs @@ -68,7 +68,7 @@ public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList Date: Tue, 10 May 2022 16:18:27 +0200 Subject: [PATCH 2/3] update for 2021.3.1f1. Add buildTarget and platform keywords (SHADER_API ones) and remove columns of keyword index, valid & enabled because they are not that useful. But instead, will print an error/warning if a keyword is not valid / not enabled. Also fixed on Mac, added Log reader for previous build info. --- .../ShaderVariantTool/ShaderVariantTool.cs | 48 +++-- .../ShaderVariantTool_BuildProcess.cs | 178 +++++++++++------- .../ShaderVariantTool_ComputeProcess.cs | 24 ++- .../ShaderVariantTool_Helper.cs | 30 +++ .../ShaderVariantTool_LogReader.cs | 136 +++++++++++++ .../ShaderVariantTool_ShaderProcess.cs | 25 ++- 6 files changed, 339 insertions(+), 102 deletions(-) create mode 100644 Assets/Editor/ShaderVariantTool/ShaderVariantTool_LogReader.cs diff --git a/Assets/Editor/ShaderVariantTool/ShaderVariantTool.cs b/Assets/Editor/ShaderVariantTool/ShaderVariantTool.cs index ec818fc..89bb6f1 100644 --- a/Assets/Editor/ShaderVariantTool/ShaderVariantTool.cs +++ b/Assets/Editor/ShaderVariantTool/ShaderVariantTool.cs @@ -21,18 +21,21 @@ public class ShaderVariantTool : EditorWindow 0.9f, 1.8f, - 1.2f, + 1f, - 1.2f, + 1.5f, 0.6f, + 0.8f, + 0.5f, + 0.8f, 2.1f, - 1f, - 0.5f, - 0.5f, 0.5f, + //0.5f, + //0.5f, + //0.5f, 0.4f, }; @@ -210,6 +213,10 @@ public static class SVL //compute shader variant public static int variantFromCompute = 0; public static int computeShaderCount = 0; + + //invalid or disabled keywords for final error logging + public static string invalidKey = ""; + public static string disabledKey = ""; //data public static List variantlist = new List(); @@ -223,12 +230,15 @@ public static class SVL "ShaderType", "KernelName", "GfxTier", - "Platform", + "Build Target", + "Compiler Platform", + //"Require", + "Platform Keywords", "Keyword Name", "Keyword Type", - "Keyword Index", - "Keyword Valid", - "Keyword Enabled", + //"Keyword Index", + //"Keyword Valid", + //"Keyword Enabled", "Compiled Count" }; @@ -261,7 +271,7 @@ public static void ResetBuildList() public static void Sorting() { //sort the list according to shader name - variantlist = variantlist.OrderBy(o=>o.shaderName).ThenBy(o=>o.shaderType).ThenBy(o=>o.shaderKeywordIndex).ToList(); + variantlist = variantlist.OrderBy(o=>o.shaderName).ThenBy(o=>o.shaderType).ThenBy(o=>o.shaderKeywordName).ToList(); //Unique item and duplicate counts Dictionary uniqueSet = new Dictionary(); @@ -298,12 +308,15 @@ public static void Sorting() variantlist[k].shaderType, variantlist[k].kernelName, variantlist[k].graphicsTier, + variantlist[k].buildTarget, variantlist[k].shaderCompilerPlatform, + //variantlist[k].shaderRequirements, + variantlist[k].platformKeywords, variantlist[k].shaderKeywordName, variantlist[k].shaderKeywordType, - variantlist[k].shaderKeywordIndex, - variantlist[k].isShaderKeywordValid, - variantlist[k].isShaderKeywordEnabled, + //variantlist[k].shaderKeywordIndex, + //variantlist[k].isShaderKeywordValid, + //variantlist[k].isShaderKeywordEnabled, uniqueSet[variantlist[k]].ToString()}); } @@ -338,19 +351,20 @@ public struct CompiledShaderVariant //data public string graphicsTier; + public string buildTarget; public string shaderCompilerPlatform; //public string shaderRequirements; //data - PlatformKeywordSet - //public string platformKeywordName; + public string platformKeywords; //public string isplatformKeywordEnabled; //from PlatformKeywordSet //data - ShaderKeywordSet public string shaderKeywordName; //ShaderKeyword.GetKeywordName public string shaderKeywordType; //ShaderKeyword.GetKeywordType - public string shaderKeywordIndex; //ShaderKeyword.index - public string isShaderKeywordValid; //from ShaderKeyword.IsValid() - public string isShaderKeywordEnabled; //from ShaderKeywordSet + //public string shaderKeywordIndex; //ShaderKeyword.index + //public string isShaderKeywordValid; //from ShaderKeyword.IsValid() + //public string isShaderKeywordEnabled; //from ShaderKeywordSet }; } diff --git a/Assets/Editor/ShaderVariantTool/ShaderVariantTool_BuildProcess.cs b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_BuildProcess.cs index ca169a9..a31ec67 100644 --- a/Assets/Editor/ShaderVariantTool/ShaderVariantTool_BuildProcess.cs +++ b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_BuildProcess.cs @@ -61,7 +61,8 @@ public void OnPostprocessBuild(BuildReport report) SVL.variantFromShader = SVL.variantTotalCount - SVL.variantFromCompute; //Reading EditorLog - ReadShaderCompileInfo(); + string editorLogPath = Helper.GetEditorLogPath(); + ReadShaderCompileInfo(SVL.buildProcessIDTitleStart+SVL.buildProcessID, editorLogPath, false); //Calculate build time SVL.buildTime = EditorApplication.timeSinceStartup - SVL.buildTime; @@ -109,10 +110,33 @@ public void OnPostprocessBuild(BuildReport report) //outputRows.Add( new string[] { "ComputeShaders ----------------------------------" } ); outputRows.Add( new string[] { "ComputeShader Count" , SVL.computeShaderCount.ToString() } ); outputRows.Add( new string[] { "ComputeShader Variant Count in Build" , SVL.variantFromCompute.ToString() } ); - - //outputRows.Add( new string[] { "Total Data Count" , ""+SVL.compiledTotalCount } ); outputRows.Add( new string[] { "" } ); + //Write Shader Result from Editor Log + outputRows = AddEditorLogShaderInfo(outputRows, SVL.shaderlist); + outputRows.Add( new string[] { "" } ); + + //Write Each variant Result + for(int i = 0; i < SVL.rowData.Count; i++) + { + outputRows.Add( SVL.rowData[i] ); + } + + //Save File + string fileName = "ShaderVariants_"+DateTime.Now.ToString("yyyyMMdd_hh-mm-ss"); + ShaderVariantTool.savedFile = SaveCSVFile(outputRows, fileName); + + //CleanUp + outputRows.Clear(); + + //Let user know the tool has successfully done it's job + Debug.Log("Build is done and ShaderVariantTool has done gathering data. Find the details on Windows > ShaderVariantTool, or look at the generated CSV report at: "+ShaderVariantTool.savedFile); + + SVL.buildProcessStarted = false; + } + + public static List AddEditorLogShaderInfo(List outputRows, List shaderlist) + { //Write Shader Result outputRows.Add( new string[] { @@ -123,27 +147,25 @@ public void OnPostprocessBuild(BuildReport report) "Variant Count in Build", "Stripping+Compilation Time" } ); - for(int i = 0; i < SVL.shaderlist.Count; i++) + for(int i = 0; i outputRows, string fileName) + { //Prepare CSV string int length = outputRows.Count; string delimiter = ","; @@ -153,42 +175,30 @@ public void OnPostprocessBuild(BuildReport report) //Write to CSV file ShaderVariantTool.folderPath = Application.dataPath.Replace("/Assets","/"); - ShaderVariantTool.savedFile = ShaderVariantTool.folderPath+"ShaderVariants_"+DateTime.Now.ToString("yyyyMMdd_hh-mm-ss")+".csv"; - StreamWriter outStream = System.IO.File.CreateText(ShaderVariantTool.savedFile); + string savedFile = ShaderVariantTool.folderPath+fileName+".csv"; + StreamWriter outStream = System.IO.File.CreateText(savedFile); outStream.WriteLine(sb); outStream.Close(); //CleanUp outputRows.Clear(); - //Let user know the tool has successfully done it's job - Debug.Log("Build is done and ShaderVariantTool has done gathering data. Find the details on Windows > ShaderVariantTool, or look at the generated CSV reort at: "+ShaderVariantTool.savedFile); - - SVL.buildProcessStarted = false; + return savedFile; } - //[MenuItem("ShaderVariantTool/Debug/TestEditorLog")] - private void ReadShaderCompileInfo() + public static List ReadShaderCompileInfo(string startTimeStamp, string editorLogPath, bool isLogReader) { //For making sure there is no bug int variantCountinBuild = 0; - //Decide EditorLog path - string editorLogPath = ""; - switch(Application.platform) - { - case RuntimePlatform.WindowsEditor: editorLogPath=Environment.GetEnvironmentVariable("AppData").Replace("Roaming","")+"Local\\Unity\\Editor\\Editor.log"; break; - case RuntimePlatform.OSXEditor: editorLogPath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Library")+"/Logs/Unity/Editor.log"; break; - case RuntimePlatform.LinuxEditor: editorLogPath="~/.config/unity3d/Editor.log"; break; - } - //Read EditorLog - string fromtext = SVL.buildProcessIDTitleStart+SVL.buildProcessID; - string totext = SVL.buildProcessIDTitleEnd+SVL.buildProcessID; + string fromtext = startTimeStamp; + string totext = startTimeStamp.Replace(SVL.buildProcessIDTitleStart,SVL.buildProcessIDTitleEnd); string currentLine = ""; bool startFound = false; bool endFound = false; FileStream fs = new FileStream(editorLogPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + List compiledShaderInfoFromEditorLog = new List(); using (StreamReader sr = new StreamReader(fs)) { while (!startFound) @@ -224,7 +234,7 @@ private void ReadShaderCompileInfo() //skip the line bla bla bla prepared currentLine = sr.ReadLine(); - if (currentLine.Contains("prepared") && !currentLine.Contains("variants, prepared")) + while(!currentLine.Contains("variants left after stripping, ")) { currentLine = sr.ReadLine(); } @@ -294,22 +304,27 @@ private void ReadShaderCompileInfo() int remoteCacheInt = int.Parse(remoteCache); float strippingTimeFloat = float.Parse(strippingTime); float compileTimeFloat = float.Parse(compileTime); - - //---------- Add to ShaderList ------------// - int listID = SVL.shaderlist.IndexOf(SVL.shaderlist.Find(x => x.name.Equals(shaderName))); - CompiledShader temp = SVL.shaderlist[listID]; - temp.editorLog_originalVariantCount += totalVariantInt; - temp.editorLog_compiledVariantCount += compiledVariantsInt; - temp.editorLog_totalProcessTime += strippingTimeFloat+compileTimeFloat; - temp.editorLog_remainingVariantCount += remainingVariantInt; - temp.editorLog_variantInCacheCount += localCacheInt+remoteCacheInt; - SVL.shaderlist[listID] = temp; - //---------- For total countinvariantInCacheg ------------// - SVL.variantBeforeStrippingCount += totalVariantInt; - SVL.variantCompiledCount += compiledVariantsInt; - SVL.variantInCache += localCacheInt+remoteCacheInt; - variantCountinBuild += remainingVariantInt; //for making sure no bug - + //---------- Temp object info ------------// + CompiledShader temp = new CompiledShader(); + temp.name = shaderName; + temp.editorLog_originalVariantCount = totalVariantInt; + temp.editorLog_compiledVariantCount = compiledVariantsInt; + temp.editorLog_totalProcessTime = strippingTimeFloat+compileTimeFloat; + temp.editorLog_remainingVariantCount = remainingVariantInt; + temp.editorLog_variantInCacheCount = localCacheInt+remoteCacheInt; + //---------- Add to temp list ------------// + int templistID = compiledShaderInfoFromEditorLog.IndexOf(compiledShaderInfoFromEditorLog.Find(x => x.name.Equals(temp.name))); + if(templistID == -1) + { + //Add a new shader record + compiledShaderInfoFromEditorLog.Add(temp); + } + else + { + //Add to existing shader record + compiledShaderInfoFromEditorLog[templistID] = AccumulateToCompiledShader(temp,compiledShaderInfoFromEditorLog[templistID]); + } + //Debug // if(shaderName == "Universal Render Pipeline/Lit") // { @@ -323,21 +338,52 @@ private void ReadShaderCompileInfo() } } - //Bug check - in case my codes in OnProcessShader VS reading EditorLog counts different result - if( SVL.variantFromShader != variantCountinBuild) - { - Debug.LogError("ShaderVariantTool error. "+ - "Tool counted there are "+SVL.variantFromShader+" shader variants in build, "+ - "but Editor Log counted "+variantCountinBuild+". Please contact @mingwai on slack."); - } - int variantCacheAndCompiledSum = SVL.variantInCache + SVL.variantCompiledCount; - if( variantCacheAndCompiledSum != variantCountinBuild) + if(!isLogReader) { - Debug.LogError("ShaderVariantTool error. "+ - "The sum of "+SVL.variantInCache+" variants in cache + "+ - SVL.variantCompiledCount+" variants compiled "+variantCacheAndCompiledSum+" variants is not equal to the accumulated sum "+ - variantCountinBuild+" variants. Please contact @mingwai on slack. This could be related to exisiting known issue: Case 1389276"); + for(int i=0; i x.name.Equals(compiledShaderInfoFromEditorLog[i].name))); + SVL.shaderlist[listID] = AccumulateToCompiledShader(compiledShaderInfoFromEditorLog[i],SVL.shaderlist[listID]); + //---------- For total countinvariantInCacheg ------------// + SVL.variantBeforeStrippingCount += compiledShaderInfoFromEditorLog[i].editorLog_originalVariantCount; + SVL.variantCompiledCount += compiledShaderInfoFromEditorLog[i].editorLog_compiledVariantCount; + SVL.variantInCache += compiledShaderInfoFromEditorLog[i].editorLog_variantInCacheCount; + variantCountinBuild += compiledShaderInfoFromEditorLog[i].editorLog_remainingVariantCount; //for making sure no bug + } + + //Bug check - in case my codes in OnProcessShader VS reading EditorLog counts different result + if( SVL.variantFromShader != variantCountinBuild) + { + Debug.LogError("ShaderVariantTool error. "+ + "Tool counted there are "+SVL.variantFromShader+" shader variants in build, "+ + "but Editor Log counted "+variantCountinBuild+". Please contact @mingwai on slack."); + } + int variantCacheAndCompiledSum = SVL.variantInCache + SVL.variantCompiledCount; + if( variantCacheAndCompiledSum != variantCountinBuild) + { + Debug.LogError("ShaderVariantTool error. "+ + "The sum of shader variants in EditorLog ("+variantCacheAndCompiledSum+" = "+SVL.variantInCache+" in cache + "+ + SVL.variantCompiledCount+" compiled) is not equal to the sum of shader variants collected by ShaderVariantTool ("+ + variantCountinBuild+"). Please contact @mingwai on slack. This could be related to exisiting known issue: Case 1389276"); + } + + //Print invalid / disabled keyword error + if(SVL.invalidKey != "") Debug.LogError("Some shader keywords are invalid: "+SVL.invalidKey); + if(SVL.disabledKey != "") Debug.LogWarning("Some shader keywords are disabled but they are not being stripped: "+SVL.disabledKey); } + + return compiledShaderInfoFromEditorLog; + } + + private static CompiledShader AccumulateToCompiledShader(CompiledShader src, CompiledShader dst) + { + dst.editorLog_originalVariantCount += src.editorLog_originalVariantCount; + dst.editorLog_compiledVariantCount += src.editorLog_compiledVariantCount; + dst.editorLog_totalProcessTime += src.editorLog_totalProcessTime; + dst.editorLog_remainingVariantCount += src.editorLog_remainingVariantCount; + dst.editorLog_variantInCacheCount += src.editorLog_variantInCacheCount; + + return dst; } } } \ No newline at end of file diff --git a/Assets/Editor/ShaderVariantTool/ShaderVariantTool_ComputeProcess.cs b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_ComputeProcess.cs index 88d26e4..95c67bd 100644 --- a/Assets/Editor/ShaderVariantTool/ShaderVariantTool_ComputeProcess.cs +++ b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_ComputeProcess.cs @@ -38,12 +38,12 @@ public void OnProcessComputeShader(ComputeShader shader, string kernelName, ILis scv_default.passType = "--"; scv_default.shaderType = "--"; scv_default.graphicsTier = "--"; + scv_default.buildTarget = "--"; scv_default.shaderCompilerPlatform = "--"; + //scv_default.shaderRequirements = "--"; + scv_default.platformKeywords = "--"; 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++; } @@ -60,18 +60,24 @@ public void OnProcessComputeShader(ComputeShader shader, string kernelName, ILis scv.shaderType = "--"; scv.graphicsTier = data[i].graphicsTier.ToString(); + scv.buildTarget = data[i].buildTarget.ToString(); scv.shaderCompilerPlatform = data[i].shaderCompilerPlatform.ToString(); - //scv.shaderRequirements = ""+data[i].shaderRequirements; - //scv.platformKeywordName = ""+data[i].platformKeywordSet.ToString(); - //scv.isplatformKeywordEnabled = ""+data[i].platformKeywordSet.IsEnabled(BuiltinShaderDefine.SHADER_API_DESKTOP); + //scv.shaderRequirements = data[i].shaderRequirements.ToString().Replace(",","\n"); + scv.platformKeywords =Helper.GetPlatformKeywordList(data[i].platformKeywordSet); bool isLocal = ShaderKeyword.IsKeywordLocal(sk[k]); LocalKeyword lkey = new LocalKeyword(shader,sk[k].name); + //bool isDynamic = lkey.isDynamic; scv.shaderKeywordName = ( isLocal? "[Local]" : "[Global]" ) + sk[k].name; //sk[k].GetKeywordName(); scv.shaderKeywordType = isLocal? "--" : ShaderKeyword.GetGlobalKeywordType(sk[k]).ToString(); //""+sk[k].GetKeywordType().ToString(); - scv.shaderKeywordIndex = sk[k].index.ToString(); - scv.isShaderKeywordValid = sk[k].IsValid().ToString(); - scv.isShaderKeywordEnabled = data[i].shaderKeywordSet.IsEnabled(sk[k]).ToString(); + if( !sk[k].IsValid() ) + { + SVL.invalidKey += "\n"+"Shader "+scv.shaderName+" Keyword "+scv.shaderKeywordName+" is invalid."; + } + if( !data[i].shaderKeywordSet.IsEnabled(sk[k]) ) + { + SVL.disabledKey += "\n"+"Shader "+scv.shaderName+" Keyword "+scv.shaderKeywordName+" is not enabled. You can create a custom shader stripping script to strip it."; + } SVL.variantlist.Add(scv); SVL.compiledTotalCount++; diff --git a/Assets/Editor/ShaderVariantTool/ShaderVariantTool_Helper.cs b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_Helper.cs index 4f94b10..4ee2832 100644 --- a/Assets/Editor/ShaderVariantTool/ShaderVariantTool_Helper.cs +++ b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_Helper.cs @@ -1,6 +1,9 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +using UnityEngine.Rendering; +using System; +using System.IO; namespace GfxQA.ShaderVariantTool { @@ -63,5 +66,32 @@ public static void DebugLog(string msg) { Debug.LogFormat(LogType.Log, LogOption.NoStacktrace, null, msg); } + + public static string GetPlatformKeywordList(PlatformKeywordSet pks) + { + string enabledPKeys = ""; + foreach(BuiltinShaderDefine sd in System.Enum.GetValues(typeof(BuiltinShaderDefine))) + { + //Only pay attention to SHADER_API_MOBILE, SHADER_API_DESKTOP and SHADER_API_GLES30 + if( sd.ToString().Contains("SHADER_API") && pks.IsEnabled(sd) ) + { + if(enabledPKeys != "") enabledPKeys += " "; + enabledPKeys += sd.ToString(); + } + } + return enabledPKeys; + } + + public static string GetEditorLogPath() + { + string editorLogPath = ""; + switch(Application.platform) + { + case RuntimePlatform.WindowsEditor: editorLogPath=Environment.GetEnvironmentVariable("AppData").Replace("Roaming","")+"Local\\Unity\\Editor\\Editor.log"; break; + case RuntimePlatform.OSXEditor: editorLogPath=Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Library")+"/Logs/Unity/Editor.log"; break; + case RuntimePlatform.LinuxEditor: editorLogPath="~/.config/unity3d/Editor.log"; break; + } + return editorLogPath; + } } } \ No newline at end of file diff --git a/Assets/Editor/ShaderVariantTool/ShaderVariantTool_LogReader.cs b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_LogReader.cs new file mode 100644 index 0000000..4e2b0e5 --- /dev/null +++ b/Assets/Editor/ShaderVariantTool/ShaderVariantTool_LogReader.cs @@ -0,0 +1,136 @@ +using UnityEditor; +using UnityEngine; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace GfxQA.ShaderVariantTool +{ + public class ShaderVariantTool_LogReader : EditorWindow + { + private List timeStamps; + private int selectedTimeStamp = 0; + private string editorLogPath = ""; + private string savedFile = ""; + + [MenuItem("Window/ShaderVariantTool_LogReader")] + public static void ShowWindow () + { + var window = EditorWindow.GetWindow (typeof(ShaderVariantTool_LogReader)); + window.name = "ShaderVariantTool LogReader"; + window.titleContent = new GUIContent("ShaderVariantTool LogReader"); + } + + public void Awake() + { + editorLogPath = Helper.GetEditorLogPath(); + } + + private void UpdateTimeStampList() + { + timeStamps = new List(); + FileStream fs = new FileStream(editorLogPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + using (StreamReader sr = new StreamReader(fs)) + { + string line; + while ((line = sr.ReadLine()) != null) + { + if(line.Contains(SVL.buildProcessIDTitleStart)) + { + timeStamps.Add(line); + } + } + } + } + + // public void OnDestroy() + // { + // } + + void OnGUI () + { + Color originalBackgroundColor = GUI.backgroundColor; + + //Width for the columns & style + float currentSize = this.position.width; + float widthForEach = currentSize / (SVL.columns.Length-1+currentSize*0.0002f); + GUIStyle background = new GUIStyle + { + normal = + { + background = Texture2D.whiteTexture, + textColor = Color.white + } + }; + + //Title + GUI.color = Color.cyan; + GUILayout.Label ("This log reader is for reading previous builds, which only reads information about variant count before/after stripping per shader. \n"+"Therefore detailed shader keywords and compute shader information is not available. So please only use this if the original ShaderVariantTool doesn't work.", EditorStyles.wordWrappedLabel); + GUILayout.Space(10); + GUI.color = Color.white; + + //Editor.Log path textbox + editorLogPath = GUILayout.TextField(editorLogPath); + + //TimeStamp select: drop down and a button to update drop down + if(timeStamps== null) UpdateTimeStampList(); + selectedTimeStamp = EditorGUILayout.Popup("Select timestamp", selectedTimeStamp, timeStamps.ToArray()); + if (GUILayout.Button ("Update TimeStampList",GUILayout.Width(200))) + { + UpdateTimeStampList(); + } + + //Read log and save the CSV file + if (GUILayout.Button ("Read log",GUILayout.Width(200))) + { + //ReadLog + List slist = ShaderVariantTool_BuildPostprocess.ReadShaderCompileInfo(timeStamps[selectedTimeStamp],editorLogPath,true); + + //Prepare CSV string + List outputRows = new List(); + + //Overview + int total_shaderCount = slist.Count; + int total_variantBeforeStripping = 0; + int total_variantInBuild = 0; + int total_variantInCache = 0; + int total_variantCompiled = 0; + for(int i=0; i Date: Tue, 21 Jan 2025 15:20:00 +0100 Subject: [PATCH 3/3] Add builtinRP replacement shader --- .../TestReplacementShader.cs | 57 +++++++++ .../TestReplacementShader.shader | 120 ++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 Assets/CameraReplacementShader_BiRP/TestReplacementShader.cs create mode 100644 Assets/CameraReplacementShader_BiRP/TestReplacementShader.shader diff --git a/Assets/CameraReplacementShader_BiRP/TestReplacementShader.cs b/Assets/CameraReplacementShader_BiRP/TestReplacementShader.cs new file mode 100644 index 0000000..7269d78 --- /dev/null +++ b/Assets/CameraReplacementShader_BiRP/TestReplacementShader.cs @@ -0,0 +1,57 @@ +using UnityEngine; + +// BuiltinRP only +// https://docs.unity3d.com/Manual/SL-ShaderReplacement.html +public class TestReplacementShader : MonoBehaviour +{ + public Camera cam; + public Shader replaceShader; + + public enum RenderTypes + { + All, + RenderType, + } + + public RenderTypes renderType = RenderTypes.RenderType; + + void OnEnable() + { + Setup(); + } + + void OnValidate() + { + Setup(); + } + + void OnDisable() + { + Cleanup(); + } + + void OnDestroy() + { + Cleanup(); + } + + private void Setup() + { + string renderTypeStr = ""; + if (renderType != RenderTypes.All) + { + renderTypeStr = renderType.ToString(); + } + cam.SetReplacementShader (replaceShader, renderTypeStr); + + Debug.Log("Configured Replacement Shader."); + } + + private void Cleanup() + { + cam.ResetReplacementShader(); + + Debug.Log("Reset Replacement Shader."); + } +} + diff --git a/Assets/CameraReplacementShader_BiRP/TestReplacementShader.shader b/Assets/CameraReplacementShader_BiRP/TestReplacementShader.shader new file mode 100644 index 0000000..b4e308c --- /dev/null +++ b/Assets/CameraReplacementShader_BiRP/TestReplacementShader.shader @@ -0,0 +1,120 @@ +Shader "Test Replacement Shader" +{ + Properties + { + _Color ("Main Color", Color) = (1,1,1,1) + _MainTex ("_MainTex (RGBA)", 2D) = "white" {} + } + + CGINCLUDE + + #include "UnityCG.cginc" + + struct appdata + { + float4 vertex : POSITION; + float2 uv : TEXCOORD0; + }; + + struct v2f + { + float2 uv : TEXCOORD0; + float4 vertex : SV_POSITION; + }; + + sampler2D _MainTex; + float4 _MainTex_ST; + + float4 _Color; + + v2f vert (appdata v) + { + v2f o; + o.vertex = UnityObjectToClipPos(v.vertex); + o.uv = TRANSFORM_TEX(v.uv, _MainTex); + return o; + } + + ENDCG + + SubShader // Opaque = Red + { + Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque" } + Cull Off Lighting Off ZWrite Off + Blend SrcAlpha OneMinusSrcAlpha + + Pass + { + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + + fixed4 frag (v2f i) : SV_Target + { + return float4(1,0,0,0.1); + } + + ENDCG + } + } + SubShader // Transparent = Green + { + Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" } + Cull Off Lighting Off ZWrite Off + Blend SrcAlpha OneMinusSrcAlpha + + Pass + { + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + + fixed4 frag (v2f i) : SV_Target + { + return float4(0,1,0,0.1); + } + + ENDCG + } + } + SubShader // Background = Cyan + { + Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Background" } + Cull Off Lighting Off ZWrite Off + Blend SrcAlpha OneMinusSrcAlpha + + Pass + { + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + + fixed4 frag (v2f i) : SV_Target + { + return float4(0,1,1,0.1); + } + + ENDCG + } + } + SubShader // Overlay = Yellow + { + Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Overlay" } + Cull Off Lighting Off ZWrite Off + Blend SrcAlpha OneMinusSrcAlpha + + Pass + { + CGPROGRAM + #pragma vertex vert + #pragma fragment frag + + fixed4 frag (v2f i) : SV_Target + { + return float4(1,1,0,0.1); + } + + ENDCG + } + } +}