b55cda38588a39bc70a48f2a540162758b816a20
[mesa.git] / src / mesa / tnl / t_vb_light.c
1 /* $Id: t_vb_light.c,v 1.1 2000/12/26 05:09:33 keithw Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27
28
29 #include "glheader.h"
30 #include "colormac.h"
31 #include "light.h"
32 #include "macros.h"
33 #include "mem.h"
34 #include "mmath.h"
35 #include "simple_list.h"
36 #include "mtypes.h"
37
38 #include "t_context.h"
39 #include "t_pipeline.h"
40
41 #define LIGHT_FLAGS 0x1 /* must be first */
42 #define LIGHT_TWOSIDE 0x2
43 #define LIGHT_COLORMATERIAL 0x4
44 #define MAX_LIGHT_FUNC 0x8
45
46 typedef void (*light_func)( GLcontext *ctx,
47 struct vertex_buffer *VB,
48 struct gl_pipeline_stage *stage,
49 GLvector4f *input );
50
51 struct light_stage_data {
52 GLvector4ub LitColor[2];
53 GLvector1ui LitIndex[2];
54 GLvector4ub LitSecondary[2];
55
56 light_func *light_func_tab;
57 };
58
59 #define LIGHT_STAGE_DATA(stage) ((struct light_stage_data *)(stage->private))
60
61 /* Tables for all the shading functions.
62 */
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];
68
69 #define TAG(x) x
70 #define IDX (0)
71 #include "t_vb_lighttmp.h"
72
73 #define TAG(x) x##_tw
74 #define IDX (LIGHT_TWOSIDE)
75 #include "t_vb_lighttmp.h"
76
77 #define TAG(x) x##_fl
78 #define IDX (LIGHT_FLAGS)
79 #include "t_vb_lighttmp.h"
80
81 #define TAG(x) x##_tw_fl
82 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE)
83 #include "t_vb_lighttmp.h"
84
85 #define TAG(x) x##_cm
86 #define IDX (LIGHT_COLORMATERIAL)
87 #include "t_vb_lighttmp.h"
88
89 #define TAG(x) x##_tw_cm
90 #define IDX (LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
91 #include "t_vb_lighttmp.h"
92
93 #define TAG(x) x##_fl_cm
94 #define IDX (LIGHT_FLAGS|LIGHT_COLORMATERIAL)
95 #include "t_vb_lighttmp.h"
96
97 #define TAG(x) x##_tw_fl_cm
98 #define IDX (LIGHT_FLAGS|LIGHT_TWOSIDE|LIGHT_COLORMATERIAL)
99 #include "t_vb_lighttmp.h"
100
101
102 static void init_lighting( void )
103 {
104 static int done;
105
106 if (!done) {
107 init_light_tab();
108 init_light_tab_tw();
109 init_light_tab_fl();
110 init_light_tab_tw_fl();
111 init_light_tab_cm();
112 init_light_tab_tw_cm();
113 init_light_tab_fl_cm();
114 init_light_tab_tw_fl_cm();
115 done = 1;
116 }
117 }
118
119
120 static GLboolean run_lighting( GLcontext *ctx, struct gl_pipeline_stage *stage )
121 {
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;
126 GLuint ind;
127
128 /* Make sure we can talk about elements 0..2 in the vector we are
129 * lighting. TODO: Don't repeat this in CVA!
130 */
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);
137 }
138
139 gl_vector4f_clean_elem(input, VB->Count, 2);
140 }
141
142 if (VB->Flag)
143 ind = LIGHT_FLAGS;
144 else
145 ind = 0;
146
147 /* The individual tabs know about replaying side-effects vs. full
148 * re-execution.
149 */
150 store->light_func_tab[ind]( ctx, VB, stage, input );
151
152 return GL_TRUE;
153 }
154
155
156 /* Called in place of do_lighting when the light table may have changed.
157 */
158 static GLboolean run_validate_lighting( GLcontext *ctx,
159 struct gl_pipeline_stage *stage )
160 {
161 GLuint ind = 0;
162 light_func *tab;
163
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;
168 else
169 tab = _tnl_light_tab;
170 }
171 else {
172 if (ctx->Light.EnabledList.next == ctx->Light.EnabledList.prev)
173 tab = _tnl_light_fast_single_tab;
174 else
175 tab = _tnl_light_fast_tab;
176 }
177 /* tab = _tnl_light_tab; */
178 }
179
180 if (ctx->Light.ColorMaterialEnabled)
181 ind |= LIGHT_COLORMATERIAL;
182
183 if (ctx->Light.Model.TwoSide)
184 ind |= LIGHT_TWOSIDE;
185
186 LIGHT_STAGE_DATA(stage)->light_func_tab = &tab[ind];
187
188 /* This and the above should only be done on _NEW_LIGHT:
189 */
190 gl_validate_all_lighting_tables( ctx );
191
192 /* Now run the stage...
193 */
194 stage->run = run_lighting;
195 return stage->run( ctx, stage );
196 }
197
198 /* Called the first time stage->run is called. In effect, don't
199 * allocate data until the first time the stage is run.
200 */
201 static GLboolean run_init_lighting( GLcontext *ctx,
202 struct gl_pipeline_stage *stage )
203 {
204 TNLcontext *tnl = TNL_CONTEXT(ctx);
205 struct light_stage_data *store;
206 GLuint size = tnl->vb.Size;
207
208 stage->private = MALLOC(sizeof(*store));
209 store = LIGHT_STAGE_DATA(stage);
210 if (!store)
211 return GL_FALSE;
212
213 /* Do onetime init.
214 */
215 init_lighting();
216
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 );
223
224 /* Now validate the stage derived data...
225 */
226 stage->run = run_validate_lighting;
227 return stage->run( ctx, stage );
228 }
229
230
231
232 /*
233 * Check if lighting is enabled. If so, configure the pipeline stage's
234 * type, inputs, and outputs.
235 */
236 static void check_lighting( GLcontext *ctx, struct gl_pipeline_stage *stage )
237 {
238 stage->active = ctx->Light.Enabled;
239 if (stage->active) {
240 if (stage->private)
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;
247 }
248 }
249
250
251 static void dtr( struct gl_pipeline_stage *stage )
252 {
253 struct light_stage_data *store = LIGHT_STAGE_DATA(stage);
254
255 if (store) {
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] );
262 FREE( store );
263 stage->private = 0;
264 }
265 }
266
267 const struct gl_pipeline_stage _tnl_lighting_stage =
268 {
269 "lighting",
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 */
276 dtr, /* destroy */
277 check_lighting, /* check */
278 run_init_lighting /* run -- initially set to ctr */
279 };
280