1 /* $Id: s_triangle.c,v 1.66 2002/12/18 15:02:19 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 * Just used for feedback mode.
55 GLboolean
_mesa_cull_triangle( GLcontext
*ctx
,
60 GLfloat ex
= v1
->win
[0] - v0
->win
[0];
61 GLfloat ey
= v1
->win
[1] - v0
->win
[1];
62 GLfloat fx
= v2
->win
[0] - v0
->win
[0];
63 GLfloat fy
= v2
->win
[1] - v0
->win
[1];
64 GLfloat c
= ex
*fy
-ey
*fx
;
66 if (c
* SWRAST_CONTEXT(ctx
)->_backface_sign
> 0)
75 * Render a flat-shaded color index triangle.
77 #define NAME flat_ci_triangle
81 span.interpMask |= SPAN_INDEX; \
82 span.index = IntToFixed(v2->index); \
84 #define RENDER_SPAN( span ) _mesa_write_index_span(ctx, &span);
85 #include "s_tritemp.h"
90 * Render a smooth-shaded color index triangle.
92 #define NAME smooth_ci_triangle
95 #define INTERP_INDEX 1
96 #define RENDER_SPAN( span ) _mesa_write_index_span(ctx, &span);
97 #include "s_tritemp.h"
102 * Render a flat-shaded RGBA triangle.
104 #define NAME flat_rgba_triangle
107 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
109 ASSERT(ctx->Texture._EnabledUnits == 0); \
110 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
111 span.interpMask |= SPAN_RGBA; \
112 span.red = ChanToFixed(v2->color[0]); \
113 span.green = ChanToFixed(v2->color[1]); \
114 span.blue = ChanToFixed(v2->color[2]); \
115 span.alpha = ChanToFixed(v2->color[3]); \
117 span.greenStep = 0; \
120 #define RENDER_SPAN( span ) _mesa_write_rgba_span(ctx, &span);
121 #include "s_tritemp.h"
126 * Render a smooth-shaded RGBA triangle.
128 #define NAME smooth_rgba_triangle
131 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
133 #define INTERP_ALPHA 1
136 /* texturing must be off */ \
137 ASSERT(ctx->Texture._EnabledUnits == 0); \
138 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
140 #define RENDER_SPAN( span ) _mesa_write_rgba_span(ctx, &span);
141 #include "s_tritemp.h"
146 * Render an RGB, GL_DECAL, textured triangle.
147 * Interpolate S,T only w/out mipmapping or perspective correction.
151 #define NAME simple_textured_triangle
152 #define INTERP_INT_TEX 1
153 #define S_SCALE twidth
154 #define T_SCALE theight
157 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
158 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
159 const GLint b = obj->BaseLevel; \
160 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
161 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
162 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
163 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
164 const GLint smask = obj->Image[b]->Width - 1; \
165 const GLint tmask = obj->Image[b]->Height - 1; \
167 /* this shouldn't happen */ \
171 #define RENDER_SPAN( span ) \
173 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
174 span.intTex[1] -= FIXED_HALF; \
175 for (i = 0; i < span.end; i++) { \
176 GLint s = FixedToInt(span.intTex[0]) & smask; \
177 GLint t = FixedToInt(span.intTex[1]) & tmask; \
178 GLint pos = (t << twidth_log2) + s; \
179 pos = pos + pos + pos; /* multiply by 3 */ \
180 span.array->rgb[i][RCOMP] = texture[pos]; \
181 span.array->rgb[i][GCOMP] = texture[pos+1]; \
182 span.array->rgb[i][BCOMP] = texture[pos+2]; \
183 span.intTex[0] += span.intTexStep[0]; \
184 span.intTex[1] += span.intTexStep[1]; \
186 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
187 (CONST GLchan (*)[3]) span.array->rgb,\
189 #include "s_tritemp.h"
194 * Render an RGB, GL_DECAL, textured triangle.
195 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
196 * perspective correction.
200 #define NAME simple_z_textured_triangle
202 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
203 #define INTERP_INT_TEX 1
204 #define S_SCALE twidth
205 #define T_SCALE theight
208 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
209 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
210 const GLint b = obj->BaseLevel; \
211 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
212 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
213 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
214 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
215 const GLint smask = obj->Image[b]->Width - 1; \
216 const GLint tmask = obj->Image[b]->Height - 1; \
218 /* this shouldn't happen */ \
222 #define RENDER_SPAN( span ) \
224 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
225 span.intTex[1] -= FIXED_HALF; \
226 for (i = 0; i < span.end; i++) { \
227 const GLdepth z = FixedToDepth(span.z); \
229 GLint s = FixedToInt(span.intTex[0]) & smask; \
230 GLint t = FixedToInt(span.intTex[1]) & tmask; \
231 GLint pos = (t << twidth_log2) + s; \
232 pos = pos + pos + pos; /* multiply by 3 */ \
233 span.array->rgb[i][RCOMP] = texture[pos]; \
234 span.array->rgb[i][GCOMP] = texture[pos+1]; \
235 span.array->rgb[i][BCOMP] = texture[pos+2]; \
237 span.array->mask[i] = 1; \
240 span.array->mask[i] = 0; \
242 span.intTex[0] += span.intTexStep[0]; \
243 span.intTex[1] += span.intTexStep[1]; \
244 span.z += span.zStep; \
246 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
247 (CONST GLchan (*)[3]) span.array->rgb,\
249 #include "s_tritemp.h"
253 #if CHAN_TYPE != GL_FLOAT
262 const GLchan
*texture
;
263 GLfixed er
, eg
, eb
, ea
;
264 GLint tbytesline
, tsize
;
268 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
269 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
273 affine_span(GLcontext
*ctx
, struct sw_span
*span
,
274 struct affine_info
*info
)
276 GLchan sample
[4]; /* the filtered texture sample */
278 /* Instead of defining a function for each mode, a test is done
279 * between the outer and inner loops. This is to reduce code size
280 * and complexity. Observe that an optimizing compiler kills
281 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
284 #define NEAREST_RGB \
285 sample[RCOMP] = tex00[RCOMP]; \
286 sample[GCOMP] = tex00[GCOMP]; \
287 sample[BCOMP] = tex00[BCOMP]; \
288 sample[ACOMP] = CHAN_MAX
291 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
292 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
293 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
294 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
295 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
296 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
297 sample[ACOMP] = CHAN_MAX
299 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
301 #define LINEAR_RGBA \
302 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
303 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT;\
304 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
305 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT;\
306 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
307 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT;\
308 sample[ACOMP] = (ti * (si * tex00[3] + sf * tex01[3]) + \
309 tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT
312 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
313 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
314 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
315 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
318 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
319 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
320 >> (FIXED_SHIFT + 8); \
321 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
322 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
323 >> (FIXED_SHIFT + 8); \
324 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
325 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
326 >> (FIXED_SHIFT + 8); \
327 dest[ACOMP] = FixedToInt(span->alpha)
330 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
331 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
332 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
333 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
334 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
335 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
336 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
338 #define REPLACE COPY_CHAN4(dest, sample)
342 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
343 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
344 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
345 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
346 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
347 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
348 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
353 #define NEAREST_RGB_REPLACE \
355 dest[0] = sample[0]; \
356 dest[1] = sample[1]; \
357 dest[2] = sample[2]; \
358 dest[3] = FixedToInt(span->alpha);
360 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
362 #define SPAN_NEAREST(DO_TEX,COMP) \
363 for (i = 0; i < span->end; i++) { \
364 /* Isn't it necessary to use FixedFloor below?? */ \
365 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
366 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
367 GLint pos = (t << info->twidth_log2) + s; \
368 const GLchan *tex00 = info->texture + COMP * pos; \
370 span->red += span->redStep; \
371 span->green += span->greenStep; \
372 span->blue += span->blueStep; \
373 span->alpha += span->alphaStep; \
374 span->intTex[0] += span->intTexStep[0]; \
375 span->intTex[1] += span->intTexStep[1]; \
379 #define SPAN_LINEAR(DO_TEX,COMP) \
380 for (i = 0; i < span->end; i++) { \
381 /* Isn't it necessary to use FixedFloor below?? */ \
382 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
383 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
384 GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
385 GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
386 GLfixed si = FIXED_FRAC_MASK - sf; \
387 GLfixed ti = FIXED_FRAC_MASK - tf; \
388 GLint pos = (t << info->twidth_log2) + s; \
389 const GLchan *tex00 = info->texture + COMP * pos; \
390 const GLchan *tex10 = tex00 + info->tbytesline; \
391 const GLchan *tex01 = tex00 + COMP; \
392 const GLchan *tex11 = tex10 + COMP; \
395 if (t == info->tmask) { \
396 tex10 -= info->tsize; \
397 tex11 -= info->tsize; \
399 if (s == info->smask) { \
400 tex01 -= info->tbytesline; \
401 tex11 -= info->tbytesline; \
404 span->red += span->redStep; \
405 span->green += span->greenStep; \
406 span->blue += span->blueStep; \
407 span->alpha += span->alphaStep; \
408 span->intTex[0] += span->intTexStep[0]; \
409 span->intTex[1] += span->intTexStep[1]; \
415 GLchan
*dest
= span
->array
->rgba
[0];
417 span
->intTex
[0] -= FIXED_HALF
;
418 span
->intTex
[1] -= FIXED_HALF
;
419 switch (info
->filter
) {
421 switch (info
->format
) {
423 switch (info
->envmode
) {
425 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
429 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
432 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
435 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
438 _mesa_problem(ctx
, "bad tex env mode in SPAN_LINEAR");
443 switch(info
->envmode
) {
445 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
448 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
451 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
454 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
457 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
460 _mesa_problem(ctx
, "bad tex env mode (2) in SPAN_LINEAR");
468 span
->intTex
[0] -= FIXED_HALF
;
469 span
->intTex
[1] -= FIXED_HALF
;
470 switch (info
->format
) {
472 switch (info
->envmode
) {
474 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
478 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
481 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
484 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
487 _mesa_problem(ctx
, "bad tex env mode (3) in SPAN_LINEAR");
492 switch (info
->envmode
) {
494 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
497 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
500 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
503 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
506 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
509 _mesa_problem(ctx
, "bad tex env mode (4) in SPAN_LINEAR");
516 span
->interpMask
&= ~SPAN_RGBA
;
517 ASSERT(span
->arrayMask
& SPAN_RGBA
);
518 _mesa_write_rgba_span(ctx
, span
);
527 * Render an RGB/RGBA textured triangle without perspective correction.
529 #define NAME affine_textured_triangle
532 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
534 #define INTERP_ALPHA 1
535 #define INTERP_INT_TEX 1
536 #define S_SCALE twidth
537 #define T_SCALE theight
540 struct affine_info info; \
541 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
542 struct gl_texture_object *obj = unit->Current2D; \
543 const GLint b = obj->BaseLevel; \
544 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
545 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
546 info.texture = (const GLchan *) obj->Image[b]->Data; \
547 info.twidth_log2 = obj->Image[b]->WidthLog2; \
548 info.smask = obj->Image[b]->Width - 1; \
549 info.tmask = obj->Image[b]->Height - 1; \
550 info.format = obj->Image[b]->Format; \
551 info.filter = obj->MinFilter; \
552 info.envmode = unit->EnvMode; \
553 span.arrayMask |= SPAN_RGBA; \
555 if (info.envmode == GL_BLEND) { \
556 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
557 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
558 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
559 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
560 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
562 if (!info.texture) { \
563 /* this shouldn't happen */ \
567 switch (info.format) { \
571 info.tbytesline = obj->Image[b]->Width; \
573 case GL_LUMINANCE_ALPHA: \
574 info.tbytesline = obj->Image[b]->Width * 2; \
577 info.tbytesline = obj->Image[b]->Width * 3; \
580 info.tbytesline = obj->Image[b]->Width * 4; \
583 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
586 info.tsize = obj->Image[b]->Height * info.tbytesline;
588 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
590 #include "s_tritemp.h"
601 const GLchan
*texture
;
602 GLfixed er
, eg
, eb
, ea
; /* texture env color */
603 GLint tbytesline
, tsize
;
608 fast_persp_span(GLcontext
*ctx
, struct sw_span
*span
,
609 struct persp_info
*info
)
611 GLchan sample
[4]; /* the filtered texture sample */
613 /* Instead of defining a function for each mode, a test is done
614 * between the outer and inner loops. This is to reduce code size
615 * and complexity. Observe that an optimizing compiler kills
616 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
618 #define SPAN_NEAREST(DO_TEX,COMP) \
619 for (i = 0; i < span->end; i++) { \
620 GLdouble invQ = tex_coord[2] ? \
621 (1.0 / tex_coord[2]) : 1.0; \
622 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
623 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
624 GLint s = IFLOOR(s_tmp) & info->smask; \
625 GLint t = IFLOOR(t_tmp) & info->tmask; \
626 GLint pos = (t << info->twidth_log2) + s; \
627 const GLchan *tex00 = info->texture + COMP * pos; \
629 span->red += span->redStep; \
630 span->green += span->greenStep; \
631 span->blue += span->blueStep; \
632 span->alpha += span->alphaStep; \
633 tex_coord[0] += tex_step[0]; \
634 tex_coord[1] += tex_step[1]; \
635 tex_coord[2] += tex_step[2]; \
639 #define SPAN_LINEAR(DO_TEX,COMP) \
640 for (i = 0; i < span->end; i++) { \
641 GLdouble invQ = tex_coord[2] ? \
642 (1.0 / tex_coord[2]) : 1.0; \
643 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
644 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
645 GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
646 GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
647 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
648 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
649 GLfixed sf = s_fix & FIXED_FRAC_MASK; \
650 GLfixed tf = t_fix & FIXED_FRAC_MASK; \
651 GLfixed si = FIXED_FRAC_MASK - sf; \
652 GLfixed ti = FIXED_FRAC_MASK - tf; \
653 GLint pos = (t << info->twidth_log2) + s; \
654 const GLchan *tex00 = info->texture + COMP * pos; \
655 const GLchan *tex10 = tex00 + info->tbytesline; \
656 const GLchan *tex01 = tex00 + COMP; \
657 const GLchan *tex11 = tex10 + COMP; \
660 if (t == info->tmask) { \
661 tex10 -= info->tsize; \
662 tex11 -= info->tsize; \
664 if (s == info->smask) { \
665 tex01 -= info->tbytesline; \
666 tex11 -= info->tbytesline; \
669 span->red += span->redStep; \
670 span->green += span->greenStep; \
671 span->blue += span->blueStep; \
672 span->alpha += span->alphaStep; \
673 tex_coord[0] += tex_step[0]; \
674 tex_coord[1] += tex_step[1]; \
675 tex_coord[2] += tex_step[2]; \
680 GLfloat tex_coord
[3], tex_step
[3];
681 GLchan
*dest
= span
->array
->rgba
[0];
683 tex_coord
[0] = span
->tex
[0][0] * (info
->smask
+ 1);
684 tex_step
[0] = span
->texStepX
[0][0] * (info
->smask
+ 1);
685 tex_coord
[1] = span
->tex
[0][1] * (info
->tmask
+ 1);
686 tex_step
[1] = span
->texStepX
[0][1] * (info
->tmask
+ 1);
687 /* span->tex[0][2] only if 3D-texturing, here only 2D */
688 tex_coord
[2] = span
->tex
[0][3];
689 tex_step
[2] = span
->texStepX
[0][3];
691 switch (info
->filter
) {
693 switch (info
->format
) {
695 switch (info
->envmode
) {
697 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
701 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
704 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
707 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
710 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
715 switch(info
->envmode
) {
717 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
720 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
723 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
726 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
729 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
732 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
740 switch (info
->format
) {
742 switch (info
->envmode
) {
744 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
748 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
751 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
754 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
757 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
762 switch (info
->envmode
) {
764 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
767 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
770 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
773 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
776 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
779 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
787 ASSERT(span
->arrayMask
& SPAN_RGBA
);
788 _mesa_write_rgba_span(ctx
, span
);
796 * Render an perspective corrected RGB/RGBA textured triangle.
797 * The Q (aka V in Mesa) coordinate must be zero such that the divide
798 * by interpolated Q/W comes out right.
801 #define NAME persp_textured_triangle
804 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
806 #define INTERP_ALPHA 1
810 struct persp_info info; \
811 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
812 const struct gl_texture_object *obj = unit->Current2D; \
813 const GLint b = obj->BaseLevel; \
814 info.texture = (const GLchan *) obj->Image[b]->Data; \
815 info.twidth_log2 = obj->Image[b]->WidthLog2; \
816 info.smask = obj->Image[b]->Width - 1; \
817 info.tmask = obj->Image[b]->Height - 1; \
818 info.format = obj->Image[b]->Format; \
819 info.filter = obj->MinFilter; \
820 info.envmode = unit->EnvMode; \
822 if (info.envmode == GL_BLEND) { \
823 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
824 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
825 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
826 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
827 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
829 if (!info.texture) { \
830 /* this shouldn't happen */ \
834 switch (info.format) { \
838 info.tbytesline = obj->Image[b]->Width; \
840 case GL_LUMINANCE_ALPHA: \
841 info.tbytesline = obj->Image[b]->Width * 2; \
844 info.tbytesline = obj->Image[b]->Width * 3; \
847 info.tbytesline = obj->Image[b]->Width * 4; \
850 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
853 info.tsize = obj->Image[b]->Height * info.tbytesline;
855 #define RENDER_SPAN( span ) \
856 span.interpMask &= ~SPAN_RGBA; \
857 span.arrayMask |= SPAN_RGBA; \
858 fast_persp_span(ctx, &span, &info);
860 #include "s_tritemp.h"
863 #endif /* CHAN_BITS != GL_FLOAT */
869 * Render a smooth-shaded, textured, RGBA triangle.
870 * Interpolate S,T,R with perspective correction, w/out mipmapping.
872 #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 ) _mesa_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
893 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
895 #define INTERP_ALPHA 1
896 #define INTERP_SPEC 1
897 #define INTERP_MULTITEX 1
898 #define RENDER_SPAN( span ) _mesa_write_texture_span(ctx, &span);
899 #include "s_tritemp.h"
903 #define NAME occlusion_zless_triangle
904 #define DO_OCCLUSION_TEST
906 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
908 if (ctx->OcclusionResult) { \
911 #define RENDER_SPAN( span ) \
913 for (i = 0; i < span.end; i++) { \
914 GLdepth z = FixedToDepth(span.z); \
916 ctx->OcclusionResult = GL_TRUE; \
919 span.z += span.zStep; \
921 #include "s_tritemp.h"
926 nodraw_triangle( GLcontext
*ctx
,
931 (void) (ctx
&& v0
&& v1
&& v2
);
936 * This is used when separate specular color is enabled, but not
937 * texturing. We add the specular color to the primary color,
938 * draw the triangle, then restore the original primary color.
939 * Inefficient, but seldom needed.
941 void _swrast_add_spec_terms_triangle( GLcontext
*ctx
,
946 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
947 SWvertex
*ncv1
= (SWvertex
*)v1
;
948 SWvertex
*ncv2
= (SWvertex
*)v2
;
949 #if CHAN_TYPE == GL_FLOAT
950 GLfloat rSum
, gSum
, bSum
;
952 GLint rSum
, gSum
, bSum
;
955 /* save original colors */
956 COPY_CHAN4( c
[0], ncv0
->color
);
957 COPY_CHAN4( c
[1], ncv1
->color
);
958 COPY_CHAN4( c
[2], ncv2
->color
);
960 rSum
= ncv0
->color
[0] + ncv0
->specular
[0];
961 gSum
= ncv0
->color
[1] + ncv0
->specular
[1];
962 bSum
= ncv0
->color
[2] + ncv0
->specular
[2];
963 ncv0
->color
[0] = MIN2(rSum
, CHAN_MAX
);
964 ncv0
->color
[1] = MIN2(gSum
, CHAN_MAX
);
965 ncv0
->color
[2] = MIN2(bSum
, CHAN_MAX
);
967 rSum
= ncv1
->color
[0] + ncv1
->specular
[0];
968 gSum
= ncv1
->color
[1] + ncv1
->specular
[1];
969 bSum
= ncv1
->color
[2] + ncv1
->specular
[2];
970 ncv1
->color
[0] = MIN2(rSum
, CHAN_MAX
);
971 ncv1
->color
[1] = MIN2(gSum
, CHAN_MAX
);
972 ncv1
->color
[2] = MIN2(bSum
, CHAN_MAX
);
974 rSum
= ncv2
->color
[0] + ncv2
->specular
[0];
975 gSum
= ncv2
->color
[1] + ncv2
->specular
[1];
976 bSum
= ncv2
->color
[2] + ncv2
->specular
[2];
977 ncv2
->color
[0] = MIN2(rSum
, CHAN_MAX
);
978 ncv2
->color
[1] = MIN2(gSum
, CHAN_MAX
);
979 ncv2
->color
[2] = MIN2(bSum
, CHAN_MAX
);
981 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
982 /* restore original colors */
983 COPY_CHAN4( ncv0
->color
, c
[0] );
984 COPY_CHAN4( ncv1
->color
, c
[1] );
985 COPY_CHAN4( ncv2
->color
, c
[2] );
992 /* record the current triangle function name */
993 const char *_mesa_triFuncName
= NULL
;
995 #define USE(triFunc) \
997 _mesa_triFuncName = #triFunc; \
998 /*printf("%s\n", _mesa_triFuncName);*/ \
999 swrast->Triangle = triFunc; \
1004 #define USE(triFunc) swrast->Triangle = triFunc;
1012 * Determine which triangle rendering function to use given the current
1013 * rendering context.
1015 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1016 * remove tests to this code.
1019 _swrast_choose_triangle( GLcontext
*ctx
)
1021 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1022 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
1024 if (ctx
->Polygon
.CullFlag
&&
1025 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1026 USE(nodraw_triangle
);
1030 if (ctx
->RenderMode
==GL_RENDER
) {
1032 if (ctx
->Polygon
.SmoothFlag
) {
1033 _mesa_set_aa_triangle_function(ctx
);
1034 ASSERT(swrast
->Triangle
);
1038 if (ctx
->Depth
.OcclusionTest
&&
1040 ctx
->Depth
.Mask
== GL_FALSE
&&
1041 ctx
->Depth
.Func
== GL_LESS
&&
1042 !ctx
->Stencil
.Enabled
) {
1044 ctx
->Color
.ColorMask
[0] == 0 &&
1045 ctx
->Color
.ColorMask
[1] == 0 &&
1046 ctx
->Color
.ColorMask
[2] == 0 &&
1047 ctx
->Color
.ColorMask
[3] == 0)
1049 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1050 USE(occlusion_zless_triangle
);
1055 if (ctx
->Texture
._EnabledUnits
) {
1056 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1057 const struct gl_texture_object
*texObj2D
;
1058 const struct gl_texture_image
*texImg
;
1059 GLenum minFilter
, magFilter
, envMode
;
1061 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1062 texImg
= texObj2D
? texObj2D
->Image
[texObj2D
->BaseLevel
] : NULL
;
1063 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1064 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1065 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1066 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1068 /* First see if we can use an optimized 2-D texture function */
1069 if (ctx
->Texture
._EnabledUnits
== 1
1070 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1071 && texObj2D
->WrapS
==GL_REPEAT
1072 && texObj2D
->WrapT
==GL_REPEAT
1073 && texImg
->Border
==0
1074 && texImg
->Width
== texImg
->RowStride
1075 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1076 && minFilter
== magFilter
1077 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1078 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1079 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1080 if (minFilter
== GL_NEAREST
1081 && format
== MESA_FORMAT_RGB
1082 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1083 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1084 && ctx
->Depth
.Func
== GL_LESS
1085 && ctx
->Depth
.Mask
== GL_TRUE
)
1086 || swrast
->_RasterMask
== TEXTURE_BIT
)
1087 && ctx
->Polygon
.StippleFlag
== GL_FALSE
) {
1088 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1089 USE(simple_z_textured_triangle
);
1092 USE(simple_textured_triangle
);
1096 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1097 USE(general_textured_triangle
);
1099 USE(affine_textured_triangle
);
1104 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1105 USE(general_textured_triangle
);
1107 USE(persp_textured_triangle
);
1112 /* general case textured triangles */
1113 if (ctx
->Texture
._EnabledUnits
> 1) {
1114 USE(multitextured_triangle
);
1117 USE(general_textured_triangle
);
1122 ASSERT(!ctx
->Texture
._EnabledUnits
);
1123 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1124 /* smooth shaded, no texturing, stippled or some raster ops */
1126 USE(smooth_rgba_triangle
);
1129 USE(smooth_ci_triangle
);
1133 /* flat shaded, no texturing, stippled or some raster ops */
1135 USE(flat_rgba_triangle
);
1138 USE(flat_ci_triangle
);
1143 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1144 USE(_mesa_feedback_triangle
);
1147 /* GL_SELECT mode */
1148 USE(_mesa_select_triangle
);