Example #1
0
        public override void Activate()
        {
            if (linked == 0 && !triedToLinkAndFailed)
            {
                GL.GetError();

                glProgramHandle = GL.CreateProgram();
                GLES2Config.GlCheckError(this);
#if !AXIOM_NO_GLES2_GLSL_OPTIMIZER
                if (vertexProgram != null)
                {
                    string paramStr = vertexProgram.GLSLProgram.GetParameter["use_optimiser"];
                    if (paramStr == "true" || paramStr.Length == 0)
                    {
                        GLSLESLinkProgramManager.Instance.OptimizeShaderSource(vertexProgram);
                    }
                }
                if (vertexProgram != null)
                {
                    string paramStr = fragmentProgram.GLSLProgram.GetParameter("use_optimiser");
                    if (paramStr == "true" || paramStr.Length == 0)
                    {
                        GLSLESLinkProgramManager.Instance.OptimizeShaderSource(fragmentProgram);
                    }
                }
#endif
                this.CompileAndLink();

                this.ExtractLayoutQualifiers();
                this.BuildGLUniformReferences();
            }
            this._useProgram();
        }
Example #2
0
        public bool Compile(bool checkErrors)
        {
            if (this.compiled == 1)
            {
                return(true);
            }

            //ONly creaet a shader object if glsl es is supported
            if (IsSupported)
            {
                //Create shader object
                GLenum shaderType = GLenum.None;
                if (type == GpuProgramType.Vertex)
                {
                    shaderType = GLenum.VertexShader;
                }
                else
                {
                    shaderType = GLenum.FragmentShader;
                }
                this.glShaderHandle = GL.CreateShader(shaderType);
                GLES2Config.GlCheckError(this);

                //TODO : Root.Instance.RenderSystem.Capabilities.HasCapability(Capabilities.SeperateShaderObjects))
                if (false)
                {
                    this.glProgramHandle = GL.CreateProgram();
                    GLES2Config.GlCheckError(this);
                }
            }

            //Add preprocessor extras and main source
            if (source.Length > 0)
            {
                var sourceArray = new string[] { source };
                int r           = 0;
                GL.ShaderSource(this.glShaderHandle, 1, sourceArray, ref r);
                GLES2Config.GlCheckError(this);
            }
            if (checkErrors)
            {
                LogManager.Instance.Write("GLSL ES compiling: " + Name);
            }
            GL.CompileShader(this.glShaderHandle);
            GLES2Config.GlCheckError(this);

            //check for compile errors
            GL.GetShader(this.glShaderHandle, GLenum.CompileStatus, ref this.compiled);
            GLES2Config.GlCheckError(this);
            if (this.compiled == 0 && checkErrors)
            {
                string message = "GLSL ES compile log: " + Name;
                this.CheckAndFixInvalidDefaultPrecisionError(message);
            }

            //Log a message that the shader compiled successfully.
            if (this.compiled == 1 && checkErrors)
            {
                LogManager.Instance.Write("GLSL ES compiled: " + Name);
            }

            return(this.compiled == 1);
        }
Example #3
0
        /// <summary>
        ///Initialize shaders and program on OpenGLES2.0
        /// </summary>
        private void InitGL20()
        {
            string vertexShaderSrc = @"uniform mat4 uMVPMatrix;
                                                    attribute vec4 aPosition; 
                                                    attribute vec2 aTexCoord;
                                                    attribute vec4 aTint;
                                                    varying vec2 vTexCoord;
                                                    varying vec4 vTint;
                                                    void main()                  
                                                    {                         
                                                       vTexCoord = aTexCoord;
                                                       vTint = aTint;
                                                       gl_Position = uMVPMatrix * aPosition; 
                                                    }";


            string fragmentShaderSrc = @"precision mediump float;

                                         varying vec2 vTexCoord;
										 varying vec4 vTint;
                                         uniform sampler2D sTexture;
                                           void main()                                
                                           {                                         
                                             vec4 baseColor = texture2D(sTexture, vTexCoord);
											 gl_FragColor = baseColor * vTint;

                                           }";

            int vertexShader   = LoadShader(All20.VertexShader, vertexShaderSrc);
            int fragmentShader = LoadShader(All20.FragmentShader, fragmentShaderSrc);

            program = GL20.CreateProgram();
            if (program == 0)
            {
                throw new InvalidOperationException("Unable to create program");
            }

            GL20.AttachShader(program, vertexShader);
            GL20.AttachShader(program, fragmentShader);

            //Set position
            GL20.BindAttribLocation(program, _batcher.attributePosition, "aPosition");
            GL20.BindAttribLocation(program, _batcher.attributeTexCoord, "aTexCoord");
            GL20.BindAttribLocation(program, _batcher.attributeTint, "aTint");

            GL20.LinkProgram(program);

            int linked = 0;

            GL20.GetProgram(program, All20.LinkStatus, ref linked);
            if (linked == 0)
            {
                // link failed
                int length = 0;
                GL20.GetProgram(program, All20.InfoLogLength, ref length);
                if (length > 0)
                {
                    var log = new StringBuilder(length);
                    GL20.GetProgramInfoLog(program, length, ref length, log);
                    Console.WriteLine("GL2" + log.ToString());
                }

                GL20.DeleteProgram(program);
                throw new InvalidOperationException("Unable to link program");
            }


            matWorld      = Matrix4.Identity;
            matViewScreen = Matrix4.CreateRotationZ((float)Math.PI) *
                            Matrix4.CreateRotationY((float)Math.PI) *
                            Matrix4.CreateTranslation(-this.graphicsDevice.Viewport.Width / 2,
                                                      this.graphicsDevice.Viewport.Height / 2,
                                                      1);
            matViewFramebuffer = Matrix4.CreateTranslation(-this.graphicsDevice.Viewport.Width / 2,
                                                           -this.graphicsDevice.Viewport.Height / 2,
                                                           1);
            matProjection = Matrix4.CreateOrthographic(this.graphicsDevice.Viewport.Width,
                                                       this.graphicsDevice.Viewport.Height,
                                                       -1f, 1f);

            matWVPScreen      = matWorld * matViewScreen * matProjection;
            matWVPFramebuffer = matWorld * matViewFramebuffer * matProjection;

            GetUniformVariables();
        }
Example #4
0
        /// <summary>
        ///Initialize shaders and program on OpenGLES2.0
        /// </summary>
        private void InitGL20()
        {
            string vertexShaderSrc = @" uniform mat4 uMVPMatrix;
											attribute vec4 aPosition;
											attribute vec2 aTexCoord;
											attribute vec4 aTint;
											varying vec2 vTexCoord;
											varying vec4 vTint;
											void main()
											{
												vTexCoord = aTexCoord;
												vTint = aTint;
												gl_Position = uMVPMatrix * aPosition;
											}"                                            ;

            string fragmentShaderSrc = @"precision mediump float;
											varying vec2 vTexCoord;
											varying vec4 vTint;
											uniform sampler2D sTexture;
											void main()
											{
												vec4 baseColor = texture2D(sTexture, vTexCoord);
												gl_FragColor = baseColor * vTint;
											}"                                            ;

            int vertexShader   = LoadShader(ALL20.VertexShader, vertexShaderSrc);
            int fragmentShader = LoadShader(ALL20.FragmentShader, fragmentShaderSrc);

            program = GL20.CreateProgram();

            if (program == 0)
            {
                throw new InvalidOperationException("Unable to create program");
            }

            GL20.AttachShader(program, vertexShader);
            GL20.AttachShader(program, fragmentShader);

            //Set position
            GL20.BindAttribLocation(program, _batcher.attributePosition, "aPosition");
            GL20.BindAttribLocation(program, _batcher.attributeTexCoord, "aTexCoord");
            GL20.BindAttribLocation(program, _batcher.attributeTint, "aTint");

            GL20.LinkProgram(program);

            int linked = 0;

            GL20.GetProgram(program, ALL20.LinkStatus, ref linked);
            if (linked == 0)
            {
                // link failed
                int length = 0;
                GL20.GetProgram(program, ALL20.InfoLogLength, ref length);
                if (length > 0)
                {
                    var log = new StringBuilder(length);
                    GL20.GetProgramInfoLog(program, length, ref length, log);
#if DEBUG
                    //Console.WriteLine ("GL2.0 error: " + log.ToString ());
#endif
                }

                GL20.DeleteProgram(program);
                throw new InvalidOperationException("Unable to link program");
            }

            UpdateWorldMatrixOrientation();
            GetUniformVariables();
        }