1 /* $Id: t_vb_light.c,v 1.6 2001/01/29 22:10:24 brianp 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
136 * lighting. TODO: Don't repeat this in CVA!
138 if (input
->size
<= 2) {
139 if (input
->flags
& VEC_NOT_WRITEABLE
) {
140 ASSERT(VB
->importable_data
& VERT_OBJ
);
141 VB
->import_data( ctx
, VERT_OBJ
, VEC_NOT_WRITEABLE
);
142 input
= ctx
->_NeedEyeCoords
? VB
->EyePtr
: VB
->ObjPtr
;
143 ASSERT((input
->flags
& VEC_NOT_WRITEABLE
) == 0);
146 gl_vector4f_clean_elem(input
, VB
->Count
, 2);
154 /* The individual tabs know about replaying side-effects vs. full
157 store
->light_func_tab
[ind
]( ctx
, VB
, stage
, input
);
163 /* Called in place of do_lighting when the light table may have changed.
165 static GLboolean
run_validate_lighting( GLcontext
*ctx
,
166 struct gl_pipeline_stage
*stage
)
171 if (ctx
->Visual
.rgbMode
) {
172 if (ctx
->Light
._NeedVertices
) {
173 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
&&
174 ctx
->Texture
._ReallyEnabled
)
175 tab
= _tnl_light_spec_tab
;
177 tab
= _tnl_light_tab
;
180 if (ctx
->Light
.EnabledList
.next
== ctx
->Light
.EnabledList
.prev
)
181 tab
= _tnl_light_fast_single_tab
;
183 tab
= _tnl_light_fast_tab
;
185 /* tab = _tnl_light_tab; */
188 tab
= _tnl_light_ci_tab
;
190 if (ctx
->Light
.ColorMaterialEnabled
)
191 ind
|= LIGHT_COLORMATERIAL
;
193 if (ctx
->Light
.Model
.TwoSide
)
194 ind
|= LIGHT_TWOSIDE
;
196 LIGHT_STAGE_DATA(stage
)->light_func_tab
= &tab
[ind
];
198 /* This and the above should only be done on _NEW_LIGHT:
200 gl_validate_all_lighting_tables( ctx
);
202 /* Now run the stage...
204 stage
->run
= run_lighting
;
205 return stage
->run( ctx
, stage
);
208 /* Called the first time stage->run is called. In effect, don't
209 * allocate data until the first time the stage is run.
211 static GLboolean
run_init_lighting( GLcontext
*ctx
,
212 struct gl_pipeline_stage
*stage
)
214 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
215 struct light_stage_data
*store
;
216 GLuint size
= tnl
->vb
.Size
;
218 stage
->private = MALLOC(sizeof(*store
));
219 store
= LIGHT_STAGE_DATA(stage
);
227 #if CHAN_TYPE == GL_UNSIGNED_BYTE
228 gl_vector4ub_alloc( &store
->LitColor
[0], 0, size
, 32 );
229 gl_vector4ub_alloc( &store
->LitColor
[1], 0, size
, 32 );
230 gl_vector4ub_alloc( &store
->LitSecondary
[0], 0, size
, 32 );
231 gl_vector4ub_alloc( &store
->LitSecondary
[1], 0, size
, 32 );
232 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
233 gl_vector4us_alloc( &store
->LitColor
[0], 0, size
, 32 );
234 gl_vector4us_alloc( &store
->LitColor
[1], 0, size
, 32 );
235 gl_vector4us_alloc( &store
->LitSecondary
[0], 0, size
, 32 );
236 gl_vector4us_alloc( &store
->LitSecondary
[1], 0, size
, 32 );
237 #elif CHAN_TYPE == GL_FLOAT
238 gl_vector4f_alloc( &store
->LitColor
[0], 0, size
, 32 );
239 gl_vector4f_alloc( &store
->LitColor
[1], 0, size
, 32 );
240 gl_vector4f_alloc( &store
->LitSecondary
[0], 0, size
, 32 );
241 gl_vector4f_alloc( &store
->LitSecondary
[1], 0, size
, 32 );
243 gl_vector1ui_alloc( &store
->LitIndex
[0], 0, size
, 32 );
244 gl_vector1ui_alloc( &store
->LitIndex
[1], 0, size
, 32 );
246 /* Now validate the stage derived data...
248 stage
->run
= run_validate_lighting
;
249 return stage
->run( ctx
, stage
);
255 * Check if lighting is enabled. If so, configure the pipeline stage's
256 * type, inputs, and outputs.
258 static void check_lighting( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
260 stage
->active
= ctx
->Light
.Enabled
;
263 stage
->run
= run_validate_lighting
;
264 stage
->inputs
= VERT_NORM
|VERT_MATERIAL
;
265 if (ctx
->Light
._NeedVertices
)
266 stage
->inputs
|= VERT_EYE
; /* effectively, even when lighting in obj */
267 if (ctx
->Light
.ColorMaterialEnabled
)
268 stage
->inputs
|= VERT_RGBA
;
270 stage
->outputs
= VERT_RGBA
;
271 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
&&
272 ctx
->Texture
._ReallyEnabled
)
273 stage
->outputs
|= VERT_SPEC_RGB
;
278 static void dtr( struct gl_pipeline_stage
*stage
)
280 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
283 #if CHAN_TYPE == GL_UNSIGNED_BYTE
284 gl_vector4ub_free( &store
->LitColor
[0] );
285 gl_vector4ub_free( &store
->LitColor
[1] );
286 gl_vector4ub_free( &store
->LitSecondary
[0] );
287 gl_vector4ub_free( &store
->LitSecondary
[1] );
288 #elif CHAN_TYPE == GL_UNSIGNED_SHORT
289 gl_vector4us_free( &store
->LitColor
[0] );
290 gl_vector4us_free( &store
->LitColor
[1] );
291 gl_vector4us_free( &store
->LitSecondary
[0] );
292 gl_vector4us_free( &store
->LitSecondary
[1] );
293 #elif CHAN_TYPE == GL_FLOAT
294 gl_vector4f_free( &store
->LitColor
[0] );
295 gl_vector4f_free( &store
->LitColor
[1] );
296 gl_vector4f_free( &store
->LitSecondary
[0] );
297 gl_vector4f_free( &store
->LitSecondary
[1] );
299 gl_vector1ui_free( &store
->LitIndex
[0] );
300 gl_vector1ui_free( &store
->LitIndex
[1] );
306 const struct gl_pipeline_stage _tnl_lighting_stage
=
309 _NEW_LIGHT
, /* recheck */
310 _NEW_LIGHT
|_NEW_MODELVIEW
, /* recalc -- modelview dependency
311 * otherwise not captured by inputs
312 * (which may be VERT_OBJ) */
313 0,0,0, /* active, inputs, outputs */
314 0,0, /* changed_inputs, private_data */
316 check_lighting
, /* check */
317 run_init_lighting
/* run -- initially set to ctr */