1 /* $Id: s_triangle.c,v 1.36 2001/07/26 15:57:49 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 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"
50 #include "s_trispan.h"
54 GLboolean
_mesa_cull_triangle( 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 static void flat_ci_triangle( GLcontext
*ctx
,
84 #define RENDER_SPAN( span ) \
85 GLdepth zSpan[MAX_WIDTH]; \
86 GLfloat fogSpan[MAX_WIDTH]; \
88 for (i = 0; i < span.count; i++) { \
89 zSpan[i] = FixedToDepth(span.z); \
90 span.z += span.zStep; \
91 fogSpan[i] = span.fog; \
92 span.fog += span.fogStep; \
94 _mesa_write_monoindex_span(ctx, span.count, span.x, span.y, \
95 zSpan, fogSpan, v0->index, NULL, GL_POLYGON );
97 #include "s_tritemp.h"
103 * Render a smooth-shaded color index triangle.
105 static void smooth_ci_triangle( GLcontext
*ctx
,
112 #define INTERP_INDEX 1
114 #define RENDER_SPAN( span ) \
115 GLdepth zSpan[MAX_WIDTH]; \
116 GLfloat fogSpan[MAX_WIDTH]; \
117 GLuint indexSpan[MAX_WIDTH]; \
119 for (i = 0; i < span.count; i++) { \
120 zSpan[i] = FixedToDepth(span.z); \
121 span.z += span.zStep; \
122 indexSpan[i] = FixedToInt(span.index); \
123 span.index += span.indexStep; \
124 fogSpan[i] = span.fog; \
125 span.fog += span.fogStep; \
127 _mesa_write_index_span(ctx, span.count, span.x, span.y, \
128 zSpan, fogSpan, indexSpan, NULL, GL_POLYGON);
130 #include "s_tritemp.h"
136 * Render a flat-shaded RGBA triangle.
138 static void flat_rgba_triangle( GLcontext
*ctx
,
145 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
147 #define RENDER_SPAN( span ) \
148 GLdepth zSpan[MAX_WIDTH]; \
149 GLfloat fogSpan[MAX_WIDTH]; \
151 for (i = 0; i < span.count; i++) { \
152 zSpan[i] = FixedToDepth(span.z); \
153 span.z += span.zStep; \
154 fogSpan[i] = span.fog; \
155 span.fog += span.fogStep; \
157 _mesa_write_monocolor_span(ctx, span.count, span.x, span.y, zSpan, \
158 fogSpan, v2->color, NULL, GL_POLYGON );
160 #include "s_tritemp.h"
162 ASSERT(!ctx
->Texture
._ReallyEnabled
); /* texturing must be off */
163 ASSERT(ctx
->Light
.ShadeModel
==GL_FLAT
);
169 * Render a smooth-shaded RGBA triangle.
171 static void smooth_rgba_triangle( GLcontext
*ctx
,
179 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
181 #define INTERP_ALPHA 1
183 #define RENDER_SPAN( span ) \
184 GLdepth zSpan[MAX_WIDTH]; \
185 GLchan rgbaSpan[MAX_WIDTH][4]; \
186 GLfloat fogSpan[MAX_WIDTH]; \
188 for (i = 0; i < span.count; i++) { \
189 rgbaSpan[i][RCOMP] = FixedToChan(span.red); \
190 rgbaSpan[i][GCOMP] = FixedToChan(span.green); \
191 rgbaSpan[i][BCOMP] = FixedToChan(span.blue); \
192 rgbaSpan[i][ACOMP] = FixedToChan(span.alpha); \
193 span.red += span.redStep; \
194 span.green += span.greenStep; \
195 span.blue += span.blueStep; \
196 span.alpha += span.alphaStep; \
197 zSpan[i] = FixedToDepth(span.z); \
198 span.z += span.zStep; \
199 fogSpan[i] = span.fog; \
200 span.fog += span.fogStep; \
202 _mesa_write_rgba_span(ctx, span.count, span.x, span.y, \
203 (CONST GLdepth *) zSpan, \
204 fogSpan, rgbaSpan, NULL, GL_POLYGON);
206 #include "s_tritemp.h"
208 ASSERT(!ctx
->Texture
._ReallyEnabled
); /* texturing must be off */
209 ASSERT(ctx
->Light
.ShadeModel
==GL_SMOOTH
);
214 * Render an RGB, GL_DECAL, textured triangle.
215 * Interpolate S,T only w/out mipmapping or perspective correction.
219 static void simple_textured_triangle( GLcontext
*ctx
,
224 #define INTERP_INT_TEX 1
225 #define S_SCALE twidth
226 #define T_SCALE theight
229 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
230 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
231 GLint b = obj->BaseLevel; \
232 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
233 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
234 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
235 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
236 const GLint smask = obj->Image[b]->Width - 1; \
237 const GLint tmask = obj->Image[b]->Height - 1; \
239 /* this shouldn't happen */ \
243 #define RENDER_SPAN( span ) \
244 GLchan rgbSpan[MAX_WIDTH][3]; \
246 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
247 span.intTex[1] -= FIXED_HALF; \
248 for (i = 0; i < span.count; i++) { \
249 GLint s = FixedToInt(span.intTex[0]) & smask; \
250 GLint t = FixedToInt(span.intTex[1]) & tmask; \
251 GLint pos = (t << twidth_log2) + s; \
252 pos = pos + pos + pos; /* multiply by 3 */ \
253 rgbSpan[i][RCOMP] = texture[pos]; \
254 rgbSpan[i][GCOMP] = texture[pos+1]; \
255 rgbSpan[i][BCOMP] = texture[pos+2]; \
256 span.intTex[0] += span.intTexStep[0]; \
257 span.intTex[1] += span.intTexStep[1]; \
259 (*swrast->Driver.WriteRGBSpan)(ctx, span.count, span.x, span.y, \
260 (CONST GLchan (*)[3]) rgbSpan, NULL );
262 #include "s_tritemp.h"
267 * Render an RGB, GL_DECAL, textured triangle.
268 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
269 * perspective correction.
273 static void simple_z_textured_triangle( GLcontext
*ctx
,
279 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
280 #define INTERP_INT_TEX 1
281 #define S_SCALE twidth
282 #define T_SCALE theight
285 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
286 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
287 GLint b = obj->BaseLevel; \
288 GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
289 GLfloat theight = (GLfloat) obj->Image[b]->Height; \
290 GLint twidth_log2 = obj->Image[b]->WidthLog2; \
291 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
292 GLint smask = obj->Image[b]->Width - 1; \
293 GLint tmask = obj->Image[b]->Height - 1; \
295 /* this shouldn't happen */ \
299 #define RENDER_SPAN( span ) \
300 GLchan rgbSpan[MAX_WIDTH][3]; \
301 GLubyte mask[MAX_WIDTH]; \
303 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
304 span.intTex[1] -= FIXED_HALF; \
305 for (i = 0; i < span.count; i++) { \
306 const GLdepth z = FixedToDepth(span.z); \
308 GLint s = FixedToInt(span.intTex[0]) & smask; \
309 GLint t = FixedToInt(span.intTex[1]) & tmask; \
310 GLint pos = (t << twidth_log2) + s; \
311 pos = pos + pos + pos; /* multiply by 3 */ \
312 rgbSpan[i][RCOMP] = texture[pos]; \
313 rgbSpan[i][GCOMP] = texture[pos+1]; \
314 rgbSpan[i][BCOMP] = texture[pos+2]; \
321 span.intTex[0] += span.intTexStep[0]; \
322 span.intTex[1] += span.intTexStep[1]; \
323 span.z += span.zStep; \
325 (*swrast->Driver.WriteRGBSpan)(ctx, span.count, span.x, span.y, \
326 (CONST GLchan (*)[3]) rgbSpan, mask );
328 #include "s_tritemp.h"
332 #if CHAN_TYPE != GL_FLOAT
341 const GLchan
*texture
;
342 GLchan er
, eg
, eb
, ea
;
343 GLint tbytesline
, tsize
;
344 GLint fixedToDepthShift
;
348 affine_span(GLcontext
*ctx
, struct triangle_span
*span
,
349 struct affine_info
*info
)
353 /* Instead of defining a function for each mode, a test is done
354 * between the outer and inner loops. This is to reduce code size
355 * and complexity. Observe that an optimizing compiler kills
356 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
359 #define NEAREST_RGB \
360 tmp_col[RCOMP] = tex00[RCOMP]; \
361 tmp_col[GCOMP] = tex00[GCOMP]; \
362 tmp_col[BCOMP] = tex00[BCOMP]; \
363 tmp_col[ACOMP] = CHAN_MAX
366 tmp_col[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
367 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
368 tmp_col[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
369 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
370 tmp_col[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
371 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
372 tmp_col[ACOMP] = CHAN_MAX
374 #define NEAREST_RGBA COPY_CHAN4(tmp_col, tex00)
376 #define LINEAR_RGBA \
377 tmp_col[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
378 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT;\
379 tmp_col[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
380 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT;\
381 tmp_col[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
382 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT;\
383 tmp_col[ACOMP] = (ti * (si * tex00[3] + sf * tex01[3]) + \
384 tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT
387 dest[RCOMP] = span->red * (tmp_col[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
388 dest[GCOMP] = span->green * (tmp_col[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
389 dest[BCOMP] = span->blue * (tmp_col[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
390 dest[ACOMP] = span->alpha * (tmp_col[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
393 dest[RCOMP] = ((CHAN_MAX - tmp_col[ACOMP]) * span->red + \
394 ((tmp_col[ACOMP] + 1) * tmp_col[RCOMP] << FIXED_SHIFT)) \
395 >> (FIXED_SHIFT + 8); \
396 dest[GCOMP] = ((CHAN_MAX - tmp_col[ACOMP]) * span->green + \
397 ((tmp_col[ACOMP] + 1) * tmp_col[GCOMP] << FIXED_SHIFT)) \
398 >> (FIXED_SHIFT + 8); \
399 dest[BCOMP] = ((CHAN_MAX - tmp_col[ACOMP]) * span->blue + \
400 ((tmp_col[ACOMP] + 1) * tmp_col[BCOMP] << FIXED_SHIFT)) \
401 >> (FIXED_SHIFT + 8); \
402 dest[ACOMP] = FixedToInt(span->alpha)
405 dest[RCOMP] = ((CHAN_MAX - tmp_col[RCOMP]) * span->red \
406 + (tmp_col[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
407 dest[GCOMP] = ((CHAN_MAX - tmp_col[GCOMP]) * span->green \
408 + (tmp_col[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
409 dest[BCOMP] = ((CHAN_MAX - tmp_col[BCOMP]) * span->blue \
410 + (tmp_col[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
411 dest[ACOMP] = span->alpha * (tmp_col[ACOMP] + 1) >> (FIXED_SHIFT + 8)
413 #define REPLACE COPY_CHAN4(dest, tmp_col)
415 #define I2CHAN_CLAMP(I) (GLchan) ((I) & CHAN_MAX)
416 /* equivalent to '(GLchan) MIN2((I),CHAN_MAX)' */
419 dest[RCOMP] = MIN2(((span->red << 8) + \
420 (tmp_col[RCOMP] + 1) * info->er) \
421 >> (FIXED_SHIFT + 8), CHAN_MAX); \
422 dest[GCOMP] = MIN2(((span->green << 8) + \
423 (tmp_col[GCOMP] + 1) * info->eg) \
424 >> (FIXED_SHIFT + 8), CHAN_MAX); \
425 dest[RCOMP] = MIN2(((span->blue << 8) + \
426 (tmp_col[BCOMP] + 1) * info->eb) \
427 >> (FIXED_SHIFT + 8), CHAN_MAX); \
428 dest[ACOMP] = span->alpha * (tmp_col[ACOMP] + 1) >> (FIXED_SHIFT + 8)
432 #define NEAREST_RGB_REPLACE NEAREST_RGB;REPLACE
434 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
436 #define SPAN_NEAREST(DO_TEX,COMP) \
437 for (i = 0; i < span->count; i++) { \
438 /* Isn't it necessary to use FixedFloor below?? */ \
439 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
440 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
441 GLint pos = (t << info->twidth_log2) + s; \
442 const GLchan *tex00 = info->texture + COMP * pos; \
443 zspan[i] = FixedToDepth(span->z); \
444 fogspan[i] = span->fog; \
446 span->fog += span->fogStep; \
447 span->z += span->zStep; \
448 span->red += span->redStep; \
449 span->green += span->greenStep; \
450 span->blue += span->blueStep; \
451 span->alpha += span->alphaStep; \
452 span->intTex[0] += span->intTexStep[0]; \
453 span->intTex[1] += span->intTexStep[1]; \
457 #define SPAN_LINEAR(DO_TEX,COMP) \
458 for (i = 0; i < span->count; i++) { \
459 /* Isn't it necessary to use FixedFloor below?? */ \
460 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
461 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
462 GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
463 GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
464 GLfixed si = FIXED_FRAC_MASK - sf; \
465 GLfixed ti = FIXED_FRAC_MASK - tf; \
466 GLint pos = (t << info->twidth_log2) + s; \
467 const GLchan *tex00 = info->texture + COMP * pos; \
468 const GLchan *tex10 = tex00 + info->tbytesline; \
469 const GLchan *tex01 = tex00 + COMP; \
470 const GLchan *tex11 = tex10 + COMP; \
473 if (t == info->tmask) { \
474 tex10 -= info->tsize; \
475 tex11 -= info->tsize; \
477 if (s == info->smask) { \
478 tex01 -= info->tbytesline; \
479 tex11 -= info->tbytesline; \
481 zspan[i] = FixedToDepth(span->z); \
482 fogspan[i] = span->fog; \
484 span->fog += span->fogStep; \
485 span->z += span->zStep; \
486 span->red += span->redStep; \
487 span->green += span->greenStep; \
488 span->blue += span->blueStep; \
489 span->alpha += span->alphaStep; \
490 span->intTex[0] += span->intTexStep[0]; \
491 span->intTex[1] += span->intTexStep[1]; \
495 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
498 GLdepth zspan
[MAX_WIDTH
];
499 GLfloat fogspan
[MAX_WIDTH
];
500 GLchan rgba
[MAX_WIDTH
][4];
501 GLchan
*dest
= rgba
[0];
502 const GLint fixedToDepthShift
= info
->fixedToDepthShift
;
504 span
->intTex
[0] -= FIXED_HALF
;
505 span
->intTex
[1] -= FIXED_HALF
;
506 switch (info
->filter
) {
508 switch (info
->format
) {
510 switch (info
->envmode
) {
512 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
516 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
519 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
522 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
529 switch(info
->envmode
) {
531 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
534 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
537 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
540 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
543 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
553 span
->intTex
[0] -= FIXED_HALF
;
554 span
->intTex
[1] -= FIXED_HALF
;
555 switch (info
->format
) {
557 switch (info
->envmode
) {
559 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
563 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
566 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
569 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
576 switch (info
->envmode
) {
578 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
581 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
584 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
587 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
590 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
598 _mesa_write_rgba_span(ctx
, span
->count
, span
->x
, span
->y
,
599 zspan
, fogspan
, rgba
, NULL
, GL_POLYGON
);
609 * Render an RGB/RGBA textured triangle without perspective correction.
611 static void affine_textured_triangle( GLcontext
*ctx
,
618 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
620 #define INTERP_ALPHA 1
621 #define INTERP_INT_TEX 1
622 #define S_SCALE twidth
623 #define T_SCALE theight
626 struct affine_info info; \
627 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
628 struct gl_texture_object *obj = unit->Current2D; \
629 GLint b = obj->BaseLevel; \
630 GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
631 GLfloat theight = (GLfloat) obj->Image[b]->Height; \
632 info.fixedToDepthShift = ctx->Visual.depthBits <= 16 ? FIXED_SHIFT : 0;\
633 info.texture = (const GLchan *) obj->Image[b]->Data; \
634 info.twidth_log2 = obj->Image[b]->WidthLog2; \
635 info.smask = obj->Image[b]->Width - 1; \
636 info.tmask = obj->Image[b]->Height - 1; \
637 info.format = obj->Image[b]->Format; \
638 info.filter = obj->MinFilter; \
639 info.envmode = unit->EnvMode; \
641 if (info.envmode == GL_BLEND) { \
642 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
643 info.er = FloatToFixed(unit->EnvColor[RCOMP]); \
644 info.eg = FloatToFixed(unit->EnvColor[GCOMP]); \
645 info.eb = FloatToFixed(unit->EnvColor[BCOMP]); \
646 info.ea = FloatToFixed(unit->EnvColor[ACOMP]); \
648 if (!info.texture) { \
649 /* this shouldn't happen */ \
653 switch (info.format) { \
657 info.tbytesline = obj->Image[b]->Width; \
659 case GL_LUMINANCE_ALPHA: \
660 info.tbytesline = obj->Image[b]->Width * 2; \
663 info.tbytesline = obj->Image[b]->Width * 3; \
666 info.tbytesline = obj->Image[b]->Width * 4; \
669 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
672 info.tsize = obj->Image[b]->Height * info.tbytesline;
674 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
676 #include "s_tritemp.h"
689 const GLchan
*texture
;
690 GLchan er
, eg
, eb
, ea
;
691 GLint tbytesline
, tsize
;
692 GLint fixedToDepthShift
;
697 fast_persp_span(GLcontext
*ctx
, struct triangle_span
*span
,
698 struct persp_info
*info
)
702 /* Instead of defining a function for each mode, a test is done
703 * between the outer and inner loops. This is to reduce code size
704 * and complexity. Observe that an optimizing compiler kills
705 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
707 #define SPAN_NEAREST(DO_TEX,COMP) \
708 for (i = 0; i < span->count; i++) { \
709 GLdouble invQ = tex_coord[2] ? \
710 (1.0 / tex_coord[2]) : 1.0; \
711 GLfloat s_tmp = tex_coord[0] * invQ; \
712 GLfloat t_tmp = tex_coord[1] * invQ; \
713 GLint s = IFLOOR(s_tmp) & info->smask; \
714 GLint t = IFLOOR(t_tmp) & info->tmask; \
715 GLint pos = (t << info->twidth_log2) + s; \
716 const GLchan *tex00 = info->texture + COMP * pos; \
717 zspan[i] = FixedToDepth(span->z); \
718 fogspan[i] = span->fog; \
720 span->fog += span->fogStep; \
721 span->z += span->zStep; \
722 span->red += span->redStep; \
723 span->green += span->greenStep; \
724 span->blue += span->blueStep; \
725 span->alpha += span->alphaStep; \
726 tex_coord[0] += tex_step[0]; \
727 tex_coord[1] += tex_step[1]; \
728 tex_coord[2] += tex_step[2]; \
732 #define SPAN_LINEAR(DO_TEX,COMP) \
733 for (i = 0; i < span->count; i++) { \
734 GLdouble invQ = tex_coord[2] ? \
735 (1.0 / tex_coord[2]) : 1.0; \
736 GLfloat s_tmp = tex_coord[0] * invQ; \
737 GLfloat t_tmp = tex_coord[1] * invQ; \
738 GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
739 GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
740 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
741 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
742 GLfixed sf = s_fix & FIXED_FRAC_MASK; \
743 GLfixed tf = t_fix & FIXED_FRAC_MASK; \
744 GLfixed si = FIXED_FRAC_MASK - sf; \
745 GLfixed ti = FIXED_FRAC_MASK - tf; \
746 GLint pos = (t << info->twidth_log2) + s; \
747 const GLchan *tex00 = info->texture + COMP * pos; \
748 const GLchan *tex10 = tex00 + info->tbytesline; \
749 const GLchan *tex01 = tex00 + COMP; \
750 const GLchan *tex11 = tex10 + COMP; \
753 if (t == info->tmask) { \
754 tex10 -= info->tsize; \
755 tex11 -= info->tsize; \
757 if (s == info->smask) { \
758 tex01 -= info->tbytesline; \
759 tex11 -= info->tbytesline; \
761 zspan[i] = FixedToDepth(span->z); \
762 fogspan[i] = span->fog; \
764 span->fog += span->fogStep; \
765 span->z += span->zStep; \
766 span->red += span->redStep; \
767 span->green += span->greenStep; \
768 span->blue += span->blueStep; \
769 span->alpha += span->alphaStep; \
770 tex_coord[0] += tex_step[0]; \
771 tex_coord[1] += tex_step[1]; \
772 tex_coord[2] += tex_step[2]; \
776 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
779 GLdepth zspan
[MAX_WIDTH
];
780 GLfloat tex_coord
[3], tex_step
[3];
781 GLfloat fogspan
[MAX_WIDTH
];
782 GLchan rgba
[MAX_WIDTH
][4];
783 GLchan
*dest
= rgba
[0];
784 const GLint fixedToDepthShift
= info
->fixedToDepthShift
;
786 tex_coord
[0] = span
->tex
[0][0] * (info
->smask
+ 1),
787 tex_step
[0] = span
->texStep
[0][0] * (info
->smask
+ 1);
788 tex_coord
[1] = span
->tex
[0][1] * (info
->tmask
+ 1),
789 tex_step
[1] = span
->texStep
[0][1] * (info
->tmask
+ 1);
790 /* span->tex[0][2] only if 3D-texturing, here only 2D */
791 tex_coord
[2] = span
->tex
[0][3],
792 tex_step
[2] = span
->texStep
[0][3];
794 switch (info
->filter
) {
796 switch (info
->format
) {
798 switch (info
->envmode
) {
800 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
804 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
807 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
810 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
817 switch(info
->envmode
) {
819 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
822 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
825 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
828 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
831 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
841 switch (info
->format
) {
843 switch (info
->envmode
) {
845 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
849 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
852 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
855 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
862 switch (info
->envmode
) {
864 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
867 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
870 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
873 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
876 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
885 /* This does not seem to be necessary, but I don't know !! */
886 /* span->tex[0][0] = tex_coord[0] / (info->smask + 1),
887 span->tex[0][1] = tex_coord[1] / (info->tmask + 1),*/
888 /* span->tex[0][2] only if 3D-texturing, here only 2D */
889 /* span->tex[0][3] = tex_coord[2]; */
891 _mesa_write_rgba_span(ctx
, span
->count
, span
->x
, span
->y
,
892 zspan
, fogspan
, rgba
, NULL
, GL_POLYGON
);
902 * Render an perspective corrected RGB/RGBA textured triangle.
903 * The Q (aka V in Mesa) coordinate must be zero such that the divide
904 * by interpolated Q/W comes out right.
907 static void persp_textured_triangle( GLcontext
*ctx
,
914 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
916 #define INTERP_ALPHA 1
920 struct persp_info info; \
921 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
922 struct gl_texture_object *obj = unit->Current2D; \
923 GLint b = obj->BaseLevel; \
924 info.fixedToDepthShift = ctx->Visual.depthBits <= 16 ? FIXED_SHIFT : 0;\
925 info.texture = (const GLchan *) obj->Image[b]->Data; \
926 info.twidth_log2 = obj->Image[b]->WidthLog2; \
927 info.smask = obj->Image[b]->Width - 1; \
928 info.tmask = obj->Image[b]->Height - 1; \
929 info.format = obj->Image[b]->Format; \
930 info.filter = obj->MinFilter; \
931 info.envmode = unit->EnvMode; \
933 if (info.envmode == GL_BLEND) { \
934 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
935 info.er = FloatToFixed(unit->EnvColor[RCOMP]); \
936 info.eg = FloatToFixed(unit->EnvColor[GCOMP]); \
937 info.eb = FloatToFixed(unit->EnvColor[BCOMP]); \
938 info.ea = FloatToFixed(unit->EnvColor[ACOMP]); \
940 if (!info.texture) { \
941 /* this shouldn't happen */ \
945 switch (info.format) { \
949 info.tbytesline = obj->Image[b]->Width; \
951 case GL_LUMINANCE_ALPHA: \
952 info.tbytesline = obj->Image[b]->Width * 2; \
955 info.tbytesline = obj->Image[b]->Width * 3; \
958 info.tbytesline = obj->Image[b]->Width * 4; \
961 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
964 info.tsize = obj->Image[b]->Height * info.tbytesline;
966 #define RENDER_SPAN( span ) fast_persp_span(ctx, &span, &info);
968 #include "s_tritemp.h"
973 #endif /* CHAN_BITS != GL_FLOAT */
977 * Generate arrays of fragment colors, z, fog, texcoords, etc from a
978 * triangle span object. Then call the span/fragment processsing
979 * functions in s_span.[ch]. This is used by a bunch of the textured
980 * triangle functions.
983 rasterize_span(GLcontext
*ctx
, const struct triangle_span
*span
)
985 DEFMARRAY(GLchan
, rgba
, MAX_WIDTH
, 4);
986 DEFMARRAY(GLchan
, spec
, MAX_WIDTH
, 4);
987 DEFARRAY(GLuint
, index
, MAX_WIDTH
);
988 DEFARRAY(GLuint
, z
, MAX_WIDTH
);
989 DEFARRAY(GLfloat
, fog
, MAX_WIDTH
);
990 DEFARRAY(GLfloat
, sTex
, MAX_WIDTH
);
991 DEFARRAY(GLfloat
, tTex
, MAX_WIDTH
);
992 DEFARRAY(GLfloat
, rTex
, MAX_WIDTH
);
993 DEFARRAY(GLfloat
, lambda
, MAX_WIDTH
);
994 DEFMARRAY(GLfloat
, msTex
, MAX_TEXTURE_UNITS
, MAX_WIDTH
);
995 DEFMARRAY(GLfloat
, mtTex
, MAX_TEXTURE_UNITS
, MAX_WIDTH
);
996 DEFMARRAY(GLfloat
, mrTex
, MAX_TEXTURE_UNITS
, MAX_WIDTH
);
997 DEFMARRAY(GLfloat
, mLambda
, MAX_TEXTURE_UNITS
, MAX_WIDTH
);
999 CHECKARRAY(rgba
, return);
1000 CHECKARRAY(spec
, return);
1001 CHECKARRAY(index
, return);
1002 CHECKARRAY(z
, return);
1003 CHECKARRAY(fog
, return);
1004 CHECKARRAY(sTex
, return);
1005 CHECKARRAY(tTex
, return);
1006 CHECKARRAY(rTex
, return);
1007 CHECKARRAY(lambda
, return);
1008 CHECKARRAY(msTex
, return);
1009 CHECKARRAY(mtTex
, return);
1010 CHECKARRAY(mrTex
, return);
1011 CHECKARRAY(mLambda
, return);
1013 if (span
->activeMask
& SPAN_RGBA
) {
1014 #if CHAN_TYPE == GL_FLOAT
1015 GLfloat r
= span
->red
;
1016 GLfloat g
= span
->green
;
1017 GLfloat b
= span
->blue
;
1018 GLfloat a
= span
->alpha
;
1020 GLfixed r
= span
->red
;
1021 GLfixed g
= span
->green
;
1022 GLfixed b
= span
->blue
;
1023 GLfixed a
= span
->alpha
;
1026 for (i
= 0; i
< span
->count
; i
++) {
1027 rgba
[i
][RCOMP
] = FixedToChan(r
);
1028 rgba
[i
][GCOMP
] = FixedToChan(g
);
1029 rgba
[i
][BCOMP
] = FixedToChan(b
);
1030 rgba
[i
][ACOMP
] = FixedToChan(a
);
1032 g
+= span
->greenStep
;
1033 b
+= span
->blueStep
;
1034 a
+= span
->alphaStep
;
1037 if (span
->activeMask
& SPAN_SPEC
) {
1038 #if CHAN_TYPE == GL_FLOAT
1039 GLfloat r
= span
->specRed
;
1040 GLfloat g
= span
->specGreen
;
1041 GLfloat b
= span
->specBlue
;
1043 GLfixed r
= span
->specRed
;
1044 GLfixed g
= span
->specGreen
;
1045 GLfixed b
= span
->specBlue
;
1048 for (i
= 0; i
< span
->count
; i
++) {
1049 spec
[i
][RCOMP
] = FixedToChan(r
);
1050 spec
[i
][GCOMP
] = FixedToChan(g
);
1051 spec
[i
][BCOMP
] = FixedToChan(b
);
1052 r
+= span
->specRedStep
;
1053 g
+= span
->specGreenStep
;
1054 b
+= span
->specBlueStep
;
1057 if (span
->activeMask
& SPAN_INDEX
) {
1059 GLfixed ind
= span
->index
;
1060 for (i
= 0; i
< span
->count
; i
++) {
1061 index
[i
] = FixedToInt(ind
);
1062 ind
+= span
->indexStep
;
1065 if (span
->activeMask
& SPAN_Z
) {
1066 if (ctx
->Visual
.depthBits
<= 16) {
1068 GLfixed zval
= span
->z
;
1069 for (i
= 0; i
< span
->count
; i
++) {
1070 z
[i
] = FixedToInt(zval
);
1071 zval
+= span
->zStep
;
1075 /* Deep Z buffer, no fixed->int shift */
1077 GLfixed zval
= span
->z
;
1078 for (i
= 0; i
< span
->count
; i
++) {
1080 zval
+= span
->zStep
;
1084 if (span
->activeMask
& SPAN_FOG
) {
1086 GLfloat f
= span
->fog
;
1087 for (i
= 0; i
< span
->count
; i
++) {
1092 if (span
->activeMask
& SPAN_TEXTURE
) {
1093 if (ctx
->Texture
._ReallyEnabled
& ~TEXTURE0_ANY
) {
1095 if (span
->activeMask
& SPAN_LAMBDA
) {
1098 for (u
= 0; u
< MAX_TEXTURE_UNITS
; u
++) {
1099 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
1100 GLfloat s
= span
->tex
[u
][0];
1101 GLfloat t
= span
->tex
[u
][1];
1102 GLfloat r
= span
->tex
[u
][2];
1103 GLfloat q
= span
->tex
[u
][3];
1105 for (i
= 0; i
< span
->count
; i
++) {
1106 const GLfloat invQ
= (q
== 0.0F
) ? 1.0 : (1.0F
/ q
);
1107 msTex
[u
][i
] = s
* invQ
;
1108 mtTex
[u
][i
] = t
* invQ
;
1109 mrTex
[u
][i
] = r
* invQ
;
1110 mLambda
[u
][i
] = log(span
->rho
[u
] * invQ
* invQ
) * 1.442695F
* 0.5F
;
1111 s
+= span
->texStep
[u
][0];
1112 t
+= span
->texStep
[u
][1];
1113 r
+= span
->texStep
[u
][2];
1114 q
+= span
->texStep
[u
][3];
1120 /* without lambda */
1122 for (u
= 0; u
< MAX_TEXTURE_UNITS
; u
++) {
1123 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
1124 GLfloat s
= span
->tex
[u
][0];
1125 GLfloat t
= span
->tex
[u
][1];
1126 GLfloat r
= span
->tex
[u
][2];
1127 GLfloat q
= span
->tex
[u
][3];
1129 for (i
= 0; i
< span
->count
; i
++) {
1130 const GLfloat invQ
= (q
== 0.0F
) ? 1.0 : (1.0F
/ q
);
1131 msTex
[u
][i
] = s
* invQ
;
1132 mtTex
[u
][i
] = t
* invQ
;
1133 mrTex
[u
][i
] = r
* invQ
;
1134 s
+= span
->texStep
[u
][0];
1135 t
+= span
->texStep
[u
][1];
1136 r
+= span
->texStep
[u
][2];
1137 q
+= span
->texStep
[u
][3];
1144 /* just texture unit 0 */
1145 if (span
->activeMask
& SPAN_LAMBDA
) {
1147 GLfloat s
= span
->tex
[0][0];
1148 GLfloat t
= span
->tex
[0][1];
1149 GLfloat r
= span
->tex
[0][2];
1150 GLfloat q
= span
->tex
[0][3];
1152 for (i
= 0; i
< span
->count
; i
++) {
1153 const GLfloat invQ
= (q
== 0.0F
) ? 1.0 : (1.0F
/ q
);
1157 lambda
[i
] = log(span
->rho
[0] * invQ
* invQ
) * 1.442695F
* 0.5F
;
1158 s
+= span
->texStep
[0][0];
1159 t
+= span
->texStep
[0][1];
1160 r
+= span
->texStep
[0][2];
1161 q
+= span
->texStep
[0][3];
1165 /* without lambda */
1166 GLfloat s
= span
->tex
[0][0];
1167 GLfloat t
= span
->tex
[0][1];
1168 GLfloat r
= span
->tex
[0][2];
1169 GLfloat q
= span
->tex
[0][3];
1171 for (i
= 0; i
< span
->count
; i
++) {
1172 const GLfloat invQ
= (q
== 0.0F
) ? 1.0 : (1.0F
/ q
);
1176 s
+= span
->texStep
[0][0];
1177 t
+= span
->texStep
[0][1];
1178 r
+= span
->texStep
[0][2];
1179 q
+= span
->texStep
[0][3];
1184 /* XXX keep this? */
1185 if (span
->activeMask
& SPAN_INT_TEXTURE
) {
1186 GLint intTexcoord
[MAX_WIDTH
][2];
1187 GLfixed s
= span
->intTex
[0];
1188 GLfixed t
= span
->intTex
[1];
1190 for (i
= 0; i
< span
->count
; i
++) {
1191 intTexcoord
[i
][0] = FixedToInt(s
);
1192 intTexcoord
[i
][1] = FixedToInt(t
);
1193 s
+= span
->intTexStep
[0];
1194 t
+= span
->intTexStep
[1];
1198 /* examine activeMask and call a s_span.c function */
1199 if (span
->activeMask
& SPAN_TEXTURE
) {
1200 const GLfloat
*fogPtr
;
1201 if (span
->activeMask
& SPAN_FOG
)
1206 if (ctx
->Texture
._ReallyEnabled
& ~TEXTURE0_ANY
) {
1207 if (span
->activeMask
& SPAN_SPEC
) {
1208 _mesa_write_multitexture_span(ctx
, span
->count
, span
->x
, span
->y
,
1210 (const GLfloat (*)[MAX_WIDTH
]) msTex
,
1211 (const GLfloat (*)[MAX_WIDTH
]) mtTex
,
1212 (const GLfloat (*)[MAX_WIDTH
]) mrTex
,
1213 (GLfloat (*)[MAX_WIDTH
]) mLambda
,
1214 rgba
, (CONST
GLchan (*)[4]) spec
,
1218 _mesa_write_multitexture_span(ctx
, span
->count
, span
->x
, span
->y
,
1220 (const GLfloat (*)[MAX_WIDTH
]) msTex
,
1221 (const GLfloat (*)[MAX_WIDTH
]) mtTex
,
1222 (const GLfloat (*)[MAX_WIDTH
]) mrTex
,
1223 (GLfloat (*)[MAX_WIDTH
]) mLambda
,
1224 rgba
, NULL
, NULL
, GL_POLYGON
);
1228 /* single texture */
1229 if (span
->activeMask
& SPAN_SPEC
) {
1230 _mesa_write_texture_span(ctx
, span
->count
, span
->x
, span
->y
,
1231 z
, fogPtr
, sTex
, tTex
, rTex
, lambda
,
1232 rgba
, (CONST
GLchan (*)[4]) spec
,
1236 _mesa_write_texture_span(ctx
, span
->count
, span
->x
, span
->y
,
1237 z
, fogPtr
, sTex
, tTex
, rTex
, lambda
,
1238 rgba
, NULL
, NULL
, GL_POLYGON
);
1243 _mesa_problem(ctx
, "rasterize_span() should only be used for texturing");
1258 UNDEFARRAY(mLambda
);
1265 * Render a smooth-shaded, textured, RGBA triangle.
1266 * Interpolate S,T,R with perspective correction, w/out mipmapping.
1268 static void general_textured_triangle( GLcontext
*ctx
,
1271 const SWvertex
*v2
)
1274 #define INTERP_FOG 1
1275 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1276 #define INTERP_RGB 1
1277 #define INTERP_ALPHA 1
1278 #define INTERP_TEX 1
1280 #define SETUP_CODE \
1281 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1282 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1283 DEFARRAY(GLfloat, sSpan, MAX_WIDTH); /* mac 32k limitation */ \
1284 DEFARRAY(GLfloat, tSpan, MAX_WIDTH); /* mac 32k limitation */ \
1285 DEFARRAY(GLfloat, uSpan, MAX_WIDTH); /* mac 32k limitation */ \
1286 CHECKARRAY(sSpan, return); /* mac 32k limitation */ \
1287 CHECKARRAY(tSpan, return); /* mac 32k limitation */ \
1288 CHECKARRAY(uSpan, return); /* mac 32k limitation */ \
1289 span.texWidth[0] = (GLfloat) texImage->Width; \
1290 span.texHeight[0] = (GLfloat) texImage->Height; \
1291 (void) fixedToDepthShift;
1293 #define RENDER_SPAN( span ) \
1294 GLdepth zSpan[MAX_WIDTH]; \
1295 GLfloat fogSpan[MAX_WIDTH]; \
1296 GLchan rgbaSpan[MAX_WIDTH][4]; \
1298 /* NOTE: we could just call rasterize_span() here instead */ \
1299 for (i = 0; i < span.count; i++) { \
1300 GLdouble invQ = span.tex[0][3] ? (1.0 / span.tex[0][3]) : 1.0; \
1301 zSpan[i] = FixedToDepth(span.z); \
1302 span.z += span.zStep; \
1303 fogSpan[i] = span.fog; \
1304 span.fog += span.fogStep; \
1305 rgbaSpan[i][RCOMP] = FixedToChan(span.red); \
1306 rgbaSpan[i][GCOMP] = FixedToChan(span.green); \
1307 rgbaSpan[i][BCOMP] = FixedToChan(span.blue); \
1308 rgbaSpan[i][ACOMP] = FixedToChan(span.alpha); \
1309 span.red += span.redStep; \
1310 span.green += span.greenStep; \
1311 span.blue += span.blueStep; \
1312 span.alpha += span.alphaStep; \
1313 sSpan[i] = span.tex[0][0] * invQ; \
1314 tSpan[i] = span.tex[0][1] * invQ; \
1315 uSpan[i] = span.tex[0][2] * invQ; \
1316 span.tex[0][0] += span.texStep[0][0]; \
1317 span.tex[0][1] += span.texStep[0][1]; \
1318 span.tex[0][2] += span.texStep[0][2]; \
1319 span.tex[0][3] += span.texStep[0][3]; \
1321 _mesa_write_texture_span(ctx, span.count, span.x, span.y, \
1322 zSpan, fogSpan, sSpan, tSpan, uSpan, \
1323 NULL, rgbaSpan, NULL, NULL, GL_POLYGON );
1325 #define CLEANUP_CODE \
1326 UNDEFARRAY(sSpan); /* mac 32k limitation */ \
1327 UNDEFARRAY(tSpan); \
1330 #include "s_tritemp.h"
1335 * Render a smooth-shaded, textured, RGBA triangle with separate specular
1336 * color interpolation.
1337 * Interpolate texcoords with perspective correction, w/out mipmapping.
1339 static void general_textured_spec_triangle( GLcontext
*ctx
,
1342 const SWvertex
*v2
)
1345 #define INTERP_FOG 1
1346 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1347 #define INTERP_RGB 1
1348 #define INTERP_SPEC 1
1349 #define INTERP_ALPHA 1
1350 #define INTERP_TEX 1
1352 #define SETUP_CODE \
1353 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1354 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1355 span.texWidth[0] = (GLfloat) texImage->Width; \
1356 span.texHeight[0] = (GLfloat) texImage->Height; \
1357 (void) fixedToDepthShift;
1359 #define RENDER_SPAN( span ) rasterize_span(ctx, &span);
1361 #include "s_tritemp.h"
1366 * Render a smooth-shaded, textured, RGBA triangle.
1367 * Interpolate S,T,R with perspective correction and compute lambda for
1368 * each fragment. Lambda is used to determine whether to use the
1369 * minification or magnification filter. If minification and using
1370 * mipmaps, lambda is also used to select the texture level of detail.
1372 static void lambda_textured_triangle( GLcontext
*ctx
,
1375 const SWvertex
*v2
)
1378 #define INTERP_FOG 1
1379 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1380 #define INTERP_RGB 1
1381 #define INTERP_ALPHA 1
1382 #define INTERP_TEX 1
1383 #define INTERP_LAMBDA 1
1385 #define SETUP_CODE \
1386 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1387 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1388 span.texWidth[0] = (GLfloat) texImage->Width; \
1389 span.texHeight[0] = (GLfloat) texImage->Height; \
1390 (void) fixedToDepthShift;
1392 #define RENDER_SPAN( span ) rasterize_span(ctx, &span);
1394 #include "s_tritemp.h"
1399 * Render a smooth-shaded, textured, RGBA triangle with separate specular
1401 * Interpolate S,T,R with perspective correction and compute lambda for
1402 * each fragment. Lambda is used to determine whether to use the
1403 * minification or magnification filter. If minification and using
1404 * mipmaps, lambda is also used to select the texture level of detail.
1406 static void lambda_textured_spec_triangle( GLcontext
*ctx
,
1409 const SWvertex
*v2
)
1412 #define INTERP_FOG 1
1413 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1414 #define INTERP_RGB 1
1415 #define INTERP_SPEC 1
1416 #define INTERP_ALPHA 1
1417 #define INTERP_TEX 1
1418 #define INTERP_LAMBDA 1
1420 #define SETUP_CODE \
1421 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1422 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1423 span.texWidth[0] = (GLfloat) texImage->Width; \
1424 span.texHeight[0] = (GLfloat) texImage->Height; \
1425 (void) fixedToDepthShift;
1427 #define RENDER_SPAN( span ) rasterize_span(ctx, &span);
1429 #include "s_tritemp.h"
1434 * This is the big one!
1435 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates
1436 * with lambda (LOD).
1440 lambda_multitextured_triangle( GLcontext
*ctx
,
1443 const SWvertex
*v2
)
1447 #define INTERP_FOG 1
1448 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1449 #define INTERP_RGB 1
1450 #define INTERP_ALPHA 1
1451 #define INTERP_SPEC 1
1452 #define INTERP_MULTITEX 1
1453 #define INTERP_LAMBDA 1
1455 #define SETUP_CODE \
1457 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) { \
1458 if (ctx->Texture.Unit[u]._ReallyEnabled) { \
1459 const struct gl_texture_object *texObj; \
1460 const struct gl_texture_image *texImage; \
1461 texObj = ctx->Texture.Unit[u]._Current; \
1462 texImage = texObj->Image[texObj->BaseLevel]; \
1463 span.texWidth[u] = (GLfloat) texImage->Width; \
1464 span.texHeight[u] = (GLfloat) texImage->Height; \
1467 (void) fixedToDepthShift;
1469 #define RENDER_SPAN( span ) rasterize_span(ctx, &span);
1471 #include "s_tritemp.h"
1476 static void occlusion_zless_triangle( GLcontext
*ctx
,
1479 const SWvertex
*v2
)
1481 if (ctx
->OcclusionResult
) {
1485 #define DO_OCCLUSION_TEST
1487 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1489 #define RENDER_SPAN( span ) \
1491 for (i = 0; i < span.count; i++) { \
1492 GLdepth z = FixedToDepth(span.z); \
1493 if (z < zRow[i]) { \
1494 ctx->OcclusionResult = GL_TRUE; \
1497 span.z += span.zStep; \
1500 #include "s_tritemp.h"
1503 static void nodraw_triangle( GLcontext
*ctx
,
1506 const SWvertex
*v2
)
1508 (void) (ctx
&& v0
&& v1
&& v2
);
1511 void _swrast_add_spec_terms_triangle( GLcontext
*ctx
,
1514 const SWvertex
*v2
)
1516 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
1517 SWvertex
*ncv1
= (SWvertex
*)v1
;
1518 SWvertex
*ncv2
= (SWvertex
*)v2
;
1520 COPY_CHAN4( c
[0], ncv0
->color
);
1521 COPY_CHAN4( c
[1], ncv1
->color
);
1522 COPY_CHAN4( c
[2], ncv2
->color
);
1523 ACC_3V( ncv0
->color
, ncv0
->specular
);
1524 ACC_3V( ncv1
->color
, ncv1
->specular
);
1525 ACC_3V( ncv2
->color
, ncv2
->specular
);
1526 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
1527 COPY_CHAN4( ncv0
->color
, c
[0] );
1528 COPY_CHAN4( ncv1
->color
, c
[1] );
1529 COPY_CHAN4( ncv2
->color
, c
[2] );
1536 /* record the current triangle function name */
1537 static const char *triFuncName
= NULL
;
1539 #define USE(triFunc) \
1541 triFuncName = #triFunc; \
1542 /*printf("%s\n", triFuncName);*/ \
1543 swrast->Triangle = triFunc; \
1548 #define USE(triFunc) swrast->Triangle = triFunc;
1556 * Determine which triangle rendering function to use given the current
1557 * rendering context.
1559 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1560 * remove tests to this code.
1563 _swrast_choose_triangle( GLcontext
*ctx
)
1565 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1566 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
1568 if (ctx
->Polygon
.CullFlag
&&
1569 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1570 USE(nodraw_triangle
);
1574 if (ctx
->RenderMode
==GL_RENDER
) {
1576 if (ctx
->Polygon
.SmoothFlag
) {
1577 _mesa_set_aa_triangle_function(ctx
);
1578 ASSERT(swrast
->Triangle
);
1582 if (ctx
->Depth
.OcclusionTest
&&
1584 ctx
->Depth
.Mask
== GL_FALSE
&&
1585 ctx
->Depth
.Func
== GL_LESS
&&
1586 !ctx
->Stencil
.Enabled
) {
1588 ctx
->Color
.ColorMask
[0] == 0 &&
1589 ctx
->Color
.ColorMask
[1] == 0 &&
1590 ctx
->Color
.ColorMask
[2] == 0 &&
1591 ctx
->Color
.ColorMask
[3] == 0)
1593 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1594 USE(occlusion_zless_triangle
);
1599 if (ctx
->Texture
._ReallyEnabled
) {
1600 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1601 const struct gl_texture_object
*texObj2D
;
1602 const struct gl_texture_image
*texImg
;
1603 GLenum minFilter
, magFilter
, envMode
;
1605 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1606 texImg
= texObj2D
? texObj2D
->Image
[texObj2D
->BaseLevel
] : NULL
;
1607 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1608 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1609 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1610 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1612 /* First see if we can used an optimized 2-D texture function */
1613 if (ctx
->Texture
._ReallyEnabled
==TEXTURE0_2D
1614 && texObj2D
->WrapS
==GL_REPEAT
1615 && texObj2D
->WrapT
==GL_REPEAT
1616 && texImg
->Border
==0
1617 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1618 && minFilter
== magFilter
1619 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1620 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1621 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1622 if (minFilter
== GL_NEAREST
1623 && format
== MESA_FORMAT_RGB
1624 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1625 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1626 && ctx
->Depth
.Func
== GL_LESS
1627 && ctx
->Depth
.Mask
== GL_TRUE
)
1628 || swrast
->_RasterMask
== TEXTURE_BIT
)
1629 && ctx
->Polygon
.StippleFlag
== GL_FALSE
) {
1630 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1631 USE(simple_z_textured_triangle
);
1634 USE(simple_textured_triangle
);
1638 #if CHAN_TYPE == GL_FLOAT
1639 USE(general_textured_triangle
);
1641 USE(affine_textured_triangle
);
1646 #if CHAN_TYPE == GL_FLOAT
1647 USE(general_textured_triangle
);
1649 USE(persp_textured_triangle
);
1654 /* More complicated textures (mipmap, multi-tex, sep specular) */
1655 GLboolean needLambda
;
1656 /* if mag filter != min filter we need to compute lambda */
1657 const struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[0]._Current
;
1658 if (obj
&& obj
->MinFilter
!= obj
->MagFilter
)
1659 needLambda
= GL_TRUE
;
1661 needLambda
= GL_FALSE
;
1662 if (ctx
->Texture
._ReallyEnabled
> TEXTURE0_ANY
) {
1663 USE(lambda_multitextured_triangle
);
1665 else if (ctx
->_TriangleCaps
& DD_SEPARATE_SPECULAR
) {
1666 /* separate specular color interpolation */
1668 USE(lambda_textured_spec_triangle
);
1671 USE(general_textured_spec_triangle
);
1676 USE(lambda_textured_triangle
);
1679 USE(general_textured_triangle
);
1685 ASSERT(!ctx
->Texture
._ReallyEnabled
);
1686 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1687 /* smooth shaded, no texturing, stippled or some raster ops */
1689 USE(smooth_rgba_triangle
);
1692 USE(smooth_ci_triangle
);
1696 /* flat shaded, no texturing, stippled or some raster ops */
1698 USE(flat_rgba_triangle
);
1701 USE(flat_ci_triangle
);
1706 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1707 USE(_mesa_feedback_triangle
);
1710 /* GL_SELECT mode */
1711 USE(_mesa_select_triangle
);