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 * 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.
32 #include "simple_list.h"
35 #include "math/m_translate.h"
37 #include "t_context.h"
38 #include "t_pipeline.h"
40 #define LIGHT_TWOSIDE 0x1
41 #define LIGHT_MATERIAL 0x2
42 #define MAX_LIGHT_FUNC 0x4
44 typedef void (*light_func
)( GLcontext
*ctx
,
45 struct vertex_buffer
*VB
,
46 struct tnl_pipeline_stage
*stage
,
50 * Information for updating current material attributes from vertex color,
51 * for GL_COLOR_MATERIAL.
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 */
61 * Data private to this pipeline stage.
63 struct light_stage_data
{
65 GLvector4f LitColor
[2];
66 GLvector4f LitSecondary
[2];
67 GLvector4f LitIndex
[2];
68 light_func
*light_func_tab
;
70 struct material_cursor mat
[MAT_ATTRIB_MAX
];
76 #define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->privatePtr))
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.
89 update_materials(GLcontext
*ctx
, struct light_stage_data
*store
)
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
);
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
105 _mesa_validate_all_lighting_tables( ctx
);
110 * Prepare things prior to running the lighting stage.
111 * Return number of material attributes which will track vertex color.
114 prepare_materials(GLcontext
*ctx
,
115 struct vertex_buffer
*VB
, struct light_stage_data
*store
)
119 store
->mat_count
= 0;
120 store
->mat_bitmask
= 0;
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.
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];
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).
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
);
150 /* FIXME: Is this already done?
152 _mesa_update_material( ctx
, ~0 );
153 _mesa_validate_all_lighting_tables( ctx
);
155 return store
->mat_count
;
158 /* Tables for all the shading functions.
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
];
168 #include "t_vb_lighttmp.h"
170 #define TAG(x) x##_twoside
171 #define IDX (LIGHT_TWOSIDE)
172 #include "t_vb_lighttmp.h"
174 #define TAG(x) x##_material
175 #define IDX (LIGHT_MATERIAL)
176 #include "t_vb_lighttmp.h"
178 #define TAG(x) x##_twoside_material
179 #define IDX (LIGHT_TWOSIDE|LIGHT_MATERIAL)
180 #include "t_vb_lighttmp.h"
183 static void init_lighting_tables( void )
189 init_light_tab_twoside();
190 init_light_tab_material();
191 init_light_tab_twoside_material();
197 static GLboolean
run_lighting( GLcontext
*ctx
,
198 struct tnl_pipeline_stage
*stage
)
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
;
206 if (!ctx
->Light
.Enabled
|| ctx
->VertexProgram
._Current
)
209 /* Make sure we can talk about position x,y and z:
211 if (input
->size
<= 2 && input
== VB
->ObjPtr
) {
213 _math_trans_4f( store
->Input
.data
,
221 if (input
->size
<= 2) {
224 _mesa_vector4f_clean_elem(&store
->Input
, VB
->Count
, 2);
227 if (input
->size
<= 1) {
230 _mesa_vector4f_clean_elem(&store
->Input
, VB
->Count
, 1);
233 input
= &store
->Input
;
238 if (prepare_materials( ctx
, VB
, store
))
239 idx
|= LIGHT_MATERIAL
;
241 if (ctx
->Light
.Model
.TwoSide
)
242 idx
|= LIGHT_TWOSIDE
;
244 /* The individual functions know about replaying side-effects
245 * vs. full re-execution.
247 store
->light_func_tab
[idx
]( ctx
, VB
, stage
, input
);
249 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = VB
->ColorPtr
[0];
250 VB
->AttribPtr
[_TNL_ATTRIB_COLOR1
] = VB
->SecondaryColorPtr
[0];
251 VB
->AttribPtr
[_TNL_ATTRIB_COLOR_INDEX
] = VB
->IndexPtr
[0];
257 /* Called in place of do_lighting when the light table may have changed.
259 static void validate_lighting( GLcontext
*ctx
,
260 struct tnl_pipeline_stage
*stage
)
264 if (!ctx
->Light
.Enabled
|| ctx
->VertexProgram
._Current
)
267 if (ctx
->Visual
.rgbMode
) {
268 if (ctx
->Light
._NeedVertices
) {
269 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
270 tab
= _tnl_light_spec_tab
;
272 tab
= _tnl_light_tab
;
275 if (ctx
->Light
.EnabledList
.next
== ctx
->Light
.EnabledList
.prev
)
276 tab
= _tnl_light_fast_single_tab
;
278 tab
= _tnl_light_fast_tab
;
282 tab
= _tnl_light_ci_tab
;
285 LIGHT_STAGE_DATA(stage
)->light_func_tab
= tab
;
287 /* This and the above should only be done on _NEW_LIGHT:
289 TNL_CONTEXT(ctx
)->Driver
.NotifyMaterialChange( ctx
);
294 /* Called the first time stage->run is called. In effect, don't
295 * allocate data until the first time the stage is run.
297 static GLboolean
init_lighting( GLcontext
*ctx
,
298 struct tnl_pipeline_stage
*stage
)
300 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
301 struct light_stage_data
*store
;
302 GLuint size
= tnl
->vb
.Size
;
304 stage
->privatePtr
= MALLOC(sizeof(*store
));
305 store
= LIGHT_STAGE_DATA(stage
);
311 init_lighting_tables();
313 _mesa_vector4f_alloc( &store
->Input
, 0, size
, 32 );
314 _mesa_vector4f_alloc( &store
->LitColor
[0], 0, size
, 32 );
315 _mesa_vector4f_alloc( &store
->LitColor
[1], 0, size
, 32 );
316 _mesa_vector4f_alloc( &store
->LitSecondary
[0], 0, size
, 32 );
317 _mesa_vector4f_alloc( &store
->LitSecondary
[1], 0, size
, 32 );
318 _mesa_vector4f_alloc( &store
->LitIndex
[0], 0, size
, 32 );
319 _mesa_vector4f_alloc( &store
->LitIndex
[1], 0, size
, 32 );
321 store
->LitColor
[0].size
= 4;
322 store
->LitColor
[1].size
= 4;
323 store
->LitSecondary
[0].size
= 3;
324 store
->LitSecondary
[1].size
= 3;
326 store
->LitIndex
[0].size
= 1;
327 store
->LitIndex
[0].stride
= sizeof(GLfloat
);
328 store
->LitIndex
[1].size
= 1;
329 store
->LitIndex
[1].stride
= sizeof(GLfloat
);
337 static void dtr( struct tnl_pipeline_stage
*stage
)
339 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
342 _mesa_vector4f_free( &store
->Input
);
343 _mesa_vector4f_free( &store
->LitColor
[0] );
344 _mesa_vector4f_free( &store
->LitColor
[1] );
345 _mesa_vector4f_free( &store
->LitSecondary
[0] );
346 _mesa_vector4f_free( &store
->LitSecondary
[1] );
347 _mesa_vector4f_free( &store
->LitIndex
[0] );
348 _mesa_vector4f_free( &store
->LitIndex
[1] );
350 stage
->privatePtr
= NULL
;
354 const struct tnl_pipeline_stage _tnl_lighting_stage
=
356 "lighting", /* name */
357 NULL
, /* private_data */