nir: use enum operator helper for nir_variable_mode and nir_metadata
[mesa.git] / src / mesa / tnl / t_vb_texgen.c
index a2912cf7af34c8c169b9b8082ebf95664b135714..aa8d10b4de4450aeac62d53678c9475e2dc381bc 100644 (file)
@@ -1,8 +1,7 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.1
  *
- * Copyright (C) 1999-2004  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * 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.
  *
  * Authors:
- *    Brian Paul
- *    Keith Whitwell <keith@tungstengraphics.com>
+ *    Brian Paul Keith Whitwell <keithw@vmware.com>
  */
 
 /*
  * including any use thereof or modifications thereto.
  */
 
-#include "glheader.h"
-#include "colormac.h"
-#include "context.h"
-#include "macros.h"
-#include "imports.h"
-#include "mtypes.h"
+#include "main/errors.h"
+#include "main/glheader.h"
+#include "main/macros.h"
+
+#include "main/mtypes.h"
 
 #include "math/m_xform.h"
 
@@ -55,7 +53,7 @@
 
 struct texgen_stage_data;
 
-typedef void (*texgen_func)( GLcontext *ctx,
+typedef void (*texgen_func)( struct gl_context *ctx,
                             struct texgen_stage_data *store,
                             GLuint unit);
 
@@ -118,7 +116,7 @@ static void build_m3( GLfloat f[][3], GLfloat m[],
       fz = f[i][2] = u[2] - norm[2] * two_nu;
       m[i] = fx * fx + fy * fy + (fz + 1.0F) * (fz + 1.0F);
       if (m[i] != 0.0F) {
-        m[i] = 0.5F * _mesa_inv_sqrtf(m[i]);
+        m[i] = 0.5F * (1.0f / sqrtf(m[i]));
       }
    }
 }
@@ -147,7 +145,7 @@ static void build_m2( GLfloat f[][3], GLfloat m[],
       fz = f[i][2] = u[2] - norm[2] * two_nu;
       m[i] = fx * fx + fy * fy + (fz + 1.0F) * (fz + 1.0F);
       if (m[i] != 0.0F) {
-        m[i] = 0.5F * _mesa_inv_sqrtf(m[i]);
+        m[i] = 0.5F * (1.0f / sqrtf(m[i]));
       }
    }
 }
@@ -161,8 +159,8 @@ typedef void (*build_m_func)( GLfloat f[][3],
 
 
 static build_m_func build_m_tab[5] = {
-   0,
-   0,
+   NULL,
+   NULL,
    build_m2,
    build_m3,
    build_m3
@@ -238,8 +236,8 @@ typedef void (*build_f_func)( GLfloat *f,
 /* Just treat 4-vectors as 3-vectors.
  */
 static build_f_func build_f_tab[5] = {
-   0,
-   0,
+   NULL,
+   NULL,
    build_f2,
    build_f3,
    build_f3
@@ -249,36 +247,36 @@ static build_f_func build_f_tab[5] = {
 
 /* Special case texgen functions.
  */
-static void texgen_reflection_map_nv( GLcontext *ctx,
+static void texgen_reflection_map_nv( struct gl_context *ctx,
                                      struct texgen_stage_data *store,
                                      GLuint unit )
 {
    struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
-   GLvector4f *in = VB->TexCoordPtr[unit];
+   GLvector4f *in = VB->AttribPtr[VERT_ATTRIB_TEX0 + unit];
    GLvector4f *out = &store->texcoord[unit];
 
    build_f_tab[VB->EyePtr->size]( out->start,
                                  out->stride,
-                                 VB->NormalPtr,
+                                 VB->AttribPtr[_TNL_ATTRIB_NORMAL],
                                  VB->EyePtr );
 
    out->flags |= (in->flags & VEC_SIZE_FLAGS) | VEC_SIZE_3;
    out->count = VB->Count;
    out->size = MAX2(in->size, 3);
-   if (in->size == 4) 
+   if (in->size == 4)
       _mesa_copy_tab[0x8]( out, in );
 }
 
 
 
-static void texgen_normal_map_nv( GLcontext *ctx,
+static void texgen_normal_map_nv( struct gl_context *ctx,
                                  struct texgen_stage_data *store,
                                  GLuint unit )
 {
    struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
-   GLvector4f *in = VB->TexCoordPtr[unit];
+   GLvector4f *in = VB->AttribPtr[VERT_ATTRIB_TEX0 + unit];
    GLvector4f *out = &store->texcoord[unit];
-   GLvector4f *normal = VB->NormalPtr;
+   GLvector4f *normal = VB->AttribPtr[_TNL_ATTRIB_NORMAL];
    GLfloat (*texcoord)[4] = (GLfloat (*)[4])out->start;
    GLuint count = VB->Count;
    GLuint i;
@@ -294,17 +292,17 @@ static void texgen_normal_map_nv( GLcontext *ctx,
    out->flags |= (in->flags & VEC_SIZE_FLAGS) | VEC_SIZE_3;
    out->count = count;
    out->size = MAX2(in->size, 3);
-   if (in->size == 4) 
+   if (in->size == 4)
       _mesa_copy_tab[0x8]( out, in );
 }
 
 
-static void texgen_sphere_map( GLcontext *ctx,
+static void texgen_sphere_map( struct gl_context *ctx,
                               struct texgen_stage_data *store,
                               GLuint unit )
 {
    struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
-   GLvector4f *in = VB->TexCoordPtr[unit];
+   GLvector4f *in = VB->AttribPtr[VERT_ATTRIB_TEX0 + unit];
    GLvector4f *out = &store->texcoord[unit];
    GLfloat (*texcoord)[4] = (GLfloat (*)[4]) out->start;
    GLuint count = VB->Count;
@@ -314,7 +312,7 @@ static void texgen_sphere_map( GLcontext *ctx,
 
    (build_m_tab[VB->EyePtr->size])( store->tmp_f,
                                    store->tmp_m,
-                                   VB->NormalPtr,
+                                   VB->AttribPtr[_TNL_ATTRIB_NORMAL],
                                    VB->EyePtr );
 
    out->size = MAX2(in->size,2);
@@ -332,18 +330,19 @@ static void texgen_sphere_map( GLcontext *ctx,
 
 
 
-static void texgen( GLcontext *ctx,
+static void texgen( struct gl_context *ctx,
                    struct texgen_stage_data *store,
                    GLuint unit )
 {
    TNLcontext *tnl = TNL_CONTEXT(ctx);
    struct vertex_buffer *VB = &tnl->vb;
-   GLvector4f *in = VB->TexCoordPtr[unit];
+   GLvector4f *in = VB->AttribPtr[VERT_ATTRIB_TEX0 + unit];
    GLvector4f *out = &store->texcoord[unit];
-   const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
-   const GLvector4f *obj = VB->ObjPtr;
+   const struct gl_fixedfunc_texture_unit *texUnit =
+      &ctx->Texture.FixedFuncUnit[unit];
+   const GLvector4f *obj = VB->AttribPtr[_TNL_ATTRIB_POS];
    const GLvector4f *eye = VB->EyePtr;
-   const GLvector4f *normal = VB->NormalPtr;
+   const GLvector4f *normal = VB->AttribPtr[_TNL_ATTRIB_NORMAL];
    const GLfloat *m = store->tmp_m;
    const GLuint count = VB->Count;
    GLfloat (*texcoord)[4] = (GLfloat (*)[4])out->data;
@@ -367,16 +366,16 @@ static void texgen( GLcontext *ctx,
 
    if (texUnit->TexGenEnabled & S_BIT) {
       GLuint i;
-      switch (texUnit->GenModeS) {
+      switch (texUnit->GenS.Mode) {
       case GL_OBJECT_LINEAR:
         _mesa_dotprod_tab[obj->size]( (GLfloat *)out->data,
                                       sizeof(out->data[0]), obj,
-                                      texUnit->ObjectPlaneS );
+                                      texUnit->GenS.ObjectPlane );
         break;
       case GL_EYE_LINEAR:
         _mesa_dotprod_tab[eye->size]( (GLfloat *)out->data,
                                       sizeof(out->data[0]), eye,
-                                      texUnit->EyePlaneS );
+                                      texUnit->GenS.EyePlane );
         break;
       case GL_SPHERE_MAP:
          for (i = 0; i < count; i++)
@@ -400,16 +399,16 @@ static void texgen( GLcontext *ctx,
 
    if (texUnit->TexGenEnabled & T_BIT) {
       GLuint i;
-      switch (texUnit->GenModeT) {
+      switch (texUnit->GenT.Mode) {
       case GL_OBJECT_LINEAR:
         _mesa_dotprod_tab[obj->size]( &(out->data[0][1]),
                                       sizeof(out->data[0]), obj,
-                                      texUnit->ObjectPlaneT );
+                                      texUnit->GenT.ObjectPlane );
         break;
       case GL_EYE_LINEAR:
         _mesa_dotprod_tab[eye->size]( &(out->data[0][1]),
                                       sizeof(out->data[0]), eye,
-                                      texUnit->EyePlaneT );
+                                      texUnit->GenT.EyePlane );
         break;
       case GL_SPHERE_MAP:
          for (i = 0; i < count; i++)
@@ -433,16 +432,16 @@ static void texgen( GLcontext *ctx,
 
    if (texUnit->TexGenEnabled & R_BIT) {
       GLuint i;
-      switch (texUnit->GenModeR) {
+      switch (texUnit->GenR.Mode) {
       case GL_OBJECT_LINEAR:
         _mesa_dotprod_tab[obj->size]( &(out->data[0][2]),
                                       sizeof(out->data[0]), obj,
-                                      texUnit->ObjectPlaneR );
+                                      texUnit->GenR.ObjectPlane );
         break;
       case GL_EYE_LINEAR:
         _mesa_dotprod_tab[eye->size]( &(out->data[0][2]),
                                       sizeof(out->data[0]), eye,
-                                      texUnit->EyePlaneR );
+                                      texUnit->GenR.EyePlane );
         break;
       case GL_REFLECTION_MAP_NV:
         for (i=0;i<count;i++)
@@ -461,16 +460,16 @@ static void texgen( GLcontext *ctx,
    }
 
    if (texUnit->TexGenEnabled & Q_BIT) {
-      switch (texUnit->GenModeQ) {
+      switch (texUnit->GenQ.Mode) {
       case GL_OBJECT_LINEAR:
         _mesa_dotprod_tab[obj->size]( &(out->data[0][3]),
                                       sizeof(out->data[0]), obj,
-                                      texUnit->ObjectPlaneQ );
+                                      texUnit->GenQ.ObjectPlane );
         break;
       case GL_EYE_LINEAR:
         _mesa_dotprod_tab[eye->size]( &(out->data[0][3]),
                                       sizeof(out->data[0]), eye,
-                                      texUnit->EyePlaneQ );
+                                      texUnit->GenQ.EyePlane );
         break;
       default:
         _mesa_problem(ctx, "Bad Q texgen");
@@ -480,36 +479,44 @@ static void texgen( GLcontext *ctx,
 
 
 
-static GLboolean run_texgen_stage( GLcontext *ctx,
+
+static GLboolean run_texgen_stage( struct gl_context *ctx,
                                   struct tnl_pipeline_stage *stage )
 {
    struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
-   struct texgen_stage_data *store = TEXGEN_STAGE_DATA( stage );
+   struct texgen_stage_data *store = TEXGEN_STAGE_DATA(stage);
    GLuint i;
 
-   for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++)
-      if (ctx->Texture._TexGenEnabled & ENABLE_TEXGEN(i)) {
-        if (stage->changed_inputs & (_TNL_BIT_POS | _TNL_BIT_NORMAL | _TNL_BIT_TEX(i)))
-           store->TexgenFunc[i]( ctx, store, i );
+   if (!ctx->Texture._TexGenEnabled || ctx->VertexProgram._Current)
+      return GL_TRUE;
 
-        VB->AttribPtr[VERT_ATTRIB_TEX0+i] = 
-           VB->TexCoordPtr[i] = &store->texcoord[i];
+   for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++) {
+      struct gl_fixedfunc_texture_unit *texUnit =
+         &ctx->Texture.FixedFuncUnit[i];
+
+      if (texUnit->TexGenEnabled) {
+        store->TexgenFunc[i]( ctx, store, i );
+
+         VB->AttribPtr[VERT_ATTRIB_TEX0 + i] = &store->texcoord[i];
       }
+   }
 
    return GL_TRUE;
 }
 
 
-
-
-static GLboolean run_validate_texgen_stage( GLcontext *ctx,
-                                           struct tnl_pipeline_stage *stage )
+static void validate_texgen_stage( struct gl_context *ctx,
+                                  struct tnl_pipeline_stage *stage )
 {
    struct texgen_stage_data *store = TEXGEN_STAGE_DATA(stage);
    GLuint i;
 
+   if (!ctx->Texture._TexGenEnabled || ctx->VertexProgram._Current)
+      return;
+
    for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++) {
-      struct gl_texture_unit *texUnit = &ctx->Texture.Unit[i];
+      struct gl_fixedfunc_texture_unit *texUnit =
+         &ctx->Texture.FixedFuncUnit[i];
 
       if (texUnit->TexGenEnabled) {
         GLuint sz;
@@ -541,61 +548,22 @@ static GLboolean run_validate_texgen_stage( GLcontext *ctx,
         }
       }
    }
-
-   stage->run = run_texgen_stage;
-   return stage->run( ctx, stage );
 }
 
 
-static void check_texgen( GLcontext *ctx, struct tnl_pipeline_stage *stage )
-{
-   GLuint i;
-   stage->active = 0;
-
-   if (ctx->Texture._TexGenEnabled && !ctx->VertexProgram._Enabled) {
-      GLuint inputs = 0;
-      GLuint outputs = 0;
-
-      if (ctx->Texture._GenFlags & (TEXGEN_OBJ_LINEAR | TEXGEN_NEED_EYE_COORD))
-        inputs |= _TNL_BIT_POS;
-
-      if (ctx->Texture._GenFlags & TEXGEN_NEED_NORMALS)
-        inputs |= _TNL_BIT_NORMAL;
-
-      for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++)
-        if (ctx->Texture._TexGenEnabled & ENABLE_TEXGEN(i))
-        {
-           outputs |= _TNL_BIT_TEX(i);
-
-           /* Need the original input in case it contains a Q coord:
-            * (sigh)
-            */
-           inputs |= _TNL_BIT_TEX(i);
-
-           /* Something for Feedback? */
-        }
-
-      if (stage->privatePtr)
-        stage->run = run_validate_texgen_stage;
-      stage->active = 1;
-      stage->inputs = inputs;
-      stage->outputs = outputs;
-   }
-}
-
 
 
 
 /* Called the first time stage->run() is invoked.
  */
-static GLboolean alloc_texgen_data( GLcontext *ctx,
+static GLboolean alloc_texgen_data( struct gl_context *ctx,
                                    struct tnl_pipeline_stage *stage )
 {
    struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
    struct texgen_stage_data *store;
    GLuint i;
 
-   stage->privatePtr = CALLOC(sizeof(*store));
+   stage->privatePtr = calloc(1, sizeof(*store));
    store = TEXGEN_STAGE_DATA(stage);
    if (!store)
       return GL_FALSE;
@@ -603,13 +571,10 @@ static GLboolean alloc_texgen_data( GLcontext *ctx,
    for (i = 0 ; i < ctx->Const.MaxTextureCoordUnits ; i++)
       _mesa_vector4f_alloc( &store->texcoord[i], 0, VB->Size, 32 );
 
-   store->tmp_f = (GLfloat (*)[3]) MALLOC(VB->Size * sizeof(GLfloat) * 3);
-   store->tmp_m = (GLfloat *) MALLOC(VB->Size * sizeof(GLfloat));
+   store->tmp_f = malloc(VB->Size * sizeof(GLfloat) * 3);
+   store->tmp_m = malloc(VB->Size * sizeof(GLfloat));
 
-   /* Now validate and run the stage.
-    */
-   stage->run = run_validate_texgen_stage;
-   return stage->run( ctx, stage );
+   return GL_TRUE;
 }
 
 
@@ -625,9 +590,9 @@ static void free_texgen_data( struct tnl_pipeline_stage *stage )
            _mesa_vector4f_free( &store->texcoord[i] );
 
 
-      if (store->tmp_f) FREE( store->tmp_f );
-      if (store->tmp_m) FREE( store->tmp_m );
-      FREE( store );
+      free( store->tmp_f );
+      free( store->tmp_m );
+      free( store );
       stage->privatePtr = NULL;
    }
 }
@@ -637,14 +602,9 @@ static void free_texgen_data( struct tnl_pipeline_stage *stage )
 const struct tnl_pipeline_stage _tnl_texgen_stage =
 {
    "texgen",                   /* name */
-   _NEW_TEXTURE|_NEW_PROGRAM,  /* when to call check() */
-   _NEW_TEXTURE,               /* when to invalidate stored data */
-   GL_FALSE,                   /* active? */
-   0,                          /* inputs */
-   0,                          /* outputs */
-   0,                          /* changed_inputs */
    NULL,                       /* private data */
+   alloc_texgen_data,          /* destructor */
    free_texgen_data,           /* destructor */
-   check_texgen,               /* check */
-   alloc_texgen_data           /* run -- initially set to alloc data */
+   validate_texgen_stage,              /* check */
+   run_texgen_stage            /* run -- initially set to alloc data */
 };