Made debugging output controllable via environment variable
[mesa.git] / src / mesa / drivers / x11 / xm_dd.c
index d1d00983029b68576a38cb519b9b1566a420fead..4afc81484a092124ed5fc1d2845cfe84805dc408 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Mesa 3-D graphics library
- * Version:  5.1
+ * Version:  6.3
  *
- * Copyright (C) 1999-2003  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"),
 
 #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 "image.h"
 #include "imports.h"
 #include "mtypes.h"
 #include "state.h"
 #include "xmesaP.h"
 #include "array_cache/acache.h"
 #include "swrast/swrast.h"
+#include "swrast/s_auxbuffer.h"
 #include "swrast/s_context.h"
-#include "swrast/s_drawpix.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
+};
 
 
 /*
@@ -66,14 +106,8 @@ get_buffer_size( GLframebuffer *buffer, GLuint *width, GLuint *height )
    unsigned int winwidth, winheight;
 #ifdef XFree86Server
    /* XFree86 GLX renderer */
-   if (xmBuffer->frontbuffer->width > MAX_WIDTH ||
-       xmBuffer->frontbuffer->height > MAX_HEIGHT) {
-     winwidth = buffer->Width;
-     winheight = buffer->Height;
-   } else {
-     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;
@@ -86,8 +120,6 @@ get_buffer_size( GLframebuffer *buffer, GLuint *width, GLuint *height )
    _glthread_UNLOCK_MUTEX(_xmesa_lock);
 #endif
 
-   (void)kernel8;              /* Muffle compiler */
-
    *width = winwidth;
    *height = winheight;
 }
@@ -114,8 +146,8 @@ finish_or_flush( GLcontext *ctx )
  * This chooses the color buffer for reading and writing spans, points,
  * lines, and triangles.
  */
-static void
-set_buffer( GLcontext *ctx, GLframebuffer *buffer, GLuint bufferBit )
+void
+xmesa_set_buffer( GLcontext *ctx, GLframebuffer *buffer, GLuint bufferBit )
 {
    /* We can make this cast since the XMesaBuffer wraps GLframebuffer.
     * GLframebuffer is the first member in a XMesaBuffer struct.
@@ -131,10 +163,11 @@ set_buffer( GLcontext *ctx, GLframebuffer *buffer, GLuint bufferBit )
    /*
     * Now determine front vs back color buffer.
     */
-   if (bufferBit == FRONT_LEFT_BIT) {
+   if (bufferBit == DD_FRONT_LEFT_BIT) {
       target->buffer = target->frontbuffer;
+      xmesa_update_span_funcs(ctx);
    }
-   else if (bufferBit == BACK_LEFT_BIT) {
+   else if (bufferBit == DD_BACK_LEFT_BIT) {
       ASSERT(target->db_state);
       if (target->backpixmap) {
          /* back buffer is a pixmap */
@@ -148,12 +181,15 @@ set_buffer( GLcontext *ctx, GLframebuffer *buffer, GLuint bufferBit )
          /* 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 0x%x in set_buffer() in xm_dd.c");
       return;
    }
-   xmesa_update_span_funcs(ctx);
 }
 
 
@@ -215,10 +251,10 @@ color_mask(GLcontext *ctx,
            GLboolean rmask, GLboolean gmask, GLboolean bmask, GLboolean amask)
 {
    const XMesaContext xmesa = XMESA_CONTEXT(ctx);
-   int xclass = GET_VISUAL_CLASS(xmesa->xm_visual);
+   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);
@@ -250,13 +286,13 @@ clear_front_pixmap( GLcontext *ctx, GLboolean all,
       XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->frontbuffer,
                           xmesa->xm_draw_buffer->cleargc,
                           0, 0,
-                          xmesa->xm_draw_buffer->width+1,
-                          xmesa->xm_draw_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_draw_buffer->frontbuffer,
                           xmesa->xm_draw_buffer->cleargc,
-                          x, xmesa->xm_draw_buffer->height - y - height,
+                          x, xmesa->xm_draw_buffer->mesa_buffer.Height - y - height,
                           width, height );
    }
 }
@@ -271,13 +307,13 @@ clear_back_pixmap( GLcontext *ctx, GLboolean all,
       XMesaFillRectangle( xmesa->display, xmesa->xm_draw_buffer->backpixmap,
                           xmesa->xm_draw_buffer->cleargc,
                           0, 0,
-                          xmesa->xm_draw_buffer->width+1,
-                          xmesa->xm_draw_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_draw_buffer->backpixmap,
                           xmesa->xm_draw_buffer->cleargc,
-                          x, xmesa->xm_draw_buffer->height - y - height,
+                          x, xmesa->xm_draw_buffer->mesa_buffer.Height - y - height,
                           width, height );
    }
 }
@@ -375,20 +411,20 @@ clear_16bit_ximage( GLcontext *ctx, GLboolean all,
       if ((pixel & 0xff) == ((pixel >> 8) & 0xff)) {
          /* low and high bytes are equal so use memset() */
          n = xmesa->xm_draw_buffer->backimage->bytes_per_line
-            * xmesa->xm_draw_buffer->height;
+            * xmesa->xm_draw_buffer->mesa_buffer.Height;
          MEMSET( ptr4, pixel & 0xff, n );
       }
       else {
          pixel = pixel | (pixel<<16);
          n = xmesa->xm_draw_buffer->backimage->bytes_per_line
-            * xmesa->xm_draw_buffer->height / 4;
+            * xmesa->xm_draw_buffer->mesa_buffer.Height / 4;
          do {
             *ptr4++ = pixel;
                n--;
          } while (n!=0);
 
          if ((xmesa->xm_draw_buffer->backimage->bytes_per_line *
-              xmesa->xm_draw_buffer->height) & 0x2)
+              xmesa->xm_draw_buffer->mesa_buffer.Height) & 0x2)
             *(GLushort *)ptr4 = pixel & 0xffff;
       }
    }
@@ -426,8 +462,8 @@ clear_24bit_ximage( GLcontext *ctx, GLboolean all,
    if (all) {
       if (r==g && g==b) {
          /* same value for all three components (gray) */
-         const GLint w3 = xmesa->xm_draw_buffer->width * 3;
-         const GLint h = xmesa->xm_draw_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_draw_buffer, 0, i);
@@ -436,8 +472,8 @@ clear_24bit_ximage( GLcontext *ctx, GLboolean all,
       }
       else {
          /* the usual case */
-         const GLint w = xmesa->xm_draw_buffer->width;
-         const GLint h = xmesa->xm_draw_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_draw_buffer, 0, i);
@@ -630,7 +666,8 @@ clear_32bit_ximage( GLcontext *ctx, GLboolean all,
             | ((pixel << 24) & 0xff000000);
    }
    if (all) {
-      register GLint n = xmesa->xm_draw_buffer->width * xmesa->xm_draw_buffer->height;
+      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 );
@@ -660,24 +697,16 @@ clear_nbit_ximage( GLcontext *ctx, GLboolean all,
 {
    const XMesaContext xmesa = XMESA_CONTEXT(ctx);
    XMesaImage *img = xmesa->xm_draw_buffer->backimage;
-   if (all) {
-      register int i, j;
-      width = xmesa->xm_draw_buffer->width;
-      height = xmesa->xm_draw_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_draw_buffer, y);
-      for (j=0;j<height;j++) {
-         for (i=0;i<width;i++) {
-            XMesaPutPixel( img, x+i, y-j, xmesa->clearpixel );
-         }
+   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);
       }
    }
 }
@@ -720,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 )
@@ -731,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 */
@@ -790,6 +818,9 @@ xmesa_DrawPixels_8R8G8B( GLcontext *ctx,
    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 &&
@@ -804,7 +835,30 @@ xmesa_DrawPixels_8R8G8B( GLcontext *ctx,
       int srcX = unpack->SkipPixels;
       int srcY = unpack->SkipRows;
       int rowLength = unpack->RowLength ? unpack->RowLength : width;
-      if (_swrast_clip_pixelrect(ctx, &dstX, &dstY, &w, &h, &srcX, &srcY)) {
+
+      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.
@@ -828,6 +882,11 @@ xmesa_DrawPixels_8R8G8B( GLcontext *ctx,
          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 */
@@ -861,6 +920,9 @@ xmesa_DrawPixels_5R6G5B( GLcontext *ctx,
    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 &&
@@ -876,7 +938,30 @@ xmesa_DrawPixels_5R6G5B( GLcontext *ctx,
       int srcX = unpack->SkipPixels;
       int srcY = unpack->SkipRows;
       int rowLength = unpack->RowLength ? unpack->RowLength : width;
-      if (_swrast_clip_pixelrect(ctx, &dstX, &dstY, &w, &h, &srcX, &srcY)) {
+
+      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.
@@ -900,6 +985,11 @@ xmesa_DrawPixels_5R6G5B( GLcontext *ctx,
          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 */
@@ -930,7 +1020,10 @@ xmesa_CopyPixels( GLcontext *ctx,
    ASSERT(dpy);
    ASSERT(gc);
 
-   if (ctx->Color.DrawBuffer == GL_FRONT &&
+   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 &&
@@ -1048,7 +1141,9 @@ void xmesa_update_state( GLcontext *ctx, GLuint new_state )
       break;
    }
 
-   xmesa_update_span_funcs(ctx);
+   if (ctx->Color._DrawDestMask[0] & (DD_FRONT_LEFT_BIT | DD_BACK_LEFT_BIT)) {
+      xmesa_update_span_funcs(ctx);
+   }
 }
 
 
@@ -1090,102 +1185,81 @@ test_proxy_teximage(GLcontext *ctx, GLenum target, GLint level,
 }
 
 
+/**
+ * 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);
+   }
+}
 
 
-/* Setup pointers and other driver state that is constant for the life
- * of a context.
+/**
+ * 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.
  */
-void xmesa_init_pointers( GLcontext *ctx )
+static void
+xmesa_viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
 {
-   TNLcontext *tnl;
-   struct swrast_device_driver *dd = _swrast_GetDeviceDriverReference( ctx );
-   const XMesaContext xmesa = XMESA_CONTEXT(ctx);
+   _mesa_ResizeBuffersMESA();
+}
 
-   /* Plug in our driver-specific functions here */
-   ctx->Driver.GetString = get_string;
-   ctx->Driver.GetBufferSize = get_buffer_size;
-   ctx->Driver.Flush = finish_or_flush;
-   ctx->Driver.Finish = finish_or_flush;
-   ctx->Driver.ClearIndex = clear_index;
-   ctx->Driver.ClearColor = clear_color;
-   ctx->Driver.IndexMask = index_mask;
-   ctx->Driver.ColorMask = color_mask;
-   ctx->Driver.Enable = enable;
-
-   /* 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;
-#ifdef XFree86Server
-   ctx->Driver.DrawPixels = _swrast_DrawPixels;
-   ctx->Driver.CopyPixels = _swrast_CopyPixels;
-#else
-   ctx->Driver.CopyPixels = /*_swrast_CopyPixels;*/xmesa_CopyPixels;
-   if (xmesa->xm_visual->undithered_pf == PF_8R8G8B &&
-       xmesa->xm_visual->dithered_pf == PF_8R8G8B) {
-      ctx->Driver.DrawPixels = xmesa_DrawPixels_8R8G8B;
-   }
-   else if (xmesa->xm_visual->undithered_pf == PF_5R6G5B) {
-      ctx->Driver.DrawPixels = xmesa_DrawPixels_5R6G5B;
+
+/**
+ * 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 {
-      ctx->Driver.DrawPixels = _swrast_DrawPixels;
+   else if (xmvisual->undithered_pf == PF_5R6G5B) {
+      driver->DrawPixels = xmesa_DrawPixels_5R6G5B;
    }
 #endif
-   ctx->Driver.ReadPixels = _swrast_ReadPixels;
-   ctx->Driver.DrawBuffer = _swrast_DrawBuffer;
-
-   /* 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 = 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.CompressedTexImage1D = _mesa_store_compressed_teximage1d;
-   ctx->Driver.CompressedTexImage2D = _mesa_store_compressed_teximage2d;
-   ctx->Driver.CompressedTexImage3D = _mesa_store_compressed_teximage3d;
-   ctx->Driver.CompressedTexSubImage1D = _mesa_store_compressed_texsubimage1d;
-   ctx->Driver.CompressedTexSubImage2D = _mesa_store_compressed_texsubimage2d;
-   ctx->Driver.CompressedTexSubImage3D = _mesa_store_compressed_texsubimage3d;
-
-   /* 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;
-
-   /* Initialize the TNL driver interface:
-    */
-   tnl = TNL_CONTEXT(ctx);
-   tnl->Driver.RunPipeline = _tnl_run_pipeline;
-   
-   dd->SetBuffer = set_buffer;
-
-   /* Install swsetup for tnl->Driver.Render.*:
-    */
-   _swsetup_Wakeup(ctx);
-
-   (void) DitherValues;  /* silenced unused var warning */
+   driver->TestProxyTexImage = test_proxy_teximage;
+#if SWTC
+   driver->ChooseTextureFormat = choose_tex_format;
+#else
+   (void) choose_tex_format;
+#endif
 }
 
 
-
-
-
 #define XMESA_NEW_POINT  (_NEW_POINT | \
                           _NEW_RENDERMODE | \
                           _SWRAST_NEW_RASTERMASK)
@@ -1211,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;