Header file clean-up:
[mesa.git] / src / mesa / main / light.c
index 715084bee62cd523845424a624902d365066400b..638be5e8353cf2f78996dd77690104dbe3b535f4 100644 (file)
@@ -1,21 +1,21 @@
-/* $Id: light.c,v 1.3 1999/09/30 11:18:22 keithw Exp $ */
+/* $Id: light.c,v 1.53 2002/10/24 23:57:21 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
- * Version:  3.1
- * 
- * Copyright (C) 1999  Brian Paul   All Rights Reserved.
- * 
+ * Version:  4.1
+ *
+ * Copyright (C) 1999-2002  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
  */
 
 
-
-
-
-#ifdef PC_HEADER
-#include "all.h"
-#else
-#include <assert.h>
-#include <float.h>
-#include <math.h>
-#include <stdlib.h>
-#include <stdio.h>
+#include "glheader.h"
+#include "imports.h"
+#include "colormac.h"
 #include "context.h"
 #include "enums.h"
 #include "light.h"
 #include "macros.h"
-#include "matrix.h"
 #include "mmath.h"
 #include "simple_list.h"
-#include "types.h"
-#include "vb.h"
-#include "xform.h"
-#ifdef XFree86Server
-#include "GL/xf86glx.h"
-#endif
-#endif
+#include "mtypes.h"
+#include "math/m_xform.h"
+#include "math/m_matrix.h"
+
 
+/* XXX this is a bit of a hack needed for compilation within XFree86 */
+#ifndef FLT_MIN
+#define FLT_MIN 1e-37
+#endif
 
 
-void gl_ShadeModel( GLcontext *ctx, GLenum mode )
+void
+_mesa_ShadeModel( GLenum mode )
 {
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glShadeModel");
+   GET_CURRENT_CONTEXT(ctx);
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
-      fprintf(stderr, "glShadeModel %s\n", gl_lookup_enum_by_nr(mode));
-
+      _mesa_debug(ctx, "glShadeModel %s\n", _mesa_lookup_enum_by_nr(mode));
 
-   switch (mode) {
-   case GL_FLAT:
-   case GL_SMOOTH:
-      if (ctx->Light.ShadeModel!=mode) {
-        ctx->Light.ShadeModel = mode;
-        ctx->TriangleCaps ^= DD_FLATSHADE;
-        ctx->NewState |= NEW_RASTER_OPS;
-      }
-      break;
-   default:
-      gl_error( ctx, GL_INVALID_ENUM, "glShadeModel" );
+   if (mode != GL_FLAT && mode != GL_SMOOTH) {
+      _mesa_error( ctx, GL_INVALID_ENUM, "glShadeModel" );
+      return;
    }
 
-   if (ctx->Driver.ShadeModel) 
+   if (ctx->Light.ShadeModel == mode)
+      return;
+
+   FLUSH_VERTICES(ctx, _NEW_LIGHT);
+   ctx->Light.ShadeModel = mode;
+   ctx->_TriangleCaps ^= DD_FLATSHADE;
+   if (ctx->Driver.ShadeModel)
       (*ctx->Driver.ShadeModel)( ctx, mode );
 }
 
 
 
-void gl_Lightfv( GLcontext *ctx,
-                 GLenum light, GLenum pname, const GLfloat *params,
-                 GLint nparams )
+void
+_mesa_Lightf( GLenum light, GLenum pname, GLfloat param )
 {
-   GLint l;
+   _mesa_Lightfv( light, pname, &param );
+}
 
-   (void) nparams;
 
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glLight");
+void
+_mesa_Lightfv( GLenum light, GLenum pname, const GLfloat *params )
+{
+   GET_CURRENT_CONTEXT(ctx);
+   GLint i = (GLint) (light - GL_LIGHT0);
+   struct gl_light *l = &ctx->Light.Light[i];
 
-   l = (GLint) (light - GL_LIGHT0);
+   if (i < 0 || i >= (GLint) ctx->Const.MaxLights) {
+      _mesa_error( ctx, GL_INVALID_ENUM, "glLight(light=0x%x)", light );
+      return;
+   }
 
-   if (l<0 || l>=MAX_LIGHTS) {
-      gl_error( ctx, GL_INVALID_ENUM, "glLight" );
+   switch (pname) {
+   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->ModelviewMatrixStack.Top->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->ModelviewMatrixStack.Top->flags & MAT_DIRTY_INVERSE) {
+        _math_matrix_analyse( ctx->ModelviewMatrixStack.Top );
+      }
+      TRANSFORM_NORMAL( tmp, params, ctx->ModelviewMatrixStack.Top->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 = (GLfloat) 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(pname=0x%x)", pname );
       return;
    }
 
+   if (ctx->Driver.Lightfv)
+      ctx->Driver.Lightfv( ctx, light, pname, params );
+}
+
+
+void
+_mesa_Lighti( GLenum light, GLenum pname, GLint param )
+{
+   _mesa_Lightiv( light, pname, &param );
+}
+
+
+void
+_mesa_Lightiv( GLenum light, GLenum pname, const GLint *params )
+{
+   GLfloat fparam[4];
+
    switch (pname) {
       case GL_AMBIENT:
-         COPY_4V( ctx->Light.Light[l].Ambient, params );
-         break;
       case GL_DIFFUSE:
-         COPY_4V( ctx->Light.Light[l].Diffuse, params );
-         break;
       case GL_SPECULAR:
-         COPY_4V( ctx->Light.Light[l].Specular, params );
+         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_POSITION:
-        /* transform position by ModelView matrix */
-        TRANSFORM_POINT( ctx->Light.Light[l].EyePosition, 
-                         ctx->ModelView.m,
-                          params );
+         fparam[0] = (GLfloat) params[0];
+         fparam[1] = (GLfloat) params[1];
+         fparam[2] = (GLfloat) params[2];
+         fparam[3] = (GLfloat) params[3];
          break;
       case GL_SPOT_DIRECTION:
-        /* transform direction by inverse modelview */
-        if (ctx->ModelView.flags & MAT_DIRTY_INVERSE) {
-           gl_matrix_analyze( &ctx->ModelView );
-        }
-        TRANSFORM_NORMAL( ctx->Light.Light[l].EyeDirection,
-                          params,
-                          ctx->ModelView.inv );
+         fparam[0] = (GLfloat) params[0];
+         fparam[1] = (GLfloat) params[1];
+         fparam[2] = (GLfloat) params[2];
          break;
       case GL_SPOT_EXPONENT:
-         if (params[0]<0.0 || params[0]>128.0) {
-            gl_error( ctx, GL_INVALID_VALUE, "glLight" );
-            return;
-         }
-         if (ctx->Light.Light[l].SpotExponent != params[0]) {
-            ctx->Light.Light[l].SpotExponent = params[0];
-            gl_compute_spot_exp_table( &ctx->Light.Light[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;
-         }
-         ctx->Light.Light[l].SpotCutoff = params[0];
-         ctx->Light.Light[l].CosCutoff = cos(params[0]*DEG2RAD);
-         if (ctx->Light.Light[l].CosCutoff < 0) 
-           ctx->Light.Light[l].CosCutoff = 0;
-         break;
       case GL_CONSTANT_ATTENUATION:
-         if (params[0]<0.0) {
-            gl_error( ctx, GL_INVALID_VALUE, "glLight" );
-            return;
-         }
-         ctx->Light.Light[l].ConstantAttenuation = params[0];
-         break;
       case GL_LINEAR_ATTENUATION:
-         if (params[0]<0.0) {
-            gl_error( ctx, GL_INVALID_VALUE, "glLight" );
-            return;
-         }
-         ctx->Light.Light[l].LinearAttenuation = params[0];
-         break;
       case GL_QUADRATIC_ATTENUATION:
-         if (params[0]<0.0) {
-            gl_error( ctx, GL_INVALID_VALUE, "glLight" );
-            return;
-         }
-         ctx->Light.Light[l].QuadraticAttenuation = params[0];
+         fparam[0] = (GLfloat) params[0];
          break;
       default:
-         gl_error( ctx, GL_INVALID_ENUM, "glLight" );
-         break;
+         /* error will be caught later in gl_Lightfv */
+         ;
    }
 
-   if (ctx->Driver.Lightfv)
-      ctx->Driver.Lightfv( ctx, light, pname, params, nparams );
-
-   ctx->NewState |= NEW_LIGHTING;
+   _mesa_Lightfv( light, pname, fparam );
 }
 
 
 
-void gl_GetLightfv( GLcontext *ctx,
-                    GLenum light, GLenum pname, GLfloat *params )
+void
+_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;
    }
 
@@ -220,21 +297,22 @@ void gl_GetLightfv( GLcontext *ctx,
          params[0] = ctx->Light.Light[l].QuadraticAttenuation;
          break;
       default:
-         gl_error( ctx, GL_INVALID_ENUM, "glGetLightfv" );
+         _mesa_error( ctx, GL_INVALID_ENUM, "glGetLightfv" );
          break;
    }
 }
 
 
 
-void gl_GetLightiv( GLcontext *ctx, GLenum light, GLenum pname, GLint *params )
+void
+_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;
    }
 
@@ -284,7 +362,7 @@ void gl_GetLightiv( GLcontext *ctx, 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;
    }
 }
@@ -296,49 +374,111 @@ void gl_GetLightiv( GLcontext *ctx, GLenum light, GLenum pname, GLint *params )
 /**********************************************************************/
 
 
-void gl_LightModelfv( GLcontext *ctx, GLenum pname, const GLfloat *params )
+void
+_mesa_LightModelfv( GLenum pname, const GLfloat *params )
 {
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glLightModel");
+   GLenum newenum;
+   GLboolean newbool;
+   GET_CURRENT_CONTEXT(ctx);
+   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;
+
+        if (ctx->Light.Enabled && ctx->Light.Model.TwoSide)
+           ctx->_TriangleCaps |= DD_TRI_LIGHT_TWOSIDE;
+        else
+           ctx->_TriangleCaps &= ~DD_TRI_LIGHT_TWOSIDE;
          break;
       case GL_LIGHT_MODEL_COLOR_CONTROL:
-        ctx->TriangleCaps &= ~DD_SEPERATE_SPECULAR;
-        ctx->NewState |= NEW_RASTER_OPS;
-         if (params[0] == (GLfloat) GL_SINGLE_COLOR) 
-            ctx->Light.Model.ColorControl = GL_SINGLE_COLOR;
-         else if (params[0] == (GLfloat) GL_SEPARATE_SPECULAR_COLOR) {
-            ctx->Light.Model.ColorControl = GL_SEPARATE_SPECULAR_COLOR;
-           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=0x0%x)",
+                         (GLint) params[0] );
+           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_SEPARATE_SPECULAR;
+        else
+           ctx->_TriangleCaps &= ~DD_SEPARATE_SPECULAR;
+
          break;
       default:
-         gl_error( ctx, GL_INVALID_ENUM, "glLightModel" );
+         _mesa_error( ctx, GL_INVALID_ENUM, "glLightModel(pname=0x%x)", pname );
          break;
    }
 
-   if (ctx->Driver.LightModelfv) 
+   if (ctx->Driver.LightModelfv)
       ctx->Driver.LightModelfv( ctx, pname, params );
+}
+
+
+void
+_mesa_LightModeliv( GLenum pname, const GLint *params )
+{
+   GLfloat fparam[4];
+
+   switch (pname) {
+      case GL_LIGHT_MODEL_AMBIENT:
+         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_LIGHT_MODEL_LOCAL_VIEWER:
+      case GL_LIGHT_MODEL_TWO_SIDE:
+      case GL_LIGHT_MODEL_COLOR_CONTROL:
+         fparam[0] = (GLfloat) params[0];
+         break;
+      default:
+         /* Error will be caught later in gl_LightModelfv */
+         ;
+   }
+   _mesa_LightModelfv( pname, fparam );
+}
+
 
-   ctx->NewState |= NEW_LIGHTING;
+void
+_mesa_LightModeli( GLenum pname, GLint param )
+{
+   _mesa_LightModeliv( pname, &param );
 }
 
 
+void
+_mesa_LightModelf( GLenum pname, GLfloat param )
+{
+   _mesa_LightModelfv( pname, &param );
+}
+
 
 
 /********** MATERIAL **********/
@@ -348,9 +488,9 @@ void gl_LightModelfv( GLcontext *ctx, GLenum pname, const GLfloat *params )
  * 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;
 
@@ -379,7 +519,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;
    }
 
@@ -390,12 +530,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;
    }
 
@@ -403,8 +543,53 @@ 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;
+   }
+}
 
 
 /*
@@ -414,107 +599,106 @@ GLuint gl_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
  * glBegin/glEnd either by calling glMaterial() or by calling glColor()
  * when GL_COLOR_MATERIAL is enabled.
  *
- * 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.
+ * src[0] is front material, src[1] is back material
+ *
+ * Additionally keeps the precomputed lighting state uptodate.
  */
-void gl_update_material( GLcontext *ctx, 
-                        struct gl_material *src, 
+void _mesa_update_material( GLcontext *ctx,
+                        const struct gl_material src[2],
                         GLuint bitmask )
 {
    struct gl_light *light, *list = &ctx->Light.EnabledList;
-   GLfloat tmp[4];
 
    if (ctx->Light.ColorMaterialEnabled)
       bitmask &= ~ctx->Light.ColorMaterialBitmask;
 
-   if (!bitmask) 
+   if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
+      _mesa_debug(ctx, "_mesa_update_material, mask 0x%x\n", bitmask);
+
+   if (!bitmask)
       return;
 
+   /* update material emission */
+   if (bitmask & FRONT_EMISSION_BIT) {
+      struct gl_material *mat = &ctx->Light.Material[0];
+      COPY_4FV( mat->Emission, src[0].Emission );
+   }
+   if (bitmask & BACK_EMISSION_BIT) {
+      struct gl_material *mat = &ctx->Light.Material[1];
+      COPY_4FV( mat->Emission, src[1].Emission );
+   }
+
+   /* update material ambience */
    if (bitmask & FRONT_AMBIENT_BIT) {
       struct gl_material *mat = &ctx->Light.Material[0];
-      SUB_3V( tmp, src[0].Ambient, mat->Ambient );
-      ACC_SCALE_3V( ctx->Light.BaseColor[0], ctx->Light.Model.Ambient, tmp);
+      COPY_4FV( mat->Ambient, src[0].Ambient );
       foreach (light, list) {
-        ACC_SCALE_3V( ctx->Light.BaseColor[0], light->Ambient, tmp );
+         SCALE_3V( light->_MatAmbient[0], light->Ambient, src[0].Ambient);
       }
-      COPY_4FV( mat->Ambient, src[0].Ambient );
    }
    if (bitmask & BACK_AMBIENT_BIT) {
       struct gl_material *mat = &ctx->Light.Material[1];
-      SUB_3V( tmp, src[1].Ambient, mat->Ambient );
-      ACC_SCALE_3V( ctx->Light.BaseColor[1], ctx->Light.Model.Ambient, tmp);
+      COPY_4FV( mat->Ambient, src[1].Ambient );
       foreach (light, list) {
-        ACC_SCALE_3V( ctx->Light.BaseColor[0], light->Ambient, tmp );
+         SCALE_3V( light->_MatAmbient[1], light->Ambient, src[1].Ambient);
       }
-      COPY_4FV( mat->Ambient, src[1].Ambient );
    }
+
+   /* update BaseColor = emission + scene's ambience * material's ambience */
+   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 );
+   }
+   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 );
+   }
+
+   /* update material diffuse values */
    if (bitmask & FRONT_DIFFUSE_BIT) {
       struct gl_material *mat = &ctx->Light.Material[0];
-      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_UBYTE_COLOR(ctx->Light.BaseAlpha[0], mat->Diffuse[3]);
    }
    if (bitmask & BACK_DIFFUSE_BIT) {
       struct gl_material *mat = &ctx->Light.Material[1];
-      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_UBYTE_COLOR(ctx->Light.BaseAlpha[1], mat->Diffuse[3]);
    }
+
+   /* update material specular values */
    if (bitmask & FRONT_SPECULAR_BIT) {
       struct gl_material *mat = &ctx->Light.Material[0];
-      SUB_3V( tmp, src[0].Specular, mat->Specular );
+      COPY_4FV( mat->Specular, src[0].Specular );
       foreach (light, list) {
-        if (light->Flags & LIGHT_SPECULAR) {
-           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];
-      SUB_3V( tmp, src[1].Specular, mat->Specular );
+      COPY_4FV( mat->Specular, src[1].Specular );
       foreach (light, list) {
-        if (light->Flags & LIGHT_SPECULAR) {
-           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_EMISSION_BIT) {
-      struct gl_material *mat = &ctx->Light.Material[0];
-      SUB_3V( tmp, src[0].Emission, mat->Emission );
-      ACC_3V( ctx->Light.BaseColor[0], tmp );
-      COPY_4FV( mat->Emission, src[0].Emission );
-   }
-   if (bitmask & BACK_EMISSION_BIT) {
-      struct gl_material *mat = &ctx->Light.Material[1];
-      SUB_3V( tmp, src[1].Emission, mat->Emission );
-      ACC_3V( ctx->Light.BaseColor[1], tmp );
-      COPY_4FV( mat->Emission, src[1].Emission );
    }
+
    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) {
       ctx->Light.Material[0].AmbientIndex = src[0].AmbientIndex;
       ctx->Light.Material[0].DiffuseIndex = src[0].DiffuseIndex;
@@ -526,6 +710,17 @@ void gl_update_material( GLcontext *ctx,
       ctx->Light.Material[1].SpecularIndex = src[1].SpecularIndex;
    }
 
+   if (0) {
+      struct gl_material *mat = &ctx->Light.Material[0];
+      _mesa_debug(ctx, "update_mat  emission : %f %f %f\n",
+                  mat->Emission[0], mat->Emission[1], mat->Emission[2]);
+      _mesa_debug(ctx, "update_mat  specular : %f %f %f\n",
+                  mat->Specular[0], mat->Specular[1], mat->Specular[2]);
+      _mesa_debug(ctx, "update_mat  diffuse : %f %f %f\n",
+                  mat->Diffuse[0], mat->Diffuse[1], mat->Diffuse[2]);
+      _mesa_debug(ctx, "update_mat  ambient : %f %f %f\n",
+                  mat->Ambient[0], mat->Ambient[1], mat->Ambient[2]);
+   }
 }
 
 
@@ -533,197 +728,159 @@ void gl_update_material( GLcontext *ctx,
 
 
 
-void gl_update_color_material( GLcontext *ctx, 
-                              const GLubyte rgba[4] )
+
+/*
+ * Update the current materials from the given rgba color
+ * according to the bitmask in ColorMaterialBitmask, which is
+ * set by glColorMaterial().
+ */
+void _mesa_update_color_material( GLcontext *ctx,
+                                 const GLfloat color[4] )
 {
    struct gl_light *light, *list = &ctx->Light.EnabledList;
    GLuint bitmask = ctx->Light.ColorMaterialBitmask;
-   GLfloat tmp[4], color[4];
 
-   UBYTE_RGBA_TO_FLOAT_RGBA( color, rgba );
-   
+   if (MESA_VERBOSE&VERBOSE_IMMEDIATE)
+      _mesa_debug(ctx, "_mesa_update_color_material, mask 0x%x\n", bitmask);
+
+   /* update emissive colors */
+   if (bitmask & FRONT_EMISSION_BIT) {
+      struct gl_material *mat = &ctx->Light.Material[0];
+      COPY_4FV( mat->Emission, color );
+   }
+
+   if (bitmask & BACK_EMISSION_BIT) {
+      struct gl_material *mat = &ctx->Light.Material[1];
+      COPY_4FV( mat->Emission, color );
+   }
+
+   /* update light->_MatAmbient = light's ambient * material's ambient */
    if (bitmask & FRONT_AMBIENT_BIT) {
       struct gl_material *mat = &ctx->Light.Material[0];
-      SUB_3V( tmp, color, mat->Ambient );
-      ACC_SCALE_3V( ctx->Light.BaseColor[0], ctx->Light.Model.Ambient, tmp);
       foreach (light, list) {
-        ACC_SCALE_3V( ctx->Light.BaseColor[0], light->Ambient, tmp );
+         SCALE_3V( light->_MatAmbient[0], light->Ambient, color);
       }
       COPY_4FV( mat->Ambient, color );
    }
 
    if (bitmask & BACK_AMBIENT_BIT) {
       struct gl_material *mat = &ctx->Light.Material[1];
-      SUB_3V( tmp, color, mat->Ambient );
-      ACC_SCALE_3V( ctx->Light.BaseColor[1], ctx->Light.Model.Ambient, tmp);
       foreach (light, list) {
-        ACC_SCALE_3V( ctx->Light.BaseColor[0], light->Ambient, tmp );
+         SCALE_3V( light->_MatAmbient[1], light->Ambient, color);
       }
       COPY_4FV( mat->Ambient, color );
    }
 
+   /* update BaseColor = emission + scene's ambience * material's ambience */
+   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 );
+   }
+
+   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 );
+   }
+
+   /* update light->_MatDiffuse = light's diffuse * material's diffuse */
    if (bitmask & FRONT_DIFFUSE_BIT) {
       struct gl_material *mat = &ctx->Light.Material[0];
-      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_UBYTE_COLOR(ctx->Light.BaseAlpha[0], mat->Diffuse[3]);
    }
 
    if (bitmask & BACK_DIFFUSE_BIT) {
       struct gl_material *mat = &ctx->Light.Material[1];
-      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_UBYTE_COLOR(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];
-      SUB_3V( tmp, color, mat->Specular );
+      COPY_4FV( mat->Specular, color );
       foreach (light, list) {
-        if (light->Flags & LIGHT_SPECULAR) {
-           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];
-      SUB_3V( tmp, color, mat->Specular );
+      COPY_4FV( mat->Specular, color );
       foreach (light, list) {
-        if (light->Flags & LIGHT_SPECULAR) {
-           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 (bitmask & FRONT_EMISSION_BIT) {
+
+   if (0) {
       struct gl_material *mat = &ctx->Light.Material[0];
-      SUB_3V( tmp, color, mat->Emission );
-      ACC_3V( ctx->Light.BaseColor[0], tmp );
-      COPY_4FV( mat->Emission, color );
-   }
-   if (bitmask & BACK_EMISSION_BIT) {
-      struct gl_material *mat = &ctx->Light.Material[1];
-      SUB_3V( tmp, color, mat->Emission );
-      ACC_3V( ctx->Light.BaseColor[1], tmp );
-      COPY_4FV( mat->Emission, color );
+      _mesa_debug(ctx, "update_color_mat  emission : %f %f %f\n",
+                  mat->Emission[0], mat->Emission[1], mat->Emission[2]);
+      _mesa_debug(ctx, "update_color_mat  specular : %f %f %f\n",
+                  mat->Specular[0], mat->Specular[1], mat->Specular[2]);
+      _mesa_debug(ctx, "update_color_mat  diffuse : %f %f %f\n",
+                  mat->Diffuse[0], mat->Diffuse[1], mat->Diffuse[2]);
+      _mesa_debug(ctx, "update_color_mat  ambient : %f %f %f\n",
+                  mat->Ambient[0], mat->Ambient[1], mat->Ambient[2]);
    }
 }
 
 
 
 
-void gl_ColorMaterial( GLcontext *ctx, GLenum face, GLenum mode )
+void
+_mesa_ColorMaterial( GLenum face, GLenum mode )
 {
+   GET_CURRENT_CONTEXT(ctx);
    GLuint bitmask;
    GLuint legal = (FRONT_EMISSION_BIT | BACK_EMISSION_BIT |
                   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");
-
-   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 (MESA_VERBOSE&VERBOSE_API)
+      _mesa_debug(ctx, "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 gl_Materialfv( GLcontext *ctx,
-                    GLenum face, GLenum pname, const GLfloat *params )
-{
-   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->Flag[count] & VERT_MATERIAL)) {
-      IM->Flag[count] |= VERT_MATERIAL;
-      IM->MaterialMask[count] = 0;
+   if (ctx->Light.ColorMaterialEnabled) {
+      FLUSH_CURRENT( ctx, 0 );
+      _mesa_update_color_material(ctx,ctx->Current.Attrib[VERT_ATTRIB_COLOR0]);
    }
 
-   IM->MaterialMask[count] |= bitmask;
-   mat = IM->Material[count];
-   IM->LastMaterial = 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->Driver.ColorMaterial)
+      (*ctx->Driver.ColorMaterial)( ctx, face, mode );
 }
 
 
 
 
-void gl_GetMaterialfv( GLcontext *ctx,
-                       GLenum face, GLenum pname, GLfloat *params )
+
+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;
@@ -732,7 +889,7 @@ void gl_GetMaterialfv( GLcontext *ctx,
       f = 1;
    }
    else {
-      gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(face)" );
+      _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(face)" );
       return;
    }
    switch (pname) {
@@ -757,18 +914,18 @@ void gl_GetMaterialfv( GLcontext *ctx,
         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)" );
    }
 }
 
 
 
-void gl_GetMaterialiv( GLcontext *ctx,
-                       GLenum face, GLenum pname, GLint *params )
+void
+_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;
@@ -777,7 +934,7 @@ void gl_GetMaterialiv( GLcontext *ctx,
       f = 1;
    }
    else {
-      gl_error( ctx, GL_INVALID_ENUM, "glGetMaterialiv(face)" );
+      _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialiv(face)" );
       return;
    }
    switch (pname) {
@@ -814,7 +971,7 @@ void gl_GetMaterialiv( GLcontext *ctx,
         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)" );
    }
 }
 
@@ -833,7 +990,7 @@ void gl_GetMaterialiv( GLcontext *ctx,
  *   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:
@@ -846,10 +1003,10 @@ void gl_GetMaterialiv( GLcontext *ctx,
  *   IF P[3]==0 THEN
  *       // light at infinity
  *       IF local_viewer THEN
- *           VP_inf_norm = unit vector from V to P      // Precompute
- *       ELSE 
+ *           _VP_inf_norm = unit vector from V to P      // Precompute
+ *       ELSE
  *           // eye at infinity
- *           h_inf_norm = Normalize( VP + <0,0,1> )     // Precompute
+ *           _h_inf_norm = Normalize( VP + <0,0,1> )     // Precompute
  *       ENDIF
  *   ENDIF
  *
@@ -864,29 +1021,36 @@ void gl_GetMaterialiv( GLcontext *ctx,
  * Whenever the spotlight exponent for a light changes we must call
  * this function to recompute the exponent lookup table.
  */
-void gl_compute_spot_exp_table( struct gl_light *l )
+void
+_mesa_invalidate_spot_exp_table( struct gl_light *l )
+{
+   l->_SpotExpTable[0][0] = -1;
+}
+
+static void validate_spot_exp_table( struct gl_light *l )
 {
-   int i;
-   double exponent = l->SpotExponent;
-   double tmp = 0;
-   int clamp = 0;
+   GLint i;
+   GLdouble exponent = l->SpotExponent;
+   GLdouble tmp = 0;
+   GLint clamp = 0;
 
-   l->SpotExpTable[0][0] = 0.0;
+   l->_SpotExpTable[0][0] = 0.0;
 
-   for (i=EXP_TABLE_SIZE-1;i>0;i--) {
+   for (i = EXP_TABLE_SIZE - 1; i > 0 ;i--) {
       if (clamp == 0) {
-         tmp = pow(i/(double)(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;
+      l->_SpotExpTable[i][0] = (GLfloat) tmp;
    }
-   for (i=0;i<EXP_TABLE_SIZE-1;i++) {
-      l->SpotExpTable[i][1] = l->SpotExpTable[i+1][0] - l->SpotExpTable[i][0];
+   for (i = 0; i < EXP_TABLE_SIZE - 1; i++) {
+      l->_SpotExpTable[i][1] = (l->_SpotExpTable[i+1][0] -
+                               l->_SpotExpTable[i][0]);
    }
-   l->SpotExpTable[EXP_TABLE_SIZE-1][1] = 0.0;
+   l->_SpotExpTable[EXP_TABLE_SIZE-1][1] = 0.0;
 }
 
 
@@ -896,297 +1060,235 @@ void 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 )
 {
-   int i;
-   GLfloat *m = tab->tab;
-
-   m[0] = 0;
-   if (shininess == 0) {
-      for (i = 1 ; i <= SHINE_TABLE_SIZE ; i++)
-        m[i] = 1;
-   } else {
-      for (i = 1 ; i <= SHINE_TABLE_SIZE ; i++) {
-        double t = pow( i/(GLfloat)SHINE_TABLE_SIZE, shininess );
-        m[i] = 0;
-        if (t > 1e-20) m[i] = t;
-      }      
-   }
-
-   tab->shininess = shininess;
+   if (ctx->_ShineTable[i])
+      ctx->_ShineTable[i]->refcount--;
+   ctx->_ShineTable[i] = 0;
 }
 
-#define DISTSQR(a,b) ((a-b)*(a-b))
-
-void gl_compute_shine_table( GLcontext *ctx, GLuint i, GLfloat shininess )
+static void validate_shine_table( GLcontext *ctx, GLuint i, GLfloat shininess )
 {
-   struct gl_shine_tab *list = ctx->ShineTabList;
+   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) 
-        if (s->refcount == 0) break;
+   if (s == list) {
+      GLint j;
+      GLfloat *m;
 
-      compute_shine_table( s, shininess );
+      foreach(s, list)
+        if (s->refcount == 0)
+           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] = (GLfloat) t;
+           else
+              m[j] = 0.0;
+        }
+        m[SHINE_TABLE_SIZE] = 1.0;
+      }
+
+      s->shininess = shininess;
    }
 
-   ctx->ShineTable[i]->refcount--;
-   ctx->ShineTable[i] = s;
+   if (ctx->_ShineTable[i])
+      ctx->_ShineTable[i]->refcount--;
+
+   ctx->_ShineTable[i] = s;
    move_to_tail( list, s );
    s->refcount++;
 }
 
-
-
-
-void gl_reinit_light_attrib( GLcontext *ctx, struct gl_light_attrib *l )
+void
+_mesa_validate_all_lighting_tables( GLcontext *ctx )
 {
-   GLuint i;
+   GLint i;
+   GLfloat shininess;
 
-   if (ctx->ShineTable[0]->shininess != l->Material[0].Shininess) {
-      gl_compute_shine_table( ctx, 0, l->Material[0].Shininess );
-      gl_compute_shine_table( ctx, 2, l->Material[0].Shininess * .5 );
-   }
+   shininess = ctx->Light.Material[0].Shininess;
+   if (!ctx->_ShineTable[0] || ctx->_ShineTable[0]->shininess != shininess)
+      validate_shine_table( ctx, 0, shininess );
 
-   if (ctx->ShineTable[1]->shininess != l->Material[1].Shininess) {
-      gl_compute_shine_table( ctx, 1, l->Material[1].Shininess );
-      gl_compute_shine_table( ctx, 3, l->Material[1].Shininess * .5 );
-   }
+   shininess = ctx->Light.Material[1].Shininess;
+   if (!ctx->_ShineTable[1] || ctx->_ShineTable[1]->shininess != shininess)
+      validate_shine_table( ctx, 1, shininess );
 
-   make_empty_list( &l->EnabledList );
-   for (i = 0 ; i < MAX_LIGHTS ; i++) {
-      if (l->Light[i].Enabled) 
-        insert_at_tail( &l->EnabledList, &l->Light[i] );
-   }
+   for (i = 0 ; i < MAX_LIGHTS ; i++)
+      if (ctx->Light.Light[i]._SpotExpTable[0][0] == -1)
+        validate_spot_exp_table( &ctx->Light.Light[i] );
 }
 
 
 
+
 /*
  * Examine current lighting parameters to determine if the optimized lighting
  * function can be used.
  * Also, precompute some lighting values such as the products of light
  * source and material ambient, diffuse and specular coefficients.
  */
-void gl_update_lighting( GLcontext *ctx )
+void
+_mesa_update_lighting( GLcontext *ctx )
 {
    struct gl_light *light;
+   ctx->_NeedEyeCoords &= ~NEED_EYE_LIGHT;
+   ctx->_NeedNormals &= ~NEED_NORMALS_LIGHT;
+   ctx->Light._Flags = 0;
+
+   if (!ctx->Light.Enabled)
+      return;
 
-   ctx->Light.Flags = 0;
+   ctx->_NeedNormals |= NEED_NORMALS_LIGHT;
 
    foreach(light, &ctx->Light.EnabledList) {
+      ctx->Light._Flags |= light->_Flags;
+   }
 
-      light->Flags = 0;
+   ctx->Light._NeedVertices =
+      ((ctx->Light._Flags & (LIGHT_POSITIONAL|LIGHT_SPOT)) ||
+       ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR ||
+       ctx->Light.Model.LocalViewer);
 
-      if (light->EyePosition[3] != 0.0F) 
-        light->Flags |= LIGHT_POSITIONAL;
-      
-      if (LEN_SQUARED_3FV(light->Specular) > 1e-16) 
-        light->Flags |= LIGHT_SPECULAR;
-      
-      if (light->SpotCutoff != 180.0F)
-        light->Flags |= LIGHT_SPOT;
+   if ((ctx->Light._Flags & LIGHT_POSITIONAL) ||
+       ctx->Light.Model.LocalViewer)
+      ctx->_NeedEyeCoords |= NEED_EYE_LIGHT;
 
-      ctx->Light.Flags |= light->Flags;
-   }
 
-   ctx->Light.NeedVertices = 
-      ((ctx->Light.Flags & (LIGHT_POSITIONAL|LIGHT_SPOT)) ||
-       (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR) ||
-       (ctx->Light.Model.LocalViewer && (ctx->Light.Flags & LIGHT_SPECULAR)));
+   /* 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.
+    */
+   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) 
-   {
-      GLuint sides = ((ctx->TriangleCaps & DD_TRI_LIGHT_TWOSIDE) ? 2 : 1);
+   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], 
+
+        COPY_3V(ctx->Light._BaseColor[side], mat->Emission);
+        ACC_SCALE_3V(ctx->Light._BaseColor[side],
                      ctx->Light.Model.Ambient,
                      mat->Ambient);
-
-        FLOAT_COLOR_TO_UBYTE_COLOR(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++) {
-           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 );
-           ACC_3V( ctx->Light.BaseColor[side], light->MatAmbient[side] ); 
-           if (light->Flags & LIGHT_SPECULAR)
-           {
-              SCALE_3V( light->MatSpecular[side], light->Specular,
-                        mat->Specular);
-              light->IsMatSpecular[side] = 
-                 (LEN_SQUARED_3FV(light->MatSpecular[side]) > 1e-16);
-           } 
-           else 
-              light->IsMatSpecular[side] = 0;
+           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);
         }
       }
-   } 
-   else
-   {
-      static GLfloat ci[3] = { .30, .59, .11 };
-
+   }
+   else {
+      static const GLfloat ci[3] = { .30F, .59F, .11F };
       foreach(light, &ctx->Light.EnabledList) {
-        light->dli = DOT3(ci, light->Diffuse);
-        light->sli = DOT3(ci, light->Specular);
+        light->_dli = DOT3(ci, light->Diffuse);
+        light->_sli = DOT3(ci, light->Specular);
       }
    }
 }
 
-/* Need to seriously restrict the circumstances under which these
- * calc's are performed.
+
+/* _NEW_MODELVIEW
+ * _NEW_LIGHT
+ * _TNL_NEW_NEED_EYE_COORDS
+ *
+ * Update on (_NEW_MODELVIEW | _NEW_LIGHT) when lighting is enabled.
+ * Also update on lighting space changes.
  */
-void gl_compute_light_positions( GLcontext *ctx )
+void
+_mesa_compute_light_positions( GLcontext *ctx )
 {
    struct gl_light *light;
-   
-   if (ctx->Light.NeedVertices && !ctx->Light.Model.LocalViewer) {
-      GLfloat eye_z[3] = { 0, 0, 1 };
-      if (!ctx->NeedEyeCoords) {
-        TRANSFORM_NORMAL( ctx->EyeZDir, eye_z, ctx->ModelView.m );
-      } else {
-        COPY_3V( ctx->EyeZDir, eye_z );
-      }
+   static const GLfloat eye_z[3] = { 0, 0, 1 };
+
+   if (!ctx->Light.Enabled)
+      return;
+
+   if (ctx->_NeedEyeCoords) {
+      COPY_3V( ctx->_EyeZDir, eye_z );
+   }
+   else {
+      TRANSFORM_NORMAL( ctx->_EyeZDir, eye_z, ctx->ModelviewMatrixStack.Top->m );
    }
 
    foreach (light, &ctx->Light.EnabledList) {
 
-      if (!ctx->NeedEyeCoords) {
-        TRANSFORM_POINT( light->Position, ctx->ModelView.inv, 
+      if (ctx->_NeedEyeCoords) {
+        COPY_4FV( light->_Position, light->EyePosition );
+      }
+      else {
+        TRANSFORM_POINT( light->_Position, ctx->ModelviewMatrixStack.Top->inv,
                          light->EyePosition );
-      } else {
-        COPY_4FV( light->Position, light->EyePosition );
       }
 
-      if (!(light->Flags & LIGHT_POSITIONAL))
-      {
+      if (!(light->_Flags & LIGHT_POSITIONAL)) {
         /* VP (VP) = Normalize( Position ) */
-        COPY_3V( light->VP_inf_norm, light->Position );
-        NORMALIZE_3FV( light->VP_inf_norm );
-
-        if (!ctx->Light.Model.LocalViewer)
-        {
-           /* h_inf_norm = Normalize( V_to_P + <0,0,1> ) */
-           ADD_3V( light->h_inf_norm, light->VP_inf_norm, ctx->EyeZDir);
-           NORMALIZE_3FV( light->h_inf_norm );
-        }
+        COPY_3V( light->_VP_inf_norm, light->_Position );
+        NORMALIZE_3FV( light->_VP_inf_norm );
 
-        light->VP_inf_spot_attenuation = 1.0;
+        if (!ctx->Light.Model.LocalViewer) {
+           /* _h_inf_norm = Normalize( V_to_P + <0,0,1> ) */
+           ADD_3V( light->_h_inf_norm, light->_VP_inf_norm, ctx->_EyeZDir);
+           NORMALIZE_3FV( light->_h_inf_norm );
+        }
+        light->_VP_inf_spot_attenuation = 1.0;
       }
-      
-      if (light->Flags & LIGHT_SPOT) 
-      {
-        if (ctx->NeedEyeNormals) {
-           COPY_3V( light->NormDirection, light->EyeDirection );
-        } else {
-           TRANSFORM_NORMAL( light->NormDirection, 
+
+      if (light->_Flags & LIGHT_SPOT) {
+        if (ctx->_NeedEyeCoords) {
+           COPY_3V( light->_NormDirection, light->EyeDirection );
+        }
+         else {
+           TRANSFORM_NORMAL( light->_NormDirection,
                              light->EyeDirection,
-                             ctx->ModelView.m);
+                             ctx->ModelviewMatrixStack.Top->m);
         }
 
-        NORMALIZE_3FV( light->NormDirection );
-
+        NORMALIZE_3FV( light->_NormDirection );
 
-        /* Unlikely occurrance?
-         */
-        if (!(light->Flags & LIGHT_POSITIONAL)) {
-           GLfloat PV_dot_dir = - DOT3(light->VP_inf_norm, 
-                                       light->NormDirection);
+        if (!(light->_Flags & LIGHT_POSITIONAL)) {
+           GLfloat PV_dot_dir = - DOT3(light->_VP_inf_norm,
+                                       light->_NormDirection);
 
-           if (PV_dot_dir > light->CosCutoff) {
+           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] + 
-                  (x-k)*light->SpotExpTable[k][1]);
+              light->_VP_inf_spot_attenuation =
+                 (GLfloat) (light->_SpotExpTable[k][0] +
+                  (x-k)*light->_SpotExpTable[k][1]);
            }
-           else 
-              light->VP_inf_spot_attenuation = 0;
-        }
-      }
-   }
-}
-
-
-
-
-
-void gl_update_normal_transform( GLcontext *ctx )
-{
-   GLuint new_flag = 0;
-   normal_func *last = ctx->NormalTransform;
-   
-   ctx->vb_rescale_factor = 1.0;
-
-   if (ctx->NeedEyeCoords) {
-      if (ctx->NeedNormals) {
-        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;
-
-           
-        new_flag = ctx->NewState & NEW_MODELVIEW;
-        ctx->vb_rescale_factor = ctx->rescale_factor;
-              
-        if (ctx->Transform.Normalize) 
-        {
-           ctx->NormalTransform = gl_normal_tab[transform | NORM_NORMALIZE];
-        } 
-        else if (ctx->Transform.RescaleNormals &&
-                 ctx->rescale_factor != 1.0)
-        {
-           ctx->NormalTransform = gl_normal_tab[transform | NORM_RESCALE];
+           else {
+              light->_VP_inf_spot_attenuation = 0;
+            }
         }
-        else 
-        {
-           ctx->NormalTransform = gl_normal_tab[transform];
-        }
-      } else {
-        ctx->NormalTransform = 0;
       }
    }
-   else {
-      if (ctx->NeedNormals) {
-        ctx->vb_rescale_factor = 1.0/ctx->rescale_factor;
-
-        if (ctx->Transform.Normalize) 
-        {
-           ctx->NormalTransform = gl_normal_tab[NORM_NORMALIZE];
-        }
-        else if (!ctx->Transform.RescaleNormals &&
-                 ctx->rescale_factor != 1.0)
-        {
-           ctx->NormalTransform = gl_normal_tab[NORM_RESCALE];
-        }
-        else
-        {
-           ctx->NormalTransform = 0;
-        }
-      } else {
-        ctx->NormalTransform = 0;
-      }
-   }
-
-   if (last != ctx->NormalTransform || new_flag)
-      ctx->NewState |= NEW_NORMAL_TRANSFORM;
 }
-