st/mesa: Include mfeatures.h in files that perform feature tests.
[mesa.git] / src / mesa / state_tracker / st_cb_drawpixels.c
index c9d1e9c6c4b2b15796405e12a5342c2556b11ca1..d128ff98f02d0da7e4b73501478017ffb6c85a67 100644 (file)
 #include "main/image.h"
 #include "main/bufferobj.h"
 #include "main/macros.h"
+#include "main/mfeatures.h"
+#include "main/mtypes.h"
+#include "main/pack.h"
 #include "main/texformat.h"
 #include "main/texstore.h"
-#include "shader/program.h"
-#include "shader/prog_print.h"
+#include "program/program.h"
+#include "program/prog_print.h"
+#include "program/prog_instruction.h"
 
-#include "st_debug.h"
-#include "st_context.h"
 #include "st_atom.h"
 #include "st_atom_constbuf.h"
-#include "st_program.h"
 #include "st_cb_drawpixels.h"
 #include "st_cb_readpixels.h"
 #include "st_cb_fbo.h"
+#include "st_context.h"
+#include "st_debug.h"
 #include "st_format.h"
+#include "st_program.h"
 #include "st_texture.h"
-#include "st_inlines.h"
 
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
-#include "util/u_inlines.h"
 #include "tgsi/tgsi_ureg.h"
-#include "util/u_tile.h"
 #include "util/u_draw_quad.h"
 #include "util/u_format.h"
+#include "util/u_inlines.h"
 #include "util/u_math.h"
-#include "shader/prog_instruction.h"
+#include "util/u_tile.h"
 #include "cso_cache/cso_context.h"
 
 
+#if FEATURE_drawpix
+
 /**
  * Check if the given program is:
  * 0: MOVE result.color, fragment.color;
@@ -92,89 +96,69 @@ is_passthrough_program(const struct gl_fragment_program *prog)
 /**
  * Make fragment shader for glDraw/CopyPixels.  This shader is made
  * by combining the pixel transfer shader with the user-defined shader.
- * \return pointer to Gallium driver fragment shader
+ * \param fpIn  the current/incoming fragment program
+ * \param fpOut  returns the combined fragment program
  */
-static void *
-combined_drawpix_fragment_program(GLcontext *ctx)
+void
+st_make_drawpix_fragment_program(struct st_context *st,
+                                 struct gl_fragment_program *fpIn,
+                                 struct gl_fragment_program **fpOut)
 {
-   struct st_context *st = st_context(ctx);
-   struct st_fragment_program *stfp;
+   struct gl_program *newProg;
 
-   if (st->pixel_xfer.program->serialNo == st->pixel_xfer.xfer_prog_sn
-       && st->fp->serialNo == st->pixel_xfer.user_prog_sn) {
-      /* the pixel tranfer program has not changed and the user-defined
-       * program has not changed, so re-use the combined program.
-       */
-      stfp = st->pixel_xfer.combined_prog;
+   if (is_passthrough_program(fpIn)) {
+      newProg = (struct gl_program *) _mesa_clone_fragment_program(st->ctx,
+                                             &st->pixel_xfer.program->Base);
    }
    else {
-      /* Concatenate the pixel transfer program with the current user-
-       * defined program.
-       */
-      if (is_passthrough_program(&st->fp->Base)) {
-         stfp = (struct st_fragment_program *)
-            _mesa_clone_fragment_program(ctx, &st->pixel_xfer.program->Base);
-      }
-      else {
 #if 0
-         printf("Base program:\n");
-         _mesa_print_program(&st->fp->Base.Base);
-         printf("DrawPix program:\n");
-         _mesa_print_program(&st->pixel_xfer.program->Base.Base);
+      /* debug */
+      printf("Base program:\n");
+      _mesa_print_program(&fpIn->Base);
+      printf("DrawPix program:\n");
+      _mesa_print_program(&st->pixel_xfer.program->Base.Base);
 #endif
-         stfp = (struct st_fragment_program *)
-            _mesa_combine_programs(ctx,
-                                   &st->pixel_xfer.program->Base.Base,
-                                   &st->fp->Base.Base);
-      }
+      newProg = _mesa_combine_programs(st->ctx,
+                                       &st->pixel_xfer.program->Base.Base,
+                                       &fpIn->Base);
+   }
 
 #if 0
-      {
-         struct gl_program *p = &stfp->Base.Base;
-         printf("Combined DrawPixels program:\n");
-         _mesa_print_program(p);
-         printf("InputsRead: 0x%x\n", p->InputsRead);
-         printf("OutputsWritten: 0x%x\n", p->OutputsWritten);
-         _mesa_print_parameter_list(p->Parameters);
-      }
+   /* debug */
+   printf("Combined DrawPixels program:\n");
+   _mesa_print_program(newProg);
+   printf("InputsRead: 0x%x\n", newProg->InputsRead);
+   printf("OutputsWritten: 0x%x\n", newProg->OutputsWritten);
+   _mesa_print_parameter_list(newProg->Parameters);
 #endif
 
-      /* translate to TGSI tokens */
-      st_translate_fragment_program(st, stfp);
-
-      /* save new program, update serial numbers */
-      st->pixel_xfer.xfer_prog_sn = st->pixel_xfer.program->serialNo;
-      st->pixel_xfer.user_prog_sn = st->fp->serialNo;
-      st->pixel_xfer.combined_prog_sn = stfp->serialNo;
-      /* can't reference new program directly, already have a reference on it */
-      st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
-      st->pixel_xfer.combined_prog = stfp;
-   }
-
-   /* Ideally we'd have updated the pipe constants during the normal
-    * st/atom mechanism.  But we can't since this is specific to glDrawPixels.
-    */
-   st_upload_constants(st, stfp->Base.Base.Parameters, PIPE_SHADER_FRAGMENT);
-
-   return stfp->driver_shader;
+   *fpOut = (struct gl_fragment_program *) newProg;
 }
 
 
 /**
- * Create fragment shader that does a TEX() instruction to get a Z
- * value, then writes to FRAG_RESULT_DEPTH.
+ * Create fragment program that does a TEX() instruction to get a Z and/or
+ * stencil value value, then writes to FRAG_RESULT_DEPTH/FRAG_RESULT_STENCIL.
+ * Used for glDrawPixels(GL_DEPTH_COMPONENT / GL_STENCIL_INDEX).
  * Pass fragment color through as-is.
- * \return pointer to the Gallium driver fragment shader
+ * \return pointer to the gl_fragment program
  */
-static void *
-make_fragment_shader_z(struct st_context *st)
+struct gl_fragment_program *
+st_make_drawpix_z_stencil_program(struct st_context *st,
+                                  GLboolean write_depth,
+                                  GLboolean write_stencil)
 {
-   GLcontext *ctx = st->ctx;
+   struct gl_context *ctx = st->ctx;
    struct gl_program *p;
+   struct gl_fragment_program *fp;
    GLuint ic = 0;
+   const GLuint shaderIndex = write_depth * 2 + write_stencil;
 
-   if (st->drawpix.z_shader) {
-      return st->drawpix.z_shader->driver_shader;
+   assert(shaderIndex < Elements(st->drawpix.shaders));
+
+   if (st->drawpix.shaders[shaderIndex]) {
+      /* already have the proper shader */
+      return st->drawpix.shaders[shaderIndex];
    }
 
    /*
@@ -184,7 +168,8 @@ make_fragment_shader_z(struct st_context *st)
    if (!p)
       return NULL;
 
-   p->NumInstructions = 3;
+   p->NumInstructions = write_depth ? 2 : 1;
+   p->NumInstructions += write_stencil ? 1 : 0;
 
    p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
    if (!p->Instructions) {
@@ -193,24 +178,31 @@ make_fragment_shader_z(struct st_context *st)
    }
    _mesa_init_instructions(p->Instructions, p->NumInstructions);
 
-   /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
-   p->Instructions[ic].Opcode = OPCODE_TEX;
-   p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
-   p->Instructions[ic].DstReg.Index = FRAG_RESULT_DEPTH;
-   p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Z;
-   p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
-   p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
-   p->Instructions[ic].TexSrcUnit = 0;
-   p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
-   ic++;
-
-   /* MOV result.color, fragment.color */
-   p->Instructions[ic].Opcode = OPCODE_MOV;
-   p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
-   p->Instructions[ic].DstReg.Index = FRAG_RESULT_COLOR;
-   p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
-   p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_COL0;
-   ic++;
+   if (write_depth) {
+      /* TEX result.depth, fragment.texcoord[0], texture[0], 2D; */
+      p->Instructions[ic].Opcode = OPCODE_TEX;
+      p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
+      p->Instructions[ic].DstReg.Index = FRAG_RESULT_DEPTH;
+      p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Z;
+      p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
+      p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
+      p->Instructions[ic].TexSrcUnit = 0;
+      p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
+      ic++;
+   }
+
+   if (write_stencil) {
+      /* TEX result.stencil, fragment.texcoord[0], texture[0], 2D; */
+      p->Instructions[ic].Opcode = OPCODE_TEX;
+      p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
+      p->Instructions[ic].DstReg.Index = FRAG_RESULT_STENCIL;
+      p->Instructions[ic].DstReg.WriteMask = WRITEMASK_Y;
+      p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
+      p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
+      p->Instructions[ic].TexSrcUnit = 1;
+      p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
+      ic++;
+   }
 
    /* END; */
    p->Instructions[ic++].Opcode = OPCODE_END;
@@ -218,15 +210,23 @@ make_fragment_shader_z(struct st_context *st)
    assert(ic == p->NumInstructions);
 
    p->InputsRead = FRAG_BIT_TEX0 | FRAG_BIT_COL0;
-   p->OutputsWritten = (1 << FRAG_RESULT_COLOR) | (1 << FRAG_RESULT_DEPTH);
-   p->SamplersUsed = 0x1;  /* sampler 0 (bit 0) is used */
+   p->OutputsWritten = 0;
+   if (write_depth)
+      p->OutputsWritten |= (1 << FRAG_RESULT_DEPTH);
+   if (write_stencil)
+      p->OutputsWritten |= (1 << FRAG_RESULT_STENCIL);
 
-   st->drawpix.z_shader = (struct st_fragment_program *) p;
-   st_translate_fragment_program(st, st->drawpix.z_shader);
+   p->SamplersUsed =  0x1;  /* sampler 0 (bit 0) is used */
+   if (write_stencil)
+      p->SamplersUsed |= 1 << 1;
 
-   return st->drawpix.z_shader->driver_shader;
-}
+   fp = (struct gl_fragment_program *) p;
 
+   /* save the new shader */
+   st->drawpix.shaders[shaderIndex] = fp;
+
+   return fp;
+}
 
 
 /**
@@ -238,8 +238,7 @@ make_passthrough_vertex_shader(struct st_context *st,
                                GLboolean passColor)
 {
    if (!st->drawpix.vert_shaders[passColor]) {
-      struct ureg_program *ureg = 
-         ureg_create( TGSI_PROCESSOR_VERTEX );
+      struct ureg_program *ureg = ureg_create( TGSI_PROCESSOR_VERTEX );
 
       if (ureg == NULL)
          return NULL;
@@ -249,13 +248,13 @@ make_passthrough_vertex_shader(struct st_context *st,
                ureg_DECL_output( ureg, TGSI_SEMANTIC_POSITION, 0 ),
                ureg_DECL_vs_input( ureg, 0 ));
       
-      /* MOV result.texcoord0, vertex.texcoord0; */
+      /* MOV result.texcoord0, vertex.attr[1]; */
       ureg_MOV(ureg, 
                ureg_DECL_output( ureg, TGSI_SEMANTIC_GENERIC, 0 ),
                ureg_DECL_vs_input( ureg, 1 ));
       
       if (passColor) {
-         /* MOV result.color0, vertex.color0; */
+         /* MOV result.color0, vertex.attr[2]; */
          ureg_MOV(ureg, 
                   ureg_DECL_output( ureg, TGSI_SEMANTIC_COLOR, 0 ),
                   ureg_DECL_vs_input( ureg, 2 ));
@@ -272,8 +271,8 @@ make_passthrough_vertex_shader(struct st_context *st,
 
 
 /**
- * Return a texture internalFormat for drawing/copying an image
- * of the given type.
+ * Return a texture base format for drawing/copying an image
+ * of the given format.
  */
 static GLenum
 base_format(GLenum format)
@@ -291,46 +290,60 @@ base_format(GLenum format)
 }
 
 
+/**
+ * Return a texture internalFormat for drawing/copying an image
+ * of the given format and type.
+ */
+static GLenum
+internal_format(GLenum format, GLenum type)
+{
+   switch (format) {
+   case GL_DEPTH_COMPONENT:
+      return GL_DEPTH_COMPONENT;
+   case GL_DEPTH_STENCIL:
+      return GL_DEPTH_STENCIL;
+   case GL_STENCIL_INDEX:
+      return GL_STENCIL_INDEX;
+   default:
+      if (_mesa_is_integer_format(format)) {
+         switch (type) {
+         case GL_BYTE:
+            return GL_RGBA8I;
+         case GL_UNSIGNED_BYTE:
+            return GL_RGBA8UI;
+         case GL_SHORT:
+            return GL_RGBA16I;
+         case GL_UNSIGNED_SHORT:
+            return GL_RGBA16UI;
+         case GL_INT:
+            return GL_RGBA32I;
+         case GL_UNSIGNED_INT:
+            return GL_RGBA32UI;
+         default:
+            assert(0 && "Unexpected type in internal_format()");
+            return GL_RGBA_INTEGER;
+         }
+      }
+      else {
+         return GL_RGBA;
+      }
+   }
+}
+
+
 /**
  * Create a temporary texture to hold an image of the given size.
  * If width, height are not POT and the driver only handles POT textures,
  * allocate the next larger size of texture that is POT.
  */
-static struct pipe_texture *
+static struct pipe_resource *
 alloc_texture(struct st_context *st, GLsizei width, GLsizei height,
               enum pipe_format texFormat)
 {
-   struct pipe_context *pipe = st->pipe;
-   struct pipe_screen *screen = pipe->screen;
-   struct pipe_texture *pt;
-   int ptw, pth;
-
-   ptw = width;
-   pth = height;
-
-   /* Need to use POT texture? */
-   if (!screen->get_param(screen, PIPE_CAP_NPOT_TEXTURES)) {
-      int l2pt, maxSize;
-
-      l2pt = util_logbase2(width);
-      if (1 << l2pt != width) {
-         ptw = 1 << (l2pt + 1);
-      }
-
-      l2pt = util_logbase2(height);
-      if (1 << l2pt != height) {
-         pth = 1 << (l2pt + 1);
-      }
-
-      /* Check against maximum texture size */
-      maxSize = 1 << (pipe->screen->get_param(pipe->screen,
-                               PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
-      assert(ptw <= maxSize);
-      assert(pth <= maxSize);
-   }
+   struct pipe_resource *pt;
 
-   pt = st_texture_create(st, PIPE_TEXTURE_2D, texFormat, 0,
-                          ptw, pth, 1, PIPE_TEXTURE_USAGE_SAMPLER);
+   pt = st_texture_create(st, st->internal_target, texFormat, 0,
+                          width, height, 1, PIPE_BIND_SAMPLER_VIEW);
 
    return pt;
 }
@@ -340,23 +353,25 @@ alloc_texture(struct st_context *st, GLsizei width, GLsizei height,
  * Make texture containing an image for glDrawPixels image.
  * If 'pixels' is NULL, leave the texture image data undefined.
  */
-static struct pipe_texture *
+static struct pipe_resource *
 make_texture(struct st_context *st,
             GLsizei width, GLsizei height, GLenum format, GLenum type,
             const struct gl_pixelstore_attrib *unpack,
             const GLvoid *pixels)
 {
-   GLcontext *ctx = st->ctx;
+   struct gl_context *ctx = st->ctx;
    struct pipe_context *pipe = st->pipe;
    gl_format mformat;
-   struct pipe_texture *pt;
+   struct pipe_resource *pt;
    enum pipe_format pipeFormat;
    GLuint cpp;
-   GLenum baseFormat;
+   GLenum baseFormat, intFormat;
 
    baseFormat = base_format(format);
+   intFormat = internal_format(format, type);
 
-   mformat = st_ChooseTextureFormat(ctx, baseFormat, format, type);
+   mformat = st_ChooseTextureFormat_renderable(ctx, intFormat,
+                                               format, type, GL_FALSE);
    assert(mformat);
 
    pipeFormat = st_mesa_format_to_pipe_format(mformat);
@@ -384,12 +399,12 @@ make_texture(struct st_context *st,
       /* we'll do pixel transfer in a fragment shader */
       ctx->_ImageTransferState = 0x0;
 
-      transfer = st_no_flush_get_tex_transfer(st, pt, 0, 0, 0,
-                                             PIPE_TRANSFER_WRITE, 0, 0,
-                                             width, height);
+      transfer = pipe_get_transfer(st->pipe, pt, 0, 0,
+                                   PIPE_TRANSFER_WRITE, 0, 0,
+                                   width, height);
 
       /* map texture transfer */
-      dest = pipe->transfer_map(pipe, transfer);
+      dest = pipe_transfer_map(pipe, transfer);
 
 
       /* Put image into texture transfer.
@@ -409,8 +424,8 @@ make_texture(struct st_context *st,
                                unpack);
 
       /* unmap */
-      pipe->transfer_unmap(pipe, transfer);
-      pipe->tex_transfer_destroy(pipe, transfer);
+      pipe_transfer_unmap(pipe, transfer);
+      pipe->transfer_destroy(pipe, transfer);
 
       assert(success);
 
@@ -431,7 +446,7 @@ make_texture(struct st_context *st,
  * \param invertTex  if true, flip texcoords vertically
  */
 static void
-draw_quad(GLcontext *ctx, GLfloat x0, GLfloat y0, GLfloat z,
+draw_quad(struct gl_context *ctx, GLfloat x0, GLfloat y0, GLfloat z,
           GLfloat x1, GLfloat y1, const GLfloat *color,
           GLboolean invertTex, GLfloat maxXcoord, GLfloat maxYcoord)
 {
@@ -451,7 +466,7 @@ draw_quad(GLcontext *ctx, GLfloat x0, GLfloat y0, GLfloat z,
       const GLfloat sLeft = 0.0f, sRight = maxXcoord;
       const GLfloat tTop = invertTex ? maxYcoord : 0.0f;
       const GLfloat tBot = invertTex ? 0.0f : maxYcoord;
-      GLuint tex, i;
+      GLuint i;
 
       /* upper-left */
       verts[0][0][0] = clip_x0;    /* v[0].attr[0].x */
@@ -469,32 +484,31 @@ draw_quad(GLcontext *ctx, GLfloat x0, GLfloat y0, GLfloat z,
       verts[3][0][0] = clip_x0;
       verts[3][0][1] = clip_y1;
 
-      tex = color ? 2 : 1;
-      verts[0][tex][0] = sLeft; /* v[0].attr[tex].s */
-      verts[0][tex][1] = tTop;  /* v[0].attr[tex].t */
-      verts[1][tex][0] = sRight;
-      verts[1][tex][1] = tTop;
-      verts[2][tex][0] = sRight;
-      verts[2][tex][1] = tBot;
-      verts[3][tex][0] = sLeft;
-      verts[3][tex][1] = tBot;
+      verts[0][1][0] = sLeft; /* v[0].attr[1].S */
+      verts[0][1][1] = tTop;  /* v[0].attr[1].T */
+      verts[1][1][0] = sRight;
+      verts[1][1][1] = tTop;
+      verts[2][1][0] = sRight;
+      verts[2][1][1] = tBot;
+      verts[3][1][0] = sLeft;
+      verts[3][1][1] = tBot;
 
       /* same for all verts: */
       if (color) {
          for (i = 0; i < 4; i++) {
-            verts[i][0][2] = z;   /*Z*/
-            verts[i][0][3] = 1.0f; /*W*/
-            verts[i][1][0] = color[0];
-            verts[i][1][1] = color[1];
-            verts[i][1][2] = color[2];
-            verts[i][1][3] = color[3];
-            verts[i][2][2] = 0.0f; /*R*/
-            verts[i][2][3] = 1.0f; /*Q*/
+            verts[i][0][2] = z;         /* v[i].attr[0].z */
+            verts[i][0][3] = 1.0f;      /* v[i].attr[0].w */
+            verts[i][2][0] = color[0];  /* v[i].attr[2].r */
+            verts[i][2][1] = color[1];  /* v[i].attr[2].g */
+            verts[i][2][2] = color[2];  /* v[i].attr[2].b */
+            verts[i][2][3] = color[3];  /* v[i].attr[2].a */
+            verts[i][1][2] = 0.0f;      /* v[i].attr[1].R */
+            verts[i][1][3] = 1.0f;      /* v[i].attr[1].Q */
          }
       }
       else {
          for (i = 0; i < 4; i++) {
-            verts[i][0][2] = z;   /*Z*/
+            verts[i][0][2] = z;    /*Z*/
             verts[i][0][3] = 1.0f; /*W*/
             verts[i][1][2] = 0.0f; /*R*/
             verts[i][1][3] = 1.0f; /*Q*/
@@ -503,44 +517,49 @@ draw_quad(GLcontext *ctx, GLfloat x0, GLfloat y0, GLfloat z,
    }
 
    {
-      struct pipe_buffer *buf;
+      struct pipe_resource *buf;
 
       /* allocate/load buffer object with vertex data */
-      buf = pipe_buffer_create(pipe->screen, 32, PIPE_BUFFER_USAGE_VERTEX,
+      buf = pipe_buffer_create(pipe->screen,
+                              PIPE_BIND_VERTEX_BUFFER,
                                sizeof(verts));
-      st_no_flush_pipe_buffer_write(st, buf, 0, sizeof(verts), verts);
+      pipe_buffer_write(st->pipe, buf, 0, sizeof(verts), verts);
 
       util_draw_vertex_buffer(pipe, buf, 0,
                               PIPE_PRIM_QUADS,
                               4,  /* verts */
                               3); /* attribs/vert */
-      pipe_buffer_reference(&buf, NULL);
+      pipe_resource_reference(&buf, NULL);
    }
 }
 
 
 
 static void
-draw_textured_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
+draw_textured_quad(struct gl_context *ctx, GLint x, GLint y, GLfloat z,
                    GLsizei width, GLsizei height,
                    GLfloat zoomX, GLfloat zoomY,
-                   struct pipe_sampler_view *sv,
+                   struct pipe_sampler_view **sv,
+                   int num_sampler_view,
                    void *driver_vp,
                    void *driver_fp,
                    const GLfloat *color,
-                   GLboolean invertTex)
+                   GLboolean invertTex,
+                   GLboolean write_depth, GLboolean write_stencil)
 {
    struct st_context *st = st_context(ctx);
    struct pipe_context *pipe = st->pipe;
    struct cso_context *cso = st->cso_context;
    GLfloat x0, y0, x1, y1;
    GLsizei maxSize;
+   boolean normalized = sv[0]->texture->target != PIPE_TEXTURE_RECT;
 
    /* limit checks */
    /* XXX if DrawPixels image is larger than max texture size, break
     * it up into chunks.
     */
-   maxSize = 1 << (pipe->screen->get_param(pipe->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
+   maxSize = 1 << (pipe->screen->get_param(pipe->screen,
+                                        PIPE_CAP_MAX_TEXTURE_2D_LEVELS) - 1);
    assert(width <= maxSize);
    assert(height <= maxSize);
 
@@ -551,6 +570,10 @@ draw_textured_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
    cso_save_fragment_shader(cso);
    cso_save_vertex_shader(cso);
    cso_save_vertex_elements(cso);
+   if (write_stencil) {
+      cso_save_depth_stencil_alpha(cso);
+      cso_save_blend(cso);
+   }
 
    /* rasterizer state: just scissor */
    {
@@ -561,6 +584,32 @@ draw_textured_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
       cso_set_rasterizer(cso, &rasterizer);
    }
 
+   if (write_stencil) {
+      /* Stencil writing bypasses the normal fragment pipeline to
+       * disable color writing and set stencil test to always pass.
+       */
+      struct pipe_depth_stencil_alpha_state dsa;
+      struct pipe_blend_state blend;
+
+      /* depth/stencil */
+      memset(&dsa, 0, sizeof(dsa));
+      dsa.stencil[0].enabled = 1;
+      dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
+      dsa.stencil[0].writemask = ctx->Stencil.WriteMask[0] & 0xff;
+      dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
+      if (write_depth) {
+         /* writing depth+stencil: depth test always passes */
+         dsa.depth.enabled = 1;
+         dsa.depth.writemask = ctx->Depth.Mask;
+         dsa.depth.func = PIPE_FUNC_ALWAYS;
+      }
+      cso_set_depth_stencil_alpha(cso, &dsa);
+
+      /* blend (colormask) */
+      memset(&blend, 0, sizeof(blend));
+      cso_set_blend(cso, &blend);
+   }
+
    /* fragment shader state: TEX lookup program */
    cso_set_fragment_shader_handle(cso, driver_fp);
 
@@ -578,10 +627,10 @@ draw_textured_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
       sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
       sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
       sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
-      sampler.normalized_coords = 1;
+      sampler.normalized_coords = normalized;
 
       cso_single_sampler(cso, 0, &sampler);
-      if (st->pixel_xfer.pixelmap_enabled) {
+      if (num_sampler_view > 1) {
          cso_single_sampler(cso, 1, &sampler);
       }
       cso_single_sampler_done(cso);
@@ -606,15 +655,7 @@ draw_textured_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
    cso_set_vertex_elements(cso, 3, st->velems_util_draw);
 
    /* texture state: */
-   if (st->pixel_xfer.pixelmap_enabled) {
-      struct pipe_sampler_view *sampler_views[2];
-      sampler_views[0] = sv;
-      sampler_views[1] = st->pixel_xfer.pixelmap_sampler_view;
-      cso_set_fragment_sampler_views(cso, 2, sampler_views);
-   }
-   else {
-      cso_set_fragment_sampler_views(cso, 1, &sv);
-   }
+   cso_set_fragment_sampler_views(cso, num_sampler_view, sv);
 
    /* Compute Gallium window coords (y=0=top) with pixel zoom.
     * Recall that these coords are transformed by the current
@@ -634,8 +675,8 @@ draw_textured_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
    z = z * 2.0 - 1.0;
 
    draw_quad(ctx, x0, y0, z, x1, y1, color, invertTex,
-             (GLfloat) width / sv->texture->width0,
-             (GLfloat) height / sv->texture->height0);
+             normalized ? ((GLfloat) width / sv[0]->texture->width0) : (GLfloat)width,
+             normalized ? ((GLfloat) height / sv[0]->texture->height0) : (GLfloat)height);
 
    /* restore state */
    cso_restore_rasterizer(cso);
@@ -645,11 +686,19 @@ draw_textured_quad(GLcontext *ctx, GLint x, GLint y, GLfloat z,
    cso_restore_fragment_shader(cso);
    cso_restore_vertex_shader(cso);
    cso_restore_vertex_elements(cso);
+   if (write_stencil) {
+      cso_restore_depth_stencil_alpha(cso);
+      cso_restore_blend(cso);
+   }
 }
 
 
+/**
+ * Software fallback to do glDrawPixels(GL_STENCIL_INDEX) when we
+ * can't use a fragment shader to write stencil values.
+ */
 static void
-draw_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
+draw_stencil_pixels(struct gl_context *ctx, GLint x, GLint y,
                     GLsizei width, GLsizei height, GLenum format, GLenum type,
                     const struct gl_pixelstore_attrib *unpack,
                     const GLvoid *pixels)
@@ -680,16 +729,17 @@ draw_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
    }
 
    if(format != GL_DEPTH_STENCIL && 
-      util_format_get_component_bits(strb->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
+      util_format_get_component_bits(strb->format,
+                                     UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
       usage = PIPE_TRANSFER_READ_WRITE;
    else
       usage = PIPE_TRANSFER_WRITE;
 
-   pt = st_cond_flush_get_tex_transfer(st_context(ctx), strb->texture, 0, 0, 0,
-                                      usage, x, y,
-                                      width, height);
+   pt = pipe_get_transfer(st_context(ctx)->pipe, strb->texture, 0, 0,
+                                     usage, x, y,
+                                     width, height);
 
-   stmap = pipe->transfer_map(pipe, pt);
+   stmap = pipe_transfer_map(pipe, pt);
 
    pixels = _mesa_map_pbo_source(ctx, &clippedUnpack, pixels);
    assert(pixels);
@@ -734,15 +784,15 @@ draw_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
             }
 
             /* now pack the stencil (and Z) values in the dest format */
-            switch (pt->texture->format) {
-            case PIPE_FORMAT_S8_UNORM:
+            switch (pt->resource->format) {
+            case PIPE_FORMAT_S8_USCALED:
                {
                   ubyte *dest = stmap + spanY * pt->stride + spanX;
                   assert(usage == PIPE_TRANSFER_WRITE);
                   memcpy(dest, sValues, spanWidth);
                }
                break;
-            case PIPE_FORMAT_Z24S8_UNORM:
+            case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
                if (format == GL_DEPTH_STENCIL) {
                   uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
                   GLint k;
@@ -760,7 +810,7 @@ draw_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
                   }
                }
                break;
-            case PIPE_FORMAT_S8Z24_UNORM:
+            case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
                if (format == GL_DEPTH_STENCIL) {
                   uint *dest = (uint *) (stmap + spanY * pt->stride + spanX*4);
                   GLint k;
@@ -789,8 +839,63 @@ draw_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
    _mesa_unmap_pbo_source(ctx, &clippedUnpack);
 
    /* unmap the stencil buffer */
-   pipe->transfer_unmap(pipe, pt);
-   pipe->tex_transfer_destroy(pipe, pt);
+   pipe_transfer_unmap(pipe, pt);
+   pipe->transfer_destroy(pipe, pt);
+}
+
+
+/**
+ * Get fragment program variant for a glDrawPixels or glCopyPixels
+ * command for RGBA data.
+ */
+static struct st_fp_variant *
+get_color_fp_variant(struct st_context *st)
+{
+   struct gl_context *ctx = st->ctx;
+   struct st_fp_variant_key key;
+   struct st_fp_variant *fpv;
+
+   memset(&key, 0, sizeof(key));
+
+   key.st = st;
+   key.drawpixels = 1;
+   key.scaleAndBias = (ctx->Pixel.RedBias != 0.0 ||
+                       ctx->Pixel.RedScale != 1.0 ||
+                       ctx->Pixel.GreenBias != 0.0 ||
+                       ctx->Pixel.GreenScale != 1.0 ||
+                       ctx->Pixel.BlueBias != 0.0 ||
+                       ctx->Pixel.BlueScale != 1.0 ||
+                       ctx->Pixel.AlphaBias != 0.0 ||
+                       ctx->Pixel.AlphaScale != 1.0);
+   key.pixelMaps = ctx->Pixel.MapColorFlag;
+
+   fpv = st_get_fp_variant(st, st->fp, &key);
+
+   return fpv;
+}
+
+
+/**
+ * Get fragment program variant for a glDrawPixels or glCopyPixels
+ * command for depth/stencil data.
+ */
+static struct st_fp_variant *
+get_depth_stencil_fp_variant(struct st_context *st, GLboolean write_depth,
+                             GLboolean write_stencil)
+{
+   struct st_fp_variant_key key;
+   struct st_fp_variant *fpv;
+
+   memset(&key, 0, sizeof(key));
+
+   key.st = st;
+   key.drawpixels = 1;
+   key.drawpixels_z = write_depth;
+   key.drawpixels_stencil = write_stencil;
+
+   fpv = st_get_fp_variant(st, st->fp, &key);
+
+   return fpv;
 }
 
 
@@ -798,19 +903,45 @@ draw_stencil_pixels(GLcontext *ctx, GLint x, GLint y,
  * Called via ctx->Driver.DrawPixels()
  */
 static void
-st_DrawPixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
+st_DrawPixels(struct gl_context *ctx, GLint x, GLint y,
+              GLsizei width, GLsizei height,
               GLenum format, GLenum type,
               const struct gl_pixelstore_attrib *unpack, const GLvoid *pixels)
 {
    void *driver_vp, *driver_fp;
    struct st_context *st = st_context(ctx);
    const GLfloat *color;
-
-   if (format == GL_STENCIL_INDEX ||
-       format == GL_DEPTH_STENCIL) {
-      draw_stencil_pixels(ctx, x, y, width, height, format, type,
-                          unpack, pixels);
-      return;
+   struct pipe_context *pipe = st->pipe;
+   GLboolean write_stencil = GL_FALSE, write_depth = GL_FALSE;
+   struct pipe_sampler_view *sv[2];
+   int num_sampler_view = 1;
+   enum pipe_format stencil_format = PIPE_FORMAT_NONE;
+   struct st_fp_variant *fpv;
+
+   if (format == GL_DEPTH_STENCIL)
+      write_stencil = write_depth = GL_TRUE;
+   else if (format == GL_STENCIL_INDEX)
+      write_stencil = GL_TRUE;
+   else if (format == GL_DEPTH_COMPONENT)
+      write_depth = GL_TRUE;
+
+   if (write_stencil) {
+      enum pipe_format tex_format;
+      /* can we write to stencil if not fallback */
+      if (!pipe->screen->get_param(pipe->screen, PIPE_CAP_SHADER_STENCIL_EXPORT))
+        goto stencil_fallback;
+      
+      tex_format = st_choose_format(st->pipe->screen, base_format(format),
+                                    PIPE_TEXTURE_2D,
+                                   0, PIPE_BIND_SAMPLER_VIEW);
+      if (tex_format == PIPE_FORMAT_Z24_UNORM_S8_USCALED)
+        stencil_format = PIPE_FORMAT_X24S8_USCALED;
+      else if (tex_format == PIPE_FORMAT_S8_USCALED_Z24_UNORM)
+        stencil_format = PIPE_FORMAT_S8X24_USCALED;
+      else
+        stencil_format = PIPE_FORMAT_S8_USCALED;
+      if (stencil_format == PIPE_FORMAT_NONE)
+        goto stencil_fallback;
    }
 
    /* Mesa state should be up to date by now */
@@ -818,47 +949,83 @@ st_DrawPixels(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height,
 
    st_validate_state(st);
 
-   if (format == GL_DEPTH_COMPONENT) {
-      driver_fp = make_fragment_shader_z(st);
+   /*
+    * Get vertex/fragment shaders
+    */
+   if (write_depth || write_stencil) {
+      fpv = get_depth_stencil_fp_variant(st, write_depth, write_stencil);
+
+      driver_fp = fpv->driver_shader;
+
       driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
+
       color = ctx->Current.RasterColor;
    }
    else {
-      driver_fp = combined_drawpix_fragment_program(ctx);
+      fpv = get_color_fp_variant(st);
+
+      driver_fp = fpv->driver_shader;
+
       driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
+
       color = NULL;
+      if (st->pixel_xfer.pixelmap_enabled) {
+         sv[1] = st->pixel_xfer.pixelmap_sampler_view;
+         num_sampler_view++;
+      }
    }
 
+   /* update fragment program constants */
+   st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
+
    /* draw with textured quad */
    {
-      struct pipe_texture *pt
+      struct pipe_resource *pt
          = make_texture(st, width, height, format, type, unpack, pixels);
       if (pt) {
-         struct pipe_sampler_view *sv = st_sampler_view_from_texture(st->pipe, pt);
+         sv[0] = st_create_texture_sampler_view(st->pipe, pt);
+
+         if (sv[0]) {
+           if (write_stencil) {
+              sv[1] = st_create_texture_sampler_view_format(st->pipe, pt,
+                                                             stencil_format);
+              num_sampler_view++;
+           }
 
-         if (sv) {
             draw_textured_quad(ctx, x, y, ctx->Current.RasterPos[2],
-                               width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
+                               width, height,
+                               ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
                                sv,
-                               driver_vp, 
+                               num_sampler_view,
+                               driver_vp,
                                driver_fp,
-                               color, GL_FALSE);
-            pipe_sampler_view_reference(&sv, NULL);
+                               color, GL_FALSE, write_depth, write_stencil);
+            pipe_sampler_view_reference(&sv[0], NULL);
+            if (num_sampler_view > 1)
+               pipe_sampler_view_reference(&sv[1], NULL);
          }
-         pipe_texture_reference(&pt, NULL);
+         pipe_resource_reference(&pt, NULL);
       }
    }
+   return;
+
+stencil_fallback:
+   draw_stencil_pixels(ctx, x, y, width, height, format, type,
+                      unpack, pixels);
 }
 
 
 
+/**
+ * Software fallback for glCopyPixels(GL_STENCIL).
+ */
 static void
-copy_stencil_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
+copy_stencil_pixels(struct gl_context *ctx, GLint srcx, GLint srcy,
                     GLsizei width, GLsizei height,
                     GLint dstx, GLint dsty)
 {
-   struct st_renderbuffer *rbDraw = st_renderbuffer(ctx->DrawBuffer->_StencilBuffer);
-   struct pipe_context *pipe = ctx->st->pipe;
+   struct st_renderbuffer *rbDraw;
+   struct pipe_context *pipe = st_context(ctx)->pipe;
    enum pipe_transfer_usage usage;
    struct pipe_transfer *ptDraw;
    ubyte *drawMap;
@@ -871,30 +1038,50 @@ copy_stencil_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
       return;
    }
 
+   /* Get the dest renderbuffer.  If there's a wrapper, use the
+    * underlying renderbuffer.
+    */
+   rbDraw = st_renderbuffer(ctx->DrawBuffer->_StencilBuffer);
+   if (rbDraw->Base.Wrapped)
+      rbDraw = st_renderbuffer(rbDraw->Base.Wrapped);
+
    /* this will do stencil pixel transfer ops */
    st_read_stencil_pixels(ctx, srcx, srcy, width, height,
                           GL_STENCIL_INDEX, GL_UNSIGNED_BYTE,
                           &ctx->DefaultPacking, buffer);
 
-   if(util_format_get_component_bits(rbDraw->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
+   if (0) {
+      /* debug code: dump stencil values */
+      GLint row, col;
+      for (row = 0; row < height; row++) {
+         printf("%3d: ", row);
+         for (col = 0; col < width; col++) {
+            printf("%02x ", buffer[col + row * width]);
+         }
+         printf("\n");
+      }
+   }
+
+   if (util_format_get_component_bits(rbDraw->format,
+                                     UTIL_FORMAT_COLORSPACE_ZS, 0) != 0)
       usage = PIPE_TRANSFER_READ_WRITE;
    else
       usage = PIPE_TRANSFER_WRITE;
-   
+
    if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP) {
       dsty = rbDraw->Base.Height - dsty - height;
    }
 
-   ptDraw = st_cond_flush_get_tex_transfer(st_context(ctx),
-                                          rbDraw->texture, 0, 0, 0,
-                                          usage, dstx, dsty,
-                                          width, height);
+   ptDraw = pipe_get_transfer(st_context(ctx)->pipe,
+                              rbDraw->texture, 0, 0,
+                              usage, dstx, dsty,
+                              width, height);
 
-   assert(util_format_get_blockwidth(ptDraw->texture->format) == 1);
-   assert(util_format_get_blockheight(ptDraw->texture->format) == 1);
+   assert(util_format_get_blockwidth(ptDraw->resource->format) == 1);
+   assert(util_format_get_blockheight(ptDraw->resource->format) == 1);
 
    /* map the stencil buffer */
-   drawMap = pipe->transfer_map(pipe, ptDraw);
+   drawMap = pipe_transfer_map(pipe, ptDraw);
 
    /* draw */
    /* XXX PixelZoom not handled yet */
@@ -912,8 +1099,8 @@ copy_stencil_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
       dst = drawMap + y * ptDraw->stride;
       src = buffer + i * width;
 
-      switch (ptDraw->texture->format) {
-      case PIPE_FORMAT_Z24S8_UNORM:
+      switch (ptDraw->resource->format) {
+      case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
          {
             uint *dst4 = (uint *) dst;
             int j;
@@ -924,7 +1111,7 @@ copy_stencil_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
             }
          }
          break;
-      case PIPE_FORMAT_S8Z24_UNORM:
+      case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
          {
             uint *dst4 = (uint *) dst;
             int j;
@@ -935,7 +1122,7 @@ copy_stencil_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
             }
          }
          break;
-      case PIPE_FORMAT_S8_UNORM:
+      case PIPE_FORMAT_S8_USCALED:
          assert(usage == PIPE_TRANSFER_WRITE);
          memcpy(dst, src, width);
          break;
@@ -947,13 +1134,13 @@ copy_stencil_pixels(GLcontext *ctx, GLint srcx, GLint srcy,
    free(buffer);
 
    /* unmap the stencil buffer */
-   pipe->transfer_unmap(pipe, ptDraw);
-   pipe->tex_transfer_destroy(pipe, ptDraw);
+   pipe_transfer_unmap(pipe, ptDraw);
+   pipe->transfer_destroy(pipe, ptDraw);
 }
 
 
 static void
-st_CopyPixels(GLcontext *ctx, GLint srcx, GLint srcy,
+st_CopyPixels(struct gl_context *ctx, GLint srcx, GLint srcy,
               GLsizei width, GLsizei height,
               GLint dstx, GLint dsty, GLenum type)
 {
@@ -962,15 +1149,16 @@ st_CopyPixels(GLcontext *ctx, GLint srcx, GLint srcy,
    struct pipe_screen *screen = pipe->screen;
    struct st_renderbuffer *rbRead;
    void *driver_vp, *driver_fp;
-   struct pipe_texture *pt;
-   struct pipe_sampler_view *sv;
+   struct pipe_resource *pt;
+   struct pipe_sampler_view *sv[2];
+   int num_sampler_view = 1;
    GLfloat *color;
    enum pipe_format srcFormat, texFormat;
    GLboolean invertTex = GL_FALSE;
    GLint readX, readY, readW, readH;
+   GLuint sample_count;
    struct gl_pixelstore_attrib pack = ctx->DefaultPacking;
-
-   pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
+   struct st_fp_variant *fpv;
 
    st_validate_state(st);
 
@@ -980,38 +1168,66 @@ st_CopyPixels(GLcontext *ctx, GLint srcx, GLint srcy,
       return;
    }
 
+   /*
+    * Get vertex/fragment shaders
+    */
    if (type == GL_COLOR) {
       rbRead = st_get_color_read_renderbuffer(ctx);
       color = NULL;
-      driver_fp = combined_drawpix_fragment_program(ctx);
+
+      fpv = get_color_fp_variant(st);
+      driver_fp = fpv->driver_shader;
+
       driver_vp = make_passthrough_vertex_shader(st, GL_FALSE);
+
+      if (st->pixel_xfer.pixelmap_enabled) {
+         sv[1] = st->pixel_xfer.pixelmap_sampler_view;
+         num_sampler_view++;
+      }
    }
    else {
       assert(type == GL_DEPTH);
       rbRead = st_renderbuffer(ctx->ReadBuffer->_DepthBuffer);
       color = ctx->Current.Attrib[VERT_ATTRIB_COLOR0];
-      driver_fp = make_fragment_shader_z(st);
+
+      fpv = get_depth_stencil_fp_variant(st, GL_TRUE, GL_FALSE);
+      driver_fp = fpv->driver_shader;
+
       driver_vp = make_passthrough_vertex_shader(st, GL_TRUE);
    }
 
+   /* update fragment program constants */
+   st_upload_constants(st, fpv->parameters, PIPE_SHADER_FRAGMENT);
+
+
+   if (rbRead->Base.Wrapped)
+      rbRead = st_renderbuffer(rbRead->Base.Wrapped);
+
+   sample_count = rbRead->texture->nr_samples;
+   /* I believe this would be legal, presumably would need to do a resolve
+      for color, and for depth/stencil spec says to just use one of the
+      depth/stencil samples per pixel? Need some transfer clarifications. */
+   assert(sample_count < 2);
+
    srcFormat = rbRead->texture->format;
 
-   if (screen->is_format_supported(screen, srcFormat, PIPE_TEXTURE_2D, 
-                                   PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
+   if (screen->is_format_supported(screen, srcFormat, st->internal_target,
+                                   sample_count,
+                                   PIPE_BIND_SAMPLER_VIEW, 0)) {
       texFormat = srcFormat;
    }
    else {
       /* srcFormat can't be used as a texture format */
       if (type == GL_DEPTH) {
          texFormat = st_choose_format(screen, GL_DEPTH_COMPONENT,
-                                      PIPE_TEXTURE_2D, 
-                                      PIPE_TEXTURE_USAGE_DEPTH_STENCIL);
+                                      st->internal_target, sample_count,
+                                      PIPE_BIND_DEPTH_STENCIL);
          assert(texFormat != PIPE_FORMAT_NONE);
       }
       else {
          /* default color format */
-         texFormat = st_choose_format(screen, GL_RGBA, PIPE_TEXTURE_2D, 
-                                      PIPE_TEXTURE_USAGE_SAMPLER);
+         texFormat = st_choose_format(screen, GL_RGBA, st->internal_target,
+                                      sample_count, PIPE_BIND_SAMPLER_VIEW);
          assert(texFormat != PIPE_FORMAT_NONE);
       }
    }
@@ -1041,43 +1257,33 @@ st_CopyPixels(GLcontext *ctx, GLint srcx, GLint srcy,
    if (!pt)
       return;
 
-   sv = st_sampler_view_from_texture(st->pipe, pt);
-   if (!sv) {
-      pipe_texture_reference(&pt, NULL);
+   sv[0] = st_create_texture_sampler_view(st->pipe, pt);
+   if (!sv[0]) {
+      pipe_resource_reference(&pt, NULL);
       return;
    }
 
    /* Make temporary texture which is a copy of the src region.
     */
    if (srcFormat == texFormat) {
-      /* copy source framebuffer surface into mipmap/texture */
-      struct pipe_surface *psRead = screen->get_tex_surface(screen,
-                                       rbRead->texture, 0, 0, 0,
-                                       PIPE_BUFFER_USAGE_GPU_READ);
-      struct pipe_surface *psTex = screen->get_tex_surface(screen, pt, 0, 0, 0, 
-                                      PIPE_BUFFER_USAGE_GPU_WRITE );
-
-      pipe->surface_copy(pipe,
-                         psTex,                               /* dest surf */
-                         pack.SkipPixels, pack.SkipRows,      /* dest pos */
-                         psRead,                              /* src surf */
-                         readX, readY, readW, readH);         /* src region */
-
-      if (0) {
-         /* debug */
-         debug_dump_surface(pipe, "copypixsrcsurf", psRead);
-         debug_dump_surface(pipe, "copypixtemptex", psTex);
-      }
+      struct pipe_box src_box;
+      u_box_2d(readX, readY, readW, readH, &src_box);
+    /* copy source framebuffer surface into mipmap/texture */
+      pipe->resource_copy_region(pipe,
+                                 pt,                                /* dest tex */
+                                 0,
+                                 pack.SkipPixels, pack.SkipRows, 0, /* dest pos */
+                                 rbRead->texture,                   /* src tex */
+                                 0,
+                                 &src_box);
 
-      pipe_surface_reference(&psRead, NULL); 
-      pipe_surface_reference(&psTex, NULL);
    }
    else {
       /* CPU-based fallback/conversion */
       struct pipe_transfer *ptRead =
-         st_cond_flush_get_tex_transfer(st, rbRead->texture, 0, 0, 0,
-                                        PIPE_TRANSFER_READ,
-                                        readX, readY, readW, readH);
+         pipe_get_transfer(st->pipe, rbRead->texture, 0, 0,
+                           PIPE_TRANSFER_READ,
+                           readX, readY, readW, readH);
       struct pipe_transfer *ptTex;
       enum pipe_transfer_usage transfer_usage;
 
@@ -1089,8 +1295,8 @@ st_CopyPixels(GLcontext *ctx, GLint srcx, GLint srcy,
       else
          transfer_usage = PIPE_TRANSFER_WRITE;
 
-      ptTex = st_cond_flush_get_tex_transfer(st, pt, 0, 0, 0, transfer_usage,
-                                             0, 0, width, height);
+      ptTex = pipe_get_transfer(st->pipe, pt, 0, 0, transfer_usage,
+                                0, 0, width, height);
 
       /* copy image from ptRead surface to ptTex surface */
       if (type == GL_COLOR) {
@@ -1110,8 +1316,8 @@ st_CopyPixels(GLcontext *ctx, GLint srcx, GLint srcy,
          free(buf);
       }
 
-      pipe->tex_transfer_destroy(pipe, ptRead);
-      pipe->tex_transfer_destroy(pipe, ptTex);
+      pipe->transfer_destroy(pipe, ptRead);
+      pipe->transfer_destroy(pipe, ptTex);
    }
 
    /* OK, the texture 'pt' contains the src image/pixels.  Now draw a
@@ -1119,13 +1325,14 @@ st_CopyPixels(GLcontext *ctx, GLint srcx, GLint srcy,
     */
    draw_textured_quad(ctx, dstx, dsty, ctx->Current.RasterPos[2],
                       width, height, ctx->Pixel.ZoomX, ctx->Pixel.ZoomY,
-                      sv, 
+                      sv,
+                      num_sampler_view,
                       driver_vp, 
                       driver_fp,
-                      color, invertTex);
+                      color, invertTex, GL_FALSE, GL_FALSE);
 
-   pipe_texture_reference(&pt, NULL);
-   pipe_sampler_view_reference(&sv, NULL);
+   pipe_resource_reference(&pt, NULL);
+   pipe_sampler_view_reference(&sv[0], NULL);
 }
 
 
@@ -1140,10 +1347,18 @@ void st_init_drawpixels_functions(struct dd_function_table *functions)
 void
 st_destroy_drawpix(struct st_context *st)
 {
-   st_reference_fragprog(st, &st->drawpix.z_shader, NULL);
+   GLuint i;
+
+   for (i = 0; i < Elements(st->drawpix.shaders); i++) {
+      if (st->drawpix.shaders[i])
+         _mesa_reference_fragprog(st->ctx, &st->drawpix.shaders[i], NULL);
+   }
+
    st_reference_fragprog(st, &st->pixel_xfer.combined_prog, NULL);
    if (st->drawpix.vert_shaders[0])
-      free(st->drawpix.vert_shaders[0]);
+      ureg_free_tokens(st->drawpix.vert_shaders[0]);
    if (st->drawpix.vert_shaders[1])
-      free(st->drawpix.vert_shaders[1]);
+      ureg_free_tokens(st->drawpix.vert_shaders[1]);
 }
+
+#endif /* FEATURE_drawpix */