3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 #include "simple_list.h"
36 #include "math/m_translate.h"
38 #include "t_context.h"
39 #include "t_pipeline.h"
41 #define LIGHT_FLAGS 0x1 /* must be first */
42 #define LIGHT_TWOSIDE 0x2
43 #define LIGHT_COLORMATERIAL 0x4
44 #define MAX_LIGHT_FUNC 0x8
46 typedef void (*light_func
)( GLcontext
*ctx
,
47 struct vertex_buffer
*VB
,
48 struct gl_pipeline_stage
*stage
,
51 struct light_stage_data
{
52 struct gl_client_array FloatColor
;
53 struct gl_client_array LitColor
[2];
54 struct gl_client_array LitSecondary
[2];
55 GLvector1ui LitIndex
[2];
56 light_func
*light_func_tab
;
60 #define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->privatePtr))
63 static void import_color_material( GLcontext
*ctx
,
64 struct gl_pipeline_stage
*stage
)
66 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
67 struct gl_client_array
*to
= &LIGHT_STAGE_DATA(stage
)->FloatColor
;
68 struct gl_client_array
*from
= VB
->ColorPtr
[0];
69 GLuint count
= VB
->Count
;
72 to
->Ptr
= ALIGN_MALLOC( VB
->Size
* 4 * sizeof(GLfloat
), 32 );
76 /* No need to transform the same value 3000 times.
83 to
->StrideB
= 4 * sizeof(GLfloat
);
85 _math_trans_4f( (GLfloat (*)[4]) to
->Ptr
,
97 static void update_materials( GLcontext
*ctx
,
98 const struct gl_material
*src
,
101 _mesa_copy_materials( &ctx
->Light
.Material
, src
, bitmask
);
102 _mesa_update_material( ctx
, bitmask
);
105 /* Tables for all the shading functions.
107 static light_func _tnl_light_tab
[MAX_LIGHT_FUNC
];
108 static light_func _tnl_light_fast_tab
[MAX_LIGHT_FUNC
];
109 static light_func _tnl_light_fast_single_tab
[MAX_LIGHT_FUNC
];
110 static light_func _tnl_light_spec_tab
[MAX_LIGHT_FUNC
];
111 static light_func _tnl_light_ci_tab
[MAX_LIGHT_FUNC
];
115 #include "t_vb_lighttmp.h"
117 #define TAG(x) x##_tw
118 #define IDX (LIGHT_TWOSIDE)
119 #include "t_vb_lighttmp.h"
121 #define TAG(x) x##_fl
122 #define IDX (LIGHT_FLAGS)
123 #include "t_vb_lighttmp.h"
125 #define TAG(x) x##_tw_fl
126 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE)
127 #include "t_vb_lighttmp.h"
129 #define TAG(x) x##_cm
130 #define IDX (LIGHT_COLORMATERIAL)
131 #include "t_vb_lighttmp.h"
133 #define TAG(x) x##_tw_cm
134 #define IDX (LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
135 #include "t_vb_lighttmp.h"
137 #define TAG(x) x##_fl_cm
138 #define IDX (LIGHT_FLAGS|LIGHT_COLORMATERIAL)
139 #include "t_vb_lighttmp.h"
141 #define TAG(x) x##_tw_fl_cm
142 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
143 #include "t_vb_lighttmp.h"
146 static void init_lighting( void )
154 init_light_tab_tw_fl();
156 init_light_tab_tw_cm();
157 init_light_tab_fl_cm();
158 init_light_tab_tw_fl_cm();
164 static GLboolean
run_lighting( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
166 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
167 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
168 struct vertex_buffer
*VB
= &tnl
->vb
;
169 GLvector4f
*input
= ctx
->_NeedEyeCoords
? VB
->EyePtr
: VB
->ObjPtr
;
172 /* _tnl_print_vert_flags( __FUNCTION__, stage->changed_inputs ); */
174 /* Make sure we can talk about elements 0..2 in the vector we are
177 if (stage
->changed_inputs
& (VERT_BIT_EYE
|VERT_BIT_POS
)) {
178 if (input
->size
<= 2) {
179 if (input
->flags
& VEC_NOT_WRITEABLE
) {
180 ASSERT(VB
->importable_data
& VERT_BIT_POS
);
182 VB
->import_data( ctx
, VERT_BIT_POS
, VEC_NOT_WRITEABLE
);
183 input
= ctx
->_NeedEyeCoords
? VB
->EyePtr
: VB
->ObjPtr
;
185 ASSERT((input
->flags
& VEC_NOT_WRITEABLE
) == 0);
188 _mesa_vector4f_clean_elem(input
, VB
->Count
, 2);
197 /* The individual functions know about replaying side-effects
198 * vs. full re-execution.
200 store
->light_func_tab
[ind
]( ctx
, VB
, stage
, input
);
206 /* Called in place of do_lighting when the light table may have changed.
208 static GLboolean
run_validate_lighting( GLcontext
*ctx
,
209 struct gl_pipeline_stage
*stage
)
214 if (ctx
->Visual
.rgbMode
) {
215 if (ctx
->Light
._NeedVertices
) {
216 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
217 tab
= _tnl_light_spec_tab
;
219 tab
= _tnl_light_tab
;
222 if (ctx
->Light
.EnabledList
.next
== ctx
->Light
.EnabledList
.prev
)
223 tab
= _tnl_light_fast_single_tab
;
225 tab
= _tnl_light_fast_tab
;
229 tab
= _tnl_light_ci_tab
;
231 if (ctx
->Light
.ColorMaterialEnabled
)
232 ind
|= LIGHT_COLORMATERIAL
;
234 if (ctx
->Light
.Model
.TwoSide
)
235 ind
|= LIGHT_TWOSIDE
;
237 LIGHT_STAGE_DATA(stage
)->light_func_tab
= &tab
[ind
];
239 /* This and the above should only be done on _NEW_LIGHT:
241 TNL_CONTEXT(ctx
)->Driver
.NotifyMaterialChange( ctx
);
243 /* Now run the stage...
245 stage
->run
= run_lighting
;
246 return stage
->run( ctx
, stage
);
249 static void alloc_4chan( struct gl_client_array
*a
, GLuint sz
)
251 a
->Ptr
= ALIGN_MALLOC( sz
* sizeof(GLchan
) * 4, 32 );
255 a
->StrideB
= sizeof(GLchan
) * 4;
261 /* Called the first time stage->run is called. In effect, don't
262 * allocate data until the first time the stage is run.
264 static GLboolean
run_init_lighting( GLcontext
*ctx
,
265 struct gl_pipeline_stage
*stage
)
267 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
268 struct light_stage_data
*store
;
269 GLuint size
= tnl
->vb
.Size
;
271 stage
->privatePtr
= MALLOC(sizeof(*store
));
272 store
= LIGHT_STAGE_DATA(stage
);
280 store
->FloatColor
.Ptr
= 0;
282 alloc_4chan( &store
->LitColor
[0], size
);
283 alloc_4chan( &store
->LitColor
[1], size
);
284 alloc_4chan( &store
->LitSecondary
[0], size
);
285 alloc_4chan( &store
->LitSecondary
[1], size
);
287 _mesa_vector1ui_alloc( &store
->LitIndex
[0], 0, size
, 32 );
288 _mesa_vector1ui_alloc( &store
->LitIndex
[1], 0, size
, 32 );
290 /* Now validate the stage derived data...
292 stage
->run
= run_validate_lighting
;
293 return stage
->run( ctx
, stage
);
299 * Check if lighting is enabled. If so, configure the pipeline stage's
300 * type, inputs, and outputs.
302 static void check_lighting( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
304 stage
->active
= ctx
->Light
.Enabled
&& !ctx
->VertexProgram
.Enabled
;
306 if (stage
->privatePtr
)
307 stage
->run
= run_validate_lighting
;
308 stage
->inputs
= VERT_BIT_NORMAL
|VERT_BIT_MATERIAL
;
309 if (ctx
->Light
._NeedVertices
)
310 stage
->inputs
|= VERT_BIT_EYE
; /* effectively, even when lighting in obj */
311 if (ctx
->Light
.ColorMaterialEnabled
)
312 stage
->inputs
|= VERT_BIT_COLOR0
;
314 stage
->outputs
= VERT_BIT_COLOR0
;
315 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
316 stage
->outputs
|= VERT_BIT_COLOR1
;
321 static void dtr( struct gl_pipeline_stage
*stage
)
323 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
326 ALIGN_FREE( store
->LitColor
[0].Ptr
);
327 ALIGN_FREE( store
->LitColor
[1].Ptr
);
328 ALIGN_FREE( store
->LitSecondary
[0].Ptr
);
329 ALIGN_FREE( store
->LitSecondary
[1].Ptr
);
331 if (store
->FloatColor
.Ptr
)
332 ALIGN_FREE( store
->FloatColor
.Ptr
);
334 _mesa_vector1ui_free( &store
->LitIndex
[0] );
335 _mesa_vector1ui_free( &store
->LitIndex
[1] );
337 stage
->privatePtr
= 0;
341 const struct gl_pipeline_stage _tnl_lighting_stage
=
343 "lighting", /* name */
344 _NEW_LIGHT
, /* recheck */
345 _NEW_LIGHT
|_NEW_MODELVIEW
, /* recalc -- modelview dependency
346 * otherwise not captured by inputs
347 * (which may be VERT_BIT_POS) */
348 GL_FALSE
, /* active? */
351 0, /* changed_inputs */
352 NULL
, /* private_data */
354 check_lighting
, /* check */
355 run_init_lighting
/* run -- initially set to ctr */