Shader

GLSL Effect wrapper Object.

Exemple #1
0
        public void SetupControl()
        {
            this.Device = new WindowsDevice(this.Handle, Presentation.Default);

            this.VertexBuffer = new VertexBuffer(this.Device, typeof(Vertex), BufferUsage.Dynamic);

            this.VertexBuffer.SetData<Vertex>(new Vertex[] {

                new Vertex { Position = new Vector4(0.0f, 0.0f, 0.0f, 1.0f) , Normal = Vector3.Forward, Texcoord = Vector2.Zero },

                new Vertex { Position = new Vector4(0.0f, 10.0f, 0.0f, 1.0f), Normal = Vector3.Forward, Texcoord = Vector2.Zero },

                new Vertex { Position = new Vector4(10.0f, 10.0f, 0.0f, 1.0f), Normal = Vector3.Forward, Texcoord = Vector2.Zero },
            });


            this.IndexBuffer = new IndexBuffer(this.Device, BufferUsage.Dynamic);

            this.IndexBuffer.SetData(new uint[] {0, 1, 2 });

            

            this.Shader = new Shader(this.Device);

            this.Shader.VertexSource = @"

                uniform mat4 world;

                uniform mat4 view;

                uniform mat4 projection;

                attribute vec4 gl_Vertex;

                attribute vec3 gl_Normal;

                attribute vec2 gl_MultiTexCoord0;
                
                void main()
                {
                    gl_Position = mul(world, mul(view, projection)) * gl_Vertex;
                }            
    
            ";

            this.Shader.FragmentSource = @"
                
                void main()
                {
                    gl_FragColor = vec4(1, 1, 1, 1);
                }            
    
            ";

            var result = this.Shader.Compile(); 

            if (result.HasError)
            {
                MessageBox.Show(result.Summary);
            }
        }
        /// <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;
        }