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_TWOSIDE 0x1
42 #define LIGHT_MATERIAL 0x2
43 #define MAX_LIGHT_FUNC 0x4
45 typedef void (*light_func
)( GLcontext
*ctx
,
46 struct vertex_buffer
*VB
,
47 struct tnl_pipeline_stage
*stage
,
50 struct material_cursor
{
56 struct light_stage_data
{
58 GLvector4f LitColor
[2];
59 GLvector4f LitSecondary
[2];
60 GLvector4f LitIndex
[2];
61 light_func
*light_func_tab
;
63 struct material_cursor mat
[MAT_ATTRIB_MAX
];
69 #define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->privatePtr))
73 /* In the case of colormaterial, the effected material attributes
74 * should already have been bound to point to the incoming color data,
75 * prior to running the pipeline.
77 static void update_materials( GLcontext
*ctx
,
78 struct light_stage_data
*store
)
82 for (i
= 0 ; i
< store
->mat_count
; i
++) {
83 COPY_4V(store
->mat
[i
].current
, store
->mat
[i
].ptr
);
84 STRIDE_F(store
->mat
[i
].ptr
, store
->mat
[i
].stride
);
87 _mesa_update_material( ctx
, store
->mat_bitmask
);
88 _mesa_validate_all_lighting_tables( ctx
);
91 static GLuint
prepare_materials( GLcontext
*ctx
,
92 struct vertex_buffer
*VB
,
93 struct light_stage_data
*store
)
98 store
->mat_bitmask
= 0;
100 /* If ColorMaterial enabled, overwrite affected AttrPtr's with
101 * the color pointer. This could be done earlier.
103 if (ctx
->Light
.ColorMaterialEnabled
) {
104 GLuint bitmask
= ctx
->Light
.ColorMaterialBitmask
;
105 for (i
= 0 ; i
< MAT_ATTRIB_MAX
; i
++)
106 if (bitmask
& (1<<i
))
107 VB
->AttribPtr
[_TNL_ATTRIB_MAT_FRONT_AMBIENT
+ i
] = VB
->ColorPtr
[0];
110 for (i
= _TNL_ATTRIB_MAT_FRONT_AMBIENT
; i
< _TNL_ATTRIB_INDEX
; i
++) {
111 if (VB
->AttribPtr
[i
]->stride
) {
112 GLuint j
= store
->mat_count
++;
113 GLuint attr
= i
- _TNL_ATTRIB_MAT_FRONT_AMBIENT
;
114 store
->mat
[j
].ptr
= VB
->AttribPtr
[i
]->start
;
115 store
->mat
[j
].stride
= VB
->AttribPtr
[i
]->stride
;
116 store
->mat
[j
].current
= ctx
->Light
.Material
.Attrib
[attr
];
117 store
->mat_bitmask
|= (1<<attr
);
122 /* FIXME: Is this already done?
124 _mesa_update_material( ctx
, ~0 );
125 _mesa_validate_all_lighting_tables( ctx
);
127 return store
->mat_count
;
130 /* Tables for all the shading functions.
132 static light_func _tnl_light_tab
[MAX_LIGHT_FUNC
];
133 static light_func _tnl_light_fast_tab
[MAX_LIGHT_FUNC
];
134 static light_func _tnl_light_fast_single_tab
[MAX_LIGHT_FUNC
];
135 static light_func _tnl_light_spec_tab
[MAX_LIGHT_FUNC
];
136 static light_func _tnl_light_ci_tab
[MAX_LIGHT_FUNC
];
140 #include "t_vb_lighttmp.h"
142 #define TAG(x) x##_twoside
143 #define IDX (LIGHT_TWOSIDE)
144 #include "t_vb_lighttmp.h"
146 #define TAG(x) x##_material
147 #define IDX (LIGHT_MATERIAL)
148 #include "t_vb_lighttmp.h"
150 #define TAG(x) x##_twoside_material
151 #define IDX (LIGHT_TWOSIDE|LIGHT_MATERIAL)
152 #include "t_vb_lighttmp.h"
155 static void init_lighting( void )
161 init_light_tab_twoside();
162 init_light_tab_material();
163 init_light_tab_twoside_material();
169 static GLboolean
run_lighting( GLcontext
*ctx
,
170 struct tnl_pipeline_stage
*stage
)
172 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
173 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
174 struct vertex_buffer
*VB
= &tnl
->vb
;
175 GLvector4f
*input
= ctx
->_NeedEyeCoords
? VB
->EyePtr
: VB
->ObjPtr
;
178 /* Make sure we can talk about position x,y and z:
180 if (stage
->changed_inputs
& _TNL_BIT_POS
) {
181 if (input
->size
<= 2 && input
== VB
->ObjPtr
) {
183 _math_trans_4f( store
->Input
.data
,
191 if (input
->size
<= 2) {
194 _mesa_vector4f_clean_elem(&store
->Input
, VB
->Count
, 2);
197 if (input
->size
<= 1) {
200 _mesa_vector4f_clean_elem(&store
->Input
, VB
->Count
, 1);
203 input
= &store
->Input
;
209 if (prepare_materials( ctx
, VB
, store
))
210 idx
|= LIGHT_MATERIAL
;
212 if (ctx
->Light
.Model
.TwoSide
)
213 idx
|= LIGHT_TWOSIDE
;
215 /* The individual functions know about replaying side-effects
216 * vs. full re-execution.
218 store
->light_func_tab
[idx
]( ctx
, VB
, stage
, input
);
224 /* Called in place of do_lighting when the light table may have changed.
226 static GLboolean
run_validate_lighting( GLcontext
*ctx
,
227 struct tnl_pipeline_stage
*stage
)
231 if (ctx
->Visual
.rgbMode
) {
232 if (ctx
->Light
._NeedVertices
) {
233 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
234 tab
= _tnl_light_spec_tab
;
236 tab
= _tnl_light_tab
;
239 if (ctx
->Light
.EnabledList
.next
== ctx
->Light
.EnabledList
.prev
)
240 tab
= _tnl_light_fast_single_tab
;
242 tab
= _tnl_light_fast_tab
;
246 tab
= _tnl_light_ci_tab
;
249 LIGHT_STAGE_DATA(stage
)->light_func_tab
= tab
;
251 /* This and the above should only be done on _NEW_LIGHT:
253 TNL_CONTEXT(ctx
)->Driver
.NotifyMaterialChange( ctx
);
255 /* Now run the stage...
257 stage
->run
= run_lighting
;
258 return stage
->run( ctx
, stage
);
263 /* Called the first time stage->run is called. In effect, don't
264 * allocate data until the first time the stage is run.
266 static GLboolean
run_init_lighting( GLcontext
*ctx
,
267 struct tnl_pipeline_stage
*stage
)
269 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
270 struct light_stage_data
*store
;
271 GLuint size
= tnl
->vb
.Size
;
273 stage
->privatePtr
= MALLOC(sizeof(*store
));
274 store
= LIGHT_STAGE_DATA(stage
);
282 _mesa_vector4f_alloc( &store
->Input
, 0, size
, 32 );
283 _mesa_vector4f_alloc( &store
->LitColor
[0], 0, size
, 32 );
284 _mesa_vector4f_alloc( &store
->LitColor
[1], 0, size
, 32 );
285 _mesa_vector4f_alloc( &store
->LitSecondary
[0], 0, size
, 32 );
286 _mesa_vector4f_alloc( &store
->LitSecondary
[1], 0, size
, 32 );
287 _mesa_vector4f_alloc( &store
->LitIndex
[0], 0, size
, 32 );
288 _mesa_vector4f_alloc( &store
->LitIndex
[1], 0, size
, 32 );
290 store
->LitIndex
[0].size
= 1;
291 store
->LitIndex
[0].stride
= sizeof(GLfloat
);
292 store
->LitIndex
[1].size
= 1;
293 store
->LitIndex
[1].stride
= sizeof(GLfloat
);
295 /* Now validate the stage derived data...
297 stage
->run
= run_validate_lighting
;
298 return stage
->run( ctx
, stage
);
304 * Check if lighting is enabled. If so, configure the pipeline stage's
305 * type, inputs, and outputs.
307 static void check_lighting( GLcontext
*ctx
, struct tnl_pipeline_stage
*stage
)
309 stage
->active
= ctx
->Light
.Enabled
&& !ctx
->VertexProgram
.Enabled
;
311 if (stage
->privatePtr
)
312 stage
->run
= run_validate_lighting
;
313 stage
->inputs
= _TNL_BIT_NORMAL
|_TNL_BITS_MAT_ANY
;
314 if (ctx
->Light
._NeedVertices
)
315 stage
->inputs
|= _TNL_BIT_POS
;
316 if (ctx
->Light
.ColorMaterialEnabled
)
317 stage
->inputs
|= _TNL_BIT_COLOR0
;
319 stage
->outputs
= _TNL_BIT_COLOR0
;
320 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
321 stage
->outputs
|= _TNL_BIT_COLOR1
;
326 static void dtr( struct tnl_pipeline_stage
*stage
)
328 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
331 _mesa_vector4f_free( &store
->Input
);
332 _mesa_vector4f_free( &store
->LitColor
[0] );
333 _mesa_vector4f_free( &store
->LitColor
[1] );
334 _mesa_vector4f_free( &store
->LitSecondary
[0] );
335 _mesa_vector4f_free( &store
->LitSecondary
[1] );
336 _mesa_vector4f_free( &store
->LitIndex
[0] );
337 _mesa_vector4f_free( &store
->LitIndex
[1] );
339 stage
->privatePtr
= 0;
343 const struct tnl_pipeline_stage _tnl_lighting_stage
=
345 "lighting", /* name */
346 _NEW_LIGHT
, /* recheck */
347 _NEW_LIGHT
|_NEW_MODELVIEW
, /* recalc -- modelview dependency
348 * otherwise not captured by inputs
349 * (which may be _TNL_BIT_POS) */
350 GL_FALSE
, /* active? */
353 0, /* changed_inputs */
354 NULL
, /* private_data */
356 check_lighting
, /* check */
357 run_init_lighting
/* run -- initially set to ctr */