Fix pow <small> and a very stypid bug with dummy srcs(0 equals to tmp0.x)</small...
[mesa.git] / src / mesa / main / texstate.c
index 38dd4442a72689b591f33d3ea1e97ff0f6c2cada..608dbc1088a8d7b549f7568db3e3a9418ddb047b 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.1
+ * Version:  6.5
  *
- * Copyright (C) 1999-2004  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
 #include "colortab.h"
 #include "context.h"
 #include "enums.h"
-#include "extensions.h"
 #include "macros.h"
-#include "nvfragprog.h"
 #include "texobj.h"
 #include "teximage.h"
 #include "texstate.h"
+#include "texenvprogram.h"
 #include "mtypes.h"
 #include "math/m_xform.h"
-#include "math/m_matrix.h"
 
 
 
-#ifdef SPECIALCAST
-/* Needed for an Amiga compiler */
 #define ENUM_TO_FLOAT(X) ((GLfloat)(GLint)(X))
 #define ENUM_TO_DOUBLE(X) ((GLdouble)(GLint)(X))
-#else
-/* all other compilers */
-#define ENUM_TO_FLOAT(X) ((GLfloat)(X))
-#define ENUM_TO_DOUBLE(X) ((GLdouble)(X))
-#endif
+
 
 /**
  * Default texture combine environment state.  This is used to initialize
@@ -253,6 +245,8 @@ calculate_derived_texenv( struct gl_tex_env_combine_state *state,
         break;
       case GL_INTENSITY:
         mode_a = GL_INTERPOLATE;
+        state->SourceA[0] = GL_CONSTANT;
+        state->OperandA[2] = GL_SRC_ALPHA;
         /* FALLTHROUGH */
       case GL_LUMINANCE:
       case GL_RGB:
@@ -300,6 +294,8 @@ _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
       case GL_TEXTURE_ENV_MODE:
          {
             const GLenum mode = (GLenum) (GLint) *param;
+           if (texUnit->EnvMode == mode)
+              return;
             if (mode == GL_MODULATE ||
                 mode == GL_BLEND ||
                 mode == GL_DECAL ||
@@ -309,22 +305,8 @@ _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
                  (ctx->Extensions.EXT_texture_env_combine ||
                   ctx->Extensions.ARB_texture_env_combine))) {
                /* legal */
-               if (texUnit->EnvMode == mode)
-                  return;
                FLUSH_VERTICES(ctx, _NEW_TEXTURE);
                texUnit->EnvMode = mode;
-
-              /* FIXME: Is this needed here or is having it in
-               * FIXME: update_texture_state enough?
-               */
-              if ( mode == GL_COMBINE ) {
-                 texUnit->_CurrentCombine = & texUnit->Combine;
-              }
-              else if ( texUnit->_Current != NULL ) {
-                 calculate_derived_texenv( & texUnit->_EnvMode, mode,
-                                            texUnit->_Current->Image[0][0]->Format );
-                 texUnit->_CurrentCombine = & texUnit->_EnvMode;
-              }
             }
             else {
                TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", mode);
@@ -349,6 +331,8 @@ _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
         if (ctx->Extensions.EXT_texture_env_combine ||
              ctx->Extensions.ARB_texture_env_combine) {
            const GLenum mode = (GLenum) (GLint) *param;
+           if (texUnit->Combine.ModeRGB == mode)
+              return;
            switch (mode) {
            case GL_REPLACE:
            case GL_MODULATE:
@@ -389,8 +373,6 @@ _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
                TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", mode);
               return;
            }
-           if (texUnit->Combine.ModeRGB == mode)
-              return;
            FLUSH_VERTICES(ctx, _NEW_TEXTURE);
            texUnit->Combine.ModeRGB = mode;
         }
@@ -403,6 +385,8 @@ _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
         if (ctx->Extensions.EXT_texture_env_combine ||
              ctx->Extensions.ARB_texture_env_combine) {
            const GLenum mode = (GLenum) (GLint) *param;
+           if (texUnit->Combine.ModeA == mode)
+              return;
             switch (mode) {
            case GL_REPLACE:
            case GL_MODULATE:
@@ -429,9 +413,6 @@ _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
               TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", mode);
               return;
            }
-
-           if (texUnit->Combine.ModeA == mode)
-               return;
            FLUSH_VERTICES(ctx, _NEW_TEXTURE);
            texUnit->Combine.ModeA = mode;
         }
@@ -447,6 +428,8 @@ _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
             ctx->Extensions.ARB_texture_env_combine) {
            const GLenum source = (GLenum) (GLint) *param;
            const GLuint s = pname - GL_SOURCE0_RGB;
+           if (texUnit->Combine.SourceRGB[s] == source)
+              return;
             if (source == GL_TEXTURE ||
                 source == GL_CONSTANT ||
                 source == GL_PRIMARY_COLOR ||
@@ -457,8 +440,6 @@ _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
                 (ctx->Extensions.ATI_texture_env_combine3 &&
                  (source == GL_ZERO || source == GL_ONE))) {
                /* legal */
-               if (texUnit->Combine.SourceRGB[s] == source)
-                  return;
               FLUSH_VERTICES(ctx, _NEW_TEXTURE);
               texUnit->Combine.SourceRGB[s] = source;
             }
@@ -479,6 +460,8 @@ _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
              ctx->Extensions.ARB_texture_env_combine) {
            const GLenum source = (GLenum) (GLint) *param;
            const GLuint s = pname - GL_SOURCE0_ALPHA;
+           if (texUnit->Combine.SourceA[s] == source)
+              return;
             if (source == GL_TEXTURE ||
                 source == GL_CONSTANT ||
                 source == GL_PRIMARY_COLOR ||
@@ -489,8 +472,6 @@ _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
                (ctx->Extensions.ATI_texture_env_combine3 &&
                  (source == GL_ZERO || source == GL_ONE))) {
                /* legal */
-              if (texUnit->Combine.SourceA[s] == source)
-                  return;
               FLUSH_VERTICES(ctx, _NEW_TEXTURE);
               texUnit->Combine.SourceA[s] = source;
             }
@@ -510,13 +491,13 @@ _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
             ctx->Extensions.ARB_texture_env_combine) {
            const GLenum operand = (GLenum) (GLint) *param;
            const GLuint s = pname - GL_OPERAND0_RGB;
+           if (texUnit->Combine.OperandRGB[s] == operand)
+              return;
            switch (operand) {
            case GL_SRC_COLOR:
            case GL_ONE_MINUS_SRC_COLOR:
            case GL_SRC_ALPHA:
            case GL_ONE_MINUS_SRC_ALPHA:
-              if (texUnit->Combine.OperandRGB[s] == operand)
-                 return;
               FLUSH_VERTICES(ctx, _NEW_TEXTURE);
               texUnit->Combine.OperandRGB[s] = operand;
               break;
@@ -535,12 +516,11 @@ _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
         if (ctx->Extensions.EXT_texture_env_combine ||
              ctx->Extensions.ARB_texture_env_combine) {
            const GLenum operand = (GLenum) (GLint) *param;
+           if (texUnit->Combine.OperandA[pname-GL_OPERAND0_ALPHA] == operand)
+              return;
            switch (operand) {
            case GL_SRC_ALPHA:
            case GL_ONE_MINUS_SRC_ALPHA:
-              if (texUnit->Combine.OperandA[pname-GL_OPERAND0_ALPHA] ==
-                  operand)
-                 return;
               FLUSH_VERTICES(ctx, _NEW_TEXTURE);
               texUnit->Combine.OperandA[pname-GL_OPERAND0_ALPHA] = operand;
               break;
@@ -555,16 +535,15 @@ _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
         }
         break;
       case GL_OPERAND2_RGB:
-        if (ctx->Extensions.EXT_texture_env_combine ||
-             ctx->Extensions.ARB_texture_env_combine) {
+        if (ctx->Extensions.ARB_texture_env_combine) {
            const GLenum operand = (GLenum) (GLint) *param;
+           if (texUnit->Combine.OperandRGB[2] == operand)
+              return;
            switch (operand) {
            case GL_SRC_COLOR:           /* ARB combine only */
            case GL_ONE_MINUS_SRC_COLOR: /* ARB combine only */
            case GL_SRC_ALPHA:
            case GL_ONE_MINUS_SRC_ALPHA: /* ARB combine only */
-              if (texUnit->Combine.OperandRGB[2] == operand)
-                 return;
               FLUSH_VERTICES(ctx, _NEW_TEXTURE);
               texUnit->Combine.OperandRGB[2] = operand;
                break;
@@ -573,20 +552,30 @@ _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
               return;
            }
         }
+        else if (ctx->Extensions.EXT_texture_env_combine) {
+           const GLenum operand = (GLenum) (GLint) *param;
+           if (texUnit->Combine.OperandRGB[2] == operand)
+              return;
+           /* operand must be GL_SRC_ALPHA which is the initial value - thus
+              don't need to actually compare the operand to the possible value */
+           else {
+               TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", operand);
+              return;
+           }
+        }
         else {
             TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
            return;
         }
         break;
       case GL_OPERAND2_ALPHA:
-        if (ctx->Extensions.EXT_texture_env_combine ||
-             ctx->Extensions.ARB_texture_env_combine) {
+        if (ctx->Extensions.ARB_texture_env_combine) {
            const GLenum operand = (GLenum) (GLint) *param;
+           if (texUnit->Combine.OperandA[2] == operand)
+              return;
            switch (operand) {
            case GL_SRC_ALPHA:
            case GL_ONE_MINUS_SRC_ALPHA: /* ARB combine only */
-              if (texUnit->Combine.OperandA[2] == operand)
-                 return;
               FLUSH_VERTICES(ctx, _NEW_TEXTURE);
               texUnit->Combine.OperandA[2] = operand;
               break;
@@ -595,6 +584,17 @@ _mesa_TexEnvfv( GLenum target, GLenum pname, const GLfloat *param )
               return;
            }
         }
+        else if (ctx->Extensions.EXT_texture_env_combine) {
+           const GLenum operand = (GLenum) (GLint) *param;
+           if (texUnit->Combine.OperandA[2] == operand)
+              return;
+           /* operand must be GL_SRC_ALPHA which is the initial value - thus
+              don't need to actually compare the operand to the possible value */
+           else {
+               TE_ERROR(GL_INVALID_ENUM, "glTexEnv(param=%s)", operand);
+              return;
+           }
+        }
         else {
             TE_ERROR(GL_INVALID_ENUM, "glTexEnv(pname=%s)", pname);
            return;
@@ -1400,7 +1400,9 @@ _mesa_TexParameterfv( GLenum target, GLenum pname, const GLfloat *params )
               return;
            }
             FLUSH_VERTICES(ctx, _NEW_TEXTURE);
-            texObj->MaxAnisotropy = params[0];
+            /* clamp to max, that's what NVIDIA does */
+            texObj->MaxAnisotropy = MIN2(params[0],
+                                         ctx->Const.MaxTextureMaxAnisotropy);
          }
          else {
             _mesa_error(ctx, GL_INVALID_ENUM,
@@ -1645,32 +1647,10 @@ _mesa_GetTexLevelParameteriv( GLenum target, GLint level,
       return;
    }
 
-   switch (target) {
-   case GL_TEXTURE_1D:
-   case GL_PROXY_TEXTURE_1D:
-   case GL_TEXTURE_2D:
-   case GL_PROXY_TEXTURE_2D:
-      maxLevels = ctx->Const.MaxTextureLevels;
-      break;
-   case GL_TEXTURE_3D:
-   case GL_PROXY_TEXTURE_3D:
-      maxLevels = ctx->Const.Max3DTextureLevels;
-      break;
-   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:
-   case GL_PROXY_TEXTURE_CUBE_MAP:
-      maxLevels = ctx->Const.MaxCubeTextureLevels;
-      break;
-   case GL_TEXTURE_RECTANGLE_NV:
-   case GL_PROXY_TEXTURE_RECTANGLE_NV:
-      maxLevels = 1;
-      break;
-   default:
-      _mesa_problem(ctx, "switch in _mesa_GetTexLevelParameter");
+   maxLevels = _mesa_max_texture_levels(ctx, target);
+   if (maxLevels == 0) {
+      /* should not happen since <target> was just checked above */
+      _mesa_problem(ctx, "maxLevels=0 in _mesa_GetTexLevelParameter");
       return;
    }
 
@@ -1689,11 +1669,7 @@ _mesa_GetTexLevelParameteriv( GLenum target, GLint level,
       return;
    }
 
-   isProxy = (target == GL_PROXY_TEXTURE_1D) ||
-             (target == GL_PROXY_TEXTURE_2D) ||
-             (target == GL_PROXY_TEXTURE_3D) ||
-             (target == GL_PROXY_TEXTURE_CUBE_MAP) ||
-             (target == GL_PROXY_TEXTURE_RECTANGLE_NV);
+   isProxy = _mesa_is_proxy_texture(target);
 
    switch (pname) {
       case GL_TEXTURE_WIDTH:
@@ -1706,38 +1682,39 @@ _mesa_GetTexLevelParameteriv( GLenum target, GLint level,
          *params = img->Depth;
          return;
       case GL_TEXTURE_INTERNAL_FORMAT:
-         *params = img->IntFormat;
+         *params = img->InternalFormat;
          return;
       case GL_TEXTURE_BORDER:
          *params = img->Border;
          return;
       case GL_TEXTURE_RED_SIZE:
-         if (img->Format == GL_RGB || img->Format == GL_RGBA)
+         if (img->_BaseFormat == GL_RGB || img->_BaseFormat == GL_RGBA)
             *params = img->TexFormat->RedBits;
          else
             *params = 0;
          return;
       case GL_TEXTURE_GREEN_SIZE:
-         if (img->Format == GL_RGB || img->Format == GL_RGBA)
+         if (img->_BaseFormat == GL_RGB || img->_BaseFormat == GL_RGBA)
             *params = img->TexFormat->GreenBits;
          else
             *params = 0;
          return;
       case GL_TEXTURE_BLUE_SIZE:
-         if (img->Format == GL_RGB || img->Format == GL_RGBA)
+         if (img->_BaseFormat == GL_RGB || img->_BaseFormat == GL_RGBA)
             *params = img->TexFormat->BlueBits;
          else
             *params = 0;
          return;
       case GL_TEXTURE_ALPHA_SIZE:
-         if (img->Format == GL_ALPHA || img->Format == GL_LUMINANCE_ALPHA ||
-             img->Format == GL_RGBA)
+         if (img->_BaseFormat == GL_ALPHA ||
+             img->_BaseFormat == GL_LUMINANCE_ALPHA ||
+             img->_BaseFormat == GL_RGBA)
             *params = img->TexFormat->AlphaBits;
          else
             *params = 0;
          return;
       case GL_TEXTURE_INTENSITY_SIZE:
-         if (img->Format != GL_INTENSITY)
+         if (img->_BaseFormat != GL_INTENSITY)
             *params = 0;
          else if (img->TexFormat->IntensityBits > 0)
             *params = img->TexFormat->IntensityBits;
@@ -1745,8 +1722,8 @@ _mesa_GetTexLevelParameteriv( GLenum target, GLint level,
             *params = MIN2(img->TexFormat->RedBits, img->TexFormat->GreenBits);
          return;
       case GL_TEXTURE_LUMINANCE_SIZE:
-         if (img->Format != GL_LUMINANCE &&
-             img->Format != GL_LUMINANCE_ALPHA)
+         if (img->_BaseFormat != GL_LUMINANCE &&
+             img->_BaseFormat != GL_LUMINANCE_ALPHA)
             *params = 0;
          else if (img->TexFormat->LuminanceBits > 0)
             *params = img->TexFormat->LuminanceBits;
@@ -1754,21 +1731,28 @@ _mesa_GetTexLevelParameteriv( GLenum target, GLint level,
             *params = MIN2(img->TexFormat->RedBits, img->TexFormat->GreenBits);
          return;
       case GL_TEXTURE_INDEX_SIZE_EXT:
-         if (img->Format == GL_COLOR_INDEX)
+         if (img->_BaseFormat == GL_COLOR_INDEX)
             *params = img->TexFormat->IndexBits;
          else
             *params = 0;
          return;
-      case GL_DEPTH_BITS:
-         /* XXX this isn't in the GL_SGIX_depth_texture spec
-          * but seems appropriate.
-          */
-         if (ctx->Extensions.SGIX_depth_texture)
+      case GL_TEXTURE_DEPTH_SIZE_ARB:
+         if (ctx->Extensions.SGIX_depth_texture ||
+             ctx->Extensions.ARB_depth_texture)
             *params = img->TexFormat->DepthBits;
          else
             _mesa_error(ctx, GL_INVALID_ENUM,
                         "glGetTexLevelParameter[if]v(pname)");
          return;
+      case GL_TEXTURE_STENCIL_SIZE_EXT:
+         if (ctx->Extensions.EXT_packed_depth_stencil) {
+            *params = img->TexFormat->StencilBits;
+         }
+         else {
+            _mesa_error(ctx, GL_INVALID_ENUM,
+                        "glGetTexLevelParameter[if]v(pname)");
+         }
+         return;
 
       /* GL_ARB_texture_compression */
       case GL_TEXTURE_COMPRESSED_IMAGE_SIZE:
@@ -1858,6 +1842,7 @@ _mesa_GetTexLevelParameteriv( GLenum target, GLint level,
                         "glGetTexLevelParameter[if]v(pname)");
          }
          return;
+
       default:
          _mesa_error(ctx, GL_INVALID_ENUM,
                      "glGetTexLevelParameter[if]v(pname)");
@@ -1978,7 +1963,7 @@ _mesa_GetTexParameterfv( GLenum target, GLenum pname, GLfloat *params )
       case GL_TEXTURE_LOD_BIAS:
          if (ctx->Extensions.EXT_texture_lod_bias) {
             *params = obj->LodBias;
-            break;
+            return;
          }
          break;
       default:
@@ -2007,12 +1992,6 @@ _mesa_GetTexParameteriv( GLenum target, GLenum pname, GLint *params )
    switch (pname) {
       case GL_TEXTURE_MAG_FILTER:
          *params = (GLint) obj->MagFilter;
-      case GL_TEXTURE_LOD_BIAS:
-         if (ctx->Extensions.EXT_texture_lod_bias) {
-            *params = (GLint) obj->LodBias;
-            break;
-         }
-         break;
          return;
       case GL_TEXTURE_MIN_FILTER:
          *params = (GLint) obj->MinFilter;
@@ -2112,6 +2091,12 @@ _mesa_GetTexParameteriv( GLenum target, GLenum pname, GLint *params )
             return;
          }
          break;
+      case GL_TEXTURE_LOD_BIAS:
+         if (ctx->Extensions.EXT_texture_lod_bias) {
+            *params = (GLint) obj->LodBias;
+            return;
+         }
+         break;
       default:
          ; /* silence warnings */
    }
@@ -2146,7 +2131,7 @@ _mesa_TexGenfv( GLenum coord, GLenum pname, const GLfloat *params )
       case GL_S:
          if (pname==GL_TEXTURE_GEN_MODE) {
            GLenum mode = (GLenum) (GLint) *params;
-           GLuint bits;
+           GLbitfield bits;
            switch (mode) {
            case GL_OBJECT_LINEAR:
               bits = TEXGEN_OBJ_LINEAR;
@@ -2177,16 +2162,12 @@ _mesa_TexGenfv( GLenum coord, GLenum pname, const GLfloat *params )
            if (TEST_EQ_4V(texUnit->ObjectPlaneS, params))
                return;
            FLUSH_VERTICES(ctx, _NEW_TEXTURE);
-           texUnit->ObjectPlaneS[0] = params[0];
-           texUnit->ObjectPlaneS[1] = params[1];
-           texUnit->ObjectPlaneS[2] = params[2];
-           texUnit->ObjectPlaneS[3] = params[3];
+            COPY_4FV(texUnit->ObjectPlaneS, params);
         }
         else if (pname==GL_EYE_PLANE) {
            GLfloat tmp[4];
-
             /* Transform plane equation by the inverse modelview matrix */
-            if (ctx->ModelviewMatrixStack.Top->flags & MAT_DIRTY_INVERSE) {
+            if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top)) {
                _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
             }
             _mesa_transform_vector( tmp, params, ctx->ModelviewMatrixStack.Top->inv );
@@ -2203,7 +2184,7 @@ _mesa_TexGenfv( GLenum coord, GLenum pname, const GLfloat *params )
       case GL_T:
          if (pname==GL_TEXTURE_GEN_MODE) {
            GLenum mode = (GLenum) (GLint) *params;
-           GLuint bitt;
+           GLbitfield bitt;
            switch (mode) {
                case GL_OBJECT_LINEAR:
                   bitt = TEXGEN_OBJ_LINEAR;
@@ -2234,15 +2215,12 @@ _mesa_TexGenfv( GLenum coord, GLenum pname, const GLfloat *params )
            if (TEST_EQ_4V(texUnit->ObjectPlaneT, params))
                return;
            FLUSH_VERTICES(ctx, _NEW_TEXTURE);
-           texUnit->ObjectPlaneT[0] = params[0];
-           texUnit->ObjectPlaneT[1] = params[1];
-           texUnit->ObjectPlaneT[2] = params[2];
-           texUnit->ObjectPlaneT[3] = params[3];
+            COPY_4FV(texUnit->ObjectPlaneT, params);
         }
         else if (pname==GL_EYE_PLANE) {
            GLfloat tmp[4];
             /* Transform plane equation by the inverse modelview matrix */
-           if (ctx->ModelviewMatrixStack.Top->flags & MAT_DIRTY_INVERSE) {
+           if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top)) {
                _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
             }
             _mesa_transform_vector( tmp, params, ctx->ModelviewMatrixStack.Top->inv );
@@ -2259,7 +2237,7 @@ _mesa_TexGenfv( GLenum coord, GLenum pname, const GLfloat *params )
       case GL_R:
          if (pname==GL_TEXTURE_GEN_MODE) {
            GLenum mode = (GLenum) (GLint) *params;
-           GLuint bitr;
+           GLbitfield bitr;
            switch (mode) {
            case GL_OBJECT_LINEAR:
               bitr = TEXGEN_OBJ_LINEAR;
@@ -2287,15 +2265,12 @@ _mesa_TexGenfv( GLenum coord, GLenum pname, const GLfloat *params )
            if (TEST_EQ_4V(texUnit->ObjectPlaneR, params))
                return;
            FLUSH_VERTICES(ctx, _NEW_TEXTURE);
-           texUnit->ObjectPlaneR[0] = params[0];
-           texUnit->ObjectPlaneR[1] = params[1];
-           texUnit->ObjectPlaneR[2] = params[2];
-           texUnit->ObjectPlaneR[3] = params[3];
+           COPY_4FV(texUnit->ObjectPlaneR, params);
         }
         else if (pname==GL_EYE_PLANE) {
            GLfloat tmp[4];
             /* Transform plane equation by the inverse modelview matrix */
-            if (ctx->ModelviewMatrixStack.Top->flags & MAT_DIRTY_INVERSE) {
+            if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top)) {
                _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
             }
             _mesa_transform_vector( tmp, params, ctx->ModelviewMatrixStack.Top->inv );
@@ -2312,7 +2287,7 @@ _mesa_TexGenfv( GLenum coord, GLenum pname, const GLfloat *params )
       case GL_Q:
          if (pname==GL_TEXTURE_GEN_MODE) {
            GLenum mode = (GLenum) (GLint) *params;
-           GLuint bitq;
+           GLbitfield bitq;
            switch (mode) {
            case GL_OBJECT_LINEAR:
               bitq = TEXGEN_OBJ_LINEAR;
@@ -2334,15 +2309,12 @@ _mesa_TexGenfv( GLenum coord, GLenum pname, const GLfloat *params )
            if (TEST_EQ_4V(texUnit->ObjectPlaneQ, params))
                return;
            FLUSH_VERTICES(ctx, _NEW_TEXTURE);
-           texUnit->ObjectPlaneQ[0] = params[0];
-           texUnit->ObjectPlaneQ[1] = params[1];
-           texUnit->ObjectPlaneQ[2] = params[2];
-           texUnit->ObjectPlaneQ[3] = params[3];
+            COPY_4FV(texUnit->ObjectPlaneQ, params);
         }
         else if (pname==GL_EYE_PLANE) {
            GLfloat tmp[4];
             /* Transform plane equation by the inverse modelview matrix */
-            if (ctx->ModelviewMatrixStack.Top->flags & MAT_DIRTY_INVERSE) {
+            if (_math_matrix_is_dirty(ctx->ModelviewMatrixStack.Top)) {
                _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
             }
             _mesa_transform_vector( tmp, params, ctx->ModelviewMatrixStack.Top->inv );
@@ -2731,138 +2703,6 @@ _mesa_ClientActiveTextureARB( GLenum target )
 
 
 
-/**********************************************************************/
-/*                     Pixel Texgen Extensions                        */
-/**********************************************************************/
-
-void GLAPIENTRY
-_mesa_PixelTexGenSGIX(GLenum mode)
-{
-   GLenum newRgbSource, newAlphaSource;
-   GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
-
-   switch (mode) {
-      case GL_NONE:
-         newRgbSource = GL_PIXEL_GROUP_COLOR_SGIS;
-         newAlphaSource = GL_PIXEL_GROUP_COLOR_SGIS;
-         break;
-      case GL_ALPHA:
-         newRgbSource = GL_PIXEL_GROUP_COLOR_SGIS;
-         newAlphaSource = GL_CURRENT_RASTER_COLOR;
-         break;
-      case GL_RGB:
-         newRgbSource = GL_CURRENT_RASTER_COLOR;
-         newAlphaSource = GL_PIXEL_GROUP_COLOR_SGIS;
-         break;
-      case GL_RGBA:
-         newRgbSource = GL_CURRENT_RASTER_COLOR;
-         newAlphaSource = GL_CURRENT_RASTER_COLOR;
-         break;
-      default:
-         _mesa_error(ctx, GL_INVALID_ENUM, "glPixelTexGenSGIX(mode)");
-         return;
-   }
-
-   if (newRgbSource == ctx->Pixel.FragmentRgbSource &&
-       newAlphaSource == ctx->Pixel.FragmentAlphaSource)
-      return;
-
-   FLUSH_VERTICES(ctx, _NEW_PIXEL);
-   ctx->Pixel.FragmentRgbSource = newRgbSource;
-   ctx->Pixel.FragmentAlphaSource = newAlphaSource;
-}
-
-
-void GLAPIENTRY
-_mesa_PixelTexGenParameterfSGIS(GLenum target, GLfloat value)
-{
-   _mesa_PixelTexGenParameteriSGIS(target, (GLint) value);
-}
-
-
-void GLAPIENTRY
-_mesa_PixelTexGenParameterfvSGIS(GLenum target, const GLfloat *value)
-{
-   _mesa_PixelTexGenParameteriSGIS(target, (GLint) *value);
-}
-
-
-void GLAPIENTRY
-_mesa_PixelTexGenParameteriSGIS(GLenum target, GLint value)
-{
-   GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
-
-   if (value != GL_CURRENT_RASTER_COLOR && value != GL_PIXEL_GROUP_COLOR_SGIS) {
-      _mesa_error(ctx, GL_INVALID_ENUM, "glPixelTexGenParameterSGIS(value)");
-      return;
-   }
-
-   switch (target) {
-   case GL_PIXEL_FRAGMENT_RGB_SOURCE_SGIS:
-      if (ctx->Pixel.FragmentRgbSource == (GLenum) value)
-        return;
-      FLUSH_VERTICES(ctx, _NEW_PIXEL);
-      ctx->Pixel.FragmentRgbSource = (GLenum) value;
-      break;
-   case GL_PIXEL_FRAGMENT_ALPHA_SOURCE_SGIS:
-      if (ctx->Pixel.FragmentAlphaSource == (GLenum) value)
-        return;
-      FLUSH_VERTICES(ctx, _NEW_PIXEL);
-      ctx->Pixel.FragmentAlphaSource = (GLenum) value;
-      break;
-   default:
-      _mesa_error(ctx, GL_INVALID_ENUM, "glPixelTexGenParameterSGIS(target)");
-      return;
-   }
-}
-
-
-void GLAPIENTRY
-_mesa_PixelTexGenParameterivSGIS(GLenum target, const GLint *value)
-{
-  _mesa_PixelTexGenParameteriSGIS(target, *value);
-}
-
-
-void GLAPIENTRY
-_mesa_GetPixelTexGenParameterfvSGIS(GLenum target, GLfloat *value)
-{
-   GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
-
-   if (target == GL_PIXEL_FRAGMENT_RGB_SOURCE_SGIS) {
-      *value = (GLfloat) ctx->Pixel.FragmentRgbSource;
-   }
-   else if (target == GL_PIXEL_FRAGMENT_ALPHA_SOURCE_SGIS) {
-      *value = (GLfloat) ctx->Pixel.FragmentAlphaSource;
-   }
-   else {
-      _mesa_error(ctx, GL_INVALID_ENUM, "glGetPixelTexGenParameterfvSGIS(target)");
-   }
-}
-
-
-void GLAPIENTRY
-_mesa_GetPixelTexGenParameterivSGIS(GLenum target, GLint *value)
-{
-   GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
-
-   if (target == GL_PIXEL_FRAGMENT_RGB_SOURCE_SGIS) {
-      *value = (GLint) ctx->Pixel.FragmentRgbSource;
-   }
-   else if (target == GL_PIXEL_FRAGMENT_ALPHA_SOURCE_SGIS) {
-      *value = (GLint) ctx->Pixel.FragmentAlphaSource;
-   }
-   else {
-      _mesa_error(ctx, GL_INVALID_ENUM, "glGetPixelTexGenParameterivSGIS(target)");
-   }
-}
-
-
-
 /**********************************************************************/
 /*****                    State management                        *****/
 /**********************************************************************/
@@ -2884,7 +2724,7 @@ update_texture_matrices( GLcontext *ctx )
    ctx->Texture._TexMatEnabled = 0;
 
    for (i=0; i < ctx->Const.MaxTextureUnits; i++) {
-      if (ctx->TextureMatrixStack[i].Top->flags & MAT_DIRTY) {
+      if (_math_matrix_is_dirty(ctx->TextureMatrixStack[i].Top)) {
         _math_matrix_analyse( ctx->TextureMatrixStack[i].Top );
 
         if (ctx->Texture.Unit[i]._ReallyEnabled &&
@@ -2898,8 +2738,6 @@ update_texture_matrices( GLcontext *ctx )
 }
 
 
-
-
 /**
  * \note This routine refers to derived texture matrix values to
  * compute the ENABLE_TEXMAT flags, but is only called on
@@ -2928,14 +2766,14 @@ update_texture_state( GLcontext *ctx )
     */
    for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
       struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
-      GLuint enableBits;
+      GLbitfield enableBits;
 
       texUnit->_Current = NULL;
       texUnit->_ReallyEnabled = 0;
       texUnit->_GenFlags = 0;
 
       /* Get the bitmask of texture enables */
-      if (ctx->FragmentProgram.Enabled && ctx->FragmentProgram.Current) {
+      if (ctx->FragmentProgram._Enabled) {
          enableBits = ctx->FragmentProgram.Current->TexturesUsed[unit];
       }
       else {
@@ -3014,11 +2852,12 @@ update_texture_state( GLcontext *ctx )
         texUnit->_CurrentCombine = & texUnit->Combine;
       }
       else {
-         GLenum format = texUnit->_Current->Image[0][0]->Format;
+         GLenum format = texUnit->_Current->Image[0][0]->_BaseFormat;
          if (format == GL_COLOR_INDEX) {
             format = GL_RGBA;  /* a bit of a hack */
          }
-         else if (format == GL_DEPTH_COMPONENT) {
+         else if (format == GL_DEPTH_COMPONENT
+                  || format == GL_DEPTH_STENCIL_EXT) {
             format = texUnit->_Current->DepthMode;
          }
         calculate_derived_texenv(&texUnit->_EnvMode, texUnit->EnvMode, format);
@@ -3099,9 +2938,9 @@ update_texture_state( GLcontext *ctx )
    /* Fragment programs may need texture coordinates but not the
     * corresponding texture images.
     */
-   if (ctx->FragmentProgram.Enabled && ctx->FragmentProgram.Current) {
+   if (ctx->FragmentProgram._Enabled) {
       ctx->Texture._EnabledCoordUnits |=
-         (ctx->FragmentProgram.Current->InputsRead >> FRAG_ATTRIB_TEX0);
+         (ctx->FragmentProgram.Current->Base.InputsRead >> FRAG_ATTRIB_TEX0);
    }
 }
 
@@ -3115,6 +2954,7 @@ void _mesa_update_texture( GLcontext *ctx, GLuint new_state )
       update_texture_state( ctx );
 }
 
+
 /**********************************************************************/
 /*****                      Initialization                        *****/
 /**********************************************************************/
@@ -3215,9 +3055,13 @@ init_texture_unit( GLcontext *ctx, GLuint unit )
 }
 
 
-GLboolean _mesa_init_texture( GLcontext * ctx )
+/**
+ * Initialize texture state for the given context.
+ */
+GLboolean
+_mesa_init_texture(GLcontext *ctx)
 {
-   int i;
+   GLuint i;
 
    assert(MAX_TEXTURE_LEVELS >= MAX_3D_TEXTURE_LEVELS);
    assert(MAX_TEXTURE_LEVELS >= MAX_CUBE_TEXTURE_LEVELS);
@@ -3244,9 +3088,14 @@ GLboolean _mesa_init_texture( GLcontext * ctx )
    return GL_TRUE;
 }
 
-void _mesa_free_texture_data( GLcontext *ctx )
+
+/**
+ * Free dynamically-allocted texture data attached to the given context.
+ */
+void
+_mesa_free_texture_data(GLcontext *ctx)
 {
-   int i;
+   GLuint i;
 
    /* Free proxy texture objects */
    (ctx->Driver.DeleteTexture)(ctx,  ctx->Texture.Proxy1D );
@@ -3257,4 +3106,6 @@ void _mesa_free_texture_data( GLcontext *ctx )
 
    for (i = 0; i < MAX_TEXTURE_IMAGE_UNITS; i++)
       _mesa_free_colortable_data( &ctx->Texture.Unit[i].ColorTable );
+
+   _mesa_TexEnvProgramCacheDestroy( ctx );
 }