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.
32 #include "main/glheader.h"
33 #include "main/context.h"
34 #include "main/colormac.h"
35 #include "main/imports.h"
36 #include "main/macros.h"
37 #include "main/mtypes.h"
38 #include "main/state.h"
39 #include "program/prog_instruction.h"
41 #include "s_aatriangle.h"
42 #include "s_context.h"
43 #include "s_feedback.h"
45 #include "s_triangle.h"
49 * Test if a triangle should be culled. Used for feedback and selection mode.
50 * \return GL_TRUE if the triangle is to be culled, GL_FALSE otherwise.
53 _swrast_culltriangle( struct gl_context *ctx,
58 SWcontext *swrast = SWRAST_CONTEXT(ctx);
59 GLfloat ex = v1->attrib[FRAG_ATTRIB_WPOS][0] - v0->attrib[FRAG_ATTRIB_WPOS][0];
60 GLfloat ey = v1->attrib[FRAG_ATTRIB_WPOS][1] - v0->attrib[FRAG_ATTRIB_WPOS][1];
61 GLfloat fx = v2->attrib[FRAG_ATTRIB_WPOS][0] - v0->attrib[FRAG_ATTRIB_WPOS][0];
62 GLfloat fy = v2->attrib[FRAG_ATTRIB_WPOS][1] - v0->attrib[FRAG_ATTRIB_WPOS][1];
63 GLfloat c = ex*fy-ey*fx;
65 if (c * swrast->_BackfaceSign * swrast->_BackfaceCullSign <= 0.0F)
74 * Render a flat-shaded RGBA triangle.
76 #define NAME flat_rgba_triangle
79 ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
80 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
81 span.interpMask |= SPAN_RGBA; \
82 span.red = ChanToFixed(v2->color[0]); \
83 span.green = ChanToFixed(v2->color[1]); \
84 span.blue = ChanToFixed(v2->color[2]); \
85 span.alpha = ChanToFixed(v2->color[3]); \
90 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
91 #include "s_tritemp.h"
96 * Render a smooth-shaded RGBA triangle.
98 #define NAME smooth_rgba_triangle
101 #define INTERP_ALPHA 1
104 /* texturing must be off */ \
105 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \
106 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
108 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
109 #include "s_tritemp.h"
114 * Render an RGB, GL_DECAL, textured triangle.
115 * Interpolate S,T only w/out mipmapping or perspective correction.
117 * No fog. No depth testing.
119 #define NAME simple_textured_triangle
120 #define INTERP_INT_TEX 1
121 #define S_SCALE twidth
122 #define T_SCALE theight
125 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
126 const struct gl_texture_object *obj = \
127 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
128 const struct gl_texture_image *texImg = \
129 obj->Image[0][obj->BaseLevel]; \
130 const struct swrast_texture_image *swImg = \
131 swrast_texture_image_const(texImg); \
132 const GLfloat twidth = (GLfloat) texImg->Width; \
133 const GLfloat theight = (GLfloat) texImg->Height; \
134 const GLint twidth_log2 = texImg->WidthLog2; \
135 const GLubyte *texture = (const GLubyte *) swImg->Data; \
136 const GLint smask = texImg->Width - 1; \
137 const GLint tmask = texImg->Height - 1; \
138 ASSERT(texImg->TexFormat == MESA_FORMAT_RGB888); \
139 if (!rb || !texture) { \
143 #define RENDER_SPAN( span ) \
145 GLubyte rgba[MAX_WIDTH][4]; \
146 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
147 span.intTex[1] -= FIXED_HALF; \
148 for (i = 0; i < span.end; i++) { \
149 GLint s = FixedToInt(span.intTex[0]) & smask; \
150 GLint t = FixedToInt(span.intTex[1]) & tmask; \
151 GLint pos = (t << twidth_log2) + s; \
152 pos = pos + pos + pos; /* multiply by 3 */ \
153 rgba[i][RCOMP] = texture[pos+2]; \
154 rgba[i][GCOMP] = texture[pos+1]; \
155 rgba[i][BCOMP] = texture[pos+0]; \
156 rgba[i][ACOMP] = 0xff; \
157 span.intTex[0] += span.intTexStep[0]; \
158 span.intTex[1] += span.intTexStep[1]; \
160 rb->PutRow(ctx, rb, span.end, span.x, span.y, rgba, NULL);
162 #include "s_tritemp.h"
167 * Render an RGB, GL_DECAL, textured triangle.
168 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
169 * perspective correction.
170 * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
174 #define NAME simple_z_textured_triangle
176 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
177 #define INTERP_INT_TEX 1
178 #define S_SCALE twidth
179 #define T_SCALE theight
182 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
183 const struct gl_texture_object *obj = \
184 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
185 const struct gl_texture_image *texImg = \
186 obj->Image[0][obj->BaseLevel]; \
187 const struct swrast_texture_image *swImg = \
188 swrast_texture_image_const(texImg); \
189 const GLfloat twidth = (GLfloat) texImg->Width; \
190 const GLfloat theight = (GLfloat) texImg->Height; \
191 const GLint twidth_log2 = texImg->WidthLog2; \
192 const GLubyte *texture = (const GLubyte *) swImg->Data; \
193 const GLint smask = texImg->Width - 1; \
194 const GLint tmask = texImg->Height - 1; \
195 ASSERT(texImg->TexFormat == MESA_FORMAT_RGB888); \
196 if (!rb || !texture) { \
200 #define RENDER_SPAN( span ) \
202 GLubyte rgba[MAX_WIDTH][4]; \
203 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
204 span.intTex[1] -= FIXED_HALF; \
205 for (i = 0; i < span.end; i++) { \
206 const GLuint z = FixedToDepth(span.z); \
208 GLint s = FixedToInt(span.intTex[0]) & smask; \
209 GLint t = FixedToInt(span.intTex[1]) & tmask; \
210 GLint pos = (t << twidth_log2) + s; \
211 pos = pos + pos + pos; /* multiply by 3 */ \
212 rgba[i][RCOMP] = texture[pos+2]; \
213 rgba[i][GCOMP] = texture[pos+1]; \
214 rgba[i][BCOMP] = texture[pos+0]; \
215 rgba[i][ACOMP] = 0xff; \
217 span.array->mask[i] = 1; \
220 span.array->mask[i] = 0; \
222 span.intTex[0] += span.intTexStep[0]; \
223 span.intTex[1] += span.intTexStep[1]; \
224 span.z += span.zStep; \
226 rb->PutRow(ctx, rb, span.end, span.x, span.y, rgba, span.array->mask);
228 #include "s_tritemp.h"
231 #if CHAN_TYPE != GL_FLOAT
240 const GLchan *texture;
241 GLfixed er, eg, eb, ea;
242 GLint tbytesline, tsize;
247 ilerp(GLint t, GLint a, GLint b)
249 return a + ((t * (b - a)) >> FIXED_SHIFT);
253 ilerp_2d(GLint ia, GLint ib, GLint v00, GLint v10, GLint v01, GLint v11)
255 const GLint temp0 = ilerp(ia, v00, v10);
256 const GLint temp1 = ilerp(ia, v01, v11);
257 return ilerp(ib, temp0, temp1);
261 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
262 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
266 affine_span(struct gl_context *ctx, SWspan *span,
267 struct affine_info *info)
269 GLchan sample[4]; /* the filtered texture sample */
270 const GLuint texEnableSave = ctx->Texture._EnabledCoordUnits;
272 /* Instead of defining a function for each mode, a test is done
273 * between the outer and inner loops. This is to reduce code size
274 * and complexity. Observe that an optimizing compiler kills
275 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
278 #define NEAREST_RGB \
279 sample[RCOMP] = tex00[2]; \
280 sample[GCOMP] = tex00[1]; \
281 sample[BCOMP] = tex00[0]; \
282 sample[ACOMP] = CHAN_MAX;
285 sample[RCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
286 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
287 sample[BCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
288 sample[ACOMP] = CHAN_MAX;
290 #define NEAREST_RGBA \
291 sample[RCOMP] = tex00[3]; \
292 sample[GCOMP] = tex00[2]; \
293 sample[BCOMP] = tex00[1]; \
294 sample[ACOMP] = tex00[0];
296 #define LINEAR_RGBA \
297 sample[RCOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3]);\
298 sample[GCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
299 sample[BCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
300 sample[ACOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0])
303 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
304 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
305 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
306 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
309 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
310 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
311 >> (FIXED_SHIFT + 8); \
312 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
313 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
314 >> (FIXED_SHIFT + 8); \
315 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
316 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
317 >> (FIXED_SHIFT + 8); \
318 dest[ACOMP] = FixedToInt(span->alpha)
321 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
322 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
323 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
324 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
325 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
326 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
327 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
329 #define REPLACE COPY_CHAN4(dest, sample)
333 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
334 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
335 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
336 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
337 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
338 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
339 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
344 #define NEAREST_RGB_REPLACE \
346 dest[0] = sample[0]; \
347 dest[1] = sample[1]; \
348 dest[2] = sample[2]; \
349 dest[3] = FixedToInt(span->alpha);
351 #define NEAREST_RGBA_REPLACE \
352 dest[RCOMP] = tex00[3]; \
353 dest[GCOMP] = tex00[2]; \
354 dest[BCOMP] = tex00[1]; \
355 dest[ACOMP] = tex00[0]
357 #define SPAN_NEAREST(DO_TEX, COMPS) \
358 for (i = 0; i < span->end; i++) { \
359 /* Isn't it necessary to use FixedFloor below?? */ \
360 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
361 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
362 GLint pos = (t << info->twidth_log2) + s; \
363 const GLchan *tex00 = info->texture + COMPS * pos; \
365 span->red += span->redStep; \
366 span->green += span->greenStep; \
367 span->blue += span->blueStep; \
368 span->alpha += span->alphaStep; \
369 span->intTex[0] += span->intTexStep[0]; \
370 span->intTex[1] += span->intTexStep[1]; \
374 #define SPAN_LINEAR(DO_TEX, COMPS) \
375 for (i = 0; i < span->end; i++) { \
376 /* Isn't it necessary to use FixedFloor below?? */ \
377 const GLint s = FixedToInt(span->intTex[0]) & info->smask; \
378 const GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
379 const GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
380 const GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
381 const GLint pos = (t << info->twidth_log2) + s; \
382 const GLchan *tex00 = info->texture + COMPS * pos; \
383 const GLchan *tex10 = tex00 + info->tbytesline; \
384 const GLchan *tex01 = tex00 + COMPS; \
385 const GLchan *tex11 = tex10 + COMPS; \
386 if (t == info->tmask) { \
387 tex10 -= info->tsize; \
388 tex11 -= info->tsize; \
390 if (s == info->smask) { \
391 tex01 -= info->tbytesline; \
392 tex11 -= info->tbytesline; \
395 span->red += span->redStep; \
396 span->green += span->greenStep; \
397 span->blue += span->blueStep; \
398 span->alpha += span->alphaStep; \
399 span->intTex[0] += span->intTexStep[0]; \
400 span->intTex[1] += span->intTexStep[1]; \
406 GLchan *dest = span->array->rgba[0];
408 /* Disable tex units so they're not re-applied in swrast_write_rgba_span */
409 ctx->Texture._EnabledCoordUnits = 0x0;
411 span->intTex[0] -= FIXED_HALF;
412 span->intTex[1] -= FIXED_HALF;
413 switch (info->filter) {
415 switch (info->format) {
416 case MESA_FORMAT_RGB888:
417 switch (info->envmode) {
419 SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
423 SPAN_NEAREST(NEAREST_RGB_REPLACE,3);
426 SPAN_NEAREST(NEAREST_RGB;BLEND,3);
429 SPAN_NEAREST(NEAREST_RGB;ADD,3);
432 _mesa_problem(ctx, "bad tex env mode in SPAN_LINEAR");
436 case MESA_FORMAT_RGBA8888:
437 switch(info->envmode) {
439 SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
442 SPAN_NEAREST(NEAREST_RGBA;DECAL,4);
445 SPAN_NEAREST(NEAREST_RGBA;BLEND,4);
448 SPAN_NEAREST(NEAREST_RGBA;ADD,4);
451 SPAN_NEAREST(NEAREST_RGBA_REPLACE,4);
454 _mesa_problem(ctx, "bad tex env mode (2) in SPAN_LINEAR");
462 span->intTex[0] -= FIXED_HALF;
463 span->intTex[1] -= FIXED_HALF;
464 switch (info->format) {
465 case MESA_FORMAT_RGB888:
466 switch (info->envmode) {
468 SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
472 SPAN_LINEAR(LINEAR_RGB;REPLACE,3);
475 SPAN_LINEAR(LINEAR_RGB;BLEND,3);
478 SPAN_LINEAR(LINEAR_RGB;ADD,3);
481 _mesa_problem(ctx, "bad tex env mode (3) in SPAN_LINEAR");
485 case MESA_FORMAT_RGBA8888:
486 switch (info->envmode) {
488 SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
491 SPAN_LINEAR(LINEAR_RGBA;DECAL,4);
494 SPAN_LINEAR(LINEAR_RGBA;BLEND,4);
497 SPAN_LINEAR(LINEAR_RGBA;ADD,4);
500 SPAN_LINEAR(LINEAR_RGBA;REPLACE,4);
503 _mesa_problem(ctx, "bad tex env mode (4) in SPAN_LINEAR");
510 span->interpMask &= ~SPAN_RGBA;
511 ASSERT(span->arrayMask & SPAN_RGBA);
513 _swrast_write_rgba_span(ctx, span);
515 /* re-enable texture units */
516 ctx->Texture._EnabledCoordUnits = texEnableSave;
525 * Render an RGB/RGBA textured triangle without perspective correction.
527 #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 const struct gl_texture_object *obj = \
539 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
540 const struct gl_texture_image *texImg = \
541 obj->Image[0][obj->BaseLevel]; \
542 const struct swrast_texture_image *swImg = \
543 swrast_texture_image_const(texImg); \
544 const GLfloat twidth = (GLfloat) texImg->Width; \
545 const GLfloat theight = (GLfloat) texImg->Height; \
546 info.texture = (const GLchan *) swImg->Data; \
547 info.twidth_log2 = texImg->WidthLog2; \
548 info.smask = texImg->Width - 1; \
549 info.tmask = texImg->Height - 1; \
550 info.format = texImg->TexFormat; \
551 info.filter = obj->Sampler.MinFilter; \
552 info.envmode = unit->EnvMode; \
556 span.arrayMask |= SPAN_RGBA; \
558 if (info.envmode == GL_BLEND) { \
559 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
560 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
561 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
562 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
563 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
565 if (!info.texture) { \
566 /* this shouldn't happen */ \
570 switch (info.format) { \
571 case MESA_FORMAT_RGB888: \
572 info.tbytesline = texImg->Width * 3; \
574 case MESA_FORMAT_RGBA8888: \
575 info.tbytesline = texImg->Width * 4; \
578 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
581 info.tsize = texImg->Height * info.tbytesline;
583 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
585 #include "s_tritemp.h"
596 const GLchan *texture;
597 GLfixed er, eg, eb, ea; /* texture env color */
598 GLint tbytesline, tsize;
603 fast_persp_span(struct gl_context *ctx, SWspan *span,
604 struct persp_info *info)
606 GLchan sample[4]; /* the filtered texture sample */
608 /* Instead of defining a function for each mode, a test is done
609 * between the outer and inner loops. This is to reduce code size
610 * and complexity. Observe that an optimizing compiler kills
611 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
613 #define SPAN_NEAREST(DO_TEX,COMP) \
614 for (i = 0; i < span->end; i++) { \
615 GLdouble invQ = tex_coord[2] ? \
616 (1.0 / tex_coord[2]) : 1.0; \
617 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
618 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
619 GLint s = IFLOOR(s_tmp) & info->smask; \
620 GLint t = IFLOOR(t_tmp) & info->tmask; \
621 GLint pos = (t << info->twidth_log2) + s; \
622 const GLchan *tex00 = info->texture + COMP * pos; \
624 span->red += span->redStep; \
625 span->green += span->greenStep; \
626 span->blue += span->blueStep; \
627 span->alpha += span->alphaStep; \
628 tex_coord[0] += tex_step[0]; \
629 tex_coord[1] += tex_step[1]; \
630 tex_coord[2] += tex_step[2]; \
634 #define SPAN_LINEAR(DO_TEX,COMP) \
635 for (i = 0; i < span->end; i++) { \
636 GLdouble invQ = tex_coord[2] ? \
637 (1.0 / tex_coord[2]) : 1.0; \
638 const GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
639 const GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
640 const GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
641 const GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
642 const GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
643 const GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
644 const GLfixed sf = s_fix & FIXED_FRAC_MASK; \
645 const GLfixed tf = t_fix & FIXED_FRAC_MASK; \
646 const GLint pos = (t << info->twidth_log2) + s; \
647 const GLchan *tex00 = info->texture + COMP * pos; \
648 const GLchan *tex10 = tex00 + info->tbytesline; \
649 const GLchan *tex01 = tex00 + COMP; \
650 const GLchan *tex11 = tex10 + COMP; \
651 if (t == info->tmask) { \
652 tex10 -= info->tsize; \
653 tex11 -= info->tsize; \
655 if (s == info->smask) { \
656 tex01 -= info->tbytesline; \
657 tex11 -= info->tbytesline; \
660 span->red += span->redStep; \
661 span->green += span->greenStep; \
662 span->blue += span->blueStep; \
663 span->alpha += span->alphaStep; \
664 tex_coord[0] += tex_step[0]; \
665 tex_coord[1] += tex_step[1]; \
666 tex_coord[2] += tex_step[2]; \
671 GLfloat tex_coord[3], tex_step[3];
672 GLchan *dest = span->array->rgba[0];
674 const GLuint texEnableSave = ctx->Texture._EnabledCoordUnits;
675 ctx->Texture._EnabledCoordUnits = 0;
677 tex_coord[0] = span->attrStart[FRAG_ATTRIB_TEX0][0] * (info->smask + 1);
678 tex_step[0] = span->attrStepX[FRAG_ATTRIB_TEX0][0] * (info->smask + 1);
679 tex_coord[1] = span->attrStart[FRAG_ATTRIB_TEX0][1] * (info->tmask + 1);
680 tex_step[1] = span->attrStepX[FRAG_ATTRIB_TEX0][1] * (info->tmask + 1);
681 /* span->attrStart[FRAG_ATTRIB_TEX0][2] only if 3D-texturing, here only 2D */
682 tex_coord[2] = span->attrStart[FRAG_ATTRIB_TEX0][3];
683 tex_step[2] = span->attrStepX[FRAG_ATTRIB_TEX0][3];
685 switch (info->filter) {
687 switch (info->format) {
688 case MESA_FORMAT_RGB888:
689 switch (info->envmode) {
691 SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
695 SPAN_NEAREST(NEAREST_RGB_REPLACE,3);
698 SPAN_NEAREST(NEAREST_RGB;BLEND,3);
701 SPAN_NEAREST(NEAREST_RGB;ADD,3);
704 _mesa_problem(ctx, "bad tex env mode (5) in SPAN_LINEAR");
708 case MESA_FORMAT_RGBA8888:
709 switch(info->envmode) {
711 SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
714 SPAN_NEAREST(NEAREST_RGBA;DECAL,4);
717 SPAN_NEAREST(NEAREST_RGBA;BLEND,4);
720 SPAN_NEAREST(NEAREST_RGBA;ADD,4);
723 SPAN_NEAREST(NEAREST_RGBA_REPLACE,4);
726 _mesa_problem(ctx, "bad tex env mode (6) in SPAN_LINEAR");
734 switch (info->format) {
735 case MESA_FORMAT_RGB888:
736 switch (info->envmode) {
738 SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
742 SPAN_LINEAR(LINEAR_RGB;REPLACE,3);
745 SPAN_LINEAR(LINEAR_RGB;BLEND,3);
748 SPAN_LINEAR(LINEAR_RGB;ADD,3);
751 _mesa_problem(ctx, "bad tex env mode (7) in SPAN_LINEAR");
755 case MESA_FORMAT_RGBA8888:
756 switch (info->envmode) {
758 SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
761 SPAN_LINEAR(LINEAR_RGBA;DECAL,4);
764 SPAN_LINEAR(LINEAR_RGBA;BLEND,4);
767 SPAN_LINEAR(LINEAR_RGBA;ADD,4);
770 SPAN_LINEAR(LINEAR_RGBA;REPLACE,4);
773 _mesa_problem(ctx, "bad tex env mode (8) in SPAN_LINEAR");
781 ASSERT(span->arrayMask & SPAN_RGBA);
782 _swrast_write_rgba_span(ctx, span);
788 ctx->Texture._EnabledCoordUnits = texEnableSave;
793 * Render an perspective corrected RGB/RGBA textured triangle.
794 * The Q (aka V in Mesa) coordinate must be zero such that the divide
795 * by interpolated Q/W comes out right.
798 #define NAME persp_textured_triangle
801 #define INTERP_ALPHA 1
802 #define INTERP_ATTRIBS 1
805 struct persp_info info; \
806 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
807 const struct gl_texture_object *obj = \
808 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
809 const struct gl_texture_image *texImg = \
810 obj->Image[0][obj->BaseLevel]; \
811 const struct swrast_texture_image *swImg = \
812 swrast_texture_image_const(texImg); \
813 info.texture = (const GLchan *) swImg->Data; \
814 info.twidth_log2 = texImg->WidthLog2; \
815 info.smask = texImg->Width - 1; \
816 info.tmask = texImg->Height - 1; \
817 info.format = texImg->TexFormat; \
818 info.filter = obj->Sampler.MinFilter; \
819 info.envmode = unit->EnvMode; \
824 if (info.envmode == GL_BLEND) { \
825 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
826 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
827 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
828 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
829 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
831 if (!info.texture) { \
832 /* this shouldn't happen */ \
836 switch (info.format) { \
837 case MESA_FORMAT_RGB888: \
838 info.tbytesline = texImg->Width * 3; \
840 case MESA_FORMAT_RGBA8888: \
841 info.tbytesline = texImg->Width * 4; \
844 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
847 info.tsize = texImg->Height * info.tbytesline;
849 #define RENDER_SPAN( span ) \
850 span.interpMask &= ~SPAN_RGBA; \
851 span.arrayMask |= SPAN_RGBA; \
852 fast_persp_span(ctx, &span, &info);
854 #include "s_tritemp.h"
856 #endif /*CHAN_TYPE != GL_FLOAT*/
861 * Render an RGBA triangle with arbitrary attributes.
863 #define NAME general_triangle
866 #define INTERP_ALPHA 1
867 #define INTERP_ATTRIBS 1
868 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
869 #include "s_tritemp.h"
875 * Special tri function for occlusion testing
877 #define NAME occlusion_zless_16_triangle
880 struct gl_renderbuffer *rb = \
881 ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer; \
882 struct gl_query_object *q = ctx->Query.CurrentOcclusionObject; \
883 ASSERT(ctx->Depth.Test); \
884 ASSERT(!ctx->Depth.Mask); \
885 ASSERT(ctx->Depth.Func == GL_LESS); \
886 assert(rb->Format == MESA_FORMAT_Z16); \
890 #define RENDER_SPAN( span ) \
893 const GLushort *zRow = (const GLushort *) \
894 _swrast_pixel_address(rb, span.x, span.y); \
895 for (i = 0; i < span.end; i++) { \
896 GLuint z = FixedToDepth(span.z); \
900 span.z += span.zStep; \
903 #include "s_tritemp.h"
908 nodraw_triangle( struct gl_context *ctx,
913 (void) (ctx && v0 && v1 && v2);
918 * This is used when separate specular color is enabled, but not
919 * texturing. We add the specular color to the primary color,
920 * draw the triangle, then restore the original primary color.
921 * Inefficient, but seldom needed.
924 _swrast_add_spec_terms_triangle(struct gl_context *ctx, const SWvertex *v0,
925 const SWvertex *v1, const SWvertex *v2)
927 SWvertex *ncv0 = (SWvertex *)v0; /* drop const qualifier */
928 SWvertex *ncv1 = (SWvertex *)v1;
929 SWvertex *ncv2 = (SWvertex *)v2;
930 GLfloat rSum, gSum, bSum;
933 /* save original colors */
934 COPY_CHAN4( cSave[0], ncv0->color );
935 COPY_CHAN4( cSave[1], ncv1->color );
936 COPY_CHAN4( cSave[2], ncv2->color );
938 rSum = CHAN_TO_FLOAT(ncv0->color[0]) + ncv0->attrib[FRAG_ATTRIB_COL1][0];
939 gSum = CHAN_TO_FLOAT(ncv0->color[1]) + ncv0->attrib[FRAG_ATTRIB_COL1][1];
940 bSum = CHAN_TO_FLOAT(ncv0->color[2]) + ncv0->attrib[FRAG_ATTRIB_COL1][2];
941 UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[0], rSum);
942 UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[1], gSum);
943 UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[2], bSum);
945 rSum = CHAN_TO_FLOAT(ncv1->color[0]) + ncv1->attrib[FRAG_ATTRIB_COL1][0];
946 gSum = CHAN_TO_FLOAT(ncv1->color[1]) + ncv1->attrib[FRAG_ATTRIB_COL1][1];
947 bSum = CHAN_TO_FLOAT(ncv1->color[2]) + ncv1->attrib[FRAG_ATTRIB_COL1][2];
948 UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[0], rSum);
949 UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[1], gSum);
950 UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[2], bSum);
952 rSum = CHAN_TO_FLOAT(ncv2->color[0]) + ncv2->attrib[FRAG_ATTRIB_COL1][0];
953 gSum = CHAN_TO_FLOAT(ncv2->color[1]) + ncv2->attrib[FRAG_ATTRIB_COL1][1];
954 bSum = CHAN_TO_FLOAT(ncv2->color[2]) + ncv2->attrib[FRAG_ATTRIB_COL1][2];
955 UNCLAMPED_FLOAT_TO_CHAN(ncv2->color[0], rSum);
956 UNCLAMPED_FLOAT_TO_CHAN(ncv2->color[1], gSum);
957 UNCLAMPED_FLOAT_TO_CHAN(ncv2->color[2], bSum);
959 SWRAST_CONTEXT(ctx)->SpecTriangle( ctx, ncv0, ncv1, ncv2 );
960 /* restore original colors */
961 COPY_CHAN4( ncv0->color, cSave[0] );
962 COPY_CHAN4( ncv1->color, cSave[1] );
963 COPY_CHAN4( ncv2->color, cSave[2] );
970 /* record the current triangle function name */
971 const char *_mesa_triFuncName = NULL;
973 #define USE(triFunc) \
975 _mesa_triFuncName = #triFunc; \
976 /*printf("%s\n", _mesa_triFuncName);*/ \
977 swrast->Triangle = triFunc; \
982 #define USE(triFunc) swrast->Triangle = triFunc;
990 * Determine which triangle rendering function to use given the current
993 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
994 * remove tests to this code.
997 _swrast_choose_triangle( struct gl_context *ctx )
999 SWcontext *swrast = SWRAST_CONTEXT(ctx);
1001 if (ctx->Polygon.CullFlag &&
1002 ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) {
1003 USE(nodraw_triangle);
1007 if (ctx->RenderMode==GL_RENDER) {
1008 struct gl_renderbuffer *depthRb =
1009 ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
1011 if (ctx->Polygon.SmoothFlag) {
1012 _swrast_set_aa_triangle_function(ctx);
1013 ASSERT(swrast->Triangle);
1017 /* special case for occlusion testing */
1018 if (ctx->Query.CurrentOcclusionObject &&
1020 ctx->Depth.Mask == GL_FALSE &&
1021 ctx->Depth.Func == GL_LESS &&
1022 !ctx->Stencil._Enabled &&
1024 depthRb->Format == MESA_FORMAT_Z16) {
1025 if (ctx->Color.ColorMask[0][0] == 0 &&
1026 ctx->Color.ColorMask[0][1] == 0 &&
1027 ctx->Color.ColorMask[0][2] == 0 &&
1028 ctx->Color.ColorMask[0][3] == 0) {
1029 USE(occlusion_zless_16_triangle);
1035 * XXX should examine swrast->_ActiveAttribMask to determine what
1036 * needs to be interpolated.
1038 if (ctx->Texture._EnabledCoordUnits ||
1039 ctx->FragmentProgram._Current ||
1040 ctx->ATIFragmentShader._Enabled ||
1041 _mesa_need_secondary_color(ctx) ||
1042 swrast->_FogEnabled) {
1043 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1044 const struct gl_texture_object *texObj2D;
1045 const struct gl_texture_image *texImg;
1046 const struct swrast_texture_image *swImg;
1047 GLenum minFilter, magFilter, envMode;
1049 texObj2D = ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];
1051 texImg = texObj2D ? texObj2D->Image[0][texObj2D->BaseLevel] : NULL;
1052 swImg = swrast_texture_image_const(texImg);
1054 format = texImg ? texImg->TexFormat : MESA_FORMAT_NONE;
1055 minFilter = texObj2D ? texObj2D->Sampler.MinFilter : GL_NONE;
1056 magFilter = texObj2D ? texObj2D->Sampler.MagFilter : GL_NONE;
1057 envMode = ctx->Texture.Unit[0].EnvMode;
1059 /* First see if we can use an optimized 2-D texture function */
1060 if (ctx->Texture._EnabledCoordUnits == 0x1
1061 && !ctx->FragmentProgram._Current
1062 && !ctx->ATIFragmentShader._Enabled
1063 && ctx->Texture._EnabledUnits == 0x1
1064 && ctx->Texture.Unit[0]._ReallyEnabled == TEXTURE_2D_BIT
1065 && texObj2D->Sampler.WrapS == GL_REPEAT
1066 && texObj2D->Sampler.WrapT == GL_REPEAT
1067 && texObj2D->_Swizzle == SWIZZLE_NOOP
1068 && swImg->_IsPowerOfTwo
1069 && texImg->Border == 0
1070 && texImg->Width == swImg->RowStride
1071 && (format == MESA_FORMAT_RGB888 || format == MESA_FORMAT_RGBA8888)
1072 && minFilter == magFilter
1073 && ctx->Light.Model.ColorControl == GL_SINGLE_COLOR
1074 && !swrast->_FogEnabled
1075 && ctx->Texture.Unit[0].EnvMode != GL_COMBINE_EXT
1076 && ctx->Texture.Unit[0].EnvMode != GL_COMBINE4_NV) {
1077 if (ctx->Hint.PerspectiveCorrection==GL_FASTEST) {
1078 if (minFilter == GL_NEAREST
1079 && format == MESA_FORMAT_RGB888
1080 && (envMode == GL_REPLACE || envMode == GL_DECAL)
1081 && ((swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)
1082 && ctx->Depth.Func == GL_LESS
1083 && ctx->Depth.Mask == GL_TRUE)
1084 || swrast->_RasterMask == TEXTURE_BIT)
1085 && ctx->Polygon.StippleFlag == GL_FALSE
1086 && ctx->DrawBuffer->Visual.depthBits <= 16) {
1087 if (swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)) {
1088 USE(simple_z_textured_triangle);
1091 USE(simple_textured_triangle);
1096 USE(general_triangle);
1098 if (format == MESA_FORMAT_RGBA8888 && !_mesa_little_endian()) {
1099 /* We only handle RGBA8888 correctly on little endian
1100 * in the optimized code above.
1102 USE(general_triangle);
1105 USE(affine_textured_triangle);
1112 USE(general_triangle);
1114 USE(persp_textured_triangle);
1119 /* general case textured triangles */
1120 USE(general_triangle);
1124 ASSERT(!swrast->_FogEnabled);
1125 ASSERT(!_mesa_need_secondary_color(ctx));
1126 if (ctx->Light.ShadeModel==GL_SMOOTH) {
1127 /* smooth shaded, no texturing, stippled or some raster ops */
1129 USE(general_triangle);
1131 USE(smooth_rgba_triangle);
1135 /* flat shaded, no texturing, stippled or some raster ops */
1137 USE(general_triangle);
1139 USE(flat_rgba_triangle);
1144 else if (ctx->RenderMode==GL_FEEDBACK) {
1145 USE(_swrast_feedback_triangle);
1148 /* GL_SELECT mode */
1149 USE(_swrast_select_triangle);