2000-12-26 05:09:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Mesa 3-D graphics library
|
2003-03-01 01:50:20 +00:00
|
|
|
* Version: 5.1
|
2000-12-26 05:09:27 +00:00
|
|
|
*
|
2003-03-01 01:50:20 +00:00
|
|
|
* Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
|
2000-12-26 05:09:27 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "glheader.h"
|
|
|
|
#include "colormac.h"
|
|
|
|
#include "light.h"
|
|
|
|
#include "macros.h"
|
2002-10-24 23:57:19 +00:00
|
|
|
#include "imports.h"
|
2000-12-26 05:09:27 +00:00
|
|
|
#include "simple_list.h"
|
|
|
|
#include "mtypes.h"
|
|
|
|
|
2001-07-17 19:39:32 +00:00
|
|
|
#include "math/m_translate.h"
|
|
|
|
|
2000-12-26 05:09:27 +00:00
|
|
|
#include "t_context.h"
|
|
|
|
#include "t_pipeline.h"
|
|
|
|
|
|
|
|
#define LIGHT_FLAGS 0x1 /* must be first */
|
|
|
|
#define LIGHT_TWOSIDE 0x2
|
|
|
|
#define LIGHT_COLORMATERIAL 0x4
|
|
|
|
#define MAX_LIGHT_FUNC 0x8
|
|
|
|
|
|
|
|
typedef void (*light_func)( GLcontext *ctx,
|
|
|
|
struct vertex_buffer *VB,
|
|
|
|
struct gl_pipeline_stage *stage,
|
|
|
|
GLvector4f *input );
|
|
|
|
|
|
|
|
struct light_stage_data {
|
2001-07-17 19:39:32 +00:00
|
|
|
struct gl_client_array FloatColor;
|
2001-04-28 08:39:17 +00:00
|
|
|
struct gl_client_array LitColor[2];
|
|
|
|
struct gl_client_array LitSecondary[2];
|
2001-01-24 00:04:58 +00:00
|
|
|
GLvector1ui LitIndex[2];
|
2000-12-26 05:09:27 +00:00
|
|
|
light_func *light_func_tab;
|
|
|
|
};
|
|
|
|
|
2001-07-17 19:39:32 +00:00
|
|
|
|
2001-03-07 05:06:11 +00:00
|
|
|
#define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->privatePtr))
|
2000-12-26 05:09:27 +00:00
|
|
|
|
2001-07-17 19:39:32 +00:00
|
|
|
|
|
|
|
static void import_color_material( GLcontext *ctx,
|
|
|
|
struct gl_pipeline_stage *stage )
|
|
|
|
{
|
|
|
|
struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
|
|
|
|
struct gl_client_array *to = &LIGHT_STAGE_DATA(stage)->FloatColor;
|
|
|
|
struct gl_client_array *from = VB->ColorPtr[0];
|
|
|
|
GLuint count = VB->Count;
|
|
|
|
|
|
|
|
if (!to->Ptr) {
|
|
|
|
to->Ptr = ALIGN_MALLOC( VB->Size * 4 * sizeof(GLfloat), 32 );
|
|
|
|
to->Type = GL_FLOAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No need to transform the same value 3000 times.
|
|
|
|
*/
|
|
|
|
if (!from->StrideB) {
|
|
|
|
to->StrideB = 0;
|
|
|
|
count = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
to->StrideB = 4 * sizeof(GLfloat);
|
|
|
|
|
2003-08-20 07:21:41 +00:00
|
|
|
_math_trans_4fc( (GLfloat (*)[4]) to->Ptr,
|
|
|
|
from->Ptr,
|
|
|
|
from->StrideB,
|
|
|
|
from->Type,
|
|
|
|
from->Size,
|
|
|
|
0,
|
|
|
|
count);
|
2001-07-17 19:39:32 +00:00
|
|
|
|
|
|
|
VB->ColorPtr[0] = to;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-05 18:55:49 +00:00
|
|
|
static void update_materials( GLcontext *ctx,
|
|
|
|
const struct gl_material *src,
|
|
|
|
GLuint bitmask )
|
|
|
|
{
|
|
|
|
_mesa_copy_materials( &ctx->Light.Material, src, bitmask );
|
|
|
|
_mesa_update_material( ctx, bitmask );
|
|
|
|
}
|
|
|
|
|
2000-12-26 05:09:27 +00:00
|
|
|
/* 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)
|
|
|
|
#include "t_vb_lighttmp.h"
|
|
|
|
|
|
|
|
#define TAG(x) x##_tw
|
|
|
|
#define IDX (LIGHT_TWOSIDE)
|
|
|
|
#include "t_vb_lighttmp.h"
|
|
|
|
|
|
|
|
#define TAG(x) x##_fl
|
|
|
|
#define IDX (LIGHT_FLAGS)
|
|
|
|
#include "t_vb_lighttmp.h"
|
|
|
|
|
|
|
|
#define TAG(x) x##_tw_fl
|
|
|
|
#define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE)
|
|
|
|
#include "t_vb_lighttmp.h"
|
|
|
|
|
|
|
|
#define TAG(x) x##_cm
|
|
|
|
#define IDX (LIGHT_COLORMATERIAL)
|
|
|
|
#include "t_vb_lighttmp.h"
|
|
|
|
|
|
|
|
#define TAG(x) x##_tw_cm
|
|
|
|
#define IDX (LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
|
|
|
|
#include "t_vb_lighttmp.h"
|
|
|
|
|
|
|
|
#define TAG(x) x##_fl_cm
|
|
|
|
#define IDX (LIGHT_FLAGS|LIGHT_COLORMATERIAL)
|
|
|
|
#include "t_vb_lighttmp.h"
|
|
|
|
|
|
|
|
#define TAG(x) x##_tw_fl_cm
|
|
|
|
#define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
|
|
|
|
#include "t_vb_lighttmp.h"
|
|
|
|
|
|
|
|
|
|
|
|
static void init_lighting( void )
|
|
|
|
{
|
|
|
|
static int done;
|
|
|
|
|
|
|
|
if (!done) {
|
|
|
|
init_light_tab();
|
|
|
|
init_light_tab_tw();
|
|
|
|
init_light_tab_fl();
|
|
|
|
init_light_tab_tw_fl();
|
|
|
|
init_light_tab_cm();
|
|
|
|
init_light_tab_tw_cm();
|
|
|
|
init_light_tab_fl_cm();
|
|
|
|
init_light_tab_tw_fl_cm();
|
|
|
|
done = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GLboolean run_lighting( GLcontext *ctx, struct gl_pipeline_stage *stage )
|
|
|
|
{
|
|
|
|
struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
|
|
|
|
TNLcontext *tnl = TNL_CONTEXT(ctx);
|
|
|
|
struct vertex_buffer *VB = &tnl->vb;
|
|
|
|
GLvector4f *input = ctx->_NeedEyeCoords ? VB->EyePtr : VB->ObjPtr;
|
|
|
|
GLuint ind;
|
|
|
|
|
2001-04-26 14:53:48 +00:00
|
|
|
/* _tnl_print_vert_flags( __FUNCTION__, stage->changed_inputs ); */
|
|
|
|
|
2000-12-26 05:09:27 +00:00
|
|
|
/* Make sure we can talk about elements 0..2 in the vector we are
|
2001-02-16 00:35:34 +00:00
|
|
|
* lighting.
|
2000-12-26 05:09:27 +00:00
|
|
|
*/
|
2002-01-22 14:35:16 +00:00
|
|
|
if (stage->changed_inputs & (VERT_BIT_EYE|VERT_BIT_POS)) {
|
2001-02-16 00:35:34 +00:00
|
|
|
if (input->size <= 2) {
|
|
|
|
if (input->flags & VEC_NOT_WRITEABLE) {
|
2002-01-22 14:35:16 +00:00
|
|
|
ASSERT(VB->importable_data & VERT_BIT_POS);
|
2001-02-16 00:35:34 +00:00
|
|
|
|
2002-01-22 14:35:16 +00:00
|
|
|
VB->import_data( ctx, VERT_BIT_POS, VEC_NOT_WRITEABLE );
|
2001-02-16 00:35:34 +00:00
|
|
|
input = ctx->_NeedEyeCoords ? VB->EyePtr : VB->ObjPtr;
|
2000-12-26 05:09:27 +00:00
|
|
|
|
2001-02-16 00:35:34 +00:00
|
|
|
ASSERT((input->flags & VEC_NOT_WRITEABLE) == 0);
|
|
|
|
}
|
|
|
|
|
2001-03-03 20:33:27 +00:00
|
|
|
_mesa_vector4f_clean_elem(input, VB->Count, 2);
|
2001-02-16 00:35:34 +00:00
|
|
|
}
|
2000-12-26 05:09:27 +00:00
|
|
|
}
|
2001-03-12 00:48:37 +00:00
|
|
|
|
2000-12-26 05:09:27 +00:00
|
|
|
if (VB->Flag)
|
|
|
|
ind = LIGHT_FLAGS;
|
2001-03-12 00:48:37 +00:00
|
|
|
else
|
2000-12-26 05:09:27 +00:00
|
|
|
ind = 0;
|
|
|
|
|
2001-04-28 08:39:17 +00:00
|
|
|
/* The individual functions know about replaying side-effects
|
|
|
|
* vs. full re-execution.
|
2000-12-26 05:09:27 +00:00
|
|
|
*/
|
|
|
|
store->light_func_tab[ind]( ctx, VB, stage, input );
|
|
|
|
|
|
|
|
return GL_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Called in place of do_lighting when the light table may have changed.
|
|
|
|
*/
|
2001-03-12 00:48:37 +00:00
|
|
|
static GLboolean run_validate_lighting( GLcontext *ctx,
|
2000-12-26 05:09:27 +00:00
|
|
|
struct gl_pipeline_stage *stage )
|
|
|
|
{
|
|
|
|
GLuint ind = 0;
|
2001-03-12 00:48:37 +00:00
|
|
|
light_func *tab;
|
|
|
|
|
2001-01-23 23:39:36 +00:00
|
|
|
if (ctx->Visual.rgbMode) {
|
2000-12-26 05:09:27 +00:00
|
|
|
if (ctx->Light._NeedVertices) {
|
2001-02-16 18:14:41 +00:00
|
|
|
if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
|
2000-12-26 05:09:27 +00:00
|
|
|
tab = _tnl_light_spec_tab;
|
|
|
|
else
|
2001-03-12 00:48:37 +00:00
|
|
|
tab = _tnl_light_tab;
|
|
|
|
}
|
2000-12-26 05:09:27 +00:00
|
|
|
else {
|
|
|
|
if (ctx->Light.EnabledList.next == ctx->Light.EnabledList.prev)
|
|
|
|
tab = _tnl_light_fast_single_tab;
|
|
|
|
else
|
|
|
|
tab = _tnl_light_fast_tab;
|
|
|
|
}
|
|
|
|
}
|
2000-12-27 19:57:37 +00:00
|
|
|
else
|
|
|
|
tab = _tnl_light_ci_tab;
|
2000-12-26 05:09:27 +00:00
|
|
|
|
|
|
|
if (ctx->Light.ColorMaterialEnabled)
|
|
|
|
ind |= LIGHT_COLORMATERIAL;
|
2001-03-12 00:48:37 +00:00
|
|
|
|
2000-12-26 05:09:27 +00:00
|
|
|
if (ctx->Light.Model.TwoSide)
|
|
|
|
ind |= LIGHT_TWOSIDE;
|
|
|
|
|
|
|
|
LIGHT_STAGE_DATA(stage)->light_func_tab = &tab[ind];
|
|
|
|
|
|
|
|
/* This and the above should only be done on _NEW_LIGHT:
|
|
|
|
*/
|
2002-02-13 00:53:19 +00:00
|
|
|
TNL_CONTEXT(ctx)->Driver.NotifyMaterialChange( ctx );
|
2000-12-26 05:09:27 +00:00
|
|
|
|
|
|
|
/* Now run the stage...
|
|
|
|
*/
|
|
|
|
stage->run = run_lighting;
|
|
|
|
return stage->run( ctx, stage );
|
|
|
|
}
|
|
|
|
|
2001-07-17 19:39:32 +00:00
|
|
|
static void alloc_4chan( struct gl_client_array *a, GLuint sz )
|
2001-04-28 08:39:17 +00:00
|
|
|
{
|
2001-07-17 19:39:32 +00:00
|
|
|
a->Ptr = ALIGN_MALLOC( sz * sizeof(GLchan) * 4, 32 );
|
2001-04-28 08:39:17 +00:00
|
|
|
a->Size = 4;
|
2001-07-17 19:39:32 +00:00
|
|
|
a->Type = CHAN_TYPE;
|
2001-04-28 08:39:17 +00:00
|
|
|
a->Stride = 0;
|
2001-07-17 19:39:32 +00:00
|
|
|
a->StrideB = sizeof(GLchan) * 4;
|
2001-04-28 08:39:17 +00:00
|
|
|
a->Enabled = 0;
|
|
|
|
a->Flags = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-26 05:09:27 +00:00
|
|
|
/* Called the first time stage->run is called. In effect, don't
|
|
|
|
* allocate data until the first time the stage is run.
|
|
|
|
*/
|
2001-03-12 00:48:37 +00:00
|
|
|
static GLboolean run_init_lighting( GLcontext *ctx,
|
2000-12-26 05:09:27 +00:00
|
|
|
struct gl_pipeline_stage *stage )
|
|
|
|
{
|
|
|
|
TNLcontext *tnl = TNL_CONTEXT(ctx);
|
|
|
|
struct light_stage_data *store;
|
|
|
|
GLuint size = tnl->vb.Size;
|
|
|
|
|
2001-03-07 05:06:11 +00:00
|
|
|
stage->privatePtr = MALLOC(sizeof(*store));
|
2000-12-26 05:09:27 +00:00
|
|
|
store = LIGHT_STAGE_DATA(stage);
|
|
|
|
if (!store)
|
|
|
|
return GL_FALSE;
|
|
|
|
|
|
|
|
/* Do onetime init.
|
|
|
|
*/
|
|
|
|
init_lighting();
|
|
|
|
|
2001-07-17 19:39:32 +00:00
|
|
|
store->FloatColor.Ptr = 0;
|
|
|
|
|
|
|
|
alloc_4chan( &store->LitColor[0], size );
|
|
|
|
alloc_4chan( &store->LitColor[1], size );
|
|
|
|
alloc_4chan( &store->LitSecondary[0], size );
|
|
|
|
alloc_4chan( &store->LitSecondary[1], size );
|
2001-04-28 08:39:17 +00:00
|
|
|
|
2001-03-03 20:33:27 +00:00
|
|
|
_mesa_vector1ui_alloc( &store->LitIndex[0], 0, size, 32 );
|
|
|
|
_mesa_vector1ui_alloc( &store->LitIndex[1], 0, size, 32 );
|
2000-12-26 05:09:27 +00:00
|
|
|
|
|
|
|
/* Now validate the stage derived data...
|
|
|
|
*/
|
|
|
|
stage->run = run_validate_lighting;
|
|
|
|
return stage->run( ctx, stage );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if lighting is enabled. If so, configure the pipeline stage's
|
|
|
|
* type, inputs, and outputs.
|
|
|
|
*/
|
|
|
|
static void check_lighting( GLcontext *ctx, struct gl_pipeline_stage *stage )
|
|
|
|
{
|
2001-12-14 02:50:01 +00:00
|
|
|
stage->active = ctx->Light.Enabled && !ctx->VertexProgram.Enabled;
|
2000-12-26 05:09:27 +00:00
|
|
|
if (stage->active) {
|
2001-03-07 05:06:11 +00:00
|
|
|
if (stage->privatePtr)
|
2000-12-26 05:09:27 +00:00
|
|
|
stage->run = run_validate_lighting;
|
2002-01-22 14:35:16 +00:00
|
|
|
stage->inputs = VERT_BIT_NORMAL|VERT_BIT_MATERIAL;
|
2001-03-12 00:48:37 +00:00
|
|
|
if (ctx->Light._NeedVertices)
|
2002-01-22 14:35:16 +00:00
|
|
|
stage->inputs |= VERT_BIT_EYE; /* effectively, even when lighting in obj */
|
2001-03-12 00:48:37 +00:00
|
|
|
if (ctx->Light.ColorMaterialEnabled)
|
2002-01-22 14:35:16 +00:00
|
|
|
stage->inputs |= VERT_BIT_COLOR0;
|
2001-01-08 04:09:41 +00:00
|
|
|
|
2002-01-22 14:35:16 +00:00
|
|
|
stage->outputs = VERT_BIT_COLOR0;
|
2001-02-16 18:14:41 +00:00
|
|
|
if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
|
2002-01-22 14:35:16 +00:00
|
|
|
stage->outputs |= VERT_BIT_COLOR1;
|
2000-12-26 05:09:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void dtr( struct gl_pipeline_stage *stage )
|
|
|
|
{
|
|
|
|
struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
|
|
|
|
|
|
|
|
if (store) {
|
2001-07-17 19:39:32 +00:00
|
|
|
ALIGN_FREE( store->LitColor[0].Ptr );
|
|
|
|
ALIGN_FREE( store->LitColor[1].Ptr );
|
|
|
|
ALIGN_FREE( store->LitSecondary[0].Ptr );
|
|
|
|
ALIGN_FREE( store->LitSecondary[1].Ptr );
|
|
|
|
|
|
|
|
if (store->FloatColor.Ptr)
|
|
|
|
ALIGN_FREE( store->FloatColor.Ptr );
|
|
|
|
|
2001-03-12 00:48:37 +00:00
|
|
|
_mesa_vector1ui_free( &store->LitIndex[0] );
|
|
|
|
_mesa_vector1ui_free( &store->LitIndex[1] );
|
2000-12-26 05:09:27 +00:00
|
|
|
FREE( store );
|
2001-03-07 05:06:11 +00:00
|
|
|
stage->privatePtr = 0;
|
2000-12-26 05:09:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-12 00:48:37 +00:00
|
|
|
const struct gl_pipeline_stage _tnl_lighting_stage =
|
|
|
|
{
|
2001-12-14 02:50:01 +00:00
|
|
|
"lighting", /* name */
|
2001-02-16 18:14:41 +00:00
|
|
|
_NEW_LIGHT, /* recheck */
|
|
|
|
_NEW_LIGHT|_NEW_MODELVIEW, /* recalc -- modelview dependency
|
2000-12-26 05:09:27 +00:00
|
|
|
* otherwise not captured by inputs
|
2002-01-22 14:35:16 +00:00
|
|
|
* (which may be VERT_BIT_POS) */
|
2001-12-14 02:50:01 +00:00
|
|
|
GL_FALSE, /* active? */
|
|
|
|
0, /* inputs */
|
|
|
|
0, /* outputs */
|
|
|
|
0, /* changed_inputs */
|
|
|
|
NULL, /* private_data */
|
2000-12-26 05:09:27 +00:00
|
|
|
dtr, /* destroy */
|
|
|
|
check_lighting, /* check */
|
|
|
|
run_init_lighting /* run -- initially set to ctr */
|
|
|
|
};
|