private string[] GetExistingShaderKeywords(Shader shader, IList <ShaderCompilerData> data)
 {
     return(data.SelectMany(d => d.shaderKeywordSet.GetShaderKeywords())
            .Where(k => ShaderKeyword.IsKeywordLocal(k))
            .Select(k => ShaderKeyword.GetKeywordName(shader, k))
            .Where(kwd => WFCommonUtility.IsEnableKeyword(kwd)).Distinct().ToArray());
 }
Example #2
0
        static string[] GetShaderKeywords(Shader shader, ShaderKeyword[] shaderKeywords)
        {
#if UNITY_2019_3_OR_NEWER
            var keywords = shaderKeywords.Select(keyword => ShaderKeyword.IsKeywordLocal(keyword) ? ShaderKeyword.GetKeywordName(shader, keyword) : ShaderKeyword.GetGlobalKeywordName(keyword)).ToArray();
#else
            var keywords = shaderKeywords.Select(keyword => keyword.GetKeywordName()).ToArray();
#endif
            return(keywords);
        }
Example #3
0
        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");
        }
Example #4
0
        public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList <ShaderCompilerData> data)
        {
            if (!UPRToolSetting.Instance.enableShaderVariantsCheck)
            {
                return;
            }

            if (snippet.shaderType != ShaderType.Fragment)
            {
                return;
            }

            var shaderName = shader.name;

            if (!s_ShaderNames.Contains(shaderName))
            {
                return;
            }
            if (!s_ShaderReports.ContainsKey(shaderName))
            {
                s_ShaderReports.Add(shaderName, new List <ShaderVariantsCheckResult>());
            }

            foreach (var shaderCompilerData in data)
            {
                var shaderKeywordSet = shaderCompilerData.shaderKeywordSet.GetShaderKeywords().ToArray();

#if UNITY_2019_3_OR_NEWER
                var keywords = shaderKeywordSet.Select(keyword =>
                                                       ShaderKeyword.IsKeywordLocal(keyword)
                        ? ShaderKeyword.GetKeywordName(shader, keyword)
                        : ShaderKeyword.GetGlobalKeywordName(keyword)).ToArray();
#else
                var keywords = shaderKeywordSet.Select(keyword => keyword.GetKeywordName()).ToArray();
#endif
                var keywordString = string.Join(", ", keywords);
                if (string.IsNullOrEmpty(keywordString))
                {
                    keywordString = "<no keywords>";
                }

                s_ShaderReports[shaderName].Add(new ShaderVariantsCheckResult(shaderName,
                                                                              shaderCompilerData.shaderCompilerPlatform.ToString(),
                                                                              shaderCompilerData.graphicsTier.ToString(), snippet.passName,
                                                                              keywordString));
            }
        }
        public void OnProcessShader(
            Shader shader, ShaderSnippetData snippet, IList <ShaderCompilerData> shaderCompilerData)
        {
            try {
                if (shaderCompilerData == null)
                {
                    return;
                }

                if (!"Hidden/Universal Render Pipeline/UberPost".Equals(shader.name) && !"Hidden/Kronnect/Beautify".Equals(shader.name))
                {
                    return;
                }

                string strippedKeywords = PlayerPrefs.GetString(PLAYER_PREF_KEYNAME);
                if (string.IsNullOrEmpty(strippedKeywords))
                {
                    return;
                }

                for (int k = shaderCompilerData.Count - 1; k >= 0; k--)
                {
                    ShaderCompilerData data     = shaderCompilerData[k];
                    ShaderKeyword[]    keywords = data.shaderKeywordSet.GetShaderKeywords();
                    for (int s = 0; s < keywords.Length; s++)
                    {
                        ShaderKeyword keyword = keywords[s];
                        string        keywordName;
                        if (ShaderKeyword.IsKeywordLocal(keyword))
                        {
                            keywordName = ShaderKeyword.GetKeywordName(shader, keyword);
                        }
                        else
                        {
                            keywordName = ShaderKeyword.GetGlobalKeywordName(keyword);
                        }
                        if (keywordName.Length > 0 && strippedKeywords.Contains(keywordName))
                        {
                            shaderCompilerData.RemoveAt(k);
                            break;
                        }
                    }
                }
            } catch { }
        }
Example #6
0
        void AddVariants(Shader shader, string assetPath, int id, List <ShaderVariantData> shaderVariants, Action <ProjectIssue> onIssueFound)
        {
            var shaderName = shader.name;
            var descriptor = new ProblemDescriptor
                             (
                id++,
                shaderName,
                Area.BuildSize,
                string.Empty,
                string.Empty
                             );

            foreach (var shaderVariantData in shaderVariants)
            {
                var compilerData     = shaderVariantData.compilerData;
                var shaderKeywordSet = compilerData.shaderKeywordSet.GetShaderKeywords().ToArray();

#if UNITY_2019_3_OR_NEWER
                var keywords = shaderKeywordSet.Select(keyword => ShaderKeyword.IsKeywordLocal(keyword) ?  ShaderKeyword.GetKeywordName(shader, keyword) : ShaderKeyword.GetGlobalKeywordName(keyword)).ToArray();
#else
                var keywords = shaderKeywordSet.Select(keyword => keyword.GetKeywordName()).ToArray();
#endif
                var keywordString = String.Join(", ", keywords);
                if (string.IsNullOrEmpty(keywordString))
                {
                    keywordString = "<no keywords>";
                }

                var issue = new ProjectIssue(descriptor, shaderName, IssueCategory.ShaderVariants, new Location(assetPath));
                issue.SetCustomProperties(new[]
                {
                    compilerData.shaderCompilerPlatform.ToString(),
                    shaderVariantData.passName,
                    keywordString,
                });

                onIssueFound(issue);
            }
        }
Example #7
0
        public void OnProcessComputeShader(ComputeShader shader, string kernelName, 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.kernelName             = kernelName;
                    scv_default.passName               = "--";
                    scv_default.passType               = "--";
                    scv_default.shaderType             = "--";
                    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.kernelName = kernelName;
                    scv.passName   = "--";
                    scv.passType   = "--";
                    scv.shaderType = "--";

                    scv.graphicsTier           = data[i].graphicsTier.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);

                    bool         isLocal   = ShaderKeyword.IsKeywordLocal(sk[k]);
                    LocalKeyword lkey      = new LocalKeyword(shader, sk[k].name);
                    bool         isDynamic = lkey.isDynamic;
                    scv.shaderKeywordName      = (isLocal? "[Local]" : "[Global]") + (isDynamic? "[Dynamic] " : " ") + 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();

                    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);
                SVL.computeShaderCount++;
                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;
            SVL.variantFromCompute += newVariantsForThisShader;
        }
        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.kernelName             = "--";
                    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 = "--";
                    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.kernelName = "--";

                    scv.graphicsTier           = data[i].graphicsTier.ToString();
                    scv.buildTarget            = data[i].buildTarget.ToString();
                    scv.shaderCompilerPlatform = data[i].shaderCompilerPlatform.ToString();
                    //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]") + (isDynamic? "[Dynamic] " : " ") + sk[k].name; //sk[k].GetKeywordName();
                    scv.shaderKeywordType = isLocal? "--" : ShaderKeyword.GetGlobalKeywordType(sk[k]).ToString();             //""+sk[k].GetKeywordType().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++;

                    //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;
        }
Example #9
0
        public void Audit(Action <ProjectIssue> onIssueFound, Action onComplete, IProgressBar progressBar = null)
        {
            var id = k_ShaderVariantFirstId;

            if (s_ShaderCompilerData == null)
            {
                var descriptor = new ProblemDescriptor
                                 (
                    id,
                    "Shader analysis incomplete",
                    Area.BuildSize,
                    string.Empty,
                    string.Empty
                                 );

                var message = "Build the project and run Project Auditor analysis";
#if !UNITY_2018_2_OR_NEWER
                message = "This feature requires Unity 2018";
#endif
                var issue = new ProjectIssue(descriptor, message, IssueCategory.Shaders);
                issue.SetCustomProperties(new[] { string.Empty, string.Empty });
                onIssueFound(issue);
                onComplete();
                return;
            }

            var shaderGuids = AssetDatabase.FindAssets("t:shader");
            foreach (var guid in shaderGuids)
            {
                var assetPath = AssetDatabase.GUIDToAssetPath(guid);
                var shader    = AssetDatabase.LoadMainAssetAtPath(assetPath) as Shader;

                List <ShaderCompilerData> shaderCompilerDataContainer;
                s_ShaderCompilerData.TryGetValue(shader.name, out shaderCompilerDataContainer);
                if (shaderCompilerDataContainer != null)
                {
                    var descriptor = new ProblemDescriptor
                                     (
                        id++,
                        shader.name,
                        Area.BuildSize,
                        string.Empty,
                        string.Empty
                                     );

                    foreach (var shaderCompilerData in shaderCompilerDataContainer)
                    {
                        var shaderKeywordSet = shaderCompilerData.shaderKeywordSet.GetShaderKeywords().ToArray();

#if UNITY_2019_3_OR_NEWER
                        var keywords = shaderKeywordSet.Select(keyword => ShaderKeyword.IsKeywordLocal(keyword) ?  ShaderKeyword.GetKeywordName(shader, keyword) : ShaderKeyword.GetGlobalKeywordName(keyword)).ToArray();
#else
                        var keywords = shaderKeywordSet.Select(keyword => keyword.GetKeywordName()).ToArray();
#endif
                        var keywordString = String.Join(", ", keywords);
                        if (string.IsNullOrEmpty(keywordString))
                        {
                            keywordString = "<no keywords>";
                        }

                        var issue = new ProjectIssue(descriptor, shader.name, IssueCategory.Shaders, new Location(assetPath));

                        issue.SetCustomProperties(new[]
                        {
                            shaderCompilerData.shaderCompilerPlatform.ToString(),
                            keywordString,
                        });

                        onIssueFound(issue);
                    }
                }
            }

            onComplete();
        }
Example #10
0
 public static string GetKeywordName(this ShaderKeyword keyword, Shader shader)
 {
     return(ShaderKeyword.IsKeywordLocal(keyword) ? ShaderKeyword.GetKeywordName(shader, keyword) : ShaderKeyword.GetGlobalKeywordName(keyword));
 }
Example #11
0
 public static bool IsLocalKeyword(this string keyword, Shader shader)
 {
     return(ShaderKeyword.IsKeywordLocal(keyword.ToShaderKeyword(shader)));
 }