1 /* $Id: s_triangle.c,v 1.70 2003/03/25 02:23:48 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.
39 #include "texformat.h"
43 #include "s_aatriangle.h"
44 #include "s_context.h"
46 #include "s_feedback.h"
48 #include "s_triangle.h"
52 * Just used for feedback mode.
54 GLboolean
_swrast_culltriangle( 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 #define NAME flat_ci_triangle
80 span.interpMask |= SPAN_INDEX; \
81 span.index = IntToFixed(v2->index); \
83 #define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
84 #include "s_tritemp.h"
89 * Render a smooth-shaded color index triangle.
91 #define NAME smooth_ci_triangle
94 #define INTERP_INDEX 1
95 #define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
96 #include "s_tritemp.h"
101 * Render a flat-shaded RGBA triangle.
103 #define NAME flat_rgba_triangle
106 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
108 ASSERT(ctx->Texture._EnabledUnits == 0); \
109 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
110 span.interpMask |= SPAN_RGBA; \
111 span.red = ChanToFixed(v2->color[0]); \
112 span.green = ChanToFixed(v2->color[1]); \
113 span.blue = ChanToFixed(v2->color[2]); \
114 span.alpha = ChanToFixed(v2->color[3]); \
116 span.greenStep = 0; \
119 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
120 #include "s_tritemp.h"
125 * Render a smooth-shaded RGBA triangle.
127 #define NAME smooth_rgba_triangle
130 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
132 #define INTERP_ALPHA 1
135 /* texturing must be off */ \
136 ASSERT(ctx->Texture._EnabledUnits == 0); \
137 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
139 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
140 #include "s_tritemp.h"
145 * Render an RGB, GL_DECAL, textured triangle.
146 * Interpolate S,T only w/out mipmapping or perspective correction.
150 #define NAME simple_textured_triangle
151 #define INTERP_INT_TEX 1
152 #define S_SCALE twidth
153 #define T_SCALE theight
156 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
157 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
158 const GLint b = obj->BaseLevel; \
159 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
160 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
161 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
162 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
163 const GLint smask = obj->Image[b]->Width - 1; \
164 const GLint tmask = obj->Image[b]->Height - 1; \
166 /* this shouldn't happen */ \
170 #define RENDER_SPAN( span ) \
172 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
173 span.intTex[1] -= FIXED_HALF; \
174 for (i = 0; i < span.end; i++) { \
175 GLint s = FixedToInt(span.intTex[0]) & smask; \
176 GLint t = FixedToInt(span.intTex[1]) & tmask; \
177 GLint pos = (t << twidth_log2) + s; \
178 pos = pos + pos + pos; /* multiply by 3 */ \
179 span.array->rgb[i][RCOMP] = texture[pos]; \
180 span.array->rgb[i][GCOMP] = texture[pos+1]; \
181 span.array->rgb[i][BCOMP] = texture[pos+2]; \
182 span.intTex[0] += span.intTexStep[0]; \
183 span.intTex[1] += span.intTexStep[1]; \
185 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
186 (CONST GLchan (*)[3]) span.array->rgb,\
188 #include "s_tritemp.h"
193 * Render an RGB, GL_DECAL, textured triangle.
194 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
195 * perspective correction.
199 #define NAME simple_z_textured_triangle
201 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
202 #define INTERP_INT_TEX 1
203 #define S_SCALE twidth
204 #define T_SCALE theight
207 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
208 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
209 const GLint b = obj->BaseLevel; \
210 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
211 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
212 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
213 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
214 const GLint smask = obj->Image[b]->Width - 1; \
215 const GLint tmask = obj->Image[b]->Height - 1; \
217 /* this shouldn't happen */ \
221 #define RENDER_SPAN( span ) \
223 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
224 span.intTex[1] -= FIXED_HALF; \
225 for (i = 0; i < span.end; i++) { \
226 const GLdepth z = FixedToDepth(span.z); \
228 GLint s = FixedToInt(span.intTex[0]) & smask; \
229 GLint t = FixedToInt(span.intTex[1]) & tmask; \
230 GLint pos = (t << twidth_log2) + s; \
231 pos = pos + pos + pos; /* multiply by 3 */ \
232 span.array->rgb[i][RCOMP] = texture[pos]; \
233 span.array->rgb[i][GCOMP] = texture[pos+1]; \
234 span.array->rgb[i][BCOMP] = texture[pos+2]; \
236 span.array->mask[i] = 1; \
239 span.array->mask[i] = 0; \
241 span.intTex[0] += span.intTexStep[0]; \
242 span.intTex[1] += span.intTexStep[1]; \
243 span.z += span.zStep; \
245 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
246 (CONST GLchan (*)[3]) span.array->rgb,\
248 #include "s_tritemp.h"
252 #if CHAN_TYPE != GL_FLOAT
261 const GLchan
*texture
;
262 GLfixed er
, eg
, eb
, ea
;
263 GLint tbytesline
, tsize
;
267 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
268 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
272 affine_span(GLcontext
*ctx
, struct sw_span
*span
,
273 struct affine_info
*info
)
275 GLchan sample
[4]; /* the filtered texture sample */
277 /* Instead of defining a function for each mode, a test is done
278 * between the outer and inner loops. This is to reduce code size
279 * and complexity. Observe that an optimizing compiler kills
280 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
283 #define NEAREST_RGB \
284 sample[RCOMP] = tex00[RCOMP]; \
285 sample[GCOMP] = tex00[GCOMP]; \
286 sample[BCOMP] = tex00[BCOMP]; \
287 sample[ACOMP] = CHAN_MAX
290 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
291 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
292 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
293 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
294 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
295 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
296 sample[ACOMP] = CHAN_MAX
298 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
300 #define LINEAR_RGBA \
301 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
302 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT;\
303 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
304 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT;\
305 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
306 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT;\
307 sample[ACOMP] = (ti * (si * tex00[3] + sf * tex01[3]) + \
308 tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT
311 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
312 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
313 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
314 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
317 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
318 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
319 >> (FIXED_SHIFT + 8); \
320 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
321 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
322 >> (FIXED_SHIFT + 8); \
323 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
324 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
325 >> (FIXED_SHIFT + 8); \
326 dest[ACOMP] = FixedToInt(span->alpha)
329 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
330 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
331 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
332 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
333 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
334 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
335 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
337 #define REPLACE COPY_CHAN4(dest, sample)
341 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
342 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
343 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
344 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
345 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
346 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
347 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
352 #define NEAREST_RGB_REPLACE \
354 dest[0] = sample[0]; \
355 dest[1] = sample[1]; \
356 dest[2] = sample[2]; \
357 dest[3] = FixedToInt(span->alpha);
359 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
361 #define SPAN_NEAREST(DO_TEX,COMP) \
362 for (i = 0; i < span->end; i++) { \
363 /* Isn't it necessary to use FixedFloor below?? */ \
364 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
365 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
366 GLint pos = (t << info->twidth_log2) + s; \
367 const GLchan *tex00 = info->texture + COMP * pos; \
369 span->red += span->redStep; \
370 span->green += span->greenStep; \
371 span->blue += span->blueStep; \
372 span->alpha += span->alphaStep; \
373 span->intTex[0] += span->intTexStep[0]; \
374 span->intTex[1] += span->intTexStep[1]; \
378 #define SPAN_LINEAR(DO_TEX,COMP) \
379 for (i = 0; i < span->end; i++) { \
380 /* Isn't it necessary to use FixedFloor below?? */ \
381 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
382 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
383 GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
384 GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
385 GLfixed si = FIXED_FRAC_MASK - sf; \
386 GLfixed ti = FIXED_FRAC_MASK - tf; \
387 GLint pos = (t << info->twidth_log2) + s; \
388 const GLchan *tex00 = info->texture + COMP * pos; \
389 const GLchan *tex10 = tex00 + info->tbytesline; \
390 const GLchan *tex01 = tex00 + COMP; \
391 const GLchan *tex11 = tex10 + COMP; \
394 if (t == info->tmask) { \
395 tex10 -= info->tsize; \
396 tex11 -= info->tsize; \
398 if (s == info->smask) { \
399 tex01 -= info->tbytesline; \
400 tex11 -= info->tbytesline; \
403 span->red += span->redStep; \
404 span->green += span->greenStep; \
405 span->blue += span->blueStep; \
406 span->alpha += span->alphaStep; \
407 span->intTex[0] += span->intTexStep[0]; \
408 span->intTex[1] += span->intTexStep[1]; \
414 GLchan
*dest
= span
->array
->rgba
[0];
416 span
->intTex
[0] -= FIXED_HALF
;
417 span
->intTex
[1] -= FIXED_HALF
;
418 switch (info
->filter
) {
420 switch (info
->format
) {
422 switch (info
->envmode
) {
424 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
428 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
431 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
434 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
437 _mesa_problem(ctx
, "bad tex env mode in SPAN_LINEAR");
442 switch(info
->envmode
) {
444 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
447 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
450 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
453 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
456 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
459 _mesa_problem(ctx
, "bad tex env mode (2) in SPAN_LINEAR");
467 span
->intTex
[0] -= FIXED_HALF
;
468 span
->intTex
[1] -= FIXED_HALF
;
469 switch (info
->format
) {
471 switch (info
->envmode
) {
473 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
477 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
480 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
483 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
486 _mesa_problem(ctx
, "bad tex env mode (3) in SPAN_LINEAR");
491 switch (info
->envmode
) {
493 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
496 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
499 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
502 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
505 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
508 _mesa_problem(ctx
, "bad tex env mode (4) in SPAN_LINEAR");
515 span
->interpMask
&= ~SPAN_RGBA
;
516 ASSERT(span
->arrayMask
& SPAN_RGBA
);
517 _swrast_write_rgba_span(ctx
, span
);
526 * Render an RGB/RGBA textured triangle without perspective correction.
528 #define NAME affine_textured_triangle
531 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
533 #define INTERP_ALPHA 1
534 #define INTERP_INT_TEX 1
535 #define S_SCALE twidth
536 #define T_SCALE theight
539 struct affine_info info; \
540 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
541 struct gl_texture_object *obj = unit->Current2D; \
542 const GLint b = obj->BaseLevel; \
543 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
544 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
545 info.texture = (const GLchan *) obj->Image[b]->Data; \
546 info.twidth_log2 = obj->Image[b]->WidthLog2; \
547 info.smask = obj->Image[b]->Width - 1; \
548 info.tmask = obj->Image[b]->Height - 1; \
549 info.format = obj->Image[b]->Format; \
550 info.filter = obj->MinFilter; \
551 info.envmode = unit->EnvMode; \
552 span.arrayMask |= SPAN_RGBA; \
554 if (info.envmode == GL_BLEND) { \
555 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
556 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
557 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
558 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
559 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
561 if (!info.texture) { \
562 /* this shouldn't happen */ \
566 switch (info.format) { \
570 info.tbytesline = obj->Image[b]->Width; \
572 case GL_LUMINANCE_ALPHA: \
573 info.tbytesline = obj->Image[b]->Width * 2; \
576 info.tbytesline = obj->Image[b]->Width * 3; \
579 info.tbytesline = obj->Image[b]->Width * 4; \
582 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
585 info.tsize = obj->Image[b]->Height * info.tbytesline;
587 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
589 #include "s_tritemp.h"
600 const GLchan
*texture
;
601 GLfixed er
, eg
, eb
, ea
; /* texture env color */
602 GLint tbytesline
, tsize
;
607 fast_persp_span(GLcontext
*ctx
, struct sw_span
*span
,
608 struct persp_info
*info
)
610 GLchan sample
[4]; /* the filtered texture sample */
612 /* Instead of defining a function for each mode, a test is done
613 * between the outer and inner loops. This is to reduce code size
614 * and complexity. Observe that an optimizing compiler kills
615 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
617 #define SPAN_NEAREST(DO_TEX,COMP) \
618 for (i = 0; i < span->end; i++) { \
619 GLdouble invQ = tex_coord[2] ? \
620 (1.0 / tex_coord[2]) : 1.0; \
621 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
622 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
623 GLint s = IFLOOR(s_tmp) & info->smask; \
624 GLint t = IFLOOR(t_tmp) & info->tmask; \
625 GLint pos = (t << info->twidth_log2) + s; \
626 const GLchan *tex00 = info->texture + COMP * pos; \
628 span->red += span->redStep; \
629 span->green += span->greenStep; \
630 span->blue += span->blueStep; \
631 span->alpha += span->alphaStep; \
632 tex_coord[0] += tex_step[0]; \
633 tex_coord[1] += tex_step[1]; \
634 tex_coord[2] += tex_step[2]; \
638 #define SPAN_LINEAR(DO_TEX,COMP) \
639 for (i = 0; i < span->end; i++) { \
640 GLdouble invQ = tex_coord[2] ? \
641 (1.0 / tex_coord[2]) : 1.0; \
642 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
643 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
644 GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
645 GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
646 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
647 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
648 GLfixed sf = s_fix & FIXED_FRAC_MASK; \
649 GLfixed tf = t_fix & FIXED_FRAC_MASK; \
650 GLfixed si = FIXED_FRAC_MASK - sf; \
651 GLfixed ti = FIXED_FRAC_MASK - tf; \
652 GLint pos = (t << info->twidth_log2) + s; \
653 const GLchan *tex00 = info->texture + COMP * pos; \
654 const GLchan *tex10 = tex00 + info->tbytesline; \
655 const GLchan *tex01 = tex00 + COMP; \
656 const GLchan *tex11 = tex10 + COMP; \
659 if (t == info->tmask) { \
660 tex10 -= info->tsize; \
661 tex11 -= info->tsize; \
663 if (s == info->smask) { \
664 tex01 -= info->tbytesline; \
665 tex11 -= info->tbytesline; \
668 span->red += span->redStep; \
669 span->green += span->greenStep; \
670 span->blue += span->blueStep; \
671 span->alpha += span->alphaStep; \
672 tex_coord[0] += tex_step[0]; \
673 tex_coord[1] += tex_step[1]; \
674 tex_coord[2] += tex_step[2]; \
679 GLfloat tex_coord
[3], tex_step
[3];
680 GLchan
*dest
= span
->array
->rgba
[0];
682 tex_coord
[0] = span
->tex
[0][0] * (info
->smask
+ 1);
683 tex_step
[0] = span
->texStepX
[0][0] * (info
->smask
+ 1);
684 tex_coord
[1] = span
->tex
[0][1] * (info
->tmask
+ 1);
685 tex_step
[1] = span
->texStepX
[0][1] * (info
->tmask
+ 1);
686 /* span->tex[0][2] only if 3D-texturing, here only 2D */
687 tex_coord
[2] = span
->tex
[0][3];
688 tex_step
[2] = span
->texStepX
[0][3];
690 switch (info
->filter
) {
692 switch (info
->format
) {
694 switch (info
->envmode
) {
696 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
700 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
703 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
706 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
709 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
714 switch(info
->envmode
) {
716 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
719 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
722 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
725 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
728 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
731 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
739 switch (info
->format
) {
741 switch (info
->envmode
) {
743 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
747 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
750 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
753 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
756 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
761 switch (info
->envmode
) {
763 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
766 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
769 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
772 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
775 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
778 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
786 ASSERT(span
->arrayMask
& SPAN_RGBA
);
787 _swrast_write_rgba_span(ctx
, span
);
795 * Render an perspective corrected RGB/RGBA textured triangle.
796 * The Q (aka V in Mesa) coordinate must be zero such that the divide
797 * by interpolated Q/W comes out right.
800 #define NAME persp_textured_triangle
803 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
805 #define INTERP_ALPHA 1
809 struct persp_info info; \
810 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
811 const struct gl_texture_object *obj = unit->Current2D; \
812 const GLint b = obj->BaseLevel; \
813 info.texture = (const GLchan *) obj->Image[b]->Data; \
814 info.twidth_log2 = obj->Image[b]->WidthLog2; \
815 info.smask = obj->Image[b]->Width - 1; \
816 info.tmask = obj->Image[b]->Height - 1; \
817 info.format = obj->Image[b]->Format; \
818 info.filter = obj->MinFilter; \
819 info.envmode = unit->EnvMode; \
821 if (info.envmode == GL_BLEND) { \
822 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
823 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
824 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
825 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
826 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
828 if (!info.texture) { \
829 /* this shouldn't happen */ \
833 switch (info.format) { \
837 info.tbytesline = obj->Image[b]->Width; \
839 case GL_LUMINANCE_ALPHA: \
840 info.tbytesline = obj->Image[b]->Width * 2; \
843 info.tbytesline = obj->Image[b]->Width * 3; \
846 info.tbytesline = obj->Image[b]->Width * 4; \
849 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
852 info.tsize = obj->Image[b]->Height * info.tbytesline;
854 #define RENDER_SPAN( span ) \
855 span.interpMask &= ~SPAN_RGBA; \
856 span.arrayMask |= SPAN_RGBA; \
857 fast_persp_span(ctx, &span, &info);
859 #include "s_tritemp.h"
862 #endif /* CHAN_BITS != GL_FLOAT */
868 * Render a smooth-shaded, textured, RGBA triangle.
869 * Interpolate S,T,R with perspective correction, w/out mipmapping.
871 #define NAME general_textured_triangle
875 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
877 #define INTERP_SPEC 1
878 #define INTERP_ALPHA 1
880 #define RENDER_SPAN( span ) _swrast_write_texture_span(ctx, &span);
881 #include "s_tritemp.h"
886 * This is the big one!
887 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates.
890 #define NAME multitextured_triangle
894 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
896 #define INTERP_ALPHA 1
897 #define INTERP_SPEC 1
898 #define INTERP_MULTITEX 1
899 #define RENDER_SPAN( span ) _swrast_write_texture_span(ctx, &span);
900 #include "s_tritemp.h"
904 #define NAME occlusion_zless_triangle
905 #define DO_OCCLUSION_TEST
907 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
909 if (ctx->OcclusionResult) { \
912 #define RENDER_SPAN( span ) \
914 for (i = 0; i < span.end; i++) { \
915 GLdepth z = FixedToDepth(span.z); \
917 ctx->OcclusionResult = GL_TRUE; \
920 span.z += span.zStep; \
922 #include "s_tritemp.h"
927 nodraw_triangle( GLcontext
*ctx
,
932 (void) (ctx
&& v0
&& v1
&& v2
);
937 * This is used when separate specular color is enabled, but not
938 * texturing. We add the specular color to the primary color,
939 * draw the triangle, then restore the original primary color.
940 * Inefficient, but seldom needed.
942 void _swrast_add_spec_terms_triangle( GLcontext
*ctx
,
947 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
948 SWvertex
*ncv1
= (SWvertex
*)v1
;
949 SWvertex
*ncv2
= (SWvertex
*)v2
;
950 #if CHAN_TYPE == GL_FLOAT
951 GLfloat rSum
, gSum
, bSum
;
953 GLint rSum
, gSum
, bSum
;
956 /* save original colors */
957 COPY_CHAN4( c
[0], ncv0
->color
);
958 COPY_CHAN4( c
[1], ncv1
->color
);
959 COPY_CHAN4( c
[2], ncv2
->color
);
961 rSum
= ncv0
->color
[0] + ncv0
->specular
[0];
962 gSum
= ncv0
->color
[1] + ncv0
->specular
[1];
963 bSum
= ncv0
->color
[2] + ncv0
->specular
[2];
964 ncv0
->color
[0] = MIN2(rSum
, CHAN_MAX
);
965 ncv0
->color
[1] = MIN2(gSum
, CHAN_MAX
);
966 ncv0
->color
[2] = MIN2(bSum
, CHAN_MAX
);
968 rSum
= ncv1
->color
[0] + ncv1
->specular
[0];
969 gSum
= ncv1
->color
[1] + ncv1
->specular
[1];
970 bSum
= ncv1
->color
[2] + ncv1
->specular
[2];
971 ncv1
->color
[0] = MIN2(rSum
, CHAN_MAX
);
972 ncv1
->color
[1] = MIN2(gSum
, CHAN_MAX
);
973 ncv1
->color
[2] = MIN2(bSum
, CHAN_MAX
);
975 rSum
= ncv2
->color
[0] + ncv2
->specular
[0];
976 gSum
= ncv2
->color
[1] + ncv2
->specular
[1];
977 bSum
= ncv2
->color
[2] + ncv2
->specular
[2];
978 ncv2
->color
[0] = MIN2(rSum
, CHAN_MAX
);
979 ncv2
->color
[1] = MIN2(gSum
, CHAN_MAX
);
980 ncv2
->color
[2] = MIN2(bSum
, CHAN_MAX
);
982 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
983 /* restore original colors */
984 COPY_CHAN4( ncv0
->color
, c
[0] );
985 COPY_CHAN4( ncv1
->color
, c
[1] );
986 COPY_CHAN4( ncv2
->color
, c
[2] );
993 /* record the current triangle function name */
994 const char *_mesa_triFuncName
= NULL
;
996 #define USE(triFunc) \
998 _mesa_triFuncName = #triFunc; \
999 /*printf("%s\n", _mesa_triFuncName);*/ \
1000 swrast->Triangle = triFunc; \
1005 #define USE(triFunc) swrast->Triangle = triFunc;
1013 * Determine which triangle rendering function to use given the current
1014 * rendering context.
1016 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1017 * remove tests to this code.
1020 _swrast_choose_triangle( GLcontext
*ctx
)
1022 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1023 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
1025 if (ctx
->Polygon
.CullFlag
&&
1026 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1027 USE(nodraw_triangle
);
1031 if (ctx
->RenderMode
==GL_RENDER
) {
1033 if (ctx
->Polygon
.SmoothFlag
) {
1034 _swrast_set_aa_triangle_function(ctx
);
1035 ASSERT(swrast
->Triangle
);
1039 if (ctx
->Depth
.OcclusionTest
&&
1041 ctx
->Depth
.Mask
== GL_FALSE
&&
1042 ctx
->Depth
.Func
== GL_LESS
&&
1043 !ctx
->Stencil
.Enabled
) {
1045 ctx
->Color
.ColorMask
[0] == 0 &&
1046 ctx
->Color
.ColorMask
[1] == 0 &&
1047 ctx
->Color
.ColorMask
[2] == 0 &&
1048 ctx
->Color
.ColorMask
[3] == 0)
1050 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1051 USE(occlusion_zless_triangle
);
1056 if (ctx
->Texture
._EnabledUnits
) {
1057 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1058 const struct gl_texture_object
*texObj2D
;
1059 const struct gl_texture_image
*texImg
;
1060 GLenum minFilter
, magFilter
, envMode
;
1062 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1063 texImg
= texObj2D
? texObj2D
->Image
[texObj2D
->BaseLevel
] : NULL
;
1064 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1065 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1066 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1067 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1069 /* First see if we can use an optimized 2-D texture function */
1070 if (ctx
->Texture
._EnabledUnits
== 1
1071 && !ctx
->FragmentProgram
.Enabled
1072 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1073 && texObj2D
->WrapS
==GL_REPEAT
1074 && texObj2D
->WrapT
==GL_REPEAT
1075 && texImg
->Border
==0
1076 && texImg
->Width
== texImg
->RowStride
1077 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1078 && minFilter
== magFilter
1079 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1080 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1081 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1082 if (minFilter
== GL_NEAREST
1083 && format
== MESA_FORMAT_RGB
1084 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1085 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1086 && ctx
->Depth
.Func
== GL_LESS
1087 && ctx
->Depth
.Mask
== GL_TRUE
)
1088 || swrast
->_RasterMask
== TEXTURE_BIT
)
1089 && ctx
->Polygon
.StippleFlag
== GL_FALSE
) {
1090 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1091 USE(simple_z_textured_triangle
);
1094 USE(simple_textured_triangle
);
1098 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1099 USE(general_textured_triangle
);
1101 USE(affine_textured_triangle
);
1106 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1107 USE(general_textured_triangle
);
1109 USE(persp_textured_triangle
);
1114 /* general case textured triangles */
1115 if (ctx
->Texture
._EnabledUnits
> 1) {
1116 USE(multitextured_triangle
);
1119 USE(general_textured_triangle
);
1124 ASSERT(!ctx
->Texture
._EnabledUnits
);
1125 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1126 /* smooth shaded, no texturing, stippled or some raster ops */
1128 USE(smooth_rgba_triangle
);
1131 USE(smooth_ci_triangle
);
1135 /* flat shaded, no texturing, stippled or some raster ops */
1137 USE(flat_rgba_triangle
);
1140 USE(flat_ci_triangle
);
1145 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1146 USE(_swrast_feedback_triangle
);
1149 /* GL_SELECT mode */
1150 USE(_swrast_select_triangle
);