Klaus's latest patches and some clean-up
[mesa.git] / src / mesa / swrast / s_triangle.c
1 /* $Id: s_triangle.c,v 1.48 2002/01/21 18:12:34 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 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 * When the device driver doesn't implement triangle rasterization it
30 * can hook in _swrast_Triangle, which eventually calls one of these
31 * functions to draw triangles.
32 */
33
34 #include "glheader.h"
35 #include "context.h"
36 #include "colormac.h"
37 #include "macros.h"
38 #include "mem.h"
39 #include "mmath.h"
40 #include "texformat.h"
41 #include "teximage.h"
42 #include "texstate.h"
43
44 #include "s_aatriangle.h"
45 #include "s_context.h"
46 #include "s_depth.h"
47 #include "s_feedback.h"
48 #include "s_span.h"
49 #include "s_triangle.h"
50
51
52
53 GLboolean _mesa_cull_triangle( GLcontext *ctx,
54 const SWvertex *v0,
55 const SWvertex *v1,
56 const SWvertex *v2 )
57 {
58 GLfloat ex = v1->win[0] - v0->win[0];
59 GLfloat ey = v1->win[1] - v0->win[1];
60 GLfloat fx = v2->win[0] - v0->win[0];
61 GLfloat fy = v2->win[1] - v0->win[1];
62 GLfloat c = ex*fy-ey*fx;
63
64 if (c * SWRAST_CONTEXT(ctx)->_backface_sign > 0)
65 return 0;
66
67 return 1;
68 }
69
70
71
72 /*
73 * Render a flat-shaded color index triangle.
74 */
75 static void flat_ci_triangle( GLcontext *ctx,
76 const SWvertex *v0,
77 const SWvertex *v1,
78 const SWvertex *v2 )
79 {
80 #define INTERP_Z 1
81 #define INTERP_FOG 1
82
83 #define RENDER_SPAN( span ) \
84 _mesa_write_monoindex_span(ctx, &span, v2->index, GL_POLYGON );
85
86 #include "s_tritemp.h"
87 }
88
89
90
91 /*
92 * Render a smooth-shaded color index triangle.
93 */
94 static void smooth_ci_triangle( GLcontext *ctx,
95 const SWvertex *v0,
96 const SWvertex *v1,
97 const SWvertex *v2 )
98 {
99 #define INTERP_Z 1
100 #define INTERP_FOG 1
101 #define INTERP_INDEX 1
102
103 #define RENDER_SPAN( span ) \
104 GLuint i; \
105 SW_SPAN_SET_FLAG(span.filledColor); \
106 for (i = 0; i < span.end; i++) { \
107 span.color.index[i] = FixedToInt(span.index); \
108 span.index += span.indexStep; \
109 } \
110 _mesa_write_index_span(ctx, &span, NULL, GL_POLYGON);
111
112 #include "s_tritemp.h"
113 }
114
115
116
117 /*
118 * Render a flat-shaded RGBA triangle.
119 */
120 static void flat_rgba_triangle( GLcontext *ctx,
121 const SWvertex *v0,
122 const SWvertex *v1,
123 const SWvertex *v2 )
124 {
125 #define INTERP_Z 1
126 #define INTERP_FOG 1
127 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
128
129 #define RENDER_SPAN( span ) \
130 _mesa_write_monocolor_span(ctx, &span, v2->color, GL_POLYGON );
131
132 #include "s_tritemp.h"
133
134 ASSERT(!ctx->Texture._ReallyEnabled); /* texturing must be off */
135 ASSERT(ctx->Light.ShadeModel==GL_FLAT);
136 }
137
138
139
140 /*
141 * Render a smooth-shaded RGBA triangle.
142 */
143 static void smooth_rgba_triangle( GLcontext *ctx,
144 const SWvertex *v0,
145 const SWvertex *v1,
146 const SWvertex *v2 )
147 {
148
149 #define INTERP_Z 1
150 #define INTERP_FOG 1
151 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
152 #define INTERP_RGB 1
153 #define INTERP_ALPHA 1
154
155 #define RENDER_SPAN( span ) \
156 GLuint i; \
157 SW_SPAN_SET_FLAG(span.filledColor); \
158 SW_SPAN_SET_FLAG(span.filledAlpha); \
159 for (i = 0; i < span.end; i++) { \
160 span.color.rgba[i][RCOMP] = FixedToChan(span.red); \
161 span.color.rgba[i][GCOMP] = FixedToChan(span.green); \
162 span.color.rgba[i][BCOMP] = FixedToChan(span.blue); \
163 span.color.rgba[i][ACOMP] = FixedToChan(span.alpha); \
164 span.red += span.redStep; \
165 span.green += span.greenStep; \
166 span.blue += span.blueStep; \
167 span.alpha += span.alphaStep; \
168 } \
169 _mesa_write_rgba_span(ctx, &span, NULL, GL_POLYGON);
170
171 #include "s_tritemp.h"
172
173 ASSERT(!ctx->Texture._ReallyEnabled); /* texturing must be off */
174 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH);
175 }
176
177
178 /*
179 * Render an RGB, GL_DECAL, textured triangle.
180 * Interpolate S,T only w/out mipmapping or perspective correction.
181 *
182 * No fog.
183 */
184 static void simple_textured_triangle( GLcontext *ctx,
185 const SWvertex *v0,
186 const SWvertex *v1,
187 const SWvertex *v2 )
188 {
189 #define INTERP_INT_TEX 1
190 #define S_SCALE twidth
191 #define T_SCALE theight
192
193 #define SETUP_CODE \
194 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
195 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
196 const GLint b = obj->BaseLevel; \
197 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
198 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
199 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
200 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
201 const GLint smask = obj->Image[b]->Width - 1; \
202 const GLint tmask = obj->Image[b]->Height - 1; \
203 if (!texture) { \
204 /* this shouldn't happen */ \
205 return; \
206 }
207
208 #define RENDER_SPAN( span ) \
209 GLuint i; \
210 SW_SPAN_SET_FLAG(span.filledColor); \
211 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
212 span.intTex[1] -= FIXED_HALF; \
213 for (i = 0; i < span.end; i++) { \
214 GLint s = FixedToInt(span.intTex[0]) & smask; \
215 GLint t = FixedToInt(span.intTex[1]) & tmask; \
216 GLint pos = (t << twidth_log2) + s; \
217 pos = pos + pos + pos; /* multiply by 3 */ \
218 span.color.rgb[i][RCOMP] = texture[pos]; \
219 span.color.rgb[i][GCOMP] = texture[pos+1]; \
220 span.color.rgb[i][BCOMP] = texture[pos+2]; \
221 span.intTex[0] += span.intTexStep[0]; \
222 span.intTex[1] += span.intTexStep[1]; \
223 } \
224 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
225 (CONST GLchan (*)[3]) span.color.rgb, \
226 NULL );
227
228 #include "s_tritemp.h"
229 }
230
231
232 /*
233 * Render an RGB, GL_DECAL, textured triangle.
234 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
235 * perspective correction.
236 *
237 * No fog.
238 */
239 static void simple_z_textured_triangle( GLcontext *ctx,
240 const SWvertex *v0,
241 const SWvertex *v1,
242 const SWvertex *v2 )
243 {
244 #define INTERP_Z 1
245 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
246 #define INTERP_INT_TEX 1
247 #define S_SCALE twidth
248 #define T_SCALE theight
249
250 #define SETUP_CODE \
251 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
252 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
253 const GLint b = obj->BaseLevel; \
254 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
255 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
256 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
257 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
258 const GLint smask = obj->Image[b]->Width - 1; \
259 const GLint tmask = obj->Image[b]->Height - 1; \
260 if (!texture) { \
261 /* this shouldn't happen */ \
262 return; \
263 }
264
265 #define RENDER_SPAN( span ) \
266 GLuint i; \
267 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
268 span.intTex[1] -= FIXED_HALF; \
269 SW_SPAN_SET_FLAG(span.filledColor); \
270 for (i = 0; i < span.end; i++) { \
271 const GLdepth z = FixedToDepth(span.z); \
272 if (z < zRow[i]) { \
273 GLint s = FixedToInt(span.intTex[0]) & smask; \
274 GLint t = FixedToInt(span.intTex[1]) & tmask; \
275 GLint pos = (t << twidth_log2) + s; \
276 pos = pos + pos + pos; /* multiply by 3 */ \
277 span.color.rgb[i][RCOMP] = texture[pos]; \
278 span.color.rgb[i][GCOMP] = texture[pos+1]; \
279 span.color.rgb[i][BCOMP] = texture[pos+2]; \
280 zRow[i] = z; \
281 span.mask[i] = 1; \
282 } \
283 else { \
284 span.mask[i] = 0; \
285 } \
286 span.intTex[0] += span.intTexStep[0]; \
287 span.intTex[1] += span.intTexStep[1]; \
288 span.z += span.zStep; \
289 } \
290 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
291 (CONST GLchan (*)[3]) span.color.rgb, \
292 span.mask );
293
294 #include "s_tritemp.h"
295 }
296
297
298 #if CHAN_TYPE != GL_FLOAT
299
300 struct affine_info
301 {
302 GLenum filter;
303 GLenum format;
304 GLenum envmode;
305 GLint smask, tmask;
306 GLint twidth_log2;
307 const GLchan *texture;
308 GLfixed er, eg, eb, ea;
309 GLint tbytesline, tsize;
310 };
311
312
313 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
314 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
315 * texture env modes.
316 */
317 static INLINE void
318 affine_span(GLcontext *ctx, struct sw_span *span,
319 struct affine_info *info)
320 {
321 GLchan sample[4]; /* the filtered texture sample */
322
323 /* Instead of defining a function for each mode, a test is done
324 * between the outer and inner loops. This is to reduce code size
325 * and complexity. Observe that an optimizing compiler kills
326 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
327 */
328
329 #define NEAREST_RGB \
330 sample[RCOMP] = tex00[RCOMP]; \
331 sample[GCOMP] = tex00[GCOMP]; \
332 sample[BCOMP] = tex00[BCOMP]; \
333 sample[ACOMP] = CHAN_MAX
334
335 #define LINEAR_RGB \
336 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
337 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
338 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
339 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
340 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
341 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
342 sample[ACOMP] = CHAN_MAX
343
344 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
345
346 #define LINEAR_RGBA \
347 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
348 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT;\
349 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
350 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT;\
351 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
352 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT;\
353 sample[ACOMP] = (ti * (si * tex00[3] + sf * tex01[3]) + \
354 tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT
355
356 #define MODULATE \
357 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
358 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
359 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
360 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
361
362 #define DECAL \
363 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
364 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
365 >> (FIXED_SHIFT + 8); \
366 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
367 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
368 >> (FIXED_SHIFT + 8); \
369 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
370 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
371 >> (FIXED_SHIFT + 8); \
372 dest[ACOMP] = FixedToInt(span->alpha)
373
374 #define BLEND \
375 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
376 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
377 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
378 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
379 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
380 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
381 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
382
383 #define REPLACE COPY_CHAN4(dest, sample)
384
385 #define ADD \
386 { \
387 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
388 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
389 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
390 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
391 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
392 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
393 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
394 }
395
396 /* shortcuts */
397
398 #define NEAREST_RGB_REPLACE \
399 NEAREST_RGB; \
400 dest[0] = sample[0]; \
401 dest[1] = sample[1]; \
402 dest[2] = sample[2]; \
403 dest[3] = FixedToInt(span->alpha);
404
405 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
406
407 #define SPAN_NEAREST(DO_TEX,COMP) \
408 for (i = 0; i < span->end; i++) { \
409 /* Isn't it necessary to use FixedFloor below?? */ \
410 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
411 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
412 GLint pos = (t << info->twidth_log2) + s; \
413 const GLchan *tex00 = info->texture + COMP * pos; \
414 DO_TEX; \
415 span->red += span->redStep; \
416 span->green += span->greenStep; \
417 span->blue += span->blueStep; \
418 span->alpha += span->alphaStep; \
419 span->intTex[0] += span->intTexStep[0]; \
420 span->intTex[1] += span->intTexStep[1]; \
421 dest += 4; \
422 }
423
424 #define SPAN_LINEAR(DO_TEX,COMP) \
425 for (i = 0; i < span->end; i++) { \
426 /* Isn't it necessary to use FixedFloor below?? */ \
427 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
428 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
429 GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
430 GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
431 GLfixed si = FIXED_FRAC_MASK - sf; \
432 GLfixed ti = FIXED_FRAC_MASK - tf; \
433 GLint pos = (t << info->twidth_log2) + s; \
434 const GLchan *tex00 = info->texture + COMP * pos; \
435 const GLchan *tex10 = tex00 + info->tbytesline; \
436 const GLchan *tex01 = tex00 + COMP; \
437 const GLchan *tex11 = tex10 + COMP; \
438 (void) ti; \
439 (void) si; \
440 if (t == info->tmask) { \
441 tex10 -= info->tsize; \
442 tex11 -= info->tsize; \
443 } \
444 if (s == info->smask) { \
445 tex01 -= info->tbytesline; \
446 tex11 -= info->tbytesline; \
447 } \
448 DO_TEX; \
449 span->red += span->redStep; \
450 span->green += span->greenStep; \
451 span->blue += span->blueStep; \
452 span->alpha += span->alphaStep; \
453 span->intTex[0] += span->intTexStep[0]; \
454 span->intTex[1] += span->intTexStep[1]; \
455 dest += 4; \
456 }
457
458
459 GLuint i;
460 GLchan *dest = span->color.rgba[0];
461
462 SW_SPAN_SET_FLAG(span->filledColor);
463
464 span->intTex[0] -= FIXED_HALF;
465 span->intTex[1] -= FIXED_HALF;
466 switch (info->filter) {
467 case GL_NEAREST:
468 switch (info->format) {
469 case GL_RGB:
470 switch (info->envmode) {
471 case GL_MODULATE:
472 SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
473 break;
474 case GL_DECAL:
475 case GL_REPLACE:
476 SPAN_NEAREST(NEAREST_RGB_REPLACE,3);
477 break;
478 case GL_BLEND:
479 SPAN_NEAREST(NEAREST_RGB;BLEND,3);
480 break;
481 case GL_ADD:
482 SPAN_NEAREST(NEAREST_RGB;ADD,3);
483 break;
484 default:
485 abort();
486 }
487 break;
488 case GL_RGBA:
489 switch(info->envmode) {
490 case GL_MODULATE:
491 SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
492 break;
493 case GL_DECAL:
494 SPAN_NEAREST(NEAREST_RGBA;DECAL,4);
495 break;
496 case GL_BLEND:
497 SPAN_NEAREST(NEAREST_RGBA;BLEND,4);
498 break;
499 case GL_ADD:
500 SPAN_NEAREST(NEAREST_RGBA;ADD,4);
501 break;
502 case GL_REPLACE:
503 SPAN_NEAREST(NEAREST_RGBA_REPLACE,4);
504 break;
505 default:
506 abort();
507 }
508 break;
509 }
510 break;
511
512 case GL_LINEAR:
513 span->intTex[0] -= FIXED_HALF;
514 span->intTex[1] -= FIXED_HALF;
515 switch (info->format) {
516 case GL_RGB:
517 switch (info->envmode) {
518 case GL_MODULATE:
519 SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
520 break;
521 case GL_DECAL:
522 case GL_REPLACE:
523 SPAN_LINEAR(LINEAR_RGB;REPLACE,3);
524 break;
525 case GL_BLEND:
526 SPAN_LINEAR(LINEAR_RGB;BLEND,3);
527 break;
528 case GL_ADD:
529 SPAN_LINEAR(LINEAR_RGB;ADD,3);
530 break;
531 default:
532 abort();
533 }
534 break;
535 case GL_RGBA:
536 switch (info->envmode) {
537 case GL_MODULATE:
538 SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
539 break;
540 case GL_DECAL:
541 SPAN_LINEAR(LINEAR_RGBA;DECAL,4);
542 break;
543 case GL_BLEND:
544 SPAN_LINEAR(LINEAR_RGBA;BLEND,4);
545 break;
546 case GL_ADD:
547 SPAN_LINEAR(LINEAR_RGBA;ADD,4);
548 break;
549 case GL_REPLACE:
550 SPAN_LINEAR(LINEAR_RGBA;REPLACE,4);
551 break;
552 default:
553 abort();
554 } break;
555 }
556 break;
557 }
558 _mesa_write_rgba_span(ctx, span, NULL, GL_POLYGON);
559
560 #undef SPAN_NEAREST
561 #undef SPAN_LINEAR
562 }
563
564
565
566 /*
567 * Render an RGB/RGBA textured triangle without perspective correction.
568 */
569 static void affine_textured_triangle( GLcontext *ctx,
570 const SWvertex *v0,
571 const SWvertex *v1,
572 const SWvertex *v2 )
573 {
574 #define INTERP_Z 1
575 #define INTERP_FOG 1
576 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
577 #define INTERP_RGB 1
578 #define INTERP_ALPHA 1
579 #define INTERP_INT_TEX 1
580 #define S_SCALE twidth
581 #define T_SCALE theight
582
583 #define SETUP_CODE \
584 struct affine_info info; \
585 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
586 struct gl_texture_object *obj = unit->Current2D; \
587 const GLint b = obj->BaseLevel; \
588 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
589 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
590 info.texture = (const GLchan *) obj->Image[b]->Data; \
591 info.twidth_log2 = obj->Image[b]->WidthLog2; \
592 info.smask = obj->Image[b]->Width - 1; \
593 info.tmask = obj->Image[b]->Height - 1; \
594 info.format = obj->Image[b]->Format; \
595 info.filter = obj->MinFilter; \
596 info.envmode = unit->EnvMode; \
597 \
598 if (info.envmode == GL_BLEND) { \
599 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
600 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
601 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
602 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
603 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
604 } \
605 if (!info.texture) { \
606 /* this shouldn't happen */ \
607 return; \
608 } \
609 \
610 switch (info.format) { \
611 case GL_ALPHA: \
612 case GL_LUMINANCE: \
613 case GL_INTENSITY: \
614 info.tbytesline = obj->Image[b]->Width; \
615 break; \
616 case GL_LUMINANCE_ALPHA: \
617 info.tbytesline = obj->Image[b]->Width * 2; \
618 break; \
619 case GL_RGB: \
620 info.tbytesline = obj->Image[b]->Width * 3; \
621 break; \
622 case GL_RGBA: \
623 info.tbytesline = obj->Image[b]->Width * 4; \
624 break; \
625 default: \
626 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
627 return; \
628 } \
629 info.tsize = obj->Image[b]->Height * info.tbytesline;
630
631 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
632
633 #include "s_tritemp.h"
634
635 }
636
637
638
639 struct persp_info
640 {
641 GLenum filter;
642 GLenum format;
643 GLenum envmode;
644 GLint smask, tmask;
645 GLint twidth_log2;
646 const GLchan *texture;
647 GLfixed er, eg, eb, ea; /* texture env color */
648 GLint tbytesline, tsize;
649 };
650
651
652 static INLINE void
653 fast_persp_span(GLcontext *ctx, struct sw_span *span,
654 struct persp_info *info)
655 {
656 GLchan sample[4]; /* the filtered texture sample */
657
658 /* Instead of defining a function for each mode, a test is done
659 * between the outer and inner loops. This is to reduce code size
660 * and complexity. Observe that an optimizing compiler kills
661 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
662 */
663 #define SPAN_NEAREST(DO_TEX,COMP) \
664 for (i = 0; i < span->end; i++) { \
665 GLdouble invQ = tex_coord[2] ? \
666 (1.0 / tex_coord[2]) : 1.0; \
667 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
668 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
669 GLint s = IFLOOR(s_tmp) & info->smask; \
670 GLint t = IFLOOR(t_tmp) & info->tmask; \
671 GLint pos = (t << info->twidth_log2) + s; \
672 const GLchan *tex00 = info->texture + COMP * pos; \
673 DO_TEX; \
674 span->red += span->redStep; \
675 span->green += span->greenStep; \
676 span->blue += span->blueStep; \
677 span->alpha += span->alphaStep; \
678 tex_coord[0] += tex_step[0]; \
679 tex_coord[1] += tex_step[1]; \
680 tex_coord[2] += tex_step[2]; \
681 dest += 4; \
682 }
683
684 #define SPAN_LINEAR(DO_TEX,COMP) \
685 for (i = 0; i < span->end; i++) { \
686 GLdouble invQ = tex_coord[2] ? \
687 (1.0 / tex_coord[2]) : 1.0; \
688 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
689 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
690 GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
691 GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
692 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
693 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
694 GLfixed sf = s_fix & FIXED_FRAC_MASK; \
695 GLfixed tf = t_fix & FIXED_FRAC_MASK; \
696 GLfixed si = FIXED_FRAC_MASK - sf; \
697 GLfixed ti = FIXED_FRAC_MASK - tf; \
698 GLint pos = (t << info->twidth_log2) + s; \
699 const GLchan *tex00 = info->texture + COMP * pos; \
700 const GLchan *tex10 = tex00 + info->tbytesline; \
701 const GLchan *tex01 = tex00 + COMP; \
702 const GLchan *tex11 = tex10 + COMP; \
703 (void) ti; \
704 (void) si; \
705 if (t == info->tmask) { \
706 tex10 -= info->tsize; \
707 tex11 -= info->tsize; \
708 } \
709 if (s == info->smask) { \
710 tex01 -= info->tbytesline; \
711 tex11 -= info->tbytesline; \
712 } \
713 DO_TEX; \
714 span->red += span->redStep; \
715 span->green += span->greenStep; \
716 span->blue += span->blueStep; \
717 span->alpha += span->alphaStep; \
718 tex_coord[0] += tex_step[0]; \
719 tex_coord[1] += tex_step[1]; \
720 tex_coord[2] += tex_step[2]; \
721 dest += 4; \
722 }
723
724 GLuint i;
725 GLfloat tex_coord[3], tex_step[3];
726 GLchan *dest = span->color.rgba[0];
727
728 SW_SPAN_SET_FLAG(span->filledColor);
729
730 tex_coord[0] = span->tex[0][0] * (info->smask + 1),
731 tex_step[0] = span->texStep[0][0] * (info->smask + 1);
732 tex_coord[1] = span->tex[0][1] * (info->tmask + 1),
733 tex_step[1] = span->texStep[0][1] * (info->tmask + 1);
734 /* span->tex[0][2] only if 3D-texturing, here only 2D */
735 tex_coord[2] = span->tex[0][3],
736 tex_step[2] = span->texStep[0][3];
737
738 switch (info->filter) {
739 case GL_NEAREST:
740 switch (info->format) {
741 case GL_RGB:
742 switch (info->envmode) {
743 case GL_MODULATE:
744 SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
745 break;
746 case GL_DECAL:
747 case GL_REPLACE:
748 SPAN_NEAREST(NEAREST_RGB_REPLACE,3);
749 break;
750 case GL_BLEND:
751 SPAN_NEAREST(NEAREST_RGB;BLEND,3);
752 break;
753 case GL_ADD:
754 SPAN_NEAREST(NEAREST_RGB;ADD,3);
755 break;
756 default:
757 abort();
758 }
759 break;
760 case GL_RGBA:
761 switch(info->envmode) {
762 case GL_MODULATE:
763 SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
764 break;
765 case GL_DECAL:
766 SPAN_NEAREST(NEAREST_RGBA;DECAL,4);
767 break;
768 case GL_BLEND:
769 SPAN_NEAREST(NEAREST_RGBA;BLEND,4);
770 break;
771 case GL_ADD:
772 SPAN_NEAREST(NEAREST_RGBA;ADD,4);
773 break;
774 case GL_REPLACE:
775 SPAN_NEAREST(NEAREST_RGBA_REPLACE,4);
776 break;
777 default:
778 abort();
779 }
780 break;
781 }
782 break;
783
784 case GL_LINEAR:
785 switch (info->format) {
786 case GL_RGB:
787 switch (info->envmode) {
788 case GL_MODULATE:
789 SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
790 break;
791 case GL_DECAL:
792 case GL_REPLACE:
793 SPAN_LINEAR(LINEAR_RGB;REPLACE,3);
794 break;
795 case GL_BLEND:
796 SPAN_LINEAR(LINEAR_RGB;BLEND,3);
797 break;
798 case GL_ADD:
799 SPAN_LINEAR(LINEAR_RGB;ADD,3);
800 break;
801 default:
802 abort();
803 }
804 break;
805 case GL_RGBA:
806 switch (info->envmode) {
807 case GL_MODULATE:
808 SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
809 break;
810 case GL_DECAL:
811 SPAN_LINEAR(LINEAR_RGBA;DECAL,4);
812 break;
813 case GL_BLEND:
814 SPAN_LINEAR(LINEAR_RGBA;BLEND,4);
815 break;
816 case GL_ADD:
817 SPAN_LINEAR(LINEAR_RGBA;ADD,4);
818 break;
819 case GL_REPLACE:
820 SPAN_LINEAR(LINEAR_RGBA;REPLACE,4);
821 break;
822 default:
823 abort();
824 }
825 break;
826 }
827 break;
828 }
829
830 _mesa_write_rgba_span(ctx, span, NULL, GL_POLYGON);
831
832
833 #undef SPAN_NEAREST
834 #undef SPAN_LINEAR
835 }
836
837
838 /*
839 * Render an perspective corrected RGB/RGBA textured triangle.
840 * The Q (aka V in Mesa) coordinate must be zero such that the divide
841 * by interpolated Q/W comes out right.
842 *
843 */
844 static void persp_textured_triangle( GLcontext *ctx,
845 const SWvertex *v0,
846 const SWvertex *v1,
847 const SWvertex *v2 )
848 {
849 #define INTERP_Z 1
850 #define INTERP_FOG 1
851 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
852 #define INTERP_RGB 1
853 #define INTERP_ALPHA 1
854 #define INTERP_TEX 1
855
856 #define SETUP_CODE \
857 struct persp_info info; \
858 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
859 const struct gl_texture_object *obj = unit->Current2D; \
860 const GLint b = obj->BaseLevel; \
861 info.texture = (const GLchan *) obj->Image[b]->Data; \
862 info.twidth_log2 = obj->Image[b]->WidthLog2; \
863 info.smask = obj->Image[b]->Width - 1; \
864 info.tmask = obj->Image[b]->Height - 1; \
865 info.format = obj->Image[b]->Format; \
866 info.filter = obj->MinFilter; \
867 info.envmode = unit->EnvMode; \
868 \
869 if (info.envmode == GL_BLEND) { \
870 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
871 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
872 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
873 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
874 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
875 } \
876 if (!info.texture) { \
877 /* this shouldn't happen */ \
878 return; \
879 } \
880 \
881 switch (info.format) { \
882 case GL_ALPHA: \
883 case GL_LUMINANCE: \
884 case GL_INTENSITY: \
885 info.tbytesline = obj->Image[b]->Width; \
886 break; \
887 case GL_LUMINANCE_ALPHA: \
888 info.tbytesline = obj->Image[b]->Width * 2; \
889 break; \
890 case GL_RGB: \
891 info.tbytesline = obj->Image[b]->Width * 3; \
892 break; \
893 case GL_RGBA: \
894 info.tbytesline = obj->Image[b]->Width * 4; \
895 break; \
896 default: \
897 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
898 return; \
899 } \
900 info.tsize = obj->Image[b]->Height * info.tbytesline;
901
902 #define RENDER_SPAN( span ) fast_persp_span(ctx, &span, &info);
903
904 #include "s_tritemp.h"
905
906 }
907
908
909 #endif /* CHAN_BITS != GL_FLOAT */
910
911
912
913
914 /*
915 * Render a smooth-shaded, textured, RGBA triangle.
916 * Interpolate S,T,R with perspective correction, w/out mipmapping.
917 */
918 static void general_textured_triangle( GLcontext *ctx,
919 const SWvertex *v0,
920 const SWvertex *v1,
921 const SWvertex *v2 )
922 {
923 #define INTERP_Z 1
924 #define INTERP_FOG 1
925 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
926 #define INTERP_RGB 1
927 #define INTERP_ALPHA 1
928 #define INTERP_TEX 1
929
930 #define SETUP_CODE \
931 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
932 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
933 span.texWidth[0] = (GLfloat) texImage->Width; \
934 span.texHeight[0] = (GLfloat) texImage->Height; \
935 (void) fixedToDepthShift;
936
937 #define RENDER_SPAN( span ) \
938 GLuint i; \
939 SW_SPAN_SET_FLAG(span.filledColor); \
940 SW_SPAN_SET_FLAG(span.filledAlpha); \
941 SW_SPAN_SET_FLAG(span.filledTex[0]); \
942 /* NOTE: we could just call rasterize_span() here instead */ \
943 for (i = 0; i < span.end; i++) { \
944 GLdouble invQ = span.tex[0][3] ? (1.0 / span.tex[0][3]) : 1.0; \
945 span.depth[i] = FixedToDepth(span.z); \
946 span.z += span.zStep; \
947 span.color.rgba[i][RCOMP] = FixedToChan(span.red); \
948 span.color.rgba[i][GCOMP] = FixedToChan(span.green); \
949 span.color.rgba[i][BCOMP] = FixedToChan(span.blue); \
950 span.color.rgba[i][ACOMP] = FixedToChan(span.alpha); \
951 span.red += span.redStep; \
952 span.green += span.greenStep; \
953 span.blue += span.blueStep; \
954 span.alpha += span.alphaStep; \
955 span.texcoords[0][i][0] = (GLfloat) (span.tex[0][0] * invQ); \
956 span.texcoords[0][i][1] = (GLfloat) (span.tex[0][1] * invQ); \
957 span.texcoords[0][i][2] = (GLfloat) (span.tex[0][2] * invQ); \
958 span.tex[0][0] += span.texStep[0][0]; \
959 span.tex[0][1] += span.texStep[0][1]; \
960 span.tex[0][2] += span.texStep[0][2]; \
961 span.tex[0][3] += span.texStep[0][3]; \
962 } \
963 _mesa_write_texture_span( ctx, &span, NULL, GL_POLYGON );
964
965 #include "s_tritemp.h"
966 }
967
968
969 /*
970 * Render a smooth-shaded, textured, RGBA triangle with separate specular
971 * color interpolation.
972 * Interpolate texcoords with perspective correction, w/out mipmapping.
973 */
974 static void general_textured_spec_triangle( GLcontext *ctx,
975 const SWvertex *v0,
976 const SWvertex *v1,
977 const SWvertex *v2 )
978 {
979 #define INTERP_Z 1
980 #define INTERP_FOG 1
981 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
982 #define INTERP_RGB 1
983 #define INTERP_SPEC 1
984 #define INTERP_ALPHA 1
985 #define INTERP_TEX 1
986
987 #define SETUP_CODE \
988 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
989 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
990 span.texWidth[0] = (GLfloat) texImage->Width; \
991 span.texHeight[0] = (GLfloat) texImage->Height; \
992 (void) fixedToDepthShift;
993
994 #define RENDER_SPAN( span ) _mesa_rasterize_span(ctx, &span);
995
996 #include "s_tritemp.h"
997 }
998
999
1000 /*
1001 * Render a smooth-shaded, textured, RGBA triangle.
1002 * Interpolate S,T,R with perspective correction and compute lambda for
1003 * each fragment. Lambda is used to determine whether to use the
1004 * minification or magnification filter. If minification and using
1005 * mipmaps, lambda is also used to select the texture level of detail.
1006 */
1007 static void lambda_textured_triangle( GLcontext *ctx,
1008 const SWvertex *v0,
1009 const SWvertex *v1,
1010 const SWvertex *v2 )
1011 {
1012 #define INTERP_Z 1
1013 #define INTERP_FOG 1
1014 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1015 #define INTERP_RGB 1
1016 #define INTERP_ALPHA 1
1017 #define INTERP_TEX 1
1018 #define INTERP_LAMBDA 1
1019
1020 #define SETUP_CODE \
1021 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1022 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1023 span.texWidth[0] = (GLfloat) texImage->Width; \
1024 span.texHeight[0] = (GLfloat) texImage->Height; \
1025 (void) fixedToDepthShift;
1026
1027 #define RENDER_SPAN( span ) _mesa_rasterize_span(ctx, &span);
1028
1029 #include "s_tritemp.h"
1030 }
1031
1032
1033 /*
1034 * Render a smooth-shaded, textured, RGBA triangle with separate specular
1035 * interpolation.
1036 * Interpolate S,T,R with perspective correction and compute lambda for
1037 * each fragment. Lambda is used to determine whether to use the
1038 * minification or magnification filter. If minification and using
1039 * mipmaps, lambda is also used to select the texture level of detail.
1040 */
1041 static void lambda_textured_spec_triangle( GLcontext *ctx,
1042 const SWvertex *v0,
1043 const SWvertex *v1,
1044 const SWvertex *v2 )
1045 {
1046 #define INTERP_Z 1
1047 #define INTERP_FOG 1
1048 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1049 #define INTERP_RGB 1
1050 #define INTERP_SPEC 1
1051 #define INTERP_ALPHA 1
1052 #define INTERP_TEX 1
1053 #define INTERP_LAMBDA 1
1054
1055 #define SETUP_CODE \
1056 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1057 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1058 span.texWidth[0] = (GLfloat) texImage->Width; \
1059 span.texHeight[0] = (GLfloat) texImage->Height; \
1060 (void) fixedToDepthShift;
1061
1062 #define RENDER_SPAN( span ) _mesa_rasterize_span(ctx, &span);
1063
1064 #include "s_tritemp.h"
1065 }
1066
1067
1068 /*
1069 * This is the big one!
1070 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates
1071 * with lambda (LOD).
1072 * Yup, it's slow.
1073 */
1074 static void
1075 lambda_multitextured_triangle( GLcontext *ctx,
1076 const SWvertex *v0,
1077 const SWvertex *v1,
1078 const SWvertex *v2 )
1079 {
1080
1081 #define INTERP_Z 1
1082 #define INTERP_FOG 1
1083 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1084 #define INTERP_RGB 1
1085 #define INTERP_ALPHA 1
1086 #define INTERP_SPEC 1
1087 #define INTERP_MULTITEX 1
1088 #define INTERP_LAMBDA 1
1089
1090 #define SETUP_CODE \
1091 GLuint u; \
1092 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) { \
1093 if (ctx->Texture.Unit[u]._ReallyEnabled) { \
1094 const struct gl_texture_object *texObj; \
1095 const struct gl_texture_image *texImage; \
1096 texObj = ctx->Texture.Unit[u]._Current; \
1097 texImage = texObj->Image[texObj->BaseLevel]; \
1098 span.texWidth[u] = (GLfloat) texImage->Width; \
1099 span.texHeight[u] = (GLfloat) texImage->Height; \
1100 } \
1101 } \
1102 (void) fixedToDepthShift;
1103
1104 #define RENDER_SPAN( span ) _mesa_rasterize_span(ctx, &span);
1105
1106 #include "s_tritemp.h"
1107
1108 }
1109
1110
1111 static void occlusion_zless_triangle( GLcontext *ctx,
1112 const SWvertex *v0,
1113 const SWvertex *v1,
1114 const SWvertex *v2 )
1115 {
1116 if (ctx->OcclusionResult) {
1117 return;
1118 }
1119
1120 #define DO_OCCLUSION_TEST
1121 #define INTERP_Z 1
1122 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1123
1124 #define RENDER_SPAN( span ) \
1125 GLuint i; \
1126 for (i = 0; i < span.end; i++) { \
1127 GLdepth z = FixedToDepth(span.z); \
1128 if (z < zRow[i]) { \
1129 ctx->OcclusionResult = GL_TRUE; \
1130 return; \
1131 } \
1132 span.z += span.zStep; \
1133 }
1134
1135 #include "s_tritemp.h"
1136 }
1137
1138 static void nodraw_triangle( GLcontext *ctx,
1139 const SWvertex *v0,
1140 const SWvertex *v1,
1141 const SWvertex *v2 )
1142 {
1143 (void) (ctx && v0 && v1 && v2);
1144 }
1145
1146
1147 /*
1148 * This is used when separate specular color is enabled, but not
1149 * texturing. We add the specular color to the primary color,
1150 * draw the triangle, then restore the original primary color.
1151 * Inefficient, but seldom needed.
1152 */
1153 void _swrast_add_spec_terms_triangle( GLcontext *ctx,
1154 const SWvertex *v0,
1155 const SWvertex *v1,
1156 const SWvertex *v2 )
1157 {
1158 SWvertex *ncv0 = (SWvertex *)v0; /* drop const qualifier */
1159 SWvertex *ncv1 = (SWvertex *)v1;
1160 SWvertex *ncv2 = (SWvertex *)v2;
1161 #if CHAN_TYPE == GL_FLOAT
1162 GLfloat rSum, gSum, bSum;
1163 #else
1164 GLint rSum, gSum, bSum;
1165 #endif
1166 GLchan c[3][4];
1167 /* save original colors */
1168 COPY_CHAN4( c[0], ncv0->color );
1169 COPY_CHAN4( c[1], ncv1->color );
1170 COPY_CHAN4( c[2], ncv2->color );
1171 /* sum v0 */
1172 rSum = ncv0->color[0] + ncv0->specular[0];
1173 gSum = ncv0->color[1] + ncv0->specular[1];
1174 bSum = ncv0->color[2] + ncv0->specular[2];
1175 ncv0->color[0] = MIN2(rSum, CHAN_MAX);
1176 ncv0->color[1] = MIN2(gSum, CHAN_MAX);
1177 ncv0->color[2] = MIN2(bSum, CHAN_MAX);
1178 /* sum v1 */
1179 rSum = ncv1->color[0] + ncv1->specular[0];
1180 gSum = ncv1->color[1] + ncv1->specular[1];
1181 bSum = ncv1->color[2] + ncv1->specular[2];
1182 ncv1->color[0] = MIN2(rSum, CHAN_MAX);
1183 ncv1->color[1] = MIN2(gSum, CHAN_MAX);
1184 ncv1->color[2] = MIN2(bSum, CHAN_MAX);
1185 /* sum v2 */
1186 rSum = ncv2->color[0] + ncv2->specular[0];
1187 gSum = ncv2->color[1] + ncv2->specular[1];
1188 bSum = ncv2->color[2] + ncv2->specular[2];
1189 ncv2->color[0] = MIN2(rSum, CHAN_MAX);
1190 ncv2->color[1] = MIN2(gSum, CHAN_MAX);
1191 ncv2->color[2] = MIN2(bSum, CHAN_MAX);
1192 /* draw */
1193 SWRAST_CONTEXT(ctx)->SpecTriangle( ctx, ncv0, ncv1, ncv2 );
1194 /* restore original colors */
1195 COPY_CHAN4( ncv0->color, c[0] );
1196 COPY_CHAN4( ncv1->color, c[1] );
1197 COPY_CHAN4( ncv2->color, c[2] );
1198 }
1199
1200
1201
1202 #ifdef DEBUG
1203
1204 /* record the current triangle function name */
1205 const char *_mesa_triFuncName = NULL;
1206
1207 #define USE(triFunc) \
1208 do { \
1209 _mesa_triFuncName = #triFunc; \
1210 /*printf("%s\n", triFuncName);*/ \
1211 swrast->Triangle = triFunc; \
1212 } while (0)
1213
1214 #else
1215
1216 #define USE(triFunc) swrast->Triangle = triFunc;
1217
1218 #endif
1219
1220
1221
1222
1223 /*
1224 * Determine which triangle rendering function to use given the current
1225 * rendering context.
1226 *
1227 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1228 * remove tests to this code.
1229 */
1230 void
1231 _swrast_choose_triangle( GLcontext *ctx )
1232 {
1233 SWcontext *swrast = SWRAST_CONTEXT(ctx);
1234 const GLboolean rgbmode = ctx->Visual.rgbMode;
1235
1236 if (ctx->Polygon.CullFlag &&
1237 ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) {
1238 USE(nodraw_triangle);
1239 return;
1240 }
1241
1242 if (ctx->RenderMode==GL_RENDER) {
1243
1244 if (ctx->Polygon.SmoothFlag) {
1245 _mesa_set_aa_triangle_function(ctx);
1246 ASSERT(swrast->Triangle);
1247 return;
1248 }
1249
1250 if (ctx->Depth.OcclusionTest &&
1251 ctx->Depth.Test &&
1252 ctx->Depth.Mask == GL_FALSE &&
1253 ctx->Depth.Func == GL_LESS &&
1254 !ctx->Stencil.Enabled) {
1255 if ((rgbmode &&
1256 ctx->Color.ColorMask[0] == 0 &&
1257 ctx->Color.ColorMask[1] == 0 &&
1258 ctx->Color.ColorMask[2] == 0 &&
1259 ctx->Color.ColorMask[3] == 0)
1260 ||
1261 (!rgbmode && ctx->Color.IndexMask == 0)) {
1262 USE(occlusion_zless_triangle);
1263 return;
1264 }
1265 }
1266
1267 if (ctx->Texture._ReallyEnabled) {
1268 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1269 const struct gl_texture_object *texObj2D;
1270 const struct gl_texture_image *texImg;
1271 GLenum minFilter, magFilter, envMode;
1272 GLint format;
1273 texObj2D = ctx->Texture.Unit[0].Current2D;
1274 texImg = texObj2D ? texObj2D->Image[texObj2D->BaseLevel] : NULL;
1275 format = texImg ? texImg->TexFormat->MesaFormat : -1;
1276 minFilter = texObj2D ? texObj2D->MinFilter : (GLenum) 0;
1277 magFilter = texObj2D ? texObj2D->MagFilter : (GLenum) 0;
1278 envMode = ctx->Texture.Unit[0].EnvMode;
1279
1280 /* First see if we can used an optimized 2-D texture function */
1281 if (ctx->Texture._ReallyEnabled==TEXTURE0_2D
1282 && texObj2D->WrapS==GL_REPEAT
1283 && texObj2D->WrapT==GL_REPEAT
1284 && texImg->Border==0
1285 && (format == MESA_FORMAT_RGB || format == MESA_FORMAT_RGBA)
1286 && minFilter == magFilter
1287 && ctx->Light.Model.ColorControl == GL_SINGLE_COLOR
1288 && ctx->Texture.Unit[0].EnvMode != GL_COMBINE_EXT) {
1289 if (ctx->Hint.PerspectiveCorrection==GL_FASTEST) {
1290 if (minFilter == GL_NEAREST
1291 && format == MESA_FORMAT_RGB
1292 && (envMode == GL_REPLACE || envMode == GL_DECAL)
1293 && ((swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)
1294 && ctx->Depth.Func == GL_LESS
1295 && ctx->Depth.Mask == GL_TRUE)
1296 || swrast->_RasterMask == TEXTURE_BIT)
1297 && ctx->Polygon.StippleFlag == GL_FALSE) {
1298 if (swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)) {
1299 USE(simple_z_textured_triangle);
1300 }
1301 else {
1302 USE(simple_textured_triangle);
1303 }
1304 }
1305 else {
1306 #if CHAN_TYPE == GL_FLOAT
1307 USE(general_textured_triangle);
1308 #else
1309 USE(affine_textured_triangle);
1310 #endif
1311 }
1312 }
1313 else {
1314 #if CHAN_TYPE == GL_FLOAT
1315 USE(general_textured_triangle);
1316 #else
1317 USE(persp_textured_triangle);
1318 #endif
1319 }
1320 }
1321 else {
1322 /* More complicated textures (mipmap, multi-tex, sep specular) */
1323 GLboolean needLambda;
1324 /* if mag filter != min filter we need to compute lambda */
1325 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current;
1326 if (obj && obj->MinFilter != obj->MagFilter)
1327 needLambda = GL_TRUE;
1328 else
1329 needLambda = GL_FALSE;
1330 if (ctx->Texture._ReallyEnabled > TEXTURE0_ANY) {
1331 USE(lambda_multitextured_triangle);
1332 }
1333 else if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
1334 /* separate specular color interpolation */
1335 if (needLambda) {
1336 USE(lambda_textured_spec_triangle);
1337 }
1338 else {
1339 USE(general_textured_spec_triangle);
1340 }
1341 }
1342 else {
1343 if (needLambda) {
1344 USE(lambda_textured_triangle);
1345 }
1346 else {
1347 USE(general_textured_triangle);
1348 }
1349 }
1350 }
1351 }
1352 else {
1353 ASSERT(!ctx->Texture._ReallyEnabled);
1354 if (ctx->Light.ShadeModel==GL_SMOOTH) {
1355 /* smooth shaded, no texturing, stippled or some raster ops */
1356 if (rgbmode) {
1357 USE(smooth_rgba_triangle);
1358 }
1359 else {
1360 USE(smooth_ci_triangle);
1361 }
1362 }
1363 else {
1364 /* flat shaded, no texturing, stippled or some raster ops */
1365 if (rgbmode) {
1366 USE(flat_rgba_triangle);
1367 }
1368 else {
1369 USE(flat_ci_triangle);
1370 }
1371 }
1372 }
1373 }
1374 else if (ctx->RenderMode==GL_FEEDBACK) {
1375 USE(_mesa_feedback_triangle);
1376 }
1377 else {
1378 /* GL_SELECT mode */
1379 USE(_mesa_select_triangle);
1380 }
1381 }