2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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
,
49 struct material_cursor
{
55 struct light_stage_data
{
57 GLvector4f LitColor
[2];
58 GLvector4f LitSecondary
[2];
59 GLvector4f LitIndex
[2];
60 light_func
*light_func_tab
;
62 struct material_cursor mat
[MAT_ATTRIB_MAX
];
68 #define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->privatePtr))
72 /* In the case of colormaterial, the effected material attributes
73 * should already have been bound to point to the incoming color data,
74 * prior to running the pipeline.
76 static void update_materials( GLcontext
*ctx
,
77 struct light_stage_data
*store
)
81 for (i
= 0 ; i
< store
->mat_count
; i
++) {
82 COPY_4V(store
->mat
[i
].current
, store
->mat
[i
].ptr
);
83 STRIDE_F(store
->mat
[i
].ptr
, store
->mat
[i
].stride
);
86 _mesa_update_material( ctx
, store
->mat_bitmask
);
87 _mesa_validate_all_lighting_tables( ctx
);
90 static GLuint
prepare_materials( GLcontext
*ctx
,
91 struct vertex_buffer
*VB
,
92 struct light_stage_data
*store
)
97 store
->mat_bitmask
= 0;
99 /* If ColorMaterial enabled, overwrite affected AttrPtr's with
100 * the color pointer. This could be done earlier.
102 if (ctx
->Light
.ColorMaterialEnabled
) {
103 GLuint bitmask
= ctx
->Light
.ColorMaterialBitmask
;
104 for (i
= 0 ; i
< MAT_ATTRIB_MAX
; i
++)
105 if (bitmask
& (1<<i
))
106 VB
->AttribPtr
[_TNL_ATTRIB_MAT_FRONT_AMBIENT
+ i
] = VB
->ColorPtr
[0];
109 for (i
= _TNL_ATTRIB_MAT_FRONT_AMBIENT
; i
< _TNL_ATTRIB_INDEX
; i
++) {
110 if (VB
->AttribPtr
[i
]->stride
) {
111 GLuint j
= store
->mat_count
++;
112 GLuint attr
= i
- _TNL_ATTRIB_MAT_FRONT_AMBIENT
;
113 store
->mat
[j
].ptr
= VB
->AttribPtr
[i
]->start
;
114 store
->mat
[j
].stride
= VB
->AttribPtr
[i
]->stride
;
115 store
->mat
[j
].current
= ctx
->Light
.Material
.Attrib
[attr
];
116 store
->mat_bitmask
|= (1<<attr
);
121 /* FIXME: Is this already done?
123 _mesa_update_material( ctx
, ~0 );
124 _mesa_validate_all_lighting_tables( ctx
);
126 return store
->mat_count
;
129 /* Tables for all the shading functions.
131 static light_func _tnl_light_tab
[MAX_LIGHT_FUNC
];
132 static light_func _tnl_light_fast_tab
[MAX_LIGHT_FUNC
];
133 static light_func _tnl_light_fast_single_tab
[MAX_LIGHT_FUNC
];
134 static light_func _tnl_light_spec_tab
[MAX_LIGHT_FUNC
];
135 static light_func _tnl_light_ci_tab
[MAX_LIGHT_FUNC
];
139 #include "t_vb_lighttmp.h"
141 #define TAG(x) x##_twoside
142 #define IDX (LIGHT_TWOSIDE)
143 #include "t_vb_lighttmp.h"
145 #define TAG(x) x##_material
146 #define IDX (LIGHT_MATERIAL)
147 #include "t_vb_lighttmp.h"
149 #define TAG(x) x##_twoside_material
150 #define IDX (LIGHT_TWOSIDE|LIGHT_MATERIAL)
151 #include "t_vb_lighttmp.h"
154 static void init_lighting( void )
160 init_light_tab_twoside();
161 init_light_tab_material();
162 init_light_tab_twoside_material();
168 static GLboolean
run_lighting( GLcontext
*ctx
,
169 struct tnl_pipeline_stage
*stage
)
171 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
172 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
173 struct vertex_buffer
*VB
= &tnl
->vb
;
174 GLvector4f
*input
= ctx
->_NeedEyeCoords
? VB
->EyePtr
: VB
->ObjPtr
;
177 /* Make sure we can talk about position x,y and z:
179 if (stage
->changed_inputs
& _TNL_BIT_POS
) {
180 if (input
->size
<= 2 && input
== VB
->ObjPtr
) {
182 _math_trans_4f( store
->Input
.data
,
190 if (input
->size
<= 2) {
193 _mesa_vector4f_clean_elem(&store
->Input
, VB
->Count
, 2);
196 if (input
->size
<= 1) {
199 _mesa_vector4f_clean_elem(&store
->Input
, VB
->Count
, 1);
202 input
= &store
->Input
;
208 if (prepare_materials( ctx
, VB
, store
))
209 idx
|= LIGHT_MATERIAL
;
211 if (ctx
->Light
.Model
.TwoSide
)
212 idx
|= LIGHT_TWOSIDE
;
214 /* The individual functions know about replaying side-effects
215 * vs. full re-execution.
217 store
->light_func_tab
[idx
]( ctx
, VB
, stage
, input
);
219 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = VB
->ColorPtr
[0];
220 VB
->AttribPtr
[_TNL_ATTRIB_COLOR1
] = VB
->SecondaryColorPtr
[0];
221 VB
->AttribPtr
[_TNL_ATTRIB_INDEX
] = VB
->IndexPtr
[0];
227 /* Called in place of do_lighting when the light table may have changed.
229 static GLboolean
run_validate_lighting( GLcontext
*ctx
,
230 struct tnl_pipeline_stage
*stage
)
234 if (ctx
->Visual
.rgbMode
) {
235 if (ctx
->Light
._NeedVertices
) {
236 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
237 tab
= _tnl_light_spec_tab
;
239 tab
= _tnl_light_tab
;
242 if (ctx
->Light
.EnabledList
.next
== ctx
->Light
.EnabledList
.prev
)
243 tab
= _tnl_light_fast_single_tab
;
245 tab
= _tnl_light_fast_tab
;
249 tab
= _tnl_light_ci_tab
;
252 LIGHT_STAGE_DATA(stage
)->light_func_tab
= tab
;
254 /* This and the above should only be done on _NEW_LIGHT:
256 TNL_CONTEXT(ctx
)->Driver
.NotifyMaterialChange( ctx
);
258 /* Now run the stage...
260 stage
->run
= run_lighting
;
261 return stage
->run( ctx
, stage
);
266 /* Called the first time stage->run is called. In effect, don't
267 * allocate data until the first time the stage is run.
269 static GLboolean
run_init_lighting( GLcontext
*ctx
,
270 struct tnl_pipeline_stage
*stage
)
272 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
273 struct light_stage_data
*store
;
274 GLuint size
= tnl
->vb
.Size
;
276 stage
->privatePtr
= MALLOC(sizeof(*store
));
277 store
= LIGHT_STAGE_DATA(stage
);
285 _mesa_vector4f_alloc( &store
->Input
, 0, size
, 32 );
286 _mesa_vector4f_alloc( &store
->LitColor
[0], 0, size
, 32 );
287 _mesa_vector4f_alloc( &store
->LitColor
[1], 0, size
, 32 );
288 _mesa_vector4f_alloc( &store
->LitSecondary
[0], 0, size
, 32 );
289 _mesa_vector4f_alloc( &store
->LitSecondary
[1], 0, size
, 32 );
290 _mesa_vector4f_alloc( &store
->LitIndex
[0], 0, size
, 32 );
291 _mesa_vector4f_alloc( &store
->LitIndex
[1], 0, size
, 32 );
293 store
->LitColor
[0].size
= 4;
294 store
->LitColor
[1].size
= 4;
295 store
->LitSecondary
[0].size
= 3;
296 store
->LitSecondary
[1].size
= 3;
298 store
->LitIndex
[0].size
= 1;
299 store
->LitIndex
[0].stride
= sizeof(GLfloat
);
300 store
->LitIndex
[1].size
= 1;
301 store
->LitIndex
[1].stride
= sizeof(GLfloat
);
303 /* Now validate the stage derived data...
305 stage
->run
= run_validate_lighting
;
306 return stage
->run( ctx
, stage
);
312 * Check if lighting is enabled. If so, configure the pipeline stage's
313 * type, inputs, and outputs.
315 static void check_lighting( GLcontext
*ctx
, struct tnl_pipeline_stage
*stage
)
317 stage
->active
= ctx
->Light
.Enabled
&& !ctx
->VertexProgram
._Enabled
;
319 if (stage
->privatePtr
)
320 stage
->run
= run_validate_lighting
;
321 stage
->inputs
= _TNL_BIT_NORMAL
|_TNL_BITS_MAT_ANY
;
322 if (ctx
->Light
._NeedVertices
)
323 stage
->inputs
|= _TNL_BIT_POS
;
324 if (ctx
->Light
.ColorMaterialEnabled
)
325 stage
->inputs
|= _TNL_BIT_COLOR0
;
327 stage
->outputs
= _TNL_BIT_COLOR0
;
328 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
329 stage
->outputs
|= _TNL_BIT_COLOR1
;
334 static void dtr( struct tnl_pipeline_stage
*stage
)
336 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
339 _mesa_vector4f_free( &store
->Input
);
340 _mesa_vector4f_free( &store
->LitColor
[0] );
341 _mesa_vector4f_free( &store
->LitColor
[1] );
342 _mesa_vector4f_free( &store
->LitSecondary
[0] );
343 _mesa_vector4f_free( &store
->LitSecondary
[1] );
344 _mesa_vector4f_free( &store
->LitIndex
[0] );
345 _mesa_vector4f_free( &store
->LitIndex
[1] );
347 stage
->privatePtr
= 0;
351 const struct tnl_pipeline_stage _tnl_lighting_stage
=
353 "lighting", /* name */
354 _NEW_LIGHT
|_NEW_PROGRAM
, /* recheck */
355 _NEW_LIGHT
|_NEW_MODELVIEW
, /* recalc -- modelview dependency
356 * otherwise not captured by inputs
357 * (which may be _TNL_BIT_POS) */
358 GL_FALSE
, /* active? */
361 0, /* changed_inputs */
362 NULL
, /* private_data */
364 check_lighting
, /* check */
365 run_init_lighting
/* run -- initially set to ctr */