fix GL_BACK color material bug
[mesa.git] / src / mesa / main / teximage.c
index aa48024029bbe9c4283f206b6b768e14de03ec6f..f5a2047b33ac18d951765c20d324d91197ceda11 100644 (file)
@@ -1,4 +1,3 @@
-/* $Id: teximage.c,v 1.23 2000/03/21 01:03:40 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
  */
 
 
-/*
- * Default pixel packing of Mesa's internal texture images:
- */
-static struct gl_pixelstore_attrib DefaultPacking = {
-      1,            /* Alignment */
-      0,            /* RowLength */
-      0,            /* SkipPixels */
-      0,            /* SkipRows */
-      0,            /* ImageHeight */
-      0,            /* SkipImages */
-      GL_FALSE,     /* SwapBytes */
-      GL_FALSE      /* LsbFirst */
-};
+#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
 
 
 
@@ -101,16 +136,24 @@ 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( GLcontext *ctx, GLint format )
 {
    switch (format) {
+      case GL_COMPRESSED_ALPHA_ARB:
+         if (ctx && !ctx->Extensions.HaveTextureCompression)
+            return -1;
+         /* fall-through */
       case GL_ALPHA:
       case GL_ALPHA4:
       case GL_ALPHA8:
       case GL_ALPHA12:
       case GL_ALPHA16:
          return GL_ALPHA;
+      case GL_COMPRESSED_LUMINANCE_ARB:
+         if (ctx && !ctx->Extensions.HaveTextureCompression)
+            return -1;
+         /* fall-through */
       case 1:
       case GL_LUMINANCE:
       case GL_LUMINANCE4:
@@ -118,6 +161,10 @@ decode_internal_format( GLint format )
       case GL_LUMINANCE12:
       case GL_LUMINANCE16:
          return GL_LUMINANCE;
+      case GL_COMPRESSED_LUMINANCE_ALPHA_ARB:
+         if (ctx && !ctx->Extensions.HaveTextureCompression)
+            return -1;
+         /* fall-through */
       case 2:
       case GL_LUMINANCE_ALPHA:
       case GL_LUMINANCE4_ALPHA4:
@@ -127,12 +174,31 @@ decode_internal_format( GLint format )
       case GL_LUMINANCE12_ALPHA12:
       case GL_LUMINANCE16_ALPHA16:
          return GL_LUMINANCE_ALPHA;
+      case GL_COMPRESSED_INTENSITY_ARB:
+         if (ctx && !ctx->Extensions.HaveTextureCompression)
+            return -1;
+         /* fall-through */
       case GL_INTENSITY:
       case GL_INTENSITY4:
       case GL_INTENSITY8:
       case GL_INTENSITY12:
       case GL_INTENSITY16:
          return GL_INTENSITY;
+      case GL_COMPRESSED_RGB_ARB:
+         if (ctx && ctx->Extensions.HaveTextureCompression)
+            return GL_RGB;
+         else
+            return -1;
+      case GL_COMPRESSED_RGB_FXT1_3DFX:
+         if (ctx && ctx->Extensions.HaveTextureCompressionFXT1)
+            return GL_RGB;
+         else
+            return -1;
+      case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
+         if (ctx && ctx->Extensions.HaveTextureCompressionS3TC)
+            return GL_RGB;
+         else
+            return -1;
       case 3:
       case GL_RGB:
       case GL_R3_G3_B2:
@@ -143,6 +209,23 @@ decode_internal_format( GLint format )
       case GL_RGB12:
       case GL_RGB16:
          return GL_RGB;
+      case GL_COMPRESSED_RGBA_ARB:
+         if (ctx && ctx->Extensions.HaveTextureCompression)
+            return GL_RGBA;
+         else
+            return -1;
+      case GL_COMPRESSED_RGBA_FXT1_3DFX:
+         if (ctx && ctx->Extensions.HaveTextureCompressionFXT1)
+            return GL_RGBA;
+         else
+            return -1;
+      case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
+      case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
+      case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
+         if (ctx && ctx->Extensions.HaveTextureCompressionS3TC)
+            return GL_RGBA;
+         else
+            return -1;
       case 4:
       case GL_RGBA:
       case GL_RGBA2:
@@ -240,6 +323,33 @@ components_in_intformat( GLint format )
 }
 
 
+/*
+ * Return GL_TRUE if internalFormat is a compressed format, return GL_FALSE
+ * otherwise.
+ */
+static GLboolean
+is_compressed_format(GLenum internalFormat)
+{
+   switch (internalFormat) {
+      case GL_COMPRESSED_ALPHA_ARB:
+      case GL_COMPRESSED_LUMINANCE_ARB:
+      case GL_COMPRESSED_LUMINANCE_ALPHA_ARB:
+      case GL_COMPRESSED_INTENSITY_ARB:
+      case GL_COMPRESSED_RGB_ARB:
+      case GL_COMPRESSED_RGBA_ARB:
+      case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
+      case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
+      case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
+      case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
+      case GL_COMPRESSED_RGB_FXT1_3DFX:
+      case GL_COMPRESSED_RGBA_FXT1_3DFX:
+         return GL_TRUE;
+      default:
+         return GL_FALSE;
+   }
+}
+
+
 
 /*
  * Examine the texImage->Format field and set the Red, Green, Blue, etc
@@ -350,12 +460,47 @@ set_teximage_component_sizes( struct gl_texture_image *texImage )
 }
 
 
+static void
+set_tex_image(struct gl_texture_object *tObj,
+              GLenum target, GLint level,
+              struct gl_texture_image *texImage)
+{
+   ASSERT(tObj);
+   ASSERT(texImage);
+   switch (target) {
+      case GL_TEXTURE_2D:
+         tObj->Image[level] = texImage;
+         return;
+      case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
+         tObj->Image[level] = texImage;
+         return;
+      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
+         tObj->NegX[level] = texImage;
+         return;
+      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
+         tObj->PosY[level] = texImage;
+         return;
+      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
+         tObj->NegY[level] = texImage;
+         return;
+      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
+         tObj->PosZ[level] = texImage;
+         return;
+      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
+         tObj->NegZ[level] = texImage;
+         return;
+      default:
+         gl_problem(NULL, "bad target in set_tex_image()");
+         return;
+   }
+}
+
 
 /*
  * Return new gl_texture_image struct with all fields initialized to zero.
  */
 struct gl_texture_image *
-gl_alloc_texture_image( void )
+_mesa_alloc_texture_image( void )
 {
    return CALLOC_STRUCT(gl_texture_image);
 }
@@ -363,17 +508,16 @@ gl_alloc_texture_image( void )
 
 
 /*
- * Return a new gl_texture_image struct with most field initialized.
+ * Initialize most fields of a gl_texture_image struct.
  */
-static struct gl_texture_image *
-new_texture_image( GLsizei width, GLsizei height, GLsizei depth,
-                   GLint border, GLenum internalFormat )
+static void
+init_texture_image( struct gl_texture_image *img,
+                    GLsizei width, GLsizei height, GLsizei depth,
+                    GLint border, GLenum internalFormat )
 {
-   struct gl_texture_image *img = CALLOC_STRUCT(gl_texture_image);
-   if (!img)
-      return NULL;
-
-   img->Format = (GLenum) decode_internal_format(internalFormat);
+   ASSERT(img);
+   ASSERT(!img->Data);
+   img->Format = (GLenum) _mesa_base_tex_format(NULL, internalFormat);
    set_teximage_component_sizes( img );
    img->IntFormat = (GLenum) internalFormat;
    img->Border = border;
@@ -393,14 +537,13 @@ new_texture_image( GLsizei width, GLsizei height, GLsizei depth,
    img->Height2 = 1 << img->HeightLog2;
    img->Depth2 = 1 << img->DepthLog2;
    img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
-
-   return img;
+   img->IsCompressed = is_compressed_format(internalFormat);
 }
 
 
 
 void
-gl_free_texture_image( struct gl_texture_image *teximage )
+_mesa_free_texture_image( struct gl_texture_image *teximage )
 {
    if (teximage->Data) {
       FREE( teximage->Data );
@@ -411,6 +554,123 @@ gl_free_texture_image( struct gl_texture_image *teximage )
 
 
 
+/*
+ * Return number of bytes of storage needed to store a compressed texture
+ * image.
+ */
+GLuint
+_mesa_compressed_image_size(GLenum internalFormat,
+                            GLint width, GLint height, GLint depth)
+{
+   return 0;
+}
+
+
+
+/*
+ * Given a texture unit and a texture target, return the corresponding
+ * texture object.
+ */
+struct gl_texture_object *
+_mesa_select_tex_object(GLcontext *ctx, struct gl_texture_unit *texUnit,
+                        GLenum target)
+{
+   switch (target) {
+      case GL_TEXTURE_1D:
+         return texUnit->CurrentD[1];
+      case GL_PROXY_TEXTURE_1D:
+         return ctx->Texture.Proxy1D;
+      case GL_TEXTURE_2D:
+         return texUnit->CurrentD[2];
+      case GL_PROXY_TEXTURE_2D:
+         return ctx->Texture.Proxy2D;
+      case GL_TEXTURE_3D:
+         return texUnit->CurrentD[3];
+      case GL_PROXY_TEXTURE_3D:
+         return ctx->Texture.Proxy3D;
+      case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
+      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
+      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
+      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
+      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
+      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
+         return ctx->Extensions.HaveTextureCubeMap
+                ? texUnit->CurrentCubeMap : NULL;
+      case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
+         return ctx->Extensions.HaveTextureCubeMap
+                ? ctx->Texture.ProxyCubeMap : NULL;
+      default:
+         gl_problem(NULL, "bad target in _mesa_select_tex_object()");
+         return NULL;
+   }
+}
+
+
+/*
+ * Return the texture image struct which corresponds to target and level
+ * for the given texture unit.
+ */
+struct gl_texture_image *
+_mesa_select_tex_image(GLcontext *ctx, const struct gl_texture_unit *texUnit,
+                       GLenum target, GLint level)
+{
+   ASSERT(texUnit);
+   switch (target) {
+      case GL_TEXTURE_1D:
+         return texUnit->CurrentD[1]->Image[level];
+      case GL_PROXY_TEXTURE_1D:
+         return ctx->Texture.Proxy1D->Image[level];
+      case GL_TEXTURE_2D:
+         return texUnit->CurrentD[2]->Image[level];
+      case GL_PROXY_TEXTURE_2D:
+         return ctx->Texture.Proxy2D->Image[level];
+      case GL_TEXTURE_3D:
+         return texUnit->CurrentD[3]->Image[level];
+      case GL_PROXY_TEXTURE_3D:
+         return ctx->Texture.Proxy3D->Image[level];
+      case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
+         if (ctx->Extensions.HaveTextureCubeMap)
+            return texUnit->CurrentCubeMap->Image[level];
+         else
+            return NULL;
+      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
+         if (ctx->Extensions.HaveTextureCubeMap)
+            return texUnit->CurrentCubeMap->NegX[level];
+         else
+            return NULL;
+      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
+         if (ctx->Extensions.HaveTextureCubeMap)
+            return texUnit->CurrentCubeMap->PosY[level];
+         else
+            return NULL;
+      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
+         if (ctx->Extensions.HaveTextureCubeMap)
+            return texUnit->CurrentCubeMap->NegY[level];
+         else
+            return NULL;
+      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
+         if (ctx->Extensions.HaveTextureCubeMap)
+            return texUnit->CurrentCubeMap->PosZ[level];
+         else
+            return NULL;
+      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
+         if (ctx->Extensions.HaveTextureCubeMap)
+            return texUnit->CurrentCubeMap->NegZ[level];
+         else
+            return NULL;
+      case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
+         if (ctx->Extensions.HaveTextureCubeMap)
+            return ctx->Texture.ProxyCubeMap->Image[level];
+         else
+            return NULL;
+      default:
+         gl_problem(ctx, "bad target in _mesa_select_tex_image()");
+         return NULL;
+   }
+}
+
+
+
 /* Need this to prevent an out-of-bounds memory access when using
  * X86 optimized code.
  */
@@ -643,7 +903,10 @@ texture_error_check( GLcontext *ctx, GLenum target,
    }
    else if (dimensions == 2) {
       isProxy = (GLboolean) (target == GL_PROXY_TEXTURE_2D);
-      if (target != GL_TEXTURE_2D && !isProxy) {
+      if (target != GL_TEXTURE_2D && !isProxy &&
+          !(ctx->Extensions.HaveTextureCubeMap &&
+            target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
+            target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
           gl_error( ctx, GL_INVALID_ENUM, "glTexImage2D(target)" );
           return GL_TRUE;
       }
@@ -661,7 +924,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);
@@ -694,6 +957,17 @@ texture_error_check( GLcontext *ctx, GLenum target,
       }
    }
 
+   /* For cube map, width must equal height */
+   if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
+       target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
+      if (width != height) {
+         if (!isProxy) {
+            gl_error(ctx, GL_INVALID_VALUE, "glTexImage2D(width != height)");
+         }
+         return GL_TRUE;
+      }
+   }
+
    /* Depth */
    if (dimensions >= 3) {
       if (depth < 2 * border || depth > 2 + ctx->Const.MaxTextureSize
@@ -706,7 +980,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);
@@ -715,7 +989,7 @@ texture_error_check( GLcontext *ctx, GLenum target,
       return GL_TRUE;
    }
 
-   iformat = decode_internal_format( internalFormat );
+   iformat = _mesa_base_tex_format( ctx, internalFormat );
    if (iformat < 0) {
       if (!isProxy) {
          char message[100];
@@ -725,16 +999,18 @@ texture_error_check( GLcontext *ctx, GLenum target,
       return GL_TRUE;
    }
 
-   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.
-       */
-      if (!isProxy) {
-         char message[100];
-         sprintf(message, "glTexImage%dD(format or type)", dimensions);
-         gl_error(ctx, GL_INVALID_OPERATION, message);
+   if (!is_compressed_format(internalFormat)) {
+      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.
+          */
+         if (!isProxy) {
+            char message[100];
+            sprintf(message, "glTexImage%dD(format or type)", dimensions);
+            gl_error(ctx, GL_INVALID_OPERATION, message);
+         }
+         return GL_TRUE;
       }
-      return GL_TRUE;
    }
 
    /* if we get here, the parameters are OK */
@@ -766,7 +1042,15 @@ subtexture_error_check( GLcontext *ctx, GLuint dimensions,
       }
    }
    else if (dimensions == 2) {
-      if (target != GL_TEXTURE_2D) {
+      if (ctx->Extensions.HaveTextureCubeMap) {
+         if ((target < GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB ||
+              target > GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) &&
+             target != GL_TEXTURE_2D) {
+            gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
+            return GL_TRUE;
+         }
+      }
+      else if (target != GL_TEXTURE_2D) {
          gl_error( ctx, GL_INVALID_ENUM, "glTexSubImage2D(target)" );
          return GL_TRUE;
       }
@@ -841,11 +1125,13 @@ subtexture_error_check( GLcontext *ctx, GLuint dimensions,
       }
    }
 
-   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);
-      return GL_TRUE;
+   if (!is_compressed_format(destTex->IntFormat)) {
+      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);
+         return GL_TRUE;
+      }
    }
 
    return GL_FALSE;
@@ -864,18 +1150,25 @@ copytexture_error_check( GLcontext *ctx, GLuint dimensions,
 {
    GLint iformat;
 
-   if (target != GL_TEXTURE_1D && target != GL_TEXTURE_2D) {
-      gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1/2D(target)" );
-      return GL_TRUE;
-   }
-
-   if (dimensions == 1 && target != GL_TEXTURE_1D) {
-      gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1D(target)" );
-      return GL_TRUE;
+   if (dimensions == 1) {
+      if (target != GL_TEXTURE_1D) {
+         gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage1D(target)" );
+         return GL_TRUE;
+      }
    }
-   else if (dimensions == 2 && target != GL_TEXTURE_2D) {
-      gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
-      return GL_TRUE;
+   else if (dimensions == 2) {
+      if (ctx->Extensions.HaveTextureCubeMap) {
+         if ((target < GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB ||
+              target > GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) &&
+             target != GL_TEXTURE_2D) {
+            gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
+            return GL_TRUE;
+         }
+      }
+      else if (target != GL_TEXTURE_2D) {
+         gl_error( ctx, GL_INVALID_ENUM, "glCopyTexImage2D(target)" );
+         return GL_TRUE;
+      }
    }
 
    /* Border */
@@ -906,6 +1199,15 @@ copytexture_error_check( GLcontext *ctx, GLuint dimensions,
       }
    }
 
+   /* For cube map, width must equal height */
+   if (target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
+       target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
+      if (width != height) {
+         gl_error(ctx, GL_INVALID_VALUE, "glCopyTexImage2D(width != height)");
+         return GL_TRUE;
+      }
+   }
+
    /* Level */
    if (level<0 || level>=ctx->Const.MaxTextureLevels) {
       char message[100];
@@ -914,7 +1216,7 @@ copytexture_error_check( GLcontext *ctx, GLuint dimensions,
       return GL_TRUE;
    }
 
-   iformat = decode_internal_format( internalFormat );
+   iformat = _mesa_base_tex_format( ctx, internalFormat );
    if (iformat < 0) {
       char message[100];
       sprintf(message, "glCopyTexImage%dD(internalFormat)", dimensions);
@@ -936,17 +1238,31 @@ copytexsubimage_error_check( GLcontext *ctx, GLuint dimensions,
    struct gl_texture_unit *texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
    struct gl_texture_image *teximage;
 
-   if (dimensions == 1 && target != GL_TEXTURE_1D) {
-      gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage1D(target)" );
-      return GL_TRUE;
+   if (dimensions == 1) {
+      if (target != GL_TEXTURE_1D) {
+         gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage1D(target)" );
+         return GL_TRUE;
+      }
    }
-   else if (dimensions == 2 && target != GL_TEXTURE_2D) {
-      gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
-      return GL_TRUE;
+   else if (dimensions == 2) {
+      if (ctx->Extensions.HaveTextureCubeMap) {
+         if ((target < GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB ||
+              target > GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) &&
+             target != GL_TEXTURE_2D) {
+            gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
+            return GL_TRUE;
+         }
+      }
+      else if (target != GL_TEXTURE_2D) {
+         gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage2D(target)" );
+         return GL_TRUE;
+      }
    }
-   else if (dimensions == 3 && target != GL_TEXTURE_3D) {
-      gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage3D(target)" );
-      return GL_TRUE;
+   else if (dimensions == 3) {
+      if (target != GL_TEXTURE_3D) {
+         gl_error( ctx, GL_INVALID_ENUM, "glCopyTexSubImage3D(target)" );
+         return GL_TRUE;
+      }
    }
 
    if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
@@ -1040,59 +1356,91 @@ _mesa_TexImage1D( GLenum target, GLint level, GLint internalFormat,
 
    if (target==GL_TEXTURE_1D) {
       struct gl_texture_unit *texUnit;
-      struct gl_texture_image *teximage;
+      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 )) {
+      if (texture_error_check(ctx, target, level, internalFormat,
+                              format, type, 1, width, 1, 1, border)) {
          return;   /* error in texture image was detected */
       }
 
       texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
-
-      /* free current texture image, if any */
-      if (texUnit->CurrentD[1]->Image[level]) {
-         gl_free_texture_image( texUnit->CurrentD[1]->Image[level] );
+      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;
       }
 
-      teximage = new_texture_image(width, 1, 1, border, internalFormat);
+      /* setup the teximage struct's fields */
+      init_texture_image(texImage, width, 1, 1, border, internalFormat);
 
-      /* make new texture from source image */
+      /* process the texture image */
       if (pixels) {
-         make_texture_image(ctx, teximage, 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 {
-         make_null_texture(teximage);
+         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,
-                               format, type, 1, width, 1, 1, border )) {
+      if (texture_error_check(ctx, target, level, internalFormat,
+                              format, type, 1, width, 1, 1, border)) {
+         /* if error, clear all proxy texture image parameters */
          if (level>=0 && level<ctx->Const.MaxTextureLevels) {
             MEMSET( ctx->Texture.Proxy1D->Image[level], 0,
                     sizeof(struct gl_texture_image) );
          }
       }
       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]->Border = border;
-         ctx->Texture.Proxy1D->Image[level]->Width = width;
-         ctx->Texture.Proxy1D->Image[level]->Height = 1;
-         ctx->Texture.Proxy1D->Image[level]->Depth = 1;
+         /* if no error, update proxy texture image parameters */
+         init_texture_image(ctx->Texture.Proxy1D->Image[level],
+                            width, 1, 1, border, internalFormat);
       }
    }
    else {
@@ -1111,61 +1459,106 @@ _mesa_TexImage2D( GLenum target, GLint level, GLint internalFormat,
    GET_CURRENT_CONTEXT(ctx);
    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glTexImage2D");
 
-   if (target==GL_TEXTURE_2D) {
+   if (target==GL_TEXTURE_2D ||
+       (ctx->Extensions.HaveTextureCubeMap &&
+        target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
+        target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
       struct gl_texture_unit *texUnit;
-      struct gl_texture_image *teximage;
+      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 )) {
+      if (texture_error_check(ctx, target, level, internalFormat,
+                              format, type, 2, width, height, 1, border)) {
          return;   /* error in texture image was detected */
       }
 
       texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
-
-      /* free current texture image, if any */
-      if (texUnit->CurrentD[2]->Image[level]) {
-         gl_free_texture_image( texUnit->CurrentD[2]->Image[level] );
+      texObj = _mesa_select_tex_object(ctx, texUnit, target);
+      texImage = _mesa_select_tex_image(ctx, texUnit, target, level);
+
+      if (!texImage) {
+         texImage = _mesa_alloc_texture_image();
+         set_tex_image(texObj, target, level, texImage);
+         /*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;
       }
 
-      teximage = new_texture_image(width, height, 1, border,internalFormat);
+      /* setup the teximage struct's fields */
+      init_texture_image(texImage, width, height, 1, border, internalFormat);
 
-      /* make new texture from source image */
+      /* process the texture image */
       if (pixels) {
-         make_texture_image(ctx, teximage, 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 {
-         make_null_texture(teximage);
+         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,
-                               format, type, 2, width, height, 1, border )) {
+      if (texture_error_check(ctx, target, level, internalFormat,
+                              format, type, 2, width, height, 1, border)) {
+         /* if error, clear all proxy texture image parameters */
          if (level>=0 && level<ctx->Const.MaxTextureLevels) {
             MEMSET( ctx->Texture.Proxy2D->Image[level], 0,
                     sizeof(struct gl_texture_image) );
          }
       }
       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]->Border = border;
-         ctx->Texture.Proxy2D->Image[level]->Width = width;
-         ctx->Texture.Proxy2D->Image[level]->Height = height;
-         ctx->Texture.Proxy2D->Image[level]->Depth = 1;
+         /* if no error, update proxy texture image parameters */
+         init_texture_image(ctx->Texture.Proxy1D->Image[level],
+                            width, height, 1, border, internalFormat);
       }
    }
    else {
@@ -1191,61 +1584,91 @@ _mesa_TexImage3D( GLenum target, GLint level, GLint internalFormat,
 
    if (target==GL_TEXTURE_3D_EXT) {
       struct gl_texture_unit *texUnit;
-      struct gl_texture_image *teximage;
-      if (texture_error_check( ctx, target, level, internalFormat,
-                               format, type, 3, width, height, depth,
-                               border )) {
+      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)) {
          return;   /* error in texture image was detected */
       }
 
       texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
-
-      /* free current texture image, if any */
-      if (texUnit->CurrentD[3]->Image[level]) {
-         gl_free_texture_image( texUnit->CurrentD[3]->Image[level] );
+      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;
       }
 
-      teximage = new_texture_image(width, height, depth,
-                                   border, internalFormat);
+      /* setup the teximage struct's fields */
+      init_texture_image(texImage, width, height, depth,
+                         border, internalFormat);
 
-      /* make new texture from source image */
+      /* process the texture image */
       if (pixels) {
-         make_texture_image(ctx, teximage, 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 {
-         make_null_texture(teximage);
+         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) {
+   else if (target==GL_PROXY_TEXTURE_3D) {
       /* Proxy texture: check for errors and update proxy state */
-      if (texture_error_check( ctx, target, level, internalFormat,
-                               format, type, 3, width, height, depth,
-                               border )) {
+      if (texture_error_check(ctx, target, level, internalFormat,
+                              format, type, 3, width, height, depth, border)) {
+         /* if error, clear all proxy texture image parameters */
          if (level>=0 && level<ctx->Const.MaxTextureLevels) {
             MEMSET( ctx->Texture.Proxy3D->Image[level], 0,
                     sizeof(struct gl_texture_image) );
          }
       }
       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]->Border = border;
-         ctx->Texture.Proxy3D->Image[level]->Width = width;
-         ctx->Texture.Proxy3D->Image[level]->Height = height;
-         ctx->Texture.Proxy3D->Image[level]->Depth  = depth;
+         /* if no error, update proxy texture image parameters */
+         init_texture_image(ctx->Texture.Proxy1D->Image[level],
+                            width, height, depth, border, internalFormat);
       }
    }
    else {
@@ -1270,9 +1693,9 @@ _mesa_TexImage3DEXT( GLenum target, GLint level, GLenum internalFormat,
  * Fetch a texture image from the device driver.
  * Store the results in the given texture object at the given mipmap level.
  */
-static void
-get_teximage_from_driver( GLcontext *ctx, GLenum target, GLint level,
-                          const struct gl_texture_object *texObj )
+void
+_mesa_get_teximage_from_driver( GLcontext *ctx, GLenum target, GLint level,
+                                const struct gl_texture_object *texObj )
 {
    GLvoid *image;
    GLenum imgFormat, imgType;
@@ -1283,7 +1706,7 @@ get_teximage_from_driver( GLcontext *ctx, GLenum target, GLint level,
    if (!ctx->Driver.GetTexImage)
       return;
 
-   image = (*ctx->Driver.GetTexImage)( ctx, target, level,
+   image = (*ctx->Driver.GetTexImage)( ctx, target, level, texObj,
                                        &imgFormat, &imgType, &freeImage);
    if (!image)
       return;
@@ -1332,7 +1755,7 @@ get_teximage_from_driver( GLcontext *ctx, GLenum target, GLint level,
          for (img = 0; img < depth; img++) {
             for (row = 0; row < height; row++) {
                _mesa_unpack_index_span(ctx, width, dstType, destPtr,
-                                   imgType, srcPtr, &DefaultPacking, GL_FALSE);
+                             imgType, srcPtr, &_mesa_native_packing, GL_FALSE);
                destPtr += destBytesPerRow;
                srcPtr += srcBytesPerRow;
             }
@@ -1344,7 +1767,7 @@ get_teximage_from_driver( GLcontext *ctx, GLenum target, GLint level,
          for (img = 0; img < depth; img++) {
             for (row = 0; row < height; row++) {
                _mesa_unpack_ubyte_color_span(ctx, width, dstFormat, destPtr,
-                        imgFormat, imgType, srcPtr, &DefaultPacking, GL_FALSE);
+                  imgFormat, imgType, srcPtr, &_mesa_native_packing, GL_FALSE);
                destPtr += destBytesPerRow;
                srcPtr += srcBytesPerRow;
             }
@@ -1389,19 +1812,45 @@ _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
    switch (target) {
       case GL_TEXTURE_1D:
          texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentD[1];
+         texImage = texObj->Image[level];
          break;
       case GL_TEXTURE_2D:
          texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentD[2];
+         texImage = texObj->Image[level];
+         break;
+      case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
+         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentCubeMap;
+         texImage = texObj->Image[level];
+         break;
+      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
+         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentCubeMap;
+         texImage = texObj->NegX[level];
+         break;
+      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
+         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentCubeMap;
+         texImage = texObj->PosY[level];
+         break;
+      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
+         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentCubeMap;
+         texImage = texObj->NegY[level];
+         break;
+      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
+         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentCubeMap;
+         texImage = texObj->PosZ[level];
+         break;
+      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
+         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentCubeMap;
+         texImage = texObj->NegZ[level];
          break;
       case GL_TEXTURE_3D:
          texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentD[3];
+         texImage = texObj->Image[level];
          break;
       default:
          gl_error( ctx, GL_INVALID_ENUM, "glGetTexImage(target)" );
          return;
    }
 
-   texImage = texObj->Image[level];
    if (!texImage) {
       /* invalid mipmap level */
       return;
@@ -1409,7 +1858,7 @@ _mesa_GetTexImage( GLenum target, GLint level, GLenum format,
 
    if (!texImage->Data) {
       /* try to get the texture image from the device driver */
-      get_teximage_from_driver(ctx, target, level, texObj);
+      _mesa_get_teximage_from_driver(ctx, target, level, texObj);
       discardImage = GL_TRUE;
    }
    else {
@@ -1516,64 +1965,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)) {
       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 = _mesa_image_address(&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 = _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);
+         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] );*/
 }
 
 
@@ -1585,75 +2048,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)) {
       return;   /* error was detected */
    }
 
-   destTex = texUnit->CurrentD[2]->Image[level];
-   assert(destTex);
+   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
+   texObj = _mesa_select_tex_object(ctx, texUnit, target);
+   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 = _mesa_image_address(&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 = _mesa_image_address(&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
    }
 }
 
@@ -1667,70 +2159,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)) {
       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 = _mesa_image_address(&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 = _mesa_image_address(&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;
+      }
+   }
 }
 
 
@@ -1758,6 +2270,8 @@ read_color_image( GLcontext *ctx, GLint x, GLint y,
    (*ctx->Driver.SetReadBuffer)( ctx, ctx->ReadBuffer,
                                  ctx->Pixel.DriverReadBuffer );
 
+   /* XXX TODO we have to apply pixel transfer ops here! */
+
    dst = image;
    stride = width * 4 * sizeof(GLubyte);
    for (i = 0; i < height; i++) {
@@ -1793,13 +2307,22 @@ _mesa_CopyTexImage1D( GLenum target, GLint level,
        || !(*ctx->Driver.CopyTexImage1D)(ctx, target, level,
                          internalFormat, x, y, width, border))
    {
+      struct gl_pixelstore_attrib unpackSave;
+
+      /* get image from framebuffer */
       GLubyte *image  = read_color_image( ctx, x, y, width, 1 );
       if (!image) {
          gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexImage1D" );
          return;
       }
+
+      /* call glTexImage1D to redefine the texture */
+      unpackSave = ctx->Unpack;
+      ctx->Unpack = _mesa_native_packing;
       (*ctx->Exec->TexImage1D)( target, level, internalFormat, width,
                                 border, GL_RGBA, GL_UNSIGNED_BYTE, image );
+      ctx->Unpack = unpackSave;
+
       FREE(image);
    }
 }
@@ -1823,76 +2346,28 @@ _mesa_CopyTexImage2D( GLenum target, GLint level, GLenum internalFormat,
        || !(*ctx->Driver.CopyTexImage2D)(ctx, target, level,
                          internalFormat, x, y, width, height, border))
    {
+      struct gl_pixelstore_attrib unpackSave;
+
+      /* get image from framebuffer */
       GLubyte *image  = read_color_image( ctx, x, y, width, height );
       if (!image) {
          gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexImage2D" );
          return;
       }
+
+      /* call glTexImage2D to redefine the texture */
+      unpackSave = ctx->Unpack;
+      ctx->Unpack = _mesa_native_packing;
       (ctx->Exec->TexImage2D)( target, level, internalFormat, width,
                       height, border, GL_RGBA, GL_UNSIGNED_BYTE, image );
+      ctx->Unpack = unpackSave;
+
       FREE(image);
    }
 }
 
 
 
-/*
- * Do the work of glCopyTexSubImage[123]D.
- */
-static void
-copy_tex_sub_image( GLcontext *ctx, struct gl_texture_image *dest,
-                    GLint width, GLint height,
-                    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;
-
-   /* dst[xyz] may be negative if we have a texture border! */
-   dstx += dest->Border;
-   dsty += dest->Border;
-   dstz += dest->Border;
-   texwidth = dest->Width;
-   texheight = dest->Height;
-   rectarea = texwidth * texheight;
-   zoffset = dstz * rectarea; 
-   format = dest->Format;
-   components = components_in_intformat( format );
-
-   /* Select buffer to read from */
-   (*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, 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);
-   }
-
-   /* Read from draw buffer (the default) */
-   (*ctx->Driver.SetReadBuffer)( ctx, ctx->DrawBuffer,
-                                 ctx->Color.DriverDrawBuffer );
-}
-
-
-
-
 void
 _mesa_CopyTexSubImage1D( GLenum target, GLint level,
                          GLint xoffset, GLint x, GLint y, GLsizei width )
@@ -1910,18 +2385,28 @@ _mesa_CopyTexSubImage1D( GLenum target, GLint level,
                                             xoffset, x, y, width)) {
       struct gl_texture_unit *texUnit;
       struct gl_texture_image *teximage;
+      struct gl_pixelstore_attrib unpackSave;
+      GLubyte *image;
+
       texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
       teximage = texUnit->CurrentD[1]->Image[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 );
-         }
+
+      /* get image from frame buffer */
+      image = read_color_image(ctx, x, y, width, 1);
+      if (!image) {
+         gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage2D" );
+         return;
       }
+      
+      /* now call glTexSubImage1D to do the real work */
+      unpackSave = ctx->Unpack;
+      ctx->Unpack = _mesa_native_packing;
+      _mesa_TexSubImage1D(target, level, xoffset, width,
+                          GL_RGBA, GL_UNSIGNED_BYTE, image);
+      ctx->Unpack = unpackSave;
+
+      FREE(image);
    }
 }
 
@@ -1945,19 +2430,28 @@ _mesa_CopyTexSubImage2D( GLenum target, GLint level,
                                 xoffset, yoffset, x, y, width, height )) {
       struct gl_texture_unit *texUnit;
       struct gl_texture_image *teximage;
+      struct gl_pixelstore_attrib unpackSave;
+      GLubyte *image;
+
       texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
       teximage = texUnit->CurrentD[2]->Image[level];
       assert(teximage);
-      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 );
-        }
+
+      /* get image from frame buffer */
+      image = read_color_image(ctx, x, y, width, height);
+      if (!image) {
+         gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage2D" );
+         return;
       }
+
+      /* now call glTexSubImage2D to do the real work */
+      unpackSave = ctx->Unpack;
+      ctx->Unpack = _mesa_native_packing;
+      _mesa_TexSubImage2D(target, level, xoffset, yoffset, width, height,
+                          GL_RGBA, GL_UNSIGNED_BYTE, image);
+      ctx->Unpack = unpackSave;
+      
+      FREE(image);
    }
 }
 
@@ -1975,24 +2469,521 @@ _mesa_CopyTexSubImage3D( GLenum target, GLint level,
                     xoffset, yoffset, zoffset, width, height))
       return;
 
-    if (ctx->Pixel.MapColorFlag || ctx->Pixel.ScaleOrBiasRGBA
+   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];
-       teximage = texUnit->CurrentD[3]->Image[level];
-       assert(teximage);
-       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 );
+                     xoffset, yoffset, zoffset, x, y, width, height )) {
+      struct gl_texture_unit *texUnit;
+      struct gl_texture_image *teximage;
+      struct gl_pixelstore_attrib unpackSave;
+      GLubyte *image;
+
+      texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
+      teximage = texUnit->CurrentD[3]->Image[level];
+      assert(teximage);
+
+      /* get image from frame buffer */
+      image = read_color_image(ctx, x, y, width, height);
+      if (!image) {
+         gl_error( ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage2D" );
+         return;
+      }
+
+      /* now call glTexSubImage2D to do the real work */
+      unpackSave = ctx->Unpack;
+      ctx->Unpack = _mesa_native_packing;
+      _mesa_TexSubImage3D(target, level, xoffset, yoffset, zoffset,
+                          width, height, 1, GL_RGBA, GL_UNSIGNED_BYTE, image);
+      ctx->Unpack = unpackSave;
+      
+      FREE(image);
+   }
+}
+
+
+
+void
+_mesa_CompressedTexImage1DARB(GLenum target, GLint level,
+                              GLenum internalFormat, GLsizei width,
+                              GLint border, GLsizei imageSize,
+                              const GLvoid *data)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCompressedTexImage1DARB");
+
+   if (target == GL_TEXTURE_1D) {
+      struct gl_texture_unit *texUnit;
+      struct gl_texture_object *texObj;
+      struct gl_texture_image *texImage;
+
+      if (texture_error_check(ctx, target, level, internalFormat,
+                              GL_NONE, GL_NONE, 1, width, 1, 1, border)) {
+         return;   /* error in texture image was detected */
+      }
+
+      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, "glCompressedTexImage1DARB");
+            return;
+         }
+      }
+      else if (texImage->Data) {
+         FREE(texImage->Data);
+         texImage->Data = NULL;
+      }
+
+      /* setup the teximage struct's fields */
+      init_texture_image(texImage, width, 1, 1, border, internalFormat);
+
+      /* process the texture image */
+      if (data) {
+         GLboolean retain = GL_TRUE;
+         GLboolean success = GL_FALSE;
+         if (ctx->Driver.CompressedTexImage1D) {
+            success = (*ctx->Driver.CompressedTexImage1D)( ctx, target, level,
+                                              data, texObj, texImage, &retain);
+         }
+         if (retain || !success) {
+            /* make internal copy of the texture image */
+            GLuint imageSize = _mesa_compressed_image_size(internalFormat,
+                                                           width, 1, 1);
+            texImage->Data = MALLOC(imageSize);
+            if (texImage->Data) {
+               MEMCPY(texImage->Data, data, imageSize);
+            }
+         }
+         if (!retain && texImage->Data) {
+            FREE(texImage->Data);
+            texImage->Data = NULL;
+         }
+      }
+      else {
+         make_null_texture(texImage);
+         if (ctx->Driver.CompressedTexImage1D) {
+            GLboolean retain;
+            (*ctx->Driver.CompressedTexImage1D)( ctx, target, level,
+                                    texImage->Data, texObj, texImage, &retain);
+         }
+      }
+
+      /* state update */
+      gl_put_texobj_on_dirty_list( ctx, texObj );
+      ctx->NewState |= NEW_TEXTURING;
+   }
+   else if (target == GL_PROXY_TEXTURE_1D) {
+      /* Proxy texture: check for errors and update proxy state */
+      if (texture_error_check(ctx, target, level, internalFormat,
+                              GL_NONE, GL_NONE, 1, width, 1, 1, border)) {
+         /* if error, clear all proxy texture image parameters */
+         if (level>=0 && level<ctx->Const.MaxTextureLevels) {
+            MEMSET( ctx->Texture.Proxy1D->Image[level], 0,
+                    sizeof(struct gl_texture_image) );
+         }
+      }
+      else {
+         /* if no error, update proxy texture image parameters */
+         init_texture_image(ctx->Texture.Proxy1D->Image[level],
+                            width, 1, 1, border, internalFormat);
+      }
+   }
+   else {
+      gl_error( ctx, GL_INVALID_ENUM, "glCompressedTexImage1DARB(target)" );
+      return;
+   }
+}
+
+
+void
+_mesa_CompressedTexImage2DARB(GLenum target, GLint level,
+                              GLenum internalFormat, GLsizei width,
+                              GLsizei height, GLint border, GLsizei imageSize,
+                              const GLvoid *data)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCompressedTexImage2DARB");
+
+   if (target==GL_TEXTURE_2D ||
+       (ctx->Extensions.HaveTextureCubeMap &&
+        target >= GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
+        target <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB)) {
+      struct gl_texture_unit *texUnit;
+      struct gl_texture_object *texObj;
+      struct gl_texture_image *texImage;
+
+      if (texture_error_check(ctx, target, level, internalFormat,
+                              GL_NONE, GL_NONE, 1, width, height, 1, border)) {
+         return;   /* error in texture image was detected */
+      }
+
+      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, "glCompressedTexImage2DARB");
+            return;
+         }
+      }
+      else if (texImage->Data) {
+         FREE(texImage->Data);
+         texImage->Data = NULL;
+      }
+
+      /* setup the teximage struct's fields */
+      init_texture_image(texImage, width, height, 1, border, internalFormat);
+
+      /* process the texture image */
+      if (data) {
+         GLboolean retain = GL_TRUE;
+         GLboolean success = GL_FALSE;
+         if (ctx->Driver.CompressedTexImage2D) {
+            success = (*ctx->Driver.CompressedTexImage2D)( ctx, target, level,
+                                              data, texObj, texImage, &retain);
+         }
+         if (retain || !success) {
+            /* make internal copy of the texture image */
+            GLuint imageSize = _mesa_compressed_image_size(internalFormat,
+                                                           width, height, 1);
+            texImage->Data = MALLOC(imageSize);
+            if (texImage->Data) {
+               MEMCPY(texImage->Data, data, imageSize);
+            }
+         }
+         if (!retain && texImage->Data) {
+            FREE(texImage->Data);
+            texImage->Data = NULL;
+         }
+      }
+      else {
+         make_null_texture(texImage);
+         if (ctx->Driver.CompressedTexImage2D) {
+            GLboolean retain;
+            (*ctx->Driver.CompressedTexImage2D)( ctx, target, level,
+                                    texImage->Data, texObj, texImage, &retain);
+         }
+      }
+
+      /* 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,
+                              GL_NONE, GL_NONE, 1, width, 1, 1, border)) {
+         /* if error, clear all proxy texture image parameters */
+         if (level>=0 && level<ctx->Const.MaxTextureLevels) {
+            MEMSET( ctx->Texture.Proxy2D->Image[level], 0,
+                    sizeof(struct gl_texture_image) );
+         }
+      }
+      else {
+         /* if no error, update proxy texture image parameters */
+         init_texture_image(ctx->Texture.Proxy2D->Image[level],
+                            width, 1, 1, border, internalFormat);
+      }
+   }
+   else {
+      gl_error( ctx, GL_INVALID_ENUM, "glCompressedTexImage2DARB(target)" );
+      return;
+   }
+}
+
+
+void
+_mesa_CompressedTexImage3DARB(GLenum target, GLint level,
+                              GLenum internalFormat, GLsizei width,
+                              GLsizei height, GLsizei depth, GLint border,
+                              GLsizei imageSize, const GLvoid *data)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glCompressedTexImage3DARB");
+
+   if (target == GL_TEXTURE_3D) {
+      struct gl_texture_unit *texUnit;
+      struct gl_texture_object *texObj;
+      struct gl_texture_image *texImage;
+
+      if (texture_error_check(ctx, target, level, internalFormat,
+                          GL_NONE, GL_NONE, 1, width, height, depth, border)) {
+         return;   /* error in texture image was detected */
+      }
+
+      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, "glCompressedTexImage3DARB");
+            return;
+         }
+      }
+      else if (texImage->Data) {
+         FREE(texImage->Data);
+         texImage->Data = NULL;
+      }
+
+      /* setup the teximage struct's fields */
+      init_texture_image(texImage, width, height, depth, border, internalFormat);
+
+      /* process the texture image */
+      if (data) {
+         GLboolean retain = GL_TRUE;
+         GLboolean success = GL_FALSE;
+         if (ctx->Driver.CompressedTexImage3D) {
+            success = (*ctx->Driver.CompressedTexImage3D)( ctx, target, level,
+                                              data, texObj, texImage, &retain);
+         }
+         if (retain || !success) {
+            /* make internal copy of the texture image */
+            GLuint imageSize = _mesa_compressed_image_size(internalFormat,
+                                                         width, height, depth);
+            texImage->Data = MALLOC(imageSize);
+            if (texImage->Data) {
+               MEMCPY(texImage->Data, data, imageSize);
+            }
+         }
+         if (!retain && texImage->Data) {
+            FREE(texImage->Data);
+            texImage->Data = NULL;
          }
       }
+      else {
+         make_null_texture(texImage);
+         if (ctx->Driver.CompressedTexImage3D) {
+            GLboolean retain;
+            (*ctx->Driver.CompressedTexImage3D)( ctx, target, level,
+                                    texImage->Data, texObj, texImage, &retain);
+         }
+      }
+
+      /* state update */
+      gl_put_texobj_on_dirty_list( ctx, texObj );
+      ctx->NewState |= NEW_TEXTURING;
+   }
+   else if (target == GL_PROXY_TEXTURE_3D) {
+      /* Proxy texture: check for errors and update proxy state */
+      if (texture_error_check(ctx, target, level, internalFormat,
+                          GL_NONE, GL_NONE, 1, width, height, depth, border)) {
+         /* if error, clear all proxy texture image parameters */
+         if (level>=0 && level<ctx->Const.MaxTextureLevels) {
+            MEMSET( ctx->Texture.Proxy3D->Image[level], 0,
+                    sizeof(struct gl_texture_image) );
+         }
+      }
+      else {
+         /* if no error, update proxy texture image parameters */
+         init_texture_image(ctx->Texture.Proxy3D->Image[level],
+                            width, 1, 1, border, internalFormat);
+      }
+   }
+   else {
+      gl_error( ctx, GL_INVALID_ENUM, "glCompressedTexImage3DARB(target)" );
+      return;
+   }
+}
+
+
+void
+_mesa_CompressedTexSubImage1DARB(GLenum target, GLint level, GLint xoffset,
+                                 GLsizei width, GLenum format,
+                                 GLsizei imageSize, const GLvoid *data)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   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, GL_NONE)) {
+      return;   /* error was detected */
+   }
+
+   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
+   texObj = _mesa_select_tex_object(ctx, texUnit, target);
+   texImage = texObj->Image[level];
+   assert(texImage);
+
+   if (width == 0 || !data)
+      return;  /* no-op, not an error */
+
+   if (ctx->Driver.CompressedTexSubImage1D) {
+      success = (*ctx->Driver.CompressedTexSubImage1D)(ctx, target, level,
+                   xoffset, width, format, imageSize, data, texObj, texImage);
+   }
+   if (!success) {
+      /* XXX what else can we do? */
+      gl_problem(ctx, "glCompressedTexSubImage1DARB failed!");
+      return;
+   }
+
+}
+
+
+void
+_mesa_CompressedTexSubImage2DARB(GLenum target, GLint level, GLint xoffset,
+                                 GLint yoffset, GLsizei width, GLsizei height,
+                                 GLenum format, GLsizei imageSize,
+                                 const GLvoid *data)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   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, GL_NONE)) {
+      return;   /* error was detected */
+   }
+
+   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
+   texObj = _mesa_select_tex_object(ctx, texUnit, target);
+   texImage = texObj->Image[level];
+   assert(texImage);
+
+   if (width == 0 || height == 0 || !data)
+      return;  /* no-op, not an error */
+
+   if (ctx->Driver.CompressedTexSubImage2D) {
+      success = (*ctx->Driver.CompressedTexSubImage2D)(ctx, target, level,
+                                       xoffset, yoffset, width, height, format,
+                                       imageSize, data, texObj, texImage);
+   }
+   if (!success) {
+      /* XXX what else can we do? */
+      gl_problem(ctx, "glCompressedTexSubImage2DARB failed!");
+      return;
+   }
+}
+
+
+void
+_mesa_CompressedTexSubImage3DARB(GLenum target, GLint level, GLint xoffset,
+                                 GLint yoffset, GLint zoffset, GLsizei width,
+                                 GLsizei height, GLsizei depth, GLenum format,
+                                 GLsizei imageSize, const GLvoid *data)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   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, GL_NONE)) {
+      return;   /* error was detected */
+   }
+
+   texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
+   texObj = _mesa_select_tex_object(ctx, texUnit, target);
+   texImage = texObj->Image[level];
+   assert(texImage);
+
+   if (width == 0 || height == 0 || depth == 0 || !data)
+      return;  /* no-op, not an error */
+
+   if (ctx->Driver.CompressedTexSubImage3D) {
+      success = (*ctx->Driver.CompressedTexSubImage3D)(ctx, target, level,
+                               xoffset, yoffset, zoffset, width, height, depth,
+                               format, imageSize, data, texObj, texImage);
+   }
+   if (!success) {
+      /* XXX what else can we do? */
+      gl_problem(ctx, "glCompressedTexSubImage3DARB failed!");
+      return;
+   }
+}
+
+
+void
+_mesa_GetCompressedTexImageARB(GLenum target, GLint level, GLvoid *img)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   const struct gl_texture_object *texObj;
+   struct gl_texture_image *texImage;
+
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetCompressedTexImageARB");
+
+   if (level < 0 || level >= ctx->Const.MaxTextureLevels) {
+      gl_error( ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)" );
+      return;
+   }
+
+   switch (target) {
+      case GL_TEXTURE_1D:
+         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentD[1];
+         texImage = texObj->Image[level];
+         break;
+      case GL_TEXTURE_2D:
+         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentD[2];
+         texImage = texObj->Image[level];
+         break;
+      case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
+         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentCubeMap;
+         texImage = texObj->Image[level];
+         break;
+      case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
+         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentCubeMap;
+         texImage = texObj->NegX[level];
+         break;
+      case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
+         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentCubeMap;
+         texImage = texObj->PosY[level];
+         break;
+      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
+         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentCubeMap;
+         texImage = texObj->NegY[level];
+         break;
+      case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
+         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentCubeMap;
+         texImage = texObj->PosZ[level];
+         break;
+      case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
+         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentCubeMap;
+         texImage = texObj->NegZ[level];
+         break;
+      case GL_TEXTURE_3D:
+         texObj = ctx->Texture.Unit[ctx->Texture.CurrentUnit].CurrentD[3];
+         texImage = texObj->Image[level];
+         break;
+      default:
+         gl_error(ctx, GL_INVALID_ENUM, "glGetCompressedTexImageARB(target)");
+         return;
+   }
+
+   if (!texImage) {
+      /* invalid mipmap level */
+      gl_error(ctx, GL_INVALID_VALUE, "glGetCompressedTexImageARB(level)");
+      return;
+   }
+
+   if (!texImage->IsCompressed) {
+      gl_error(ctx, GL_INVALID_OPERATION, "glGetCompressedTexImageARB");
+      return;
+   }
+
+   if (!img)
+      return;
+
+   if (ctx->Driver.GetCompressedTexImage) {
+      (*ctx->Driver.GetCompressedTexImage)(ctx, target, level, img, texObj,
+                                           texImage);
+   }
+   else {
+      gl_problem(ctx, "Driver doesn't implement GetCompressedTexImage");
    }
 }