Example #1
0
        /// <summary>
        /// Compiles Shaders related to this effect.
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public static List <Shader> CompileAll(Device device, ShaderLibrary library)
        {
            List <Shader> shaders = new List <Shader>();

            foreach (Program program in library.Programs)
            {
                Shader shader = new Shader(device);

                if (program.VertexShader != null)
                {
                    foreach (Source source in from n in program.VertexShader.Includes
                             from m in library.Sources
                             where  n.Source == m.ID
                             select m)
                    {
                        shader.AppendVertexSource(source.Code);
                    }
                }

                if (program.GeometryShader != null)
                {
                    shader.InputTopology     = program.GeometryShader.InputTopology;
                    shader.OutputTopology    = program.GeometryShader.OutputTopology;
                    shader.OutputVertexCount = program.GeometryShader.OutputVertexCount;
                    foreach (Source source in from n in program.GeometryShader.Includes
                             from m in library.Sources
                             where  n.Source == m.ID
                             select m)
                    {
                        shader.AppendGeometrySource(source.Code);
                    }
                }

                if (program.FragmentShader != null)
                {
                    foreach (Source source in from n in program.FragmentShader.Includes
                             from m in library.Sources
                             where  n.Source == m.ID
                             select m)
                    {
                        shader.AppendFragmentSource(source.Code);
                    }
                }


                ShaderCompileResult compileResult = shader.Compile();

                if (!compileResult.HasError)
                {
                    shaders.Add(shader);
                }
                else
                {
                    throw new Exception(compileResult.Summary);
                }
            }

            return(shaders);
        }
Example #2
0
        /// <summary>
        /// Compiles all Shader Source code into a workable program. Will only
        /// compile a Shader Type, Vertex, Fragment or Geometry if the Source for
        /// each program is greater than 0 in length.
        /// </summary>
        /// <returns></returns>
        public ShaderCompileResult Compile()
        {
            ShaderCompileResult compileResult = new ShaderCompileResult();

            // Delete the Vertex Shader
            this.DestroyVertexShader();

            // Delete the Fragment Shader
            this.DestroyFragmentShader();

            // Delete the Geometry Shader
            this.DestroyGeometryShader();

            // Compile Vertex Source
            if (this.vertexSource.Length > 0)
            {
                string error = string.Empty;

                if (!this.CompileVertexSource(out error))
                {
                    compileResult.HasError = true;

                    compileResult.VertexShaderError = error;
                }
            }
            // Compile Fragment Source
            if (this.fragmentSource.Length > 0)
            {
                string error = string.Empty;

                if (!this.CompileFragmentSource(out error))
                {
                    compileResult.HasError = true;

                    compileResult.FragmentShaderError = error;
                }
            }

            // Compile Geometry Source
            if (this.geometrySource.Length > 0)
            {
                string error = string.Empty;

                if (!this.CompileGeometrySource(out error))
                {
                    compileResult.HasError = true;

                    compileResult.GeometryShaderError = error;
                }
            }

            // Exit on failer to compile shaders
            if (compileResult.HasError)
            {
                return(compileResult);
            }

            if (this.vertexHandle != 0)
            {
                Gl.glAttachObjectARB(this.programHandle, this.vertexHandle);
            }
            if (this.geometryHandle != 0)
            {
                Gl.glAttachObjectARB(this.programHandle, this.geometryHandle);
            }
            if (this.fragmentHandle != 0)
            {
                Gl.glAttachObjectARB(this.programHandle, this.fragmentHandle);
            }

            Gl.glLinkProgramARB(this.programHandle);

            #region Error Handling

            int result;

            Gl.glGetObjectParameterivARB(this.programHandle, Gl.GL_OBJECT_LINK_STATUS_ARB, out result);

            if (result == 0)
            {
                int infologlength;

                int infolength;

                Gl.glGetShaderiv(this.programHandle, Gl.GL_INFO_LOG_LENGTH, out infologlength);

                string error = string.Empty;

                Gl.glGetInfoLogARB(this.programHandle, 8096, out infolength, out error);

                compileResult.HasError = true;

                compileResult.LinkerError = error;

                return(compileResult);
            }

            #endregion

            return(compileResult);
        }
Example #3
0
        /// <summary>
        /// Compiles all Shader Source code into a workable program. Will only
        /// compile a Shader Type, Vertex, Fragment or Geometry if the Source for
        /// each program is greater than 0 in length. 
        /// </summary>
        /// <returns></returns>
        public ShaderCompileResult Compile()
        {
            ShaderCompileResult compileResult = new ShaderCompileResult();

            // Delete the Vertex Shader
            this.DestroyVertexShader   ();

            // Delete the Fragment Shader
            this.DestroyFragmentShader ();
            
            // Delete the Geometry Shader
            this.DestroyGeometryShader ();

            // Compile Vertex Source
            if(this.vertexSource.Length > 0) 
            {
                string error = string.Empty;

                if (!this.CompileVertexSource(out error))
                {
                    compileResult.HasError = true;

                    compileResult.VertexShaderError = error;
                }
            }
            // Compile Fragment Source
            if(this.fragmentSource.Length > 0) 
            {
                string error = string.Empty;

                if (!this.CompileFragmentSource(out error))
                {
                    compileResult.HasError = true;

                    compileResult.FragmentShaderError = error;
                }
            }
             
            // Compile Geometry Source
            if(this.geometrySource.Length > 0) 
            {
                string error = string.Empty;

                if (!this.CompileGeometrySource(out error))
                {
                    compileResult.HasError = true;

                    compileResult.GeometryShaderError = error;
                }
            }

            // Exit on failer to compile shaders
            if(compileResult.HasError)
            {
                return compileResult;
            }

            if (this.vertexHandle   != 0)
            {
                Gl.glAttachObjectARB(this.programHandle, this.vertexHandle);
            }
            if (this.geometryHandle != 0)
            {
                Gl.glAttachObjectARB(this.programHandle, this.geometryHandle);
            }
            if (this.fragmentHandle != 0)
            {
                Gl.glAttachObjectARB(this.programHandle, this.fragmentHandle);
            }

            Gl.glLinkProgramARB(this.programHandle);

            #region Error Handling

            int result;

            Gl.glGetObjectParameterivARB(this.programHandle, Gl.GL_OBJECT_LINK_STATUS_ARB, out result);

            if (result == 0)
            {
                int infologlength;

                int infolength;

                Gl.glGetShaderiv(this.programHandle, Gl.GL_INFO_LOG_LENGTH, out infologlength);

                string error = string.Empty;

                Gl.glGetInfoLogARB(this.programHandle, 8096, out infolength, out error);

                compileResult.HasError    = true;

                compileResult.LinkerError = error;

                return compileResult;
            }

            #endregion

            return compileResult;
             
        }