fragment program execution
[mesa.git] / src / mesa / tnl / t_vb_lighttmp.h
index ff3dead13ef7ffa42cae867980039daeca826721..90d7652063df96e87fd2a065b819dcdacb498797 100644 (file)
@@ -1,8 +1,8 @@
-/* $Id: t_vb_lighttmp.h,v 1.14 2001/07/28 19:28:49 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,
@@ -89,12 +97,11 @@ static void TAG(light_rgba_spec)( GLcontext *ctx,
    struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
    GLfloat (*base)[3] = ctx->Light._BaseColor;
    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;
@@ -104,26 +111,23 @@ static void TAG(light_rgba_spec)( GLcontext *ctx,
    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;
-   GLchan (*spec[2])[4];
 
-   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)
+      if (VB->ColorPtr[0]->Type != GL_FLOAT || 
+         VB->ColorPtr[0]->Size != 4)
         import_color_material( ctx, stage );
 
       CMcolor = (GLfloat *) VB->ColorPtr[0]->Ptr;
@@ -159,7 +163,7 @@ static void TAG(light_rgba_spec)( 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 );
         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]);
@@ -214,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 */
 
@@ -310,9 +313,9 @@ static void TAG(light_rgba)( GLcontext *ctx,
    GLfloat (*base)[3] = ctx->Light._BaseColor;
    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;
@@ -321,13 +324,16 @@ static void TAG(light_rgba)( GLcontext *ctx,
    GLchan (*Fcolor)[4] = (GLchan (*)[4]) store->LitColor[0].Ptr;
    GLchan (*Bcolor)[4] = (GLchan (*)[4]) store->LitColor[1].Ptr;
    GLchan (*color[2])[4];
-   GLuint *flags = VB->Flag;
+   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;
@@ -336,7 +342,8 @@ static void TAG(light_rgba)( GLcontext *ctx,
    color[1] = Bcolor;
 
    if (IDX & LIGHT_COLORMATERIAL) {
-      if (VB->ColorPtr[0]->Type != GL_FLOAT)
+      if (VB->ColorPtr[0]->Type != GL_FLOAT || 
+         VB->ColorPtr[0]->Size != 4)
         import_color_material( ctx, stage );
 
       CMcolor = (GLfloat *)VB->ColorPtr[0]->Ptr;
@@ -368,7 +375,7 @@ static void TAG(light_rgba)( 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 );
         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]);
@@ -403,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;
@@ -422,20 +429,18 @@ 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 );
 
@@ -521,29 +526,33 @@ 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;
    GLchan (*Fcolor)[4] = (GLchan (*)[4]) store->LitColor[0].Ptr;
    GLchan (*Bcolor)[4] = (GLchan (*)[4]) store->LitColor[1].Ptr;
-   struct gl_light *light = ctx->Light.EnabledList.next;
-   GLuint *flags = VB->Flag;
+   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;
+   const GLuint *new_material_mask = VB->MaterialMask;
    GLfloat base[2][3];
-   GLuint nr = VB->Count;
+   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)
+      if (VB->ColorPtr[0]->Type != GL_FLOAT || 
+         VB->ColorPtr[0]->Size != 4)
         import_color_material( ctx, stage );
 
       CMcolor = (GLfloat *)VB->ColorPtr[0]->Ptr;
@@ -559,14 +568,15 @@ 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.
@@ -588,7 +598,6 @@ static void TAG(light_fast_rgba_single)( GLcontext *ctx,
       do {
         GLfloat n_dot_VP = DOT3(normal, light->_VP_inf_norm);
 
-
         if (n_dot_VP < 0.0F) {
            if (IDX & LIGHT_TWOSIDE) {
               GLfloat n_dot_h = -DOT3(normal, light->_h_inf_norm);
@@ -601,9 +610,11 @@ static void TAG(light_fast_rgba_single)( GLcontext *ctx,
                  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 {
+        }
+         else {
            GLfloat n_dot_h = DOT3(normal, light->_h_inf_norm);
            GLfloat sum[3];
            COPY_3V(sum, base[0]);
@@ -615,6 +626,7 @@ static void TAG(light_fast_rgba_single)( GLcontext *ctx,
 
            }
            UNCLAMPED_FLOAT_TO_RGB_CHAN(Fcolor[j], sum );
+           Fcolor[j][3] = basechan[0][3];
            if (IDX & LIGHT_TWOSIDE) COPY_CHAN4(Bcolor[j], basechan[1]);
         }
 
@@ -644,20 +656,23 @@ static void TAG(light_fast_rgba)( GLcontext *ctx,
 {
    struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
    GLchan sumA[2];
-   GLuint  nstride = VB->NormalPtr->stride;
+   const GLuint nstride = VB->NormalPtr->stride;
    const GLfloat *normal = (GLfloat *)VB->NormalPtr->data;
    GLfloat *CMcolor;
    GLuint CMstride;
    GLchan (*Fcolor)[4] = (GLchan (*)[4]) store->LitColor[0].Ptr;
    GLchan (*Bcolor)[4] = (GLchan (*)[4]) store->LitColor[1].Ptr;
-   GLuint *flags = VB->Flag;
+   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;
@@ -667,7 +682,8 @@ static void TAG(light_fast_rgba)( GLcontext *ctx,
    UNCLAMPED_FLOAT_TO_CHAN(sumA[1], ctx->Light.Material[1].Diffuse[3]);
 
    if (IDX & LIGHT_COLORMATERIAL) {
-      if (VB->ColorPtr[0]->Type != GL_FLOAT)
+      if (VB->ColorPtr[0]->Type != GL_FLOAT || 
+         VB->ColorPtr[0]->Size != 4)
         import_color_material( ctx, stage );
 
       CMcolor = (GLfloat *)VB->ColorPtr[0]->Ptr;
@@ -692,13 +708,14 @@ static void TAG(light_fast_rgba)( 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 );
            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], ctx->Light._BaseColor[0]);
         if (IDX & LIGHT_TWOSIDE)
            COPY_3V(sum[1], ctx->Light._BaseColor[1]);
@@ -780,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;
@@ -809,7 +829,8 @@ static void TAG(light_ci)( GLcontext *ctx,
       indexResult[1] = VB->IndexPtr[1]->data;
 
    if (IDX & LIGHT_COLORMATERIAL) {
-      if (VB->ColorPtr[0]->Type != GL_FLOAT)
+      if (VB->ColorPtr[0]->Type != GL_FLOAT || 
+         VB->ColorPtr[0]->Size != 4)
         import_color_material( ctx, stage );
 
       CMcolor = (GLfloat *)VB->ColorPtr[0]->Ptr;
@@ -832,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;
 
@@ -858,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);
@@ -875,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;
               }