From 09574bee850edec46d967b7cdbc2073f92b8223d Mon Sep 17 00:00:00 2001
From: Brian <brian@i915.localnet.net>
Date: Mon, 30 Jul 2007 14:00:27 -0600
Subject: implement read_quad_f_swz()

---
 src/mesa/drivers/dri/i915tex/intel_surface.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

(limited to 'src/mesa')

diff --git a/src/mesa/drivers/dri/i915tex/intel_surface.c b/src/mesa/drivers/dri/i915tex/intel_surface.c
index 29b640e58ee..3be902cf9c1 100644
--- a/src/mesa/drivers/dri/i915tex/intel_surface.c
+++ b/src/mesa/drivers/dri/i915tex/intel_surface.c
@@ -23,17 +23,28 @@
  * XXX a lof of this is a temporary kludge
  */
 
-extern void
-intel_map_unmap_buffers(struct intel_context *intel, GLboolean map);
-
 
 
 static void
-read_quad_f_swz(struct softpipe_surface *gs, GLint x, GLint y,
+read_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
                 GLfloat (*rrrr)[QUAD_SIZE])
 {
+   struct intel_surface *is = (struct intel_surface *) sps;
+   struct intel_renderbuffer *irb = is->rb;
+   const GLubyte *src = (const GLubyte *) irb->region->map
+      + (y * irb->region->pitch + x) * irb->region->cpp;
+   GLfloat *dst = (GLfloat *) rrrr;
+   GLubyte temp[16];
+   GLuint i, j;
 
+   memcpy(temp, src, 8);
+   memcpy(temp + 8, src + irb->region->pitch * irb->region->cpp, 8);
 
+   for (i = 0; i < 4; i++) {
+      for (j = 0; j < 4; j++) {
+         dst[j * 4 + i] = UBYTE_TO_FLOAT(temp[i * 4 + j]);
+      }
+   }
 }
 
 
@@ -55,8 +66,6 @@ write_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
       }
    }
 
-   printf("intel_surface::write_quad\n");
-
    memcpy(dst, temp, 8);
    memcpy(dst + irb->region->pitch * irb->region->cpp, temp + 8, 8);
 }
-- 
cgit v1.2.3


From 6558af67d380f2855b112ea3ce4dded9215c7cf7 Mon Sep 17 00:00:00 2001
From: Brian <brian@i915.localnet.net>
Date: Mon, 30 Jul 2007 17:15:50 -0600
Subject: compute scale for Z24 buffers

---
 src/mesa/pipe/softpipe/sp_quad_depth_test.c | 2 ++
 1 file changed, 2 insertions(+)

(limited to 'src/mesa')

diff --git a/src/mesa/pipe/softpipe/sp_quad_depth_test.c b/src/mesa/pipe/softpipe/sp_quad_depth_test.c
index d47c4c42b8f..a26bd51d846 100644
--- a/src/mesa/pipe/softpipe/sp_quad_depth_test.c
+++ b/src/mesa/pipe/softpipe/sp_quad_depth_test.c
@@ -59,6 +59,8 @@ sp_depth_test_quad(struct quad_stage *qs, struct quad_header *quad)
     */
    if (sps->surface.format == PIPE_FORMAT_U_Z16)
       scale = 65535.0;
+   else if (sps->surface.format == PIPE_FORMAT_Z24_S8)
+      scale = (float) ((1 << 24) - 1);
    else
       assert(0);  /* XXX fix this someday */
 
-- 
cgit v1.2.3


From 4576d754c98e3fb5d413e294d48fb70a893defcf Mon Sep 17 00:00:00 2001
From: Brian <brian@i915.localnet.net>
Date: Mon, 30 Jul 2007 17:17:44 -0600
Subject: Lots of improvements to the surface-related code.

Z testing now works with i915 driver.
Add gl_renderbuffer::surface pointer (and reverse pointer).
Remove intel_surface and xmesa_surface types - no longer used.
---
 src/mesa/drivers/dri/i915tex/intel_fbo.c     |  18 ++
 src/mesa/drivers/dri/i915tex/intel_fbo.h     |  14 +-
 src/mesa/drivers/dri/i915tex/intel_surface.c | 231 ++++++++++++++-------
 src/mesa/drivers/x11/xm_buffer.c             |   8 +
 src/mesa/drivers/x11/xm_surface.c            | 288 +++++----------------------
 src/mesa/drivers/x11/xmesaP.h                |  14 +-
 src/mesa/main/mtypes.h                       |   3 +
 src/mesa/main/renderbuffer.c                 |  22 +-
 src/mesa/pipe/p_state.h                      |  16 +-
 src/mesa/pipe/softpipe/sp_context.c          |  13 ++
 src/mesa/pipe/softpipe/sp_z_surface.c        | 118 +++++++++++
 src/mesa/pipe/softpipe/sp_z_surface.h        |  37 ++++
 src/mesa/sources                             |   1 +
 src/mesa/state_tracker/st_atom_framebuffer.c |  19 +-
 14 files changed, 449 insertions(+), 353 deletions(-)
 create mode 100644 src/mesa/pipe/softpipe/sp_z_surface.c
 create mode 100644 src/mesa/pipe/softpipe/sp_z_surface.h

(limited to 'src/mesa')

diff --git a/src/mesa/drivers/dri/i915tex/intel_fbo.c b/src/mesa/drivers/dri/i915tex/intel_fbo.c
index a09db46163d..5a93eb7ad19 100644
--- a/src/mesa/drivers/dri/i915tex/intel_fbo.c
+++ b/src/mesa/drivers/dri/i915tex/intel_fbo.c
@@ -289,6 +289,12 @@ intel_alloc_renderbuffer_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
       rb->Width = width;
       rb->Height = height;
 
+#if 1
+      /* update the surface's size too */
+      rb->surface->width = width;
+      rb->surface->height = height;
+#endif
+
       /* This sets the Get/PutRow/Value functions */
       intel_set_span_functions(&irb->Base);
 
@@ -451,6 +457,12 @@ intel_create_renderbuffer(GLenum intFormat, GLsizei width, GLsizei height,
    return irb;
 }
 
+
+/**
+ * Create a new renderbuffer which corresponds to an X window buffer
+ * (color, depth, stencil, etc) - not a user-created GL renderbuffer.
+ * The internal format is set at creation time and does not change.
+ */
 struct gl_renderbuffer *
 intel_new_renderbuffer_fb(GLcontext * ctx, GLuint intFormat)
 {
@@ -472,6 +484,9 @@ intel_new_renderbuffer_fb(GLcontext * ctx, GLuint intFormat)
    irb->Base.GetPointer = intel_get_pointer;
    /* span routines set in alloc_storage function */
 
+   irb->Base.surface = intel_new_surface(intFormat);
+   irb->Base.surface->rb = irb;
+
    return &irb->Base;
 }
 
@@ -500,6 +515,9 @@ intel_new_renderbuffer(GLcontext * ctx, GLuint name)
    irb->Base.GetPointer = intel_get_pointer;
    /* span routines set in alloc_storage function */
 
+   irb->Base.surface = intel_new_surface(0 /*unknown format*/);
+   irb->Base.surface->rb = irb;
+
    return &irb->Base;
 }
 
diff --git a/src/mesa/drivers/dri/i915tex/intel_fbo.h b/src/mesa/drivers/dri/i915tex/intel_fbo.h
index 1642ce774f0..86c81060845 100644
--- a/src/mesa/drivers/dri/i915tex/intel_fbo.h
+++ b/src/mesa/drivers/dri/i915tex/intel_fbo.h
@@ -37,17 +37,6 @@ struct intel_context;
 struct intel_region;
 
 
-/**
- * Intel "pipe" surface.  This is kind of a temporary thing as
- * renderbuffers and surfaces should eventually become one.
- */
-struct intel_surface
-{
-   struct softpipe_surface surface;  /**< base class */
-   struct intel_renderbuffer *rb;    /**< ptr back to matching renderbuffer */
-};
-
-
 /**
  * Intel framebuffer, derived from gl_framebuffer.
  */
@@ -129,5 +118,8 @@ extern struct intel_region *intel_get_rb_region(struct gl_framebuffer *fb,
 
 
 
+extern struct pipe_surface *
+intel_new_surface(GLuint intFormat);
+
 
 #endif /* INTEL_FBO_H */
diff --git a/src/mesa/drivers/dri/i915tex/intel_surface.c b/src/mesa/drivers/dri/i915tex/intel_surface.c
index 3be902cf9c1..043c5aa5feb 100644
--- a/src/mesa/drivers/dri/i915tex/intel_surface.c
+++ b/src/mesa/drivers/dri/i915tex/intel_surface.c
@@ -23,27 +23,33 @@
  * XXX a lof of this is a temporary kludge
  */
 
+/**
+ * Note: the arithmetic/addressing in these functions is a little
+ * tricky since we need to invert the Y axis.
+ */
 
 
 static void
 read_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
                 GLfloat (*rrrr)[QUAD_SIZE])
 {
-   struct intel_surface *is = (struct intel_surface *) sps;
-   struct intel_renderbuffer *irb = is->rb;
-   const GLubyte *src = (const GLubyte *) irb->region->map
-      + (y * irb->region->pitch + x) * irb->region->cpp;
+   const GLint bytesPerRow = sps->surface.stride * sps->surface.cpp;
+   const GLint invY = sps->surface.height - y - 1;
+   const GLubyte *src = sps->surface.ptr + invY * bytesPerRow + x * sps->surface.cpp;
    GLfloat *dst = (GLfloat *) rrrr;
    GLubyte temp[16];
-   GLuint i, j;
+   GLuint j;
+
+   assert(sps->surface.format == PIPE_FORMAT_U_A8_R8_G8_B8);
 
-   memcpy(temp, src, 8);
-   memcpy(temp + 8, src + irb->region->pitch * irb->region->cpp, 8);
+   memcpy(temp + 8, src, 8);
+   memcpy(temp + 0, src + bytesPerRow, 8);
 
-   for (i = 0; i < 4; i++) {
-      for (j = 0; j < 4; j++) {
-         dst[j * 4 + i] = UBYTE_TO_FLOAT(temp[i * 4 + j]);
-      }
+   for (j = 0; j < 4; j++) {
+      dst[0 * 4 + j] = UBYTE_TO_FLOAT(temp[j * 4 + 2]); /*R*/
+      dst[1 * 4 + j] = UBYTE_TO_FLOAT(temp[j * 4 + 1]); /*G*/
+      dst[2 * 4 + j] = UBYTE_TO_FLOAT(temp[j * 4 + 0]); /*B*/
+      dst[3 * 4 + j] = UBYTE_TO_FLOAT(temp[j * 4 + 3]); /*A*/
    }
 }
 
@@ -52,45 +58,128 @@ static void
 write_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
                  GLfloat (*rrrr)[QUAD_SIZE])
 {
-   struct intel_surface *is = (struct intel_surface *) sps;
-   struct intel_renderbuffer *irb = is->rb;
    const GLfloat *src = (const GLfloat *) rrrr;
-   GLubyte *dst = (GLubyte *) irb->region->map
-      + (y * irb->region->pitch + x) * irb->region->cpp;
+   const GLint bytesPerRow = sps->surface.stride * sps->surface.cpp;
+   const GLint invY = sps->surface.height - y - 1;
+   GLubyte *dst = sps->surface.ptr + invY * bytesPerRow + x * sps->surface.cpp;
    GLubyte temp[16];
-   GLuint i, j;
+   GLuint j;
+
+   assert(sps->surface.format == PIPE_FORMAT_U_A8_R8_G8_B8);
 
-   for (i = 0; i < 4; i++) {
-      for (j = 0; j < 4; j++) {
-         UNCLAMPED_FLOAT_TO_UBYTE(temp[j * 4 + i], src[i * 4 + j]);
-      }
+   for (j = 0; j < 4; j++) {
+      UNCLAMPED_FLOAT_TO_UBYTE(temp[j * 4 + 2], src[0 * 4 + j]); /*R*/
+      UNCLAMPED_FLOAT_TO_UBYTE(temp[j * 4 + 1], src[1 * 4 + j]); /*G*/
+      UNCLAMPED_FLOAT_TO_UBYTE(temp[j * 4 + 0], src[2 * 4 + j]); /*B*/
+      UNCLAMPED_FLOAT_TO_UBYTE(temp[j * 4 + 3], src[3 * 4 + j]); /*A*/
    }
 
-   memcpy(dst, temp, 8);
-   memcpy(dst + irb->region->pitch * irb->region->cpp, temp + 8, 8);
+   memcpy(dst, temp + 8, 8);
+   memcpy(dst + bytesPerRow, temp + 0, 8);
 }
 
 
 
+static void
+read_quad_z24(struct softpipe_surface *sps,
+              GLint x, GLint y, GLuint zzzz[QUAD_SIZE])
+{
+   static const GLuint mask = 0xffffff;
+   const GLint invY = sps->surface.height - y - 1;
+   const GLuint *src
+      = (GLuint *) (sps->surface.ptr
+                    + (invY * sps->surface.stride + x) * sps->surface.cpp);
+
+   assert(sps->surface.format == PIPE_FORMAT_Z24_S8);
+
+   /* extract lower three bytes */
+   zzzz[0] = src[0] & mask;
+   zzzz[1] = src[1] & mask;
+   zzzz[2] = src[-sps->surface.stride] & mask;
+   zzzz[3] = src[-sps->surface.stride + 1] & mask;
+}
+
+static void
+write_quad_z24(struct softpipe_surface *sps,
+               GLint x, GLint y, const GLuint zzzz[QUAD_SIZE])
+{
+   static const GLuint mask = 0xff000000;
+   const GLint invY = sps->surface.height - y - 1;
+   GLuint *dst
+      = (GLuint *) (sps->surface.ptr
+                    + (invY * sps->surface.stride + x) * sps->surface.cpp);
+
+   assert(sps->surface.format == PIPE_FORMAT_Z24_S8);
+
+   /* write lower three bytes */
+   dst[0] = (dst[0] & mask) | zzzz[0];
+   dst[1] = (dst[1] & mask) | zzzz[1];
+   dst -= sps->surface.stride;
+   dst[0] = (dst[0] & mask) | zzzz[2];
+   dst[1] = (dst[1] & mask) | zzzz[3];
+}
+
+
+static void
+read_quad_stencil(struct softpipe_surface *sps,
+                  GLint x, GLint y, GLubyte ssss[QUAD_SIZE])
+{
+   const GLint invY = sps->surface.height - y - 1;
+   const GLuint *src = (const GLuint *) (sps->surface.ptr
+                     + (invY * sps->surface.stride + x) * sps->surface.cpp);
+
+   assert(sps->surface.format == PIPE_FORMAT_Z24_S8);
+
+   /* extract high byte */
+   ssss[0] = src[0] >> 24;
+   ssss[1] = src[1] >> 24;
+   ssss[2] = src[-sps->surface.width] >> 24;
+   ssss[3] = src[-sps->surface.width + 1] >> 24;
+}
+
+static void
+write_quad_stencil(struct softpipe_surface *sps,
+                   GLint x, GLint y, const GLubyte ssss[QUAD_SIZE])
+{
+   static const GLuint mask = 0x00ffffff;
+   const GLint invY = sps->surface.height - y - 1;
+   GLuint *dst = (GLuint *) (sps->surface.ptr
+               + (invY * sps->surface.stride + x) * sps->surface.cpp);
+
+   assert(sps->surface.format == PIPE_FORMAT_Z24_S8);
+
+   /* write high byte */
+   dst[0] = (dst[0] & mask) | (ssss[0] << 24);
+   dst[1] = (dst[1] & mask) | (ssss[1] << 24);
+   dst -= sps->surface.stride;
+   dst[0] = (dst[0] & mask) | (ssss[2] << 24);
+   dst[1] = (dst[1] & mask) | (ssss[3] << 24);
+}
+
+
 static void *
 map_surface_buffer(struct pipe_buffer *pb, GLuint access_mode)
 {
-   struct intel_surface *is = (struct intel_surface *) pb;
-   struct intel_renderbuffer *irb = is->rb;
-   GET_CURRENT_CONTEXT(ctx);
-   struct intel_context *intel = intel_context(ctx);
-
+   struct softpipe_surface *sps = (struct softpipe_surface *) pb;
+   struct intel_renderbuffer *irb = (struct intel_renderbuffer *) sps->surface.rb;
    assert(access_mode == PIPE_MAP_READ_WRITE);
 
-   intelFinish(&intel->ctx);
-
    /*LOCK_HARDWARE(intel);*/
 
    if (irb->region) {
+      GET_CURRENT_CONTEXT(ctx);
+      struct intel_context *intel = intel_context(ctx);
+#if 0
+      intelFinish(&intel->ctx);  /* XXX need this? */
+#endif
       intel_region_map(intel->intelScreen, irb->region);
    }
    pb->ptr = irb->region->map;
 
+   sps->surface.stride = irb->region->pitch;
+   sps->surface.cpp = irb->region->cpp;
+   sps->surface.ptr = irb->region->map;
+
    return pb->ptr;
 }
 
@@ -98,69 +187,67 @@ map_surface_buffer(struct pipe_buffer *pb, GLuint access_mode)
 static void
 unmap_surface_buffer(struct pipe_buffer *pb)
 {
-   struct intel_surface *is = (struct intel_surface *) pb;
-   struct intel_renderbuffer *irb = is->rb;
-   GET_CURRENT_CONTEXT(ctx);
-   struct intel_context *intel = intel_context(ctx);
+   struct softpipe_surface *sps = (struct softpipe_surface *) pb;
+   struct intel_renderbuffer *irb = (struct intel_renderbuffer *) sps->surface.rb;
 
    if (irb->region) {
+      GET_CURRENT_CONTEXT(ctx);
+      struct intel_context *intel = intel_context(ctx);
       intel_region_unmap(intel->intelScreen, irb->region);
    }
    pb->ptr = NULL;
 
+   sps->surface.stride = 0;
+   sps->surface.cpp = 0;
+   sps->surface.ptr = NULL;
+
    /*UNLOCK_HARDWARE(intel);*/
 }
 
 
 struct pipe_surface *
-xmesa_get_color_surface(GLcontext *ctx, GLuint i)
+intel_new_surface(GLuint intFormat)
 {
-   struct intel_context *intel = intel_context(ctx);
-   struct intel_framebuffer *intel_fb;
-   struct intel_renderbuffer *intel_rb;
-
-   intel_fb = (struct intel_framebuffer *) ctx->DrawBuffer;
-   intel_rb = intel_fb->color_rb[1];
-
-   if (!intel_rb->surface) {
-      /* create surface and attach to intel_rb */
-      struct intel_surface *is;
-      is = CALLOC_STRUCT(intel_surface);
-      if (is) {
-         is->surface.surface.width = intel_rb->Base.Width;
-         is->surface.surface.height = intel_rb->Base.Height;
-
-         is->surface.read_quad_f_swz = read_quad_f_swz;
-         is->surface.write_quad_f_swz = write_quad_f_swz;
-
-         is->surface.surface.buffer.map = map_surface_buffer;
-         is->surface.surface.buffer.unmap = unmap_surface_buffer;
-
-         is->rb = intel_rb;
-      }
-      intel_rb->surface = is;
+   struct softpipe_surface *sps = CALLOC_STRUCT(softpipe_surface);
+   if (!sps)
+      return NULL;
+
+   sps->surface.width = 0; /* set in intel_alloc_renderbuffer_storage() */
+   sps->surface.height = 0;
+
+   if (intFormat == GL_RGBA8) {
+      sps->surface.format = PIPE_FORMAT_U_A8_R8_G8_B8;
+      sps->read_quad_f_swz = read_quad_f_swz;
+      sps->write_quad_f_swz = write_quad_f_swz;
    }
-   else {
-      /* update surface size */
-      struct intel_surface *is = intel_rb->surface;
-      is->surface.surface.width = intel_rb->Base.Width;
-      is->surface.surface.height = intel_rb->Base.Height;
-      /* sanity check */
-      assert(is->surface.surface.buffer.map == map_surface_buffer);
+   else if (intFormat == GL_RGB5) {
+      sps->surface.format = PIPE_FORMAT_U_R5_G6_B5;
+
    }
+   else if (intFormat == GL_DEPTH_COMPONENT16) {
+      sps->surface.format = PIPE_FORMAT_U_Z16;
 
-   return &intel_rb->surface->surface.surface;
-}
+   }
+   else if (intFormat == GL_DEPTH24_STENCIL8_EXT) {
+      sps->surface.format = PIPE_FORMAT_Z24_S8;
+      sps->read_quad_z = read_quad_z24;
+      sps->write_quad_z = write_quad_z24;
+      sps->read_quad_stencil = read_quad_stencil;
+      sps->write_quad_stencil = write_quad_stencil;
+   }
+   else {
+      /* TBD / unknown */
 
+   }
 
-struct pipe_surface *
-xmesa_get_z_surface(GLcontext *ctx)
-{
-   /* XXX fix */
-   return NULL;
+   sps->surface.buffer.map = map_surface_buffer;
+   sps->surface.buffer.unmap = unmap_surface_buffer;
+
+   return &sps->surface;
 }
 
 
+
 struct pipe_surface *
 xmesa_get_stencil_surface(GLcontext *ctx)
 {
diff --git a/src/mesa/drivers/x11/xm_buffer.c b/src/mesa/drivers/x11/xm_buffer.c
index 51d183bb435..8fbd9a783b0 100644
--- a/src/mesa/drivers/x11/xm_buffer.c
+++ b/src/mesa/drivers/x11/xm_buffer.c
@@ -35,6 +35,7 @@
 #include "imports.h"
 #include "framebuffer.h"
 #include "renderbuffer.h"
+#include "pipe/p_state.h"
 
 
 #if defined(USE_XSHM) && !defined(XFree86Server)
@@ -268,6 +269,8 @@ xmesa_alloc_front_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
    rb->Height = height;
    rb->InternalFormat = internalFormat;
 
+   rb->surface->resize(rb->surface, width, height);
+
    return GL_TRUE;
 }
 
@@ -317,6 +320,8 @@ xmesa_alloc_back_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
       xrb->origin4 = NULL;
    }
 
+   rb->surface->resize(rb->surface, width, height);
+
    return GL_TRUE;
 }
 
@@ -352,6 +357,9 @@ xmesa_new_renderbuffer(GLcontext *ctx, GLuint name, const GLvisual *visual,
          xrb->Base.IndexBits = visual->indexBits;
       }
       /* only need to set Red/Green/EtcBits fields for user-created RBs */
+
+      xrb->Base.surface = xmesa_new_surface(xrb);
+
    }
    return xrb;
 }
diff --git a/src/mesa/drivers/x11/xm_surface.c b/src/mesa/drivers/x11/xm_surface.c
index 9d6db2b5ce6..17f5f28a9dd 100644
--- a/src/mesa/drivers/x11/xm_surface.c
+++ b/src/mesa/drivers/x11/xm_surface.c
@@ -24,7 +24,7 @@
 
 
 /**
- * \file xm_surface.h
+ * \file xm_surface.c
  * Code to allow the softpipe code to write to X windows/buffers.
  * This is a bit of a hack for now.  We've basically got two different
  * abstractions for color buffers: gl_renderbuffer and softpipe_surface.
@@ -48,31 +48,11 @@
 #include "pipe/softpipe/sp_surface.h"
 
 
-/**
- * An xm_surface is derived from a softpipe_surface
- */
-struct xmesa_surface
-{
-   struct softpipe_surface sps;
-   struct xmesa_renderbuffer *xrb;  /** ptr back to matching xmesa_renderbuffer */
-   struct gl_renderbuffer *rb; /* ptr to matching gl_renderbuffer */
-};
-
-
-/**
- * Cast wrapper
- */
-static INLINE struct xmesa_surface *
-xmesa_surface(struct softpipe_surface *sps)
-{
-   return (struct xmesa_surface *) sps;
-}
-
-
 static void *
 map_surface_buffer(struct pipe_buffer *pb, GLuint access_mode)
 {
    /* no-op */
+   return NULL;
 }
 
 
@@ -83,6 +63,13 @@ unmap_surface_buffer(struct pipe_buffer *pb)
 }
 
 
+static INLINE struct xmesa_renderbuffer *
+xmesa_rb(struct softpipe_surface *sps)
+{
+   return (struct xmesa_renderbuffer *) sps->surface.rb;
+}
+
+
 /**
  * quad reading/writing
  * These functions are just wrappers around the existing renderbuffer
@@ -90,11 +77,10 @@ unmap_surface_buffer(struct pipe_buffer *pb)
  */
 
 static void
-read_quad_f(struct softpipe_surface *gs, GLint x, GLint y,
+read_quad_f(struct softpipe_surface *sps, GLint x, GLint y,
             GLfloat (*rgba)[NUM_CHANNELS])
 {
-   struct xmesa_surface *xmsurf = xmesa_surface(gs);
-   struct xmesa_renderbuffer *xrb = xmsurf->xrb;
+   struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
    GLubyte temp[16];
    GLfloat *dst = (GLfloat *) rgba;
    GLuint i;
@@ -107,11 +93,10 @@ read_quad_f(struct softpipe_surface *gs, GLint x, GLint y,
 }
 
 static void
-read_quad_f_swz(struct softpipe_surface *gs, GLint x, GLint y,
+read_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
                 GLfloat (*rrrr)[QUAD_SIZE])
 {
-   struct xmesa_surface *xmsurf = xmesa_surface(gs);
-   struct xmesa_renderbuffer *xrb = xmsurf->xrb;
+   struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
    GLubyte temp[16];
    GLfloat *dst = (GLfloat *) rrrr;
    GLuint i, j;
@@ -126,11 +111,10 @@ read_quad_f_swz(struct softpipe_surface *gs, GLint x, GLint y,
 }
 
 static void
-write_quad_f(struct softpipe_surface *gs, GLint x, GLint y,
+write_quad_f(struct softpipe_surface *sps, GLint x, GLint y,
              GLfloat (*rgba)[NUM_CHANNELS])
 {
-   struct xmesa_surface *xmsurf = xmesa_surface(gs);
-   struct xmesa_renderbuffer *xrb = xmsurf->xrb;
+   struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
    GLubyte temp[16];
    const GLfloat *src = (const GLfloat *) rgba;
    GLuint i;
@@ -143,11 +127,10 @@ write_quad_f(struct softpipe_surface *gs, GLint x, GLint y,
 }
 
 static void
-write_quad_f_swz(struct softpipe_surface *gs, GLint x, GLint y,
+write_quad_f_swz(struct softpipe_surface *sps, GLint x, GLint y,
                  GLfloat (*rrrr)[QUAD_SIZE])
 {
-   struct xmesa_surface *xmsurf = xmesa_surface(gs);
-   struct xmesa_renderbuffer *xrb = xmsurf->xrb;
+   struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
    GLubyte temp[16];
    const GLfloat *src = (const GLfloat *) rrrr;
    GLuint i, j;
@@ -162,251 +145,70 @@ write_quad_f_swz(struct softpipe_surface *gs, GLint x, GLint y,
 }
 
 static void
-read_quad_ub(struct softpipe_surface *gs, GLint x, GLint y,
+read_quad_ub(struct softpipe_surface *sps, GLint x, GLint y,
              GLubyte (*rgba)[NUM_CHANNELS])
 {
-   struct xmesa_surface *xmsurf = xmesa_surface(gs);
-   struct xmesa_renderbuffer *xrb = xmsurf->xrb;
+   struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
    GET_CURRENT_CONTEXT(ctx);
    xrb->Base.GetRow(ctx, &xrb->Base, 2, x, y,     rgba);
    xrb->Base.GetRow(ctx, &xrb->Base, 2, x, y + 1, rgba + 2);
 }
 
 static void
-write_quad_ub(struct softpipe_surface *gs, GLint x, GLint y,
+write_quad_ub(struct softpipe_surface *sps, GLint x, GLint y,
               GLubyte (*rgba)[NUM_CHANNELS])
 {
-   struct xmesa_surface *xmsurf = xmesa_surface(gs);
-   struct xmesa_renderbuffer *xrb = xmsurf->xrb;
+   struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
    GET_CURRENT_CONTEXT(ctx);
    xrb->Base.GetRow(ctx, &xrb->Base, 2, x, y,     rgba);
    xrb->Base.GetRow(ctx, &xrb->Base, 2, x, y + 1, rgba + 2);
 }
 
 static void
-write_mono_row_ub(struct softpipe_surface *gs, GLuint count, GLint x, GLint y,
+write_mono_row_ub(struct softpipe_surface *sps, GLuint count, GLint x, GLint y,
                   GLubyte rgba[NUM_CHANNELS])
 {
-   struct xmesa_surface *xmsurf = xmesa_surface(gs);
-   struct xmesa_renderbuffer *xrb = xmsurf->xrb;
+   struct xmesa_renderbuffer *xrb = xmesa_rb(sps);
    GET_CURRENT_CONTEXT(ctx);
    xrb->Base.PutMonoRow(ctx, &xrb->Base, count, x, y, rgba, NULL);
 }
 
 
-static struct xmesa_surface *
-create_surface(XMesaContext xmctx, struct xmesa_renderbuffer *xrb)
-{
-   struct xmesa_surface *xmsurf;
-
-   xmsurf = CALLOC_STRUCT(xmesa_surface);
-   if (xmsurf) {
-      xmsurf->xrb = xrb;
-      xmsurf->sps.surface.width = xrb->Base.Width;
-      xmsurf->sps.surface.height = xrb->Base.Height;
-
-      xmsurf->sps.read_quad_f = read_quad_f;
-      xmsurf->sps.read_quad_f_swz = read_quad_f_swz;
-      xmsurf->sps.read_quad_ub = read_quad_ub;
-      xmsurf->sps.write_quad_f = write_quad_f;
-      xmsurf->sps.write_quad_f_swz = write_quad_f_swz;
-      xmsurf->sps.write_quad_ub = write_quad_ub;
-      xmsurf->sps.write_mono_row_ub = write_mono_row_ub;
-
-      xmsurf->sps.surface.buffer.map = map_surface_buffer;
-      xmsurf->sps.surface.buffer.unmap = unmap_surface_buffer;
-
-#if 0
-      if (xrb->ximage) {
-         xmsurf->sps.surface.ptr = (GLubyte *) xrb->ximage->data;
-         xmsurf->sps.surface.stride = xrb->ximage->bytes_per_line;
-         xmsurf->sps.surface.cpp = xrb->ximage->depth;
-
-      }
-#endif
-   }
-   return xmsurf;
-}
-
-
-static void
-free_surface(struct softpipe_surface *sps)
-{
-   /* XXX may need to do more in the future */
-   free(sps);
-}
-
-
-/**
- * Return generic surface pointer corresponding to the current color buffer.
- */
-struct pipe_surface *
-xmesa_get_color_surface(GLcontext *ctx, GLuint buf)
-{
-   XMesaContext xmctx = XMESA_CONTEXT(ctx);
-   struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][buf];   
-   struct xmesa_renderbuffer *xrb = xmesa_renderbuffer(rb);
-   struct softpipe_surface *sps = (struct softpipe_surface *) xrb->pSurface;
-
-   if (!sps) {
-      xrb->pSurface = create_surface(xmctx, xrb);
-   }
-   else if (sps->surface.width != rb->Width ||
-            sps->surface.height != rb->Height) {
-      free_surface(sps);
-      xrb->pSurface = create_surface(xmctx, xrb);
-   }
-
-   return (struct pipe_surface *) xrb->pSurface;
-}
-
-
-
-
-static void
-read_quad_z(struct softpipe_surface *sps,
-            GLint x, GLint y, GLuint zzzz[QUAD_SIZE])
-{
-   struct xmesa_surface *xmsurf = xmesa_surface(sps);
-   struct gl_renderbuffer *rb = xmsurf->rb;
-   GLushort temp[4];
-   GLuint i;
-   GET_CURRENT_CONTEXT(ctx);
-   rb->GetRow(ctx, rb, 2, x, y,     temp);
-   rb->GetRow(ctx, rb, 2, x, y + 1, temp + 2);
-   /* convert from GLushort to GLuint */
-   for (i = 0; i < 4; i++) {
-      zzzz[i] = temp[i];
-   }
-}
-
-static void
-write_quad_z(struct softpipe_surface *sps,
-             GLint x, GLint y, const GLuint zzzz[QUAD_SIZE])
-{
-   struct xmesa_surface *xmsurf = xmesa_surface(sps);
-   struct gl_renderbuffer *rb = xmsurf->rb;
-   GLushort temp[4];
-   GLuint i;
-   GET_CURRENT_CONTEXT(ctx);
-   /* convert from GLuint to GLushort */
-   for (i = 0; i < 4; i++) {
-      temp[i] = zzzz[i];
-   }
-   rb->PutRow(ctx, rb, 2, x, y,     temp,     NULL);
-   rb->PutRow(ctx, rb, 2, x, y + 1, temp + 2, NULL);
-}
-
-
-static struct xmesa_surface *
-create_z_surface(XMesaContext xmctx, struct gl_renderbuffer *rb)
-{
-   struct xmesa_surface *xmsurf;
-
-   xmsurf = CALLOC_STRUCT(xmesa_surface);
-   if (xmsurf) {
-      xmsurf->sps.surface.format = PIPE_FORMAT_U_Z16;
-      xmsurf->sps.surface.width = rb->Width;
-      xmsurf->sps.surface.height = rb->Height;
-      xmsurf->sps.read_quad_z = read_quad_z;
-      xmsurf->sps.write_quad_z = write_quad_z;
-      xmsurf->rb = rb;
-   }
-   return xmsurf;
-}
-
-
-
-
 static void
-read_quad_stencil(struct softpipe_surface *sps,
-                  GLint x, GLint y, GLubyte ssss[QUAD_SIZE])
+resize_surface(struct pipe_surface *ps, GLuint width, GLuint height)
 {
-   struct xmesa_surface *xmsurf = xmesa_surface(sps);
-   struct gl_renderbuffer *rb = xmsurf->rb;
-   GET_CURRENT_CONTEXT(ctx);
-   rb->GetRow(ctx, rb, 2, x, y,     ssss);
-   rb->GetRow(ctx, rb, 2, x, y + 1, ssss + 2);
-}
-
-static void
-write_quad_stencil(struct softpipe_surface *sps,
-                   GLint x, GLint y, const GLubyte ssss[QUAD_SIZE])
-{
-   struct xmesa_surface *xmsurf = xmesa_surface(sps);
-   struct gl_renderbuffer *rb = xmsurf->rb;
-   GET_CURRENT_CONTEXT(ctx);
-   rb->PutRow(ctx, rb, 2, x, y,     ssss,     NULL);
-   rb->PutRow(ctx, rb, 2, x, y + 1, ssss + 2, NULL);
-}
-
-static struct xmesa_surface *
-create_stencil_surface(XMesaContext xmctx, struct gl_renderbuffer *rb)
-{
-   struct xmesa_surface *xmsurf;
-
-   xmsurf = CALLOC_STRUCT(xmesa_surface);
-   if (xmsurf) {
-      xmsurf->sps.surface.format = PIPE_FORMAT_U_S8;
-      xmsurf->sps.surface.width = rb->Width;
-      xmsurf->sps.surface.height = rb->Height;
-      xmsurf->sps.read_quad_stencil = read_quad_stencil;
-      xmsurf->sps.write_quad_stencil = write_quad_stencil;
-      xmsurf->rb = rb;
-   }
-   return xmsurf;
+   ps->width = width;
+   ps->height = height;
 }
 
 
-
-
 /**
- * Return a pipe_surface that wraps the current Z/depth buffer.
- * XXX this is pretty much a total hack until gl_renderbuffers and
- * pipe_surfaces are merged...
+ * Called to create a pipe_surface for each X renderbuffer.
  */
 struct pipe_surface *
-xmesa_get_z_surface(GLcontext *ctx)
+xmesa_new_surface(struct xmesa_renderbuffer *xrb)
 {
-   XMesaContext xmctx = XMESA_CONTEXT(ctx);
-   struct gl_renderbuffer *rb = ctx->DrawBuffer->_DepthBuffer;
-   static struct xmesa_surface *xms = NULL;
+   struct softpipe_surface *sps;
 
-   if (!rb)
+   sps = CALLOC_STRUCT(softpipe_surface);
+   if (!sps)
       return NULL;
 
-   if (!xms) {
-      xms = create_z_surface(xmctx, rb);
-   }
-   else if (xms->sps.surface.width != rb->Width ||
-            xms->sps.surface.height != rb->Height) {
-      free_surface(&xms->sps);
-      xms = create_z_surface(xmctx, rb);
-   }
-
-   return (struct pipe_surface *) &xms->sps.surface;
-}
+   sps->surface.rb = xrb;
+   sps->surface.width = xrb->Base.Width;
+   sps->surface.height = xrb->Base.Height;
 
+   sps->read_quad_f = read_quad_f;
+   sps->read_quad_f_swz = read_quad_f_swz;
+   sps->read_quad_ub = read_quad_ub;
+   sps->write_quad_f = write_quad_f;
+   sps->write_quad_f_swz = write_quad_f_swz;
+   sps->write_quad_ub = write_quad_ub;
+   sps->write_mono_row_ub = write_mono_row_ub;
 
-struct pipe_surface *
-xmesa_get_stencil_surface(GLcontext *ctx)
-{
-   XMesaContext xmctx = XMESA_CONTEXT(ctx);
-   struct gl_renderbuffer *rb = ctx->DrawBuffer->_StencilBuffer;
-   static struct xmesa_surface *xms = NULL;
+   sps->surface.buffer.map = map_surface_buffer;
+   sps->surface.buffer.unmap = unmap_surface_buffer;
+   sps->surface.resize = resize_surface;
 
-   if (!rb)
-      return NULL;
-
-   if (!xms) {
-      xms = create_stencil_surface(xmctx, rb);
-   }
-   else if (xms->sps.surface.width != rb->Width ||
-            xms->sps.surface.height != rb->Height) {
-      free_surface(&xms->sps);
-      xms = create_stencil_surface(xmctx, rb);
-   }
-
-   return (struct pipe_surface *) &xms->sps.surface;
+   return &sps->surface;
 }
-
diff --git a/src/mesa/drivers/x11/xmesaP.h b/src/mesa/drivers/x11/xmesaP.h
index 8648b199399..daf6a3f9424 100644
--- a/src/mesa/drivers/x11/xmesaP.h
+++ b/src/mesa/drivers/x11/xmesaP.h
@@ -585,23 +585,11 @@ extern void xmesa_register_swrast_functions( GLcontext *ctx );
 #define ENABLE_EXT_timer_query 0 /* may not have 64-bit GLuint64EXT */
 #endif
 
-#if 0
-GLboolean xmesa_get_cbuf_details( GLcontext *ctx,
-				  void **ptr,
-				  GLuint *cpp,
-				  GLint *stride,
-				  GLuint *format );
-#endif
 
 struct pipe_surface;
-struct pipe_surface *
-xmesa_get_color_surface(GLcontext *ctx, GLuint buf);
-
-struct pipe_surface *
-xmesa_get_z_surface(GLcontext *ctx);
 
 struct pipe_surface *
-xmesa_get_stencil_surface(GLcontext *ctx);
+xmesa_new_surface(struct xmesa_renderbuffer *xrb);
 
 
 #endif
diff --git a/src/mesa/main/mtypes.h b/src/mesa/main/mtypes.h
index 52448ee04eb..d70df5d945c 100644
--- a/src/mesa/main/mtypes.h
+++ b/src/mesa/main/mtypes.h
@@ -127,6 +127,7 @@ struct gl_texture_format;
 struct gl_texture_image;
 struct gl_texture_object;
 struct st_context;
+struct pipe_surface;
 typedef struct __GLcontextRec GLcontext;
 typedef struct __GLcontextModesRec GLvisual;
 typedef struct gl_framebuffer GLframebuffer;
@@ -2268,6 +2269,8 @@ struct gl_renderbuffer
    GLubyte StencilBits;
    GLvoid *Data;        /**< This may not be used by some kinds of RBs */
 
+   struct pipe_surface *surface;
+
    /* Used to wrap one renderbuffer around another: */
    struct gl_renderbuffer *Wrapped;
 
diff --git a/src/mesa/main/renderbuffer.c b/src/mesa/main/renderbuffer.c
index 6f1d7c39605..a1412ef007c 100644
--- a/src/mesa/main/renderbuffer.c
+++ b/src/mesa/main/renderbuffer.c
@@ -49,6 +49,9 @@
 
 #include "rbadaptors.h"
 
+#include "pipe/softpipe/sp_z_surface.h"
+#include "pipe/p_state.h"
+
 
 /* 32-bit color index format.  Not a public format. */
 #define COLOR_INDEX32 0x424243
@@ -1091,6 +1094,7 @@ _mesa_soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
       rb->PutValues = put_values_ushort;
       rb->PutMonoValues = put_mono_values_ushort;
       rb->DepthBits = 8 * sizeof(GLushort);
+      rb->surface = (struct pipe_surface *) softpipe_new_z_surface(16);
       pixelSize = sizeof(GLushort);
       break;
    case GL_DEPTH_COMPONENT24:
@@ -1193,13 +1197,27 @@ _mesa_soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
 
    /* free old buffer storage */
    if (rb->Data) {
-      _mesa_free(rb->Data);
+      if (rb->surface) {
+         /* pipe surface */
+      }
+      else {
+         /* legacy renderbuffer */
+         _mesa_free(rb->Data);
+      }
       rb->Data = NULL;
    }
 
    if (width > 0 && height > 0) {
       /* allocate new buffer storage */
-      rb->Data = _mesa_malloc(width * height * pixelSize);
+      if (rb->surface) {
+         /* pipe surface */
+         rb->surface->resize(rb->surface, width, height);
+         rb->Data = rb->surface->buffer.ptr;
+      }
+      else {
+         /* legacy renderbuffer */
+         rb->Data = _mesa_malloc(width * height * pixelSize);
+      }
       if (rb->Data == NULL) {
          rb->Width = 0;
          rb->Height = 0;
diff --git a/src/mesa/pipe/p_state.h b/src/mesa/pipe/p_state.h
index 4ae8928018e..9973a7b8dd7 100644
--- a/src/mesa/pipe/p_state.h
+++ b/src/mesa/pipe/p_state.h
@@ -239,6 +239,7 @@ struct pipe_sampler_state
 
 /**
  * A mappable buffer (vertex data, pixel data, etc)
+ * XXX replace with "intel_region".
  */
 struct pipe_buffer
 {
@@ -247,7 +248,7 @@ struct pipe_buffer
                            const void *src);
    void *(*map)(struct pipe_buffer *pb, GLuint access_mode);
    void (*unmap)(struct pipe_buffer *pb);
-   void *ptr;        /**< address, only valid while mapped */
+   GLubyte *ptr;     /**< address, only valid while mapped */
    GLuint mode;      /**< PIPE_MAP_x, only valid while mapped */
 };
 
@@ -261,12 +262,13 @@ struct pipe_surface
    struct pipe_buffer buffer;  /**< surfaces can be mapped */
    GLuint format:5;            /**< PIPE_FORMAT_x */
    GLuint width, height;
-#if 0
-   GLubyte *ptr;
-   GLint stride;
-   GLuint cpp;
-   GLuint format;
-#endif
+
+   GLint stride, cpp;
+   GLubyte *ptr;    /**< only valid while mapped, may not equal buffer->ptr */
+
+   void *rb;  /**< Ptr back to renderbuffer (temporary?) */
+
+   void (*resize)(struct pipe_surface *ps, GLuint width, GLuint height);
 };
 
 
diff --git a/src/mesa/pipe/softpipe/sp_context.c b/src/mesa/pipe/softpipe/sp_context.c
index 6b44fabfa45..8655aa83fd4 100644
--- a/src/mesa/pipe/softpipe/sp_context.c
+++ b/src/mesa/pipe/softpipe/sp_context.c
@@ -49,6 +49,13 @@ static void map_surfaces(struct softpipe_context *sp)
       struct pipe_buffer *buf = &sps->surface.buffer;
       buf->map(buf, PIPE_MAP_READ_WRITE);
    }
+
+   if (sp->framebuffer.zbuf) {
+      struct softpipe_surface *sps = softpipe_surface(sp->framebuffer.zbuf);
+      struct pipe_buffer *buf = &sps->surface.buffer;
+      buf->map(buf, PIPE_MAP_READ_WRITE);
+   }
+
    /* XXX depth & stencil bufs */
 }
 
@@ -62,6 +69,12 @@ static void unmap_surfaces(struct softpipe_context *sp)
       struct pipe_buffer *buf = &sps->surface.buffer;
       buf->unmap(buf);
    }
+
+   if (sp->framebuffer.zbuf) {
+      struct softpipe_surface *sps = softpipe_surface(sp->framebuffer.zbuf);
+      struct pipe_buffer *buf = &sps->surface.buffer;
+      buf->unmap(buf);
+   }
    /* XXX depth & stencil bufs */
 }
 
diff --git a/src/mesa/pipe/softpipe/sp_z_surface.c b/src/mesa/pipe/softpipe/sp_z_surface.c
new file mode 100644
index 00000000000..662a4a15ee2
--- /dev/null
+++ b/src/mesa/pipe/softpipe/sp_z_surface.c
@@ -0,0 +1,118 @@
+/**************************************************************************
+ * 
+ * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * 
+ **************************************************************************/
+
+
+/**
+ * Software Z buffer/surface.
+ *
+ * \author Brian Paul
+ */
+
+
+#include "main/imports.h"
+#include "pipe/p_state.h"
+#include "pipe/p_defines.h"
+#include "sp_surface.h"
+#include "sp_z_surface.h"
+
+static void*
+z16_map(struct pipe_buffer *pb, GLuint access_mode)
+{
+   struct softpipe_surface *sps = (struct softpipe_surface *) pb;
+   sps->surface.ptr = pb->ptr;
+   return pb->ptr;
+}
+
+static void
+z16_unmap(struct pipe_buffer *pb)
+{
+   struct softpipe_surface *sps = (struct softpipe_surface *) pb;
+   sps->surface.ptr = NULL;
+}
+
+static void
+z16_resize(struct pipe_surface *ps, GLuint width, GLuint height)
+{
+   struct softpipe_surface *sps = (struct softpipe_surface *) ps;
+
+   if (sps->surface.buffer.ptr)
+      free(sps->surface.buffer.ptr);
+
+   ps->buffer.ptr = (GLubyte *) malloc(width * height * sizeof(GLushort));
+   ps->width = width;
+   ps->height = height;
+
+   sps->surface.stride = sps->surface.width;
+   sps->surface.cpp = 2;
+}
+
+static void
+z16_read_quad_z(struct softpipe_surface *sps,
+                GLint x, GLint y, GLuint zzzz[QUAD_SIZE])
+{
+   const GLushort *src
+      = (GLushort *) sps->surface.ptr + y * sps->surface.stride + x;
+
+   /* converting GLushort to GLuint: */
+   zzzz[0] = src[0];
+   zzzz[1] = src[1];
+   zzzz[2] = src[sps->surface.width];
+   zzzz[3] = src[sps->surface.width + 1];
+}
+
+static void
+z16_write_quad_z(struct softpipe_surface *sps,
+                 GLint x, GLint y, const GLuint zzzz[QUAD_SIZE])
+{
+   GLushort *dst = (GLushort *) sps->surface.ptr + y * sps->surface.stride + x;
+
+   /* converting GLuint to GLushort: */
+   dst[0] = zzzz[0];
+   dst[1] = zzzz[1];
+   dst[sps->surface.width] = zzzz[2];
+   dst[sps->surface.width + 1] = zzzz[3];
+}
+
+struct softpipe_surface *
+softpipe_new_z_surface(GLuint depth)
+{
+   struct softpipe_surface *sps = CALLOC_STRUCT(softpipe_surface);
+   if (!sps)
+      return NULL;
+
+   /* XXX ignoring depth param for now */
+
+   sps->surface.format = PIPE_FORMAT_U_Z16;
+
+   sps->surface.resize = z16_resize;
+   sps->surface.buffer.map = z16_map;
+   sps->surface.buffer.unmap = z16_unmap;
+   sps->read_quad_z = z16_read_quad_z;
+   sps->write_quad_z = z16_write_quad_z;
+
+   return sps;
+}
diff --git a/src/mesa/pipe/softpipe/sp_z_surface.h b/src/mesa/pipe/softpipe/sp_z_surface.h
new file mode 100644
index 00000000000..9c3c43ca57b
--- /dev/null
+++ b/src/mesa/pipe/softpipe/sp_z_surface.h
@@ -0,0 +1,37 @@
+/**************************************************************************
+ * 
+ * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ * 
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * 
+ **************************************************************************/
+
+
+#ifndef SP_Z_SURFACE_H
+#define SP_Z_SURFACE_H
+
+
+extern struct softpipe_surface *
+softpipe_new_z_surface(GLuint depth);
+
+
+#endif /* SP_Z_SURFACE_H */
diff --git a/src/mesa/sources b/src/mesa/sources
index a589ae4373b..32c2ff23500 100644
--- a/src/mesa/sources
+++ b/src/mesa/sources
@@ -176,6 +176,7 @@ SOFTPIPE_SOURCES = \
 	pipe/softpipe/sp_state_sampler.c \
 	pipe/softpipe/sp_state_setup.c \
 	pipe/softpipe/sp_state_surface.c \
+	pipe/softpipe/sp_z_surface.c \
 	pipe/softpipe/sp_prim_setup.c
 
 DRAW_SOURCES = \
diff --git a/src/mesa/state_tracker/st_atom_framebuffer.c b/src/mesa/state_tracker/st_atom_framebuffer.c
index 595f390b28d..f5e3ce8b675 100644
--- a/src/mesa/state_tracker/st_atom_framebuffer.c
+++ b/src/mesa/state_tracker/st_atom_framebuffer.c
@@ -54,22 +54,31 @@ static void
 update_framebuffer_state( struct st_context *st )
 {
    struct pipe_framebuffer_state framebuffer;
+   struct gl_renderbuffer *rb;
    GLuint i;
 
+   memset(&framebuffer, 0, sizeof(framebuffer));
+
    /* Examine Mesa's ctx->DrawBuffer->_ColorDrawBuffers state
     * to determine which surfaces to draw to
     */
    framebuffer.num_cbufs = st->ctx->DrawBuffer->_NumColorDrawBuffers[0];
    for (i = 0; i < framebuffer.num_cbufs; i++) {
-      framebuffer.cbufs[i] = xmesa_get_color_surface(st->ctx, i);
+      rb = st->ctx->DrawBuffer->_ColorDrawBuffers[0][i];
+      assert(rb->surface);
+      framebuffer.cbufs[i] = rb->surface;
    }
 
-   if (st->ctx->DrawBuffer->_DepthBuffer/*Attachment[BUFFER_DEPTH].Renderbuffer*/) {
-      framebuffer.zbuf = xmesa_get_z_surface(st->ctx);
+   rb = st->ctx->DrawBuffer->_DepthBuffer;
+   if (rb) {
+      assert(rb->surface);
+      framebuffer.zbuf = rb->Wrapped->surface;
    }
 
-   if (st->ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer) {
-      framebuffer.sbuf = xmesa_get_stencil_surface(st->ctx);
+   rb = st->ctx->DrawBuffer->_StencilBuffer;
+   if (rb) {
+      assert(rb->surface);
+      framebuffer.sbuf = rb->Wrapped->surface;
    }
 
    if (memcmp(&framebuffer, &st->state.framebuffer, sizeof(framebuffer)) != 0) {
-- 
cgit v1.2.3