1 /* $Id: s_triangle.c,v 1.22 2001/03/28 20:40:52 gareth Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 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.
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.
43 #include "s_aatriangle.h"
44 #include "s_context.h"
46 #include "s_feedback.h"
48 #include "s_triangle.h"
50 GLboolean
_mesa_cull_triangle( GLcontext
*ctx
,
55 GLfloat ex
= v1
->win
[0] - v0
->win
[0];
56 GLfloat ey
= v1
->win
[1] - v0
->win
[1];
57 GLfloat fx
= v2
->win
[0] - v0
->win
[0];
58 GLfloat fy
= v2
->win
[1] - v0
->win
[1];
59 GLfloat c
= ex
*fy
-ey
*fx
;
61 if (c
* SWRAST_CONTEXT(ctx
)->_backface_sign
> 0)
70 * Render a flat-shaded color index triangle.
72 static void flat_ci_triangle( GLcontext
*ctx
,
80 #define INNER_LOOP( LEFT, RIGHT, Y ) \
82 const GLint n = RIGHT-LEFT; \
84 GLdepth zspan[MAX_WIDTH]; \
85 GLfixed fogspan[MAX_WIDTH]; \
88 zspan[i] = FixedToDepth(ffz); \
90 fogspan[i] = fffog / 256; \
93 _mesa_write_monoindex_span( ctx, n, LEFT, Y, zspan, \
94 fogspan, v0->index, GL_POLYGON ); \
98 #include "s_tritemp.h"
104 * Render a smooth-shaded color index triangle.
106 static void smooth_ci_triangle( GLcontext
*ctx
,
113 #define INTERP_INDEX 1
115 #define INNER_LOOP( LEFT, RIGHT, Y ) \
117 const GLint n = RIGHT-LEFT; \
119 GLdepth zspan[MAX_WIDTH]; \
120 GLfixed fogspan[MAX_WIDTH]; \
121 GLuint index[MAX_WIDTH]; \
123 for (i=0;i<n;i++) { \
124 zspan[i] = FixedToDepth(ffz); \
126 index[i] = FixedToInt(ffi); \
128 fogspan[i] = fffog / 256; \
131 _mesa_write_index_span( ctx, n, LEFT, Y, zspan, fogspan, \
132 index, GL_POLYGON ); \
136 #include "s_tritemp.h"
142 * Render a flat-shaded RGBA triangle.
144 static void flat_rgba_triangle( GLcontext
*ctx
,
151 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
153 #define INNER_LOOP( LEFT, RIGHT, Y ) \
155 const GLint n = RIGHT-LEFT; \
157 GLdepth zspan[MAX_WIDTH]; \
158 GLfixed fogspan[MAX_WIDTH]; \
160 for (i=0;i<n;i++) { \
161 zspan[i] = FixedToDepth(ffz); \
163 fogspan[i] = fffog / 256; \
166 _mesa_write_monocolor_span( ctx, n, LEFT, Y, zspan, \
167 fogspan, v2->color, \
172 #include "s_tritemp.h"
174 ASSERT(!ctx
->Texture
._ReallyEnabled
); /* texturing must be off */
175 ASSERT(ctx
->Light
.ShadeModel
==GL_FLAT
);
181 * Render a smooth-shaded RGBA triangle.
183 static void smooth_rgba_triangle( GLcontext
*ctx
,
191 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
193 #define INTERP_ALPHA 1
195 #define INNER_LOOP( LEFT, RIGHT, Y ) \
197 const GLint n = RIGHT-LEFT; \
199 GLdepth zspan[MAX_WIDTH]; \
200 GLchan rgba[MAX_WIDTH][4]; \
201 GLfixed fogspan[MAX_WIDTH]; \
203 for (i=0;i<n;i++) { \
204 zspan[i] = FixedToDepth(ffz); \
205 rgba[i][RCOMP] = FixedToInt(ffr); \
206 rgba[i][GCOMP] = FixedToInt(ffg); \
207 rgba[i][BCOMP] = FixedToInt(ffb); \
208 rgba[i][ACOMP] = FixedToInt(ffa); \
209 fogspan[i] = fffog / 256; \
217 _mesa_write_rgba_span( ctx, n, LEFT, Y, \
218 (CONST GLdepth *) zspan, \
220 rgba, GL_POLYGON ); \
224 #include "s_tritemp.h"
226 ASSERT(!ctx
->Texture
._ReallyEnabled
); /* texturing must be off */
227 ASSERT(ctx
->Light
.ShadeModel
==GL_SMOOTH
);
232 * Render an RGB, GL_DECAL, textured triangle.
233 * Interpolate S,T only w/out mipmapping or perspective correction.
237 static void simple_textured_triangle( GLcontext
*ctx
,
242 #define INTERP_INT_TEX 1
243 #define S_SCALE twidth
244 #define T_SCALE theight
246 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
247 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
248 GLint b = obj->BaseLevel; \
249 GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
250 GLfloat theight = (GLfloat) obj->Image[b]->Height; \
251 GLint twidth_log2 = obj->Image[b]->WidthLog2; \
252 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
253 GLint smask = obj->Image[b]->Width - 1; \
254 GLint tmask = obj->Image[b]->Height - 1; \
256 /* this shouldn't happen */ \
260 #define INNER_LOOP( LEFT, RIGHT, Y ) \
262 CONST GLint n = RIGHT-LEFT; \
264 GLchan rgb[MAX_WIDTH][3]; \
266 ffs -= FIXED_HALF; /* off-by-one error? */ \
268 for (i=0;i<n;i++) { \
269 GLint s = FixedToInt(ffs) & smask; \
270 GLint t = FixedToInt(fft) & tmask; \
271 GLint pos = (t << twidth_log2) + s; \
272 pos = pos + pos + pos; /* multiply by 3 */ \
273 rgb[i][RCOMP] = texture[pos]; \
274 rgb[i][GCOMP] = texture[pos+1]; \
275 rgb[i][BCOMP] = texture[pos+2]; \
279 (*swrast->Driver.WriteRGBSpan)( ctx, n, LEFT, Y, \
280 (CONST GLchan (*)[3]) rgb, NULL ); \
284 #include "s_tritemp.h"
289 * Render an RGB, GL_DECAL, textured triangle.
290 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
291 * perspective correction.
295 static void simple_z_textured_triangle( GLcontext
*ctx
,
302 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
303 #define INTERP_INT_TEX 1
304 #define S_SCALE twidth
305 #define T_SCALE theight
307 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
308 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
309 GLint b = obj->BaseLevel; \
310 GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
311 GLfloat theight = (GLfloat) obj->Image[b]->Height; \
312 GLint twidth_log2 = obj->Image[b]->WidthLog2; \
313 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
314 GLint smask = obj->Image[b]->Width - 1; \
315 GLint tmask = obj->Image[b]->Height - 1; \
317 /* this shouldn't happen */ \
321 #define INNER_LOOP( LEFT, RIGHT, Y ) \
323 CONST GLint n = RIGHT-LEFT; \
325 GLchan rgb[MAX_WIDTH][3]; \
326 GLubyte mask[MAX_WIDTH]; \
329 ffs -= FIXED_HALF; /* off-by-one error? */ \
331 for (i=0;i<n;i++) { \
332 GLdepth z = FixedToDepth(ffz); \
334 GLint s = FixedToInt(ffs) & smask; \
335 GLint t = FixedToInt(fft) & tmask; \
336 GLint pos = (t << twidth_log2) + s; \
337 pos = pos + pos + pos; /* multiply by 3 */ \
338 rgb[i][RCOMP] = texture[pos]; \
339 rgb[i][GCOMP] = texture[pos+1]; \
340 rgb[i][BCOMP] = texture[pos+2]; \
351 (*swrast->Driver.WriteRGBSpan)( ctx, n, LEFT, Y, \
352 (CONST GLchan (*)[3]) rgb, mask ); \
356 #include "s_tritemp.h"
362 * Render an RGB/RGBA textured triangle without perspective correction.
364 static void affine_textured_triangle( GLcontext
*ctx
,
371 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
373 #define INTERP_ALPHA 1
374 #define INTERP_INT_TEX 1
375 #define S_SCALE twidth
376 #define T_SCALE theight
378 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
379 struct gl_texture_object *obj = unit->Current2D; \
380 GLint b = obj->BaseLevel; \
381 GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
382 GLfloat theight = (GLfloat) obj->Image[b]->Height; \
383 GLint twidth_log2 = obj->Image[b]->WidthLog2; \
384 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
385 GLint smask = obj->Image[b]->Width - 1; \
386 GLint tmask = obj->Image[b]->Height - 1; \
387 GLint format = obj->Image[b]->Format; \
388 GLint filter = obj->MinFilter; \
389 GLint envmode = unit->EnvMode; \
390 GLint comp, tbytesline, tsize; \
391 GLfixed er, eg, eb, ea; \
392 GLint tr, tg, tb, ta; \
394 /* this shouldn't happen */ \
397 if (envmode == GL_BLEND || envmode == GL_ADD) { \
398 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
399 er = FloatToFixed(unit->EnvColor[RCOMP]); \
400 eg = FloatToFixed(unit->EnvColor[GCOMP]); \
401 eb = FloatToFixed(unit->EnvColor[BCOMP]); \
402 ea = FloatToFixed(unit->EnvColor[ACOMP]); \
410 case GL_LUMINANCE_ALPHA: \
420 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
423 tbytesline = obj->Image[b]->Width * comp; \
424 tsize = obj->Image[b]->Height * tbytesline;
427 /* Instead of defining a function for each mode, a test is done
428 * between the outer and inner loops. This is to reduce code size
429 * and complexity. Observe that an optimizing compiler kills
430 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
433 #define NEAREST_RGB \
440 tr = (ti * (si * tex00[0] + sf * tex01[0]) + \
441 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
442 tg = (ti * (si * tex00[1] + sf * tex01[1]) + \
443 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
444 tb = (ti * (si * tex00[2] + sf * tex01[2]) + \
445 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
448 #define NEAREST_RGBA \
454 #define LINEAR_RGBA \
455 tr = (ti * (si * tex00[0] + sf * tex01[0]) + \
456 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
457 tg = (ti * (si * tex00[1] + sf * tex01[1]) + \
458 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
459 tb = (ti * (si * tex00[2] + sf * tex01[2]) + \
460 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
461 ta = (ti * (si * tex00[3] + sf * tex01[3]) + \
462 tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT
465 dest[RCOMP] = ffr * (tr + 1) >> (FIXED_SHIFT + 8); \
466 dest[GCOMP] = ffg * (tg + 1) >> (FIXED_SHIFT + 8); \
467 dest[BCOMP] = ffb * (tb + 1) >> (FIXED_SHIFT + 8); \
468 dest[ACOMP] = ffa * (ta + 1) >> (FIXED_SHIFT + 8)
471 dest[RCOMP] = ((0xff - ta) * ffr + ((ta + 1) * tr << FIXED_SHIFT)) >> (FIXED_SHIFT + 8); \
472 dest[GCOMP] = ((0xff - ta) * ffg + ((ta + 1) * tg << FIXED_SHIFT)) >> (FIXED_SHIFT + 8); \
473 dest[BCOMP] = ((0xff - ta) * ffb + ((ta + 1) * tb << FIXED_SHIFT)) >> (FIXED_SHIFT + 8); \
474 dest[ACOMP] = FixedToInt(ffa)
477 dest[RCOMP] = ((0xff - tr) * ffr + (tr + 1) * er) >> (FIXED_SHIFT + 8); \
478 dest[GCOMP] = ((0xff - tg) * ffg + (tg + 1) * eg) >> (FIXED_SHIFT + 8); \
479 dest[BCOMP] = ((0xff - tb) * ffb + (tb + 1) * eb) >> (FIXED_SHIFT + 8); \
480 dest[ACOMP] = ffa * (ta + 1) >> (FIXED_SHIFT + 8)
489 dest[RCOMP] = ((ffr << 8) + (tr + 1) * er) >> (FIXED_SHIFT + 8); \
490 dest[GCOMP] = ((ffg << 8) + (tg + 1) * eg) >> (FIXED_SHIFT + 8); \
491 dest[BCOMP] = ((ffb << 8) + (tb + 1) * eb) >> (FIXED_SHIFT + 8); \
492 dest[ACOMP] = ffa * (ta + 1) >> (FIXED_SHIFT + 8)
496 #define NEAREST_RGB_REPLACE NEAREST_RGB;REPLACE
498 #define NEAREST_RGBA_REPLACE *(GLint *)dest = *(GLint *)tex00
500 #define SPAN1(DO_TEX,COMP) \
501 for (i=0;i<n;i++) { \
502 GLint s = FixedToInt(ffs) & smask; \
503 GLint t = FixedToInt(fft) & tmask; \
504 GLint pos = (t << twidth_log2) + s; \
505 const GLchan *tex00 = texture + COMP * pos; \
506 zspan[i] = FixedToDepth(ffz); \
507 fogspan[i] = fffog / 256; \
520 #define SPAN2(DO_TEX,COMP) \
521 for (i=0;i<n;i++) { \
522 GLint s = FixedToInt(ffs) & smask; \
523 GLint t = FixedToInt(fft) & tmask; \
524 GLint sf = ffs & FIXED_FRAC_MASK; \
525 GLint tf = fft & FIXED_FRAC_MASK; \
526 GLint si = FIXED_FRAC_MASK - sf; \
527 GLint ti = FIXED_FRAC_MASK - tf; \
528 GLint pos = (t << twidth_log2) + s; \
529 const GLchan *tex00 = texture + COMP * pos; \
530 const GLchan *tex10 = tex00 + tbytesline; \
531 const GLchan *tex01 = tex00 + COMP; \
532 const GLchan *tex11 = tex10 + COMP; \
538 tex01 -= tbytesline; \
539 tex11 -= tbytesline; \
541 zspan[i] = FixedToDepth(ffz); \
542 fogspan[i] = fffog / 256; \
555 /* here comes the heavy part.. (something for the compiler to chew on) */
556 #define INNER_LOOP( LEFT, RIGHT, Y ) \
558 CONST GLint n = RIGHT-LEFT; \
560 GLdepth zspan[MAX_WIDTH]; \
561 GLfixed fogspan[MAX_WIDTH]; \
562 GLchan rgba[MAX_WIDTH][4]; \
564 GLchan *dest = rgba[0]; \
565 ffs -= FIXED_HALF; /* off-by-one error? */ \
573 SPAN1(NEAREST_RGB;MODULATE,3); \
577 SPAN1(NEAREST_RGB_REPLACE,3); \
580 SPAN1(NEAREST_RGB;BLEND,3); \
583 SPAN1(NEAREST_RGB;ADD,3); \
585 default: /* unexpected env mode */ \
592 SPAN1(NEAREST_RGBA;MODULATE,4); \
595 SPAN1(NEAREST_RGBA;DECAL,4); \
598 SPAN1(NEAREST_RGBA;BLEND,4); \
601 SPAN1(NEAREST_RGBA;ADD,4); \
604 SPAN1(NEAREST_RGBA_REPLACE,4); \
606 default: /* unexpected env mode */ \
619 SPAN2(LINEAR_RGB;MODULATE,3); \
623 SPAN2(LINEAR_RGB;REPLACE,3); \
626 SPAN2(LINEAR_RGB;BLEND,3); \
629 SPAN2(LINEAR_RGB;ADD,3); \
631 default: /* unexpected env mode */ \
638 SPAN2(LINEAR_RGBA;MODULATE,4); \
641 SPAN2(LINEAR_RGBA;DECAL,4); \
644 SPAN2(LINEAR_RGBA;BLEND,4); \
647 SPAN2(LINEAR_RGBA;ADD,4); \
650 SPAN2(LINEAR_RGBA;REPLACE,4); \
652 default: /* unexpected env mode */ \
659 _mesa_write_rgba_span(ctx, n, LEFT, Y, zspan, \
662 /* explicit kill of variables: */ \
663 ffr = ffg = ffb = ffa = 0; \
667 #include "s_tritemp.h"
675 * Render an perspective corrected RGB/RGBA textured triangle.
676 * The Q (aka V in Mesa) coordinate must be zero such that the divide
677 * by interpolated Q/W comes out right.
679 * This function only renders textured triangles that use GL_NEAREST.
680 * Perspective correction works right.
682 * This function written by Klaus Niederkrueger <klaus@math.leidenuniv.nl>
683 * Send all questions and bug reports to him.
685 #if 0 /* XXX disabled because of texcoord interpolation errors */
686 static void near_persp_textured_triangle(GLcontext
*ctx
,
691 /* The BIAS value is used to shift negative values into positive values.
692 * Without this, negative texture values don't GL_REPEAT correctly at just
693 * below zero, because (int)-0.5 = 0 = (int)0.5. We're not going to worry
694 * about texture coords less than -BIAS. This could be fixed by using
695 * FLOORF etc. instead, but this is slower...
701 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
703 #define INTERP_ALPHA 1
706 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
707 struct gl_texture_object *obj = unit->Current2D; \
708 const GLint b = obj->BaseLevel; \
709 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
710 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
711 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
712 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
713 const GLint smask = (obj->Image[b]->Width - 1); \
714 const GLint tmask = (obj->Image[b]->Height - 1); \
715 const GLint format = obj->Image[b]->Format; \
716 const GLint envmode = unit->EnvMode; \
717 GLfloat sscale, tscale; \
718 GLfixed er, eg, eb, ea; \
719 GLint tr, tg, tb, ta; \
721 /* this shouldn't happen */ \
724 if (envmode == GL_BLEND || envmode == GL_ADD) { \
725 er = FloatToFixed(unit->EnvColor[RCOMP]); \
726 eg = FloatToFixed(unit->EnvColor[GCOMP]); \
727 eb = FloatToFixed(unit->EnvColor[BCOMP]); \
728 ea = FloatToFixed(unit->EnvColor[ACOMP]); \
734 #define OLD_SPAN(DO_TEX,COMP) \
735 for (i=0;i<n;i++) { \
736 GLfloat invQ = 1.0f / vv; \
737 GLint s = (int)(SS * invQ + BIAS) & smask; \
738 GLint t = (int)(TT * invQ + BIAS) & tmask; \
739 GLint pos = COMP * ((t << twidth_log2) + s); \
740 const GLchan *tex00 = texture + pos; \
741 zspan[i] = FixedToDepth(ffz); \
742 fogspan[i] = fffog / 256; \
756 #define X_Y_TEX_COORD(X, Y) ((((int)(X) & tmask) << twidth_log2) + ((int)(Y) & smask))
757 #define Y_X_TEX_COORD(X, Y) ((((int)(Y) & tmask) << twidth_log2) + ((int)(X) & smask))
759 #define SPAN1(DO_TEX, COMP, TEX_COORD) { \
760 GLfloat x_max = CEILF(x_tex); \
761 GLfloat y_max = y_tex + (x_max - x_tex) * dy_dx; \
762 GLint j, x_m = (int)x_max; \
764 if ((int)y_max != (int)y_tex) { \
765 GLfloat x_mid = x_tex + (CEILF(y_tex)-y_tex) * dx_dy; \
766 j = (nominator + vv * x_mid)/(denominator - dvdx*x_mid); \
767 pos = COMP * TEX_COORD(x_tex, y_tex); \
768 DRAW_LINE (DO_TEX); \
771 nominator += vv * x_max; \
772 denominator -= dvdx * x_max; \
773 j = nominator / denominator; \
774 pos = COMP * TEX_COORD(x_tex, y_tex); \
775 DRAW_LINE (DO_TEX); \
779 if ((int)y_max != (int)y_tex) { \
780 GLfloat x_mid = (CEILF(y_tex)-y_tex) * dx_dy; \
781 j = (nominator + vv * x_mid)/(denominator - dvdx*x_mid); \
782 pos = COMP * TEX_COORD(x_m, y_tex); \
783 DRAW_LINE (DO_TEX); \
787 denominator -= dvdx; \
788 j = nominator/denominator; \
789 pos = COMP * TEX_COORD(x_m, y_tex); \
790 DRAW_LINE (DO_TEX); \
795 #define SPAN2(DO_TEX, COMP, TEX_COORD) { \
796 GLfloat x_max = CEILF (x_tex); \
797 GLfloat y_max = y_tex + (x_max - x_tex) * dy_dx; \
798 GLint j, x_m = (int) x_max; \
800 if ((int)y_max != (int)y_tex) { \
801 GLfloat x_mid = x_tex + (FLOORF(y_tex)-y_tex) * dx_dy; \
802 j = (nominator + vv * x_mid)/(denominator - dvdx*x_mid); \
803 pos = COMP * TEX_COORD(x_tex, y_tex); \
804 DRAW_LINE (DO_TEX); \
807 nominator += vv * x_max; \
808 denominator -= dvdx * x_max; \
809 j = nominator / denominator; \
810 pos = COMP * TEX_COORD(x_tex, y_tex); \
811 DRAW_LINE (DO_TEX); \
815 if ((int)y_max != (int)y_tex) { \
816 GLfloat x_mid = (FLOORF(y_tex)-y_tex) * dx_dy; \
817 j = (nominator + vv * x_mid)/(denominator - dvdx*x_mid);\
818 pos = COMP * TEX_COORD(x_m, y_tex); \
819 DRAW_LINE (DO_TEX); \
823 denominator -= dvdx; \
824 j = nominator/denominator; \
825 pos = COMP * TEX_COORD(x_m, y_tex); \
826 DRAW_LINE (DO_TEX); \
831 #define SPAN3(DO_TEX, COMP, TEX_COORD) { \
832 GLfloat x_min = FLOORF (x_tex); \
833 GLfloat y_min = y_tex + (x_min - x_tex) * dy_dx; \
834 GLint j, x_m = (int)x_min; \
836 if ((int)y_min != (int)y_tex) { \
837 GLfloat x_mid = x_tex + (CEILF(y_tex)-y_tex) * dx_dy; \
838 j = (nominator + vv * x_mid)/(denominator - dvdx*x_mid); \
839 pos = COMP * TEX_COORD(x_m, y_tex); \
840 DRAW_LINE (DO_TEX); \
843 nominator += vv*x_min; \
844 denominator -= dvdx*x_min; \
845 j = nominator / denominator; \
846 pos = COMP * TEX_COORD(x_m, y_tex); \
847 DRAW_LINE (DO_TEX); \
852 if ((int)y_min != (int)y_tex) { \
853 GLfloat x_mid = (CEILF(y_tex)-y_tex) * dx_dy; \
854 j = (nominator + vv * x_mid)/(denominator - dvdx*x_mid); \
855 pos = COMP * TEX_COORD(x_m, y_tex); \
856 DRAW_LINE (DO_TEX); \
860 denominator += dvdx; \
861 j = nominator/denominator; \
862 pos = COMP * TEX_COORD(x_m, y_tex); \
863 DRAW_LINE (DO_TEX); \
867 #define SPAN4(DO_TEX, COMP, TEX_COORD) \
869 GLfloat x_min = FLOORF(x_tex); \
870 GLint x_m = (int)x_min; \
871 GLfloat y_min = y_tex + (x_min - x_tex) * dy_dx; \
874 if ((int)y_min != (int)y_tex) { \
875 GLfloat x_mid = x_tex + (FLOORF(y_tex)-y_tex) * dx_dy; \
876 j = (nominator + vv * x_mid)/(denominator - dvdx*x_mid); \
877 pos = COMP * TEX_COORD(x_m, y_tex); \
878 DRAW_LINE (DO_TEX); \
881 nominator += vv * x_min; \
882 denominator -= dvdx * x_min; \
883 j = nominator / denominator; \
884 pos = COMP * TEX_COORD(x_m, y_tex); \
885 DRAW_LINE (DO_TEX); \
890 if ((int)y_min != (int)y_tex) { \
891 GLfloat x_mid = (FLOORF(y_tex)-y_tex) * dx_dy; \
892 j = (nominator + vv * x_mid)/(denominator - dvdx*x_mid); \
893 pos = COMP * TEX_COORD(x_m, (y_tex)); \
894 DRAW_LINE (DO_TEX); \
898 denominator += dvdx; \
899 j = nominator/denominator; \
900 pos = COMP * TEX_COORD(x_m, y_tex); \
901 DRAW_LINE (DO_TEX); \
905 #define DRAW_LINE(DO_TEX) \
907 GLchan *tex00 = texture + pos; \
908 if (j>n || j<-100000) \
911 zspan[i] = FixedToDepth(ffz); \
912 fogspan[i] = fffog / 256; \
925 #define INNER_LOOP( LEFT, RIGHT, Y ) \
928 const GLint n = RIGHT-LEFT; \
929 GLdepth zspan[MAX_WIDTH]; \
930 GLfixed fogspan[MAX_WIDTH]; \
931 GLchan rgba[MAX_WIDTH][4]; \
932 (void)uu; /* please GCC */ \
934 GLchan *dest = rgba[0]; \
935 GLfloat SS = ss * sscale; \
936 GLfloat TT = tt * tscale; \
937 GLfloat dSdx = dsdx * sscale; \
938 GLfloat dTdx = dtdx * tscale; \
943 if (n<5) /* When line very short, setup-time > speed-gain. */ \
944 goto old_span; /* So: take old method */ \
947 dx_tex = (SS + n * dSdx) / (vv + n * dvdx) - x_tex, \
948 dy_tex = (TT + n * dTdx) / (vv + n * dvdx) - y_tex; \
949 /* Choose between walking over texture or over pixelline: */ \
950 /* If there are few texels, walk over texture otherwise */ \
951 /* walk over pixelarray. The quotient on the right side */ \
952 /* should give the timeratio needed to draw one texel in */ \
953 /* comparison to one pixel. Depends on CPU. */ \
954 if (dx_tex*dx_tex + dy_tex*dy_tex < (n*n)/16) { \
957 if (dx_tex*dx_tex > dy_tex*dy_tex) { \
958 /* if (FABSF(dx_tex) > FABSF(dy_tex)) */ \
959 GLfloat nominator = - SS - vv * BIAS; \
960 GLfloat denominator = dvdx * BIAS + dSdx; \
963 if (dy_tex != 0.0f) { \
964 dy_dx = dy_tex / dx_tex; \
965 dx_dy = 1.0f/dy_dx; \
969 if (dx_tex > 0.0f) { \
970 if (dy_tex > 0.0f) { \
975 SPAN1(NEAREST_RGB;MODULATE,3, Y_X_TEX_COORD);\
979 SPAN1(NEAREST_RGB_REPLACE,3, Y_X_TEX_COORD); \
982 SPAN1(NEAREST_RGB;BLEND,3, Y_X_TEX_COORD); \
985 SPAN1(NEAREST_RGB;ADD,3, Y_X_TEX_COORD); \
987 default: /* unexpected env mode */ \
994 SPAN1(NEAREST_RGBA;MODULATE,4, Y_X_TEX_COORD);\
997 SPAN1(NEAREST_RGBA;DECAL,4, Y_X_TEX_COORD); \
1000 SPAN1(NEAREST_RGBA;BLEND,4, Y_X_TEX_COORD); \
1003 SPAN1(NEAREST_RGBA;ADD,4, Y_X_TEX_COORD); \
1006 SPAN1(NEAREST_RGBA_REPLACE,4, Y_X_TEX_COORD);\
1008 default: /* unexpected env mode */ \
1014 else { /* dy_tex <= 0.0f */ \
1017 switch (envmode) { \
1019 SPAN2(NEAREST_RGB;MODULATE,3, Y_X_TEX_COORD);\
1023 SPAN2(NEAREST_RGB_REPLACE,3, Y_X_TEX_COORD); \
1026 SPAN2(NEAREST_RGB;BLEND,3, Y_X_TEX_COORD); \
1029 SPAN2(NEAREST_RGB;ADD,3, Y_X_TEX_COORD); \
1031 default: /* unexpected env mode */ \
1038 SPAN2(NEAREST_RGBA;MODULATE,4, Y_X_TEX_COORD);\
1041 SPAN2(NEAREST_RGBA;DECAL,4, Y_X_TEX_COORD); \
1044 SPAN2(NEAREST_RGBA;BLEND,4, Y_X_TEX_COORD); \
1047 SPAN2(NEAREST_RGBA;ADD,4, Y_X_TEX_COORD); \
1050 SPAN2(NEAREST_RGBA_REPLACE,4, Y_X_TEX_COORD);\
1052 default: /* unexpected env mode */ \
1059 else { /* dx_tex < 0.0f */ \
1060 if (dy_tex > 0.0f) { \
1063 switch (envmode) { \
1065 SPAN3(NEAREST_RGB;MODULATE,3, Y_X_TEX_COORD);\
1069 SPAN3(NEAREST_RGB_REPLACE,3, Y_X_TEX_COORD); \
1072 SPAN3(NEAREST_RGB;BLEND,3, Y_X_TEX_COORD); \
1075 SPAN3(NEAREST_RGB;ADD,3, Y_X_TEX_COORD); \
1077 default: /* unexpected env mode */ \
1084 SPAN3(NEAREST_RGBA;MODULATE,4, Y_X_TEX_COORD);\
1087 SPAN3(NEAREST_RGBA;DECAL,4, Y_X_TEX_COORD); \
1090 SPAN3(NEAREST_RGBA;BLEND,4, Y_X_TEX_COORD); \
1093 SPAN3(NEAREST_RGBA;ADD,4, Y_X_TEX_COORD); \
1096 SPAN3(NEAREST_RGBA_REPLACE,4, Y_X_TEX_COORD);\
1098 default: /* unexpected env mode */ \
1104 else { /* dy_tex <= 0.0f */ \
1107 switch (envmode) { \
1109 SPAN4(NEAREST_RGB;MODULATE,3, Y_X_TEX_COORD);\
1113 SPAN4(NEAREST_RGB_REPLACE,3, Y_X_TEX_COORD); \
1116 SPAN4(NEAREST_RGB;BLEND,3, Y_X_TEX_COORD); \
1119 SPAN4(NEAREST_RGB;ADD,3, Y_X_TEX_COORD); \
1128 SPAN4(NEAREST_RGBA;MODULATE,4, Y_X_TEX_COORD);\
1131 SPAN4(NEAREST_RGBA;DECAL,4, Y_X_TEX_COORD); \
1134 SPAN4(NEAREST_RGBA;BLEND,4, Y_X_TEX_COORD); \
1137 SPAN4(NEAREST_RGBA;ADD,4, Y_X_TEX_COORD); \
1140 SPAN4(NEAREST_RGBA_REPLACE,4, Y_X_TEX_COORD);\
1142 default: /* unexpected env mode */ \
1150 else { /* FABSF(dx_tex) > FABSF(dy_tex) */ \
1154 GLfloat nominator, denominator; \
1155 if (dx_tex == 0.0f /* && dy_tex == 0.0f*/) \
1156 goto old_span; /* case so special, that use old */ \
1157 /* swap some x-values and y-values */ \
1160 swap = x_tex, x_tex = y_tex, y_tex = swap; \
1161 swap = dx_tex, dx_tex = dy_tex, dy_tex = swap; \
1162 nominator = - SS - vv * BIAS; \
1163 denominator = dvdx * BIAS + dSdx; \
1164 if (dy_tex != 0.0f) { \
1165 dy_dx = dy_tex / dx_tex; \
1166 dx_dy = 1.0f/dy_dx; \
1170 if (dx_tex > 0.0f) { \
1171 if (dy_tex > 0.0f) { \
1174 switch (envmode) { \
1176 SPAN1(NEAREST_RGB;MODULATE,3, X_Y_TEX_COORD);\
1180 SPAN1(NEAREST_RGB_REPLACE,3, X_Y_TEX_COORD); \
1183 SPAN1(NEAREST_RGB;BLEND,3, X_Y_TEX_COORD); \
1186 SPAN1(NEAREST_RGB;ADD,3, X_Y_TEX_COORD); \
1188 default: /* unexpected env mode */ \
1195 SPAN1(NEAREST_RGBA;MODULATE,4, X_Y_TEX_COORD);\
1198 SPAN1(NEAREST_RGBA;DECAL,4, X_Y_TEX_COORD); \
1201 SPAN1(NEAREST_RGBA;BLEND,4, X_Y_TEX_COORD); \
1204 SPAN1(NEAREST_RGBA;ADD,4, X_Y_TEX_COORD); \
1207 SPAN1(NEAREST_RGBA_REPLACE,4, X_Y_TEX_COORD);\
1215 else { /* dy_tex <= 0.0f */ \
1218 switch (envmode) { \
1220 SPAN2(NEAREST_RGB;MODULATE,3, X_Y_TEX_COORD);\
1224 SPAN2(NEAREST_RGB_REPLACE,3, X_Y_TEX_COORD); \
1227 SPAN2(NEAREST_RGB;BLEND,3, X_Y_TEX_COORD); \
1230 SPAN2(NEAREST_RGB;ADD,3, X_Y_TEX_COORD); \
1239 SPAN2(NEAREST_RGBA;MODULATE,4, X_Y_TEX_COORD);\
1242 SPAN2(NEAREST_RGBA;DECAL,4, X_Y_TEX_COORD); \
1245 SPAN2(NEAREST_RGBA;BLEND,4, X_Y_TEX_COORD); \
1248 SPAN2(NEAREST_RGBA;ADD,4, X_Y_TEX_COORD); \
1251 SPAN2(NEAREST_RGBA_REPLACE,4, X_Y_TEX_COORD);\
1260 else { /* dx_tex < 0.0f */ \
1261 if (dy_tex > 0.0f) { \
1264 switch (envmode) { \
1266 SPAN3(NEAREST_RGB;MODULATE,3, X_Y_TEX_COORD);\
1270 SPAN3(NEAREST_RGB_REPLACE,3, X_Y_TEX_COORD); \
1273 SPAN3(NEAREST_RGB;BLEND,3, X_Y_TEX_COORD); \
1276 SPAN3(NEAREST_RGB;ADD,3, X_Y_TEX_COORD); \
1285 SPAN3(NEAREST_RGBA;MODULATE,4, X_Y_TEX_COORD);\
1288 SPAN3(NEAREST_RGBA;DECAL,4, X_Y_TEX_COORD); \
1291 SPAN3(NEAREST_RGBA;BLEND,4, X_Y_TEX_COORD); \
1294 SPAN3(NEAREST_RGBA;ADD,4, X_Y_TEX_COORD); \
1297 SPAN3(NEAREST_RGBA_REPLACE,4, X_Y_TEX_COORD);\
1305 else { /* dy_tex <= 0.0f */ \
1308 switch (envmode) { \
1310 SPAN4(NEAREST_RGB;MODULATE,3, X_Y_TEX_COORD);\
1314 SPAN4(NEAREST_RGB_REPLACE,3, X_Y_TEX_COORD); \
1317 SPAN4(NEAREST_RGB;BLEND,3, X_Y_TEX_COORD); \
1320 SPAN4(NEAREST_RGB;ADD,3, X_Y_TEX_COORD); \
1329 SPAN4(NEAREST_RGBA;MODULATE,4, X_Y_TEX_COORD);\
1332 SPAN4(NEAREST_RGBA;DECAL,4, X_Y_TEX_COORD); \
1335 SPAN4(NEAREST_RGBA;BLEND,4, X_Y_TEX_COORD); \
1338 SPAN4(NEAREST_RGBA;ADD,4, X_Y_TEX_COORD); \
1341 SPAN4(NEAREST_RGBA_REPLACE,4, X_Y_TEX_COORD);\
1356 switch (envmode) { \
1358 OLD_SPAN(NEAREST_RGB;MODULATE,3); \
1362 OLD_SPAN(NEAREST_RGB_REPLACE,3); \
1365 OLD_SPAN(NEAREST_RGB;BLEND,3); \
1368 OLD_SPAN(NEAREST_RGB;ADD,3); \
1377 OLD_SPAN(NEAREST_RGBA;MODULATE,4); \
1380 OLD_SPAN(NEAREST_RGBA;DECAL,4); \
1383 OLD_SPAN(NEAREST_RGBA;BLEND,4); \
1386 OLD_SPAN(NEAREST_RGBA;ADD,4); \
1389 OLD_SPAN(NEAREST_RGBA_REPLACE,4); \
1397 _mesa_write_rgba_span( ctx, n, LEFT, Y, zspan, \
1398 fogspan, rgba, GL_POLYGON); \
1399 ffr = ffg = ffb = ffa = 0; \
1403 #include "s_tritemp.h"
1409 #undef X_Y_TEX_COORD
1410 #undef Y_X_TEX_COORD
1418 * Render an perspective corrected RGB/RGBA textured triangle.
1419 * The Q (aka V in Mesa) coordinate must be zero such that the divide
1420 * by interpolated Q/W comes out right.
1422 * This function written by Klaus Niederkrueger <klaus@math.leidenuniv.nl>
1423 * Send all questions and bug reports to him.
1425 #if 0 /* XXX disabled because of texcoord interpolation errors */
1426 static void lin_persp_textured_triangle( GLcontext
*ctx
,
1429 const SWvertex
*v2
)
1432 #define INTERP_FOG 1
1433 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1434 #define INTERP_RGB 1
1435 #define INTERP_ALPHA 1
1436 #define INTERP_TEX 1
1437 #define SETUP_CODE \
1438 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
1439 struct gl_texture_object *obj = unit->Current2D; \
1440 const GLint b = obj->BaseLevel; \
1441 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
1442 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
1443 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
1444 GLchan *texture = obj->Image[b]->Data; \
1445 const GLint smask = (obj->Image[b]->Width - 1); \
1446 const GLint tmask = (obj->Image[b]->Height - 1); \
1447 const GLint format = obj->Image[b]->Format; \
1448 const GLint envmode = unit->EnvMode; \
1449 GLfloat sscale, tscale; \
1450 GLint comp, tbytesline, tsize; \
1451 GLfixed er, eg, eb, ea; \
1452 GLint tr, tg, tb, ta; \
1456 if (envmode == GL_BLEND || envmode == GL_ADD) { \
1457 er = FloatToFixed(unit->EnvColor[RCOMP]); \
1458 eg = FloatToFixed(unit->EnvColor[GCOMP]); \
1459 eb = FloatToFixed(unit->EnvColor[BCOMP]); \
1460 ea = FloatToFixed(unit->EnvColor[ACOMP]); \
1464 case GL_LUMINANCE: \
1465 case GL_INTENSITY: \
1468 case GL_LUMINANCE_ALPHA: \
1478 _mesa_problem(NULL, "Bad texture format in lin_persp_texture_triangle"); \
1481 sscale = FIXED_SCALE * twidth; \
1482 tscale = FIXED_SCALE * theight; \
1483 tbytesline = obj->Image[b]->Width * comp; \
1484 tsize = theight * tbytesline;
1487 #define SPAN(DO_TEX,COMP) \
1488 for (i=0;i<n;i++) { \
1489 GLfloat invQ = 1.0f / vv; \
1490 GLfixed ffs = (int)(SS * invQ); \
1491 GLfixed fft = (int)(TT * invQ); \
1492 GLint s = FixedToInt(ffs) & smask; \
1493 GLint t = FixedToInt(fft) & tmask; \
1494 GLint sf = ffs & FIXED_FRAC_MASK; \
1495 GLint tf = fft & FIXED_FRAC_MASK; \
1496 GLint si = FIXED_FRAC_MASK - sf; \
1497 GLint ti = FIXED_FRAC_MASK - tf; \
1498 GLint pos = COMP * ((t << twidth_log2) + s); \
1499 GLchan *tex00 = texture + pos; \
1500 GLchan *tex10 = tex00 + tbytesline; \
1501 GLchan *tex01 = tex00 + COMP; \
1502 GLchan *tex11 = tex10 + COMP; \
1508 tex01 -= tbytesline; \
1509 tex11 -= tbytesline; \
1511 zspan[i] = FixedToDepth(ffz); \
1512 fogspan[i] = fffog / 256; \
1526 #define INNER_LOOP( LEFT, RIGHT, Y ) \
1529 const GLint n = RIGHT-LEFT; \
1530 GLdepth zspan[MAX_WIDTH]; \
1531 GLfixed fogspan[MAX_WIDTH]; \
1532 GLchan rgba[MAX_WIDTH][4]; \
1533 (void) uu; /* please GCC */ \
1535 GLfloat SS = ss * sscale; \
1536 GLfloat TT = tt * tscale; \
1537 GLfloat dSdx = dsdx * sscale; \
1538 GLfloat dTdx = dtdx * tscale; \
1539 GLchan *dest = rgba[0]; \
1540 SS -= 0.5f * FIXED_SCALE * vv; \
1541 TT -= 0.5f * FIXED_SCALE * vv; \
1544 switch (envmode) { \
1546 SPAN(LINEAR_RGB;MODULATE,3); \
1550 SPAN(LINEAR_RGB;REPLACE,3); \
1553 SPAN(LINEAR_RGB;BLEND,3); \
1556 SPAN(LINEAR_RGB;ADD,3); \
1563 switch (envmode) { \
1565 SPAN(LINEAR_RGBA;MODULATE,4); \
1568 SPAN(LINEAR_RGBA;DECAL,4); \
1571 SPAN(LINEAR_RGBA;BLEND,4); \
1574 SPAN(LINEAR_RGBA;REPLACE,4); \
1577 SPAN(LINEAR_RGBA;ADD,4); \
1579 default: /* unexpected env mode */ \
1583 _mesa_write_rgba_span( ctx, n, LEFT, Y, zspan, \
1585 rgba, GL_POLYGON ); \
1586 ffr = ffg = ffb = ffa = 0; \
1590 #include "s_tritemp.h"
1597 * Render a smooth-shaded, textured, RGBA triangle.
1598 * Interpolate S,T,U with perspective correction, w/out mipmapping.
1599 * Note: we use texture coordinates S,T,U,V instead of S,T,R,Q because
1600 * R is already used for red.
1602 static void general_textured_triangle( GLcontext
*ctx
,
1605 const SWvertex
*v2
)
1608 #define INTERP_FOG 1
1609 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1610 #define INTERP_RGB 1
1611 #define INTERP_ALPHA 1
1612 #define INTERP_TEX 1
1613 #define SETUP_CODE \
1614 GLboolean flat_shade = (ctx->Light.ShadeModel==GL_FLAT); \
1617 r = v2->color[RCOMP]; \
1618 g = v2->color[GCOMP]; \
1619 b = v2->color[BCOMP]; \
1620 a = v2->color[ACOMP]; \
1622 #define INNER_LOOP( LEFT, RIGHT, Y ) \
1625 const GLint n = RIGHT-LEFT; \
1626 GLdepth zspan[MAX_WIDTH]; \
1627 GLfixed fogspan[MAX_WIDTH]; \
1628 GLchan rgba[MAX_WIDTH][4]; \
1629 GLfloat s[MAX_WIDTH], t[MAX_WIDTH], u[MAX_WIDTH]; \
1632 for (i=0;i<n;i++) { \
1633 GLdouble invQ = vv ? (1.0 / vv) : 1.0; \
1634 zspan[i] = FixedToDepth(ffz); \
1635 fogspan[i] = fffog / 256; \
1636 rgba[i][RCOMP] = r; \
1637 rgba[i][GCOMP] = g; \
1638 rgba[i][BCOMP] = b; \
1639 rgba[i][ACOMP] = a; \
1652 for (i=0;i<n;i++) { \
1653 GLdouble invQ = vv ? (1.0 / vv) : 1.0; \
1654 zspan[i] = FixedToDepth(ffz); \
1655 rgba[i][RCOMP] = FixedToInt(ffr); \
1656 rgba[i][GCOMP] = FixedToInt(ffg); \
1657 rgba[i][BCOMP] = FixedToInt(ffb); \
1658 rgba[i][ACOMP] = FixedToInt(ffa); \
1659 fogspan[i] = fffog / 256; \
1675 _mesa_write_texture_span( ctx, n, LEFT, Y, zspan, fogspan, \
1678 NULL, GL_POLYGON ); \
1682 #include "s_tritemp.h"
1687 * Render a smooth-shaded, textured, RGBA triangle with separate specular
1688 * color interpolation.
1689 * Interpolate S,T,U with perspective correction, w/out mipmapping.
1690 * Note: we use texture coordinates S,T,U,V instead of S,T,R,Q because
1691 * R is already used for red.
1693 static void general_textured_spec_triangle1( GLcontext
*ctx
,
1697 GLdepth zspan
[MAX_WIDTH
],
1698 GLfixed fogspan
[MAX_WIDTH
],
1699 GLchan rgba
[MAX_WIDTH
][4],
1700 GLchan spec
[MAX_WIDTH
][4] )
1703 #define INTERP_FOG 1
1704 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1705 #define INTERP_RGB 1
1706 #define INTERP_SPEC 1
1707 #define INTERP_ALPHA 1
1708 #define INTERP_TEX 1
1709 #define SETUP_CODE \
1710 GLboolean flat_shade = (ctx->Light.ShadeModel==GL_FLAT); \
1711 GLint r, g, b, a, sr, sg, sb; \
1713 r = v2->color[RCOMP]; \
1714 g = v2->color[GCOMP]; \
1715 b = v2->color[BCOMP]; \
1716 a = v2->color[ACOMP]; \
1717 sr = v2->specular[RCOMP]; \
1718 sg = v2->specular[GCOMP]; \
1719 sb = v2->specular[BCOMP]; \
1721 #define INNER_LOOP( LEFT, RIGHT, Y ) \
1724 const GLint n = RIGHT-LEFT; \
1725 GLfloat s[MAX_WIDTH], t[MAX_WIDTH], u[MAX_WIDTH]; \
1728 for (i=0;i<n;i++) { \
1729 GLdouble invQ = vv ? (1.0 / vv) : 1.0; \
1730 zspan[i] = FixedToDepth(ffz); \
1731 fogspan[i] = fffog / 256; \
1732 rgba[i][RCOMP] = r; \
1733 rgba[i][GCOMP] = g; \
1734 rgba[i][BCOMP] = b; \
1735 rgba[i][ACOMP] = a; \
1736 spec[i][RCOMP] = sr; \
1737 spec[i][GCOMP] = sg; \
1738 spec[i][BCOMP] = sb; \
1751 for (i=0;i<n;i++) { \
1752 GLdouble invQ = vv ? (1.0 / vv) : 1.0; \
1753 zspan[i] = FixedToDepth(ffz); \
1754 fogspan[i] = fffog / 256; \
1755 rgba[i][RCOMP] = FixedToInt(ffr); \
1756 rgba[i][GCOMP] = FixedToInt(ffg); \
1757 rgba[i][BCOMP] = FixedToInt(ffb); \
1758 rgba[i][ACOMP] = FixedToInt(ffa); \
1759 spec[i][RCOMP] = FixedToInt(ffsr); \
1760 spec[i][GCOMP] = FixedToInt(ffsg); \
1761 spec[i][BCOMP] = FixedToInt(ffsb); \
1780 _mesa_write_texture_span( ctx, n, LEFT, Y, zspan, \
1782 s, t, u, NULL, rgba, \
1783 (CONST GLchan (*)[4]) spec, \
1788 #include "s_tritemp.h"
1793 * Render a smooth-shaded, textured, RGBA triangle.
1794 * Interpolate S,T,U with perspective correction and compute lambda for
1795 * each fragment. Lambda is used to determine whether to use the
1796 * minification or magnification filter. If minification and using
1797 * mipmaps, lambda is also used to select the texture level of detail.
1799 static void lambda_textured_triangle1( GLcontext
*ctx
,
1803 GLfloat s
[MAX_WIDTH
],
1804 GLfloat t
[MAX_WIDTH
],
1805 GLfloat u
[MAX_WIDTH
] )
1808 #define INTERP_FOG 1
1809 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1810 #define INTERP_RGB 1
1811 #define INTERP_ALPHA 1
1812 #define INTERP_LAMBDA 1
1813 #define INTERP_TEX 1
1815 #define SETUP_CODE \
1816 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1817 const GLint baseLevel = obj->BaseLevel; \
1818 const struct gl_texture_image *texImage = obj->Image[baseLevel]; \
1819 const GLfloat twidth = (GLfloat) texImage->Width; \
1820 const GLfloat theight = (GLfloat) texImage->Height; \
1821 const GLboolean flat_shade = (ctx->Light.ShadeModel==GL_FLAT); \
1824 r = v2->color[RCOMP]; \
1825 g = v2->color[GCOMP]; \
1826 b = v2->color[BCOMP]; \
1827 a = v2->color[ACOMP]; \
1830 #define INNER_LOOP( LEFT, RIGHT, Y ) \
1833 const GLint n = RIGHT-LEFT; \
1834 GLdepth zspan[MAX_WIDTH]; \
1835 GLfixed fogspan[MAX_WIDTH]; \
1836 GLchan rgba[MAX_WIDTH][4]; \
1837 GLfloat lambda[MAX_WIDTH]; \
1840 for (i=0;i<n;i++) { \
1841 GLdouble invQ = vv ? (1.0 / vv) : 1.0; \
1842 zspan[i] = FixedToDepth(ffz); \
1843 fogspan[i] = fffog / 256; \
1844 rgba[i][RCOMP] = r; \
1845 rgba[i][GCOMP] = g; \
1846 rgba[i][BCOMP] = b; \
1847 rgba[i][ACOMP] = a; \
1851 COMPUTE_LAMBDA(lambda[i], invQ); \
1861 for (i=0;i<n;i++) { \
1862 GLdouble invQ = vv ? (1.0 / vv) : 1.0; \
1863 zspan[i] = FixedToDepth(ffz); \
1864 fogspan[i] = fffog / 256; \
1865 rgba[i][RCOMP] = FixedToInt(ffr); \
1866 rgba[i][GCOMP] = FixedToInt(ffg); \
1867 rgba[i][BCOMP] = FixedToInt(ffb); \
1868 rgba[i][ACOMP] = FixedToInt(ffa); \
1872 COMPUTE_LAMBDA(lambda[i], invQ); \
1885 _mesa_write_texture_span( ctx, n, LEFT, Y, zspan, fogspan, \
1887 rgba, NULL, GL_POLYGON ); \
1891 #include "s_tritemp.h"
1896 * Render a smooth-shaded, textured, RGBA triangle with separate specular
1898 * Interpolate S,T,U with perspective correction and compute lambda for
1899 * each fragment. Lambda is used to determine whether to use the
1900 * minification or magnification filter. If minification and using
1901 * mipmaps, lambda is also used to select the texture level of detail.
1903 static void lambda_textured_spec_triangle1( GLcontext
*ctx
,
1907 GLfloat s
[MAX_WIDTH
],
1908 GLfloat t
[MAX_WIDTH
],
1909 GLfloat u
[MAX_WIDTH
] )
1912 #define INTERP_FOG 1
1913 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1914 #define INTERP_RGB 1
1915 #define INTERP_SPEC 1
1916 #define INTERP_ALPHA 1
1917 #define INTERP_TEX 1
1918 #define INTERP_LAMBDA 1
1920 #define SETUP_CODE \
1921 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1922 const GLint baseLevel = obj->BaseLevel; \
1923 const struct gl_texture_image *texImage = obj->Image[baseLevel]; \
1924 const GLfloat twidth = (GLfloat) texImage->Width; \
1925 const GLfloat theight = (GLfloat) texImage->Height; \
1926 const GLboolean flat_shade = (ctx->Light.ShadeModel==GL_FLAT); \
1927 GLint r, g, b, a, sr, sg, sb; \
1929 r = v2->color[RCOMP]; \
1930 g = v2->color[GCOMP]; \
1931 b = v2->color[BCOMP]; \
1932 a = v2->color[ACOMP]; \
1933 sr = v2->specular[RCOMP]; \
1934 sg = v2->specular[GCOMP]; \
1935 sb = v2->specular[BCOMP]; \
1938 #define INNER_LOOP( LEFT, RIGHT, Y ) \
1941 const GLint n = RIGHT-LEFT; \
1942 GLdepth zspan[MAX_WIDTH]; \
1943 GLfixed fogspan[MAX_WIDTH]; \
1944 GLchan spec[MAX_WIDTH][4]; \
1945 GLchan rgba[MAX_WIDTH][4]; \
1946 GLfloat lambda[MAX_WIDTH]; \
1949 for (i=0;i<n;i++) { \
1950 GLdouble invQ = vv ? (1.0 / vv) : 1.0; \
1951 zspan[i] = FixedToDepth(ffz); \
1952 fogspan[i] = fffog / 256; \
1953 rgba[i][RCOMP] = r; \
1954 rgba[i][GCOMP] = g; \
1955 rgba[i][BCOMP] = b; \
1956 rgba[i][ACOMP] = a; \
1957 spec[i][RCOMP] = sr; \
1958 spec[i][GCOMP] = sg; \
1959 spec[i][BCOMP] = sb; \
1963 COMPUTE_LAMBDA(lambda[i], invQ); \
1973 for (i=0;i<n;i++) { \
1974 GLdouble invQ = vv ? (1.0 / vv) : 1.0; \
1975 zspan[i] = FixedToDepth(ffz); \
1976 fogspan[i] = fffog / 256; \
1977 rgba[i][RCOMP] = FixedToInt(ffr); \
1978 rgba[i][GCOMP] = FixedToInt(ffg); \
1979 rgba[i][BCOMP] = FixedToInt(ffb); \
1980 rgba[i][ACOMP] = FixedToInt(ffa); \
1981 spec[i][RCOMP] = FixedToInt(ffsr); \
1982 spec[i][GCOMP] = FixedToInt(ffsg); \
1983 spec[i][BCOMP] = FixedToInt(ffsb); \
1987 COMPUTE_LAMBDA(lambda[i], invQ); \
2003 _mesa_write_texture_span( ctx, n, LEFT, Y, zspan, fogspan, \
2005 rgba, (CONST GLchan (*)[4]) spec, \
2010 #include "s_tritemp.h"
2015 * This is the big one!
2016 * Interpolate Z, RGB, Alpha, and two sets of texture coordinates.
2020 lambda_multitextured_triangle1( GLcontext
*ctx
,
2024 GLfloat s
[MAX_TEXTURE_UNITS
][MAX_WIDTH
],
2025 GLfloat t
[MAX_TEXTURE_UNITS
][MAX_WIDTH
],
2026 GLfloat u
[MAX_TEXTURE_UNITS
][MAX_WIDTH
])
2029 #define INTERP_FOG 1
2030 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
2031 #define INTERP_RGB 1
2032 #define INTERP_ALPHA 1
2033 #define INTERP_MULTITEX 1
2034 #define INTERP_MULTILAMBDA 1
2036 #define SETUP_CODE \
2037 GLchan rgba[MAX_WIDTH][4]; \
2038 const GLboolean flat_shade = (ctx->Light.ShadeModel==GL_FLAT); \
2039 GLfloat twidth[MAX_TEXTURE_UNITS], theight[MAX_TEXTURE_UNITS]; \
2049 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) { \
2050 if (ctx->Texture.Unit[unit]._ReallyEnabled) { \
2051 const struct gl_texture_object *obj = ctx->Texture.Unit[unit]._Current; \
2052 const GLint baseLevel = obj->BaseLevel; \
2053 const struct gl_texture_image *texImage = obj->Image[baseLevel];\
2054 twidth[unit] = (GLfloat) texImage->Width; \
2055 theight[unit] = (GLfloat) texImage->Height; \
2060 #define INNER_LOOP( LEFT, RIGHT, Y ) \
2063 const GLint n = RIGHT-LEFT; \
2064 GLdepth zspan[MAX_WIDTH]; \
2065 GLfixed fogspan[MAX_WIDTH]; \
2066 GLfloat lambda[MAX_TEXTURE_UNITS][MAX_WIDTH]; \
2069 for (i=0;i<n;i++) { \
2071 zspan[i] = FixedToDepth(ffz); \
2072 fogspan[i] = fffog / 256; \
2075 rgba[i][RCOMP] = r; \
2076 rgba[i][GCOMP] = g; \
2077 rgba[i][BCOMP] = b; \
2078 rgba[i][ACOMP] = a; \
2079 for (unit=0; unit < ctx->Const.MaxTextureUnits; unit++) {\
2080 if (ctx->Texture.Unit[unit]._ReallyEnabled) { \
2081 GLdouble invQ = 1.0 / vv[unit]; \
2082 s[unit][i] = ss[unit] * invQ; \
2083 t[unit][i] = tt[unit] * invQ; \
2084 u[unit][i] = uu[unit] * invQ; \
2085 COMPUTE_MULTILAMBDA(lambda[unit][i], invQ, unit); \
2086 ss[unit] += dsdx[unit]; \
2087 tt[unit] += dtdx[unit]; \
2088 uu[unit] += dudx[unit]; \
2089 vv[unit] += dvdx[unit]; \
2094 else { /* smooth shade */ \
2095 for (i=0;i<n;i++) { \
2097 zspan[i] = FixedToDepth(ffz); \
2098 fogspan[i] = fffog / 256; \
2101 rgba[i][RCOMP] = FixedToInt(ffr); \
2102 rgba[i][GCOMP] = FixedToInt(ffg); \
2103 rgba[i][BCOMP] = FixedToInt(ffb); \
2104 rgba[i][ACOMP] = FixedToInt(ffa); \
2109 for (unit=0; unit < ctx->Const.MaxTextureUnits; unit++) {\
2110 if (ctx->Texture.Unit[unit]._ReallyEnabled) { \
2111 GLdouble invQ = 1.0 / vv[unit]; \
2112 s[unit][i] = ss[unit] * invQ; \
2113 t[unit][i] = tt[unit] * invQ; \
2114 u[unit][i] = uu[unit] * invQ; \
2115 COMPUTE_MULTILAMBDA(lambda[unit][i], invQ, unit); \
2116 ss[unit] += dsdx[unit]; \
2117 tt[unit] += dtdx[unit]; \
2118 uu[unit] += dudx[unit]; \
2119 vv[unit] += dvdx[unit]; \
2124 _mesa_write_multitexture_span(ctx, n, LEFT, Y, zspan, fogspan, \
2125 (const GLfloat (*)[MAX_WIDTH]) s,\
2126 (const GLfloat (*)[MAX_WIDTH]) t,\
2127 (const GLfloat (*)[MAX_WIDTH]) u,\
2128 (GLfloat (*)[MAX_WIDTH]) lambda, \
2129 rgba, NULL, GL_POLYGON ); \
2133 #include "s_tritemp.h"
2138 * These wrappers are needed to deal with the 32KB / stack frame limit
2139 * on Mac / PowerPC systems.
2142 static void general_textured_spec_triangle(GLcontext
*ctx
,
2145 const SWvertex
*v2
)
2147 GLdepth zspan
[MAX_WIDTH
];
2148 GLfixed fogspan
[MAX_WIDTH
];
2149 GLchan rgba
[MAX_WIDTH
][4], spec
[MAX_WIDTH
][4];
2150 general_textured_spec_triangle1(ctx
,v0
,v1
,v2
,zspan
,fogspan
,rgba
,spec
);
2153 static void lambda_textured_triangle( GLcontext
*ctx
,
2156 const SWvertex
*v2
)
2158 GLfloat s
[MAX_WIDTH
], t
[MAX_WIDTH
], u
[MAX_WIDTH
];
2159 lambda_textured_triangle1(ctx
,v0
,v1
,v2
,s
,t
,u
);
2162 static void lambda_textured_spec_triangle( GLcontext
*ctx
,
2165 const SWvertex
*v2
)
2167 GLfloat s
[MAX_WIDTH
];
2168 GLfloat t
[MAX_WIDTH
];
2169 GLfloat u
[MAX_WIDTH
];
2170 lambda_textured_spec_triangle1(ctx
,v0
,v1
,v2
,s
,t
,u
);
2174 static void lambda_multitextured_triangle( GLcontext
*ctx
,
2177 const SWvertex
*v2
)
2180 GLfloat s
[MAX_TEXTURE_UNITS
][MAX_WIDTH
];
2181 GLfloat t
[MAX_TEXTURE_UNITS
][MAX_WIDTH
];
2182 DEFMARRAY(GLfloat
,u
,MAX_TEXTURE_UNITS
,MAX_WIDTH
);
2183 CHECKARRAY(u
,return);
2185 lambda_multitextured_triangle1(ctx
,v0
,v1
,v2
,s
,t
,u
);
2192 static void occlusion_zless_triangle( GLcontext
*ctx
,
2195 const SWvertex
*v2
)
2197 if (ctx
->OcclusionResult
) {
2201 #define DO_OCCLUSION_TEST
2203 #define INTERP_FOG 1
2204 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
2205 #define INNER_LOOP( LEFT, RIGHT, Y ) \
2208 const GLint len = RIGHT-LEFT; \
2209 for (i=0;i<len;i++) { \
2210 GLdepth z = FixedToDepth(ffz); \
2212 if (z < zRow[i]) { \
2213 ctx->OcclusionResult = GL_TRUE; \
2219 #include "s_tritemp.h"
2222 static void nodraw_triangle( GLcontext
*ctx
,
2225 const SWvertex
*v2
)
2227 (void) (ctx
&& v0
&& v1
&& v2
);
2230 void _swrast_add_spec_terms_triangle( GLcontext
*ctx
,
2233 const SWvertex
*v2
)
2235 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
2236 SWvertex
*ncv1
= (SWvertex
*)v1
;
2237 SWvertex
*ncv2
= (SWvertex
*)v2
;
2239 COPY_CHAN4( c
[0], ncv0
->color
);
2240 COPY_CHAN4( c
[1], ncv1
->color
);
2241 COPY_CHAN4( c
[2], ncv2
->color
);
2242 ACC_3V( ncv0
->color
, ncv0
->specular
);
2243 ACC_3V( ncv1
->color
, ncv1
->specular
);
2244 ACC_3V( ncv2
->color
, ncv2
->specular
);
2245 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
2246 COPY_CHAN4( ncv0
->color
, c
[0] );
2247 COPY_CHAN4( ncv1
->color
, c
[1] );
2248 COPY_CHAN4( ncv2
->color
, c
[2] );
2255 /* record the current triangle function name */
2256 static const char *triFuncName
= NULL
;
2258 #define USE(triFunc) \
2260 triFuncName = #triFunc; \
2261 /*printf("%s\n", triFuncName);*/ \
2262 swrast->Triangle = triFunc; \
2267 #define USE(triFunc) swrast->Triangle = triFunc;
2275 * Determine which triangle rendering function to use given the current
2276 * rendering context.
2278 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
2279 * remove tests to this code.
2282 _swrast_choose_triangle( GLcontext
*ctx
)
2284 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
2285 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
2287 if (ctx
->Polygon
.CullFlag
&&
2288 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
2289 USE(nodraw_triangle
);
2293 if (ctx
->RenderMode
==GL_RENDER
) {
2295 if (ctx
->Polygon
.SmoothFlag
) {
2296 _mesa_set_aa_triangle_function(ctx
);
2297 ASSERT(swrast
->Triangle
);
2301 if (ctx
->Depth
.OcclusionTest
&&
2303 ctx
->Depth
.Mask
== GL_FALSE
&&
2304 ctx
->Depth
.Func
== GL_LESS
&&
2305 !ctx
->Stencil
.Enabled
) {
2307 ctx
->Color
.ColorMask
[0] == 0 &&
2308 ctx
->Color
.ColorMask
[1] == 0 &&
2309 ctx
->Color
.ColorMask
[2] == 0 &&
2310 ctx
->Color
.ColorMask
[3] == 0)
2312 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
2313 USE(occlusion_zless_triangle
);
2318 if (ctx
->Texture
._ReallyEnabled
) {
2319 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
2320 const struct gl_texture_object
*texObj2D
;
2321 const struct gl_texture_image
*texImg
;
2322 GLenum minFilter
, magFilter
, envMode
, format
;
2323 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
2324 texImg
= texObj2D
? texObj2D
->Image
[texObj2D
->BaseLevel
] : NULL
;
2325 format
= texImg
? texImg
->Format
: (GLenum
) 0;
2326 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
2327 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
2328 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
2330 /* First see if we can used an optimized 2-D texture function */
2331 if (ctx
->Texture
._ReallyEnabled
==TEXTURE0_2D
2332 && texObj2D
->WrapS
==GL_REPEAT
2333 && texObj2D
->WrapT
==GL_REPEAT
2334 && texImg
->Border
==0
2335 && (format
== GL_RGB
|| format
== GL_RGBA
)
2336 && texImg
->TexFormat
->Type
== CHAN_TYPE
2337 && minFilter
== magFilter
2338 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
2339 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
2340 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
2341 if (minFilter
== GL_NEAREST
2343 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
2344 && ((swrast
->_RasterMask
== DEPTH_BIT
2345 && ctx
->Depth
.Func
== GL_LESS
2346 && ctx
->Depth
.Mask
== GL_TRUE
)
2347 || swrast
->_RasterMask
== 0)
2348 && ctx
->Polygon
.StippleFlag
== GL_FALSE
) {
2349 if (swrast
->_RasterMask
==DEPTH_BIT
) {
2350 USE(simple_z_textured_triangle
);
2353 USE(simple_textured_triangle
);
2357 if (ctx
->Texture
.Unit
[0].EnvMode
==GL_ADD
) {
2358 USE(general_textured_triangle
);
2361 USE(affine_textured_triangle
);
2366 #if 00 /* XXX these function have problems with texture coord interpolation */
2367 if (filter
==GL_NEAREST
) {
2368 USE(near_persp_textured_triangle
);
2371 USE(lin_persp_textured_triangle
);
2374 USE(general_textured_triangle
);
2378 /* More complicated textures (mipmap, multi-tex, sep specular) */
2379 GLboolean needLambda
;
2380 /* if mag filter != min filter we need to compute lambda */
2381 const struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[0]._Current
;
2382 if (obj
&& obj
->MinFilter
!= obj
->MagFilter
)
2383 needLambda
= GL_TRUE
;
2385 needLambda
= GL_FALSE
;
2386 if (swrast
->_MultiTextureEnabled
) {
2387 USE(lambda_multitextured_triangle
);
2389 else if (ctx
->_TriangleCaps
& DD_SEPERATE_SPECULAR
) {
2390 /* separate specular color interpolation */
2392 USE(lambda_textured_spec_triangle
);
2395 USE(general_textured_spec_triangle
);
2400 USE(lambda_textured_triangle
);
2403 USE(general_textured_triangle
);
2409 ASSERT(!ctx
->Texture
._ReallyEnabled
);
2410 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
2411 /* smooth shaded, no texturing, stippled or some raster ops */
2413 USE(smooth_rgba_triangle
);
2416 USE(smooth_ci_triangle
);
2420 /* flat shaded, no texturing, stippled or some raster ops */
2422 USE(flat_rgba_triangle
);
2425 USE(flat_ci_triangle
);
2430 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
2431 USE(_mesa_feedback_triangle
);
2434 /* GL_SELECT mode */
2435 USE(_mesa_select_triangle
);