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
105 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
107 ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
108 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
109 span.interpMask |= SPAN_RGBA; \
110 span.red = ChanToFixed(v2->color[0]); \
111 span.green = ChanToFixed(v2->color[1]); \
112 span.blue = ChanToFixed(v2->color[2]); \
113 span.alpha = ChanToFixed(v2->color[3]); \
115 span.greenStep = 0; \
118 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
119 #include "s_tritemp.h"
124 * Render a smooth-shaded RGBA triangle.
126 #define NAME smooth_rgba_triangle
129 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
131 #define INTERP_ALPHA 1
134 /* texturing must be off */ \
135 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \
136 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
138 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
139 #include "s_tritemp.h"
144 * Render an RGB, GL_DECAL, textured triangle.
145 * Interpolate S,T only w/out mipmapping or perspective correction.
149 #define NAME simple_textured_triangle
150 #define INTERP_INT_TEX 1
151 #define S_SCALE twidth
152 #define T_SCALE theight
155 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
156 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
157 const GLint b = obj->BaseLevel; \
158 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
159 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
160 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
161 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
162 const GLint smask = obj->Image[0][b]->Width - 1; \
163 const GLint tmask = obj->Image[0][b]->Height - 1; \
165 /* this shouldn't happen */ \
169 #define RENDER_SPAN( span ) \
171 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
172 span.intTex[1] -= FIXED_HALF; \
173 for (i = 0; i < span.end; i++) { \
174 GLint s = FixedToInt(span.intTex[0]) & smask; \
175 GLint t = FixedToInt(span.intTex[1]) & tmask; \
176 GLint pos = (t << twidth_log2) + s; \
177 pos = pos + pos + pos; /* multiply by 3 */ \
178 span.array->rgb[i][RCOMP] = texture[pos]; \
179 span.array->rgb[i][GCOMP] = texture[pos+1]; \
180 span.array->rgb[i][BCOMP] = texture[pos+2]; \
181 span.intTex[0] += span.intTexStep[0]; \
182 span.intTex[1] += span.intTexStep[1]; \
184 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
185 (CONST GLchan (*)[3]) span.array->rgb,\
187 #include "s_tritemp.h"
192 * Render an RGB, GL_DECAL, textured triangle.
193 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
194 * perspective correction.
195 * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
199 #define NAME simple_z_textured_triangle
201 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
202 #define INTERP_INT_TEX 1
203 #define S_SCALE twidth
204 #define T_SCALE theight
207 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
208 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
209 const GLint b = obj->BaseLevel; \
210 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
211 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
212 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
213 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
214 const GLint smask = obj->Image[0][b]->Width - 1; \
215 const GLint tmask = obj->Image[0][b]->Height - 1; \
217 /* this shouldn't happen */ \
221 #define RENDER_SPAN( span ) \
223 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
224 span.intTex[1] -= FIXED_HALF; \
225 for (i = 0; i < span.end; i++) { \
226 const GLdepth z = FixedToDepth(span.z); \
228 GLint s = FixedToInt(span.intTex[0]) & smask; \
229 GLint t = FixedToInt(span.intTex[1]) & tmask; \
230 GLint pos = (t << twidth_log2) + s; \
231 pos = pos + pos + pos; /* multiply by 3 */ \
232 span.array->rgb[i][RCOMP] = texture[pos]; \
233 span.array->rgb[i][GCOMP] = texture[pos+1]; \
234 span.array->rgb[i][BCOMP] = texture[pos+2]; \
236 span.array->mask[i] = 1; \
239 span.array->mask[i] = 0; \
241 span.intTex[0] += span.intTexStep[0]; \
242 span.intTex[1] += span.intTexStep[1]; \
243 span.z += span.zStep; \
245 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
246 (CONST GLchan (*)[3]) span.array->rgb,\
248 #include "s_tritemp.h"
252 #if CHAN_TYPE != GL_FLOAT
261 const GLchan
*texture
;
262 GLfixed er
, eg
, eb
, ea
;
263 GLint tbytesline
, tsize
;
267 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
268 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
272 affine_span(GLcontext
*ctx
, struct sw_span
*span
,
273 struct affine_info
*info
)
275 GLchan sample
[4]; /* the filtered texture sample */
277 /* Instead of defining a function for each mode, a test is done
278 * between the outer and inner loops. This is to reduce code size
279 * and complexity. Observe that an optimizing compiler kills
280 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
283 #define NEAREST_RGB \
284 sample[RCOMP] = tex00[RCOMP]; \
285 sample[GCOMP] = tex00[GCOMP]; \
286 sample[BCOMP] = tex00[BCOMP]; \
287 sample[ACOMP] = CHAN_MAX
290 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
291 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
292 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
293 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
294 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
295 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
296 sample[ACOMP] = CHAN_MAX
298 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
300 #define LINEAR_RGBA \
301 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
302 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT;\
303 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
304 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT;\
305 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
306 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT;\
307 sample[ACOMP] = (ti * (si * tex00[3] + sf * tex01[3]) + \
308 tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT
311 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
312 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
313 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
314 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
317 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
318 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
319 >> (FIXED_SHIFT + 8); \
320 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
321 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
322 >> (FIXED_SHIFT + 8); \
323 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
324 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
325 >> (FIXED_SHIFT + 8); \
326 dest[ACOMP] = FixedToInt(span->alpha)
329 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
330 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
331 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
332 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
333 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
334 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
335 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
337 #define REPLACE COPY_CHAN4(dest, sample)
341 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
342 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
343 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
344 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
345 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
346 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
347 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
352 #define NEAREST_RGB_REPLACE \
354 dest[0] = sample[0]; \
355 dest[1] = sample[1]; \
356 dest[2] = sample[2]; \
357 dest[3] = FixedToInt(span->alpha);
359 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
361 #define SPAN_NEAREST(DO_TEX,COMP) \
362 for (i = 0; i < span->end; i++) { \
363 /* Isn't it necessary to use FixedFloor below?? */ \
364 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
365 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
366 GLint pos = (t << info->twidth_log2) + s; \
367 const GLchan *tex00 = info->texture + COMP * pos; \
369 span->red += span->redStep; \
370 span->green += span->greenStep; \
371 span->blue += span->blueStep; \
372 span->alpha += span->alphaStep; \
373 span->intTex[0] += span->intTexStep[0]; \
374 span->intTex[1] += span->intTexStep[1]; \
378 #define SPAN_LINEAR(DO_TEX,COMP) \
379 for (i = 0; i < span->end; i++) { \
380 /* Isn't it necessary to use FixedFloor below?? */ \
381 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
382 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
383 GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
384 GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
385 GLfixed si = FIXED_FRAC_MASK - sf; \
386 GLfixed ti = FIXED_FRAC_MASK - tf; \
387 GLint pos = (t << info->twidth_log2) + s; \
388 const GLchan *tex00 = info->texture + COMP * pos; \
389 const GLchan *tex10 = tex00 + info->tbytesline; \
390 const GLchan *tex01 = tex00 + COMP; \
391 const GLchan *tex11 = tex10 + COMP; \
394 if (t == info->tmask) { \
395 tex10 -= info->tsize; \
396 tex11 -= info->tsize; \
398 if (s == info->smask) { \
399 tex01 -= info->tbytesline; \
400 tex11 -= info->tbytesline; \
403 span->red += span->redStep; \
404 span->green += span->greenStep; \
405 span->blue += span->blueStep; \
406 span->alpha += span->alphaStep; \
407 span->intTex[0] += span->intTexStep[0]; \
408 span->intTex[1] += span->intTexStep[1]; \
414 GLchan
*dest
= span
->array
->rgba
[0];
416 span
->intTex
[0] -= FIXED_HALF
;
417 span
->intTex
[1] -= FIXED_HALF
;
418 switch (info
->filter
) {
420 switch (info
->format
) {
422 switch (info
->envmode
) {
424 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
428 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
431 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
434 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
437 _mesa_problem(ctx
, "bad tex env mode in SPAN_LINEAR");
442 switch(info
->envmode
) {
444 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
447 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
450 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
453 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
456 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
459 _mesa_problem(ctx
, "bad tex env mode (2) in SPAN_LINEAR");
467 span
->intTex
[0] -= FIXED_HALF
;
468 span
->intTex
[1] -= FIXED_HALF
;
469 switch (info
->format
) {
471 switch (info
->envmode
) {
473 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
477 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
480 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
483 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
486 _mesa_problem(ctx
, "bad tex env mode (3) in SPAN_LINEAR");
491 switch (info
->envmode
) {
493 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
496 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
499 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
502 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
505 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
508 _mesa_problem(ctx
, "bad tex env mode (4) in SPAN_LINEAR");
515 span
->interpMask
&= ~SPAN_RGBA
;
516 ASSERT(span
->arrayMask
& SPAN_RGBA
);
517 _swrast_write_rgba_span(ctx
, span
);
526 * Render an RGB/RGBA textured triangle without perspective correction.
528 #define NAME affine_textured_triangle
531 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
533 #define INTERP_ALPHA 1
534 #define INTERP_INT_TEX 1
535 #define S_SCALE twidth
536 #define T_SCALE theight
539 struct affine_info info; \
540 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
541 struct gl_texture_object *obj = unit->Current2D; \
542 const GLint b = obj->BaseLevel; \
543 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
544 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
545 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
546 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
547 info.smask = obj->Image[0][b]->Width - 1; \
548 info.tmask = obj->Image[0][b]->Height - 1; \
549 info.format = obj->Image[0][b]->Format; \
550 info.filter = obj->MinFilter; \
551 info.envmode = unit->EnvMode; \
552 span.arrayMask |= SPAN_RGBA; \
554 if (info.envmode == GL_BLEND) { \
555 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
556 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
557 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
558 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
559 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
561 if (!info.texture) { \
562 /* this shouldn't happen */ \
566 switch (info.format) { \
570 info.tbytesline = obj->Image[0][b]->Width; \
572 case GL_LUMINANCE_ALPHA: \
573 info.tbytesline = obj->Image[0][b]->Width * 2; \
576 info.tbytesline = obj->Image[0][b]->Width * 3; \
579 info.tbytesline = obj->Image[0][b]->Width * 4; \
582 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
585 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
587 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
589 #include "s_tritemp.h"
600 const GLchan
*texture
;
601 GLfixed er
, eg
, eb
, ea
; /* texture env color */
602 GLint tbytesline
, tsize
;
607 fast_persp_span(GLcontext
*ctx
, struct sw_span
*span
,
608 struct persp_info
*info
)
610 GLchan sample
[4]; /* the filtered texture sample */
612 /* Instead of defining a function for each mode, a test is done
613 * between the outer and inner loops. This is to reduce code size
614 * and complexity. Observe that an optimizing compiler kills
615 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
617 #define SPAN_NEAREST(DO_TEX,COMP) \
618 for (i = 0; i < span->end; i++) { \
619 GLdouble invQ = tex_coord[2] ? \
620 (1.0 / tex_coord[2]) : 1.0; \
621 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
622 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
623 GLint s = IFLOOR(s_tmp) & info->smask; \
624 GLint t = IFLOOR(t_tmp) & info->tmask; \
625 GLint pos = (t << info->twidth_log2) + s; \
626 const GLchan *tex00 = info->texture + COMP * pos; \
628 span->red += span->redStep; \
629 span->green += span->greenStep; \
630 span->blue += span->blueStep; \
631 span->alpha += span->alphaStep; \
632 tex_coord[0] += tex_step[0]; \
633 tex_coord[1] += tex_step[1]; \
634 tex_coord[2] += tex_step[2]; \
638 #define SPAN_LINEAR(DO_TEX,COMP) \
639 for (i = 0; i < span->end; i++) { \
640 GLdouble invQ = tex_coord[2] ? \
641 (1.0 / tex_coord[2]) : 1.0; \
642 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
643 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
644 GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
645 GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
646 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
647 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
648 GLfixed sf = s_fix & FIXED_FRAC_MASK; \
649 GLfixed tf = t_fix & FIXED_FRAC_MASK; \
650 GLfixed si = FIXED_FRAC_MASK - sf; \
651 GLfixed ti = FIXED_FRAC_MASK - tf; \
652 GLint pos = (t << info->twidth_log2) + s; \
653 const GLchan *tex00 = info->texture + COMP * pos; \
654 const GLchan *tex10 = tex00 + info->tbytesline; \
655 const GLchan *tex01 = tex00 + COMP; \
656 const GLchan *tex11 = tex10 + COMP; \
659 if (t == info->tmask) { \
660 tex10 -= info->tsize; \
661 tex11 -= info->tsize; \
663 if (s == info->smask) { \
664 tex01 -= info->tbytesline; \
665 tex11 -= info->tbytesline; \
668 span->red += span->redStep; \
669 span->green += span->greenStep; \
670 span->blue += span->blueStep; \
671 span->alpha += span->alphaStep; \
672 tex_coord[0] += tex_step[0]; \
673 tex_coord[1] += tex_step[1]; \
674 tex_coord[2] += tex_step[2]; \
679 GLfloat tex_coord
[3], tex_step
[3];
680 GLchan
*dest
= span
->array
->rgba
[0];
682 tex_coord
[0] = span
->tex
[0][0] * (info
->smask
+ 1);
683 tex_step
[0] = span
->texStepX
[0][0] * (info
->smask
+ 1);
684 tex_coord
[1] = span
->tex
[0][1] * (info
->tmask
+ 1);
685 tex_step
[1] = span
->texStepX
[0][1] * (info
->tmask
+ 1);
686 /* span->tex[0][2] only if 3D-texturing, here only 2D */
687 tex_coord
[2] = span
->tex
[0][3];
688 tex_step
[2] = span
->texStepX
[0][3];
690 switch (info
->filter
) {
692 switch (info
->format
) {
694 switch (info
->envmode
) {
696 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
700 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
703 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
706 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
709 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
714 switch(info
->envmode
) {
716 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
719 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
722 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
725 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
728 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
731 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
739 switch (info
->format
) {
741 switch (info
->envmode
) {
743 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
747 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
750 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
753 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
756 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
761 switch (info
->envmode
) {
763 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
766 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
769 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
772 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
775 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
778 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
786 ASSERT(span
->arrayMask
& SPAN_RGBA
);
787 _swrast_write_rgba_span(ctx
, span
);
795 * Render an perspective corrected RGB/RGBA textured triangle.
796 * The Q (aka V in Mesa) coordinate must be zero such that the divide
797 * by interpolated Q/W comes out right.
800 #define NAME persp_textured_triangle
804 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
806 #define INTERP_ALPHA 1
810 struct persp_info info; \
811 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
812 const struct gl_texture_object *obj = unit->Current2D; \
813 const GLint b = obj->BaseLevel; \
814 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
815 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
816 info.smask = obj->Image[0][b]->Width - 1; \
817 info.tmask = obj->Image[0][b]->Height - 1; \
818 info.format = obj->Image[0][b]->Format; \
819 info.filter = obj->MinFilter; \
820 info.envmode = unit->EnvMode; \
822 if (info.envmode == GL_BLEND) { \
823 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
824 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
825 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
826 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
827 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
829 if (!info.texture) { \
830 /* this shouldn't happen */ \
834 switch (info.format) { \
838 info.tbytesline = obj->Image[0][b]->Width; \
840 case GL_LUMINANCE_ALPHA: \
841 info.tbytesline = obj->Image[0][b]->Width * 2; \
844 info.tbytesline = obj->Image[0][b]->Width * 3; \
847 info.tbytesline = obj->Image[0][b]->Width * 4; \
850 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
853 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
855 #define RENDER_SPAN( span ) \
856 span.interpMask &= ~SPAN_RGBA; \
857 span.arrayMask |= SPAN_RGBA; \
858 fast_persp_span(ctx, &span, &info);
860 #include "s_tritemp.h"
863 #endif /* CHAN_BITS != GL_FLOAT */
869 * Render a smooth-shaded, textured, RGBA triangle.
870 * Interpolate S,T,R with perspective correction, w/out mipmapping.
872 #define NAME general_textured_triangle
876 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
878 #define INTERP_SPEC 1
879 #define INTERP_ALPHA 1
881 #define RENDER_SPAN( span ) _swrast_write_texture_span(ctx, &span);
882 #include "s_tritemp.h"
887 * This is the big one!
888 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates.
891 #define NAME multitextured_triangle
895 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
897 #define INTERP_ALPHA 1
898 #define INTERP_SPEC 1
899 #define INTERP_MULTITEX 1
900 #define RENDER_SPAN( span ) _swrast_write_texture_span(ctx, &span);
901 #include "s_tritemp.h"
906 * Special tri function for occlusion testing
908 #define NAME occlusion_zless_triangle
909 #define DO_OCCLUSION_TEST
911 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
913 if (ctx->OcclusionResult && !ctx->Occlusion.Active) { \
916 #define RENDER_SPAN( span ) \
918 for (i = 0; i < span.end; i++) { \
919 GLdepth z = FixedToDepth(span.z); \
921 ctx->OcclusionResult = GL_TRUE; \
922 ctx->Occlusion.PassedCounter++; \
924 span.z += span.zStep; \
926 #include "s_tritemp.h"
931 nodraw_triangle( GLcontext
*ctx
,
936 (void) (ctx
&& v0
&& v1
&& v2
);
941 * This is used when separate specular color is enabled, but not
942 * texturing. We add the specular color to the primary color,
943 * draw the triangle, then restore the original primary color.
944 * Inefficient, but seldom needed.
946 void _swrast_add_spec_terms_triangle( GLcontext
*ctx
,
951 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
952 SWvertex
*ncv1
= (SWvertex
*)v1
;
953 SWvertex
*ncv2
= (SWvertex
*)v2
;
954 #if CHAN_TYPE == GL_FLOAT
955 GLfloat rSum
, gSum
, bSum
;
957 GLint rSum
, gSum
, bSum
;
960 /* save original colors */
961 COPY_CHAN4( c
[0], ncv0
->color
);
962 COPY_CHAN4( c
[1], ncv1
->color
);
963 COPY_CHAN4( c
[2], ncv2
->color
);
965 rSum
= ncv0
->color
[0] + ncv0
->specular
[0];
966 gSum
= ncv0
->color
[1] + ncv0
->specular
[1];
967 bSum
= ncv0
->color
[2] + ncv0
->specular
[2];
968 ncv0
->color
[0] = MIN2(rSum
, CHAN_MAX
);
969 ncv0
->color
[1] = MIN2(gSum
, CHAN_MAX
);
970 ncv0
->color
[2] = MIN2(bSum
, CHAN_MAX
);
972 rSum
= ncv1
->color
[0] + ncv1
->specular
[0];
973 gSum
= ncv1
->color
[1] + ncv1
->specular
[1];
974 bSum
= ncv1
->color
[2] + ncv1
->specular
[2];
975 ncv1
->color
[0] = MIN2(rSum
, CHAN_MAX
);
976 ncv1
->color
[1] = MIN2(gSum
, CHAN_MAX
);
977 ncv1
->color
[2] = MIN2(bSum
, CHAN_MAX
);
979 rSum
= ncv2
->color
[0] + ncv2
->specular
[0];
980 gSum
= ncv2
->color
[1] + ncv2
->specular
[1];
981 bSum
= ncv2
->color
[2] + ncv2
->specular
[2];
982 ncv2
->color
[0] = MIN2(rSum
, CHAN_MAX
);
983 ncv2
->color
[1] = MIN2(gSum
, CHAN_MAX
);
984 ncv2
->color
[2] = MIN2(bSum
, CHAN_MAX
);
986 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
987 /* restore original colors */
988 COPY_CHAN4( ncv0
->color
, c
[0] );
989 COPY_CHAN4( ncv1
->color
, c
[1] );
990 COPY_CHAN4( ncv2
->color
, c
[2] );
997 /* record the current triangle function name */
998 const char *_mesa_triFuncName
= NULL
;
1000 #define USE(triFunc) \
1002 _mesa_triFuncName = #triFunc; \
1003 /*printf("%s\n", _mesa_triFuncName);*/ \
1004 swrast->Triangle = triFunc; \
1009 #define USE(triFunc) swrast->Triangle = triFunc;
1017 * Determine which triangle rendering function to use given the current
1018 * rendering context.
1020 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1021 * remove tests to this code.
1024 _swrast_choose_triangle( GLcontext
*ctx
)
1026 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1027 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
1029 if (ctx
->Polygon
.CullFlag
&&
1030 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1031 USE(nodraw_triangle
);
1035 if (ctx
->RenderMode
==GL_RENDER
) {
1037 if (ctx
->Polygon
.SmoothFlag
) {
1038 _swrast_set_aa_triangle_function(ctx
);
1039 ASSERT(swrast
->Triangle
);
1043 /* special case for occlusion testing */
1044 if ((ctx
->Depth
.OcclusionTest
|| ctx
->Occlusion
.Active
) &&
1046 ctx
->Depth
.Mask
== GL_FALSE
&&
1047 ctx
->Depth
.Func
== GL_LESS
&&
1048 !ctx
->Stencil
.Enabled
) {
1050 ctx
->Color
.ColorMask
[0] == 0 &&
1051 ctx
->Color
.ColorMask
[1] == 0 &&
1052 ctx
->Color
.ColorMask
[2] == 0 &&
1053 ctx
->Color
.ColorMask
[3] == 0)
1055 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1056 USE(occlusion_zless_triangle
);
1061 if (ctx
->Texture
._EnabledCoordUnits
|| ctx
->FragmentProgram
._Enabled
) {
1062 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1063 const struct gl_texture_object
*texObj2D
;
1064 const struct gl_texture_image
*texImg
;
1065 GLenum minFilter
, magFilter
, envMode
;
1067 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1068 texImg
= texObj2D
? texObj2D
->Image
[0][texObj2D
->BaseLevel
] : NULL
;
1069 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1070 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1071 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1072 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1074 /* First see if we can use an optimized 2-D texture function */
1075 if (ctx
->Texture
._EnabledCoordUnits
== 1
1076 && !ctx
->FragmentProgram
._Enabled
1077 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1078 && texObj2D
->WrapS
==GL_REPEAT
1079 && texObj2D
->WrapT
==GL_REPEAT
1080 && texObj2D
->_IsPowerOfTwo
1081 && texImg
->Border
==0
1082 && texImg
->Width
== texImg
->RowStride
1083 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1084 && minFilter
== magFilter
1085 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1086 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1087 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1088 if (minFilter
== GL_NEAREST
1089 && format
== MESA_FORMAT_RGB
1090 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1091 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1092 && ctx
->Depth
.Func
== GL_LESS
1093 && ctx
->Depth
.Mask
== GL_TRUE
)
1094 || swrast
->_RasterMask
== TEXTURE_BIT
)
1095 && ctx
->Polygon
.StippleFlag
== GL_FALSE
1096 && ctx
->Visual
.depthBits
<= 16) {
1097 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1098 USE(simple_z_textured_triangle
);
1101 USE(simple_textured_triangle
);
1105 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1106 USE(general_textured_triangle
);
1108 USE(affine_textured_triangle
);
1113 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1114 USE(general_textured_triangle
);
1116 USE(persp_textured_triangle
);
1121 /* general case textured triangles */
1122 if (ctx
->Texture
._EnabledCoordUnits
> 1) {
1123 USE(multitextured_triangle
);
1126 USE(general_textured_triangle
);
1131 ASSERT(!ctx
->Texture
._EnabledCoordUnits
);
1132 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1133 /* smooth shaded, no texturing, stippled or some raster ops */
1135 USE(smooth_rgba_triangle
);
1138 USE(smooth_ci_triangle
);
1142 /* flat shaded, no texturing, stippled or some raster ops */
1144 USE(flat_rgba_triangle
);
1147 USE(flat_ci_triangle
);
1152 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1153 USE(_swrast_feedback_triangle
);
1156 /* GL_SELECT mode */
1157 USE(_swrast_select_triangle
);