fixed a bunch of g++ warnings/errors. Compiling with g++ can help find lots of poten...
[mesa.git] / src / mesa / tnl / t_vb_light.c
1 /* $Id: t_vb_light.c,v 1.11 2001/03/07 05:06:13 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27
28
29 #include "glheader.h"
30 #include "colormac.h"
31 #include "light.h"
32 #include "macros.h"
33 #include "mem.h"
34 #include "mmath.h"
35 #include "simple_list.h"
36 #include "mtypes.h"
37
38 #include "t_context.h"
39 #include "t_pipeline.h"
40
41 #define LIGHT_FLAGS 0x1 /* must be first */
42 #define LIGHT_TWOSIDE 0x2
43 #define LIGHT_COLORMATERIAL 0x4
44 #define MAX_LIGHT_FUNC 0x8
45
46 typedef void (*light_func)( GLcontext *ctx,
47 struct vertex_buffer *VB,
48 struct gl_pipeline_stage *stage,
49 GLvector4f *input );
50
51 struct light_stage_data {
52 GLvector4chan LitColor[2];
53 GLvector4chan LitSecondary[2];
54 GLvector1ui LitIndex[2];
55 light_func *light_func_tab;
56 };
57
58 #define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->privatePtr))
59
60 /* Tables for all the shading functions.
61 */
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];
67
68 #define TAG(x) x
69 #define IDX (0)
70 #include "t_vb_lighttmp.h"
71
72 #define TAG(x) x##_tw
73 #define IDX (LIGHT_TWOSIDE)
74 #include "t_vb_lighttmp.h"
75
76 #define TAG(x) x##_fl
77 #define IDX (LIGHT_FLAGS)
78 #include "t_vb_lighttmp.h"
79
80 #define TAG(x) x##_tw_fl
81 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE)
82 #include "t_vb_lighttmp.h"
83
84 #define TAG(x) x##_cm
85 #define IDX (LIGHT_COLORMATERIAL)
86 #include "t_vb_lighttmp.h"
87
88 #define TAG(x) x##_tw_cm
89 #define IDX (LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
90 #include "t_vb_lighttmp.h"
91
92 #define TAG(x) x##_fl_cm
93 #define IDX (LIGHT_FLAGS|LIGHT_COLORMATERIAL)
94 #include "t_vb_lighttmp.h"
95
96 #define TAG(x) x##_tw_fl_cm
97 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
98 #include "t_vb_lighttmp.h"
99
100
101 static void init_lighting( void )
102 {
103 static int done;
104
105 if (!done) {
106 init_light_tab();
107 init_light_tab_tw();
108 init_light_tab_fl();
109 init_light_tab_tw_fl();
110 init_light_tab_cm();
111 init_light_tab_tw_cm();
112 init_light_tab_fl_cm();
113 init_light_tab_tw_fl_cm();
114 done = 1;
115 }
116 }
117
118
119 static GLboolean run_lighting( GLcontext *ctx, struct gl_pipeline_stage *stage )
120 {
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;
125 GLuint ind;
126
127 /* Make sure we can talk about elements 0..2 in the vector we are
128 * lighting.
129 */
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);
134
135 VB->import_data( ctx, VERT_OBJ, VEC_NOT_WRITEABLE );
136 input = ctx->_NeedEyeCoords ? VB->EyePtr : VB->ObjPtr;
137
138 ASSERT((input->flags & VEC_NOT_WRITEABLE) == 0);
139 }
140
141 _mesa_vector4f_clean_elem(input, VB->Count, 2);
142 }
143 }
144
145 if (VB->Flag)
146 ind = LIGHT_FLAGS;
147 else
148 ind = 0;
149
150 /* The individual tabs know about replaying side-effects vs. full
151 * re-execution.
152 */
153 store->light_func_tab[ind]( ctx, VB, stage, input );
154
155 return GL_TRUE;
156 }
157
158
159 /* Called in place of do_lighting when the light table may have changed.
160 */
161 static GLboolean run_validate_lighting( GLcontext *ctx,
162 struct gl_pipeline_stage *stage )
163 {
164 GLuint ind = 0;
165 light_func *tab;
166
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;
171 else
172 tab = _tnl_light_tab;
173 }
174 else {
175 if (ctx->Light.EnabledList.next == ctx->Light.EnabledList.prev)
176 tab = _tnl_light_fast_single_tab;
177 else
178 tab = _tnl_light_fast_tab;
179 }
180 }
181 else
182 tab = _tnl_light_ci_tab;
183
184 if (ctx->Light.ColorMaterialEnabled)
185 ind |= LIGHT_COLORMATERIAL;
186
187 if (ctx->Light.Model.TwoSide)
188 ind |= LIGHT_TWOSIDE;
189
190 LIGHT_STAGE_DATA(stage)->light_func_tab = &tab[ind];
191
192 /* This and the above should only be done on _NEW_LIGHT:
193 */
194 _mesa_validate_all_lighting_tables( ctx );
195
196 /* Now run the stage...
197 */
198 stage->run = run_lighting;
199 return stage->run( ctx, stage );
200 }
201
202 /* Called the first time stage->run is called. In effect, don't
203 * allocate data until the first time the stage is run.
204 */
205 static GLboolean run_init_lighting( GLcontext *ctx,
206 struct gl_pipeline_stage *stage )
207 {
208 TNLcontext *tnl = TNL_CONTEXT(ctx);
209 struct light_stage_data *store;
210 GLuint size = tnl->vb.Size;
211
212 stage->privatePtr = MALLOC(sizeof(*store));
213 store = LIGHT_STAGE_DATA(stage);
214 if (!store)
215 return GL_FALSE;
216
217 /* Do onetime init.
218 */
219 init_lighting();
220
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 );
227
228 /* Now validate the stage derived data...
229 */
230 stage->run = run_validate_lighting;
231 return stage->run( ctx, stage );
232 }
233
234
235
236 /*
237 * Check if lighting is enabled. If so, configure the pipeline stage's
238 * type, inputs, and outputs.
239 */
240 static void check_lighting( GLcontext *ctx, struct gl_pipeline_stage *stage )
241 {
242 stage->active = ctx->Light.Enabled;
243 if (stage->active) {
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;
251
252 stage->outputs = VERT_RGBA;
253 if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
254 stage->outputs |= VERT_SPEC_RGB;
255 }
256 }
257
258
259 static void dtr( struct gl_pipeline_stage *stage )
260 {
261 struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
262
263 if (store) {
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] );
270 FREE( store );
271 stage->privatePtr = 0;
272 }
273 }
274
275 const struct gl_pipeline_stage _tnl_lighting_stage =
276 {
277 "lighting",
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 */
284 dtr, /* destroy */
285 check_lighting, /* check */
286 run_init_lighting /* run -- initially set to ctr */
287 };
288