2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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"
39 #include "s_aatriangle.h"
40 #include "s_context.h"
42 #include "s_feedback.h"
44 #include "s_triangle.h"
48 * Just used for feedback mode.
51 _swrast_culltriangle( GLcontext
*ctx
,
56 GLfloat ex
= v1
->win
[0] - v0
->win
[0];
57 GLfloat ey
= v1
->win
[1] - v0
->win
[1];
58 GLfloat fx
= v2
->win
[0] - v0
->win
[0];
59 GLfloat fy
= v2
->win
[1] - v0
->win
[1];
60 GLfloat c
= ex
*fy
-ey
*fx
;
62 if (c
* SWRAST_CONTEXT(ctx
)->_BackfaceSign
> 0)
71 * Render a flat-shaded color index triangle.
73 #define NAME flat_ci_triangle
77 span.interpMask |= SPAN_INDEX; \
78 span.index = FloatToFixed(v2->index);\
80 #define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
81 #include "s_tritemp.h"
86 * Render a smooth-shaded color index triangle.
88 #define NAME smooth_ci_triangle
91 #define INTERP_INDEX 1
92 #define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
93 #include "s_tritemp.h"
98 * Render a flat-shaded RGBA triangle.
100 #define NAME flat_rgba_triangle
104 ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
105 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
106 span.interpMask |= SPAN_RGBA; \
107 span.red = ChanToFixed(v2->color[0]); \
108 span.green = ChanToFixed(v2->color[1]); \
109 span.blue = ChanToFixed(v2->color[2]); \
110 span.alpha = ChanToFixed(v2->color[3]); \
112 span.greenStep = 0; \
115 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
116 #include "s_tritemp.h"
121 * Render a smooth-shaded RGBA triangle.
123 #define NAME smooth_rgba_triangle
127 #define INTERP_ALPHA 1
130 /* texturing must be off */ \
131 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \
132 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
134 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
135 #include "s_tritemp.h"
140 * Render an RGB, GL_DECAL, textured triangle.
141 * Interpolate S,T only w/out mipmapping or perspective correction.
145 #define NAME simple_textured_triangle
146 #define INTERP_INT_TEX 1
147 #define S_SCALE twidth
148 #define T_SCALE theight
151 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];\
152 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
153 const GLint b = obj->BaseLevel; \
154 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
155 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
156 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
157 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
158 const GLint smask = obj->Image[0][b]->Width - 1; \
159 const GLint tmask = obj->Image[0][b]->Height - 1; \
161 /* this shouldn't happen */ \
165 #define RENDER_SPAN( span ) \
167 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
168 span.intTex[1] -= FIXED_HALF; \
169 for (i = 0; i < span.end; i++) { \
170 GLint s = FixedToInt(span.intTex[0]) & smask; \
171 GLint t = FixedToInt(span.intTex[1]) & tmask; \
172 GLint pos = (t << twidth_log2) + s; \
173 pos = pos + pos + pos; /* multiply by 3 */ \
174 span.array->rgb[i][RCOMP] = texture[pos]; \
175 span.array->rgb[i][GCOMP] = texture[pos+1]; \
176 span.array->rgb[i][BCOMP] = texture[pos+2]; \
177 span.intTex[0] += span.intTexStep[0]; \
178 span.intTex[1] += span.intTexStep[1]; \
180 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, span.array->rgb, NULL);
182 #include "s_tritemp.h"
187 * Render an RGB, GL_DECAL, textured triangle.
188 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
189 * perspective correction.
190 * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
194 #define NAME simple_z_textured_triangle
196 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
197 #define INTERP_INT_TEX 1
198 #define S_SCALE twidth
199 #define T_SCALE theight
202 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];\
203 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
204 const GLint b = obj->BaseLevel; \
205 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
206 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
207 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
208 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
209 const GLint smask = obj->Image[0][b]->Width - 1; \
210 const GLint tmask = obj->Image[0][b]->Height - 1; \
212 /* this shouldn't happen */ \
216 #define RENDER_SPAN( span ) \
218 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
219 span.intTex[1] -= FIXED_HALF; \
220 for (i = 0; i < span.end; i++) { \
221 const GLuint z = FixedToDepth(span.z); \
223 GLint s = FixedToInt(span.intTex[0]) & smask; \
224 GLint t = FixedToInt(span.intTex[1]) & tmask; \
225 GLint pos = (t << twidth_log2) + s; \
226 pos = pos + pos + pos; /* multiply by 3 */ \
227 span.array->rgb[i][RCOMP] = texture[pos]; \
228 span.array->rgb[i][GCOMP] = texture[pos+1]; \
229 span.array->rgb[i][BCOMP] = texture[pos+2]; \
231 span.array->mask[i] = 1; \
234 span.array->mask[i] = 0; \
236 span.intTex[0] += span.intTexStep[0]; \
237 span.intTex[1] += span.intTexStep[1]; \
238 span.z += span.zStep; \
240 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, \
241 span.array->rgb, span.array->mask);
243 #include "s_tritemp.h"
247 #if CHAN_TYPE != GL_FLOAT
256 const GLchan
*texture
;
257 GLfixed er
, eg
, eb
, ea
;
258 GLint tbytesline
, tsize
;
263 ilerp(GLint t
, GLint a
, GLint b
)
265 return a
+ ((t
* (b
- a
)) >> FIXED_SHIFT
);
269 ilerp_2d(GLint ia
, GLint ib
, GLint v00
, GLint v10
, GLint v01
, GLint v11
)
271 const GLint temp0
= ilerp(ia
, v00
, v10
);
272 const GLint temp1
= ilerp(ia
, v01
, v11
);
273 return ilerp(ib
, temp0
, temp1
);
277 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
278 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
282 affine_span(GLcontext
*ctx
, struct sw_span
*span
,
283 struct affine_info
*info
)
285 GLchan sample
[4]; /* the filtered texture sample */
287 /* Instead of defining a function for each mode, a test is done
288 * between the outer and inner loops. This is to reduce code size
289 * and complexity. Observe that an optimizing compiler kills
290 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
293 #define NEAREST_RGB \
294 sample[RCOMP] = tex00[RCOMP]; \
295 sample[GCOMP] = tex00[GCOMP]; \
296 sample[BCOMP] = tex00[BCOMP]; \
297 sample[ACOMP] = CHAN_MAX
300 sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
301 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
302 sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
303 sample[ACOMP] = CHAN_MAX;
305 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
307 #define LINEAR_RGBA \
308 sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
309 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
310 sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
311 sample[ACOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3])
314 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
315 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
316 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
317 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
320 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
321 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
322 >> (FIXED_SHIFT + 8); \
323 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
324 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
325 >> (FIXED_SHIFT + 8); \
326 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
327 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
328 >> (FIXED_SHIFT + 8); \
329 dest[ACOMP] = FixedToInt(span->alpha)
332 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
333 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
334 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
335 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
336 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
337 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
338 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
340 #define REPLACE COPY_CHAN4(dest, sample)
344 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
345 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
346 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
347 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
348 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
349 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
350 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
355 #define NEAREST_RGB_REPLACE \
357 dest[0] = sample[0]; \
358 dest[1] = sample[1]; \
359 dest[2] = sample[2]; \
360 dest[3] = FixedToInt(span->alpha);
362 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
364 #define SPAN_NEAREST(DO_TEX, COMPS) \
365 for (i = 0; i < span->end; i++) { \
366 /* Isn't it necessary to use FixedFloor below?? */ \
367 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
368 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
369 GLint pos = (t << info->twidth_log2) + s; \
370 const GLchan *tex00 = info->texture + COMPS * pos; \
372 span->red += span->redStep; \
373 span->green += span->greenStep; \
374 span->blue += span->blueStep; \
375 span->alpha += span->alphaStep; \
376 span->intTex[0] += span->intTexStep[0]; \
377 span->intTex[1] += span->intTexStep[1]; \
381 #define SPAN_LINEAR(DO_TEX, COMPS) \
382 for (i = 0; i < span->end; i++) { \
383 /* Isn't it necessary to use FixedFloor below?? */ \
384 const GLint s = FixedToInt(span->intTex[0]) & info->smask; \
385 const GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
386 const GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
387 const GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
388 const GLint pos = (t << info->twidth_log2) + s; \
389 const GLchan *tex00 = info->texture + COMPS * pos; \
390 const GLchan *tex10 = tex00 + info->tbytesline; \
391 const GLchan *tex01 = tex00 + COMPS; \
392 const GLchan *tex11 = tex10 + COMPS; \
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
531 #define INTERP_ALPHA 1
532 #define INTERP_INT_TEX 1
533 #define S_SCALE twidth
534 #define T_SCALE theight
537 struct affine_info info; \
538 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
539 struct gl_texture_object *obj = unit->Current2D; \
540 const GLint b = obj->BaseLevel; \
541 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
542 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
543 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
544 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
545 info.smask = obj->Image[0][b]->Width - 1; \
546 info.tmask = obj->Image[0][b]->Height - 1; \
547 info.format = obj->Image[0][b]->_BaseFormat; \
548 info.filter = obj->MinFilter; \
549 info.envmode = unit->EnvMode; \
550 span.arrayMask |= SPAN_RGBA; \
552 if (info.envmode == GL_BLEND) { \
553 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
554 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
555 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
556 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
557 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
559 if (!info.texture) { \
560 /* this shouldn't happen */ \
564 switch (info.format) { \
568 info.tbytesline = obj->Image[0][b]->Width; \
570 case GL_LUMINANCE_ALPHA: \
571 info.tbytesline = obj->Image[0][b]->Width * 2; \
574 info.tbytesline = obj->Image[0][b]->Width * 3; \
577 info.tbytesline = obj->Image[0][b]->Width * 4; \
580 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
583 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
585 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
587 #include "s_tritemp.h"
598 const GLchan
*texture
;
599 GLfixed er
, eg
, eb
, ea
; /* texture env color */
600 GLint tbytesline
, tsize
;
605 fast_persp_span(GLcontext
*ctx
, struct sw_span
*span
,
606 struct persp_info
*info
)
608 GLchan sample
[4]; /* the filtered texture sample */
610 /* Instead of defining a function for each mode, a test is done
611 * between the outer and inner loops. This is to reduce code size
612 * and complexity. Observe that an optimizing compiler kills
613 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
615 #define SPAN_NEAREST(DO_TEX,COMP) \
616 for (i = 0; i < span->end; i++) { \
617 GLdouble invQ = tex_coord[2] ? \
618 (1.0 / tex_coord[2]) : 1.0; \
619 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
620 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
621 GLint s = IFLOOR(s_tmp) & info->smask; \
622 GLint t = IFLOOR(t_tmp) & info->tmask; \
623 GLint pos = (t << info->twidth_log2) + s; \
624 const GLchan *tex00 = info->texture + COMP * pos; \
626 span->red += span->redStep; \
627 span->green += span->greenStep; \
628 span->blue += span->blueStep; \
629 span->alpha += span->alphaStep; \
630 tex_coord[0] += tex_step[0]; \
631 tex_coord[1] += tex_step[1]; \
632 tex_coord[2] += tex_step[2]; \
636 #define SPAN_LINEAR(DO_TEX,COMP) \
637 for (i = 0; i < span->end; i++) { \
638 GLdouble invQ = tex_coord[2] ? \
639 (1.0 / tex_coord[2]) : 1.0; \
640 const GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
641 const GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
642 const GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
643 const GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
644 const GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
645 const GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
646 const GLfixed sf = s_fix & FIXED_FRAC_MASK; \
647 const GLfixed tf = t_fix & FIXED_FRAC_MASK; \
648 const GLint pos = (t << info->twidth_log2) + s; \
649 const GLchan *tex00 = info->texture + COMP * pos; \
650 const GLchan *tex10 = tex00 + info->tbytesline; \
651 const GLchan *tex01 = tex00 + COMP; \
652 const GLchan *tex11 = tex10 + COMP; \
653 if (t == info->tmask) { \
654 tex10 -= info->tsize; \
655 tex11 -= info->tsize; \
657 if (s == info->smask) { \
658 tex01 -= info->tbytesline; \
659 tex11 -= info->tbytesline; \
662 span->red += span->redStep; \
663 span->green += span->greenStep; \
664 span->blue += span->blueStep; \
665 span->alpha += span->alphaStep; \
666 tex_coord[0] += tex_step[0]; \
667 tex_coord[1] += tex_step[1]; \
668 tex_coord[2] += tex_step[2]; \
673 GLfloat tex_coord
[3], tex_step
[3];
674 GLchan
*dest
= span
->array
->rgba
[0];
676 const GLuint savedTexEnable
= ctx
->Texture
._EnabledUnits
;
677 ctx
->Texture
._EnabledUnits
= 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
);
790 ctx
->Texture
._EnabledUnits
= savedTexEnable
;
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
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]->_BaseFormat; \
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
876 #define INTERP_SPEC 1
877 #define INTERP_ALPHA 1
879 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
880 #include "s_tritemp.h"
885 * This is the big one!
886 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates.
889 #define NAME multitextured_triangle
894 #define INTERP_ALPHA 1
895 #define INTERP_SPEC 1
896 #define INTERP_MULTITEX 1
897 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
898 #include "s_tritemp.h"
903 * Special tri function for occlusion testing
905 #define NAME occlusion_zless_triangle
908 struct gl_renderbuffer *rb = ctx->DrawBuffer->_DepthBuffer; \
909 struct gl_query_object *q = ctx->Query.CurrentOcclusionObject; \
910 ASSERT(ctx->Depth.Test); \
911 ASSERT(!ctx->Depth.Mask); \
912 ASSERT(ctx->Depth.Func == GL_LESS); \
916 #define RENDER_SPAN( span ) \
917 if (ctx->Visual.depthBits <= 16) { \
919 const GLushort *zRow = (const GLushort *) \
920 rb->GetPointer(ctx, rb, span.x, span.y); \
921 for (i = 0; i < span.end; i++) { \
922 GLuint z = FixedToDepth(span.z); \
926 span.z += span.zStep; \
931 const GLuint *zRow = (const GLuint *) \
932 rb->GetPointer(ctx, rb, span.x, span.y); \
933 for (i = 0; i < span.end; i++) { \
934 if ((GLuint)span.z < zRow[i]) { \
937 span.z += span.zStep; \
940 #include "s_tritemp.h"
945 nodraw_triangle( GLcontext
*ctx
,
950 (void) (ctx
&& v0
&& v1
&& v2
);
955 * This is used when separate specular color is enabled, but not
956 * texturing. We add the specular color to the primary color,
957 * draw the triangle, then restore the original primary color.
958 * Inefficient, but seldom needed.
960 void _swrast_add_spec_terms_triangle( GLcontext
*ctx
,
965 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
966 SWvertex
*ncv1
= (SWvertex
*)v1
;
967 SWvertex
*ncv2
= (SWvertex
*)v2
;
968 #if CHAN_TYPE == GL_FLOAT
969 GLfloat rSum
, gSum
, bSum
;
971 GLint rSum
, gSum
, bSum
;
974 /* save original colors */
975 COPY_CHAN4( c
[0], ncv0
->color
);
976 COPY_CHAN4( c
[1], ncv1
->color
);
977 COPY_CHAN4( c
[2], ncv2
->color
);
979 rSum
= ncv0
->color
[0] + ncv0
->specular
[0];
980 gSum
= ncv0
->color
[1] + ncv0
->specular
[1];
981 bSum
= ncv0
->color
[2] + ncv0
->specular
[2];
982 ncv0
->color
[0] = MIN2(rSum
, CHAN_MAX
);
983 ncv0
->color
[1] = MIN2(gSum
, CHAN_MAX
);
984 ncv0
->color
[2] = MIN2(bSum
, CHAN_MAX
);
986 rSum
= ncv1
->color
[0] + ncv1
->specular
[0];
987 gSum
= ncv1
->color
[1] + ncv1
->specular
[1];
988 bSum
= ncv1
->color
[2] + ncv1
->specular
[2];
989 ncv1
->color
[0] = MIN2(rSum
, CHAN_MAX
);
990 ncv1
->color
[1] = MIN2(gSum
, CHAN_MAX
);
991 ncv1
->color
[2] = MIN2(bSum
, CHAN_MAX
);
993 rSum
= ncv2
->color
[0] + ncv2
->specular
[0];
994 gSum
= ncv2
->color
[1] + ncv2
->specular
[1];
995 bSum
= ncv2
->color
[2] + ncv2
->specular
[2];
996 ncv2
->color
[0] = MIN2(rSum
, CHAN_MAX
);
997 ncv2
->color
[1] = MIN2(gSum
, CHAN_MAX
);
998 ncv2
->color
[2] = MIN2(bSum
, CHAN_MAX
);
1000 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
1001 /* restore original colors */
1002 COPY_CHAN4( ncv0
->color
, c
[0] );
1003 COPY_CHAN4( ncv1
->color
, c
[1] );
1004 COPY_CHAN4( ncv2
->color
, c
[2] );
1011 /* record the current triangle function name */
1012 const char *_mesa_triFuncName
= NULL
;
1014 #define USE(triFunc) \
1016 _mesa_triFuncName = #triFunc; \
1017 /*printf("%s\n", _mesa_triFuncName);*/ \
1018 swrast->Triangle = triFunc; \
1023 #define USE(triFunc) swrast->Triangle = triFunc;
1031 * Determine which triangle rendering function to use given the current
1032 * rendering context.
1034 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1035 * remove tests to this code.
1038 _swrast_choose_triangle( GLcontext
*ctx
)
1040 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1041 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
1043 if (ctx
->Polygon
.CullFlag
&&
1044 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1045 USE(nodraw_triangle
);
1049 if (ctx
->RenderMode
==GL_RENDER
) {
1051 if (ctx
->Polygon
.SmoothFlag
) {
1052 _swrast_set_aa_triangle_function(ctx
);
1053 ASSERT(swrast
->Triangle
);
1057 /* special case for occlusion testing */
1058 if (ctx
->Query
.CurrentOcclusionObject
&&
1060 ctx
->Depth
.Mask
== GL_FALSE
&&
1061 ctx
->Depth
.Func
== GL_LESS
&&
1062 !ctx
->Stencil
.Enabled
) {
1064 ctx
->Color
.ColorMask
[0] == 0 &&
1065 ctx
->Color
.ColorMask
[1] == 0 &&
1066 ctx
->Color
.ColorMask
[2] == 0 &&
1067 ctx
->Color
.ColorMask
[3] == 0)
1069 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1070 USE(occlusion_zless_triangle
);
1075 if (ctx
->Texture
._EnabledCoordUnits
|| ctx
->FragmentProgram
._Active
|| ctx
->ATIFragmentShader
._Enabled
) {
1076 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1077 const struct gl_texture_object
*texObj2D
;
1078 const struct gl_texture_image
*texImg
;
1079 GLenum minFilter
, magFilter
, envMode
;
1081 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1082 texImg
= texObj2D
? texObj2D
->Image
[0][texObj2D
->BaseLevel
] : NULL
;
1083 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1084 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1085 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1086 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1088 /* First see if we can use an optimized 2-D texture function */
1089 if (ctx
->Texture
._EnabledCoordUnits
== 0x1
1090 && !ctx
->FragmentProgram
._Active
1091 && !ctx
->ATIFragmentShader
._Enabled
1092 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1093 && texObj2D
->WrapS
== GL_REPEAT
1094 && texObj2D
->WrapT
== GL_REPEAT
1095 && texImg
->_IsPowerOfTwo
1096 && texImg
->Border
== 0
1097 && texImg
->Width
== texImg
->RowStride
1098 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1099 && minFilter
== magFilter
1100 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1101 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1102 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1103 if (minFilter
== GL_NEAREST
1104 && format
== MESA_FORMAT_RGB
1105 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1106 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1107 && ctx
->Depth
.Func
== GL_LESS
1108 && ctx
->Depth
.Mask
== GL_TRUE
)
1109 || swrast
->_RasterMask
== TEXTURE_BIT
)
1110 && ctx
->Polygon
.StippleFlag
== GL_FALSE
1111 && ctx
->Visual
.depthBits
<= 16) {
1112 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1113 USE(simple_z_textured_triangle
);
1116 USE(simple_textured_triangle
);
1120 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1121 USE(general_textured_triangle
);
1123 USE(affine_textured_triangle
);
1128 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1129 USE(general_textured_triangle
);
1131 USE(persp_textured_triangle
);
1136 /* general case textured triangles */
1137 if (ctx
->Texture
._EnabledCoordUnits
> 1) {
1138 USE(multitextured_triangle
);
1141 USE(general_textured_triangle
);
1146 ASSERT(!ctx
->Texture
._EnabledCoordUnits
);
1147 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1148 /* smooth shaded, no texturing, stippled or some raster ops */
1150 USE(smooth_rgba_triangle
);
1153 USE(smooth_ci_triangle
);
1157 /* flat shaded, no texturing, stippled or some raster ops */
1159 USE(flat_rgba_triangle
);
1162 USE(flat_ci_triangle
);
1167 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1168 USE(_swrast_feedback_triangle
);
1171 /* GL_SELECT mode */
1172 USE(_swrast_select_triangle
);