1 /* $Id: t_vb_light.c,v 1.8 2001/02/16 18:14:42 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 #include "simple_list.h"
38 #include "t_context.h"
39 #include "t_pipeline.h"
41 #define LIGHT_FLAGS 0x1 /* must be first */
42 #define LIGHT_TWOSIDE 0x2
43 #define LIGHT_COLORMATERIAL 0x4
44 #define MAX_LIGHT_FUNC 0x8
46 typedef void (*light_func
)( GLcontext
*ctx
,
47 struct vertex_buffer
*VB
,
48 struct gl_pipeline_stage
*stage
,
51 struct light_stage_data
{
52 #if CHAN_TYPE == GL_UNSIGNED_BYTE
53 GLvector4ub LitColor
[2];
54 GLvector4ub LitSecondary
[2];
55 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
56 GLvector4us LitColor
[2];
57 GLvector4us LitSecondary
[2];
58 #elif CHAN_TYPE == GL_FLOAT
59 GLvector4f LitColor
[2];
60 GLvector4f LitSecondary
[2];
62 GLvector1ui LitIndex
[2];
63 light_func
*light_func_tab
;
66 #define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->private))
68 /* Tables for all the shading functions.
70 static light_func _tnl_light_tab
[MAX_LIGHT_FUNC
];
71 static light_func _tnl_light_fast_tab
[MAX_LIGHT_FUNC
];
72 static light_func _tnl_light_fast_single_tab
[MAX_LIGHT_FUNC
];
73 static light_func _tnl_light_spec_tab
[MAX_LIGHT_FUNC
];
74 static light_func _tnl_light_ci_tab
[MAX_LIGHT_FUNC
];
78 #include "t_vb_lighttmp.h"
81 #define IDX (LIGHT_TWOSIDE)
82 #include "t_vb_lighttmp.h"
85 #define IDX (LIGHT_FLAGS)
86 #include "t_vb_lighttmp.h"
88 #define TAG(x) x##_tw_fl
89 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE)
90 #include "t_vb_lighttmp.h"
93 #define IDX (LIGHT_COLORMATERIAL)
94 #include "t_vb_lighttmp.h"
96 #define TAG(x) x##_tw_cm
97 #define IDX (LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
98 #include "t_vb_lighttmp.h"
100 #define TAG(x) x##_fl_cm
101 #define IDX (LIGHT_FLAGS|LIGHT_COLORMATERIAL)
102 #include "t_vb_lighttmp.h"
104 #define TAG(x) x##_tw_fl_cm
105 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
106 #include "t_vb_lighttmp.h"
109 static void init_lighting( void )
117 init_light_tab_tw_fl();
119 init_light_tab_tw_cm();
120 init_light_tab_fl_cm();
121 init_light_tab_tw_fl_cm();
127 static GLboolean
run_lighting( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
129 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
130 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
131 struct vertex_buffer
*VB
= &tnl
->vb
;
132 GLvector4f
*input
= ctx
->_NeedEyeCoords
? VB
->EyePtr
: VB
->ObjPtr
;
135 /* Make sure we can talk about elements 0..2 in the vector we are
138 if (stage
->changed_inputs
& (VERT_EYE
|VERT_OBJ
)) {
139 if (input
->size
<= 2) {
140 if (input
->flags
& VEC_NOT_WRITEABLE
) {
141 ASSERT(VB
->importable_data
& VERT_OBJ
);
143 VB
->import_data( ctx
, VERT_OBJ
, VEC_NOT_WRITEABLE
);
144 input
= ctx
->_NeedEyeCoords
? VB
->EyePtr
: VB
->ObjPtr
;
146 ASSERT((input
->flags
& VEC_NOT_WRITEABLE
) == 0);
149 gl_vector4f_clean_elem(input
, VB
->Count
, 2);
158 /* The individual tabs know about replaying side-effects vs. full
161 store
->light_func_tab
[ind
]( ctx
, VB
, stage
, input
);
167 /* Called in place of do_lighting when the light table may have changed.
169 static GLboolean
run_validate_lighting( GLcontext
*ctx
,
170 struct gl_pipeline_stage
*stage
)
175 if (ctx
->Visual
.rgbMode
) {
176 if (ctx
->Light
._NeedVertices
) {
177 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
178 tab
= _tnl_light_spec_tab
;
180 tab
= _tnl_light_tab
;
183 if (ctx
->Light
.EnabledList
.next
== ctx
->Light
.EnabledList
.prev
)
184 tab
= _tnl_light_fast_single_tab
;
186 tab
= _tnl_light_fast_tab
;
190 tab
= _tnl_light_ci_tab
;
192 if (ctx
->Light
.ColorMaterialEnabled
)
193 ind
|= LIGHT_COLORMATERIAL
;
195 if (ctx
->Light
.Model
.TwoSide
)
196 ind
|= LIGHT_TWOSIDE
;
198 LIGHT_STAGE_DATA(stage
)->light_func_tab
= &tab
[ind
];
200 /* This and the above should only be done on _NEW_LIGHT:
202 gl_validate_all_lighting_tables( ctx
);
204 /* Now run the stage...
206 stage
->run
= run_lighting
;
207 return stage
->run( ctx
, stage
);
210 /* Called the first time stage->run is called. In effect, don't
211 * allocate data until the first time the stage is run.
213 static GLboolean
run_init_lighting( GLcontext
*ctx
,
214 struct gl_pipeline_stage
*stage
)
216 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
217 struct light_stage_data
*store
;
218 GLuint size
= tnl
->vb
.Size
;
220 stage
->private = MALLOC(sizeof(*store
));
221 store
= LIGHT_STAGE_DATA(stage
);
229 #if CHAN_TYPE == GL_UNSIGNED_BYTE
230 gl_vector4ub_alloc( &store
->LitColor
[0], 0, size
, 32 );
231 gl_vector4ub_alloc( &store
->LitColor
[1], 0, size
, 32 );
232 gl_vector4ub_alloc( &store
->LitSecondary
[0], 0, size
, 32 );
233 gl_vector4ub_alloc( &store
->LitSecondary
[1], 0, size
, 32 );
234 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
235 gl_vector4us_alloc( &store
->LitColor
[0], 0, size
, 32 );
236 gl_vector4us_alloc( &store
->LitColor
[1], 0, size
, 32 );
237 gl_vector4us_alloc( &store
->LitSecondary
[0], 0, size
, 32 );
238 gl_vector4us_alloc( &store
->LitSecondary
[1], 0, size
, 32 );
239 #elif CHAN_TYPE == GL_FLOAT
240 gl_vector4f_alloc( &store
->LitColor
[0], 0, size
, 32 );
241 gl_vector4f_alloc( &store
->LitColor
[1], 0, size
, 32 );
242 gl_vector4f_alloc( &store
->LitSecondary
[0], 0, size
, 32 );
243 gl_vector4f_alloc( &store
->LitSecondary
[1], 0, size
, 32 );
245 gl_vector1ui_alloc( &store
->LitIndex
[0], 0, size
, 32 );
246 gl_vector1ui_alloc( &store
->LitIndex
[1], 0, size
, 32 );
248 /* Now validate the stage derived data...
250 stage
->run
= run_validate_lighting
;
251 return stage
->run( ctx
, stage
);
257 * Check if lighting is enabled. If so, configure the pipeline stage's
258 * type, inputs, and outputs.
260 static void check_lighting( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
262 stage
->active
= ctx
->Light
.Enabled
;
265 stage
->run
= run_validate_lighting
;
266 stage
->inputs
= VERT_NORM
|VERT_MATERIAL
;
267 if (ctx
->Light
._NeedVertices
)
268 stage
->inputs
|= VERT_EYE
; /* effectively, even when lighting in obj */
269 if (ctx
->Light
.ColorMaterialEnabled
)
270 stage
->inputs
|= VERT_RGBA
;
272 stage
->outputs
= VERT_RGBA
;
273 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
274 stage
->outputs
|= VERT_SPEC_RGB
;
279 static void dtr( struct gl_pipeline_stage
*stage
)
281 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
284 #if CHAN_TYPE == GL_UNSIGNED_BYTE
285 gl_vector4ub_free( &store
->LitColor
[0] );
286 gl_vector4ub_free( &store
->LitColor
[1] );
287 gl_vector4ub_free( &store
->LitSecondary
[0] );
288 gl_vector4ub_free( &store
->LitSecondary
[1] );
289 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
290 gl_vector4us_free( &store
->LitColor
[0] );
291 gl_vector4us_free( &store
->LitColor
[1] );
292 gl_vector4us_free( &store
->LitSecondary
[0] );
293 gl_vector4us_free( &store
->LitSecondary
[1] );
294 #elif CHAN_TYPE == GL_FLOAT
295 gl_vector4f_free( &store
->LitColor
[0] );
296 gl_vector4f_free( &store
->LitColor
[1] );
297 gl_vector4f_free( &store
->LitSecondary
[0] );
298 gl_vector4f_free( &store
->LitSecondary
[1] );
300 gl_vector1ui_free( &store
->LitIndex
[0] );
301 gl_vector1ui_free( &store
->LitIndex
[1] );
307 const struct gl_pipeline_stage _tnl_lighting_stage
=
310 _NEW_LIGHT
, /* recheck */
311 _NEW_LIGHT
|_NEW_MODELVIEW
, /* recalc -- modelview dependency
312 * otherwise not captured by inputs
313 * (which may be VERT_OBJ) */
314 0,0,0, /* active, inputs, outputs */
315 0,0, /* changed_inputs, private_data */
317 check_lighting
, /* check */
318 run_init_lighting
/* run -- initially set to ctr */