[Mesa-dev] [PATCH 2/2] meta: Handle bitmaps with alpha test enabled.

Francisco Jerez currojerez at riseup.net
Mon Nov 1 13:10:12 PDT 2010


---
 src/mesa/drivers/common/meta.c |   55 ++++++++++++++++++++++++++++++++--------
 1 files changed, 44 insertions(+), 11 deletions(-)

diff --git a/src/mesa/drivers/common/meta.c b/src/mesa/drivers/common/meta.c
index 9615b52..03c1e66 100644
--- a/src/mesa/drivers/common/meta.c
+++ b/src/mesa/drivers/common/meta.c
@@ -104,6 +104,8 @@ struct save_state
 
    /** META_ALPHA_TEST */
    GLboolean AlphaEnabled;
+   GLenum AlphaFunc;
+   GLclampf AlphaRef;
 
    /** META_BLEND */
    GLbitfield BlendEnabled;
@@ -328,6 +330,8 @@ _mesa_meta_begin(struct gl_context *ctx, GLbitfield state)
 
    if (state & META_ALPHA_TEST) {
       save->AlphaEnabled = ctx->Color.AlphaEnabled;
+      save->AlphaFunc = ctx->Color.AlphaFunc;
+      save->AlphaRef = ctx->Color.AlphaRef;
       if (ctx->Color.AlphaEnabled)
          _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_FALSE);
    }
@@ -577,6 +581,7 @@ _mesa_meta_end(struct gl_context *ctx)
    if (state & META_ALPHA_TEST) {
       if (ctx->Color.AlphaEnabled != save->AlphaEnabled)
          _mesa_set_enable(ctx, GL_ALPHA_TEST, save->AlphaEnabled);
+      _mesa_AlphaFunc(save->AlphaFunc, save->AlphaRef);
    }
 
    if (state & META_BLEND) {
@@ -1975,13 +1980,41 @@ _mesa_meta_DrawPixels(struct gl_context *ctx,
    _mesa_meta_end(ctx);
 }
 
+static void
+setup_bitmap_alpha(struct gl_context *ctx, GLubyte *bg, GLubyte *fg)
+{
+   struct save_state *save = &ctx->Meta->Save;
+   GLenum func = (save->AlphaEnabled ? save->AlphaFunc : GL_ALWAYS);
+   GLubyte ref = FLOAT_TO_UBYTE(save->AlphaRef);
+   GLfloat rast = ctx->Current.RasterColor[ACOMP];
+
+   _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE);
+   *fg = FLOAT_TO_UBYTE(rast);
+
+   /* Choose a background alpha value that wouldn't pass through.
+    */
+   if (func == GL_NEVER || func == GL_NOTEQUAL ||
+       func == GL_LESS || func == GL_GREATER) {
+      *bg = ref;
+
+   } else if ((func == GL_EQUAL && *fg != ref) ||
+	      (func == GL_LEQUAL && *fg > ref) ||
+	      (func == GL_GEQUAL && *fg < ref)) {
+      *bg = *fg;
+
+   } else {
+      /* Alpha is supposed to pass, make it accept the current raster
+       * value, and use something else as background.
+       */
+      _mesa_AlphaFunc(GL_EQUAL, rast);
+      *bg = ~*fg;
+   }
+}
 
 /**
- * Do glBitmap with a alpha texture quad.  Use the alpha test to
- * cull the 'off' bits.  If alpha test is already enabled, fall back
- * to swrast (should be a rare case).
- * A bitmap cache as in the gallium/mesa state tracker would
- * improve performance a lot.
+ * Do glBitmap with a alpha texture quad.  Use the alpha test to cull
+ * the 'off' bits.  A bitmap cache as in the gallium/mesa state
+ * tracker would improve performance a lot.
  */
 void
 _mesa_meta_Bitmap(struct gl_context *ctx,
@@ -1997,6 +2030,7 @@ _mesa_meta_Bitmap(struct gl_context *ctx,
       GLfloat x, y, z, s, t, r, g, b, a;
    };
    struct vertex verts[4];
+   GLubyte bg, fg;
    GLboolean newTex;
    GLubyte *bitmap8;
 
@@ -2004,7 +2038,6 @@ _mesa_meta_Bitmap(struct gl_context *ctx,
     * Check if swrast fallback is needed.
     */
    if (ctx->_ImageTransferState ||
-       ctx->Color.AlphaEnabled ||
        ctx->Fog.Enabled ||
        ctx->Texture._EnabledUnits ||
        width > tex->MaxSize ||
@@ -2100,16 +2133,16 @@ _mesa_meta_Bitmap(struct gl_context *ctx,
       return;
    }
 
-   bitmap8 = (GLubyte *) calloc(1, width * height);
+   bitmap8 = (GLubyte *) malloc(width * height);
    if (bitmap8) {
+      setup_bitmap_alpha(ctx, &bg, &fg);
+
+      memset(bitmap8, bg, width * height);
       _mesa_expand_bitmap(width, height, &unpackSave, bitmap1,
-                          bitmap8, width, 0xff);
+                          bitmap8, width, fg);
 
       _mesa_set_enable(ctx, tex->Target, GL_TRUE);
 
-      _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE);
-      _mesa_AlphaFunc(GL_GREATER, 0.0);
-
       setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
                             GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8);
 
-- 
1.6.4.4



More information about the mesa-dev mailing list