X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fmesa%2Ftnl%2Ft_vb_fog.c;h=f3a7bd49f40189a4ca81ebc92e0365a9d315138a;hb=e93848e595176ae0bad3bfe64e0ca63fd089bb72;hp=38a80702ab29ebc779eae4b26dd3c7d34416f66e;hpb=cab974cf6c2dbfbf5dd5d291e1aae0f8eeb34290;p=mesa.git diff --git a/src/mesa/tnl/t_vb_fog.c b/src/mesa/tnl/t_vb_fog.c index 38a80702ab2..f3a7bd49f40 100644 --- a/src/mesa/tnl/t_vb_fog.c +++ b/src/mesa/tnl/t_vb_fog.c @@ -1,10 +1,8 @@ -/* $Id: t_vb_fog.c,v 1.1 2000/12/26 05:09:33 keithw Exp $ */ - /* * Mesa 3-D graphics library - * Version: 3.5 + * Version: 6.5 * - * Copyright (C) 1999-2000 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"), @@ -23,18 +21,17 @@ * 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. * - * Author: - * Keith Whitwell + * Authors: + * Keith Whitwell */ -#include "glheader.h" -#include "colormac.h" -#include "context.h" -#include "macros.h" -#include "mem.h" -#include "mmath.h" -#include "mtypes.h" +#include "main/glheader.h" +#include "main/colormac.h" +#include "main/context.h" +#include "main/macros.h" +#include "main/imports.h" +#include "main/mtypes.h" #include "math/m_xform.h" @@ -43,24 +40,68 @@ struct fog_stage_data { - GLvector1f fogcoord; /* has actual storage allocated */ - GLvector1f input; /* points into VB->EyePtr Z values */ + GLvector4f fogcoord; /* has actual storage allocated */ }; -#define FOG_STAGE_DATA(stage) ((struct fog_stage_data *)stage->private) - +#define FOG_STAGE_DATA(stage) ((struct fog_stage_data *)stage->privatePtr) + +#define FOG_EXP_TABLE_SIZE 256 +#define FOG_MAX (10.0) +#define EXP_FOG_MAX .0006595 +#define FOG_INCR (FOG_MAX/FOG_EXP_TABLE_SIZE) +static GLfloat exp_table[FOG_EXP_TABLE_SIZE]; +static GLfloat inited = 0; + +#if 1 +#define NEG_EXP( result, narg ) \ +do { \ + GLfloat f = (GLfloat) (narg * (1.0/FOG_INCR)); \ + GLint k = (GLint) f; \ + if (k > FOG_EXP_TABLE_SIZE-2) \ + result = (GLfloat) EXP_FOG_MAX; \ + else \ + result = exp_table[k] + (f-k)*(exp_table[k+1]-exp_table[k]); \ +} while (0) +#else +#define NEG_EXP( result, narg ) \ +do { \ + result = exp(-narg); \ +} while (0) +#endif + + +/** + * Initialize the exp_table[] lookup table for approximating exp(). + */ +static void +init_static_data( void ) +{ + GLfloat f = 0.0F; + GLint i = 0; + for ( ; i < FOG_EXP_TABLE_SIZE ; i++, f += FOG_INCR) { + exp_table[i] = EXPF(-f); + } + inited = 1; +} -/* Use lookup table & interpolation? +/** + * Compute per-vertex fog blend factors from fog coordinates by + * evaluating the GL_LINEAR, GL_EXP or GL_EXP2 fog function. + * Fog coordinates are distances from the eye (typically between the + * near and far clip plane distances). + * Note that fogcoords may be negative, if eye z is source absolute + * value must be taken earlier. + * Fog blend factors are in the range [0,1]. */ -static void make_win_fog_coords( GLcontext *ctx, GLvector1f *out, - const GLvector1f *in ) +static void +compute_fog_blend_factors(GLcontext *ctx, GLvector4f *out, const GLvector4f *in) { GLfloat end = ctx->Fog.End; GLfloat *v = in->start; GLuint stride = in->stride; GLuint n = in->count; - GLfloat *data = out->data; + GLfloat (*data)[4] = out->data; GLfloat d; GLuint i; @@ -68,46 +109,62 @@ static void make_win_fog_coords( GLcontext *ctx, GLvector1f *out, switch (ctx->Fog.Mode) { case GL_LINEAR: - d = 1.0F / (ctx->Fog.End - ctx->Fog.Start); - for ( i = 0 ; i < n ; i++, STRIDE_F(v, stride)) - data[i] = (end - ABSF(*v)) * d; + if (ctx->Fog.Start == ctx->Fog.End) + d = 1.0F; + else + d = 1.0F / (ctx->Fog.End - ctx->Fog.Start); + for ( i = 0 ; i < n ; i++, STRIDE_F(v, stride)) { + const GLfloat z = *v; + GLfloat f = (end - z) * d; + data[i][0] = CLAMP(f, 0.0F, 1.0F); + } break; case GL_EXP: - d = -ctx->Fog.Density; - for ( i = 0 ; i < n ; i++, STRIDE_F(v,stride)) - data[i] = exp( d*ABSF(*v) ); + d = ctx->Fog.Density; + for ( i = 0 ; i < n ; i++, STRIDE_F(v,stride)) { + const GLfloat z = *v; + NEG_EXP( data[i][0], d * z ); + } break; case GL_EXP2: - d = -(ctx->Fog.Density*ctx->Fog.Density); + d = ctx->Fog.Density*ctx->Fog.Density; for ( i = 0 ; i < n ; i++, STRIDE_F(v, stride)) { - GLfloat z = *v; - data[i] = exp( d*z*z ); + const GLfloat z = *v; + NEG_EXP( data[i][0], d * z * z ); } break; default: - gl_problem(ctx, "Bad fog mode in make_fog_coord"); + _mesa_problem(ctx, "Bad fog mode in make_fog_coord"); return; } } -static GLboolean run_fog_stage( GLcontext *ctx, - struct gl_pipeline_stage *stage ) +static GLboolean +run_fog_stage(GLcontext *ctx, struct tnl_pipeline_stage *stage) { - struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb; + TNLcontext *tnl = TNL_CONTEXT(ctx); + struct vertex_buffer *VB = &tnl->vb; struct fog_stage_data *store = FOG_STAGE_DATA(stage); - GLvector1f *input; + GLvector4f *input; - VB->FogCoordPtr = &store->fogcoord; - if (stage->changed_inputs == 0) + if (!ctx->Fog.Enabled) return GL_TRUE; - if (ctx->Fog.FogCoordinateSource == GL_FRAGMENT_DEPTH_EXT) { + if (ctx->Fog.FogCoordinateSource == GL_FRAGMENT_DEPTH_EXT && !ctx->VertexProgram._Current) { + GLuint i; + GLfloat *coord; + /* Fog is computed from vertex or fragment Z values */ + /* source = VB->ObjPtr or VB->EyePtr coords */ + /* dest = VB->AttribPtr[_TNL_ATTRIB_FOG] = fog stage private storage */ + VB->AttribPtr[_TNL_ATTRIB_FOG] = &store->fogcoord; + if (!ctx->_NeedEyeCoords) { - GLfloat *m = ctx->ModelView.m; + /* compute fog coords from object coords */ + const GLfloat *m = ctx->ModelviewMatrixStack.Top->m; GLfloat plane[4]; - + /* Use this to store calculated eye z values: */ input = &store->fogcoord; @@ -116,86 +173,105 @@ static GLboolean run_fog_stage( GLcontext *ctx, plane[1] = m[6]; plane[2] = m[10]; plane[3] = m[14]; - /* Full eye coords weren't required, just calculate the * eye Z values. */ - gl_dotprod_tab[0][VB->ObjPtr->size](input->data, sizeof(GLfloat), - VB->ObjPtr, plane, 0 ); + _mesa_dotprod_tab[VB->ObjPtr->size]( (GLfloat *) input->data, + 4 * sizeof(GLfloat), + VB->ObjPtr, plane ); input->count = VB->ObjPtr->count; + + /* make sure coords are really positive + NOTE should avoid going through array twice */ + coord = input->start; + for (i = 0; i < input->count; i++) { + *coord = FABSF(*coord); + STRIDE_F(coord, input->stride); + } } - else - { - input = &store->input; + else { + /* fog coordinates = eye Z coordinates - need to copy for ABS */ + input = &store->fogcoord; if (VB->EyePtr->size < 2) - gl_vector4f_clean_elem( VB->EyePtr, VB->Count, 2 ); + _mesa_vector4f_clean_elem( VB->EyePtr, VB->Count, 2 ); - input->data = &(VB->EyePtr->data[0][2]); - input->start = VB->EyePtr->start+2; - input->stride = VB->EyePtr->stride; + input->stride = 4 * sizeof(GLfloat); input->count = VB->EyePtr->count; + coord = VB->EyePtr->start; + for (i = 0 ; i < VB->EyePtr->count; i++) { + input->data[i][0] = FABSF(coord[2]); + STRIDE_F(coord, VB->EyePtr->stride); + } } - } else - input = VB->FogCoordPtr; + } + else { + /* use glFogCoord() coordinates */ + input = VB->AttribPtr[_TNL_ATTRIB_FOG]; /* source data */ - make_win_fog_coords( ctx, VB->FogCoordPtr, input ); - return GL_TRUE; -} + /* input->count may be one if glFogCoord was only called once + * before glBegin. But we need to compute fog for all vertices. + */ + input->count = VB->ObjPtr->count; -static void check_fog_stage( GLcontext *ctx, struct gl_pipeline_stage *stage ) -{ - stage->active = ctx->Fog.Enabled; + VB->AttribPtr[_TNL_ATTRIB_FOG] = &store->fogcoord; /* dest data */ + } - if (ctx->Fog.FogCoordinateSource == GL_FRAGMENT_DEPTH_EXT) - stage->inputs = VERT_EYE; - else - stage->inputs = VERT_FOG_COORD; + if (tnl->_DoVertexFog) { + /* compute blend factors from fog coordinates */ + compute_fog_blend_factors( ctx, VB->AttribPtr[_TNL_ATTRIB_FOG], input ); + } + else { + /* results = incoming fog coords (compute fog per-fragment later) */ + VB->AttribPtr[_TNL_ATTRIB_FOG] = input; + } + + VB->FogCoordPtr = VB->AttribPtr[_TNL_ATTRIB_FOG]; + return GL_TRUE; } + /* Called the first time stage->run() is invoked. */ -static GLboolean alloc_fog_data( GLcontext *ctx, - struct gl_pipeline_stage *stage ) +static GLboolean +alloc_fog_data(GLcontext *ctx, struct tnl_pipeline_stage *stage) { TNLcontext *tnl = TNL_CONTEXT(ctx); struct fog_stage_data *store; - stage->private = MALLOC(sizeof(*store)); + stage->privatePtr = MALLOC(sizeof(*store)); store = FOG_STAGE_DATA(stage); if (!store) return GL_FALSE; - gl_vector1f_alloc( &store->fogcoord, 0, tnl->vb.Size, 32 ); - gl_vector1f_init( &store->input, 0, 0 ); + _mesa_vector4f_alloc( &store->fogcoord, 0, tnl->vb.Size, 32 ); - /* Now run the stage. - */ - stage->run = run_fog_stage; - return stage->run( ctx, stage ); + if (!inited) + init_static_data(); + + return GL_TRUE; } -static void free_fog_data( struct gl_pipeline_stage *stage ) +static void +free_fog_data(struct tnl_pipeline_stage *stage) { struct fog_stage_data *store = FOG_STAGE_DATA(stage); if (store) { - gl_vector1f_free( &store->fogcoord ); + _mesa_vector4f_free( &store->fogcoord ); FREE( store ); - stage->private = 0; + stage->privatePtr = NULL; } } -const struct gl_pipeline_stage _tnl_fog_coordinate_stage = -{ - "build fog coordinates", - _NEW_FOG, - _NEW_FOG, - 0, 0, VERT_FOG_COORD, /* active, inputs, outputs */ - 0, 0, /* changed_inputs, private_data */ +const struct tnl_pipeline_stage _tnl_fog_coordinate_stage = +{ + "build fog coordinates", /* name */ + NULL, /* private_data */ + alloc_fog_data, /* dtr */ free_fog_data, /* dtr */ - check_fog_stage, /* check */ - alloc_fog_data /* run -- initially set to init. */ + NULL, /* check */ + run_fog_stage /* run -- initially set to init. */ };