public static ShadersBuildInfo CheckShadersBuildStore(ShadersBuildInfo shadersBuildInfo)
        {
            if (shadersBuildInfo == null)
            {
                string filename = GetStoredDataPath();
                shadersBuildInfo = AssetDatabase.LoadAssetAtPath <ShadersBuildInfo>(filename);
                if (shadersBuildInfo != null)
                {
                    return(shadersBuildInfo);
                }
            }

            // Check if scriptable object exists
            string path = GetStoredDataPath();

            if (!File.Exists(path))
            {
                string dir = Path.GetDirectoryName(path);
                Directory.CreateDirectory(dir);
                shadersBuildInfo = ScriptableObject.CreateInstance <ShadersBuildInfo>();
                AssetDatabase.CreateAsset(shadersBuildInfo, path);
                AssetDatabase.SaveAssets();
            }
            return(shadersBuildInfo);
        }
 void ClearBuildData()
 {
     shadersBuildInfo = ShaderDebugBuildProcessor.CheckShadersBuildStore(shadersBuildInfo);
     if (shadersBuildInfo != null)
     {
         shadersBuildInfo.Clear();
     }
 }
        public static void CheckShadersBuildStore()
        {
            // Check if scriptable object exists
            string path = GetStoredDataPath();

            if (!File.Exists(path))
            {
                string dir = Path.GetDirectoryName(path);
                Directory.CreateDirectory(dir);
                ShadersBuildInfo shadersBuildInfo = ScriptableObject.CreateInstance <ShadersBuildInfo>();
                AssetDatabase.CreateAsset(shadersBuildInfo, path);
                AssetDatabase.SaveAssets();
            }
        }
        public void OnProcessShader(
            Shader shader, ShaderSnippetData snippet, IList <ShaderCompilerData> shaderCompilerData)
        {
            bool skipCompilation = false;

            if (SCWindow.GetEditorPrefBool("QUICK_BUILD", false))
            {
                skipCompilation = true;
            }

            if (shadersBuildInfo == null)
            {
                string filename = GetStoredDataPath();
                shadersBuildInfo = AssetDatabase.LoadAssetAtPath <ShadersBuildInfo>(filename);
                if (shadersBuildInfo == null)
                {
                    shadersBuildInfo = ScriptableObject.CreateInstance <ShadersBuildInfo>();
                    Directory.CreateDirectory(Path.GetDirectoryName(filename));
                    AssetDatabase.CreateAsset(shadersBuildInfo, filename);
                    EditorUtility.SetDirty(shadersBuildInfo);
                }
            }

            ShaderBuildInfo sb = shadersBuildInfo.GetShader(shader.name);

            if (sb == null)
            {
                sb            = new ShaderBuildInfo();
                sb.name       = shader.name;
                sb.simpleName = SCShader.GetSimpleName(sb.name);
                sb.type       = snippet.shaderType;
                string path = AssetDatabase.GetAssetPath(shader);
                sb.isInternal = string.IsNullOrEmpty(path) || !File.Exists(path);
                shadersBuildInfo.Add(sb);
                EditorUtility.SetDirty(shadersBuildInfo);
            }
            else if (!sb.includeInBuild)
            {
                skipCompilation = true;
            }

            int count = shaderCompilerData.Count;

            for (int i = 0; i < count; ++i)
            {
                ShaderKeywordSet ks = shaderCompilerData[i].shaderKeywordSet;
                foreach (ShaderKeyword kw in ks.GetShaderKeywords())
                {
#if UNITY_2019_3_OR_NEWER
                    string kname = ShaderKeyword.GetKeywordName(shader, kw);
#else
                    string kname = kw.GetKeywordName();
#endif
                    if (string.IsNullOrEmpty(kname))
                    {
                        continue;
                    }
                    if (!sb.KeywordsIsIncluded(kname))
                    {
                        shaderCompilerData.RemoveAt(i);
                        count--;
                        i--;
                        break;
                    }
                    else
                    {
                        EditorUtility.SetDirty(shadersBuildInfo);
                    }
                }
            }

            if (skipCompilation)
            {
                shaderCompilerData.Clear();
            }
        }
        public void OnProcessShader(
            Shader shader, ShaderSnippetData snippet, IList <ShaderCompilerData> shaderCompilerData)
        {
            try {
                bool skipCompilation = false;
                if (SCWindow.GetEditorPrefBool("QUICK_BUILD", false))
                {
                    skipCompilation = true;
                }

                if (shadersBuildInfo == null)
                {
                    string filename = GetStoredDataPath();
                    shadersBuildInfo = AssetDatabase.LoadAssetAtPath <ShadersBuildInfo>(filename);
                    if (shadersBuildInfo == null)
                    {
                        return;
                    }
                }

                ShaderBuildInfo sb = shadersBuildInfo.GetShader(shader.name);
                if (sb == null)
                {
                    sb            = new ShaderBuildInfo();
                    sb.name       = shader.name;
                    sb.simpleName = SCShader.GetSimpleName(sb.name);
                    sb.type       = snippet.shaderType;
                    string path = AssetDatabase.GetAssetPath(shader);
                    sb.isInternal = string.IsNullOrEmpty(path) || !File.Exists(path);
                    shadersBuildInfo.Add(sb);
                    EditorUtility.SetDirty(shadersBuildInfo);
                }
                else if (!sb.includeInBuild)
                {
                    skipCompilation = true;
                }

                int count = shaderCompilerData.Count;
                for (int i = 0; i < count; ++i)
                {
                    ShaderKeywordSet ks = shaderCompilerData[i].shaderKeywordSet;
                    foreach (ShaderKeyword kw in ks.GetShaderKeywords())
                    {
#if UNITY_2019_3_OR_NEWER
                        string kname = ShaderKeyword.GetKeywordName(shader, kw);
#else
                        string kname = kw.GetName();
#endif
                        if (string.IsNullOrEmpty(kname))
                        {
                            continue;
                        }
                        if (!sb.KeywordsIsIncluded(kname))
                        {
                            shaderCompilerData.RemoveAt(i);
                            count--;
                            i--;
                            break;
                        }
                        else
                        {
                            EditorUtility.SetDirty(shadersBuildInfo);
                        }
                    }
                }

                if (skipCompilation)
                {
                    shaderCompilerData.Clear();
                    return;
                }
            } catch (Exception ex) {
                Debug.LogWarning("Shader Control detected an error during compilation of one shader: " + ex.ToString());
            }
        }
        void RefreshBuildStats(bool quick)
        {
            issueRefresh       = 1;
            nextQuickBuild     = GetEditorPrefBool("QUICK_BUILD", false);
            shadersBuildInfo   = ShaderDebugBuildProcessor.CheckShadersBuildStore(shadersBuildInfo);
            totalBuildKeywords = totalBuildIncludedKeywords = totalBuildShadersWithKeywords = totalBuildShaders = totalBuildIncludedShaders = totalBuildIncludedShadersWithKeywords = 0;
            shadersBuildInfo   = Resources.Load <ShadersBuildInfo>("BuiltShaders");
            if (shadersBuildInfo == null || shadersBuildInfo.shaders == null)
            {
                return;
            }

            if (uniqueBuildKeywords == null)
            {
                uniqueBuildKeywords = new Dictionary <string, List <ShaderBuildInfo> >();
            }
            else
            {
                uniqueBuildKeywords.Clear();
            }
            if (uniqueIncludedBuildKeywords == null)
            {
                uniqueIncludedBuildKeywords = new Dictionary <string, List <ShaderBuildInfo> >();
            }
            else
            {
                uniqueIncludedBuildKeywords.Clear();
            }

            int count = shadersBuildInfo.shaders.Count;

            totalBuildShaders          = 0;
            maxBuildKeywordsCountFound = 0;

            for (int k = 0; k < count; k++)
            {
                ShaderBuildInfo sb      = shadersBuildInfo.shaders[k];
                int             kwCount = sb.keywords != null ? sb.keywords.Count : 0;
                if (shadersBuildInfo.sortType != BuildViewSortType.Keyword)
                {
                    if (minimumKeywordCount > 0 && kwCount < minimumKeywordCount)
                    {
                        continue;
                    }
                    if (!string.IsNullOrEmpty(keywordFilter) && !sb.ContainsKeyword(keywordFilter, false))
                    {
                        continue;
                    }
                }
                if (shadersBuildInfo.viewType == BuildViewShaderOption.ProjectShaders && sb.isInternal)
                {
                    continue;
                }
                if (shadersBuildInfo.viewType == BuildViewShaderOption.UnityInternalShaders && !sb.isInternal)
                {
                    continue;
                }
                if (!string.IsNullOrEmpty(buildShaderNameFilter) && sb.name.IndexOf(buildShaderNameFilter, StringComparison.InvariantCultureIgnoreCase) < 0)
                {
                    continue;
                }
                totalBuildShaders++;

                // Check shaders exist
                if (!quick && Shader.Find(sb.name) == null)
                {
                    shadersBuildInfo.shaders.RemoveAt(k);
                    k--;
                    totalBuildShaders--;
                    count--;
                    continue;
                }
                if (sb.includeInBuild)
                {
                    totalBuildIncludedShaders++;
                }
                if (kwCount > 0)
                {
                    if (kwCount > maxBuildKeywordsCountFound)
                    {
                        maxBuildKeywordsCountFound = kwCount;
                    }
                    //totalBuildKeywords += kwCount;
                    totalBuildShadersWithKeywords++;
                    if (sb.includeInBuild)
                    {
                        totalBuildIncludedShadersWithKeywords++;
                        for (int j = 0; j < kwCount; j++)
                        {
                            List <ShaderBuildInfo> shaderList;
                            if (!uniqueBuildKeywords.TryGetValue(sb.keywords[j].keyword, out shaderList))
                            {
                                totalBuildKeywords++;
                                shaderList = new List <ShaderBuildInfo>();
                                uniqueBuildKeywords[sb.keywords[j].keyword] = shaderList;
                            }
                            shaderList.Add(sb);
                            if (sb.keywords[j].includeInBuild)
                            {
                                List <ShaderBuildInfo> includedList;
                                if (!uniqueIncludedBuildKeywords.TryGetValue(sb.keywords[j].keyword, out includedList))
                                {
                                    totalBuildIncludedKeywords++;
                                    includedList = new List <ShaderBuildInfo>();
                                    uniqueIncludedBuildKeywords[sb.keywords[j].keyword] = includedList;
                                }
                                includedList.Add(sb);
                            }
                        }
                    }
                }
            }

            if (buildKeywordView == null)
            {
                buildKeywordView = new List <BuildKeywordView>();
            }
            else
            {
                buildKeywordView.Clear();
            }
            foreach (KeyValuePair <string, List <ShaderBuildInfo> > kvp in uniqueBuildKeywords)
            {
                BuildKeywordView kv = new BuildKeywordView {
                    keyword = kvp.Key, shaders = kvp.Value
                };
                buildKeywordView.Add(kv);
            }
            buildKeywordView.Sort(delegate(BuildKeywordView x, BuildKeywordView y) {
                return(y.shaders.Count.CompareTo(x.shaders.Count));
            });
            // Annotate which keywords are used in project
            int bkwCount = buildKeywordView.Count;

            for (int k = 0; k < bkwCount; k++)
            {
                BuildKeywordView bkv        = buildKeywordView[k];
                bool             isInternal = true;
                int shadersCount            = bkv.shaders.Count;
                for (int j = 0; j < shadersCount; j++)
                {
                    if (!bkv.shaders[j].isInternal)
                    {
                        isInternal = false;
                        break;
                    }
                }
                bkv.isInternal = isInternal;
            }

            UpdateProjectStats();
        }