updated some printfs, added comment about sched_yield
[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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26
27 #include "glheader.h"
28 #include "colormac.h"
29 #include "light.h"
30 #include "macros.h"
31 #include "imports.h"
32 #include "simple_list.h"
33 #include "mtypes.h"
34
35 #include "math/m_translate.h"
36
37 #include "t_context.h"
38 #include "t_pipeline.h"
39
40 #define LIGHT_TWOSIDE 0x1
41 #define LIGHT_MATERIAL 0x2
42 #define MAX_LIGHT_FUNC 0x4
43
44 typedef void (*light_func)( GLcontext *ctx,
45 struct vertex_buffer *VB,
46 struct tnl_pipeline_stage *stage,
47 GLvector4f *input );
48
49 /**
50 * Information for updating current material attributes from vertex color,
51 * for GL_COLOR_MATERIAL.
52 */
53 struct material_cursor {
54 const GLfloat *ptr; /* points to src vertex color (in VB array) */
55 GLuint stride; /* stride to next vertex color (bytes) */
56 GLfloat *current; /* points to material attribute to update */
57 GLuint size; /* vertex/color size: 1, 2, 3 or 4 */
58 };
59
60 /**
61 * Data private to this pipeline stage.
62 */
63 struct light_stage_data {
64 GLvector4f Input;
65 GLvector4f LitColor[2];
66 GLvector4f LitSecondary[2];
67 GLvector4f LitIndex[2];
68 light_func *light_func_tab;
69
70 struct material_cursor mat[MAT_ATTRIB_MAX];
71 GLuint mat_count;
72 GLuint mat_bitmask;
73 };
74
75
76 #define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->privatePtr))
77
78
79
80 /**
81 * In the case of colormaterial, the effected material attributes
82 * should already have been bound to point to the incoming color data,
83 * prior to running the pipeline.
84 * This function copies the vertex's color to the material attributes
85 * which are tracking glColor.
86 * It's called per-vertex in the lighting loop.
87 */
88 static void
89 update_materials(GLcontext *ctx, struct light_stage_data *store)
90 {
91 GLuint i;
92
93 for (i = 0 ; i < store->mat_count ; i++) {
94 /* update the material */
95 COPY_CLEAN_4V(store->mat[i].current, store->mat[i].size, store->mat[i].ptr);
96 /* increment src vertex color pointer */
97 STRIDE_F(store->mat[i].ptr, store->mat[i].stride);
98 }
99
100 /* recompute derived light/material values */
101 _mesa_update_material( ctx, store->mat_bitmask );
102 /* XXX we should only call this if we're tracking/changing the specular
103 * exponent.
104 */
105 _mesa_validate_all_lighting_tables( ctx );
106 }
107
108
109 /**
110 * Prepare things prior to running the lighting stage.
111 * Return number of material attributes which will track vertex color.
112 */
113 static GLuint
114 prepare_materials(GLcontext *ctx,
115 struct vertex_buffer *VB, struct light_stage_data *store)
116 {
117 GLuint i;
118
119 store->mat_count = 0;
120 store->mat_bitmask = 0;
121
122 /* Examine the ColorMaterialBitmask to determine which materials
123 * track vertex color. Override the material attribute's pointer
124 * with the color pointer for each one.
125 */
126 if (ctx->Light.ColorMaterialEnabled) {
127 const GLuint bitmask = ctx->Light.ColorMaterialBitmask;
128 for (i = 0 ; i < MAT_ATTRIB_MAX ; i++)
129 if (bitmask & (1<<i))
130 VB->AttribPtr[_TNL_ATTRIB_MAT_FRONT_AMBIENT + i] = VB->ColorPtr[0];
131 }
132
133 /* Now, for each material attribute that's tracking vertex color, save
134 * some values (ptr, stride, size, current) that we'll need in
135 * update_materials(), above, that'll actually copy the vertex color to
136 * the material attribute(s).
137 */
138 for (i = _TNL_ATTRIB_MAT_FRONT_AMBIENT ; i < _TNL_ATTRIB_INDEX ; i++) {
139 if (VB->AttribPtr[i]->stride) {
140 const GLuint j = store->mat_count++;
141 const GLuint attr = i - _TNL_ATTRIB_MAT_FRONT_AMBIENT;
142 store->mat[j].ptr = VB->AttribPtr[i]->start;
143 store->mat[j].stride = VB->AttribPtr[i]->stride;
144 store->mat[j].size = VB->AttribPtr[i]->size;
145 store->mat[j].current = ctx->Light.Material.Attrib[attr];
146 store->mat_bitmask |= (1<<attr);
147 }
148 }
149
150 /* FIXME: Is this already done?
151 */
152 _mesa_update_material( ctx, ~0 );
153 _mesa_validate_all_lighting_tables( ctx );
154
155 return store->mat_count;
156 }
157
158 /* Tables for all the shading functions.
159 */
160 static light_func _tnl_light_tab[MAX_LIGHT_FUNC];
161 static light_func _tnl_light_fast_tab[MAX_LIGHT_FUNC];
162 static light_func _tnl_light_fast_single_tab[MAX_LIGHT_FUNC];
163 static light_func _tnl_light_spec_tab[MAX_LIGHT_FUNC];
164 static light_func _tnl_light_ci_tab[MAX_LIGHT_FUNC];
165
166 #define TAG(x) x
167 #define IDX (0)
168 #include "t_vb_lighttmp.h"
169
170 #define TAG(x) x##_twoside
171 #define IDX (LIGHT_TWOSIDE)
172 #include "t_vb_lighttmp.h"
173
174 #define TAG(x) x##_material
175 #define IDX (LIGHT_MATERIAL)
176 #include "t_vb_lighttmp.h"
177
178 #define TAG(x) x##_twoside_material
179 #define IDX (LIGHT_TWOSIDE|LIGHT_MATERIAL)
180 #include "t_vb_lighttmp.h"
181
182
183 static void init_lighting_tables( void )
184 {
185 static int done;
186
187 if (!done) {
188 init_light_tab();
189 init_light_tab_twoside();
190 init_light_tab_material();
191 init_light_tab_twoside_material();
192 done = 1;
193 }
194 }
195
196
197 static GLboolean run_lighting( GLcontext *ctx,
198 struct tnl_pipeline_stage *stage )
199 {
200 struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
201 TNLcontext *tnl = TNL_CONTEXT(ctx);
202 struct vertex_buffer *VB = &tnl->vb;
203 GLvector4f *input = ctx->_NeedEyeCoords ? VB->EyePtr : VB->ObjPtr;
204 GLuint idx;
205
206 if (ctx->ShaderObjects.CurrentProgram != NULL)
207 return GL_TRUE;
208
209 if (!ctx->Light.Enabled || ctx->VertexProgram._Enabled)
210 return GL_TRUE;
211
212 /* Make sure we can talk about position x,y and z:
213 */
214 if (input->size <= 2 && input == VB->ObjPtr) {
215
216 _math_trans_4f( store->Input.data,
217 VB->ObjPtr->data,
218 VB->ObjPtr->stride,
219 GL_FLOAT,
220 VB->ObjPtr->size,
221 0,
222 VB->Count );
223
224 if (input->size <= 2) {
225 /* Clean z.
226 */
227 _mesa_vector4f_clean_elem(&store->Input, VB->Count, 2);
228 }
229
230 if (input->size <= 1) {
231 /* Clean y.
232 */
233 _mesa_vector4f_clean_elem(&store->Input, VB->Count, 1);
234 }
235
236 input = &store->Input;
237 }
238
239 idx = 0;
240
241 if (prepare_materials( ctx, VB, store ))
242 idx |= LIGHT_MATERIAL;
243
244 if (ctx->Light.Model.TwoSide)
245 idx |= LIGHT_TWOSIDE;
246
247 /* The individual functions know about replaying side-effects
248 * vs. full re-execution.
249 */
250 store->light_func_tab[idx]( ctx, VB, stage, input );
251
252 VB->AttribPtr[_TNL_ATTRIB_COLOR0] = VB->ColorPtr[0];
253 VB->AttribPtr[_TNL_ATTRIB_COLOR1] = VB->SecondaryColorPtr[0];
254 VB->AttribPtr[_TNL_ATTRIB_INDEX] = VB->IndexPtr[0];
255
256 return GL_TRUE;
257 }
258
259
260 /* Called in place of do_lighting when the light table may have changed.
261 */
262 static void validate_lighting( GLcontext *ctx,
263 struct tnl_pipeline_stage *stage )
264 {
265 light_func *tab;
266
267 if (ctx->ShaderObjects.CurrentProgram != NULL)
268 return;
269
270 if (!ctx->Light.Enabled || ctx->VertexProgram._Enabled)
271 return;
272
273 if (ctx->Visual.rgbMode) {
274 if (ctx->Light._NeedVertices) {
275 if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
276 tab = _tnl_light_spec_tab;
277 else
278 tab = _tnl_light_tab;
279 }
280 else {
281 if (ctx->Light.EnabledList.next == ctx->Light.EnabledList.prev)
282 tab = _tnl_light_fast_single_tab;
283 else
284 tab = _tnl_light_fast_tab;
285 }
286 }
287 else
288 tab = _tnl_light_ci_tab;
289
290
291 LIGHT_STAGE_DATA(stage)->light_func_tab = tab;
292
293 /* This and the above should only be done on _NEW_LIGHT:
294 */
295 TNL_CONTEXT(ctx)->Driver.NotifyMaterialChange( ctx );
296 }
297
298
299
300 /* Called the first time stage->run is called. In effect, don't
301 * allocate data until the first time the stage is run.
302 */
303 static GLboolean init_lighting( GLcontext *ctx,
304 struct tnl_pipeline_stage *stage )
305 {
306 TNLcontext *tnl = TNL_CONTEXT(ctx);
307 struct light_stage_data *store;
308 GLuint size = tnl->vb.Size;
309
310 stage->privatePtr = MALLOC(sizeof(*store));
311 store = LIGHT_STAGE_DATA(stage);
312 if (!store)
313 return GL_FALSE;
314
315 /* Do onetime init.
316 */
317 init_lighting_tables();
318
319 _mesa_vector4f_alloc( &store->Input, 0, size, 32 );
320 _mesa_vector4f_alloc( &store->LitColor[0], 0, size, 32 );
321 _mesa_vector4f_alloc( &store->LitColor[1], 0, size, 32 );
322 _mesa_vector4f_alloc( &store->LitSecondary[0], 0, size, 32 );
323 _mesa_vector4f_alloc( &store->LitSecondary[1], 0, size, 32 );
324 _mesa_vector4f_alloc( &store->LitIndex[0], 0, size, 32 );
325 _mesa_vector4f_alloc( &store->LitIndex[1], 0, size, 32 );
326
327 store->LitColor[0].size = 4;
328 store->LitColor[1].size = 4;
329 store->LitSecondary[0].size = 3;
330 store->LitSecondary[1].size = 3;
331
332 store->LitIndex[0].size = 1;
333 store->LitIndex[0].stride = sizeof(GLfloat);
334 store->LitIndex[1].size = 1;
335 store->LitIndex[1].stride = sizeof(GLfloat);
336
337 return GL_TRUE;
338 }
339
340
341
342
343 static void dtr( struct tnl_pipeline_stage *stage )
344 {
345 struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
346
347 if (store) {
348 _mesa_vector4f_free( &store->Input );
349 _mesa_vector4f_free( &store->LitColor[0] );
350 _mesa_vector4f_free( &store->LitColor[1] );
351 _mesa_vector4f_free( &store->LitSecondary[0] );
352 _mesa_vector4f_free( &store->LitSecondary[1] );
353 _mesa_vector4f_free( &store->LitIndex[0] );
354 _mesa_vector4f_free( &store->LitIndex[1] );
355 FREE( store );
356 stage->privatePtr = NULL;
357 }
358 }
359
360 const struct tnl_pipeline_stage _tnl_lighting_stage =
361 {
362 "lighting", /* name */
363 NULL, /* private_data */
364 init_lighting,
365 dtr, /* destroy */
366 validate_lighting,
367 run_lighting
368 };