1 /* $Id: s_triangle.c,v 1.48 2002/01/21 18:12:34 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 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"
53 GLboolean
_mesa_cull_triangle( GLcontext
*ctx
,
58 GLfloat ex
= v1
->win
[0] - v0
->win
[0];
59 GLfloat ey
= v1
->win
[1] - v0
->win
[1];
60 GLfloat fx
= v2
->win
[0] - v0
->win
[0];
61 GLfloat fy
= v2
->win
[1] - v0
->win
[1];
62 GLfloat c
= ex
*fy
-ey
*fx
;
64 if (c
* SWRAST_CONTEXT(ctx
)->_backface_sign
> 0)
73 * Render a flat-shaded color index triangle.
75 static void flat_ci_triangle( GLcontext
*ctx
,
83 #define RENDER_SPAN( span ) \
84 _mesa_write_monoindex_span(ctx, &span, v2->index, GL_POLYGON );
86 #include "s_tritemp.h"
92 * Render a smooth-shaded color index triangle.
94 static void smooth_ci_triangle( GLcontext
*ctx
,
101 #define INTERP_INDEX 1
103 #define RENDER_SPAN( span ) \
105 SW_SPAN_SET_FLAG(span.filledColor); \
106 for (i = 0; i < span.end; i++) { \
107 span.color.index[i] = FixedToInt(span.index); \
108 span.index += span.indexStep; \
110 _mesa_write_index_span(ctx, &span, NULL, GL_POLYGON);
112 #include "s_tritemp.h"
118 * Render a flat-shaded RGBA triangle.
120 static void flat_rgba_triangle( GLcontext
*ctx
,
127 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
129 #define RENDER_SPAN( span ) \
130 _mesa_write_monocolor_span(ctx, &span, v2->color, GL_POLYGON );
132 #include "s_tritemp.h"
134 ASSERT(!ctx
->Texture
._ReallyEnabled
); /* texturing must be off */
135 ASSERT(ctx
->Light
.ShadeModel
==GL_FLAT
);
141 * Render a smooth-shaded RGBA triangle.
143 static void smooth_rgba_triangle( GLcontext
*ctx
,
151 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
153 #define INTERP_ALPHA 1
155 #define RENDER_SPAN( span ) \
157 SW_SPAN_SET_FLAG(span.filledColor); \
158 SW_SPAN_SET_FLAG(span.filledAlpha); \
159 for (i = 0; i < span.end; i++) { \
160 span.color.rgba[i][RCOMP] = FixedToChan(span.red); \
161 span.color.rgba[i][GCOMP] = FixedToChan(span.green); \
162 span.color.rgba[i][BCOMP] = FixedToChan(span.blue); \
163 span.color.rgba[i][ACOMP] = FixedToChan(span.alpha); \
164 span.red += span.redStep; \
165 span.green += span.greenStep; \
166 span.blue += span.blueStep; \
167 span.alpha += span.alphaStep; \
169 _mesa_write_rgba_span(ctx, &span, NULL, GL_POLYGON);
171 #include "s_tritemp.h"
173 ASSERT(!ctx
->Texture
._ReallyEnabled
); /* texturing must be off */
174 ASSERT(ctx
->Light
.ShadeModel
==GL_SMOOTH
);
179 * Render an RGB, GL_DECAL, textured triangle.
180 * Interpolate S,T only w/out mipmapping or perspective correction.
184 static void simple_textured_triangle( GLcontext
*ctx
,
189 #define INTERP_INT_TEX 1
190 #define S_SCALE twidth
191 #define T_SCALE theight
194 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
195 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
196 const GLint b = obj->BaseLevel; \
197 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
198 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
199 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
200 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
201 const GLint smask = obj->Image[b]->Width - 1; \
202 const GLint tmask = obj->Image[b]->Height - 1; \
204 /* this shouldn't happen */ \
208 #define RENDER_SPAN( span ) \
210 SW_SPAN_SET_FLAG(span.filledColor); \
211 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
212 span.intTex[1] -= FIXED_HALF; \
213 for (i = 0; i < span.end; i++) { \
214 GLint s = FixedToInt(span.intTex[0]) & smask; \
215 GLint t = FixedToInt(span.intTex[1]) & tmask; \
216 GLint pos = (t << twidth_log2) + s; \
217 pos = pos + pos + pos; /* multiply by 3 */ \
218 span.color.rgb[i][RCOMP] = texture[pos]; \
219 span.color.rgb[i][GCOMP] = texture[pos+1]; \
220 span.color.rgb[i][BCOMP] = texture[pos+2]; \
221 span.intTex[0] += span.intTexStep[0]; \
222 span.intTex[1] += span.intTexStep[1]; \
224 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
225 (CONST GLchan (*)[3]) span.color.rgb, \
228 #include "s_tritemp.h"
233 * Render an RGB, GL_DECAL, textured triangle.
234 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
235 * perspective correction.
239 static void simple_z_textured_triangle( GLcontext
*ctx
,
245 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
246 #define INTERP_INT_TEX 1
247 #define S_SCALE twidth
248 #define T_SCALE theight
251 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
252 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
253 const GLint b = obj->BaseLevel; \
254 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
255 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
256 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
257 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
258 const GLint smask = obj->Image[b]->Width - 1; \
259 const GLint tmask = obj->Image[b]->Height - 1; \
261 /* this shouldn't happen */ \
265 #define RENDER_SPAN( span ) \
267 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
268 span.intTex[1] -= FIXED_HALF; \
269 SW_SPAN_SET_FLAG(span.filledColor); \
270 for (i = 0; i < span.end; i++) { \
271 const GLdepth z = FixedToDepth(span.z); \
273 GLint s = FixedToInt(span.intTex[0]) & smask; \
274 GLint t = FixedToInt(span.intTex[1]) & tmask; \
275 GLint pos = (t << twidth_log2) + s; \
276 pos = pos + pos + pos; /* multiply by 3 */ \
277 span.color.rgb[i][RCOMP] = texture[pos]; \
278 span.color.rgb[i][GCOMP] = texture[pos+1]; \
279 span.color.rgb[i][BCOMP] = texture[pos+2]; \
286 span.intTex[0] += span.intTexStep[0]; \
287 span.intTex[1] += span.intTexStep[1]; \
288 span.z += span.zStep; \
290 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
291 (CONST GLchan (*)[3]) span.color.rgb, \
294 #include "s_tritemp.h"
298 #if CHAN_TYPE != GL_FLOAT
307 const GLchan
*texture
;
308 GLfixed er
, eg
, eb
, ea
;
309 GLint tbytesline
, tsize
;
313 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
314 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
318 affine_span(GLcontext
*ctx
, struct sw_span
*span
,
319 struct affine_info
*info
)
321 GLchan sample
[4]; /* the filtered texture sample */
323 /* Instead of defining a function for each mode, a test is done
324 * between the outer and inner loops. This is to reduce code size
325 * and complexity. Observe that an optimizing compiler kills
326 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
329 #define NEAREST_RGB \
330 sample[RCOMP] = tex00[RCOMP]; \
331 sample[GCOMP] = tex00[GCOMP]; \
332 sample[BCOMP] = tex00[BCOMP]; \
333 sample[ACOMP] = CHAN_MAX
336 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
337 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
338 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
339 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
340 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
341 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
342 sample[ACOMP] = CHAN_MAX
344 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
346 #define LINEAR_RGBA \
347 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
348 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT;\
349 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
350 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT;\
351 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
352 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT;\
353 sample[ACOMP] = (ti * (si * tex00[3] + sf * tex01[3]) + \
354 tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT
357 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
358 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
359 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
360 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
363 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
364 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
365 >> (FIXED_SHIFT + 8); \
366 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
367 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
368 >> (FIXED_SHIFT + 8); \
369 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
370 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
371 >> (FIXED_SHIFT + 8); \
372 dest[ACOMP] = FixedToInt(span->alpha)
375 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
376 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
377 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
378 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
379 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
380 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
381 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
383 #define REPLACE COPY_CHAN4(dest, sample)
387 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
388 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
389 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
390 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
391 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
392 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
393 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
398 #define NEAREST_RGB_REPLACE \
400 dest[0] = sample[0]; \
401 dest[1] = sample[1]; \
402 dest[2] = sample[2]; \
403 dest[3] = FixedToInt(span->alpha);
405 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
407 #define SPAN_NEAREST(DO_TEX,COMP) \
408 for (i = 0; i < span->end; i++) { \
409 /* Isn't it necessary to use FixedFloor below?? */ \
410 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
411 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
412 GLint pos = (t << info->twidth_log2) + s; \
413 const GLchan *tex00 = info->texture + COMP * pos; \
415 span->red += span->redStep; \
416 span->green += span->greenStep; \
417 span->blue += span->blueStep; \
418 span->alpha += span->alphaStep; \
419 span->intTex[0] += span->intTexStep[0]; \
420 span->intTex[1] += span->intTexStep[1]; \
424 #define SPAN_LINEAR(DO_TEX,COMP) \
425 for (i = 0; i < span->end; i++) { \
426 /* Isn't it necessary to use FixedFloor below?? */ \
427 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
428 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
429 GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
430 GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
431 GLfixed si = FIXED_FRAC_MASK - sf; \
432 GLfixed ti = FIXED_FRAC_MASK - tf; \
433 GLint pos = (t << info->twidth_log2) + s; \
434 const GLchan *tex00 = info->texture + COMP * pos; \
435 const GLchan *tex10 = tex00 + info->tbytesline; \
436 const GLchan *tex01 = tex00 + COMP; \
437 const GLchan *tex11 = tex10 + COMP; \
440 if (t == info->tmask) { \
441 tex10 -= info->tsize; \
442 tex11 -= info->tsize; \
444 if (s == info->smask) { \
445 tex01 -= info->tbytesline; \
446 tex11 -= info->tbytesline; \
449 span->red += span->redStep; \
450 span->green += span->greenStep; \
451 span->blue += span->blueStep; \
452 span->alpha += span->alphaStep; \
453 span->intTex[0] += span->intTexStep[0]; \
454 span->intTex[1] += span->intTexStep[1]; \
460 GLchan
*dest
= span
->color
.rgba
[0];
462 SW_SPAN_SET_FLAG(span
->filledColor
);
464 span
->intTex
[0] -= FIXED_HALF
;
465 span
->intTex
[1] -= FIXED_HALF
;
466 switch (info
->filter
) {
468 switch (info
->format
) {
470 switch (info
->envmode
) {
472 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
476 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
479 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
482 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
489 switch(info
->envmode
) {
491 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
494 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
497 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
500 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
503 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
513 span
->intTex
[0] -= FIXED_HALF
;
514 span
->intTex
[1] -= FIXED_HALF
;
515 switch (info
->format
) {
517 switch (info
->envmode
) {
519 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
523 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
526 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
529 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
536 switch (info
->envmode
) {
538 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
541 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
544 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
547 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
550 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
558 _mesa_write_rgba_span(ctx
, span
, NULL
, GL_POLYGON
);
567 * Render an RGB/RGBA textured triangle without perspective correction.
569 static void affine_textured_triangle( GLcontext
*ctx
,
576 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
578 #define INTERP_ALPHA 1
579 #define INTERP_INT_TEX 1
580 #define S_SCALE twidth
581 #define T_SCALE theight
584 struct affine_info info; \
585 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
586 struct gl_texture_object *obj = unit->Current2D; \
587 const GLint b = obj->BaseLevel; \
588 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
589 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
590 info.texture = (const GLchan *) obj->Image[b]->Data; \
591 info.twidth_log2 = obj->Image[b]->WidthLog2; \
592 info.smask = obj->Image[b]->Width - 1; \
593 info.tmask = obj->Image[b]->Height - 1; \
594 info.format = obj->Image[b]->Format; \
595 info.filter = obj->MinFilter; \
596 info.envmode = unit->EnvMode; \
598 if (info.envmode == GL_BLEND) { \
599 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
600 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
601 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
602 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
603 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
605 if (!info.texture) { \
606 /* this shouldn't happen */ \
610 switch (info.format) { \
614 info.tbytesline = obj->Image[b]->Width; \
616 case GL_LUMINANCE_ALPHA: \
617 info.tbytesline = obj->Image[b]->Width * 2; \
620 info.tbytesline = obj->Image[b]->Width * 3; \
623 info.tbytesline = obj->Image[b]->Width * 4; \
626 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
629 info.tsize = obj->Image[b]->Height * info.tbytesline;
631 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
633 #include "s_tritemp.h"
646 const GLchan
*texture
;
647 GLfixed er
, eg
, eb
, ea
; /* texture env color */
648 GLint tbytesline
, tsize
;
653 fast_persp_span(GLcontext
*ctx
, struct sw_span
*span
,
654 struct persp_info
*info
)
656 GLchan sample
[4]; /* the filtered texture sample */
658 /* Instead of defining a function for each mode, a test is done
659 * between the outer and inner loops. This is to reduce code size
660 * and complexity. Observe that an optimizing compiler kills
661 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
663 #define SPAN_NEAREST(DO_TEX,COMP) \
664 for (i = 0; i < span->end; i++) { \
665 GLdouble invQ = tex_coord[2] ? \
666 (1.0 / tex_coord[2]) : 1.0; \
667 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
668 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
669 GLint s = IFLOOR(s_tmp) & info->smask; \
670 GLint t = IFLOOR(t_tmp) & info->tmask; \
671 GLint pos = (t << info->twidth_log2) + s; \
672 const GLchan *tex00 = info->texture + COMP * pos; \
674 span->red += span->redStep; \
675 span->green += span->greenStep; \
676 span->blue += span->blueStep; \
677 span->alpha += span->alphaStep; \
678 tex_coord[0] += tex_step[0]; \
679 tex_coord[1] += tex_step[1]; \
680 tex_coord[2] += tex_step[2]; \
684 #define SPAN_LINEAR(DO_TEX,COMP) \
685 for (i = 0; i < span->end; i++) { \
686 GLdouble invQ = tex_coord[2] ? \
687 (1.0 / tex_coord[2]) : 1.0; \
688 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
689 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
690 GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
691 GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
692 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
693 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
694 GLfixed sf = s_fix & FIXED_FRAC_MASK; \
695 GLfixed tf = t_fix & FIXED_FRAC_MASK; \
696 GLfixed si = FIXED_FRAC_MASK - sf; \
697 GLfixed ti = FIXED_FRAC_MASK - tf; \
698 GLint pos = (t << info->twidth_log2) + s; \
699 const GLchan *tex00 = info->texture + COMP * pos; \
700 const GLchan *tex10 = tex00 + info->tbytesline; \
701 const GLchan *tex01 = tex00 + COMP; \
702 const GLchan *tex11 = tex10 + COMP; \
705 if (t == info->tmask) { \
706 tex10 -= info->tsize; \
707 tex11 -= info->tsize; \
709 if (s == info->smask) { \
710 tex01 -= info->tbytesline; \
711 tex11 -= info->tbytesline; \
714 span->red += span->redStep; \
715 span->green += span->greenStep; \
716 span->blue += span->blueStep; \
717 span->alpha += span->alphaStep; \
718 tex_coord[0] += tex_step[0]; \
719 tex_coord[1] += tex_step[1]; \
720 tex_coord[2] += tex_step[2]; \
725 GLfloat tex_coord
[3], tex_step
[3];
726 GLchan
*dest
= span
->color
.rgba
[0];
728 SW_SPAN_SET_FLAG(span
->filledColor
);
730 tex_coord
[0] = span
->tex
[0][0] * (info
->smask
+ 1),
731 tex_step
[0] = span
->texStep
[0][0] * (info
->smask
+ 1);
732 tex_coord
[1] = span
->tex
[0][1] * (info
->tmask
+ 1),
733 tex_step
[1] = span
->texStep
[0][1] * (info
->tmask
+ 1);
734 /* span->tex[0][2] only if 3D-texturing, here only 2D */
735 tex_coord
[2] = span
->tex
[0][3],
736 tex_step
[2] = span
->texStep
[0][3];
738 switch (info
->filter
) {
740 switch (info
->format
) {
742 switch (info
->envmode
) {
744 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
748 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
751 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
754 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
761 switch(info
->envmode
) {
763 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
766 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
769 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
772 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
775 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
785 switch (info
->format
) {
787 switch (info
->envmode
) {
789 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
793 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
796 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
799 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
806 switch (info
->envmode
) {
808 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
811 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
814 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
817 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
820 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
830 _mesa_write_rgba_span(ctx
, span
, NULL
, GL_POLYGON
);
839 * Render an perspective corrected RGB/RGBA textured triangle.
840 * The Q (aka V in Mesa) coordinate must be zero such that the divide
841 * by interpolated Q/W comes out right.
844 static void persp_textured_triangle( GLcontext
*ctx
,
851 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
853 #define INTERP_ALPHA 1
857 struct persp_info info; \
858 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
859 const struct gl_texture_object *obj = unit->Current2D; \
860 const GLint b = obj->BaseLevel; \
861 info.texture = (const GLchan *) obj->Image[b]->Data; \
862 info.twidth_log2 = obj->Image[b]->WidthLog2; \
863 info.smask = obj->Image[b]->Width - 1; \
864 info.tmask = obj->Image[b]->Height - 1; \
865 info.format = obj->Image[b]->Format; \
866 info.filter = obj->MinFilter; \
867 info.envmode = unit->EnvMode; \
869 if (info.envmode == GL_BLEND) { \
870 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
871 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
872 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
873 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
874 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
876 if (!info.texture) { \
877 /* this shouldn't happen */ \
881 switch (info.format) { \
885 info.tbytesline = obj->Image[b]->Width; \
887 case GL_LUMINANCE_ALPHA: \
888 info.tbytesline = obj->Image[b]->Width * 2; \
891 info.tbytesline = obj->Image[b]->Width * 3; \
894 info.tbytesline = obj->Image[b]->Width * 4; \
897 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
900 info.tsize = obj->Image[b]->Height * info.tbytesline;
902 #define RENDER_SPAN( span ) fast_persp_span(ctx, &span, &info);
904 #include "s_tritemp.h"
909 #endif /* CHAN_BITS != GL_FLOAT */
915 * Render a smooth-shaded, textured, RGBA triangle.
916 * Interpolate S,T,R with perspective correction, w/out mipmapping.
918 static void general_textured_triangle( GLcontext
*ctx
,
925 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
927 #define INTERP_ALPHA 1
931 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
932 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
933 span.texWidth[0] = (GLfloat) texImage->Width; \
934 span.texHeight[0] = (GLfloat) texImage->Height; \
935 (void) fixedToDepthShift;
937 #define RENDER_SPAN( span ) \
939 SW_SPAN_SET_FLAG(span.filledColor); \
940 SW_SPAN_SET_FLAG(span.filledAlpha); \
941 SW_SPAN_SET_FLAG(span.filledTex[0]); \
942 /* NOTE: we could just call rasterize_span() here instead */ \
943 for (i = 0; i < span.end; i++) { \
944 GLdouble invQ = span.tex[0][3] ? (1.0 / span.tex[0][3]) : 1.0; \
945 span.depth[i] = FixedToDepth(span.z); \
946 span.z += span.zStep; \
947 span.color.rgba[i][RCOMP] = FixedToChan(span.red); \
948 span.color.rgba[i][GCOMP] = FixedToChan(span.green); \
949 span.color.rgba[i][BCOMP] = FixedToChan(span.blue); \
950 span.color.rgba[i][ACOMP] = FixedToChan(span.alpha); \
951 span.red += span.redStep; \
952 span.green += span.greenStep; \
953 span.blue += span.blueStep; \
954 span.alpha += span.alphaStep; \
955 span.texcoords[0][i][0] = (GLfloat) (span.tex[0][0] * invQ); \
956 span.texcoords[0][i][1] = (GLfloat) (span.tex[0][1] * invQ); \
957 span.texcoords[0][i][2] = (GLfloat) (span.tex[0][2] * invQ); \
958 span.tex[0][0] += span.texStep[0][0]; \
959 span.tex[0][1] += span.texStep[0][1]; \
960 span.tex[0][2] += span.texStep[0][2]; \
961 span.tex[0][3] += span.texStep[0][3]; \
963 _mesa_write_texture_span( ctx, &span, NULL, GL_POLYGON );
965 #include "s_tritemp.h"
970 * Render a smooth-shaded, textured, RGBA triangle with separate specular
971 * color interpolation.
972 * Interpolate texcoords with perspective correction, w/out mipmapping.
974 static void general_textured_spec_triangle( GLcontext
*ctx
,
981 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
983 #define INTERP_SPEC 1
984 #define INTERP_ALPHA 1
988 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
989 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
990 span.texWidth[0] = (GLfloat) texImage->Width; \
991 span.texHeight[0] = (GLfloat) texImage->Height; \
992 (void) fixedToDepthShift;
994 #define RENDER_SPAN( span ) _mesa_rasterize_span(ctx, &span);
996 #include "s_tritemp.h"
1001 * Render a smooth-shaded, textured, RGBA triangle.
1002 * Interpolate S,T,R with perspective correction and compute lambda for
1003 * each fragment. Lambda is used to determine whether to use the
1004 * minification or magnification filter. If minification and using
1005 * mipmaps, lambda is also used to select the texture level of detail.
1007 static void lambda_textured_triangle( GLcontext
*ctx
,
1010 const SWvertex
*v2
)
1013 #define INTERP_FOG 1
1014 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1015 #define INTERP_RGB 1
1016 #define INTERP_ALPHA 1
1017 #define INTERP_TEX 1
1018 #define INTERP_LAMBDA 1
1020 #define SETUP_CODE \
1021 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1022 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1023 span.texWidth[0] = (GLfloat) texImage->Width; \
1024 span.texHeight[0] = (GLfloat) texImage->Height; \
1025 (void) fixedToDepthShift;
1027 #define RENDER_SPAN( span ) _mesa_rasterize_span(ctx, &span);
1029 #include "s_tritemp.h"
1034 * Render a smooth-shaded, textured, RGBA triangle with separate specular
1036 * Interpolate S,T,R with perspective correction and compute lambda for
1037 * each fragment. Lambda is used to determine whether to use the
1038 * minification or magnification filter. If minification and using
1039 * mipmaps, lambda is also used to select the texture level of detail.
1041 static void lambda_textured_spec_triangle( GLcontext
*ctx
,
1044 const SWvertex
*v2
)
1047 #define INTERP_FOG 1
1048 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1049 #define INTERP_RGB 1
1050 #define INTERP_SPEC 1
1051 #define INTERP_ALPHA 1
1052 #define INTERP_TEX 1
1053 #define INTERP_LAMBDA 1
1055 #define SETUP_CODE \
1056 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1057 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1058 span.texWidth[0] = (GLfloat) texImage->Width; \
1059 span.texHeight[0] = (GLfloat) texImage->Height; \
1060 (void) fixedToDepthShift;
1062 #define RENDER_SPAN( span ) _mesa_rasterize_span(ctx, &span);
1064 #include "s_tritemp.h"
1069 * This is the big one!
1070 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates
1071 * with lambda (LOD).
1075 lambda_multitextured_triangle( GLcontext
*ctx
,
1078 const SWvertex
*v2
)
1082 #define INTERP_FOG 1
1083 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1084 #define INTERP_RGB 1
1085 #define INTERP_ALPHA 1
1086 #define INTERP_SPEC 1
1087 #define INTERP_MULTITEX 1
1088 #define INTERP_LAMBDA 1
1090 #define SETUP_CODE \
1092 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) { \
1093 if (ctx->Texture.Unit[u]._ReallyEnabled) { \
1094 const struct gl_texture_object *texObj; \
1095 const struct gl_texture_image *texImage; \
1096 texObj = ctx->Texture.Unit[u]._Current; \
1097 texImage = texObj->Image[texObj->BaseLevel]; \
1098 span.texWidth[u] = (GLfloat) texImage->Width; \
1099 span.texHeight[u] = (GLfloat) texImage->Height; \
1102 (void) fixedToDepthShift;
1104 #define RENDER_SPAN( span ) _mesa_rasterize_span(ctx, &span);
1106 #include "s_tritemp.h"
1111 static void occlusion_zless_triangle( GLcontext
*ctx
,
1114 const SWvertex
*v2
)
1116 if (ctx
->OcclusionResult
) {
1120 #define DO_OCCLUSION_TEST
1122 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1124 #define RENDER_SPAN( span ) \
1126 for (i = 0; i < span.end; i++) { \
1127 GLdepth z = FixedToDepth(span.z); \
1128 if (z < zRow[i]) { \
1129 ctx->OcclusionResult = GL_TRUE; \
1132 span.z += span.zStep; \
1135 #include "s_tritemp.h"
1138 static void nodraw_triangle( GLcontext
*ctx
,
1141 const SWvertex
*v2
)
1143 (void) (ctx
&& v0
&& v1
&& v2
);
1148 * This is used when separate specular color is enabled, but not
1149 * texturing. We add the specular color to the primary color,
1150 * draw the triangle, then restore the original primary color.
1151 * Inefficient, but seldom needed.
1153 void _swrast_add_spec_terms_triangle( GLcontext
*ctx
,
1156 const SWvertex
*v2
)
1158 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
1159 SWvertex
*ncv1
= (SWvertex
*)v1
;
1160 SWvertex
*ncv2
= (SWvertex
*)v2
;
1161 #if CHAN_TYPE == GL_FLOAT
1162 GLfloat rSum
, gSum
, bSum
;
1164 GLint rSum
, gSum
, bSum
;
1167 /* save original colors */
1168 COPY_CHAN4( c
[0], ncv0
->color
);
1169 COPY_CHAN4( c
[1], ncv1
->color
);
1170 COPY_CHAN4( c
[2], ncv2
->color
);
1172 rSum
= ncv0
->color
[0] + ncv0
->specular
[0];
1173 gSum
= ncv0
->color
[1] + ncv0
->specular
[1];
1174 bSum
= ncv0
->color
[2] + ncv0
->specular
[2];
1175 ncv0
->color
[0] = MIN2(rSum
, CHAN_MAX
);
1176 ncv0
->color
[1] = MIN2(gSum
, CHAN_MAX
);
1177 ncv0
->color
[2] = MIN2(bSum
, CHAN_MAX
);
1179 rSum
= ncv1
->color
[0] + ncv1
->specular
[0];
1180 gSum
= ncv1
->color
[1] + ncv1
->specular
[1];
1181 bSum
= ncv1
->color
[2] + ncv1
->specular
[2];
1182 ncv1
->color
[0] = MIN2(rSum
, CHAN_MAX
);
1183 ncv1
->color
[1] = MIN2(gSum
, CHAN_MAX
);
1184 ncv1
->color
[2] = MIN2(bSum
, CHAN_MAX
);
1186 rSum
= ncv2
->color
[0] + ncv2
->specular
[0];
1187 gSum
= ncv2
->color
[1] + ncv2
->specular
[1];
1188 bSum
= ncv2
->color
[2] + ncv2
->specular
[2];
1189 ncv2
->color
[0] = MIN2(rSum
, CHAN_MAX
);
1190 ncv2
->color
[1] = MIN2(gSum
, CHAN_MAX
);
1191 ncv2
->color
[2] = MIN2(bSum
, CHAN_MAX
);
1193 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
1194 /* restore original colors */
1195 COPY_CHAN4( ncv0
->color
, c
[0] );
1196 COPY_CHAN4( ncv1
->color
, c
[1] );
1197 COPY_CHAN4( ncv2
->color
, c
[2] );
1204 /* record the current triangle function name */
1205 const char *_mesa_triFuncName
= NULL
;
1207 #define USE(triFunc) \
1209 _mesa_triFuncName = #triFunc; \
1210 /*printf("%s\n", triFuncName);*/ \
1211 swrast->Triangle = triFunc; \
1216 #define USE(triFunc) swrast->Triangle = triFunc;
1224 * Determine which triangle rendering function to use given the current
1225 * rendering context.
1227 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1228 * remove tests to this code.
1231 _swrast_choose_triangle( GLcontext
*ctx
)
1233 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1234 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
1236 if (ctx
->Polygon
.CullFlag
&&
1237 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1238 USE(nodraw_triangle
);
1242 if (ctx
->RenderMode
==GL_RENDER
) {
1244 if (ctx
->Polygon
.SmoothFlag
) {
1245 _mesa_set_aa_triangle_function(ctx
);
1246 ASSERT(swrast
->Triangle
);
1250 if (ctx
->Depth
.OcclusionTest
&&
1252 ctx
->Depth
.Mask
== GL_FALSE
&&
1253 ctx
->Depth
.Func
== GL_LESS
&&
1254 !ctx
->Stencil
.Enabled
) {
1256 ctx
->Color
.ColorMask
[0] == 0 &&
1257 ctx
->Color
.ColorMask
[1] == 0 &&
1258 ctx
->Color
.ColorMask
[2] == 0 &&
1259 ctx
->Color
.ColorMask
[3] == 0)
1261 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1262 USE(occlusion_zless_triangle
);
1267 if (ctx
->Texture
._ReallyEnabled
) {
1268 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1269 const struct gl_texture_object
*texObj2D
;
1270 const struct gl_texture_image
*texImg
;
1271 GLenum minFilter
, magFilter
, envMode
;
1273 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1274 texImg
= texObj2D
? texObj2D
->Image
[texObj2D
->BaseLevel
] : NULL
;
1275 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1276 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1277 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1278 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1280 /* First see if we can used an optimized 2-D texture function */
1281 if (ctx
->Texture
._ReallyEnabled
==TEXTURE0_2D
1282 && texObj2D
->WrapS
==GL_REPEAT
1283 && texObj2D
->WrapT
==GL_REPEAT
1284 && texImg
->Border
==0
1285 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1286 && minFilter
== magFilter
1287 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1288 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1289 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1290 if (minFilter
== GL_NEAREST
1291 && format
== MESA_FORMAT_RGB
1292 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1293 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1294 && ctx
->Depth
.Func
== GL_LESS
1295 && ctx
->Depth
.Mask
== GL_TRUE
)
1296 || swrast
->_RasterMask
== TEXTURE_BIT
)
1297 && ctx
->Polygon
.StippleFlag
== GL_FALSE
) {
1298 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1299 USE(simple_z_textured_triangle
);
1302 USE(simple_textured_triangle
);
1306 #if CHAN_TYPE == GL_FLOAT
1307 USE(general_textured_triangle
);
1309 USE(affine_textured_triangle
);
1314 #if CHAN_TYPE == GL_FLOAT
1315 USE(general_textured_triangle
);
1317 USE(persp_textured_triangle
);
1322 /* More complicated textures (mipmap, multi-tex, sep specular) */
1323 GLboolean needLambda
;
1324 /* if mag filter != min filter we need to compute lambda */
1325 const struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[0]._Current
;
1326 if (obj
&& obj
->MinFilter
!= obj
->MagFilter
)
1327 needLambda
= GL_TRUE
;
1329 needLambda
= GL_FALSE
;
1330 if (ctx
->Texture
._ReallyEnabled
> TEXTURE0_ANY
) {
1331 USE(lambda_multitextured_triangle
);
1333 else if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) {
1334 /* separate specular color interpolation */
1336 USE(lambda_textured_spec_triangle
);
1339 USE(general_textured_spec_triangle
);
1344 USE(lambda_textured_triangle
);
1347 USE(general_textured_triangle
);
1353 ASSERT(!ctx
->Texture
._ReallyEnabled
);
1354 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1355 /* smooth shaded, no texturing, stippled or some raster ops */
1357 USE(smooth_rgba_triangle
);
1360 USE(smooth_ci_triangle
);
1364 /* flat shaded, no texturing, stippled or some raster ops */
1366 USE(flat_rgba_triangle
);
1369 USE(flat_ci_triangle
);
1374 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1375 USE(_mesa_feedback_triangle
);
1378 /* GL_SELECT mode */
1379 USE(_mesa_select_triangle
);