texFormat was a random value in _mesa_store_teximage3d(), fix by undoing last changes
[mesa.git] / src / mesa / main / light.c
index e5159253e6aacc4ceb13790aca601320446e40ee..ed449b9f58fbe7433fbbb56298969a12e61d085c 100644 (file)
@@ -1,21 +1,21 @@
-/* $Id: light.c,v 1.24 2000/11/13 20:02:56 keithw Exp $ */
+/* $Id: light.c,v 1.41 2001/03/12 00:48:38 gareth Exp $ */
 
 /*
  * Mesa 3-D graphics library
  * Version:  3.5
- * 
- * Copyright (C) 1999-2000  Brian Paul   All Rights Reserved.
- * 
+ *
+ * Copyright (C) 1999-2001  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"),
  * to deal in the Software without restriction, including without limitation
  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  * and/or sell copies of the Software, and to permit persons to whom the
  * Software is furnished to do so, subject to the following conditions:
- * 
+ *
  * The above copyright notice and this permission notice shall be included
  * in all copies or substantial portions of the Software.
- * 
+ *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 #include "enums.h"
 #include "light.h"
 #include "macros.h"
-#include "matrix.h"
 #include "mem.h"
 #include "mmath.h"
 #include "simple_list.h"
-#include "types.h"
-#include "vb.h"
-#include "xform.h"
+#include "mtypes.h"
+
+#include "math/m_xform.h"
+#include "math/m_matrix.h"
 #endif
 
 
@@ -54,28 +54,24 @@ void
 _mesa_ShadeModel( GLenum mode )
 {
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glShadeModel");
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
-      fprintf(stderr, "glShadeModel %s\n", gl_lookup_enum_by_nr(mode));
+      fprintf(stderr, "glShadeModel %s\n", _mesa_lookup_enum_by_nr(mode));
 
-   if (mode == GL_FLAT || mode == GL_SMOOTH) {
-      if (ctx->Light.ShadeModel != mode) {
-         ctx->Light.ShadeModel = mode;
-         if (ctx->Light.ShadeModel == GL_FLAT)
-            SET_BITS(ctx->_TriangleCaps, DD_FLATSHADE);
-         else
-            CLEAR_BITS(ctx->_TriangleCaps, DD_FLATSHADE);
+   if (mode != GL_FLAT && mode != GL_SMOOTH) {
+      _mesa_error( ctx, GL_INVALID_ENUM, "glShadeModel" );
+      return;
+   }
 
-         ctx->NewState |= _NEW_LIGHT;
+   if (ctx->Light.ShadeModel == mode)
+      return;
 
-         if (ctx->Driver.ShadeModel) 
-            (*ctx->Driver.ShadeModel)( ctx, mode );
-      }
-   }
-   else {
-      gl_error( ctx, GL_INVALID_ENUM, "glShadeModel" );
-   }
+   FLUSH_VERTICES(ctx, _NEW_LIGHT);
+   ctx->Light.ShadeModel = mode;
+   ctx->_TriangleCaps ^= DD_FLATSHADE;
+   if (ctx->Driver.ShadeModel)
+      (*ctx->Driver.ShadeModel)( ctx, mode );
 }
 
 
@@ -94,92 +90,122 @@ _mesa_Lightfv( GLenum light, GLenum pname, const GLfloat *params )
    GLint i = (GLint) (light - GL_LIGHT0);
    struct gl_light *l = &ctx->Light.Light[i];
 
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glLight");
-
-   if (i < 0 || i >= MAX_LIGHTS) {
-      gl_error( ctx, GL_INVALID_ENUM, "glLight" );
+   if (i < 0 || i >= (GLint) ctx->Const.MaxLights) {
+      _mesa_error( ctx, GL_INVALID_ENUM, "glLight" );
       return;
    }
 
    switch (pname) {
-      case GL_AMBIENT:
-         COPY_4V( l->Ambient, params );
-         break;
-      case GL_DIFFUSE:
-         COPY_4V( l->Diffuse, params );
-         break;
-      case GL_SPECULAR:
-         COPY_4V( l->Specular, params );
-         break;
-      case GL_POSITION:
-        /* transform position by ModelView matrix */
-        TRANSFORM_POINT( l->EyePosition, ctx->ModelView.m, params );
-        if (l->EyePosition[3] != 0.0F) 
-           l->_Flags |= LIGHT_POSITIONAL;
-        else
-           l->_Flags &= ~LIGHT_POSITIONAL;
-         break;
-      case GL_SPOT_DIRECTION:
-        /* transform direction by inverse modelview */
-        if (ctx->ModelView.flags & MAT_DIRTY_INVERSE) {
-           gl_matrix_analyze( &ctx->ModelView );
-        }
-        TRANSFORM_NORMAL( l->EyeDirection, params, ctx->ModelView.inv );
-         break;
-      case GL_SPOT_EXPONENT:
-         if (params[0]<0.0 || params[0]>128.0) {
-            gl_error( ctx, GL_INVALID_VALUE, "glLight" );
-            return;
-         }
-         if (l->SpotExponent != params[0]) {
-            l->SpotExponent = params[0];
-            gl_compute_spot_exp_table( l );
-         }
-         break;
-      case GL_SPOT_CUTOFF:
-         if ((params[0]<0.0 || params[0]>90.0) && params[0]!=180.0) {
-            gl_error( ctx, GL_INVALID_VALUE, "glLight" );
-            return;
-         }
-         l->SpotCutoff = params[0];
-         l->_CosCutoff = cos(params[0]*DEG2RAD);
-         if (l->_CosCutoff < 0) 
-           l->_CosCutoff = 0;
-        if (l->SpotCutoff != 180.0F)
-           l->_Flags |= LIGHT_SPOT;
-        else
-           l->_Flags &= ~LIGHT_SPOT;
-         break;
-      case GL_CONSTANT_ATTENUATION:
-         if (params[0]<0.0) {
-            gl_error( ctx, GL_INVALID_VALUE, "glLight" );
-            return;
-         }
-         l->ConstantAttenuation = params[0];
-         break;
-      case GL_LINEAR_ATTENUATION:
-         if (params[0]<0.0) {
-            gl_error( ctx, GL_INVALID_VALUE, "glLight" );
-            return;
-         }
-         l->LinearAttenuation = params[0];
-         break;
-      case GL_QUADRATIC_ATTENUATION:
-         if (params[0]<0.0) {
-            gl_error( ctx, GL_INVALID_VALUE, "glLight" );
-            return;
-         }
-         l->QuadraticAttenuation = params[0];
-         break;
-      default:
-         gl_error( ctx, GL_INVALID_ENUM, "glLight" );
-         return;
+   case GL_AMBIENT:
+      if (TEST_EQ_4V(l->Ambient, params))
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      COPY_4V( l->Ambient, params );
+      break;
+   case GL_DIFFUSE:
+      if (TEST_EQ_4V(l->Diffuse, params))
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      COPY_4V( l->Diffuse, params );
+      break;
+   case GL_SPECULAR:
+      if (TEST_EQ_4V(l->Specular, params))
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      COPY_4V( l->Specular, params );
+      break;
+   case GL_POSITION: {
+      GLfloat tmp[4];
+      /* transform position by ModelView matrix */
+      TRANSFORM_POINT( tmp, ctx->ModelView.m, params );
+      if (TEST_EQ_4V(l->EyePosition, tmp))
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      COPY_4V(l->EyePosition, tmp);
+      if (l->EyePosition[3] != 0.0F)
+        l->_Flags |= LIGHT_POSITIONAL;
+      else
+        l->_Flags &= ~LIGHT_POSITIONAL;
+      break;
+   }
+   case GL_SPOT_DIRECTION: {
+      GLfloat tmp[4];
+      /* transform direction by inverse modelview */
+      if (ctx->ModelView.flags & MAT_DIRTY_INVERSE) {
+        _math_matrix_analyse( &ctx->ModelView );
+      }
+      TRANSFORM_NORMAL( tmp, params, ctx->ModelView.inv );
+      if (TEST_EQ_3V(l->EyeDirection, tmp))
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      COPY_3V(l->EyeDirection, tmp);
+      break;
+   }
+   case GL_SPOT_EXPONENT:
+      if (params[0]<0.0 || params[0]>128.0) {
+        _mesa_error( ctx, GL_INVALID_VALUE, "glLight" );
+        return;
+      }
+      if (l->SpotExponent == params[0])
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      l->SpotExponent = params[0];
+      _mesa_invalidate_spot_exp_table( l );
+      break;
+   case GL_SPOT_CUTOFF:
+      if ((params[0]<0.0 || params[0]>90.0) && params[0]!=180.0) {
+        _mesa_error( ctx, GL_INVALID_VALUE, "glLight" );
+        return;
+      }
+      if (l->SpotCutoff == params[0])
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      l->SpotCutoff = params[0];
+      l->_CosCutoff = cos(params[0]*DEG2RAD);
+      if (l->_CosCutoff < 0)
+        l->_CosCutoff = 0;
+      if (l->SpotCutoff != 180.0F)
+        l->_Flags |= LIGHT_SPOT;
+      else
+        l->_Flags &= ~LIGHT_SPOT;
+      break;
+   case GL_CONSTANT_ATTENUATION:
+      if (params[0]<0.0) {
+        _mesa_error( ctx, GL_INVALID_VALUE, "glLight" );
+        return;
+      }
+      if (l->ConstantAttenuation == params[0])
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      l->ConstantAttenuation = params[0];
+      break;
+   case GL_LINEAR_ATTENUATION:
+      if (params[0]<0.0) {
+        _mesa_error( ctx, GL_INVALID_VALUE, "glLight" );
+        return;
+      }
+      if (l->LinearAttenuation == params[0])
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      l->LinearAttenuation = params[0];
+      break;
+   case GL_QUADRATIC_ATTENUATION:
+      if (params[0]<0.0) {
+        _mesa_error( ctx, GL_INVALID_VALUE, "glLight" );
+        return;
+      }
+      if (l->QuadraticAttenuation == params[0])
+        return;
+      FLUSH_VERTICES(ctx, _NEW_LIGHT);
+      l->QuadraticAttenuation = params[0];
+      break;
+   default:
+      _mesa_error( ctx, GL_INVALID_ENUM, "glLight" );
+      return;
    }
 
    if (ctx->Driver.Lightfv)
       ctx->Driver.Lightfv( ctx, light, pname, params );
-
-   ctx->NewState |= _NEW_LIGHT;
 }
 
 
@@ -237,11 +263,10 @@ _mesa_GetLightfv( GLenum light, GLenum pname, GLfloat *params )
 {
    GET_CURRENT_CONTEXT(ctx);
    GLint l = (GLint) (light - GL_LIGHT0);
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetLight");
-
-   if (l<0 || l>=MAX_LIGHTS) {
-      gl_error( ctx, GL_INVALID_ENUM, "glGetLightfv" );
+   if (l < 0 || l >= (GLint) ctx->Const.MaxLights) {
+      _mesa_error( ctx, GL_INVALID_ENUM, "glGetLightfv" );
       return;
    }
 
@@ -277,7 +302,7 @@ _mesa_GetLightfv( GLenum light, GLenum pname, GLfloat *params )
          params[0] = ctx->Light.Light[l].QuadraticAttenuation;
          break;
       default:
-         gl_error( ctx, GL_INVALID_ENUM, "glGetLightfv" );
+         _mesa_error( ctx, GL_INVALID_ENUM, "glGetLightfv" );
          break;
    }
 }
@@ -289,11 +314,10 @@ _mesa_GetLightiv( GLenum light, GLenum pname, GLint *params )
 {
    GET_CURRENT_CONTEXT(ctx);
    GLint l = (GLint) (light - GL_LIGHT0);
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetLight");
-
-   if (l<0 || l>=MAX_LIGHTS) {
-      gl_error( ctx, GL_INVALID_ENUM, "glGetLightiv" );
+   if (l < 0 || l >= (GLint) ctx->Const.MaxLights) {
+      _mesa_error( ctx, GL_INVALID_ENUM, "glGetLightiv" );
       return;
    }
 
@@ -343,7 +367,7 @@ _mesa_GetLightiv( GLenum light, GLenum pname, GLint *params )
          params[0] = (GLint) ctx->Light.Light[l].QuadraticAttenuation;
          break;
       default:
-         gl_error( ctx, GL_INVALID_ENUM, "glGetLightiv" );
+         _mesa_error( ctx, GL_INVALID_ENUM, "glGetLightiv" );
          break;
    }
 }
@@ -358,48 +382,61 @@ _mesa_GetLightiv( GLenum light, GLenum pname, GLint *params )
 void
 _mesa_LightModelfv( GLenum pname, const GLfloat *params )
 {
+   GLenum newenum;
+   GLboolean newbool;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glLightModelfv");
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    switch (pname) {
       case GL_LIGHT_MODEL_AMBIENT:
+         if (TEST_EQ_4V( ctx->Light.Model.Ambient, params ))
+           return;
+        FLUSH_VERTICES(ctx, _NEW_LIGHT);
          COPY_4V( ctx->Light.Model.Ambient, params );
          break;
       case GL_LIGHT_MODEL_LOCAL_VIEWER:
-         if (params[0]==0.0)
-            ctx->Light.Model.LocalViewer = GL_FALSE;
-         else
-            ctx->Light.Model.LocalViewer = GL_TRUE;
+         newbool = (params[0]!=0.0);
+        if (ctx->Light.Model.LocalViewer == newbool)
+           return;
+        FLUSH_VERTICES(ctx, _NEW_LIGHT);
+        ctx->Light.Model.LocalViewer = newbool;
          break;
       case GL_LIGHT_MODEL_TWO_SIDE:
-         if (params[0]==0.0) 
-            ctx->Light.Model.TwoSide = GL_FALSE;
-         else
-            ctx->Light.Model.TwoSide = GL_TRUE;
+         newbool = (params[0]!=0.0);
+        if (ctx->Light.Model.TwoSide == newbool)
+           return;
+        FLUSH_VERTICES(ctx, _NEW_LIGHT);
+        ctx->Light.Model.TwoSide = newbool;
          break;
       case GL_LIGHT_MODEL_COLOR_CONTROL:
-         if (params[0] == (GLfloat) GL_SINGLE_COLOR) {
-            ctx->Light.Model.ColorControl = GL_SINGLE_COLOR;
-           if (!ctx->Fog.ColorSumEnabled)
-              CLEAR_BITS(ctx->_TriangleCaps, DD_SEPERATE_SPECULAR);
-         }
-         else if (params[0] == (GLfloat) GL_SEPARATE_SPECULAR_COLOR) {
-            ctx->Light.Model.ColorControl = GL_SEPARATE_SPECULAR_COLOR;
-           SET_BITS(ctx->_TriangleCaps, DD_SEPERATE_SPECULAR);
-        }
-         else {
-            gl_error( ctx, GL_INVALID_ENUM, "glLightModel(param)" );
+         if (params[0] == (GLfloat) GL_SINGLE_COLOR)
+           newenum = GL_SINGLE_COLOR;
+         else if (params[0] == (GLfloat) GL_SEPARATE_SPECULAR_COLOR)
+           newenum = GL_SEPARATE_SPECULAR_COLOR;
+        else {
+            _mesa_error( ctx, GL_INVALID_ENUM, "glLightModel(param)" );
+           return;
          }
+        if (ctx->Light.Model.ColorControl == newenum)
+           return;
+        FLUSH_VERTICES(ctx, _NEW_LIGHT);
+        ctx->Light.Model.ColorControl = newenum;
+
+        if ((ctx->Light.Enabled &&
+             ctx->Light.Model.ColorControl==GL_SEPARATE_SPECULAR_COLOR)
+            || ctx->Fog.ColorSumEnabled)
+           ctx->_TriangleCaps |= DD_SEPERATE_SPECULAR;
+        else
+           ctx->_TriangleCaps &= ~DD_SEPERATE_SPECULAR;
+
          break;
       default:
-         gl_error( ctx, GL_INVALID_ENUM, "glLightModel" );
+         _mesa_error( ctx, GL_INVALID_ENUM, "glLightModel" );
          break;
    }
 
-   if (ctx->Driver.LightModelfv) 
+   if (ctx->Driver.LightModelfv)
       ctx->Driver.LightModelfv( ctx, pname, params );
-
-   ctx->NewState |= _NEW_LIGHT;
 }
 
 
@@ -407,8 +444,6 @@ void
 _mesa_LightModeliv( GLenum pname, const GLint *params )
 {
    GLfloat fparam[4];
-   GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glLightModeliv");
 
    switch (pname) {
       case GL_LIGHT_MODEL_AMBIENT:
@@ -452,9 +487,9 @@ _mesa_LightModelf( GLenum pname, GLfloat param )
  * Given a face and pname value (ala glColorMaterial), compute a bitmask
  * of the targeted material values.
  */
-GLuint gl_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname, 
-                           GLuint legal,
-                           const char *where )
+GLuint
+_mesa_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
+                        GLuint legal, const char *where )
 {
    GLuint bitmask = 0;
 
@@ -483,7 +518,7 @@ GLuint gl_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
          bitmask |= FRONT_INDEXES_BIT  | BACK_INDEXES_BIT;
          break;
       default:
-         gl_error( ctx, GL_INVALID_ENUM, where );
+         _mesa_error( ctx, GL_INVALID_ENUM, where );
          return 0;
    }
 
@@ -494,12 +529,12 @@ GLuint gl_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
       bitmask &= BACK_MATERIAL_BITS;
    }
    else if (face != GL_FRONT_AND_BACK) {
-      gl_error( ctx, GL_INVALID_ENUM, where );
+      _mesa_error( ctx, GL_INVALID_ENUM, where );
       return 0;
    }
-   
+
    if (bitmask & ~legal) {
-      gl_error( ctx, GL_INVALID_ENUM, where );
+      _mesa_error( ctx, GL_INVALID_ENUM, where );
       return 0;
    }
 
@@ -507,6 +542,54 @@ GLuint gl_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
 }
 
 
+/* Perform a straight copy between pairs of materials.
+ */
+void _mesa_copy_material_pairs( struct gl_material dst[2],
+                            const struct gl_material src[2],
+                            GLuint bitmask )
+{
+   if (bitmask & FRONT_EMISSION_BIT) {
+      COPY_4FV( dst[0].Emission, src[0].Emission );
+   }
+   if (bitmask & BACK_EMISSION_BIT) {
+      COPY_4FV( dst[1].Emission, src[1].Emission );
+   }
+   if (bitmask & FRONT_AMBIENT_BIT) {
+      COPY_4FV( dst[0].Ambient, src[0].Ambient );
+   }
+   if (bitmask & BACK_AMBIENT_BIT) {
+      COPY_4FV( dst[1].Ambient, src[1].Ambient );
+   }
+   if (bitmask & FRONT_DIFFUSE_BIT) {
+      COPY_4FV( dst[0].Diffuse, src[0].Diffuse );
+   }
+   if (bitmask & BACK_DIFFUSE_BIT) {
+      COPY_4FV( dst[1].Diffuse, src[1].Diffuse );
+   }
+   if (bitmask & FRONT_SPECULAR_BIT) {
+      COPY_4FV( dst[0].Specular, src[0].Specular );
+   }
+   if (bitmask & BACK_SPECULAR_BIT) {
+      COPY_4FV( dst[1].Specular, src[1].Specular );
+   }
+   if (bitmask & FRONT_SHININESS_BIT) {
+      dst[0].Shininess = src[0].Shininess;
+   }
+   if (bitmask & BACK_SHININESS_BIT) {
+      dst[1].Shininess = src[1].Shininess;
+   }
+   if (bitmask & FRONT_INDEXES_BIT) {
+      dst[0].AmbientIndex = src[0].AmbientIndex;
+      dst[0].DiffuseIndex = src[0].DiffuseIndex;
+      dst[0].SpecularIndex = src[0].SpecularIndex;
+   }
+   if (bitmask & BACK_INDEXES_BIT) {
+      dst[1].AmbientIndex = src[1].AmbientIndex;
+      dst[1].DiffuseIndex = src[1].DiffuseIndex;
+      dst[1].SpecularIndex = src[1].SpecularIndex;
+   }
+}
+
 
 /*
  * Check if the global material has to be updated with info that was
@@ -517,14 +600,10 @@ GLuint gl_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
  *
  * src[0] is front material, src[1] is back material
  *
- * KW: Added code here to keep the precomputed variables uptodate.
- *     This means we can use the faster shade functions when using
- *     GL_COLOR_MATERIAL, and we can also now use the precomputed 
- *     values in the slower shading functions, which further offsets
- *     the cost of doing this here.
+ * Additionally keeps the precomputed lighting state uptodate.
  */
-void gl_update_material( GLcontext *ctx, 
-                        const struct gl_material src[2], 
+void _mesa_update_material( GLcontext *ctx,
+                        const struct gl_material src[2],
                         GLuint bitmask )
 {
    struct gl_light *light, *list = &ctx->Light.EnabledList;
@@ -533,9 +612,9 @@ void gl_update_material( GLcontext *ctx,
       bitmask &= ~ctx->Light.ColorMaterialBitmask;
 
    if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
-      fprintf(stderr, "gl_update_material, mask 0x%x\n", bitmask);
+      fprintf(stderr, "_mesa_update_material, mask 0x%x\n", bitmask);
 
-   if (!bitmask) 
+   if (!bitmask)
       return;
 
    /* update material emission */
@@ -568,69 +647,57 @@ void gl_update_material( GLcontext *ctx,
    if (bitmask & (FRONT_EMISSION_BIT | FRONT_AMBIENT_BIT)) {
       struct gl_material *mat = &ctx->Light.Material[0];
       COPY_3V( ctx->Light._BaseColor[0], mat->Emission );
-      ACC_SCALE_3V( ctx->Light._BaseColor[0], mat->Ambient, ctx->Light.Model.Ambient );
+      ACC_SCALE_3V( ctx->Light._BaseColor[0], mat->Ambient,
+                   ctx->Light.Model.Ambient );
    }
    if (bitmask & (BACK_EMISSION_BIT | BACK_AMBIENT_BIT)) {
       struct gl_material *mat = &ctx->Light.Material[1];
       COPY_3V( ctx->Light._BaseColor[1], mat->Emission );
-      ACC_SCALE_3V( ctx->Light._BaseColor[1], mat->Ambient, ctx->Light.Model.Ambient );
+      ACC_SCALE_3V( ctx->Light._BaseColor[1], mat->Ambient,
+                   ctx->Light.Model.Ambient );
    }
 
    /* update material diffuse values */
    if (bitmask & FRONT_DIFFUSE_BIT) {
       struct gl_material *mat = &ctx->Light.Material[0];
-      GLfloat tmp[4];
-      SUB_3V( tmp, src[0].Diffuse, mat->Diffuse );
+      COPY_4FV( mat->Diffuse, src[0].Diffuse );
       foreach (light, list) {
-        ACC_SCALE_3V( light->_MatDiffuse[0], light->Diffuse, tmp );
+        SCALE_3V( light->_MatDiffuse[0], light->Diffuse, mat->Diffuse );
       }
-      COPY_4FV( mat->Diffuse, src[0].Diffuse );
-      FLOAT_COLOR_TO_CHAN(ctx->Light._BaseAlpha[0], mat->Diffuse[3]);
+      UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[0], mat->Diffuse[3]);
    }
    if (bitmask & BACK_DIFFUSE_BIT) {
       struct gl_material *mat = &ctx->Light.Material[1];
-      GLfloat tmp[4];
-      SUB_3V( tmp, src[1].Diffuse, mat->Diffuse );
+      COPY_4FV( mat->Diffuse, src[1].Diffuse );
       foreach (light, list) {
-        ACC_SCALE_3V( light->_MatDiffuse[1], light->Diffuse, tmp );
+        SCALE_3V( light->_MatDiffuse[1], light->Diffuse, mat->Diffuse );
       }
-      COPY_4FV( mat->Diffuse, src[1].Diffuse );
-      FLOAT_COLOR_TO_CHAN(ctx->Light._BaseAlpha[1], mat->Diffuse[3]);
+      UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[1], mat->Diffuse[3]);
    }
 
    /* update material specular values */
    if (bitmask & FRONT_SPECULAR_BIT) {
       struct gl_material *mat = &ctx->Light.Material[0];
-      GLfloat tmp[4];
-      SUB_3V( tmp, src[0].Specular, mat->Specular );
+      COPY_4FV( mat->Specular, src[0].Specular );
       foreach (light, list) {
-        ACC_SCALE_3V( light->_MatSpecular[0], light->Specular, tmp );
-        light->_IsMatSpecular[0] = 
-           (LEN_SQUARED_3FV(light->_MatSpecular[0]) > 1e-16);
+        SCALE_3V( light->_MatSpecular[0], light->Specular, mat->Specular);
       }
-      COPY_4FV( mat->Specular, src[0].Specular );
    }
    if (bitmask & BACK_SPECULAR_BIT) {
       struct gl_material *mat = &ctx->Light.Material[1];
-      GLfloat tmp[4];
-      SUB_3V( tmp, src[1].Specular, mat->Specular );
+      COPY_4FV( mat->Specular, src[1].Specular );
       foreach (light, list) {
-        ACC_SCALE_3V( light->_MatSpecular[1], light->Specular, tmp );
-        light->_IsMatSpecular[1] = 
-           (LEN_SQUARED_3FV(light->_MatSpecular[1]) > 1e-16);
+        SCALE_3V( light->_MatSpecular[1], light->Specular, mat->Specular);
       }
-      COPY_4FV( mat->Specular, src[1].Specular );
    }
 
    if (bitmask & FRONT_SHININESS_BIT) {
-      GLfloat shininess = ctx->Light.Material[0].Shininess = src[0].Shininess;
-      gl_compute_shine_table( ctx, 0, shininess );
-      gl_compute_shine_table( ctx, 2, shininess * .5 );
+      ctx->Light.Material[0].Shininess = src[0].Shininess;
+      _mesa_invalidate_shine_table( ctx, 0 );
    }
    if (bitmask & BACK_SHININESS_BIT) {
-      GLfloat shininess = ctx->Light.Material[1].Shininess = src[1].Shininess;
-      gl_compute_shine_table( ctx, 1, shininess );
-      gl_compute_shine_table( ctx, 3, shininess * .5 );
+      ctx->Light.Material[1].Shininess = src[1].Shininess;
+      _mesa_invalidate_shine_table( ctx, 1 );
    }
 
    if (bitmask & FRONT_INDEXES_BIT) {
@@ -669,12 +736,15 @@ void gl_update_material( GLcontext *ctx,
 
 
 
+
+
+
 /*
  * Update the current materials from the given rgba color
  * according to the bitmask in ColorMaterialBitmask, which is
  * set by glColorMaterial().
  */
-void gl_update_color_material( GLcontext *ctx, 
+void _mesa_update_color_material( GLcontext *ctx,
                               const GLchan rgba[4] )
 {
    struct gl_light *light, *list = &ctx->Light.EnabledList;
@@ -687,7 +757,7 @@ void gl_update_color_material( GLcontext *ctx,
    color[3] = CHAN_TO_FLOAT(rgba[3]);
 
    if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
-      fprintf(stderr, "gl_update_color_material, mask 0x%x\n", bitmask);
+      fprintf(stderr, "_mesa_update_color_material, mask 0x%x\n", bitmask);
 
    /* update emissive colors */
    if (bitmask & FRONT_EMISSION_BIT) {
@@ -733,49 +803,37 @@ void gl_update_color_material( GLcontext *ctx,
    /* update light->_MatDiffuse = light's diffuse * material's diffuse */
    if (bitmask & FRONT_DIFFUSE_BIT) {
       struct gl_material *mat = &ctx->Light.Material[0];
-      GLfloat tmp[4];
-      SUB_3V( tmp, color, mat->Diffuse );
+      COPY_4FV( mat->Diffuse, color );
       foreach (light, list) {
-        ACC_SCALE_3V( light->_MatDiffuse[0], light->Diffuse, tmp );
+        SCALE_3V( light->_MatDiffuse[0], light->Diffuse, mat->Diffuse );
       }
-      COPY_4FV( mat->Diffuse, color );
-      FLOAT_COLOR_TO_CHAN(ctx->Light._BaseAlpha[0], mat->Diffuse[3]);
+      UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[0], mat->Diffuse[3]);
    }
 
    if (bitmask & BACK_DIFFUSE_BIT) {
       struct gl_material *mat = &ctx->Light.Material[1];
-      GLfloat tmp[4];
-      SUB_3V( tmp, color, mat->Diffuse );
+      COPY_4FV( mat->Diffuse, color );
       foreach (light, list) {
-        ACC_SCALE_3V( light->_MatDiffuse[1], light->Diffuse, tmp );
+        SCALE_3V( light->_MatDiffuse[1], light->Diffuse, mat->Diffuse );
       }
-      COPY_4FV( mat->Diffuse, color );
-      FLOAT_COLOR_TO_CHAN(ctx->Light._BaseAlpha[1], mat->Diffuse[3]);
+      UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[1], mat->Diffuse[3]);
    }
 
    /* update light->_MatSpecular = light's specular * material's specular */
    if (bitmask & FRONT_SPECULAR_BIT) {
       struct gl_material *mat = &ctx->Light.Material[0];
-      GLfloat tmp[4];
-      SUB_3V( tmp, color, mat->Specular );
+      COPY_4FV( mat->Specular, color );
       foreach (light, list) {
-        ACC_SCALE_3V( light->_MatSpecular[0], light->Specular, tmp );
-        light->_IsMatSpecular[0] = 
-           (LEN_SQUARED_3FV(light->_MatSpecular[0]) > 1e-16);
+        ACC_SCALE_3V( light->_MatSpecular[0], light->Specular, mat->Specular);
       }
-      COPY_4FV( mat->Specular, color );
    }
 
    if (bitmask & BACK_SPECULAR_BIT) {
       struct gl_material *mat = &ctx->Light.Material[1];
-      GLfloat tmp[4];
-      SUB_3V( tmp, color, mat->Specular );
+      COPY_4FV( mat->Specular, color );
       foreach (light, list) {
-        ACC_SCALE_3V( light->_MatSpecular[1], light->Specular, tmp );
-        light->_IsMatSpecular[1] = 
-           (LEN_SQUARED_3FV(light->_MatSpecular[1]) > 1e-16);
+        ACC_SCALE_3V( light->_MatSpecular[1], light->Specular, mat->Specular);
       }
-      COPY_4FV( mat->Specular, color );
    }
 
    if (0)
@@ -812,164 +870,41 @@ _mesa_ColorMaterial( GLenum face, GLenum mode )
                   FRONT_SPECULAR_BIT | BACK_SPECULAR_BIT |
                   FRONT_DIFFUSE_BIT  | BACK_DIFFUSE_BIT  |
                   FRONT_AMBIENT_BIT  | BACK_AMBIENT_BIT);
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glColorMaterial");
-
-   if (MESA_VERBOSE&VERBOSE_API) 
-      fprintf(stderr, "glColorMaterial %s %s\n", 
-             gl_lookup_enum_by_nr(face),
-             gl_lookup_enum_by_nr(mode));
-
-   bitmask = gl_material_bitmask( ctx, face, mode, legal, "glColorMaterial" );
-
-   if (bitmask != 0) {
-      ctx->Light.ColorMaterialBitmask = bitmask;
-      ctx->Light.ColorMaterialFace = face;
-      ctx->Light.ColorMaterialMode = mode;
-   }
-
-   if (ctx->Light.ColorMaterialEnabled)
-      gl_update_color_material( ctx, ctx->Current.Color );
-
-   ctx->NewState |= _NEW_LIGHT;
-}
-
-
-
-
-void
-_mesa_Materialf( GLenum face, GLenum pname, GLfloat param )
-{
-   _mesa_Materialfv( face, pname, &param );
-}
-
+   if (MESA_VERBOSE&VERBOSE_API)
+      fprintf(stderr, "glColorMaterial %s %s\n",
+             _mesa_lookup_enum_by_nr(face),
+             _mesa_lookup_enum_by_nr(mode));
 
-/* KW:  This is now called directly (ie by name) from the glMaterial* 
- *      API functions.
- */
-void
-_mesa_Materialfv( GLenum face, GLenum pname, const GLfloat *params )
-{
-   GET_CURRENT_CONTEXT(ctx);
-   struct immediate *IM;
-   struct gl_material *mat;
-   GLuint bitmask;
-   GLuint count;
+   bitmask = _mesa_material_bitmask(ctx, face, mode, legal, "glColorMaterial");
 
-   bitmask = gl_material_bitmask( ctx, face, pname, ~0, "gl_Materialfv" );
-   if (bitmask == 0)
+   if (ctx->Light.ColorMaterialBitmask == bitmask &&
+       ctx->Light.ColorMaterialFace == face &&
+       ctx->Light.ColorMaterialMode == mode)
       return;
 
-   IM = ctx->input;
-   count = IM->Count;
+   FLUSH_VERTICES(ctx, _NEW_LIGHT);
+   ctx->Light.ColorMaterialBitmask = bitmask;
+   ctx->Light.ColorMaterialFace = face;
+   ctx->Light.ColorMaterialMode = mode;
 
-   if (!IM->Material) {
-      IM->Material = 
-        (struct gl_material (*)[2]) MALLOC( sizeof(struct gl_material) * 
-                                            VB_SIZE * 2 );
-      IM->MaterialMask = (GLuint *) MALLOC( sizeof(GLuint) * VB_SIZE );
-   }
-
-
-   if (!(IM->Flag[count] & VERT_MATERIAL)) {
-      IM->Flag[count] |= VERT_MATERIAL;
-      IM->MaterialMask[count] = 0;      
-   }
-
-
-   IM->MaterialMask[count] |= bitmask;
-   mat = IM->Material[count];
-
-   if (bitmask & FRONT_AMBIENT_BIT) {
-      COPY_4FV( mat[0].Ambient, params );
-   }
-   if (bitmask & BACK_AMBIENT_BIT) {
-      COPY_4FV( mat[1].Ambient, params );
-   }
-   if (bitmask & FRONT_DIFFUSE_BIT) {
-      COPY_4FV( mat[0].Diffuse, params );
-   }
-   if (bitmask & BACK_DIFFUSE_BIT) {
-      COPY_4FV( mat[1].Diffuse, params );
-   }
-   if (bitmask & FRONT_SPECULAR_BIT) {
-      COPY_4FV( mat[0].Specular, params );
-   }
-   if (bitmask & BACK_SPECULAR_BIT) {
-      COPY_4FV( mat[1].Specular, params );
-   }
-   if (bitmask & FRONT_EMISSION_BIT) {
-      COPY_4FV( mat[0].Emission, params );
-   }
-   if (bitmask & BACK_EMISSION_BIT) {
-      COPY_4FV( mat[1].Emission, params );
-   }
-   if (bitmask & FRONT_SHININESS_BIT) {
-      GLfloat shininess = CLAMP( params[0], 0.0F, 128.0F );
-      mat[0].Shininess = shininess;
-   }
-   if (bitmask & BACK_SHININESS_BIT) {
-      GLfloat shininess = CLAMP( params[0], 0.0F, 128.0F );
-      mat[1].Shininess = shininess;
-   }
-   if (bitmask & FRONT_INDEXES_BIT) {
-      mat[0].AmbientIndex = params[0];
-      mat[0].DiffuseIndex = params[1];
-      mat[0].SpecularIndex = params[2];
-   }
-   if (bitmask & BACK_INDEXES_BIT) {
-      mat[1].AmbientIndex = params[0];
-      mat[1].DiffuseIndex = params[1];
-      mat[1].SpecularIndex = params[2];
+   if (ctx->Light.ColorMaterialEnabled) {
+      FLUSH_CURRENT( ctx, 0 );
+      _mesa_update_color_material( ctx, ctx->Current.Color );
    }
 }
 
 
-void
-_mesa_Materiali(GLenum face, GLenum pname, GLint param )
-{
-   _mesa_Materialiv(face, pname, &param);
-}
 
 
-void
-_mesa_Materialiv(GLenum face, GLenum pname, const GLint *params )
-{
-   GLfloat fparam[4];
-   switch (pname) {
-      case GL_AMBIENT:
-      case GL_DIFFUSE:
-      case GL_SPECULAR:
-      case GL_EMISSION:
-      case GL_AMBIENT_AND_DIFFUSE:
-         fparam[0] = INT_TO_FLOAT( params[0] );
-         fparam[1] = INT_TO_FLOAT( params[1] );
-         fparam[2] = INT_TO_FLOAT( params[2] );
-         fparam[3] = INT_TO_FLOAT( params[3] );
-         break;
-      case GL_SHININESS:
-         fparam[0] = (GLfloat) params[0];
-         break;
-      case GL_COLOR_INDEXES:
-         fparam[0] = (GLfloat) params[0];
-         fparam[1] = (GLfloat) params[1];
-         fparam[2] = (GLfloat) params[2];
-         break;
-      default:
-         /* Error will be caught later in gl_Materialfv */
-         ;
-   }
-   _mesa_Materialfv(face, pname, fparam);
-}
-
 
 void
 _mesa_GetMaterialfv( GLenum face, GLenum pname, GLfloat *params )
 {
    GET_CURRENT_CONTEXT(ctx);
    GLuint f;
-
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetMaterialfv");
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* update materials */
 
    if (face==GL_FRONT) {
       f = 0;
@@ -978,7 +913,7 @@ _mesa_GetMaterialfv( GLenum face, GLenum pname, GLfloat *params )
       f = 1;
    }
    else {
-      gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(face)" );
+      _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(face)" );
       return;
    }
    switch (pname) {
@@ -1003,7 +938,7 @@ _mesa_GetMaterialfv( GLenum face, GLenum pname, GLfloat *params )
         params[2] = ctx->Light.Material[f].SpecularIndex;
         break;
       default:
-         gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" );
+         _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" );
    }
 }
 
@@ -1014,8 +949,7 @@ _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
 {
    GET_CURRENT_CONTEXT(ctx);
    GLuint f;
-
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glGetMaterialiv");
+   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* update materials */
 
    if (face==GL_FRONT) {
       f = 0;
@@ -1024,7 +958,7 @@ _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
       f = 1;
    }
    else {
-      gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialiv(face)" );
+      _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialiv(face)" );
       return;
    }
    switch (pname) {
@@ -1061,7 +995,7 @@ _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
         params[2] = ROUNDF( ctx->Light.Material[f].SpecularIndex );
         break;
       default:
-         gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" );
+         _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" );
    }
 }
 
@@ -1080,7 +1014,7 @@ _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
  *   orientation of the facet is later learned, we can determine which
  *   color (or index) to use for rendering.
  *
- *   KW: We now know orientation in advance and only shade for 
+ *   KW: We now know orientation in advance and only shade for
  *       the side or sides which are actually required.
  *
  * Variables:
@@ -1094,7 +1028,7 @@ _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
  *       // light at infinity
  *       IF local_viewer THEN
  *           _VP_inf_norm = unit vector from V to P      // Precompute
- *       ELSE 
+ *       ELSE
  *           // eye at infinity
  *           _h_inf_norm = Normalize( VP + <0,0,1> )     // Precompute
  *       ENDIF
@@ -1112,7 +1046,12 @@ _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
  * this function to recompute the exponent lookup table.
  */
 void
-gl_compute_spot_exp_table( struct gl_light *l )
+_mesa_invalidate_spot_exp_table( struct gl_light *l )
+{
+   l->_SpotExpTable[0][0] = -1;
+}
+
+static void validate_spot_exp_table( struct gl_light *l )
 {
    GLint i;
    GLdouble exponent = l->SpotExponent;
@@ -1123,16 +1062,17 @@ gl_compute_spot_exp_table( struct gl_light *l )
 
    for (i = EXP_TABLE_SIZE - 1; i > 0 ;i--) {
       if (clamp == 0) {
-         tmp = pow(i / (GLdouble) (EXP_TABLE_SIZE - 1), exponent);
-         if (tmp < FLT_MIN * 100.0) {
-            tmp = 0.0;
-            clamp = 1;
-         }
+        tmp = pow(i / (GLdouble) (EXP_TABLE_SIZE - 1), exponent);
+        if (tmp < FLT_MIN * 100.0) {
+           tmp = 0.0;
+           clamp = 1;
+        }
       }
       l->_SpotExpTable[i][0] = tmp;
    }
    for (i = 0; i < EXP_TABLE_SIZE - 1; i++) {
-      l->_SpotExpTable[i][1] = l->_SpotExpTable[i+1][0] - l->_SpotExpTable[i][0];
+      l->_SpotExpTable[i][1] = (l->_SpotExpTable[i+1][0] -
+                               l->_SpotExpTable[i][0]);
    }
    l->_SpotExpTable[EXP_TABLE_SIZE-1][1] = 0.0;
 }
@@ -1144,56 +1084,79 @@ gl_compute_spot_exp_table( struct gl_light *l )
  * lighting, and the cost of doing it early may be partially offset
  * by keeping a MRU cache of shine tables for various shine values.
  */
-static void
-compute_shine_table( struct gl_shine_tab *tab, GLfloat shininess )
+void
+_mesa_invalidate_shine_table( GLcontext *ctx, GLuint i )
 {
-   GLint i;
-   GLfloat *m = tab->tab;
-
-   m[0] = 0.0;
-   if (shininess == 0.0) {
-      for (i = 1 ; i <= SHINE_TABLE_SIZE ; i++)
-        m[i] = 1.0;
-   }
-   else {
-      for (i = 1 ; i < SHINE_TABLE_SIZE ; i++) {
-        GLdouble t = pow(i / (GLfloat) (SHINE_TABLE_SIZE - 1), shininess);
-        if (t > 1e-20)
-            m[i] = t;
-         else
-            m[i] = 0.0;
-      }      
-      m[SHINE_TABLE_SIZE] = 1.0;
-   }
-
-   tab->shininess = shininess;
+   if (ctx->_ShineTable[i])
+      ctx->_ShineTable[i]->refcount--;
+   ctx->_ShineTable[i] = 0;
 }
 
-
-void
-gl_compute_shine_table( GLcontext *ctx, GLuint i, GLfloat shininess )
+static void validate_shine_table( GLcontext *ctx, GLuint i, GLfloat shininess )
 {
-#define DISTSQR(a,b) ((a-b)*(a-b))
    struct gl_shine_tab *list = ctx->_ShineTabList;
    struct gl_shine_tab *s;
 
-   foreach(s, list) 
-      if ( DISTSQR(s->shininess, shininess) < 1e-4 ) 
+   foreach(s, list)
+      if ( s->shininess == shininess )
         break;
 
    if (s == list) {
-      foreach(s, list) 
+      GLint j;
+      GLfloat *m;
+
+      foreach(s, list)
         if (s->refcount == 0)
-            break;
+           break;
+
+      m = s->tab;
+      m[0] = 0.0;
+      if (shininess == 0.0) {
+        for (j = 1 ; j <= SHINE_TABLE_SIZE ; j++)
+           m[j] = 1.0;
+      }
+      else {
+        for (j = 1 ; j < SHINE_TABLE_SIZE ; j++) {
+            GLdouble t, x = j / (GLfloat) (SHINE_TABLE_SIZE - 1);
+            if (x < 0.005) /* underflow check */
+               x = 0.005;
+            t = pow(x, shininess);
+           if (t > 1e-20)
+              m[j] = t;
+           else
+              m[j] = 0.0;
+        }
+        m[SHINE_TABLE_SIZE] = 1.0;
+      }
 
-      compute_shine_table( s, shininess );
+      s->shininess = shininess;
    }
 
-   ctx->_ShineTable[i]->refcount--;
+   if (ctx->_ShineTable[i])
+      ctx->_ShineTable[i]->refcount--;
+
    ctx->_ShineTable[i] = s;
    move_to_tail( list, s );
    s->refcount++;
-#undef DISTSQR
+}
+
+void
+_mesa_validate_all_lighting_tables( GLcontext *ctx )
+{
+   GLint i;
+   GLfloat shininess;
+
+   shininess = ctx->Light.Material[0].Shininess;
+   if (!ctx->_ShineTable[0] || ctx->_ShineTable[0]->shininess != shininess)
+      validate_shine_table( ctx, 0, shininess );
+
+   shininess = ctx->Light.Material[1].Shininess;
+   if (!ctx->_ShineTable[1] || ctx->_ShineTable[1]->shininess != shininess)
+      validate_shine_table( ctx, 1, shininess );
+
+   for (i = 0 ; i < MAX_LIGHTS ; i++)
+      if (ctx->Light.Light[i]._SpotExpTable[0][0] == -1)
+        validate_spot_exp_table( &ctx->Light.Light[i] );
 }
 
 
@@ -1206,73 +1169,74 @@ gl_compute_shine_table( GLcontext *ctx, GLuint i, GLfloat shininess )
  * source and material ambient, diffuse and specular coefficients.
  */
 void
-gl_update_lighting( GLcontext *ctx )
+_mesa_update_lighting( GLcontext *ctx )
 {
    struct gl_light *light;
-   ctx->_TriangleCaps &= ~(DD_TRI_LIGHT_TWOSIDE|DD_LIGHTING_CULL);
+   ctx->_TriangleCaps &= ~DD_TRI_LIGHT_TWOSIDE;
    ctx->_NeedEyeCoords &= ~NEED_EYE_LIGHT;
    ctx->_NeedNormals &= ~NEED_NORMALS_LIGHT;
    ctx->Light._Flags = 0;
-   
-   if (!ctx->Light.Enabled) 
+
+   if (!ctx->Light.Enabled)
       return;
 
    ctx->_NeedNormals |= NEED_NORMALS_LIGHT;
 
    if (ctx->Light.Model.TwoSide)
-      ctx->_TriangleCaps |= (DD_TRI_LIGHT_TWOSIDE|DD_LIGHTING_CULL);
+      ctx->_TriangleCaps |= DD_TRI_LIGHT_TWOSIDE;
 
    foreach(light, &ctx->Light.EnabledList) {
       ctx->Light._Flags |= light->_Flags;
    }
 
-   ctx->Light._NeedVertices = 
+   ctx->Light._NeedVertices =
       ((ctx->Light._Flags & (LIGHT_POSITIONAL|LIGHT_SPOT)) ||
        ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR ||
        ctx->Light.Model.LocalViewer);
-   
-   if ((ctx->Light._Flags & LIGHT_POSITIONAL) || 
-       ctx->Light.Model.LocalViewer) 
+
+   if ((ctx->Light._Flags & LIGHT_POSITIONAL) ||
+       ctx->Light.Model.LocalViewer)
       ctx->_NeedEyeCoords |= NEED_EYE_LIGHT;
-      
-   
+
+
    /* XXX: This test is overkill & needs to be fixed both for software and
     * hardware t&l drivers.  The above should be sufficient & should
-    * be tested to verify this.  
+    * be tested to verify this.
     */
    if (ctx->Light._NeedVertices)
       ctx->_NeedEyeCoords |= NEED_EYE_LIGHT;
-  
 
-   /* Precompute some shading values.
+
+   /* Precompute some shading values.  Although we reference
+    * Light.Material here, we can get away without flushing
+    * FLUSH_UPDATE_CURRENT, as when any outstanding material changes
+    * are flushed, they will update the derived state at that time.
     */
-   if (ctx->Visual.RGBAflag) {
+   if (ctx->Visual.rgbMode) {
       GLuint sides = ctx->Light.Model.TwoSide ? 2 : 1;
       GLuint side;
       for (side=0; side < sides; side++) {
         struct gl_material *mat = &ctx->Light.Material[side];
 
         COPY_3V(ctx->Light._BaseColor[side], mat->Emission);
-        ACC_SCALE_3V(ctx->Light._BaseColor[side], 
+        ACC_SCALE_3V(ctx->Light._BaseColor[side],
                      ctx->Light.Model.Ambient,
                      mat->Ambient);
 
-        FLOAT_COLOR_TO_CHAN(ctx->Light._BaseAlpha[side],
-                             ctx->Light.Material[side].Diffuse[3] );
+        UNCLAMPED_FLOAT_TO_CHAN(ctx->Light._BaseAlpha[side],
+                                 ctx->Light.Material[side].Diffuse[3] );
       }
-      
-      foreach (light, &ctx->Light.EnabledList) {        
+
+      foreach (light, &ctx->Light.EnabledList) {
         for (side=0; side< sides; side++) {
            const struct gl_material *mat = &ctx->Light.Material[side];
            SCALE_3V( light->_MatDiffuse[side], light->Diffuse, mat->Diffuse );
            SCALE_3V( light->_MatAmbient[side], light->Ambient, mat->Ambient );
            SCALE_3V( light->_MatSpecular[side], light->Specular,
                      mat->Specular);
-           light->_IsMatSpecular[side] = 
-              (LEN_SQUARED_3FV(light->_MatSpecular[side]) > 1e-16);
         }
       }
-   } 
+   }
    else {
       static const GLfloat ci[3] = { .30, .59, .11 };
       foreach(light, &ctx->Light.EnabledList) {
@@ -1280,8 +1244,6 @@ gl_update_lighting( GLcontext *ctx )
         light->_sli = DOT3(ci, light->Specular);
       }
    }
-
-   gl_update_lighting_function(ctx);
 }
 
 
@@ -1293,7 +1255,7 @@ gl_update_lighting( GLcontext *ctx )
  * Also update on lighting space changes.
  */
 void
-gl_compute_light_positions( GLcontext *ctx )
+_mesa_compute_light_positions( GLcontext *ctx )
 {
    struct gl_light *light;
    static const GLfloat eye_z[3] = { 0, 0, 1 };
@@ -1307,14 +1269,14 @@ gl_compute_light_positions( GLcontext *ctx )
    else {
       TRANSFORM_NORMAL( ctx->_EyeZDir, eye_z, ctx->ModelView.m );
    }
-   
+
    foreach (light, &ctx->Light.EnabledList) {
 
       if (ctx->_NeedEyeCoords) {
         COPY_4FV( light->_Position, light->EyePosition );
       }
       else {
-        TRANSFORM_POINT( light->_Position, ctx->ModelView.inv, 
+        TRANSFORM_POINT( light->_Position, ctx->ModelView.inv,
                          light->EyePosition );
       }
 
@@ -1330,13 +1292,13 @@ gl_compute_light_positions( GLcontext *ctx )
         }
         light->_VP_inf_spot_attenuation = 1.0;
       }
-      
+
       if (light->_Flags & LIGHT_SPOT) {
         if (ctx->_NeedEyeCoords) {
            COPY_3V( light->_NormDirection, light->EyeDirection );
         }
          else {
-           TRANSFORM_NORMAL( light->_NormDirection, 
+           TRANSFORM_NORMAL( light->_NormDirection,
                              light->EyeDirection,
                              ctx->ModelView.m);
         }
@@ -1344,14 +1306,14 @@ gl_compute_light_positions( GLcontext *ctx )
         NORMALIZE_3FV( light->_NormDirection );
 
         if (!(light->_Flags & LIGHT_POSITIONAL)) {
-           GLfloat PV_dot_dir = - DOT3(light->_VP_inf_norm, 
+           GLfloat PV_dot_dir = - DOT3(light->_VP_inf_norm,
                                        light->_NormDirection);
 
            if (PV_dot_dir > light->_CosCutoff) {
               double x = PV_dot_dir * (EXP_TABLE_SIZE-1);
               int k = (int) x;
-              light->_VP_inf_spot_attenuation = 
-                 (light->_SpotExpTable[k][0] + 
+              light->_VP_inf_spot_attenuation =
+                 (light->_SpotExpTable[k][0] +
                   (x-k)*light->_SpotExpTable[k][1]);
            }
            else {
@@ -1361,56 +1323,3 @@ gl_compute_light_positions( GLcontext *ctx )
       }
    }
 }
-
-
-/* _NEW_TRANSFORM
- * _NEW_MODELVIEW
- * _TNL_NEW_NEED_NORMALS    
- * _TNL_NEW_NEED_EYE_COORDS
- *
- * Update on (_NEW_TRANSFORM|_NEW_MODELVIEW)
- * And also on NewLightingSpaces() callback.
- */
-void
-gl_update_normal_transform( GLcontext *ctx )
-{
-
-   if (!ctx->_NeedNormals) {
-      ctx->_NormalTransform = 0;
-      return;
-   }
-
-   if (ctx->_NeedEyeCoords) {
-      GLuint transform = NORM_TRANSFORM_NO_ROT;
-
-      if (ctx->ModelView.flags & (MAT_FLAG_GENERAL |
-                                 MAT_FLAG_ROTATION |
-                                 MAT_FLAG_GENERAL_3D |
-                                 MAT_FLAG_PERSPECTIVE)) 
-        transform = NORM_TRANSFORM;
-           
-              
-      if (ctx->Transform.Normalize) {
-        ctx->_NormalTransform = gl_normal_tab[transform | NORM_NORMALIZE];
-      } 
-      else if (ctx->Transform.RescaleNormals &&
-              ctx->_ModelViewInvScale != 1.0) {
-        ctx->_NormalTransform = gl_normal_tab[transform | NORM_RESCALE];
-      }
-      else {
-        ctx->_NormalTransform = gl_normal_tab[transform];
-      }
-   }
-   else {
-      if (ctx->Transform.Normalize) {
-        ctx->_NormalTransform = gl_normal_tab[NORM_NORMALIZE];
-      }
-      else if (!ctx->Transform.RescaleNormals &&
-              ctx->_ModelViewInvScale != 1.0) {
-        ctx->_NormalTransform = gl_normal_tab[NORM_RESCALE];
-      }
-      else {
-        ctx->_NormalTransform = 0;
-      }
-   }
-}