[Mesa-dev] [PATCH 3/6] meta: Add functionality to do _mesa_meta_BlitFrameBuffer() using glsl

Anuj Phogat anuj.phogat at gmail.com
Mon Dec 17 07:27:37 PST 2012


On Wed, Dec 12, 2012 at 3:25 PM, Anuj Phogat <anuj.phogat at gmail.com> wrote:
> This is required by glBlitFrameBuffer() in gles3. This patch, along with
> other patches in this series, make failing framebuffer_blit test cases in
> gles3 conformancen pass.
>
I noticed few bugs in my glsl implementation of glBlitFrameBuffer().
I'll soon post the
updated patch.
> Signed-off-by: Anuj Phogat <anuj.phogat at gmail.com>
> ---
>  src/mesa/drivers/common/meta.c |  313 +++++++++++++++++++++++++++++++---------
>  1 files changed, 248 insertions(+), 65 deletions(-)
>
> diff --git a/src/mesa/drivers/common/meta.c b/src/mesa/drivers/common/meta.c
> index f95d207..c792b44 100644
> --- a/src/mesa/drivers/common/meta.c
> +++ b/src/mesa/drivers/common/meta.c
> @@ -222,6 +222,7 @@ struct blit_state
>     GLuint ArrayObj;
>     GLuint VBO;
>     GLuint DepthFP;
> +   GLuint ShaderProg;
>  };
>
>
> @@ -1145,7 +1146,7 @@ static void
>  init_temp_texture(struct gl_context *ctx, struct temp_texture *tex)
>  {
>     /* prefer texture rectangle */
> -   if (ctx->Extensions.NV_texture_rectangle) {
> +   if (_mesa_is_desktop_gl(ctx) && ctx->Extensions.NV_texture_rectangle) {
>        tex->Target = GL_TEXTURE_RECTANGLE;
>        tex->MaxSize = ctx->Const.MaxTextureRectSize;
>        tex->NPOT = GL_TRUE;
> @@ -1404,11 +1405,13 @@ blitframebuffer_texture(struct gl_context *ctx,
>                          GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
>                          GLbitfield mask, GLenum filter)
>  {
> +   const GLint dstW = abs(dstX1 - dstX0);
> +   const GLint dstH = abs(dstY1 - dstY0);
> +
>     if (mask & GL_COLOR_BUFFER_BIT) {
>        const struct gl_framebuffer *drawFb = ctx->DrawBuffer;
>        const struct gl_framebuffer *readFb = ctx->ReadBuffer;
> -      const struct gl_renderbuffer_attachment *drawAtt =
> -         &drawFb->Attachment[drawFb->_ColorDrawBufferIndexes[0]];
> +      const struct gl_renderbuffer_attachment *drawAtt;
>        const struct gl_renderbuffer_attachment *readAtt =
>           &readFb->Attachment[readFb->_ColorReadBufferIndex];
>
> @@ -1422,12 +1425,23 @@ blitframebuffer_texture(struct gl_context *ctx,
>              ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler ?
>              ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler->Name : 0;
>
> -         if (drawAtt->Texture == readAtt->Texture) {
> -            /* Can't use same texture as both the source and dest.  We need
> -             * to handle overlapping blits and besides, some hw may not
> -             * support this.
> -             */
> -            return mask;
> +         /* Iterate through all draw buffers */
> +         for (int i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
> +            int idx = ctx->DrawBuffer->_ColorDrawBufferIndexes[i];
> +            if (idx != -1) {
> +               drawAtt = &drawFb->Attachment[idx];
> +            }
> +            else {
> +               continue;
> +            }
> +
> +            if (drawAtt->Texture == readAtt->Texture) {
> +               /* Can't use same texture as both the source and dest.  We need
> +                * to handle overlapping blits and besides, some hw may not
> +                * support this.
> +                */
> +               return mask;
> +            }
>           }
>
>           if (target != GL_TEXTURE_2D && target != GL_TEXTURE_RECTANGLE_ARB) {
> @@ -1455,17 +1469,18 @@ blitframebuffer_texture(struct gl_context *ctx,
>           _mesa_SamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
>           _mesa_SamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
>
> -        /* Always do our blits with no sRGB decode or encode.  Note that
> +         /* Always do our blits with no sRGB decode or encode.  Note that
>            * GL_FRAMEBUFFER_SRGB has already been disabled by
>            * _mesa_meta_begin().
>            */
> -        if (ctx->Extensions.EXT_texture_sRGB_decode) {
> -           _mesa_SamplerParameteri(sampler, GL_TEXTURE_SRGB_DECODE_EXT,
> -                               GL_SKIP_DECODE_EXT);
> -        }
> +         if (ctx->Extensions.EXT_texture_sRGB_decode) {
> +            _mesa_SamplerParameteri(sampler, GL_TEXTURE_SRGB_DECODE_EXT,
> +                                    GL_SKIP_DECODE_EXT);
> +         }
>
>           _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
> -         _mesa_set_enable(ctx, target, GL_TRUE);
> +         if (_mesa_is_desktop_gl(ctx) || ctx->API == API_OPENGLES)
> +            _mesa_set_enable(ctx, target, GL_TRUE);
>
>           /* Prepare vertex data (the VBO was previously created and bound) */
>           {
> @@ -1491,14 +1506,15 @@ blitframebuffer_texture(struct gl_context *ctx,
>                 t1 = srcY1;
>              }
>
> -            verts[0].x = (GLfloat) dstX0;
> -            verts[0].y = (GLfloat) dstY0;
> -            verts[1].x = (GLfloat) dstX1;
> -            verts[1].y = (GLfloat) dstY0;
> -            verts[2].x = (GLfloat) dstX1;
> -            verts[2].y = (GLfloat) dstY1;
> -            verts[3].x = (GLfloat) dstX0;
> -            verts[3].y = (GLfloat) dstY1;
> +            /* setup vertex positions */
> +            verts[0].x = -1.0F;
> +            verts[0].y = -1.0F;
> +            verts[1].x =  1.0F;
> +            verts[1].y = -1.0F;
> +            verts[2].x =  1.0F;
> +            verts[2].y =  1.0F;
> +            verts[3].x = -1.0F;
> +            verts[3].y =  1.0F;
>
>              verts[0].s = s0;
>              verts[0].t = t0;
> @@ -1512,6 +1528,10 @@ blitframebuffer_texture(struct gl_context *ctx,
>              _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
>           }
>
> +         /* setup viewport */
> +         _mesa_set_viewport(ctx, dstX0, dstY0, dstW, dstH);
> +         _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
> +         _mesa_DepthMask(GL_FALSE);
>           _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
>
>           /* Restore texture object state, the texture binding will
> @@ -1533,6 +1553,145 @@ blitframebuffer_texture(struct gl_context *ctx,
>     return mask;
>  }
>
> +static void
> +setup_ff_blit_framebuffer(struct gl_context *ctx,
> +                          struct blit_state *blit)
> +{
> +   struct vertex {
> +      GLfloat x, y, s, t;
> +   };
> +   struct vertex verts[4];
> +
> +   if (blit->ArrayObj == 0) {
> +      /* one-time setup */
> +
> +      /* create vertex array object */
> +      _mesa_GenVertexArrays(1, &blit->ArrayObj);
> +      _mesa_BindVertexArray(blit->ArrayObj);
> +
> +      /* create vertex array buffer */
> +      _mesa_GenBuffers(1, &blit->VBO);
> +      _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, blit->VBO);
> +      _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
> +                          NULL, GL_DYNAMIC_DRAW_ARB);
> +
> +      /* setup vertex arrays */
> +      _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
> +      _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
> +      _mesa_EnableClientState(GL_VERTEX_ARRAY);
> +      _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
> +   }
> +
> +   /* setup projection matrix */
> +   _mesa_MatrixMode(GL_PROJECTION);
> +   _mesa_LoadIdentity();
> +   _mesa_Ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
> +
> +}
> +
> +static void
> +setup_glsl_blit_framebuffer(struct gl_context *ctx,
> +                            struct blit_state *blit)
> +{
> +   struct vertex {
> +      GLfloat x, y, s, t;
> +   };
> +   struct vertex verts[4];
> +   const char *vs_source;
> +   char *fs_source;
> +   GLuint vs, fs;
> +   void *mem_ctx;
> +
> +   /* Check if already initialized */
> +   if (blit->ArrayObj == 0) {
> +
> +      /* create vertex array object */
> +      _mesa_GenVertexArrays(1, &blit->ArrayObj);
> +      _mesa_BindVertexArray(blit->ArrayObj);
> +
> +      /* create vertex array buffer */
> +      _mesa_GenBuffers(1, &blit->VBO);
> +      _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, blit->VBO);
> +      _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
> +                          NULL, GL_DYNAMIC_DRAW_ARB);
> +
> +      /* setup vertex arrays */
> +      _mesa_VertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE,
> +                                   sizeof(struct vertex), OFFSET(x));
> +      _mesa_VertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
> +                                   sizeof(struct vertex), OFFSET(s));
> +   }
> +
> +   /* Generate a fragment shader program appropriate for the texture target */
> +   if (blit->ShaderProg != 0) {
> +      return;
> +   }
> +
> +   mem_ctx = ralloc_context(NULL);
> +
> +   if ((ctx->API == API_OPENGLES2 && ctx->Version < 30) ||
> +       ctx->Const.GLSLVersion < 130) {
> +      vs_source =
> +         "attribute vec2 position;\n"
> +         "attribute vec2 textureCoords;\n"
> +         "varying vec2 texCoords;\n"
> +         "void main()\n"
> +         "{\n"
> +         "   texCoords = textureCoords;\n"
> +         "   gl_Position = vec4(position, 0.0, 1.0);\n"
> +         "}\n";
> +
> +      fs_source = ralloc_asprintf(mem_ctx,
> +                                  "#extension GL_EXT_texture_array : enable\n"
> +                                  "uniform sampler2D texSampler;\n"
> +                                  "varying vec2 texCoords;\n"
> +                                  "void main()\n"
> +                                  "{\n"
> +                                  "   gl_FragColor = texture2D(texSampler, texCoords);\n"
> +                                  "   gl_FragDepth = gl_FragColor.r;\n"
> +                                  "}\n");
> +   }
> +   else {
> +      vs_source = ralloc_asprintf(mem_ctx,
> +                                  "#version %s\n"
> +                                  "in vec2 position;\n"
> +                                  "in vec2 textureCoords;\n"
> +                                  "out vec2 texCoords;\n"
> +                                  "void main()\n"
> +                                  "{\n"
> +                                  "   texCoords = textureCoords;\n"
> +                                  "   gl_Position = vec4(position, 0.0, 1.0);\n"
> +                                  "}\n",
> +                                  _mesa_is_desktop_gl(ctx) ? "130" : "300 es");
> +      fs_source = ralloc_asprintf(mem_ctx,
> +                                  "#version %s\n"
> +                                  "uniform sampler2D texSampler;\n"
> +                                  "in vec2 texCoords;\n"
> +                                  "out vec4 out_color;\n"
> +                                  "\n"
> +                                  "void main()\n"
> +                                  "{\n"
> +                                  "   out_color = texture(texSampler, texCoords);\n"
> +                                  "   gl_FragDepth = out_color.r;\n"
> +                                  "}\n",
> +                                  _mesa_is_desktop_gl(ctx) ? "130" : "300 es");
> +   }
> +
> +   vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_source);
> +   fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_source);
> +
> +   blit->ShaderProg = _mesa_CreateProgramObjectARB();
> +   _mesa_AttachShader(blit->ShaderProg, fs);
> +   _mesa_DeleteObjectARB(fs);
> +   _mesa_AttachShader(blit->ShaderProg, vs);
> +   _mesa_DeleteObjectARB(vs);
> +   _mesa_BindAttribLocation(blit->ShaderProg, 0, "position");
> +   _mesa_BindAttribLocation(blit->ShaderProg, 1, "texcoords");
> +   _mesa_EnableVertexAttribArray(0);
> +   _mesa_EnableVertexAttribArray(1);
> +   link_program_with_debug(ctx, blit->ShaderProg);
> +   ralloc_free(mem_ctx);
> +}
>
>  /**
>   * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
> @@ -1546,11 +1705,16 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
>  {
>     struct blit_state *blit = &ctx->Meta->Blit;
>     struct temp_texture *tex = get_temp_texture(ctx);
> +   struct temp_texture *texDepth = get_temp_texture(ctx);
>     const GLsizei maxTexSize = tex->MaxSize;
>     const GLint srcX = MIN2(srcX0, srcX1);
>     const GLint srcY = MIN2(srcY0, srcY1);
>     const GLint srcW = abs(srcX1 - srcX0);
>     const GLint srcH = abs(srcY1 - srcY0);
> +   const GLint dstX = MIN2(dstX0, dstX1);
> +   const GLint dstY = MIN2(dstY0, dstY1);
> +   const GLint dstW = abs(dstX1 - dstX0);
> +   const GLint dstH = abs(dstY1 - dstY0);
>     const GLboolean srcFlipX = srcX1 < srcX0;
>     const GLboolean srcFlipY = srcY1 < srcY0;
>     struct vertex {
> @@ -1558,6 +1722,9 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
>     };
>     struct vertex verts[4];
>     GLboolean newTex;
> +   const GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
> +                                      ctx->Extensions.ARB_fragment_shader &&
> +                                      (ctx->API != API_OPENGLES);
>
>     /* In addition to falling back if the blit size is larger than the maximum
>      * texture size, fallback if the source is multisampled.  This fallback can
> @@ -1586,30 +1753,20 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
>     /* only scissor effects blit so save/clear all other relevant state */
>     _mesa_meta_begin(ctx, ~MESA_META_SCISSOR);
>
> -   if (blit->ArrayObj == 0) {
> -      /* one-time setup */
> -
> -      /* create vertex array object */
> -      _mesa_GenVertexArrays(1, &blit->ArrayObj);
> -      _mesa_BindVertexArray(blit->ArrayObj);
> -
> -      /* create vertex array buffer */
> -      _mesa_GenBuffers(1, &blit->VBO);
> -      _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, blit->VBO);
> -      _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
> -                          NULL, GL_DYNAMIC_DRAW_ARB);
> -
> -      /* setup vertex arrays */
> -      _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
> -      _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
> -      _mesa_EnableClientState(GL_VERTEX_ARRAY);
> -      _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
> +   /* Choose between glsl version and fixed function version of
> +    * GenerateMipmap function.
> +    */
> +   if (use_glsl_version) {
> +      setup_glsl_blit_framebuffer(ctx, blit);
> +      _mesa_UseProgram(blit->ShaderProg);
>     }
>     else {
> -      _mesa_BindVertexArray(blit->ArrayObj);
> -      _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, blit->VBO);
> +      setup_ff_blit_framebuffer(ctx, blit);
>     }
>
> +   _mesa_BindVertexArray(blit->ArrayObj);
> +   _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, blit->VBO);
> +
>     /* Try faster, direct texture approach first */
>     mask = blitframebuffer_texture(ctx, srcX0, srcY0, srcX1, srcY1,
>                                    dstX0, dstY0, dstX1, dstY1, mask, filter);
> @@ -1626,14 +1783,15 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
>
>     /* vertex positions/texcoords (after texture allocation!) */
>     {
> -      verts[0].x = (GLfloat) dstX0;
> -      verts[0].y = (GLfloat) dstY0;
> -      verts[1].x = (GLfloat) dstX1;
> -      verts[1].y = (GLfloat) dstY0;
> -      verts[2].x = (GLfloat) dstX1;
> -      verts[2].y = (GLfloat) dstY1;
> -      verts[3].x = (GLfloat) dstX0;
> -      verts[3].y = (GLfloat) dstY1;
> +      /* setup vertex positions */
> +      verts[0].x = -1.0F;
> +      verts[0].y = -1.0F;
> +      verts[1].x =  1.0F;
> +      verts[1].y = -1.0F;
> +      verts[2].x =  1.0F;
> +      verts[2].y =  1.0F;
> +      verts[3].x = -1.0F;
> +      verts[3].y =  1.0F;
>
>        verts[0].s = 0.0F;
>        verts[0].t = 0.0F;
> @@ -1648,24 +1806,37 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
>        _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
>     }
>
> -   _mesa_set_enable(ctx, tex->Target, GL_TRUE);
> +   /* glEnable() in gles2 and gles3 doesn't allow GL_TEXTURE_{1D, 2D, etc.}
> +    * tokens.
> +    */
> +   if (_mesa_is_desktop_gl(ctx) || ctx->API == API_OPENGLES)
> +      _mesa_set_enable(ctx, tex->Target, GL_TRUE);
>
>     if (mask & GL_COLOR_BUFFER_BIT) {
>        setup_copypix_texture(tex, newTex, srcX, srcY, srcW, srcH,
>                              GL_RGBA, filter);
> +      _mesa_set_viewport(ctx, dstX, dstY, dstW, dstH);
> +      _mesa_ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
> +      _mesa_DepthMask(GL_FALSE);
>        _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
>        mask &= ~GL_COLOR_BUFFER_BIT;
>     }
>
>     if (mask & GL_DEPTH_BUFFER_BIT) {
> +
>        GLuint *tmp = malloc(srcW * srcH * sizeof(GLuint));
> -      if (tmp) {
> +
> +      /* maybe change tex format here */
> +      newTex = alloc_texture(tex, srcW, srcH, GL_DEPTH_COMPONENT);
> +
> +      if (_mesa_is_desktop_gl(ctx) &&
> +          ctx->Extensions.ARB_depth_texture &&
> +          ctx->Extensions.ARB_fragment_program &&
> +          tmp) {
> +
>           if (!blit->DepthFP)
>              init_blit_depth_pixels(ctx);
>
> -         /* maybe change tex format here */
> -         newTex = alloc_texture(tex, srcW, srcH, GL_DEPTH_COMPONENT);
> -
>           _mesa_ReadPixels(srcX, srcY, srcW, srcH,
>                            GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp);
>
> @@ -1674,23 +1845,32 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
>
>           _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP);
>           _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
> -         _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
> -         _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
> -         _mesa_DepthFunc(GL_ALWAYS);
> -         _mesa_DepthMask(GL_TRUE);
> +      }
> +      else {
> +         setup_copypix_texture(texDepth, newTex, srcX, srcY, srcW, srcH,
> +                               GL_DEPTH_COMPONENT, filter);
> +         _mesa_UseProgram(blit->ShaderProg);
> +      }
>
> -         _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
> -         mask &= ~GL_DEPTH_BUFFER_BIT;
> +      _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
> +      _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_TRUE);
> +      _mesa_DepthFunc(GL_ALWAYS);
> +      _mesa_DepthMask(GL_TRUE);
>
> -         free(tmp);
> -      }
> +      _mesa_set_viewport(ctx, dstX0, dstY0, dstW, dstH);
> +      _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
> +      _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
> +
> +     free(tmp);
> +     mask &= ~GL_DEPTH_BUFFER_BIT;
>     }
>
>     if (mask & GL_STENCIL_BUFFER_BIT) {
>        /* XXX can't easily do stencil */
>     }
>
> -   _mesa_set_enable(ctx, tex->Target, GL_FALSE);
> +   if (_mesa_is_desktop_gl(ctx) || ctx->API == API_OPENGLES)
> +      _mesa_set_enable(ctx, tex->Target, GL_FALSE);
>
>     _mesa_meta_end(ctx);
>
> @@ -1713,6 +1893,9 @@ meta_glsl_blit_cleanup(struct gl_context *ctx, struct blit_state *blit)
>        _mesa_DeleteProgramsARB(1, &blit->DepthFP);
>        blit->DepthFP = 0;
>     }
> +
> +   _mesa_DeleteObjectARB(blit->ShaderProg);
> +   blit->ShaderProg = 0;
>  }
>
>
> --
> 1.7.7.6
>


More information about the mesa-dev mailing list