2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
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"
36 #include "math/m_translate.h"
38 #include "t_context.h"
39 #include "t_pipeline.h"
42 #define LIGHT_TWOSIDE 0x1
43 #define LIGHT_MATERIAL 0x2
44 #define MAX_LIGHT_FUNC 0x4
46 typedef void (*light_func
)( struct gl_context
*ctx
,
47 struct vertex_buffer
*VB
,
48 struct tnl_pipeline_stage
*stage
,
52 * Information for updating current material attributes from vertex color,
53 * for GL_COLOR_MATERIAL.
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 */
63 * Data private to this pipeline stage.
65 struct light_stage_data
{
67 GLvector4f LitColor
[2];
68 GLvector4f LitSecondary
[2];
69 light_func
*light_func_tab
;
71 struct material_cursor mat
[MAT_ATTRIB_MAX
];
77 #define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->privatePtr))
81 /**********************************************************************/
82 /***** Lighting computation *****/
83 /**********************************************************************/
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.
93 * KW: We now know orientation in advance and only shade for
94 * the side or sides which are actually required.
99 * P = light source position
104 * // light at infinity
105 * IF local_viewer THEN
106 * _VP_inf_norm = unit vector from V to P // Precompute
109 * _h_inf_norm = Normalize( VP + <0,0,1> ) // Precompute
114 * Normalize( v ) = normalized vector v
115 * Magnitude( v ) = length of vector v
121 validate_shine_table( struct gl_context
*ctx
, GLuint side
, GLfloat shininess
)
123 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
124 struct tnl_shine_tab
*list
= tnl
->_ShineTabList
;
125 struct tnl_shine_tab
*s
;
130 if ( s
->shininess
== shininess
)
138 if (s
->refcount
== 0)
143 if (shininess
== 0.0) {
144 for (j
= 1 ; j
<= SHINE_TABLE_SIZE
; j
++)
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 */
152 t
= pow(x
, shininess
);
158 m
[SHINE_TABLE_SIZE
] = 1.0;
161 s
->shininess
= shininess
;
164 if (tnl
->_ShineTable
[side
])
165 tnl
->_ShineTable
[side
]->refcount
--;
167 tnl
->_ShineTable
[side
] = s
;
168 move_to_tail( list
, s
);
174 _tnl_validate_shine_tables( struct gl_context
*ctx
)
176 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
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
);
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
);
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.
198 update_materials(struct gl_context
*ctx
, struct light_stage_data
*store
)
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
);
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
214 _tnl_validate_shine_tables( ctx
);
219 * Prepare things prior to running the lighting stage.
220 * Return number of material attributes which will track vertex color.
223 prepare_materials(struct gl_context
*ctx
,
224 struct vertex_buffer
*VB
, struct light_stage_data
*store
)
228 store
->mat_count
= 0;
229 store
->mat_bitmask
= 0;
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.
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
];
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).
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
);
259 /* FIXME: Is this already done?
261 _mesa_update_material( ctx
, ~0 );
263 _tnl_validate_shine_tables( ctx
);
265 return store
->mat_count
;
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).
274 static inline GLfloat
275 lookup_shininess(const struct gl_context
*ctx
, GLuint face
, GLfloat dp
)
277 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
278 const struct tnl_shine_tab
*tab
= tnl
->_ShineTable
[face
];
279 float f
= dp
* (SHINE_TABLE_SIZE
- 1);
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
);
285 return tab
->tab
[k
] + (f
- k
) * (tab
->tab
[k
+1] - tab
->tab
[k
]);
288 /* Tables for all the shading functions.
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
];
297 #include "t_vb_lighttmp.h"
299 #define TAG(x) x##_twoside
300 #define IDX (LIGHT_TWOSIDE)
301 #include "t_vb_lighttmp.h"
303 #define TAG(x) x##_material
304 #define IDX (LIGHT_MATERIAL)
305 #include "t_vb_lighttmp.h"
307 #define TAG(x) x##_twoside_material
308 #define IDX (LIGHT_TWOSIDE|LIGHT_MATERIAL)
309 #include "t_vb_lighttmp.h"
312 static void init_lighting_tables( void )
318 init_light_tab_twoside();
319 init_light_tab_material();
320 init_light_tab_twoside_material();
326 static GLboolean
run_lighting( struct gl_context
*ctx
,
327 struct tnl_pipeline_stage
*stage
)
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
];
335 if (!ctx
->Light
.Enabled
|| ctx
->VertexProgram
._Current
)
338 /* Make sure we can talk about position x,y and z:
340 if (input
->size
<= 2 && input
== VB
->AttribPtr
[_TNL_ATTRIB_POS
]) {
342 _math_trans_4f( store
->Input
.data
,
343 VB
->AttribPtr
[_TNL_ATTRIB_POS
]->data
,
344 VB
->AttribPtr
[_TNL_ATTRIB_POS
]->stride
,
346 VB
->AttribPtr
[_TNL_ATTRIB_POS
]->size
,
350 if (input
->size
<= 2) {
353 _mesa_vector4f_clean_elem(&store
->Input
, VB
->Count
, 2);
356 if (input
->size
<= 1) {
359 _mesa_vector4f_clean_elem(&store
->Input
, VB
->Count
, 1);
362 input
= &store
->Input
;
367 if (prepare_materials( ctx
, VB
, store
))
368 idx
|= LIGHT_MATERIAL
;
370 if (ctx
->Light
.Model
.TwoSide
)
371 idx
|= LIGHT_TWOSIDE
;
373 /* The individual functions know about replaying side-effects
374 * vs. full re-execution.
376 store
->light_func_tab
[idx
]( ctx
, VB
, stage
, input
);
382 /* Called in place of do_lighting when the light table may have changed.
384 static void validate_lighting( struct gl_context
*ctx
,
385 struct tnl_pipeline_stage
*stage
)
389 if (!ctx
->Light
.Enabled
|| ctx
->VertexProgram
._Current
)
392 if (ctx
->Light
._NeedVertices
) {
393 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
394 tab
= _tnl_light_spec_tab
;
396 tab
= _tnl_light_tab
;
399 if (ctx
->Light
.EnabledList
.next
== ctx
->Light
.EnabledList
.prev
)
400 tab
= _tnl_light_fast_single_tab
;
402 tab
= _tnl_light_fast_tab
;
406 LIGHT_STAGE_DATA(stage
)->light_func_tab
= tab
;
408 /* This and the above should only be done on _NEW_LIGHT:
410 TNL_CONTEXT(ctx
)->Driver
.NotifyMaterialChange( ctx
);
415 /* Called the first time stage->run is called. In effect, don't
416 * allocate data until the first time the stage is run.
418 static GLboolean
init_lighting( struct gl_context
*ctx
,
419 struct tnl_pipeline_stage
*stage
)
421 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
422 struct light_stage_data
*store
;
423 GLuint size
= tnl
->vb
.Size
;
425 stage
->privatePtr
= malloc(sizeof(*store
));
426 store
= LIGHT_STAGE_DATA(stage
);
432 init_lighting_tables();
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 );
440 store
->LitColor
[0].size
= 4;
441 store
->LitColor
[1].size
= 4;
442 store
->LitSecondary
[0].size
= 3;
443 store
->LitSecondary
[1].size
= 3;
451 static void dtr( struct tnl_pipeline_stage
*stage
)
453 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
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] );
462 stage
->privatePtr
= NULL
;
466 const struct tnl_pipeline_stage _tnl_lighting_stage
=
468 "lighting", /* name */
469 NULL
, /* private_data */