added z16/z32_git_tile(), change s8z24_get_tile() to return Z as float, ignore stencil
[mesa.git] / src / mesa / pipe / softpipe / sp_surface.c
old mode 100644 (file)
new mode 100755 (executable)
index 5ef8ec9..07def21
@@ -29,8 +29,7 @@
 #include "sp_state.h"
 #include "sp_surface.h"
 #include "pipe/p_defines.h"
-#include "main/imports.h"
-#include "main/macros.h"
+#include "pipe/p_util.h"
 
 
 /**
 /*** PIPE_FORMAT_U_A8_R8_G8_B8 ***/
 
 static void
-a8r8g8b8_read_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
-                         GLfloat (*rrrr)[QUAD_SIZE])
+a8r8g8b8_read_quad_f_swz(struct softpipe_surface *sps, int x, int y,
+                         float (*rrrr)[QUAD_SIZE])
 {
-   const GLuint *src
-      = ((const GLuint *) (sps->surface.region->map + sps->surface.offset))
+   const unsigned *src
+      = ((const unsigned *) (sps->surface.region->map + sps->surface.offset))
       + y * sps->surface.region->pitch + x;
-   GLuint i, j;
+   unsigned i, j;
 
    assert(sps->surface.format == PIPE_FORMAT_U_A8_R8_G8_B8);
-   assert(x < sps->surface.width - 1);
-   assert(y < sps->surface.height - 1);
-
+#if 0
+   assert(x < (int) sps->surface.width - 1);
+   assert(y < (int) sps->surface.height - 1);
+#endif
    for (i = 0; i < 2; i++) { /* loop over pixel row */
       for (j = 0; j < 2; j++) {  /* loop over pixel column */
-         const GLuint p = src[j];
+         const unsigned p = src[j];
          rrrr[0][i * 2 + j] = UBYTE_TO_FLOAT((p >> 16) & 0xff); /*R*/
          rrrr[1][i * 2 + j] = UBYTE_TO_FLOAT((p >>  8) & 0xff); /*G*/
          rrrr[2][i * 2 + j] = UBYTE_TO_FLOAT((p      ) & 0xff); /*B*/
@@ -73,19 +73,19 @@ a8r8g8b8_read_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
 }
 
 static void
-a8r8g8b8_write_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
-                          GLfloat (*rrrr)[QUAD_SIZE])
+a8r8g8b8_write_quad_f_swz(struct softpipe_surface *sps, int x, int y,
+                          float (*rrrr)[QUAD_SIZE])
 {
-   GLuint *dst
-      = ((GLuint *) (sps->surface.region->map + sps->surface.offset))
+   unsigned *dst
+      = ((unsigned *) (sps->surface.region->map + sps->surface.offset))
       + y * sps->surface.region->pitch + x;
-   GLuint i, j;
+   unsigned i, j;
 
    assert(sps->surface.format == PIPE_FORMAT_U_A8_R8_G8_B8);
 
    for (i = 0; i < 2; i++) { /* loop over pixel row */
       for (j = 0; j < 2; j++) {  /* loop over pixel column */
-         GLubyte r, g, b, a;
+         ubyte r, g, b, a;
          UNCLAMPED_FLOAT_TO_UBYTE(r, rrrr[0][i * 2 + j]); /*R*/
          UNCLAMPED_FLOAT_TO_UBYTE(g, rrrr[1][i * 2 + j]); /*G*/
          UNCLAMPED_FLOAT_TO_UBYTE(b, rrrr[2][i * 2 + j]); /*B*/
@@ -98,12 +98,13 @@ a8r8g8b8_write_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
 
 static void
 a8r8g8b8_get_tile(struct pipe_surface *ps,
-                  GLuint x, GLuint y, GLuint w, GLuint h, GLfloat *p)
+                  unsigned x, unsigned y, unsigned w, unsigned h, float *p)
 {
-   const GLuint *src
-      = ((const GLuint *) (ps->region->map + ps->offset))
+   const unsigned *src
+      = ((const unsigned *) (ps->region->map + ps->offset))
       + y * ps->region->pitch + x;
-   GLuint i, j;
+   unsigned i, j;
+   unsigned w0 = w;
 
    assert(ps->format == PIPE_FORMAT_U_A8_R8_G8_B8);
 
@@ -111,22 +112,64 @@ a8r8g8b8_get_tile(struct pipe_surface *ps,
    assert(x + w <= ps->width);
    assert(y + h <= ps->height);
 #else
-   /* temp hack */
+   /* temp clipping hack */
    if (x + w > ps->width)
       w = ps->width - x;
    if (y + h > ps->height)
       h = ps->height -y;
 #endif
    for (i = 0; i < h; i++) {
+      float *pRow = p;
       for (j = 0; j < w; j++) {
-         const GLuint pixel = src[j];
-         p[0] = UBYTE_TO_FLOAT((pixel >> 16) & 0xff);
-         p[1] = UBYTE_TO_FLOAT((pixel >>  8) & 0xff);
-         p[2] = UBYTE_TO_FLOAT((pixel >>  0) & 0xff);
-         p[3] = UBYTE_TO_FLOAT((pixel >> 24) & 0xff);
-         p += 4;
+         const unsigned pixel = src[j];
+         pRow[0] = UBYTE_TO_FLOAT((pixel >> 16) & 0xff);
+         pRow[1] = UBYTE_TO_FLOAT((pixel >>  8) & 0xff);
+         pRow[2] = UBYTE_TO_FLOAT((pixel >>  0) & 0xff);
+         pRow[3] = UBYTE_TO_FLOAT((pixel >> 24) & 0xff);
+         pRow += 4;
       }
       src += ps->region->pitch;
+      p += w0 * 4;
+   }
+}
+
+
+static void
+a8r8g8b8_put_tile(struct pipe_surface *ps,
+                  unsigned x, unsigned y, unsigned w, unsigned h,
+                  const float *p)
+{
+   unsigned *dst
+      = ((unsigned *) (ps->region->map + ps->offset))
+      + y * ps->region->pitch + x;
+   unsigned i, j;
+   unsigned w0 = w;
+
+   assert(ps->format == PIPE_FORMAT_U_A8_R8_G8_B8);
+
+#if 0
+   assert(x + w <= ps->width);
+   assert(y + h <= ps->height);
+#else
+   /* temp clipping hack */
+   if (x + w > ps->width)
+      w = ps->width - x;
+   if (y + h > ps->height)
+      h = ps->height -y;
+#endif
+   for (i = 0; i < h; i++) {
+      const float *pRow = p;
+      for (j = 0; j < w; j++) {
+         unsigned r, g, b, a;
+         UNCLAMPED_FLOAT_TO_UBYTE(r, pRow[0]);
+         UNCLAMPED_FLOAT_TO_UBYTE(g, pRow[1]);
+         UNCLAMPED_FLOAT_TO_UBYTE(b, pRow[2]);
+         UNCLAMPED_FLOAT_TO_UBYTE(a, pRow[3]);
+         dst[j] = (a << 24) | (r << 16) | (g << 8) | b;
+         pRow += 4;
+      }
+      dst += ps->region->pitch;
+      p += w0 * 4;
    }
 }
 
@@ -135,22 +178,22 @@ a8r8g8b8_get_tile(struct pipe_surface *ps,
 
 static void
 a1r5g5b5_get_tile(struct pipe_surface *ps,
-                  GLuint x, GLuint y, GLuint w, GLuint h, GLfloat *p)
+                  unsigned x, unsigned y, unsigned w, unsigned h, float *p)
 {
-   const GLushort *src
-      = ((const GLushort *) (ps->region->map + ps->offset))
+   const ushort *src
+      = ((const ushort *) (ps->region->map + ps->offset))
       + y * ps->region->pitch + x;
-   GLuint i, j;
+   unsigned i, j;
 
    assert(ps->format == PIPE_FORMAT_U_A1_R5_G5_B5);
 
    for (i = 0; i < h; i++) {
       for (j = 0; j < w; j++) {
-         const GLushort pixel = src[j];
-         p[0] = ((pixel >> 10) & 0x1f) * (1.0 / 31);
-         p[1] = ((pixel >>  5) & 0x1f) * (1.0 / 31);
-         p[2] = ((pixel      ) & 0x1f) * (1.0 / 31);
-         p[3] = ((pixel >> 15)       );
+         const ushort pixel = src[j];
+         p[0] = ((pixel >> 10) & 0x1f) * (1.0f / 31.0f);
+         p[1] = ((pixel >>  5) & 0x1f) * (1.0f / 31.0f);
+         p[2] = ((pixel      ) & 0x1f) * (1.0f / 31.0f);
+         p[3] = ((pixel >> 15)       ) * 1.0f;
          p += 4;
       }
       src += ps->region->pitch;
@@ -163,15 +206,15 @@ a1r5g5b5_get_tile(struct pipe_surface *ps,
 
 static void
 z16_read_quad_z(struct softpipe_surface *sps,
-                GLint x, GLint y, GLuint zzzz[QUAD_SIZE])
+                int x, int y, unsigned zzzz[QUAD_SIZE])
 {
-   const GLushort *src
-      = ((const GLushort *) (sps->surface.region->map + sps->surface.offset))
+   const ushort *src
+      = ((const ushort *) (sps->surface.region->map + sps->surface.offset))
       + y * sps->surface.region->pitch + x;
 
    assert(sps->surface.format == PIPE_FORMAT_U_Z16);
 
-   /* converting GLushort to GLuint: */
+   /* converting ushort to unsigned: */
    zzzz[0] = src[0];
    zzzz[1] = src[1];
    src += sps->surface.region->pitch;
@@ -181,15 +224,15 @@ z16_read_quad_z(struct softpipe_surface *sps,
 
 static void
 z16_write_quad_z(struct softpipe_surface *sps,
-                 GLint x, GLint y, const GLuint zzzz[QUAD_SIZE])
+                 int x, int y, const unsigned zzzz[QUAD_SIZE])
 {
-   GLushort *dst
-      = ((GLushort *) (sps->surface.region->map + sps->surface.offset))
+   ushort *dst
+      = ((ushort *) (sps->surface.region->map + sps->surface.offset))
       + y * sps->surface.region->pitch + x;
 
    assert(sps->surface.format == PIPE_FORMAT_U_Z16);
 
-   /* converting GLuint to GLushort: */
+   /* converting unsigned to ushort: */
    dst[0] = zzzz[0];
    dst[1] = zzzz[1];
    dst += sps->surface.region->pitch;
@@ -197,15 +240,395 @@ z16_write_quad_z(struct softpipe_surface *sps,
    dst[1] = zzzz[3];
 }
 
+/**
+ * Return as floats in [0,1].
+ */
+static void
+z16_get_tile(struct pipe_surface *ps,
+             unsigned x, unsigned y, unsigned w, unsigned h, float *p)
+{
+   const ushort *src
+      = ((const ushort *) (ps->region->map + ps->offset))
+      + y * ps->region->pitch + x;
+   const float scale = 1.0 / 65535.0;
+   unsigned i, j;
+   unsigned w0 = w;
+
+   assert(ps->format == PIPE_FORMAT_U_Z16);
+
+#if 0
+   assert(x + w <= ps->width);
+   assert(y + h <= ps->height);
+#else
+   /* temp clipping hack */
+   if (x + w > ps->width)
+      w = ps->width - x;
+   if (y + h > ps->height)
+      h = ps->height -y;
+#endif
+   for (i = 0; i < h; i++) {
+      float *pRow = p;
+      for (j = 0; j < w; j++) {
+         pRow[j] = src[j] * scale;
+      }
+      src += ps->region->pitch;
+      p += w0;
+   }
+}
+
+
+
+
+/*** PIPE_FORMAT_U_L8 ***/
+
+static void
+l8_read_quad_f_swz(struct softpipe_surface *sps, int x, int y,
+                   float (*rrrr)[QUAD_SIZE])
+{
+   const ubyte *src
+      = ((const ubyte *) (sps->surface.region->map + sps->surface.offset))
+      + y * sps->surface.region->pitch + x;
+   unsigned i, j;
+
+   assert(sps->surface.format == PIPE_FORMAT_U_L8);
+   assert(x < (int) sps->surface.width - 1);
+   assert(y < (int) sps->surface.height - 1);
+
+   for (i = 0; i < 2; i++) { /* loop over pixel row */
+      for (j = 0; j < 2; j++) {  /* loop over pixel column */
+         rrrr[0][i * 2 + j] =
+         rrrr[1][i * 2 + j] =
+         rrrr[2][i * 2 + j] = UBYTE_TO_FLOAT(src[j]);
+         rrrr[3][i * 2 + j] = 1.0F;
+      }
+      src += sps->surface.region->pitch;
+   }
+}
+
+static void
+l8_write_quad_f_swz(struct softpipe_surface *sps, int x, int y,
+                    float (*rrrr)[QUAD_SIZE])
+{
+   ubyte *dst
+      = ((ubyte *) (sps->surface.region->map + sps->surface.offset))
+      + y * sps->surface.region->pitch + x;
+   unsigned i, j;
+
+   assert(sps->surface.format == PIPE_FORMAT_U_L8);
+
+   for (i = 0; i < 2; i++) { /* loop over pixel row */
+      for (j = 0; j < 2; j++) {  /* loop over pixel column */
+         ubyte r;
+         UNCLAMPED_FLOAT_TO_UBYTE(r, rrrr[0][i * 2 + j]); /*R*/
+         dst[j] = r;
+      }
+      dst += sps->surface.region->pitch;
+   }
+}
+
+static void
+l8_get_tile(struct pipe_surface *ps,
+                  unsigned x, unsigned y, unsigned w, unsigned h, float *p)
+{
+   const ubyte *src
+      = ((const ubyte *) (ps->region->map + ps->offset))
+      + y * ps->region->pitch + x;
+   unsigned i, j;
+   unsigned w0 = w;
+
+   assert(ps->format == PIPE_FORMAT_U_L8);
+
+#if 0
+   assert(x + w <= ps->width);
+   assert(y + h <= ps->height);
+#else
+   /* temp clipping hack */
+   if (x + w > ps->width)
+      w = ps->width - x;
+   if (y + h > ps->height)
+      h = ps->height -y;
+#endif
+   for (i = 0; i < h; i++) {
+      float *pRow = p;
+      for (j = 0; j < w; j++) {
+         pRow[0] =
+         pRow[1] =
+         pRow[2] = UBYTE_TO_FLOAT(src[j]);
+         pRow[3] = 1.0;
+         pRow += 4;
+      }
+      src += ps->region->pitch;
+      p += w0 * 4;
+   }
+}
+
+
+/*** PIPE_FORMAT_U_A8 ***/
+
+static void
+a8_read_quad_f_swz(struct softpipe_surface *sps, int x, int y,
+                   float (*rrrr)[QUAD_SIZE])
+{
+   const ubyte *src
+      = ((const ubyte *) (sps->surface.region->map + sps->surface.offset))
+      + y * sps->surface.region->pitch + x;
+   unsigned i, j;
+
+   assert(sps->surface.format == PIPE_FORMAT_U_A8);
+   assert(x < (int) sps->surface.width - 1);
+   assert(y < (int) sps->surface.height - 1);
+
+   for (i = 0; i < 2; i++) { /* loop over pixel row */
+      for (j = 0; j < 2; j++) {  /* loop over pixel column */
+         rrrr[0][i * 2 + j] =
+         rrrr[1][i * 2 + j] =
+         rrrr[2][i * 2 + j] = 0.0F;
+         rrrr[3][i * 2 + j] = UBYTE_TO_FLOAT(src[j]);
+      }
+      src += sps->surface.region->pitch;
+   }
+}
+
+static void
+a8_write_quad_f_swz(struct softpipe_surface *sps, int x, int y,
+                    float (*rrrr)[QUAD_SIZE])
+{
+   ubyte *dst
+      = ((ubyte *) (sps->surface.region->map + sps->surface.offset))
+      + y * sps->surface.region->pitch + x;
+   unsigned i, j;
+
+   assert(sps->surface.format == PIPE_FORMAT_U_A8);
+
+   for (i = 0; i < 2; i++) { /* loop over pixel row */
+      for (j = 0; j < 2; j++) {  /* loop over pixel column */
+         ubyte r;
+         UNCLAMPED_FLOAT_TO_UBYTE(r, rrrr[3][i * 2 + j]); /*A*/
+         dst[j] = r;
+      }
+      dst += sps->surface.region->pitch;
+   }
+}
+
+static void
+a8_get_tile(struct pipe_surface *ps,
+                  unsigned x, unsigned y, unsigned w, unsigned h, float *p)
+{
+   const ubyte *src
+      = ((const ubyte *) (ps->region->map + ps->offset))
+      + y * ps->region->pitch + x;
+   unsigned i, j;
+   unsigned w0 = w;
+
+   assert(ps->format == PIPE_FORMAT_U_A8);
+
+#if 0
+   assert(x + w <= ps->width);
+   assert(y + h <= ps->height);
+#else
+   /* temp clipping hack */
+   if (x + w > ps->width)
+      w = ps->width - x;
+   if (y + h > ps->height)
+      h = ps->height -y;
+#endif
+   for (i = 0; i < h; i++) {
+      float *pRow = p;
+      for (j = 0; j < w; j++) {
+         pRow[0] =
+         pRow[1] =
+         pRow[2] = 0.0;
+         pRow[3] = UBYTE_TO_FLOAT(src[j]);
+         pRow += 4;
+      }
+      src += ps->region->pitch;
+      p += w0 * 4;
+   }
+}
+
+
+
+/*** PIPE_FORMAT_U_I8 ***/
+
+static void
+i8_read_quad_f_swz(struct softpipe_surface *sps, int x, int y,
+                   float (*rrrr)[QUAD_SIZE])
+{
+   const ubyte *src
+      = ((const ubyte *) (sps->surface.region->map + sps->surface.offset))
+      + y * sps->surface.region->pitch + x;
+   unsigned i, j;
+
+   assert(sps->surface.format == PIPE_FORMAT_U_I8);
+   assert(x < (int) sps->surface.width - 1);
+   assert(y < (int) sps->surface.height - 1);
+
+   for (i = 0; i < 2; i++) { /* loop over pixel row */
+      for (j = 0; j < 2; j++) {  /* loop over pixel column */
+         rrrr[0][i * 2 + j] =
+         rrrr[1][i * 2 + j] =
+         rrrr[2][i * 2 + j] =
+         rrrr[3][i * 2 + j] = UBYTE_TO_FLOAT(src[j]);
+      }
+      src += sps->surface.region->pitch;
+   }
+}
+
+static void
+i8_write_quad_f_swz(struct softpipe_surface *sps, int x, int y,
+                    float (*rrrr)[QUAD_SIZE])
+{
+   ubyte *dst
+      = ((ubyte *) (sps->surface.region->map + sps->surface.offset))
+      + y * sps->surface.region->pitch + x;
+   unsigned i, j;
+
+   assert(sps->surface.format == PIPE_FORMAT_U_I8);
+
+   for (i = 0; i < 2; i++) { /* loop over pixel row */
+      for (j = 0; j < 2; j++) {  /* loop over pixel column */
+         ubyte r;
+         UNCLAMPED_FLOAT_TO_UBYTE(r, rrrr[0][i * 2 + j]); /*R*/
+         dst[j] = r;
+      }
+      dst += sps->surface.region->pitch;
+   }
+}
+
+static void
+i8_get_tile(struct pipe_surface *ps,
+            unsigned x, unsigned y, unsigned w, unsigned h, float *p)
+{
+   const ubyte *src
+      = ((const ubyte *) (ps->region->map + ps->offset))
+      + y * ps->region->pitch + x;
+   unsigned i, j;
+   unsigned w0 = w;
+
+   assert(ps->format == PIPE_FORMAT_U_I8);
+
+#if 0
+   assert(x + w <= ps->width);
+   assert(y + h <= ps->height);
+#else
+   /* temp clipping hack */
+   if (x + w > ps->width)
+      w = ps->width - x;
+   if (y + h > ps->height)
+      h = ps->height -y;
+#endif
+   for (i = 0; i < h; i++) {
+      float *pRow = p;
+      for (j = 0; j < w; j++) {
+         pRow[0] =
+         pRow[1] =
+         pRow[2] =
+         pRow[3] = UBYTE_TO_FLOAT(src[j]);
+         pRow += 4;
+      }
+      src += ps->region->pitch;
+      p += w0 * 4;
+   }
+}
+
+
+/*** PIPE_FORMAT_U_A8_L8 ***/
+
+static void
+a8_l8_read_quad_f_swz(struct softpipe_surface *sps, int x, int y,
+                      float (*rrrr)[QUAD_SIZE])
+{
+   const ushort *src
+      = ((const ushort *) (sps->surface.region->map + sps->surface.offset))
+      + y * sps->surface.region->pitch + x;
+   unsigned i, j;
+
+   assert(sps->surface.format == PIPE_FORMAT_U_A8_L8);
+   assert(x < (int) sps->surface.width - 1);
+   assert(y < (int) sps->surface.height - 1);
+
+   for (i = 0; i < 2; i++) { /* loop over pixel row */
+      for (j = 0; j < 2; j++) {  /* loop over pixel column */
+         const ushort p = src[j];
+         rrrr[0][i * 2 + j] =
+         rrrr[1][i * 2 + j] =
+         rrrr[2][i * 2 + j] = UBYTE_TO_FLOAT(p >> 8);
+         rrrr[3][i * 2 + j] = UBYTE_TO_FLOAT(p & 0xff);
+      }
+      src += sps->surface.region->pitch;
+   }
+}
+
+static void
+a8_l8_write_quad_f_swz(struct softpipe_surface *sps, int x, int y,
+                    float (*rrrr)[QUAD_SIZE])
+{
+   ushort *dst
+      = ((ushort *) (sps->surface.region->map + sps->surface.offset))
+      + y * sps->surface.region->pitch + x;
+   unsigned i, j;
+
+   assert(sps->surface.format == PIPE_FORMAT_U_A8_L8);
+
+   for (i = 0; i < 2; i++) { /* loop over pixel row */
+      for (j = 0; j < 2; j++) {  /* loop over pixel column */
+         ubyte l, a;
+         UNCLAMPED_FLOAT_TO_UBYTE(l, rrrr[0][i * 2 + j]); /*R*/
+         UNCLAMPED_FLOAT_TO_UBYTE(a, rrrr[3][i * 2 + j]); /*A*/
+         dst[j] = (l << 8) | a;
+      }
+      dst += sps->surface.region->pitch;
+   }
+}
+
+static void
+a8_l8_get_tile(struct pipe_surface *ps,
+            unsigned x, unsigned y, unsigned w, unsigned h, float *p)
+{
+   const ushort *src
+      = ((const ushort *) (ps->region->map + ps->offset))
+      + y * ps->region->pitch + x;
+   unsigned i, j;
+   unsigned w0 = w;
+
+   assert(ps->format == PIPE_FORMAT_U_A8_L8);
+
+#if 0
+   assert(x + w <= ps->width);
+   assert(y + h <= ps->height);
+#else
+   /* temp clipping hack */
+   if (x + w > ps->width)
+      w = ps->width - x;
+   if (y + h > ps->height)
+      h = ps->height -y;
+#endif
+   for (i = 0; i < h; i++) {
+      float *pRow = p;
+      for (j = 0; j < w; j++) {
+         const ushort p = src[j];
+         pRow[0] =
+         pRow[1] =
+         pRow[2] = UBYTE_TO_FLOAT(p & 0xff);
+         pRow[3] = UBYTE_TO_FLOAT(p >> 8);
+         pRow += 4;
+      }
+      src += ps->region->pitch;
+      p += w0 * 4;
+   }
+}
+
+
+
 
 /*** PIPE_FORMAT_U_Z32 ***/
 
 static void
 z32_read_quad_z(struct softpipe_surface *sps,
-                GLint x, GLint y, GLuint zzzz[QUAD_SIZE])
+                int x, int y, unsigned zzzz[QUAD_SIZE])
 {
-   const GLuint *src
-      = ((GLuint *) (sps->surface.region->map + sps->surface.offset))
+   const unsigned *src
+      = ((unsigned *) (sps->surface.region->map + sps->surface.offset))
       + y * sps->surface.region->pitch + x;
 
    assert(sps->surface.format == PIPE_FORMAT_U_Z32);
@@ -219,10 +642,10 @@ z32_read_quad_z(struct softpipe_surface *sps,
 
 static void
 z32_write_quad_z(struct softpipe_surface *sps,
-                 GLint x, GLint y, const GLuint zzzz[QUAD_SIZE])
+                 int x, int y, const unsigned zzzz[QUAD_SIZE])
 {
-   GLuint *dst
-      = ((GLuint *) (sps->surface.region->map + sps->surface.offset))
+   unsigned *dst
+      = ((unsigned *) (sps->surface.region->map + sps->surface.offset))
       + y * sps->surface.region->pitch + x;
 
    assert(sps->surface.format == PIPE_FORMAT_U_Z32);
@@ -234,16 +657,52 @@ z32_write_quad_z(struct softpipe_surface *sps,
    dst[1] = zzzz[3];
 }
 
+/**
+ * Return as floats in [0,1].
+ */
+static void
+z32_get_tile(struct pipe_surface *ps,
+             unsigned x, unsigned y, unsigned w, unsigned h, float *p)
+{
+   const uint *src
+      = ((const uint *) (ps->region->map + ps->offset))
+      + y * ps->region->pitch + x;
+   const double scale = 1.0 / (double) 0xffffffff;
+   unsigned i, j;
+   unsigned w0 = w;
+
+   assert(ps->format == PIPE_FORMAT_U_Z16);
+
+#if 0
+   assert(x + w <= ps->width);
+   assert(y + h <= ps->height);
+#else
+   /* temp clipping hack */
+   if (x + w > ps->width)
+      w = ps->width - x;
+   if (y + h > ps->height)
+      h = ps->height -y;
+#endif
+   for (i = 0; i < h; i++) {
+      float *pRow = p;
+      for (j = 0; j < w; j++) {
+         pRow[j] = src[j] * scale;
+      }
+      src += ps->region->pitch;
+      p += w0;
+   }
+}
+
 
 /*** PIPE_FORMAT_S8_Z24 ***/
 
 static void
 s8z24_read_quad_z(struct softpipe_surface *sps,
-                  GLint x, GLint y, GLuint zzzz[QUAD_SIZE])
+                  int x, int y, unsigned zzzz[QUAD_SIZE])
 {
-   static const GLuint mask = 0x00ffffff;
-   const GLuint *src
-      = ((GLuint *) (sps->surface.region->map + sps->surface.offset))
+   static const unsigned mask = 0x00ffffff;
+   const unsigned *src
+      = ((unsigned *) (sps->surface.region->map + sps->surface.offset))
       + y * sps->surface.region->pitch + x;
 
    assert(sps->surface.format == PIPE_FORMAT_S8_Z24);
@@ -258,11 +717,11 @@ s8z24_read_quad_z(struct softpipe_surface *sps,
 
 static void
 s8z24_write_quad_z(struct softpipe_surface *sps,
-                   GLint x, GLint y, const GLuint zzzz[QUAD_SIZE])
+                   int x, int y, const unsigned zzzz[QUAD_SIZE])
 {
-   static const GLuint mask = 0xff000000;
-   GLuint *dst
-      = ((GLuint *) (sps->surface.region->map + sps->surface.offset))
+   static const unsigned mask = 0xff000000;
+   unsigned *dst
+      = ((unsigned *) (sps->surface.region->map + sps->surface.offset))
       + y * sps->surface.region->pitch + x;
 
    assert(sps->surface.format == PIPE_FORMAT_S8_Z24);
@@ -277,10 +736,10 @@ s8z24_write_quad_z(struct softpipe_surface *sps,
 
 static void
 s8z24_read_quad_stencil(struct softpipe_surface *sps,
-                        GLint x, GLint y, GLubyte ssss[QUAD_SIZE])
+                        int x, int y, ubyte ssss[QUAD_SIZE])
 {
-   const GLuint *src
-      = ((GLuint *) (sps->surface.region->map + sps->surface.offset))
+   const unsigned *src
+      = ((unsigned *) (sps->surface.region->map + sps->surface.offset))
       + y * sps->surface.region->pitch + x;
 
    assert(sps->surface.format == PIPE_FORMAT_S8_Z24);
@@ -294,11 +753,11 @@ s8z24_read_quad_stencil(struct softpipe_surface *sps,
 
 static void
 s8z24_write_quad_stencil(struct softpipe_surface *sps,
-                         GLint x, GLint y, const GLubyte ssss[QUAD_SIZE])
+                         int x, int y, const ubyte ssss[QUAD_SIZE])
 {
-   static const GLuint mask = 0x00ffffff;
-   GLuint *dst
-      = ((GLuint *) (sps->surface.region->map + sps->surface.offset))
+   static const unsigned mask = 0x00ffffff;
+   unsigned *dst
+      = ((unsigned *) (sps->surface.region->map + sps->surface.offset))
       + y * sps->surface.region->pitch + x;
 
    assert(sps->surface.format == PIPE_FORMAT_S8_Z24);
@@ -311,13 +770,50 @@ s8z24_write_quad_stencil(struct softpipe_surface *sps,
 }
 
 
+/**
+ * Return Z component as float in [0,1].  Stencil part ignored.
+ */
+static void
+s8z24_get_tile(struct pipe_surface *ps,
+               unsigned x, unsigned y, unsigned w, unsigned h, float *p)
+{
+   const uint *src
+      = ((const uint *) (ps->region->map + ps->offset))
+      + y * ps->region->pitch + x;
+   const double scale = 1.0 / ((1 << 24) - 1);
+   unsigned i, j;
+   unsigned w0 = w;
+
+   assert(ps->format == PIPE_FORMAT_S8_Z24);
+
+#if 0
+   assert(x + w <= ps->width);
+   assert(y + h <= ps->height);
+#else
+   /* temp clipping hack */
+   if (x + w > ps->width)
+      w = ps->width - x;
+   if (y + h > ps->height)
+      h = ps->height -y;
+#endif
+   for (i = 0; i < h; i++) {
+      float *pRow = p;
+      for (j = 0; j < w; j++) {
+         pRow[j] = (src[j] & 0xffffff) * scale;
+      }
+      src += ps->region->pitch;
+      p += w0;
+   }
+}
+
+
 /*** PIPE_FORMAT_U_S8 ***/
 
 static void
 s8_read_quad_stencil(struct softpipe_surface *sps,
-                     GLint x, GLint y, GLubyte ssss[QUAD_SIZE])
+                     int x, int y, ubyte ssss[QUAD_SIZE])
 {
-   const GLubyte *src
+   const ubyte *src
       = sps->surface.region->map + sps->surface.offset
       + y * sps->surface.region->pitch + x;
 
@@ -332,9 +828,9 @@ s8_read_quad_stencil(struct softpipe_surface *sps,
 
 static void
 s8_write_quad_stencil(struct softpipe_surface *sps,
-                      GLint x, GLint y, const GLubyte ssss[QUAD_SIZE])
+                      int x, int y, const ubyte ssss[QUAD_SIZE])
 {
-   GLubyte *dst
+   ubyte *dst
       = sps->surface.region->map + sps->surface.offset
       + y * sps->surface.region->pitch + x;
 
@@ -348,32 +844,63 @@ s8_write_quad_stencil(struct softpipe_surface *sps,
 }
 
 
+/**
+ * Initialize the quad_read/write and get/put_tile() methods.
+ */
 void
 softpipe_init_surface_funcs(struct softpipe_surface *sps)
 {
+   assert(sps->surface.format);
+
    switch (sps->surface.format) {
    case PIPE_FORMAT_U_A8_R8_G8_B8:
       sps->read_quad_f_swz = a8r8g8b8_read_quad_f_swz;
       sps->write_quad_f_swz = a8r8g8b8_write_quad_f_swz;
       sps->surface.get_tile = a8r8g8b8_get_tile;
+      sps->surface.put_tile = a8r8g8b8_put_tile;
       break;
    case PIPE_FORMAT_U_A1_R5_G5_B5:
       sps->surface.get_tile = a1r5g5b5_get_tile;
       break;
+   case PIPE_FORMAT_U_L8:
+      sps->read_quad_f_swz = l8_read_quad_f_swz;
+      sps->write_quad_f_swz = l8_write_quad_f_swz;
+      sps->surface.get_tile = l8_get_tile;
+      break;
+   case PIPE_FORMAT_U_A8:
+      sps->read_quad_f_swz = a8_read_quad_f_swz;
+      sps->write_quad_f_swz = a8_write_quad_f_swz;
+      sps->surface.get_tile = a8_get_tile;
+      break;
+   case PIPE_FORMAT_U_I8:
+      sps->read_quad_f_swz = i8_read_quad_f_swz;
+      sps->write_quad_f_swz = i8_write_quad_f_swz;
+      sps->surface.get_tile = i8_get_tile;
+      break;
+   case PIPE_FORMAT_U_A8_L8:
+      sps->read_quad_f_swz = a8_l8_read_quad_f_swz;
+      sps->write_quad_f_swz = a8_l8_write_quad_f_swz;
+      sps->surface.get_tile = a8_l8_get_tile;
+      break;
+
    case PIPE_FORMAT_U_Z16:
       sps->read_quad_z = z16_read_quad_z;
       sps->write_quad_z = z16_write_quad_z;
+      sps->surface.get_tile = z16_get_tile;
       break;
    case PIPE_FORMAT_U_Z32:
       sps->read_quad_z = z32_read_quad_z;
       sps->write_quad_z = z32_write_quad_z;
+      sps->surface.get_tile = z32_get_tile;
       break;
    case PIPE_FORMAT_S8_Z24:
       sps->read_quad_z = s8z24_read_quad_z;
       sps->write_quad_z = s8z24_write_quad_z;
       sps->read_quad_stencil = s8z24_read_quad_stencil;
       sps->write_quad_stencil = s8z24_write_quad_stencil;
+      sps->surface.get_tile = s8z24_get_tile;
       break;
+
    case PIPE_FORMAT_U_S8:
       sps->read_quad_stencil = s8_read_quad_stencil;
       sps->write_quad_stencil = s8_write_quad_stencil;
@@ -385,7 +912,7 @@ softpipe_init_surface_funcs(struct softpipe_surface *sps)
 
 
 static struct pipe_surface *
-softpipe_surface_alloc(struct pipe_context *pipe, GLuint pipeFormat)
+softpipe_surface_alloc(struct pipe_context *pipe, unsigned pipeFormat)
 {
    struct softpipe_surface *sps = CALLOC_STRUCT(softpipe_surface);
    if (!sps)
@@ -411,17 +938,17 @@ softpipe_surface_alloc(struct pipe_context *pipe, GLuint pipeFormat)
 struct pipe_surface *
 softpipe_get_tex_surface(struct pipe_context *pipe,
                          struct pipe_mipmap_tree *mt,
-                         GLuint face, GLuint level, GLuint zslice)
+                         unsigned face, unsigned level, unsigned zslice)
 {
    struct pipe_surface *ps;
-   GLuint offset;  /* in bytes */
+   unsigned offset;  /* in bytes */
 
    offset = mt->level[level].level_offset;
 
-   if (mt->target == GL_TEXTURE_CUBE_MAP_ARB) {
+   if (mt->target == PIPE_TEXTURE_CUBE) {
       offset += mt->level[level].image_offset[face] * mt->cpp;
    }
-   else if (mt->target == GL_TEXTURE_3D) {
+   else if (mt->target == PIPE_TEXTURE_3D) {
       offset += mt->level[level].image_offset[zslice] * mt->cpp;
    }
    else {
@@ -433,7 +960,7 @@ softpipe_get_tex_surface(struct pipe_context *pipe,
    if (ps) {
       assert(ps->format);
       assert(ps->refcount);
-      ps->region = mt->region;
+      pipe_region_reference(&ps->region, mt->region);
       ps->width = mt->level[level].width;
       ps->height = mt->level[level].height;
       ps->offset = offset;