Merge branch 'master' into glsl-pp-rework-2
[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 "main/glheader.h"
28 #include "main/colormac.h"
29 #include "main/light.h"
30 #include "main/macros.h"
31 #include "main/imports.h"
32 #include "main/simple_list.h"
33 #include "main/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->AttribPtr[_TNL_ATTRIB_COLOR0];
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_FIRST_MAT; i <= _TNL_LAST_MAT; 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->AttribPtr[_TNL_ATTRIB_POS];
204 GLuint idx;
205
206 if (!ctx->Light.Enabled || ctx->VertexProgram._Current)
207 return GL_TRUE;
208
209 /* Make sure we can talk about position x,y and z:
210 */
211 if (input->size <= 2 && input == VB->AttribPtr[_TNL_ATTRIB_POS]) {
212
213 _math_trans_4f( store->Input.data,
214 VB->AttribPtr[_TNL_ATTRIB_POS]->data,
215 VB->AttribPtr[_TNL_ATTRIB_POS]->stride,
216 GL_FLOAT,
217 VB->AttribPtr[_TNL_ATTRIB_POS]->size,
218 0,
219 VB->Count );
220
221 if (input->size <= 2) {
222 /* Clean z.
223 */
224 _mesa_vector4f_clean_elem(&store->Input, VB->Count, 2);
225 }
226
227 if (input->size <= 1) {
228 /* Clean y.
229 */
230 _mesa_vector4f_clean_elem(&store->Input, VB->Count, 1);
231 }
232
233 input = &store->Input;
234 }
235
236 idx = 0;
237
238 if (prepare_materials( ctx, VB, store ))
239 idx |= LIGHT_MATERIAL;
240
241 if (ctx->Light.Model.TwoSide)
242 idx |= LIGHT_TWOSIDE;
243
244 /* The individual functions know about replaying side-effects
245 * vs. full re-execution.
246 */
247 store->light_func_tab[idx]( ctx, VB, stage, input );
248
249 return GL_TRUE;
250 }
251
252
253 /* Called in place of do_lighting when the light table may have changed.
254 */
255 static void validate_lighting( GLcontext *ctx,
256 struct tnl_pipeline_stage *stage )
257 {
258 light_func *tab;
259
260 if (!ctx->Light.Enabled || ctx->VertexProgram._Current)
261 return;
262
263 if (ctx->Visual.rgbMode) {
264 if (ctx->Light._NeedVertices) {
265 if (ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)
266 tab = _tnl_light_spec_tab;
267 else
268 tab = _tnl_light_tab;
269 }
270 else {
271 if (ctx->Light.EnabledList.next == ctx->Light.EnabledList.prev)
272 tab = _tnl_light_fast_single_tab;
273 else
274 tab = _tnl_light_fast_tab;
275 }
276 }
277 else
278 tab = _tnl_light_ci_tab;
279
280
281 LIGHT_STAGE_DATA(stage)->light_func_tab = tab;
282
283 /* This and the above should only be done on _NEW_LIGHT:
284 */
285 TNL_CONTEXT(ctx)->Driver.NotifyMaterialChange( ctx );
286 }
287
288
289
290 /* Called the first time stage->run is called. In effect, don't
291 * allocate data until the first time the stage is run.
292 */
293 static GLboolean init_lighting( GLcontext *ctx,
294 struct tnl_pipeline_stage *stage )
295 {
296 TNLcontext *tnl = TNL_CONTEXT(ctx);
297 struct light_stage_data *store;
298 GLuint size = tnl->vb.Size;
299
300 stage->privatePtr = MALLOC(sizeof(*store));
301 store = LIGHT_STAGE_DATA(stage);
302 if (!store)
303 return GL_FALSE;
304
305 /* Do onetime init.
306 */
307 init_lighting_tables();
308
309 _mesa_vector4f_alloc( &store->Input, 0, size, 32 );
310 _mesa_vector4f_alloc( &store->LitColor[0], 0, size, 32 );
311 _mesa_vector4f_alloc( &store->LitColor[1], 0, size, 32 );
312 _mesa_vector4f_alloc( &store->LitSecondary[0], 0, size, 32 );
313 _mesa_vector4f_alloc( &store->LitSecondary[1], 0, size, 32 );
314 _mesa_vector4f_alloc( &store->LitIndex[0], 0, size, 32 );
315 _mesa_vector4f_alloc( &store->LitIndex[1], 0, size, 32 );
316
317 store->LitColor[0].size = 4;
318 store->LitColor[1].size = 4;
319 store->LitSecondary[0].size = 3;
320 store->LitSecondary[1].size = 3;
321
322 store->LitIndex[0].size = 1;
323 store->LitIndex[0].stride = sizeof(GLfloat);
324 store->LitIndex[1].size = 1;
325 store->LitIndex[1].stride = sizeof(GLfloat);
326
327 return GL_TRUE;
328 }
329
330
331
332
333 static void dtr( struct tnl_pipeline_stage *stage )
334 {
335 struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
336
337 if (store) {
338 _mesa_vector4f_free( &store->Input );
339 _mesa_vector4f_free( &store->LitColor[0] );
340 _mesa_vector4f_free( &store->LitColor[1] );
341 _mesa_vector4f_free( &store->LitSecondary[0] );
342 _mesa_vector4f_free( &store->LitSecondary[1] );
343 _mesa_vector4f_free( &store->LitIndex[0] );
344 _mesa_vector4f_free( &store->LitIndex[1] );
345 FREE( store );
346 stage->privatePtr = NULL;
347 }
348 }
349
350 const struct tnl_pipeline_stage _tnl_lighting_stage =
351 {
352 "lighting", /* name */
353 NULL, /* private_data */
354 init_lighting,
355 dtr, /* destroy */
356 validate_lighting,
357 run_lighting
358 };