Merge branch 'mesa_7_5_branch'
[mesa.git] / src / mesa / drivers / dri / r200 / r200_tex.c
index 256fc8dac808d362b41e55febfa804b0e92d37f0..259f35a34c3638c223821e72b675b7f5035135f1 100644 (file)
@@ -1,4 +1,3 @@
-/* $XFree86: xc/lib/GL/mesa/src/drv/r200/r200_tex.c,v 1.2 2002/11/05 17:46:08 tsi Exp $ */
 /*
 Copyright (C) The Weather Channel, Inc.  2002.  All Rights Reserved.
 
@@ -32,18 +31,19 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  *   Keith Whitwell <keith@tungstengraphics.com>
  */
 
-#include "glheader.h"
-#include "imports.h"
-#include "colormac.h"
-#include "context.h"
-#include "enums.h"
-#include "image.h"
-#include "simple_list.h"
-#include "texformat.h"
-#include "texstore.h"
-#include "texutil.h"
+#include "main/glheader.h"
+#include "main/imports.h"
+#include "main/colormac.h"
+#include "main/context.h"
+#include "main/enums.h"
+#include "main/image.h"
+#include "main/simple_list.h"
+#include "main/texformat.h"
+#include "main/texstore.h"
+#include "main/teximage.h"
+#include "main/texobj.h"
+
 #include "texmem.h"
-#include "teximage.h"
 
 #include "r200_context.h"
 #include "r200_state.h"
@@ -88,44 +88,54 @@ static void r200SetTexWrap( r200TexObjPtr t, GLenum swrap, GLenum twrap, GLenum
    case GL_MIRRORED_REPEAT:
       t->pp_txfilter |= R200_CLAMP_S_MIRROR;
       break;
-   case GL_MIRROR_CLAMP_ATI:
+   case GL_MIRROR_CLAMP_EXT:
       t->pp_txfilter |= R200_CLAMP_S_MIRROR_CLAMP_GL;
       is_clamp = GL_TRUE;
       break;
-   case GL_MIRROR_CLAMP_TO_EDGE_ATI:
+   case GL_MIRROR_CLAMP_TO_EDGE_EXT:
       t->pp_txfilter |= R200_CLAMP_S_MIRROR_CLAMP_LAST;
       break;
+   case GL_MIRROR_CLAMP_TO_BORDER_EXT:
+      t->pp_txfilter |= R200_CLAMP_S_MIRROR_CLAMP_GL;
+      is_clamp_to_border = GL_TRUE;
+      break;
    default:
       _mesa_problem(NULL, "bad S wrap mode in %s", __FUNCTION__);
    }
 
-   switch ( twrap ) {
-   case GL_REPEAT:
-      t->pp_txfilter |= R200_CLAMP_T_WRAP;
-      break;
-   case GL_CLAMP:
-      t->pp_txfilter |= R200_CLAMP_T_CLAMP_GL;
-      is_clamp = GL_TRUE;
-      break;
-   case GL_CLAMP_TO_EDGE:
-      t->pp_txfilter |= R200_CLAMP_T_CLAMP_LAST;
-      break;
-   case GL_CLAMP_TO_BORDER:
-      t->pp_txfilter |= R200_CLAMP_T_CLAMP_GL | R200_BORDER_MODE_D3D;
-      is_clamp_to_border = GL_TRUE;
-      break;
-   case GL_MIRRORED_REPEAT:
-      t->pp_txfilter |= R200_CLAMP_T_MIRROR;
-      break;
-   case GL_MIRROR_CLAMP_ATI:
-      t->pp_txfilter |= R200_CLAMP_T_MIRROR_CLAMP_GL;
-      is_clamp = GL_TRUE;
-      break;
-   case GL_MIRROR_CLAMP_TO_EDGE_ATI:
-      t->pp_txfilter |= R200_CLAMP_T_MIRROR_CLAMP_LAST;
-      break;
-   default:
-      _mesa_problem(NULL, "bad T wrap mode in %s", __FUNCTION__);
+   if (t->base.tObj->Target != GL_TEXTURE_1D) {
+      switch ( twrap ) {
+      case GL_REPEAT:
+         t->pp_txfilter |= R200_CLAMP_T_WRAP;
+         break;
+      case GL_CLAMP:
+         t->pp_txfilter |= R200_CLAMP_T_CLAMP_GL;
+         is_clamp = GL_TRUE;
+         break;
+      case GL_CLAMP_TO_EDGE:
+         t->pp_txfilter |= R200_CLAMP_T_CLAMP_LAST;
+         break;
+      case GL_CLAMP_TO_BORDER:
+         t->pp_txfilter |= R200_CLAMP_T_CLAMP_GL;
+         is_clamp_to_border = GL_TRUE;
+         break;
+      case GL_MIRRORED_REPEAT:
+         t->pp_txfilter |= R200_CLAMP_T_MIRROR;
+         break;
+      case GL_MIRROR_CLAMP_EXT:
+         t->pp_txfilter |= R200_CLAMP_T_MIRROR_CLAMP_GL;
+         is_clamp = GL_TRUE;
+         break;
+      case GL_MIRROR_CLAMP_TO_EDGE_EXT:
+         t->pp_txfilter |= R200_CLAMP_T_MIRROR_CLAMP_LAST;
+         break;
+      case GL_MIRROR_CLAMP_TO_BORDER_EXT:
+         t->pp_txfilter |= R200_CLAMP_T_MIRROR_CLAMP_GL;
+         is_clamp_to_border = GL_TRUE;
+         break;
+      default:
+         _mesa_problem(NULL, "bad T wrap mode in %s", __FUNCTION__);
+      }
    }
 
    t->pp_txformat_x &= ~R200_CLAMP_Q_MASK;
@@ -148,13 +158,17 @@ static void r200SetTexWrap( r200TexObjPtr t, GLenum swrap, GLenum twrap, GLenum
    case GL_MIRRORED_REPEAT:
       t->pp_txformat_x |= R200_CLAMP_Q_MIRROR;
       break;
-   case GL_MIRROR_CLAMP_ATI:
+   case GL_MIRROR_CLAMP_EXT:
       t->pp_txformat_x |= R200_CLAMP_Q_MIRROR_CLAMP_GL;
       is_clamp = GL_TRUE;
       break;
-   case GL_MIRROR_CLAMP_TO_EDGE_ATI:
+   case GL_MIRROR_CLAMP_TO_EDGE_EXT:
       t->pp_txformat_x |= R200_CLAMP_Q_MIRROR_CLAMP_LAST;
       break;
+   case GL_MIRROR_CLAMP_TO_BORDER_EXT:
+      t->pp_txformat_x |= R200_CLAMP_Q_MIRROR_CLAMP_GL;
+      is_clamp_to_border = GL_TRUE;
+      break;
    default:
       _mesa_problem(NULL, "bad R wrap mode in %s", __FUNCTION__);
    }
@@ -170,7 +184,7 @@ static void r200SetTexMaxAnisotropy( r200TexObjPtr t, GLfloat max )
 {
    t->pp_txfilter &= ~R200_MAX_ANISO_MASK;
 
-   if ( max == 1.0 ) {
+   if ( max <= 1.0 ) {
       t->pp_txfilter |= R200_MAX_ANISO_1_TO_1;
    } else if ( max <= 2.0 ) {
       t->pp_txfilter |= R200_MAX_ANISO_2_TO_1;
@@ -253,8 +267,13 @@ static void r200SetTexFilter( r200TexObjPtr t, GLenum minf, GLenum magf )
    }
 }
 
-static void r200SetTexBorderColor( r200TexObjPtr t, GLubyte c[4] )
+static void r200SetTexBorderColor( r200TexObjPtr t, const GLfloat color[4] )
 {
+   GLubyte c[4];
+   CLAMPED_FLOAT_TO_UBYTE(c[0], color[0]);
+   CLAMPED_FLOAT_TO_UBYTE(c[1], color[1]);
+   CLAMPED_FLOAT_TO_UBYTE(c[2], color[2]);
+   CLAMPED_FLOAT_TO_UBYTE(c[3], color[3]);
    t->pp_border_color = r200PackColor( 4, c[0], c[1], c[2], c[3] );
 }
 
@@ -273,7 +292,8 @@ static r200TexObjPtr r200AllocTexObj( struct gl_texture_object *texObj )
    texObj->DriverData = t;
    if ( t != NULL ) {
       if ( R200_DEBUG & DEBUG_TEXTURE ) {
-        fprintf( stderr, "%s( %p, %p )\n", __FUNCTION__, texObj, t );
+        fprintf( stderr, "%s( %p, %p )\n", __FUNCTION__, (void *)texObj, 
+                 (void *)t );
       }
 
       /* Initialize non-image-dependent parts of the state:
@@ -286,12 +306,33 @@ static r200TexObjPtr r200AllocTexObj( struct gl_texture_object *texObj )
       r200SetTexWrap( t, texObj->WrapS, texObj->WrapT, texObj->WrapR );
       r200SetTexMaxAnisotropy( t, texObj->MaxAnisotropy );
       r200SetTexFilter( t, texObj->MinFilter, texObj->MagFilter );
-      r200SetTexBorderColor( t, texObj->_BorderChan );
+      r200SetTexBorderColor( t, texObj->BorderColor );
    }
 
    return t;
 }
 
+/* try to find a format which will only need a memcopy */
+static const struct gl_texture_format *
+r200Choose8888TexFormat( GLenum srcFormat, GLenum srcType )
+{
+   const GLuint ui = 1;
+   const GLubyte littleEndian = *((const GLubyte *) &ui);
+
+   if ((srcFormat == GL_RGBA && srcType == GL_UNSIGNED_INT_8_8_8_8) ||
+       (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE && !littleEndian) ||
+       (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_INT_8_8_8_8_REV) ||
+       (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_BYTE && littleEndian)) {
+      return &_mesa_texformat_rgba8888;
+   }
+   else if ((srcFormat == GL_RGBA && srcType == GL_UNSIGNED_INT_8_8_8_8_REV) ||
+       (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE && littleEndian) ||
+       (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_INT_8_8_8_8) ||
+       (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_BYTE && !littleEndian)) {
+      return &_mesa_texformat_rgba8888_rev;
+   }
+   else return _dri_texformat_argb8888;
+}
 
 static const struct gl_texture_format *
 r200ChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
@@ -311,15 +352,16 @@ r200ChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
       switch ( type ) {
       case GL_UNSIGNED_INT_10_10_10_2:
       case GL_UNSIGNED_INT_2_10_10_10_REV:
-        return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb1555;
+        return do32bpt ? _dri_texformat_argb8888 : _dri_texformat_argb1555;
       case GL_UNSIGNED_SHORT_4_4_4_4:
       case GL_UNSIGNED_SHORT_4_4_4_4_REV:
-        return &_mesa_texformat_argb4444;
+        return _dri_texformat_argb4444;
       case GL_UNSIGNED_SHORT_5_5_5_1:
       case GL_UNSIGNED_SHORT_1_5_5_5_REV:
-        return &_mesa_texformat_argb1555;
+        return _dri_texformat_argb1555;
       default:
-         return do32bpt ? &_mesa_texformat_rgba8888 : &_mesa_texformat_argb4444;
+         return do32bpt ?
+           r200Choose8888TexFormat(format, type) : _dri_texformat_argb4444;
       }
 
    case 3:
@@ -328,15 +370,15 @@ r200ChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
       switch ( type ) {
       case GL_UNSIGNED_SHORT_4_4_4_4:
       case GL_UNSIGNED_SHORT_4_4_4_4_REV:
-        return &_mesa_texformat_argb4444;
+        return _dri_texformat_argb4444;
       case GL_UNSIGNED_SHORT_5_5_5_1:
       case GL_UNSIGNED_SHORT_1_5_5_5_REV:
-        return &_mesa_texformat_argb1555;
+        return _dri_texformat_argb1555;
       case GL_UNSIGNED_SHORT_5_6_5:
       case GL_UNSIGNED_SHORT_5_6_5_REV:
-        return &_mesa_texformat_rgb565;
+        return _dri_texformat_rgb565;
       default:
-         return do32bpt ? &_mesa_texformat_rgba8888 : &_mesa_texformat_rgb565;
+         return do32bpt ? _dri_texformat_argb8888 : _dri_texformat_rgb565;
       }
 
    case GL_RGBA8:
@@ -344,25 +386,25 @@ r200ChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
    case GL_RGBA12:
    case GL_RGBA16:
       return !force16bpt ?
-         &_mesa_texformat_rgba8888 : &_mesa_texformat_argb4444;
+         r200Choose8888TexFormat(format, type) : _dri_texformat_argb4444;
 
    case GL_RGBA4:
    case GL_RGBA2:
-      return &_mesa_texformat_argb4444;
+      return _dri_texformat_argb4444;
 
    case GL_RGB5_A1:
-      return &_mesa_texformat_argb1555;
+      return _dri_texformat_argb1555;
 
    case GL_RGB8:
    case GL_RGB10:
    case GL_RGB12:
    case GL_RGB16:
-      return !force16bpt ? &_mesa_texformat_rgba8888 : &_mesa_texformat_rgb565;
+      return !force16bpt ? _dri_texformat_argb8888 : _dri_texformat_rgb565;
 
    case GL_RGB5:
    case GL_RGB4:
    case GL_R3_G3_B2:
-      return &_mesa_texformat_rgb565;
+      return _dri_texformat_rgb565;
 
    case GL_ALPHA:
    case GL_ALPHA4:
@@ -370,7 +412,9 @@ r200ChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
    case GL_ALPHA12:
    case GL_ALPHA16:
    case GL_COMPRESSED_ALPHA:
-      return &_mesa_texformat_al88;
+   /* can't use a8 format since interpreting hw I8 as a8 would result
+      in wrong rgb values (same as alpha value instead of 0). */
+      return _dri_texformat_al88;
 
    case 1:
    case GL_LUMINANCE:
@@ -379,7 +423,7 @@ r200ChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
    case GL_LUMINANCE12:
    case GL_LUMINANCE16:
    case GL_COMPRESSED_LUMINANCE:
-      return &_mesa_texformat_al88;
+      return _dri_texformat_l8;
 
    case 2:
    case GL_LUMINANCE_ALPHA:
@@ -390,7 +434,7 @@ r200ChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
    case GL_LUMINANCE12_ALPHA12:
    case GL_LUMINANCE16_ALPHA16:
    case GL_COMPRESSED_LUMINANCE_ALPHA:
-      return &_mesa_texformat_al88;
+      return _dri_texformat_al88;
 
    case GL_INTENSITY:
    case GL_INTENSITY4:
@@ -398,21 +442,35 @@ r200ChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
    case GL_INTENSITY12:
    case GL_INTENSITY16:
    case GL_COMPRESSED_INTENSITY:
-      /* At the moment, glean & conform both fail using the i8 internal
-       * format.
-       */
-      return &_mesa_texformat_al88;
-/*       return &_mesa_texformat_i8; */
+       return _dri_texformat_i8;
 
    case GL_YCBCR_MESA:
       if (type == GL_UNSIGNED_SHORT_8_8_APPLE ||
-         type == GL_UNSIGNED_BYTE)
+          type == GL_UNSIGNED_BYTE)
          return &_mesa_texformat_ycbcr;
       else
          return &_mesa_texformat_ycbcr_rev;
 
+   case GL_RGB_S3TC:
+   case GL_RGB4_S3TC:
+   case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
+      return &_mesa_texformat_rgb_dxt1;
+
+   case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
+      return &_mesa_texformat_rgba_dxt1;
+
+   case GL_RGBA_S3TC:
+   case GL_RGBA4_S3TC:
+   case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
+      return &_mesa_texformat_rgba_dxt3;
+
+   case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
+      return &_mesa_texformat_rgba_dxt5;
+
    default:
-      _mesa_problem(ctx, "unexpected texture format in %s", __FUNCTION__);
+      _mesa_problem(ctx,
+         "unexpected internalFormat 0x%x in r200ChooseTextureFormat",
+         (int) internalFormat);
       return NULL;
    }
 
@@ -431,9 +489,8 @@ r200ValidateClientStorage( GLcontext *ctx, GLenum target,
 
 {
    r200ContextPtr rmesa = R200_CONTEXT(ctx);
-   int texelBytes;
 
-   if (0)
+   if ( R200_DEBUG & DEBUG_TEXTURE )
       fprintf(stderr, "intformat %s format %s type %s\n",
              _mesa_lookup_enum_by_nr( internalFormat ),
              _mesa_lookup_enum_by_nr( format ),
@@ -453,8 +510,7 @@ r200ValidateClientStorage( GLcontext *ctx, GLenum target,
    switch ( internalFormat ) {
    case GL_RGBA:
       if ( format == GL_BGRA && type == GL_UNSIGNED_INT_8_8_8_8_REV ) {
-        texImage->TexFormat = &_mesa_texformat_argb8888;
-        texelBytes = 4;
+        texImage->TexFormat = _dri_texformat_argb8888;
       }
       else
         return 0;
@@ -462,8 +518,7 @@ r200ValidateClientStorage( GLcontext *ctx, GLenum target,
 
    case GL_RGB:
       if ( format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5 ) {
-        texImage->TexFormat = &_mesa_texformat_rgb565;
-        texelBytes = 2;
+        texImage->TexFormat = _dri_texformat_rgb565;
       }
       else
         return 0;
@@ -473,19 +528,16 @@ r200ValidateClientStorage( GLcontext *ctx, GLenum target,
       if ( format == GL_YCBCR_MESA && 
           type == GL_UNSIGNED_SHORT_8_8_REV_APPLE ) {
         texImage->TexFormat = &_mesa_texformat_ycbcr_rev;
-        texelBytes = 2;
       }
       else if ( format == GL_YCBCR_MESA && 
                (type == GL_UNSIGNED_SHORT_8_8_APPLE || 
                 type == GL_UNSIGNED_BYTE)) {
         texImage->TexFormat = &_mesa_texformat_ycbcr;
-        texelBytes = 2;
       }
       else
         return 0;
       break;
-      
-        
+
    default:
       return 0;
    }
@@ -504,7 +556,7 @@ r200ValidateClientStorage( GLcontext *ctx, GLenum target,
                                                  format, type);
 
       
-      if (0)
+      if ( R200_DEBUG & DEBUG_TEXTURE )
         fprintf(stderr, "%s: srcRowStride %d/%x\n", 
                 __FUNCTION__, srcRowStride, srcRowStride);
 
@@ -524,7 +576,8 @@ r200ValidateClientStorage( GLcontext *ctx, GLenum target,
        */
       texImage->Data = (void *)pixels;
       texImage->IsClientData = GL_TRUE;
-      texImage->RowStride = srcRowStride / texelBytes;
+      texImage->RowStride = srcRowStride / texImage->TexFormat->TexelBytes;
+
       return 1;
    }
 }
@@ -669,7 +722,6 @@ static void r200TexSubImage2D( GLcontext *ctx, GLenum target, GLint level,
    driTextureObject * t = (driTextureObject *) texObj->DriverData;
    GLuint face;
 
-
    /* which cube face or ordinary 2D image */
    switch (target) {
    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
@@ -705,6 +757,116 @@ static void r200TexSubImage2D( GLcontext *ctx, GLenum target, GLint level,
 }
 
 
+static void r200CompressedTexImage2D( GLcontext *ctx, GLenum target, GLint level,
+                              GLint internalFormat,
+                              GLint width, GLint height, GLint border,
+                              GLsizei imageSize, const GLvoid *data,
+                              struct gl_texture_object *texObj,
+                              struct gl_texture_image *texImage )
+{
+   driTextureObject * t = (driTextureObject *) texObj->DriverData;
+   GLuint face;
+
+   /* which cube face or ordinary 2D image */
+   switch (target) {
+   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
+   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
+   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
+   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
+   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
+   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
+      face = (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
+      ASSERT(face < 6);
+      break;
+   default:
+      face = 0;
+   }
+
+   if ( t != NULL ) {
+      driSwapOutTextureObject( t );
+   }
+   else {
+      t = (driTextureObject *) r200AllocTexObj( texObj );
+      if (!t) {
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2D");
+         return;
+      }
+   }
+
+   texImage->IsClientData = GL_FALSE;
+/* can't call this, different parameters. Would never evaluate to true anyway currently
+   if (r200ValidateClientStorage( ctx, target, 
+                                 internalFormat,
+                                 width, height,
+                                 format, type, pixels,
+                                 packing, texObj, texImage)) {
+      if (R200_DEBUG & DEBUG_TEXTURE)
+        fprintf(stderr, "%s: Using client storage\n", __FUNCTION__);
+   }
+   else */{
+      if (R200_DEBUG & DEBUG_TEXTURE)
+        fprintf(stderr, "%s: Using normal storage\n", __FUNCTION__);
+
+      /* Normal path: copy (to cached memory) and eventually upload
+       * via another copy to GART memory and then a blit...  Could
+       * eliminate one copy by going straight to (permanent) GART.
+       *
+       * Note, this will call r200ChooseTextureFormat.
+       */
+      _mesa_store_compressed_teximage2d(ctx, target, level, internalFormat, width,
+                                 height, border, imageSize, data, texObj, texImage);
+
+      t->dirty_images[face] |= (1 << level);
+   }
+}
+
+
+static void r200CompressedTexSubImage2D( GLcontext *ctx, GLenum target, GLint level,
+                                 GLint xoffset, GLint yoffset,
+                                 GLsizei width, GLsizei height,
+                                 GLenum format,
+                                 GLsizei imageSize, const GLvoid *data,
+                                 struct gl_texture_object *texObj,
+                                 struct gl_texture_image *texImage )
+{
+   driTextureObject * t = (driTextureObject *) texObj->DriverData;
+   GLuint face;
+
+
+   /* which cube face or ordinary 2D image */
+   switch (target) {
+   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
+   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
+   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
+   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
+   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
+   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
+      face = (GLuint) target - (GLuint) GL_TEXTURE_CUBE_MAP_POSITIVE_X;
+      ASSERT(face < 6);
+      break;
+   default:
+      face = 0;
+   }
+
+   assert( t ); /* this _should_ be true */
+   if ( t ) {
+      driSwapOutTextureObject( t );
+   }
+   else {
+      t = (driTextureObject *) r200AllocTexObj( texObj );
+      if (!t) {
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexSubImage2D");
+         return;
+      }
+   }
+
+   _mesa_store_compressed_texsubimage2d(ctx, target, level, xoffset, yoffset, width,
+                            height, format, imageSize, data, texObj, texImage);
+
+   t->dirty_images[face] |= (1 << level);
+}
+
+
 #if ENABLE_HW_3D_TEXTURE
 static void r200TexImage3D( GLcontext *ctx, GLenum target, GLint level,
                             GLint internalFormat,
@@ -721,7 +883,7 @@ static void r200TexImage3D( GLcontext *ctx, GLenum target, GLint level,
       driSwapOutTextureObject( t );
    }
    else {
-      t = r200AllocTexObj( texObj );
+      t = (driTextureObject *) r200AllocTexObj( texObj );
       if (!t) {
          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
          return;
@@ -782,7 +944,7 @@ r200TexSubImage3D( GLcontext *ctx, GLenum target, GLint level,
       driSwapOutTextureObject( t );
    }
    else {
-      t = r200AllocTexObj(texObj);
+      t = (driTextureObject *) r200AllocTexObj( texObj );
       if (!t) {
          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage3D");
          return;
@@ -830,7 +992,7 @@ static void r200TexEnv( GLcontext *ctx, GLenum target,
    }
 
    case GL_TEXTURE_LOD_BIAS_EXT: {
-      GLfloat bias;
+      GLfloat bias, min;
       GLuint b;
       const int fixed_one = 0x8000000;
 
@@ -840,7 +1002,9 @@ static void r200TexEnv( GLcontext *ctx, GLenum target,
        * NOTE: Add a small bias to the bias for conform mipsel.c test.
        */
       bias = *param + .01;
-      bias = CLAMP( bias, -16.0, 16.0 );
+      min = driQueryOptionb (&rmesa->optionCache, "no_neg_lod_bias") ?
+         0.0 : -16.0;
+      bias = CLAMP( bias, min, 16.0 );
       b = (int)(bias * fixed_one) & R200_LOD_BIAS_MASK;
       
       if ( (rmesa->hw.tex[unit].cmd[TEX_PP_TXFORMAT_X] & R200_LOD_BIAS_MASK) != b ) {
@@ -850,7 +1014,16 @@ static void r200TexEnv( GLcontext *ctx, GLenum target,
       }
       break;
    }
-
+   case GL_COORD_REPLACE_ARB:
+      if (ctx->Point.PointSprite) {
+        R200_STATECHANGE( rmesa, spr );
+        if ((GLenum)param[0]) {
+           rmesa->hw.spr.cmd[SPR_POINT_SPRITE_CNTL] |= R200_PS_GEN_TEX_0 << unit;
+        } else {
+           rmesa->hw.spr.cmd[SPR_POINT_SPRITE_CNTL] &= ~(R200_PS_GEN_TEX_0 << unit);
+        }
+      }
+      break;
    default:
       return;
    }
@@ -873,10 +1046,6 @@ static void r200TexParameter( GLcontext *ctx, GLenum target,
               _mesa_lookup_enum_by_nr( pname ) );
    }
 
-   if ( ( target != GL_TEXTURE_2D ) &&
-       ( target != GL_TEXTURE_1D ) )
-      return;
-
    switch ( pname ) {
    case GL_TEXTURE_MIN_FILTER:
    case GL_TEXTURE_MAG_FILTER:
@@ -892,7 +1061,7 @@ static void r200TexParameter( GLcontext *ctx, GLenum target,
       break;
 
    case GL_TEXTURE_BORDER_COLOR:
-      r200SetTexBorderColor( t, texObj->_BorderChan );
+      r200SetTexBorderColor( t, texObj->BorderColor );
       break;
 
    case GL_TEXTURE_BASE_LEVEL:
@@ -922,17 +1091,22 @@ static void r200BindTexture( GLcontext *ctx, GLenum target,
                               struct gl_texture_object *texObj )
 {
    if ( R200_DEBUG & (DEBUG_STATE|DEBUG_TEXTURE) ) {
-      fprintf( stderr, "%s( %p ) unit=%d\n", __FUNCTION__, texObj,
+      fprintf( stderr, "%s( %p ) unit=%d\n", __FUNCTION__, (void *)texObj,
               ctx->Texture.CurrentUnit );
    }
 
-   if ( target == GL_TEXTURE_2D || target == GL_TEXTURE_1D ) {
-      if ( texObj->DriverData == NULL ) {
-        r200AllocTexObj( texObj );
-      }
+   if ( (target == GL_TEXTURE_1D)
+       || (target == GL_TEXTURE_2D) 
+#if ENABLE_HW_3D_TEXTURE
+       || (target == GL_TEXTURE_3D)
+#endif
+       || (target == GL_TEXTURE_CUBE_MAP)
+       || (target == GL_TEXTURE_RECTANGLE_NV) ) {
+      assert( texObj->DriverData != NULL );
    }
 }
 
+
 static void r200DeleteTexture( GLcontext *ctx,
                                 struct gl_texture_object *texObj )
 {
@@ -940,7 +1114,7 @@ static void r200DeleteTexture( GLcontext *ctx,
    driTextureObject * t = (driTextureObject *) texObj->DriverData;
 
    if ( R200_DEBUG & (DEBUG_STATE|DEBUG_TEXTURE) ) {
-      fprintf( stderr, "%s( %p (target = %s) )\n", __FUNCTION__, texObj,
+      fprintf( stderr, "%s( %p (target = %s) )\n", __FUNCTION__, (void *)texObj,
               _mesa_lookup_enum_by_nr( texObj->Target ) );
    }
 
@@ -951,6 +1125,8 @@ static void r200DeleteTexture( GLcontext *ctx,
 
       driDestroyTextureObject( t );
    }
+   /* Free mipmap images and the texture object itself */
+   _mesa_delete_texture_object(ctx, texObj);
 }
 
 /* Need:  
@@ -974,46 +1150,74 @@ static void r200TexGen( GLcontext *ctx,
 }
 
 
-void r200InitTextureFuncs( GLcontext *ctx )
+/**
+ * Allocate a new texture object.
+ * Called via ctx->Driver.NewTextureObject.
+ * Note: this function will be called during context creation to
+ * allocate the default texture objects.
+ * Note: we could use containment here to 'derive' the driver-specific
+ * texture object from the core mesa gl_texture_object.  Not done at this time.
+ * Fixup MaxAnisotropy according to user preference.
+ */
+static struct gl_texture_object *
+r200NewTextureObject( GLcontext *ctx, GLuint name, GLenum target )
 {
    r200ContextPtr rmesa = R200_CONTEXT(ctx);
+   struct gl_texture_object *obj;
+   obj = _mesa_new_texture_object(ctx, name, target);
+   if (!obj)
+      return NULL;
+   obj->MaxAnisotropy = rmesa->initialMaxAnisotropy;
+   r200AllocTexObj( obj );
+   return obj;
+}
 
 
-   ctx->Driver.ChooseTextureFormat     = r200ChooseTextureFormat;
-   ctx->Driver.TexImage1D              = r200TexImage1D;
-   ctx->Driver.TexImage2D              = r200TexImage2D;
+void r200InitTextureFuncs( struct dd_function_table *functions )
+{
+   /* Note: we only plug in the functions we implement in the driver
+    * since _mesa_init_driver_functions() was already called.
+    */
+   functions->ChooseTextureFormat      = r200ChooseTextureFormat;
+   functions->TexImage1D               = r200TexImage1D;
+   functions->TexImage2D               = r200TexImage2D;
 #if ENABLE_HW_3D_TEXTURE
-   ctx->Driver.TexImage3D              = r200TexImage3D;
+   functions->TexImage3D               = r200TexImage3D;
 #else
-   ctx->Driver.TexImage3D              = _mesa_store_teximage3d;
+   functions->TexImage3D               = _mesa_store_teximage3d;
 #endif
-   ctx->Driver.TexSubImage1D           = r200TexSubImage1D;
-   ctx->Driver.TexSubImage2D           = r200TexSubImage2D;
+   functions->TexSubImage1D            = r200TexSubImage1D;
+   functions->TexSubImage2D            = r200TexSubImage2D;
 #if ENABLE_HW_3D_TEXTURE
-   ctx->Driver.TexSubImage3D           = r200TexSubImage3D;
+   functions->TexSubImage3D            = r200TexSubImage3D;
 #else
-   ctx->Driver.TexSubImage3D           = _mesa_store_texsubimage3d;
+   functions->TexSubImage3D            = _mesa_store_texsubimage3d;
 #endif
-   ctx->Driver.CopyTexImage1D          = _swrast_copy_teximage1d;
-   ctx->Driver.CopyTexImage2D          = _swrast_copy_teximage2d;
-   ctx->Driver.CopyTexSubImage1D       = _swrast_copy_texsubimage1d;
-   ctx->Driver.CopyTexSubImage2D       = _swrast_copy_texsubimage2d;
-   ctx->Driver.CopyTexSubImage3D       = _swrast_copy_texsubimage3d;
-   ctx->Driver.TestProxyTexImage       = _mesa_test_proxy_teximage;
-
-   ctx->Driver.BindTexture             = r200BindTexture;
-   ctx->Driver.CreateTexture           = NULL; /* FIXME: Is this used??? */
-   ctx->Driver.DeleteTexture           = r200DeleteTexture;
-   ctx->Driver.IsTextureResident       = driIsTextureResident;
-   ctx->Driver.PrioritizeTexture       = NULL;
-   ctx->Driver.ActiveTexture           = NULL;
-   ctx->Driver.UpdateTexturePalette    = NULL;
-
-   ctx->Driver.TexEnv                  = r200TexEnv;
-   ctx->Driver.TexParameter            = r200TexParameter;
-   ctx->Driver.TexGen                   = r200TexGen;
+   functions->NewTextureObject         = r200NewTextureObject;
+   functions->BindTexture              = r200BindTexture;
+   functions->DeleteTexture            = r200DeleteTexture;
+   functions->IsTextureResident                = driIsTextureResident;
+
+   functions->TexEnv                   = r200TexEnv;
+   functions->TexParameter             = r200TexParameter;
+   functions->TexGen                   = r200TexGen;
+
+   functions->CompressedTexImage2D     = r200CompressedTexImage2D;
+   functions->CompressedTexSubImage2D  = r200CompressedTexSubImage2D;
 
+   driInitTextureFormats();
+
+#if 000
+   /* moved or obsolete code */
+   r200ContextPtr rmesa = R200_CONTEXT(ctx);
    driInitTextureObjects( ctx, & rmesa->swapped,
                          DRI_TEXMGR_DO_TEXTURE_1D
                          | DRI_TEXMGR_DO_TEXTURE_2D );
+
+   /* Hack: r200NewTextureObject is not yet installed when the
+    * default textures are created. Therefore set MaxAnisotropy of the
+    * default 2D texture now. */
+   ctx->Shared->Default2D->MaxAnisotropy = driQueryOptionf (&rmesa->optionCache,
+                                                           "def_max_anisotropy");
+#endif
 }