Ejemplo n.º 1
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(string.Join("\n", lines));
            }

            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(Transformations.ExpandIncludes(shaderCode, GetIncludeCode));
        }
Ejemplo n.º 2
0
        private static void Update(ShaderProgramGL shaderProgram, IEnumerable <NamedStream> namedStreams, bool solutionMode, IContentLoader contentLoader)
        {
            string ShaderCode(Stream stream)
            {
                using (var reader = new StreamReader(stream, true))
                {
                    var code = reader.ReadToEnd();
                    if (solutionMode)
                    {
                        code = code.Replace("#ifdef SOLUTION", "#if 1");
                    }
                    return(code);
                }
            }

            var count = namedStreams.Count();

            if (2 > count)
            {
                return;
            }
            foreach (var res in namedStreams)
            {
                var shaderType = GetShaderTypeFromExtension(Path.GetExtension(res.Name));
                var shaderCode = ShaderCode(res.Stream);
                string GetIncludeCode(string includeName)
                {
                    var resourceName = includeName.Replace(Path.DirectorySeparatorChar, '.');

                    resourceName = resourceName.Replace(Path.AltDirectorySeparatorChar, '.');
                    var includeCode = contentLoader.Load <StringBuilder>(resourceName).ToString();

                    using (var includeShader = new ShaderGL(shaderType))
                    {
                        if (!includeShader.Compile(includeCode))
                        {
                            var e = includeShader.CreateException(includeCode);
                            throw new NamedShaderException(contentLoader.Names.GetFullName(includeName), e);
                        }
                    }
                    return(includeCode);
                }

                var expandedShaderCode = Transformations.ExpandIncludes(shaderCode, GetIncludeCode);
                var shader             = new ShaderGL(shaderType);
                if (shader.Compile(expandedShaderCode))
                {
                    shaderProgram.Attach(shader);
                }
                else
                {
                    shaderProgram.RemoveShaders();                     //clean out whole shader program to avoid double attachments next time
                    var e = shader.CreateException(shaderCode);
                    shader.Dispose();
                    throw new NamedShaderException(res.Name, e);
                }
            }
            if (!shaderProgram.Link())
            {
                var e = new ShaderLinkException(shaderProgram.Log);
                var s = new StringBuilder();
                foreach (var ns in namedStreams)
                {
                    s.Append(ns.Name);
                    s.Append(';');
                }
                throw new NamedShaderException(s.ToString(), e);
            }
        }