added Window-isms previously in gl.h
[mesa.git] / src / mesa / main / teximage.c
index 42d1c90efbf2c279d6032e124428b2ff9f581b73..ae50558bf26c0121a69b640b66132d380ce12729 100644 (file)
@@ -1,10 +1,9 @@
-/* $Id: teximage.c,v 1.12 1999/11/11 01:22:27 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
  * Version:  3.3
  * 
- * Copyright (C) 1999  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2000  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"),
  */
 
 
+#ifdef DEBUG
+static void PrintTexture(const struct gl_texture_image *img)
+{
+  int i, j, c;
+  GLubyte *data = img->Data;
+
+  if (!data) {
+     printf("No texture data\n");
+     return;
+  }
+
+  switch (img->Format) {
+     case GL_ALPHA:
+     case GL_LUMINANCE:
+     case GL_INTENSITY:
+     case GL_COLOR_INDEX:
+        c = 1;
+        break;
+     case GL_LUMINANCE_ALPHA:
+        c = 2;
+        break;
+     case GL_RGB:
+        c = 3;
+        break;
+     case GL_RGBA:
+        c = 4;
+        break;
+     default:
+        gl_problem(NULL, "error in PrintTexture\n");
+        return;
+  }
+
+
+  for (i = 0; i < img->Height; i++) {
+    for (j = 0; j < img->Width; j++) {
+      if (c==1)
+        printf("%02x  ", data[0]);
+      else if (c==2)
+        printf("%02x%02x  ", data[0], data[1]);
+      else if (c==3)
+        printf("%02x%02x%02x  ", data[0], data[1], data[2]);
+      else if (c==4)
+        printf("%02x%02x%02x%02x  ", data[0], data[1], data[2], data[3]);
+      data += c;
+    }
+    printf("\n");
+  }
+}
+#endif
+
+
 
 /*
  * Compute log base 2 of n.
@@ -86,8 +136,8 @@ logbase2( int n )
  * GL_LUMANCE_ALPHA, GL_INTENSITY, GL_RGB, or GL_RGBA.
  * Return -1 if invalid enum.
  */
-static GLint
-decode_internal_format( GLint format )
+GLint
+_mesa_base_tex_format( GLint format )
 {
    switch (format) {
       case GL_ALPHA:
@@ -226,26 +276,6 @@ components_in_intformat( GLint format )
 
 
 
-struct gl_texture_image *
-gl_alloc_texture_image( void )
-{
-   return CALLOC_STRUCT(gl_texture_image);
-}
-
-
-
-void
-gl_free_texture_image( struct gl_texture_image *teximage )
-{
-   if (teximage->Data) {
-      FREE( teximage->Data );
-      teximage->Data = NULL;
-   }
-   FREE( teximage );
-}
-
-
-
 /*
  * Examine the texImage->Format field and set the Red, Green, Blue, etc
  * texel component sizes to default values.
@@ -355,6 +385,64 @@ set_teximage_component_sizes( struct gl_texture_image *texImage )
 }
 
 
+
+/*
+ * Return new gl_texture_image struct with all fields initialized to zero.
+ */
+struct gl_texture_image *
+_mesa_alloc_texture_image( void )
+{
+   return CALLOC_STRUCT(gl_texture_image);
+}
+
+
+
+/*
+ * Initialize most fields of a gl_texture_image struct.
+ */
+static void
+init_texture_image( struct gl_texture_image *img,
+                    GLsizei width, GLsizei height, GLsizei depth,
+                    GLint border, GLenum internalFormat )
+{
+   ASSERT(img);
+   ASSERT(!img->Data);
+   img->Format = (GLenum) _mesa_base_tex_format(internalFormat);
+   set_teximage_component_sizes( img );
+   img->IntFormat = (GLenum) internalFormat;
+   img->Border = border;
+   img->Width = width;
+   img->Height = height;
+   img->Depth = depth;
+   img->WidthLog2 = logbase2(width - 2 * border);
+   if (height == 1)  /* 1-D texture */
+      img->HeightLog2 = 0;
+   else
+      img->HeightLog2 = logbase2(height - 2 * border);
+   if (depth == 1)   /* 2-D texture */
+      img->DepthLog2 = 0;
+   else
+      img->DepthLog2 = logbase2(depth - 2 * border);
+   img->Width2 = 1 << img->WidthLog2;
+   img->Height2 = 1 << img->HeightLog2;
+   img->Depth2 = 1 << img->DepthLog2;
+   img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
+}
+
+
+
+void
+_mesa_free_texture_image( struct gl_texture_image *teximage )
+{
+   if (teximage->Data) {
+      FREE( teximage->Data );
+      teximage->Data = NULL;
+   }
+   FREE( teximage );
+}
+
+
+
 /* Need this to prevent an out-of-bounds memory access when using
  * X86 optimized code.
  */
@@ -367,68 +455,45 @@ set_teximage_component_sizes( struct gl_texture_image *texImage )
 
 
 /*
- * This is called by glTexImage[123]D in order to build a gl_texture_image
- * object given the client's parameters and image data.
- * 
- * NOTES: Width, height and depth should include the border.
- *        All texture image parameters should have already been error checked.
+ * Called by glTexImage[123]D.  Fill in a texture image with data given
+ * by the client.  All pixel transfer and unpack modes are handled here.
+ * NOTE: All texture image parameters should have already been error checked.
  */
-static struct gl_texture_image *
-make_texture_image( GLcontext *ctx, GLint internalFormat,
-                    GLint width, GLint height, GLint depth, GLint border,
+static void
+make_texture_image( GLcontext *ctx,
+                    struct gl_texture_image *texImage,
                     GLenum srcFormat, GLenum srcType, const GLvoid *pixels,
                     const struct gl_pixelstore_attrib *unpacking)
 {
    GLint components, numPixels;
-   struct gl_texture_image *texImage;
-
-   assert(width > 0);
-   assert(height > 0);
-   assert(depth > 0);
-   assert(border == 0 || border == 1);
-   assert(pixels);
-   assert(unpacking);
-
-
-   /*
-    * Allocate and initialize the texture_image struct
-    */
-   texImage = gl_alloc_texture_image();
-   if (!texImage)
-      return NULL;
-
-   texImage->Format = (GLenum) decode_internal_format(internalFormat);
-   set_teximage_component_sizes( texImage );
-   texImage->IntFormat = (GLenum) internalFormat;
-   texImage->Border = border;
-   texImage->Width = width;
-   texImage->Height = height;
-   texImage->Depth = depth;
-   texImage->WidthLog2 = logbase2(width - 2 * border);
-   if (height == 1)  /* 1-D texture */
-      texImage->HeightLog2 = 0;
-   else
-      texImage->HeightLog2 = logbase2(height - 2 * border);
-   if (depth == 1)   /* 2-D texture */
-      texImage->DepthLog2 = 0;
-   else
-      texImage->DepthLog2 = logbase2(depth - 2 * border);
-   texImage->Width2 = 1 << texImage->WidthLog2;
-   texImage->Height2 = 1 << texImage->HeightLog2;
-   texImage->Depth2 = 1 << texImage->DepthLog2;
-   texImage->MaxLog2 = MAX2(texImage->WidthLog2, texImage->HeightLog2);
-
+   GLint internalFormat, width, height, depth, border;
+
+   ASSERT(ctx);
+   ASSERT(texImage);
+   ASSERT(!texImage->Data);
+   ASSERT(pixels);
+   ASSERT(unpacking);
+
+   internalFormat = texImage->IntFormat;
+   width = texImage->Width;
+   height = texImage->Height;
+   depth = texImage->Depth;
+   border = texImage->Border;
    components = components_in_intformat(internalFormat);
-   numPixels = texImage->Width * texImage->Height * texImage->Depth;
 
-   texImage->Data = (GLubyte *) MALLOC(numPixels * components + EXTRA_BYTE);
+   ASSERT(width > 0);
+   ASSERT(height > 0);
+   ASSERT(depth > 0);
+   ASSERT(border == 0 || border == 1);
+   ASSERT(pixels);
+   ASSERT(unpacking);
+   ASSERT(components);
 
-   if (!texImage->Data) {
-      /* out of memory */
-      gl_free_texture_image(texImage);
-      return NULL;
-   }
+   numPixels = width * height * depth;
 
+   texImage->Data = (GLubyte *) MALLOC(numPixels * components + EXTRA_BYTE);
+   if (!texImage->Data)
+      return;      /* out of memory */
 
    /*
     * OK, the texture image struct has been initialized and the texture
@@ -442,15 +507,18 @@ make_texture_image( GLcontext *ctx, GLint internalFormat,
        && !ctx->Pixel.IndexOffset && !ctx->Pixel.IndexShift
        && srcType == GL_UNSIGNED_BYTE && depth == 1) {
 
-      if (srcFormat == internalFormat) {
+      if (srcFormat == internalFormat ||
+          (srcFormat == GL_LUMINANCE && internalFormat == 1) ||
+          (srcFormat == GL_LUMINANCE_ALPHA && internalFormat == 2) ||
+          (srcFormat == GL_RGB && internalFormat == 3) ||
+          (srcFormat == GL_RGBA && internalFormat == 4)) {
          /* This will cover the common GL_RGB, GL_RGBA, GL_ALPHA,
           * GL_LUMINANCE_ALPHA, etc. texture formats.
           */
-         const GLubyte *src = gl_pixel_addr_in_image(unpacking,
-                pixels, width, height, srcFormat, srcType, 0, 0, 0);
-         const GLubyte *src1 = gl_pixel_addr_in_image(unpacking,
-                pixels, width, height, srcFormat, srcType, 0, 1, 0);
-         const GLint srcStride = src1 - src;
+         const GLubyte *src = (const GLubyte *) _mesa_image_address(
+            unpacking, pixels, width, height, srcFormat, srcType, 0, 0, 0);
+         const GLint srcStride = _mesa_image_row_stride(unpacking, width,
+                                                        srcFormat, srcType);
          GLubyte *dst = texImage->Data;
          GLint dstBytesPerRow = width * components * sizeof(GLubyte);
          if (srcStride == dstBytesPerRow) {
@@ -464,15 +532,14 @@ make_texture_image( GLcontext *ctx, GLint internalFormat,
                dst += dstBytesPerRow;
             }
          }
-         return texImage;  /* all done */
+         return;  /* all done */
       }
       else if (srcFormat == GL_RGBA && internalFormat == GL_RGB) {
          /* commonly used by Quake */
-         const GLubyte *src = gl_pixel_addr_in_image(unpacking,
-                pixels, width, height, srcFormat, srcType, 0, 0, 0);
-         const GLubyte *src1 = gl_pixel_addr_in_image(unpacking,
-                pixels, width, height, srcFormat, srcType, 0, 1, 0);
-         const GLint srcStride = src1 - src;
+         const GLubyte *src = (const GLubyte *) _mesa_image_address(
+            unpacking, pixels, width, height, srcFormat, srcType, 0, 0, 0);
+         const GLint srcStride = _mesa_image_row_stride(unpacking, width,
+                                                        srcFormat, srcType);
          GLubyte *dst = texImage->Data;
          GLint i, j;
          for (i = 0; i < height; i++) {
@@ -485,7 +552,7 @@ make_texture_image( GLcontext *ctx, GLint internalFormat,
             }
             src += srcStride;
          }
-         return texImage;  /* all done */
+         return;  /* all done */
       }
    }      
 
@@ -501,7 +568,7 @@ make_texture_image( GLcontext *ctx, GLint internalFormat,
       GLint img, row;
       for (img = 0; img < depth; img++) {
          for (row = 0; row < height; row++) {
-            const GLvoid *source = gl_pixel_addr_in_image(unpacking,
+            const GLvoid *source = _mesa_image_address(unpacking,
                 pixels, width, height, srcFormat, srcType, img, row, 0);
             _mesa_unpack_index_span(ctx, width, dstType, dest,
                                     srcType, source, unpacking, GL_TRUE);
@@ -517,7 +584,7 @@ make_texture_image( GLcontext *ctx, GLint internalFormat,
       GLint img, row;
       for (img = 0; img < depth; img++) {
          for (row = 0; row < height; row++) {
-            const GLvoid *source = gl_pixel_addr_in_image(unpacking,
+            const GLvoid *source = _mesa_image_address(unpacking,
                    pixels, width, height, srcFormat, srcType, img, row, 0);
             _mesa_unpack_ubyte_color_span(ctx, width, dstFormat, dest,
                    srcFormat, srcType, source, unpacking, GL_TRUE);
@@ -525,8 +592,6 @@ make_texture_image( GLcontext *ctx, GLint internalFormat,
          }
       }
    }
-
-   return texImage;   /* All done! */
 }
 
 
@@ -534,48 +599,19 @@ make_texture_image( GLcontext *ctx, GLint internalFormat,
 /*
  * glTexImage[123]D can accept a NULL image pointer.  In this case we
  * create a texture image with unspecified image contents per the OpenGL
- * spec.
+ * spec.  This function creates an empty image for the given texture image.
  */
-static struct gl_texture_image *
-make_null_texture( GLcontext *ctx, GLenum internalFormat,
-                   GLsizei width, GLsizei height, GLsizei depth, GLint border )
+static void
+make_null_texture( struct gl_texture_image *texImage )
 {
    GLint components;
-   struct gl_texture_image *texImage;
    GLint numPixels;
-   (void) ctx;
 
-   /*internalFormat = decode_internal_format(internalFormat);*/
-   components = components_in_intformat(internalFormat);
-   numPixels = width * height * depth;
-
-   texImage = gl_alloc_texture_image();
-   if (!texImage)
-      return NULL;
+   ASSERT(texImage);
+   ASSERT(!texImage->Data);
 
-   texImage->Format = (GLenum) decode_internal_format(internalFormat);
-   set_teximage_component_sizes( texImage );
-   texImage->IntFormat = internalFormat;
-   texImage->Border = border;
-   texImage->Width = width;
-   texImage->Height = height;
-   texImage->Depth = depth;
-   texImage->WidthLog2 = logbase2(width - 2*border);
-   if (height==1)  /* 1-D texture */
-      texImage->HeightLog2 = 0;
-   else
-      texImage->HeightLog2 = logbase2(height - 2*border);
-   if (depth==1)   /* 2-D texture */
-      texImage->DepthLog2 = 0;
-   else
-      texImage->DepthLog2 = logbase2(depth - 2*border);
-   texImage->Width2 = 1 << texImage->WidthLog2;
-   texImage->Height2 = 1 << texImage->HeightLog2;
-   texImage->Depth2 = 1 << texImage->DepthLog2;
-   texImage->MaxLog2 = MAX2( texImage->WidthLog2, texImage->HeightLog2 );
-
-   /* XXX should we really allocate memory for the image or let it be NULL? */
-   /*texImage->Data = NULL;*/
+   components = components_in_intformat(texImage->IntFormat);
+   numPixels = texImage->Width * texImage->Height * texImage->Depth;
 
    texImage->Data = (GLubyte *) MALLOC( numPixels * components + EXTRA_BYTE );
 
@@ -585,7 +621,7 @@ make_null_texture( GLcontext *ctx, GLenum internalFormat,
     * interesting instead of leaving it indeterminate.
     */
    if (texImage->Data) {
-      char message[8][32] = {
+      static const char message[8][32] = {
          "   X   X  XXXXX   XXX     X    ",
          "   XX XX  X      X   X   X X   ",
          "   X X X  X      X      X   X  ",
@@ -598,9 +634,9 @@ make_null_texture( GLcontext *ctx, GLenum internalFormat,
 
       GLubyte *imgPtr = texImage->Data;
       GLint i, j, k;
-      for (i=0;i<height;i++) {
+      for (i = 0; i < texImage->Height; i++) {
          GLint srcRow = 7 - i % 8;
-         for (j=0;j<width;j++) {
+         for (j = 0; j < texImage->Width; j++) {
             GLint srcCol = j % 32;
             GLint texel = (message[srcRow][srcCol]=='X') ? 255 : 70;
             for (k=0;k<components;k++) {
@@ -609,8 +645,6 @@ make_null_texture( GLcontext *ctx, GLenum internalFormat,
          }
       }
    }
-
-   return texImage;
 }
 
 
@@ -659,7 +693,7 @@ texture_error_check( GLcontext *ctx, GLenum target,
    }
 
    /* Border */
-   if (border!=0 && border!=1) {
+   if (border != 0 && border != 1) {
       if (!isProxy) {
          char message[100];
          sprintf(message, "glTexImage%dD(border)", dimensions);
@@ -704,7 +738,7 @@ texture_error_check( GLcontext *ctx, GLenum target,
    }
 
    /* Level */
-   if (level<0 || level>=ctx->Const.MaxTextureLevels) {
+   if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
       if (!isProxy) {
          char message[100];
          sprintf(message, "glTexImage%dD(level)", dimensions);
@@ -713,7 +747,7 @@ texture_error_check( GLcontext *ctx, GLenum target,
       return GL_TRUE;
    }
 
-   iformat = decode_internal_format( internalFormat );
+   iformat = _mesa_base_tex_format( internalFormat );
    if (iformat < 0) {
       if (!isProxy) {
          char message[100];
@@ -723,7 +757,7 @@ texture_error_check( GLcontext *ctx, GLenum target,
       return GL_TRUE;
    }
 
-   if (!gl_is_legal_format_and_type( format, type )) {
+   if (!_mesa_is_legal_format_and_type( format, type )) {
       /* Yes, generate GL_INVALID_OPERATION, not GL_INVALID_ENUM, if there
        * is a type/format mismatch.  See 1.2 spec page 94, sec 3.6.4.
        */
@@ -839,7 +873,7 @@ subtexture_error_check( GLcontext *ctx, GLuint dimensions,
       }
    }
 
-   if (!gl_is_legal_format_and_type(format, type)) {
+   if (!_mesa_is_legal_format_and_type(format, type)) {
       char message[100];
       sprintf(message, "glTexSubImage%dD(format or type)", dimensions);
       gl_error(ctx, GL_INVALID_ENUM, message);
@@ -912,7 +946,7 @@ copytexture_error_check( GLcontext *ctx, GLuint dimensions,
       return GL_TRUE;
    }
 
-   iformat = decode_internal_format( internalFormat );
+   iformat = _mesa_base_tex_format( internalFormat );
    if (iformat < 0) {
       char message[100];
       sprintf(message, "glCopyTexImage%dD(internalFormat)", dimensions);
@@ -967,7 +1001,7 @@ copytexsubimage_error_check( GLcontext *ctx, GLuint dimensions,
       return GL_TRUE;
    }
 
-   teximage = texUnit->CurrentD[3]->Image[level];
+   teximage = texUnit->CurrentD[dimensions]->Image[level];
    if (!teximage) {
       char message[100];
       sprintf(message, "glCopyTexSubImage%dD(undefined texture)", dimensions);
@@ -1029,52 +1063,89 @@ copytexsubimage_error_check( GLcontext *ctx, GLuint dimensions,
  * Called from the API.  Note that width includes the border.
  */
 void
-_mesa_TexImage1D( GLenum target, GLint level, GLint internalformat,
+_mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
                   GLsizei width, GLint border, GLenum format,
                   GLenum type, const GLvoid *pixels )
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage1D");
 
    if (target==GL_TEXTURE_1D) {
-      struct gl_texture_image *teximage;
-      if (texture_error_check( ctx, target, level, internalformat,
+      struct gl_texture_unit *texUnit;
+      struct gl_texture_object *texObj;
+      struct gl_texture_image *texImage;
+
+      if (texture_error_check( ctx, target, level, internalFormat,
                                format, type, 1, width, 1, 1, border )) {
-         /* error in texture image was detected */
-         return;
+         return;   /* error in texture image was detected */
       }
 
-      /* free current texture image, if any */
-      if (texUnit->CurrentD[1]->Image[level]) {
-         gl_free_texture_image( texUnit->CurrentD[1]->Image[level] );
+      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
+      texObj = texUnit->CurrentD[1];
+      texImage = texObj->Image[level];
+
+      if (!texImage) {
+         texImage = _mesa_alloc_texture_image();
+         texObj->Image[level] = texImage;
+         if (!texImage) {
+            gl_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
+            return;
+         }
+      }
+      else if (texImage->Data) {
+         FREE(texImage->Data);
+         texImage->Data = NULL;
       }
 
-      /* make new texture from source image */
+      /* setup the teximage struct's fields */
+      init_texture_image(texImage, width, 1, 1, border, internalFormat);
+
+      /* process the texture image */
       if (pixels) {
-         teximage = make_texture_image(ctx, internalformat, width, 1, 1,
-                              border, format, type, pixels, &ctx->Unpack);
+         GLboolean retain = GL_TRUE;
+         GLboolean success = GL_FALSE;
+         if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
+             && ctx->Driver.TexImage1D) {
+            /* let device driver try to use raw image */
+            success = (*ctx->Driver.TexImage1D)( ctx, target, level, format,
+                                                 type, pixels, &ctx->Unpack,
+                                                 texObj, texImage, &retain);
+         }
+         if (retain || !success) {
+            /* make internal copy of the texture image */
+            make_texture_image(ctx, texImage, format, type,
+                               pixels, &ctx->Unpack);
+            if (!success && ctx->Driver.TexImage1D) {
+               /* let device driver try to use unpacked image */
+               (*ctx->Driver.TexImage1D)( ctx, target, level, texImage->Format,
+                                          GL_UNSIGNED_BYTE, texImage->Data,
+                                          &_mesa_native_packing,
+                                          texObj, texImage, &retain);
+            }
+         }
+         if (!retain && texImage->Data) {
+            FREE(texImage->Data);
+            texImage->Data = NULL;
+         }
       }
       else {
-         teximage = make_null_texture(ctx, (GLenum) internalformat,
-                                      width, 1, 1, border);
+         make_null_texture(texImage);
+         if (ctx->Driver.TexImage1D) {
+            GLboolean retain;
+            (*ctx->Driver.TexImage1D)( ctx, target, level, texImage->Format,
+                                       GL_UNSIGNED_BYTE, texImage->Data,
+                                       &_mesa_native_packing,
+                                       texObj, texImage, &retain);
+         }
       }
 
-      /* install new texture image */
-      texUnit->CurrentD[1]->Image[level] = teximage;
-      gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[1] );
+      /* state update */
+      gl_put_texobj_on_dirty_list( ctx, texObj );
       ctx->NewState |= NEW_TEXTURING;
-
-      /* tell driver about change */
-      if (ctx->Driver.TexImage) {
-         (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_1D,
-                                  texUnit->CurrentD[1],
-                                  level, internalformat, teximage );
-      }
    }
    else if (target==GL_PROXY_TEXTURE_1D) {
       /* Proxy texture: check for errors and update proxy state */
-      if (texture_error_check( ctx, target, level, internalformat,
+      if (texture_error_check( ctx, target, level, internalFormat,
                                format, type, 1, width, 1, 1, border )) {
          if (level>=0 && level<ctx->Const.MaxTextureLevels) {
             MEMSET( ctx->Texture.Proxy1D->Image[level], 0,
@@ -1084,7 +1155,7 @@ _mesa_TexImage1D( GLenum target, GLint level, GLint internalformat,
       else {
          ctx->Texture.Proxy1D->Image[level]->Format = (GLenum) format;
          set_teximage_component_sizes( ctx->Texture.Proxy1D->Image[level] );
-         ctx->Texture.Proxy1D->Image[level]->IntFormat = (GLenum) internalformat;
+         ctx->Texture.Proxy1D->Image[level]->IntFormat = (GLenum) internalFormat;
          ctx->Texture.Proxy1D->Image[level]->Border = border;
          ctx->Texture.Proxy1D->Image[level]->Width = width;
          ctx->Texture.Proxy1D->Image[level]->Height = 1;
@@ -1099,53 +1170,99 @@ _mesa_TexImage1D( GLenum target, GLint level, GLint internalformat,
 
 
 void
-_mesa_TexImage2D( GLenum target, GLint level, GLint internalformat,
+_mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
                   GLsizei width, GLsizei height, GLint border,
                   GLenum format, GLenum type,
                   const GLvoid *pixels )
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage2D");
 
    if (target==GL_TEXTURE_2D) {
-      struct gl_texture_image *teximage;
-      if (texture_error_check( ctx, target, level, internalformat,
+      struct gl_texture_unit *texUnit;
+      struct gl_texture_object *texObj;
+      struct gl_texture_image *texImage;
+
+      if (texture_error_check( ctx, target, level, internalFormat,
                                format, type, 2, width, height, 1, border )) {
-         /* error in texture image was detected */
-         return;
+         return;   /* error in texture image was detected */
       }
 
-      /* free current texture image, if any */
-      if (texUnit->CurrentD[2]->Image[level]) {
-         gl_free_texture_image( texUnit->CurrentD[2]->Image[level] );
+      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
+      texObj = texUnit->CurrentD[2];
+      texImage = texObj->Image[level];
+
+      if (!texImage) {
+         texImage = _mesa_alloc_texture_image();
+         texObj->Image[level] = texImage;
+         if (!texImage) {
+            gl_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
+            return;
+         }
+      }
+      else if (texImage->Data) {
+         FREE(texImage->Data);
+         texImage->Data = NULL;
       }
 
-      /* make new texture from source image */
+      /* setup the teximage struct's fields */
+      init_texture_image(texImage, width, height, 1, border, internalFormat);
+
+      /* process the texture image */
       if (pixels) {
-         teximage = make_texture_image(ctx, internalformat, width, height, 1,
-                                  border, format, type, pixels, &ctx->Unpack);
+         GLboolean retain = GL_TRUE;
+         GLboolean success = GL_FALSE;
+         if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
+             && ctx->Driver.TexImage2D) {
+            /* let device driver try to use raw image */
+            success = (*ctx->Driver.TexImage2D)( ctx, target, level, format,
+                                                 type, pixels, &ctx->Unpack,
+                                                 texObj, texImage, &retain);
+         }
+         if (retain || !success) {
+            /* make internal copy of the texture image */
+            make_texture_image(ctx, texImage, format, type,
+                               pixels, &ctx->Unpack);
+            if (!success && ctx->Driver.TexImage2D) {
+               /* let device driver try to use unpacked image */
+               (*ctx->Driver.TexImage2D)( ctx, target, level, texImage->Format,
+                                          GL_UNSIGNED_BYTE, texImage->Data,
+                                          &_mesa_native_packing,
+                                          texObj, texImage, &retain);
+            }
+         }
+         if (!retain && texImage->Data) {
+            FREE(texImage->Data);
+            texImage->Data = NULL;
+         }
       }
       else {
-         teximage = make_null_texture(ctx, (GLenum) internalformat,
-                                      width, height, 1, border);
+         make_null_texture(texImage);
+         if (ctx->Driver.TexImage2D) {
+            GLboolean retain;
+            (*ctx->Driver.TexImage2D)( ctx, target, level, texImage->Format,
+                                       GL_UNSIGNED_BYTE, texImage->Data,
+                                       &_mesa_native_packing,
+                                       texObj, texImage, &retain);
+         }
       }
 
-      /* install new texture image */
-      texUnit->CurrentD[2]->Image[level] = teximage;
-      gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[2] );
-      ctx->NewState |= NEW_TEXTURING;
-
-      /* tell driver about change */
+#define OLD_DD_TEXTURE
+#ifdef OLD_DD_TEXTURE
+      /* XXX this will be removed in the future */
       if (ctx->Driver.TexImage) {
-         (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_2D,
-                                  texUnit->CurrentD[2],
-                                  level, internalformat, teximage );
+         (*ctx->Driver.TexImage)( ctx, target, texObj, level, internalFormat,
+                                  texImage );
       }
+#endif
+
+      /* state update */
+      gl_put_texobj_on_dirty_list( ctx, texObj );
+      ctx->NewState |= NEW_TEXTURING;
    }
    else if (target==GL_PROXY_TEXTURE_2D) {
       /* Proxy texture: check for errors and update proxy state */
-      if (texture_error_check( ctx, target, level, internalformat,
+      if (texture_error_check( ctx, target, level, internalFormat,
                                format, type, 2, width, height, 1, border )) {
          if (level>=0 && level<ctx->Const.MaxTextureLevels) {
             MEMSET( ctx->Texture.Proxy2D->Image[level], 0,
@@ -1155,7 +1272,7 @@ _mesa_TexImage2D( GLenum target, GLint level, GLint internalformat,
       else {
          ctx->Texture.Proxy2D->Image[level]->Format = (GLenum) format;
          set_teximage_component_sizes( ctx->Texture.Proxy2D->Image[level] );
-         ctx->Texture.Proxy2D->Image[level]->IntFormat = (GLenum) internalformat;
+         ctx->Texture.Proxy2D->Image[level]->IntFormat = (GLenum) internalFormat;
          ctx->Texture.Proxy2D->Image[level]->Border = border;
          ctx->Texture.Proxy2D->Image[level]->Width = width;
          ctx->Texture.Proxy2D->Image[level]->Height = height;
@@ -1175,54 +1292,91 @@ _mesa_TexImage2D( GLenum target, GLint level, GLint internalformat,
  * Note that width and height include the border.
  */
 void
-_mesa_TexImage3D( GLenum target, GLint level, GLint internalformat,
+_mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
                   GLsizei width, GLsizei height, GLsizei depth,
                   GLint border, GLenum format, GLenum type,
                   const GLvoid *pixels )
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage3DEXT");
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage3D");
 
    if (target==GL_TEXTURE_3D_EXT) {
-      struct gl_texture_image *teximage;
-      if (texture_error_check( ctx, target, level, internalformat,
+      struct gl_texture_unit *texUnit;
+      struct gl_texture_object *texObj;
+      struct gl_texture_image *texImage;
+      if (texture_error_check( ctx, target, level, internalFormat,
                                format, type, 3, width, height, depth,
                                border )) {
-         /* error in texture image was detected */
-         return;
+         return;   /* error in texture image was detected */
       }
 
-      /* free current texture image, if any */
-      if (texUnit->CurrentD[3]->Image[level]) {
-         gl_free_texture_image( texUnit->CurrentD[3]->Image[level] );
+      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
+      texObj = texUnit->CurrentD[3];
+      texImage = texObj->Image[level];
+
+      if (!texImage) {
+         texImage = _mesa_alloc_texture_image();
+         texObj->Image[level] = texImage;
+         if (!texImage) {
+            gl_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
+            return;
+         }
+      }
+      else if (texImage->Data) {
+         FREE(texImage->Data);
+         texImage->Data = NULL;
       }
 
-      /* make new texture from source image */
+      /* setup the teximage struct's fields */
+      init_texture_image(texImage, width, height, depth,
+                         border, internalFormat);
+
+      /* process the texture image */
       if (pixels) {
-         teximage = make_texture_image(ctx, internalformat, width, height,
-                       depth, border, format, type, pixels, &ctx->Unpack);
+         GLboolean retain = GL_TRUE;
+         GLboolean success = GL_FALSE;
+         if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
+             && ctx->Driver.TexImage3D) {
+            /* let device driver try to use raw image */
+            success = (*ctx->Driver.TexImage3D)( ctx, target, level, format,
+                                                 type, pixels, &ctx->Unpack,
+                                                 texObj, texImage, &retain);
+         }
+         if (retain || !success) {
+            /* make internal copy of the texture image */
+            make_texture_image(ctx, texImage, format, type,
+                               pixels, &ctx->Unpack);
+            if (!success && ctx->Driver.TexImage3D) {
+               /* let device driver try to use unpacked image */
+               (*ctx->Driver.TexImage3D)( ctx, target, level, texImage->Format,
+                                          GL_UNSIGNED_BYTE, texImage->Data,
+                                          &_mesa_native_packing,
+                                          texObj, texImage, &retain);
+            }
+         }
+         if (!retain && texImage->Data) {
+            FREE(texImage->Data);
+            texImage->Data = NULL;
+         }
       }
       else {
-         teximage = make_null_texture(ctx, (GLenum) internalformat,
-                                      width, height, depth, border);
+         make_null_texture(texImage);
+         if (ctx->Driver.TexImage3D) {
+            GLboolean retain;
+            (*ctx->Driver.TexImage3D)( ctx, target, level, texImage->Format,
+                                       GL_UNSIGNED_BYTE, texImage->Data,
+                                       &_mesa_native_packing,
+                                       texObj, texImage, &retain);
+         }
       }
 
-      /* install new texture image */
-      texUnit->CurrentD[3]->Image[level] = teximage;
-      gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[3] );
+      /* state update */
+      gl_put_texobj_on_dirty_list( ctx, texObj );
       ctx->NewState |= NEW_TEXTURING;
-
-      /* tell driver about change */
-      if (ctx->Driver.TexImage) {
-         (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_3D_EXT,
-                                  texUnit->CurrentD[3],
-                                  level, internalformat, teximage );
-      }
    }
    else if (target==GL_PROXY_TEXTURE_3D_EXT) {
       /* Proxy texture: check for errors and update proxy state */
-      if (texture_error_check( ctx, target, level, internalformat,
+      if (texture_error_check( ctx, target, level, internalFormat,
                                format, type, 3, width, height, depth,
                                border )) {
          if (level>=0 && level<ctx->Const.MaxTextureLevels) {
@@ -1233,7 +1387,7 @@ _mesa_TexImage3D( GLenum target, GLint level, GLint internalformat,
       else {
          ctx->Texture.Proxy3D->Image[level]->Format = (GLenum) format;
          set_teximage_component_sizes( ctx->Texture.Proxy3D->Image[level] );
-         ctx->Texture.Proxy3D->Image[level]->IntFormat = (GLenum) internalformat;
+         ctx->Texture.Proxy3D->Image[level]->IntFormat = (GLenum) internalFormat;
          ctx->Texture.Proxy3D->Image[level]->Border = border;
          ctx->Texture.Proxy3D->Image[level]->Width = width;
          ctx->Texture.Proxy3D->Image[level]->Height = height;
@@ -1247,6 +1401,107 @@ _mesa_TexImage3D( GLenum target, GLint level, GLint internalformat,
 }
 
 
+void
+_mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
+                     GLsizei width, GLsizei height, GLsizei depth,
+                     GLint border, GLenum format, GLenum type,
+                     const GLvoid *pixels )
+{
+   _mesa_TexImage3D(target, level, (GLint) internalFormat, width, height,
+                    depth, border, format, type, pixels);
+}
+
+
+/*
+ * Fetch a texture image from the device driver.
+ * Store the results in the given texture object at the given mipmap level.
+ */
+void
+_mesa_get_teximage_from_driver( GLcontext *ctx, GLenum target, GLint level,
+                                const struct gl_texture_object *texObj )
+{
+   GLvoid *image;
+   GLenum imgFormat, imgType;
+   GLboolean freeImage;
+   struct gl_texture_image *texImage;
+   GLint destComponents, numPixels, srcBytesPerTexel;
+
+   if (!ctx->Driver.GetTexImage)
+      return;
+
+   image = (*ctx->Driver.GetTexImage)( ctx, target, level, texObj,
+                                       &imgFormat, &imgType, &freeImage);
+   if (!image)
+      return;
+
+   texImage = texObj->Image[level];
+   ASSERT(texImage);
+   if (!texImage)
+      return;
+
+   destComponents = components_in_intformat(texImage->Format);
+   assert(destComponents > 0);
+   numPixels = texImage->Width * texImage->Height * texImage->Depth;
+   assert(numPixels > 0);
+   srcBytesPerTexel = _mesa_bytes_per_pixel(imgFormat, imgType);
+   assert(srcBytesPerTexel > 0);
+
+   if (!texImage->Data) {
+      /* Allocate memory for the texture image data */
+      texImage->Data = (GLubyte *) MALLOC(numPixels * destComponents + EXTRA_BYTE);
+   }
+
+   if (imgFormat == texImage->Format && imgType == GL_UNSIGNED_BYTE) {
+      /* We got lucky!  The driver's format and type match Mesa's format. */
+      if (texImage->Data) {
+         MEMCPY(texImage->Data, image, numPixels * destComponents);
+      }
+   }
+   else {
+      /* Convert the texture image from the driver's format to Mesa's
+       * internal format.
+       */
+      const GLint width = texImage->Width;
+      const GLint height = texImage->Height;
+      const GLint depth = texImage->Depth;
+      const GLint destBytesPerRow = width * destComponents * sizeof(GLchan);
+      const GLint srcBytesPerRow = width * srcBytesPerTexel;
+      const GLenum dstType = GL_UNSIGNED_BYTE;
+      const GLenum dstFormat = texImage->Format;
+      const GLubyte *srcPtr = (const GLubyte *) image;
+      GLubyte *destPtr = texImage->Data;
+
+      if (texImage->Format == GL_COLOR_INDEX) {
+         /* color index texture */
+         GLint img, row;
+         assert(imgFormat == GL_COLOR_INDEX);
+         for (img = 0; img < depth; img++) {
+            for (row = 0; row < height; row++) {
+               _mesa_unpack_index_span(ctx, width, dstType, destPtr,
+                             imgType, srcPtr, &_mesa_native_packing, GL_FALSE);
+               destPtr += destBytesPerRow;
+               srcPtr += srcBytesPerRow;
+            }
+         }
+      }
+      else {
+         /* color texture */
+         GLint img, row;
+         for (img = 0; img < depth; img++) {
+            for (row = 0; row < height; row++) {
+               _mesa_unpack_ubyte_color_span(ctx, width, dstFormat, destPtr,
+                  imgFormat, imgType, srcPtr, &_mesa_native_packing, GL_FALSE);
+               destPtr += destBytesPerRow;
+               srcPtr += srcBytesPerRow;
+            }
+         }
+      }
+   }
+
+   if (freeImage)
+      FREE(image);
+}
+
 
 void
 _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
@@ -1254,6 +1509,8 @@ _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
 {
    GET_CURRENT_CONTEXT(ctx);
    const struct gl_texture_object *texObj;
+   struct gl_texture_image *texImage;
+   GLboolean discardImage;
 
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetTexImage");
 
@@ -1262,18 +1519,18 @@ _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
       return;
    }
 
-   if (gl_sizeof_type(type) <= 0) {
+   if (_mesa_sizeof_type(type) <= 0) {
       gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(type)" );
       return;
    }
 
-   if (gl_components_in_format(format) <= 0) {
+   if (_mesa_components_in_format(format) <= 0) {
       gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
       return;
    }
 
    if (!pixels)
-      return;  /* XXX generate an error??? */
+      return;
 
    switch (target) {
       case GL_TEXTURE_1D:
@@ -1290,23 +1547,37 @@ _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
          return;
    }
 
-   if (texObj->Image[level] && texObj->Image[level]->Data) {
-      const struct gl_texture_image *texImage = texObj->Image[level];
+   texImage = texObj->Image[level];
+   if (!texImage) {
+      /* invalid mipmap level */
+      return;
+   }
+
+   if (!texImage->Data) {
+      /* try to get the texture image from the device driver */
+      _mesa_get_teximage_from_driver(ctx, target, level, texObj);
+      discardImage = GL_TRUE;
+   }
+   else {
+      discardImage = GL_FALSE;
+   }
+
+   if (texImage->Data) {
       GLint width = texImage->Width;
       GLint height = texImage->Height;
       GLint row;
 
       for (row = 0; row < height; row++) {
          /* compute destination address in client memory */
-         GLvoid *dest = gl_pixel_addr_in_image( &ctx->Unpack, pixels,
+         GLvoid *dest = _mesa_image_address( &ctx->Unpack, pixels,
                                                 width, height,
                                                 format, type, 0, row, 0);
 
          assert(dest);
          if (texImage->Format == GL_RGBA) {
             const GLubyte *src = texImage->Data + row * width * 4 * sizeof(GLubyte);
-            gl_pack_rgba_span( ctx, width, (void *) src, format, type, dest,
-                               &ctx->Pack, GL_TRUE );
+            _mesa_pack_rgba_span( ctx, width, (CONST GLubyte (*)[4]) src,
+                                  format, type, dest, &ctx->Pack, GL_TRUE );
          }
          else {
             /* fetch RGBA row from texture image then pack it in client mem */
@@ -1369,10 +1640,16 @@ _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
                default:
                   gl_problem( ctx, "bad format in gl_GetTexImage" );
             }
-            gl_pack_rgba_span( ctx, width, (const GLubyte (*)[4])rgba,
-                               format, type, dest, &ctx->Pack, GL_TRUE );
+            _mesa_pack_rgba_span( ctx, width, (const GLubyte (*)[4])rgba,
+                                  format, type, dest, &ctx->Pack, GL_TRUE );
          }
       }
+
+      /* if we got the teximage from the device driver we'll discard it now */
+      if (discardImage) {
+         FREE(texImage->Data);
+         texImage->Data = NULL;
+      }
    }
 }
 
@@ -1385,65 +1662,78 @@ _mesa_TexSubImage1D( GLenum target, GLint level,
                      const GLvoid *pixels )
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
-   struct gl_texture_image *destTex;
+   struct gl_texture_unit *texUnit;
+   struct gl_texture_object *texObj;
+   struct gl_texture_image *texImage;
+   GLboolean success = GL_FALSE;
 
    if (subtexture_error_check(ctx, 1, target, level, xoffset, 0, 0,
                               width, 1, 1, format, type)) {
-      /* error was detected */
-      return;
+      return;   /* error was detected */
    }
 
-   destTex = texUnit->CurrentD[1]->Image[level];
-   assert(destTex);
+   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
+   texObj = texUnit->CurrentD[1];
+   texImage = texObj->Image[level];
+   assert(texImage);
 
    if (width == 0 || !pixels)
       return;  /* no-op, not an error */
 
 
-   /*
-    * Replace the texture subimage
-    */
-   {
-      const GLint texComponents = components_in_intformat(destTex->Format);
-      const GLenum texFormat = destTex->Format;
-      const GLint xoffsetb = xoffset + destTex->Border;
-      GLubyte *dst = destTex->Data + xoffsetb * texComponents;
+   if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
+       && ctx->Driver.TexSubImage1D) {
+      success = (*ctx->Driver.TexSubImage1D)( ctx, target, level, xoffset,
+                                              width, format, type, pixels,
+                                              &ctx->Unpack, texObj, texImage );
+   }
+   if (!success) {
+      /* XXX if Driver.TexSubImage1D, unpack image and try again? */
+
+      const GLint texComponents = components_in_intformat(texImage->Format);
+      const GLenum texFormat = texImage->Format;
+      const GLint xoffsetb = xoffset + texImage->Border;
+      GLboolean retain = GL_TRUE;
+      if (!texImage->Data) {
+         _mesa_get_teximage_from_driver( ctx, target, level, texObj );
+         if (!texImage->Data) {
+            make_null_texture(texImage);
+         }
+         if (!texImage->Data)
+            return;  /* we're really out of luck! */
+      }
+
       if (texFormat == GL_COLOR_INDEX) {
          /* color index texture */
-         const GLvoid *src = gl_pixel_addr_in_image(&ctx->Unpack, pixels,
-                                  width, 1, format, type, 0, 0, 0);
+         GLubyte *dst = texImage->Data + xoffsetb * texComponents;
+         const GLvoid *src = _mesa_image_address(&ctx->Unpack, pixels, width,
+                                                 1, format, type, 0, 0, 0);
          _mesa_unpack_index_span(ctx, width, GL_UNSIGNED_BYTE, dst,
-                                    type, src, &ctx->Unpack, GL_TRUE);
+                                 type, src, &ctx->Unpack, GL_TRUE);
       }
       else {
          /* color texture */
-         const GLvoid *src = gl_pixel_addr_in_image(&ctx->Unpack, pixels,
-                                  width, 1, format, type, 0, 0, 0);
-         _mesa_unpack_ubyte_color_span(ctx, width, texFormat, dst,
-                              format, type, src, &ctx->Unpack, GL_TRUE);
+         GLubyte *dst = texImage->Data + xoffsetb * texComponents;
+         const GLvoid *src = _mesa_image_address(&ctx->Unpack, pixels, width,
+                                                 1, format, type, 0, 0, 0);
+         _mesa_unpack_ubyte_color_span(ctx, width, texFormat, dst, format,
+                                       type, src, &ctx->Unpack, GL_TRUE);
       }
-   }
 
-   gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[1] );
+      if (ctx->Driver.TexImage1D) {
+         (*ctx->Driver.TexImage1D)( ctx, target, level, texImage->Format,
+                                    GL_UNSIGNED_BYTE, texImage->Data,
+                                    &_mesa_native_packing, texObj, texImage,
+                                    &retain );
+      }
 
-   /*
-    * Inform device driver of texture image change.
-    */
-   if (ctx->Driver.TexSubImage) {
-      (*ctx->Driver.TexSubImage)(ctx, GL_TEXTURE_1D, texUnit->CurrentD[1],
-                                 level, xoffset, 0, width, 1,
-                                 texUnit->CurrentD[1]->Image[level]->IntFormat,
-                                 destTex );
-   }
-   else {
-      if (ctx->Driver.TexImage) {
-         (*ctx->Driver.TexImage)(ctx, GL_TEXTURE_1D, texUnit->CurrentD[1],
-                                 level,
-                                 texUnit->CurrentD[1]->Image[level]->IntFormat,
-                                 destTex );
+      if (!retain && texImage->Data) {
+         FREE(texImage->Data);
+         texImage->Data = NULL;
       }
    }
+
+   /*gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[1] );*/
 }
 
 
@@ -1455,76 +1745,104 @@ _mesa_TexSubImage2D( GLenum target, GLint level,
                      const GLvoid *pixels )
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
-   struct gl_texture_image *destTex;
+   struct gl_texture_unit *texUnit;
+   struct gl_texture_object *texObj;
+   struct gl_texture_image *texImage;
+   GLboolean success = GL_FALSE;
 
    if (subtexture_error_check(ctx, 2, target, level, xoffset, yoffset, 0,
                               width, height, 1, format, type)) {
-      /* error was detected */
-      return;
+      return;   /* error was detected */
    }
 
-   destTex = texUnit->CurrentD[2]->Image[level];
-   assert(destTex);
+   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
+   texObj = texUnit->CurrentD[2];
+   texImage = texObj->Image[level];
+   assert(texImage);
 
    if (width == 0 || height == 0 || !pixels)
       return;  /* no-op, not an error */
 
+   if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
+       && ctx->Driver.TexSubImage2D) {
+      success = (*ctx->Driver.TexSubImage2D)( ctx, target, level, xoffset,
+                                     yoffset, width, height, format, type,
+                                     pixels, &ctx->Unpack, texObj, texImage );
+   }
+   if (!success) {
+      /* XXX if Driver.TexSubImage2D, unpack image and try again? */
+
+      const GLint texComponents = components_in_intformat(texImage->Format);
+      const GLenum texFormat = texImage->Format;
+      const GLint xoffsetb = xoffset + texImage->Border;
+      const GLint yoffsetb = yoffset + texImage->Border;
+      const GLint srcStride = _mesa_image_row_stride(&ctx->Unpack, width,
+                                                     format, type);
+      const GLint dstStride = texImage->Width * texComponents *sizeof(GLubyte);
+      GLboolean retain = GL_TRUE;
+
+      if (!texImage->Data) {
+         _mesa_get_teximage_from_driver( ctx, target, level, texObj );
+         if (!texImage->Data) {
+            make_null_texture(texImage);
+         }
+         if (!texImage->Data)
+            return;  /* we're really out of luck! */
+      }
 
-   /*
-    * Replace the texture subimage
-    */
-   {
-      const GLint texComponents = components_in_intformat(destTex->Format);
-      const GLenum texFormat = destTex->Format;
-      const GLint xoffsetb = xoffset + destTex->Border;
-      const GLint yoffsetb = yoffset + destTex->Border;
-      GLubyte *dst = destTex->Data
-                   + (yoffsetb * destTex->Width + xoffsetb) * texComponents;
       if (texFormat == GL_COLOR_INDEX) {
          /* color index texture */
-         const GLint stride = destTex->Width * sizeof(GLubyte);
+         GLubyte *dst = texImage->Data
+                    + (yoffsetb * texImage->Width + xoffsetb) * texComponents;
+         const GLubyte *src = _mesa_image_address(&ctx->Unpack, pixels,
+                                     width, height, format, type, 0, 0, 0);
          GLint row;
          for (row = 0; row < height; row++) {
-            const GLvoid *src = gl_pixel_addr_in_image(&ctx->Unpack, pixels,
-                                     width, height, format, type, 0, row, 0);
-            _mesa_unpack_index_span(ctx, width, GL_UNSIGNED_BYTE, dst,
-                                    type, src, &ctx->Unpack, GL_TRUE);
-            dst += stride;
+            _mesa_unpack_index_span(ctx, width, GL_UNSIGNED_BYTE, dst, type,
+                                 (const GLvoid *) src, &ctx->Unpack, GL_TRUE);
+            src += srcStride;
+            dst += dstStride;
          }
       }
       else {
          /* color texture */
-         const GLint stride = destTex->Width * texComponents * sizeof(GLubyte);
+         GLubyte *dst = texImage->Data
+                    + (yoffsetb * texImage->Width + xoffsetb) * texComponents;
+         const GLubyte *src = _mesa_image_address(&ctx->Unpack, pixels,
+                                     width, height, format, type, 0, 0, 0);
          GLint row;
          for (row = 0; row < height; row++) {
-            const GLvoid *src = gl_pixel_addr_in_image(&ctx->Unpack, pixels,
-                                     width, height, format, type, 0, row, 0);
-            _mesa_unpack_ubyte_color_span(ctx, width, texFormat, dst,
-                                 format, type, src, &ctx->Unpack, GL_TRUE);
-            dst += stride;
+            _mesa_unpack_ubyte_color_span(ctx, width, texFormat, dst, format,
+                           type, (const GLvoid *) src, &ctx->Unpack, GL_TRUE);
+            src += srcStride;
+            dst += dstStride;
          }
       }
-   }
 
-   gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[2] );
+      if (ctx->Driver.TexImage2D) {
+         (*ctx->Driver.TexImage2D)(ctx, target, level, texImage->Format,
+                                   GL_UNSIGNED_BYTE, texImage->Data,
+                                   &_mesa_native_packing, texObj, texImage,
+                                   &retain);
+      }
 
-   /*
-    * Inform device driver of texture image change.
-    */
-   if (ctx->Driver.TexSubImage) {
-      (*ctx->Driver.TexSubImage)(ctx, GL_TEXTURE_2D, texUnit->CurrentD[2],
-                                 level, xoffset, yoffset, width, height,
-                                 texUnit->CurrentD[2]->Image[level]->IntFormat,
-                                 destTex );
-   }
-   else {
-      if (ctx->Driver.TexImage) {
-         (*ctx->Driver.TexImage)(ctx, GL_TEXTURE_2D, texUnit->CurrentD[2],
-                                 level,
-                                 texUnit->CurrentD[2]->Image[level]->IntFormat,
-                                 destTex );
+      if (!retain && texImage->Data) {
+         FREE(texImage->Data);
+         texImage->Data = NULL;
+      }
+
+#ifdef OLD_DD_TEXTURE
+      /* XXX this will be removed in the future */
+      if (ctx->Driver.TexSubImage) {
+         (*ctx->Driver.TexSubImage)(ctx, target, texObj, level,
+                                    xoffset, yoffset, width, height,
+                                    texImage->IntFormat, texImage);
       }
+      else if (ctx->Driver.TexImage) {
+         (*ctx->Driver.TexImage)(ctx, GL_TEXTURE_2D, texObj,
+                                 level, texImage->IntFormat, texImage );
+      }
+#endif
    }
 }
 
@@ -1538,71 +1856,90 @@ _mesa_TexSubImage3D( GLenum target, GLint level,
                      const GLvoid *pixels )
 {
    GET_CURRENT_CONTEXT(ctx);
-   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
-   struct gl_texture_image *destTex;
+   struct gl_texture_unit *texUnit;
+   struct gl_texture_object *texObj;
+   struct gl_texture_image *texImage;
+   GLboolean success = GL_FALSE;
 
    if (subtexture_error_check(ctx, 3, target, level, xoffset, yoffset, zoffset,
                               width, height, depth, format, type)) {
-      /* error was detected */
-      return;
+      return;   /* error was detected */
    }
 
-   destTex = texUnit->CurrentD[3]->Image[level];
-   assert(destTex);
+   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
+   texObj = texUnit->CurrentD[3];
+   texImage = texObj->Image[level];
+   assert(texImage);
 
    if (width == 0 || height == 0 || height == 0 || !pixels)
       return;  /* no-op, not an error */
 
-   /*
-    * Replace the texture subimage
-    */
-   {
-      const GLint texComponents = components_in_intformat(destTex->Format);
-      const GLenum texFormat = destTex->Format;
-      const GLint xoffsetb = xoffset + destTex->Border;
-      const GLint yoffsetb = yoffset + destTex->Border;
-      const GLint zoffsetb = zoffset + destTex->Border;
-      GLint dstRectArea = destTex->Width * destTex->Height;
-      GLubyte *dst = destTex->Data 
-            + (zoffsetb * dstRectArea +  yoffsetb * destTex->Width + xoffsetb)
-            * texComponents;
+   if (!ctx->Pixel.MapColorFlag && !ctx->Pixel.ScaleOrBiasRGBA
+       && ctx->Driver.TexSubImage3D) {
+      success = (*ctx->Driver.TexSubImage3D)( ctx, target, level, xoffset,
+                                yoffset, zoffset, width, height, depth, format,
+                                type, pixels, &ctx->Unpack, texObj, texImage );
+   }
+   if (!success) {
+      /* XXX if Driver.TexSubImage3D, unpack image and try again? */
+
+      const GLint texComponents = components_in_intformat(texImage->Format);
+      const GLenum texFormat = texImage->Format;
+      const GLint xoffsetb = xoffset + texImage->Border;
+      const GLint yoffsetb = yoffset + texImage->Border;
+      const GLint zoffsetb = zoffset + texImage->Border;
+      const GLint texWidth = texImage->Width;
+      const GLint dstRectArea = texWidth * texImage->Height;
+      const GLint srcStride = _mesa_image_row_stride(&ctx->Unpack,
+                                                     width, format, type);
+      const GLint dstStride = texWidth * texComponents * sizeof(GLubyte);
+      GLboolean retain = GL_TRUE;
 
       if (texFormat == GL_COLOR_INDEX) {
          /* color index texture */
-         const GLint stride = destTex->Width * sizeof(GLubyte);
          GLint img, row;
          for (img = 0; img < depth; img++) {
+            const GLubyte *src = _mesa_image_address(&ctx->Unpack, pixels,
+                                    width, height, format, type, img, 0, 0);
+            GLubyte *dst = texImage->Data + ((zoffsetb + img) * dstRectArea
+                     + yoffsetb * texWidth + xoffsetb) * texComponents;
             for (row = 0; row < height; row++) {
-               const GLvoid *src = gl_pixel_addr_in_image(&ctx->Unpack, pixels,
-                                    width, height, format, type, img, row, 0);
                _mesa_unpack_index_span(ctx, width, GL_UNSIGNED_BYTE, dst,
-                                       type, src, &ctx->Unpack, GL_TRUE);
-               dst += stride;
+                         type, (const GLvoid *) src, &ctx->Unpack, GL_TRUE);
+               src += srcStride;
+               dst += dstStride;
             }
          }
       }
       else {
          /* color texture */
-         const GLint stride = destTex->Width * texComponents * sizeof(GLubyte);
          GLint img, row;
          for (img = 0; img < depth; img++) {
+            const GLubyte *src = _mesa_image_address(&ctx->Unpack, pixels,
+                                     width, height, format, type, img, 0, 0);
+            GLubyte *dst = texImage->Data + ((zoffsetb + img) * dstRectArea
+                     + yoffsetb * texWidth + xoffsetb) * texComponents;
             for (row = 0; row < height; row++) {
-               const GLvoid *src = gl_pixel_addr_in_image(&ctx->Unpack, pixels,
-                                     width, height, format, type, img, row, 0);
                _mesa_unpack_ubyte_color_span(ctx, width, texFormat, dst,
-                                    format, type, src, &ctx->Unpack, GL_TRUE);
-               dst += stride;
+                   format, type, (const GLvoid *) src, &ctx->Unpack, GL_TRUE);
+               src += srcStride;
+               dst += dstStride;
             }
          }
       }
-   }
 
-   gl_put_texobj_on_dirty_list( ctx, texUnit->CurrentD[1] );
+      if (ctx->Driver.TexImage3D) {
+         (*ctx->Driver.TexImage3D)(ctx, target, level, texImage->Format,
+                                   GL_UNSIGNED_BYTE, texImage->Data,
+                                   &_mesa_native_packing, texObj, texImage,
+                                   &retain);
+      }
 
-   /*
-    * Inform device driver of texture image change.
-    */
-   /* XXX todo */
+      if (!retain && texImage->Data) {
+         FREE(texImage->Data);
+         texImage->Data = NULL;
+      }
+   }
 }
 
 
@@ -1622,22 +1959,25 @@ read_color_image( GLcontext *ctx, GLint x, GLint y,
    GLint stride, i;
    GLubyte *image, *dst;
 
-   image = MALLOC(width * height * 4 * sizeof(GLubyte));
+   image = (GLubyte *) MALLOC(width * height * 4 * sizeof(GLubyte));
    if (!image)
       return NULL;
 
    /* Select buffer to read from */
-   (void) (*ctx->Driver.SetBuffer)( ctx, ctx->Pixel.DriverReadBuffer );
+   (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
+                                 ctx->Pixel.DriverReadBuffer );
 
    dst = image;
    stride = width * 4 * sizeof(GLubyte);
    for (i = 0; i < height; i++) {
-      gl_read_rgba_span( ctx, width, x, y + i, (GLubyte (*)[4]) dst );
+      gl_read_rgba_span( ctx, ctx->ReadBuffer, width, x, y + i,
+                         (GLubyte (*)[4]) dst );
       dst += stride;
    }
 
-   /* Restore drawing buffer */
-   (void) (*ctx->Driver.SetBuffer)( ctx, ctx->Color.DriverDrawBuffer );
+   /* Read from draw buffer (the default) */
+   (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
+                                 ctx->Color.DriverDrawBuffer );
 
    return image;
 }
@@ -1653,15 +1993,22 @@ _mesa_CopyTexImage1D( GLenum target, GLint level,
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexImage1D");
 
-   if (!copytexture_error_check(ctx, 1, target, level, internalFormat,
-                               width, 1, border)) {
+   if (copytexture_error_check(ctx, 1, target, level, internalFormat,
+                               width, 1, border))
+      return;
+
+   if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
+       || !ctx->Driver.CopyTexImage1D 
+       || !(*ctx->Driver.CopyTexImage1D)(ctx, target, level,
+                         internalFormat, x, y, width, border))
+   {
       GLubyte *image  = read_color_image( ctx, x, y, width, 1 );
       if (!image) {
          gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D" );
          return;
       }
-      (*ctx->Exec.TexImage1D)( target, level, internalFormat, width,
-                     border, GL_RGBA, GL_UNSIGNED_BYTE, image );
+      (*ctx->Exec->TexImage1D)( target, level, internalFormat, width,
+                                border, GL_RGBA, GL_UNSIGNED_BYTE, image );
       FREE(image);
    }
 }
@@ -1676,15 +2023,22 @@ _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexImage2D");
 
-   if (!copytexture_error_check(ctx, 2, target, level, internalFormat,
-                               width, height, border)) {
+   if (copytexture_error_check(ctx, 2, target, level, internalFormat,
+                               width, height, border))
+      return;
+
+   if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
+       || !ctx->Driver.CopyTexImage2D
+       || !(*ctx->Driver.CopyTexImage2D)(ctx, target, level,
+                         internalFormat, x, y, width, height, border))
+   {
       GLubyte *image  = read_color_image( ctx, x, y, width, height );
       if (!image) {
          gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D" );
          return;
       }
-      (ctx->Exec.TexImage2D)( target, level, internalFormat, width,
-                         height, border, GL_RGBA, GL_UNSIGNED_BYTE, image );
+      (ctx->Exec->TexImage2D)( target, level, internalFormat, width,
+                      height, border, GL_RGBA, GL_UNSIGNED_BYTE, image );
       FREE(image);
    }
 }
@@ -1700,17 +2054,6 @@ copy_tex_sub_image( GLcontext *ctx, struct gl_texture_image *dest,
                     GLint srcx, GLint srcy,
                     GLint dstx, GLint dsty, GLint dstz )
 {
-   static struct gl_pixelstore_attrib packing = {
-      1,            /* Alignment */
-      0,            /* RowLength */
-      0,            /* SkipPixels */
-      0,            /* SkipRows */
-      0,            /* ImageHeight */
-      0,            /* SkipImages */
-      GL_FALSE,     /* SwapBytes */
-      GL_FALSE      /* LsbFirst */
-   };
-
    GLint i;
    GLint format, components, rectarea;
    GLint texwidth, texheight, zoffset;
@@ -1727,20 +2070,22 @@ copy_tex_sub_image( GLcontext *ctx, struct gl_texture_image *dest,
    components = components_in_intformat( format );
 
    /* Select buffer to read from */
-   (void) (*ctx->Driver.SetBuffer)( ctx, ctx->Pixel.DriverReadBuffer );
+   (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
+                                 ctx->Pixel.DriverReadBuffer );
 
    for (i = 0;i < height; i++) {
       GLubyte rgba[MAX_WIDTH][4];
       GLubyte *dst;
-      gl_read_rgba_span( ctx, width, srcx, srcy + i, rgba );
+      gl_read_rgba_span( ctx, ctx->ReadBuffer, width, srcx, srcy + i, rgba );
       dst = dest->Data + ( zoffset + (dsty+i) * texwidth + dstx) * components;
       _mesa_unpack_ubyte_color_span(ctx, width, format, dst,
                                     GL_RGBA, GL_UNSIGNED_BYTE, rgba,
-                                    &packing, GL_TRUE);
+                                    &_mesa_native_packing, GL_TRUE);
    }
 
-   /* Restore drawing buffer */
-   (void) (*ctx->Driver.SetBuffer)( ctx, ctx->Color.DriverDrawBuffer );
+   /* Read from draw buffer (the default) */
+   (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
+                                 ctx->Color.DriverDrawBuffer );
 }
 
 
@@ -1753,8 +2098,14 @@ _mesa_CopyTexSubImage1D( GLenum target, GLint level,
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexSubImage1D");
 
-   if (!copytexsubimage_error_check(ctx, 1, target, level,
-                    xoffset, 0, 0, width, 1)) {
+   if (copytexsubimage_error_check(ctx, 1, target, level,
+                                   xoffset, 0, 0, width, 1))
+      return;
+
+   if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
+       || !ctx->Driver.CopyTexSubImage1D
+       || !(*ctx->Driver.CopyTexSubImage1D)(ctx, target, level,
+                                            xoffset, x, y, width)) {
       struct gl_texture_unit *texUnit;
       struct gl_texture_image *teximage;
       texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
@@ -1762,12 +2113,13 @@ _mesa_CopyTexSubImage1D( GLenum target, GLint level,
       assert(teximage);
       if (teximage->Data) {
          copy_tex_sub_image(ctx, teximage, width, 1, x, y, xoffset, 0, 0);
-        /* tell driver about the change */
-        if (ctx->Driver.TexImage) {
-          (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_1D,
-                                    texUnit->CurrentD[1],
-                                   level, teximage->IntFormat, teximage );
-        }
+         /* tell driver about the change */
+         /* XXX this is obsolete */
+         if (ctx->Driver.TexImage) {
+            (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_1D,
+                                     texUnit->CurrentD[1],
+                                     level, teximage->IntFormat, teximage );
+         }
       }
    }
 }
@@ -1782,8 +2134,14 @@ _mesa_CopyTexSubImage2D( GLenum target, GLint level,
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexSubImage2D");
 
-   if (!copytexsubimage_error_check(ctx, 2, target, level,
-                    xoffset, yoffset, 0, width, height)) {
+   if (copytexsubimage_error_check(ctx, 2, target, level,
+                                   xoffset, yoffset, 0, width, height))
+      return;
+
+   if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
+       || !ctx->Driver.CopyTexSubImage2D
+       || !(*ctx->Driver.CopyTexSubImage2D)(ctx, target, level,
+                                xoffset, yoffset, x, y, width, height )) {
       struct gl_texture_unit *texUnit;
       struct gl_texture_image *teximage;
       texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
@@ -1792,11 +2150,12 @@ _mesa_CopyTexSubImage2D( GLenum target, GLint level,
       if (teximage->Data) {
          copy_tex_sub_image(ctx, teximage, width, height,
                             x, y, xoffset, yoffset, 0);
-        /* tell driver about the change */
-        if (ctx->Driver.TexImage) {
-          (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_2D,
-                                    texUnit->CurrentD[2],
-                                   level, teximage->IntFormat, teximage );
+         /* tell driver about the change */
+         /* XXX this is obsolete */
+         if (ctx->Driver.TexImage) {
+            (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_2D,
+                                     texUnit->CurrentD[2],
+                                     level, teximage->IntFormat, teximage );
         }
       }
    }
@@ -1812,8 +2171,14 @@ _mesa_CopyTexSubImage3D( GLenum target, GLint level,
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCopyTexSubImage3D");
 
-   if (!copytexsubimage_error_check(ctx, 3, target, level,
-                    xoffset, yoffset, zoffset, width, height)) {
+   if (copytexsubimage_error_check(ctx, 3, target, level,
+                    xoffset, yoffset, zoffset, width, height))
+      return;
+
+   if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
+       || !ctx->Driver.CopyTexSubImage3D
+       || !(*ctx->Driver.CopyTexSubImage3D)(ctx, target, level,
+                     xoffset, yoffset, zoffset, x, y, width, height )) {
       struct gl_texture_unit *texUnit;
       struct gl_texture_image *teximage;
       texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
@@ -1822,13 +2187,13 @@ _mesa_CopyTexSubImage3D( GLenum target, GLint level,
       if (teximage->Data) {
          copy_tex_sub_image(ctx, teximage, width, height, 
                             x, y, xoffset, yoffset, zoffset);
-        /* tell driver about the change */
-        if (ctx->Driver.TexImage) {
-          (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_3D,
-                                    texUnit->CurrentD[3],
-                                   level, teximage->IntFormat, teximage );
-        }
+         /* tell driver about the change */
+         /* XXX this is obsolete */
+         if (ctx->Driver.TexImage) {
+            (*ctx->Driver.TexImage)( ctx, GL_TEXTURE_3D,
+                                     texUnit->CurrentD[3],
+                                     level, teximage->IntFormat, teximage );
+         }
       }
    }
 }
-