Merge branch 'mesa_7_6_branch'
[mesa.git] / src / mesa / drivers / common / meta.c
index e42beabc9ba0fce900cc1c60d950e1e297a2d703..d1c2232e26a8aa1c890e54631c08b0233c45ab5b 100644 (file)
 #include "main/arrayobj.h"
 #include "main/blend.h"
 #include "main/bufferobj.h"
+#include "main/buffers.h"
 #include "main/depth.h"
 #include "main/enable.h"
+#include "main/fbobject.h"
 #include "main/image.h"
 #include "main/macros.h"
 #include "main/matrix.h"
+#include "main/mipmap.h"
 #include "main/polygon.h"
+#include "main/readpix.h"
 #include "main/scissor.h"
 #include "main/shaders.h"
 #include "main/stencil.h"
@@ -54,6 +58,7 @@
 #include "main/varray.h"
 #include "main/viewport.h"
 #include "shader/program.h"
+#include "shader/arbprogram.h"
 #include "swrast/swrast.h"
 #include "drivers/common/meta.h"
 
@@ -82,8 +87,8 @@ struct save_state
    /** META_FOG */
    GLboolean Fog;
 
-   /** META_PIXELSTORE */
-   /* XXX / TO-DO */
+   /** META_PIXEL_STORE */
+   struct gl_pixelstore_attrib Pack, Unpack;
 
    /** META_RASTERIZATION */
    GLenum FrontPolygonMode, BackPolygonMode;
@@ -135,17 +140,32 @@ struct save_state
 };
 
 
+/**
+ * Temporary texture used for glBlitFramebuffer, glDrawPixels, etc.
+ * This is currently shared by all the meta ops.  But we could create a
+ * separate one for each of glDrawPixel, glBlitFramebuffer, glCopyPixels, etc.
+ */
+struct temp_texture
+{
+   GLuint TexObj;
+   GLenum Target;         /**< GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE */
+   GLsizei MinSize;       /**< Min texture size to allocate */
+   GLsizei MaxSize;       /**< Max possible texture size */
+   GLboolean NPOT;        /**< Non-power of two size OK? */
+   GLsizei Width, Height; /**< Current texture size */
+   GLenum IntFormat;
+   GLfloat Sright, Ttop;  /**< right, top texcoords */
+};
+
+
 /**
  * State for glBlitFramebufer()
  */
 struct blit_state
 {
-   GLuint TexObj;
-   GLsizei TexWidth, TexHeight;
-   GLenum TexType;
    GLuint ArrayObj;
    GLuint VBO;
-   GLfloat verts[4][4]; /** four verts of X,Y,S,T */
+   GLuint DepthFP;
 };
 
 
@@ -156,7 +176,6 @@ struct clear_state
 {
    GLuint ArrayObj;
    GLuint VBO;
-   GLfloat verts[4][7]; /** four verts of X,Y,Z,R,G,B,A */
 };
 
 
@@ -165,12 +184,8 @@ struct clear_state
  */
 struct copypix_state
 {
-   GLuint TexObj;
-   GLsizei TexWidth, TexHeight;
-   GLenum TexType;
    GLuint ArrayObj;
    GLuint VBO;
-   GLfloat verts[4][5]; /** four verts of X,Y,Z,S,T */
 };
 
 
@@ -179,15 +194,35 @@ struct copypix_state
  */
 struct drawpix_state
 {
-   GLuint TexObj;
-   GLsizei TexWidth, TexHeight;
-   GLenum TexIntFormat;
    GLuint ArrayObj;
    GLuint VBO;
-   GLfloat verts[4][5]; /** four verts of X,Y,Z,S,T */
+
+   GLuint StencilFP;  /**< Fragment program for drawing stencil images */
+   GLuint DepthFP;  /**< Fragment program for drawing depth images */
 };
 
 
+/**
+ * State for glBitmap()
+ */
+struct bitmap_state
+{
+   GLuint ArrayObj;
+   GLuint VBO;
+   struct temp_texture Tex;  /**< separate texture from other meta ops */
+};
+
+
+/**
+ * State for _mesa_meta_generate_mipmap()
+ */
+struct gen_mipmap_state
+{
+   GLuint ArrayObj;
+   GLuint VBO;
+   GLuint FBO;
+};
+
 
 /**
  * All per-context meta state.
@@ -196,16 +231,14 @@ struct gl_meta_state
 {
    struct save_state Save;    /**< state saved during meta-ops */
 
+   struct temp_texture TempTex;
+
    struct blit_state Blit;    /**< For _mesa_meta_blit_framebuffer() */
    struct clear_state Clear;  /**< For _mesa_meta_clear() */
    struct copypix_state CopyPix;  /**< For _mesa_meta_copy_pixels() */
    struct drawpix_state DrawPix;  /**< For _mesa_meta_draw_pixels() */
-
-   /* other possible meta-ops:
-    * glDrawPixels()
-    * glBitmap()
-    * glGenerateMipmap()
-    */
+   struct bitmap_state Bitmap;    /**< For _mesa_meta_bitmap() */
+   struct gen_mipmap_state Mipmap;    /**< For _mesa_meta_generate_mipmap() */
 };
 
 
@@ -231,27 +264,37 @@ _mesa_meta_free(GLcontext *ctx)
 {
    struct gl_meta_state *meta = ctx->Meta;
 
-   if (meta->Blit.TexObj) {
-      _mesa_DeleteTextures(1, &meta->Blit.TexObj);
+   if (_mesa_get_current_context()) {
+      /* if there's no current context, these textures, buffers, etc should
+       * still get freed by _mesa_free_context_data().
+       */
+
+      /* the temporary texture */
+      _mesa_DeleteTextures(1, &meta->TempTex.TexObj);
+
+      /* glBlitFramebuffer */
       _mesa_DeleteBuffersARB(1, & meta->Blit.VBO);
       _mesa_DeleteVertexArraysAPPLE(1, &meta->Blit.ArrayObj);
-   }
+      _mesa_DeletePrograms(1, &meta->Blit.DepthFP);
 
-   if (meta->Clear.VBO) {
+      /* glClear */
       _mesa_DeleteBuffersARB(1, & meta->Clear.VBO);
       _mesa_DeleteVertexArraysAPPLE(1, &meta->Clear.ArrayObj);
-   }
 
-   if (meta->CopyPix.TexObj) {
-      _mesa_DeleteTextures(1, &meta->CopyPix.TexObj);
+      /* glCopyPixels */
       _mesa_DeleteBuffersARB(1, & meta->CopyPix.VBO);
       _mesa_DeleteVertexArraysAPPLE(1, &meta->CopyPix.ArrayObj);
-   }
 
-   if (meta->DrawPix.TexObj) {
-      _mesa_DeleteTextures(1, &meta->DrawPix.TexObj);
+      /* glDrawPixels */
       _mesa_DeleteBuffersARB(1, & meta->DrawPix.VBO);
       _mesa_DeleteVertexArraysAPPLE(1, &meta->DrawPix.ArrayObj);
+      _mesa_DeletePrograms(1, &meta->DrawPix.DepthFP);
+      _mesa_DeletePrograms(1, &meta->DrawPix.StencilFP);
+
+      /* glBitmap */
+      _mesa_DeleteBuffersARB(1, & meta->Bitmap.VBO);
+      _mesa_DeleteVertexArraysAPPLE(1, &meta->Bitmap.ArrayObj);
+      _mesa_DeleteTextures(1, &meta->Bitmap.Tex.TexObj);
    }
 
    _mesa_free(ctx->Meta);
@@ -276,16 +319,16 @@ _mesa_meta_begin(GLcontext *ctx, GLbitfield state)
    if (state & META_ALPHA_TEST) {
       save->AlphaEnabled = ctx->Color.AlphaEnabled;
       if (ctx->Color.AlphaEnabled)
-         _mesa_Disable(GL_ALPHA_TEST);
+         _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_FALSE);
    }
 
    if (state & META_BLEND) {
       save->BlendEnabled = ctx->Color.BlendEnabled;
       if (ctx->Color.BlendEnabled)
-         _mesa_Disable(GL_BLEND);
+         _mesa_set_enable(ctx, GL_BLEND, GL_FALSE);
       save->ColorLogicOpEnabled = ctx->Color.ColorLogicOpEnabled;
       if (ctx->Color.ColorLogicOpEnabled)
-         _mesa_Disable(GL_COLOR_LOGIC_OP);
+         _mesa_set_enable(ctx, GL_COLOR_LOGIC_OP, GL_FALSE);
    }
 
    if (state & META_COLOR_MASK) {
@@ -300,7 +343,7 @@ _mesa_meta_begin(GLcontext *ctx, GLbitfield state)
    if (state & META_DEPTH_TEST) {
       save->Depth = ctx->Depth; /* struct copy */
       if (ctx->Depth.Test)
-         _mesa_Disable(GL_DEPTH_TEST);
+         _mesa_set_enable(ctx, GL_DEPTH_TEST, GL_FALSE);
    }
 
    if (state & META_FOG) {
@@ -309,6 +352,13 @@ _mesa_meta_begin(GLcontext *ctx, GLbitfield state)
          _mesa_set_enable(ctx, GL_FOG, GL_FALSE);
    }
 
+   if (state & META_PIXEL_STORE) {
+      save->Pack = ctx->Pack;
+      save->Unpack = ctx->Unpack;
+      ctx->Pack = ctx->DefaultPacking;
+      ctx->Unpack = ctx->DefaultPacking;
+   }
+
    if (state & META_RASTERIZATION) {
       save->FrontPolygonMode = ctx->Polygon.FrontMode;
       save->BackPolygonMode = ctx->Polygon.BackMode;
@@ -350,7 +400,7 @@ _mesa_meta_begin(GLcontext *ctx, GLbitfield state)
    if (state & META_STENCIL_TEST) {
       save->Stencil = ctx->Stencil; /* struct copy */
       if (ctx->Stencil.Enabled)
-         _mesa_Disable(GL_STENCIL_TEST);
+         _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_FALSE);
       /* NOTE: other stencil state not reset */
    }
 
@@ -361,6 +411,11 @@ _mesa_meta_begin(GLcontext *ctx, GLbitfield state)
       save->ClientActiveUnit = ctx->Array.ActiveTexture;
       save->EnvMode = ctx->Texture.Unit[0].EnvMode;
 
+      if (ctx->Texture._EnabledUnits |
+          ctx->Texture._EnabledCoordUnits |
+          ctx->Texture._TexGenEnabled |
+          ctx->Texture._TexMatEnabled) {
+
       /* Disable all texture units */
       for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
          save->TexEnabled[u] = ctx->Texture.Unit[u].Enabled;
@@ -379,6 +434,7 @@ _mesa_meta_begin(GLcontext *ctx, GLbitfield state)
             _mesa_set_enable(ctx, GL_TEXTURE_GEN_Q, GL_FALSE);
          }
       }
+      }
 
       /* save current texture objects for unit[0] only */
       for (tgt = 0; tgt < NUM_TEXTURE_TARGETS; tgt++) {
@@ -432,13 +488,23 @@ _mesa_meta_begin(GLcontext *ctx, GLbitfield state)
    }
 
    if (state & META_VIEWPORT) {
+      /* save viewport state */
       save->ViewportX = ctx->Viewport.X;
       save->ViewportY = ctx->Viewport.Y;
       save->ViewportW = ctx->Viewport.Width;
       save->ViewportH = ctx->Viewport.Height;
-      _mesa_Viewport(0, 0, ctx->DrawBuffer->Width, ctx->DrawBuffer->Height);
+      /* set viewport to match window size */
+      if (ctx->Viewport.X != 0 ||
+          ctx->Viewport.Y != 0 ||
+          ctx->Viewport.Width != ctx->DrawBuffer->Width ||
+          ctx->Viewport.Height != ctx->DrawBuffer->Height) {
+         _mesa_set_viewport(ctx, 0, 0,
+                            ctx->DrawBuffer->Width, ctx->DrawBuffer->Height);
+      }
+      /* save depth range state */
       save->DepthNear = ctx->Viewport.Near;
       save->DepthFar = ctx->Viewport.Far;
+      /* set depth range to default */
       _mesa_DepthRange(0.0, 1.0);
    }
 
@@ -489,6 +555,11 @@ _mesa_meta_end(GLcontext *ctx)
       _mesa_set_enable(ctx, GL_FOG, save->Fog);
    }
 
+   if (state & META_PIXEL_STORE) {
+      ctx->Pack = save->Pack;
+      ctx->Unpack = save->Unpack;
+   }
+
    if (state & META_RASTERIZATION) {
       _mesa_PolygonMode(GL_FRONT, save->FrontPolygonMode);
       _mesa_PolygonMode(GL_BACK, save->BackPolygonMode);
@@ -642,8 +713,13 @@ _mesa_meta_end(GLcontext *ctx)
    }
 
    if (state & META_VIEWPORT) {
-      _mesa_Viewport(save->ViewportX, save->ViewportY,
-                     save->ViewportW, save->ViewportH);
+      if (save->ViewportX != ctx->Viewport.X ||
+          save->ViewportY != ctx->Viewport.Y ||
+          save->ViewportW != ctx->Viewport.Width ||
+          save->ViewportH != ctx->Viewport.Height) {
+         _mesa_set_viewport(ctx, save->ViewportX, save->ViewportY,
+                            save->ViewportW, save->ViewportH);
+      }
       _mesa_DepthRange(save->DepthNear, save->DepthFar);
    }
 
@@ -657,10 +733,245 @@ _mesa_meta_end(GLcontext *ctx)
 }
 
 
+/**
+ * One-time init for a temp_texture object.
+ * Choose tex target, compute max tex size, etc.
+ */
+static void
+init_temp_texture(GLcontext *ctx, struct temp_texture *tex)
+{
+   /* prefer texture rectangle */
+   if (ctx->Extensions.NV_texture_rectangle) {
+      tex->Target = GL_TEXTURE_RECTANGLE;
+      tex->MaxSize = ctx->Const.MaxTextureRectSize;
+      tex->NPOT = GL_TRUE;
+   }
+   else {
+      /* use 2D texture, NPOT if possible */
+      tex->Target = GL_TEXTURE_2D;
+      tex->MaxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
+      tex->NPOT = ctx->Extensions.ARB_texture_non_power_of_two;
+   }
+   tex->MinSize = 16;  /* 16 x 16 at least */
+   assert(tex->MaxSize > 0);
+
+   _mesa_GenTextures(1, &tex->TexObj);
+   _mesa_BindTexture(tex->Target, tex->TexObj);
+}
+
+
+/**
+ * Return pointer to temp_texture info for non-bitmap ops.
+ * This does some one-time init if needed.
+ */
+static struct temp_texture *
+get_temp_texture(GLcontext *ctx)
+{
+   struct temp_texture *tex = &ctx->Meta->TempTex;
+
+   if (!tex->TexObj) {
+      init_temp_texture(ctx, tex);
+   }
+
+   return tex;
+}
+
+
+/**
+ * Return pointer to temp_texture info for _mesa_meta_bitmap().
+ * We use a separate texture for bitmaps to reduce texture
+ * allocation/deallocation.
+ */
+static struct temp_texture *
+get_bitmap_temp_texture(GLcontext *ctx)
+{
+   struct temp_texture *tex = &ctx->Meta->Bitmap.Tex;
+
+   if (!tex->TexObj) {
+      init_temp_texture(ctx, tex);
+   }
+
+   return tex;
+}
+
+
+/**
+ * Compute the width/height of texture needed to draw an image of the
+ * given size.  Return a flag indicating whether the current texture
+ * can be re-used (glTexSubImage2D) or if a new texture needs to be
+ * allocated (glTexImage2D).
+ * Also, compute s/t texcoords for drawing.
+ *
+ * \return GL_TRUE if new texture is needed, GL_FALSE otherwise
+ */
+static GLboolean
+alloc_texture(struct temp_texture *tex,
+              GLsizei width, GLsizei height, GLenum intFormat)
+{
+   GLboolean newTex = GL_FALSE;
+
+   ASSERT(width <= tex->MaxSize);
+   ASSERT(height <= tex->MaxSize);
+
+   if (width > tex->Width ||
+       height > tex->Height ||
+       intFormat != tex->IntFormat) {
+      /* alloc new texture (larger or different format) */
+
+      if (tex->NPOT) {
+         /* use non-power of two size */
+         tex->Width = MAX2(tex->MinSize, width);
+         tex->Height = MAX2(tex->MinSize, height);
+      }
+      else {
+         /* find power of two size */
+         GLsizei w, h;
+         w = h = tex->MinSize;
+         while (w < width)
+            w *= 2;
+         while (h < height)
+            h *= 2;
+         tex->Width = w;
+         tex->Height = h;
+      }
+
+      tex->IntFormat = intFormat;
+
+      newTex = GL_TRUE;
+   }
+
+   /* compute texcoords */
+   if (tex->Target == GL_TEXTURE_RECTANGLE) {
+      tex->Sright = (GLfloat) width;
+      tex->Ttop = (GLfloat) height;
+   }
+   else {
+      tex->Sright = (GLfloat) width / tex->Width;
+      tex->Ttop = (GLfloat) height / tex->Height;
+   }
+
+   return newTex;
+}
+
+
+/**
+ * Setup/load texture for glCopyPixels or glBlitFramebuffer.
+ */
+static void
+setup_copypix_texture(struct temp_texture *tex,
+                      GLboolean newTex,
+                      GLint srcX, GLint srcY,
+                      GLsizei width, GLsizei height, GLenum intFormat,
+                      GLenum filter)
+{
+   _mesa_BindTexture(tex->Target, tex->TexObj);
+   _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, filter);
+   _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, filter);
+   _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
+
+   /* copy framebuffer image to texture */
+   if (newTex) {
+      /* create new tex image */
+      if (tex->Width == width && tex->Height == height) {
+         /* create new tex with framebuffer data */
+         _mesa_CopyTexImage2D(tex->Target, 0, tex->IntFormat,
+                              srcX, srcY, width, height, 0);
+      }
+      else {
+         /* create empty texture */
+         _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
+                          tex->Width, tex->Height, 0,
+                          intFormat, GL_UNSIGNED_BYTE, NULL);
+         /* load image */
+         _mesa_CopyTexSubImage2D(tex->Target, 0,
+                                 0, 0, srcX, srcY, width, height);
+      }
+   }
+   else {
+      /* replace existing tex image */
+      _mesa_CopyTexSubImage2D(tex->Target, 0,
+                              0, 0, srcX, srcY, width, height);
+   }
+}
+
+
+/**
+ * Setup/load texture for glDrawPixels.
+ */
+static void
+setup_drawpix_texture(struct temp_texture *tex,
+                      GLboolean newTex,
+                      GLenum texIntFormat,
+                      GLsizei width, GLsizei height,
+                      GLenum format, GLenum type,
+                      const GLvoid *pixels)
+{
+   _mesa_BindTexture(tex->Target, tex->TexObj);
+   _mesa_TexParameteri(tex->Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+   _mesa_TexParameteri(tex->Target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+   _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
+
+   /* copy pixel data to texture */
+   if (newTex) {
+      /* create new tex image */
+      if (tex->Width == width && tex->Height == height) {
+         /* create new tex and load image data */
+         _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
+                          tex->Width, tex->Height, 0, format, type, pixels);
+      }
+      else {
+         /* create empty texture */
+         _mesa_TexImage2D(tex->Target, 0, tex->IntFormat,
+                          tex->Width, tex->Height, 0, format, type, NULL);
+         /* load image */
+         _mesa_TexSubImage2D(tex->Target, 0,
+                             0, 0, width, height, format, type, pixels);
+      }
+   }
+   else {
+      /* replace existing tex image */
+      _mesa_TexSubImage2D(tex->Target, 0,
+                          0, 0, width, height, format, type, pixels);
+   }
+}
+
+
+
+/**
+ * One-time init for drawing depth pixels.
+ */
+static void
+init_blit_depth_pixels(GLcontext *ctx)
+{
+   static const char *program =
+      "!!ARBfp1.0\n"
+      "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
+      "END \n";
+   char program2[200];
+   struct blit_state *blit = &ctx->Meta->Blit;
+   struct temp_texture *tex = get_temp_texture(ctx);
+   const char *texTarget;
+
+   assert(blit->DepthFP == 0);
+
+   /* replace %s with "RECT" or "2D" */
+   assert(strlen(program) + 4 < sizeof(program2));
+   if (tex->Target == GL_TEXTURE_RECTANGLE)
+      texTarget = "RECT";
+   else
+      texTarget = "2D";
+   _mesa_snprintf(program2, sizeof(program2), program, texTarget);
+
+   _mesa_GenPrograms(1, &blit->DepthFP);
+   _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, blit->DepthFP);
+   _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
+                          strlen(program2), (const GLubyte *) program2);
+}
+
+
 /**
  * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
  * of texture mapping and polygon rendering.
- * Note: this function requires GL_ARB_texture_rectangle support.
  */
 void
 _mesa_meta_blit_framebuffer(GLcontext *ctx,
@@ -669,24 +980,24 @@ _mesa_meta_blit_framebuffer(GLcontext *ctx,
                             GLbitfield mask, GLenum filter)
 {
    struct blit_state *blit = &ctx->Meta->Blit;
+   struct temp_texture *tex = 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);
-   GLboolean srcFlipX = srcX1 < srcX0;
-   GLboolean srcFlipY = srcY1 < srcY0;
-
-   ASSERT(ctx->Extensions.NV_texture_rectangle);
+   const GLboolean srcFlipX = srcX1 < srcX0;
+   const GLboolean srcFlipY = srcY1 < srcY0;
+   GLfloat verts[4][4]; /* four verts of X,Y,S,T */
+   GLboolean newTex;
 
-   if (srcW > ctx->Const.MaxTextureRectSize ||
-       srcH > ctx->Const.MaxTextureRectSize) {
+   if (srcW > maxTexSize || srcH > maxTexSize) {
       /* XXX avoid this fallback */
       _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
                               dstX0, dstY0, dstX1, dstY1, mask, filter);
       return;
    }
 
-
    if (srcFlipX) {
       GLint tmp = dstX0;
       dstX0 = dstX1;
@@ -702,21 +1013,6 @@ _mesa_meta_blit_framebuffer(GLcontext *ctx,
    /* only scissor effects blit so save/clear all other relevant state */
    _mesa_meta_begin(ctx, ~META_SCISSOR);
 
-   if (blit->TexObj == 0) {
-      /* one-time setup */
-
-      /* create texture object */
-      _mesa_GenTextures(1, &blit->TexObj);
-      _mesa_BindTexture(GL_TEXTURE_RECTANGLE, blit->TexObj);
-      _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
-   }
-   else {
-      _mesa_BindTexture(GL_TEXTURE_RECTANGLE, blit->TexObj);
-   }
-
-   _mesa_TexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, filter);
-   _mesa_TexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, filter);
-
    if (blit->ArrayObj == 0) {
       /* one-time setup */
 
@@ -727,13 +1023,13 @@ _mesa_meta_blit_framebuffer(GLcontext *ctx,
       /* create vertex array buffer */
       _mesa_GenBuffersARB(1, &blit->VBO);
       _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, blit->VBO);
-      _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(blit->verts),
-                          blit->verts, GL_STREAM_DRAW_ARB);
+      _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
+                          NULL, GL_DYNAMIC_DRAW_ARB);
 
       /* setup vertex arrays */
-      _mesa_VertexPointer(2, GL_FLOAT, 4 * sizeof(GLfloat),
-                          (void*) (0 * sizeof(GLfloat)));
-      _mesa_TexCoordPointer(2, GL_FLOAT, 4 * sizeof(GLfloat),
+      _mesa_VertexPointer(2, GL_FLOAT, sizeof(verts[0]),
+                          (void *) (0 * sizeof(GLfloat)));
+      _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(verts[0]),
                             (void *) (2 * sizeof(GLfloat)));
       _mesa_EnableClientState(GL_VERTEX_ARRAY);
       _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
@@ -743,62 +1039,79 @@ _mesa_meta_blit_framebuffer(GLcontext *ctx,
       _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, blit->VBO);
    }
 
-   /* vertex positions */
-   blit->verts[0][0] = (GLfloat) dstX0;
-   blit->verts[0][1] = (GLfloat) dstY0;
-   blit->verts[1][0] = (GLfloat) dstX1;
-   blit->verts[1][1] = (GLfloat) dstY0;
-   blit->verts[2][0] = (GLfloat) dstX1;
-   blit->verts[2][1] = (GLfloat) dstY1;
-   blit->verts[3][0] = (GLfloat) dstX0;
-   blit->verts[3][1] = (GLfloat) dstY1;
-
-   /* texcoords */
-   blit->verts[0][2] = 0.0F;
-   blit->verts[0][3] = 0.0F;
-   blit->verts[1][2] = (GLfloat) srcW;
-   blit->verts[1][3] = 0.0F;
-   blit->verts[2][2] = (GLfloat) srcW;
-   blit->verts[2][3] = (GLfloat) srcH;
-   blit->verts[3][2] = 0.0F;
-   blit->verts[3][3] = (GLfloat) srcH;
-
-   /* upload new vertex data */
-   _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0,
-                          sizeof(blit->verts), blit->verts);
+   newTex = alloc_texture(tex, srcW, srcH, GL_RGBA);
 
-   /* copy framebuffer image to texture */
-   if (mask & GL_COLOR_BUFFER_BIT) {
-      if (blit->TexWidth == srcW &&
-          blit->TexHeight == srcH &&
-          blit->TexType == GL_RGBA) {
-         /* replace existing tex image */
-         _mesa_CopyTexSubImage2D(GL_TEXTURE_RECTANGLE, 0,
-                                 0, 0, srcX, srcY, srcW, srcH);
-      }
-      else {
-         /* create new tex image */
-         _mesa_CopyTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA,
-                              srcX, srcY, srcW, srcH, 0);
-         blit->TexWidth = srcW;
-         blit->TexHeight = srcH;
-         blit->TexType = GL_RGBA;
-      }
+   /* vertex positions/texcoords (after texture allocation!) */
+   {
+      verts[0][0] = (GLfloat) dstX0;
+      verts[0][1] = (GLfloat) dstY0;
+      verts[1][0] = (GLfloat) dstX1;
+      verts[1][1] = (GLfloat) dstY0;
+      verts[2][0] = (GLfloat) dstX1;
+      verts[2][1] = (GLfloat) dstY1;
+      verts[3][0] = (GLfloat) dstX0;
+      verts[3][1] = (GLfloat) dstY1;
+
+      verts[0][2] = 0.0F;
+      verts[0][3] = 0.0F;
+      verts[1][2] = tex->Sright;
+      verts[1][3] = 0.0F;
+      verts[2][2] = tex->Sright;
+      verts[2][3] = tex->Ttop;
+      verts[3][2] = 0.0F;
+      verts[3][3] = tex->Ttop;
+
+      /* upload new vertex data */
+      _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
+   }
+
+   _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_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
       mask &= ~GL_COLOR_BUFFER_BIT;
    }
 
-   _mesa_Enable(GL_TEXTURE_RECTANGLE);
+   if (mask & GL_DEPTH_BUFFER_BIT) {
+      GLuint *tmp = (GLuint *) _mesa_malloc(srcW * srcH * sizeof(GLuint));
+      if (tmp) {
+         if (!blit->DepthFP)
+            init_blit_depth_pixels(ctx);
 
-   /* draw textured quad */
-   _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
+         /* 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);
+
+         setup_drawpix_texture(tex, newTex, GL_DEPTH_COMPONENT, srcW, srcH,
+                               GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, tmp);
 
-   _mesa_Disable(GL_TEXTURE_RECTANGLE);
+         _mesa_BindProgram(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);
+
+         _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
+         mask &= ~GL_DEPTH_BUFFER_BIT;
+
+         _mesa_free(tmp);
+      }
+   }
+
+   if (mask & GL_STENCIL_BUFFER_BIT) {
+      /* XXX can't easily do stencil */
+   }
+
+   _mesa_set_enable(ctx, tex->Target, GL_FALSE);
 
    _mesa_meta_end(ctx);
 
-   /* XXX, TO-DO: try to handle these cases above! */
-   if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)) {
+   if (mask) {
       _swrast_BlitFramebuffer(ctx, srcX0, srcY0, srcX1, srcY1,
                               dstX0, dstY0, dstX1, dstY1, mask, filter);
    }
@@ -812,11 +1125,16 @@ void
 _mesa_meta_clear(GLcontext *ctx, GLbitfield buffers)
 {
    struct clear_state *clear = &ctx->Meta->Clear;
-   GLfloat z = 1.0 - 2.0 * ctx->Depth.Clear;
-   GLuint i;
+   GLfloat verts[4][7]; /* four verts of X,Y,Z,R,G,B,A */
+   /* save all state but scissor, pixel pack/unpack */
+   GLbitfield metaSave = META_ALL - META_SCISSOR - META_PIXEL_STORE;
+
+   if (buffers & BUFFER_BITS_COLOR) {
+      /* if clearing color buffers, don't save/restore colormask */
+      metaSave -= META_COLOR_MASK;
+   }
 
-   /* only scissor and color mask effects clearing */
-   _mesa_meta_begin(ctx, ~(META_SCISSOR | META_COLOR_MASK));
+   _mesa_meta_begin(ctx, metaSave);
 
    if (clear->ArrayObj == 0) {
       /* one-time setup */
@@ -828,12 +1146,13 @@ _mesa_meta_clear(GLcontext *ctx, GLbitfield buffers)
       /* create vertex array buffer */
       _mesa_GenBuffersARB(1, &clear->VBO);
       _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, clear->VBO);
-      _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(clear->verts),
-                          clear->verts, GL_STREAM_DRAW_ARB);
+      _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
+                          NULL, GL_DYNAMIC_DRAW_ARB);
 
       /* setup vertex arrays */
-      _mesa_VertexPointer(3, GL_FLOAT, 7 * sizeof(GLfloat), (void *) 0);
-      _mesa_ColorPointer(4, GL_FLOAT, 7 * sizeof(GLfloat),
+      _mesa_VertexPointer(3, GL_FLOAT, sizeof(verts[0]),
+                          (void *) (0 * sizeof(GLfloat)));
+      _mesa_ColorPointer(4, GL_FLOAT, sizeof(verts[0]),
                          (void *) (3 * sizeof(GLfloat)));
       _mesa_EnableClientState(GL_VERTEX_ARRAY);
       _mesa_EnableClientState(GL_COLOR_ARRAY);
@@ -848,6 +1167,7 @@ _mesa_meta_clear(GLcontext *ctx, GLbitfield buffers)
       /* leave colormask, glDrawBuffer state as-is */
    }
    else {
+      ASSERT(metaSave & META_COLOR_MASK);
       _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    }
 
@@ -874,29 +1194,37 @@ _mesa_meta_clear(GLcontext *ctx, GLbitfield buffers)
       assert(!ctx->Stencil.Enabled);
    }
 
-   /* vertex positions */
-   clear->verts[0][0] = (GLfloat) ctx->DrawBuffer->_Xmin;
-   clear->verts[0][1] = (GLfloat) ctx->DrawBuffer->_Ymin;
-   clear->verts[0][2] = z;
-   clear->verts[1][0] = (GLfloat) ctx->DrawBuffer->_Xmax;
-   clear->verts[1][1] = (GLfloat) ctx->DrawBuffer->_Ymin;
-   clear->verts[1][2] = z;
-   clear->verts[2][0] = (GLfloat) ctx->DrawBuffer->_Xmax;
-   clear->verts[2][1] = (GLfloat) ctx->DrawBuffer->_Ymax;
-   clear->verts[2][2] = z;
-   clear->verts[3][0] = (GLfloat) ctx->DrawBuffer->_Xmin;
-   clear->verts[3][1] = (GLfloat) ctx->DrawBuffer->_Ymax;
-   clear->verts[3][2] = z;
+   /* vertex positions/colors */
+   {
+      const GLfloat x0 = (GLfloat) ctx->DrawBuffer->_Xmin;
+      const GLfloat y0 = (GLfloat) ctx->DrawBuffer->_Ymin;
+      const GLfloat x1 = (GLfloat) ctx->DrawBuffer->_Xmax;
+      const GLfloat y1 = (GLfloat) ctx->DrawBuffer->_Ymax;
+      const GLfloat z = 1.0 - 2.0 * ctx->Depth.Clear;
+      GLuint i;
+
+      verts[0][0] = x0;
+      verts[0][1] = y0;
+      verts[0][2] = z;
+      verts[1][0] = x1;
+      verts[1][1] = y0;
+      verts[1][2] = z;
+      verts[2][0] = x1;
+      verts[2][1] = y1;
+      verts[2][2] = z;
+      verts[3][0] = x0;
+      verts[3][1] = y1;
+      verts[3][2] = z;
+
+      /* vertex colors */
+      for (i = 0; i < 4; i++) {
+         COPY_4FV(&verts[i][3], ctx->Color.ClearColor);
+      }
 
-   /* vertex colors */
-   for (i = 0; i < 4; i++) {
-      COPY_4FV(&clear->verts[i][3], ctx->Color.ClearColor);
+      /* upload new vertex data */
+      _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
    }
 
-   /* upload new vertex data */
-   _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0,
-                          sizeof(clear->verts), clear->verts);
-
    /* draw quad */
    _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
 
@@ -907,26 +1235,23 @@ _mesa_meta_clear(GLcontext *ctx, GLbitfield buffers)
 /**
  * Meta implementation of ctx->Driver.CopyPixels() in terms
  * of texture mapping and polygon rendering.
- * Note: this function requires GL_ARB_texture_rectangle support.
  */
 void
 _mesa_meta_copy_pixels(GLcontext *ctx, GLint srcX, GLint srcY,
                        GLsizei width, GLsizei height,
                        GLint dstX, GLint dstY, GLenum type)
 {
-   const GLenum filter = GL_NEAREST;
    struct copypix_state *copypix = &ctx->Meta->CopyPix;
-   const GLfloat z = ctx->Current.RasterPos[2];
-   const GLfloat dstX1 = dstX + width * ctx->Pixel.ZoomX;
-   const GLfloat dstY1 = dstY + height * ctx->Pixel.ZoomY;
-
-   ASSERT(ctx->Extensions.NV_texture_rectangle);
+   struct temp_texture *tex = get_temp_texture(ctx);
+   GLfloat verts[4][5]; /* four verts of X,Y,Z,S,T */
+   GLboolean newTex;
+   GLenum intFormat = GL_RGBA;
 
    if (type != GL_COLOR ||
        ctx->_ImageTransferState ||
        ctx->Fog.Enabled ||
-       width > ctx->Const.MaxTextureRectSize ||
-       height > ctx->Const.MaxTextureRectSize) {
+       width > tex->MaxSize ||
+       height > tex->MaxSize) {
       /* XXX avoid this fallback */
       _swrast_CopyPixels(ctx, srcX, srcY, width, height, dstX, dstY, type);
       return;
@@ -942,20 +1267,6 @@ _mesa_meta_copy_pixels(GLcontext *ctx, GLint srcX, GLint srcY,
                           META_VERTEX |
                           META_VIEWPORT));
 
-   if (copypix->TexObj == 0) {
-      /* one-time setup */
-
-      /* create texture object */
-      _mesa_GenTextures(1, &copypix->TexObj);
-      _mesa_BindTexture(GL_TEXTURE_RECTANGLE, copypix->TexObj);
-      _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
-      _mesa_TexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, filter);
-      _mesa_TexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, filter);
-   }
-   else {
-      _mesa_BindTexture(GL_TEXTURE_RECTANGLE, copypix->TexObj);
-   }
-
    if (copypix->ArrayObj == 0) {
       /* one-time setup */
 
@@ -966,13 +1277,13 @@ _mesa_meta_copy_pixels(GLcontext *ctx, GLint srcX, GLint srcY,
       /* create vertex array buffer */
       _mesa_GenBuffersARB(1, &copypix->VBO);
       _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, copypix->VBO);
-      _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(copypix->verts),
-                          copypix->verts, GL_STREAM_DRAW_ARB);
+      _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
+                          NULL, GL_DYNAMIC_DRAW_ARB);
 
       /* setup vertex arrays */
-      _mesa_VertexPointer(3, GL_FLOAT, sizeof(copypix->verts[0]),
-                          (void*) (0 * sizeof(GLfloat)));
-      _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(copypix->verts[0]),
+      _mesa_VertexPointer(3, GL_FLOAT, sizeof(verts[0]),
+                          (void *) (0 * sizeof(GLfloat)));
+      _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(verts[0]),
                             (void *) (3 * sizeof(GLfloat)));
       _mesa_EnableClientState(GL_VERTEX_ARRAY);
       _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
@@ -982,66 +1293,51 @@ _mesa_meta_copy_pixels(GLcontext *ctx, GLint srcX, GLint srcY,
       _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, copypix->VBO);
    }
 
-   /* vertex positions, texcoords */
-   copypix->verts[0][0] = (GLfloat) dstX;
-   copypix->verts[0][1] = (GLfloat) dstY;
-   copypix->verts[0][2] = z;
-   copypix->verts[0][3] = 0.0F;
-   copypix->verts[0][4] = 0.0F;
-   copypix->verts[1][0] = (GLfloat) dstX1;
-   copypix->verts[1][1] = (GLfloat) dstY;
-   copypix->verts[1][2] = z;
-   copypix->verts[1][3] = (GLfloat) width;
-   copypix->verts[1][4] = 0.0F;
-   copypix->verts[2][0] = (GLfloat) dstX1;
-   copypix->verts[2][1] = (GLfloat) dstY1;
-   copypix->verts[2][2] = z;
-   copypix->verts[2][3] = (GLfloat) width;
-   copypix->verts[2][4] = (GLfloat) height;
-   copypix->verts[3][0] = (GLfloat) dstX;
-   copypix->verts[3][1] = (GLfloat) dstY1;
-   copypix->verts[3][2] = z;
-   copypix->verts[3][3] = 0.0F;
-   copypix->verts[3][4] = (GLfloat) height;
-
-   /* upload new vertex data */
-   _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0,
-                          sizeof(copypix->verts), copypix->verts);
-
-   /* copy framebuffer image to texture */
-   if (type == GL_COLOR) {
-      if (copypix->TexWidth == width &&
-          copypix->TexHeight == height &&
-          copypix->TexType == type) {
-         /* replace existing tex image */
-         _mesa_CopyTexSubImage2D(GL_TEXTURE_RECTANGLE, 0,
-                                 0, 0, srcX, srcY, width, height);
-      }
-      else {
-         /* create new tex image */
-         _mesa_CopyTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA,
-                              srcX, srcY, width, height, 0);
-         copypix->TexWidth = width;
-         copypix->TexHeight = height;
-         copypix->TexType = type;
-      }
-   }
-   else if (type == GL_DEPTH) {
-      /* TO-DO: Use a GL_DEPTH_COMPONENT texture and a fragment program/shader
-       * that replaces the fragment.z value.
-       */
-   }
-   else {
-      ASSERT(type == GL_STENCIL);
-      /* have to use sw fallback */
-   }
+   newTex = alloc_texture(tex, width, height, intFormat);
 
-   _mesa_Enable(GL_TEXTURE_RECTANGLE);
+   /* vertex positions, texcoords (after texture allocation!) */
+   {
+      const GLfloat dstX0 = (GLfloat) dstX;
+      const GLfloat dstY0 = (GLfloat) dstY;
+      const GLfloat dstX1 = dstX + width * ctx->Pixel.ZoomX;
+      const GLfloat dstY1 = dstY + height * ctx->Pixel.ZoomY;
+      const GLfloat z = ctx->Current.RasterPos[2];
+
+      verts[0][0] = dstX0;
+      verts[0][1] = dstY0;
+      verts[0][2] = z;
+      verts[0][3] = 0.0F;
+      verts[0][4] = 0.0F;
+      verts[1][0] = dstX1;
+      verts[1][1] = dstY0;
+      verts[1][2] = z;
+      verts[1][3] = tex->Sright;
+      verts[1][4] = 0.0F;
+      verts[2][0] = dstX1;
+      verts[2][1] = dstY1;
+      verts[2][2] = z;
+      verts[2][3] = tex->Sright;
+      verts[2][4] = tex->Ttop;
+      verts[3][0] = dstX0;
+      verts[3][1] = dstY1;
+      verts[3][2] = z;
+      verts[3][3] = 0.0F;
+      verts[3][4] = tex->Ttop;
+
+      /* upload new vertex data */
+      _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
+   }
+
+   /* Alloc/setup texture */
+   setup_copypix_texture(tex, newTex, srcX, srcY, width, height,
+                         GL_RGBA, GL_NEAREST);
+
+   _mesa_set_enable(ctx, tex->Target, GL_TRUE);
 
    /* draw textured quad */
    _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
 
-   _mesa_Disable(GL_TEXTURE_RECTANGLE);
+   _mesa_set_enable(ctx, tex->Target, GL_FALSE);
 
    _mesa_meta_end(ctx);
 }
@@ -1055,23 +1351,26 @@ _mesa_meta_copy_pixels(GLcontext *ctx, GLint srcX, GLint srcY,
  */
 static void
 tiled_draw_pixels(GLcontext *ctx,
+                  GLint tileSize,
                   GLint x, GLint y, GLsizei width, GLsizei height,
                   GLenum format, GLenum type,
                   const struct gl_pixelstore_attrib *unpack,
                   const GLvoid *pixels)
 {
-   const GLint maxSize = ctx->Const.MaxTextureRectSize;
    struct gl_pixelstore_attrib tileUnpack = *unpack;
    GLint i, j;
 
-   for (i = 0; i < width; i += maxSize) {
-      const GLint tileWidth = MIN2(maxSize, width - i);
+   if (tileUnpack.RowLength == 0)
+      tileUnpack.RowLength = width;
+
+   for (i = 0; i < width; i += tileSize) {
+      const GLint tileWidth = MIN2(tileSize, width - i);
       const GLint tileX = (GLint) (x + i * ctx->Pixel.ZoomX);
 
       tileUnpack.SkipPixels = unpack->SkipPixels + i;
 
-      for (j = 0; j < height; j += maxSize) {
-         const GLint tileHeight = MIN2(maxSize, height - j);
+      for (j = 0; j < height; j += tileSize) {
+         const GLint tileHeight = MIN2(tileSize, height - j);
          const GLint tileY = (GLint) (y + j * ctx->Pixel.ZoomY);
 
          tileUnpack.SkipRows = unpack->SkipRows + j;
@@ -1084,10 +1383,107 @@ tiled_draw_pixels(GLcontext *ctx,
 }
 
 
+/**
+ * One-time init for drawing stencil pixels.
+ */
+static void
+init_draw_stencil_pixels(GLcontext *ctx)
+{
+   /* This program is run eight times, once for each stencil bit.
+    * The stencil values to draw are found in an 8-bit alpha texture.
+    * We read the texture/stencil value and test if bit 'b' is set.
+    * If the bit is not set, use KIL to kill the fragment.
+    * Finally, we use the stencil test to update the stencil buffer.
+    *
+    * The basic algorithm for checking if a bit is set is:
+    *   if (is_odd(value / (1 << bit)))
+    *      result is one (or non-zero).
+    *   else
+    *      result is zero.
+    * The program parameter contains three values:
+    *   parm.x = 255 / (1 << bit)
+    *   parm.y = 0.5
+    *   parm.z = 0.0
+    */
+   static const char *program =
+      "!!ARBfp1.0\n"
+      "PARAM parm = program.local[0]; \n"
+      "TEMP t; \n"
+      "TEX t, fragment.texcoord[0], texture[0], %s; \n"   /* NOTE %s here! */
+      "# t = t * 255 / bit \n"
+      "MUL t.x, t.a, parm.x; \n"
+      "# t = (int) t \n"
+      "FRC t.y, t.x; \n"
+      "SUB t.x, t.x, t.y; \n"
+      "# t = t * 0.5 \n"
+      "MUL t.x, t.x, parm.y; \n"
+      "# t = fract(t.x) \n"
+      "FRC t.x, t.x; # if t.x != 0, then the bit is set \n"
+      "# t.x = (t.x == 0 ? 1 : 0) \n"
+      "SGE t.x, -t.x, parm.z; \n"
+      "KIL -t.x; \n"
+      "# for debug only \n"
+      "#MOV result.color, t.x; \n"
+      "END \n";
+   char program2[1000];
+   struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
+   struct temp_texture *tex = get_temp_texture(ctx);
+   const char *texTarget;
+
+   assert(drawpix->StencilFP == 0);
+
+   /* replace %s with "RECT" or "2D" */
+   assert(strlen(program) + 4 < sizeof(program2));
+   if (tex->Target == GL_TEXTURE_RECTANGLE)
+      texTarget = "RECT";
+   else
+      texTarget = "2D";
+   _mesa_snprintf(program2, sizeof(program2), program, texTarget);
+
+   _mesa_GenPrograms(1, &drawpix->StencilFP);
+   _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
+   _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
+                          strlen(program2), (const GLubyte *) program2);
+}
+
+
+/**
+ * One-time init for drawing depth pixels.
+ */
+static void
+init_draw_depth_pixels(GLcontext *ctx)
+{
+   static const char *program =
+      "!!ARBfp1.0\n"
+      "PARAM color = program.local[0]; \n"
+      "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
+      "MOV result.color, color; \n"
+      "END \n";
+   char program2[200];
+   struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
+   struct temp_texture *tex = get_temp_texture(ctx);
+   const char *texTarget;
+
+   assert(drawpix->DepthFP == 0);
+
+   /* replace %s with "RECT" or "2D" */
+   assert(strlen(program) + 4 < sizeof(program2));
+   if (tex->Target == GL_TEXTURE_RECTANGLE)
+      texTarget = "RECT";
+   else
+      texTarget = "2D";
+   _mesa_snprintf(program2, sizeof(program2), program, texTarget);
+
+   _mesa_GenPrograms(1, &drawpix->DepthFP);
+   _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
+   _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
+                          strlen(program2), (const GLubyte *) program2);
+}
+
+
 /**
  * Meta implementation of ctx->Driver.DrawPixels() in terms
  * of texture mapping and polygon rendering.
- * Note: this function requires GL_ARB_texture_rectangle support.
  */
 void
 _mesa_meta_draw_pixels(GLcontext *ctx,
@@ -1096,16 +1492,14 @@ _mesa_meta_draw_pixels(GLcontext *ctx,
                       const struct gl_pixelstore_attrib *unpack,
                       const GLvoid *pixels)
 {
-   const GLenum filter = GL_NEAREST;
    struct drawpix_state *drawpix = &ctx->Meta->DrawPix;
-   const GLfloat z = ctx->Current.RasterPos[2];
-   const GLfloat x1 = x + width * ctx->Pixel.ZoomX;
-   const GLfloat y1 = y + height * ctx->Pixel.ZoomY;
+   struct temp_texture *tex = get_temp_texture(ctx);
    const struct gl_pixelstore_attrib unpackSave = ctx->Unpack;
+   const GLuint origStencilMask = ctx->Stencil.WriteMask[0];
+   GLfloat verts[4][5]; /* four verts of X,Y,Z,S,T */
    GLenum texIntFormat;
-   GLboolean fallback;
-
-   ASSERT(ctx->Extensions.NV_texture_rectangle);
+   GLboolean fallback, newTex;
+   GLbitfield metaExtraSave = 0x0;
 
    /*
     * Determine if we can do the glDrawPixels with texture mapping.
@@ -1117,7 +1511,41 @@ _mesa_meta_draw_pixels(GLcontext *ctx,
    }
 
    if (_mesa_is_color_format(format)) {
-      texIntFormat = GL_RGBA;
+      /* use more compact format when possible */
+      /* XXX disable special case for GL_LUMINANCE for now to work around
+       * apparent i965 driver bug (see bug #23670).
+       */
+      if (/*format == GL_LUMINANCE ||*/ format == GL_LUMINANCE_ALPHA)
+         texIntFormat = format;
+      else
+         texIntFormat = GL_RGBA;
+   }
+   else if (_mesa_is_stencil_format(format)) {
+      if (ctx->Extensions.ARB_fragment_program &&
+          type == GL_UNSIGNED_BYTE) {
+         /* We'll store stencil as alpha.  This only works for GLubyte
+          * image data because of how incoming values are mapped to alpha
+          * in [0,1].
+          */
+         texIntFormat = GL_ALPHA;
+         metaExtraSave = (META_COLOR_MASK |
+                          META_DEPTH_TEST |
+                          META_SHADER |
+                          META_STENCIL_TEST);
+      }
+      else {
+         fallback = GL_TRUE;
+      }
+   }
+   else if (_mesa_is_depth_format(format)) {
+      if (ctx->Extensions.ARB_depth_texture &&
+          ctx->Extensions.ARB_fragment_program) {
+         texIntFormat = GL_DEPTH_COMPONENT;
+         metaExtraSave = (META_SHADER);
+      }
+      else {
+         fallback = GL_TRUE;
+      }
    }
    else {
       fallback = GL_TRUE;
@@ -1132,36 +1560,22 @@ _mesa_meta_draw_pixels(GLcontext *ctx,
    /*
     * Check image size against max texture size, draw as tiles if needed.
     */
-   if (width > ctx->Const.MaxTextureRectSize ||
-       height > ctx->Const.MaxTextureRectSize) {
-      tiled_draw_pixels(ctx, x, y, width, height,
+   if (width > tex->MaxSize || height > tex->MaxSize) {
+      tiled_draw_pixels(ctx, tex->MaxSize, x, y, width, height,
                         format, type, unpack, pixels);
       return;
    }
 
-   /* Most GL state applies to glDrawPixels, but a there's a few things
-    * we need to override:
+   /* Most GL state applies to glDrawPixels (like blending, stencil, etc),
+    * but a there's a few things we need to override:
     */
    _mesa_meta_begin(ctx, (META_RASTERIZATION |
                           META_SHADER |
                           META_TEXTURE |
                           META_TRANSFORM |
                           META_VERTEX |
-                          META_VIEWPORT));
-
-   if (drawpix->TexObj == 0) {
-      /* one-time setup */
-
-      /* create texture object */
-      _mesa_GenTextures(1, &drawpix->TexObj);
-      _mesa_BindTexture(GL_TEXTURE_RECTANGLE, drawpix->TexObj);
-      _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
-      _mesa_TexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, filter);
-      _mesa_TexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, filter);
-   }
-   else {
-      _mesa_BindTexture(GL_TEXTURE_RECTANGLE, drawpix->TexObj);
-   }
+                          META_VIEWPORT |
+                          metaExtraSave));
 
    if (drawpix->ArrayObj == 0) {
       /* one-time setup */
@@ -1173,13 +1587,13 @@ _mesa_meta_draw_pixels(GLcontext *ctx,
       /* create vertex array buffer */
       _mesa_GenBuffersARB(1, &drawpix->VBO);
       _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, drawpix->VBO);
-      _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(drawpix->verts),
-                          drawpix->verts, GL_STREAM_DRAW_ARB);
+      _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
+                          NULL, GL_DYNAMIC_DRAW_ARB);
 
       /* setup vertex arrays */
-      _mesa_VertexPointer(3, GL_FLOAT, sizeof(drawpix->verts[0]),
-                          (void*) (0 * sizeof(GLfloat)));
-      _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(drawpix->verts[0]),
+      _mesa_VertexPointer(3, GL_FLOAT, sizeof(verts[0]),
+                          (void *) (0 * sizeof(GLfloat)));
+      _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(verts[0]),
                             (void *) (3 * sizeof(GLfloat)));
       _mesa_EnableClientState(GL_VERTEX_ARRAY);
       _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
@@ -1189,61 +1603,418 @@ _mesa_meta_draw_pixels(GLcontext *ctx,
       _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, drawpix->VBO);
    }
 
-   /* vertex positions, texcoords */
-   drawpix->verts[0][0] = (GLfloat) x;
-   drawpix->verts[0][1] = (GLfloat) y;
-   drawpix->verts[0][2] = z;
-   drawpix->verts[0][3] = 0.0F;
-   drawpix->verts[0][4] = 0.0F;
-   drawpix->verts[1][0] = (GLfloat) x1;
-   drawpix->verts[1][1] = (GLfloat) y;
-   drawpix->verts[1][2] = z;
-   drawpix->verts[1][3] = (GLfloat) width;
-   drawpix->verts[1][4] = 0.0F;
-   drawpix->verts[2][0] = (GLfloat) x1;
-   drawpix->verts[2][1] = (GLfloat) y1;
-   drawpix->verts[2][2] = z;
-   drawpix->verts[2][3] = (GLfloat) width;
-   drawpix->verts[2][4] = (GLfloat) height;
-   drawpix->verts[3][0] = (GLfloat) x;
-   drawpix->verts[3][1] = (GLfloat) y1;
-   drawpix->verts[3][2] = z;
-   drawpix->verts[3][3] = 0.0F;
-   drawpix->verts[3][4] = (GLfloat) height;
-
-   /* upload new vertex data */
-   _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0,
-                          sizeof(drawpix->verts), drawpix->verts);
+   newTex = alloc_texture(tex, width, height, texIntFormat);
+
+   /* vertex positions, texcoords (after texture allocation!) */
+   {
+      const GLfloat x0 = (GLfloat) x;
+      const GLfloat y0 = (GLfloat) y;
+      const GLfloat x1 = x + width * ctx->Pixel.ZoomX;
+      const GLfloat y1 = y + height * ctx->Pixel.ZoomY;
+      const GLfloat z = ctx->Current.RasterPos[2];
+
+      verts[0][0] = x0;
+      verts[0][1] = y0;
+      verts[0][2] = z;
+      verts[0][3] = 0.0F;
+      verts[0][4] = 0.0F;
+      verts[1][0] = x1;
+      verts[1][1] = y0;
+      verts[1][2] = z;
+      verts[1][3] = tex->Sright;
+      verts[1][4] = 0.0F;
+      verts[2][0] = x1;
+      verts[2][1] = y1;
+      verts[2][2] = z;
+      verts[2][3] = tex->Sright;
+      verts[2][4] = tex->Ttop;
+      verts[3][0] = x0;
+      verts[3][1] = y1;
+      verts[3][2] = z;
+      verts[3][3] = 0.0F;
+      verts[3][4] = tex->Ttop;
+
+      /* upload new vertex data */
+      _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
+   }
 
    /* set given unpack params */
-   ctx->Unpack = *unpack; /* XXX bufobj */
+   ctx->Unpack = *unpack;
 
-   /* copy pixel data to texture */
-   if (drawpix->TexWidth == width &&
-       drawpix->TexHeight == height &&
-       drawpix->TexIntFormat == texIntFormat) {
-      /* replace existing tex image */
-      _mesa_TexSubImage2D(GL_TEXTURE_RECTANGLE, 0,
-                          0, 0, width, height, format, type, pixels);
+   _mesa_set_enable(ctx, tex->Target, GL_TRUE);
+
+   if (_mesa_is_stencil_format(format)) {
+      /* Drawing stencil */
+      GLint bit;
+
+      if (!drawpix->StencilFP)
+         init_draw_stencil_pixels(ctx);
+
+      setup_drawpix_texture(tex, newTex, texIntFormat, width, height,
+                            GL_ALPHA, type, pixels);
+
+      _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+
+      _mesa_set_enable(ctx, GL_STENCIL_TEST, GL_TRUE);
+      _mesa_StencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
+
+      _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->StencilFP);
+      _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
+
+      for (bit = 0; bit < ctx->Visual.stencilBits; bit++) {
+         const GLuint mask = 1 << bit;
+         if (mask & origStencilMask) {
+            _mesa_StencilFunc(GL_ALWAYS, mask, mask);
+            _mesa_StencilMask(mask);
+
+            _mesa_ProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0,
+                                             255.0 / mask, 0.5, 0.0, 0.0);
+
+            _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
+         }
+      }
+   }
+   else if (_mesa_is_depth_format(format)) {
+      /* Drawing depth */
+      if (!drawpix->DepthFP)
+         init_draw_depth_pixels(ctx);
+
+      _mesa_BindProgram(GL_FRAGMENT_PROGRAM_ARB, drawpix->DepthFP);
+      _mesa_set_enable(ctx, GL_FRAGMENT_PROGRAM_ARB, GL_TRUE);
+
+      /* polygon color = current raster color */
+      _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
+                                        ctx->Current.RasterColor);
+
+      setup_drawpix_texture(tex, newTex, texIntFormat, width, height,
+                            format, type, pixels);
+
+      _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
    }
    else {
-      /* create new tex image */
-      _mesa_TexImage2D(GL_TEXTURE_RECTANGLE, 0, texIntFormat,
-                       width, height, 0, format, type, pixels);
-      drawpix->TexWidth = width;
-      drawpix->TexHeight = height;
-      drawpix->TexIntFormat = texIntFormat;
+      /* Drawing RGBA */
+      setup_drawpix_texture(tex, newTex, texIntFormat, width, height,
+                            format, type, pixels);
+      _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
    }
 
+   _mesa_set_enable(ctx, tex->Target, GL_FALSE);
+
    /* restore unpack params */
-   ctx->Unpack = unpackSave; /* XXX bufobj */
+   ctx->Unpack = unpackSave;
+
+   _mesa_meta_end(ctx);
+}
 
-   _mesa_Enable(GL_TEXTURE_RECTANGLE);
 
-   /* draw textured quad */
-   _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
+/**
+ * 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.
+ */
+void
+_mesa_meta_bitmap(GLcontext *ctx,
+                  GLint x, GLint y, GLsizei width, GLsizei height,
+                  const struct gl_pixelstore_attrib *unpack,
+                  const GLubyte *bitmap1)
+{
+   struct bitmap_state *bitmap = &ctx->Meta->Bitmap;
+   struct temp_texture *tex = get_bitmap_temp_texture(ctx);
+   const GLenum texIntFormat = GL_ALPHA;
+   const struct gl_pixelstore_attrib unpackSave = *unpack;
+   GLfloat verts[4][9]; /* four verts of X,Y,Z,S,T,R,G,B,A */
+   GLboolean newTex;
+   GLubyte *bitmap8;
+
+   /*
+    * Check if swrast fallback is needed.
+    */
+   if (ctx->_ImageTransferState ||
+       ctx->Color.AlphaEnabled ||
+       ctx->Fog.Enabled ||
+       ctx->Texture._EnabledUnits ||
+       width > tex->MaxSize ||
+       height > tex->MaxSize) {
+      _swrast_Bitmap(ctx, x, y, width, height, unpack, bitmap1);
+      return;
+   }
 
-   _mesa_Disable(GL_TEXTURE_RECTANGLE);
+   /* Most GL state applies to glBitmap (like blending, stencil, etc),
+    * but a there's a few things we need to override:
+    */
+   _mesa_meta_begin(ctx, (META_ALPHA_TEST |
+                          META_PIXEL_STORE |
+                          META_RASTERIZATION |
+                          META_SHADER |
+                          META_TEXTURE |
+                          META_TRANSFORM |
+                          META_VERTEX |
+                          META_VIEWPORT));
+
+   if (bitmap->ArrayObj == 0) {
+      /* one-time setup */
+
+      /* create vertex array object */
+      _mesa_GenVertexArraysAPPLE(1, &bitmap->ArrayObj);
+      _mesa_BindVertexArrayAPPLE(bitmap->ArrayObj);
+
+      /* create vertex array buffer */
+      _mesa_GenBuffersARB(1, &bitmap->VBO);
+      _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
+      _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
+                          NULL, GL_DYNAMIC_DRAW_ARB);
+
+      /* setup vertex arrays */
+      _mesa_VertexPointer(3, GL_FLOAT, sizeof(verts[0]),
+                          (void *) (0 * sizeof(GLfloat)));
+      _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(verts[0]),
+                            (void *) (3 * sizeof(GLfloat)));
+      _mesa_ColorPointer(4, GL_FLOAT, sizeof(verts[0]),
+                         (void *) (5 * sizeof(GLfloat)));
+
+      _mesa_EnableClientState(GL_VERTEX_ARRAY);
+      _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
+      _mesa_EnableClientState(GL_COLOR_ARRAY);
+   }
+   else {
+      _mesa_BindVertexArray(bitmap->ArrayObj);
+      _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
+   }
+
+   newTex = alloc_texture(tex, width, height, texIntFormat);
+
+   /* vertex positions, texcoords, colors (after texture allocation!) */
+   {
+      const GLfloat x0 = (GLfloat) x;
+      const GLfloat y0 = (GLfloat) y;
+      const GLfloat x1 = (GLfloat) (x + width);
+      const GLfloat y1 = (GLfloat) (y + height);
+      const GLfloat z = ctx->Current.RasterPos[2];
+      GLuint i;
+
+      verts[0][0] = x0;
+      verts[0][1] = y0;
+      verts[0][2] = z;
+      verts[0][3] = 0.0F;
+      verts[0][4] = 0.0F;
+      verts[1][0] = x1;
+      verts[1][1] = y0;
+      verts[1][2] = z;
+      verts[1][3] = tex->Sright;
+      verts[1][4] = 0.0F;
+      verts[2][0] = x1;
+      verts[2][1] = y1;
+      verts[2][2] = z;
+      verts[2][3] = tex->Sright;
+      verts[2][4] = tex->Ttop;
+      verts[3][0] = x0;
+      verts[3][1] = y1;
+      verts[3][2] = z;
+      verts[3][3] = 0.0F;
+      verts[3][4] = tex->Ttop;
+
+      for (i = 0; i < 4; i++) {
+         verts[i][5] = ctx->Current.RasterColor[0];
+         verts[i][6] = ctx->Current.RasterColor[1];
+         verts[i][7] = ctx->Current.RasterColor[2];
+         verts[i][8] = ctx->Current.RasterColor[3];
+      }
+
+      /* upload new vertex data */
+      _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
+   }
+
+   bitmap1 = _mesa_map_pbo_source(ctx, &unpackSave, bitmap1);
+   if (!bitmap1)
+      return;
+
+   bitmap8 = (GLubyte *) _mesa_calloc(width * height);
+   if (bitmap8) {
+      _mesa_expand_bitmap(width, height, &unpackSave, bitmap1,
+                          bitmap8, width, 0xff);
+
+      _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(tex, newTex, texIntFormat, width, height,
+                            GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8);
+
+      _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
+
+      _mesa_set_enable(ctx, tex->Target, GL_FALSE);
+
+      _mesa_free(bitmap8);
+   }
+
+   _mesa_unmap_pbo_source(ctx, &unpackSave);
+
+   _mesa_meta_end(ctx);
+}
+
+
+void
+_mesa_meta_generate_mipmap(GLcontext *ctx, GLenum target,
+                           struct gl_texture_object *texObj)
+{
+   struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
+   struct { GLfloat x, y, s, t, r; } verts[4];
+   const GLuint baseLevel = texObj->BaseLevel;
+   const GLuint maxLevel = texObj->MaxLevel;
+   const GLenum minFilterSave = texObj->MinFilter;
+   const GLenum magFilterSave = texObj->MagFilter;
+   const GLuint fboSave = ctx->DrawBuffer->Name;
+   GLenum faceTarget;
+   GLuint level;
+   GLuint border = 0;
+
+   /* check for fallbacks */
+   if (!ctx->Extensions.EXT_framebuffer_object) {
+      _mesa_generate_mipmap(ctx, target, texObj);
+      return;
+   }
+
+   if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X &&
+       target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z) {
+      faceTarget = target;
+      target = GL_TEXTURE_CUBE_MAP;
+   }
+   else {
+      faceTarget = target;
+   }
+
+   _mesa_meta_begin(ctx, META_ALL);
+
+   if (mipmap->ArrayObj == 0) {
+      /* one-time setup */
+
+      /* create vertex array object */
+      _mesa_GenVertexArraysAPPLE(1, &mipmap->ArrayObj);
+      _mesa_BindVertexArrayAPPLE(mipmap->ArrayObj);
+
+      /* create vertex array buffer */
+      _mesa_GenBuffersARB(1, &mipmap->VBO);
+      _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
+      _mesa_BufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts),
+                          NULL, GL_DYNAMIC_DRAW_ARB);
+
+      /* setup vertex arrays */
+      _mesa_VertexPointer(2, GL_FLOAT, sizeof(verts[0]),
+                          (void *) (0 * sizeof(GLfloat)));
+      _mesa_TexCoordPointer(3, GL_FLOAT, sizeof(verts[0]),
+                            (void *) (2 * sizeof(GLfloat)));
+
+      _mesa_EnableClientState(GL_VERTEX_ARRAY);
+      _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
+   }
+   else {
+      _mesa_BindVertexArray(mipmap->ArrayObj);
+      _mesa_BindBufferARB(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
+   }
+
+   if (!mipmap->FBO) {
+      /* Bind the new renderbuffer to the color attachment point. */
+      _mesa_GenFramebuffersEXT(1, &mipmap->FBO);
+   }
+
+   _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, mipmap->FBO);
+
+   _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+   _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+   _mesa_set_enable(ctx, target, GL_TRUE);
+
+   /* setup texcoords once (XXX what about border?) */
+   switch (faceTarget) {
+   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
+      break;
+   case GL_TEXTURE_2D:
+      verts[0].s = 0.0F;
+      verts[0].t = 0.0F;
+      verts[0].r = 0.0F;
+      verts[1].s = 1.0F;
+      verts[1].t = 0.0F;
+      verts[2].r = 0.0F;
+      verts[3].s = 1.0F;
+      verts[3].t = 1.0F;
+      verts[3].r = 0.0F;
+      verts[4].s = 0.0F;
+      verts[4].t = 1.0F;
+      verts[4].r = 0.0F;
+      break;
+   }
+
+
+   for (level = baseLevel + 1; level <= maxLevel; level++) {
+      const struct gl_texture_image *srcImage;
+      const GLuint srcLevel = level - 1;
+      GLsizei srcWidth, srcHeight;
+      GLsizei newWidth, newHeight;
+      GLenum status;
+
+      srcImage = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
+      assert(srcImage->Border == 0); /* XXX we can fix this */
+
+      srcWidth = srcImage->Width - 2 * border;
+      srcHeight = srcImage->Height - 2 * border;
+
+      newWidth = MAX2(1, srcWidth / 2) + 2 * border;
+      newHeight = MAX2(1, srcHeight / 2) + 2 * border;
+
+      if (newWidth == srcImage->Width && newHeight == srcImage->Height) {
+        break;
+      }
+
+      /* Create empty image */
+      _mesa_TexImage2D(GL_TEXTURE_2D, level, srcImage->InternalFormat,
+                      newWidth, newHeight, border,
+                      GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+
+      /* vertex positions */
+      {
+         verts[0].x = 0.0F;
+         verts[0].y = 0.0F;
+         verts[1].x = (GLfloat) newWidth;
+         verts[1].y = 0.0F;
+         verts[2].x = (GLfloat) newWidth;
+         verts[2].y = (GLfloat) newHeight;
+         verts[3].x = 0.0F;
+         verts[3].y = (GLfloat) newHeight;
+
+         /* upload new vertex data */
+         _mesa_BufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
+      }
+
+      /* limit sampling to src level */
+      _mesa_TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, srcLevel);
+      _mesa_TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, srcLevel);
+
+      /* Set to draw into the current level */
+      _mesa_FramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT,
+                                    GL_COLOR_ATTACHMENT0_EXT,
+                                    target,
+                                    texObj->Name,
+                                    level);
+
+      /* Choose to render to the color attachment. */
+      _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
+
+      status = _mesa_CheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT);
+      if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
+         abort();
+         break;
+      }
+
+      _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
+   }
 
    _mesa_meta_end(ctx);
+
+   _mesa_TexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilterSave);
+   _mesa_TexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilterSave);
+
+   /* restore (XXX add to meta_begin/end()? */
+   _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboSave);
 }