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.
198 #define NAME simple_z_textured_triangle
200 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
201 #define INTERP_INT_TEX 1
202 #define S_SCALE twidth
203 #define T_SCALE theight
206 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
207 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
208 const GLint b = obj->BaseLevel; \
209 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
210 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
211 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
212 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
213 const GLint smask = obj->Image[0][b]->Width - 1; \
214 const GLint tmask = obj->Image[0][b]->Height - 1; \
216 /* this shouldn't happen */ \
220 #define RENDER_SPAN( span ) \
222 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
223 span.intTex[1] -= FIXED_HALF; \
224 for (i = 0; i < span.end; i++) { \
225 const GLdepth z = FixedToDepth(span.z); \
227 GLint s = FixedToInt(span.intTex[0]) & smask; \
228 GLint t = FixedToInt(span.intTex[1]) & tmask; \
229 GLint pos = (t << twidth_log2) + s; \
230 pos = pos + pos + pos; /* multiply by 3 */ \
231 span.array->rgb[i][RCOMP] = texture[pos]; \
232 span.array->rgb[i][GCOMP] = texture[pos+1]; \
233 span.array->rgb[i][BCOMP] = texture[pos+2]; \
235 span.array->mask[i] = 1; \
238 span.array->mask[i] = 0; \
240 span.intTex[0] += span.intTexStep[0]; \
241 span.intTex[1] += span.intTexStep[1]; \
242 span.z += span.zStep; \
244 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
245 (CONST GLchan (*)[3]) span.array->rgb,\
247 #include "s_tritemp.h"
251 #if CHAN_TYPE != GL_FLOAT
260 const GLchan
*texture
;
261 GLfixed er
, eg
, eb
, ea
;
262 GLint tbytesline
, tsize
;
266 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
267 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
271 affine_span(GLcontext
*ctx
, struct sw_span
*span
,
272 struct affine_info
*info
)
274 GLchan sample
[4]; /* the filtered texture sample */
276 /* Instead of defining a function for each mode, a test is done
277 * between the outer and inner loops. This is to reduce code size
278 * and complexity. Observe that an optimizing compiler kills
279 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
282 #define NEAREST_RGB \
283 sample[RCOMP] = tex00[RCOMP]; \
284 sample[GCOMP] = tex00[GCOMP]; \
285 sample[BCOMP] = tex00[BCOMP]; \
286 sample[ACOMP] = CHAN_MAX
289 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
290 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
291 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
292 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
293 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
294 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
295 sample[ACOMP] = CHAN_MAX
297 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
299 #define LINEAR_RGBA \
300 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
301 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT;\
302 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
303 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT;\
304 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
305 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT;\
306 sample[ACOMP] = (ti * (si * tex00[3] + sf * tex01[3]) + \
307 tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT
310 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
311 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
312 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
313 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
316 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
317 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
318 >> (FIXED_SHIFT + 8); \
319 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
320 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
321 >> (FIXED_SHIFT + 8); \
322 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
323 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
324 >> (FIXED_SHIFT + 8); \
325 dest[ACOMP] = FixedToInt(span->alpha)
328 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
329 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
330 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
331 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
332 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
333 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
334 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
336 #define REPLACE COPY_CHAN4(dest, sample)
340 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
341 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
342 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
343 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
344 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
345 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
346 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
351 #define NEAREST_RGB_REPLACE \
353 dest[0] = sample[0]; \
354 dest[1] = sample[1]; \
355 dest[2] = sample[2]; \
356 dest[3] = FixedToInt(span->alpha);
358 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
360 #define SPAN_NEAREST(DO_TEX,COMP) \
361 for (i = 0; i < span->end; i++) { \
362 /* Isn't it necessary to use FixedFloor below?? */ \
363 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
364 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
365 GLint pos = (t << info->twidth_log2) + s; \
366 const GLchan *tex00 = info->texture + COMP * pos; \
368 span->red += span->redStep; \
369 span->green += span->greenStep; \
370 span->blue += span->blueStep; \
371 span->alpha += span->alphaStep; \
372 span->intTex[0] += span->intTexStep[0]; \
373 span->intTex[1] += span->intTexStep[1]; \
377 #define SPAN_LINEAR(DO_TEX,COMP) \
378 for (i = 0; i < span->end; i++) { \
379 /* Isn't it necessary to use FixedFloor below?? */ \
380 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
381 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
382 GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
383 GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
384 GLfixed si = FIXED_FRAC_MASK - sf; \
385 GLfixed ti = FIXED_FRAC_MASK - tf; \
386 GLint pos = (t << info->twidth_log2) + s; \
387 const GLchan *tex00 = info->texture + COMP * pos; \
388 const GLchan *tex10 = tex00 + info->tbytesline; \
389 const GLchan *tex01 = tex00 + COMP; \
390 const GLchan *tex11 = tex10 + COMP; \
393 if (t == info->tmask) { \
394 tex10 -= info->tsize; \
395 tex11 -= info->tsize; \
397 if (s == info->smask) { \
398 tex01 -= info->tbytesline; \
399 tex11 -= info->tbytesline; \
402 span->red += span->redStep; \
403 span->green += span->greenStep; \
404 span->blue += span->blueStep; \
405 span->alpha += span->alphaStep; \
406 span->intTex[0] += span->intTexStep[0]; \
407 span->intTex[1] += span->intTexStep[1]; \
413 GLchan
*dest
= span
->array
->rgba
[0];
415 span
->intTex
[0] -= FIXED_HALF
;
416 span
->intTex
[1] -= FIXED_HALF
;
417 switch (info
->filter
) {
419 switch (info
->format
) {
421 switch (info
->envmode
) {
423 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
427 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
430 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
433 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
436 _mesa_problem(ctx
, "bad tex env mode in SPAN_LINEAR");
441 switch(info
->envmode
) {
443 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
446 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
449 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
452 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
455 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
458 _mesa_problem(ctx
, "bad tex env mode (2) in SPAN_LINEAR");
466 span
->intTex
[0] -= FIXED_HALF
;
467 span
->intTex
[1] -= FIXED_HALF
;
468 switch (info
->format
) {
470 switch (info
->envmode
) {
472 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
476 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
479 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
482 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
485 _mesa_problem(ctx
, "bad tex env mode (3) in SPAN_LINEAR");
490 switch (info
->envmode
) {
492 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
495 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
498 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
501 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
504 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
507 _mesa_problem(ctx
, "bad tex env mode (4) in SPAN_LINEAR");
514 span
->interpMask
&= ~SPAN_RGBA
;
515 ASSERT(span
->arrayMask
& SPAN_RGBA
);
516 _swrast_write_rgba_span(ctx
, span
);
525 * Render an RGB/RGBA textured triangle without perspective correction.
527 #define NAME affine_textured_triangle
530 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
532 #define INTERP_ALPHA 1
533 #define INTERP_INT_TEX 1
534 #define S_SCALE twidth
535 #define T_SCALE theight
538 struct affine_info info; \
539 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
540 struct gl_texture_object *obj = unit->Current2D; \
541 const GLint b = obj->BaseLevel; \
542 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
543 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
544 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
545 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
546 info.smask = obj->Image[0][b]->Width - 1; \
547 info.tmask = obj->Image[0][b]->Height - 1; \
548 info.format = obj->Image[0][b]->Format; \
549 info.filter = obj->MinFilter; \
550 info.envmode = unit->EnvMode; \
551 span.arrayMask |= SPAN_RGBA; \
553 if (info.envmode == GL_BLEND) { \
554 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
555 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
556 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
557 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
558 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
560 if (!info.texture) { \
561 /* this shouldn't happen */ \
565 switch (info.format) { \
569 info.tbytesline = obj->Image[0][b]->Width; \
571 case GL_LUMINANCE_ALPHA: \
572 info.tbytesline = obj->Image[0][b]->Width * 2; \
575 info.tbytesline = obj->Image[0][b]->Width * 3; \
578 info.tbytesline = obj->Image[0][b]->Width * 4; \
581 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
584 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
586 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
588 #include "s_tritemp.h"
599 const GLchan
*texture
;
600 GLfixed er
, eg
, eb
, ea
; /* texture env color */
601 GLint tbytesline
, tsize
;
606 fast_persp_span(GLcontext
*ctx
, struct sw_span
*span
,
607 struct persp_info
*info
)
609 GLchan sample
[4]; /* the filtered texture sample */
611 /* Instead of defining a function for each mode, a test is done
612 * between the outer and inner loops. This is to reduce code size
613 * and complexity. Observe that an optimizing compiler kills
614 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
616 #define SPAN_NEAREST(DO_TEX,COMP) \
617 for (i = 0; i < span->end; i++) { \
618 GLdouble invQ = tex_coord[2] ? \
619 (1.0 / tex_coord[2]) : 1.0; \
620 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
621 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
622 GLint s = IFLOOR(s_tmp) & info->smask; \
623 GLint t = IFLOOR(t_tmp) & info->tmask; \
624 GLint pos = (t << info->twidth_log2) + s; \
625 const GLchan *tex00 = info->texture + COMP * pos; \
627 span->red += span->redStep; \
628 span->green += span->greenStep; \
629 span->blue += span->blueStep; \
630 span->alpha += span->alphaStep; \
631 tex_coord[0] += tex_step[0]; \
632 tex_coord[1] += tex_step[1]; \
633 tex_coord[2] += tex_step[2]; \
637 #define SPAN_LINEAR(DO_TEX,COMP) \
638 for (i = 0; i < span->end; i++) { \
639 GLdouble invQ = tex_coord[2] ? \
640 (1.0 / tex_coord[2]) : 1.0; \
641 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
642 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
643 GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
644 GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
645 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
646 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
647 GLfixed sf = s_fix & FIXED_FRAC_MASK; \
648 GLfixed tf = t_fix & FIXED_FRAC_MASK; \
649 GLfixed si = FIXED_FRAC_MASK - sf; \
650 GLfixed ti = FIXED_FRAC_MASK - tf; \
651 GLint pos = (t << info->twidth_log2) + s; \
652 const GLchan *tex00 = info->texture + COMP * pos; \
653 const GLchan *tex10 = tex00 + info->tbytesline; \
654 const GLchan *tex01 = tex00 + COMP; \
655 const GLchan *tex11 = tex10 + COMP; \
658 if (t == info->tmask) { \
659 tex10 -= info->tsize; \
660 tex11 -= info->tsize; \
662 if (s == info->smask) { \
663 tex01 -= info->tbytesline; \
664 tex11 -= info->tbytesline; \
667 span->red += span->redStep; \
668 span->green += span->greenStep; \
669 span->blue += span->blueStep; \
670 span->alpha += span->alphaStep; \
671 tex_coord[0] += tex_step[0]; \
672 tex_coord[1] += tex_step[1]; \
673 tex_coord[2] += tex_step[2]; \
678 GLfloat tex_coord
[3], tex_step
[3];
679 GLchan
*dest
= span
->array
->rgba
[0];
681 tex_coord
[0] = span
->tex
[0][0] * (info
->smask
+ 1);
682 tex_step
[0] = span
->texStepX
[0][0] * (info
->smask
+ 1);
683 tex_coord
[1] = span
->tex
[0][1] * (info
->tmask
+ 1);
684 tex_step
[1] = span
->texStepX
[0][1] * (info
->tmask
+ 1);
685 /* span->tex[0][2] only if 3D-texturing, here only 2D */
686 tex_coord
[2] = span
->tex
[0][3];
687 tex_step
[2] = span
->texStepX
[0][3];
689 switch (info
->filter
) {
691 switch (info
->format
) {
693 switch (info
->envmode
) {
695 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
699 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
702 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
705 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
708 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
713 switch(info
->envmode
) {
715 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
718 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
721 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
724 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
727 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
730 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
738 switch (info
->format
) {
740 switch (info
->envmode
) {
742 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
746 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
749 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
752 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
755 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
760 switch (info
->envmode
) {
762 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
765 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
768 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
771 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
774 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
777 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
785 ASSERT(span
->arrayMask
& SPAN_RGBA
);
786 _swrast_write_rgba_span(ctx
, span
);
794 * Render an perspective corrected RGB/RGBA textured triangle.
795 * The Q (aka V in Mesa) coordinate must be zero such that the divide
796 * by interpolated Q/W comes out right.
799 #define NAME persp_textured_triangle
803 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
805 #define INTERP_ALPHA 1
809 struct persp_info info; \
810 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
811 const struct gl_texture_object *obj = unit->Current2D; \
812 const GLint b = obj->BaseLevel; \
813 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
814 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
815 info.smask = obj->Image[0][b]->Width - 1; \
816 info.tmask = obj->Image[0][b]->Height - 1; \
817 info.format = obj->Image[0][b]->Format; \
818 info.filter = obj->MinFilter; \
819 info.envmode = unit->EnvMode; \
821 if (info.envmode == GL_BLEND) { \
822 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
823 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
824 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
825 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
826 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
828 if (!info.texture) { \
829 /* this shouldn't happen */ \
833 switch (info.format) { \
837 info.tbytesline = obj->Image[0][b]->Width; \
839 case GL_LUMINANCE_ALPHA: \
840 info.tbytesline = obj->Image[0][b]->Width * 2; \
843 info.tbytesline = obj->Image[0][b]->Width * 3; \
846 info.tbytesline = obj->Image[0][b]->Width * 4; \
849 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
852 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
854 #define RENDER_SPAN( span ) \
855 span.interpMask &= ~SPAN_RGBA; \
856 span.arrayMask |= SPAN_RGBA; \
857 fast_persp_span(ctx, &span, &info);
859 #include "s_tritemp.h"
862 #endif /* CHAN_BITS != GL_FLOAT */
868 * Render a smooth-shaded, textured, RGBA triangle.
869 * Interpolate S,T,R with perspective correction, w/out mipmapping.
871 #define NAME general_textured_triangle
875 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
877 #define INTERP_SPEC 1
878 #define INTERP_ALPHA 1
880 #define RENDER_SPAN( span ) _swrast_write_texture_span(ctx, &span);
881 #include "s_tritemp.h"
886 * This is the big one!
887 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates.
890 #define NAME multitextured_triangle
894 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
896 #define INTERP_ALPHA 1
897 #define INTERP_SPEC 1
898 #define INTERP_MULTITEX 1
899 #define RENDER_SPAN( span ) _swrast_write_texture_span(ctx, &span);
900 #include "s_tritemp.h"
905 * Special tri function for occlusion testing
907 #define NAME occlusion_zless_triangle
908 #define DO_OCCLUSION_TEST
910 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
912 if (ctx->OcclusionResult && !ctx->Occlusion.Active) { \
915 #define RENDER_SPAN( span ) \
917 for (i = 0; i < span.end; i++) { \
918 GLdepth z = FixedToDepth(span.z); \
920 ctx->OcclusionResult = GL_TRUE; \
921 ctx->Occlusion.PassedCounter++; \
923 span.z += span.zStep; \
925 #include "s_tritemp.h"
930 nodraw_triangle( GLcontext
*ctx
,
935 (void) (ctx
&& v0
&& v1
&& v2
);
940 * This is used when separate specular color is enabled, but not
941 * texturing. We add the specular color to the primary color,
942 * draw the triangle, then restore the original primary color.
943 * Inefficient, but seldom needed.
945 void _swrast_add_spec_terms_triangle( GLcontext
*ctx
,
950 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
951 SWvertex
*ncv1
= (SWvertex
*)v1
;
952 SWvertex
*ncv2
= (SWvertex
*)v2
;
953 #if CHAN_TYPE == GL_FLOAT
954 GLfloat rSum
, gSum
, bSum
;
956 GLint rSum
, gSum
, bSum
;
959 /* save original colors */
960 COPY_CHAN4( c
[0], ncv0
->color
);
961 COPY_CHAN4( c
[1], ncv1
->color
);
962 COPY_CHAN4( c
[2], ncv2
->color
);
964 rSum
= ncv0
->color
[0] + ncv0
->specular
[0];
965 gSum
= ncv0
->color
[1] + ncv0
->specular
[1];
966 bSum
= ncv0
->color
[2] + ncv0
->specular
[2];
967 ncv0
->color
[0] = MIN2(rSum
, CHAN_MAX
);
968 ncv0
->color
[1] = MIN2(gSum
, CHAN_MAX
);
969 ncv0
->color
[2] = MIN2(bSum
, CHAN_MAX
);
971 rSum
= ncv1
->color
[0] + ncv1
->specular
[0];
972 gSum
= ncv1
->color
[1] + ncv1
->specular
[1];
973 bSum
= ncv1
->color
[2] + ncv1
->specular
[2];
974 ncv1
->color
[0] = MIN2(rSum
, CHAN_MAX
);
975 ncv1
->color
[1] = MIN2(gSum
, CHAN_MAX
);
976 ncv1
->color
[2] = MIN2(bSum
, CHAN_MAX
);
978 rSum
= ncv2
->color
[0] + ncv2
->specular
[0];
979 gSum
= ncv2
->color
[1] + ncv2
->specular
[1];
980 bSum
= ncv2
->color
[2] + ncv2
->specular
[2];
981 ncv2
->color
[0] = MIN2(rSum
, CHAN_MAX
);
982 ncv2
->color
[1] = MIN2(gSum
, CHAN_MAX
);
983 ncv2
->color
[2] = MIN2(bSum
, CHAN_MAX
);
985 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
986 /* restore original colors */
987 COPY_CHAN4( ncv0
->color
, c
[0] );
988 COPY_CHAN4( ncv1
->color
, c
[1] );
989 COPY_CHAN4( ncv2
->color
, c
[2] );
996 /* record the current triangle function name */
997 const char *_mesa_triFuncName
= NULL
;
999 #define USE(triFunc) \
1001 _mesa_triFuncName = #triFunc; \
1002 /*printf("%s\n", _mesa_triFuncName);*/ \
1003 swrast->Triangle = triFunc; \
1008 #define USE(triFunc) swrast->Triangle = triFunc;
1016 * Determine which triangle rendering function to use given the current
1017 * rendering context.
1019 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1020 * remove tests to this code.
1023 _swrast_choose_triangle( GLcontext
*ctx
)
1025 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1026 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
1028 if (ctx
->Polygon
.CullFlag
&&
1029 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1030 USE(nodraw_triangle
);
1034 if (ctx
->RenderMode
==GL_RENDER
) {
1036 if (ctx
->Polygon
.SmoothFlag
) {
1037 _swrast_set_aa_triangle_function(ctx
);
1038 ASSERT(swrast
->Triangle
);
1042 /* special case for occlusion testing */
1043 if ((ctx
->Depth
.OcclusionTest
|| ctx
->Occlusion
.Active
) &&
1045 ctx
->Depth
.Mask
== GL_FALSE
&&
1046 ctx
->Depth
.Func
== GL_LESS
&&
1047 !ctx
->Stencil
.Enabled
) {
1049 ctx
->Color
.ColorMask
[0] == 0 &&
1050 ctx
->Color
.ColorMask
[1] == 0 &&
1051 ctx
->Color
.ColorMask
[2] == 0 &&
1052 ctx
->Color
.ColorMask
[3] == 0)
1054 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1055 USE(occlusion_zless_triangle
);
1060 if (ctx
->Texture
._EnabledCoordUnits
|| ctx
->FragmentProgram
.Enabled
) {
1061 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1062 const struct gl_texture_object
*texObj2D
;
1063 const struct gl_texture_image
*texImg
;
1064 GLenum minFilter
, magFilter
, envMode
;
1066 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1067 texImg
= texObj2D
? texObj2D
->Image
[0][texObj2D
->BaseLevel
] : NULL
;
1068 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1069 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1070 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1071 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1073 /* First see if we can use an optimized 2-D texture function */
1074 if (ctx
->Texture
._EnabledCoordUnits
== 1
1075 && !ctx
->FragmentProgram
.Enabled
1076 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1077 && texObj2D
->WrapS
==GL_REPEAT
1078 && texObj2D
->WrapT
==GL_REPEAT
1079 && texObj2D
->_IsPowerOfTwo
1080 && texImg
->Border
==0
1081 && texImg
->Width
== texImg
->RowStride
1082 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1083 && minFilter
== magFilter
1084 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1085 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1086 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1087 if (minFilter
== GL_NEAREST
1088 && format
== MESA_FORMAT_RGB
1089 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1090 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1091 && ctx
->Depth
.Func
== GL_LESS
1092 && ctx
->Depth
.Mask
== GL_TRUE
)
1093 || swrast
->_RasterMask
== TEXTURE_BIT
)
1094 && ctx
->Polygon
.StippleFlag
== GL_FALSE
) {
1095 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1096 USE(simple_z_textured_triangle
);
1099 USE(simple_textured_triangle
);
1103 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1104 USE(general_textured_triangle
);
1106 USE(affine_textured_triangle
);
1111 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1112 USE(general_textured_triangle
);
1114 USE(persp_textured_triangle
);
1119 /* general case textured triangles */
1120 if (ctx
->Texture
._EnabledCoordUnits
> 1) {
1121 USE(multitextured_triangle
);
1124 USE(general_textured_triangle
);
1129 ASSERT(!ctx
->Texture
._EnabledCoordUnits
);
1130 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1131 /* smooth shaded, no texturing, stippled or some raster ops */
1133 USE(smooth_rgba_triangle
);
1136 USE(smooth_ci_triangle
);
1140 /* flat shaded, no texturing, stippled or some raster ops */
1142 USE(flat_rgba_triangle
);
1145 USE(flat_ci_triangle
);
1150 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1151 USE(_swrast_feedback_triangle
);
1154 /* GL_SELECT mode */
1155 USE(_swrast_select_triangle
);