Esempio n. 1
0
        private static void Update(ShaderProgramGL shaderProgram, IEnumerable <NamedStream> namedStreams, bool solutionMode)
        {
            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);
                shaderProgram.Compile(shaderCode, shaderType);
            }
            shaderProgram.Link();
        }
        private static IShaderProgram ShaderProgramImporter(IEnumerable <NamedStream> resources)
        {
            ShaderProgramGL shaderProgram = new ShaderProgramGL();

            Update(shaderProgram, resources);
            return(shaderProgram);
        }
Esempio n. 3
0
        private static IShaderProgram ShaderProgramImporter(IEnumerable <NamedStream> resources, bool solutionMode, IContentLoader contentLoader)
        {
            ShaderProgramGL shaderProgram = new ShaderProgramGL();

            Update(shaderProgram, resources, solutionMode, contentLoader);
            return(shaderProgram);
        }
Esempio n. 4
0
        private static void InitShader()
        {
            const string sVertexShader = @"
				#version 130
				out vec2 uv; 
				void main() 
				{
					const vec2 vertices[4] = vec2[4](vec2(-1.0, -1.0),
						vec2( 1.0, -1.0),
						vec2( 1.0,  1.0),
						vec2(-1.0,  1.0));

					vec2 pos = vertices[gl_VertexID];
					uv = pos * 0.5 + 0.5;
					gl_Position = vec4(pos, 0.0, 1.0);
				}"                ;
            const string sFragmentShd  = @"#version 430 core
				uniform sampler2D image;
				in vec2 uv;
				void main() 
				{
					gl_FragColor = abs(texture(image, uv));
				}"                ;

            shaderProgram = ShaderLoader.CreateFromStrings(sVertexShader, sFragmentShd);
        }
Esempio n. 5
0
        /// <summary>
        /// Creates this instance.
        /// </summary>
        /// <returns></returns>
        public TypedHandle <IShaderProgram> Create()
        {
            var shader = new ShaderProgramGL();

            shaders.Add(shader.ProgramID, shader);
            return(new TypedHandle <IShaderProgram>(shader.ProgramID));
        }
Esempio n. 6
0
        /// <summary>
        /// Reads the contents of a file into a string
        /// </summary>
        /// <param name="shaderFile">path to the shader file</param>
        /// <param name="testCompileInclude">should includes be compiled (for error checking) before being pasted into the including shader</param>
        /// <returns>
        /// string with contents of shaderFile
        /// </returns>
        /// <exception cref="FileNotFoundException">
        /// Could not find shader file '" + shaderFile + "'
        /// or
        /// Could not find include-file '" + sIncludeFileName + "' for shader '" + shaderFile + "'.
        /// </exception>
        public static string ShaderStringFromFileWithIncludes(string shaderFile, bool testCompileInclude)
        {
            string sShader = null;

            if (!File.Exists(shaderFile))
            {
                throw new FileNotFoundException("Could not find shader file '" + shaderFile + "'");
            }
            sShader = File.ReadAllText(shaderFile);

            //handle includes
            string sCurrentPath = Path.GetDirectoryName(shaderFile) + Path.DirectorySeparatorChar; // get path to current shader
            string sName        = Path.GetFileName(shaderFile);
            //split into lines
            var lines   = sShader.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            var pattern = @"^\s*#include\s+""([^""]+)"""; //match everything inside " except " so we get shortest ".+" match
            int lineNr  = 1;

            foreach (var line in lines)
            {
                // Search for include pattern (e.g. #include raycast.glsl) (nested not supported)
                foreach (Match match in Regex.Matches(line, pattern, RegexOptions.Singleline))
                {
                    string sFullMatch       = match.Value;
                    string sIncludeFileName = match.Groups[1].ToString();      // get the filename to include
                    string sIncludePath     = sCurrentPath + sIncludeFileName; // build path to file

                    if (!File.Exists(sIncludePath))
                    {
                        throw new FileNotFoundException("Could not find include-file '" + sIncludeFileName + "' for shader '" + shaderFile + "'.");
                    }
                    string sIncludeShd = File.ReadAllText(sIncludePath); // read include as string
                    if (testCompileInclude)
                    {
                        using (var shader = new ShaderProgramGL())
                        {
                            try
                            {
                                shader.Compile(sIncludeShd, ShaderType.FragmentShader); //test compile include shader
                            }
                            catch (ShaderCompileException e)
                            {
                                var ce = new ShaderCompileException(e.ShaderType,
                                                                    "include compile '" + sIncludePath + "'",
                                                                    e.ShaderLog, sIncludeShd);
                                ce.Data.Add(ExceptionDataFileName, sIncludePath);
                                throw ce;
                            }
                        }
                    }
                    sIncludeShd += Environment.NewLine + "#line " + lineNr.ToString() + Environment.NewLine;
                    sShader      = sShader.Replace(sFullMatch, sIncludeShd); // replace #include with actual include
                }
                ++lineNr;
            }
            return(sShader);
        }
        private static void Update(ShaderProgramGL shaderProgram, IEnumerable <NamedStream> namedStreams)
        {
            var count = namedStreams.Count();

            if (2 > count)
            {
                return;
            }
            foreach (var res in namedStreams)
            {
                var shaderType = GetShaderTypeFromExtension(Path.GetExtension(res.Name));
                shaderProgram.Compile(ShaderCode(res.Stream), shaderType);
            }
            shaderProgram.Link();
        }
Esempio n. 8
0
        /// <summary>
        /// Compiles and links vertex and fragment shaders from strings.
        /// </summary>
        /// <param name="sVertexShd_">The vertex shader source code string.</param>
        /// <param name="sFragmentShd_">The fragment shader source code string.</param>
        /// <returns>
        /// a new instance
        /// </returns>
        public static ShaderProgramGL FromStrings(string sVertexShd_, string sFragmentShd_)
        {
            ShaderProgramGL shd = new ShaderProgramGL();

            try
            {
                shd.FromStrings(sVertexShd_, sFragmentShd_);
            }
            catch (Exception e)
            {
                shd.Dispose();
                throw e;
            }
            return(shd);
        }
Esempio n. 9
0
        /// <summary>
        /// Creates from strings.
        /// </summary>
        /// <param name="vertexShaderCode">The vertex shader code.</param>
        /// <param name="fragmentShaderCode">The fragment shader code.</param>
        /// <returns></returns>
        public static ShaderProgramGL CreateFromStrings(string vertexShaderCode, string fragmentShaderCode)
        {
            var vertexShader   = CreateShader(ShaderType.VertexShader, vertexShaderCode);
            var fragmentShader = CreateShader(ShaderType.FragmentShader, fragmentShaderCode);
            var shaderProgram  = new ShaderProgramGL();

            shaderProgram.Attach(vertexShader);
            shaderProgram.Attach(fragmentShader);
            if (!shaderProgram.Link())
            {
                var e = new ShaderLinkException(shaderProgram.Log);
                shaderProgram.Dispose();
                throw e;
            }
            return(shaderProgram);
        }
Esempio n. 10
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);
            }
        }