fragment program execution
[mesa.git] / src / mesa / tnl / t_vb_lighttmp.h
index 76d2a8c047b26a2b447fd9ff035bb24ab2288e85..90d7652063df96e87fd2a065b819dcdacb498797 100644 (file)
@@ -1,8 +1,8 @@
-/* $Id: t_vb_lighttmp.h,v 1.12 2001/04/28 08:39:18 keithw Exp $ */
+/* $Id: t_vb_lighttmp.h,v 1.26 2002/10/29 20:29:03 brianp Exp $ */
 
 /*
  * Mesa 3-D graphics library
- * Version:  3.5
+ * Version:  4.1
  *
  * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
  *
  *
  *
  * Authors:
- *    Brian Paul <brianp@valinux.com>
- *    Keith Whitwell <keithw@valinux.com>
+ *    Brian Paul
+ *    Keith Whitwell <keith@tungstengraphics.com>
  */
 
 
 #if (IDX & LIGHT_FLAGS)
 #  define VSTRIDE (4 * sizeof(GLfloat))
-#  define NSTRIDE (3 * sizeof(GLfloat))
-#  define CHECK_MATERIAL(x)  (flags[x] & VERT_MATERIAL)
-#  define CHECK_END_VB(x)    (flags[x] & VERT_END_VB)
+#  define NSTRIDE nstride /*(3 * sizeof(GLfloat))*/
+#  define CHECK_MATERIAL(x)  (flags[x] & VERT_BIT_MATERIAL)
+#  define CHECK_END_VB(x)    (flags[x] & VERT_BIT_END_VB)
 #  if (IDX & LIGHT_COLORMATERIAL)
-#    define CMSTRIDE STRIDE_F(CMcolor, (4 * sizeof(GLfloat)))
-#    define CHECK_COLOR_MATERIAL(x) (flags[x] & VERT_RGBA)
-#    define CHECK_VALIDATE(x) (flags[x] & (VERT_RGBA|VERT_MATERIAL))
+#    define CMSTRIDE STRIDE_F(CMcolor, CMstride)
+#    define CHECK_COLOR_MATERIAL(x) (flags[x] & VERT_BIT_COLOR0)
+#    define CHECK_VALIDATE(x) (flags[x] & (VERT_BIT_COLOR0|VERT_BIT_MATERIAL))
 #    define DO_ANOTHER_NORMAL(x) \
-     ((flags[x] & (VERT_RGBA|VERT_NORM|VERT_END_VB|VERT_MATERIAL)) == VERT_NORM)
+     ((flags[x] & (VERT_BIT_COLOR0|VERT_BIT_NORMAL|VERT_BIT_END_VB|VERT_BIT_MATERIAL)) == VERT_BIT_NORMAL)
 #    define REUSE_LIGHT_RESULTS(x) \
-     ((flags[x] & (VERT_RGBA|VERT_NORM|VERT_END_VB|VERT_MATERIAL)) == 0)
+     ((flags[x] & (VERT_BIT_COLOR0|VERT_BIT_NORMAL|VERT_BIT_END_VB|VERT_BIT_MATERIAL)) == 0)
 #  else
 #    define CMSTRIDE (void)0
 #    define CHECK_COLOR_MATERIAL(x) 0
-#    define CHECK_VALIDATE(x) (flags[x] & (VERT_MATERIAL))
+#    define CHECK_VALIDATE(x) (flags[x] & (VERT_BIT_MATERIAL))
 #    define DO_ANOTHER_NORMAL(x) \
-      ((flags[x] & (VERT_NORM|VERT_END_VB|VERT_MATERIAL)) == VERT_NORM)
+      ((flags[x] & (VERT_BIT_NORMAL|VERT_BIT_END_VB|VERT_BIT_MATERIAL)) == VERT_BIT_NORMAL)
 #    define REUSE_LIGHT_RESULTS(x) \
-      ((flags[x] & (VERT_NORM|VERT_END_VB|VERT_MATERIAL)) == 0)
+      ((flags[x] & (VERT_BIT_NORMAL|VERT_BIT_END_VB|VERT_BIT_MATERIAL)) == 0)
 #  endif
 #else
 #  define VSTRIDE vstride
 #endif
 
 
+/* define TRACE if to trace lighting code */
 
+
+/*
+ * ctx is the current context
+ * VB is the vertex buffer
+ * stage is the lighting stage-private data
+ * input is the vector of eye or object-space vertex coordinates
+ */
 static void TAG(light_rgba_spec)( GLcontext *ctx,
                                  struct vertex_buffer *VB,
                                  struct gl_pipeline_stage *stage,
@@ -88,42 +96,39 @@ static void TAG(light_rgba_spec)( GLcontext *ctx,
 {
    struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
    GLfloat (*base)[3] = ctx->Light._BaseColor;
-   const GLfloat *sumA = ctx->Light._BaseAlpha;
-
+   GLchan sumA[2];
    GLuint j;
 
-   GLuint  vstride = input->stride;
+   const GLuint vstride = input->stride;
    const GLfloat *vertex = (GLfloat *)input->data;
-   GLuint  nstride = VB->NormalPtr->stride;
+   const GLuint nstride = VB->NormalPtr->stride;
    const GLfloat *normal = (GLfloat *)VB->NormalPtr->data;
 
    GLfloat *CMcolor;
    GLuint CMstride;
 
-   GLfloat (*Fcolor)[4] = (GLfloat (*)[4]) store->LitColor[0].Ptr;
-   GLfloat (*Bcolor)[4] = (GLfloat (*)[4]) store->LitColor[1].Ptr;
-   GLfloat (*Fspec)[4] = (GLfloat (*)[4]) store->LitSecondary[0].Ptr;
-   GLfloat (*Bspec)[4] = (GLfloat (*)[4]) store->LitSecondary[1].Ptr;
-   GLfloat (*spec[2])[4];
+   GLchan (*Fcolor)[4] = (GLchan (*)[4]) store->LitColor[0].Ptr;
+   GLchan (*Bcolor)[4] = (GLchan (*)[4]) store->LitColor[1].Ptr;
+   GLchan (*Fspec)[4] = (GLchan (*)[4]) store->LitSecondary[0].Ptr;
+   GLchan (*Bspec)[4] = (GLchan (*)[4]) store->LitSecondary[1].Ptr;
 
-   GLuint nr = VB->Count;
-   GLuint *flags = VB->Flag;
+   const GLuint nr = VB->Count;
+   const GLuint *flags = VB->Flag;
    struct gl_material (*new_material)[2] = VB->Material;
-   GLuint *new_material_mask = VB->MaterialMask;
+   const GLuint *new_material_mask = VB->MaterialMask;
 
    (void) flags;
    (void) nstride;
    (void) vstride;
 
-
-/*     fprintf(stderr, "%s\n", __FUNCTION__ );  */
-
-   spec[0] = Fspec;
-   spec[1] = Bspec;
+#ifdef TRACE
+   fprintf(stderr, "%s\n", __FUNCTION__ );
+#endif
 
    if (IDX & LIGHT_COLORMATERIAL) {
-      if (VB->ColorPtr[0]->Type != GL_FLOAT)
-        VB->import_data( ctx, VERT_RGBA, ~0 );
+      if (VB->ColorPtr[0]->Type != GL_FLOAT || 
+         VB->ColorPtr[0]->Size != 4)
+        import_color_material( ctx, stage );
 
       CMcolor = (GLfloat *) VB->ColorPtr[0]->Ptr;
       CMstride = VB->ColorPtr[0]->StrideB;
@@ -131,10 +136,12 @@ static void TAG(light_rgba_spec)( GLcontext *ctx,
 
    VB->ColorPtr[0] = &store->LitColor[0];
    VB->SecondaryColorPtr[0] = &store->LitSecondary[0];
+   UNCLAMPED_FLOAT_TO_CHAN(sumA[0], ctx->Light.Material[0].Diffuse[3]);
 
    if (IDX & LIGHT_TWOSIDE) {
       VB->ColorPtr[1] = &store->LitColor[1];
       VB->SecondaryColorPtr[1] = &store->LitSecondary[1];
+      UNCLAMPED_FLOAT_TO_CHAN(sumA[1], ctx->Light.Material[1].Diffuse[3]);
    }
 
    /* Side-effects done, can we finish now?
@@ -146,6 +153,7 @@ static void TAG(light_rgba_spec)( GLcontext *ctx,
         j<nr ;
         j++,STRIDE_F(vertex,VSTRIDE),STRIDE_F(normal,NSTRIDE),CMSTRIDE)
    {
+      GLfloat sum[2][3], spec[2][3];
       struct gl_light *light;
 
       if ( CHECK_COLOR_MATERIAL(j) )
@@ -154,17 +162,19 @@ static void TAG(light_rgba_spec)( GLcontext *ctx,
       if ( CHECK_MATERIAL(j) )
         _mesa_update_material( ctx, new_material[j], new_material_mask[j] );
 
-      if ( CHECK_VALIDATE(j) )
-        _mesa_validate_all_lighting_tables( ctx );
+      if ( CHECK_VALIDATE(j) ) {
+        TNL_CONTEXT(ctx)->Driver.NotifyMaterialChange( ctx );
+        UNCLAMPED_FLOAT_TO_CHAN(sumA[0], ctx->Light.Material[0].Diffuse[3]);
+        if (IDX & LIGHT_TWOSIDE) 
+           UNCLAMPED_FLOAT_TO_CHAN(sumA[1], ctx->Light.Material[1].Diffuse[3]);
+      }
 
-      COPY_3V(Fcolor[j], base[0]);
-      Fcolor[j][3] = sumA[0];
-      ZERO_3V(Fspec[j]);
+      COPY_3V(sum[0], base[0]);
+      ZERO_3V(spec[0]);
 
       if (IDX & LIGHT_TWOSIDE) {
-        COPY_3V(Bcolor[j], base[1]);
-        Bcolor[j][3] = sumA[1];
-        ZERO_3V(Bspec[j]);
+        COPY_3V(sum[1], base[1]);
+        ZERO_3V(spec[1]);
       }
 
       /* Add contribution from each enabled light source */
@@ -208,16 +218,15 @@ static void TAG(light_rgba_spec)( GLcontext *ctx,
                  continue; /* this light makes no contribution */
               }
               else {
-                 double x = PV_dot_dir * (EXP_TABLE_SIZE-1);
-                 int k = (int) x;
+                 GLdouble x = PV_dot_dir * (EXP_TABLE_SIZE-1);
+                 GLint k = (GLint) x;
                  GLfloat spot = (GLfloat) (light->_SpotExpTable[k][0]
-                                           + (x-k)*light->_SpotExpTable[k][1]);
+                                   + (x-k)*light->_SpotExpTable[k][1]);
                  attenuation *= spot;
               }
            }
         }
 
-
         if (attenuation < 1e-3)
            continue;           /* this light makes no contribution */
 
@@ -226,30 +235,27 @@ static void TAG(light_rgba_spec)( GLcontext *ctx,
 
         /* Which side gets the diffuse & specular terms? */
         if (n_dot_VP < 0.0F) {
-           ACC_SCALE_SCALAR_3V(Fcolor[j], attenuation, light->_MatAmbient[0]);
+           ACC_SCALE_SCALAR_3V(sum[0], attenuation, light->_MatAmbient[0]);
            if (!(IDX & LIGHT_TWOSIDE)) {
               continue;
            }
-           /* diffuse term */
-           COPY_3V(contrib, light->_MatAmbient[1]);
-           ACC_SCALE_SCALAR_3V(contrib, -n_dot_VP, light->_MatDiffuse[1]);
-           ACC_SCALE_SCALAR_3V(Bcolor[j], attenuation, contrib );
-           correction = -1;
            side = 1;
+           correction = -1;
+           n_dot_VP = -n_dot_VP;
         }
          else {
            if (IDX & LIGHT_TWOSIDE) {
-              ACC_SCALE_SCALAR_3V( Bcolor[j], attenuation, 
-                                   light->_MatAmbient[1]);
+              ACC_SCALE_SCALAR_3V( sum[1], attenuation, light->_MatAmbient[1]);
            }
-           /* diffuse term */
-           COPY_3V(contrib, light->_MatAmbient[0]);
-           ACC_SCALE_SCALAR_3V(contrib, n_dot_VP, light->_MatDiffuse[0]);
-           ACC_SCALE_SCALAR_3V(Fcolor[j], attenuation, contrib );
-           correction = 1;
            side = 0;
+           correction = 1;
         }
 
+        /* diffuse term */
+        COPY_3V(contrib, light->_MatAmbient[side]);
+        ACC_SCALE_SCALAR_3V(contrib, n_dot_VP, light->_MatDiffuse[side]);
+        ACC_SCALE_SCALAR_3V(sum[side], attenuation, contrib );
+
         /* specular term - cannibalize VP... */
         if (ctx->Light.Model.LocalViewer) {
            GLfloat v[3];
@@ -277,11 +283,21 @@ static void TAG(light_rgba_spec)( GLcontext *ctx,
 
            if (spec_coef > 1.0e-10) {
               spec_coef *= attenuation;
-              ACC_SCALE_SCALAR_3V( spec[side][j], spec_coef,
+              ACC_SCALE_SCALAR_3V( spec[side], spec_coef,
                                    light->_MatSpecular[side]);
            }
         }
       } /*loop over lights*/
+
+      UNCLAMPED_FLOAT_TO_RGB_CHAN( Fcolor[j], sum[0] );
+      UNCLAMPED_FLOAT_TO_RGB_CHAN( Fspec[j], spec[0] );
+      Fcolor[j][3] = sumA[0];
+
+      if (IDX & LIGHT_TWOSIDE) {
+        UNCLAMPED_FLOAT_TO_RGB_CHAN( Bcolor[j], sum[1] );
+        UNCLAMPED_FLOAT_TO_RGB_CHAN( Bspec[j], spec[1] );
+        Bcolor[j][3] = sumA[1];
+      }
    }
 }
 
@@ -295,26 +311,29 @@ static void TAG(light_rgba)( GLcontext *ctx,
    GLuint j;
 
    GLfloat (*base)[3] = ctx->Light._BaseColor;
-   const GLfloat *sumA = ctx->Light._BaseAlpha;
+   GLchan sumA[2];
 
-   GLuint  vstride = input->stride;
+   const GLuint vstride = input->stride;
    const GLfloat *vertex = (GLfloat *) input->data;
-   GLuint  nstride = VB->NormalPtr->stride;
+   const GLuint nstride = VB->NormalPtr->stride;
    const GLfloat *normal = (GLfloat *)VB->NormalPtr->data;
 
    GLfloat *CMcolor;
    GLuint CMstride;
 
-   GLfloat (*Fcolor)[4] = (GLfloat (*)[4]) store->LitColor[0].Ptr;
-   GLfloat (*Bcolor)[4] = (GLfloat (*)[4]) store->LitColor[1].Ptr;
-   GLfloat (*color[2])[4];
-   GLuint *flags = VB->Flag;
+   GLchan (*Fcolor)[4] = (GLchan (*)[4]) store->LitColor[0].Ptr;
+   GLchan (*Bcolor)[4] = (GLchan (*)[4]) store->LitColor[1].Ptr;
+   GLchan (*color[2])[4];
+   const GLuint *flags = VB->Flag;
 
    struct gl_material (*new_material)[2] = VB->Material;
-   GLuint *new_material_mask = VB->MaterialMask;
-   GLuint nr = VB->Count;
+   const GLuint *new_material_mask = VB->MaterialMask;
+   const GLuint nr = VB->Count;
+
+#ifdef TRACE
+   fprintf(stderr, "%s\n", __FUNCTION__ );
+#endif
 
-/*     fprintf(stderr, "%s\n", __FUNCTION__ );  */
    (void) flags;
    (void) nstride;
    (void) vstride;
@@ -323,16 +342,21 @@ static void TAG(light_rgba)( GLcontext *ctx,
    color[1] = Bcolor;
 
    if (IDX & LIGHT_COLORMATERIAL) {
-      if (VB->ColorPtr[0]->Type != GL_FLOAT)
-        VB->import_data( ctx, VERT_RGBA, ~0 );
+      if (VB->ColorPtr[0]->Type != GL_FLOAT || 
+         VB->ColorPtr[0]->Size != 4)
+        import_color_material( ctx, stage );
 
       CMcolor = (GLfloat *)VB->ColorPtr[0]->Ptr;
       CMstride = VB->ColorPtr[0]->StrideB;
    }
 
    VB->ColorPtr[0] = &store->LitColor[0];
-   if (IDX & LIGHT_TWOSIDE)
+   UNCLAMPED_FLOAT_TO_CHAN(sumA[0], ctx->Light.Material[0].Diffuse[3]);
+
+   if (IDX & LIGHT_TWOSIDE) {
       VB->ColorPtr[1] = &store->LitColor[1];
+      UNCLAMPED_FLOAT_TO_CHAN(sumA[1], ctx->Light.Material[1].Diffuse[3]);
+   }
 
    if (stage->changed_inputs == 0)
       return;
@@ -341,6 +365,7 @@ static void TAG(light_rgba)( GLcontext *ctx,
         j<nr ;
         j++,STRIDE_F(vertex,VSTRIDE), STRIDE_F(normal,NSTRIDE),CMSTRIDE)
    {
+      GLfloat sum[2][3];
       struct gl_light *light;
 
       if ( CHECK_COLOR_MATERIAL(j) )
@@ -349,17 +374,17 @@ static void TAG(light_rgba)( GLcontext *ctx,
       if ( CHECK_MATERIAL(j) )
         _mesa_update_material( ctx, new_material[j], new_material_mask[j] );
 
-      if ( CHECK_VALIDATE(j) )
-        _mesa_validate_all_lighting_tables( ctx );
-
-      COPY_3V(Fcolor[j], base[0]);
-      Fcolor[j][3] = sumA[0];
-
-      if (IDX & LIGHT_TWOSIDE) {
-        COPY_3V(Bcolor[j], base[1]);
-        Bcolor[j][3] = sumA[1];
+      if ( CHECK_VALIDATE(j) ) {
+        TNL_CONTEXT(ctx)->Driver.NotifyMaterialChange( ctx );
+        UNCLAMPED_FLOAT_TO_CHAN(sumA[0], ctx->Light.Material[0].Diffuse[3]);
+        if (IDX & LIGHT_TWOSIDE)
+           UNCLAMPED_FLOAT_TO_CHAN(sumA[1], ctx->Light.Material[1].Diffuse[3]);
       }
 
+      COPY_3V(sum[0], base[0]);
+
+      if ( IDX & LIGHT_TWOSIDE )
+        COPY_3V(sum[1], base[1]);
 
       /* Add contribution from each enabled light source */
       foreach (light, &ctx->Light.EnabledList) {
@@ -385,7 +410,7 @@ static void TAG(light_rgba)( GLcontext *ctx,
 
            SUB_3V(VP, light->_Position, vertex);
 
-           d = LEN_3FV( VP );
+           d = (GLfloat) LEN_3FV( VP );
 
            if ( d > 1e-6) {
               GLfloat invd = 1.0F / d;
@@ -404,26 +429,24 @@ static void TAG(light_rgba)( GLcontext *ctx,
                  continue; /* this light makes no contribution */
               }
               else {
-                 double x = PV_dot_dir * (EXP_TABLE_SIZE-1);
-                 int k = (int) x;
-                 GLfloat spot = (light->_SpotExpTable[k][0]
+                 GLdouble x = PV_dot_dir * (EXP_TABLE_SIZE-1);
+                 GLint k = (GLint) x;
+                 GLfloat spot = (GLfloat) (light->_SpotExpTable[k][0]
                                  + (x-k)*light->_SpotExpTable[k][1]);
                  attenuation *= spot;
               }
            }
         }
 
-
         if (attenuation < 1e-3)
            continue;           /* this light makes no contribution */
 
-
         /* Compute dot product or normal and vector from V to light pos */
         n_dot_VP = DOT3( normal, VP );
 
         /* which side are we lighting? */
         if (n_dot_VP < 0.0F) {
-           ACC_SCALE_SCALAR_3V(Fcolor[j], attenuation, light->_MatAmbient[0]);
+           ACC_SCALE_SCALAR_3V(sum[0], attenuation, light->_MatAmbient[0]);
 
            if (!(IDX & LIGHT_TWOSIDE))
               continue;
@@ -434,8 +457,7 @@ static void TAG(light_rgba)( GLcontext *ctx,
         }
          else {
            if (IDX & LIGHT_TWOSIDE) {
-              ACC_SCALE_SCALAR_3V(Bcolor[j], attenuation, 
-                                  light->_MatAmbient[1]);
+              ACC_SCALE_SCALAR_3V( sum[1], attenuation, light->_MatAmbient[1]);
            }
            side = 0;
            correction = 1;
@@ -478,9 +500,16 @@ static void TAG(light_rgba)( GLcontext *ctx,
                                    light->_MatSpecular[side]);
            }
         }
-        
-        
-        ACC_SCALE_SCALAR_3V( color[side][j], attenuation, contrib );
+
+        ACC_SCALE_SCALAR_3V( sum[side], attenuation, contrib );
+      }
+
+      UNCLAMPED_FLOAT_TO_RGB_CHAN( Fcolor[j], sum[0] );
+      Fcolor[j][3] = sumA[0];
+
+      if (IDX & LIGHT_TWOSIDE) {
+        UNCLAMPED_FLOAT_TO_RGB_CHAN( Bcolor[j], sum[1] );
+        Bcolor[j][3] = sumA[1];
       }
    }
 }
@@ -497,29 +526,34 @@ static void TAG(light_fast_rgba_single)( GLcontext *ctx,
 
 {
    struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
-   GLuint  nstride = VB->NormalPtr->stride;
+   const GLuint nstride = VB->NormalPtr->stride;
    const GLfloat *normal = (GLfloat *)VB->NormalPtr->data;
    GLfloat *CMcolor;
    GLuint CMstride;
-   GLfloat (*Fcolor)[4] = (GLfloat (*)[4]) store->LitColor[0].Ptr;
-   GLfloat (*Bcolor)[4] = (GLfloat (*)[4]) store->LitColor[1].Ptr;
-   struct gl_light *light = ctx->Light.EnabledList.next;
-   GLuint *flags = VB->Flag;
+   GLchan (*Fcolor)[4] = (GLchan (*)[4]) store->LitColor[0].Ptr;
+   GLchan (*Bcolor)[4] = (GLchan (*)[4]) store->LitColor[1].Ptr;
+   const struct gl_light *light = ctx->Light.EnabledList.next;
+   const GLuint *flags = VB->Flag;
+   GLchan basechan[2][4];
    GLuint j = 0;
    struct gl_material (*new_material)[2] = VB->Material;
-   GLuint *new_material_mask = VB->MaterialMask;
-   GLfloat base[2][4];
-   GLuint nr = VB->Count;
+   const GLuint *new_material_mask = VB->MaterialMask;
+   GLfloat base[2][3];
+   const GLuint nr = VB->Count;
+
+#ifdef TRACE
+   fprintf(stderr, "%s\n", __FUNCTION__ );
+#endif
 
-/*     fprintf(stderr, "%s\n", __FUNCTION__ );  */
    (void) input;               /* doesn't refer to Eye or Obj */
    (void) flags;
    (void) nr;
    (void) nstride;
 
    if (IDX & LIGHT_COLORMATERIAL) {
-      if (VB->ColorPtr[0]->Type != GL_FLOAT)
-        VB->import_data( ctx, VERT_RGBA, ~0 );
+      if (VB->ColorPtr[0]->Type != GL_FLOAT || 
+         VB->ColorPtr[0]->Size != 4)
+        import_color_material( ctx, stage );
 
       CMcolor = (GLfloat *)VB->ColorPtr[0]->Ptr;
       CMstride = VB->ColorPtr[0]->StrideB;
@@ -534,51 +568,66 @@ static void TAG(light_fast_rgba_single)( GLcontext *ctx,
 
    do {
       
-      if ( CHECK_COLOR_MATERIAL(j) )
+      if ( CHECK_COLOR_MATERIAL(j) ) {
         _mesa_update_color_material( ctx, CMcolor );
+      }
 
       if ( CHECK_MATERIAL(j) )
         _mesa_update_material( ctx, new_material[j], new_material_mask[j] );
 
       if ( CHECK_VALIDATE(j) )
-        _mesa_validate_all_lighting_tables( ctx );
+        TNL_CONTEXT(ctx)->Driver.NotifyMaterialChange( ctx );
 
 
+      /* No attenuation, so incoporate _MatAmbient into base color.
+       */
       COPY_3V(base[0], light->_MatAmbient[0]);
       ACC_3V(base[0], ctx->Light._BaseColor[0] );
-      base[0][3] = ctx->Light._BaseAlpha[0];
+      UNCLAMPED_FLOAT_TO_RGB_CHAN( basechan[0], base[0] );
+      UNCLAMPED_FLOAT_TO_CHAN(basechan[0][3], 
+                             ctx->Light.Material[0].Diffuse[3]);
 
       if (IDX & LIGHT_TWOSIDE) {
         COPY_3V(base[1], light->_MatAmbient[1]);
         ACC_3V(base[1], ctx->Light._BaseColor[1]);
-        base[1][3] = ctx->Light._BaseAlpha[1];
+        UNCLAMPED_FLOAT_TO_RGB_CHAN( basechan[1], base[1]);
+        UNCLAMPED_FLOAT_TO_CHAN(basechan[1][3], 
+                                ctx->Light.Material[1].Diffuse[3]);
       }
 
       do {
-        GLfloat n_dot_VP;
+        GLfloat n_dot_VP = DOT3(normal, light->_VP_inf_norm);
 
-        COPY_4FV(Fcolor[j], base[0]);
-        if (IDX & LIGHT_TWOSIDE) COPY_4FV(Bcolor[j], base[1]);
-
-        n_dot_VP = DOT3(normal, light->_VP_inf_norm);
-
-        if (n_dot_VP > 0.0F) {
+        if (n_dot_VP < 0.0F) {
+           if (IDX & LIGHT_TWOSIDE) {
+              GLfloat n_dot_h = -DOT3(normal, light->_h_inf_norm);
+              GLfloat sum[3];
+              COPY_3V(sum, base[1]);
+              ACC_SCALE_SCALAR_3V(sum, -n_dot_VP, light->_MatDiffuse[1]);
+              if (n_dot_h > 0.0F) {
+                 GLfloat spec;
+                 GET_SHINE_TAB_ENTRY( ctx->_ShineTable[1], n_dot_h, spec );
+                 ACC_SCALE_SCALAR_3V(sum, spec, light->_MatSpecular[1]);
+              }
+              UNCLAMPED_FLOAT_TO_RGB_CHAN(Bcolor[j], sum );
+              Bcolor[j][3] = basechan[1][3];
+           }
+           COPY_CHAN4(Fcolor[j], basechan[0]);
+        }
+         else {
            GLfloat n_dot_h = DOT3(normal, light->_h_inf_norm);
-           ACC_SCALE_SCALAR_3V(Fcolor[j], n_dot_VP, light->_MatDiffuse[0]);
+           GLfloat sum[3];
+           COPY_3V(sum, base[0]);
+           ACC_SCALE_SCALAR_3V(sum, n_dot_VP, light->_MatDiffuse[0]);
            if (n_dot_h > 0.0F) {
               GLfloat spec;
               GET_SHINE_TAB_ENTRY( ctx->_ShineTable[0], n_dot_h, spec );
-              ACC_SCALE_SCALAR_3V( Fcolor[j], spec, light->_MatSpecular[0]);
-           }
-        } 
-        else if (IDX & LIGHT_TWOSIDE) {
-           GLfloat n_dot_h = -DOT3(normal, light->_h_inf_norm);
-           ACC_SCALE_SCALAR_3V(Bcolor[j], -n_dot_VP, light->_MatDiffuse[1]);
-           if (n_dot_h > 0.0F) {
-              GLfloat spec;
-              GET_SHINE_TAB_ENTRY( ctx->_ShineTable[1], n_dot_h, spec );
-              ACC_SCALE_SCALAR_3V( Bcolor[j], spec, light->_MatSpecular[1]);
+              ACC_SCALE_SCALAR_3V(sum, spec, light->_MatSpecular[0]);
+
            }
+           UNCLAMPED_FLOAT_TO_RGB_CHAN(Fcolor[j], sum );
+           Fcolor[j][3] = basechan[0][3];
+           if (IDX & LIGHT_TWOSIDE) COPY_CHAN4(Bcolor[j], basechan[1]);
         }
 
         j++;
@@ -589,9 +638,9 @@ static void TAG(light_fast_rgba_single)( GLcontext *ctx,
 
       for ( ; REUSE_LIGHT_RESULTS(j) ; j++, CMSTRIDE, STRIDE_F(normal,NSTRIDE))
       {
-        COPY_4FV(Fcolor[j], Fcolor[j-1]);
+        COPY_CHAN4(Fcolor[j], Fcolor[j-1]);
         if (IDX & LIGHT_TWOSIDE)
-           COPY_4FV(Bcolor[j], Bcolor[j-1]);
+           COPY_CHAN4(Bcolor[j], Bcolor[j-1]);
       }
 
    } while (!CHECK_END_VB(j));
@@ -606,29 +655,36 @@ static void TAG(light_fast_rgba)( GLcontext *ctx,
                                  GLvector4f *input )
 {
    struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
-   const GLfloat *sumA = ctx->Light._BaseAlpha;
-   GLuint  nstride = VB->NormalPtr->stride;
+   GLchan sumA[2];
+   const GLuint nstride = VB->NormalPtr->stride;
    const GLfloat *normal = (GLfloat *)VB->NormalPtr->data;
    GLfloat *CMcolor;
    GLuint CMstride;
-   GLfloat (*Fcolor)[4] = (GLfloat (*)[4]) store->LitColor[0].Ptr;
-   GLfloat (*Bcolor)[4] = (GLfloat (*)[4]) store->LitColor[1].Ptr;
-   GLuint *flags = VB->Flag;
+   GLchan (*Fcolor)[4] = (GLchan (*)[4]) store->LitColor[0].Ptr;
+   GLchan (*Bcolor)[4] = (GLchan (*)[4]) store->LitColor[1].Ptr;
+   const GLuint *flags = VB->Flag;
    GLuint j = 0;
    struct gl_material (*new_material)[2] = VB->Material;
    GLuint *new_material_mask = VB->MaterialMask;
-   GLuint nr = VB->Count;
-   struct gl_light *light;
+   const GLuint nr = VB->Count;
+   const struct gl_light *light;
+
+#ifdef TRACE
+   fprintf(stderr, "%s\n", __FUNCTION__ );
+#endif
 
-/*     fprintf(stderr, "%s\n", __FUNCTION__ );  */
    (void) flags;
    (void) input;
    (void) nr;
    (void) nstride;
 
+   UNCLAMPED_FLOAT_TO_CHAN(sumA[0], ctx->Light.Material[0].Diffuse[3]);
+   UNCLAMPED_FLOAT_TO_CHAN(sumA[1], ctx->Light.Material[1].Diffuse[3]);
+
    if (IDX & LIGHT_COLORMATERIAL) {
-      if (VB->ColorPtr[0]->Type != GL_FLOAT)
-        VB->import_data( ctx, VERT_RGBA, ~0 );
+      if (VB->ColorPtr[0]->Type != GL_FLOAT || 
+         VB->ColorPtr[0]->Size != 4)
+        import_color_material( ctx, stage );
 
       CMcolor = (GLfloat *)VB->ColorPtr[0]->Ptr;
       CMstride = VB->ColorPtr[0]->StrideB;
@@ -643,55 +699,66 @@ static void TAG(light_fast_rgba)( GLcontext *ctx,
 
    do {
       do {
+        GLfloat sum[2][3];
+
         if ( CHECK_COLOR_MATERIAL(j) )
            _mesa_update_color_material( ctx, CMcolor );
 
         if ( CHECK_MATERIAL(j) )
            _mesa_update_material( ctx, new_material[j], new_material_mask[j] );
 
-        if ( CHECK_VALIDATE(j) )
-           _mesa_validate_all_lighting_tables( ctx );
-
+        if ( CHECK_VALIDATE(j) ) {
+           TNL_CONTEXT(ctx)->Driver.NotifyMaterialChange( ctx );
+           UNCLAMPED_FLOAT_TO_CHAN(sumA[0], ctx->Light.Material[0].Diffuse[3]);
+           if (IDX & LIGHT_TWOSIDE)
+              UNCLAMPED_FLOAT_TO_CHAN(sumA[1], 
+                                      ctx->Light.Material[1].Diffuse[3]);
+        }
 
-        COPY_3V(Fcolor[j], ctx->Light._BaseColor[0]);
-        Fcolor[j][3] = sumA[0];
 
-        if (IDX & LIGHT_TWOSIDE) {
-           COPY_3V(Bcolor[j], ctx->Light._BaseColor[1]);
-           Bcolor[j][3] = sumA[1];
-        }
+        COPY_3V(sum[0], ctx->Light._BaseColor[0]);
+        if (IDX & LIGHT_TWOSIDE)
+           COPY_3V(sum[1], ctx->Light._BaseColor[1]);
 
         foreach (light, &ctx->Light.EnabledList) {
            GLfloat n_dot_h, n_dot_VP, spec;
 
-           ACC_3V(Fcolor[j], light->_MatAmbient[0]);
+           ACC_3V(sum[0], light->_MatAmbient[0]);
            if (IDX & LIGHT_TWOSIDE)
-              ACC_3V(Bcolor[j], light->_MatAmbient[1]);
+              ACC_3V(sum[1], light->_MatAmbient[1]);
 
            n_dot_VP = DOT3(normal, light->_VP_inf_norm);
 
            if (n_dot_VP > 0.0F) {
-              ACC_SCALE_SCALAR_3V(Fcolor[j], n_dot_VP, light->_MatDiffuse[0]);
+              ACC_SCALE_SCALAR_3V(sum[0], n_dot_VP, light->_MatDiffuse[0]);
               n_dot_h = DOT3(normal, light->_h_inf_norm);
               if (n_dot_h > 0.0F) {
                  struct gl_shine_tab *tab = ctx->_ShineTable[0];
                  GET_SHINE_TAB_ENTRY( tab, n_dot_h, spec );
-                 ACC_SCALE_SCALAR_3V( Fcolor[j], spec,
+                 ACC_SCALE_SCALAR_3V( sum[0], spec,
                                       light->_MatSpecular[0]);
               }
            }
            else if (IDX & LIGHT_TWOSIDE) {
-              ACC_SCALE_SCALAR_3V(Bcolor[j], -n_dot_VP, light->_MatDiffuse[1]);
+              ACC_SCALE_SCALAR_3V(sum[1], -n_dot_VP, light->_MatDiffuse[1]);
               n_dot_h = -DOT3(normal, light->_h_inf_norm);
               if (n_dot_h > 0.0F) {
                  struct gl_shine_tab *tab = ctx->_ShineTable[1];
                  GET_SHINE_TAB_ENTRY( tab, n_dot_h, spec );
-                 ACC_SCALE_SCALAR_3V( Bcolor[j], spec,
+                 ACC_SCALE_SCALAR_3V( sum[1], spec,
                                       light->_MatSpecular[1]);
               }
            }
         }
 
+        UNCLAMPED_FLOAT_TO_RGB_CHAN( Fcolor[j], sum[0] );
+        Fcolor[j][3] = sumA[0];
+
+        if (IDX & LIGHT_TWOSIDE) {
+           UNCLAMPED_FLOAT_TO_RGB_CHAN( Bcolor[j], sum[1] );
+           Bcolor[j][3] = sumA[1];
+        }
+
         j++;
         CMSTRIDE;
         STRIDE_F(normal, NSTRIDE);
@@ -702,9 +769,9 @@ static void TAG(light_fast_rgba)( GLcontext *ctx,
        */
       for ( ; REUSE_LIGHT_RESULTS(j) ; j++, CMSTRIDE, STRIDE_F(normal, NSTRIDE))
       {
-        COPY_4FV(Fcolor[j], Fcolor[j-1]);
+        COPY_CHAN4(Fcolor[j], Fcolor[j-1]);
         if (IDX & LIGHT_TWOSIDE)
-           COPY_4FV(Bcolor[j], Bcolor[j-1]);
+           COPY_CHAN4(Bcolor[j], Bcolor[j-1]);
       }
 
    } while (!CHECK_END_VB(j));
@@ -730,19 +797,22 @@ static void TAG(light_ci)( GLcontext *ctx,
 {
    struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
    GLuint j;
-   GLuint vstride = input->stride;
+   const GLuint vstride = input->stride;
    const GLfloat *vertex = (GLfloat *) input->data;
-   GLuint nstride = VB->NormalPtr->stride;
+   const GLuint nstride = VB->NormalPtr->stride;
    const GLfloat *normal = (GLfloat *)VB->NormalPtr->data;
    GLfloat *CMcolor;
    GLuint CMstride;
-   GLuint *flags = VB->Flag;
+   const GLuint *flags = VB->Flag;
    GLuint *indexResult[2];
    struct gl_material (*new_material)[2] = VB->Material;
    GLuint *new_material_mask = VB->MaterialMask;
-   GLuint nr = VB->Count;
+   const GLuint nr = VB->Count;
+
+#ifdef TRACE
+   fprintf(stderr, "%s\n", __FUNCTION__ );
+#endif
 
-/*     fprintf(stderr, "%s\n", __FUNCTION__ );  */
    (void) flags;
    (void) nstride;
    (void) vstride;
@@ -759,8 +829,9 @@ static void TAG(light_ci)( GLcontext *ctx,
       indexResult[1] = VB->IndexPtr[1]->data;
 
    if (IDX & LIGHT_COLORMATERIAL) {
-      if (VB->ColorPtr[0]->Type != GL_FLOAT)
-        VB->import_data( ctx, VERT_RGBA, ~0 );
+      if (VB->ColorPtr[0]->Type != GL_FLOAT || 
+         VB->ColorPtr[0]->Size != 4)
+        import_color_material( ctx, stage );
 
       CMcolor = (GLfloat *)VB->ColorPtr[0]->Ptr;
       CMstride = VB->ColorPtr[0]->StrideB;
@@ -782,7 +853,7 @@ static void TAG(light_ci)( GLcontext *ctx,
         _mesa_update_material( ctx, new_material[j], new_material_mask[j] );
 
       if ( CHECK_VALIDATE(j) )
-        _mesa_validate_all_lighting_tables( ctx );
+        TNL_CONTEXT(ctx)->Driver.NotifyMaterialChange( ctx );
 
       diffuse[0] = specular[0] = 0.0F;
 
@@ -808,7 +879,7 @@ static void TAG(light_ci)( GLcontext *ctx,
 
            SUB_3V(VP, light->_Position, vertex);
 
-           d = LEN_3FV( VP );
+           d = (GLfloat) LEN_3FV( VP );
            if ( d > 1e-6) {
               GLfloat invd = 1.0F / d;
               SELF_SCALE_SCALAR_3V(VP, invd);
@@ -825,9 +896,9 @@ static void TAG(light_ci)( GLcontext *ctx,
                  continue; /* this light makes no contribution */
               }
               else {
-                 double x = PV_dot_dir * (EXP_TABLE_SIZE-1);
-                 int k = (int) x;
-                 GLfloat spot = (light->_SpotExpTable[k][0]
+                 GLdouble x = PV_dot_dir * (EXP_TABLE_SIZE-1);
+                 GLint k = (GLint) x;
+                 GLfloat spot = (GLfloat) (light->_SpotExpTable[k][0]
                                  + (x-k)*light->_SpotExpTable[k][1]);
                  attenuation *= spot;
               }