mesa: move max texture image unit constants to gl_program_constants
[mesa.git] / src / mesa / tnl / t_vb_light.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27
28 #include "main/glheader.h"
29 #include "main/colormac.h"
30 #include "main/light.h"
31 #include "main/macros.h"
32 #include "main/imports.h"
33 #include "main/simple_list.h"
34 #include "main/mtypes.h"
35
36 #include "math/m_translate.h"
37
38 #include "t_context.h"
39 #include "t_pipeline.h"
40 #include "tnl.h"
41
42 #define LIGHT_TWOSIDE 0x1
43 #define LIGHT_MATERIAL 0x2
44 #define MAX_LIGHT_FUNC 0x4
45
46 typedef void (*light_func)( struct gl_context *ctx,
47 struct vertex_buffer *VB,
48 struct tnl_pipeline_stage *stage,
49 GLvector4f *input );
50
51 /**
52 * Information for updating current material attributes from vertex color,
53 * for GL_COLOR_MATERIAL.
54 */
55 struct material_cursor {
56 const GLfloat *ptr; /* points to src vertex color (in VB array) */
57 GLuint stride; /* stride to next vertex color (bytes) */
58 GLfloat *current; /* points to material attribute to update */
59 GLuint size; /* vertex/color size: 1, 2, 3 or 4 */
60 };
61
62 /**
63 * Data private to this pipeline stage.
64 */
65 struct light_stage_data {
66 GLvector4f Input;
67 GLvector4f LitColor[2];
68 GLvector4f LitSecondary[2];
69 light_func *light_func_tab;
70
71 struct material_cursor mat[MAT_ATTRIB_MAX];
72 GLuint mat_count;
73 GLuint mat_bitmask;
74 };
75
76
77 #define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->privatePtr))
78
79
80
81 /**********************************************************************/
82 /***** Lighting computation *****/
83 /**********************************************************************/
84
85
86 /*
87 * Notes:
88 * When two-sided lighting is enabled we compute the color (or index)
89 * for both the front and back side of the primitive. Then, when the
90 * orientation of the facet is later learned, we can determine which
91 * color (or index) to use for rendering.
92 *
93 * KW: We now know orientation in advance and only shade for
94 * the side or sides which are actually required.
95 *
96 * Variables:
97 * n = normal vector
98 * V = vertex position
99 * P = light source position
100 * Pe = (0,0,0,1)
101 *
102 * Precomputed:
103 * IF P[3]==0 THEN
104 * // light at infinity
105 * IF local_viewer THEN
106 * _VP_inf_norm = unit vector from V to P // Precompute
107 * ELSE
108 * // eye at infinity
109 * _h_inf_norm = Normalize( VP + <0,0,1> ) // Precompute
110 * ENDIF
111 * ENDIF
112 *
113 * Functions:
114 * Normalize( v ) = normalized vector v
115 * Magnitude( v ) = length of vector v
116 */
117
118
119
120 static void
121 validate_shine_table( struct gl_context *ctx, GLuint side, GLfloat shininess )
122 {
123 TNLcontext *tnl = TNL_CONTEXT(ctx);
124 struct tnl_shine_tab *list = tnl->_ShineTabList;
125 struct tnl_shine_tab *s;
126
127 ASSERT(side < 2);
128
129 foreach(s, list)
130 if ( s->shininess == shininess )
131 break;
132
133 if (s == list) {
134 GLint j;
135 GLfloat *m;
136
137 foreach(s, list)
138 if (s->refcount == 0)
139 break;
140
141 m = s->tab;
142 m[0] = 0.0;
143 if (shininess == 0.0) {
144 for (j = 1 ; j <= SHINE_TABLE_SIZE ; j++)
145 m[j] = 1.0;
146 }
147 else {
148 for (j = 1 ; j < SHINE_TABLE_SIZE ; j++) {
149 GLdouble t, x = j / (GLfloat) (SHINE_TABLE_SIZE - 1);
150 if (x < 0.005) /* underflow check */
151 x = 0.005;
152 t = pow(x, shininess);
153 if (t > 1e-20)
154 m[j] = (GLfloat) t;
155 else
156 m[j] = 0.0;
157 }
158 m[SHINE_TABLE_SIZE] = 1.0;
159 }
160
161 s->shininess = shininess;
162 }
163
164 if (tnl->_ShineTable[side])
165 tnl->_ShineTable[side]->refcount--;
166
167 tnl->_ShineTable[side] = s;
168 move_to_tail( list, s );
169 s->refcount++;
170 }
171
172
173 void
174 _tnl_validate_shine_tables( struct gl_context *ctx )
175 {
176 TNLcontext *tnl = TNL_CONTEXT(ctx);
177 GLfloat shininess;
178
179 shininess = ctx->Light.Material.Attrib[MAT_ATTRIB_FRONT_SHININESS][0];
180 if (!tnl->_ShineTable[0] || tnl->_ShineTable[0]->shininess != shininess)
181 validate_shine_table( ctx, 0, shininess );
182
183 shininess = ctx->Light.Material.Attrib[MAT_ATTRIB_BACK_SHININESS][0];
184 if (!tnl->_ShineTable[1] || tnl->_ShineTable[1]->shininess != shininess)
185 validate_shine_table( ctx, 1, shininess );
186 }
187
188
189 /**
190 * In the case of colormaterial, the effected material attributes
191 * should already have been bound to point to the incoming color data,
192 * prior to running the pipeline.
193 * This function copies the vertex's color to the material attributes
194 * which are tracking glColor.
195 * It's called per-vertex in the lighting loop.
196 */
197 static void
198 update_materials(struct gl_context *ctx, struct light_stage_data *store)
199 {
200 GLuint i;
201
202 for (i = 0 ; i < store->mat_count ; i++) {
203 /* update the material */
204 COPY_CLEAN_4V(store->mat[i].current, store->mat[i].size, store->mat[i].ptr);
205 /* increment src vertex color pointer */
206 STRIDE_F(store->mat[i].ptr, store->mat[i].stride);
207 }
208
209 /* recompute derived light/material values */
210 _mesa_update_material( ctx, store->mat_bitmask );
211 /* XXX we should only call this if we're tracking/changing the specular
212 * exponent.
213 */
214 _tnl_validate_shine_tables( ctx );
215 }
216
217
218 /**
219 * Prepare things prior to running the lighting stage.
220 * Return number of material attributes which will track vertex color.
221 */
222 static GLuint
223 prepare_materials(struct gl_context *ctx,
224 struct vertex_buffer *VB, struct light_stage_data *store)
225 {
226 GLuint i;
227
228 store->mat_count = 0;
229 store->mat_bitmask = 0;
230
231 /* Examine the _ColorMaterialBitmask to determine which materials
232 * track vertex color. Override the material attribute's pointer
233 * with the color pointer for each one.
234 */
235 if (ctx->Light.ColorMaterialEnabled) {
236 const GLuint bitmask = ctx->Light._ColorMaterialBitmask;
237 for (i = 0 ; i < MAT_ATTRIB_MAX ; i++)
238 if (bitmask & (1<<i))
239 VB->AttribPtr[_TNL_ATTRIB_MAT_FRONT_AMBIENT + i] = VB->AttribPtr[_TNL_ATTRIB_COLOR0];
240 }
241
242 /* Now, for each material attribute that's tracking vertex color, save
243 * some values (ptr, stride, size, current) that we'll need in
244 * update_materials(), above, that'll actually copy the vertex color to
245 * the material attribute(s).
246 */
247 for (i = _TNL_FIRST_MAT; i <= _TNL_LAST_MAT; i++) {
248 if (VB->AttribPtr[i]->stride) {
249 const GLuint j = store->mat_count++;
250 const GLuint attr = i - _TNL_ATTRIB_MAT_FRONT_AMBIENT;
251 store->mat[j].ptr = VB->AttribPtr[i]->start;
252 store->mat[j].stride = VB->AttribPtr[i]->stride;
253 store->mat[j].size = VB->AttribPtr[i]->size;
254 store->mat[j].current = ctx->Light.Material.Attrib[attr];
255 store->mat_bitmask |= (1<<attr);
256 }
257 }
258
259 /* FIXME: Is this already done?
260 */
261 _mesa_update_material( ctx, ~0 );
262
263 _tnl_validate_shine_tables( ctx );
264
265 return store->mat_count;
266 }
267
268 /*
269 * Compute dp ^ SpecularExponent.
270 * Lerp between adjacent values in the f(x) lookup table, giving a
271 * continuous function, with adequate overall accuracy. (Though still
272 * pretty good compared to a straight lookup).
273 */
274 static inline GLfloat
275 lookup_shininess(const struct gl_context *ctx, GLuint face, GLfloat dp)
276 {
277 TNLcontext *tnl = TNL_CONTEXT(ctx);
278 const struct tnl_shine_tab *tab = tnl->_ShineTable[face];
279 float f = dp * (SHINE_TABLE_SIZE - 1);
280 int k = (int) f;
281 if (k < 0 /* gcc may cast an overflow float value to negative int value */
282 || k > SHINE_TABLE_SIZE - 2)
283 return powf(dp, tab->shininess);
284 else
285 return tab->tab[k] + (f - k) * (tab->tab[k+1] - tab->tab[k]);
286 }
287
288 /* Tables for all the shading functions.
289 */
290 static light_func _tnl_light_tab[MAX_LIGHT_FUNC];
291 static light_func _tnl_light_fast_tab[MAX_LIGHT_FUNC];
292 static light_func _tnl_light_fast_single_tab[MAX_LIGHT_FUNC];
293 static light_func _tnl_light_spec_tab[MAX_LIGHT_FUNC];
294
295 #define TAG(x) x
296 #define IDX (0)
297 #include "t_vb_lighttmp.h"
298
299 #define TAG(x) x##_twoside
300 #define IDX (LIGHT_TWOSIDE)
301 #include "t_vb_lighttmp.h"
302
303 #define TAG(x) x##_material
304 #define IDX (LIGHT_MATERIAL)
305 #include "t_vb_lighttmp.h"
306
307 #define TAG(x) x##_twoside_material
308 #define IDX (LIGHT_TWOSIDE|LIGHT_MATERIAL)
309 #include "t_vb_lighttmp.h"
310
311
312 static void init_lighting_tables( void )
313 {
314 static int done;
315
316 if (!done) {
317 init_light_tab();
318 init_light_tab_twoside();
319 init_light_tab_material();
320 init_light_tab_twoside_material();
321 done = 1;
322 }
323 }
324
325
326 static GLboolean run_lighting( struct gl_context *ctx,
327 struct tnl_pipeline_stage *stage )
328 {
329 struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
330 TNLcontext *tnl = TNL_CONTEXT(ctx);
331 struct vertex_buffer *VB = &tnl->vb;
332 GLvector4f *input = ctx->_NeedEyeCoords ? VB->EyePtr : VB->AttribPtr[_TNL_ATTRIB_POS];
333 GLuint idx;
334
335 if (!ctx->Light.Enabled || ctx->VertexProgram._Current)
336 return GL_TRUE;
337
338 /* Make sure we can talk about position x,y and z:
339 */
340 if (input->size <= 2 && input == VB->AttribPtr[_TNL_ATTRIB_POS]) {
341
342 _math_trans_4f( store->Input.data,
343 VB->AttribPtr[_TNL_ATTRIB_POS]->data,
344 VB->AttribPtr[_TNL_ATTRIB_POS]->stride,
345 GL_FLOAT,
346 VB->AttribPtr[_TNL_ATTRIB_POS]->size,
347 0,
348 VB->Count );
349
350 if (input->size <= 2) {
351 /* Clean z.
352 */
353 _mesa_vector4f_clean_elem(&store->Input, VB->Count, 2);
354 }
355
356 if (input->size <= 1) {
357 /* Clean y.
358 */
359 _mesa_vector4f_clean_elem(&store->Input, VB->Count, 1);
360 }
361
362 input = &store->Input;
363 }
364
365 idx = 0;
366
367 if (prepare_materials( ctx, VB, store ))
368 idx |= LIGHT_MATERIAL;
369
370 if (ctx->Light.Model.TwoSide)
371 idx |= LIGHT_TWOSIDE;
372
373 /* The individual functions know about replaying side-effects
374 * vs. full re-execution.
375 */
376 store->light_func_tab[idx]( ctx, VB, stage, input );
377
378 return GL_TRUE;
379 }
380
381
382 /* Called in place of do_lighting when the light table may have changed.
383 */
384 static void validate_lighting( struct gl_context *ctx,
385 struct tnl_pipeline_stage *stage )
386 {
387 light_func *tab;
388
389 if (!ctx->Light.Enabled || ctx->VertexProgram._Current)
390 return;
391
392 if (ctx->Light._NeedVertices) {
393 if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
394 tab = _tnl_light_spec_tab;
395 else
396 tab = _tnl_light_tab;
397 }
398 else {
399 if (ctx->Light.EnabledList.next == ctx->Light.EnabledList.prev)
400 tab = _tnl_light_fast_single_tab;
401 else
402 tab = _tnl_light_fast_tab;
403 }
404
405
406 LIGHT_STAGE_DATA(stage)->light_func_tab = tab;
407
408 /* This and the above should only be done on _NEW_LIGHT:
409 */
410 TNL_CONTEXT(ctx)->Driver.NotifyMaterialChange( ctx );
411 }
412
413
414
415 /* Called the first time stage->run is called. In effect, don't
416 * allocate data until the first time the stage is run.
417 */
418 static GLboolean init_lighting( struct gl_context *ctx,
419 struct tnl_pipeline_stage *stage )
420 {
421 TNLcontext *tnl = TNL_CONTEXT(ctx);
422 struct light_stage_data *store;
423 GLuint size = tnl->vb.Size;
424
425 stage->privatePtr = malloc(sizeof(*store));
426 store = LIGHT_STAGE_DATA(stage);
427 if (!store)
428 return GL_FALSE;
429
430 /* Do onetime init.
431 */
432 init_lighting_tables();
433
434 _mesa_vector4f_alloc( &store->Input, 0, size, 32 );
435 _mesa_vector4f_alloc( &store->LitColor[0], 0, size, 32 );
436 _mesa_vector4f_alloc( &store->LitColor[1], 0, size, 32 );
437 _mesa_vector4f_alloc( &store->LitSecondary[0], 0, size, 32 );
438 _mesa_vector4f_alloc( &store->LitSecondary[1], 0, size, 32 );
439
440 store->LitColor[0].size = 4;
441 store->LitColor[1].size = 4;
442 store->LitSecondary[0].size = 3;
443 store->LitSecondary[1].size = 3;
444
445 return GL_TRUE;
446 }
447
448
449
450
451 static void dtr( struct tnl_pipeline_stage *stage )
452 {
453 struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
454
455 if (store) {
456 _mesa_vector4f_free( &store->Input );
457 _mesa_vector4f_free( &store->LitColor[0] );
458 _mesa_vector4f_free( &store->LitColor[1] );
459 _mesa_vector4f_free( &store->LitSecondary[0] );
460 _mesa_vector4f_free( &store->LitSecondary[1] );
461 free( store );
462 stage->privatePtr = NULL;
463 }
464 }
465
466 const struct tnl_pipeline_stage _tnl_lighting_stage =
467 {
468 "lighting", /* name */
469 NULL, /* private_data */
470 init_lighting,
471 dtr, /* destroy */
472 validate_lighting,
473 run_lighting
474 };