Esempio n. 1
0
        public bool KeepVariants(Shader shader, ShaderSnippetData snippet, ShaderCompilerData variants)
        {
            keywords.Clear();
            var shaderKeywords = variants.shaderKeywordSet.GetShaderKeywords();

            foreach (var shaderkeyword in shaderKeywords)
            {
                keywords.Add(ShaderKeyword.GetGlobalKeywordName(shaderkeyword));
            }
            bool hasKeywords = keywords.Count > 0;

            Debug.Log(shader.name + "\tVariants:" + (hasKeywords ? string.Join("\t", keywords.ToArray()) : "<no keywords>"));

            if (hasKeywords && keepExportVariants != null)
            {
                ShaderVariant shaderVariant = new ShaderVariant(shader, snippet.passType, keywords.ToArray());
                // 必须,自动导出的变体组合
                if (keepExportVariants.Contains(shaderVariant))
                {
                    return(true);
                }
                // 可选,自定义变体组合
                if (keepCustomVariants != null && keepCustomVariants.Contains(shaderVariant))
                {
                    return(true);
                }
                return(false);
            }
            return(true);
        }
Esempio n. 2
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));
            }
        }
        /// <summary>
        /// 转换成Keywords
        /// </summary>
        private string[] ConvertToKeywords(ShaderKeywordSet shaderKeywordSet)
        {
            ShaderKeyword[] shaderKeywords = shaderKeywordSet.GetShaderKeywords();
            string[]        keywords       = new string[shaderKeywords.Length];
            for (int iKeyword = 0; iKeyword < shaderKeywords.Length; iKeyword++)
            {
                ShaderKeyword iterKeyword = shaderKeywords[iKeyword];
                keywords[iKeyword] =
#if UNITY_2019_1_OR_NEWER
                    ShaderKeyword.GetGlobalKeywordName(iterKeyword)
#else
                    m_KeywordNames[UnityEngineReflectionUtility.ShaderKeyword.GetShaderKeywordIndex(iterKeyword)]
#endif
                ;
            }
            return(keywords);
        }
        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 { }
        }
Esempio n. 5
0
        private void RefreshShaderKeywords()
        {
            m_GlobalKeywords.Clear();
            for (int iKeyword = 0; iKeyword < UnityEngineReflectionUtility.ShaderKeyword.MAX_SHADER_KEYWORDS; iKeyword++)
            {
                ShaderKeyword iterKeyword = UnityEngineReflectionUtility.ShaderKeyword.NewShaderKeyword(iKeyword);
                string        keywordName = ShaderKeyword.GetGlobalKeywordName(iterKeyword);
                if (!ShaderUtility.IsValidAndUsed(iterKeyword, keywordName))
                {
                    continue;
                }

                KeywordItem keywordItem = new KeywordItem();
                keywordItem.KeywordName = keywordName;
                keywordItem.KeywordType = ShaderKeyword.GetGlobalKeywordType(iterKeyword);
                keywordItem.Enable      = Shader.IsKeywordEnabled(keywordItem.KeywordName);
                keywordItem.Content     = new GUIContent(keywordItem.KeywordName);
                m_GlobalKeywords.Add(keywordItem);
            }
        }
Esempio n. 6
0
        public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList <ShaderCompilerData> shaderData)
        {
            if (shader.name.Contains("Universal Render Pipeline") ||
                shader.name.Contains("TextMeshPro") ||
                shader.name.Contains("Default") ||
                shader.name.Contains("Hidden") ||
                shader.name.Contains("UI") ||
                shader.name.Contains("Sprites") ||
                shader.name.Contains("Skybox")
                )
            {
                return;
            }

            _shaderVariantCache.shader   = shader;
            _shaderVariantCache.passType = snippet.passType;
            for (var i = 0; i < shaderData.Count; ++i)
            {
                var internalShaderData = shaderData[i];
                var shaderKeywords     = internalShaderData.shaderKeywordSet.GetShaderKeywords();

                _compilerKeywords.Clear();
                foreach (var internalKeyword in shaderKeywords)
                {
                    _compilerKeywords.Add(ShaderKeyword.GetGlobalKeywordName(internalKeyword));
                }

                _shaderVariantCache.keywords = _compilerKeywords.ToArray();

                if (_shaderVariantCollection.Contains(_shaderVariantCache))
                {
                    continue;
                }
                shaderData.RemoveAt(i);
                i--;
            }
        }
Esempio n. 7
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);
        }
    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;
    }
Esempio n. 9
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);
            }
        }
Esempio n. 10
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();
        }
Esempio n. 11
0
 public static string GetKeywordName(this ShaderKeyword keyword, Shader shader)
 {
     return(ShaderKeyword.IsKeywordLocal(keyword) ? ShaderKeyword.GetKeywordName(shader, keyword) : ShaderKeyword.GetGlobalKeywordName(keyword));
 }
        public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList <ShaderCompilerData> data)
        {
            if (shader.name.StartsWith("Shapes/") == false)
            {
                return;                 // ignore all non-Shapes shaders
            }
            // Shapes immediate mode has to force instancing on.
            // find variants that don't have an instancing counterpart, copy them, and add instancing
            string GetKeywordsStrWithoutInstancing(ShaderCompilerData set)
            {
                return(string.Join(",", set.shaderKeywordSet.GetShaderKeywords()
                                #if UNITY_2019_3_OR_NEWER
                                   .Select(ShaderKeyword.GetGlobalKeywordName).Where(a => a != ShaderKeyword.GetGlobalKeywordName(inst))
                                #else
                                   .Select(a => a.GetKeywordName()).Where(a => a != inst.GetKeywordName())
                                #endif
                                   .OrderBy(a => a)));
            }

            HashSet <string> thingsWithInstancing    = new HashSet <string>(data.Where(x => x.shaderKeywordSet.IsEnabled(inst)).Select(GetKeywordsStrWithoutInstancing));
            HashSet <string> thingsWithoutInstancing = new HashSet <string>(data.Where(x => !x.shaderKeywordSet.IsEnabled(inst)).Select(GetKeywordsStrWithoutInstancing));
            thingsWithoutInstancing.ExceptWith(thingsWithInstancing);               // filter out only the ones missing instancing versions
            List <ShaderCompilerData> thingsToClone = data.Where(x => !x.shaderKeywordSet.IsEnabled(inst) && thingsWithoutInstancing.Contains(GetKeywordsStrWithoutInstancing(x))).ToList();
            foreach (ShaderCompilerData thing in thingsToClone)
            {
                ShaderCompilerData copy = thing;
                copy.shaderKeywordSet.Enable(inst);
                data.Add(copy);
            }
        }