1 /* $Id: s_triangle.c,v 1.33 2001/07/13 20:07:37 brianp 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.
40 #include "texformat.h"
44 #include "s_aatriangle.h"
45 #include "s_context.h"
47 #include "s_feedback.h"
49 #include "s_triangle.h"
50 #include "s_trispan.h"
54 GLboolean
_mesa_cull_triangle( GLcontext
*ctx
,
59 GLfloat ex
= v1
->win
[0] - v0
->win
[0];
60 GLfloat ey
= v1
->win
[1] - v0
->win
[1];
61 GLfloat fx
= v2
->win
[0] - v0
->win
[0];
62 GLfloat fy
= v2
->win
[1] - v0
->win
[1];
63 GLfloat c
= ex
*fy
-ey
*fx
;
65 if (c
* SWRAST_CONTEXT(ctx
)->_backface_sign
> 0)
74 * Render a flat-shaded color index triangle.
76 static void flat_ci_triangle( GLcontext
*ctx
,
84 #define RENDER_SPAN( span ) \
85 GLdepth zSpan[MAX_WIDTH]; \
86 GLfloat fogSpan[MAX_WIDTH]; \
88 for (i = 0; i < span.count; i++) { \
89 zSpan[i] = FixedToDepth(span.z); \
90 span.z += span.zStep; \
91 fogSpan[i] = span.fog; \
92 span.fog += span.fogStep; \
94 _mesa_write_monoindex_span(ctx, span.count, span.x, span.y, \
95 zSpan, fogSpan, v0->index, NULL, GL_POLYGON );
97 #include "s_tritemp.h"
103 * Render a smooth-shaded color index triangle.
105 static void smooth_ci_triangle( GLcontext
*ctx
,
112 #define INTERP_INDEX 1
114 #define RENDER_SPAN( span ) \
115 GLdepth zSpan[MAX_WIDTH]; \
116 GLfloat fogSpan[MAX_WIDTH]; \
117 GLuint indexSpan[MAX_WIDTH]; \
119 for (i = 0; i < span.count; i++) { \
120 zSpan[i] = FixedToDepth(span.z); \
121 span.z += span.zStep; \
122 indexSpan[i] = FixedToInt(span.index); \
123 span.index += span.indexStep; \
124 fogSpan[i] = span.fog; \
125 span.fog += span.fogStep; \
127 _mesa_write_index_span(ctx, span.count, span.x, span.y, \
128 zSpan, fogSpan, indexSpan, NULL, GL_POLYGON);
130 #include "s_tritemp.h"
136 * Render a flat-shaded RGBA triangle.
138 static void flat_rgba_triangle( GLcontext
*ctx
,
145 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
147 #define RENDER_SPAN( span ) \
148 GLdepth zSpan[MAX_WIDTH]; \
149 GLfloat fogSpan[MAX_WIDTH]; \
151 for (i = 0; i < span.count; i++) { \
152 zSpan[i] = FixedToDepth(span.z); \
153 span.z += span.zStep; \
154 fogSpan[i] = span.fog; \
155 span.fog += span.fogStep; \
157 _mesa_write_monocolor_span(ctx, span.count, span.x, span.y, zSpan, \
158 fogSpan, v2->color, NULL, GL_POLYGON );
160 #include "s_tritemp.h"
162 ASSERT(!ctx
->Texture
._ReallyEnabled
); /* texturing must be off */
163 ASSERT(ctx
->Light
.ShadeModel
==GL_FLAT
);
169 * Render a smooth-shaded RGBA triangle.
171 static void smooth_rgba_triangle( GLcontext
*ctx
,
179 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
181 #define INTERP_ALPHA 1
183 #define RENDER_SPAN( span ) \
184 GLdepth zSpan[MAX_WIDTH]; \
185 GLchan rgbaSpan[MAX_WIDTH][4]; \
186 GLfloat fogSpan[MAX_WIDTH]; \
188 for (i = 0; i < span.count; i++) { \
189 rgbaSpan[i][RCOMP] = FixedToInt(span.red); \
190 rgbaSpan[i][GCOMP] = FixedToInt(span.green); \
191 rgbaSpan[i][BCOMP] = FixedToInt(span.blue); \
192 rgbaSpan[i][ACOMP] = FixedToInt(span.alpha); \
193 span.red += span.redStep; \
194 span.green += span.greenStep; \
195 span.blue += span.blueStep; \
196 span.alpha += span.alphaStep; \
197 zSpan[i] = FixedToDepth(span.z); \
198 span.z += span.zStep; \
199 fogSpan[i] = span.fog; \
200 span.fog += span.fogStep; \
202 _mesa_write_rgba_span(ctx, span.count, span.x, span.y, \
203 (CONST GLdepth *) zSpan, \
204 fogSpan, rgbaSpan, NULL, GL_POLYGON);
206 #include "s_tritemp.h"
208 ASSERT(!ctx
->Texture
._ReallyEnabled
); /* texturing must be off */
209 ASSERT(ctx
->Light
.ShadeModel
==GL_SMOOTH
);
214 * Render an RGB, GL_DECAL, textured triangle.
215 * Interpolate S,T only w/out mipmapping or perspective correction.
219 static void simple_textured_triangle( GLcontext
*ctx
,
224 #define INTERP_INT_TEX 1
225 #define S_SCALE twidth
226 #define T_SCALE theight
229 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
230 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
231 GLint b = obj->BaseLevel; \
232 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
233 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
234 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
235 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
236 const GLint smask = obj->Image[b]->Width - 1; \
237 const GLint tmask = obj->Image[b]->Height - 1; \
239 /* this shouldn't happen */ \
243 #define RENDER_SPAN( span ) \
244 GLchan rgbSpan[MAX_WIDTH][3]; \
246 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
247 span.intTex[1] -= FIXED_HALF; \
248 for (i = 0; i < span.count; i++) { \
249 GLint s = FixedToInt(span.intTex[0]) & smask; \
250 GLint t = FixedToInt(span.intTex[1]) & tmask; \
251 GLint pos = (t << twidth_log2) + s; \
252 pos = pos + pos + pos; /* multiply by 3 */ \
253 rgbSpan[i][RCOMP] = texture[pos]; \
254 rgbSpan[i][GCOMP] = texture[pos+1]; \
255 rgbSpan[i][BCOMP] = texture[pos+2]; \
256 span.intTex[0] += span.intTexStep[0]; \
257 span.intTex[1] += span.intTexStep[1]; \
259 (*swrast->Driver.WriteRGBSpan)(ctx, span.count, span.x, span.y, \
260 (CONST GLchan (*)[3]) rgbSpan, NULL );
262 #include "s_tritemp.h"
267 * Render an RGB, GL_DECAL, textured triangle.
268 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
269 * perspective correction.
273 static void simple_z_textured_triangle( GLcontext
*ctx
,
279 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
280 #define INTERP_INT_TEX 1
281 #define S_SCALE twidth
282 #define T_SCALE theight
285 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
286 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
287 GLint b = obj->BaseLevel; \
288 GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
289 GLfloat theight = (GLfloat) obj->Image[b]->Height; \
290 GLint twidth_log2 = obj->Image[b]->WidthLog2; \
291 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
292 GLint smask = obj->Image[b]->Width - 1; \
293 GLint tmask = obj->Image[b]->Height - 1; \
295 /* this shouldn't happen */ \
299 #define RENDER_SPAN( span ) \
300 GLchan rgbSpan[MAX_WIDTH][3]; \
301 GLubyte mask[MAX_WIDTH]; \
303 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
304 span.intTex[1] -= FIXED_HALF; \
305 for (i = 0; i < span.count; i++) { \
306 const GLdepth z = FixedToDepth(span.z); \
308 GLint s = FixedToInt(span.intTex[0]) & smask; \
309 GLint t = FixedToInt(span.intTex[1]) & tmask; \
310 GLint pos = (t << twidth_log2) + s; \
311 pos = pos + pos + pos; /* multiply by 3 */ \
312 rgbSpan[i][RCOMP] = texture[pos]; \
313 rgbSpan[i][GCOMP] = texture[pos+1]; \
314 rgbSpan[i][BCOMP] = texture[pos+2]; \
321 span.intTex[0] += span.intTexStep[0]; \
322 span.intTex[1] += span.intTexStep[1]; \
323 span.z += span.zStep; \
325 (*swrast->Driver.WriteRGBSpan)(ctx, span.count, span.x, span.y, \
326 (CONST GLchan (*)[3]) rgbSpan, mask );
328 #include "s_tritemp.h"
332 #if CHAN_TYPE != GL_FLOAT
341 const GLchan
*texture
;
342 GLchan er
, eg
, eb
, ea
;
343 GLint tbytesline
, tsize
;
344 GLint fixedToDepthShift
;
348 affine_span(GLcontext
*ctx
, struct triangle_span
*span
,
349 struct affine_info
*info
)
351 GLint tr
, tg
, tb
, ta
;
353 /* Instead of defining a function for each mode, a test is done
354 * between the outer and inner loops. This is to reduce code size
355 * and complexity. Observe that an optimizing compiler kills
356 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
359 #define NEAREST_RGB \
366 tr = (ti * (si * tex00[0] + sf * tex01[0]) + \
367 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
368 tg = (ti * (si * tex00[1] + sf * tex01[1]) + \
369 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
370 tb = (ti * (si * tex00[2] + sf * tex01[2]) + \
371 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
374 #define NEAREST_RGBA \
380 #define LINEAR_RGBA \
381 tr = (ti * (si * tex00[0] + sf * tex01[0]) + \
382 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
383 tg = (ti * (si * tex00[1] + sf * tex01[1]) + \
384 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
385 tb = (ti * (si * tex00[2] + sf * tex01[2]) + \
386 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
387 ta = (ti * (si * tex00[3] + sf * tex01[3]) + \
388 tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT
391 dest[RCOMP] = span->red * (tr + 1) >> (FIXED_SHIFT + 8); \
392 dest[GCOMP] = span->green * (tg + 1) >> (FIXED_SHIFT + 8); \
393 dest[BCOMP] = span->blue * (tb + 1) >> (FIXED_SHIFT + 8); \
394 dest[ACOMP] = span->alpha * (ta + 1) >> (FIXED_SHIFT + 8)
397 dest[RCOMP] = ((CHAN_MAX - ta) * span->red \
398 + ((ta + 1) * tr << FIXED_SHIFT)) >> (FIXED_SHIFT + 8); \
399 dest[GCOMP] = ((CHAN_MAX - ta) * span->green \
400 + ((ta + 1) * tg << FIXED_SHIFT)) >> (FIXED_SHIFT + 8); \
401 dest[BCOMP] = ((CHAN_MAX - ta) * span->blue \
402 + ((ta + 1) * tb << FIXED_SHIFT)) >> (FIXED_SHIFT + 8); \
403 dest[ACOMP] = FixedToInt(span->alpha)
406 dest[RCOMP] = ((CHAN_MAX - tr) * span->red \
407 + (tr + 1) * info->er) >> (FIXED_SHIFT + 8); \
408 dest[GCOMP] = ((CHAN_MAX - tg) * span->green \
409 + (tg + 1) * info->eg) >> (FIXED_SHIFT + 8); \
410 dest[BCOMP] = ((CHAN_MAX - tb) * span->blue \
411 + (tb + 1) * info->eb) >> (FIXED_SHIFT + 8); \
412 dest[ACOMP] = span->alpha * (ta + 1) >> (FIXED_SHIFT + 8)
421 dest[RCOMP] = ((span->red << 8) \
422 + (tr + 1) * info->er) >> (FIXED_SHIFT + 8); \
423 dest[GCOMP] = ((span->green << 8) \
424 + (tg + 1) * info->eg) >> (FIXED_SHIFT + 8); \
425 dest[BCOMP] = ((span->blue << 8) \
426 + (tb + 1) * info->eb) >> (FIXED_SHIFT + 8); \
427 dest[ACOMP] = span->alpha * (ta + 1) >> (FIXED_SHIFT + 8)
431 #define NEAREST_RGB_REPLACE NEAREST_RGB;REPLACE
433 #define NEAREST_RGBA_REPLACE *(GLint *)dest = *(GLint *)tex00
435 #define SPAN_NEAREST(DO_TEX,COMP) \
436 for (i = 0; i < span->count; i++) { \
437 /* Isn't it necessary to use FixedFloor below?? */ \
438 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
439 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
440 GLint pos = (t << info->twidth_log2) + s; \
441 const GLchan *tex00 = info->texture + COMP * pos; \
442 zspan[i] = FixedToDepth(span->z); \
443 fogspan[i] = span->fog; \
445 span->fog += span->fogStep; \
446 span->z += span->zStep; \
447 span->red += span->redStep; \
448 span->green += span->greenStep; \
449 span->blue += span->blueStep; \
450 span->alpha += span->alphaStep; \
451 span->intTex[0] += span->intTexStep[0]; \
452 span->intTex[1] += span->intTexStep[1]; \
456 #define SPAN_LINEAR(DO_TEX,COMP) \
457 for (i = 0; i < span->count; i++) { \
458 /* Isn't it necessary to use FixedFloor below?? */ \
459 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
460 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
461 GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
462 GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
463 GLfixed si = FIXED_FRAC_MASK - sf; \
464 GLfixed ti = FIXED_FRAC_MASK - tf; \
465 GLint pos = (t << info->twidth_log2) + s; \
466 const GLchan *tex00 = info->texture + COMP * pos; \
467 const GLchan *tex10 = tex00 + info->tbytesline; \
468 const GLchan *tex01 = tex00 + COMP; \
469 const GLchan *tex11 = tex10 + COMP; \
472 if (t == info->tmask) { \
473 tex10 -= info->tsize; \
474 tex11 -= info->tsize; \
476 if (s == info->smask) { \
477 tex01 -= info->tbytesline; \
478 tex11 -= info->tbytesline; \
480 zspan[i] = FixedToDepth(span->z); \
481 fogspan[i] = span->fog; \
483 span->fog += span->fogStep; \
484 span->z += span->zStep; \
485 span->red += span->redStep; \
486 span->green += span->greenStep; \
487 span->blue += span->blueStep; \
488 span->alpha += span->alphaStep; \
489 span->intTex[0] += span->intTexStep[0]; \
490 span->intTex[1] += span->intTexStep[1]; \
494 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
497 GLdepth zspan
[MAX_WIDTH
];
498 GLfloat fogspan
[MAX_WIDTH
];
499 GLchan rgba
[MAX_WIDTH
][4];
500 GLchan
*dest
= rgba
[0];
501 const GLint fixedToDepthShift
= info
->fixedToDepthShift
;
503 span
->intTex
[0] -= FIXED_HALF
;
504 span
->intTex
[1] -= FIXED_HALF
;
505 switch (info
->filter
) {
507 switch (info
->format
) {
509 switch (info
->envmode
) {
511 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
515 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
518 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
521 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
528 switch(info
->envmode
) {
530 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
533 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
536 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
539 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
542 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
552 span
->intTex
[0] -= FIXED_HALF
;
553 span
->intTex
[1] -= FIXED_HALF
;
554 switch (info
->format
) {
556 switch (info
->envmode
) {
558 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
562 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
565 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
568 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
575 switch (info
->envmode
) {
577 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
580 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
583 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
586 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
589 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
597 _mesa_write_rgba_span(ctx
, span
->count
, span
->x
, span
->y
,
598 zspan
, fogspan
, rgba
, NULL
, GL_POLYGON
);
608 * Render an RGB/RGBA textured triangle without perspective correction.
610 static void affine_textured_triangle( GLcontext
*ctx
,
617 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
619 #define INTERP_ALPHA 1
620 #define INTERP_INT_TEX 1
621 #define S_SCALE twidth
622 #define T_SCALE theight
625 struct affine_info info; \
626 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
627 struct gl_texture_object *obj = unit->Current2D; \
628 GLint b = obj->BaseLevel; \
629 GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
630 GLfloat theight = (GLfloat) obj->Image[b]->Height; \
631 info.fixedToDepthShift = ctx->Visual.depthBits <= 16 ? FIXED_SHIFT : 0;\
632 info.texture = (const GLchan *) obj->Image[b]->Data; \
633 info.twidth_log2 = obj->Image[b]->WidthLog2; \
634 info.smask = obj->Image[b]->Width - 1; \
635 info.tmask = obj->Image[b]->Height - 1; \
636 info.format = obj->Image[b]->Format; \
637 info.filter = obj->MinFilter; \
638 info.envmode = unit->EnvMode; \
640 if (info.envmode == GL_BLEND) { \
641 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
642 info.er = FloatToFixed(unit->EnvColor[RCOMP]); \
643 info.eg = FloatToFixed(unit->EnvColor[GCOMP]); \
644 info.eb = FloatToFixed(unit->EnvColor[BCOMP]); \
645 info.ea = FloatToFixed(unit->EnvColor[ACOMP]); \
647 if (!info.texture) { \
648 /* this shouldn't happen */ \
652 switch (info.format) { \
656 info.tbytesline = obj->Image[b]->Width; \
658 case GL_LUMINANCE_ALPHA: \
659 info.tbytesline = obj->Image[b]->Width * 2; \
662 info.tbytesline = obj->Image[b]->Width * 3; \
665 info.tbytesline = obj->Image[b]->Width * 4; \
668 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
671 info.tsize = obj->Image[b]->Height * info.tbytesline;
673 #define RENDER_SPAN( span ) \
674 if (ctx->Light.ShadeModel == GL_FLAT) { \
675 span.red = IntToFixed(v2->color[RCOMP]); \
676 span.green = IntToFixed(v2->color[GCOMP]); \
677 span.blue = IntToFixed(v2->color[BCOMP]); \
678 span.alpha = IntToFixed(v2->color[ACOMP]); \
680 affine_span(ctx, &span, &info);
682 #include "s_tritemp.h"
686 #endif /* CHAN_BITS != GL_FLOAT */
696 const GLchan
*texture
;
697 GLchan er
, eg
, eb
, ea
;
698 GLint tbytesline
, tsize
;
699 GLint fixedToDepthShift
;
704 fast_persp_span(GLcontext
*ctx
, struct triangle_span
*span
,
705 struct persp_info
*info
)
707 GLint tr
, tg
, tb
, ta
;
709 /* Instead of defining a function for each mode, a test is done
710 * between the outer and inner loops. This is to reduce code size
711 * and complexity. Observe that an optimizing compiler kills
712 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
714 #define SPAN_NEAREST(DO_TEX,COMP) \
715 for (i = 0; i < span->count; i++) { \
716 GLdouble invQ = tex_coord[2] ? \
717 (1.0 / tex_coord[2]) : 1.0; \
718 GLfloat s_tmp = tex_coord[0] * invQ; \
719 GLfloat t_tmp = tex_coord[1] * invQ; \
720 GLint s = IFLOOR(s_tmp) & info->smask; \
721 GLint t = IFLOOR(t_tmp) & info->tmask; \
722 GLint pos = (t << info->twidth_log2) + s; \
723 const GLchan *tex00 = info->texture + COMP * pos; \
724 zspan[i] = FixedToDepth(span->z); \
725 fogspan[i] = span->fog; \
727 span->fog += span->fogStep; \
728 span->z += span->zStep; \
729 span->red += span->redStep; \
730 span->green += span->greenStep; \
731 span->blue += span->blueStep; \
732 span->alpha += span->alphaStep; \
733 tex_coord[0] += tex_step[0]; \
734 tex_coord[1] += tex_step[1]; \
735 tex_coord[2] += tex_step[2]; \
739 #define SPAN_LINEAR(DO_TEX,COMP) \
740 for (i = 0; i < span->count; i++) { \
741 GLdouble invQ = tex_coord[2] ? \
742 (1.0 / tex_coord[2]) : 1.0; \
743 GLfloat s_tmp = tex_coord[0] * invQ - 0.5F; \
744 GLfloat t_tmp = tex_coord[1] * invQ - 0.5F; \
745 GLfixed s_fix = FloatToFixed(s_tmp); \
746 GLfixed t_fix = FloatToFixed(t_tmp); \
747 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
748 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
749 GLfixed sf = s_fix & FIXED_FRAC_MASK; \
750 GLfixed tf = t_fix & FIXED_FRAC_MASK; \
751 GLfixed si = FIXED_FRAC_MASK - sf; \
752 GLfixed ti = FIXED_FRAC_MASK - tf; \
753 GLint pos = (t << info->twidth_log2) + s; \
754 const GLchan *tex00 = info->texture + COMP * pos; \
755 const GLchan *tex10 = tex00 + info->tbytesline; \
756 const GLchan *tex01 = tex00 + COMP; \
757 const GLchan *tex11 = tex10 + COMP; \
760 if (t == info->tmask) { \
761 tex10 -= info->tsize; \
762 tex11 -= info->tsize; \
764 if (s == info->smask) { \
765 tex01 -= info->tbytesline; \
766 tex11 -= info->tbytesline; \
768 zspan[i] = FixedToDepth(span->z); \
769 fogspan[i] = span->fog; \
771 span->fog += span->fogStep; \
772 span->z += span->zStep; \
773 span->red += span->redStep; \
774 span->green += span->greenStep; \
775 span->blue += span->blueStep; \
776 span->alpha += span->alphaStep; \
777 tex_coord[0] += tex_step[0]; \
778 tex_coord[1] += tex_step[1]; \
779 tex_coord[2] += tex_step[2]; \
783 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
786 GLdepth zspan
[MAX_WIDTH
];
787 GLfloat tex_coord
[3], tex_step
[3];
788 GLfloat fogspan
[MAX_WIDTH
];
789 GLchan rgba
[MAX_WIDTH
][4];
790 GLchan
*dest
= rgba
[0];
791 const GLint fixedToDepthShift
= info
->fixedToDepthShift
;
793 tex_coord
[0] = span
->tex
[0][0] * (info
->smask
+ 1),
794 tex_step
[0] = span
->texStep
[0][0] * (info
->smask
+ 1);
795 tex_coord
[1] = span
->tex
[0][1] * (info
->tmask
+ 1),
796 tex_step
[1] = span
->texStep
[0][1] * (info
->tmask
+ 1);
797 /* span->tex[0][2] only if 3D-texturing, here only 2D */
798 tex_coord
[2] = span
->tex
[0][3],
799 tex_step
[2] = span
->texStep
[0][3];
801 switch (info
->filter
) {
803 switch (info
->format
) {
805 switch (info
->envmode
) {
807 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
811 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
814 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
817 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
824 switch(info
->envmode
) {
826 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
829 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
832 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
835 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
838 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
848 switch (info
->format
) {
850 switch (info
->envmode
) {
852 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
856 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
859 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
862 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
869 switch (info
->envmode
) {
871 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
874 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
877 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
880 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
883 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
892 /* This does not seem to be necessary, but I don't know !! */
893 /* span->tex[0][0] = tex_coord[0] / (info->smask + 1),
894 span->tex[0][1] = tex_coord[1] / (info->tmask + 1),*/
895 /* span->tex[0][2] only if 3D-texturing, here only 2D */
896 /* span->tex[0][3] = tex_coord[2]; */
898 _mesa_write_rgba_span(ctx
, span
->count
, span
->x
, span
->y
,
899 zspan
, fogspan
, rgba
, NULL
, GL_POLYGON
);
909 * Render an perspective corrected RGB/RGBA textured triangle.
910 * The Q (aka V in Mesa) coordinate must be zero such that the divide
911 * by interpolated Q/W comes out right.
914 static void persp_textured_triangle( GLcontext
*ctx
,
921 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
923 #define INTERP_ALPHA 1
927 struct persp_info info; \
928 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
929 struct gl_texture_object *obj = unit->Current2D; \
930 GLint b = obj->BaseLevel; \
931 info.fixedToDepthShift = ctx->Visual.depthBits <= 16 ? FIXED_SHIFT : 0;\
932 info.texture = (const GLchan *) obj->Image[b]->Data; \
933 info.twidth_log2 = obj->Image[b]->WidthLog2; \
934 info.smask = obj->Image[b]->Width - 1; \
935 info.tmask = obj->Image[b]->Height - 1; \
936 info.format = obj->Image[b]->Format; \
937 info.filter = obj->MinFilter; \
938 info.envmode = unit->EnvMode; \
940 if (info.envmode == GL_BLEND) { \
941 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
942 info.er = FloatToFixed(unit->EnvColor[RCOMP]); \
943 info.eg = FloatToFixed(unit->EnvColor[GCOMP]); \
944 info.eb = FloatToFixed(unit->EnvColor[BCOMP]); \
945 info.ea = FloatToFixed(unit->EnvColor[ACOMP]); \
947 if (!info.texture) { \
948 /* this shouldn't happen */ \
952 switch (info.format) { \
956 info.tbytesline = obj->Image[b]->Width; \
958 case GL_LUMINANCE_ALPHA: \
959 info.tbytesline = obj->Image[b]->Width * 2; \
962 info.tbytesline = obj->Image[b]->Width * 3; \
965 info.tbytesline = obj->Image[b]->Width * 4; \
968 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
971 info.tsize = obj->Image[b]->Height * info.tbytesline;
973 #define RENDER_SPAN( span ) \
974 if (ctx->Light.ShadeModel == GL_FLAT) { \
975 span.red = IntToFixed(v2->color[RCOMP]); \
976 span.green = IntToFixed(v2->color[GCOMP]); \
977 span.blue = IntToFixed(v2->color[BCOMP]); \
978 span.alpha = IntToFixed(v2->color[ACOMP]); \
980 fast_persp_span(ctx, &span, &info);
982 #include "s_tritemp.h"
989 * Generate arrays of fragment colors, z, fog, texcoords, etc from a
990 * triangle span object. Then call the span/fragment processsing
991 * functions in s_span.[ch]. This is used by a bunch of the textured
992 * triangle functions.
995 rasterize_span(GLcontext
*ctx
, const struct triangle_span
*span
)
997 DEFMARRAY(GLchan
, rgba
, MAX_WIDTH
, 4);
998 DEFMARRAY(GLchan
, spec
, MAX_WIDTH
, 4);
999 DEFARRAY(GLuint
, index
, MAX_WIDTH
);
1000 DEFARRAY(GLuint
, z
, MAX_WIDTH
);
1001 DEFARRAY(GLfloat
, fog
, MAX_WIDTH
);
1002 DEFARRAY(GLfloat
, sTex
, MAX_WIDTH
);
1003 DEFARRAY(GLfloat
, tTex
, MAX_WIDTH
);
1004 DEFARRAY(GLfloat
, rTex
, MAX_WIDTH
);
1005 DEFARRAY(GLfloat
, lambda
, MAX_WIDTH
);
1006 DEFMARRAY(GLfloat
, msTex
, MAX_TEXTURE_UNITS
, MAX_WIDTH
);
1007 DEFMARRAY(GLfloat
, mtTex
, MAX_TEXTURE_UNITS
, MAX_WIDTH
);
1008 DEFMARRAY(GLfloat
, mrTex
, MAX_TEXTURE_UNITS
, MAX_WIDTH
);
1009 DEFMARRAY(GLfloat
, mLambda
, MAX_TEXTURE_UNITS
, MAX_WIDTH
);
1011 CHECKARRAY(rgba
, return);
1012 CHECKARRAY(spec
, return);
1013 CHECKARRAY(index
, return);
1014 CHECKARRAY(z
, return);
1015 CHECKARRAY(fog
, return);
1016 CHECKARRAY(sTex
, return);
1017 CHECKARRAY(tTex
, return);
1018 CHECKARRAY(rTex
, return);
1019 CHECKARRAY(lambda
, return);
1020 CHECKARRAY(msTex
, return);
1021 CHECKARRAY(mtTex
, return);
1022 CHECKARRAY(mrTex
, return);
1023 CHECKARRAY(mLambda
, return);
1025 if (span
->activeMask
& SPAN_RGBA
) {
1026 GLfixed r
= span
->red
;
1027 GLfixed g
= span
->green
;
1028 GLfixed b
= span
->blue
;
1029 GLfixed a
= span
->alpha
;
1031 for (i
= 0; i
< span
->count
; i
++) {
1032 rgba
[i
][RCOMP
] = FixedToInt(r
);
1033 rgba
[i
][GCOMP
] = FixedToInt(g
);
1034 rgba
[i
][BCOMP
] = FixedToInt(b
);
1035 rgba
[i
][ACOMP
] = FixedToInt(a
);
1037 g
+= span
->greenStep
;
1038 b
+= span
->blueStep
;
1039 a
+= span
->alphaStep
;
1042 if (span
->activeMask
& SPAN_SPEC
) {
1043 GLfixed r
= span
->specRed
;
1044 GLfixed g
= span
->specGreen
;
1045 GLfixed b
= span
->specBlue
;
1047 for (i
= 0; i
< span
->count
; i
++) {
1048 spec
[i
][RCOMP
] = FixedToInt(r
);
1049 spec
[i
][GCOMP
] = FixedToInt(g
);
1050 spec
[i
][BCOMP
] = FixedToInt(b
);
1051 r
+= span
->specRedStep
;
1052 g
+= span
->specGreenStep
;
1053 b
+= span
->specBlueStep
;
1056 if (span
->activeMask
& SPAN_INDEX
) {
1058 GLfixed ind
= span
->index
;
1059 for (i
= 0; i
< span
->count
; i
++) {
1060 index
[i
] = FixedToInt(ind
);
1061 ind
+= span
->indexStep
;
1064 if (span
->activeMask
& SPAN_Z
) {
1065 if (ctx
->Visual
.depthBits
<= 16) {
1067 GLfixed zval
= span
->z
;
1068 for (i
= 0; i
< span
->count
; i
++) {
1069 z
[i
] = FixedToInt(zval
);
1070 zval
+= span
->zStep
;
1074 /* Deep Z buffer, no fixed->int shift */
1076 GLfixed zval
= span
->z
;
1077 for (i
= 0; i
< span
->count
; i
++) {
1079 zval
+= span
->zStep
;
1083 if (span
->activeMask
& SPAN_FOG
) {
1085 GLfloat f
= span
->fog
;
1086 for (i
= 0; i
< span
->count
; i
++) {
1091 if (span
->activeMask
& SPAN_TEXTURE
) {
1092 if (ctx
->Texture
._ReallyEnabled
& ~TEXTURE0_ANY
) {
1094 if (span
->activeMask
& SPAN_LAMBDA
) {
1097 for (u
= 0; u
< MAX_TEXTURE_UNITS
; u
++) {
1098 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
1099 GLfloat s
= span
->tex
[u
][0];
1100 GLfloat t
= span
->tex
[u
][1];
1101 GLfloat r
= span
->tex
[u
][2];
1102 GLfloat q
= span
->tex
[u
][3];
1104 for (i
= 0; i
< span
->count
; i
++) {
1105 const GLfloat invQ
= (q
== 0.0F
) ? 1.0 : (1.0F
/ q
);
1106 msTex
[u
][i
] = s
* invQ
;
1107 mtTex
[u
][i
] = t
* invQ
;
1108 mrTex
[u
][i
] = r
* invQ
;
1109 mLambda
[u
][i
] = log(span
->rho
[u
] * invQ
* invQ
) * 1.442695F
* 0.5F
;
1110 s
+= span
->texStep
[u
][0];
1111 t
+= span
->texStep
[u
][1];
1112 r
+= span
->texStep
[u
][2];
1113 q
+= span
->texStep
[u
][3];
1119 /* without lambda */
1121 for (u
= 0; u
< MAX_TEXTURE_UNITS
; u
++) {
1122 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
1123 GLfloat s
= span
->tex
[u
][0];
1124 GLfloat t
= span
->tex
[u
][1];
1125 GLfloat r
= span
->tex
[u
][2];
1126 GLfloat q
= span
->tex
[u
][3];
1128 for (i
= 0; i
< span
->count
; i
++) {
1129 const GLfloat invQ
= (q
== 0.0F
) ? 1.0 : (1.0F
/ q
);
1130 msTex
[u
][i
] = s
* invQ
;
1131 mtTex
[u
][i
] = t
* invQ
;
1132 mrTex
[u
][i
] = r
* invQ
;
1133 s
+= span
->texStep
[u
][0];
1134 t
+= span
->texStep
[u
][1];
1135 r
+= span
->texStep
[u
][2];
1136 q
+= span
->texStep
[u
][3];
1143 /* just texture unit 0 */
1144 if (span
->activeMask
& SPAN_LAMBDA
) {
1146 GLfloat s
= span
->tex
[0][0];
1147 GLfloat t
= span
->tex
[0][1];
1148 GLfloat r
= span
->tex
[0][2];
1149 GLfloat q
= span
->tex
[0][3];
1151 for (i
= 0; i
< span
->count
; i
++) {
1152 const GLfloat invQ
= (q
== 0.0F
) ? 1.0 : (1.0F
/ q
);
1156 lambda
[i
] = log(span
->rho
[0] * invQ
* invQ
) * 1.442695F
* 0.5F
;
1157 s
+= span
->texStep
[0][0];
1158 t
+= span
->texStep
[0][1];
1159 r
+= span
->texStep
[0][2];
1160 q
+= span
->texStep
[0][3];
1164 /* without lambda */
1165 GLfloat s
= span
->tex
[0][0];
1166 GLfloat t
= span
->tex
[0][1];
1167 GLfloat r
= span
->tex
[0][2];
1168 GLfloat q
= span
->tex
[0][3];
1170 for (i
= 0; i
< span
->count
; i
++) {
1171 const GLfloat invQ
= (q
== 0.0F
) ? 1.0 : (1.0F
/ q
);
1175 s
+= span
->texStep
[0][0];
1176 t
+= span
->texStep
[0][1];
1177 r
+= span
->texStep
[0][2];
1178 q
+= span
->texStep
[0][3];
1183 /* XXX keep this? */
1184 if (span
->activeMask
& SPAN_INT_TEXTURE
) {
1185 GLint intTexcoord
[MAX_WIDTH
][2];
1186 GLfixed s
= span
->intTex
[0];
1187 GLfixed t
= span
->intTex
[1];
1189 for (i
= 0; i
< span
->count
; i
++) {
1190 intTexcoord
[i
][0] = FixedToInt(s
);
1191 intTexcoord
[i
][1] = FixedToInt(t
);
1192 s
+= span
->intTexStep
[0];
1193 t
+= span
->intTexStep
[1];
1197 /* examine activeMask and call a s_span.c function */
1198 if (span
->activeMask
& SPAN_TEXTURE
) {
1199 const GLfloat
*fogPtr
;
1200 if (span
->activeMask
& SPAN_FOG
)
1205 if (ctx
->Texture
._ReallyEnabled
& ~TEXTURE0_ANY
) {
1206 if (span
->activeMask
& SPAN_SPEC
) {
1207 _mesa_write_multitexture_span(ctx
, span
->count
, span
->x
, span
->y
,
1209 (const GLfloat (*)[MAX_WIDTH
]) msTex
,
1210 (const GLfloat (*)[MAX_WIDTH
]) mtTex
,
1211 (const GLfloat (*)[MAX_WIDTH
]) mrTex
,
1212 (GLfloat (*)[MAX_WIDTH
]) mLambda
,
1213 rgba
, (CONST
GLchan (*)[4]) spec
,
1217 _mesa_write_multitexture_span(ctx
, span
->count
, span
->x
, span
->y
,
1219 (const GLfloat (*)[MAX_WIDTH
]) msTex
,
1220 (const GLfloat (*)[MAX_WIDTH
]) mtTex
,
1221 (const GLfloat (*)[MAX_WIDTH
]) mrTex
,
1222 (GLfloat (*)[MAX_WIDTH
]) mLambda
,
1223 rgba
, NULL
, NULL
, GL_POLYGON
);
1227 /* single texture */
1228 if (span
->activeMask
& SPAN_SPEC
) {
1229 _mesa_write_texture_span(ctx
, span
->count
, span
->x
, span
->y
,
1230 z
, fogPtr
, sTex
, tTex
, rTex
, lambda
,
1231 rgba
, (CONST
GLchan (*)[4]) spec
,
1235 _mesa_write_texture_span(ctx
, span
->count
, span
->x
, span
->y
,
1236 z
, fogPtr
, sTex
, tTex
, rTex
, lambda
,
1237 rgba
, NULL
, NULL
, GL_POLYGON
);
1242 _mesa_problem(ctx
, "rasterize_span() should only be used for texturing");
1257 UNDEFARRAY(mLambda
);
1264 * Render a smooth-shaded, textured, RGBA triangle.
1265 * Interpolate S,T,R with perspective correction, w/out mipmapping.
1267 static void general_textured_triangle( GLcontext
*ctx
,
1270 const SWvertex
*v2
)
1273 #define INTERP_FOG 1
1274 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1275 #define INTERP_RGB 1
1276 #define INTERP_ALPHA 1
1277 #define INTERP_TEX 1
1279 #define SETUP_CODE \
1280 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1281 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1282 const GLboolean flatShade = (ctx->Light.ShadeModel==GL_FLAT); \
1283 GLfixed rFlat, gFlat, bFlat, aFlat; \
1284 DEFARRAY(GLfloat, sSpan, MAX_WIDTH); /* mac 32k limitation */ \
1285 DEFARRAY(GLfloat, tSpan, MAX_WIDTH); /* mac 32k limitation */ \
1286 DEFARRAY(GLfloat, uSpan, MAX_WIDTH); /* mac 32k limitation */ \
1287 CHECKARRAY(sSpan, return); /* mac 32k limitation */ \
1288 CHECKARRAY(tSpan, return); /* mac 32k limitation */ \
1289 CHECKARRAY(uSpan, return); /* mac 32k limitation */ \
1291 rFlat = ChanToFixed(v2->color[RCOMP]); \
1292 gFlat = ChanToFixed(v2->color[GCOMP]); \
1293 bFlat = ChanToFixed(v2->color[BCOMP]); \
1294 aFlat = ChanToFixed(v2->color[ACOMP]); \
1296 span.texWidth[0] = (GLfloat) texImage->Width; \
1297 span.texHeight[0] = (GLfloat) texImage->Height; \
1298 (void) fixedToDepthShift;
1300 #define RENDER_SPAN( span ) \
1301 GLdepth zSpan[MAX_WIDTH]; \
1302 GLfloat fogSpan[MAX_WIDTH]; \
1303 GLchan rgbaSpan[MAX_WIDTH][4]; \
1306 span.red = rFlat; span.redStep = 0; \
1307 span.green = gFlat; span.greenStep = 0; \
1308 span.blue = bFlat; span.blueStep = 0; \
1309 span.alpha = aFlat; span.alphaStep = 0; \
1311 /* NOTE: we could just call rasterize_span() here instead */ \
1312 for (i = 0; i < span.count; i++) { \
1313 GLdouble invQ = span.tex[0][3] ? (1.0 / span.tex[0][3]) : 1.0; \
1314 zSpan[i] = FixedToDepth(span.z); \
1315 span.z += span.zStep; \
1316 fogSpan[i] = span.fog; \
1317 span.fog += span.fogStep; \
1318 rgbaSpan[i][RCOMP] = FixedToInt(span.red); \
1319 rgbaSpan[i][GCOMP] = FixedToInt(span.green); \
1320 rgbaSpan[i][BCOMP] = FixedToInt(span.blue); \
1321 rgbaSpan[i][ACOMP] = FixedToInt(span.alpha); \
1322 span.red += span.redStep; \
1323 span.green += span.greenStep; \
1324 span.blue += span.blueStep; \
1325 span.alpha += span.alphaStep; \
1326 sSpan[i] = span.tex[0][0] * invQ; \
1327 tSpan[i] = span.tex[0][1] * invQ; \
1328 uSpan[i] = span.tex[0][2] * invQ; \
1329 span.tex[0][0] += span.texStep[0][0]; \
1330 span.tex[0][1] += span.texStep[0][1]; \
1331 span.tex[0][2] += span.texStep[0][2]; \
1332 span.tex[0][3] += span.texStep[0][3]; \
1334 _mesa_write_texture_span(ctx, span.count, span.x, span.y, \
1335 zSpan, fogSpan, sSpan, tSpan, uSpan, \
1336 NULL, rgbaSpan, NULL, NULL, GL_POLYGON );
1338 #define CLEANUP_CODE \
1339 UNDEFARRAY(sSpan); /* mac 32k limitation */ \
1340 UNDEFARRAY(tSpan); \
1343 #include "s_tritemp.h"
1348 * Render a smooth-shaded, textured, RGBA triangle with separate specular
1349 * color interpolation.
1350 * Interpolate texcoords with perspective correction, w/out mipmapping.
1352 static void general_textured_spec_triangle( GLcontext
*ctx
,
1355 const SWvertex
*v2
)
1358 #define INTERP_FOG 1
1359 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1360 #define INTERP_RGB 1
1361 #define INTERP_SPEC 1
1362 #define INTERP_ALPHA 1
1363 #define INTERP_TEX 1
1365 #define SETUP_CODE \
1366 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1367 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1368 const GLboolean flatShade = (ctx->Light.ShadeModel == GL_FLAT); \
1369 GLfixed rFlat, gFlat, bFlat, aFlat; \
1370 GLfixed srFlat, sgFlat, sbFlat; \
1372 rFlat = ChanToFixed(v2->color[RCOMP]); \
1373 gFlat = ChanToFixed(v2->color[GCOMP]); \
1374 bFlat = ChanToFixed(v2->color[BCOMP]); \
1375 aFlat = ChanToFixed(v2->color[ACOMP]); \
1376 srFlat = ChanToFixed(v2->specular[RCOMP]); \
1377 sgFlat = ChanToFixed(v2->specular[GCOMP]); \
1378 sbFlat = ChanToFixed(v2->specular[BCOMP]); \
1380 span.texWidth[0] = (GLfloat) texImage->Width; \
1381 span.texHeight[0] = (GLfloat) texImage->Height; \
1382 (void) fixedToDepthShift;
1384 #define RENDER_SPAN( span ) \
1386 span.red = rFlat; span.redStep = 0; \
1387 span.green = gFlat; span.greenStep = 0; \
1388 span.blue = bFlat; span.blueStep = 0; \
1389 span.alpha = aFlat; span.alphaStep = 0; \
1390 span.specRed = srFlat; span.specRedStep = 0; \
1391 span.specGreen = sgFlat; span.specGreenStep = 0; \
1392 span.specBlue = sbFlat; span.specBlueStep = 0; \
1394 rasterize_span(ctx, &span);
1396 #include "s_tritemp.h"
1401 * Render a smooth-shaded, textured, RGBA triangle.
1402 * Interpolate S,T,R with perspective correction and compute lambda for
1403 * each fragment. Lambda is used to determine whether to use the
1404 * minification or magnification filter. If minification and using
1405 * mipmaps, lambda is also used to select the texture level of detail.
1407 static void lambda_textured_triangle( GLcontext
*ctx
,
1410 const SWvertex
*v2
)
1413 #define INTERP_FOG 1
1414 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1415 #define INTERP_RGB 1
1416 #define INTERP_ALPHA 1
1417 #define INTERP_TEX 1
1418 #define INTERP_LAMBDA 1
1420 #define SETUP_CODE \
1421 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1422 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1423 const GLboolean flatShade = (ctx->Light.ShadeModel==GL_FLAT); \
1424 GLfixed rFlat, gFlat, bFlat, aFlat; \
1425 GLfixed srFlat, sgFlat, sbFlat; \
1427 rFlat = ChanToFixed(v2->color[RCOMP]); \
1428 gFlat = ChanToFixed(v2->color[GCOMP]); \
1429 bFlat = ChanToFixed(v2->color[BCOMP]); \
1430 aFlat = ChanToFixed(v2->color[ACOMP]); \
1431 srFlat = ChanToFixed(v2->specular[RCOMP]); \
1432 sgFlat = ChanToFixed(v2->specular[GCOMP]); \
1433 sbFlat = ChanToFixed(v2->specular[BCOMP]); \
1435 span.texWidth[0] = (GLfloat) texImage->Width; \
1436 span.texHeight[0] = (GLfloat) texImage->Height; \
1437 (void) fixedToDepthShift;
1439 #define RENDER_SPAN( span ) \
1441 span.red = rFlat; span.redStep = 0; \
1442 span.green = gFlat; span.greenStep = 0; \
1443 span.blue = bFlat; span.blueStep = 0; \
1444 span.alpha = aFlat; span.alphaStep = 0; \
1445 span.specRed = srFlat; span.specRedStep = 0; \
1446 span.specGreen = sgFlat; span.specGreenStep = 0; \
1447 span.specBlue = sbFlat; span.specBlueStep = 0; \
1449 rasterize_span(ctx, &span);
1451 #include "s_tritemp.h"
1456 * Render a smooth-shaded, textured, RGBA triangle with separate specular
1458 * Interpolate S,T,R with perspective correction and compute lambda for
1459 * each fragment. Lambda is used to determine whether to use the
1460 * minification or magnification filter. If minification and using
1461 * mipmaps, lambda is also used to select the texture level of detail.
1463 static void lambda_textured_spec_triangle( GLcontext
*ctx
,
1466 const SWvertex
*v2
)
1469 #define INTERP_FOG 1
1470 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1471 #define INTERP_RGB 1
1472 #define INTERP_SPEC 1
1473 #define INTERP_ALPHA 1
1474 #define INTERP_TEX 1
1475 #define INTERP_LAMBDA 1
1477 #define SETUP_CODE \
1478 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1479 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1480 const GLboolean flatShade = (ctx->Light.ShadeModel == GL_FLAT); \
1481 GLfixed rFlat, gFlat, bFlat, aFlat; \
1482 GLfixed srFlat, sgFlat, sbFlat; \
1484 rFlat = ChanToFixed(v2->color[RCOMP]); \
1485 gFlat = ChanToFixed(v2->color[GCOMP]); \
1486 bFlat = ChanToFixed(v2->color[BCOMP]); \
1487 aFlat = ChanToFixed(v2->color[ACOMP]); \
1488 srFlat = ChanToFixed(v2->specular[RCOMP]); \
1489 sgFlat = ChanToFixed(v2->specular[GCOMP]); \
1490 sbFlat = ChanToFixed(v2->specular[BCOMP]); \
1492 span.texWidth[0] = (GLfloat) texImage->Width; \
1493 span.texHeight[0] = (GLfloat) texImage->Height; \
1494 (void) fixedToDepthShift;
1496 #define RENDER_SPAN( span ) \
1498 span.red = rFlat; span.redStep = 0; \
1499 span.green = gFlat; span.greenStep = 0; \
1500 span.blue = bFlat; span.blueStep = 0; \
1501 span.alpha = aFlat; span.alphaStep = 0; \
1502 span.specRed = srFlat; span.specRedStep = 0; \
1503 span.specGreen = sgFlat; span.specGreenStep = 0; \
1504 span.specBlue = sbFlat; span.specBlueStep = 0; \
1506 rasterize_span(ctx, &span);
1508 #include "s_tritemp.h"
1513 * This is the big one!
1514 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates
1515 * with lambda (LOD).
1519 lambda_multitextured_triangle( GLcontext
*ctx
,
1522 const SWvertex
*v2
)
1526 #define INTERP_FOG 1
1527 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1528 #define INTERP_RGB 1
1529 #define INTERP_ALPHA 1
1530 #define INTERP_SPEC 1
1531 #define INTERP_MULTITEX 1
1532 #define INTERP_LAMBDA 1
1534 #define SETUP_CODE \
1535 const GLboolean flatShade = (ctx->Light.ShadeModel == GL_FLAT); \
1536 GLfixed rFlat, gFlat, bFlat, aFlat; \
1537 GLfixed srFlat, sgFlat, sbFlat; \
1540 rFlat = ChanToFixed(v2->color[RCOMP]); \
1541 gFlat = ChanToFixed(v2->color[GCOMP]); \
1542 bFlat = ChanToFixed(v2->color[BCOMP]); \
1543 aFlat = ChanToFixed(v2->color[ACOMP]); \
1544 srFlat = ChanToFixed(v2->specular[RCOMP]); \
1545 sgFlat = ChanToFixed(v2->specular[GCOMP]); \
1546 sbFlat = ChanToFixed(v2->specular[BCOMP]); \
1548 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) { \
1549 if (ctx->Texture.Unit[u]._ReallyEnabled) { \
1550 const struct gl_texture_object *texObj; \
1551 const struct gl_texture_image *texImage; \
1552 texObj = ctx->Texture.Unit[u]._Current; \
1553 texImage = texObj->Image[texObj->BaseLevel]; \
1554 span.texWidth[u] = (GLfloat) texImage->Width; \
1555 span.texHeight[u] = (GLfloat) texImage->Height; \
1558 (void) fixedToDepthShift;
1560 #define RENDER_SPAN( span ) \
1562 span.red = rFlat; span.redStep = 0; \
1563 span.green = gFlat; span.greenStep = 0; \
1564 span.blue = bFlat; span.blueStep = 0; \
1565 span.alpha = aFlat; span.alphaStep = 0; \
1566 span.specRed = srFlat; span.specRedStep = 0; \
1567 span.specGreen = sgFlat; span.specGreenStep = 0; \
1568 span.specBlue = sbFlat; span.specBlueStep = 0; \
1570 rasterize_span(ctx, &span);
1572 #include "s_tritemp.h"
1577 static void occlusion_zless_triangle( GLcontext
*ctx
,
1580 const SWvertex
*v2
)
1582 if (ctx
->OcclusionResult
) {
1586 #define DO_OCCLUSION_TEST
1588 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1590 #define RENDER_SPAN( span ) \
1592 for (i = 0; i < span.count; i++) { \
1593 GLdepth z = FixedToDepth(span.z); \
1594 if (z < zRow[i]) { \
1595 ctx->OcclusionResult = GL_TRUE; \
1598 span.z += span.zStep; \
1601 #include "s_tritemp.h"
1604 static void nodraw_triangle( GLcontext
*ctx
,
1607 const SWvertex
*v2
)
1609 (void) (ctx
&& v0
&& v1
&& v2
);
1612 void _swrast_add_spec_terms_triangle( GLcontext
*ctx
,
1615 const SWvertex
*v2
)
1617 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
1618 SWvertex
*ncv1
= (SWvertex
*)v1
;
1619 SWvertex
*ncv2
= (SWvertex
*)v2
;
1621 COPY_CHAN4( c
[0], ncv0
->color
);
1622 COPY_CHAN4( c
[1], ncv1
->color
);
1623 COPY_CHAN4( c
[2], ncv2
->color
);
1624 ACC_3V( ncv0
->color
, ncv0
->specular
);
1625 ACC_3V( ncv1
->color
, ncv1
->specular
);
1626 ACC_3V( ncv2
->color
, ncv2
->specular
);
1627 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
1628 COPY_CHAN4( ncv0
->color
, c
[0] );
1629 COPY_CHAN4( ncv1
->color
, c
[1] );
1630 COPY_CHAN4( ncv2
->color
, c
[2] );
1637 /* record the current triangle function name */
1638 static const char *triFuncName
= NULL
;
1640 #define USE(triFunc) \
1642 triFuncName = #triFunc; \
1643 /*printf("%s\n", triFuncName);*/ \
1644 swrast->Triangle = triFunc; \
1649 #define USE(triFunc) swrast->Triangle = triFunc;
1657 * Determine which triangle rendering function to use given the current
1658 * rendering context.
1660 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1661 * remove tests to this code.
1664 _swrast_choose_triangle( GLcontext
*ctx
)
1666 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1667 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
1669 if (ctx
->Polygon
.CullFlag
&&
1670 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1671 USE(nodraw_triangle
);
1675 if (ctx
->RenderMode
==GL_RENDER
) {
1677 if (ctx
->Polygon
.SmoothFlag
) {
1678 _mesa_set_aa_triangle_function(ctx
);
1679 ASSERT(swrast
->Triangle
);
1683 if (ctx
->Depth
.OcclusionTest
&&
1685 ctx
->Depth
.Mask
== GL_FALSE
&&
1686 ctx
->Depth
.Func
== GL_LESS
&&
1687 !ctx
->Stencil
.Enabled
) {
1689 ctx
->Color
.ColorMask
[0] == 0 &&
1690 ctx
->Color
.ColorMask
[1] == 0 &&
1691 ctx
->Color
.ColorMask
[2] == 0 &&
1692 ctx
->Color
.ColorMask
[3] == 0)
1694 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1695 USE(occlusion_zless_triangle
);
1700 if (ctx
->Texture
._ReallyEnabled
) {
1701 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1702 const struct gl_texture_object
*texObj2D
;
1703 const struct gl_texture_image
*texImg
;
1704 GLenum minFilter
, magFilter
, envMode
;
1706 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1707 texImg
= texObj2D
? texObj2D
->Image
[texObj2D
->BaseLevel
] : NULL
;
1708 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1709 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1710 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1711 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1713 /* First see if we can used an optimized 2-D texture function */
1714 if (ctx
->Texture
._ReallyEnabled
==TEXTURE0_2D
1715 && texObj2D
->WrapS
==GL_REPEAT
1716 && texObj2D
->WrapT
==GL_REPEAT
1717 && texImg
->Border
==0
1718 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1719 && minFilter
== magFilter
1720 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1721 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1722 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1723 if (minFilter
== GL_NEAREST
1724 && format
== MESA_FORMAT_RGB
1725 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1726 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1727 && ctx
->Depth
.Func
== GL_LESS
1728 && ctx
->Depth
.Mask
== GL_TRUE
)
1729 || swrast
->_RasterMask
== TEXTURE_BIT
)
1730 && ctx
->Polygon
.StippleFlag
== GL_FALSE
) {
1731 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1732 USE(simple_z_textured_triangle
);
1735 USE(simple_textured_triangle
);
1739 #if CHAN_TYPE != GL_FLOAT
1740 if (ctx
->Texture
.Unit
[0].EnvMode
!= GL_ADD
) {
1741 USE(affine_textured_triangle
);
1746 USE(general_textured_triangle
);
1751 if (ctx
->Texture
.Unit
[0].EnvMode
==GL_ADD
) {
1752 USE(general_textured_triangle
);
1755 USE(persp_textured_triangle
);
1760 /* More complicated textures (mipmap, multi-tex, sep specular) */
1761 GLboolean needLambda
;
1762 /* if mag filter != min filter we need to compute lambda */
1763 const struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[0]._Current
;
1764 if (obj
&& obj
->MinFilter
!= obj
->MagFilter
)
1765 needLambda
= GL_TRUE
;
1767 needLambda
= GL_FALSE
;
1768 if (ctx
->Texture
._ReallyEnabled
> TEXTURE0_ANY
) {
1769 USE(lambda_multitextured_triangle
);
1771 else if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) {
1772 /* separate specular color interpolation */
1774 USE(lambda_textured_spec_triangle
);
1777 USE(general_textured_spec_triangle
);
1782 USE(lambda_textured_triangle
);
1785 USE(general_textured_triangle
);
1791 ASSERT(!ctx
->Texture
._ReallyEnabled
);
1792 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1793 /* smooth shaded, no texturing, stippled or some raster ops */
1795 USE(smooth_rgba_triangle
);
1798 USE(smooth_ci_triangle
);
1802 /* flat shaded, no texturing, stippled or some raster ops */
1804 USE(flat_rgba_triangle
);
1807 USE(flat_ci_triangle
);
1812 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1813 USE(_mesa_feedback_triangle
);
1816 /* GL_SELECT mode */
1817 USE(_mesa_select_triangle
);