コード例 #1
0
        /// <summary>
        /// Compiles the specified shader source code.
        /// </summary>
        /// <param name="shaderSourceCode">The shader source code.</param>
        /// <param name="type">The type.</param>
        /// <exception cref="ShaderCompileException"></exception>
        public void Compile(string shaderSourceCode, ShaderType type)
        {
            var shader = new ShaderGL(type);

            if (!shader.Compile(shaderSourceCode))
            {
                var e = new ShaderCompileException(type, shader.Log, shaderSourceCode);
                shader.Dispose();
                throw e;
            }
            Attach(shader);
        }
コード例 #2
0
        private static ShaderGL CreateShader(ShaderType shaderType, string shaderCode)
        {
            var shader = new ShaderGL(shaderType);

            if (!shader.Compile(shaderCode))
            {
                var e = shader.CreateException(shaderCode);
                shader.Dispose();
                throw e;
            }
            return(shader);
        }
コード例 #3
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 = ShaderLoader.ResolveIncludes(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);
            }
        }