void ScanShaderGraph(SCShader shader)
        {
            shader.editedByShaderControl = File.Exists(shader.path + BACKUP_SUFFIX);

            // Reads shader
            string shaderContents = File.ReadAllText(shader.path, Encoding.UTF8);

            shaderContents = shaderContents.Replace("UnityEditor.ShaderGraph.ShaderKeyword", "ShaderControl.SCWindow.SerializedKeyword");
            ShaderGraphProxy graph = JsonUtility.FromJson <ShaderGraphProxy>(shaderContents);

            SCShaderPass currentPass = new SCShaderPass();

            if (graph.m_SerializedKeywords != null)
            {
                for (int k = 0; k < graph.m_SerializedKeywords.Length; k++)
                {
                    SerializedKeywordData skw = graph.m_SerializedKeywords[k];
                    if (string.IsNullOrEmpty(skw.JSONnodeData))
                    {
                        continue;
                    }

                    SerializedKeywordProxy kw = JsonUtility.FromJson <SerializedKeywordProxy>(skw.JSONnodeData);

                    PragmaType pragmaType = PragmaType.Unknown;
                    if (kw.m_KeywordDefinition == SHADER_GRAPH_KEYWORD_DEFINITION_MULTI_COMPILE && kw.m_KeywordScope == SHADER_GRAPH_KEYWORD_SCOPE_GLOBAL)
                    {
                        pragmaType = PragmaType.MultiCompileGlobal;
                    }
                    else if (kw.m_KeywordDefinition == SHADER_GRAPH_KEYWORD_DEFINITION_MULTI_COMPILE && kw.m_KeywordScope == SHADER_GRAPH_KEYWORD_SCOPE_LOCAL)
                    {
                        pragmaType = PragmaType.MultiCompileLocal;
                    }
                    else if (kw.m_KeywordDefinition == SHADER_GRAPH_KEYWORD_DEFINITION_SHADER_FEATURE && kw.m_KeywordScope == SHADER_GRAPH_KEYWORD_SCOPE_GLOBAL)
                    {
                        pragmaType = PragmaType.FeatureGlobal;
                    }
                    else if (kw.m_KeywordDefinition == SHADER_GRAPH_KEYWORD_DEFINITION_SHADER_FEATURE && kw.m_KeywordScope == SHADER_GRAPH_KEYWORD_SCOPE_LOCAL)
                    {
                        pragmaType = PragmaType.FeatureLocal;
                    }

                    SCKeywordLine keywordLine = new SCKeywordLine();
                    keywordLine.pragmaType = pragmaType;

                    SCKeyword keyword = new SCKeyword(kw.m_DefaultReferenceName, kw.m_Name);
                    keywordLine.Add(keyword);
                    currentPass.Add(keywordLine);
                }
            }
            shader.Add(currentPass);
            shader.UpdateVariantCount();
        }
Example #2
0
        void ScanShaderNonGraph(SCShader shader)
        {
            // Reads shader
            string[]      shaderLines   = File.ReadAllLines(shader.path);
            string[]      separator     = new string[] { " " };
            SCShaderPass  currentPass   = new SCShaderPass();
            SCShaderPass  basePass      = null;
            int           pragmaControl = 0;
            int           pass          = -1;
            bool          blockComment  = false;
            SCKeywordLine keywordLine   = new SCKeywordLine();

            for (int k = 0; k < shaderLines.Length; k++)
            {
                string line = shaderLines[k].Trim();
                if (line.Length == 0)
                {
                    continue;
                }

                int lineCommentIndex = line.IndexOf("//");
                int blocCommentIndex = line.IndexOf("/*");
                int endCommentIndex  = line.IndexOf("*/");
                if (blocCommentIndex > 0 && (lineCommentIndex > blocCommentIndex || lineCommentIndex < 0))
                {
                    blockComment = true;
                }
                if (endCommentIndex > blocCommentIndex && (lineCommentIndex > endCommentIndex || lineCommentIndex < 0))
                {
                    blockComment = false;
                }
                if (blockComment)
                {
                    continue;
                }

                string lineUPPER = line.ToUpper();
                if (lineUPPER.Equals("PASS") || lineUPPER.StartsWith("PASS "))
                {
                    if (pass >= 0)
                    {
                        currentPass.pass = pass;
                        if (basePass != null)
                        {
                            currentPass.Add(basePass.keywordLines);
                        }
                        shader.Add(currentPass);
                    }
                    else if (currentPass.keywordCount > 0)
                    {
                        basePass = currentPass;
                    }
                    currentPass = new SCShaderPass();
                    pass++;
                    continue;
                }
                int j = line.IndexOf(PRAGMA_COMMENT_MARK);
                if (j >= 0)
                {
                    pragmaControl = 1;
                }
                else
                {
                    j = line.IndexOf(PRAGMA_DISABLED_MARK);
                    if (j >= 0)
                    {
                        pragmaControl = 3;
                    }
                }
                if (lineCommentIndex == 0 && pragmaControl != 1 && pragmaControl != 3)
                {
                    continue; // do not process lines commented by user
                }

                PragmaType pragmaType = PragmaType.Unknown;
                int        offset     = 0;
                j = line.IndexOf(SCKeywordLine.PRAGMA_MULTICOMPILE_GLOBAL);
                if (j >= 0)
                {
                    pragmaType = PragmaType.MultiCompileGlobal;
                    offset     = SCKeywordLine.PRAGMA_MULTICOMPILE_GLOBAL.Length;
                }
                else
                {
                    j = line.IndexOf(SCKeywordLine.PRAGMA_FEATURE_GLOBAL);
                    if (j >= 0)
                    {
                        pragmaType = PragmaType.FeatureGlobal;
                        offset     = SCKeywordLine.PRAGMA_FEATURE_GLOBAL.Length;
                    }
                    else
                    {
                        j = line.IndexOf(SCKeywordLine.PRAGMA_MULTICOMPILE_LOCAL);
                        if (j >= 0)
                        {
                            pragmaType = PragmaType.MultiCompileLocal;
                            offset     = SCKeywordLine.PRAGMA_MULTICOMPILE_LOCAL.Length;
                        }
                        else
                        {
                            j = line.IndexOf(SCKeywordLine.PRAGMA_FEATURE_LOCAL);
                            if (j >= 0)
                            {
                                pragmaType = PragmaType.FeatureLocal;
                                offset     = SCKeywordLine.PRAGMA_FEATURE_LOCAL.Length;
                            }
                        }
                    }
                }
                if (j >= 0)
                {
                    if (pragmaControl != 2)
                    {
                        keywordLine = new SCKeywordLine();
                    }
                    keywordLine.pragmaType = pragmaType;
                    // exclude potential comments inside the #pragma line
                    int lastStringPos = line.IndexOf("//", j + offset);
                    if (lastStringPos < 0)
                    {
                        lastStringPos = line.Length;
                    }
                    int      length = lastStringPos - j - offset;
                    string[] kk     = line.Substring(j + offset, length).Split(separator, StringSplitOptions.RemoveEmptyEntries);
                    // Sanitize keywords
                    for (int i = 0; i < kk.Length; i++)
                    {
                        kk[i] = kk[i].Trim();
                    }
                    // Act on keywords
                    switch (pragmaControl)
                    {
                    case 1:     // Edited by Shader Control line
                        shader.editedByShaderControl = true;
                        // Add original keywords to current line
                        for (int s = 0; s < kk.Length; s++)
                        {
                            keywordLine.Add(shader.GetKeyword(kk[s]));
                        }
                        pragmaControl = 2;
                        break;

                    case 2:
                        // check enabled keywords
                        keywordLine.DisableKeywords();
                        for (int s = 0; s < kk.Length; s++)
                        {
                            SCKeyword keyword = keywordLine.GetKeyword(kk[s]);
                            if (keyword != null)
                            {
                                keyword.enabled = true;
                            }
                        }
                        currentPass.Add(keywordLine);
                        pragmaControl = 0;
                        break;

                    case 3:     // disabled by Shader Control line
                        shader.editedByShaderControl = true;
                        // Add original keywords to current line
                        for (int s = 0; s < kk.Length; s++)
                        {
                            SCKeyword keyword = shader.GetKeyword(kk[s]);
                            keyword.enabled = false;
                            keywordLine.Add(keyword);
                        }
                        currentPass.Add(keywordLine);
                        pragmaControl = 0;
                        break;

                    case 0:
                        // Add keywords to current line
                        for (int s = 0; s < kk.Length; s++)
                        {
                            keywordLine.Add(shader.GetKeyword(kk[s]));
                        }
                        currentPass.Add(keywordLine);
                        break;
                    }
                }
            }
            currentPass.pass = Mathf.Max(pass, 0);
            if (basePass != null)
            {
                currentPass.Add(basePass.keywordLines);
            }
            shader.Add(currentPass);
            shader.UpdateVariantCount();
        }
Example #3
0
 public void Add(SCShaderPass pass)
 {
     passes.Add(pass);
     UpdateKeywords();
 }