2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * When the device driver doesn't implement triangle rasterization it
28 * can hook in _swrast_Triangle, which eventually calls one of these
29 * functions to draw triangles.
37 #include "texformat.h"
41 #include "s_aatriangle.h"
42 #include "s_context.h"
44 #include "s_feedback.h"
46 #include "s_triangle.h"
50 * Just used for feedback mode.
52 GLboolean
_swrast_culltriangle( GLcontext
*ctx
,
57 GLfloat ex
= v1
->win
[0] - v0
->win
[0];
58 GLfloat ey
= v1
->win
[1] - v0
->win
[1];
59 GLfloat fx
= v2
->win
[0] - v0
->win
[0];
60 GLfloat fy
= v2
->win
[1] - v0
->win
[1];
61 GLfloat c
= ex
*fy
-ey
*fx
;
63 if (c
* SWRAST_CONTEXT(ctx
)->_BackfaceSign
> 0)
72 * Render a flat-shaded color index triangle.
74 #define NAME flat_ci_triangle
78 span.interpMask |= SPAN_INDEX; \
79 span.index = IntToFixed(v2->index); \
81 #define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
82 #include "s_tritemp.h"
87 * Render a smooth-shaded color index triangle.
89 #define NAME smooth_ci_triangle
92 #define INTERP_INDEX 1
93 #define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
94 #include "s_tritemp.h"
99 * Render a flat-shaded RGBA triangle.
101 #define NAME flat_rgba_triangle
104 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
106 ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
107 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
108 span.interpMask |= SPAN_RGBA; \
109 span.red = ChanToFixed(v2->color[0]); \
110 span.green = ChanToFixed(v2->color[1]); \
111 span.blue = ChanToFixed(v2->color[2]); \
112 span.alpha = ChanToFixed(v2->color[3]); \
114 span.greenStep = 0; \
117 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
118 #include "s_tritemp.h"
123 * Render a smooth-shaded RGBA triangle.
125 #define NAME smooth_rgba_triangle
128 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
130 #define INTERP_ALPHA 1
133 /* texturing must be off */ \
134 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \
135 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
137 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
138 #include "s_tritemp.h"
143 * Render an RGB, GL_DECAL, textured triangle.
144 * Interpolate S,T only w/out mipmapping or perspective correction.
148 #define NAME simple_textured_triangle
149 #define INTERP_INT_TEX 1
150 #define S_SCALE twidth
151 #define T_SCALE theight
154 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
155 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
156 const GLint b = obj->BaseLevel; \
157 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
158 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
159 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
160 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
161 const GLint smask = obj->Image[0][b]->Width - 1; \
162 const GLint tmask = obj->Image[0][b]->Height - 1; \
164 /* this shouldn't happen */ \
168 #define RENDER_SPAN( span ) \
170 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
171 span.intTex[1] -= FIXED_HALF; \
172 for (i = 0; i < span.end; i++) { \
173 GLint s = FixedToInt(span.intTex[0]) & smask; \
174 GLint t = FixedToInt(span.intTex[1]) & tmask; \
175 GLint pos = (t << twidth_log2) + s; \
176 pos = pos + pos + pos; /* multiply by 3 */ \
177 span.array->rgb[i][RCOMP] = texture[pos]; \
178 span.array->rgb[i][GCOMP] = texture[pos+1]; \
179 span.array->rgb[i][BCOMP] = texture[pos+2]; \
180 span.intTex[0] += span.intTexStep[0]; \
181 span.intTex[1] += span.intTexStep[1]; \
183 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
184 (CONST GLchan (*)[3]) span.array->rgb,\
186 #include "s_tritemp.h"
191 * Render an RGB, GL_DECAL, textured triangle.
192 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
193 * perspective correction.
197 #define NAME simple_z_textured_triangle
199 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
200 #define INTERP_INT_TEX 1
201 #define S_SCALE twidth
202 #define T_SCALE theight
205 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
206 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
207 const GLint b = obj->BaseLevel; \
208 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
209 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
210 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
211 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
212 const GLint smask = obj->Image[0][b]->Width - 1; \
213 const GLint tmask = obj->Image[0][b]->Height - 1; \
215 /* this shouldn't happen */ \
219 #define RENDER_SPAN( span ) \
221 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
222 span.intTex[1] -= FIXED_HALF; \
223 for (i = 0; i < span.end; i++) { \
224 const GLdepth z = FixedToDepth(span.z); \
226 GLint s = FixedToInt(span.intTex[0]) & smask; \
227 GLint t = FixedToInt(span.intTex[1]) & tmask; \
228 GLint pos = (t << twidth_log2) + s; \
229 pos = pos + pos + pos; /* multiply by 3 */ \
230 span.array->rgb[i][RCOMP] = texture[pos]; \
231 span.array->rgb[i][GCOMP] = texture[pos+1]; \
232 span.array->rgb[i][BCOMP] = texture[pos+2]; \
234 span.array->mask[i] = 1; \
237 span.array->mask[i] = 0; \
239 span.intTex[0] += span.intTexStep[0]; \
240 span.intTex[1] += span.intTexStep[1]; \
241 span.z += span.zStep; \
243 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
244 (CONST GLchan (*)[3]) span.array->rgb,\
246 #include "s_tritemp.h"
250 #if CHAN_TYPE != GL_FLOAT
259 const GLchan
*texture
;
260 GLfixed er
, eg
, eb
, ea
;
261 GLint tbytesline
, tsize
;
265 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
266 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
270 affine_span(GLcontext
*ctx
, struct sw_span
*span
,
271 struct affine_info
*info
)
273 GLchan sample
[4]; /* the filtered texture sample */
275 /* Instead of defining a function for each mode, a test is done
276 * between the outer and inner loops. This is to reduce code size
277 * and complexity. Observe that an optimizing compiler kills
278 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
281 #define NEAREST_RGB \
282 sample[RCOMP] = tex00[RCOMP]; \
283 sample[GCOMP] = tex00[GCOMP]; \
284 sample[BCOMP] = tex00[BCOMP]; \
285 sample[ACOMP] = CHAN_MAX
288 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
289 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
290 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
291 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
292 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
293 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
294 sample[ACOMP] = CHAN_MAX
296 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
298 #define LINEAR_RGBA \
299 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
300 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT;\
301 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
302 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT;\
303 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
304 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT;\
305 sample[ACOMP] = (ti * (si * tex00[3] + sf * tex01[3]) + \
306 tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT
309 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
310 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
311 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
312 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
315 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
316 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
317 >> (FIXED_SHIFT + 8); \
318 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
319 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
320 >> (FIXED_SHIFT + 8); \
321 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
322 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
323 >> (FIXED_SHIFT + 8); \
324 dest[ACOMP] = FixedToInt(span->alpha)
327 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
328 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
329 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
330 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
331 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
332 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
333 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
335 #define REPLACE COPY_CHAN4(dest, sample)
339 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
340 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
341 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
342 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
343 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
344 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
345 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
350 #define NEAREST_RGB_REPLACE \
352 dest[0] = sample[0]; \
353 dest[1] = sample[1]; \
354 dest[2] = sample[2]; \
355 dest[3] = FixedToInt(span->alpha);
357 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
359 #define SPAN_NEAREST(DO_TEX,COMP) \
360 for (i = 0; i < span->end; i++) { \
361 /* Isn't it necessary to use FixedFloor below?? */ \
362 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
363 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
364 GLint pos = (t << info->twidth_log2) + s; \
365 const GLchan *tex00 = info->texture + COMP * pos; \
367 span->red += span->redStep; \
368 span->green += span->greenStep; \
369 span->blue += span->blueStep; \
370 span->alpha += span->alphaStep; \
371 span->intTex[0] += span->intTexStep[0]; \
372 span->intTex[1] += span->intTexStep[1]; \
376 #define SPAN_LINEAR(DO_TEX,COMP) \
377 for (i = 0; i < span->end; i++) { \
378 /* Isn't it necessary to use FixedFloor below?? */ \
379 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
380 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
381 GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
382 GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
383 GLfixed si = FIXED_FRAC_MASK - sf; \
384 GLfixed ti = FIXED_FRAC_MASK - tf; \
385 GLint pos = (t << info->twidth_log2) + s; \
386 const GLchan *tex00 = info->texture + COMP * pos; \
387 const GLchan *tex10 = tex00 + info->tbytesline; \
388 const GLchan *tex01 = tex00 + COMP; \
389 const GLchan *tex11 = tex10 + COMP; \
392 if (t == info->tmask) { \
393 tex10 -= info->tsize; \
394 tex11 -= info->tsize; \
396 if (s == info->smask) { \
397 tex01 -= info->tbytesline; \
398 tex11 -= info->tbytesline; \
401 span->red += span->redStep; \
402 span->green += span->greenStep; \
403 span->blue += span->blueStep; \
404 span->alpha += span->alphaStep; \
405 span->intTex[0] += span->intTexStep[0]; \
406 span->intTex[1] += span->intTexStep[1]; \
412 GLchan
*dest
= span
->array
->rgba
[0];
414 span
->intTex
[0] -= FIXED_HALF
;
415 span
->intTex
[1] -= FIXED_HALF
;
416 switch (info
->filter
) {
418 switch (info
->format
) {
420 switch (info
->envmode
) {
422 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
426 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
429 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
432 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
435 _mesa_problem(ctx
, "bad tex env mode in SPAN_LINEAR");
440 switch(info
->envmode
) {
442 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
445 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
448 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
451 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
454 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
457 _mesa_problem(ctx
, "bad tex env mode (2) in SPAN_LINEAR");
465 span
->intTex
[0] -= FIXED_HALF
;
466 span
->intTex
[1] -= FIXED_HALF
;
467 switch (info
->format
) {
469 switch (info
->envmode
) {
471 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
475 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
478 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
481 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
484 _mesa_problem(ctx
, "bad tex env mode (3) in SPAN_LINEAR");
489 switch (info
->envmode
) {
491 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
494 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
497 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
500 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
503 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
506 _mesa_problem(ctx
, "bad tex env mode (4) in SPAN_LINEAR");
513 span
->interpMask
&= ~SPAN_RGBA
;
514 ASSERT(span
->arrayMask
& SPAN_RGBA
);
515 _swrast_write_rgba_span(ctx
, span
);
524 * Render an RGB/RGBA textured triangle without perspective correction.
526 #define NAME affine_textured_triangle
529 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
531 #define INTERP_ALPHA 1
532 #define INTERP_INT_TEX 1
533 #define S_SCALE twidth
534 #define T_SCALE theight
537 struct affine_info info; \
538 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
539 struct gl_texture_object *obj = unit->Current2D; \
540 const GLint b = obj->BaseLevel; \
541 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
542 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
543 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
544 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
545 info.smask = obj->Image[0][b]->Width - 1; \
546 info.tmask = obj->Image[0][b]->Height - 1; \
547 info.format = obj->Image[0][b]->Format; \
548 info.filter = obj->MinFilter; \
549 info.envmode = unit->EnvMode; \
550 span.arrayMask |= SPAN_RGBA; \
552 if (info.envmode == GL_BLEND) { \
553 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
554 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
555 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
556 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
557 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
559 if (!info.texture) { \
560 /* this shouldn't happen */ \
564 switch (info.format) { \
568 info.tbytesline = obj->Image[0][b]->Width; \
570 case GL_LUMINANCE_ALPHA: \
571 info.tbytesline = obj->Image[0][b]->Width * 2; \
574 info.tbytesline = obj->Image[0][b]->Width * 3; \
577 info.tbytesline = obj->Image[0][b]->Width * 4; \
580 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
583 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
585 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
587 #include "s_tritemp.h"
598 const GLchan
*texture
;
599 GLfixed er
, eg
, eb
, ea
; /* texture env color */
600 GLint tbytesline
, tsize
;
605 fast_persp_span(GLcontext
*ctx
, struct sw_span
*span
,
606 struct persp_info
*info
)
608 GLchan sample
[4]; /* the filtered texture sample */
610 /* Instead of defining a function for each mode, a test is done
611 * between the outer and inner loops. This is to reduce code size
612 * and complexity. Observe that an optimizing compiler kills
613 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
615 #define SPAN_NEAREST(DO_TEX,COMP) \
616 for (i = 0; i < span->end; i++) { \
617 GLdouble invQ = tex_coord[2] ? \
618 (1.0 / tex_coord[2]) : 1.0; \
619 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
620 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
621 GLint s = IFLOOR(s_tmp) & info->smask; \
622 GLint t = IFLOOR(t_tmp) & info->tmask; \
623 GLint pos = (t << info->twidth_log2) + s; \
624 const GLchan *tex00 = info->texture + COMP * pos; \
626 span->red += span->redStep; \
627 span->green += span->greenStep; \
628 span->blue += span->blueStep; \
629 span->alpha += span->alphaStep; \
630 tex_coord[0] += tex_step[0]; \
631 tex_coord[1] += tex_step[1]; \
632 tex_coord[2] += tex_step[2]; \
636 #define SPAN_LINEAR(DO_TEX,COMP) \
637 for (i = 0; i < span->end; i++) { \
638 GLdouble invQ = tex_coord[2] ? \
639 (1.0 / tex_coord[2]) : 1.0; \
640 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
641 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
642 GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
643 GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
644 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
645 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
646 GLfixed sf = s_fix & FIXED_FRAC_MASK; \
647 GLfixed tf = t_fix & FIXED_FRAC_MASK; \
648 GLfixed si = FIXED_FRAC_MASK - sf; \
649 GLfixed ti = FIXED_FRAC_MASK - tf; \
650 GLint pos = (t << info->twidth_log2) + s; \
651 const GLchan *tex00 = info->texture + COMP * pos; \
652 const GLchan *tex10 = tex00 + info->tbytesline; \
653 const GLchan *tex01 = tex00 + COMP; \
654 const GLchan *tex11 = tex10 + COMP; \
657 if (t == info->tmask) { \
658 tex10 -= info->tsize; \
659 tex11 -= info->tsize; \
661 if (s == info->smask) { \
662 tex01 -= info->tbytesline; \
663 tex11 -= info->tbytesline; \
666 span->red += span->redStep; \
667 span->green += span->greenStep; \
668 span->blue += span->blueStep; \
669 span->alpha += span->alphaStep; \
670 tex_coord[0] += tex_step[0]; \
671 tex_coord[1] += tex_step[1]; \
672 tex_coord[2] += tex_step[2]; \
677 GLfloat tex_coord
[3], tex_step
[3];
678 GLchan
*dest
= span
->array
->rgba
[0];
680 tex_coord
[0] = span
->tex
[0][0] * (info
->smask
+ 1);
681 tex_step
[0] = span
->texStepX
[0][0] * (info
->smask
+ 1);
682 tex_coord
[1] = span
->tex
[0][1] * (info
->tmask
+ 1);
683 tex_step
[1] = span
->texStepX
[0][1] * (info
->tmask
+ 1);
684 /* span->tex[0][2] only if 3D-texturing, here only 2D */
685 tex_coord
[2] = span
->tex
[0][3];
686 tex_step
[2] = span
->texStepX
[0][3];
688 switch (info
->filter
) {
690 switch (info
->format
) {
692 switch (info
->envmode
) {
694 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
698 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
701 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
704 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
707 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
712 switch(info
->envmode
) {
714 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
717 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
720 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
723 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
726 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
729 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
737 switch (info
->format
) {
739 switch (info
->envmode
) {
741 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
745 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
748 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
751 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
754 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
759 switch (info
->envmode
) {
761 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
764 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
767 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
770 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
773 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
776 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
784 ASSERT(span
->arrayMask
& SPAN_RGBA
);
785 _swrast_write_rgba_span(ctx
, span
);
793 * Render an perspective corrected RGB/RGBA textured triangle.
794 * The Q (aka V in Mesa) coordinate must be zero such that the divide
795 * by interpolated Q/W comes out right.
798 #define NAME persp_textured_triangle
801 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
803 #define INTERP_ALPHA 1
807 struct persp_info info; \
808 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
809 const struct gl_texture_object *obj = unit->Current2D; \
810 const GLint b = obj->BaseLevel; \
811 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
812 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
813 info.smask = obj->Image[0][b]->Width - 1; \
814 info.tmask = obj->Image[0][b]->Height - 1; \
815 info.format = obj->Image[0][b]->Format; \
816 info.filter = obj->MinFilter; \
817 info.envmode = unit->EnvMode; \
819 if (info.envmode == GL_BLEND) { \
820 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
821 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
822 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
823 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
824 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
826 if (!info.texture) { \
827 /* this shouldn't happen */ \
831 switch (info.format) { \
835 info.tbytesline = obj->Image[0][b]->Width; \
837 case GL_LUMINANCE_ALPHA: \
838 info.tbytesline = obj->Image[0][b]->Width * 2; \
841 info.tbytesline = obj->Image[0][b]->Width * 3; \
844 info.tbytesline = obj->Image[0][b]->Width * 4; \
847 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
850 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
852 #define RENDER_SPAN( span ) \
853 span.interpMask &= ~SPAN_RGBA; \
854 span.arrayMask |= SPAN_RGBA; \
855 fast_persp_span(ctx, &span, &info);
857 #include "s_tritemp.h"
860 #endif /* CHAN_BITS != GL_FLOAT */
866 * Render a smooth-shaded, textured, RGBA triangle.
867 * Interpolate S,T,R with perspective correction, w/out mipmapping.
869 #define NAME general_textured_triangle
873 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
875 #define INTERP_SPEC 1
876 #define INTERP_ALPHA 1
878 #define RENDER_SPAN( span ) _swrast_write_texture_span(ctx, &span);
879 #include "s_tritemp.h"
884 * This is the big one!
885 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates.
888 #define NAME multitextured_triangle
892 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
894 #define INTERP_ALPHA 1
895 #define INTERP_SPEC 1
896 #define INTERP_MULTITEX 1
897 #define RENDER_SPAN( span ) _swrast_write_texture_span(ctx, &span);
898 #include "s_tritemp.h"
903 * Special tri function for occlusion testing
905 #define NAME occlusion_zless_triangle
906 #define DO_OCCLUSION_TEST
908 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
910 if (ctx->OcclusionResult && !ctx->Occlusion.Active) { \
913 #define RENDER_SPAN( span ) \
915 for (i = 0; i < span.end; i++) { \
916 GLdepth z = FixedToDepth(span.z); \
918 ctx->OcclusionResult = GL_TRUE; \
919 ctx->Occlusion.PassedCounter++; \
921 span.z += span.zStep; \
923 #include "s_tritemp.h"
928 nodraw_triangle( GLcontext
*ctx
,
933 (void) (ctx
&& v0
&& v1
&& v2
);
938 * This is used when separate specular color is enabled, but not
939 * texturing. We add the specular color to the primary color,
940 * draw the triangle, then restore the original primary color.
941 * Inefficient, but seldom needed.
943 void _swrast_add_spec_terms_triangle( GLcontext
*ctx
,
948 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
949 SWvertex
*ncv1
= (SWvertex
*)v1
;
950 SWvertex
*ncv2
= (SWvertex
*)v2
;
951 #if CHAN_TYPE == GL_FLOAT
952 GLfloat rSum
, gSum
, bSum
;
954 GLint rSum
, gSum
, bSum
;
957 /* save original colors */
958 COPY_CHAN4( c
[0], ncv0
->color
);
959 COPY_CHAN4( c
[1], ncv1
->color
);
960 COPY_CHAN4( c
[2], ncv2
->color
);
962 rSum
= ncv0
->color
[0] + ncv0
->specular
[0];
963 gSum
= ncv0
->color
[1] + ncv0
->specular
[1];
964 bSum
= ncv0
->color
[2] + ncv0
->specular
[2];
965 ncv0
->color
[0] = MIN2(rSum
, CHAN_MAX
);
966 ncv0
->color
[1] = MIN2(gSum
, CHAN_MAX
);
967 ncv0
->color
[2] = MIN2(bSum
, CHAN_MAX
);
969 rSum
= ncv1
->color
[0] + ncv1
->specular
[0];
970 gSum
= ncv1
->color
[1] + ncv1
->specular
[1];
971 bSum
= ncv1
->color
[2] + ncv1
->specular
[2];
972 ncv1
->color
[0] = MIN2(rSum
, CHAN_MAX
);
973 ncv1
->color
[1] = MIN2(gSum
, CHAN_MAX
);
974 ncv1
->color
[2] = MIN2(bSum
, CHAN_MAX
);
976 rSum
= ncv2
->color
[0] + ncv2
->specular
[0];
977 gSum
= ncv2
->color
[1] + ncv2
->specular
[1];
978 bSum
= ncv2
->color
[2] + ncv2
->specular
[2];
979 ncv2
->color
[0] = MIN2(rSum
, CHAN_MAX
);
980 ncv2
->color
[1] = MIN2(gSum
, CHAN_MAX
);
981 ncv2
->color
[2] = MIN2(bSum
, CHAN_MAX
);
983 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
984 /* restore original colors */
985 COPY_CHAN4( ncv0
->color
, c
[0] );
986 COPY_CHAN4( ncv1
->color
, c
[1] );
987 COPY_CHAN4( ncv2
->color
, c
[2] );
994 /* record the current triangle function name */
995 const char *_mesa_triFuncName
= NULL
;
997 #define USE(triFunc) \
999 _mesa_triFuncName = #triFunc; \
1000 /*printf("%s\n", _mesa_triFuncName);*/ \
1001 swrast->Triangle = triFunc; \
1006 #define USE(triFunc) swrast->Triangle = triFunc;
1014 * Determine which triangle rendering function to use given the current
1015 * rendering context.
1017 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1018 * remove tests to this code.
1021 _swrast_choose_triangle( GLcontext
*ctx
)
1023 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1024 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
1026 if (ctx
->Polygon
.CullFlag
&&
1027 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1028 USE(nodraw_triangle
);
1032 if (ctx
->RenderMode
==GL_RENDER
) {
1034 if (ctx
->Polygon
.SmoothFlag
) {
1035 _swrast_set_aa_triangle_function(ctx
);
1036 ASSERT(swrast
->Triangle
);
1040 /* special case for occlusion testing */
1041 if ((ctx
->Depth
.OcclusionTest
|| ctx
->Occlusion
.Active
) &&
1043 ctx
->Depth
.Mask
== GL_FALSE
&&
1044 ctx
->Depth
.Func
== GL_LESS
&&
1045 !ctx
->Stencil
.Enabled
) {
1047 ctx
->Color
.ColorMask
[0] == 0 &&
1048 ctx
->Color
.ColorMask
[1] == 0 &&
1049 ctx
->Color
.ColorMask
[2] == 0 &&
1050 ctx
->Color
.ColorMask
[3] == 0)
1052 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1053 USE(occlusion_zless_triangle
);
1058 if (ctx
->Texture
._EnabledCoordUnits
|| ctx
->FragmentProgram
.Enabled
) {
1059 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1060 const struct gl_texture_object
*texObj2D
;
1061 const struct gl_texture_image
*texImg
;
1062 GLenum minFilter
, magFilter
, envMode
;
1064 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1065 texImg
= texObj2D
? texObj2D
->Image
[0][texObj2D
->BaseLevel
] : NULL
;
1066 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1067 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1068 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1069 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1071 /* First see if we can use an optimized 2-D texture function */
1072 if (ctx
->Texture
._EnabledCoordUnits
== 1
1073 && !ctx
->FragmentProgram
.Enabled
1074 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1075 && texObj2D
->WrapS
==GL_REPEAT
1076 && texObj2D
->WrapT
==GL_REPEAT
1077 && texObj2D
->_IsPowerOfTwo
1078 && texImg
->Border
==0
1079 && texImg
->Width
== texImg
->RowStride
1080 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1081 && minFilter
== magFilter
1082 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1083 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1084 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1085 if (minFilter
== GL_NEAREST
1086 && format
== MESA_FORMAT_RGB
1087 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1088 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1089 && ctx
->Depth
.Func
== GL_LESS
1090 && ctx
->Depth
.Mask
== GL_TRUE
)
1091 || swrast
->_RasterMask
== TEXTURE_BIT
)
1092 && ctx
->Polygon
.StippleFlag
== GL_FALSE
) {
1093 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1094 USE(simple_z_textured_triangle
);
1097 USE(simple_textured_triangle
);
1101 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1102 USE(general_textured_triangle
);
1104 USE(affine_textured_triangle
);
1109 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1110 USE(general_textured_triangle
);
1112 USE(persp_textured_triangle
);
1117 /* general case textured triangles */
1118 if (ctx
->Texture
._EnabledCoordUnits
> 1) {
1119 USE(multitextured_triangle
);
1122 USE(general_textured_triangle
);
1127 ASSERT(!ctx
->Texture
._EnabledCoordUnits
);
1128 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1129 /* smooth shaded, no texturing, stippled or some raster ops */
1131 USE(smooth_rgba_triangle
);
1134 USE(smooth_ci_triangle
);
1138 /* flat shaded, no texturing, stippled or some raster ops */
1140 USE(flat_rgba_triangle
);
1143 USE(flat_ci_triangle
);
1148 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1149 USE(_swrast_feedback_triangle
);
1152 /* GL_SELECT mode */
1153 USE(_swrast_select_triangle
);