mesa: enable GL_EXT_draw_instanced for gles2
[mesa.git] / src / mesa / main / light.c
index 1c8a081e9a66583f731da9e938af5fd6838ce5ef..0883832dbd6a8197153175681a7b616155759453 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Mesa 3-D graphics library
- * Version:  7.5
  *
  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
  * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
  * 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
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  */
 
 
+#include "c99_math.h"
 #include "glheader.h"
-#include "imports.h"
 #include "context.h"
 #include "enums.h"
 #include "light.h"
 #include "macros.h"
-#include "simple_list.h"
 #include "mtypes.h"
 #include "math/m_matrix.h"
+#include "util/bitscan.h"
 
 
 void GLAPIENTRY
 _mesa_ShadeModel( GLenum mode )
 {
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE & VERBOSE_API)
-      _mesa_debug(ctx, "glShadeModel %s\n", _mesa_lookup_enum_by_nr(mode));
+      _mesa_debug(ctx, "glShadeModel %s\n", _mesa_enum_to_string(mode));
+
+   if (ctx->Light.ShadeModel == mode)
+      return;
 
    if (mode != GL_FLAT && mode != GL_SMOOTH) {
       _mesa_error(ctx, GL_INVALID_ENUM, "glShadeModel");
       return;
    }
 
-   if (ctx->Light.ShadeModel == mode)
-      return;
-
    FLUSH_VERTICES(ctx, _NEW_LIGHT);
    ctx->Light.ShadeModel = mode;
-   if (mode == GL_FLAT)
-      ctx->_TriangleCaps |= DD_FLATSHADE;
-   else
-      ctx->_TriangleCaps &= ~DD_FLATSHADE;
 
    if (ctx->Driver.ShadeModel)
       ctx->Driver.ShadeModel( ctx, mode );
@@ -70,14 +65,16 @@ _mesa_ShadeModel( GLenum mode )
  * triangle or line.
  */
 void GLAPIENTRY
-_mesa_ProvokingVertexEXT(GLenum mode)
+_mesa_ProvokingVertex(GLenum mode)
 {
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE&VERBOSE_API)
       _mesa_debug(ctx, "glProvokingVertexEXT 0x%x\n", mode);
 
+   if (ctx->Light.ProvokingVertex == mode)
+      return;
+
    switch (mode) {
    case GL_FIRST_VERTEX_CONVENTION_EXT:
    case GL_LAST_VERTEX_CONVENTION_EXT:
@@ -87,9 +84,6 @@ _mesa_ProvokingVertexEXT(GLenum mode)
       return;
    }
 
-   if (ctx->Light.ProvokingVertex == mode)
-      return;
-
    FLUSH_VERTICES(ctx, _NEW_LIGHT);
    ctx->Light.ProvokingVertex = mode;
 }
@@ -103,11 +97,11 @@ _mesa_ProvokingVertexEXT(GLenum mode)
  * Also, all error checking should have already been done.
  */
 void
-_mesa_light(GLcontext *ctx, GLuint lnum, GLenum pname, const GLfloat *params)
+_mesa_light(struct gl_context *ctx, GLuint lnum, GLenum pname, const GLfloat *params)
 {
    struct gl_light *light;
 
-   ASSERT(lnum < MAX_LIGHTS);
+   assert(lnum < MAX_LIGHTS);
    light = &ctx->Light.Light[lnum];
 
    switch (pname) {
@@ -148,54 +142,50 @@ _mesa_light(GLcontext *ctx, GLuint lnum, GLenum pname, const GLfloat *params)
       COPY_3V(light->SpotDirection, params);
       break;
    case GL_SPOT_EXPONENT:
-      ASSERT(params[0] >= 0.0);
-      ASSERT(params[0] <= ctx->Const.MaxSpotExponent);
+      assert(params[0] >= 0.0F);
+      assert(params[0] <= ctx->Const.MaxSpotExponent);
       if (light->SpotExponent == params[0])
         return;
       FLUSH_VERTICES(ctx, _NEW_LIGHT);
       light->SpotExponent = params[0];
-      _mesa_invalidate_spot_exp_table(light);
       break;
    case GL_SPOT_CUTOFF:
-      ASSERT(params[0] == 180.0 || (params[0] >= 0.0 && params[0] <= 90.0));
+      assert(params[0] == 180.0F || (params[0] >= 0.0F && params[0] <= 90.0F));
       if (light->SpotCutoff == params[0])
          return;
       FLUSH_VERTICES(ctx, _NEW_LIGHT);
       light->SpotCutoff = params[0];
-      light->_CosCutoffNeg = (GLfloat) (_mesa_cos(light->SpotCutoff * DEG2RAD));
-      if (light->_CosCutoffNeg < 0)
+      light->_CosCutoff = (cosf(light->SpotCutoff * M_PI / 180.0));
+      if (light->_CosCutoff < 0)
          light->_CosCutoff = 0;
-      else
-         light->_CosCutoff = light->_CosCutoffNeg;
       if (light->SpotCutoff != 180.0F)
          light->_Flags |= LIGHT_SPOT;
       else
          light->_Flags &= ~LIGHT_SPOT;
       break;
    case GL_CONSTANT_ATTENUATION:
-      ASSERT(params[0] >= 0.0);
+      assert(params[0] >= 0.0F);
       if (light->ConstantAttenuation == params[0])
         return;
       FLUSH_VERTICES(ctx, _NEW_LIGHT);
       light->ConstantAttenuation = params[0];
       break;
    case GL_LINEAR_ATTENUATION:
-      ASSERT(params[0] >= 0.0);
+      assert(params[0] >= 0.0F);
       if (light->LinearAttenuation == params[0])
         return;
       FLUSH_VERTICES(ctx, _NEW_LIGHT);
       light->LinearAttenuation = params[0];
       break;
    case GL_QUADRATIC_ATTENUATION:
-      ASSERT(params[0] >= 0.0);
+      assert(params[0] >= 0.0F);
       if (light->QuadraticAttenuation == params[0])
         return;
       FLUSH_VERTICES(ctx, _NEW_LIGHT);
       light->QuadraticAttenuation = params[0];
       break;
    default:
-      _mesa_problem(ctx, "Unexpected pname in _mesa_light()");
-      return;
+      unreachable("Unexpected pname in _mesa_light()");
    }
 
    if (ctx->Driver.Lightfv)
@@ -206,7 +196,10 @@ _mesa_light(GLcontext *ctx, GLuint lnum, GLenum pname, const GLfloat *params)
 void GLAPIENTRY
 _mesa_Lightf( GLenum light, GLenum pname, GLfloat param )
 {
-   _mesa_Lightfv( light, pname, &param );
+   GLfloat fparam[4];
+   fparam[0] = param;
+   fparam[1] = fparam[2] = fparam[3] = 0.0F;
+   _mesa_Lightfv( light, pname, fparam );
 }
 
 
@@ -216,7 +209,6 @@ _mesa_Lightfv( GLenum light, GLenum pname, const GLfloat *params )
    GET_CURRENT_CONTEXT(ctx);
    GLint i = (GLint) (light - GL_LIGHT0);
    GLfloat temp[4];
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (i < 0 || i >= (GLint) ctx->Const.MaxLights) {
       _mesa_error( ctx, GL_INVALID_ENUM, "glLight(light=0x%x)", light );
@@ -244,31 +236,21 @@ _mesa_Lightfv( GLenum light, GLenum pname, const GLfloat *params )
       params = temp;
       break;
    case GL_SPOT_EXPONENT:
-      if (params[0] < 0.0 || params[0] > ctx->Const.MaxSpotExponent) {
+      if (params[0] < 0.0F || params[0] > ctx->Const.MaxSpotExponent) {
         _mesa_error(ctx, GL_INVALID_VALUE, "glLight");
         return;
       }
       break;
    case GL_SPOT_CUTOFF:
-      if ((params[0] < 0.0 || params[0] > 90.0) && params[0] != 180.0) {
+      if ((params[0] < 0.0F || params[0] > 90.0F) && params[0] != 180.0F) {
         _mesa_error(ctx, GL_INVALID_VALUE, "glLight");
         return;
       }
       break;
    case GL_CONSTANT_ATTENUATION:
-      if (params[0] < 0.0) {
-        _mesa_error(ctx, GL_INVALID_VALUE, "glLight");
-        return;
-      }
-      break;
    case GL_LINEAR_ATTENUATION:
-      if (params[0] < 0.0) {
-        _mesa_error(ctx, GL_INVALID_VALUE, "glLight");
-        return;
-      }
-      break;
    case GL_QUADRATIC_ATTENUATION:
-      if (params[0] < 0.0) {
+      if (params[0] < 0.0F) {
         _mesa_error(ctx, GL_INVALID_VALUE, "glLight");
         return;
       }
@@ -285,7 +267,10 @@ _mesa_Lightfv( GLenum light, GLenum pname, const GLfloat *params )
 void GLAPIENTRY
 _mesa_Lighti( GLenum light, GLenum pname, GLint param )
 {
-   _mesa_Lightiv( light, pname, &param );
+   GLint iparam[4];
+   iparam[0] = param;
+   iparam[1] = iparam[2] = iparam[3] = 0;
+   _mesa_Lightiv( light, pname, iparam );
 }
 
 
@@ -336,7 +321,6 @@ _mesa_GetLightfv( GLenum light, GLenum pname, GLfloat *params )
 {
    GET_CURRENT_CONTEXT(ctx);
    GLint l = (GLint) (light - GL_LIGHT0);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (l < 0 || l >= (GLint) ctx->Const.MaxLights) {
       _mesa_error( ctx, GL_INVALID_ENUM, "glGetLightfv" );
@@ -386,7 +370,6 @@ _mesa_GetLightiv( GLenum light, GLenum pname, GLint *params )
 {
    GET_CURRENT_CONTEXT(ctx);
    GLint l = (GLint) (light - GL_LIGHT0);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (l < 0 || l >= (GLint) ctx->Const.MaxLights) {
       _mesa_error( ctx, GL_INVALID_ENUM, "glGetLightiv" );
@@ -457,7 +440,6 @@ _mesa_LightModelfv( GLenum pname, const GLfloat *params )
    GLenum newenum;
    GLboolean newbool;
    GET_CURRENT_CONTEXT(ctx);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    switch (pname) {
       case GL_LIGHT_MODEL_AMBIENT:
@@ -467,24 +449,24 @@ _mesa_LightModelfv( GLenum pname, const GLfloat *params )
          COPY_4V( ctx->Light.Model.Ambient, params );
          break;
       case GL_LIGHT_MODEL_LOCAL_VIEWER:
-         newbool = (params[0]!=0.0);
+         if (ctx->API != API_OPENGL_COMPAT)
+            goto invalid_pname;
+         newbool = (params[0] != 0.0F);
         if (ctx->Light.Model.LocalViewer == newbool)
            return;
         FLUSH_VERTICES(ctx, _NEW_LIGHT);
         ctx->Light.Model.LocalViewer = newbool;
          break;
       case GL_LIGHT_MODEL_TWO_SIDE:
-         newbool = (params[0]!=0.0);
+         newbool = (params[0] != 0.0F);
         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:
+         if (ctx->API != API_OPENGL_COMPAT)
+            goto invalid_pname;
          if (params[0] == (GLfloat) GL_SINGLE_COLOR)
            newenum = GL_SINGLE_COLOR;
          else if (params[0] == (GLfloat) GL_SEPARATE_SPECULAR_COLOR)
@@ -500,12 +482,17 @@ _mesa_LightModelfv( GLenum pname, const GLfloat *params )
         ctx->Light.Model.ColorControl = newenum;
          break;
       default:
-         _mesa_error( ctx, GL_INVALID_ENUM, "glLightModel(pname=0x%x)", pname );
-         break;
+         goto invalid_pname;
    }
 
    if (ctx->Driver.LightModelfv)
       ctx->Driver.LightModelfv( ctx, pname, params );
+
+   return;
+
+invalid_pname:
+   _mesa_error( ctx, GL_INVALID_ENUM, "glLightModel(pname=0x%x)", pname );
+   return;
 }
 
 
@@ -537,14 +524,20 @@ _mesa_LightModeliv( GLenum pname, const GLint *params )
 void GLAPIENTRY
 _mesa_LightModeli( GLenum pname, GLint param )
 {
-   _mesa_LightModeliv( pname, &param );
+   GLint iparam[4];
+   iparam[0] = param;
+   iparam[1] = iparam[2] = iparam[3] = 0;
+   _mesa_LightModeliv( pname, iparam );
 }
 
 
 void GLAPIENTRY
 _mesa_LightModelf( GLenum pname, GLfloat param )
 {
-   _mesa_LightModelfv( pname, &param );
+   GLfloat fparam[4];
+   fparam[0] = param;
+   fparam[1] = fparam[2] = fparam[3] = 0.0F;
+   _mesa_LightModelfv( pname, fparam );
 }
 
 
@@ -557,7 +550,7 @@ _mesa_LightModelf( GLenum pname, GLfloat param )
  * of the targeted material values.
  */
 GLuint
-_mesa_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
+_mesa_material_bitmask( struct gl_context *ctx, GLenum face, GLenum pname,
                         GLuint legal, const char *where )
 {
    GLuint bitmask = 0;
@@ -587,7 +580,7 @@ _mesa_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
          bitmask |= MAT_BIT_FRONT_INDEXES  | MAT_BIT_BACK_INDEXES;
          break;
       default:
-         _mesa_error( ctx, GL_INVALID_ENUM, where );
+         _mesa_error( ctx, GL_INVALID_ENUM, "%s", where );
          return 0;
    }
 
@@ -598,12 +591,12 @@ _mesa_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
       bitmask &= BACK_MATERIAL_BITS;
    }
    else if (face != GL_FRONT_AND_BACK) {
-      _mesa_error( ctx, GL_INVALID_ENUM, where );
+      _mesa_error( ctx, GL_INVALID_ENUM, "%s", where );
       return 0;
    }
 
    if (bitmask & ~legal) {
-      _mesa_error( ctx, GL_INVALID_ENUM, where );
+      _mesa_error( ctx, GL_INVALID_ENUM, "%s", where );
       return 0;
    }
 
@@ -612,31 +605,14 @@ _mesa_material_bitmask( GLcontext *ctx, GLenum face, GLenum pname,
 
 
 
-/* Perform a straight copy between materials.
- */
-void
-_mesa_copy_materials( struct gl_material *dst,
-                      const struct gl_material *src,
-                      GLuint bitmask )
-{
-   int i;
-
-   for (i = 0 ; i < MAT_ATTRIB_MAX ; i++) 
-      if (bitmask & (1<<i))
-        COPY_4FV( dst->Attrib[i], src->Attrib[i] );
-}
-
-
-
 /* Update derived values following a change in ctx->Light.Material
  */
 void
-_mesa_update_material( GLcontext *ctx, GLuint bitmask )
+_mesa_update_material( struct gl_context *ctx, GLuint bitmask )
 {
-   struct gl_light *light, *list = &ctx->Light.EnabledList;
    GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
 
-   if (MESA_VERBOSE & VERBOSE_MATERIAL) 
+   if (MESA_VERBOSE & VERBOSE_MATERIAL)
       _mesa_debug(ctx, "_mesa_update_material, mask 0x%x\n", bitmask);
 
    if (!bitmask)
@@ -644,15 +620,21 @@ _mesa_update_material( GLcontext *ctx, GLuint bitmask )
 
    /* update material ambience */
    if (bitmask & MAT_BIT_FRONT_AMBIENT) {
-      foreach (light, list) {
-         SCALE_3V( light->_MatAmbient[0], light->Ambient, 
+      GLbitfield mask = ctx->Light._EnabledLights;
+      while (mask) {
+         const int i = u_bit_scan(&mask);
+         struct gl_light *light = &ctx->Light.Light[i];
+         SCALE_3V( light->_MatAmbient[0], light->Ambient,
                   mat[MAT_ATTRIB_FRONT_AMBIENT]);
       }
    }
 
    if (bitmask & MAT_BIT_BACK_AMBIENT) {
-      foreach (light, list) {
-         SCALE_3V( light->_MatAmbient[1], light->Ambient, 
+      GLbitfield mask = ctx->Light._EnabledLights;
+      while (mask) {
+         const int i = u_bit_scan(&mask);
+         struct gl_light *light = &ctx->Light.Light[i];
+         SCALE_3V( light->_MatAmbient[1], light->Ambient,
                   mat[MAT_ATTRIB_BACK_AMBIENT]);
       }
    }
@@ -672,59 +654,64 @@ _mesa_update_material( GLcontext *ctx, GLuint bitmask )
 
    /* update material diffuse values */
    if (bitmask & MAT_BIT_FRONT_DIFFUSE) {
-      foreach (light, list) {
-        SCALE_3V( light->_MatDiffuse[0], light->Diffuse, 
+      GLbitfield mask = ctx->Light._EnabledLights;
+      while (mask) {
+         const int i = u_bit_scan(&mask);
+         struct gl_light *light = &ctx->Light.Light[i];
+        SCALE_3V( light->_MatDiffuse[0], light->Diffuse,
                   mat[MAT_ATTRIB_FRONT_DIFFUSE] );
       }
    }
 
    if (bitmask & MAT_BIT_BACK_DIFFUSE) {
-      foreach (light, list) {
-        SCALE_3V( light->_MatDiffuse[1], light->Diffuse, 
+      GLbitfield mask = ctx->Light._EnabledLights;
+      while (mask) {
+         const int i = u_bit_scan(&mask);
+         struct gl_light *light = &ctx->Light.Light[i];
+        SCALE_3V( light->_MatDiffuse[1], light->Diffuse,
                   mat[MAT_ATTRIB_BACK_DIFFUSE] );
       }
    }
 
    /* update material specular values */
    if (bitmask & MAT_BIT_FRONT_SPECULAR) {
-      foreach (light, list) {
-        SCALE_3V( light->_MatSpecular[0], light->Specular, 
+      GLbitfield mask = ctx->Light._EnabledLights;
+      while (mask) {
+         const int i = u_bit_scan(&mask);
+         struct gl_light *light = &ctx->Light.Light[i];
+        SCALE_3V( light->_MatSpecular[0], light->Specular,
                   mat[MAT_ATTRIB_FRONT_SPECULAR]);
       }
    }
 
    if (bitmask & MAT_BIT_BACK_SPECULAR) {
-      foreach (light, list) {
+      GLbitfield mask = ctx->Light._EnabledLights;
+      while (mask) {
+         const int i = u_bit_scan(&mask);
+         struct gl_light *light = &ctx->Light.Light[i];
         SCALE_3V( light->_MatSpecular[1], light->Specular,
                   mat[MAT_ATTRIB_BACK_SPECULAR]);
       }
    }
-
-   if (bitmask & MAT_BIT_FRONT_SHININESS) {
-      _mesa_invalidate_shine_table( ctx, 0 );
-   }
-
-   if (bitmask & MAT_BIT_BACK_SHININESS) {
-      _mesa_invalidate_shine_table( ctx, 1 );
-   }
 }
 
 
 /*
  * Update the current materials from the given rgba color
- * according to the bitmask in ColorMaterialBitmask, which is
+ * according to the bitmask in _ColorMaterialBitmask, which is
  * set by glColorMaterial().
  */
 void
-_mesa_update_color_material( GLcontext *ctx, const GLfloat color[4] )
+_mesa_update_color_material( struct gl_context *ctx, const GLfloat color[4] )
 {
-   GLuint bitmask = ctx->Light.ColorMaterialBitmask;
+   GLbitfield bitmask = ctx->Light._ColorMaterialBitmask;
    struct gl_material *mat = &ctx->Light.Material;
-   int i;
 
-   for (i = 0 ; i < MAT_ATTRIB_MAX ; i++) 
-      if (bitmask & (1<<i))
-        COPY_4FV( mat->Attrib[i], color );
+   while (bitmask) {
+      const int i = u_bit_scan(&bitmask);
+
+      COPY_4FV( mat->Attrib[i], color );
+   }
 
    _mesa_update_material( ctx, bitmask );
 }
@@ -739,22 +726,23 @@ _mesa_ColorMaterial( GLenum face, GLenum mode )
                   MAT_BIT_FRONT_SPECULAR | MAT_BIT_BACK_SPECULAR |
                   MAT_BIT_FRONT_DIFFUSE  | MAT_BIT_BACK_DIFFUSE  |
                   MAT_BIT_FRONT_AMBIENT  | MAT_BIT_BACK_AMBIENT);
-   ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    if (MESA_VERBOSE&VERBOSE_API)
       _mesa_debug(ctx, "glColorMaterial %s %s\n",
-                  _mesa_lookup_enum_by_nr(face),
-                  _mesa_lookup_enum_by_nr(mode));
+                  _mesa_enum_to_string(face),
+                  _mesa_enum_to_string(mode));
 
    bitmask = _mesa_material_bitmask(ctx, face, mode, legal, "glColorMaterial");
+   if (bitmask == 0)
+      return; /* error was recorded */
 
-   if (ctx->Light.ColorMaterialBitmask == bitmask &&
+   if (ctx->Light._ColorMaterialBitmask == bitmask &&
        ctx->Light.ColorMaterialFace == face &&
        ctx->Light.ColorMaterialMode == mode)
       return;
 
    FLUSH_VERTICES(ctx, _NEW_LIGHT);
-   ctx->Light.ColorMaterialBitmask = bitmask;
+   ctx->Light._ColorMaterialBitmask = bitmask;
    ctx->Light.ColorMaterialFace = face;
    ctx->Light.ColorMaterialMode = mode;
 
@@ -774,7 +762,7 @@ _mesa_GetMaterialfv( GLenum face, GLenum pname, GLfloat *params )
    GET_CURRENT_CONTEXT(ctx);
    GLuint f;
    GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* update materials */
+   FLUSH_VERTICES(ctx, 0); /* update materials */
 
    FLUSH_CURRENT(ctx, 0); /* update ctx->Light.Material from vertex buffer */
 
@@ -806,6 +794,10 @@ _mesa_GetMaterialfv( GLenum face, GLenum pname, GLfloat *params )
         *params = mat[MAT_ATTRIB_SHININESS(f)][0];
         break;
       case GL_COLOR_INDEXES:
+         if (ctx->API != API_OPENGL_COMPAT) {
+            _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" );
+            return;
+         }
         params[0] = mat[MAT_ATTRIB_INDEXES(f)][0];
         params[1] = mat[MAT_ATTRIB_INDEXES(f)][1];
         params[2] = mat[MAT_ATTRIB_INDEXES(f)][2];
@@ -822,8 +814,10 @@ _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
    GET_CURRENT_CONTEXT(ctx);
    GLuint f;
    GLfloat (*mat)[4] = ctx->Light.Material.Attrib;
-   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx); /* update materials */
 
+   assert(ctx->API == API_OPENGL_COMPAT);
+
+   FLUSH_VERTICES(ctx, 0); /* update materials */
    FLUSH_CURRENT(ctx, 0); /* update ctx->Light.Material from vertex buffer */
 
    if (face==GL_FRONT) {
@@ -862,12 +856,12 @@ _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
          params[3] = FLOAT_TO_INT( mat[MAT_ATTRIB_EMISSION(f)][3] );
         break;
       case GL_SHININESS:
-         *params = IROUND( mat[MAT_ATTRIB_SHININESS(f)][0] );
+         *params = lroundf( mat[MAT_ATTRIB_SHININESS(f)][0] );
         break;
       case GL_COLOR_INDEXES:
-        params[0] = IROUND( mat[MAT_ATTRIB_INDEXES(f)][0] );
-        params[1] = IROUND( mat[MAT_ATTRIB_INDEXES(f)][1] );
-        params[2] = IROUND( mat[MAT_ATTRIB_INDEXES(f)][2] );
+        params[0] = lroundf( mat[MAT_ATTRIB_INDEXES(f)][0] );
+        params[1] = lroundf( mat[MAT_ATTRIB_INDEXES(f)][1] );
+        params[2] = lroundf( mat[MAT_ATTRIB_INDEXES(f)][2] );
         break;
       default:
          _mesa_error( ctx, GL_INVALID_ENUM, "glGetMaterialfv(pname)" );
@@ -876,171 +870,6 @@ _mesa_GetMaterialiv( GLenum face, GLenum pname, GLint *params )
 
 
 
-/**********************************************************************/
-/*****                  Lighting computation                      *****/
-/**********************************************************************/
-
-
-/*
- * Notes:
- *   When two-sided lighting is enabled we compute the color (or index)
- *   for both the front and back side of the primitive.  Then, when the
- *   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
- *       the side or sides which are actually required.
- *
- * Variables:
- *   n = normal vector
- *   V = vertex position
- *   P = light source position
- *   Pe = (0,0,0,1)
- *
- * Precomputed:
- *   IF P[3]==0 THEN
- *       // light at infinity
- *       IF local_viewer THEN
- *           _VP_inf_norm = unit vector from V to P      // Precompute
- *       ELSE
- *           // eye at infinity
- *           _h_inf_norm = Normalize( VP + <0,0,1> )     // Precompute
- *       ENDIF
- *   ENDIF
- *
- * Functions:
- *   Normalize( v ) = normalized vector v
- *   Magnitude( v ) = length of vector v
- */
-
-
-
-/*
- * Whenever the spotlight exponent for a light changes we must call
- * this function to recompute the exponent lookup table.
- */
-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 )
-{
-   GLint i;
-   GLdouble exponent = l->SpotExponent;
-   GLdouble tmp = 0;
-   GLint clamp = 0;
-
-   l->_SpotExpTable[0][0] = 0.0;
-
-   for (i = EXP_TABLE_SIZE - 1; i > 0 ;i--) {
-      if (clamp == 0) {
-        tmp = _mesa_pow(i / (GLdouble) (EXP_TABLE_SIZE - 1), exponent);
-        if (tmp < FLT_MIN * 100.0) {
-           tmp = 0.0;
-           clamp = 1;
-        }
-      }
-      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]);
-   }
-   l->_SpotExpTable[EXP_TABLE_SIZE-1][1] = 0.0;
-}
-
-
-
-/* Calculate a new shine table.  Doing this here saves a branch in
- * lighting, and the cost of doing it early may be partially offset
- * by keeping a MRU cache of shine tables for various shine values.
- */
-void
-_mesa_invalidate_shine_table( GLcontext *ctx, GLuint side )
-{
-   ASSERT(side < 2);
-   if (ctx->_ShineTable[side])
-      ctx->_ShineTable[side]->refcount--;
-   ctx->_ShineTable[side] = NULL;
-}
-
-
-static void
-validate_shine_table( GLcontext *ctx, GLuint side, GLfloat shininess )
-{
-   struct gl_shine_tab *list = ctx->_ShineTabList;
-   struct gl_shine_tab *s;
-
-   ASSERT(side < 2);
-
-   foreach(s, list)
-      if ( s->shininess == shininess )
-        break;
-
-   if (s == list) {
-      GLint j;
-      GLfloat *m;
-
-      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 = _mesa_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;
-   }
-
-   if (ctx->_ShineTable[side])
-      ctx->_ShineTable[side]->refcount--;
-
-   ctx->_ShineTable[side] = s;
-   move_to_tail( list, s );
-   s->refcount++;
-}
-
-
-void
-_mesa_validate_all_lighting_tables( GLcontext *ctx )
-{
-   GLuint i;
-   GLfloat shininess;
-   
-   shininess = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
-   if (!ctx->_ShineTable[0] || ctx->_ShineTable[0]->shininess != shininess)
-      validate_shine_table( ctx, 0, shininess );
-
-   shininess = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0];
-   if (!ctx->_ShineTable[1] || ctx->_ShineTable[1]->shininess != shininess)
-      validate_shine_table( ctx, 1, shininess );
-
-   for (i = 0; i < ctx->Const.MaxLights; 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.
@@ -1048,25 +877,27 @@ _mesa_validate_all_lighting_tables( GLcontext *ctx )
  * source and material ambient, diffuse and specular coefficients.
  */
 void
-_mesa_update_lighting( GLcontext *ctx )
+_mesa_update_lighting( struct gl_context *ctx )
 {
-   struct gl_light *light;
+   GLbitfield flags = 0;
    ctx->Light._NeedEyeCoords = GL_FALSE;
-   ctx->Light._Flags = 0;
 
    if (!ctx->Light.Enabled)
       return;
 
-   foreach(light, &ctx->Light.EnabledList) {
-      ctx->Light._Flags |= light->_Flags;
+   GLbitfield mask = ctx->Light._EnabledLights;
+   while (mask) {
+      const int i = u_bit_scan(&mask);
+      struct gl_light *light = &ctx->Light.Light[i];
+      flags |= light->_Flags;
    }
 
    ctx->Light._NeedVertices =
-      ((ctx->Light._Flags & (LIGHT_POSITIONAL|LIGHT_SPOT)) ||
+      ((flags & (LIGHT_POSITIONAL|LIGHT_SPOT)) ||
        ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR ||
        ctx->Light.Model.LocalViewer);
 
-   ctx->Light._NeedEyeCoords = ((ctx->Light._Flags & LIGHT_POSITIONAL) ||
+   ctx->Light._NeedEyeCoords = ((flags & LIGHT_POSITIONAL) ||
                                ctx->Light.Model.LocalViewer);
 
    /* XXX: This test is overkill & needs to be fixed both for software and
@@ -1081,31 +912,22 @@ _mesa_update_lighting( GLcontext *ctx )
     * FLUSH_UPDATE_CURRENT, as when any outstanding material changes
     * are flushed, they will update the derived state at that time.
     */
-   if (ctx->Visual.rgbMode) {
-      if (ctx->Light.Model.TwoSide)
-        _mesa_update_material( ctx, 
-                               MAT_BIT_FRONT_EMISSION |
-                               MAT_BIT_FRONT_AMBIENT |
-                               MAT_BIT_FRONT_DIFFUSE | 
-                               MAT_BIT_FRONT_SPECULAR |
-                               MAT_BIT_BACK_EMISSION |
-                               MAT_BIT_BACK_AMBIENT |
-                               MAT_BIT_BACK_DIFFUSE | 
-                               MAT_BIT_BACK_SPECULAR);
-      else
-        _mesa_update_material( ctx, 
-                               MAT_BIT_FRONT_EMISSION |
-                               MAT_BIT_FRONT_AMBIENT |
-                               MAT_BIT_FRONT_DIFFUSE | 
-                               MAT_BIT_FRONT_SPECULAR);
-   }
-   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);
-      }
-   }
+   if (ctx->Light.Model.TwoSide)
+      _mesa_update_material(ctx,
+                           MAT_BIT_FRONT_EMISSION |
+                           MAT_BIT_FRONT_AMBIENT |
+                           MAT_BIT_FRONT_DIFFUSE |
+                           MAT_BIT_FRONT_SPECULAR |
+                           MAT_BIT_BACK_EMISSION |
+                           MAT_BIT_BACK_AMBIENT |
+                           MAT_BIT_BACK_DIFFUSE |
+                           MAT_BIT_BACK_SPECULAR);
+   else
+      _mesa_update_material(ctx,
+                           MAT_BIT_FRONT_EMISSION |
+                           MAT_BIT_FRONT_AMBIENT |
+                           MAT_BIT_FRONT_DIFFUSE |
+                           MAT_BIT_FRONT_SPECULAR);
 }
 
 
@@ -1120,9 +942,8 @@ _mesa_update_lighting( GLcontext *ctx )
  * Also update on lighting space changes.
  */
 static void
-compute_light_positions( GLcontext *ctx )
+compute_light_positions( struct gl_context *ctx )
 {
-   struct gl_light *light;
    static const GLfloat eye_z[3] = { 0, 0, 1 };
 
    if (!ctx->Light.Enabled)
@@ -1135,7 +956,10 @@ compute_light_positions( GLcontext *ctx )
       TRANSFORM_NORMAL( ctx->_EyeZDir, eye_z, ctx->ModelviewMatrixStack.Top->m );
    }
 
-   foreach (light, &ctx->Light.EnabledList) {
+   GLbitfield mask = ctx->Light._EnabledLights;
+   while (mask) {
+      const int i = u_bit_scan(&mask);
+      struct gl_light *light = &ctx->Light.Light[i];
 
       if (ctx->_NeedEyeCoords) {
          /* _Position is in eye coordinate space */
@@ -1161,7 +985,7 @@ compute_light_positions( GLcontext *ctx )
       }
       else {
          /* positional light w/ homogeneous coordinate, divide by W */
-         GLfloat wInv = (GLfloat)1.0 / light->_Position[3];
+         GLfloat wInv = 1.0F / light->_Position[3];
          light->_Position[0] *= wInv;
          light->_Position[1] *= wInv;
          light->_Position[2] *= wInv;
@@ -1190,11 +1014,8 @@ compute_light_positions( GLcontext *ctx )
                                        light->_NormSpotDirection);
 
            if (PV_dot_dir > light->_CosCutoff) {
-              double x = PV_dot_dir * (EXP_TABLE_SIZE-1);
-              int k = (int) x;
               light->_VP_inf_spot_attenuation =
-                 (GLfloat) (light->_SpotExpTable[k][0] +
-                  (x-k)*light->_SpotExpTable[k][1]);
+                  powf(PV_dot_dir, light->SpotExponent);
            }
            else {
               light->_VP_inf_spot_attenuation = 0;
@@ -1207,17 +1028,19 @@ compute_light_positions( GLcontext *ctx )
 
 
 static void
-update_modelview_scale( GLcontext *ctx )
+update_modelview_scale( struct gl_context *ctx )
 {
    ctx->_ModelViewInvScale = 1.0F;
+   ctx->_ModelViewInvScaleEyespace = 1.0F;
    if (!_math_matrix_is_length_preserving(ctx->ModelviewMatrixStack.Top)) {
       const GLfloat *m = ctx->ModelviewMatrixStack.Top->inv;
       GLfloat f = m[2] * m[2] + m[6] * m[6] + m[10] * m[10];
-      if (f < 1e-12) f = 1.0;
+      if (f < 1e-12f) f = 1.0f;
       if (ctx->_NeedEyeCoords)
-        ctx->_ModelViewInvScale = (GLfloat) INV_SQRTF(f);
+        ctx->_ModelViewInvScale = 1.0f / sqrtf(f);
       else
-        ctx->_ModelViewInvScale = (GLfloat) SQRTF(f);
+        ctx->_ModelViewInvScale = sqrtf(f);
+      ctx->_ModelViewInvScaleEyespace = 1.0f / sqrtf(f);
    }
 }
 
@@ -1226,7 +1049,7 @@ update_modelview_scale( GLcontext *ctx )
  * Bring up to date any state that relies on _NeedEyeCoords.
  */
 void
-_mesa_update_tnl_spaces( GLcontext *ctx, GLuint new_state )
+_mesa_update_tnl_spaces( struct gl_context *ctx, GLuint new_state )
 {
    const GLuint oldneedeyecoords = ctx->_NeedEyeCoords;
 
@@ -1275,7 +1098,7 @@ _mesa_update_tnl_spaces( GLcontext *ctx, GLuint new_state )
  * light-in-modelspace optimization.  It's also useful for debugging.
  */
 void
-_mesa_allow_light_in_model( GLcontext *ctx, GLboolean flag )
+_mesa_allow_light_in_model( struct gl_context *ctx, GLboolean flag )
 {
    ctx->_ForceEyeCoords = !flag;
    ctx->NewState |= _NEW_POINT;        /* one of the bits from
@@ -1293,14 +1116,12 @@ _mesa_allow_light_in_model( GLcontext *ctx, GLboolean flag )
  * Initialize the n-th light data structure.
  *
  * \param l pointer to the gl_light structure to be initialized.
- * \param n number of the light. 
+ * \param n number of the light.
  * \note The defaults for light 0 are different than the other lights.
  */
 static void
 init_light( struct gl_light *l, GLuint n )
 {
-   make_empty_list( l );
-
    ASSIGN_4V( l->Ambient, 0.0, 0.0, 0.0, 1.0 );
    if (n==0) {
       ASSIGN_4V( l->Diffuse, 1.0, 1.0, 1.0, 1.0 );
@@ -1313,9 +1134,7 @@ init_light( struct gl_light *l, GLuint n )
    ASSIGN_4V( l->EyePosition, 0.0, 0.0, 1.0, 0.0 );
    ASSIGN_3V( l->SpotDirection, 0.0, 0.0, -1.0 );
    l->SpotExponent = 0.0;
-   _mesa_invalidate_spot_exp_table( l );
    l->SpotCutoff = 180.0;
-   l->_CosCutoffNeg = -1.0f;
    l->_CosCutoff = 0.0;                /* KW: -ve values not admitted */
    l->ConstantAttenuation = 1.0;
    l->LinearAttenuation = 0.0;
@@ -1341,7 +1160,7 @@ init_lightmodel( struct gl_lightmodel *lm )
 
 /**
  * Initialize the material data structure.
- * 
+ *
  * \param m pointer to the gl_material structure to be initialized.
  */
 static void
@@ -1353,7 +1172,7 @@ init_material( struct gl_material *m )
    ASSIGN_4V( m->Attrib[MAT_ATTRIB_FRONT_EMISSION], 0.0F, 0.0F, 0.0F, 1.0F );
    ASSIGN_4V( m->Attrib[MAT_ATTRIB_FRONT_SHININESS], 0.0F, 0.0F, 0.0F, 0.0F );
    ASSIGN_4V( m->Attrib[MAT_ATTRIB_FRONT_INDEXES], 0.0F, 1.0F, 1.0F, 0.0F );
+
    ASSIGN_4V( m->Attrib[MAT_ATTRIB_BACK_AMBIENT],  0.2F, 0.2F, 0.2F, 1.0F );
    ASSIGN_4V( m->Attrib[MAT_ATTRIB_BACK_DIFFUSE],  0.8F, 0.8F, 0.8F, 1.0F );
    ASSIGN_4V( m->Attrib[MAT_ATTRIB_BACK_SPECULAR], 0.0F, 0.0F, 0.0F, 1.0F );
@@ -1367,15 +1186,15 @@ init_material( struct gl_material *m )
  * Initialize all lighting state for the given context.
  */
 void
-_mesa_init_lighting( GLcontext *ctx )
+_mesa_init_lighting( struct gl_context *ctx )
 {
    GLuint i;
 
    /* Lighting group */
+   ctx->Light._EnabledLights = 0;
    for (i = 0; i < MAX_LIGHTS; i++) {
       init_light( &ctx->Light.Light[i], i );
    }
-   make_empty_list( &ctx->Light.EnabledList );
 
    init_lightmodel( &ctx->Light.Model );
    init_material( &ctx->Light.Material );
@@ -1384,44 +1203,19 @@ _mesa_init_lighting( GLcontext *ctx )
    ctx->Light.Enabled = GL_FALSE;
    ctx->Light.ColorMaterialFace = GL_FRONT_AND_BACK;
    ctx->Light.ColorMaterialMode = GL_AMBIENT_AND_DIFFUSE;
-   ctx->Light.ColorMaterialBitmask = _mesa_material_bitmask( ctx,
+   ctx->Light._ColorMaterialBitmask = _mesa_material_bitmask( ctx,
                                                GL_FRONT_AND_BACK,
                                                GL_AMBIENT_AND_DIFFUSE, ~0,
                                                NULL );
 
    ctx->Light.ColorMaterialEnabled = GL_FALSE;
-   ctx->Light.ClampVertexColor = GL_TRUE;
-
-   /* Lighting miscellaneous */
-   ctx->_ShineTabList = MALLOC_STRUCT( gl_shine_tab );
-   make_empty_list( ctx->_ShineTabList );
-   /* Allocate 10 (arbitrary) shininess lookup tables */
-   for (i = 0 ; i < 10 ; i++) {
-      struct gl_shine_tab *s = MALLOC_STRUCT( gl_shine_tab );
-      s->shininess = -1;
-      s->refcount = 0;
-      insert_at_tail( ctx->_ShineTabList, s );
-   }
+   ctx->Light.ClampVertexColor = ctx->API == API_OPENGL_COMPAT;
+   ctx->Light._ClampVertexColor = ctx->API == API_OPENGL_COMPAT;
 
    /* Miscellaneous */
    ctx->Light._NeedEyeCoords = GL_FALSE;
    ctx->_NeedEyeCoords = GL_FALSE;
    ctx->_ForceEyeCoords = GL_FALSE;
    ctx->_ModelViewInvScale = 1.0;
-}
-
-
-/**
- * Deallocate malloc'd lighting state attached to given context.
- */
-void
-_mesa_free_lighting_data( GLcontext *ctx )
-{
-   struct gl_shine_tab *s, *tmps;
-
-   /* Free lighting shininess exponentiation table */
-   foreach_s( s, tmps, ctx->_ShineTabList ) {
-      _mesa_free( s );
-   }
-   _mesa_free( ctx->_ShineTabList );
+   ctx->_ModelViewInvScaleEyespace = 1.0;
 }