Example #1
0
        private static string Compile(string shaderCode, ShaderType shaderType, string shaderFileDir)
        {
            string GetIncludeCode(string includeName)
            {
                var includeFileName = Path.Combine(shaderFileDir, includeName);

                if (File.Exists(includeFileName))
                {
                    return(File.ReadAllText(includeFileName));
                }
                return($"#error include file '{includeName}' not found");
            }

            try
            {
                using (var shader = new ShaderGL(shaderType))
                {
                    var expandedCode = ShaderLoader.ResolveIncludes(shaderCode, GetIncludeCode);
                    shader.Compile(expandedCode);
                    return(shader.Log);
                }
            }
            catch (AccessViolationException)
            {
                return($"(1 1):ERROR: OpenGL shader compiler has crashed");
            }
        }
Example #2
0
        private static string ExpandedCode(string shaderCode, string shaderFileDir)
        {
            string SpecialCommentReplacement(string code, string specialComment)
            {
                var lines = code.Split(new[] { '\n' }, StringSplitOptions.None);                 //if UNIX style line endings still working so do not use Envirnoment.NewLine

                for (int i = 0; i < lines.Length; ++i)
                {
                    var index = lines[i].IndexOf(specialComment);                     // search for special comment
                    if (-1 != index)
                    {
                        lines[i] = lines[i].Substring(index + specialComment.Length);                         // remove everything before special comment
                    }
                }
                return(lines.Combine("\n"));
            }

            string GetIncludeCode(string includeName)
            {
                var includeFileName = Path.Combine(shaderFileDir, includeName);

                if (File.Exists(includeFileName))
                {
                    var includeCode = File.ReadAllText(includeFileName);
                    includeCode = SpecialCommentReplacement(includeCode, "//!");
                    return(includeCode);
                }
                return($"#error include file '{includeName}' not found");
            }

            shaderCode = SpecialCommentReplacement(shaderCode, "//!");
            shaderCode = SpecialCommentReplacement(shaderCode, "//?");
            return(ShaderLoader.ResolveIncludes(shaderCode, GetIncludeCode));
        }
Example #3
0
        private static string Compile(string shaderCode, ShaderType shaderType, string shaderFileDir)
        {
            string SpecialCommentReplacement(string code, string specialComment)
            {
                var lines = code.Split(new[] { '\n' }, StringSplitOptions.None);                 //if UNIX style line endings still working so do not use Envirnoment.NewLine

                for (int i = 0; i < lines.Length; ++i)
                {
                    var index = lines[i].IndexOf(specialComment);                     // search for special comment
                    if (-1 != index)
                    {
                        lines[i] = lines[i].Substring(index + specialComment.Length);                         // remove everything before special comment
                    }
                }
                return(lines.Combine("\n"));
            }

            string GetIncludeCode(string includeName)
            {
                var includeFileName = Path.Combine(shaderFileDir, includeName);

                if (File.Exists(includeFileName))
                {
                    var includeCode = File.ReadAllText(includeFileName);
                    includeCode = SpecialCommentReplacement(includeCode, "//!");
                    return(includeCode);
                }
                return($"#error include file '{includeName}' not found");
            }

            try
            {
                using (var shader = new ShaderGL(shaderType))
                {
                    shaderCode = SpecialCommentReplacement(shaderCode, "//!");
                    shaderCode = SpecialCommentReplacement(shaderCode, "//?");
                    var expandedCode = ShaderLoader.ResolveIncludes(shaderCode, GetIncludeCode);
                    shader.Compile(expandedCode);
                    return(shader.Log);
                }
            }
            catch (AccessViolationException)
            {
                return($"(1 1):ERROR: OpenGL shader compiler has crashed");
            }
        }