Fix TCL_LIGHT_MODEL_CTL setting in radeonColorMaterial.
[mesa.git] / src / mesa / swrast_setup / ss_vb.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 *
24 * Authors:
25 * Keith Whitwell <keith@tungstengraphics.com>
26 */
27
28 #include "glheader.h"
29 #include "colormac.h"
30 #include "context.h"
31 #include "macros.h"
32 #include "imports.h"
33 #include "nvfragprog.h"
34
35 #include "swrast/swrast.h"
36 #include "tnl/t_context.h"
37 #include "math/m_vector.h"
38 #include "math/m_translate.h"
39
40 #include "ss_context.h"
41 #include "ss_vb.h"
42
43
44 #if 0
45 static void do_import( struct vertex_buffer *VB,
46 struct gl_client_array *to,
47 struct gl_client_array *from )
48 {
49 GLuint count = VB->Count;
50
51 if (!to->Ptr) {
52 to->Ptr = (GLubyte *) ALIGN_MALLOC( VB->Size * 4 * sizeof(GLchan), 32 );
53 to->Type = CHAN_TYPE;
54 }
55
56 /* No need to transform the same value 3000 times.
57 */
58 if (!from->StrideB) {
59 to->StrideB = 0;
60 count = 1;
61 }
62 else
63 to->StrideB = 4 * sizeof(GLchan);
64
65 _math_trans_4chan( (GLchan (*)[4]) to->Ptr,
66 from->Ptr,
67 from->StrideB,
68 from->Type,
69 from->Size,
70 0,
71 count);
72 }
73
74 static void import_float_colors( GLcontext *ctx )
75 {
76 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
77 struct gl_client_array *to = &SWSETUP_CONTEXT(ctx)->ChanColor;
78 do_import( VB, to, VB->ColorPtr[0] );
79 VB->ColorPtr[0] = to;
80 }
81
82 static void import_float_spec_colors( GLcontext *ctx )
83 {
84 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
85 struct gl_client_array *to = &SWSETUP_CONTEXT(ctx)->ChanSecondaryColor;
86 do_import( VB, to, VB->SecondaryColorPtr[0] );
87 VB->SecondaryColorPtr[0] = to;
88 }
89 #endif
90
91 /* Provides a RasterSetup function which prebuilds vertices for the
92 * software rasterizer. This is required for the triangle functions
93 * in this module, but not the rest of the swrast module.
94 */
95
96
97 #define COLOR 0x1
98 #define INDEX 0x2
99 #define TEX0 0x4
100 #define MULTITEX 0x8
101 #define SPEC 0x10
102 #define FOG 0x20
103 #define POINT 0x40
104 #define MAX_SETUPFUNC 0x80
105
106 static setup_func setup_tab[MAX_SETUPFUNC];
107 static interp_func interp_tab[MAX_SETUPFUNC];
108 static copy_pv_func copy_pv_tab[MAX_SETUPFUNC];
109
110
111 #define IND (0)
112 #define TAG(x) x##_none
113 #include "ss_vbtmp.h"
114
115 #define IND (COLOR)
116 #define TAG(x) x##_color
117 #include "ss_vbtmp.h"
118
119 #define IND (COLOR|SPEC)
120 #define TAG(x) x##_color_spec
121 #include "ss_vbtmp.h"
122
123 #define IND (COLOR|FOG)
124 #define TAG(x) x##_color_fog
125 #include "ss_vbtmp.h"
126
127 #define IND (COLOR|SPEC|FOG)
128 #define TAG(x) x##_color_spec_fog
129 #include "ss_vbtmp.h"
130
131 #define IND (COLOR|TEX0)
132 #define TAG(x) x##_color_tex0
133 #include "ss_vbtmp.h"
134
135 #define IND (COLOR|TEX0|SPEC)
136 #define TAG(x) x##_color_tex0_spec
137 #include "ss_vbtmp.h"
138
139 #define IND (COLOR|TEX0|FOG)
140 #define TAG(x) x##_color_tex0_fog
141 #include "ss_vbtmp.h"
142
143 #define IND (COLOR|TEX0|SPEC|FOG)
144 #define TAG(x) x##_color_tex0_spec_fog
145 #include "ss_vbtmp.h"
146
147 #define IND (COLOR|MULTITEX)
148 #define TAG(x) x##_color_multitex
149 #include "ss_vbtmp.h"
150
151 #define IND (COLOR|MULTITEX|SPEC)
152 #define TAG(x) x##_color_multitex_spec
153 #include "ss_vbtmp.h"
154
155 #define IND (COLOR|MULTITEX|FOG)
156 #define TAG(x) x##_color_multitex_fog
157 #include "ss_vbtmp.h"
158
159 #define IND (COLOR|MULTITEX|SPEC|FOG)
160 #define TAG(x) x##_color_multitex_spec_fog
161 #include "ss_vbtmp.h"
162
163 #define IND (COLOR|POINT)
164 #define TAG(x) x##_color_point
165 #include "ss_vbtmp.h"
166
167 #define IND (COLOR|SPEC|POINT)
168 #define TAG(x) x##_color_spec_point
169 #include "ss_vbtmp.h"
170
171 #define IND (COLOR|FOG|POINT)
172 #define TAG(x) x##_color_fog_point
173 #include "ss_vbtmp.h"
174
175 #define IND (COLOR|SPEC|FOG|POINT)
176 #define TAG(x) x##_color_spec_fog_point
177 #include "ss_vbtmp.h"
178
179 #define IND (COLOR|TEX0|POINT)
180 #define TAG(x) x##_color_tex0_point
181 #include "ss_vbtmp.h"
182
183 #define IND (COLOR|TEX0|SPEC|POINT)
184 #define TAG(x) x##_color_tex0_spec_point
185 #include "ss_vbtmp.h"
186
187 #define IND (COLOR|TEX0|FOG|POINT)
188 #define TAG(x) x##_color_tex0_fog_point
189 #include "ss_vbtmp.h"
190
191 #define IND (COLOR|TEX0|SPEC|FOG|POINT)
192 #define TAG(x) x##_color_tex0_spec_fog_point
193 #include "ss_vbtmp.h"
194
195 #define IND (COLOR|MULTITEX|POINT)
196 #define TAG(x) x##_color_multitex_point
197 #include "ss_vbtmp.h"
198
199 #define IND (COLOR|MULTITEX|SPEC|POINT)
200 #define TAG(x) x##_color_multitex_spec_point
201 #include "ss_vbtmp.h"
202
203 #define IND (COLOR|MULTITEX|FOG|POINT)
204 #define TAG(x) x##_color_multitex_fog_point
205 #include "ss_vbtmp.h"
206
207 #define IND (COLOR|MULTITEX|SPEC|FOG|POINT)
208 #define TAG(x) x##_color_multitex_spec_fog_point
209 #include "ss_vbtmp.h"
210
211 #define IND (INDEX)
212 #define TAG(x) x##_index
213 #include "ss_vbtmp.h"
214
215 #define IND (INDEX|FOG)
216 #define TAG(x) x##_index_fog
217 #include "ss_vbtmp.h"
218
219 #define IND (INDEX|POINT)
220 #define TAG(x) x##_index_point
221 #include "ss_vbtmp.h"
222
223 #define IND (INDEX|FOG|POINT)
224 #define TAG(x) x##_index_fog_point
225 #include "ss_vbtmp.h"
226
227
228 /***********************************************************************
229 * Additional setup and interp for back color and edgeflag.
230 ***********************************************************************/
231
232 #define GET_COLOR(ptr, idx) (((GLfloat (*)[4])((ptr)->data))[idx])
233
234 static void interp_extras( GLcontext *ctx,
235 GLfloat t,
236 GLuint dst, GLuint out, GLuint in,
237 GLboolean force_boundary )
238 {
239 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
240
241 if (VB->ColorPtr[1]) {
242 INTERP_4F( t,
243 GET_COLOR(VB->ColorPtr[1], dst),
244 GET_COLOR(VB->ColorPtr[1], out),
245 GET_COLOR(VB->ColorPtr[1], in) );
246
247 if (VB->SecondaryColorPtr[1]) {
248 INTERP_3F( t,
249 GET_COLOR(VB->SecondaryColorPtr[1], dst),
250 GET_COLOR(VB->SecondaryColorPtr[1], out),
251 GET_COLOR(VB->SecondaryColorPtr[1], in) );
252 }
253 }
254 else if (VB->IndexPtr[1]) {
255 VB->IndexPtr[1]->data[dst][0] = LINTERP( t,
256 VB->IndexPtr[1]->data[out][0],
257 VB->IndexPtr[1]->data[in][0] );
258 }
259
260 if (VB->EdgeFlag) {
261 VB->EdgeFlag[dst] = VB->EdgeFlag[out] || force_boundary;
262 }
263
264 interp_tab[SWSETUP_CONTEXT(ctx)->SetupIndex](ctx, t, dst, out, in,
265 force_boundary);
266 }
267
268 static void copy_pv_extras( GLcontext *ctx, GLuint dst, GLuint src )
269 {
270 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
271
272 if (VB->ColorPtr[1]) {
273 COPY_4FV( GET_COLOR(VB->ColorPtr[1], dst),
274 GET_COLOR(VB->ColorPtr[1], src) );
275
276 if (VB->SecondaryColorPtr[1]) {
277 COPY_3V( GET_COLOR(VB->SecondaryColorPtr[1], dst),
278 GET_COLOR(VB->SecondaryColorPtr[1], src) );
279 }
280 }
281 else if (VB->IndexPtr[1]) {
282 VB->IndexPtr[1]->data[dst][0] = VB->IndexPtr[1]->data[src][0];
283 }
284
285 copy_pv_tab[SWSETUP_CONTEXT(ctx)->SetupIndex](ctx, dst, src);
286 }
287
288
289
290
291 /***********************************************************************
292 * Initialization
293 ***********************************************************************/
294
295 static void
296 emit_invalid( GLcontext *ctx, GLuint start, GLuint end, GLuint newinputs )
297 {
298 _mesa_debug(ctx, "swrast_setup: invalid setup function\n");
299 (void) (ctx && start && end && newinputs);
300 }
301
302
303 static void
304 interp_invalid( GLcontext *ctx, GLfloat t,
305 GLuint edst, GLuint eout, GLuint ein,
306 GLboolean force_boundary )
307 {
308 _mesa_debug(ctx, "swrast_setup: invalid interp function\n");
309 (void) (ctx && t && edst && eout && ein && force_boundary);
310 }
311
312
313 static void
314 copy_pv_invalid( GLcontext *ctx, GLuint edst, GLuint esrc )
315 {
316 _mesa_debug(ctx, "swrast_setup: invalid copy_pv function\n");
317 (void) (ctx && edst && esrc );
318 }
319
320
321 static void init_standard( void )
322 {
323 GLuint i;
324
325 for (i = 0 ; i < Elements(setup_tab) ; i++) {
326 setup_tab[i] = emit_invalid;
327 interp_tab[i] = interp_invalid;
328 copy_pv_tab[i] = copy_pv_invalid;
329 }
330
331 init_none();
332 init_color();
333 init_color_spec();
334 init_color_fog();
335 init_color_spec_fog();
336 init_color_tex0();
337 init_color_tex0_spec();
338 init_color_tex0_fog();
339 init_color_tex0_spec_fog();
340 init_color_multitex();
341 init_color_multitex_spec();
342 init_color_multitex_fog();
343 init_color_multitex_spec_fog();
344 init_color_point();
345 init_color_spec_point();
346 init_color_fog_point();
347 init_color_spec_fog_point();
348 init_color_tex0_point();
349 init_color_tex0_spec_point();
350 init_color_tex0_fog_point();
351 init_color_tex0_spec_fog_point();
352 init_color_multitex_point();
353 init_color_multitex_spec_point();
354 init_color_multitex_fog_point();
355 init_color_multitex_spec_fog_point();
356 init_index();
357 init_index_fog();
358 init_index_point();
359 init_index_fog_point();
360 }
361
362
363 /* debug only */
364 #if 0
365 static void
366 printSetupFlags(const GLcontext *ctx, char *msg, GLuint flags )
367 {
368 _mesa_debug(ctx, "%s(%x): %s%s%s%s%s%s%s\n",
369 msg,
370 (int) flags,
371 (flags & COLOR) ? "color, " : "",
372 (flags & INDEX) ? "index, " : "",
373 (flags & TEX0) ? "tex0, " : "",
374 (flags & MULTITEX) ? "multitex, " : "",
375 (flags & SPEC) ? "spec, " : "",
376 (flags & FOG) ? "fog, " : "",
377 (flags & POINT) ? "point, " : "");
378 }
379 #endif
380
381 void
382 _swsetup_choose_rastersetup_func(GLcontext *ctx)
383 {
384 SScontext *swsetup = SWSETUP_CONTEXT(ctx);
385 TNLcontext *tnl = TNL_CONTEXT(ctx);
386 int funcindex = 0;
387
388 if (ctx->RenderMode == GL_RENDER) {
389 if (ctx->Visual.rgbMode) {
390 funcindex = COLOR;
391
392 if (ctx->Texture._EnabledCoordUnits > 1)
393 funcindex |= MULTITEX; /* a unit above unit[0] is enabled */
394 else if (ctx->Texture._EnabledCoordUnits == 1)
395 funcindex |= TEX0; /* only unit 0 is enabled */
396
397 if (NEED_SECONDARY_COLOR(ctx))
398 funcindex |= SPEC;
399 }
400 else {
401 funcindex = INDEX;
402 }
403
404 if (ctx->Point._Attenuated ||
405 (ctx->VertexProgram.Enabled && ctx->VertexProgram.PointSizeEnabled))
406 funcindex |= POINT;
407
408 if (ctx->Fog.Enabled)
409 funcindex |= FOG;
410 }
411 else if (ctx->RenderMode == GL_FEEDBACK) {
412 if (ctx->Visual.rgbMode)
413 funcindex = (COLOR | TEX0); /* is feedback color subject to fogging? */
414 else
415 funcindex = INDEX;
416 }
417 else
418 funcindex = 0;
419
420 swsetup->SetupIndex = funcindex;
421 tnl->Driver.Render.BuildVertices = setup_tab[funcindex];
422
423 if (NEED_TWO_SIDED_LIGHTING(ctx) ||
424 ctx->Polygon.FrontMode != GL_FILL ||
425 ctx->Polygon.BackMode != GL_FILL) {
426 tnl->Driver.Render.Interp = interp_extras;
427 tnl->Driver.Render.CopyPV = copy_pv_extras;
428 }
429 else {
430 tnl->Driver.Render.Interp = interp_tab[funcindex];
431 tnl->Driver.Render.CopyPV = copy_pv_tab[funcindex];
432 }
433
434 ASSERT(tnl->Driver.Render.BuildVertices);
435 ASSERT(tnl->Driver.Render.BuildVertices != emit_invalid);
436 }
437
438
439 void
440 _swsetup_vb_init( GLcontext *ctx )
441 {
442 (void) ctx;
443 init_standard();
444 /*
445 printSetupFlags(ctx);
446 */
447 }
448