1 /* $Id: t_vb_light.c,v 1.1 2000/12/26 05:09:33 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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 "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 GLvector4ub LitColor
[2];
53 GLvector1ui LitIndex
[2];
54 GLvector4ub LitSecondary
[2];
56 light_func
*light_func_tab
;
59 #define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->private))
61 /* Tables for all the shading functions.
63 static light_func _tnl_light_tab
[MAX_LIGHT_FUNC
];
64 static light_func _tnl_light_fast_tab
[MAX_LIGHT_FUNC
];
65 static light_func _tnl_light_fast_single_tab
[MAX_LIGHT_FUNC
];
66 static light_func _tnl_light_spec_tab
[MAX_LIGHT_FUNC
];
67 static light_func _tnl_light_ci_tab
[MAX_LIGHT_FUNC
];
71 #include "t_vb_lighttmp.h"
74 #define IDX (LIGHT_TWOSIDE)
75 #include "t_vb_lighttmp.h"
78 #define IDX (LIGHT_FLAGS)
79 #include "t_vb_lighttmp.h"
81 #define TAG(x) x##_tw_fl
82 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE)
83 #include "t_vb_lighttmp.h"
86 #define IDX (LIGHT_COLORMATERIAL)
87 #include "t_vb_lighttmp.h"
89 #define TAG(x) x##_tw_cm
90 #define IDX (LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
91 #include "t_vb_lighttmp.h"
93 #define TAG(x) x##_fl_cm
94 #define IDX (LIGHT_FLAGS|LIGHT_COLORMATERIAL)
95 #include "t_vb_lighttmp.h"
97 #define TAG(x) x##_tw_fl_cm
98 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
99 #include "t_vb_lighttmp.h"
102 static void init_lighting( void )
110 init_light_tab_tw_fl();
112 init_light_tab_tw_cm();
113 init_light_tab_fl_cm();
114 init_light_tab_tw_fl_cm();
120 static GLboolean
run_lighting( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
122 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
123 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
124 struct vertex_buffer
*VB
= &tnl
->vb
;
125 GLvector4f
*input
= ctx
->_NeedEyeCoords
? VB
->EyePtr
: VB
->ObjPtr
;
128 /* Make sure we can talk about elements 0..2 in the vector we are
129 * lighting. TODO: Don't repeat this in CVA!
131 if (input
->size
<= 2) {
132 if (input
->flags
& VEC_NOT_WRITEABLE
) {
133 ASSERT(VB
->importable_data
& VERT_OBJ
);
134 VB
->import_data( ctx
, VERT_OBJ
, VEC_NOT_WRITEABLE
);
135 input
= ctx
->_NeedEyeCoords
? VB
->EyePtr
: VB
->ObjPtr
;
136 ASSERT((input
->flags
& VEC_NOT_WRITEABLE
) == 0);
139 gl_vector4f_clean_elem(input
, VB
->Count
, 2);
147 /* The individual tabs know about replaying side-effects vs. full
150 store
->light_func_tab
[ind
]( ctx
, VB
, stage
, input
);
156 /* Called in place of do_lighting when the light table may have changed.
158 static GLboolean
run_validate_lighting( GLcontext
*ctx
,
159 struct gl_pipeline_stage
*stage
)
164 if (ctx
->Visual
.RGBAflag
) {
165 if (ctx
->Light
._NeedVertices
) {
166 if (ctx
->Light
.Model
.ColorControl
==GL_SEPARATE_SPECULAR_COLOR
)
167 tab
= _tnl_light_spec_tab
;
169 tab
= _tnl_light_tab
;
172 if (ctx
->Light
.EnabledList
.next
== ctx
->Light
.EnabledList
.prev
)
173 tab
= _tnl_light_fast_single_tab
;
175 tab
= _tnl_light_fast_tab
;
177 /* tab = _tnl_light_tab; */
180 if (ctx
->Light
.ColorMaterialEnabled
)
181 ind
|= LIGHT_COLORMATERIAL
;
183 if (ctx
->Light
.Model
.TwoSide
)
184 ind
|= LIGHT_TWOSIDE
;
186 LIGHT_STAGE_DATA(stage
)->light_func_tab
= &tab
[ind
];
188 /* This and the above should only be done on _NEW_LIGHT:
190 gl_validate_all_lighting_tables( ctx
);
192 /* Now run the stage...
194 stage
->run
= run_lighting
;
195 return stage
->run( ctx
, stage
);
198 /* Called the first time stage->run is called. In effect, don't
199 * allocate data until the first time the stage is run.
201 static GLboolean
run_init_lighting( GLcontext
*ctx
,
202 struct gl_pipeline_stage
*stage
)
204 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
205 struct light_stage_data
*store
;
206 GLuint size
= tnl
->vb
.Size
;
208 stage
->private = MALLOC(sizeof(*store
));
209 store
= LIGHT_STAGE_DATA(stage
);
217 gl_vector4ub_alloc( &store
->LitColor
[0], 0, size
, 32 );
218 gl_vector4ub_alloc( &store
->LitColor
[1], 0, size
, 32 );
219 gl_vector4ub_alloc( &store
->LitSecondary
[0], 0, size
, 32 );
220 gl_vector4ub_alloc( &store
->LitSecondary
[1], 0, size
, 32 );
221 gl_vector1ui_alloc( &store
->LitIndex
[0], 0, size
, 32 );
222 gl_vector1ui_alloc( &store
->LitIndex
[1], 0, size
, 32 );
224 /* Now validate the stage derived data...
226 stage
->run
= run_validate_lighting
;
227 return stage
->run( ctx
, stage
);
233 * Check if lighting is enabled. If so, configure the pipeline stage's
234 * type, inputs, and outputs.
236 static void check_lighting( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
238 stage
->active
= ctx
->Light
.Enabled
;
241 stage
->run
= run_validate_lighting
;
242 stage
->inputs
= VERT_NORM
|VERT_MATERIAL
;
243 if (ctx
->Light
._NeedVertices
)
244 stage
->inputs
|= VERT_EYE
; /* effectively, even when lighting in obj */
245 if (ctx
->Light
.ColorMaterialEnabled
)
246 stage
->inputs
|= VERT_RGBA
;
251 static void dtr( struct gl_pipeline_stage
*stage
)
253 struct light_stage_data
*store
= LIGHT_STAGE_DATA(stage
);
256 gl_vector4ub_free( &store
->LitColor
[0] );
257 gl_vector4ub_free( &store
->LitColor
[1] );
258 gl_vector1ui_free( &store
->LitIndex
[0] );
259 gl_vector1ui_free( &store
->LitIndex
[1] );
260 gl_vector4ub_free( &store
->LitSecondary
[0] );
261 gl_vector4ub_free( &store
->LitSecondary
[1] );
267 const struct gl_pipeline_stage _tnl_lighting_stage
=
270 _NEW_LIGHT
, /* recheck */
271 _NEW_LIGHT
|_NEW_MODELVIEW
, /* recalc -- modelview dependency
272 * otherwise not captured by inputs
273 * (which may be VERT_OBJ) */
274 0,0,VERT_RGBA
, /* active, inputs, outputs */
275 0,0, /* changed_inputs, private_data */
277 check_lighting
, /* check */
278 run_init_lighting
/* run -- initially set to ctr */