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 "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
)->_BackfaceCullSign
> 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]; \
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; \
142 if (!rb || !texture) { \
146 #define RENDER_SPAN( span ) \
148 GLchan rgb[MAX_WIDTH][3]; \
149 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
150 span.intTex[1] -= FIXED_HALF; \
151 for (i = 0; i < span.end; i++) { \
152 GLint s = FixedToInt(span.intTex[0]) & smask; \
153 GLint t = FixedToInt(span.intTex[1]) & tmask; \
154 GLint pos = (t << twidth_log2) + s; \
155 pos = pos + pos + pos; /* multiply by 3 */ \
156 rgb[i][RCOMP] = texture[pos]; \
157 rgb[i][GCOMP] = texture[pos+1]; \
158 rgb[i][BCOMP] = texture[pos+2]; \
159 span.intTex[0] += span.intTexStep[0]; \
160 span.intTex[1] += span.intTexStep[1]; \
162 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, NULL);
164 #include "s_tritemp.h"
169 * Render an RGB, GL_DECAL, textured triangle.
170 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
171 * perspective correction.
172 * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
176 #define NAME simple_z_textured_triangle
178 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
179 #define INTERP_INT_TEX 1
180 #define S_SCALE twidth
181 #define T_SCALE theight
184 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
185 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
186 const GLint b = obj->BaseLevel; \
187 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
188 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
189 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
190 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
191 const GLint smask = obj->Image[0][b]->Width - 1; \
192 const GLint tmask = obj->Image[0][b]->Height - 1; \
193 if (!rb || !texture) { \
197 #define RENDER_SPAN( span ) \
199 GLchan rgb[MAX_WIDTH][3]; \
200 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
201 span.intTex[1] -= FIXED_HALF; \
202 for (i = 0; i < span.end; i++) { \
203 const GLuint z = FixedToDepth(span.z); \
205 GLint s = FixedToInt(span.intTex[0]) & smask; \
206 GLint t = FixedToInt(span.intTex[1]) & tmask; \
207 GLint pos = (t << twidth_log2) + s; \
208 pos = pos + pos + pos; /* multiply by 3 */ \
209 rgb[i][RCOMP] = texture[pos]; \
210 rgb[i][GCOMP] = texture[pos+1]; \
211 rgb[i][BCOMP] = texture[pos+2]; \
213 span.array->mask[i] = 1; \
216 span.array->mask[i] = 0; \
218 span.intTex[0] += span.intTexStep[0]; \
219 span.intTex[1] += span.intTexStep[1]; \
220 span.z += span.zStep; \
222 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, span.array->mask);
224 #include "s_tritemp.h"
227 #if CHAN_TYPE != GL_FLOAT
236 const GLchan
*texture
;
237 GLfixed er
, eg
, eb
, ea
;
238 GLint tbytesline
, tsize
;
243 ilerp(GLint t
, GLint a
, GLint b
)
245 return a
+ ((t
* (b
- a
)) >> FIXED_SHIFT
);
249 ilerp_2d(GLint ia
, GLint ib
, GLint v00
, GLint v10
, GLint v01
, GLint v11
)
251 const GLint temp0
= ilerp(ia
, v00
, v10
);
252 const GLint temp1
= ilerp(ia
, v01
, v11
);
253 return ilerp(ib
, temp0
, temp1
);
257 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
258 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
262 affine_span(GLcontext
*ctx
, SWspan
*span
,
263 struct affine_info
*info
)
265 GLchan sample
[4]; /* the filtered texture sample */
267 /* Instead of defining a function for each mode, a test is done
268 * between the outer and inner loops. This is to reduce code size
269 * and complexity. Observe that an optimizing compiler kills
270 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
273 #define NEAREST_RGB \
274 sample[RCOMP] = tex00[RCOMP]; \
275 sample[GCOMP] = tex00[GCOMP]; \
276 sample[BCOMP] = tex00[BCOMP]; \
277 sample[ACOMP] = CHAN_MAX
280 sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
281 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
282 sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
283 sample[ACOMP] = CHAN_MAX;
285 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
287 #define LINEAR_RGBA \
288 sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
289 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
290 sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
291 sample[ACOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3])
294 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
295 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
296 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
297 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
300 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
301 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
302 >> (FIXED_SHIFT + 8); \
303 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
304 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
305 >> (FIXED_SHIFT + 8); \
306 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
307 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
308 >> (FIXED_SHIFT + 8); \
309 dest[ACOMP] = FixedToInt(span->alpha)
312 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
313 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
314 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
315 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
316 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
317 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
318 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
320 #define REPLACE COPY_CHAN4(dest, sample)
324 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
325 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
326 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
327 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
328 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
329 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
330 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
335 #define NEAREST_RGB_REPLACE \
337 dest[0] = sample[0]; \
338 dest[1] = sample[1]; \
339 dest[2] = sample[2]; \
340 dest[3] = FixedToInt(span->alpha);
342 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
344 #define SPAN_NEAREST(DO_TEX, COMPS) \
345 for (i = 0; i < span->end; i++) { \
346 /* Isn't it necessary to use FixedFloor below?? */ \
347 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
348 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
349 GLint pos = (t << info->twidth_log2) + s; \
350 const GLchan *tex00 = info->texture + COMPS * pos; \
352 span->red += span->redStep; \
353 span->green += span->greenStep; \
354 span->blue += span->blueStep; \
355 span->alpha += span->alphaStep; \
356 span->intTex[0] += span->intTexStep[0]; \
357 span->intTex[1] += span->intTexStep[1]; \
361 #define SPAN_LINEAR(DO_TEX, COMPS) \
362 for (i = 0; i < span->end; i++) { \
363 /* Isn't it necessary to use FixedFloor below?? */ \
364 const GLint s = FixedToInt(span->intTex[0]) & info->smask; \
365 const GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
366 const GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
367 const GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
368 const GLint pos = (t << info->twidth_log2) + s; \
369 const GLchan *tex00 = info->texture + COMPS * pos; \
370 const GLchan *tex10 = tex00 + info->tbytesline; \
371 const GLchan *tex01 = tex00 + COMPS; \
372 const GLchan *tex11 = tex10 + COMPS; \
373 if (t == info->tmask) { \
374 tex10 -= info->tsize; \
375 tex11 -= info->tsize; \
377 if (s == info->smask) { \
378 tex01 -= info->tbytesline; \
379 tex11 -= info->tbytesline; \
382 span->red += span->redStep; \
383 span->green += span->greenStep; \
384 span->blue += span->blueStep; \
385 span->alpha += span->alphaStep; \
386 span->intTex[0] += span->intTexStep[0]; \
387 span->intTex[1] += span->intTexStep[1]; \
393 GLchan
*dest
= span
->array
->rgba
[0];
395 span
->intTex
[0] -= FIXED_HALF
;
396 span
->intTex
[1] -= FIXED_HALF
;
397 switch (info
->filter
) {
399 switch (info
->format
) {
401 switch (info
->envmode
) {
403 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
407 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
410 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
413 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
416 _mesa_problem(ctx
, "bad tex env mode in SPAN_LINEAR");
421 switch(info
->envmode
) {
423 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
426 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
429 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
432 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
435 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
438 _mesa_problem(ctx
, "bad tex env mode (2) in SPAN_LINEAR");
446 span
->intTex
[0] -= FIXED_HALF
;
447 span
->intTex
[1] -= FIXED_HALF
;
448 switch (info
->format
) {
450 switch (info
->envmode
) {
452 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
456 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
459 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
462 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
465 _mesa_problem(ctx
, "bad tex env mode (3) in SPAN_LINEAR");
470 switch (info
->envmode
) {
472 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
475 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
478 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
481 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
484 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
487 _mesa_problem(ctx
, "bad tex env mode (4) in SPAN_LINEAR");
494 span
->interpMask
&= ~SPAN_RGBA
;
495 ASSERT(span
->arrayMask
& SPAN_RGBA
);
496 _swrast_write_rgba_span(ctx
, span
);
505 * Render an RGB/RGBA textured triangle without perspective correction.
507 #define NAME affine_textured_triangle
510 #define INTERP_ALPHA 1
511 #define INTERP_INT_TEX 1
512 #define S_SCALE twidth
513 #define T_SCALE theight
516 struct affine_info info; \
517 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
518 struct gl_texture_object *obj = unit->Current2D; \
519 const GLint b = obj->BaseLevel; \
520 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
521 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
522 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
523 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
524 info.smask = obj->Image[0][b]->Width - 1; \
525 info.tmask = obj->Image[0][b]->Height - 1; \
526 info.format = obj->Image[0][b]->_BaseFormat; \
527 info.filter = obj->MinFilter; \
528 info.envmode = unit->EnvMode; \
529 span.arrayMask |= SPAN_RGBA; \
531 if (info.envmode == GL_BLEND) { \
532 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
533 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
534 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
535 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
536 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
538 if (!info.texture) { \
539 /* this shouldn't happen */ \
543 switch (info.format) { \
547 info.tbytesline = obj->Image[0][b]->Width; \
549 case GL_LUMINANCE_ALPHA: \
550 info.tbytesline = obj->Image[0][b]->Width * 2; \
553 info.tbytesline = obj->Image[0][b]->Width * 3; \
556 info.tbytesline = obj->Image[0][b]->Width * 4; \
559 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
562 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
564 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
566 #include "s_tritemp.h"
577 const GLchan
*texture
;
578 GLfixed er
, eg
, eb
, ea
; /* texture env color */
579 GLint tbytesline
, tsize
;
584 fast_persp_span(GLcontext
*ctx
, SWspan
*span
,
585 struct persp_info
*info
)
587 GLchan sample
[4]; /* the filtered texture sample */
589 /* Instead of defining a function for each mode, a test is done
590 * between the outer and inner loops. This is to reduce code size
591 * and complexity. Observe that an optimizing compiler kills
592 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
594 #define SPAN_NEAREST(DO_TEX,COMP) \
595 for (i = 0; i < span->end; i++) { \
596 GLdouble invQ = tex_coord[2] ? \
597 (1.0 / tex_coord[2]) : 1.0; \
598 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
599 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
600 GLint s = IFLOOR(s_tmp) & info->smask; \
601 GLint t = IFLOOR(t_tmp) & info->tmask; \
602 GLint pos = (t << info->twidth_log2) + s; \
603 const GLchan *tex00 = info->texture + COMP * pos; \
605 span->red += span->redStep; \
606 span->green += span->greenStep; \
607 span->blue += span->blueStep; \
608 span->alpha += span->alphaStep; \
609 tex_coord[0] += tex_step[0]; \
610 tex_coord[1] += tex_step[1]; \
611 tex_coord[2] += tex_step[2]; \
615 #define SPAN_LINEAR(DO_TEX,COMP) \
616 for (i = 0; i < span->end; i++) { \
617 GLdouble invQ = tex_coord[2] ? \
618 (1.0 / tex_coord[2]) : 1.0; \
619 const GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
620 const GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
621 const GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
622 const GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
623 const GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
624 const GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
625 const GLfixed sf = s_fix & FIXED_FRAC_MASK; \
626 const GLfixed tf = t_fix & FIXED_FRAC_MASK; \
627 const GLint pos = (t << info->twidth_log2) + s; \
628 const GLchan *tex00 = info->texture + COMP * pos; \
629 const GLchan *tex10 = tex00 + info->tbytesline; \
630 const GLchan *tex01 = tex00 + COMP; \
631 const GLchan *tex11 = tex10 + COMP; \
632 if (t == info->tmask) { \
633 tex10 -= info->tsize; \
634 tex11 -= info->tsize; \
636 if (s == info->smask) { \
637 tex01 -= info->tbytesline; \
638 tex11 -= info->tbytesline; \
641 span->red += span->redStep; \
642 span->green += span->greenStep; \
643 span->blue += span->blueStep; \
644 span->alpha += span->alphaStep; \
645 tex_coord[0] += tex_step[0]; \
646 tex_coord[1] += tex_step[1]; \
647 tex_coord[2] += tex_step[2]; \
652 GLfloat tex_coord
[3], tex_step
[3];
653 GLchan
*dest
= span
->array
->rgba
[0];
655 const GLuint savedTexEnable
= ctx
->Texture
._EnabledUnits
;
656 ctx
->Texture
._EnabledUnits
= 0;
658 tex_coord
[0] = span
->attrStart
[FRAG_ATTRIB_TEX0
][0] * (info
->smask
+ 1);
659 tex_step
[0] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][0] * (info
->smask
+ 1);
660 tex_coord
[1] = span
->attrStart
[FRAG_ATTRIB_TEX0
][1] * (info
->tmask
+ 1);
661 tex_step
[1] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][1] * (info
->tmask
+ 1);
662 /* span->attrStart[FRAG_ATTRIB_TEX0][2] only if 3D-texturing, here only 2D */
663 tex_coord
[2] = span
->attrStart
[FRAG_ATTRIB_TEX0
][3];
664 tex_step
[2] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][3];
666 switch (info
->filter
) {
668 switch (info
->format
) {
670 switch (info
->envmode
) {
672 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
676 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
679 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
682 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
685 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
690 switch(info
->envmode
) {
692 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
695 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
698 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
701 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
704 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
707 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
715 switch (info
->format
) {
717 switch (info
->envmode
) {
719 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
723 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
726 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
729 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
732 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
737 switch (info
->envmode
) {
739 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
742 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
745 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
748 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
751 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
754 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
762 ASSERT(span
->arrayMask
& SPAN_RGBA
);
763 _swrast_write_rgba_span(ctx
, span
);
769 ctx
->Texture
._EnabledUnits
= savedTexEnable
;
774 * Render an perspective corrected RGB/RGBA textured triangle.
775 * The Q (aka V in Mesa) coordinate must be zero such that the divide
776 * by interpolated Q/W comes out right.
779 #define NAME persp_textured_triangle
782 #define INTERP_ALPHA 1
783 #define INTERP_ATTRIBS 1
786 struct persp_info info; \
787 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
788 const struct gl_texture_object *obj = unit->Current2D; \
789 const GLint b = obj->BaseLevel; \
790 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
791 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
792 info.smask = obj->Image[0][b]->Width - 1; \
793 info.tmask = obj->Image[0][b]->Height - 1; \
794 info.format = obj->Image[0][b]->_BaseFormat; \
795 info.filter = obj->MinFilter; \
796 info.envmode = unit->EnvMode; \
798 if (info.envmode == GL_BLEND) { \
799 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
800 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
801 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
802 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
803 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
805 if (!info.texture) { \
806 /* this shouldn't happen */ \
810 switch (info.format) { \
814 info.tbytesline = obj->Image[0][b]->Width; \
816 case GL_LUMINANCE_ALPHA: \
817 info.tbytesline = obj->Image[0][b]->Width * 2; \
820 info.tbytesline = obj->Image[0][b]->Width * 3; \
823 info.tbytesline = obj->Image[0][b]->Width * 4; \
826 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
829 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
831 #define RENDER_SPAN( span ) \
832 span.interpMask &= ~SPAN_RGBA; \
833 span.arrayMask |= SPAN_RGBA; \
834 fast_persp_span(ctx, &span, &info);
836 #include "s_tritemp.h"
838 #endif /*CHAN_TYPE != GL_FLOAT*/
843 * Render an RGBA triangle with arbitrary attributes.
845 #define NAME general_triangle
848 #define INTERP_ALPHA 1
849 #define INTERP_ATTRIBS 1
850 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
851 #include "s_tritemp.h"
857 * Special tri function for occlusion testing
859 #define NAME occlusion_zless_triangle
862 struct gl_renderbuffer *rb = ctx->DrawBuffer->_DepthBuffer; \
863 struct gl_query_object *q = ctx->Query.CurrentOcclusionObject; \
864 ASSERT(ctx->Depth.Test); \
865 ASSERT(!ctx->Depth.Mask); \
866 ASSERT(ctx->Depth.Func == GL_LESS); \
870 #define RENDER_SPAN( span ) \
871 if (rb->DepthBits <= 16) { \
873 const GLushort *zRow = (const GLushort *) \
874 rb->GetPointer(ctx, rb, span.x, span.y); \
875 for (i = 0; i < span.end; i++) { \
876 GLuint z = FixedToDepth(span.z); \
880 span.z += span.zStep; \
885 const GLuint *zRow = (const GLuint *) \
886 rb->GetPointer(ctx, rb, span.x, span.y); \
887 for (i = 0; i < span.end; i++) { \
888 if ((GLuint)span.z < zRow[i]) { \
891 span.z += span.zStep; \
894 #include "s_tritemp.h"
899 nodraw_triangle( GLcontext
*ctx
,
904 (void) (ctx
&& v0
&& v1
&& v2
);
909 * This is used when separate specular color is enabled, but not
910 * texturing. We add the specular color to the primary color,
911 * draw the triangle, then restore the original primary color.
912 * Inefficient, but seldom needed.
915 _swrast_add_spec_terms_triangle(GLcontext
*ctx
, const SWvertex
*v0
,
916 const SWvertex
*v1
, const SWvertex
*v2
)
918 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
919 SWvertex
*ncv1
= (SWvertex
*)v1
;
920 SWvertex
*ncv2
= (SWvertex
*)v2
;
921 GLfloat rSum
, gSum
, bSum
;
924 /* save original colors */
925 COPY_CHAN4( cSave
[0], ncv0
->color
);
926 COPY_CHAN4( cSave
[1], ncv1
->color
);
927 COPY_CHAN4( cSave
[2], ncv2
->color
);
929 rSum
= CHAN_TO_FLOAT(ncv0
->color
[0]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][0];
930 gSum
= CHAN_TO_FLOAT(ncv0
->color
[1]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][1];
931 bSum
= CHAN_TO_FLOAT(ncv0
->color
[2]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][2];
932 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[0], rSum
);
933 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[1], gSum
);
934 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[2], bSum
);
936 rSum
= CHAN_TO_FLOAT(ncv1
->color
[0]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][0];
937 gSum
= CHAN_TO_FLOAT(ncv1
->color
[1]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][1];
938 bSum
= CHAN_TO_FLOAT(ncv1
->color
[2]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][2];
939 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[0], rSum
);
940 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[1], gSum
);
941 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[2], bSum
);
943 rSum
= CHAN_TO_FLOAT(ncv2
->color
[0]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][0];
944 gSum
= CHAN_TO_FLOAT(ncv2
->color
[1]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][1];
945 bSum
= CHAN_TO_FLOAT(ncv2
->color
[2]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][2];
946 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[0], rSum
);
947 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[1], gSum
);
948 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[2], bSum
);
950 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
951 /* restore original colors */
952 COPY_CHAN4( ncv0
->color
, cSave
[0] );
953 COPY_CHAN4( ncv1
->color
, cSave
[1] );
954 COPY_CHAN4( ncv2
->color
, cSave
[2] );
961 /* record the current triangle function name */
962 const char *_mesa_triFuncName
= NULL
;
964 #define USE(triFunc) \
966 _mesa_triFuncName = #triFunc; \
967 /*printf("%s\n", _mesa_triFuncName);*/ \
968 swrast->Triangle = triFunc; \
973 #define USE(triFunc) swrast->Triangle = triFunc;
981 * Determine which triangle rendering function to use given the current
984 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
985 * remove tests to this code.
988 _swrast_choose_triangle( GLcontext
*ctx
)
990 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
991 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
993 if (ctx
->Polygon
.CullFlag
&&
994 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
995 USE(nodraw_triangle
);
999 if (ctx
->RenderMode
==GL_RENDER
) {
1001 if (ctx
->Polygon
.SmoothFlag
) {
1002 _swrast_set_aa_triangle_function(ctx
);
1003 ASSERT(swrast
->Triangle
);
1007 /* special case for occlusion testing */
1008 if (ctx
->Query
.CurrentOcclusionObject
&&
1010 ctx
->Depth
.Mask
== GL_FALSE
&&
1011 ctx
->Depth
.Func
== GL_LESS
&&
1012 !ctx
->Stencil
.Enabled
) {
1014 ctx
->Color
.ColorMask
[0] == 0 &&
1015 ctx
->Color
.ColorMask
[1] == 0 &&
1016 ctx
->Color
.ColorMask
[2] == 0 &&
1017 ctx
->Color
.ColorMask
[3] == 0)
1019 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1020 USE(occlusion_zless_triangle
);
1031 * XXX should examine swrast->_ActiveAttribMask to determine what
1032 * needs to be interpolated.
1034 if (ctx
->Texture
._EnabledCoordUnits
||
1035 ctx
->FragmentProgram
._Current
||
1036 ctx
->ATIFragmentShader
._Enabled
||
1037 NEED_SECONDARY_COLOR(ctx
) ||
1038 swrast
->_FogEnabled
) {
1039 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1040 const struct gl_texture_object
*texObj2D
;
1041 const struct gl_texture_image
*texImg
;
1042 GLenum minFilter
, magFilter
, envMode
;
1044 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1045 texImg
= texObj2D
? texObj2D
->Image
[0][texObj2D
->BaseLevel
] : NULL
;
1046 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1047 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1048 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1049 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1051 /* First see if we can use an optimized 2-D texture function */
1052 if (ctx
->Texture
._EnabledCoordUnits
== 0x1
1053 && !ctx
->FragmentProgram
._Current
1054 && !ctx
->ATIFragmentShader
._Enabled
1055 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1056 && texObj2D
->WrapS
== GL_REPEAT
1057 && texObj2D
->WrapT
== GL_REPEAT
1058 && texImg
->_IsPowerOfTwo
1059 && texImg
->Border
== 0
1060 && texImg
->Width
== texImg
->RowStride
1061 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1062 && minFilter
== magFilter
1063 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1064 && !swrast
->_FogEnabled
1065 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1066 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1067 if (minFilter
== GL_NEAREST
1068 && format
== MESA_FORMAT_RGB
1069 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1070 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1071 && ctx
->Depth
.Func
== GL_LESS
1072 && ctx
->Depth
.Mask
== GL_TRUE
)
1073 || swrast
->_RasterMask
== TEXTURE_BIT
)
1074 && ctx
->Polygon
.StippleFlag
== GL_FALSE
1075 && ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
1076 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1077 USE(simple_z_textured_triangle
);
1080 USE(simple_textured_triangle
);
1085 USE(general_triangle
);
1087 USE(affine_textured_triangle
);
1093 USE(general_triangle
);
1095 USE(persp_textured_triangle
);
1100 /* general case textured triangles */
1101 USE(general_triangle
);
1105 ASSERT(!swrast
->_FogEnabled
);
1106 ASSERT(!NEED_SECONDARY_COLOR(ctx
));
1107 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1108 /* smooth shaded, no texturing, stippled or some raster ops */
1110 USE(general_triangle
);
1112 USE(smooth_rgba_triangle
);
1116 /* flat shaded, no texturing, stippled or some raster ops */
1118 USE(general_triangle
);
1120 USE(flat_rgba_triangle
);
1125 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1126 USE(_swrast_feedback_triangle
);
1129 /* GL_SELECT mode */
1130 USE(_swrast_select_triangle
);