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/texformat.h"
38 #include "shader/prog_instruction.h"
40 #include "s_aatriangle.h"
41 #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->attrib[FRAG_ATTRIB_WPOS][0] - v0->attrib[FRAG_ATTRIB_WPOS][0];
57 GLfloat ey = v1->attrib[FRAG_ATTRIB_WPOS][1] - v0->attrib[FRAG_ATTRIB_WPOS][1];
58 GLfloat fx = v2->attrib[FRAG_ATTRIB_WPOS][0] - v0->attrib[FRAG_ATTRIB_WPOS][0];
59 GLfloat fy = v2->attrib[FRAG_ATTRIB_WPOS][1] - v0->attrib[FRAG_ATTRIB_WPOS][1];
60 GLfloat c = ex*fy-ey*fx;
62 if (c * SWRAST_CONTEXT(ctx)->_BackfaceCullSign > 0)
71 * Render a smooth or flat-shaded color index triangle.
73 #define NAME ci_triangle
75 #define INTERP_ATTRIBS 1 /* just for fog */
76 #define INTERP_INDEX 1
77 #define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
78 #include "s_tritemp.h"
83 * Render a flat-shaded RGBA triangle.
85 #define NAME flat_rgba_triangle
88 ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
89 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
90 span.interpMask |= SPAN_RGBA; \
91 span.red = ChanToFixed(v2->color[0]); \
92 span.green = ChanToFixed(v2->color[1]); \
93 span.blue = ChanToFixed(v2->color[2]); \
94 span.alpha = ChanToFixed(v2->color[3]); \
99 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
100 #include "s_tritemp.h"
105 * Render a smooth-shaded RGBA triangle.
107 #define NAME smooth_rgba_triangle
110 #define INTERP_ALPHA 1
113 /* texturing must be off */ \
114 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \
115 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
117 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
118 #include "s_tritemp.h"
123 * Render an RGB, GL_DECAL, textured triangle.
124 * Interpolate S,T only w/out mipmapping or perspective correction.
126 * No fog. No depth testing.
128 #define NAME simple_textured_triangle
129 #define INTERP_INT_TEX 1
130 #define S_SCALE twidth
131 #define T_SCALE theight
134 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
135 struct gl_texture_object *obj = \
136 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
137 const GLint b = obj->BaseLevel; \
138 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
139 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
140 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
141 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
142 const GLint smask = obj->Image[0][b]->Width - 1; \
143 const GLint tmask = obj->Image[0][b]->Height - 1; \
144 if (!rb || !texture) { \
148 #define RENDER_SPAN( span ) \
150 GLchan rgb[MAX_WIDTH][3]; \
151 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
152 span.intTex[1] -= FIXED_HALF; \
153 for (i = 0; i < span.end; i++) { \
154 GLint s = FixedToInt(span.intTex[0]) & smask; \
155 GLint t = FixedToInt(span.intTex[1]) & tmask; \
156 GLint pos = (t << twidth_log2) + s; \
157 pos = pos + pos + pos; /* multiply by 3 */ \
158 rgb[i][RCOMP] = texture[pos]; \
159 rgb[i][GCOMP] = texture[pos+1]; \
160 rgb[i][BCOMP] = texture[pos+2]; \
161 span.intTex[0] += span.intTexStep[0]; \
162 span.intTex[1] += span.intTexStep[1]; \
164 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, NULL);
166 #include "s_tritemp.h"
171 * Render an RGB, GL_DECAL, textured triangle.
172 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
173 * perspective correction.
174 * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
178 #define NAME simple_z_textured_triangle
180 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
181 #define INTERP_INT_TEX 1
182 #define S_SCALE twidth
183 #define T_SCALE theight
186 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
187 struct gl_texture_object *obj = \
188 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
189 const GLint b = obj->BaseLevel; \
190 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
191 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
192 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
193 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
194 const GLint smask = obj->Image[0][b]->Width - 1; \
195 const GLint tmask = obj->Image[0][b]->Height - 1; \
196 if (!rb || !texture) { \
200 #define RENDER_SPAN( span ) \
202 GLchan rgb[MAX_WIDTH][3]; \
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 rgb[i][RCOMP] = texture[pos]; \
213 rgb[i][GCOMP] = texture[pos+1]; \
214 rgb[i][BCOMP] = texture[pos+2]; \
216 span.array->mask[i] = 1; \
219 span.array->mask[i] = 0; \
221 span.intTex[0] += span.intTexStep[0]; \
222 span.intTex[1] += span.intTexStep[1]; \
223 span.z += span.zStep; \
225 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, span.array->mask);
227 #include "s_tritemp.h"
230 #if CHAN_TYPE != GL_FLOAT
239 const GLchan *texture;
240 GLfixed er, eg, eb, ea;
241 GLint tbytesline, tsize;
246 ilerp(GLint t, GLint a, GLint b)
248 return a + ((t * (b - a)) >> FIXED_SHIFT);
252 ilerp_2d(GLint ia, GLint ib, GLint v00, GLint v10, GLint v01, GLint v11)
254 const GLint temp0 = ilerp(ia, v00, v10);
255 const GLint temp1 = ilerp(ia, v01, v11);
256 return ilerp(ib, temp0, temp1);
260 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
261 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
265 affine_span(GLcontext *ctx, SWspan *span,
266 struct affine_info *info)
268 GLchan sample[4]; /* the filtered texture sample */
269 const GLuint texEnableSave = ctx->Texture._EnabledCoordUnits;
271 /* Instead of defining a function for each mode, a test is done
272 * between the outer and inner loops. This is to reduce code size
273 * and complexity. Observe that an optimizing compiler kills
274 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
277 #define NEAREST_RGB \
278 sample[RCOMP] = tex00[RCOMP]; \
279 sample[GCOMP] = tex00[GCOMP]; \
280 sample[BCOMP] = tex00[BCOMP]; \
281 sample[ACOMP] = CHAN_MAX
284 sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
285 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
286 sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
287 sample[ACOMP] = CHAN_MAX;
289 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
291 #define LINEAR_RGBA \
292 sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
293 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
294 sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
295 sample[ACOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3])
298 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
299 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
300 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
301 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
304 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
305 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
306 >> (FIXED_SHIFT + 8); \
307 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
308 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
309 >> (FIXED_SHIFT + 8); \
310 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
311 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
312 >> (FIXED_SHIFT + 8); \
313 dest[ACOMP] = FixedToInt(span->alpha)
316 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
317 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
318 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
319 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
320 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
321 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
322 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
324 #define REPLACE COPY_CHAN4(dest, sample)
328 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
329 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
330 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
331 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
332 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
333 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
334 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
339 #define NEAREST_RGB_REPLACE \
341 dest[0] = sample[0]; \
342 dest[1] = sample[1]; \
343 dest[2] = sample[2]; \
344 dest[3] = FixedToInt(span->alpha);
346 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
348 #define SPAN_NEAREST(DO_TEX, COMPS) \
349 for (i = 0; i < span->end; i++) { \
350 /* Isn't it necessary to use FixedFloor below?? */ \
351 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
352 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
353 GLint pos = (t << info->twidth_log2) + s; \
354 const GLchan *tex00 = info->texture + COMPS * pos; \
356 span->red += span->redStep; \
357 span->green += span->greenStep; \
358 span->blue += span->blueStep; \
359 span->alpha += span->alphaStep; \
360 span->intTex[0] += span->intTexStep[0]; \
361 span->intTex[1] += span->intTexStep[1]; \
365 #define SPAN_LINEAR(DO_TEX, COMPS) \
366 for (i = 0; i < span->end; i++) { \
367 /* Isn't it necessary to use FixedFloor below?? */ \
368 const GLint s = FixedToInt(span->intTex[0]) & info->smask; \
369 const GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
370 const GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
371 const GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
372 const GLint pos = (t << info->twidth_log2) + s; \
373 const GLchan *tex00 = info->texture + COMPS * pos; \
374 const GLchan *tex10 = tex00 + info->tbytesline; \
375 const GLchan *tex01 = tex00 + COMPS; \
376 const GLchan *tex11 = tex10 + COMPS; \
377 if (t == info->tmask) { \
378 tex10 -= info->tsize; \
379 tex11 -= info->tsize; \
381 if (s == info->smask) { \
382 tex01 -= info->tbytesline; \
383 tex11 -= info->tbytesline; \
386 span->red += span->redStep; \
387 span->green += span->greenStep; \
388 span->blue += span->blueStep; \
389 span->alpha += span->alphaStep; \
390 span->intTex[0] += span->intTexStep[0]; \
391 span->intTex[1] += span->intTexStep[1]; \
397 GLchan *dest = span->array->rgba[0];
399 /* Disable tex units so they're not re-applied in swrast_write_rgba_span */
400 ctx->Texture._EnabledCoordUnits = 0x0;
402 span->intTex[0] -= FIXED_HALF;
403 span->intTex[1] -= FIXED_HALF;
404 switch (info->filter) {
406 switch (info->format) {
408 switch (info->envmode) {
410 SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
414 SPAN_NEAREST(NEAREST_RGB_REPLACE,3);
417 SPAN_NEAREST(NEAREST_RGB;BLEND,3);
420 SPAN_NEAREST(NEAREST_RGB;ADD,3);
423 _mesa_problem(ctx, "bad tex env mode in SPAN_LINEAR");
428 switch(info->envmode) {
430 SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
433 SPAN_NEAREST(NEAREST_RGBA;DECAL,4);
436 SPAN_NEAREST(NEAREST_RGBA;BLEND,4);
439 SPAN_NEAREST(NEAREST_RGBA;ADD,4);
442 SPAN_NEAREST(NEAREST_RGBA_REPLACE,4);
445 _mesa_problem(ctx, "bad tex env mode (2) in SPAN_LINEAR");
453 span->intTex[0] -= FIXED_HALF;
454 span->intTex[1] -= FIXED_HALF;
455 switch (info->format) {
457 switch (info->envmode) {
459 SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
463 SPAN_LINEAR(LINEAR_RGB;REPLACE,3);
466 SPAN_LINEAR(LINEAR_RGB;BLEND,3);
469 SPAN_LINEAR(LINEAR_RGB;ADD,3);
472 _mesa_problem(ctx, "bad tex env mode (3) in SPAN_LINEAR");
477 switch (info->envmode) {
479 SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
482 SPAN_LINEAR(LINEAR_RGBA;DECAL,4);
485 SPAN_LINEAR(LINEAR_RGBA;BLEND,4);
488 SPAN_LINEAR(LINEAR_RGBA;ADD,4);
491 SPAN_LINEAR(LINEAR_RGBA;REPLACE,4);
494 _mesa_problem(ctx, "bad tex env mode (4) in SPAN_LINEAR");
501 span->interpMask &= ~SPAN_RGBA;
502 ASSERT(span->arrayMask & SPAN_RGBA);
504 _swrast_write_rgba_span(ctx, span);
506 /* re-enable texture units */
507 ctx->Texture._EnabledCoordUnits = texEnableSave;
516 * Render an RGB/RGBA textured triangle without perspective correction.
518 #define NAME affine_textured_triangle
521 #define INTERP_ALPHA 1
522 #define INTERP_INT_TEX 1
523 #define S_SCALE twidth
524 #define T_SCALE theight
527 struct affine_info info; \
528 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
529 struct gl_texture_object *obj = \
530 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
531 const GLint b = obj->BaseLevel; \
532 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
533 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
534 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
535 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
536 info.smask = obj->Image[0][b]->Width - 1; \
537 info.tmask = obj->Image[0][b]->Height - 1; \
538 info.format = obj->Image[0][b]->_BaseFormat; \
539 info.filter = obj->MinFilter; \
540 info.envmode = unit->EnvMode; \
541 span.arrayMask |= SPAN_RGBA; \
543 if (info.envmode == GL_BLEND) { \
544 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
545 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
546 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
547 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
548 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
550 if (!info.texture) { \
551 /* this shouldn't happen */ \
555 switch (info.format) { \
559 info.tbytesline = obj->Image[0][b]->Width; \
561 case GL_LUMINANCE_ALPHA: \
562 info.tbytesline = obj->Image[0][b]->Width * 2; \
565 info.tbytesline = obj->Image[0][b]->Width * 3; \
568 info.tbytesline = obj->Image[0][b]->Width * 4; \
571 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
574 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
576 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
578 #include "s_tritemp.h"
589 const GLchan *texture;
590 GLfixed er, eg, eb, ea; /* texture env color */
591 GLint tbytesline, tsize;
596 fast_persp_span(GLcontext *ctx, SWspan *span,
597 struct persp_info *info)
599 GLchan sample[4]; /* the filtered texture sample */
601 /* Instead of defining a function for each mode, a test is done
602 * between the outer and inner loops. This is to reduce code size
603 * and complexity. Observe that an optimizing compiler kills
604 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
606 #define SPAN_NEAREST(DO_TEX,COMP) \
607 for (i = 0; i < span->end; i++) { \
608 GLdouble invQ = tex_coord[2] ? \
609 (1.0 / tex_coord[2]) : 1.0; \
610 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
611 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
612 GLint s = IFLOOR(s_tmp) & info->smask; \
613 GLint t = IFLOOR(t_tmp) & info->tmask; \
614 GLint pos = (t << info->twidth_log2) + s; \
615 const GLchan *tex00 = info->texture + COMP * pos; \
617 span->red += span->redStep; \
618 span->green += span->greenStep; \
619 span->blue += span->blueStep; \
620 span->alpha += span->alphaStep; \
621 tex_coord[0] += tex_step[0]; \
622 tex_coord[1] += tex_step[1]; \
623 tex_coord[2] += tex_step[2]; \
627 #define SPAN_LINEAR(DO_TEX,COMP) \
628 for (i = 0; i < span->end; i++) { \
629 GLdouble invQ = tex_coord[2] ? \
630 (1.0 / tex_coord[2]) : 1.0; \
631 const GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
632 const GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
633 const GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
634 const GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
635 const GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
636 const GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
637 const GLfixed sf = s_fix & FIXED_FRAC_MASK; \
638 const GLfixed tf = t_fix & FIXED_FRAC_MASK; \
639 const GLint pos = (t << info->twidth_log2) + s; \
640 const GLchan *tex00 = info->texture + COMP * pos; \
641 const GLchan *tex10 = tex00 + info->tbytesline; \
642 const GLchan *tex01 = tex00 + COMP; \
643 const GLchan *tex11 = tex10 + COMP; \
644 if (t == info->tmask) { \
645 tex10 -= info->tsize; \
646 tex11 -= info->tsize; \
648 if (s == info->smask) { \
649 tex01 -= info->tbytesline; \
650 tex11 -= info->tbytesline; \
653 span->red += span->redStep; \
654 span->green += span->greenStep; \
655 span->blue += span->blueStep; \
656 span->alpha += span->alphaStep; \
657 tex_coord[0] += tex_step[0]; \
658 tex_coord[1] += tex_step[1]; \
659 tex_coord[2] += tex_step[2]; \
664 GLfloat tex_coord[3], tex_step[3];
665 GLchan *dest = span->array->rgba[0];
667 const GLuint texEnableSave = ctx->Texture._EnabledCoordUnits;
668 ctx->Texture._EnabledCoordUnits = 0;
670 tex_coord[0] = span->attrStart[FRAG_ATTRIB_TEX0][0] * (info->smask + 1);
671 tex_step[0] = span->attrStepX[FRAG_ATTRIB_TEX0][0] * (info->smask + 1);
672 tex_coord[1] = span->attrStart[FRAG_ATTRIB_TEX0][1] * (info->tmask + 1);
673 tex_step[1] = span->attrStepX[FRAG_ATTRIB_TEX0][1] * (info->tmask + 1);
674 /* span->attrStart[FRAG_ATTRIB_TEX0][2] only if 3D-texturing, here only 2D */
675 tex_coord[2] = span->attrStart[FRAG_ATTRIB_TEX0][3];
676 tex_step[2] = span->attrStepX[FRAG_ATTRIB_TEX0][3];
678 switch (info->filter) {
680 switch (info->format) {
682 switch (info->envmode) {
684 SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
688 SPAN_NEAREST(NEAREST_RGB_REPLACE,3);
691 SPAN_NEAREST(NEAREST_RGB;BLEND,3);
694 SPAN_NEAREST(NEAREST_RGB;ADD,3);
697 _mesa_problem(ctx, "bad tex env mode (5) in SPAN_LINEAR");
702 switch(info->envmode) {
704 SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
707 SPAN_NEAREST(NEAREST_RGBA;DECAL,4);
710 SPAN_NEAREST(NEAREST_RGBA;BLEND,4);
713 SPAN_NEAREST(NEAREST_RGBA;ADD,4);
716 SPAN_NEAREST(NEAREST_RGBA_REPLACE,4);
719 _mesa_problem(ctx, "bad tex env mode (6) in SPAN_LINEAR");
727 switch (info->format) {
729 switch (info->envmode) {
731 SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
735 SPAN_LINEAR(LINEAR_RGB;REPLACE,3);
738 SPAN_LINEAR(LINEAR_RGB;BLEND,3);
741 SPAN_LINEAR(LINEAR_RGB;ADD,3);
744 _mesa_problem(ctx, "bad tex env mode (7) in SPAN_LINEAR");
749 switch (info->envmode) {
751 SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
754 SPAN_LINEAR(LINEAR_RGBA;DECAL,4);
757 SPAN_LINEAR(LINEAR_RGBA;BLEND,4);
760 SPAN_LINEAR(LINEAR_RGBA;ADD,4);
763 SPAN_LINEAR(LINEAR_RGBA;REPLACE,4);
766 _mesa_problem(ctx, "bad tex env mode (8) in SPAN_LINEAR");
774 ASSERT(span->arrayMask & SPAN_RGBA);
775 _swrast_write_rgba_span(ctx, span);
781 ctx->Texture._EnabledCoordUnits = texEnableSave;
786 * Render an perspective corrected RGB/RGBA textured triangle.
787 * The Q (aka V in Mesa) coordinate must be zero such that the divide
788 * by interpolated Q/W comes out right.
791 #define NAME persp_textured_triangle
794 #define INTERP_ALPHA 1
795 #define INTERP_ATTRIBS 1
798 struct persp_info info; \
799 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
800 struct gl_texture_object *obj = \
801 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
802 const GLint b = obj->BaseLevel; \
803 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
804 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
805 info.smask = obj->Image[0][b]->Width - 1; \
806 info.tmask = obj->Image[0][b]->Height - 1; \
807 info.format = obj->Image[0][b]->_BaseFormat; \
808 info.filter = obj->MinFilter; \
809 info.envmode = unit->EnvMode; \
811 if (info.envmode == GL_BLEND) { \
812 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
813 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
814 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
815 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
816 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
818 if (!info.texture) { \
819 /* this shouldn't happen */ \
823 switch (info.format) { \
827 info.tbytesline = obj->Image[0][b]->Width; \
829 case GL_LUMINANCE_ALPHA: \
830 info.tbytesline = obj->Image[0][b]->Width * 2; \
833 info.tbytesline = obj->Image[0][b]->Width * 3; \
836 info.tbytesline = obj->Image[0][b]->Width * 4; \
839 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
842 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
844 #define RENDER_SPAN( span ) \
845 span.interpMask &= ~SPAN_RGBA; \
846 span.arrayMask |= SPAN_RGBA; \
847 fast_persp_span(ctx, &span, &info);
849 #include "s_tritemp.h"
851 #endif /*CHAN_TYPE != GL_FLOAT*/
856 * Render an RGBA triangle with arbitrary attributes.
858 #define NAME general_triangle
861 #define INTERP_ALPHA 1
862 #define INTERP_ATTRIBS 1
863 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
864 #include "s_tritemp.h"
870 * Special tri function for occlusion testing
872 #define NAME occlusion_zless_triangle
875 struct gl_renderbuffer *rb = ctx->DrawBuffer->_DepthBuffer; \
876 struct gl_query_object *q = ctx->Query.CurrentOcclusionObject; \
877 ASSERT(ctx->Depth.Test); \
878 ASSERT(!ctx->Depth.Mask); \
879 ASSERT(ctx->Depth.Func == GL_LESS); \
883 #define RENDER_SPAN( span ) \
884 if (rb->DepthBits <= 16) { \
886 const GLushort *zRow = (const GLushort *) \
887 rb->GetPointer(ctx, rb, span.x, span.y); \
888 for (i = 0; i < span.end; i++) { \
889 GLuint z = FixedToDepth(span.z); \
893 span.z += span.zStep; \
898 const GLuint *zRow = (const GLuint *) \
899 rb->GetPointer(ctx, rb, span.x, span.y); \
900 for (i = 0; i < span.end; i++) { \
901 if ((GLuint)span.z < zRow[i]) { \
904 span.z += span.zStep; \
907 #include "s_tritemp.h"
912 nodraw_triangle( GLcontext *ctx,
917 (void) (ctx && v0 && v1 && v2);
922 * This is used when separate specular color is enabled, but not
923 * texturing. We add the specular color to the primary color,
924 * draw the triangle, then restore the original primary color.
925 * Inefficient, but seldom needed.
928 _swrast_add_spec_terms_triangle(GLcontext *ctx, const SWvertex *v0,
929 const SWvertex *v1, const SWvertex *v2)
931 SWvertex *ncv0 = (SWvertex *)v0; /* drop const qualifier */
932 SWvertex *ncv1 = (SWvertex *)v1;
933 SWvertex *ncv2 = (SWvertex *)v2;
934 GLfloat rSum, gSum, bSum;
937 /* save original colors */
938 COPY_CHAN4( cSave[0], ncv0->color );
939 COPY_CHAN4( cSave[1], ncv1->color );
940 COPY_CHAN4( cSave[2], ncv2->color );
942 rSum = CHAN_TO_FLOAT(ncv0->color[0]) + ncv0->attrib[FRAG_ATTRIB_COL1][0];
943 gSum = CHAN_TO_FLOAT(ncv0->color[1]) + ncv0->attrib[FRAG_ATTRIB_COL1][1];
944 bSum = CHAN_TO_FLOAT(ncv0->color[2]) + ncv0->attrib[FRAG_ATTRIB_COL1][2];
945 UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[0], rSum);
946 UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[1], gSum);
947 UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[2], bSum);
949 rSum = CHAN_TO_FLOAT(ncv1->color[0]) + ncv1->attrib[FRAG_ATTRIB_COL1][0];
950 gSum = CHAN_TO_FLOAT(ncv1->color[1]) + ncv1->attrib[FRAG_ATTRIB_COL1][1];
951 bSum = CHAN_TO_FLOAT(ncv1->color[2]) + ncv1->attrib[FRAG_ATTRIB_COL1][2];
952 UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[0], rSum);
953 UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[1], gSum);
954 UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[2], bSum);
956 rSum = CHAN_TO_FLOAT(ncv2->color[0]) + ncv2->attrib[FRAG_ATTRIB_COL1][0];
957 gSum = CHAN_TO_FLOAT(ncv2->color[1]) + ncv2->attrib[FRAG_ATTRIB_COL1][1];
958 bSum = CHAN_TO_FLOAT(ncv2->color[2]) + ncv2->attrib[FRAG_ATTRIB_COL1][2];
959 UNCLAMPED_FLOAT_TO_CHAN(ncv2->color[0], rSum);
960 UNCLAMPED_FLOAT_TO_CHAN(ncv2->color[1], gSum);
961 UNCLAMPED_FLOAT_TO_CHAN(ncv2->color[2], bSum);
963 SWRAST_CONTEXT(ctx)->SpecTriangle( ctx, ncv0, ncv1, ncv2 );
964 /* restore original colors */
965 COPY_CHAN4( ncv0->color, cSave[0] );
966 COPY_CHAN4( ncv1->color, cSave[1] );
967 COPY_CHAN4( ncv2->color, cSave[2] );
974 /* record the current triangle function name */
975 const char *_mesa_triFuncName = NULL;
977 #define USE(triFunc) \
979 _mesa_triFuncName = #triFunc; \
980 /*printf("%s\n", _mesa_triFuncName);*/ \
981 swrast->Triangle = triFunc; \
986 #define USE(triFunc) swrast->Triangle = triFunc;
994 * Determine which triangle rendering function to use given the current
997 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
998 * remove tests to this code.
1001 _swrast_choose_triangle( GLcontext *ctx )
1003 SWcontext *swrast = SWRAST_CONTEXT(ctx);
1004 const GLboolean rgbmode = ctx->Visual.rgbMode;
1006 if (ctx->Polygon.CullFlag &&
1007 ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) {
1008 USE(nodraw_triangle);
1012 if (ctx->RenderMode==GL_RENDER) {
1014 if (ctx->Polygon.SmoothFlag) {
1015 _swrast_set_aa_triangle_function(ctx);
1016 ASSERT(swrast->Triangle);
1020 /* special case for occlusion testing */
1021 if (ctx->Query.CurrentOcclusionObject &&
1023 ctx->Depth.Mask == GL_FALSE &&
1024 ctx->Depth.Func == GL_LESS &&
1025 !ctx->Stencil._Enabled) {
1027 ctx->Color.ColorMask[0] == 0 &&
1028 ctx->Color.ColorMask[1] == 0 &&
1029 ctx->Color.ColorMask[2] == 0 &&
1030 ctx->Color.ColorMask[3] == 0)
1032 (!rgbmode && ctx->Color.IndexMask == 0)) {
1033 USE(occlusion_zless_triangle);
1044 * XXX should examine swrast->_ActiveAttribMask to determine what
1045 * needs to be interpolated.
1047 if (ctx->Texture._EnabledCoordUnits ||
1048 ctx->FragmentProgram._Current ||
1049 ctx->ATIFragmentShader._Enabled ||
1050 NEED_SECONDARY_COLOR(ctx) ||
1051 swrast->_FogEnabled) {
1052 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1053 const struct gl_texture_object *texObj2D;
1054 const struct gl_texture_image *texImg;
1055 GLenum minFilter, magFilter, envMode;
1057 texObj2D = ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX];
1059 texImg = texObj2D ? texObj2D->Image[0][texObj2D->BaseLevel] : NULL;
1060 format = texImg ? texImg->TexFormat->MesaFormat : -1;
1061 minFilter = texObj2D ? texObj2D->MinFilter : (GLenum) 0;
1062 magFilter = texObj2D ? texObj2D->MagFilter : (GLenum) 0;
1063 envMode = ctx->Texture.Unit[0].EnvMode;
1065 /* First see if we can use an optimized 2-D texture function */
1066 if (ctx->Texture._EnabledCoordUnits == 0x1
1067 && !ctx->FragmentProgram._Current
1068 && !ctx->ATIFragmentShader._Enabled
1069 && ctx->Texture.Unit[0]._ReallyEnabled == TEXTURE_2D_BIT
1070 && texObj2D->WrapS == GL_REPEAT
1071 && texObj2D->WrapT == GL_REPEAT
1072 && texObj2D->_Swizzle == SWIZZLE_NOOP
1073 && texImg->_IsPowerOfTwo
1074 && texImg->Border == 0
1075 && texImg->Width == texImg->RowStride
1076 && (format == MESA_FORMAT_RGB || format == MESA_FORMAT_RGBA)
1077 && minFilter == magFilter
1078 && ctx->Light.Model.ColorControl == GL_SINGLE_COLOR
1079 && !swrast->_FogEnabled
1080 && ctx->Texture.Unit[0].EnvMode != GL_COMBINE_EXT
1081 && ctx->Texture.Unit[0].EnvMode != GL_COMBINE4_NV) {
1082 if (ctx->Hint.PerspectiveCorrection==GL_FASTEST) {
1083 if (minFilter == GL_NEAREST
1084 && format == MESA_FORMAT_RGB
1085 && (envMode == GL_REPLACE || envMode == GL_DECAL)
1086 && ((swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)
1087 && ctx->Depth.Func == GL_LESS
1088 && ctx->Depth.Mask == GL_TRUE)
1089 || swrast->_RasterMask == TEXTURE_BIT)
1090 && ctx->Polygon.StippleFlag == GL_FALSE
1091 && ctx->DrawBuffer->Visual.depthBits <= 16) {
1092 if (swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)) {
1093 USE(simple_z_textured_triangle);
1096 USE(simple_textured_triangle);
1101 USE(general_triangle);
1103 USE(affine_textured_triangle);
1109 USE(general_triangle);
1111 USE(persp_textured_triangle);
1116 /* general case textured triangles */
1117 USE(general_triangle);
1121 ASSERT(!swrast->_FogEnabled);
1122 ASSERT(!NEED_SECONDARY_COLOR(ctx));
1123 if (ctx->Light.ShadeModel==GL_SMOOTH) {
1124 /* smooth shaded, no texturing, stippled or some raster ops */
1126 USE(general_triangle);
1128 USE(smooth_rgba_triangle);
1132 /* flat shaded, no texturing, stippled or some raster ops */
1134 USE(general_triangle);
1136 USE(flat_rgba_triangle);
1141 else if (ctx->RenderMode==GL_FEEDBACK) {
1142 USE(_swrast_feedback_triangle);
1145 /* GL_SELECT mode */
1146 USE(_swrast_select_triangle);