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"
41 #include "s_feedback.h"
43 #include "s_triangle.h"
47 * Just used for feedback mode.
50 _swrast_culltriangle( GLcontext
*ctx
,
55 GLfloat ex
= v1
->win
[0] - v0
->win
[0];
56 GLfloat ey
= v1
->win
[1] - v0
->win
[1];
57 GLfloat fx
= v2
->win
[0] - v0
->win
[0];
58 GLfloat fy
= v2
->win
[1] - v0
->win
[1];
59 GLfloat c
= ex
*fy
-ey
*fx
;
61 if (c
* SWRAST_CONTEXT(ctx
)->_BackfaceSign
> 0)
70 * Render a flat-shaded color index triangle.
72 #define NAME flat_ci_triangle
76 span.interpMask |= SPAN_INDEX; \
77 span.index = FloatToFixed(v2->index);\
79 #define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
80 #include "s_tritemp.h"
85 * Render a smooth-shaded color index triangle.
87 #define NAME smooth_ci_triangle
90 #define INTERP_INDEX 1
91 #define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
92 #include "s_tritemp.h"
97 * Render a flat-shaded RGBA triangle.
99 #define NAME flat_rgba_triangle
103 ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
104 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
105 span.interpMask |= SPAN_RGBA; \
106 span.red = ChanToFixed(v2->color[0]); \
107 span.green = ChanToFixed(v2->color[1]); \
108 span.blue = ChanToFixed(v2->color[2]); \
109 span.alpha = ChanToFixed(v2->color[3]); \
111 span.greenStep = 0; \
114 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
115 #include "s_tritemp.h"
120 * Render a smooth-shaded RGBA triangle.
122 #define NAME smooth_rgba_triangle
126 #define INTERP_ALPHA 1
129 /* texturing must be off */ \
130 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \
131 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
133 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
134 #include "s_tritemp.h"
139 * Render an RGB, GL_DECAL, textured triangle.
140 * Interpolate S,T only w/out mipmapping or perspective correction.
144 #define NAME simple_textured_triangle
145 #define INTERP_INT_TEX 1
146 #define S_SCALE twidth
147 #define T_SCALE theight
150 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];\
151 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
152 const GLint b = obj->BaseLevel; \
153 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
154 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
155 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
156 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
157 const GLint smask = obj->Image[0][b]->Width - 1; \
158 const GLint tmask = obj->Image[0][b]->Height - 1; \
160 /* this shouldn't happen */ \
164 #define RENDER_SPAN( span ) \
166 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
167 span.intTex[1] -= FIXED_HALF; \
168 for (i = 0; i < span.end; i++) { \
169 GLint s = FixedToInt(span.intTex[0]) & smask; \
170 GLint t = FixedToInt(span.intTex[1]) & tmask; \
171 GLint pos = (t << twidth_log2) + s; \
172 pos = pos + pos + pos; /* multiply by 3 */ \
173 span.array->rgb[i][RCOMP] = texture[pos]; \
174 span.array->rgb[i][GCOMP] = texture[pos+1]; \
175 span.array->rgb[i][BCOMP] = texture[pos+2]; \
176 span.intTex[0] += span.intTexStep[0]; \
177 span.intTex[1] += span.intTexStep[1]; \
179 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, span.array->rgb, NULL);
181 #include "s_tritemp.h"
186 * Render an RGB, GL_DECAL, textured triangle.
187 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
188 * perspective correction.
189 * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
193 #define NAME simple_z_textured_triangle
195 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
196 #define INTERP_INT_TEX 1
197 #define S_SCALE twidth
198 #define T_SCALE theight
201 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];\
202 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
203 const GLint b = obj->BaseLevel; \
204 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
205 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
206 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
207 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
208 const GLint smask = obj->Image[0][b]->Width - 1; \
209 const GLint tmask = obj->Image[0][b]->Height - 1; \
211 /* this shouldn't happen */ \
215 #define RENDER_SPAN( span ) \
217 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
218 span.intTex[1] -= FIXED_HALF; \
219 for (i = 0; i < span.end; i++) { \
220 const GLuint z = FixedToDepth(span.z); \
222 GLint s = FixedToInt(span.intTex[0]) & smask; \
223 GLint t = FixedToInt(span.intTex[1]) & tmask; \
224 GLint pos = (t << twidth_log2) + s; \
225 pos = pos + pos + pos; /* multiply by 3 */ \
226 span.array->rgb[i][RCOMP] = texture[pos]; \
227 span.array->rgb[i][GCOMP] = texture[pos+1]; \
228 span.array->rgb[i][BCOMP] = texture[pos+2]; \
230 span.array->mask[i] = 1; \
233 span.array->mask[i] = 0; \
235 span.intTex[0] += span.intTexStep[0]; \
236 span.intTex[1] += span.intTexStep[1]; \
237 span.z += span.zStep; \
239 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, \
240 span.array->rgb, span.array->mask);
242 #include "s_tritemp.h"
246 #if CHAN_TYPE != GL_FLOAT
255 const GLchan
*texture
;
256 GLfixed er
, eg
, eb
, ea
;
257 GLint tbytesline
, tsize
;
262 ilerp(GLint t
, GLint a
, GLint b
)
264 return a
+ ((t
* (b
- a
)) >> FIXED_SHIFT
);
268 ilerp_2d(GLint ia
, GLint ib
, GLint v00
, GLint v10
, GLint v01
, GLint v11
)
270 const GLint temp0
= ilerp(ia
, v00
, v10
);
271 const GLint temp1
= ilerp(ia
, v01
, v11
);
272 return ilerp(ib
, temp0
, temp1
);
276 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
277 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
281 affine_span(GLcontext
*ctx
, struct sw_span
*span
,
282 struct affine_info
*info
)
284 GLchan sample
[4]; /* the filtered texture sample */
286 /* Instead of defining a function for each mode, a test is done
287 * between the outer and inner loops. This is to reduce code size
288 * and complexity. Observe that an optimizing compiler kills
289 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
292 #define NEAREST_RGB \
293 sample[RCOMP] = tex00[RCOMP]; \
294 sample[GCOMP] = tex00[GCOMP]; \
295 sample[BCOMP] = tex00[BCOMP]; \
296 sample[ACOMP] = CHAN_MAX
299 sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
300 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
301 sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
302 sample[ACOMP] = CHAN_MAX;
304 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
306 #define LINEAR_RGBA \
307 sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
308 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
309 sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
310 sample[ACOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3])
313 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
314 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
315 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
316 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
319 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
320 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
321 >> (FIXED_SHIFT + 8); \
322 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
323 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
324 >> (FIXED_SHIFT + 8); \
325 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
326 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
327 >> (FIXED_SHIFT + 8); \
328 dest[ACOMP] = FixedToInt(span->alpha)
331 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
332 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
333 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
334 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
335 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
336 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
337 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
339 #define REPLACE COPY_CHAN4(dest, sample)
343 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
344 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
345 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
346 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
347 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
348 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
349 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
354 #define NEAREST_RGB_REPLACE \
356 dest[0] = sample[0]; \
357 dest[1] = sample[1]; \
358 dest[2] = sample[2]; \
359 dest[3] = FixedToInt(span->alpha);
361 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
363 #define SPAN_NEAREST(DO_TEX, COMPS) \
364 for (i = 0; i < span->end; i++) { \
365 /* Isn't it necessary to use FixedFloor below?? */ \
366 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
367 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
368 GLint pos = (t << info->twidth_log2) + s; \
369 const GLchan *tex00 = info->texture + COMPS * pos; \
371 span->red += span->redStep; \
372 span->green += span->greenStep; \
373 span->blue += span->blueStep; \
374 span->alpha += span->alphaStep; \
375 span->intTex[0] += span->intTexStep[0]; \
376 span->intTex[1] += span->intTexStep[1]; \
380 #define SPAN_LINEAR(DO_TEX, COMPS) \
381 for (i = 0; i < span->end; i++) { \
382 /* Isn't it necessary to use FixedFloor below?? */ \
383 const GLint s = FixedToInt(span->intTex[0]) & info->smask; \
384 const GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
385 const GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
386 const GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
387 const GLint pos = (t << info->twidth_log2) + s; \
388 const GLchan *tex00 = info->texture + COMPS * pos; \
389 const GLchan *tex10 = tex00 + info->tbytesline; \
390 const GLchan *tex01 = tex00 + COMPS; \
391 const GLchan *tex11 = tex10 + COMPS; \
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]->_BaseFormat; \
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 const GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
640 const GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
641 const GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
642 const GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
643 const GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
644 const GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
645 const GLfixed sf = s_fix & FIXED_FRAC_MASK; \
646 const GLfixed tf = t_fix & FIXED_FRAC_MASK; \
647 const GLint pos = (t << info->twidth_log2) + s; \
648 const GLchan *tex00 = info->texture + COMP * pos; \
649 const GLchan *tex10 = tex00 + info->tbytesline; \
650 const GLchan *tex01 = tex00 + COMP; \
651 const GLchan *tex11 = tex10 + COMP; \
652 if (t == info->tmask) { \
653 tex10 -= info->tsize; \
654 tex11 -= info->tsize; \
656 if (s == info->smask) { \
657 tex01 -= info->tbytesline; \
658 tex11 -= info->tbytesline; \
661 span->red += span->redStep; \
662 span->green += span->greenStep; \
663 span->blue += span->blueStep; \
664 span->alpha += span->alphaStep; \
665 tex_coord[0] += tex_step[0]; \
666 tex_coord[1] += tex_step[1]; \
667 tex_coord[2] += tex_step[2]; \
672 GLfloat tex_coord
[3], tex_step
[3];
673 GLchan
*dest
= span
->array
->rgba
[0];
675 const GLuint savedTexEnable
= ctx
->Texture
._EnabledUnits
;
676 ctx
->Texture
._EnabledUnits
= 0;
678 tex_coord
[0] = span
->tex
[0][0] * (info
->smask
+ 1);
679 tex_step
[0] = span
->texStepX
[0][0] * (info
->smask
+ 1);
680 tex_coord
[1] = span
->tex
[0][1] * (info
->tmask
+ 1);
681 tex_step
[1] = span
->texStepX
[0][1] * (info
->tmask
+ 1);
682 /* span->tex[0][2] only if 3D-texturing, here only 2D */
683 tex_coord
[2] = span
->tex
[0][3];
684 tex_step
[2] = span
->texStepX
[0][3];
686 switch (info
->filter
) {
688 switch (info
->format
) {
690 switch (info
->envmode
) {
692 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
696 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
699 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
702 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
705 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
710 switch(info
->envmode
) {
712 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
715 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
718 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
721 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
724 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
727 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
735 switch (info
->format
) {
737 switch (info
->envmode
) {
739 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
743 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
746 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
749 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
752 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
757 switch (info
->envmode
) {
759 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
762 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
765 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
768 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
771 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
774 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
782 ASSERT(span
->arrayMask
& SPAN_RGBA
);
783 _swrast_write_rgba_span(ctx
, span
);
789 ctx
->Texture
._EnabledUnits
= savedTexEnable
;
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
804 #define INTERP_ALPHA 1
808 struct persp_info info; \
809 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
810 const struct gl_texture_object *obj = unit->Current2D; \
811 const GLint b = obj->BaseLevel; \
812 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
813 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
814 info.smask = obj->Image[0][b]->Width - 1; \
815 info.tmask = obj->Image[0][b]->Height - 1; \
816 info.format = obj->Image[0][b]->_BaseFormat; \
817 info.filter = obj->MinFilter; \
818 info.envmode = unit->EnvMode; \
820 if (info.envmode == GL_BLEND) { \
821 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
822 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
823 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
824 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
825 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
827 if (!info.texture) { \
828 /* this shouldn't happen */ \
832 switch (info.format) { \
836 info.tbytesline = obj->Image[0][b]->Width; \
838 case GL_LUMINANCE_ALPHA: \
839 info.tbytesline = obj->Image[0][b]->Width * 2; \
842 info.tbytesline = obj->Image[0][b]->Width * 3; \
845 info.tbytesline = obj->Image[0][b]->Width * 4; \
848 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
851 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
853 #define RENDER_SPAN( span ) \
854 span.interpMask &= ~SPAN_RGBA; \
855 span.arrayMask |= SPAN_RGBA; \
856 fast_persp_span(ctx, &span, &info);
858 #include "s_tritemp.h"
861 #endif /* CHAN_BITS != GL_FLOAT */
867 * Render a smooth-shaded, textured, RGBA triangle.
868 * Interpolate S,T,R with perspective correction, w/out mipmapping.
870 #define NAME general_textured_triangle
875 #define INTERP_SPEC 1
876 #define INTERP_ALPHA 1
878 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
879 #include "s_tritemp.h"
884 * This is the big one!
885 * Interpolate Z, RGB, Alpha, specular, fog, N sets of texture coordinates, and varying floats.
888 #define NAME multitextured_triangle
893 #define INTERP_ALPHA 1
894 #define INTERP_SPEC 1
895 #define INTERP_MULTITEX 1
896 #define INTERP_VARYING 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
||
1076 ctx
->ATIFragmentShader
._Enabled
|| ctx
->ShaderObjects
._FragmentShaderPresent
) {
1077 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1078 const struct gl_texture_object
*texObj2D
;
1079 const struct gl_texture_image
*texImg
;
1080 GLenum minFilter
, magFilter
, envMode
;
1082 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1083 texImg
= texObj2D
? texObj2D
->Image
[0][texObj2D
->BaseLevel
] : NULL
;
1084 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1085 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1086 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1087 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1089 /* First see if we can use an optimized 2-D texture function */
1090 if (ctx
->Texture
._EnabledCoordUnits
== 0x1
1091 && !ctx
->FragmentProgram
._Active
1092 && !ctx
->ATIFragmentShader
._Enabled
1093 && !ctx
->ShaderObjects
._FragmentShaderPresent
1094 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1095 && texObj2D
->WrapS
== GL_REPEAT
1096 && texObj2D
->WrapT
== GL_REPEAT
1097 && texImg
->_IsPowerOfTwo
1098 && texImg
->Border
== 0
1099 && texImg
->Width
== texImg
->RowStride
1100 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1101 && minFilter
== magFilter
1102 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1103 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1104 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1105 if (minFilter
== GL_NEAREST
1106 && format
== MESA_FORMAT_RGB
1107 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1108 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1109 && ctx
->Depth
.Func
== GL_LESS
1110 && ctx
->Depth
.Mask
== GL_TRUE
)
1111 || swrast
->_RasterMask
== TEXTURE_BIT
)
1112 && ctx
->Polygon
.StippleFlag
== GL_FALSE
1113 && ctx
->Visual
.depthBits
<= 16) {
1114 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1115 USE(simple_z_textured_triangle
);
1118 USE(simple_textured_triangle
);
1122 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1123 USE(general_textured_triangle
);
1125 USE(affine_textured_triangle
);
1130 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1131 USE(general_textured_triangle
);
1133 USE(persp_textured_triangle
);
1138 /* general case textured triangles */
1139 if (ctx
->Texture
._EnabledCoordUnits
> 1) {
1140 USE(multitextured_triangle
);
1143 USE(general_textured_triangle
);
1148 ASSERT(!ctx
->Texture
._EnabledCoordUnits
);
1149 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1150 /* smooth shaded, no texturing, stippled or some raster ops */
1152 USE(smooth_rgba_triangle
);
1155 USE(smooth_ci_triangle
);
1159 /* flat shaded, no texturing, stippled or some raster ops */
1161 USE(flat_rgba_triangle
);
1164 USE(flat_ci_triangle
);
1169 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1170 USE(_swrast_feedback_triangle
);
1173 /* GL_SELECT mode */
1174 USE(_swrast_select_triangle
);