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
);
220 VB
->AttribPtr
[_TNL_ATTRIB_COLOR0
] = VB
->ColorPtr
[0];
221 VB
->AttribPtr
[_TNL_ATTRIB_COLOR1
] = VB
->SecondaryColorPtr
[0];
222 VB
->AttribPtr
[_TNL_ATTRIB_INDEX
] = VB
->IndexPtr
[0];
228 /* Called in place of do_lighting when the light table may have changed.
230 static GLboolean
run_validate_lighting( GLcontext
*ctx
,
231 struct tnl_pipeline_stage
*stage
)
235 if (ctx
->Visual
.rgbMode
) {
236 if (ctx
->Light
._NeedVertices
) {
237 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
238 tab
= _tnl_light_spec_tab
;
240 tab
= _tnl_light_tab
;
243 if (ctx
->Light
.EnabledList
.next
== ctx
->Light
.EnabledList
.prev
)
244 tab
= _tnl_light_fast_single_tab
;
246 tab
= _tnl_light_fast_tab
;
250 tab
= _tnl_light_ci_tab
;
253 LIGHT_STAGE_DATA(stage
)->light_func_tab
= tab
;
255 /* This and the above should only be done on _NEW_LIGHT:
257 TNL_CONTEXT(ctx
)->Driver
.NotifyMaterialChange( ctx
);
259 /* Now run the stage...
261 stage
->run
= run_lighting
;
262 return stage
->run( ctx
, stage
);
267 /* Called the first time stage->run is called. In effect, don't
268 * allocate data until the first time the stage is run.
270 static GLboolean
run_init_lighting( GLcontext
*ctx
,
271 struct tnl_pipeline_stage
*stage
)
273 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
274 struct light_stage_data
*store
;
275 GLuint size
= tnl
->vb
.Size
;
277 stage
->privatePtr
= MALLOC(sizeof(*store
));
278 store
= LIGHT_STAGE_DATA(stage
);
286 _mesa_vector4f_alloc( &store
->Input
, 0, size
, 32 );
287 _mesa_vector4f_alloc( &store
->LitColor
[0], 0, size
, 32 );
288 _mesa_vector4f_alloc( &store
->LitColor
[1], 0, size
, 32 );
289 _mesa_vector4f_alloc( &store
->LitSecondary
[0], 0, size
, 32 );
290 _mesa_vector4f_alloc( &store
->LitSecondary
[1], 0, size
, 32 );
291 _mesa_vector4f_alloc( &store
->LitIndex
[0], 0, size
, 32 );
292 _mesa_vector4f_alloc( &store
->LitIndex
[1], 0, size
, 32 );
294 store
->LitIndex
[0].size
= 1;
295 store
->LitIndex
[0].stride
= sizeof(GLfloat
);
296 store
->LitIndex
[1].size
= 1;
297 store
->LitIndex
[1].stride
= sizeof(GLfloat
);
299 /* Now validate the stage derived data...
301 stage
->run
= run_validate_lighting
;
302 return stage
->run( ctx
, stage
);
308 * Check if lighting is enabled. If so, configure the pipeline stage's
309 * type, inputs, and outputs.
311 static void check_lighting( GLcontext
*ctx
, struct tnl_pipeline_stage
*stage
)
313 stage
->active
= ctx
->Light
.Enabled
&& !ctx
->VertexProgram
.Enabled
;
315 if (stage
->privatePtr
)
316 stage
->run
= run_validate_lighting
;
317 stage
->inputs
= _TNL_BIT_NORMAL
|_TNL_BITS_MAT_ANY
;
318 if (ctx
->Light
._NeedVertices
)
319 stage
->inputs
|= _TNL_BIT_POS
;
320 if (ctx
->Light
.ColorMaterialEnabled
)
321 stage
->inputs
|= _TNL_BIT_COLOR0
;
323 stage
->outputs
= _TNL_BIT_COLOR0
;
324 if (ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)
325 stage
->outputs
|= _TNL_BIT_COLOR1
;
330 static void dtr( struct tnl_pipeline_stage
*stage
)
332 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
335 _mesa_vector4f_free( &store
->Input
);
336 _mesa_vector4f_free( &store
->LitColor
[0] );
337 _mesa_vector4f_free( &store
->LitColor
[1] );
338 _mesa_vector4f_free( &store
->LitSecondary
[0] );
339 _mesa_vector4f_free( &store
->LitSecondary
[1] );
340 _mesa_vector4f_free( &store
->LitIndex
[0] );
341 _mesa_vector4f_free( &store
->LitIndex
[1] );
343 stage
->privatePtr
= 0;
347 const struct tnl_pipeline_stage _tnl_lighting_stage
=
349 "lighting", /* name */
350 _NEW_LIGHT
, /* recheck */
351 _NEW_LIGHT
|_NEW_MODELVIEW
, /* recalc -- modelview dependency
352 * otherwise not captured by inputs
353 * (which may be _TNL_BIT_POS) */
354 GL_FALSE
, /* active? */
357 0, /* changed_inputs */
358 NULL
, /* private_data */
360 check_lighting
, /* check */
361 run_init_lighting
/* run -- initially set to ctr */