1 /* $Id: s_triangle.c,v 1.57 2002/04/12 15:39:59 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
,
84 span.interpMask |= SPAN_INDEX; \
85 span.index = IntToFixed(v2->index); \
88 #define RENDER_SPAN( span ) _mesa_write_index_span(ctx, &span);
90 #include "s_tritemp.h"
96 * Render a smooth-shaded color index triangle.
98 static void smooth_ci_triangle( GLcontext
*ctx
,
105 #define INTERP_INDEX 1
107 #define RENDER_SPAN( span ) _mesa_write_index_span(ctx, &span);
109 #include "s_tritemp.h"
115 * Render a flat-shaded RGBA triangle.
117 static void flat_rgba_triangle( GLcontext
*ctx
,
124 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
127 ASSERT(!ctx->Texture._ReallyEnabled); \
128 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
129 span.interpMask |= SPAN_RGBA; \
130 span.red = ChanToFixed(v2->color[0]); \
131 span.green = ChanToFixed(v2->color[1]); \
132 span.blue = ChanToFixed(v2->color[2]); \
133 span.alpha = ChanToFixed(v2->color[3]); \
135 span.greenStep = 0; \
139 #define RENDER_SPAN( span ) _mesa_write_rgba_span(ctx, &span);
141 #include "s_tritemp.h"
147 * Render a smooth-shaded RGBA triangle.
149 static void smooth_rgba_triangle( GLcontext
*ctx
,
157 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
159 #define INTERP_ALPHA 1
163 /* texturing must be off */ \
164 ASSERT(!ctx->Texture._ReallyEnabled); \
165 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
168 #define RENDER_SPAN( span ) _mesa_write_rgba_span(ctx, &span);
170 #include "s_tritemp.h"
176 * Render an RGB, GL_DECAL, textured triangle.
177 * Interpolate S,T only w/out mipmapping or perspective correction.
181 static void simple_textured_triangle( GLcontext
*ctx
,
186 #define INTERP_INT_TEX 1
187 #define S_SCALE twidth
188 #define T_SCALE theight
191 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
192 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
193 const GLint b = obj->BaseLevel; \
194 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
195 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
196 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
197 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
198 const GLint smask = obj->Image[b]->Width - 1; \
199 const GLint tmask = obj->Image[b]->Height - 1; \
201 /* this shouldn't happen */ \
205 #define RENDER_SPAN( span ) \
207 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
208 span.intTex[1] -= FIXED_HALF; \
209 for (i = 0; i < span.end; i++) { \
210 GLint s = FixedToInt(span.intTex[0]) & smask; \
211 GLint t = FixedToInt(span.intTex[1]) & tmask; \
212 GLint pos = (t << twidth_log2) + s; \
213 pos = pos + pos + pos; /* multiply by 3 */ \
214 span.color.rgb[i][RCOMP] = texture[pos]; \
215 span.color.rgb[i][GCOMP] = texture[pos+1]; \
216 span.color.rgb[i][BCOMP] = texture[pos+2]; \
217 span.intTex[0] += span.intTexStep[0]; \
218 span.intTex[1] += span.intTexStep[1]; \
220 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
221 (CONST GLchan (*)[3]) span.color.rgb, \
224 #include "s_tritemp.h"
229 * Render an RGB, GL_DECAL, textured triangle.
230 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
231 * perspective correction.
235 static void simple_z_textured_triangle( GLcontext
*ctx
,
241 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
242 #define INTERP_INT_TEX 1
243 #define S_SCALE twidth
244 #define T_SCALE theight
247 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
248 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
249 const GLint b = obj->BaseLevel; \
250 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
251 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
252 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
253 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
254 const GLint smask = obj->Image[b]->Width - 1; \
255 const GLint tmask = obj->Image[b]->Height - 1; \
257 /* this shouldn't happen */ \
261 #define RENDER_SPAN( span ) \
263 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
264 span.intTex[1] -= FIXED_HALF; \
265 for (i = 0; i < span.end; i++) { \
266 const GLdepth z = FixedToDepth(span.z); \
268 GLint s = FixedToInt(span.intTex[0]) & smask; \
269 GLint t = FixedToInt(span.intTex[1]) & tmask; \
270 GLint pos = (t << twidth_log2) + s; \
271 pos = pos + pos + pos; /* multiply by 3 */ \
272 span.color.rgb[i][RCOMP] = texture[pos]; \
273 span.color.rgb[i][GCOMP] = texture[pos+1]; \
274 span.color.rgb[i][BCOMP] = texture[pos+2]; \
281 span.intTex[0] += span.intTexStep[0]; \
282 span.intTex[1] += span.intTexStep[1]; \
283 span.z += span.zStep; \
285 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
286 (CONST GLchan (*)[3]) span.color.rgb, \
289 #include "s_tritemp.h"
293 #if CHAN_TYPE != GL_FLOAT
302 const GLchan
*texture
;
303 GLfixed er
, eg
, eb
, ea
;
304 GLint tbytesline
, tsize
;
308 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
309 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
313 affine_span(GLcontext
*ctx
, struct sw_span
*span
,
314 struct affine_info
*info
)
316 GLchan sample
[4]; /* the filtered texture sample */
318 /* Instead of defining a function for each mode, a test is done
319 * between the outer and inner loops. This is to reduce code size
320 * and complexity. Observe that an optimizing compiler kills
321 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
324 #define NEAREST_RGB \
325 sample[RCOMP] = tex00[RCOMP]; \
326 sample[GCOMP] = tex00[GCOMP]; \
327 sample[BCOMP] = tex00[BCOMP]; \
328 sample[ACOMP] = CHAN_MAX
331 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
332 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
333 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
334 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
335 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
336 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
337 sample[ACOMP] = CHAN_MAX
339 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
341 #define LINEAR_RGBA \
342 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
343 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT;\
344 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
345 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT;\
346 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
347 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT;\
348 sample[ACOMP] = (ti * (si * tex00[3] + sf * tex01[3]) + \
349 tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT
352 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
353 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
354 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
355 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
358 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
359 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
360 >> (FIXED_SHIFT + 8); \
361 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
362 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
363 >> (FIXED_SHIFT + 8); \
364 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
365 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
366 >> (FIXED_SHIFT + 8); \
367 dest[ACOMP] = FixedToInt(span->alpha)
370 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
371 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
372 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
373 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
374 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
375 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
376 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
378 #define REPLACE COPY_CHAN4(dest, sample)
382 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
383 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
384 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
385 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
386 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
387 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
388 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
393 #define NEAREST_RGB_REPLACE \
395 dest[0] = sample[0]; \
396 dest[1] = sample[1]; \
397 dest[2] = sample[2]; \
398 dest[3] = FixedToInt(span->alpha);
400 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
402 #define SPAN_NEAREST(DO_TEX,COMP) \
403 for (i = 0; i < span->end; i++) { \
404 /* Isn't it necessary to use FixedFloor below?? */ \
405 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
406 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
407 GLint pos = (t << info->twidth_log2) + s; \
408 const GLchan *tex00 = info->texture + COMP * pos; \
410 span->red += span->redStep; \
411 span->green += span->greenStep; \
412 span->blue += span->blueStep; \
413 span->alpha += span->alphaStep; \
414 span->intTex[0] += span->intTexStep[0]; \
415 span->intTex[1] += span->intTexStep[1]; \
419 #define SPAN_LINEAR(DO_TEX,COMP) \
420 for (i = 0; i < span->end; i++) { \
421 /* Isn't it necessary to use FixedFloor below?? */ \
422 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
423 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
424 GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
425 GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
426 GLfixed si = FIXED_FRAC_MASK - sf; \
427 GLfixed ti = FIXED_FRAC_MASK - tf; \
428 GLint pos = (t << info->twidth_log2) + s; \
429 const GLchan *tex00 = info->texture + COMP * pos; \
430 const GLchan *tex10 = tex00 + info->tbytesline; \
431 const GLchan *tex01 = tex00 + COMP; \
432 const GLchan *tex11 = tex10 + COMP; \
435 if (t == info->tmask) { \
436 tex10 -= info->tsize; \
437 tex11 -= info->tsize; \
439 if (s == info->smask) { \
440 tex01 -= info->tbytesline; \
441 tex11 -= info->tbytesline; \
444 span->red += span->redStep; \
445 span->green += span->greenStep; \
446 span->blue += span->blueStep; \
447 span->alpha += span->alphaStep; \
448 span->intTex[0] += span->intTexStep[0]; \
449 span->intTex[1] += span->intTexStep[1]; \
455 GLchan
*dest
= span
->color
.rgba
[0];
457 span
->intTex
[0] -= FIXED_HALF
;
458 span
->intTex
[1] -= FIXED_HALF
;
459 switch (info
->filter
) {
461 switch (info
->format
) {
463 switch (info
->envmode
) {
465 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
469 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
472 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
475 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
482 switch(info
->envmode
) {
484 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
487 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
490 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
493 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
496 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
506 span
->intTex
[0] -= FIXED_HALF
;
507 span
->intTex
[1] -= FIXED_HALF
;
508 switch (info
->format
) {
510 switch (info
->envmode
) {
512 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
516 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
519 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
522 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
529 switch (info
->envmode
) {
531 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
534 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
537 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
540 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
543 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
551 span
->interpMask
&= ~SPAN_RGBA
;
552 ASSERT(span
->arrayMask
& SPAN_RGBA
);
553 _mesa_write_rgba_span(ctx
, span
);
562 * Render an RGB/RGBA textured triangle without perspective correction.
564 static void affine_textured_triangle( GLcontext
*ctx
,
571 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
573 #define INTERP_ALPHA 1
574 #define INTERP_INT_TEX 1
575 #define S_SCALE twidth
576 #define T_SCALE theight
579 struct affine_info info; \
580 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
581 struct gl_texture_object *obj = unit->Current2D; \
582 const GLint b = obj->BaseLevel; \
583 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
584 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
585 info.texture = (const GLchan *) obj->Image[b]->Data; \
586 info.twidth_log2 = obj->Image[b]->WidthLog2; \
587 info.smask = obj->Image[b]->Width - 1; \
588 info.tmask = obj->Image[b]->Height - 1; \
589 info.format = obj->Image[b]->Format; \
590 info.filter = obj->MinFilter; \
591 info.envmode = unit->EnvMode; \
592 span.arrayMask |= SPAN_RGBA; \
594 if (info.envmode == GL_BLEND) { \
595 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
596 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
597 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
598 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
599 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
601 if (!info.texture) { \
602 /* this shouldn't happen */ \
606 switch (info.format) { \
610 info.tbytesline = obj->Image[b]->Width; \
612 case GL_LUMINANCE_ALPHA: \
613 info.tbytesline = obj->Image[b]->Width * 2; \
616 info.tbytesline = obj->Image[b]->Width * 3; \
619 info.tbytesline = obj->Image[b]->Width * 4; \
622 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
625 info.tsize = obj->Image[b]->Height * info.tbytesline;
627 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
629 #include "s_tritemp.h"
642 const GLchan
*texture
;
643 GLfixed er
, eg
, eb
, ea
; /* texture env color */
644 GLint tbytesline
, tsize
;
649 fast_persp_span(GLcontext
*ctx
, struct sw_span
*span
,
650 struct persp_info
*info
)
652 GLchan sample
[4]; /* the filtered texture sample */
654 /* Instead of defining a function for each mode, a test is done
655 * between the outer and inner loops. This is to reduce code size
656 * and complexity. Observe that an optimizing compiler kills
657 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
659 #define SPAN_NEAREST(DO_TEX,COMP) \
660 for (i = 0; i < span->end; i++) { \
661 GLdouble invQ = tex_coord[2] ? \
662 (1.0 / tex_coord[2]) : 1.0; \
663 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
664 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
665 GLint s = IFLOOR(s_tmp) & info->smask; \
666 GLint t = IFLOOR(t_tmp) & info->tmask; \
667 GLint pos = (t << info->twidth_log2) + s; \
668 const GLchan *tex00 = info->texture + COMP * pos; \
670 span->red += span->redStep; \
671 span->green += span->greenStep; \
672 span->blue += span->blueStep; \
673 span->alpha += span->alphaStep; \
674 tex_coord[0] += tex_step[0]; \
675 tex_coord[1] += tex_step[1]; \
676 tex_coord[2] += tex_step[2]; \
680 #define SPAN_LINEAR(DO_TEX,COMP) \
681 for (i = 0; i < span->end; i++) { \
682 GLdouble invQ = tex_coord[2] ? \
683 (1.0 / tex_coord[2]) : 1.0; \
684 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
685 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
686 GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
687 GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
688 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
689 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
690 GLfixed sf = s_fix & FIXED_FRAC_MASK; \
691 GLfixed tf = t_fix & FIXED_FRAC_MASK; \
692 GLfixed si = FIXED_FRAC_MASK - sf; \
693 GLfixed ti = FIXED_FRAC_MASK - tf; \
694 GLint pos = (t << info->twidth_log2) + s; \
695 const GLchan *tex00 = info->texture + COMP * pos; \
696 const GLchan *tex10 = tex00 + info->tbytesline; \
697 const GLchan *tex01 = tex00 + COMP; \
698 const GLchan *tex11 = tex10 + COMP; \
701 if (t == info->tmask) { \
702 tex10 -= info->tsize; \
703 tex11 -= info->tsize; \
705 if (s == info->smask) { \
706 tex01 -= info->tbytesline; \
707 tex11 -= info->tbytesline; \
710 span->red += span->redStep; \
711 span->green += span->greenStep; \
712 span->blue += span->blueStep; \
713 span->alpha += span->alphaStep; \
714 tex_coord[0] += tex_step[0]; \
715 tex_coord[1] += tex_step[1]; \
716 tex_coord[2] += tex_step[2]; \
721 GLfloat tex_coord
[3], tex_step
[3];
722 GLchan
*dest
= span
->color
.rgba
[0];
724 tex_coord
[0] = span
->tex
[0][0] * (info
->smask
+ 1);
725 tex_step
[0] = span
->texStepX
[0][0] * (info
->smask
+ 1);
726 tex_coord
[1] = span
->tex
[0][1] * (info
->tmask
+ 1);
727 tex_step
[1] = span
->texStepX
[0][1] * (info
->tmask
+ 1);
728 /* span->tex[0][2] only if 3D-texturing, here only 2D */
729 tex_coord
[2] = span
->tex
[0][3];
730 tex_step
[2] = span
->texStepX
[0][3];
732 switch (info
->filter
) {
734 switch (info
->format
) {
736 switch (info
->envmode
) {
738 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
742 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
745 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
748 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
755 switch(info
->envmode
) {
757 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
760 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
763 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
766 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
769 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
779 switch (info
->format
) {
781 switch (info
->envmode
) {
783 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
787 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
790 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
793 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
800 switch (info
->envmode
) {
802 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
805 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
808 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
811 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
814 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
824 ASSERT(span
->arrayMask
& SPAN_RGBA
);
825 _mesa_write_rgba_span(ctx
, span
);
834 * Render an perspective corrected RGB/RGBA textured triangle.
835 * The Q (aka V in Mesa) coordinate must be zero such that the divide
836 * by interpolated Q/W comes out right.
839 static void persp_textured_triangle( GLcontext
*ctx
,
846 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
848 #define INTERP_ALPHA 1
852 struct persp_info info; \
853 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
854 const struct gl_texture_object *obj = unit->Current2D; \
855 const GLint b = obj->BaseLevel; \
856 info.texture = (const GLchan *) obj->Image[b]->Data; \
857 info.twidth_log2 = obj->Image[b]->WidthLog2; \
858 info.smask = obj->Image[b]->Width - 1; \
859 info.tmask = obj->Image[b]->Height - 1; \
860 info.format = obj->Image[b]->Format; \
861 info.filter = obj->MinFilter; \
862 info.envmode = unit->EnvMode; \
864 if (info.envmode == GL_BLEND) { \
865 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
866 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
867 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
868 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
869 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
871 if (!info.texture) { \
872 /* this shouldn't happen */ \
876 switch (info.format) { \
880 info.tbytesline = obj->Image[b]->Width; \
882 case GL_LUMINANCE_ALPHA: \
883 info.tbytesline = obj->Image[b]->Width * 2; \
886 info.tbytesline = obj->Image[b]->Width * 3; \
889 info.tbytesline = obj->Image[b]->Width * 4; \
892 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
895 info.tsize = obj->Image[b]->Height * info.tbytesline;
897 #define RENDER_SPAN( span ) \
898 span.interpMask &= ~SPAN_RGBA; \
899 span.arrayMask |= SPAN_RGBA; \
900 fast_persp_span(ctx, &span, &info);
902 #include "s_tritemp.h"
907 #endif /* CHAN_BITS != GL_FLOAT */
913 * Render a smooth-shaded, textured, RGBA triangle.
914 * Interpolate S,T,R with perspective correction, w/out mipmapping.
916 static void general_textured_triangle( GLcontext
*ctx
,
923 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
925 #define INTERP_SPEC 1
926 #define INTERP_ALPHA 1
929 #define RENDER_SPAN( span ) _mesa_write_texture_span(ctx, &span);
931 #include "s_tritemp.h"
937 * This is the big one!
938 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates.
942 multitextured_triangle( GLcontext
*ctx
,
950 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
952 #define INTERP_ALPHA 1
953 #define INTERP_SPEC 1
954 #define INTERP_MULTITEX 1
956 #define RENDER_SPAN( span ) _mesa_write_texture_span(ctx, &span);
958 #include "s_tritemp.h"
963 static void occlusion_zless_triangle( GLcontext
*ctx
,
968 if (ctx
->OcclusionResult
) {
972 #define DO_OCCLUSION_TEST
974 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
976 #define RENDER_SPAN( span ) \
978 for (i = 0; i < span.end; i++) { \
979 GLdepth z = FixedToDepth(span.z); \
981 ctx->OcclusionResult = GL_TRUE; \
984 span.z += span.zStep; \
987 #include "s_tritemp.h"
990 static void nodraw_triangle( GLcontext
*ctx
,
995 (void) (ctx
&& v0
&& v1
&& v2
);
1000 * This is used when separate specular color is enabled, but not
1001 * texturing. We add the specular color to the primary color,
1002 * draw the triangle, then restore the original primary color.
1003 * Inefficient, but seldom needed.
1005 void _swrast_add_spec_terms_triangle( GLcontext
*ctx
,
1008 const SWvertex
*v2
)
1010 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
1011 SWvertex
*ncv1
= (SWvertex
*)v1
;
1012 SWvertex
*ncv2
= (SWvertex
*)v2
;
1013 #if CHAN_TYPE == GL_FLOAT
1014 GLfloat rSum
, gSum
, bSum
;
1016 GLint rSum
, gSum
, bSum
;
1019 /* save original colors */
1020 COPY_CHAN4( c
[0], ncv0
->color
);
1021 COPY_CHAN4( c
[1], ncv1
->color
);
1022 COPY_CHAN4( c
[2], ncv2
->color
);
1024 rSum
= ncv0
->color
[0] + ncv0
->specular
[0];
1025 gSum
= ncv0
->color
[1] + ncv0
->specular
[1];
1026 bSum
= ncv0
->color
[2] + ncv0
->specular
[2];
1027 ncv0
->color
[0] = MIN2(rSum
, CHAN_MAX
);
1028 ncv0
->color
[1] = MIN2(gSum
, CHAN_MAX
);
1029 ncv0
->color
[2] = MIN2(bSum
, CHAN_MAX
);
1031 rSum
= ncv1
->color
[0] + ncv1
->specular
[0];
1032 gSum
= ncv1
->color
[1] + ncv1
->specular
[1];
1033 bSum
= ncv1
->color
[2] + ncv1
->specular
[2];
1034 ncv1
->color
[0] = MIN2(rSum
, CHAN_MAX
);
1035 ncv1
->color
[1] = MIN2(gSum
, CHAN_MAX
);
1036 ncv1
->color
[2] = MIN2(bSum
, CHAN_MAX
);
1038 rSum
= ncv2
->color
[0] + ncv2
->specular
[0];
1039 gSum
= ncv2
->color
[1] + ncv2
->specular
[1];
1040 bSum
= ncv2
->color
[2] + ncv2
->specular
[2];
1041 ncv2
->color
[0] = MIN2(rSum
, CHAN_MAX
);
1042 ncv2
->color
[1] = MIN2(gSum
, CHAN_MAX
);
1043 ncv2
->color
[2] = MIN2(bSum
, CHAN_MAX
);
1045 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
1046 /* restore original colors */
1047 COPY_CHAN4( ncv0
->color
, c
[0] );
1048 COPY_CHAN4( ncv1
->color
, c
[1] );
1049 COPY_CHAN4( ncv2
->color
, c
[2] );
1056 /* record the current triangle function name */
1057 const char *_mesa_triFuncName
= NULL
;
1059 #define USE(triFunc) \
1061 _mesa_triFuncName = #triFunc; \
1062 /*printf("%s\n", _mesa_triFuncName);*/ \
1063 swrast->Triangle = triFunc; \
1068 #define USE(triFunc) swrast->Triangle = triFunc;
1076 * Determine which triangle rendering function to use given the current
1077 * rendering context.
1079 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1080 * remove tests to this code.
1083 _swrast_choose_triangle( GLcontext
*ctx
)
1085 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1086 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
1088 if (ctx
->Polygon
.CullFlag
&&
1089 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1090 USE(nodraw_triangle
);
1094 if (ctx
->RenderMode
==GL_RENDER
) {
1096 if (ctx
->Polygon
.SmoothFlag
) {
1097 _mesa_set_aa_triangle_function(ctx
);
1098 ASSERT(swrast
->Triangle
);
1102 if (ctx
->Depth
.OcclusionTest
&&
1104 ctx
->Depth
.Mask
== GL_FALSE
&&
1105 ctx
->Depth
.Func
== GL_LESS
&&
1106 !ctx
->Stencil
.Enabled
) {
1108 ctx
->Color
.ColorMask
[0] == 0 &&
1109 ctx
->Color
.ColorMask
[1] == 0 &&
1110 ctx
->Color
.ColorMask
[2] == 0 &&
1111 ctx
->Color
.ColorMask
[3] == 0)
1113 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1114 USE(occlusion_zless_triangle
);
1119 if (ctx
->Texture
._ReallyEnabled
) {
1120 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1121 const struct gl_texture_object
*texObj2D
;
1122 const struct gl_texture_image
*texImg
;
1123 GLenum minFilter
, magFilter
, envMode
;
1125 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1126 texImg
= texObj2D
? texObj2D
->Image
[texObj2D
->BaseLevel
] : NULL
;
1127 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1128 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1129 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1130 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1132 /* First see if we can used an optimized 2-D texture function */
1133 if (ctx
->Texture
._ReallyEnabled
==TEXTURE0_2D
1134 && texObj2D
->WrapS
==GL_REPEAT
1135 && texObj2D
->WrapT
==GL_REPEAT
1136 && texImg
->Border
==0
1137 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1138 && minFilter
== magFilter
1139 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1140 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1141 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1142 if (minFilter
== GL_NEAREST
1143 && format
== MESA_FORMAT_RGB
1144 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1145 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1146 && ctx
->Depth
.Func
== GL_LESS
1147 && ctx
->Depth
.Mask
== GL_TRUE
)
1148 || swrast
->_RasterMask
== TEXTURE_BIT
)
1149 && ctx
->Polygon
.StippleFlag
== GL_FALSE
) {
1150 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1151 USE(simple_z_textured_triangle
);
1154 USE(simple_textured_triangle
);
1158 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1159 USE(general_textured_triangle
);
1161 USE(affine_textured_triangle
);
1166 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1167 USE(general_textured_triangle
);
1169 USE(persp_textured_triangle
);
1174 /* general case textured triangles */
1175 if (ctx
->Texture
._ReallyEnabled
> TEXTURE0_ANY
) {
1176 USE(multitextured_triangle
);
1179 USE(general_textured_triangle
);
1184 ASSERT(!ctx
->Texture
._ReallyEnabled
);
1185 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1186 /* smooth shaded, no texturing, stippled or some raster ops */
1188 USE(smooth_rgba_triangle
);
1191 USE(smooth_ci_triangle
);
1195 /* flat shaded, no texturing, stippled or some raster ops */
1197 USE(flat_rgba_triangle
);
1200 USE(flat_ci_triangle
);
1205 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1206 USE(_mesa_feedback_triangle
);
1209 /* GL_SELECT mode */
1210 USE(_mesa_select_triangle
);