meta: Fold the glUseProgram() into the blit program generator.
[mesa.git] / src / mesa / drivers / common / meta.c
index f4995db594ee2b729d45a51e5e3c9e80d8b692e9..f0cd5a0a8eb9bc946665611757642584c13891d3 100644 (file)
@@ -164,7 +164,7 @@ struct save_state
    GLuint EnvMode;  /* unit[0] only */
 
    /** MESA_META_VERTEX */
-   struct gl_array_object *VAO;
+   struct gl_vertex_array_object *VAO;
    struct gl_buffer_object *ArrayBufferObj;
 
    /** MESA_META_VIEWPORT */
@@ -215,6 +215,30 @@ struct temp_texture
    GLfloat Sright, Ttop;  /**< right, top texcoords */
 };
 
+/**
+ * State for GLSL texture sampler which is used to generate fragment
+ * shader in _mesa_meta_generate_mipmap().
+ */
+struct glsl_sampler {
+   const char *type;
+   const char *func;
+   const char *texcoords;
+   GLuint shader_prog;
+};
+
+/**
+ * Table of all sampler types and shaders for accessing them.
+ */
+struct sampler_table {
+   struct glsl_sampler sampler_1d;
+   struct glsl_sampler sampler_2d;
+   struct glsl_sampler sampler_3d;
+   struct glsl_sampler sampler_rect;
+   struct glsl_sampler sampler_cubemap;
+   struct glsl_sampler sampler_1d_array;
+   struct glsl_sampler sampler_2d_array;
+   struct glsl_sampler sampler_cubemap_array;
+};
 
 /**
  * State for glBlitFramebufer()
@@ -224,8 +248,7 @@ struct blit_state
    GLuint VAO;
    GLuint VBO;
    GLuint DepthFP;
-   GLuint ShaderProg;
-   GLuint RectShaderProg;
+   struct sampler_table samplers;
    struct temp_texture depthTex;
 };
 
@@ -263,6 +286,7 @@ struct copypix_state
 struct drawpix_state
 {
    GLuint VAO;
+   GLuint VBO;
 
    GLuint StencilFP;  /**< Fragment program for drawing stencil images */
    GLuint DepthFP;  /**< Fragment program for drawing depth images */
@@ -279,17 +303,6 @@ struct bitmap_state
    struct temp_texture Tex;  /**< separate texture from other meta ops */
 };
 
-/**
- * State for GLSL texture sampler which is used to generate fragment
- * shader in _mesa_meta_generate_mipmap().
- */
-struct glsl_sampler {
-   const char *type;
-   const char *func;
-   const char *texcoords;
-   GLuint shader_prog;
-};
-
 /**
  * State for _mesa_meta_generate_mipmap()
  */
@@ -299,13 +312,8 @@ struct gen_mipmap_state
    GLuint VBO;
    GLuint FBO;
    GLuint Sampler;
-   GLuint ShaderProg;
-   struct glsl_sampler sampler_1d;
-   struct glsl_sampler sampler_2d;
-   struct glsl_sampler sampler_3d;
-   struct glsl_sampler sampler_cubemap;
-   struct glsl_sampler sampler_1d_array;
-   struct glsl_sampler sampler_2d_array;
+
+   struct sampler_table samplers;
 };
 
 /**
@@ -316,6 +324,8 @@ struct decompress_state
    GLuint VAO;
    GLuint VBO, FBO, RBO, Sampler;
    GLint Width, Height;
+
+   struct sampler_table samplers;
 };
 
 /**
@@ -350,12 +360,21 @@ struct gl_meta_state
    struct drawtex_state DrawTex;  /**< For _mesa_meta_DrawTex() */
 };
 
+struct vertex {
+   GLfloat x, y, z, tex[4];
+   GLfloat r, g, b, a;
+};
+
+static struct glsl_sampler *
+setup_texture_sampler(GLenum target, struct sampler_table *table);
+
 static void meta_glsl_blit_cleanup(struct blit_state *blit);
 static void cleanup_temp_texture(struct temp_texture *tex);
 static void meta_glsl_clear_cleanup(struct clear_state *clear);
 static void meta_glsl_generate_mipmap_cleanup(struct gen_mipmap_state *mipmap);
 static void meta_decompress_cleanup(struct decompress_state *decompress);
 static void meta_drawpix_cleanup(struct drawpix_state *drawpix);
+static void sampler_table_cleanup(struct sampler_table *table);
 
 static GLuint
 compile_shader_with_debug(struct gl_context *ctx, GLenum target, const GLcharARB *source)
@@ -384,7 +403,7 @@ compile_shader_with_debug(struct gl_context *ctx, GLenum target, const GLcharARB
       return 0;
    }
 
-   _mesa_GetProgramInfoLog(shader, size, NULL, info);
+   _mesa_GetShaderInfoLog(shader, size, NULL, info);
    _mesa_problem(ctx,
                 "meta program compile failed:\n%s\n"
                 "source:\n%s\n",
@@ -424,6 +443,192 @@ link_program_with_debug(struct gl_context *ctx, GLuint program)
    return 0;
 }
 
+/**
+ * Generate a generic shader to blit from a texture to a framebuffer
+ *
+ * \param ctx       Current GL context
+ * \param texTarget Texture target that will be the source of the blit
+ *
+ * \returns a handle to a shader program on success or zero on failure.
+ */
+static void
+setup_blit_shader(struct gl_context *ctx,
+                  GLenum target,
+                  struct sampler_table *table)
+{
+   const char *vs_source;
+   char *fs_source;
+   GLuint vs, fs;
+   void *const mem_ctx = ralloc_context(NULL);
+   struct glsl_sampler *sampler =
+      setup_texture_sampler(target, table);
+
+   assert(sampler != NULL);
+
+   if (sampler->shader_prog != 0) {
+      _mesa_UseProgram(sampler->shader_prog);
+      return;
+   }
+
+   /* The version check is a little tricky.  API is set to API_OPENGLES2 even
+    * for OpenGL ES 3.0 contexts, and GLSLVersion may be set to 140, for
+    * example, in an OpenGL ES 2.0 context.
+    */
+   if ((ctx->API == API_OPENGLES2 && ctx->Version < 30)
+       || ctx->Const.GLSLVersion < 130) {
+      vs_source =
+         "attribute vec2 position;\n"
+         "attribute vec3 textureCoords;\n"
+         "varying vec4 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"
+                                  "#extension GL_ARB_texture_cube_map_array: enable\n"
+                                  "#ifdef GL_ES\n"
+                                  "precision highp float;\n"
+                                  "#endif\n"
+                                  "uniform %s texSampler;\n"
+                                  "varying vec4 texCoords;\n"
+                                  "void main()\n"
+                                  "{\n"
+                                  "   gl_FragColor = %s(texSampler, %s);\n"
+                                  "   gl_FragDepth = gl_FragColor.x;\n"
+                                  "}\n",
+                                  sampler->type,
+                                  sampler->func, sampler->texcoords);
+   }
+   else {
+      vs_source = ralloc_asprintf(mem_ctx,
+                                  "#version %s\n"
+                                  "in vec2 position;\n"
+                                  "in vec4 textureCoords;\n"
+                                  "out vec4 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"
+                                  "#extension GL_ARB_texture_cube_map_array: enable\n"
+                                  "#ifdef GL_ES\n"
+                                  "precision highp float;\n"
+                                  "#endif\n"
+                                  "uniform %s texSampler;\n"
+                                  "in vec4 texCoords;\n"
+                                  "out vec4 out_color;\n"
+                                  "\n"
+                                  "void main()\n"
+                                  "{\n"
+                                  "   out_color = texture(texSampler, %s);\n"
+                                  "   gl_FragDepth = out_color.x;\n"
+                                  "}\n",
+                                  _mesa_is_desktop_gl(ctx) ? "130" : "300 es",
+                                  sampler->type,
+                                  sampler->texcoords);
+   }
+
+   vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_source);
+   fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_source);
+
+   sampler->shader_prog = _mesa_CreateProgramObjectARB();
+   _mesa_AttachShader(sampler->shader_prog, fs);
+   _mesa_DeleteObjectARB(fs);
+   _mesa_AttachShader(sampler->shader_prog, vs);
+   _mesa_DeleteObjectARB(vs);
+   _mesa_BindAttribLocation(sampler->shader_prog, 0, "position");
+   _mesa_BindAttribLocation(sampler->shader_prog, 1, "texcoords");
+   link_program_with_debug(ctx, sampler->shader_prog);
+   ralloc_free(mem_ctx);
+
+   _mesa_UseProgram(sampler->shader_prog);
+}
+
+/**
+ * Configure vertex buffer and vertex array objects for tests
+ *
+ * Regardless of whether a new VAO and new VBO are created, the objects
+ * referenced by \c VAO and \c VBO will be bound into the GL state vector
+ * when this function terminates.
+ *
+ * \param VAO       Storage for vertex array object handle.  If 0, a new VAO
+ *                  will be created.
+ * \param VBO       Storage for vertex buffer object handle.  If 0, a new VBO
+ *                  will be created.  The new VBO will have storage for 4
+ *                  \c vertex structures.
+ * \param use_generic_attributes  Should generic attributes 0 and 1 be used,
+ *                  or should traditional, fixed-function color and texture
+ *                  coordinate be used?
+ * \param vertex_size  Number of components for attribute 0 / vertex.
+ * \param texcoord_size  Number of components for attribute 1 / texture
+ *                  coordinate.  If this is 0, attribute 1 will not be set or
+ *                  enabled.
+ * \param color_size  Number of components for attribute 1 / primary color.
+ *                  If this is 0, attribute 1 will not be set or enabled.
+ *
+ * \note If \c use_generic_attributes is \c true, \c color_size must be zero.
+ * Use \c texcoord_size instead.
+ */
+static void
+setup_vertex_objects(GLuint *VAO, GLuint *VBO, bool use_generic_attributes,
+                     unsigned vertex_size, unsigned texcoord_size,
+                     unsigned color_size)
+{
+   if (*VAO == 0) {
+      assert(*VBO == 0);
+
+      /* create vertex array object */
+      _mesa_GenVertexArrays(1, VAO);
+      _mesa_BindVertexArray(*VAO);
+
+      /* create vertex array buffer */
+      _mesa_GenBuffers(1, VBO);
+      _mesa_BindBuffer(GL_ARRAY_BUFFER, *VBO);
+      _mesa_BufferData(GL_ARRAY_BUFFER, 4 * sizeof(struct vertex), NULL,
+                       GL_DYNAMIC_DRAW);
+
+      /* setup vertex arrays */
+      if (use_generic_attributes) {
+         assert(color_size == 0);
+
+         _mesa_VertexAttribPointer(0, vertex_size, GL_FLOAT, GL_FALSE,
+                                   sizeof(struct vertex), OFFSET(x));
+         _mesa_EnableVertexAttribArray(0);
+
+         if (texcoord_size > 0) {
+            _mesa_VertexAttribPointer(1, texcoord_size, GL_FLOAT, GL_FALSE,
+                                      sizeof(struct vertex), OFFSET(tex));
+            _mesa_EnableVertexAttribArray(1);
+         }
+      } else {
+         _mesa_VertexPointer(vertex_size, GL_FLOAT, sizeof(struct vertex),
+                             OFFSET(x));
+         _mesa_EnableClientState(GL_VERTEX_ARRAY);
+
+         if (texcoord_size > 0) {
+            _mesa_TexCoordPointer(texcoord_size, GL_FLOAT,
+                                  sizeof(struct vertex), OFFSET(tex));
+            _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
+         }
+
+         if (color_size > 0) {
+            _mesa_ColorPointer(color_size, GL_FLOAT,
+                               sizeof(struct vertex), OFFSET(r));
+            _mesa_EnableClientState(GL_COLOR_ARRAY);
+         }
+      }
+   } else {
+      _mesa_BindVertexArray(*VAO);
+      _mesa_BindBuffer(GL_ARRAY_BUFFER, *VBO);
+   }
+}
+
 /**
  * Initialize meta-ops for a context.
  * To be called once during context creation.
@@ -729,7 +934,7 @@ _mesa_meta_begin(struct gl_context *ctx, GLbitfield state)
 
    if (state & MESA_META_VERTEX) {
       /* save vertex array object state */
-      _mesa_reference_array_object(ctx, &save->VAO,
+      _mesa_reference_vao(ctx, &save->VAO,
                                    ctx->Array.VAO);
       _mesa_reference_buffer_object(ctx, &save->ArrayBufferObj,
                                     ctx->Array.ArrayBufferObj);
@@ -1096,7 +1301,7 @@ _mesa_meta_end(struct gl_context *ctx)
 
       /* restore vertex array object */
       _mesa_BindVertexArray(save->VAO->Name);
-      _mesa_reference_array_object(ctx, &save->VAO, NULL);
+      _mesa_reference_vao(ctx, &save->VAO, NULL);
    }
 
    if (state & MESA_META_VIEWPORT) {
@@ -1379,7 +1584,6 @@ static void
 setup_drawpix_texture(struct gl_context *ctx,
                      struct temp_texture *tex,
                       GLboolean newTex,
-                      GLenum texIntFormat,
                       GLsizei width, GLsizei height,
                       GLenum format, GLenum type,
                       const GLvoid *pixels)
@@ -1456,38 +1660,13 @@ init_blit_depth_pixels(struct gl_context *ctx)
 }
 
 static void
-setup_ff_blit_framebuffer(struct blit_state *blit)
+setup_ff_tnl_for_blit(GLuint *VAO, GLuint *VBO, unsigned texcoord_size)
 {
-   struct vertex {
-      GLfloat x, y, s, t;
-   };
-   struct vertex verts[4];
-
-   if (blit->VAO == 0) {
-      /* one-time setup */
-
-      /* create vertex array object */
-      _mesa_GenVertexArrays(1, &blit->VAO);
-      _mesa_BindVertexArray(blit->VAO);
-
-      /* 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_vertex_objects(VAO, VBO, false, 2, texcoord_size, 0);
 
    /* 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
@@ -1495,123 +1674,12 @@ setup_glsl_blit_framebuffer(struct gl_context *ctx,
                             struct blit_state *blit,
                             GLenum target)
 {
-   struct vertex {
-      GLfloat x, y, s, t;
-   };
-   struct vertex verts[4];
-   const char *vs_source;
-   char *fs_source;
-   GLuint vs, fs;
-   void *mem_ctx;
-   GLuint ShaderProg;
-   GLboolean texture_2d = (target == GL_TEXTURE_2D);
-
    /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
-   assert(_mesa_is_desktop_gl(ctx) || texture_2d);
-
-   /* Check if already initialized */
-   if (blit->VAO == 0) {
-
-      /* create vertex array object */
-      _mesa_GenVertexArrays(1, &blit->VAO);
-      _mesa_BindVertexArray(blit->VAO);
-
-      /* 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);
+   assert(_mesa_is_desktop_gl(ctx) || target == GL_TEXTURE_2D);
 
-      /* 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));
-
-      _mesa_EnableVertexAttribArray(0);
-      _mesa_EnableVertexAttribArray(1);
-   }
-
-   /* Generate a relevant fragment shader program for the texture target */
-   if ((target == GL_TEXTURE_2D && blit->ShaderProg != 0) ||
-       (target == GL_TEXTURE_RECTANGLE && blit->RectShaderProg != 0)) {
-      return;
-   }
-
-   mem_ctx = ralloc_context(NULL);
-
-   if (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";
+   setup_vertex_objects(&blit->VAO, &blit->VBO, true, 2, 2, 0);
 
-      fs_source = ralloc_asprintf(mem_ctx,
-                                  "#ifdef GL_ES\n"
-                                  "precision highp float;\n"
-                                  "#endif\n"
-                                  "uniform %s texSampler;\n"
-                                  "varying vec2 texCoords;\n"
-                                  "void main()\n"
-                                  "{\n"
-                                  "   gl_FragColor = %s(texSampler, texCoords);\n"
-                                  "   gl_FragDepth = gl_FragColor.r;\n"
-                                  "}\n",
-                                  texture_2d ? "sampler2D" : "sampler2DRect",
-                                  texture_2d ? "texture2D" : "texture2DRect");
-   }
-   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"
-                                  "#ifdef GL_ES\n"
-                                  "precision highp float;\n"
-                                  "#endif\n"
-                                  "uniform %s texSampler;\n"
-                                  "in vec2 texCoords;\n"
-                                  "out vec4 out_color;\n"
-                                  "\n"
-                                  "void main()\n"
-                                  "{\n"
-                                  "   out_color = %s(texSampler, texCoords);\n"
-                                  "   gl_FragDepth = out_color.r;\n"
-                                  "}\n",
-                                  _mesa_is_desktop_gl(ctx) ? "130" : "300 es",
-                                  texture_2d ? "sampler2D" : "sampler2DRect",
-                                  texture_2d ? "texture" : "texture2DRect");
-   }
-
-   vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_source);
-   fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_source);
-
-   ShaderProg = _mesa_CreateProgramObjectARB();
-   _mesa_AttachShader(ShaderProg, fs);
-   _mesa_DeleteObjectARB(fs);
-   _mesa_AttachShader(ShaderProg, vs);
-   _mesa_DeleteObjectARB(vs);
-   _mesa_BindAttribLocation(ShaderProg, 0, "position");
-   _mesa_BindAttribLocation(ShaderProg, 1, "texcoords");
-   link_program_with_debug(ctx, ShaderProg);
-   ralloc_free(mem_ctx);
-   if (texture_2d)
-      blit->ShaderProg = ShaderProg;
-   else
-      blit->RectShaderProg = ShaderProg;
+   setup_blit_shader(ctx, target, &blit->samplers);
 }
 
 /**
@@ -1678,18 +1746,13 @@ blitframebuffer_texture(struct gl_context *ctx,
           */
          if (glsl_version) {
             setup_glsl_blit_framebuffer(ctx, blit, target);
-            if (target == GL_TEXTURE_2D)
-               _mesa_UseProgram(blit->ShaderProg);
-            else
-               _mesa_UseProgram(blit->RectShaderProg);
          }
          else {
-            setup_ff_blit_framebuffer(&ctx->Meta->Blit);
+            setup_ff_tnl_for_blit(&ctx->Meta->Blit.VAO,
+                                  &ctx->Meta->Blit.VBO,
+                                  2);
          }
 
-         _mesa_BindVertexArray(blit->VAO);
-         _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, blit->VBO);
-
          _mesa_GenSamplers(1, &sampler);
          _mesa_BindSampler(ctx->Texture.CurrentUnit, sampler);
 
@@ -1726,9 +1789,6 @@ blitframebuffer_texture(struct gl_context *ctx,
 
          /* Prepare vertex data (the VBO was previously created and bound) */
          {
-            struct vertex {
-               GLfloat x, y, s, t;
-            };
             struct vertex verts[4];
             GLfloat s0, t0, s1, t1;
 
@@ -1748,6 +1808,9 @@ blitframebuffer_texture(struct gl_context *ctx,
                t1 = (float) srcY1;
             }
 
+            /* Silence valgrind warnings about reading uninitialized stack. */
+            memset(verts, 0, sizeof(verts));
+
             /* setup vertex positions */
             verts[0].x = -1.0F * flipX;
             verts[0].y = -1.0F * flipY;
@@ -1758,14 +1821,14 @@ blitframebuffer_texture(struct gl_context *ctx,
             verts[3].x = -1.0F * flipX;
             verts[3].y =  1.0F * flipY;
 
-            verts[0].s = s0;
-            verts[0].t = t0;
-            verts[1].s = s1;
-            verts[1].t = t0;
-            verts[2].s = s1;
-            verts[2].t = t1;
-            verts[3].s = s0;
-            verts[3].t = t1;
+            verts[0].tex[0] = s0;
+            verts[0].tex[1] = t0;
+            verts[1].tex[0] = s1;
+            verts[1].tex[1] = t0;
+            verts[2].tex[0] = s1;
+            verts[2].tex[1] = t1;
+            verts[3].tex[0] = s0;
+            verts[3].tex[1] = t1;
 
             _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
          }
@@ -1825,9 +1888,6 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
    const GLint flipX = srcFlipX * dstFlipX;
    const GLint flipY = srcFlipY * dstFlipY;
 
-   struct vertex {
-      GLfloat x, y, s, t;
-   };
    struct vertex verts[4];
    GLboolean newTex;
    const GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
@@ -1863,17 +1923,13 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
     */
    if (use_glsl_version) {
       setup_glsl_blit_framebuffer(ctx, blit, tex->Target);
-      if (tex->Target == GL_TEXTURE_2D)
-         _mesa_UseProgram(blit->ShaderProg);
-      else
-         _mesa_UseProgram(blit->RectShaderProg);
    }
    else {
-      setup_ff_blit_framebuffer(blit);
+      setup_ff_tnl_for_blit(&blit->VAO, &blit->VBO, 2);
    }
 
-   _mesa_BindVertexArray(blit->VAO);
-   _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, blit->VBO);
+   /* Silence valgrind warnings about reading uninitialized stack. */
+   memset(verts, 0, sizeof(verts));
 
    /* Continue with "normal" approach which involves copying the src rect
     * into a temporary texture and is "blitted" by drawing a textured quad.
@@ -1913,14 +1969,14 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
                             rb_base_format, filter);
       /* texcoords (after texture allocation!) */
       {
-         verts[0].s = 1.0F;
-         verts[0].t = 1.0F;
-         verts[1].s = tex->Sright - 1.0F;
-         verts[1].t = 1.0F;
-         verts[2].s = tex->Sright - 1.0F;
-         verts[2].t = tex->Ttop - 1.0F;
-         verts[3].s = 1.0F;
-         verts[3].t = tex->Ttop - 1.0F;
+         verts[0].tex[0] = 1.0F;
+         verts[0].tex[1] = 1.0F;
+         verts[1].tex[0] = tex->Sright - 1.0F;
+         verts[1].tex[1] = 1.0F;
+         verts[2].tex[0] = tex->Sright - 1.0F;
+         verts[2].tex[1] = tex->Ttop - 1.0F;
+         verts[3].tex[0] = 1.0F;
+         verts[3].tex[1] = tex->Ttop - 1.0F;
 
          /* upload new vertex data */
          _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
@@ -1946,20 +2002,20 @@ _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
          newTex = alloc_texture(depthTex, srcW, srcH, GL_DEPTH_COMPONENT);
          _mesa_ReadPixels(srcX, srcY, srcW, srcH, GL_DEPTH_COMPONENT,
                           GL_UNSIGNED_INT, tmp);
-         setup_drawpix_texture(ctx, depthTex, newTex, GL_DEPTH_COMPONENT,
+         setup_drawpix_texture(ctx, depthTex, newTex,
                                srcW, srcH, GL_DEPTH_COMPONENT,
                                GL_UNSIGNED_INT, tmp);
 
          /* texcoords (after texture allocation!) */
          {
-            verts[0].s = 0.0F;
-            verts[0].t = 0.0F;
-            verts[1].s = depthTex->Sright;
-            verts[1].t = 0.0F;
-            verts[2].s = depthTex->Sright;
-            verts[2].t = depthTex->Ttop;
-            verts[3].s = 0.0F;
-            verts[3].t = depthTex->Ttop;
+            verts[0].tex[0] = 0.0F;
+            verts[0].tex[1] = 0.0F;
+            verts[1].tex[0] = depthTex->Sright;
+            verts[1].tex[1] = 0.0F;
+            verts[2].tex[0] = depthTex->Sright;
+            verts[2].tex[1] = depthTex->Ttop;
+            verts[3].tex[0] = 0.0F;
+            verts[3].tex[1] = depthTex->Ttop;
 
             /* upload new vertex data */
             _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
@@ -2013,10 +2069,7 @@ meta_glsl_blit_cleanup(struct blit_state *blit)
       blit->DepthFP = 0;
    }
 
-   _mesa_DeleteObjectARB(blit->ShaderProg);
-   blit->ShaderProg = 0;
-   _mesa_DeleteObjectARB(blit->RectShaderProg);
-   blit->RectShaderProg = 0;
+   sampler_table_cleanup(&blit->samplers);
 
    _mesa_DeleteTextures(1, &blit->depthTex.TexObj);
    blit->depthTex.TexObj = 0;
@@ -2030,9 +2083,6 @@ void
 _mesa_meta_Clear(struct gl_context *ctx, GLbitfield buffers)
 {
    struct clear_state *clear = &ctx->Meta->Clear;
-   struct vertex {
-      GLfloat x, y, z, r, g, b, a;
-   };
    struct vertex verts[4];
    /* save all state but scissor, pixel pack/unpack */
    GLbitfield metaSave = (MESA_META_ALL -
@@ -2049,27 +2099,7 @@ _mesa_meta_Clear(struct gl_context *ctx, GLbitfield buffers)
 
    _mesa_meta_begin(ctx, metaSave);
 
-   if (clear->VAO == 0) {
-      /* one-time setup */
-
-      /* create vertex array object */
-      _mesa_GenVertexArrays(1, &clear->VAO);
-      _mesa_BindVertexArray(clear->VAO);
-
-      /* create vertex array buffer */
-      _mesa_GenBuffers(1, &clear->VBO);
-      _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, clear->VBO);
-
-      /* setup vertex arrays */
-      _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
-      _mesa_ColorPointer(4, GL_FLOAT, sizeof(struct vertex), OFFSET(r));
-      _mesa_EnableClientState(GL_VERTEX_ARRAY);
-      _mesa_EnableClientState(GL_COLOR_ARRAY);
-   }
-   else {
-      _mesa_BindVertexArray(clear->VAO);
-      _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, clear->VBO);
-   }
+   setup_vertex_objects(&clear->VAO, &clear->VBO, false, 3, 0, 4);
 
    /* GL_COLOR_BUFFER_BIT */
    if (buffers & BUFFER_BITS_COLOR) {
@@ -2182,20 +2212,10 @@ meta_glsl_clear_init(struct gl_context *ctx, struct clear_state *clear)
    GLuint vs, gs = 0, fs;
    bool has_integer_textures;
 
-   if (clear->VAO != 0)
-      return;
-
-   /* create vertex array object */
-   _mesa_GenVertexArrays(1, &clear->VAO);
-   _mesa_BindVertexArray(clear->VAO);
+   setup_vertex_objects(&clear->VAO, &clear->VBO, true, 3, 0, 0);
 
-   /* create vertex array buffer */
-   _mesa_GenBuffers(1, &clear->VBO);
-   _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, clear->VBO);
-
-   /* setup vertex arrays */
-   _mesa_VertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
-   _mesa_EnableVertexAttribArray(0);
+   if (clear->ShaderProg != 0)
+      return;
 
    vs = _mesa_CreateShaderObjectARB(GL_VERTEX_SHADER);
    _mesa_ShaderSource(vs, 1, &vs_source, NULL);
@@ -2321,9 +2341,7 @@ _mesa_meta_glsl_Clear(struct gl_context *ctx, GLbitfield buffers)
    const float x1 = ((float)fb->_Xmax / fb->Width)  * 2.0f - 1.0f;
    const float y1 = ((float)fb->_Ymax / fb->Height) * 2.0f - 1.0f;
    const float z = -invert_z(ctx->Depth.Clear);
-   struct vertex {
-      GLfloat x, y, z;
-   } verts[4];
+   struct vertex verts[4];
 
    metaSave = (MESA_META_ALPHA_TEST |
               MESA_META_BLEND |
@@ -2359,9 +2377,6 @@ _mesa_meta_glsl_Clear(struct gl_context *ctx, GLbitfield buffers)
                          ctx->Color.ClearColor.f);
    }
 
-   _mesa_BindVertexArray(clear->VAO);
-   _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, clear->VBO);
-
    /* GL_COLOR_BUFFER_BIT */
    if (buffers & BUFFER_BITS_COLOR) {
       /* leave colormask, glDrawBuffer state as-is */
@@ -2444,9 +2459,6 @@ _mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcX, GLint srcY,
 {
    struct copypix_state *copypix = &ctx->Meta->CopyPix;
    struct temp_texture *tex = get_temp_texture(ctx);
-   struct vertex {
-      GLfloat x, y, z, s, t;
-   };
    struct vertex verts[4];
    GLboolean newTex;
    GLenum intFormat = GL_RGBA;
@@ -2472,32 +2484,13 @@ _mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcX, GLint srcY,
                           MESA_META_VERTEX |
                           MESA_META_VIEWPORT));
 
-   if (copypix->VAO == 0) {
-      /* one-time setup */
-
-      /* create vertex array object */
-      _mesa_GenVertexArrays(1, &copypix->VAO);
-      _mesa_BindVertexArray(copypix->VAO);
-
-      /* create vertex array buffer */
-      _mesa_GenBuffers(1, &copypix->VBO);
-      _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, copypix->VBO);
-      _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
-                          NULL, GL_DYNAMIC_DRAW_ARB);
-
-      /* setup vertex arrays */
-      _mesa_VertexPointer(3, 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);
-   }
-   else {
-      _mesa_BindVertexArray(copypix->VAO);
-      _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, copypix->VBO);
-   }
+   setup_vertex_objects(&copypix->VAO, &copypix->VBO, false, 3, 2, 0);
 
    newTex = alloc_texture(tex, width, height, intFormat);
 
+   /* Silence valgrind warnings about reading uninitialized stack. */
+   memset(verts, 0, sizeof(verts));
+
    /* vertex positions, texcoords (after texture allocation!) */
    {
       const GLfloat dstX0 = (GLfloat) dstX;
@@ -2509,23 +2502,23 @@ _mesa_meta_CopyPixels(struct gl_context *ctx, GLint srcX, GLint srcY,
       verts[0].x = dstX0;
       verts[0].y = dstY0;
       verts[0].z = z;
-      verts[0].s = 0.0F;
-      verts[0].t = 0.0F;
+      verts[0].tex[0] = 0.0F;
+      verts[0].tex[1] = 0.0F;
       verts[1].x = dstX1;
       verts[1].y = dstY0;
       verts[1].z = z;
-      verts[1].s = tex->Sright;
-      verts[1].t = 0.0F;
+      verts[1].tex[0] = tex->Sright;
+      verts[1].tex[1] = 0.0F;
       verts[2].x = dstX1;
       verts[2].y = dstY1;
       verts[2].z = z;
-      verts[2].s = tex->Sright;
-      verts[2].t = tex->Ttop;
+      verts[2].tex[0] = tex->Sright;
+      verts[2].tex[1] = tex->Ttop;
       verts[3].x = dstX0;
       verts[3].y = dstY1;
       verts[3].z = z;
-      verts[3].s = 0.0F;
-      verts[3].t = tex->Ttop;
+      verts[3].tex[0] = 0.0F;
+      verts[3].tex[1] = tex->Ttop;
 
       /* upload new vertex data */
       _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
@@ -2551,6 +2544,9 @@ meta_drawpix_cleanup(struct drawpix_state *drawpix)
    if (drawpix->VAO != 0) {
       _mesa_DeleteVertexArrays(1, &drawpix->VAO);
       drawpix->VAO = 0;
+
+      _mesa_DeleteBuffers(1, &drawpix->VBO);
+      drawpix->VBO = 0;
    }
 
    if (drawpix->StencilFP != 0) {
@@ -2715,14 +2711,10 @@ _mesa_meta_DrawPixels(struct gl_context *ctx,
    struct temp_texture *tex = get_temp_texture(ctx);
    const struct gl_pixelstore_attrib unpackSave = ctx->Unpack;
    const GLuint origStencilMask = ctx->Stencil.WriteMask[0];
-   struct vertex {
-      GLfloat x, y, z, s, t;
-   };
    struct vertex verts[4];
    GLenum texIntFormat;
    GLboolean fallback, newTex;
    GLbitfield metaExtraSave = 0x0;
-   GLuint vbo;
 
    /*
     * Determine if we can do the glDrawPixels with texture mapping.
@@ -2814,6 +2806,11 @@ _mesa_meta_DrawPixels(struct gl_context *ctx,
 
    newTex = alloc_texture(tex, width, height, texIntFormat);
 
+   setup_vertex_objects(&drawpix->VAO, &drawpix->VBO, false, 3, 2, 0);
+
+   /* Silence valgrind warnings about reading uninitialized stack. */
+   memset(verts, 0, sizeof(verts));
+
    /* vertex positions, texcoords (after texture allocation!) */
    {
       const GLfloat x0 = (GLfloat) x;
@@ -2825,43 +2822,29 @@ _mesa_meta_DrawPixels(struct gl_context *ctx,
       verts[0].x = x0;
       verts[0].y = y0;
       verts[0].z = z;
-      verts[0].s = 0.0F;
-      verts[0].t = 0.0F;
+      verts[0].tex[0] = 0.0F;
+      verts[0].tex[1] = 0.0F;
       verts[1].x = x1;
       verts[1].y = y0;
       verts[1].z = z;
-      verts[1].s = tex->Sright;
-      verts[1].t = 0.0F;
+      verts[1].tex[0] = tex->Sright;
+      verts[1].tex[1] = 0.0F;
       verts[2].x = x1;
       verts[2].y = y1;
       verts[2].z = z;
-      verts[2].s = tex->Sright;
-      verts[2].t = tex->Ttop;
+      verts[2].tex[0] = tex->Sright;
+      verts[2].tex[1] = tex->Ttop;
       verts[3].x = x0;
       verts[3].y = y1;
       verts[3].z = z;
-      verts[3].s = 0.0F;
-      verts[3].t = tex->Ttop;
-   }
-
-   if (drawpix->VAO == 0) {
-      /* one-time setup: create vertex array object */
-      _mesa_GenVertexArrays(1, &drawpix->VAO);
+      verts[3].tex[0] = 0.0F;
+      verts[3].tex[1] = tex->Ttop;
    }
-   _mesa_BindVertexArray(drawpix->VAO);
 
-   /* create vertex array buffer */
-   _mesa_GenBuffers(1, &vbo);
-   _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, vbo);
+   /* upload new vertex data */
    _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
                        verts, GL_DYNAMIC_DRAW_ARB);
 
-   /* setup vertex arrays */
-   _mesa_VertexPointer(3, 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);
-
    /* set given unpack params */
    ctx->Unpack = *unpack;
 
@@ -2874,7 +2857,7 @@ _mesa_meta_DrawPixels(struct gl_context *ctx,
       if (!drawpix->StencilFP)
          init_draw_stencil_pixels(ctx);
 
-      setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
+      setup_drawpix_texture(ctx, tex, newTex, width, height,
                             GL_ALPHA, type, pixels);
 
       _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
@@ -2917,22 +2900,20 @@ _mesa_meta_DrawPixels(struct gl_context *ctx,
       _mesa_ProgramLocalParameter4fvARB(GL_FRAGMENT_PROGRAM_ARB, 0,
                                         ctx->Current.RasterColor);
 
-      setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
+      setup_drawpix_texture(ctx, tex, newTex, width, height,
                             format, type, pixels);
 
       _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
    }
    else {
       /* Drawing RGBA */
-      setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
+      setup_drawpix_texture(ctx, tex, newTex, width, height,
                             format, type, pixels);
       _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
    }
 
    _mesa_set_enable(ctx, tex->Target, GL_FALSE);
 
-   _mesa_DeleteBuffers(1, &vbo);
-
    /* restore unpack params */
    ctx->Unpack = unpackSave;
 
@@ -2984,9 +2965,6 @@ _mesa_meta_Bitmap(struct gl_context *ctx,
    const GLenum texIntFormat = GL_ALPHA;
    const struct gl_pixelstore_attrib unpackSave = *unpack;
    GLubyte fg, bg;
-   struct vertex {
-      GLfloat x, y, z, s, t, r, g, b, a;
-   };
    struct vertex verts[4];
    GLboolean newTex;
    GLubyte *bitmap8;
@@ -3020,34 +2998,13 @@ _mesa_meta_Bitmap(struct gl_context *ctx,
                           MESA_META_VERTEX |
                           MESA_META_VIEWPORT));
 
-   if (bitmap->VAO == 0) {
-      /* one-time setup */
-
-      /* create vertex array object */
-      _mesa_GenVertexArrays(1, &bitmap->VAO);
-      _mesa_BindVertexArray(bitmap->VAO);
-
-      /* create vertex array buffer */
-      _mesa_GenBuffers(1, &bitmap->VBO);
-      _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
-      _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
-                          NULL, GL_DYNAMIC_DRAW_ARB);
-
-      /* setup vertex arrays */
-      _mesa_VertexPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
-      _mesa_TexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(s));
-      _mesa_ColorPointer(4, GL_FLOAT, sizeof(struct vertex), OFFSET(r));
-      _mesa_EnableClientState(GL_VERTEX_ARRAY);
-      _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
-      _mesa_EnableClientState(GL_COLOR_ARRAY);
-   }
-   else {
-      _mesa_BindVertexArray(bitmap->VAO);
-      _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, bitmap->VBO);
-   }
+   setup_vertex_objects(&bitmap->VAO, &bitmap->VBO, false, 3, 2, 4);
 
    newTex = alloc_texture(tex, width, height, texIntFormat);
 
+   /* Silence valgrind warnings about reading uninitialized stack. */
+   memset(verts, 0, sizeof(verts));
+
    /* vertex positions, texcoords, colors (after texture allocation!) */
    {
       const GLfloat x0 = (GLfloat) x;
@@ -3060,23 +3017,23 @@ _mesa_meta_Bitmap(struct gl_context *ctx,
       verts[0].x = x0;
       verts[0].y = y0;
       verts[0].z = z;
-      verts[0].s = 0.0F;
-      verts[0].t = 0.0F;
+      verts[0].tex[0] = 0.0F;
+      verts[0].tex[1] = 0.0F;
       verts[1].x = x1;
       verts[1].y = y0;
       verts[1].z = z;
-      verts[1].s = tex->Sright;
-      verts[1].t = 0.0F;
+      verts[1].tex[0] = tex->Sright;
+      verts[1].tex[1] = 0.0F;
       verts[2].x = x1;
       verts[2].y = y1;
       verts[2].z = z;
-      verts[2].s = tex->Sright;
-      verts[2].t = tex->Ttop;
+      verts[2].tex[0] = tex->Sright;
+      verts[2].tex[1] = tex->Ttop;
       verts[3].x = x0;
       verts[3].y = y1;
       verts[3].z = z;
-      verts[3].s = 0.0F;
-      verts[3].t = tex->Ttop;
+      verts[3].tex[0] = 0.0F;
+      verts[3].tex[1] = tex->Ttop;
 
       for (i = 0; i < 4; i++) {
          verts[i].r = ctx->Current.RasterColor[0];
@@ -3110,7 +3067,7 @@ _mesa_meta_Bitmap(struct gl_context *ctx,
       _mesa_set_enable(ctx, GL_ALPHA_TEST, GL_TRUE);
       _mesa_AlphaFunc(GL_NOTEQUAL, UBYTE_TO_FLOAT(bg));
 
-      setup_drawpix_texture(ctx, tex, newTex, texIntFormat, width, height,
+      setup_drawpix_texture(ctx, tex, newTex, width, height,
                             GL_ALPHA, GL_UNSIGNED_BYTE, bitmap8);
 
       _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
@@ -3236,10 +3193,10 @@ setup_texture_coords(GLenum faceTarget,
                      GLint width,
                      GLint height,
                      GLint depth,
-                     GLfloat coords0[3],
-                     GLfloat coords1[3],
-                     GLfloat coords2[3],
-                     GLfloat coords3[3])
+                     GLfloat coords0[4],
+                     GLfloat coords1[4],
+                     GLfloat coords2[4],
+                     GLfloat coords3[4])
 {
    static const GLfloat st[4][2] = {
       {0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}
@@ -3247,6 +3204,13 @@ setup_texture_coords(GLenum faceTarget,
    GLuint i;
    GLfloat r;
 
+   /* Currently all texture targets want the W component to be 1.0.
+    */
+   coords0[3] = 1.0F;
+   coords1[3] = 1.0F;
+   coords2[3] = 1.0F;
+   coords3[3] = 1.0F;
+
    switch (faceTarget) {
    case GL_TEXTURE_1D:
    case GL_TEXTURE_2D:
@@ -3337,6 +3301,8 @@ setup_texture_coords(GLenum faceTarget,
             assert(0);
          }
 
+         coord[3] = (float) (slice / 6);
+
          switch (faceTarget) {
          case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
             coord[0] = 1.0f;
@@ -3378,74 +3344,53 @@ setup_texture_coords(GLenum faceTarget,
    }
 }
 
-
-static void
-setup_ff_generate_mipmap(struct gen_mipmap_state *mipmap)
-{
-   struct vertex {
-      GLfloat x, y, tex[3];
-   };
-
-   if (mipmap->VAO == 0) {
-      /* one-time setup */
-      /* create vertex array object */
-      _mesa_GenVertexArrays(1, &mipmap->VAO);
-      _mesa_BindVertexArray(mipmap->VAO);
-
-      /* create vertex array buffer */
-      _mesa_GenBuffers(1, &mipmap->VBO);
-      _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
-      /* setup vertex arrays */
-      _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
-      _mesa_TexCoordPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(tex));
-      _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 struct glsl_sampler *
-setup_texture_sampler(GLenum target, struct gen_mipmap_state *mipmap)
+setup_texture_sampler(GLenum target, struct sampler_table *table)
 {
    switch(target) {
    case GL_TEXTURE_1D:
-      mipmap->sampler_1d.type = "sampler1D";
-      mipmap->sampler_1d.func = "texture1D";
-      mipmap->sampler_1d.texcoords = "texCoords.x";
-      return &mipmap->sampler_1d;
+      table->sampler_1d.type = "sampler1D";
+      table->sampler_1d.func = "texture1D";
+      table->sampler_1d.texcoords = "texCoords.x";
+      return &table->sampler_1d;
    case GL_TEXTURE_2D:
-      mipmap->sampler_2d.type = "sampler2D";
-      mipmap->sampler_2d.func = "texture2D";
-      mipmap->sampler_2d.texcoords = "texCoords.xy";
-      return &mipmap->sampler_2d;
+      table->sampler_2d.type = "sampler2D";
+      table->sampler_2d.func = "texture2D";
+      table->sampler_2d.texcoords = "texCoords.xy";
+      return &table->sampler_2d;
+   case GL_TEXTURE_RECTANGLE:
+      table->sampler_rect.type = "sampler2DRect";
+      table->sampler_rect.func = "texture2DRect";
+      table->sampler_rect.texcoords = "texCoords.xy";
+      return &table->sampler_rect;
    case GL_TEXTURE_3D:
       /* Code for mipmap generation with 3D textures is not used yet.
        * It's a sw fallback.
        */
-      mipmap->sampler_3d.type = "sampler3D";
-      mipmap->sampler_3d.func = "texture3D";
-      mipmap->sampler_3d.texcoords = "texCoords";
-      return &mipmap->sampler_3d;
+      table->sampler_3d.type = "sampler3D";
+      table->sampler_3d.func = "texture3D";
+      table->sampler_3d.texcoords = "texCoords.xyz";
+      return &table->sampler_3d;
    case GL_TEXTURE_CUBE_MAP:
-      mipmap->sampler_cubemap.type = "samplerCube";
-      mipmap->sampler_cubemap.func = "textureCube";
-      mipmap->sampler_cubemap.texcoords = "texCoords";
-      return &mipmap->sampler_cubemap;
+      table->sampler_cubemap.type = "samplerCube";
+      table->sampler_cubemap.func = "textureCube";
+      table->sampler_cubemap.texcoords = "texCoords.xyz";
+      return &table->sampler_cubemap;
    case GL_TEXTURE_1D_ARRAY:
-      mipmap->sampler_1d_array.type = "sampler1DArray";
-      mipmap->sampler_1d_array.func = "texture1DArray";
-      mipmap->sampler_1d_array.texcoords = "texCoords.xy";
-      return &mipmap->sampler_1d_array;
+      table->sampler_1d_array.type = "sampler1DArray";
+      table->sampler_1d_array.func = "texture1DArray";
+      table->sampler_1d_array.texcoords = "texCoords.xy";
+      return &table->sampler_1d_array;
    case GL_TEXTURE_2D_ARRAY:
-      mipmap->sampler_2d_array.type = "sampler2DArray";
-      mipmap->sampler_2d_array.func = "texture2DArray";
-      mipmap->sampler_2d_array.texcoords = "texCoords";
-      return &mipmap->sampler_2d_array;
+      table->sampler_2d_array.type = "sampler2DArray";
+      table->sampler_2d_array.func = "texture2DArray";
+      table->sampler_2d_array.texcoords = "texCoords.xyz";
+      return &table->sampler_2d_array;
+   case GL_TEXTURE_CUBE_MAP_ARRAY:
+      table->sampler_cubemap_array.type = "samplerCubeArray";
+      table->sampler_cubemap_array.func = "textureCubeArray";
+      table->sampler_cubemap_array.texcoords = "texCoords.xyzw";
+      return &table->sampler_cubemap_array;
    default:
       _mesa_problem(NULL, "Unexpected texture target 0x%x in"
                     " setup_texture_sampler()\n", target);
@@ -3453,119 +3398,36 @@ setup_texture_sampler(GLenum target, struct gen_mipmap_state *mipmap)
    }
 }
 
+static void
+sampler_table_cleanup(struct sampler_table *table)
+{
+   _mesa_DeleteObjectARB(table->sampler_1d.shader_prog);
+   _mesa_DeleteObjectARB(table->sampler_2d.shader_prog);
+   _mesa_DeleteObjectARB(table->sampler_3d.shader_prog);
+   _mesa_DeleteObjectARB(table->sampler_rect.shader_prog);
+   _mesa_DeleteObjectARB(table->sampler_cubemap.shader_prog);
+   _mesa_DeleteObjectARB(table->sampler_1d_array.shader_prog);
+   _mesa_DeleteObjectARB(table->sampler_2d_array.shader_prog);
+   _mesa_DeleteObjectARB(table->sampler_cubemap_array.shader_prog);
+
+   table->sampler_1d.shader_prog = 0;
+   table->sampler_2d.shader_prog = 0;
+   table->sampler_3d.shader_prog = 0;
+   table->sampler_rect.shader_prog = 0;
+   table->sampler_cubemap.shader_prog = 0;
+   table->sampler_1d_array.shader_prog = 0;
+   table->sampler_2d_array.shader_prog = 0;
+   table->sampler_cubemap_array.shader_prog = 0;
+}
 
 static void
 setup_glsl_generate_mipmap(struct gl_context *ctx,
                            struct gen_mipmap_state *mipmap,
                            GLenum target)
 {
-   struct vertex {
-      GLfloat x, y, tex[3];
-   };
-   struct glsl_sampler *sampler;
-   const char *vs_source;
-   char *fs_source;
-   GLuint vs, fs;
-   void *mem_ctx;
-
-   /* Check if already initialized */
-   if (mipmap->VAO == 0) {
-
-      /* create vertex array object */
-      _mesa_GenVertexArrays(1, &mipmap->VAO);
-      _mesa_BindVertexArray(mipmap->VAO);
-
-      /* create vertex array buffer */
-      _mesa_GenBuffers(1, &mipmap->VBO);
-      _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
-
-      /* setup vertex arrays */
-      _mesa_VertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE,
-                                   sizeof(struct vertex), OFFSET(x));
-      _mesa_VertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,
-                                   sizeof(struct vertex), OFFSET(tex));
-      _mesa_EnableVertexAttribArray(0);
-      _mesa_EnableVertexAttribArray(1);
-   }
-
-   /* Generate a fragment shader program appropriate for the texture target */
-   sampler = setup_texture_sampler(target, mipmap);
-   assert(sampler != NULL);
-   if (sampler->shader_prog != 0) {
-      mipmap->ShaderProg = sampler->shader_prog;
-      return;
-   }
-
-   mem_ctx = ralloc_context(NULL);
-
-   if (ctx->API == API_OPENGLES2 || ctx->Const.GLSLVersion < 130) {
-      vs_source =
-         "attribute vec2 position;\n"
-         "attribute vec3 textureCoords;\n"
-         "varying vec3 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"
-                                  "#ifdef GL_ES\n"
-                                  "precision highp float;\n"
-                                  "#endif\n"
-                                  "uniform %s texSampler;\n"
-                                  "varying vec3 texCoords;\n"
-                                  "void main()\n"
-                                  "{\n"
-                                  "   gl_FragColor = %s(texSampler, %s);\n"
-                                  "}\n",
-                                  sampler->type,
-                                  sampler->func, sampler->texcoords);
-   }
-   else {
-      vs_source = ralloc_asprintf(mem_ctx,
-                                  "#version %s\n"
-                                  "in vec2 position;\n"
-                                  "in vec3 textureCoords;\n"
-                                  "out vec3 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"
-                                  "#ifdef GL_ES\n"
-                                  "precision highp float;\n"
-                                  "#endif\n"
-                                  "uniform %s texSampler;\n"
-                                  "in vec3 texCoords;\n"
-                                  "out vec4 out_color;\n"
-                                  "\n"
-                                  "void main()\n"
-                                  "{\n"
-                                  "   out_color = texture(texSampler, %s);\n"
-                                  "}\n",
-                                  _mesa_is_desktop_gl(ctx) ? "130" : "300 es",
-                                  sampler->type,
-                                  sampler->texcoords);
-   }
+   setup_vertex_objects(&mipmap->VAO, &mipmap->VBO, true, 2, 3, 0);
 
-   vs = compile_shader_with_debug(ctx, GL_VERTEX_SHADER, vs_source);
-   fs = compile_shader_with_debug(ctx, GL_FRAGMENT_SHADER, fs_source);
-
-   mipmap->ShaderProg = _mesa_CreateProgramObjectARB();
-   _mesa_AttachShader(mipmap->ShaderProg, fs);
-   _mesa_DeleteObjectARB(fs);
-   _mesa_AttachShader(mipmap->ShaderProg, vs);
-   _mesa_DeleteObjectARB(vs);
-   _mesa_BindAttribLocation(mipmap->ShaderProg, 0, "position");
-   _mesa_BindAttribLocation(mipmap->ShaderProg, 1, "texcoords");
-   link_program_with_debug(ctx, mipmap->ShaderProg);
-   sampler->shader_prog = mipmap->ShaderProg;
-   ralloc_free(mem_ctx);
+   setup_blit_shader(ctx, target, &mipmap->samplers);
 }
 
 
@@ -3579,19 +3441,7 @@ meta_glsl_generate_mipmap_cleanup(struct gen_mipmap_state *mipmap)
    _mesa_DeleteBuffers(1, &mipmap->VBO);
    mipmap->VBO = 0;
 
-   _mesa_DeleteObjectARB(mipmap->sampler_1d.shader_prog);
-   _mesa_DeleteObjectARB(mipmap->sampler_2d.shader_prog);
-   _mesa_DeleteObjectARB(mipmap->sampler_3d.shader_prog);
-   _mesa_DeleteObjectARB(mipmap->sampler_cubemap.shader_prog);
-   _mesa_DeleteObjectARB(mipmap->sampler_1d_array.shader_prog);
-   _mesa_DeleteObjectARB(mipmap->sampler_2d_array.shader_prog);
-
-   mipmap->sampler_1d.shader_prog = 0;
-   mipmap->sampler_2d.shader_prog = 0;
-   mipmap->sampler_3d.shader_prog = 0;
-   mipmap->sampler_cubemap.shader_prog = 0;
-   mipmap->sampler_1d_array.shader_prog = 0;
-   mipmap->sampler_2d_array.shader_prog = 0;
+   sampler_table_cleanup(&mipmap->samplers);
 }
 
 
@@ -3605,9 +3455,6 @@ _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
                           struct gl_texture_object *texObj)
 {
    struct gen_mipmap_state *mipmap = &ctx->Meta->Mipmap;
-   struct vertex {
-      GLfloat x, y, tex[3];
-   };
    struct vertex verts[4];
    const GLuint baseLevel = texObj->BaseLevel;
    const GLuint maxLevel = texObj->MaxLevel;
@@ -3644,16 +3491,12 @@ _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
     */
    if (use_glsl_version) {
       setup_glsl_generate_mipmap(ctx, mipmap, target);
-      _mesa_UseProgram(mipmap->ShaderProg);
    }
    else {
-      setup_ff_generate_mipmap(mipmap);
+      setup_ff_tnl_for_blit(&mipmap->VAO, &mipmap->VBO, 3);
       _mesa_set_enable(ctx, target, GL_TRUE);
    }
 
-   _mesa_BindVertexArray(mipmap->VAO);
-   _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, mipmap->VBO);
-
    samplerSave = ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler ?
       ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler->Name : 0;
 
@@ -3696,6 +3539,9 @@ _mesa_meta_GenerateMipmap(struct gl_context *ctx, GLenum target,
    else
       assert(!genMipmapSave);
 
+   /* Silence valgrind warnings about reading uninitialized stack. */
+   memset(verts, 0, sizeof(verts));
+
    /* Setup texture coordinates */
    setup_texture_coords(faceTarget,
                         slice,
@@ -3895,6 +3741,12 @@ _mesa_meta_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
    GLint bpp;
    void *buf;
 
+   /* The gl_renderbuffer is part of the interface for
+    * dd_function_table::CopyTexSubImage, but this implementation does not use
+    * it.
+    */
+   (void) rb;
+
    /* Choose format/type for temporary image buffer */
    format = _mesa_get_format_base_format(texImage->TexFormat);
    if (format == GL_LUMINANCE ||
@@ -4004,17 +3856,18 @@ decompress_texture_image(struct gl_context *ctx,
    const GLint depth = texImage->Height;
    const GLenum target = texObj->Target;
    GLenum faceTarget;
-   struct vertex {
-      GLfloat x, y, tex[3];
-   };
    struct vertex verts[4];
    GLuint fboDrawSave, fboReadSave;
    GLuint rbSave;
    GLuint samplerSave;
+   const bool use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
+                                      ctx->Extensions.ARB_fragment_shader &&
+                                      (ctx->API != API_OPENGLES);
 
    if (slice > 0) {
       assert(target == GL_TEXTURE_3D ||
-             target == GL_TEXTURE_2D_ARRAY);
+             target == GL_TEXTURE_2D_ARRAY ||
+             target == GL_TEXTURE_CUBE_MAP_ARRAY);
    }
 
    switch (target) {
@@ -4027,10 +3880,9 @@ decompress_texture_image(struct gl_context *ctx,
       assert(!"No compressed 3D textures.");
       return;
 
-   case GL_TEXTURE_2D_ARRAY:
    case GL_TEXTURE_CUBE_MAP_ARRAY:
-      /* These targets are just broken currently. */
-      return;
+      faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + (slice % 6);
+      break;
 
    case GL_TEXTURE_CUBE_MAP:
       faceTarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + texImage->Face;
@@ -4075,27 +3927,13 @@ decompress_texture_image(struct gl_context *ctx,
       decompress->Height = height;
    }
 
-   /* setup VBO data */
-   if (decompress->VAO == 0) {
-      /* create vertex array object */
-      _mesa_GenVertexArrays(1, &decompress->VAO);
-      _mesa_BindVertexArray(decompress->VAO);
-
-      /* create vertex array buffer */
-      _mesa_GenBuffers(1, &decompress->VBO);
-      _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, decompress->VBO);
-      _mesa_BufferData(GL_ARRAY_BUFFER_ARB, sizeof(verts),
-                          NULL, GL_DYNAMIC_DRAW_ARB);
+   if (use_glsl_version) {
+      setup_vertex_objects(&decompress->VAO, &decompress->VBO, true,
+                           2, 4, 0);
 
-      /* setup vertex arrays */
-      _mesa_VertexPointer(2, GL_FLOAT, sizeof(struct vertex), OFFSET(x));
-      _mesa_TexCoordPointer(3, GL_FLOAT, sizeof(struct vertex), OFFSET(tex));
-      _mesa_EnableClientState(GL_VERTEX_ARRAY);
-      _mesa_EnableClientState(GL_TEXTURE_COORD_ARRAY);
-   }
-   else {
-      _mesa_BindVertexArray(decompress->VAO);
-      _mesa_BindBuffer(GL_ARRAY_BUFFER_ARB, decompress->VBO);
+      setup_blit_shader(ctx, target, &decompress->samplers);
+   } else {
+      setup_ff_tnl_for_blit(&decompress->VAO, &decompress->VBO, 3);
    }
 
    if (!decompress->Sampler) {
@@ -4114,6 +3952,9 @@ decompress_texture_image(struct gl_context *ctx,
       _mesa_BindSampler(ctx->Texture.CurrentUnit, decompress->Sampler);
    }
 
+   /* Silence valgrind warnings about reading uninitialized stack. */
+   memset(verts, 0, sizeof(verts));
+
    setup_texture_coords(faceTarget, slice, width, height, depth,
                         verts[0].tex,
                         verts[1].tex,
@@ -4130,8 +3971,6 @@ decompress_texture_image(struct gl_context *ctx,
    verts[3].x = -1.0F;
    verts[3].y =  1.0F;
 
-   _mesa_MatrixMode(GL_PROJECTION);
-   _mesa_LoadIdentity();
    _mesa_set_viewport(ctx, 0, 0, 0, width, height);
 
    /* upload new vertex data */
@@ -4139,7 +3978,9 @@ decompress_texture_image(struct gl_context *ctx,
 
    /* setup texture state */
    _mesa_BindTexture(target, texObj->Name);
-   _mesa_set_enable(ctx, target, GL_TRUE);
+
+   if (!use_glsl_version)
+      _mesa_set_enable(ctx, target, GL_TRUE);
 
    {
       /* save texture object state */
@@ -4199,7 +4040,8 @@ decompress_texture_image(struct gl_context *ctx,
    }
 
    /* disable texture unit */
-   _mesa_set_enable(ctx, target, GL_FALSE);
+   if (!use_glsl_version)
+      _mesa_set_enable(ctx, target, GL_FALSE);
 
    _mesa_BindSampler(ctx->Texture.CurrentUnit, samplerSave);
 
@@ -4231,8 +4073,7 @@ _mesa_meta_GetTexImage(struct gl_context *ctx,
     * unsigned, normalized values.  We could handle signed and unnormalized 
     * with floating point renderbuffers...
     */
-   if (texImage->TexObject->Target != GL_TEXTURE_CUBE_MAP_ARRAY
-       && _mesa_is_format_compressed(texImage->TexFormat) &&
+   if (_mesa_is_format_compressed(texImage->TexFormat) &&
        _mesa_get_format_datatype(texImage->TexFormat)
        == GL_UNSIGNED_NORMALIZED) {
       struct gl_texture_object *texObj = texImage->TexObject;
@@ -4241,7 +4082,8 @@ _mesa_meta_GetTexImage(struct gl_context *ctx,
       _mesa_unlock_texture(ctx, texObj);
       for (slice = 0; slice < texImage->Depth; slice++) {
          void *dst;
-         if (texImage->TexObject->Target == GL_TEXTURE_2D_ARRAY) {
+         if (texImage->TexObject->Target == GL_TEXTURE_2D_ARRAY
+             || texImage->TexObject->Target == GL_TEXTURE_CUBE_MAP_ARRAY) {
             /* Setup pixel packing.  SkipPixels and SkipRows will be applied
              * in the decompress_texture_image() function's call to
              * glReadPixels but we need to compute the dest slice's address