i965: Reduce repeated calculation of the attribute-offset-in-VUE.
[mesa.git] / src / mesa / drivers / dri / mga / mgatex.c
index 8b8d472c525feacd2ae4313cf5ec9f6d94b1fc3b..ca3dd4b01391c218db83f5ebd18a3346c4592b70 100644 (file)
  * Authors:
  *    Keith Whitwell <keith@tungstengraphics.com>
  */
-/* $XFree86: xc/lib/GL/mesa/src/drv/mga/mgatex.c,v 1.14 2002/10/30 12:51:36 alanh Exp $ */
 
-#include "glheader.h"
-#include "mm.h"
+#include "main/glheader.h"
+#include "main/mm.h"
+#include "main/colormac.h"
+#include "main/context.h"
+#include "main/enums.h"
+#include "main/simple_list.h"
+#include "main/imports.h"
+#include "main/macros.h"
+#include "main/texstore.h"
+#include "main/teximage.h"
+#include "main/texobj.h"
+
 #include "mgacontext.h"
 #include "mgatex.h"
 #include "mgaregs.h"
-#include "mgatris.h"
 #include "mgaioctl.h"
 
-#include "colormac.h"
-#include "context.h"
-#include "enums.h"
-#include "simple_list.h"
-#include "imports.h"
-#include "macros.h"
-#include "texformat.h"
-#include "texstore.h"
-#include "teximage.h"
-
-#include "swrast/swrast.h"
-
 #include "xmlpool.h"
 
 /**
@@ -153,14 +149,18 @@ mgaSetTexFilter( mgaTextureObjectPtr t, GLenum minf, GLenum magf )
    t->setup.texfilter |= val;
 }
 
-static void mgaSetTexBorderColor(mgaTextureObjectPtr t, GLubyte color[4])
+static void mgaSetTexBorderColor(mgaTextureObjectPtr t, const GLfloat color[4])
 {
-   t->setup.texbordercol = PACK_COLOR_8888(color[3], color[0], 
-                                          color[1], color[2] );
+   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->setup.texbordercol = PACK_COLOR_8888(c[3], c[0], c[1], c[2] );
 }
 
 
-static const struct gl_texture_format *
+static gl_format
 mgaChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
                        GLenum format, GLenum type )
 {
@@ -178,15 +178,15 @@ mgaChooseTextureFormat( 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 ? MESA_FORMAT_ARGB8888 : MESA_FORMAT_ARGB1555;
       case GL_UNSIGNED_SHORT_4_4_4_4:
       case GL_UNSIGNED_SHORT_4_4_4_4_REV:
-        return &_mesa_texformat_argb4444;
+        return MESA_FORMAT_ARGB4444;
       case GL_UNSIGNED_SHORT_5_5_5_1:
       case GL_UNSIGNED_SHORT_1_5_5_5_REV:
-        return &_mesa_texformat_argb1555;
+        return MESA_FORMAT_ARGB1555;
       default:
-         return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
+         return do32bpt ? MESA_FORMAT_ARGB8888 : MESA_FORMAT_ARGB4444;
       }
 
    case 3:
@@ -195,15 +195,15 @@ mgaChooseTextureFormat( 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 MESA_FORMAT_ARGB4444;
       case GL_UNSIGNED_SHORT_5_5_5_1:
       case GL_UNSIGNED_SHORT_1_5_5_5_REV:
-        return &_mesa_texformat_argb1555;
+        return MESA_FORMAT_ARGB1555;
       case GL_UNSIGNED_SHORT_5_6_5:
       case GL_UNSIGNED_SHORT_5_6_5_REV:
-        return &_mesa_texformat_rgb565;
+        return MESA_FORMAT_RGB565;
       default:
-         return do32bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_rgb565;
+         return do32bpt ? MESA_FORMAT_ARGB8888 : MESA_FORMAT_RGB565;
       }
 
    case GL_RGBA8:
@@ -211,25 +211,25 @@ mgaChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
    case GL_RGBA12:
    case GL_RGBA16:
       return !force16bpt ?
-         &_mesa_texformat_argb8888 : &_mesa_texformat_argb4444;
+         MESA_FORMAT_ARGB8888 : MESA_FORMAT_ARGB4444;
 
    case GL_RGBA4:
    case GL_RGBA2:
-      return &_mesa_texformat_argb4444;
+      return MESA_FORMAT_ARGB4444;
 
    case GL_RGB5_A1:
-      return &_mesa_texformat_argb1555;
+      return MESA_FORMAT_ARGB1555;
 
    case GL_RGB8:
    case GL_RGB10:
    case GL_RGB12:
    case GL_RGB16:
-      return !force16bpt ? &_mesa_texformat_argb8888 : &_mesa_texformat_rgb565;
+      return !force16bpt ? MESA_FORMAT_ARGB8888 : MESA_FORMAT_RGB565;
 
    case GL_RGB5:
    case GL_RGB4:
    case GL_R3_G3_B2:
-      return &_mesa_texformat_rgb565;
+      return MESA_FORMAT_RGB565;
 
    case GL_ALPHA:
    case GL_ALPHA4:
@@ -238,7 +238,7 @@ mgaChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
    case GL_ALPHA16:
    case GL_COMPRESSED_ALPHA:
       /* FIXME: This will report incorrect component sizes... */
-      return MGA_IS_G400(mmesa) ? &_mesa_texformat_al88 : &_mesa_texformat_argb4444;
+      return MGA_IS_G400(mmesa) ? MESA_FORMAT_AL88 : MESA_FORMAT_ARGB4444;
 
    case 1:
    case GL_LUMINANCE:
@@ -248,7 +248,7 @@ mgaChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
    case GL_LUMINANCE16:
    case GL_COMPRESSED_LUMINANCE:
       /* FIXME: This will report incorrect component sizes... */
-      return MGA_IS_G400(mmesa) ? &_mesa_texformat_al88 : &_mesa_texformat_rgb565;
+      return MGA_IS_G400(mmesa) ? MESA_FORMAT_AL88 : MESA_FORMAT_RGB565;
 
    case 2:
    case GL_LUMINANCE_ALPHA:
@@ -260,7 +260,7 @@ mgaChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
    case GL_LUMINANCE16_ALPHA16:
    case GL_COMPRESSED_LUMINANCE_ALPHA:
       /* FIXME: This will report incorrect component sizes... */
-      return MGA_IS_G400(mmesa) ? &_mesa_texformat_al88 : &_mesa_texformat_argb4444;
+      return MGA_IS_G400(mmesa) ? MESA_FORMAT_AL88 : MESA_FORMAT_ARGB4444;
 
    case GL_INTENSITY:
    case GL_INTENSITY4:
@@ -269,15 +269,15 @@ mgaChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
    case GL_INTENSITY16:
    case GL_COMPRESSED_INTENSITY:
       /* FIXME: This will report incorrect component sizes... */
-      return MGA_IS_G400(mmesa) ? &_mesa_texformat_i8 : &_mesa_texformat_argb4444;
+      return MGA_IS_G400(mmesa) ? MESA_FORMAT_I8 : MESA_FORMAT_ARGB4444;
 
    case GL_YCBCR_MESA:
       if (MGA_IS_G400(mmesa) &&
           (type == GL_UNSIGNED_SHORT_8_8_APPLE ||
            type == GL_UNSIGNED_BYTE))
-         return &_mesa_texformat_ycbcr;
+         return MESA_FORMAT_YCBCR;
       else
-         return &_mesa_texformat_ycbcr_rev;
+         return MESA_FORMAT_YCBCR_REV;
 
    case GL_COLOR_INDEX:
    case GL_COLOR_INDEX1_EXT:
@@ -286,14 +286,14 @@ mgaChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
    case GL_COLOR_INDEX8_EXT:
    case GL_COLOR_INDEX12_EXT:
    case GL_COLOR_INDEX16_EXT:
-      return &_mesa_texformat_ci8;
+      return MESA_FORMAT_CI8;
 
    default:
       _mesa_problem( ctx, "unexpected texture format in %s", __FUNCTION__ );
-      return NULL;
+      return MESA_FORMAT_NONE;
    }
 
-   return NULL; /* never get here */
+   return MESA_FORMAT_NONE; /* never get here */
 }
 
 
@@ -329,14 +329,14 @@ mgaAllocTexObj( struct gl_texture_object *tObj )
 
       mgaSetTexWrapping( t, tObj->WrapS, tObj->WrapT );
       mgaSetTexFilter( t, tObj->MinFilter, tObj->MagFilter );
-      mgaSetTexBorderColor( t, tObj->_BorderChan );
+      mgaSetTexBorderColor( t, tObj->BorderColor.f );
    }
 
    return( t );
 }
 
 
-static void mgaDDTexEnv( GLcontext *ctx, GLenum target,
+static void mgaTexEnv( GLcontext *ctx, GLenum target,
                         GLenum pname, const GLfloat *param )
 {
    GLuint unit = ctx->Texture.CurrentUnit;
@@ -365,7 +365,6 @@ static void mgaTexImage2D( GLcontext *ctx, GLenum target, GLint level,
 {
    driTextureObject * t = (driTextureObject *) texObj->DriverData;
 
-
    if ( t != NULL ) {
       driSwapOutTextureObject( t );
    } 
@@ -398,8 +397,7 @@ static void mgaTexSubImage2D( GLcontext *ctx,
 {
    driTextureObject * t = (driTextureObject *) texObj->DriverData;
 
-
-   assert( t != NULL ); /* this _should_ be true */
+   assert( t ); /* this _should_ be true */
    if ( t != NULL ) {
       driSwapOutTextureObject( t );
    } 
@@ -426,20 +424,17 @@ static void mgaTexSubImage2D( GLcontext *ctx,
  */
 
 static void
-mgaDDTexParameter( GLcontext *ctx, GLenum target,
+mgaTexParameter( GLcontext *ctx, GLenum target,
                   struct gl_texture_object *tObj,
                   GLenum pname, const GLfloat *params )
 {
-   mgaContextPtr       mmesa = MGA_CONTEXT( ctx );
-   mgaTextureObjectPtr t;
-
-   t = (mgaTextureObjectPtr) tObj->DriverData;
+   mgaContextPtr mmesa = MGA_CONTEXT( ctx );
+   mgaTextureObjectPtr t = (mgaTextureObjectPtr) tObj->DriverData;
 
    /* If we don't have a hardware texture, it will be automatically
     * created with current state before it is used, so we don't have
     * to do anything now 
     */
-
    if ( (t == NULL) ||
         (target != GL_TEXTURE_2D &&
          target != GL_TEXTURE_RECTANGLE_NV) ) {
@@ -463,7 +458,7 @@ mgaDDTexParameter( GLcontext *ctx, GLenum target,
 
    case GL_TEXTURE_BORDER_COLOR:
       FLUSH_BATCH(mmesa);
-      mgaSetTexBorderColor(t, tObj->_BorderChan);
+      mgaSetTexBorderColor(t, tObj->BorderColor.f);
       break;
 
    case GL_TEXTURE_BASE_LEVEL:
@@ -485,20 +480,16 @@ mgaDDTexParameter( GLcontext *ctx, GLenum target,
 
 
 static void
-mgaDDBindTexture( GLcontext *ctx, GLenum target,
+mgaBindTexture( GLcontext *ctx, GLenum target,
                  struct gl_texture_object *tObj )
 {
-   if ( target == GL_TEXTURE_2D ||
-        target == GL_TEXTURE_RECTANGLE_NV ) {
-      if ( tObj->DriverData == NULL ) {
-        mgaAllocTexObj( tObj );
-      }
-   }
+   assert( (target != GL_TEXTURE_2D && target != GL_TEXTURE_RECTANGLE_NV) ||
+           (tObj->DriverData != NULL) );
 }
 
 
 static void
-mgaDDDeleteTexture( GLcontext *ctx, struct gl_texture_object *tObj )
+mgaDeleteTexture( GLcontext *ctx, struct gl_texture_object *tObj )
 {
    mgaContextPtr mmesa = MGA_CONTEXT( ctx );
    driTextureObject * t = (driTextureObject *) tObj->DriverData;
@@ -510,41 +501,40 @@ mgaDDDeleteTexture( GLcontext *ctx, struct gl_texture_object *tObj )
 
       driDestroyTextureObject( t );
    }
+
+   /* Free mipmap images and the texture object itself */
+   _mesa_delete_texture_object(ctx, tObj);
 }
 
 
-void
-mgaDDInitTextureFuncs( 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.
+ */
+static struct gl_texture_object *
+mgaNewTextureObject( GLcontext *ctx, GLuint name, GLenum target )
 {
-   mgaContextPtr mmesa = MGA_CONTEXT(ctx);
+   struct gl_texture_object *obj;
+   obj = _mesa_new_texture_object(ctx, name, target);
+   mgaAllocTexObj( obj );
+   return obj;
+}
 
 
-   ctx->Driver.ChooseTextureFormat     = mgaChooseTextureFormat;
-   ctx->Driver.TexImage1D              = _mesa_store_teximage1d;
-   ctx->Driver.TexImage2D              = mgaTexImage2D;
-   ctx->Driver.TexImage3D              = _mesa_store_teximage3d;
-   ctx->Driver.TexSubImage1D           = _mesa_store_texsubimage1d;
-   ctx->Driver.TexSubImage2D           = mgaTexSubImage2D;
-   ctx->Driver.TexSubImage3D           = _mesa_store_texsubimage3d;
-   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             = mgaDDBindTexture;
-   ctx->Driver.CreateTexture           = NULL; /* FIXME: Is this used??? */
-   ctx->Driver.DeleteTexture           = mgaDDDeleteTexture;
-   ctx->Driver.IsTextureResident       = driIsTextureResident;
-   ctx->Driver.PrioritizeTexture       = NULL;
-   ctx->Driver.ActiveTexture           = NULL;
-   ctx->Driver.UpdateTexturePalette    = NULL;
-
-   ctx->Driver.TexEnv                  = mgaDDTexEnv;
-   ctx->Driver.TexParameter            = mgaDDTexParameter;
-
-   driInitTextureObjects( ctx, & mmesa->swapped,
-                          (DRI_TEXMGR_DO_TEXTURE_2D |
-                           DRI_TEXMGR_DO_TEXTURE_RECT) );
+void
+mgaInitTextureFuncs( struct dd_function_table *functions )
+{
+   functions->ChooseTextureFormat      = mgaChooseTextureFormat;
+   functions->TexImage2D               = mgaTexImage2D;
+   functions->TexSubImage2D            = mgaTexSubImage2D;
+   functions->BindTexture              = mgaBindTexture;
+   functions->NewTextureObject         = mgaNewTextureObject;
+   functions->DeleteTexture            = mgaDeleteTexture;
+   functions->IsTextureResident                = driIsTextureResident;
+   functions->TexEnv                   = mgaTexEnv;
+   functions->TexParameter             = mgaTexParameter;
 }