Move compiler.h and imports.h/c from src/mesa/main into src/util
[mesa.git] / src / mesa / tnl / t_vb_light.c
index 8a0fe63fd8b75ab60feaa3042c883ae093aa79b8..cd0a5544c9399d3796a5ff15217766ce570a3ca5 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.5
  *
  * Copyright (C) 1999-2006  Brian Paul   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 "main/glheader.h"
-#include "main/colormac.h"
 #include "main/light.h"
 #include "main/macros.h"
-#include "main/imports.h"
-#include "main/simple_list.h"
+#include "util/imports.h"
+#include "util/simple_list.h"
 #include "main/mtypes.h"
 
 #include "math/m_translate.h"
 
+#include "util/bitscan.h"
+
 #include "t_context.h"
 #include "t_pipeline.h"
+#include "tnl.h"
 
 #define LIGHT_TWOSIDE       0x1
 #define LIGHT_MATERIAL      0x2
 #define MAX_LIGHT_FUNC      0x4
 
-typedef void (*light_func)( GLcontext *ctx,
+typedef void (*light_func)( struct gl_context *ctx,
                            struct vertex_buffer *VB,
                            struct tnl_pipeline_stage *stage,
                            GLvector4f *input );
@@ -64,7 +66,6 @@ struct light_stage_data {
    GLvector4f Input;
    GLvector4f LitColor[2];
    GLvector4f LitSecondary[2];
-   GLvector4f LitIndex[2];
    light_func *light_func_tab;
 
    struct material_cursor mat[MAT_ATTRIB_MAX];
@@ -77,6 +78,114 @@ struct light_stage_data {
 
 
 
+/**********************************************************************/
+/*****                  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
+ */
+
+
+
+static void
+validate_shine_table( struct gl_context *ctx, GLuint side, GLfloat shininess )
+{
+   TNLcontext *tnl = TNL_CONTEXT(ctx);
+   struct tnl_shine_tab *list = tnl->_ShineTabList;
+   struct tnl_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.0F;
+      if (shininess == 0.0F) {
+        for (j = 1 ; j <= SHINE_TABLE_SIZE ; j++)
+           m[j] = 1.0F;
+      }
+      else {
+        for (j = 1 ; j < SHINE_TABLE_SIZE ; j++) {
+            GLfloat t, x = j / (GLfloat) (SHINE_TABLE_SIZE - 1);
+            if (x < 0.005F) /* underflow check */
+               x = 0.005F;
+            t = powf(x, shininess);
+           if (t > 1e-20F)
+              m[j] = t;
+           else
+              m[j] = 0.0F;
+        }
+        m[SHINE_TABLE_SIZE] = 1.0F;
+      }
+
+      s->shininess = shininess;
+   }
+
+   if (tnl->_ShineTable[side])
+      tnl->_ShineTable[side]->refcount--;
+
+   tnl->_ShineTable[side] = s;
+   move_to_tail( list, s );
+   s->refcount++;
+}
+
+
+void
+_tnl_validate_shine_tables( struct gl_context *ctx )
+{
+   TNLcontext *tnl = TNL_CONTEXT(ctx);
+   GLfloat shininess;
+   
+   shininess = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
+   if (!tnl->_ShineTable[0] || tnl->_ShineTable[0]->shininess != shininess)
+      validate_shine_table( ctx, 0, shininess );
+
+   shininess = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0];
+   if (!tnl->_ShineTable[1] || tnl->_ShineTable[1]->shininess != shininess)
+      validate_shine_table( ctx, 1, shininess );
+}
+
+
 /**
  * In the case of colormaterial, the effected material attributes
  * should already have been bound to point to the incoming color data,
@@ -86,7 +195,7 @@ struct light_stage_data {
  * It's called per-vertex in the lighting loop.
  */
 static void
-update_materials(GLcontext *ctx, struct light_stage_data *store)
+update_materials(struct gl_context *ctx, struct light_stage_data *store)
 {
    GLuint i;
 
@@ -102,7 +211,7 @@ update_materials(GLcontext *ctx, struct light_stage_data *store)
    /* XXX we should only call this if we're tracking/changing the specular
     * exponent.
     */
-   _mesa_validate_all_lighting_tables( ctx );
+   _tnl_validate_shine_tables( ctx );
 }
 
 
@@ -111,7 +220,7 @@ update_materials(GLcontext *ctx, struct light_stage_data *store)
  * Return number of material attributes which will track vertex color.
  */
 static GLuint
-prepare_materials(GLcontext *ctx,
+prepare_materials(struct gl_context *ctx,
                   struct vertex_buffer *VB, struct light_stage_data *store)
 {
    GLuint i;
@@ -119,15 +228,17 @@ prepare_materials(GLcontext *ctx,
    store->mat_count = 0;
    store->mat_bitmask = 0;
 
-   /* Examine the ColorMaterialBitmask to determine which materials
+   /* Examine the _ColorMaterialBitmask to determine which materials
     * track vertex color.  Override the material attribute's pointer
     * with the color pointer for each one.
     */
    if (ctx->Light.ColorMaterialEnabled) {
-      const GLuint bitmask = ctx->Light.ColorMaterialBitmask;
-      for (i = 0 ; i < MAT_ATTRIB_MAX ; i++)
-        if (bitmask & (1<<i))
-           VB->AttribPtr[_TNL_ATTRIB_MAT_FRONT_AMBIENT + i] = VB->AttribPtr[_TNL_ATTRIB_COLOR0];
+      GLbitfield bitmask = ctx->Light._ColorMaterialBitmask;
+      while (bitmask) {
+         const int i = u_bit_scan(&bitmask);
+         VB->AttribPtr[_TNL_ATTRIB_MAT_FRONT_AMBIENT + i] =
+            VB->AttribPtr[_TNL_ATTRIB_COLOR0];
+      }
    }
 
    /* Now, for each material attribute that's tracking vertex color, save
@@ -150,18 +261,38 @@ prepare_materials(GLcontext *ctx,
    /* FIXME: Is this already done?
     */
    _mesa_update_material( ctx, ~0 );
-   _mesa_validate_all_lighting_tables( ctx );
+
+   _tnl_validate_shine_tables( ctx );
 
    return store->mat_count;
 }
 
+/*
+ * Compute dp ^ SpecularExponent.
+ * Lerp between adjacent values in the f(x) lookup table, giving a
+ * continuous function, with adequate overall accuracy.  (Though still
+ * pretty good compared to a straight lookup).
+ */
+static inline GLfloat
+lookup_shininess(const struct gl_context *ctx, GLuint face, GLfloat dp)
+{
+   TNLcontext *tnl = TNL_CONTEXT(ctx);
+   const struct tnl_shine_tab *tab = tnl->_ShineTable[face];
+   float f = dp * (SHINE_TABLE_SIZE - 1);
+   int k = (int) f;
+   if (k < 0 /* gcc may cast an overflow float value to negative int value */
+       || k > SHINE_TABLE_SIZE - 2)
+      return powf(dp, tab->shininess);
+   else
+      return tab->tab[k] + (f - k) * (tab->tab[k+1] - tab->tab[k]);
+}
+
 /* Tables for all the shading functions.
  */
 static light_func _tnl_light_tab[MAX_LIGHT_FUNC];
 static light_func _tnl_light_fast_tab[MAX_LIGHT_FUNC];
 static light_func _tnl_light_fast_single_tab[MAX_LIGHT_FUNC];
 static light_func _tnl_light_spec_tab[MAX_LIGHT_FUNC];
-static light_func _tnl_light_ci_tab[MAX_LIGHT_FUNC];
 
 #define TAG(x)           x
 #define IDX              (0)
@@ -194,7 +325,7 @@ static void init_lighting_tables( void )
 }
 
 
-static GLboolean run_lighting( GLcontext *ctx, 
+static GLboolean run_lighting( struct gl_context *ctx, 
                               struct tnl_pipeline_stage *stage )
 {
    struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
@@ -252,7 +383,7 @@ static GLboolean run_lighting( GLcontext *ctx,
 
 /* Called in place of do_lighting when the light table may have changed.
  */
-static void validate_lighting( GLcontext *ctx,
+static void validate_lighting( struct gl_context *ctx,
                                        struct tnl_pipeline_stage *stage )
 {
    light_func *tab;
@@ -260,22 +391,19 @@ static void validate_lighting( GLcontext *ctx,
    if (!ctx->Light.Enabled || ctx->VertexProgram._Current)
       return;
 
-   if (ctx->Visual.rgbMode) {
-      if (ctx->Light._NeedVertices) {
-        if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
-           tab = _tnl_light_spec_tab;
-        else
-           tab = _tnl_light_tab;
-      }
-      else {
-        if (ctx->Light.EnabledList.next == ctx->Light.EnabledList.prev)
-           tab = _tnl_light_fast_single_tab;
-        else
-           tab = _tnl_light_fast_tab;
-      }
+   if (ctx->Light._NeedVertices) {
+      if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
+        tab = _tnl_light_spec_tab;
+      else
+        tab = _tnl_light_tab;
+   }
+   else {
+      /* Power of two means only a single active light. */
+      if (_mesa_is_pow_two(ctx->Light._EnabledLights))
+        tab = _tnl_light_fast_single_tab;
+      else
+        tab = _tnl_light_fast_tab;
    }
-   else
-      tab = _tnl_light_ci_tab;
 
 
    LIGHT_STAGE_DATA(stage)->light_func_tab = tab;
@@ -290,14 +418,14 @@ static void validate_lighting( GLcontext *ctx,
 /* Called the first time stage->run is called.  In effect, don't
  * allocate data until the first time the stage is run.
  */
-static GLboolean init_lighting( GLcontext *ctx,
+static GLboolean init_lighting( struct gl_context *ctx,
                                struct tnl_pipeline_stage *stage )
 {
    TNLcontext *tnl = TNL_CONTEXT(ctx);
    struct light_stage_data *store;
    GLuint size = tnl->vb.Size;
 
-   stage->privatePtr = MALLOC(sizeof(*store));
+   stage->privatePtr = malloc(sizeof(*store));
    store = LIGHT_STAGE_DATA(stage);
    if (!store)
       return GL_FALSE;
@@ -311,19 +439,12 @@ static GLboolean init_lighting( GLcontext *ctx,
    _mesa_vector4f_alloc( &store->LitColor[1], 0, size, 32 );
    _mesa_vector4f_alloc( &store->LitSecondary[0], 0, size, 32 );
    _mesa_vector4f_alloc( &store->LitSecondary[1], 0, size, 32 );
-   _mesa_vector4f_alloc( &store->LitIndex[0], 0, size, 32 );
-   _mesa_vector4f_alloc( &store->LitIndex[1], 0, size, 32 );
 
    store->LitColor[0].size = 4;
    store->LitColor[1].size = 4;
    store->LitSecondary[0].size = 3;
    store->LitSecondary[1].size = 3;
 
-   store->LitIndex[0].size = 1;
-   store->LitIndex[0].stride = sizeof(GLfloat);
-   store->LitIndex[1].size = 1;
-   store->LitIndex[1].stride = sizeof(GLfloat);
-
    return GL_TRUE;
 }
 
@@ -340,9 +461,7 @@ static void dtr( struct tnl_pipeline_stage *stage )
       _mesa_vector4f_free( &store->LitColor[1] );
       _mesa_vector4f_free( &store->LitSecondary[0] );
       _mesa_vector4f_free( &store->LitSecondary[1] );
-      _mesa_vector4f_free( &store->LitIndex[0] );
-      _mesa_vector4f_free( &store->LitIndex[1] );
-      FREE( store );
+      free( store );
       stage->privatePtr = NULL;
    }
 }