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 "program/prog_instruction.h"
40 #include "s_aatriangle.h"
41 #include "s_context.h"
42 #include "s_feedback.h"
44 #include "s_triangle.h"
48 * Test if a triangle should be culled. Used for feedback and selection mode.
49 * \return GL_TRUE if the triangle is to be culled, GL_FALSE otherwise.
52 _swrast_culltriangle( struct gl_context
*ctx
,
57 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
58 GLfloat ex
= v1
->attrib
[FRAG_ATTRIB_WPOS
][0] - v0
->attrib
[FRAG_ATTRIB_WPOS
][0];
59 GLfloat ey
= v1
->attrib
[FRAG_ATTRIB_WPOS
][1] - v0
->attrib
[FRAG_ATTRIB_WPOS
][1];
60 GLfloat fx
= v2
->attrib
[FRAG_ATTRIB_WPOS
][0] - v0
->attrib
[FRAG_ATTRIB_WPOS
][0];
61 GLfloat fy
= v2
->attrib
[FRAG_ATTRIB_WPOS
][1] - v0
->attrib
[FRAG_ATTRIB_WPOS
][1];
62 GLfloat c
= ex
*fy
-ey
*fx
;
64 if (c
* swrast
->_BackfaceSign
* swrast
->_BackfaceCullSign
<= 0.0F
)
73 * Render a flat-shaded RGBA triangle.
75 #define NAME flat_rgba_triangle
78 ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
79 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
80 span.interpMask |= SPAN_RGBA; \
81 span.red = ChanToFixed(v2->color[0]); \
82 span.green = ChanToFixed(v2->color[1]); \
83 span.blue = ChanToFixed(v2->color[2]); \
84 span.alpha = ChanToFixed(v2->color[3]); \
89 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
90 #include "s_tritemp.h"
95 * Render a smooth-shaded RGBA triangle.
97 #define NAME smooth_rgba_triangle
100 #define INTERP_ALPHA 1
103 /* texturing must be off */ \
104 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \
105 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
107 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
108 #include "s_tritemp.h"
113 * Render an RGB, GL_DECAL, textured triangle.
114 * Interpolate S,T only w/out mipmapping or perspective correction.
116 * No fog. No depth testing.
118 #define NAME simple_textured_triangle
119 #define INTERP_INT_TEX 1
120 #define S_SCALE twidth
121 #define T_SCALE theight
124 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
125 const struct gl_texture_object *obj = \
126 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
127 const struct gl_texture_image *texImg = \
128 obj->Image[0][obj->BaseLevel]; \
129 const GLfloat twidth = (GLfloat) texImg->Width; \
130 const GLfloat theight = (GLfloat) texImg->Height; \
131 const GLint twidth_log2 = texImg->WidthLog2; \
132 const GLubyte *texture = (const GLubyte *) texImg->Data; \
133 const GLint smask = texImg->Width - 1; \
134 const GLint tmask = texImg->Height - 1; \
135 ASSERT(texImg->TexFormat == MESA_FORMAT_RGB888); \
136 if (!rb || !texture) { \
140 #define RENDER_SPAN( span ) \
142 GLubyte rgb[MAX_WIDTH][3]; \
143 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
144 span.intTex[1] -= FIXED_HALF; \
145 for (i = 0; i < span.end; i++) { \
146 GLint s = FixedToInt(span.intTex[0]) & smask; \
147 GLint t = FixedToInt(span.intTex[1]) & tmask; \
148 GLint pos = (t << twidth_log2) + s; \
149 pos = pos + pos + pos; /* multiply by 3 */ \
150 rgb[i][RCOMP] = texture[pos+2]; \
151 rgb[i][GCOMP] = texture[pos+1]; \
152 rgb[i][BCOMP] = texture[pos+0]; \
153 span.intTex[0] += span.intTexStep[0]; \
154 span.intTex[1] += span.intTexStep[1]; \
156 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, NULL);
158 #include "s_tritemp.h"
163 * Render an RGB, GL_DECAL, textured triangle.
164 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
165 * perspective correction.
166 * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
170 #define NAME simple_z_textured_triangle
172 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
173 #define INTERP_INT_TEX 1
174 #define S_SCALE twidth
175 #define T_SCALE theight
178 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
179 const struct gl_texture_object *obj = \
180 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
181 const struct gl_texture_image *texImg = \
182 obj->Image[0][obj->BaseLevel]; \
183 const GLfloat twidth = (GLfloat) texImg->Width; \
184 const GLfloat theight = (GLfloat) texImg->Height; \
185 const GLint twidth_log2 = texImg->WidthLog2; \
186 const GLubyte *texture = (const GLubyte *) texImg->Data; \
187 const GLint smask = texImg->Width - 1; \
188 const GLint tmask = texImg->Height - 1; \
189 ASSERT(texImg->TexFormat == MESA_FORMAT_RGB888); \
190 if (!rb || !texture) { \
194 #define RENDER_SPAN( span ) \
196 GLubyte rgb[MAX_WIDTH][3]; \
197 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
198 span.intTex[1] -= FIXED_HALF; \
199 for (i = 0; i < span.end; i++) { \
200 const GLuint z = FixedToDepth(span.z); \
202 GLint s = FixedToInt(span.intTex[0]) & smask; \
203 GLint t = FixedToInt(span.intTex[1]) & tmask; \
204 GLint pos = (t << twidth_log2) + s; \
205 pos = pos + pos + pos; /* multiply by 3 */ \
206 rgb[i][RCOMP] = texture[pos+2]; \
207 rgb[i][GCOMP] = texture[pos+1]; \
208 rgb[i][BCOMP] = texture[pos+0]; \
210 span.array->mask[i] = 1; \
213 span.array->mask[i] = 0; \
215 span.intTex[0] += span.intTexStep[0]; \
216 span.intTex[1] += span.intTexStep[1]; \
217 span.z += span.zStep; \
219 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, span.array->mask);
221 #include "s_tritemp.h"
224 #if CHAN_TYPE != GL_FLOAT
233 const GLchan
*texture
;
234 GLfixed er
, eg
, eb
, ea
;
235 GLint tbytesline
, tsize
;
240 ilerp(GLint t
, GLint a
, GLint b
)
242 return a
+ ((t
* (b
- a
)) >> FIXED_SHIFT
);
246 ilerp_2d(GLint ia
, GLint ib
, GLint v00
, GLint v10
, GLint v01
, GLint v11
)
248 const GLint temp0
= ilerp(ia
, v00
, v10
);
249 const GLint temp1
= ilerp(ia
, v01
, v11
);
250 return ilerp(ib
, temp0
, temp1
);
254 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
255 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
259 affine_span(struct gl_context
*ctx
, SWspan
*span
,
260 struct affine_info
*info
)
262 GLchan sample
[4]; /* the filtered texture sample */
263 const GLuint texEnableSave
= ctx
->Texture
._EnabledCoordUnits
;
265 /* Instead of defining a function for each mode, a test is done
266 * between the outer and inner loops. This is to reduce code size
267 * and complexity. Observe that an optimizing compiler kills
268 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
271 #define NEAREST_RGB \
272 sample[RCOMP] = tex00[2]; \
273 sample[GCOMP] = tex00[1]; \
274 sample[BCOMP] = tex00[0]; \
275 sample[ACOMP] = CHAN_MAX;
278 sample[RCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
279 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
280 sample[BCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
281 sample[ACOMP] = CHAN_MAX;
283 #define NEAREST_RGBA \
284 sample[RCOMP] = tex00[3]; \
285 sample[GCOMP] = tex00[2]; \
286 sample[BCOMP] = tex00[1]; \
287 sample[ACOMP] = tex00[0];
289 #define LINEAR_RGBA \
290 sample[RCOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3]);\
291 sample[GCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
292 sample[BCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
293 sample[ACOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0])
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 \
345 dest[RCOMP] = tex00[3]; \
346 dest[GCOMP] = tex00[2]; \
347 dest[BCOMP] = tex00[1]; \
348 dest[ACOMP] = tex00[0]
350 #define SPAN_NEAREST(DO_TEX, COMPS) \
351 for (i = 0; i < span->end; i++) { \
352 /* Isn't it necessary to use FixedFloor below?? */ \
353 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
354 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
355 GLint pos = (t << info->twidth_log2) + s; \
356 const GLchan *tex00 = info->texture + COMPS * pos; \
358 span->red += span->redStep; \
359 span->green += span->greenStep; \
360 span->blue += span->blueStep; \
361 span->alpha += span->alphaStep; \
362 span->intTex[0] += span->intTexStep[0]; \
363 span->intTex[1] += span->intTexStep[1]; \
367 #define SPAN_LINEAR(DO_TEX, COMPS) \
368 for (i = 0; i < span->end; i++) { \
369 /* Isn't it necessary to use FixedFloor below?? */ \
370 const GLint s = FixedToInt(span->intTex[0]) & info->smask; \
371 const GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
372 const GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
373 const GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
374 const GLint pos = (t << info->twidth_log2) + s; \
375 const GLchan *tex00 = info->texture + COMPS * pos; \
376 const GLchan *tex10 = tex00 + info->tbytesline; \
377 const GLchan *tex01 = tex00 + COMPS; \
378 const GLchan *tex11 = tex10 + COMPS; \
379 if (t == info->tmask) { \
380 tex10 -= info->tsize; \
381 tex11 -= info->tsize; \
383 if (s == info->smask) { \
384 tex01 -= info->tbytesline; \
385 tex11 -= info->tbytesline; \
388 span->red += span->redStep; \
389 span->green += span->greenStep; \
390 span->blue += span->blueStep; \
391 span->alpha += span->alphaStep; \
392 span->intTex[0] += span->intTexStep[0]; \
393 span->intTex[1] += span->intTexStep[1]; \
399 GLchan
*dest
= span
->array
->rgba
[0];
401 /* Disable tex units so they're not re-applied in swrast_write_rgba_span */
402 ctx
->Texture
._EnabledCoordUnits
= 0x0;
404 span
->intTex
[0] -= FIXED_HALF
;
405 span
->intTex
[1] -= FIXED_HALF
;
406 switch (info
->filter
) {
408 switch (info
->format
) {
409 case MESA_FORMAT_RGB888
:
410 switch (info
->envmode
) {
412 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
416 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
419 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
422 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
425 _mesa_problem(ctx
, "bad tex env mode in SPAN_LINEAR");
429 case MESA_FORMAT_RGBA8888
:
430 switch(info
->envmode
) {
432 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
435 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
438 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
441 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
444 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
447 _mesa_problem(ctx
, "bad tex env mode (2) in SPAN_LINEAR");
455 span
->intTex
[0] -= FIXED_HALF
;
456 span
->intTex
[1] -= FIXED_HALF
;
457 switch (info
->format
) {
458 case MESA_FORMAT_RGB888
:
459 switch (info
->envmode
) {
461 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
465 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
468 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
471 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
474 _mesa_problem(ctx
, "bad tex env mode (3) in SPAN_LINEAR");
478 case MESA_FORMAT_RGBA8888
:
479 switch (info
->envmode
) {
481 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
484 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
487 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
490 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
493 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
496 _mesa_problem(ctx
, "bad tex env mode (4) in SPAN_LINEAR");
503 span
->interpMask
&= ~SPAN_RGBA
;
504 ASSERT(span
->arrayMask
& SPAN_RGBA
);
506 _swrast_write_rgba_span(ctx
, span
);
508 /* re-enable texture units */
509 ctx
->Texture
._EnabledCoordUnits
= texEnableSave
;
518 * Render an RGB/RGBA textured triangle without perspective correction.
520 #define NAME affine_textured_triangle
523 #define INTERP_ALPHA 1
524 #define INTERP_INT_TEX 1
525 #define S_SCALE twidth
526 #define T_SCALE theight
529 struct affine_info info; \
530 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
531 const struct gl_texture_object *obj = \
532 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
533 const struct gl_texture_image *texImg = \
534 obj->Image[0][obj->BaseLevel]; \
535 const GLfloat twidth = (GLfloat) texImg->Width; \
536 const GLfloat theight = (GLfloat) texImg->Height; \
537 info.texture = (const GLchan *) texImg->Data; \
538 info.twidth_log2 = texImg->WidthLog2; \
539 info.smask = texImg->Width - 1; \
540 info.tmask = texImg->Height - 1; \
541 info.format = texImg->TexFormat; \
542 info.filter = obj->MinFilter; \
543 info.envmode = unit->EnvMode; \
547 span.arrayMask |= SPAN_RGBA; \
549 if (info.envmode == GL_BLEND) { \
550 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
551 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
552 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
553 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
554 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
556 if (!info.texture) { \
557 /* this shouldn't happen */ \
561 switch (info.format) { \
562 case MESA_FORMAT_RGB888: \
563 info.tbytesline = texImg->Width * 3; \
565 case MESA_FORMAT_RGBA8888: \
566 info.tbytesline = texImg->Width * 4; \
569 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
572 info.tsize = texImg->Height * info.tbytesline;
574 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
576 #include "s_tritemp.h"
587 const GLchan
*texture
;
588 GLfixed er
, eg
, eb
, ea
; /* texture env color */
589 GLint tbytesline
, tsize
;
594 fast_persp_span(struct gl_context
*ctx
, SWspan
*span
,
595 struct persp_info
*info
)
597 GLchan sample
[4]; /* the filtered texture sample */
599 /* Instead of defining a function for each mode, a test is done
600 * between the outer and inner loops. This is to reduce code size
601 * and complexity. Observe that an optimizing compiler kills
602 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
604 #define SPAN_NEAREST(DO_TEX,COMP) \
605 for (i = 0; i < span->end; i++) { \
606 GLdouble invQ = tex_coord[2] ? \
607 (1.0 / tex_coord[2]) : 1.0; \
608 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
609 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
610 GLint s = IFLOOR(s_tmp) & info->smask; \
611 GLint t = IFLOOR(t_tmp) & info->tmask; \
612 GLint pos = (t << info->twidth_log2) + s; \
613 const GLchan *tex00 = info->texture + COMP * pos; \
615 span->red += span->redStep; \
616 span->green += span->greenStep; \
617 span->blue += span->blueStep; \
618 span->alpha += span->alphaStep; \
619 tex_coord[0] += tex_step[0]; \
620 tex_coord[1] += tex_step[1]; \
621 tex_coord[2] += tex_step[2]; \
625 #define SPAN_LINEAR(DO_TEX,COMP) \
626 for (i = 0; i < span->end; i++) { \
627 GLdouble invQ = tex_coord[2] ? \
628 (1.0 / tex_coord[2]) : 1.0; \
629 const GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
630 const GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
631 const GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
632 const GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
633 const GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
634 const GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
635 const GLfixed sf = s_fix & FIXED_FRAC_MASK; \
636 const GLfixed tf = t_fix & FIXED_FRAC_MASK; \
637 const GLint pos = (t << info->twidth_log2) + s; \
638 const GLchan *tex00 = info->texture + COMP * pos; \
639 const GLchan *tex10 = tex00 + info->tbytesline; \
640 const GLchan *tex01 = tex00 + COMP; \
641 const GLchan *tex11 = tex10 + COMP; \
642 if (t == info->tmask) { \
643 tex10 -= info->tsize; \
644 tex11 -= info->tsize; \
646 if (s == info->smask) { \
647 tex01 -= info->tbytesline; \
648 tex11 -= info->tbytesline; \
651 span->red += span->redStep; \
652 span->green += span->greenStep; \
653 span->blue += span->blueStep; \
654 span->alpha += span->alphaStep; \
655 tex_coord[0] += tex_step[0]; \
656 tex_coord[1] += tex_step[1]; \
657 tex_coord[2] += tex_step[2]; \
662 GLfloat tex_coord
[3], tex_step
[3];
663 GLchan
*dest
= span
->array
->rgba
[0];
665 const GLuint texEnableSave
= ctx
->Texture
._EnabledCoordUnits
;
666 ctx
->Texture
._EnabledCoordUnits
= 0;
668 tex_coord
[0] = span
->attrStart
[FRAG_ATTRIB_TEX0
][0] * (info
->smask
+ 1);
669 tex_step
[0] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][0] * (info
->smask
+ 1);
670 tex_coord
[1] = span
->attrStart
[FRAG_ATTRIB_TEX0
][1] * (info
->tmask
+ 1);
671 tex_step
[1] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][1] * (info
->tmask
+ 1);
672 /* span->attrStart[FRAG_ATTRIB_TEX0][2] only if 3D-texturing, here only 2D */
673 tex_coord
[2] = span
->attrStart
[FRAG_ATTRIB_TEX0
][3];
674 tex_step
[2] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][3];
676 switch (info
->filter
) {
678 switch (info
->format
) {
679 case MESA_FORMAT_RGB888
:
680 switch (info
->envmode
) {
682 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
686 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
689 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
692 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
695 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
699 case MESA_FORMAT_RGBA8888
:
700 switch(info
->envmode
) {
702 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
705 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
708 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
711 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
714 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
717 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
725 switch (info
->format
) {
726 case MESA_FORMAT_RGB888
:
727 switch (info
->envmode
) {
729 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
733 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
736 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
739 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
742 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
746 case MESA_FORMAT_RGBA8888
:
747 switch (info
->envmode
) {
749 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
752 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
755 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
758 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
761 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
764 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
772 ASSERT(span
->arrayMask
& SPAN_RGBA
);
773 _swrast_write_rgba_span(ctx
, span
);
779 ctx
->Texture
._EnabledCoordUnits
= texEnableSave
;
784 * Render an perspective corrected RGB/RGBA textured triangle.
785 * The Q (aka V in Mesa) coordinate must be zero such that the divide
786 * by interpolated Q/W comes out right.
789 #define NAME persp_textured_triangle
792 #define INTERP_ALPHA 1
793 #define INTERP_ATTRIBS 1
796 struct persp_info info; \
797 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
798 const struct gl_texture_object *obj = \
799 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
800 const struct gl_texture_image *texImg = \
801 obj->Image[0][obj->BaseLevel]; \
802 info.texture = (const GLchan *) texImg->Data; \
803 info.twidth_log2 = texImg->WidthLog2; \
804 info.smask = texImg->Width - 1; \
805 info.tmask = texImg->Height - 1; \
806 info.format = texImg->TexFormat; \
807 info.filter = obj->MinFilter; \
808 info.envmode = unit->EnvMode; \
813 if (info.envmode == GL_BLEND) { \
814 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
815 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
816 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
817 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
818 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
820 if (!info.texture) { \
821 /* this shouldn't happen */ \
825 switch (info.format) { \
826 case MESA_FORMAT_RGB888: \
827 info.tbytesline = texImg->Width * 3; \
829 case MESA_FORMAT_RGBA8888: \
830 info.tbytesline = texImg->Width * 4; \
833 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
836 info.tsize = texImg->Height * info.tbytesline;
838 #define RENDER_SPAN( span ) \
839 span.interpMask &= ~SPAN_RGBA; \
840 span.arrayMask |= SPAN_RGBA; \
841 fast_persp_span(ctx, &span, &info);
843 #include "s_tritemp.h"
845 #endif /*CHAN_TYPE != GL_FLOAT*/
850 * Render an RGBA triangle with arbitrary attributes.
852 #define NAME general_triangle
855 #define INTERP_ALPHA 1
856 #define INTERP_ATTRIBS 1
857 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
858 #include "s_tritemp.h"
864 * Special tri function for occlusion testing
866 #define NAME occlusion_zless_triangle
869 struct gl_renderbuffer *rb = ctx->DrawBuffer->_DepthBuffer; \
870 struct gl_query_object *q = ctx->Query.CurrentOcclusionObject; \
871 ASSERT(ctx->Depth.Test); \
872 ASSERT(!ctx->Depth.Mask); \
873 ASSERT(ctx->Depth.Func == GL_LESS); \
877 #define RENDER_SPAN( span ) \
878 if (rb->Format == MESA_FORMAT_Z16) { \
880 const GLushort *zRow = (const GLushort *) \
881 rb->GetPointer(ctx, rb, span.x, span.y); \
882 for (i = 0; i < span.end; i++) { \
883 GLuint z = FixedToDepth(span.z); \
887 span.z += span.zStep; \
892 const GLuint *zRow = (const GLuint *) \
893 rb->GetPointer(ctx, rb, span.x, span.y); \
894 for (i = 0; i < span.end; i++) { \
895 if ((GLuint)span.z < zRow[i]) { \
898 span.z += span.zStep; \
901 #include "s_tritemp.h"
906 nodraw_triangle( struct gl_context
*ctx
,
911 (void) (ctx
&& v0
&& v1
&& v2
);
916 * This is used when separate specular color is enabled, but not
917 * texturing. We add the specular color to the primary color,
918 * draw the triangle, then restore the original primary color.
919 * Inefficient, but seldom needed.
922 _swrast_add_spec_terms_triangle(struct gl_context
*ctx
, const SWvertex
*v0
,
923 const SWvertex
*v1
, const SWvertex
*v2
)
925 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
926 SWvertex
*ncv1
= (SWvertex
*)v1
;
927 SWvertex
*ncv2
= (SWvertex
*)v2
;
928 GLfloat rSum
, gSum
, bSum
;
931 /* save original colors */
932 COPY_CHAN4( cSave
[0], ncv0
->color
);
933 COPY_CHAN4( cSave
[1], ncv1
->color
);
934 COPY_CHAN4( cSave
[2], ncv2
->color
);
936 rSum
= CHAN_TO_FLOAT(ncv0
->color
[0]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][0];
937 gSum
= CHAN_TO_FLOAT(ncv0
->color
[1]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][1];
938 bSum
= CHAN_TO_FLOAT(ncv0
->color
[2]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][2];
939 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[0], rSum
);
940 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[1], gSum
);
941 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[2], bSum
);
943 rSum
= CHAN_TO_FLOAT(ncv1
->color
[0]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][0];
944 gSum
= CHAN_TO_FLOAT(ncv1
->color
[1]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][1];
945 bSum
= CHAN_TO_FLOAT(ncv1
->color
[2]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][2];
946 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[0], rSum
);
947 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[1], gSum
);
948 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[2], bSum
);
950 rSum
= CHAN_TO_FLOAT(ncv2
->color
[0]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][0];
951 gSum
= CHAN_TO_FLOAT(ncv2
->color
[1]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][1];
952 bSum
= CHAN_TO_FLOAT(ncv2
->color
[2]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][2];
953 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[0], rSum
);
954 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[1], gSum
);
955 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[2], bSum
);
957 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
958 /* restore original colors */
959 COPY_CHAN4( ncv0
->color
, cSave
[0] );
960 COPY_CHAN4( ncv1
->color
, cSave
[1] );
961 COPY_CHAN4( ncv2
->color
, cSave
[2] );
968 /* record the current triangle function name */
969 const char *_mesa_triFuncName
= NULL
;
971 #define USE(triFunc) \
973 _mesa_triFuncName = #triFunc; \
974 /*printf("%s\n", _mesa_triFuncName);*/ \
975 swrast->Triangle = triFunc; \
980 #define USE(triFunc) swrast->Triangle = triFunc;
988 * Determine which triangle rendering function to use given the current
991 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
992 * remove tests to this code.
995 _swrast_choose_triangle( struct gl_context
*ctx
)
997 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
999 if (ctx
->Polygon
.CullFlag
&&
1000 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1001 USE(nodraw_triangle
);
1005 if (ctx
->RenderMode
==GL_RENDER
) {
1007 if (ctx
->Polygon
.SmoothFlag
) {
1008 _swrast_set_aa_triangle_function(ctx
);
1009 ASSERT(swrast
->Triangle
);
1013 /* special case for occlusion testing */
1014 if (ctx
->Query
.CurrentOcclusionObject
&&
1016 ctx
->Depth
.Mask
== GL_FALSE
&&
1017 ctx
->Depth
.Func
== GL_LESS
&&
1018 !ctx
->Stencil
._Enabled
) {
1019 if (ctx
->Color
.ColorMask
[0][0] == 0 &&
1020 ctx
->Color
.ColorMask
[0][1] == 0 &&
1021 ctx
->Color
.ColorMask
[0][2] == 0 &&
1022 ctx
->Color
.ColorMask
[0][3] == 0) {
1023 USE(occlusion_zless_triangle
);
1029 * XXX should examine swrast->_ActiveAttribMask to determine what
1030 * needs to be interpolated.
1032 if (ctx
->Texture
._EnabledCoordUnits
||
1033 ctx
->FragmentProgram
._Current
||
1034 ctx
->ATIFragmentShader
._Enabled
||
1035 NEED_SECONDARY_COLOR(ctx
) ||
1036 swrast
->_FogEnabled
) {
1037 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1038 const struct gl_texture_object
*texObj2D
;
1039 const struct gl_texture_image
*texImg
;
1040 GLenum minFilter
, magFilter
, envMode
;
1042 texObj2D
= ctx
->Texture
.Unit
[0].CurrentTex
[TEXTURE_2D_INDEX
];
1044 texImg
= texObj2D
? texObj2D
->Image
[0][texObj2D
->BaseLevel
] : NULL
;
1045 format
= texImg
? texImg
->TexFormat
: MESA_FORMAT_NONE
;
1046 minFilter
= texObj2D
? texObj2D
->MinFilter
: GL_NONE
;
1047 magFilter
= texObj2D
? texObj2D
->MagFilter
: GL_NONE
;
1048 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1050 /* First see if we can use an optimized 2-D texture function */
1051 if (ctx
->Texture
._EnabledCoordUnits
== 0x1
1052 && !ctx
->FragmentProgram
._Current
1053 && !ctx
->ATIFragmentShader
._Enabled
1054 && ctx
->Texture
._EnabledUnits
== 0x1
1055 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1056 && texObj2D
->WrapS
== GL_REPEAT
1057 && texObj2D
->WrapT
== GL_REPEAT
1058 && texObj2D
->_Swizzle
== SWIZZLE_NOOP
1059 && texImg
->_IsPowerOfTwo
1060 && texImg
->Border
== 0
1061 && texImg
->Width
== texImg
->RowStride
1062 && (format
== MESA_FORMAT_RGB888
|| format
== MESA_FORMAT_RGBA8888
)
1063 && minFilter
== magFilter
1064 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1065 && !swrast
->_FogEnabled
1066 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
1067 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE4_NV
) {
1068 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1069 if (minFilter
== GL_NEAREST
1070 && format
== MESA_FORMAT_RGB888
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 if (format
== MESA_FORMAT_RGBA8888
&& !_mesa_little_endian()) {
1090 /* We only handle RGBA8888 correctly on little endian
1091 * in the optimized code above.
1093 USE(general_triangle
);
1096 USE(affine_textured_triangle
);
1103 USE(general_triangle
);
1105 USE(persp_textured_triangle
);
1110 /* general case textured triangles */
1111 USE(general_triangle
);
1115 ASSERT(!swrast
->_FogEnabled
);
1116 ASSERT(!NEED_SECONDARY_COLOR(ctx
));
1117 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1118 /* smooth shaded, no texturing, stippled or some raster ops */
1120 USE(general_triangle
);
1122 USE(smooth_rgba_triangle
);
1126 /* flat shaded, no texturing, stippled or some raster ops */
1128 USE(general_triangle
);
1130 USE(flat_rgba_triangle
);
1135 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1136 USE(_swrast_feedback_triangle
);
1139 /* GL_SELECT mode */
1140 USE(_swrast_select_triangle
);