2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2004 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.
53 _swrast_culltriangle( GLcontext
*ctx
,
58 GLfloat ex
= v1
->win
[0] - v0
->win
[0];
59 GLfloat ey
= v1
->win
[1] - v0
->win
[1];
60 GLfloat fx
= v2
->win
[0] - v0
->win
[0];
61 GLfloat fy
= v2
->win
[1] - v0
->win
[1];
62 GLfloat c
= ex
*fy
-ey
*fx
;
64 if (c
* SWRAST_CONTEXT(ctx
)->_BackfaceSign
> 0)
73 * Render a flat-shaded color index triangle.
75 #define NAME flat_ci_triangle
79 span.interpMask |= SPAN_INDEX; \
80 span.index = FloatToFixed(v2->index);\
82 #define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
83 #include "s_tritemp.h"
88 * Render a smooth-shaded color index triangle.
90 #define NAME smooth_ci_triangle
93 #define INTERP_INDEX 1
94 #define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
95 #include "s_tritemp.h"
100 * Render a flat-shaded RGBA triangle.
102 #define NAME flat_rgba_triangle
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
129 #define INTERP_ALPHA 1
132 /* texturing must be off */ \
133 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \
134 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
136 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
137 #include "s_tritemp.h"
142 * Render an RGB, GL_DECAL, textured triangle.
143 * Interpolate S,T only w/out mipmapping or perspective correction.
147 #define NAME simple_textured_triangle
148 #define INTERP_INT_TEX 1
149 #define S_SCALE twidth
150 #define T_SCALE theight
153 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
154 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
155 const GLint b = obj->BaseLevel; \
156 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
157 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
158 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
159 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
160 const GLint smask = obj->Image[0][b]->Width - 1; \
161 const GLint tmask = obj->Image[0][b]->Height - 1; \
163 /* this shouldn't happen */ \
167 #define RENDER_SPAN( span ) \
169 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
170 span.intTex[1] -= FIXED_HALF; \
171 for (i = 0; i < span.end; i++) { \
172 GLint s = FixedToInt(span.intTex[0]) & smask; \
173 GLint t = FixedToInt(span.intTex[1]) & tmask; \
174 GLint pos = (t << twidth_log2) + s; \
175 pos = pos + pos + pos; /* multiply by 3 */ \
176 span.array->rgb[i][RCOMP] = texture[pos]; \
177 span.array->rgb[i][GCOMP] = texture[pos+1]; \
178 span.array->rgb[i][BCOMP] = texture[pos+2]; \
179 span.intTex[0] += span.intTexStep[0]; \
180 span.intTex[1] += span.intTexStep[1]; \
182 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
183 (CONST GLchan (*)[3]) span.array->rgb,\
185 #include "s_tritemp.h"
190 * Render an RGB, GL_DECAL, textured triangle.
191 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
192 * perspective correction.
193 * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
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
530 #define INTERP_ALPHA 1
531 #define INTERP_INT_TEX 1
532 #define S_SCALE twidth
533 #define T_SCALE theight
536 struct affine_info info; \
537 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
538 struct gl_texture_object *obj = unit->Current2D; \
539 const GLint b = obj->BaseLevel; \
540 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
541 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
542 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
543 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
544 info.smask = obj->Image[0][b]->Width - 1; \
545 info.tmask = obj->Image[0][b]->Height - 1; \
546 info.format = obj->Image[0][b]->Format; \
547 info.filter = obj->MinFilter; \
548 info.envmode = unit->EnvMode; \
549 span.arrayMask |= SPAN_RGBA; \
551 if (info.envmode == GL_BLEND) { \
552 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
553 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
554 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
555 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
556 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
558 if (!info.texture) { \
559 /* this shouldn't happen */ \
563 switch (info.format) { \
567 info.tbytesline = obj->Image[0][b]->Width; \
569 case GL_LUMINANCE_ALPHA: \
570 info.tbytesline = obj->Image[0][b]->Width * 2; \
573 info.tbytesline = obj->Image[0][b]->Width * 3; \
576 info.tbytesline = obj->Image[0][b]->Width * 4; \
579 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
582 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
584 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
586 #include "s_tritemp.h"
597 const GLchan
*texture
;
598 GLfixed er
, eg
, eb
, ea
; /* texture env color */
599 GLint tbytesline
, tsize
;
604 fast_persp_span(GLcontext
*ctx
, struct sw_span
*span
,
605 struct persp_info
*info
)
607 GLchan sample
[4]; /* the filtered texture sample */
609 /* Instead of defining a function for each mode, a test is done
610 * between the outer and inner loops. This is to reduce code size
611 * and complexity. Observe that an optimizing compiler kills
612 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
614 #define SPAN_NEAREST(DO_TEX,COMP) \
615 for (i = 0; i < span->end; i++) { \
616 GLdouble invQ = tex_coord[2] ? \
617 (1.0 / tex_coord[2]) : 1.0; \
618 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
619 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
620 GLint s = IFLOOR(s_tmp) & info->smask; \
621 GLint t = IFLOOR(t_tmp) & info->tmask; \
622 GLint pos = (t << info->twidth_log2) + s; \
623 const GLchan *tex00 = info->texture + COMP * pos; \
625 span->red += span->redStep; \
626 span->green += span->greenStep; \
627 span->blue += span->blueStep; \
628 span->alpha += span->alphaStep; \
629 tex_coord[0] += tex_step[0]; \
630 tex_coord[1] += tex_step[1]; \
631 tex_coord[2] += tex_step[2]; \
635 #define SPAN_LINEAR(DO_TEX,COMP) \
636 for (i = 0; i < span->end; i++) { \
637 GLdouble invQ = tex_coord[2] ? \
638 (1.0 / tex_coord[2]) : 1.0; \
639 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
640 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
641 GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
642 GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
643 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
644 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
645 GLfixed sf = s_fix & FIXED_FRAC_MASK; \
646 GLfixed tf = t_fix & FIXED_FRAC_MASK; \
647 GLfixed si = FIXED_FRAC_MASK - sf; \
648 GLfixed ti = FIXED_FRAC_MASK - tf; \
649 GLint pos = (t << info->twidth_log2) + s; \
650 const GLchan *tex00 = info->texture + COMP * pos; \
651 const GLchan *tex10 = tex00 + info->tbytesline; \
652 const GLchan *tex01 = tex00 + COMP; \
653 const GLchan *tex11 = tex10 + COMP; \
656 if (t == info->tmask) { \
657 tex10 -= info->tsize; \
658 tex11 -= info->tsize; \
660 if (s == info->smask) { \
661 tex01 -= info->tbytesline; \
662 tex11 -= info->tbytesline; \
665 span->red += span->redStep; \
666 span->green += span->greenStep; \
667 span->blue += span->blueStep; \
668 span->alpha += span->alphaStep; \
669 tex_coord[0] += tex_step[0]; \
670 tex_coord[1] += tex_step[1]; \
671 tex_coord[2] += tex_step[2]; \
676 GLfloat tex_coord
[3], tex_step
[3];
677 GLchan
*dest
= span
->array
->rgba
[0];
679 tex_coord
[0] = span
->tex
[0][0] * (info
->smask
+ 1);
680 tex_step
[0] = span
->texStepX
[0][0] * (info
->smask
+ 1);
681 tex_coord
[1] = span
->tex
[0][1] * (info
->tmask
+ 1);
682 tex_step
[1] = span
->texStepX
[0][1] * (info
->tmask
+ 1);
683 /* span->tex[0][2] only if 3D-texturing, here only 2D */
684 tex_coord
[2] = span
->tex
[0][3];
685 tex_step
[2] = span
->texStepX
[0][3];
687 switch (info
->filter
) {
689 switch (info
->format
) {
691 switch (info
->envmode
) {
693 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
697 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
700 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
703 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
706 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
711 switch(info
->envmode
) {
713 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
716 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
719 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
722 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
725 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
728 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
736 switch (info
->format
) {
738 switch (info
->envmode
) {
740 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
744 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
747 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
750 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
753 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
758 switch (info
->envmode
) {
760 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
763 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
766 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
769 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
772 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
775 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
783 ASSERT(span
->arrayMask
& SPAN_RGBA
);
784 _swrast_write_rgba_span(ctx
, span
);
792 * Render an perspective corrected RGB/RGBA textured triangle.
793 * The Q (aka V in Mesa) coordinate must be zero such that the divide
794 * by interpolated Q/W comes out right.
797 #define NAME persp_textured_triangle
802 #define INTERP_ALPHA 1
806 struct persp_info info; \
807 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
808 const struct gl_texture_object *obj = unit->Current2D; \
809 const GLint b = obj->BaseLevel; \
810 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
811 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
812 info.smask = obj->Image[0][b]->Width - 1; \
813 info.tmask = obj->Image[0][b]->Height - 1; \
814 info.format = obj->Image[0][b]->Format; \
815 info.filter = obj->MinFilter; \
816 info.envmode = unit->EnvMode; \
818 if (info.envmode == GL_BLEND) { \
819 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
820 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
821 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
822 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
823 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
825 if (!info.texture) { \
826 /* this shouldn't happen */ \
830 switch (info.format) { \
834 info.tbytesline = obj->Image[0][b]->Width; \
836 case GL_LUMINANCE_ALPHA: \
837 info.tbytesline = obj->Image[0][b]->Width * 2; \
840 info.tbytesline = obj->Image[0][b]->Width * 3; \
843 info.tbytesline = obj->Image[0][b]->Width * 4; \
846 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
849 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
851 #define RENDER_SPAN( span ) \
852 span.interpMask &= ~SPAN_RGBA; \
853 span.arrayMask |= SPAN_RGBA; \
854 fast_persp_span(ctx, &span, &info);
856 #include "s_tritemp.h"
859 #endif /* CHAN_BITS != GL_FLOAT */
865 * Render a smooth-shaded, textured, RGBA triangle.
866 * Interpolate S,T,R with perspective correction, w/out mipmapping.
868 #define NAME general_textured_triangle
873 #define INTERP_SPEC 1
874 #define INTERP_ALPHA 1
876 #define RENDER_SPAN( span ) _swrast_write_texture_span(ctx, &span);
877 #include "s_tritemp.h"
882 * This is the big one!
883 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates.
886 #define NAME multitextured_triangle
891 #define INTERP_ALPHA 1
892 #define INTERP_SPEC 1
893 #define INTERP_MULTITEX 1
894 #define RENDER_SPAN( span ) _swrast_write_texture_span(ctx, &span);
895 #include "s_tritemp.h"
900 * Special tri function for occlusion testing
902 #define NAME occlusion_zless_triangle
905 ASSERT(ctx->Depth.Test); \
906 ASSERT(!ctx->Depth.Mask); \
907 ASSERT(ctx->Depth.Func == GL_LESS); \
908 if (ctx->OcclusionResult && !ctx->Occlusion.Active) { \
911 #define RENDER_SPAN( span ) \
912 if (ctx->Visual.depthBits <= 16) { \
914 const GLushort *zRow = (const GLushort *) \
915 _swrast_zbuffer_address(ctx, span.x, span.y); \
916 for (i = 0; i < span.end; i++) { \
917 GLdepth z = FixedToDepth(span.z); \
919 ctx->OcclusionResult = GL_TRUE; \
920 ctx->Occlusion.PassedCounter++; \
922 span.z += span.zStep; \
927 const GLuint *zRow = (const GLuint *) \
928 _swrast_zbuffer_address(ctx, span.x, span.y); \
929 for (i = 0; i < span.end; i++) { \
930 if (span.z < zRow[i]) { \
931 ctx->OcclusionResult = GL_TRUE; \
932 ctx->Occlusion.PassedCounter++; \
934 span.z += span.zStep; \
937 #include "s_tritemp.h"
942 nodraw_triangle( GLcontext
*ctx
,
947 (void) (ctx
&& v0
&& v1
&& v2
);
952 * This is used when separate specular color is enabled, but not
953 * texturing. We add the specular color to the primary color,
954 * draw the triangle, then restore the original primary color.
955 * Inefficient, but seldom needed.
957 void _swrast_add_spec_terms_triangle( GLcontext
*ctx
,
962 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
963 SWvertex
*ncv1
= (SWvertex
*)v1
;
964 SWvertex
*ncv2
= (SWvertex
*)v2
;
965 #if CHAN_TYPE == GL_FLOAT
966 GLfloat rSum
, gSum
, bSum
;
968 GLint rSum
, gSum
, bSum
;
971 /* save original colors */
972 COPY_CHAN4( c
[0], ncv0
->color
);
973 COPY_CHAN4( c
[1], ncv1
->color
);
974 COPY_CHAN4( c
[2], ncv2
->color
);
976 rSum
= ncv0
->color
[0] + ncv0
->specular
[0];
977 gSum
= ncv0
->color
[1] + ncv0
->specular
[1];
978 bSum
= ncv0
->color
[2] + ncv0
->specular
[2];
979 ncv0
->color
[0] = MIN2(rSum
, CHAN_MAX
);
980 ncv0
->color
[1] = MIN2(gSum
, CHAN_MAX
);
981 ncv0
->color
[2] = MIN2(bSum
, CHAN_MAX
);
983 rSum
= ncv1
->color
[0] + ncv1
->specular
[0];
984 gSum
= ncv1
->color
[1] + ncv1
->specular
[1];
985 bSum
= ncv1
->color
[2] + ncv1
->specular
[2];
986 ncv1
->color
[0] = MIN2(rSum
, CHAN_MAX
);
987 ncv1
->color
[1] = MIN2(gSum
, CHAN_MAX
);
988 ncv1
->color
[2] = MIN2(bSum
, CHAN_MAX
);
990 rSum
= ncv2
->color
[0] + ncv2
->specular
[0];
991 gSum
= ncv2
->color
[1] + ncv2
->specular
[1];
992 bSum
= ncv2
->color
[2] + ncv2
->specular
[2];
993 ncv2
->color
[0] = MIN2(rSum
, CHAN_MAX
);
994 ncv2
->color
[1] = MIN2(gSum
, CHAN_MAX
);
995 ncv2
->color
[2] = MIN2(bSum
, CHAN_MAX
);
997 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
998 /* restore original colors */
999 COPY_CHAN4( ncv0
->color
, c
[0] );
1000 COPY_CHAN4( ncv1
->color
, c
[1] );
1001 COPY_CHAN4( ncv2
->color
, c
[2] );
1008 /* record the current triangle function name */
1009 const char *_mesa_triFuncName
= NULL
;
1011 #define USE(triFunc) \
1013 _mesa_triFuncName = #triFunc; \
1014 /*printf("%s\n", _mesa_triFuncName);*/ \
1015 swrast->Triangle = triFunc; \
1020 #define USE(triFunc) swrast->Triangle = triFunc;
1028 * Determine which triangle rendering function to use given the current
1029 * rendering context.
1031 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1032 * remove tests to this code.
1035 _swrast_choose_triangle( GLcontext
*ctx
)
1037 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1038 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
1040 if (ctx
->Polygon
.CullFlag
&&
1041 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1042 USE(nodraw_triangle
);
1046 if (ctx
->RenderMode
==GL_RENDER
) {
1048 if (ctx
->Polygon
.SmoothFlag
) {
1049 _swrast_set_aa_triangle_function(ctx
);
1050 ASSERT(swrast
->Triangle
);
1054 /* special case for occlusion testing */
1055 if ((ctx
->Depth
.OcclusionTest
|| ctx
->Occlusion
.Active
) &&
1057 ctx
->Depth
.Mask
== GL_FALSE
&&
1058 ctx
->Depth
.Func
== GL_LESS
&&
1059 !ctx
->Stencil
.Enabled
) {
1061 ctx
->Color
.ColorMask
[0] == 0 &&
1062 ctx
->Color
.ColorMask
[1] == 0 &&
1063 ctx
->Color
.ColorMask
[2] == 0 &&
1064 ctx
->Color
.ColorMask
[3] == 0)
1066 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1067 USE(occlusion_zless_triangle
);
1072 if (ctx
->Texture
._EnabledCoordUnits
|| ctx
->FragmentProgram
._Enabled
) {
1073 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1074 const struct gl_texture_object
*texObj2D
;
1075 const struct gl_texture_image
*texImg
;
1076 GLenum minFilter
, magFilter
, envMode
;
1078 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1079 texImg
= texObj2D
? texObj2D
->Image
[0][texObj2D
->BaseLevel
] : NULL
;
1080 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1081 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1082 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1083 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1085 /* First see if we can use an optimized 2-D texture function */
1086 if (ctx
->Texture
._EnabledCoordUnits
== 0x1
1087 && !ctx
->FragmentProgram
._Enabled
1088 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1089 && texObj2D
->WrapS
== GL_REPEAT
1090 && texObj2D
->WrapT
== GL_REPEAT
1091 && texObj2D
->_IsPowerOfTwo
1092 && texImg
->Border
== 0
1093 && texImg
->Width
== texImg
->RowStride
1094 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1095 && minFilter
== magFilter
1096 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1097 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1098 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1099 if (minFilter
== GL_NEAREST
1100 && format
== MESA_FORMAT_RGB
1101 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1102 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1103 && ctx
->Depth
.Func
== GL_LESS
1104 && ctx
->Depth
.Mask
== GL_TRUE
)
1105 || swrast
->_RasterMask
== TEXTURE_BIT
)
1106 && ctx
->Polygon
.StippleFlag
== GL_FALSE
1107 && ctx
->Visual
.depthBits
<= 16) {
1108 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1109 USE(simple_z_textured_triangle
);
1112 USE(simple_textured_triangle
);
1116 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1117 USE(general_textured_triangle
);
1119 USE(affine_textured_triangle
);
1124 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1125 USE(general_textured_triangle
);
1127 USE(persp_textured_triangle
);
1132 /* general case textured triangles */
1133 if (ctx
->Texture
._EnabledCoordUnits
> 1) {
1134 USE(multitextured_triangle
);
1137 USE(general_textured_triangle
);
1142 ASSERT(!ctx
->Texture
._EnabledCoordUnits
);
1143 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1144 /* smooth shaded, no texturing, stippled or some raster ops */
1146 USE(smooth_rgba_triangle
);
1149 USE(smooth_ci_triangle
);
1153 /* flat shaded, no texturing, stippled or some raster ops */
1155 USE(flat_rgba_triangle
);
1158 USE(flat_ci_triangle
);
1163 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1164 USE(_swrast_feedback_triangle
);
1167 /* GL_SELECT mode */
1168 USE(_swrast_select_triangle
);