3
* Mesa 3-D graphics library
6
* Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8
* Permission is hereby granted, free of charge, to any person obtaining a
9
* copy of this software and associated documentation files (the "Software"),
10
* to deal in the Software without restriction, including without limitation
11
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
* and/or sell copies of the Software, and to permit persons to whom the
13
* Software is furnished to do so, subject to the following conditions:
15
* The above copyright notice and this permission notice shall be included
16
* in all copies or substantial portions of the Software.
18
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34
#include "simple_list.h"
37
#include "math/m_translate.h"
39
#include "t_context.h"
40
#include "t_pipeline.h"
42
#define LIGHT_FLAGS 0x1 /* must be first */
43
#define LIGHT_TWOSIDE 0x2
44
#define LIGHT_COLORMATERIAL 0x4
45
#define MAX_LIGHT_FUNC 0x8
47
typedef void (*light_func)( GLcontext *ctx,
48
struct vertex_buffer *VB,
49
struct gl_pipeline_stage *stage,
52
struct light_stage_data {
53
struct gl_client_array FloatColor;
54
struct gl_client_array LitColor[2];
55
struct gl_client_array LitSecondary[2];
56
GLvector1ui LitIndex[2];
57
light_func *light_func_tab;
61
#define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->privatePtr))
64
static void import_color_material( GLcontext *ctx,
65
struct gl_pipeline_stage *stage )
67
struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
68
struct gl_client_array *to = &LIGHT_STAGE_DATA(stage)->FloatColor;
69
struct gl_client_array *from = VB->ColorPtr[0];
70
GLuint count = VB->Count;
73
to->Ptr = ALIGN_MALLOC( VB->Size * 4 * sizeof(GLfloat), 32 );
77
/* No need to transform the same value 3000 times.
84
to->StrideB = 4 * sizeof(GLfloat);
86
_math_trans_4f( (GLfloat (*)[4]) to->Ptr,
98
/* Tables for all the shading functions.
100
static light_func _tnl_light_tab[MAX_LIGHT_FUNC];
101
static light_func _tnl_light_fast_tab[MAX_LIGHT_FUNC];
102
static light_func _tnl_light_fast_single_tab[MAX_LIGHT_FUNC];
103
static light_func _tnl_light_spec_tab[MAX_LIGHT_FUNC];
104
static light_func _tnl_light_ci_tab[MAX_LIGHT_FUNC];
108
#include "t_vb_lighttmp.h"
110
#define TAG(x) x##_tw
111
#define IDX (LIGHT_TWOSIDE)
112
#include "t_vb_lighttmp.h"
114
#define TAG(x) x##_fl
115
#define IDX (LIGHT_FLAGS)
116
#include "t_vb_lighttmp.h"
118
#define TAG(x) x##_tw_fl
119
#define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE)
120
#include "t_vb_lighttmp.h"
122
#define TAG(x) x##_cm
123
#define IDX (LIGHT_COLORMATERIAL)
124
#include "t_vb_lighttmp.h"
126
#define TAG(x) x##_tw_cm
127
#define IDX (LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
128
#include "t_vb_lighttmp.h"
130
#define TAG(x) x##_fl_cm
131
#define IDX (LIGHT_FLAGS|LIGHT_COLORMATERIAL)
132
#include "t_vb_lighttmp.h"
134
#define TAG(x) x##_tw_fl_cm
135
#define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
136
#include "t_vb_lighttmp.h"
139
static void init_lighting( void )
147
init_light_tab_tw_fl();
149
init_light_tab_tw_cm();
150
init_light_tab_fl_cm();
151
init_light_tab_tw_fl_cm();
157
static GLboolean run_lighting( GLcontext *ctx, struct gl_pipeline_stage *stage )
159
struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
160
TNLcontext *tnl = TNL_CONTEXT(ctx);
161
struct vertex_buffer *VB = &tnl->vb;
162
GLvector4f *input = ctx->_NeedEyeCoords ? VB->EyePtr : VB->ObjPtr;
165
/* _tnl_print_vert_flags( __FUNCTION__, stage->changed_inputs ); */
167
/* Make sure we can talk about elements 0..2 in the vector we are
170
if (stage->changed_inputs & (VERT_EYE|VERT_OBJ)) {
171
if (input->size <= 2) {
172
if (input->flags & VEC_NOT_WRITEABLE) {
173
ASSERT(VB->importable_data & VERT_OBJ);
175
VB->import_data( ctx, VERT_OBJ, VEC_NOT_WRITEABLE );
176
input = ctx->_NeedEyeCoords ? VB->EyePtr : VB->ObjPtr;
178
ASSERT((input->flags & VEC_NOT_WRITEABLE) == 0);
181
_mesa_vector4f_clean_elem(input, VB->Count, 2);
190
/* The individual functions know about replaying side-effects
191
* vs. full re-execution.
193
store->light_func_tab[ind]( ctx, VB, stage, input );
199
/* Called in place of do_lighting when the light table may have changed.
201
static GLboolean run_validate_lighting( GLcontext *ctx,
202
struct gl_pipeline_stage *stage )
207
if (ctx->Visual.rgbMode) {
208
if (ctx->Light._NeedVertices) {
209
if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
210
tab = _tnl_light_spec_tab;
212
tab = _tnl_light_tab;
215
if (ctx->Light.EnabledList.next == ctx->Light.EnabledList.prev)
216
tab = _tnl_light_fast_single_tab;
218
tab = _tnl_light_fast_tab;
222
tab = _tnl_light_ci_tab;
224
if (ctx->Light.ColorMaterialEnabled)
225
ind |= LIGHT_COLORMATERIAL;
227
if (ctx->Light.Model.TwoSide)
228
ind |= LIGHT_TWOSIDE;
230
LIGHT_STAGE_DATA(stage)->light_func_tab = &tab[ind];
232
/* This and the above should only be done on _NEW_LIGHT:
234
TNL_CONTEXT(ctx)->Driver.NotifyMaterialChange( ctx );
236
/* Now run the stage...
238
stage->run = run_lighting;
239
return stage->run( ctx, stage );
242
static void alloc_4chan( struct gl_client_array *a, GLuint sz )
244
a->Ptr = ALIGN_MALLOC( sz * sizeof(GLchan) * 4, 32 );
248
a->StrideB = sizeof(GLchan) * 4;
254
/* Called the first time stage->run is called. In effect, don't
255
* allocate data until the first time the stage is run.
257
static GLboolean run_init_lighting( GLcontext *ctx,
258
struct gl_pipeline_stage *stage )
260
TNLcontext *tnl = TNL_CONTEXT(ctx);
261
struct light_stage_data *store;
262
GLuint size = tnl->vb.Size;
264
stage->privatePtr = MALLOC(sizeof(*store));
265
store = LIGHT_STAGE_DATA(stage);
273
store->FloatColor.Ptr = 0;
275
alloc_4chan( &store->LitColor[0], size );
276
alloc_4chan( &store->LitColor[1], size );
277
alloc_4chan( &store->LitSecondary[0], size );
278
alloc_4chan( &store->LitSecondary[1], size );
280
_mesa_vector1ui_alloc( &store->LitIndex[0], 0, size, 32 );
281
_mesa_vector1ui_alloc( &store->LitIndex[1], 0, size, 32 );
283
/* Now validate the stage derived data...
285
stage->run = run_validate_lighting;
286
return stage->run( ctx, stage );
292
* Check if lighting is enabled. If so, configure the pipeline stage's
293
* type, inputs, and outputs.
295
static void check_lighting( GLcontext *ctx, struct gl_pipeline_stage *stage )
297
stage->active = ctx->Light.Enabled;
299
if (stage->privatePtr)
300
stage->run = run_validate_lighting;
301
stage->inputs = VERT_NORM|VERT_MATERIAL;
302
if (ctx->Light._NeedVertices)
303
stage->inputs |= VERT_EYE; /* effectively, even when lighting in obj */
304
if (ctx->Light.ColorMaterialEnabled)
305
stage->inputs |= VERT_RGBA;
307
stage->outputs = VERT_RGBA;
308
if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
309
stage->outputs |= VERT_SPEC_RGB;
314
static void dtr( struct gl_pipeline_stage *stage )
316
struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
319
ALIGN_FREE( store->LitColor[0].Ptr );
320
ALIGN_FREE( store->LitColor[1].Ptr );
321
ALIGN_FREE( store->LitSecondary[0].Ptr );
322
ALIGN_FREE( store->LitSecondary[1].Ptr );
324
if (store->FloatColor.Ptr)
325
ALIGN_FREE( store->FloatColor.Ptr );
327
_mesa_vector1ui_free( &store->LitIndex[0] );
328
_mesa_vector1ui_free( &store->LitIndex[1] );
330
stage->privatePtr = 0;
334
const struct gl_pipeline_stage _tnl_lighting_stage =
337
_NEW_LIGHT, /* recheck */
338
_NEW_LIGHT|_NEW_MODELVIEW, /* recalc -- modelview dependency
339
* otherwise not captured by inputs
340
* (which may be VERT_OBJ) */
341
0,0,0, /* active, inputs, outputs */
342
0,0, /* changed_inputs, private_data */
344
check_lighting, /* check */
345
run_init_lighting /* run -- initially set to ctr */