1 /* $Id: t_vb_light.c,v 1.11 2001/03/07 05:06:13 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 GLvector4chan LitColor
[2];
53 GLvector4chan LitSecondary
[2];
54 GLvector1ui LitIndex
[2];
55 light_func
*light_func_tab
;
58 #define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->privatePtr))
60 /* Tables for all the shading functions.
62 static light_func _tnl_light_tab
[MAX_LIGHT_FUNC
];
63 static light_func _tnl_light_fast_tab
[MAX_LIGHT_FUNC
];
64 static light_func _tnl_light_fast_single_tab
[MAX_LIGHT_FUNC
];
65 static light_func _tnl_light_spec_tab
[MAX_LIGHT_FUNC
];
66 static light_func _tnl_light_ci_tab
[MAX_LIGHT_FUNC
];
70 #include "t_vb_lighttmp.h"
73 #define IDX (LIGHT_TWOSIDE)
74 #include "t_vb_lighttmp.h"
77 #define IDX (LIGHT_FLAGS)
78 #include "t_vb_lighttmp.h"
80 #define TAG(x) x##_tw_fl
81 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE)
82 #include "t_vb_lighttmp.h"
85 #define IDX (LIGHT_COLORMATERIAL)
86 #include "t_vb_lighttmp.h"
88 #define TAG(x) x##_tw_cm
89 #define IDX (LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
90 #include "t_vb_lighttmp.h"
92 #define TAG(x) x##_fl_cm
93 #define IDX (LIGHT_FLAGS|LIGHT_COLORMATERIAL)
94 #include "t_vb_lighttmp.h"
96 #define TAG(x) x##_tw_fl_cm
97 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
98 #include "t_vb_lighttmp.h"
101 static void init_lighting( void )
109 init_light_tab_tw_fl();
111 init_light_tab_tw_cm();
112 init_light_tab_fl_cm();
113 init_light_tab_tw_fl_cm();
119 static GLboolean
run_lighting( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
121 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
122 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
123 struct vertex_buffer
*VB
= &tnl
->vb
;
124 GLvector4f
*input
= ctx
->_NeedEyeCoords
? VB
->EyePtr
: VB
->ObjPtr
;
127 /* Make sure we can talk about elements 0..2 in the vector we are
130 if (stage
->changed_inputs
& (VERT_EYE
|VERT_OBJ
)) {
131 if (input
->size
<= 2) {
132 if (input
->flags
& VEC_NOT_WRITEABLE
) {
133 ASSERT(VB
->importable_data
& VERT_OBJ
);
135 VB
->import_data( ctx
, VERT_OBJ
, VEC_NOT_WRITEABLE
);
136 input
= ctx
->_NeedEyeCoords
? VB
->EyePtr
: VB
->ObjPtr
;
138 ASSERT((input
->flags
& VEC_NOT_WRITEABLE
) == 0);
141 _mesa_vector4f_clean_elem(input
, VB
->Count
, 2);
150 /* The individual tabs know about replaying side-effects vs. full
153 store
->light_func_tab
[ind
]( ctx
, VB
, stage
, input
);
159 /* Called in place of do_lighting when the light table may have changed.
161 static GLboolean
run_validate_lighting( GLcontext
*ctx
,
162 struct gl_pipeline_stage
*stage
)
167 if (ctx
->Visual
.rgbMode
) {
168 if (ctx
->Light
._NeedVertices
) {
169 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
170 tab
= _tnl_light_spec_tab
;
172 tab
= _tnl_light_tab
;
175 if (ctx
->Light
.EnabledList
.next
== ctx
->Light
.EnabledList
.prev
)
176 tab
= _tnl_light_fast_single_tab
;
178 tab
= _tnl_light_fast_tab
;
182 tab
= _tnl_light_ci_tab
;
184 if (ctx
->Light
.ColorMaterialEnabled
)
185 ind
|= LIGHT_COLORMATERIAL
;
187 if (ctx
->Light
.Model
.TwoSide
)
188 ind
|= LIGHT_TWOSIDE
;
190 LIGHT_STAGE_DATA(stage
)->light_func_tab
= &tab
[ind
];
192 /* This and the above should only be done on _NEW_LIGHT:
194 _mesa_validate_all_lighting_tables( ctx
);
196 /* Now run the stage...
198 stage
->run
= run_lighting
;
199 return stage
->run( ctx
, stage
);
202 /* Called the first time stage->run is called. In effect, don't
203 * allocate data until the first time the stage is run.
205 static GLboolean
run_init_lighting( GLcontext
*ctx
,
206 struct gl_pipeline_stage
*stage
)
208 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
209 struct light_stage_data
*store
;
210 GLuint size
= tnl
->vb
.Size
;
212 stage
->privatePtr
= MALLOC(sizeof(*store
));
213 store
= LIGHT_STAGE_DATA(stage
);
221 _mesa_vector4chan_alloc( &store
->LitColor
[0], 0, size
, 32 );
222 _mesa_vector4chan_alloc( &store
->LitColor
[1], 0, size
, 32 );
223 _mesa_vector4chan_alloc( &store
->LitSecondary
[0], 0, size
, 32 );
224 _mesa_vector4chan_alloc( &store
->LitSecondary
[1], 0, size
, 32 );
225 _mesa_vector1ui_alloc( &store
->LitIndex
[0], 0, size
, 32 );
226 _mesa_vector1ui_alloc( &store
->LitIndex
[1], 0, size
, 32 );
228 /* Now validate the stage derived data...
230 stage
->run
= run_validate_lighting
;
231 return stage
->run( ctx
, stage
);
237 * Check if lighting is enabled. If so, configure the pipeline stage's
238 * type, inputs, and outputs.
240 static void check_lighting( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
242 stage
->active
= ctx
->Light
.Enabled
;
244 if (stage
->privatePtr
)
245 stage
->run
= run_validate_lighting
;
246 stage
->inputs
= VERT_NORM
|VERT_MATERIAL
;
247 if (ctx
->Light
._NeedVertices
)
248 stage
->inputs
|= VERT_EYE
; /* effectively, even when lighting in obj */
249 if (ctx
->Light
.ColorMaterialEnabled
)
250 stage
->inputs
|= VERT_RGBA
;
252 stage
->outputs
= VERT_RGBA
;
253 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
254 stage
->outputs
|= VERT_SPEC_RGB
;
259 static void dtr( struct gl_pipeline_stage
*stage
)
261 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
264 _mesa_vector4chan_free( &store
->LitColor
[0] );
265 _mesa_vector4chan_free( &store
->LitColor
[1] );
266 _mesa_vector4chan_free( &store
->LitSecondary
[0] );
267 _mesa_vector4chan_free( &store
->LitSecondary
[1] );
268 _mesa_vector1ui_free( &store
->LitIndex
[0] );
269 _mesa_vector1ui_free( &store
->LitIndex
[1] );
271 stage
->privatePtr
= 0;
275 const struct gl_pipeline_stage _tnl_lighting_stage
=
278 _NEW_LIGHT
, /* recheck */
279 _NEW_LIGHT
|_NEW_MODELVIEW
, /* recalc -- modelview dependency
280 * otherwise not captured by inputs
281 * (which may be VERT_OBJ) */
282 0,0,0, /* active, inputs, outputs */
283 0,0, /* changed_inputs, private_data */
285 check_lighting
, /* check */
286 run_init_lighting
/* run -- initially set to ctr */