13ab9a395bae6ca19e922b1a78ec611364c5622e
[mesa.git] / src / mesa / tnl / t_vb_light.c
1 /* $Id: t_vb_light.c,v 1.6 2001/01/29 22:10:24 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 #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];
61 #endif
62 GLvector1ui LitIndex[2];
63 light_func *light_func_tab;
64 };
65
66 #define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->private))
67
68 /* Tables for all the shading functions.
69 */
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];
75
76 #define TAG(x) x
77 #define IDX (0)
78 #include "t_vb_lighttmp.h"
79
80 #define TAG(x) x##_tw
81 #define IDX (LIGHT_TWOSIDE)
82 #include "t_vb_lighttmp.h"
83
84 #define TAG(x) x##_fl
85 #define IDX (LIGHT_FLAGS)
86 #include "t_vb_lighttmp.h"
87
88 #define TAG(x) x##_tw_fl
89 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE)
90 #include "t_vb_lighttmp.h"
91
92 #define TAG(x) x##_cm
93 #define IDX (LIGHT_COLORMATERIAL)
94 #include "t_vb_lighttmp.h"
95
96 #define TAG(x) x##_tw_cm
97 #define IDX (LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
98 #include "t_vb_lighttmp.h"
99
100 #define TAG(x) x##_fl_cm
101 #define IDX (LIGHT_FLAGS|LIGHT_COLORMATERIAL)
102 #include "t_vb_lighttmp.h"
103
104 #define TAG(x) x##_tw_fl_cm
105 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
106 #include "t_vb_lighttmp.h"
107
108
109 static void init_lighting( void )
110 {
111 static int done;
112
113 if (!done) {
114 init_light_tab();
115 init_light_tab_tw();
116 init_light_tab_fl();
117 init_light_tab_tw_fl();
118 init_light_tab_cm();
119 init_light_tab_tw_cm();
120 init_light_tab_fl_cm();
121 init_light_tab_tw_fl_cm();
122 done = 1;
123 }
124 }
125
126
127 static GLboolean run_lighting( GLcontext *ctx, struct gl_pipeline_stage *stage )
128 {
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;
133 GLuint ind;
134
135 /* Make sure we can talk about elements 0..2 in the vector we are
136 * lighting. TODO: Don't repeat this in CVA!
137 */
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);
144 }
145
146 gl_vector4f_clean_elem(input, VB->Count, 2);
147 }
148
149 if (VB->Flag)
150 ind = LIGHT_FLAGS;
151 else
152 ind = 0;
153
154 /* The individual tabs know about replaying side-effects vs. full
155 * re-execution.
156 */
157 store->light_func_tab[ind]( ctx, VB, stage, input );
158
159 return GL_TRUE;
160 }
161
162
163 /* Called in place of do_lighting when the light table may have changed.
164 */
165 static GLboolean run_validate_lighting( GLcontext *ctx,
166 struct gl_pipeline_stage *stage )
167 {
168 GLuint ind = 0;
169 light_func *tab;
170
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;
176 else
177 tab = _tnl_light_tab;
178 }
179 else {
180 if (ctx->Light.EnabledList.next == ctx->Light.EnabledList.prev)
181 tab = _tnl_light_fast_single_tab;
182 else
183 tab = _tnl_light_fast_tab;
184 }
185 /* tab = _tnl_light_tab; */
186 }
187 else
188 tab = _tnl_light_ci_tab;
189
190 if (ctx->Light.ColorMaterialEnabled)
191 ind |= LIGHT_COLORMATERIAL;
192
193 if (ctx->Light.Model.TwoSide)
194 ind |= LIGHT_TWOSIDE;
195
196 LIGHT_STAGE_DATA(stage)->light_func_tab = &tab[ind];
197
198 /* This and the above should only be done on _NEW_LIGHT:
199 */
200 gl_validate_all_lighting_tables( ctx );
201
202 /* Now run the stage...
203 */
204 stage->run = run_lighting;
205 return stage->run( ctx, stage );
206 }
207
208 /* Called the first time stage->run is called. In effect, don't
209 * allocate data until the first time the stage is run.
210 */
211 static GLboolean run_init_lighting( GLcontext *ctx,
212 struct gl_pipeline_stage *stage )
213 {
214 TNLcontext *tnl = TNL_CONTEXT(ctx);
215 struct light_stage_data *store;
216 GLuint size = tnl->vb.Size;
217
218 stage->private = MALLOC(sizeof(*store));
219 store = LIGHT_STAGE_DATA(stage);
220 if (!store)
221 return GL_FALSE;
222
223 /* Do onetime init.
224 */
225 init_lighting();
226
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 );
242 #endif
243 gl_vector1ui_alloc( &store->LitIndex[0], 0, size, 32 );
244 gl_vector1ui_alloc( &store->LitIndex[1], 0, size, 32 );
245
246 /* Now validate the stage derived data...
247 */
248 stage->run = run_validate_lighting;
249 return stage->run( ctx, stage );
250 }
251
252
253
254 /*
255 * Check if lighting is enabled. If so, configure the pipeline stage's
256 * type, inputs, and outputs.
257 */
258 static void check_lighting( GLcontext *ctx, struct gl_pipeline_stage *stage )
259 {
260 stage->active = ctx->Light.Enabled;
261 if (stage->active) {
262 if (stage->private)
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;
269
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;
274 }
275 }
276
277
278 static void dtr( struct gl_pipeline_stage *stage )
279 {
280 struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
281
282 if (store) {
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] );
298 #endif
299 gl_vector1ui_free( &store->LitIndex[0] );
300 gl_vector1ui_free( &store->LitIndex[1] );
301 FREE( store );
302 stage->private = 0;
303 }
304 }
305
306 const struct gl_pipeline_stage _tnl_lighting_stage =
307 {
308 "lighting",
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 */
315 dtr, /* destroy */
316 check_lighting, /* check */
317 run_init_lighting /* run -- initially set to ctr */
318 };
319