1 /* $Id: t_vb_light.c,v 1.19 2002/10/24 23:57:25 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 #include "simple_list.h"
38 #include "math/m_translate.h"
40 #include "t_context.h"
41 #include "t_pipeline.h"
43 #define LIGHT_FLAGS 0x1 /* must be first */
44 #define LIGHT_TWOSIDE 0x2
45 #define LIGHT_COLORMATERIAL 0x4
46 #define MAX_LIGHT_FUNC 0x8
48 typedef void (*light_func
)( GLcontext
*ctx
,
49 struct vertex_buffer
*VB
,
50 struct gl_pipeline_stage
*stage
,
53 struct light_stage_data
{
54 struct gl_client_array FloatColor
;
55 struct gl_client_array LitColor
[2];
56 struct gl_client_array LitSecondary
[2];
57 GLvector1ui LitIndex
[2];
58 light_func
*light_func_tab
;
62 #define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->privatePtr))
65 static void import_color_material( GLcontext
*ctx
,
66 struct gl_pipeline_stage
*stage
)
68 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
69 struct gl_client_array
*to
= &LIGHT_STAGE_DATA(stage
)->FloatColor
;
70 struct gl_client_array
*from
= VB
->ColorPtr
[0];
71 GLuint count
= VB
->Count
;
74 to
->Ptr
= ALIGN_MALLOC( VB
->Size
* 4 * sizeof(GLfloat
), 32 );
78 /* No need to transform the same value 3000 times.
85 to
->StrideB
= 4 * sizeof(GLfloat
);
87 _math_trans_4f( (GLfloat (*)[4]) to
->Ptr
,
99 /* Tables for all the shading functions.
101 static light_func _tnl_light_tab
[MAX_LIGHT_FUNC
];
102 static light_func _tnl_light_fast_tab
[MAX_LIGHT_FUNC
];
103 static light_func _tnl_light_fast_single_tab
[MAX_LIGHT_FUNC
];
104 static light_func _tnl_light_spec_tab
[MAX_LIGHT_FUNC
];
105 static light_func _tnl_light_ci_tab
[MAX_LIGHT_FUNC
];
109 #include "t_vb_lighttmp.h"
111 #define TAG(x) x##_tw
112 #define IDX (LIGHT_TWOSIDE)
113 #include "t_vb_lighttmp.h"
115 #define TAG(x) x##_fl
116 #define IDX (LIGHT_FLAGS)
117 #include "t_vb_lighttmp.h"
119 #define TAG(x) x##_tw_fl
120 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE)
121 #include "t_vb_lighttmp.h"
123 #define TAG(x) x##_cm
124 #define IDX (LIGHT_COLORMATERIAL)
125 #include "t_vb_lighttmp.h"
127 #define TAG(x) x##_tw_cm
128 #define IDX (LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
129 #include "t_vb_lighttmp.h"
131 #define TAG(x) x##_fl_cm
132 #define IDX (LIGHT_FLAGS|LIGHT_COLORMATERIAL)
133 #include "t_vb_lighttmp.h"
135 #define TAG(x) x##_tw_fl_cm
136 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
137 #include "t_vb_lighttmp.h"
140 static void init_lighting( void )
148 init_light_tab_tw_fl();
150 init_light_tab_tw_cm();
151 init_light_tab_fl_cm();
152 init_light_tab_tw_fl_cm();
158 static GLboolean
run_lighting( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
160 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
161 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
162 struct vertex_buffer
*VB
= &tnl
->vb
;
163 GLvector4f
*input
= ctx
->_NeedEyeCoords
? VB
->EyePtr
: VB
->ObjPtr
;
166 /* _tnl_print_vert_flags( __FUNCTION__, stage->changed_inputs ); */
168 /* Make sure we can talk about elements 0..2 in the vector we are
171 if (stage
->changed_inputs
& (VERT_BIT_EYE
|VERT_BIT_POS
)) {
172 if (input
->size
<= 2) {
173 if (input
->flags
& VEC_NOT_WRITEABLE
) {
174 ASSERT(VB
->importable_data
& VERT_BIT_POS
);
176 VB
->import_data( ctx
, VERT_BIT_POS
, VEC_NOT_WRITEABLE
);
177 input
= ctx
->_NeedEyeCoords
? VB
->EyePtr
: VB
->ObjPtr
;
179 ASSERT((input
->flags
& VEC_NOT_WRITEABLE
) == 0);
182 _mesa_vector4f_clean_elem(input
, VB
->Count
, 2);
191 /* The individual functions know about replaying side-effects
192 * vs. full re-execution.
194 store
->light_func_tab
[ind
]( ctx
, VB
, stage
, input
);
200 /* Called in place of do_lighting when the light table may have changed.
202 static GLboolean
run_validate_lighting( GLcontext
*ctx
,
203 struct gl_pipeline_stage
*stage
)
208 if (ctx
->Visual
.rgbMode
) {
209 if (ctx
->Light
._NeedVertices
) {
210 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
211 tab
= _tnl_light_spec_tab
;
213 tab
= _tnl_light_tab
;
216 if (ctx
->Light
.EnabledList
.next
== ctx
->Light
.EnabledList
.prev
)
217 tab
= _tnl_light_fast_single_tab
;
219 tab
= _tnl_light_fast_tab
;
223 tab
= _tnl_light_ci_tab
;
225 if (ctx
->Light
.ColorMaterialEnabled
)
226 ind
|= LIGHT_COLORMATERIAL
;
228 if (ctx
->Light
.Model
.TwoSide
)
229 ind
|= LIGHT_TWOSIDE
;
231 LIGHT_STAGE_DATA(stage
)->light_func_tab
= &tab
[ind
];
233 /* This and the above should only be done on _NEW_LIGHT:
235 TNL_CONTEXT(ctx
)->Driver
.NotifyMaterialChange( ctx
);
237 /* Now run the stage...
239 stage
->run
= run_lighting
;
240 return stage
->run( ctx
, stage
);
243 static void alloc_4chan( struct gl_client_array
*a
, GLuint sz
)
245 a
->Ptr
= ALIGN_MALLOC( sz
* sizeof(GLchan
) * 4, 32 );
249 a
->StrideB
= sizeof(GLchan
) * 4;
255 /* Called the first time stage->run is called. In effect, don't
256 * allocate data until the first time the stage is run.
258 static GLboolean
run_init_lighting( GLcontext
*ctx
,
259 struct gl_pipeline_stage
*stage
)
261 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
262 struct light_stage_data
*store
;
263 GLuint size
= tnl
->vb
.Size
;
265 stage
->privatePtr
= MALLOC(sizeof(*store
));
266 store
= LIGHT_STAGE_DATA(stage
);
274 store
->FloatColor
.Ptr
= 0;
276 alloc_4chan( &store
->LitColor
[0], size
);
277 alloc_4chan( &store
->LitColor
[1], size
);
278 alloc_4chan( &store
->LitSecondary
[0], size
);
279 alloc_4chan( &store
->LitSecondary
[1], size
);
281 _mesa_vector1ui_alloc( &store
->LitIndex
[0], 0, size
, 32 );
282 _mesa_vector1ui_alloc( &store
->LitIndex
[1], 0, size
, 32 );
284 /* Now validate the stage derived data...
286 stage
->run
= run_validate_lighting
;
287 return stage
->run( ctx
, stage
);
293 * Check if lighting is enabled. If so, configure the pipeline stage's
294 * type, inputs, and outputs.
296 static void check_lighting( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
298 stage
->active
= ctx
->Light
.Enabled
&& !ctx
->VertexProgram
.Enabled
;
300 if (stage
->privatePtr
)
301 stage
->run
= run_validate_lighting
;
302 stage
->inputs
= VERT_BIT_NORMAL
|VERT_BIT_MATERIAL
;
303 if (ctx
->Light
._NeedVertices
)
304 stage
->inputs
|= VERT_BIT_EYE
; /* effectively, even when lighting in obj */
305 if (ctx
->Light
.ColorMaterialEnabled
)
306 stage
->inputs
|= VERT_BIT_COLOR0
;
308 stage
->outputs
= VERT_BIT_COLOR0
;
309 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
310 stage
->outputs
|= VERT_BIT_COLOR1
;
315 static void dtr( struct gl_pipeline_stage
*stage
)
317 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
320 ALIGN_FREE( store
->LitColor
[0].Ptr
);
321 ALIGN_FREE( store
->LitColor
[1].Ptr
);
322 ALIGN_FREE( store
->LitSecondary
[0].Ptr
);
323 ALIGN_FREE( store
->LitSecondary
[1].Ptr
);
325 if (store
->FloatColor
.Ptr
)
326 ALIGN_FREE( store
->FloatColor
.Ptr
);
328 _mesa_vector1ui_free( &store
->LitIndex
[0] );
329 _mesa_vector1ui_free( &store
->LitIndex
[1] );
331 stage
->privatePtr
= 0;
335 const struct gl_pipeline_stage _tnl_lighting_stage
=
337 "lighting", /* name */
338 _NEW_LIGHT
, /* recheck */
339 _NEW_LIGHT
|_NEW_MODELVIEW
, /* recalc -- modelview dependency
340 * otherwise not captured by inputs
341 * (which may be VERT_BIT_POS) */
342 GL_FALSE
, /* active? */
345 0, /* changed_inputs */
346 NULL
, /* private_data */
348 check_lighting
, /* check */
349 run_init_lighting
/* run -- initially set to ctr */