Made debugging output controllable via environment variable
[mesa.git] / src / mesa / drivers / x11 / xm_dd.c
index c10afede9226b5cc908b19c6b80bc62a6d9f2240..4afc81484a092124ed5fc1d2845cfe84805dc408 100644 (file)
@@ -1,10 +1,8 @@
-/* $Id: xm_dd.c,v 1.33 2002/06/19 21:49:37 brianp Exp $ */
-
 /*
  * Mesa 3-D graphics library
- * Version:  4.0.3
+ * Version:  6.3
  *
- * Copyright (C) 1999-2002  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2004  Brian Paul   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"),
  * 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.
  */
-/* $XFree86: xc/extras/Mesa/src/X/xm_dd.c,v 1.2 2002/02/26 23:37:31 tsi Exp $ */
+
 
 #include "glxheader.h"
+#include "bufferobj.h"
+#include "buffers.h"
 #include "context.h"
+#include "colormac.h"
 #include "depth.h"
 #include "drawpix.h"
 #include "extensions.h"
 #include "macros.h"
-#include "mem.h"
+#include "image.h"
+#include "imports.h"
 #include "mtypes.h"
 #include "state.h"
+#include "texobj.h"
+#include "teximage.h"
 #include "texstore.h"
 #include "texformat.h"
 #include "xmesaP.h"
 #include "array_cache/acache.h"
-#include "swrast/s_context.h"
 #include "swrast/swrast.h"
+#include "swrast/s_auxbuffer.h"
+#include "swrast/s_context.h"
 #include "swrast/s_alphabuf.h"
 #include "swrast_setup/swrast_setup.h"
 #include "tnl/tnl.h"
 #include "tnl/t_context.h"
-#include "tnl/t_pipeline.h"
+
+#ifdef XFree86Server
+#include <GL/glxtokens.h>
+#endif
+
+
+
+/*
+ * Dithering kernels and lookup tables.
+ */
+
+const int xmesa_kernel8[DITH_DY * DITH_DX] = {
+    0 * MAXC,  8 * MAXC,  2 * MAXC, 10 * MAXC,
+   12 * MAXC,  4 * MAXC, 14 * MAXC,  6 * MAXC,
+    3 * MAXC, 11 * MAXC,  1 * MAXC,  9 * MAXC,
+   15 * MAXC,  7 * MAXC, 13 * MAXC,  5 * MAXC,
+};
+
+const short xmesa_HPCR_DRGB[3][2][16] = {
+   {
+      { 16, -4,  1,-11, 14, -6,  3, -9, 15, -5,  2,-10, 13, -7,  4, -8},
+      {-15,  5,  0, 12,-13,  7, -2, 10,-14,  6, -1, 11,-12,  8, -3,  9}
+   },
+   {
+      {-11, 15, -7,  3, -8, 14, -4,  2,-10, 16, -6,  4, -9, 13, -5,  1},
+      { 12,-14,  8, -2,  9,-13,  5, -1, 11,-15,  7, -3, 10,-12,  6,  0}
+   },
+   {
+      {  6,-18, 26,-14,  2,-22, 30,-10,  8,-16, 28,-12,  4,-20, 32, -8},
+      { -4, 20,-24, 16,  0, 24,-28, 12, -6, 18,-26, 14, -2, 22,-30, 10}
+   }
+};
+
+const int xmesa_kernel1[16] = {
+   0*47,  9*47,  4*47, 12*47,     /* 47 = (255*3)/16 */
+   6*47,  2*47, 14*47,  8*47,
+  10*47,  1*47,  5*47, 11*47,
+   7*47, 13*47,  3*47, 15*47
+};
 
 
 /*
@@ -63,8 +106,8 @@ get_buffer_size( GLframebuffer *buffer, GLuint *width, GLuint *height )
    unsigned int winwidth, winheight;
 #ifdef XFree86Server
    /* XFree86 GLX renderer */
-   winwidth = xmBuffer->frontbuffer->width;
-   winheight = xmBuffer->frontbuffer->height;
+   winwidth = MIN2(xmBuffer->frontbuffer->width, MAX_WIDTH);
+   winheight = MIN2(xmBuffer->frontbuffer->height, MAX_HEIGHT);
 #else
    Window root;
    int winx, winy;
@@ -77,20 +120,18 @@ get_buffer_size( GLframebuffer *buffer, GLuint *width, GLuint *height )
    _glthread_UNLOCK_MUTEX(_xmesa_lock);
 #endif
 
-   (void)kernel8;              /* Muffle compiler */
-
    *width = winwidth;
    *height = winheight;
 }
 
 
 static void
-finish( GLcontext *ctx )
+finish_or_flush( GLcontext *ctx )
 {
 #ifdef XFree86Server
       /* NOT_NEEDED */
 #else
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
    if (xmesa) {
       _glthread_LOCK_MUTEX(_xmesa_lock);
       XSync( xmesa->display, False );
@@ -100,89 +141,54 @@ finish( GLcontext *ctx )
 }
 
 
-static void
-flush( GLcontext *ctx )
-{
-#ifdef XFree86Server
-      /* NOT_NEEDED */
-#else
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
-   if (xmesa) {
-      _glthread_LOCK_MUTEX(_xmesa_lock);
-      XFlush( xmesa->display );
-      _glthread_UNLOCK_MUTEX(_xmesa_lock);
-   }
-#endif
-}
-
-
-static void
-set_draw_buffer( GLcontext *ctx, GLenum mode )
-{
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
-   if (mode == GL_FRONT_LEFT) {
-      /* write to front buffer */
-      xmesa->xm_buffer->buffer = xmesa->xm_buffer->frontbuffer;
-      xmesa_update_span_funcs(ctx);
-   }
-   else if (mode==GL_BACK_LEFT && xmesa->xm_buffer->db_state) {
-      /* write to back buffer */
-      if (xmesa->xm_buffer->backpixmap) {
-         xmesa->xm_buffer->buffer =
-            (XMesaDrawable)xmesa->xm_buffer->backpixmap;
-      }
-      else if (xmesa->xm_buffer->backimage) {
-         xmesa->xm_buffer->buffer = None;
-      }
-      else {
-         /* just in case there wasn't enough memory for back buffer */
-         xmesa->xm_buffer->buffer = xmesa->xm_buffer->frontbuffer;
-      }
-      xmesa_update_span_funcs(ctx);
-   }
-   else {
-      /* the swrast->_RasterMask MULTI_DRAW_BIT will be set and
-       * we'll fall back to swrast to draw points/lines/triangles.
-       */
-   }
-}
-
 
+/*
+ * This chooses the color buffer for reading and writing spans, points,
+ * lines, and triangles.
+ */
 void
-xmesa_set_read_buffer( GLcontext *ctx, GLframebuffer *buffer, GLenum mode )
+xmesa_set_buffer( GLcontext *ctx, GLframebuffer *buffer, GLuint bufferBit )
 {
-   XMesaBuffer target;
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
+   /* We can make this cast since the XMesaBuffer wraps GLframebuffer.
+    * GLframebuffer is the first member in a XMesaBuffer struct.
+    */
+   XMesaBuffer target = (XMesaBuffer) buffer;
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
 
-   if (buffer == ctx->DrawBuffer) {
-      target = xmesa->xm_buffer;
-      xmesa->use_read_buffer = GL_FALSE;
-   }
-   else {
-      ASSERT(buffer == ctx->ReadBuffer);
-      target = xmesa->xm_read_buffer;
-      xmesa->use_read_buffer = GL_TRUE;
-   }
+   /* This assignment tells the span/point/line/triangle functions
+    * which XMesaBuffer to use.
+    */
+   xmesa->xm_buffer = target;
 
-   if (mode == GL_FRONT_LEFT) {
+   /*
+    * Now determine front vs back color buffer.
+    */
+   if (bufferBit == DD_FRONT_LEFT_BIT) {
       target->buffer = target->frontbuffer;
       xmesa_update_span_funcs(ctx);
    }
-   else if (mode==GL_BACK_LEFT && xmesa->xm_read_buffer->db_state) {
+   else if (bufferBit == DD_BACK_LEFT_BIT) {
+      ASSERT(target->db_state);
       if (target->backpixmap) {
-         target->buffer = (XMesaDrawable)xmesa->xm_buffer->backpixmap;
+         /* back buffer is a pixmap */
+         target->buffer = (XMesaDrawable) target->backpixmap;
       }
       else if (target->backimage) {
+         /* back buffer is an XImage */
          target->buffer = None;
       }
       else {
-         /* just in case there wasn't enough memory for back buffer */
+         /* No back buffer!!!!  Must be out of memory, use front buffer */
          target->buffer = target->frontbuffer;
       }
       xmesa_update_span_funcs(ctx);
    }
+   else if (bufferBit & (DD_AUX0_BIT | DD_AUX1_BIT | DD_AUX2_BIT | DD_AUX3_BIT)) {
+      _swrast_use_aux_buffer(ctx, buffer, bufferBit);
+   } 
    else {
-      _mesa_problem(ctx, "invalid buffer in set_read_buffer() in xmesa2.c");
+      _mesa_problem(ctx, "invalid buffer 0x%x in set_buffer() in xm_dd.c");
+      return;
    }
 }
 
@@ -191,26 +197,29 @@ xmesa_set_read_buffer( GLcontext *ctx, GLframebuffer *buffer, GLenum mode )
 static void
 clear_index( GLcontext *ctx, GLuint index )
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
    xmesa->clearpixel = (unsigned long) index;
-   XMesaSetForeground( xmesa->display, xmesa->xm_buffer->cleargc,
+   XMesaSetForeground( xmesa->display, xmesa->xm_draw_buffer->cleargc,
                        (unsigned long) index );
 }
 
 
 static void
-clear_color( GLcontext *ctx, const GLchan color[4] )
+clear_color( GLcontext *ctx, const GLfloat color[4] )
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
-   xmesa->clearcolor[0] = color[0];
-   xmesa->clearcolor[1] = color[1];
-   xmesa->clearcolor[2] = color[2];
-   xmesa->clearcolor[3] = color[3];
-   xmesa->clearpixel = xmesa_color_to_pixel( xmesa, color[0], color[1],
-                                             color[2], color[3],
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
+   CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[0], color[0]);
+   CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[1], color[1]);
+   CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[2], color[2]);
+   CLAMPED_FLOAT_TO_UBYTE(xmesa->clearcolor[3], color[3]);
+   xmesa->clearpixel = xmesa_color_to_pixel( xmesa,
+                                             xmesa->clearcolor[0],
+                                             xmesa->clearcolor[1],
+                                             xmesa->clearcolor[2],
+                                             xmesa->clearcolor[3],
                                              xmesa->xm_visual->undithered_pf );
    _glthread_LOCK_MUTEX(_xmesa_lock);
-   XMesaSetForeground( xmesa->display, xmesa->xm_buffer->cleargc,
+   XMesaSetForeground( xmesa->display, xmesa->xm_draw_buffer->cleargc,
                        xmesa->clearpixel );
    _glthread_UNLOCK_MUTEX(_xmesa_lock);
 }
@@ -221,8 +230,8 @@ clear_color( GLcontext *ctx, const GLchan color[4] )
 static void
 index_mask( GLcontext *ctx, GLuint mask )
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
-   if (xmesa->xm_buffer->buffer != XIMAGE) {
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
+   if (xmesa->xm_draw_buffer->buffer != XIMAGE) {
       unsigned long m;
       if (mask==0xffffffff) {
         m = ((unsigned long)~0L);
@@ -230,7 +239,8 @@ index_mask( GLcontext *ctx, GLuint mask )
       else {
          m = (unsigned long) mask;
       }
-      XMesaSetPlaneMask( xmesa->display, xmesa->xm_buffer->cleargc, m );
+      XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->cleargc, m );
+      XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->gc, m );
    }
 }
 
@@ -240,11 +250,11 @@ static void
 color_mask(GLcontext *ctx,
            GLboolean rmask, GLboolean gmask, GLboolean bmask, GLboolean amask)
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
-   int xclass = GET_VISUAL_CLASS(xmesa->xm_visual);
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
+   const int xclass = xmesa->xm_visual->mesa_visual.visualType;
    (void) amask;
 
-   if (xclass == TrueColor || xclass == DirectColor) {
+   if (xclass == GLX_TRUE_COLOR || xclass == GLX_DIRECT_COLOR) {
       unsigned long m;
       if (rmask && gmask && bmask) {
          m = ((unsigned long)~0L);
@@ -255,7 +265,8 @@ color_mask(GLcontext *ctx,
          if (gmask)   m |= GET_GREENMASK(xmesa->xm_visual);
          if (bmask)   m |= GET_BLUEMASK(xmesa->xm_visual);
       }
-      XMesaSetPlaneMask( xmesa->display, xmesa->xm_buffer->cleargc, m );
+      XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->cleargc, m );
+      XMesaSetPlaneMask( xmesa->display, xmesa->xm_draw_buffer->gc, m );
    }
 }
 
@@ -270,18 +281,18 @@ static void
 clear_front_pixmap( GLcontext *ctx, GLboolean all,
                     GLint x, GLint y, GLint width, GLint height )
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
    if (all) {
-      XMesaFillRectangle( xmesa->display, xmesa->xm_buffer->frontbuffer,
-                          xmesa->xm_buffer->cleargc,
+      XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->frontbuffer,
+                          xmesa->xm_draw_buffer->cleargc,
                           0, 0,
-                          xmesa->xm_buffer->width+1,
-                          xmesa->xm_buffer->height+1 );
+                          xmesa->xm_draw_buffer->mesa_buffer.Width + 1,
+                          xmesa->xm_draw_buffer->mesa_buffer.Height + 1 );
    }
    else {
-      XMesaFillRectangle( xmesa->display, xmesa->xm_buffer->frontbuffer,
-                          xmesa->xm_buffer->cleargc,
-                          x, xmesa->xm_buffer->height - y - height,
+      XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->frontbuffer,
+                          xmesa->xm_draw_buffer->cleargc,
+                          x, xmesa->xm_draw_buffer->mesa_buffer.Height - y - height,
                           width, height );
    }
 }
@@ -291,18 +302,18 @@ static void
 clear_back_pixmap( GLcontext *ctx, GLboolean all,
                    GLint x, GLint y, GLint width, GLint height )
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
    if (all) {
-      XMesaFillRectangle( xmesa->display, xmesa->xm_buffer->backpixmap,
-                          xmesa->xm_buffer->cleargc,
+      XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->backpixmap,
+                          xmesa->xm_draw_buffer->cleargc,
                           0, 0,
-                          xmesa->xm_buffer->width+1,
-                          xmesa->xm_buffer->height+1 );
+                          xmesa->xm_draw_buffer->mesa_buffer.Width + 1,
+                          xmesa->xm_draw_buffer->mesa_buffer.Height + 1 );
    }
    else {
-      XMesaFillRectangle( xmesa->display, xmesa->xm_buffer->backpixmap,
-                          xmesa->xm_buffer->cleargc,
-                          x, xmesa->xm_buffer->height - y - height,
+      XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->backpixmap,
+                          xmesa->xm_draw_buffer->cleargc,
+                          x, xmesa->xm_draw_buffer->mesa_buffer.Height - y - height,
                           width, height );
    }
 }
@@ -312,16 +323,16 @@ static void
 clear_8bit_ximage( GLcontext *ctx, GLboolean all,
                    GLint x, GLint y, GLint width, GLint height )
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
    if (all) {
-      size_t n = xmesa->xm_buffer->backimage->bytes_per_line
-         * xmesa->xm_buffer->backimage->height;
-      MEMSET( xmesa->xm_buffer->backimage->data, xmesa->clearpixel, n );
+      size_t n = xmesa->xm_draw_buffer->backimage->bytes_per_line
+         * xmesa->xm_draw_buffer->backimage->height;
+      MEMSET( xmesa->xm_draw_buffer->backimage->data, xmesa->clearpixel, n );
    }
    else {
       GLint i;
       for (i=0;i<height;i++) {
-         GLubyte *ptr = PIXELADDR1( xmesa->xm_buffer, x, y+i );
+         GLubyte *ptr = PIXELADDR1( xmesa->xm_draw_buffer, x, y+i );
          MEMSET( ptr, xmesa->clearpixel, width );
       }
    }
@@ -332,11 +343,11 @@ static void
 clear_HPCR_ximage( GLcontext *ctx, GLboolean all,
                    GLint x, GLint y, GLint width, GLint height )
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
    if (all) {
-      GLint i, c16 = (xmesa->xm_buffer->backimage->bytes_per_line>>4)<<4;
-      GLubyte *ptr  = (GLubyte *)xmesa->xm_buffer->backimage->data;
-      for (i=0; i<xmesa->xm_buffer->backimage->height; i++) {
+      GLint i, c16 = (xmesa->xm_draw_buffer->backimage->bytes_per_line>>4)<<4;
+      GLubyte *ptr  = (GLubyte *)xmesa->xm_draw_buffer->backimage->data;
+      for (i=0; i<xmesa->xm_draw_buffer->backimage->height; i++) {
          GLint j;
          GLubyte *sptr = xmesa->xm_visual->hpcr_clear_ximage_pattern[0];
          if (i&1) {
@@ -361,7 +372,7 @@ clear_HPCR_ximage( GLcontext *ctx, GLboolean all,
             ptr[15] = sptr[15];
             ptr += 16;
          }
-         for (; j<xmesa->xm_buffer->backimage->bytes_per_line; j++) {
+         for (; j<xmesa->xm_draw_buffer->backimage->bytes_per_line; j++) {
             *ptr = sptr[j&15];
             ptr++;
          }
@@ -370,7 +381,7 @@ clear_HPCR_ximage( GLcontext *ctx, GLboolean all,
    else {
       GLint i;
       for (i=y; i<y+height; i++) {
-         GLubyte *ptr = PIXELADDR1( xmesa->xm_buffer, x, i );
+         GLubyte *ptr = PIXELADDR1( xmesa->xm_draw_buffer, x, i );
          int j;
          GLubyte *sptr = xmesa->xm_visual->hpcr_clear_ximage_pattern[0];
          if (i&1) {
@@ -389,38 +400,38 @@ static void
 clear_16bit_ximage( GLcontext *ctx, GLboolean all,
                     GLint x, GLint y, GLint width, GLint height )
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
    register GLuint pixel = (GLuint) xmesa->clearpixel;
    if (xmesa->swapbytes) {
       pixel = ((pixel >> 8) & 0x00ff) | ((pixel << 8) & 0xff00);
    }
    if (all) {
       register GLuint n;
-      register GLuint *ptr4 = (GLuint *) xmesa->xm_buffer->backimage->data;
+      register GLuint *ptr4 = (GLuint *) xmesa->xm_draw_buffer->backimage->data;
       if ((pixel & 0xff) == ((pixel >> 8) & 0xff)) {
          /* low and high bytes are equal so use memset() */
-         n = xmesa->xm_buffer->backimage->bytes_per_line
-            * xmesa->xm_buffer->height;
+         n = xmesa->xm_draw_buffer->backimage->bytes_per_line
+            * xmesa->xm_draw_buffer->mesa_buffer.Height;
          MEMSET( ptr4, pixel & 0xff, n );
       }
       else {
          pixel = pixel | (pixel<<16);
-         n = xmesa->xm_buffer->backimage->bytes_per_line
-            * xmesa->xm_buffer->height / 4;
+         n = xmesa->xm_draw_buffer->backimage->bytes_per_line
+            * xmesa->xm_draw_buffer->mesa_buffer.Height / 4;
          do {
             *ptr4++ = pixel;
                n--;
          } while (n!=0);
 
-         if ((xmesa->xm_buffer->backimage->bytes_per_line *
-              xmesa->xm_buffer->height) & 0x2)
+         if ((xmesa->xm_draw_buffer->backimage->bytes_per_line *
+              xmesa->xm_draw_buffer->mesa_buffer.Height) & 0x2)
             *(GLushort *)ptr4 = pixel & 0xffff;
       }
    }
    else {
       register int i, j;
       for (j=0;j<height;j++) {
-         register GLushort *ptr2 = PIXELADDR2( xmesa->xm_buffer, x, y+j );
+         register GLushort *ptr2 = PIXELADDR2( xmesa->xm_draw_buffer, x, y+j );
          for (i=0;i<width;i++) {
             *ptr2++ = pixel;
          }
@@ -434,10 +445,11 @@ static void
 clear_24bit_ximage( GLcontext *ctx, GLboolean all,
                     GLint x, GLint y, GLint width, GLint height )
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
    const GLubyte r = xmesa->clearcolor[0];
    const GLubyte g = xmesa->clearcolor[1];
    const GLubyte b = xmesa->clearcolor[2];
+#if 0  /* See below */
    register GLuint clearPixel;
    if (xmesa->swapbytes) {
       clearPixel = (b << 16) | (g << 8) | r;
@@ -445,25 +457,26 @@ clear_24bit_ximage( GLcontext *ctx, GLboolean all,
    else {
       clearPixel = (r << 16) | (g << 8) | b;
    }
+#endif
 
    if (all) {
       if (r==g && g==b) {
          /* same value for all three components (gray) */
-         const GLint w3 = xmesa->xm_buffer->width * 3;
-         const GLint h = xmesa->xm_buffer->height;
+         const GLint w3 = xmesa->xm_draw_buffer->mesa_buffer.Width * 3;
+         const GLint h = xmesa->xm_draw_buffer->mesa_buffer.Height;
          GLint i;
          for (i = 0; i < h; i++) {
-            bgr_t *ptr3 = PIXELADDR3(xmesa->xm_buffer, 0, i);
+            bgr_t *ptr3 = PIXELADDR3(xmesa->xm_draw_buffer, 0, i);
             MEMSET(ptr3, r, w3);
          }
       }
       else {
          /* the usual case */
-         const GLint w = xmesa->xm_buffer->width;
-         const GLint h = xmesa->xm_buffer->height;
+         const GLint w = xmesa->xm_draw_buffer->mesa_buffer.Width;
+         const GLint h = xmesa->xm_draw_buffer->mesa_buffer.Height;
          GLint i, j;
          for (i = 0; i < h; i++) {
-            bgr_t *ptr3 = PIXELADDR3(xmesa->xm_buffer, 0, i);
+            bgr_t *ptr3 = PIXELADDR3(xmesa->xm_draw_buffer, 0, i);
             for (j = 0; j < w; j++) {
                ptr3->r = r;
                ptr3->g = g;
@@ -547,7 +560,7 @@ clear_24bit_ximage( GLcontext *ctx, GLboolean all,
          /* same value for all three components (gray) */
          GLint j;
          for (j=0;j<height;j++) {
-            bgr_t *ptr3 = PIXELADDR3( xmesa->xm_buffer, x, y+j );
+            bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
             MEMSET(ptr3, r, 3 * width);
          }
       }
@@ -555,7 +568,7 @@ clear_24bit_ximage( GLcontext *ctx, GLboolean all,
          /* non-gray clear color */
          GLint i, j;
          for (j = 0; j < height; j++) {
-            bgr_t *ptr3 = PIXELADDR3( xmesa->xm_buffer, x, y+j );
+            bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
             for (i = 0; i < width; i++) {
                ptr3->r = r;
                ptr3->g = g;
@@ -570,7 +583,7 @@ clear_24bit_ximage( GLcontext *ctx, GLboolean all,
          pixel4[1] = (clearPixel << 16) | (clearPixel >> 8);
          pixel4[2] = (clearPixel << 8) | (clearPixel >>  16);
          for (j=0;j<height;j++) {
-            bgr_t *ptr3 = PIXELADDR3( xmesa->xm_buffer, x, y+j );
+            bgr_t *ptr3 = PIXELADDR3( xmesa->xm_draw_buffer, x, y+j );
             register GLuint *ptr4 = (GLuint *)ptr3;
             register GLuint *p, px;
             GLuint w = width;
@@ -644,7 +657,7 @@ static void
 clear_32bit_ximage( GLcontext *ctx, GLboolean all,
                     GLint x, GLint y, GLint width, GLint height )
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
    register GLuint pixel = (GLuint) xmesa->clearpixel;
    if (xmesa->swapbytes) {
       pixel = ((pixel >> 24) & 0x000000ff)
@@ -653,8 +666,9 @@ clear_32bit_ximage( GLcontext *ctx, GLboolean all,
             | ((pixel << 24) & 0xff000000);
    }
    if (all) {
-      register GLint n = xmesa->xm_buffer->width * xmesa->xm_buffer->height;
-      register GLuint *ptr4 = (GLuint *) xmesa->xm_buffer->backimage->data;
+      register GLint n = xmesa->xm_draw_buffer->mesa_buffer.Width
+         * xmesa->xm_draw_buffer->mesa_buffer.Height;
+      register GLuint *ptr4 = (GLuint *) xmesa->xm_draw_buffer->backimage->data;
       if (pixel==0) {
          MEMSET( ptr4, pixel, 4*n );
       }
@@ -668,7 +682,7 @@ clear_32bit_ximage( GLcontext *ctx, GLboolean all,
    else {
       register int i, j;
       for (j=0;j<height;j++) {
-         register GLuint *ptr4 = PIXELADDR4( xmesa->xm_buffer, x, y+j );
+         register GLuint *ptr4 = PIXELADDR4( xmesa->xm_draw_buffer, x, y+j );
          for (i=0;i<width;i++) {
             *ptr4++ = pixel;
          }
@@ -681,26 +695,18 @@ static void
 clear_nbit_ximage( GLcontext *ctx, GLboolean all,
                    GLint x, GLint y, GLint width, GLint height )
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
-   XMesaImage *img = xmesa->xm_buffer->backimage;
-   if (all) {
-      register int i, j;
-      width = xmesa->xm_buffer->width;
-      height = xmesa->xm_buffer->height;
-      for (j=0;j<height;j++) {
-         for (i=0;i<width;i++) {
-            XMesaPutPixel( img, i, j, xmesa->clearpixel );
-         }
-      }
-   }
-   else {
-      /* TODO: optimize this */
-      register int i, j;
-      y = FLIP(xmesa->xm_buffer, y);
-      for (j=0;j<height;j++) {
-         for (i=0;i<width;i++) {
-            XMesaPutPixel( img, x+i, y-j, xmesa->clearpixel );
-         }
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
+   XMesaImage *img = xmesa->xm_draw_buffer->backimage;
+   register int i, j;
+
+   /* We can ignore 'all' here - x, y, width, height are always right */
+   (void) all;
+
+   /* TODO: optimize this */
+   y = FLIP(xmesa->xm_draw_buffer, y);
+   for (j = 0; j < height; j++) {
+      for (i = 0; i < width; i++) {
+         XMesaPutPixel(img, x+i, y-j, xmesa->clearpixel);
       }
    }
 }
@@ -711,25 +717,25 @@ static void
 clear_buffers( GLcontext *ctx, GLbitfield mask,
                GLboolean all, GLint x, GLint y, GLint width, GLint height )
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
    const GLuint *colorMask = (GLuint *) &ctx->Color.ColorMask;
 
    if ((mask & (DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT)) &&
-       xmesa->xm_buffer->mesa_buffer.UseSoftwareAlphaBuffers &&
+       xmesa->xm_draw_buffer->mesa_buffer.UseSoftwareAlphaBuffers &&
        ctx->Color.ColorMask[ACOMP]) {
-      _mesa_clear_alpha_buffers(ctx);
+      _swrast_clear_alpha_buffers(ctx);
    }
 
    /* we can't handle color or index masking */
    if (*colorMask == 0xffffffff && ctx->Color.IndexMask == 0xffffffff) {
       if (mask & DD_FRONT_LEFT_BIT) {
-        ASSERT(xmesa->xm_buffer->front_clear_func);
-        (*xmesa->xm_buffer->front_clear_func)( ctx, all, x, y, width, height );
+        ASSERT(xmesa->xm_draw_buffer->front_clear_func);
+        (*xmesa->xm_draw_buffer->front_clear_func)( ctx, all, x, y, width, height );
         mask &= ~DD_FRONT_LEFT_BIT;
       }
       if (mask & DD_BACK_LEFT_BIT) {
-        ASSERT(xmesa->xm_buffer->back_clear_func);
-        (*xmesa->xm_buffer->back_clear_func)( ctx, all, x, y, width, height );
+        ASSERT(xmesa->xm_draw_buffer->back_clear_func);
+        (*xmesa->xm_draw_buffer->back_clear_func)( ctx, all, x, y, width, height );
         mask &= ~DD_BACK_LEFT_BIT;
       }
    }
@@ -743,6 +749,7 @@ clear_buffers( GLcontext *ctx, GLbitfield mask,
  * When we detect that the user has resized the window this function will
  * get called.  Here we'll reallocate the back buffer, depth buffer,
  * stencil buffer etc. to match the new window size.
+ * The buffer->Width and buffer->Height values will indicate the new size.
  */
 void
 xmesa_resize_buffers( GLframebuffer *buffer )
@@ -754,8 +761,6 @@ xmesa_resize_buffers( GLframebuffer *buffer )
     */
    XMesaBuffer xmBuffer = (XMesaBuffer) buffer;
 
-   xmBuffer->width = buffer->Width;
-   xmBuffer->height = buffer->Height;
    xmesa_alloc_back_buffer( xmBuffer );
 
    /* Needed by FLIP macro */
@@ -786,64 +791,267 @@ xmesa_resize_buffers( GLframebuffer *buffer )
    _swrast_alloc_buffers( buffer );
 }
 
-#if 0
-/*
+
+#ifndef XFree86Server
+/* XXX this was never tested in the Xserver environment */
+
+/**
  * This function implements glDrawPixels() with an XPutImage call when
  * drawing to the front buffer (X Window drawable).
  * The image format must be GL_BGRA to match the PF_8R8G8B pixel format.
- * XXX top/bottom edge clipping is broken!
  */
-static GLboolean
-drawpixels_8R8G8B( GLcontext *ctx,
-                   GLint x, GLint y, GLsizei width, GLsizei height,
-                   GLenum format, GLenum type,
-                   const struct gl_pixelstore_attrib *unpack,
-                   const GLvoid *pixels )
+static void
+xmesa_DrawPixels_8R8G8B( GLcontext *ctx,
+                         GLint x, GLint y, GLsizei width, GLsizei height,
+                         GLenum format, GLenum type,
+                         const struct gl_pixelstore_attrib *unpack,
+                         const GLvoid *pixels )
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
+   const SWcontext *swrast = SWRAST_CONTEXT( ctx );
    XMesaDisplay *dpy = xmesa->xm_visual->display;
-   XMesaDrawable buffer = xmesa->xm_buffer->buffer;
-   XMesaGC gc = xmesa->xm_buffer->gc;
-   assert(dpy);
-   assert(buffer);
-   assert(gc);
-
-   /* XXX also check for pixel scale/bias/lookup/zooming! */
-   if (format == GL_BGRA && type == GL_UNSIGNED_BYTE) {
+   const XMesaDrawable buffer = xmesa->xm_draw_buffer->buffer;
+   const XMesaGC gc = xmesa->xm_draw_buffer->gc;
+
+   ASSERT(dpy);
+   ASSERT(gc);
+   ASSERT(xmesa->xm_visual->dithered_pf == PF_8R8G8B);
+   ASSERT(xmesa->xm_visual->undithered_pf == PF_8R8G8B);
+
+   if (swrast->NewState)
+      _swrast_validate_derived( ctx );
+
+   if (buffer &&   /* buffer != 0 means it's a Window or Pixmap */
+       format == GL_BGRA &&
+       type == GL_UNSIGNED_BYTE &&
+       (swrast->_RasterMask & ~CLIP_BIT) == 0 && /* no blend, z-test, etc */
+       ctx->_ImageTransferState == 0 &&  /* no color tables, scale/bias, etc */
+       ctx->Pixel.ZoomX == 1.0 &&        /* no zooming */
+       ctx->Pixel.ZoomY == 1.0) {
       int dstX = x;
       int dstY = y;
       int w = width;
       int h = height;
       int srcX = unpack->SkipPixels;
       int srcY = unpack->SkipRows;
-      if (_mesa_clip_pixelrect(ctx, &dstX, &dstY, &w, &h, &srcX, &srcY)) {
+      int rowLength = unpack->RowLength ? unpack->RowLength : width;
+
+      if (unpack->BufferObj->Name) {
+         /* unpack from PBO */
+         GLubyte *buf;
+         if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
+                                        format, type, pixels)) {
+            _mesa_error(ctx, GL_INVALID_OPERATION,
+                        "glDrawPixels(invalid PBO access)");
+            return;
+         }
+         buf = (GLubyte *) ctx->Driver.MapBuffer(ctx,
+                                                 GL_PIXEL_UNPACK_BUFFER_EXT,
+                                                 GL_READ_ONLY_ARB,
+                                                 unpack->BufferObj);
+         if (!buf) {
+            /* buffer is already mapped - that's an error */
+            _mesa_error(ctx, GL_INVALID_OPERATION,
+                        "glDrawPixels(PBO is mapped)");
+            return;
+         }
+         pixels = ADD_POINTERS(buf, pixels);
+      }
+
+      if (_mesa_clip_drawpixels(ctx, &dstX, &dstY, &w, &h, &srcX, &srcY)) {
+         /* This is a little tricky since all coordinates up to now have
+          * been in the OpenGL bottom-to-top orientation.  X is top-to-bottom
+          * so we have to carefully compute the Y coordinates/addresses here.
+          */
          XMesaImage ximage;
          MEMSET(&ximage, 0, sizeof(XMesaImage));
          ximage.width = width;
          ximage.height = height;
          ximage.format = ZPixmap;
-         ximage.data = (char *) pixels + (height - 1) * width * 4;
+         ximage.data = (char *) pixels
+            + ((srcY + h - 1) * rowLength + srcX) * 4;
          ximage.byte_order = LSBFirst;
          ximage.bitmap_unit = 32;
          ximage.bitmap_bit_order = LSBFirst;
          ximage.bitmap_pad = 32;
          ximage.depth = 24;
-         ximage.bytes_per_line = -width * 4;
+         ximage.bytes_per_line = -rowLength * 4; /* negative to flip image */
          ximage.bits_per_pixel = 32;
-         ximage.red_mask   = 0xff0000;
-         ximage.green_mask = 0x00ff00;
-         ximage.blue_mask  = 0x0000ff;
-         dstY = FLIP(xmesa->xm_buffer,dstY) - height + 1;
-         XPutImage(dpy, buffer, gc, &ximage, srcX, srcY, dstX, dstY, w, h);
-         return GL_TRUE;
+         /* it seems we don't need to set the ximage.red/green/blue_mask fields */
+         /* flip Y axis for dest position */
+         dstY = FLIP(xmesa->xm_draw_buffer, dstY) - h + 1;
+         XPutImage(dpy, buffer, gc, &ximage, 0, 0, dstX, dstY, w, h);
+      }
+
+      if (unpack->BufferObj->Name) {
+         ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
+                                 unpack->BufferObj);
       }
    }
-   return GL_FALSE;
+   else {
+      /* software fallback */
+      _swrast_DrawPixels(ctx, x, y, width, height,
+                         format, type, unpack, pixels);
+   }
 }
-#endif
 
 
 
+/**
+ * This function implements glDrawPixels() with an XPutImage call when
+ * drawing to the front buffer (X Window drawable).  The image format
+ * must be GL_RGB and image type must be GL_UNSIGNED_SHORT_5_6_5 to
+ * match the PF_5R6G5B pixel format.
+ */
+static void
+xmesa_DrawPixels_5R6G5B( GLcontext *ctx,
+                         GLint x, GLint y, GLsizei width, GLsizei height,
+                         GLenum format, GLenum type,
+                         const struct gl_pixelstore_attrib *unpack,
+                         const GLvoid *pixels )
+{
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
+   const SWcontext *swrast = SWRAST_CONTEXT( ctx );
+   XMesaDisplay *dpy = xmesa->xm_visual->display;
+   const XMesaDrawable buffer = xmesa->xm_draw_buffer->buffer;
+   const XMesaGC gc = xmesa->xm_draw_buffer->gc;
+
+   ASSERT(dpy);
+   ASSERT(gc);
+   ASSERT(xmesa->xm_visual->undithered_pf == PF_5R6G5B);
+
+   if (swrast->NewState)
+      _swrast_validate_derived( ctx );
+
+   if (buffer &&   /* buffer != 0 means it's a Window or Pixmap */
+       format == GL_RGB &&
+       type == GL_UNSIGNED_SHORT_5_6_5 &&
+       !ctx->Color.DitherFlag &&  /* no dithering */
+       (swrast->_RasterMask & ~CLIP_BIT) == 0 && /* no blend, z-test, etc */
+       ctx->_ImageTransferState == 0 &&  /* no color tables, scale/bias, etc */
+       ctx->Pixel.ZoomX == 1.0 &&        /* no zooming */
+       ctx->Pixel.ZoomY == 1.0) {
+      int dstX = x;
+      int dstY = y;
+      int w = width;
+      int h = height;
+      int srcX = unpack->SkipPixels;
+      int srcY = unpack->SkipRows;
+      int rowLength = unpack->RowLength ? unpack->RowLength : width;
+
+      if (unpack->BufferObj->Name) {
+         /* unpack from PBO */
+         GLubyte *buf;
+         if (!_mesa_validate_pbo_access(2, unpack, width, height, 1,
+                                        format, type, pixels)) {
+            _mesa_error(ctx, GL_INVALID_OPERATION,
+                        "glDrawPixels(invalid PBO access)");
+            return;
+         }
+         buf = (GLubyte *) ctx->Driver.MapBuffer(ctx,
+                                                 GL_PIXEL_UNPACK_BUFFER_EXT,
+                                                 GL_READ_ONLY_ARB,
+                                                 unpack->BufferObj);
+         if (!buf) {
+            /* buffer is already mapped - that's an error */
+            _mesa_error(ctx, GL_INVALID_OPERATION,
+                        "glDrawPixels(PBO is mapped)");
+            return;
+         }
+         pixels = ADD_POINTERS(buf, pixels);
+      }
+
+      if (_mesa_clip_drawpixels(ctx, &dstX, &dstY, &w, &h, &srcX, &srcY)) {
+         /* This is a little tricky since all coordinates up to now have
+          * been in the OpenGL bottom-to-top orientation.  X is top-to-bottom
+          * so we have to carefully compute the Y coordinates/addresses here.
+          */
+         XMesaImage ximage;
+         MEMSET(&ximage, 0, sizeof(XMesaImage));
+         ximage.width = width;
+         ximage.height = height;
+         ximage.format = ZPixmap;
+         ximage.data = (char *) pixels
+            + ((srcY + h - 1) * rowLength + srcX) * 2;
+         ximage.byte_order = LSBFirst;
+         ximage.bitmap_unit = 16;
+         ximage.bitmap_bit_order = LSBFirst;
+         ximage.bitmap_pad = 16;
+         ximage.depth = 16;
+         ximage.bytes_per_line = -rowLength * 2; /* negative to flip image */
+         ximage.bits_per_pixel = 16;
+         /* it seems we don't need to set the ximage.red/green/blue_mask fields */
+         /* flip Y axis for dest position */
+         dstY = FLIP(xmesa->xm_draw_buffer, dstY) - h + 1;
+         XPutImage(dpy, buffer, gc, &ximage, 0, 0, dstX, dstY, w, h);
+      }
+
+      if (unpack->BufferObj->Name) {
+         ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
+                                 unpack->BufferObj);
+      }
+   }
+   else {
+      /* software fallback */
+      _swrast_DrawPixels(ctx, x, y, width, height,
+                         format, type, unpack, pixels);
+   }
+}
+
+
+
+/**
+ * Implement glCopyPixels for the front color buffer (or back buffer Pixmap)
+ * for the color buffer.  Don't support zooming, pixel transfer, etc.
+ * We do support copying from one window to another, ala glXMakeCurrentRead.
+ */
+static void
+xmesa_CopyPixels( GLcontext *ctx,
+                  GLint srcx, GLint srcy, GLsizei width, GLsizei height,
+                  GLint destx, GLint desty, GLenum type )
+{
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
+   const SWcontext *swrast = SWRAST_CONTEXT( ctx );
+   XMesaDisplay *dpy = xmesa->xm_visual->display;
+   const XMesaDrawable drawBuffer = xmesa->xm_draw_buffer->buffer;
+   const XMesaDrawable readBuffer = xmesa->xm_read_buffer->buffer;
+   const XMesaGC gc = xmesa->xm_draw_buffer->gc;
+
+   ASSERT(dpy);
+   ASSERT(gc);
+
+   if (swrast->NewState)
+      _swrast_validate_derived( ctx );
+
+   if (ctx->Color.DrawBuffer[0] == GL_FRONT &&
+       ctx->Pixel.ReadBuffer == GL_FRONT &&
+       drawBuffer &&  /* buffer != 0 means it's a Window or Pixmap */
+       readBuffer &&
+       type == GL_COLOR &&
+       (swrast->_RasterMask & ~CLIP_BIT) == 0 && /* no blend, z-test, etc */
+       ctx->_ImageTransferState == 0 &&  /* no color tables, scale/bias, etc */
+       ctx->Pixel.ZoomX == 1.0 &&        /* no zooming */
+       ctx->Pixel.ZoomY == 1.0) {
+      /* Note: we don't do any special clipping work here.  We could,
+       * but X will do it for us.
+       */
+      srcy = FLIP(xmesa->xm_read_buffer, srcy) - height + 1;
+      desty = FLIP(xmesa->xm_draw_buffer, desty) - height + 1;
+      XCopyArea(dpy, readBuffer, drawBuffer, gc,
+                srcx, srcy, width, height, destx, desty);
+   }
+   else {
+      _swrast_CopyPixels(ctx, srcx, srcy, width, height, destx, desty, type );
+   }
+}
+#endif /* XFree86Server */
+
+
+
+/*
+ * Every driver should implement a GetString function in order to
+ * return a meaningful GL_RENDERER string.
+ */
 static const GLubyte *
 get_string( GLcontext *ctx, GLenum name )
 {
@@ -867,10 +1075,14 @@ get_string( GLcontext *ctx, GLenum name )
 }
 
 
+/*
+ * We implement the glEnable function only because we care about
+ * dither enable/disable.
+ */
 static void
 enable( GLcontext *ctx, GLenum pname, GLboolean state )
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
 
    switch (pname) {
       case GL_DITHER:
@@ -887,7 +1099,7 @@ enable( GLcontext *ctx, GLenum pname, GLboolean state )
 
 void xmesa_update_state( GLcontext *ctx, GLuint new_state )
 {
-   const XMesaContext xmesa = (XMesaContext) ctx->DriverCtx;
+   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
 
    /* Propogate statechange information to swrast and swrast_setup
     * modules.  The X11 driver has no internal GL-dependent state.
@@ -899,116 +1111,153 @@ void xmesa_update_state( GLcontext *ctx, GLuint new_state )
 
 
    /* setup pointers to front and back buffer clear functions */
-   xmesa->xm_buffer->front_clear_func = clear_front_pixmap;
-   if (xmesa->xm_buffer->backpixmap != XIMAGE) {
-      xmesa->xm_buffer->back_clear_func = clear_back_pixmap;
+   xmesa->xm_draw_buffer->front_clear_func = clear_front_pixmap;
+   if (xmesa->xm_draw_buffer->backpixmap != XIMAGE) {
+      xmesa->xm_draw_buffer->back_clear_func = clear_back_pixmap;
    }
    else if (sizeof(GLushort)!=2 || sizeof(GLuint)!=4) {
-      xmesa->xm_buffer->back_clear_func = clear_nbit_ximage;
+      xmesa->xm_draw_buffer->back_clear_func = clear_nbit_ximage;
    }
    else switch (xmesa->xm_visual->BitsPerPixel) {
    case 8:
       if (xmesa->xm_visual->hpcr_clear_flag) {
-        xmesa->xm_buffer->back_clear_func = clear_HPCR_ximage;
+        xmesa->xm_draw_buffer->back_clear_func = clear_HPCR_ximage;
       }
       else {
-        xmesa->xm_buffer->back_clear_func = clear_8bit_ximage;
+        xmesa->xm_draw_buffer->back_clear_func = clear_8bit_ximage;
       }
       break;
    case 16:
-      xmesa->xm_buffer->back_clear_func = clear_16bit_ximage;
+      xmesa->xm_draw_buffer->back_clear_func = clear_16bit_ximage;
       break;
    case 24:
-      xmesa->xm_buffer->back_clear_func = clear_24bit_ximage;
+      xmesa->xm_draw_buffer->back_clear_func = clear_24bit_ximage;
       break;
    case 32:
-      xmesa->xm_buffer->back_clear_func = clear_32bit_ximage;
+      xmesa->xm_draw_buffer->back_clear_func = clear_32bit_ximage;
       break;
    default:
-      xmesa->xm_buffer->back_clear_func = clear_nbit_ximage;
+      xmesa->xm_draw_buffer->back_clear_func = clear_nbit_ximage;
       break;
    }
 
-   xmesa_update_span_funcs(ctx);
+   if (ctx->Color._DrawDestMask[0] & (DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT)) {
+      xmesa_update_span_funcs(ctx);
+   }
 }
 
 
 
-/* Setup pointers and other driver state that is constant for the life
- * of a context.
+/**
+ * Called via ctx->Driver.TestProxyTeximage().  Normally, we'd just use
+ * the _mesa_test_proxy_teximage() fallback function, but we're going to
+ * special-case the 3D texture case to allow textures up to 512x512x32
+ * texels.
  */
-void xmesa_init_pointers( GLcontext *ctx )
+static GLboolean
+test_proxy_teximage(GLcontext *ctx, GLenum target, GLint level,
+                    GLint internalFormat, GLenum format, GLenum type,
+                    GLint width, GLint height, GLint depth, GLint border)
 {
-   TNLcontext *tnl;
-
-   ctx->Driver.GetString = get_string;
-   ctx->Driver.GetBufferSize = get_buffer_size;
-   ctx->Driver.Flush = flush;
-   ctx->Driver.Finish = finish;
-    
-   /* Software rasterizer pixel paths:
-    */
-   ctx->Driver.Accum = _swrast_Accum;
-   ctx->Driver.Bitmap = _swrast_Bitmap;
-   ctx->Driver.Clear = clear_buffers;
-   ctx->Driver.ResizeBuffers = xmesa_resize_buffers;
-   ctx->Driver.CopyPixels = _swrast_CopyPixels;
-   ctx->Driver.DrawPixels = _swrast_DrawPixels;
-   ctx->Driver.ReadPixels = _swrast_ReadPixels;
-
-   /* Software texture functions:
-    */
-   ctx->Driver.ChooseTextureFormat = _mesa_choose_tex_format;
-   ctx->Driver.TexImage1D = _mesa_store_teximage1d;
-   ctx->Driver.TexImage2D = _mesa_store_teximage2d;
-   ctx->Driver.TexImage3D = _mesa_store_teximage3d;
-   ctx->Driver.TexSubImage1D = _mesa_store_texsubimage1d;
-   ctx->Driver.TexSubImage2D = _mesa_store_texsubimage2d;
-   ctx->Driver.TexSubImage3D = _mesa_store_texsubimage3d;
-   ctx->Driver.TestProxyTexImage = _mesa_test_proxy_teximage;
-
-   ctx->Driver.CopyTexImage1D = _swrast_copy_teximage1d;
-   ctx->Driver.CopyTexImage2D = _swrast_copy_teximage2d;
-   ctx->Driver.CopyTexSubImage1D = _swrast_copy_texsubimage1d;
-   ctx->Driver.CopyTexSubImage2D = _swrast_copy_texsubimage2d;
-   ctx->Driver.CopyTexSubImage3D = _swrast_copy_texsubimage3d;
-
-   ctx->Driver.BaseCompressedTexFormat = _mesa_base_compressed_texformat;
-   ctx->Driver.CompressedTextureSize = _mesa_compressed_texture_size;
-   ctx->Driver.GetCompressedTexImage = _mesa_get_compressed_teximage;
-
-   /* Swrast hooks for imaging extensions:
-    */
-   ctx->Driver.CopyColorTable = _swrast_CopyColorTable;
-   ctx->Driver.CopyColorSubTable = _swrast_CopyColorSubTable;
-   ctx->Driver.CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
-   ctx->Driver.CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
-
+   if (target == GL_PROXY_TEXTURE_3D) {
+      /* special case for 3D textures */
+      if (width * height * depth > 512 * 512 * 64 ||
+          width  < 2 * border ||
+          (!ctx->Extensions.ARB_texture_non_power_of_two &&
+           _mesa_bitcount(width  - 2 * border) != 1) ||
+          height < 2 * border ||
+          (!ctx->Extensions.ARB_texture_non_power_of_two &&
+           _mesa_bitcount(height - 2 * border) != 1) ||
+          depth  < 2 * border ||
+          (!ctx->Extensions.ARB_texture_non_power_of_two &&
+           _mesa_bitcount(depth  - 2 * border) != 1)) {
+         /* Bad size, or too many texels */
+         return GL_FALSE;
+      }
+      return GL_TRUE;
+   }
+   else {
+      /* use the fallback routine for 1D, 2D, cube and rect targets */
+      return _mesa_test_proxy_teximage(ctx, target, level, internalFormat,
+                                       format, type, width, height, depth,
+                                       border);
+   }
+}
 
-   /* Statechange callbacks:
-    */
-   ctx->Driver.SetDrawBuffer = set_draw_buffer;
-   ctx->Driver.ClearIndex = clear_index;
-   ctx->Driver.ClearColor = clear_color;
-   ctx->Driver.IndexMask = index_mask;
-   ctx->Driver.ColorMask = color_mask;
-   ctx->Driver.Enable = enable;
 
+/**
+ * In SW, we don't really compress GL_COMPRESSED_RGB[A] textures!
+ */
+static const struct gl_texture_format *
+choose_tex_format( GLcontext *ctx, GLint internalFormat,
+                   GLenum format, GLenum type )
+{
+   switch (internalFormat) {
+      case GL_COMPRESSED_RGB_ARB:
+         return &_mesa_texformat_rgb;
+      case GL_COMPRESSED_RGBA_ARB:
+         return &_mesa_texformat_rgba;
+      default:
+         return _mesa_choose_tex_format(ctx, internalFormat, format, type);
+   }
+}
 
-   /* Initialize the TNL driver interface:
-    */
-   tnl = TNL_CONTEXT(ctx);
-   tnl->Driver.RunPipeline = _tnl_run_pipeline;
-   
-   /* Install swsetup for tnl->Driver.Render.*:
-    */
-   _swsetup_Wakeup(ctx);
 
-   (void) DitherValues;  /* silenced unused var warning */
+/**
+ * Called by glViewport.
+ * This is a good time for us to poll the current X window size and adjust
+ * our ancillary (back color, depth, stencil, etc) buffers to match the
+ * current window size.  Remember, we have no opportunity to respond to
+ * conventional X Resize/StructureNotify events since the X driver has no
+ * event loop.  Thus, we poll.
+ * Note that this trick isn't fool-proof.  If the application never calls
+ * glViewport, our notion of the current window size may be incorrect.
+ */
+static void
+xmesa_viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
+{
+   _mesa_ResizeBuffersMESA();
 }
 
 
-
+/**
+ * Initialize the device driver function table with the functions
+ * we implement in this driver.
+ */
+void
+xmesa_init_driver_functions( XMesaVisual xmvisual,
+                             struct dd_function_table *driver )
+{
+   driver->GetString = get_string;
+   driver->UpdateState = xmesa_update_state;
+   driver->GetBufferSize = get_buffer_size;
+   driver->Flush = finish_or_flush;
+   driver->Finish = finish_or_flush;
+   driver->ClearIndex = clear_index;
+   driver->ClearColor = clear_color;
+   driver->IndexMask = index_mask;
+   driver->ColorMask = color_mask;
+   driver->Enable = enable;
+   driver->Clear = clear_buffers;
+   driver->ResizeBuffers = xmesa_resize_buffers;
+   driver->Viewport = xmesa_viewport;
+#ifndef XFree86Server
+   driver->CopyPixels = xmesa_CopyPixels;
+   if (xmvisual->undithered_pf == PF_8R8G8B &&
+       xmvisual->dithered_pf == PF_8R8G8B) {
+      driver->DrawPixels = xmesa_DrawPixels_8R8G8B;
+   }
+   else if (xmvisual->undithered_pf == PF_5R6G5B) {
+      driver->DrawPixels = xmesa_DrawPixels_5R6G5B;
+   }
+#endif
+   driver->TestProxyTexImage = test_proxy_teximage;
+#if SWTC
+   driver->ChooseTextureFormat = choose_tex_format;
+#else
+   (void) choose_tex_format;
+#endif
+}
 
 
 #define XMESA_NEW_POINT  (_NEW_POINT | \
@@ -1036,6 +1285,9 @@ void xmesa_init_pointers( GLcontext *ctx )
 void xmesa_register_swrast_functions( GLcontext *ctx )
 {
    SWcontext *swrast = SWRAST_CONTEXT( ctx );
+   struct swrast_device_driver *dd = _swrast_GetDeviceDriverReference(ctx);
+
+   dd->SetBuffer = xmesa_set_buffer;
 
    swrast->choose_point = xmesa_choose_point;
    swrast->choose_line = xmesa_choose_line;