Allow swrast to cope (fairly) cleanly with GL_SEPERATE_SPECULAR when
[mesa.git] / src / mesa / tnl / t_vb_light.c
1 /* $Id: t_vb_light.c,v 1.8 2001/02/16 18:14:42 keithw 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.
137 */
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);
142
143 VB->import_data( ctx, VERT_OBJ, VEC_NOT_WRITEABLE );
144 input = ctx->_NeedEyeCoords ? VB->EyePtr : VB->ObjPtr;
145
146 ASSERT((input->flags & VEC_NOT_WRITEABLE) == 0);
147 }
148
149 gl_vector4f_clean_elem(input, VB->Count, 2);
150 }
151 }
152
153 if (VB->Flag)
154 ind = LIGHT_FLAGS;
155 else
156 ind = 0;
157
158 /* The individual tabs know about replaying side-effects vs. full
159 * re-execution.
160 */
161 store->light_func_tab[ind]( ctx, VB, stage, input );
162
163 return GL_TRUE;
164 }
165
166
167 /* Called in place of do_lighting when the light table may have changed.
168 */
169 static GLboolean run_validate_lighting( GLcontext *ctx,
170 struct gl_pipeline_stage *stage )
171 {
172 GLuint ind = 0;
173 light_func *tab;
174
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;
179 else
180 tab = _tnl_light_tab;
181 }
182 else {
183 if (ctx->Light.EnabledList.next == ctx->Light.EnabledList.prev)
184 tab = _tnl_light_fast_single_tab;
185 else
186 tab = _tnl_light_fast_tab;
187 }
188 }
189 else
190 tab = _tnl_light_ci_tab;
191
192 if (ctx->Light.ColorMaterialEnabled)
193 ind |= LIGHT_COLORMATERIAL;
194
195 if (ctx->Light.Model.TwoSide)
196 ind |= LIGHT_TWOSIDE;
197
198 LIGHT_STAGE_DATA(stage)->light_func_tab = &tab[ind];
199
200 /* This and the above should only be done on _NEW_LIGHT:
201 */
202 gl_validate_all_lighting_tables( ctx );
203
204 /* Now run the stage...
205 */
206 stage->run = run_lighting;
207 return stage->run( ctx, stage );
208 }
209
210 /* Called the first time stage->run is called. In effect, don't
211 * allocate data until the first time the stage is run.
212 */
213 static GLboolean run_init_lighting( GLcontext *ctx,
214 struct gl_pipeline_stage *stage )
215 {
216 TNLcontext *tnl = TNL_CONTEXT(ctx);
217 struct light_stage_data *store;
218 GLuint size = tnl->vb.Size;
219
220 stage->private = MALLOC(sizeof(*store));
221 store = LIGHT_STAGE_DATA(stage);
222 if (!store)
223 return GL_FALSE;
224
225 /* Do onetime init.
226 */
227 init_lighting();
228
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 );
244 #endif
245 gl_vector1ui_alloc( &store->LitIndex[0], 0, size, 32 );
246 gl_vector1ui_alloc( &store->LitIndex[1], 0, size, 32 );
247
248 /* Now validate the stage derived data...
249 */
250 stage->run = run_validate_lighting;
251 return stage->run( ctx, stage );
252 }
253
254
255
256 /*
257 * Check if lighting is enabled. If so, configure the pipeline stage's
258 * type, inputs, and outputs.
259 */
260 static void check_lighting( GLcontext *ctx, struct gl_pipeline_stage *stage )
261 {
262 stage->active = ctx->Light.Enabled;
263 if (stage->active) {
264 if (stage->private)
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;
271
272 stage->outputs = VERT_RGBA;
273 if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
274 stage->outputs |= VERT_SPEC_RGB;
275 }
276 }
277
278
279 static void dtr( struct gl_pipeline_stage *stage )
280 {
281 struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
282
283 if (store) {
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] );
299 #endif
300 gl_vector1ui_free( &store->LitIndex[0] );
301 gl_vector1ui_free( &store->LitIndex[1] );
302 FREE( store );
303 stage->private = 0;
304 }
305 }
306
307 const struct gl_pipeline_stage _tnl_lighting_stage =
308 {
309 "lighting",
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 */
316 dtr, /* destroy */
317 check_lighting, /* check */
318 run_init_lighting /* run -- initially set to ctr */
319 };
320