2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 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
->attrib
[FRAG_ATTRIB_WPOS
][0] - v0
->attrib
[FRAG_ATTRIB_WPOS
][0];
56 GLfloat ey
= v1
->attrib
[FRAG_ATTRIB_WPOS
][1] - v0
->attrib
[FRAG_ATTRIB_WPOS
][1];
57 GLfloat fx
= v2
->attrib
[FRAG_ATTRIB_WPOS
][0] - v0
->attrib
[FRAG_ATTRIB_WPOS
][0];
58 GLfloat fy
= v2
->attrib
[FRAG_ATTRIB_WPOS
][1] - v0
->attrib
[FRAG_ATTRIB_WPOS
][1];
59 GLfloat c
= ex
*fy
-ey
*fx
;
61 if (c
* SWRAST_CONTEXT(ctx
)->_BackfaceSign
> 0)
70 * Render a smooth or flat-shaded color index triangle.
72 #define NAME ci_triangle
74 #define INTERP_ATTRIBS 1 /* just for fog */
75 #define INTERP_INDEX 1
76 #define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
77 #include "s_tritemp.h"
82 * Render a flat-shaded RGBA triangle.
84 #define NAME flat_rgba_triangle
87 ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
88 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
89 span.interpMask |= SPAN_RGBA; \
90 span.red = ChanToFixed(v2->color[0]); \
91 span.green = ChanToFixed(v2->color[1]); \
92 span.blue = ChanToFixed(v2->color[2]); \
93 span.alpha = ChanToFixed(v2->color[3]); \
98 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
99 #include "s_tritemp.h"
104 * Render a smooth-shaded RGBA triangle.
106 #define NAME smooth_rgba_triangle
109 #define INTERP_ALPHA 1
112 /* texturing must be off */ \
113 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \
114 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
116 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
117 #include "s_tritemp.h"
122 * Render an RGB, GL_DECAL, textured triangle.
123 * Interpolate S,T only w/out mipmapping or perspective correction.
125 * No fog. No depth testing.
127 #define NAME simple_textured_triangle
128 #define INTERP_INT_TEX 1
129 #define S_SCALE twidth
130 #define T_SCALE theight
133 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];\
134 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
135 const GLint b = obj->BaseLevel; \
136 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
137 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
138 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
139 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
140 const GLint smask = obj->Image[0][b]->Width - 1; \
141 const GLint tmask = obj->Image[0][b]->Height - 1; \
143 /* this shouldn't happen */ \
147 #define RENDER_SPAN( span ) \
149 GLchan rgb[MAX_WIDTH][3]; \
150 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
151 span.intTex[1] -= FIXED_HALF; \
152 for (i = 0; i < span.end; i++) { \
153 GLint s = FixedToInt(span.intTex[0]) & smask; \
154 GLint t = FixedToInt(span.intTex[1]) & tmask; \
155 GLint pos = (t << twidth_log2) + s; \
156 pos = pos + pos + pos; /* multiply by 3 */ \
157 rgb[i][RCOMP] = texture[pos]; \
158 rgb[i][GCOMP] = texture[pos+1]; \
159 rgb[i][BCOMP] = texture[pos+2]; \
160 span.intTex[0] += span.intTexStep[0]; \
161 span.intTex[1] += span.intTexStep[1]; \
163 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, NULL);
165 #include "s_tritemp.h"
170 * Render an RGB, GL_DECAL, textured triangle.
171 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
172 * perspective correction.
173 * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
177 #define NAME simple_z_textured_triangle
179 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
180 #define INTERP_INT_TEX 1
181 #define S_SCALE twidth
182 #define T_SCALE theight
185 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];\
186 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
187 const GLint b = obj->BaseLevel; \
188 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
189 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
190 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
191 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
192 const GLint smask = obj->Image[0][b]->Width - 1; \
193 const GLint tmask = obj->Image[0][b]->Height - 1; \
195 /* this shouldn't happen */ \
199 #define RENDER_SPAN( span ) \
201 GLchan rgb[MAX_WIDTH][3]; \
202 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
203 span.intTex[1] -= FIXED_HALF; \
204 for (i = 0; i < span.end; i++) { \
205 const GLuint z = FixedToDepth(span.z); \
207 GLint s = FixedToInt(span.intTex[0]) & smask; \
208 GLint t = FixedToInt(span.intTex[1]) & tmask; \
209 GLint pos = (t << twidth_log2) + s; \
210 pos = pos + pos + pos; /* multiply by 3 */ \
211 rgb[i][RCOMP] = texture[pos]; \
212 rgb[i][GCOMP] = texture[pos+1]; \
213 rgb[i][BCOMP] = texture[pos+2]; \
215 span.array->mask[i] = 1; \
218 span.array->mask[i] = 0; \
220 span.intTex[0] += span.intTexStep[0]; \
221 span.intTex[1] += span.intTexStep[1]; \
222 span.z += span.zStep; \
224 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, span.array->mask);
226 #include "s_tritemp.h"
229 #if CHAN_TYPE != GL_FLOAT
238 const GLchan
*texture
;
239 GLfixed er
, eg
, eb
, ea
;
240 GLint tbytesline
, tsize
;
245 ilerp(GLint t
, GLint a
, GLint b
)
247 return a
+ ((t
* (b
- a
)) >> FIXED_SHIFT
);
251 ilerp_2d(GLint ia
, GLint ib
, GLint v00
, GLint v10
, GLint v01
, GLint v11
)
253 const GLint temp0
= ilerp(ia
, v00
, v10
);
254 const GLint temp1
= ilerp(ia
, v01
, v11
);
255 return ilerp(ib
, temp0
, temp1
);
259 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
260 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
264 affine_span(GLcontext
*ctx
, SWspan
*span
,
265 struct affine_info
*info
)
267 GLchan sample
[4]; /* the filtered texture sample */
269 /* Instead of defining a function for each mode, a test is done
270 * between the outer and inner loops. This is to reduce code size
271 * and complexity. Observe that an optimizing compiler kills
272 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
275 #define NEAREST_RGB \
276 sample[RCOMP] = tex00[RCOMP]; \
277 sample[GCOMP] = tex00[GCOMP]; \
278 sample[BCOMP] = tex00[BCOMP]; \
279 sample[ACOMP] = CHAN_MAX
282 sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
283 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
284 sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
285 sample[ACOMP] = CHAN_MAX;
287 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
289 #define LINEAR_RGBA \
290 sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
291 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
292 sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
293 sample[ACOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3])
296 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
297 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
298 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
299 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
302 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
303 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
304 >> (FIXED_SHIFT + 8); \
305 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
306 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
307 >> (FIXED_SHIFT + 8); \
308 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
309 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
310 >> (FIXED_SHIFT + 8); \
311 dest[ACOMP] = FixedToInt(span->alpha)
314 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
315 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
316 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
317 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
318 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
319 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
320 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
322 #define REPLACE COPY_CHAN4(dest, sample)
326 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
327 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
328 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
329 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
330 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
331 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
332 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
337 #define NEAREST_RGB_REPLACE \
339 dest[0] = sample[0]; \
340 dest[1] = sample[1]; \
341 dest[2] = sample[2]; \
342 dest[3] = FixedToInt(span->alpha);
344 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
346 #define SPAN_NEAREST(DO_TEX, COMPS) \
347 for (i = 0; i < span->end; i++) { \
348 /* Isn't it necessary to use FixedFloor below?? */ \
349 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
350 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
351 GLint pos = (t << info->twidth_log2) + s; \
352 const GLchan *tex00 = info->texture + COMPS * pos; \
354 span->red += span->redStep; \
355 span->green += span->greenStep; \
356 span->blue += span->blueStep; \
357 span->alpha += span->alphaStep; \
358 span->intTex[0] += span->intTexStep[0]; \
359 span->intTex[1] += span->intTexStep[1]; \
363 #define SPAN_LINEAR(DO_TEX, COMPS) \
364 for (i = 0; i < span->end; i++) { \
365 /* Isn't it necessary to use FixedFloor below?? */ \
366 const GLint s = FixedToInt(span->intTex[0]) & info->smask; \
367 const GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
368 const GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
369 const GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
370 const GLint pos = (t << info->twidth_log2) + s; \
371 const GLchan *tex00 = info->texture + COMPS * pos; \
372 const GLchan *tex10 = tex00 + info->tbytesline; \
373 const GLchan *tex01 = tex00 + COMPS; \
374 const GLchan *tex11 = tex10 + COMPS; \
375 if (t == info->tmask) { \
376 tex10 -= info->tsize; \
377 tex11 -= info->tsize; \
379 if (s == info->smask) { \
380 tex01 -= info->tbytesline; \
381 tex11 -= info->tbytesline; \
384 span->red += span->redStep; \
385 span->green += span->greenStep; \
386 span->blue += span->blueStep; \
387 span->alpha += span->alphaStep; \
388 span->intTex[0] += span->intTexStep[0]; \
389 span->intTex[1] += span->intTexStep[1]; \
395 GLchan
*dest
= span
->array
->rgba
[0];
397 span
->intTex
[0] -= FIXED_HALF
;
398 span
->intTex
[1] -= FIXED_HALF
;
399 switch (info
->filter
) {
401 switch (info
->format
) {
403 switch (info
->envmode
) {
405 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
409 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
412 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
415 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
418 _mesa_problem(ctx
, "bad tex env mode in SPAN_LINEAR");
423 switch(info
->envmode
) {
425 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
428 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
431 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
434 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
437 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
440 _mesa_problem(ctx
, "bad tex env mode (2) in SPAN_LINEAR");
448 span
->intTex
[0] -= FIXED_HALF
;
449 span
->intTex
[1] -= FIXED_HALF
;
450 switch (info
->format
) {
452 switch (info
->envmode
) {
454 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
458 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
461 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
464 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
467 _mesa_problem(ctx
, "bad tex env mode (3) in SPAN_LINEAR");
472 switch (info
->envmode
) {
474 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
477 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
480 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
483 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
486 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
489 _mesa_problem(ctx
, "bad tex env mode (4) in SPAN_LINEAR");
496 span
->interpMask
&= ~SPAN_RGBA
;
497 ASSERT(span
->arrayMask
& SPAN_RGBA
);
498 _swrast_write_rgba_span(ctx
, span
);
507 * Render an RGB/RGBA textured triangle without perspective correction.
509 #define NAME affine_textured_triangle
512 #define INTERP_ALPHA 1
513 #define INTERP_INT_TEX 1
514 #define S_SCALE twidth
515 #define T_SCALE theight
518 struct affine_info info; \
519 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
520 struct gl_texture_object *obj = unit->Current2D; \
521 const GLint b = obj->BaseLevel; \
522 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
523 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
524 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
525 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
526 info.smask = obj->Image[0][b]->Width - 1; \
527 info.tmask = obj->Image[0][b]->Height - 1; \
528 info.format = obj->Image[0][b]->_BaseFormat; \
529 info.filter = obj->MinFilter; \
530 info.envmode = unit->EnvMode; \
531 span.arrayMask |= SPAN_RGBA; \
533 if (info.envmode == GL_BLEND) { \
534 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
535 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
536 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
537 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
538 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
540 if (!info.texture) { \
541 /* this shouldn't happen */ \
545 switch (info.format) { \
549 info.tbytesline = obj->Image[0][b]->Width; \
551 case GL_LUMINANCE_ALPHA: \
552 info.tbytesline = obj->Image[0][b]->Width * 2; \
555 info.tbytesline = obj->Image[0][b]->Width * 3; \
558 info.tbytesline = obj->Image[0][b]->Width * 4; \
561 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
564 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
566 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
568 #include "s_tritemp.h"
579 const GLchan
*texture
;
580 GLfixed er
, eg
, eb
, ea
; /* texture env color */
581 GLint tbytesline
, tsize
;
586 fast_persp_span(GLcontext
*ctx
, SWspan
*span
,
587 struct persp_info
*info
)
589 GLchan sample
[4]; /* the filtered texture sample */
591 /* Instead of defining a function for each mode, a test is done
592 * between the outer and inner loops. This is to reduce code size
593 * and complexity. Observe that an optimizing compiler kills
594 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
596 #define SPAN_NEAREST(DO_TEX,COMP) \
597 for (i = 0; i < span->end; i++) { \
598 GLdouble invQ = tex_coord[2] ? \
599 (1.0 / tex_coord[2]) : 1.0; \
600 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
601 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
602 GLint s = IFLOOR(s_tmp) & info->smask; \
603 GLint t = IFLOOR(t_tmp) & info->tmask; \
604 GLint pos = (t << info->twidth_log2) + s; \
605 const GLchan *tex00 = info->texture + COMP * pos; \
607 span->red += span->redStep; \
608 span->green += span->greenStep; \
609 span->blue += span->blueStep; \
610 span->alpha += span->alphaStep; \
611 tex_coord[0] += tex_step[0]; \
612 tex_coord[1] += tex_step[1]; \
613 tex_coord[2] += tex_step[2]; \
617 #define SPAN_LINEAR(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 const GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
622 const GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
623 const GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
624 const GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
625 const GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
626 const GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
627 const GLfixed sf = s_fix & FIXED_FRAC_MASK; \
628 const GLfixed tf = t_fix & FIXED_FRAC_MASK; \
629 const GLint pos = (t << info->twidth_log2) + s; \
630 const GLchan *tex00 = info->texture + COMP * pos; \
631 const GLchan *tex10 = tex00 + info->tbytesline; \
632 const GLchan *tex01 = tex00 + COMP; \
633 const GLchan *tex11 = tex10 + COMP; \
634 if (t == info->tmask) { \
635 tex10 -= info->tsize; \
636 tex11 -= info->tsize; \
638 if (s == info->smask) { \
639 tex01 -= info->tbytesline; \
640 tex11 -= info->tbytesline; \
643 span->red += span->redStep; \
644 span->green += span->greenStep; \
645 span->blue += span->blueStep; \
646 span->alpha += span->alphaStep; \
647 tex_coord[0] += tex_step[0]; \
648 tex_coord[1] += tex_step[1]; \
649 tex_coord[2] += tex_step[2]; \
654 GLfloat tex_coord
[3], tex_step
[3];
655 GLchan
*dest
= span
->array
->rgba
[0];
657 const GLuint savedTexEnable
= ctx
->Texture
._EnabledUnits
;
658 ctx
->Texture
._EnabledUnits
= 0;
660 tex_coord
[0] = span
->attrStart
[FRAG_ATTRIB_TEX0
][0] * (info
->smask
+ 1);
661 tex_step
[0] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][0] * (info
->smask
+ 1);
662 tex_coord
[1] = span
->attrStart
[FRAG_ATTRIB_TEX0
][1] * (info
->tmask
+ 1);
663 tex_step
[1] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][1] * (info
->tmask
+ 1);
664 /* span->attrStart[FRAG_ATTRIB_TEX0][2] only if 3D-texturing, here only 2D */
665 tex_coord
[2] = span
->attrStart
[FRAG_ATTRIB_TEX0
][3];
666 tex_step
[2] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][3];
668 switch (info
->filter
) {
670 switch (info
->format
) {
672 switch (info
->envmode
) {
674 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
678 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
681 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
684 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
687 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
692 switch(info
->envmode
) {
694 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
697 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
700 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
703 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
706 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
709 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
717 switch (info
->format
) {
719 switch (info
->envmode
) {
721 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
725 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
728 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
731 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
734 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
739 switch (info
->envmode
) {
741 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
744 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
747 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
750 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
753 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
756 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
764 ASSERT(span
->arrayMask
& SPAN_RGBA
);
765 _swrast_write_rgba_span(ctx
, span
);
771 ctx
->Texture
._EnabledUnits
= savedTexEnable
;
776 * Render an perspective corrected RGB/RGBA textured triangle.
777 * The Q (aka V in Mesa) coordinate must be zero such that the divide
778 * by interpolated Q/W comes out right.
781 #define NAME persp_textured_triangle
784 #define INTERP_ALPHA 1
785 #define INTERP_ATTRIBS 1
788 struct persp_info info; \
789 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
790 const struct gl_texture_object *obj = unit->Current2D; \
791 const GLint b = obj->BaseLevel; \
792 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
793 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
794 info.smask = obj->Image[0][b]->Width - 1; \
795 info.tmask = obj->Image[0][b]->Height - 1; \
796 info.format = obj->Image[0][b]->_BaseFormat; \
797 info.filter = obj->MinFilter; \
798 info.envmode = unit->EnvMode; \
800 if (info.envmode == GL_BLEND) { \
801 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
802 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
803 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
804 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
805 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
807 if (!info.texture) { \
808 /* this shouldn't happen */ \
812 switch (info.format) { \
816 info.tbytesline = obj->Image[0][b]->Width; \
818 case GL_LUMINANCE_ALPHA: \
819 info.tbytesline = obj->Image[0][b]->Width * 2; \
822 info.tbytesline = obj->Image[0][b]->Width * 3; \
825 info.tbytesline = obj->Image[0][b]->Width * 4; \
828 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
831 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
833 #define RENDER_SPAN( span ) \
834 span.interpMask &= ~SPAN_RGBA; \
835 span.arrayMask |= SPAN_RGBA; \
836 fast_persp_span(ctx, &span, &info);
838 #include "s_tritemp.h"
840 #endif /*CHAN_TYPE != GL_FLOAT*/
845 * Render an RGBA triangle with arbitrary attributes.
847 #define NAME general_triangle
850 #define INTERP_ALPHA 1
851 #define INTERP_ATTRIBS 1
852 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
853 #include "s_tritemp.h"
859 * Special tri function for occlusion testing
861 #define NAME occlusion_zless_triangle
864 struct gl_renderbuffer *rb = ctx->DrawBuffer->_DepthBuffer; \
865 struct gl_query_object *q = ctx->Query.CurrentOcclusionObject; \
866 ASSERT(ctx->Depth.Test); \
867 ASSERT(!ctx->Depth.Mask); \
868 ASSERT(ctx->Depth.Func == GL_LESS); \
872 #define RENDER_SPAN( span ) \
873 if (rb->DepthBits <= 16) { \
875 const GLushort *zRow = (const GLushort *) \
876 rb->GetPointer(ctx, rb, span.x, span.y); \
877 for (i = 0; i < span.end; i++) { \
878 GLuint z = FixedToDepth(span.z); \
882 span.z += span.zStep; \
887 const GLuint *zRow = (const GLuint *) \
888 rb->GetPointer(ctx, rb, span.x, span.y); \
889 for (i = 0; i < span.end; i++) { \
890 if ((GLuint)span.z < zRow[i]) { \
893 span.z += span.zStep; \
896 #include "s_tritemp.h"
901 nodraw_triangle( GLcontext
*ctx
,
906 (void) (ctx
&& v0
&& v1
&& v2
);
911 * This is used when separate specular color is enabled, but not
912 * texturing. We add the specular color to the primary color,
913 * draw the triangle, then restore the original primary color.
914 * Inefficient, but seldom needed.
917 _swrast_add_spec_terms_triangle(GLcontext
*ctx
, const SWvertex
*v0
,
918 const SWvertex
*v1
, const SWvertex
*v2
)
920 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
921 SWvertex
*ncv1
= (SWvertex
*)v1
;
922 SWvertex
*ncv2
= (SWvertex
*)v2
;
923 GLfloat rSum
, gSum
, bSum
;
926 /* save original colors */
927 COPY_CHAN4( cSave
[0], ncv0
->color
);
928 COPY_CHAN4( cSave
[1], ncv1
->color
);
929 COPY_CHAN4( cSave
[2], ncv2
->color
);
931 rSum
= CHAN_TO_FLOAT(ncv0
->color
[0]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][0];
932 gSum
= CHAN_TO_FLOAT(ncv0
->color
[1]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][1];
933 bSum
= CHAN_TO_FLOAT(ncv0
->color
[2]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][2];
934 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[0], rSum
);
935 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[1], gSum
);
936 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[2], bSum
);
938 rSum
= CHAN_TO_FLOAT(ncv1
->color
[0]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][0];
939 gSum
= CHAN_TO_FLOAT(ncv1
->color
[1]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][1];
940 bSum
= CHAN_TO_FLOAT(ncv1
->color
[2]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][2];
941 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[0], rSum
);
942 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[1], gSum
);
943 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[2], bSum
);
945 rSum
= CHAN_TO_FLOAT(ncv2
->color
[0]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][0];
946 gSum
= CHAN_TO_FLOAT(ncv2
->color
[1]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][1];
947 bSum
= CHAN_TO_FLOAT(ncv2
->color
[2]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][2];
948 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[0], rSum
);
949 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[1], gSum
);
950 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[2], bSum
);
952 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
953 /* restore original colors */
954 COPY_CHAN4( ncv0
->color
, cSave
[0] );
955 COPY_CHAN4( ncv1
->color
, cSave
[1] );
956 COPY_CHAN4( ncv2
->color
, cSave
[2] );
963 /* record the current triangle function name */
964 const char *_mesa_triFuncName
= NULL
;
966 #define USE(triFunc) \
968 _mesa_triFuncName = #triFunc; \
969 /*printf("%s\n", _mesa_triFuncName);*/ \
970 swrast->Triangle = triFunc; \
975 #define USE(triFunc) swrast->Triangle = triFunc;
983 * Determine which triangle rendering function to use given the current
986 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
987 * remove tests to this code.
990 _swrast_choose_triangle( GLcontext
*ctx
)
992 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
993 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
995 if (ctx
->Polygon
.CullFlag
&&
996 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
997 USE(nodraw_triangle
);
1001 if (ctx
->RenderMode
==GL_RENDER
) {
1003 if (ctx
->Polygon
.SmoothFlag
) {
1004 _swrast_set_aa_triangle_function(ctx
);
1005 ASSERT(swrast
->Triangle
);
1009 /* special case for occlusion testing */
1010 if (ctx
->Query
.CurrentOcclusionObject
&&
1012 ctx
->Depth
.Mask
== GL_FALSE
&&
1013 ctx
->Depth
.Func
== GL_LESS
&&
1014 !ctx
->Stencil
.Enabled
) {
1016 ctx
->Color
.ColorMask
[0] == 0 &&
1017 ctx
->Color
.ColorMask
[1] == 0 &&
1018 ctx
->Color
.ColorMask
[2] == 0 &&
1019 ctx
->Color
.ColorMask
[3] == 0)
1021 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1022 USE(occlusion_zless_triangle
);
1033 * XXX should examine swrast->_ActiveAttribMask to determine what
1034 * needs to be interpolated.
1036 if (ctx
->Texture
._EnabledCoordUnits
||
1037 ctx
->FragmentProgram
._Current
||
1038 ctx
->ATIFragmentShader
._Enabled
||
1039 NEED_SECONDARY_COLOR(ctx
) ||
1040 swrast
->_FogEnabled
) {
1041 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1042 const struct gl_texture_object
*texObj2D
;
1043 const struct gl_texture_image
*texImg
;
1044 GLenum minFilter
, magFilter
, envMode
;
1046 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1047 texImg
= texObj2D
? texObj2D
->Image
[0][texObj2D
->BaseLevel
] : NULL
;
1048 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1049 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1050 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1051 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1053 /* First see if we can use an optimized 2-D texture function */
1054 if (ctx
->Texture
._EnabledCoordUnits
== 0x1
1055 && !ctx
->FragmentProgram
._Current
1056 && !ctx
->ATIFragmentShader
._Enabled
1057 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1058 && texObj2D
->WrapS
== GL_REPEAT
1059 && texObj2D
->WrapT
== GL_REPEAT
1060 && texImg
->_IsPowerOfTwo
1061 && texImg
->Border
== 0
1062 && texImg
->Width
== texImg
->RowStride
1063 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1064 && minFilter
== magFilter
1065 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1066 && !swrast
->_FogEnabled
1067 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1068 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1069 if (minFilter
== GL_NEAREST
1070 && format
== MESA_FORMAT_RGB
1071 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1072 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1073 && ctx
->Depth
.Func
== GL_LESS
1074 && ctx
->Depth
.Mask
== GL_TRUE
)
1075 || swrast
->_RasterMask
== TEXTURE_BIT
)
1076 && ctx
->Polygon
.StippleFlag
== GL_FALSE
1077 && ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
1078 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1079 USE(simple_z_textured_triangle
);
1082 USE(simple_textured_triangle
);
1087 USE(general_triangle
);
1089 USE(affine_textured_triangle
);
1095 USE(general_triangle
);
1097 USE(persp_textured_triangle
);
1102 /* general case textured triangles */
1103 USE(general_triangle
);
1107 ASSERT(!swrast
->_FogEnabled
);
1108 ASSERT(!NEED_SECONDARY_COLOR(ctx
));
1109 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1110 /* smooth shaded, no texturing, stippled or some raster ops */
1112 USE(general_triangle
);
1114 USE(smooth_rgba_triangle
);
1118 /* flat shaded, no texturing, stippled or some raster ops */
1120 USE(general_triangle
);
1122 USE(flat_rgba_triangle
);
1127 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1128 USE(_swrast_feedback_triangle
);
1131 /* GL_SELECT mode */
1132 USE(_swrast_select_triangle
);