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 "program/prog_instruction.h"
39 #include "s_aatriangle.h"
40 #include "s_context.h"
41 #include "s_feedback.h"
43 #include "s_triangle.h"
47 * Test if a triangle should be culled. Used for feedback and selection mode.
48 * \return GL_TRUE if the triangle is to be culled, GL_FALSE otherwise.
51 _swrast_culltriangle( struct gl_context
*ctx
,
56 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
57 GLfloat ex
= v1
->attrib
[FRAG_ATTRIB_WPOS
][0] - v0
->attrib
[FRAG_ATTRIB_WPOS
][0];
58 GLfloat ey
= v1
->attrib
[FRAG_ATTRIB_WPOS
][1] - v0
->attrib
[FRAG_ATTRIB_WPOS
][1];
59 GLfloat fx
= v2
->attrib
[FRAG_ATTRIB_WPOS
][0] - v0
->attrib
[FRAG_ATTRIB_WPOS
][0];
60 GLfloat fy
= v2
->attrib
[FRAG_ATTRIB_WPOS
][1] - v0
->attrib
[FRAG_ATTRIB_WPOS
][1];
61 GLfloat c
= ex
*fy
-ey
*fx
;
63 if (c
* swrast
->_BackfaceSign
* swrast
->_BackfaceCullSign
<= 0.0F
)
72 * Render a flat-shaded RGBA triangle.
74 #define NAME flat_rgba_triangle
77 ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
78 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
79 span.interpMask |= SPAN_RGBA; \
80 span.red = ChanToFixed(v2->color[0]); \
81 span.green = ChanToFixed(v2->color[1]); \
82 span.blue = ChanToFixed(v2->color[2]); \
83 span.alpha = ChanToFixed(v2->color[3]); \
88 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
89 #include "s_tritemp.h"
94 * Render a smooth-shaded RGBA triangle.
96 #define NAME smooth_rgba_triangle
99 #define INTERP_ALPHA 1
102 /* texturing must be off */ \
103 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \
104 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
106 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
107 #include "s_tritemp.h"
112 * Render an RGB, GL_DECAL, textured triangle.
113 * Interpolate S,T only w/out mipmapping or perspective correction.
115 * No fog. No depth testing.
117 #define NAME simple_textured_triangle
118 #define INTERP_INT_TEX 1
119 #define S_SCALE twidth
120 #define T_SCALE theight
123 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
124 const struct gl_texture_object *obj = \
125 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
126 const struct gl_texture_image *texImg = \
127 obj->Image[0][obj->BaseLevel]; \
128 const GLfloat twidth = (GLfloat) texImg->Width; \
129 const GLfloat theight = (GLfloat) texImg->Height; \
130 const GLint twidth_log2 = texImg->WidthLog2; \
131 const GLubyte *texture = (const GLubyte *) texImg->Data; \
132 const GLint smask = texImg->Width - 1; \
133 const GLint tmask = texImg->Height - 1; \
134 ASSERT(texImg->TexFormat == MESA_FORMAT_RGB888); \
135 if (!rb || !texture) { \
139 #define RENDER_SPAN( span ) \
141 GLubyte rgb[MAX_WIDTH][3]; \
142 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
143 span.intTex[1] -= FIXED_HALF; \
144 for (i = 0; i < span.end; i++) { \
145 GLint s = FixedToInt(span.intTex[0]) & smask; \
146 GLint t = FixedToInt(span.intTex[1]) & tmask; \
147 GLint pos = (t << twidth_log2) + s; \
148 pos = pos + pos + pos; /* multiply by 3 */ \
149 rgb[i][RCOMP] = texture[pos+2]; \
150 rgb[i][GCOMP] = texture[pos+1]; \
151 rgb[i][BCOMP] = texture[pos+0]; \
152 span.intTex[0] += span.intTexStep[0]; \
153 span.intTex[1] += span.intTexStep[1]; \
155 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, NULL);
157 #include "s_tritemp.h"
162 * Render an RGB, GL_DECAL, textured triangle.
163 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
164 * perspective correction.
165 * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
169 #define NAME simple_z_textured_triangle
171 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
172 #define INTERP_INT_TEX 1
173 #define S_SCALE twidth
174 #define T_SCALE theight
177 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
178 const struct gl_texture_object *obj = \
179 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
180 const struct gl_texture_image *texImg = \
181 obj->Image[0][obj->BaseLevel]; \
182 const GLfloat twidth = (GLfloat) texImg->Width; \
183 const GLfloat theight = (GLfloat) texImg->Height; \
184 const GLint twidth_log2 = texImg->WidthLog2; \
185 const GLubyte *texture = (const GLubyte *) texImg->Data; \
186 const GLint smask = texImg->Width - 1; \
187 const GLint tmask = texImg->Height - 1; \
188 ASSERT(texImg->TexFormat == MESA_FORMAT_RGB888); \
189 if (!rb || !texture) { \
193 #define RENDER_SPAN( span ) \
195 GLubyte rgb[MAX_WIDTH][3]; \
196 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
197 span.intTex[1] -= FIXED_HALF; \
198 for (i = 0; i < span.end; i++) { \
199 const GLuint z = FixedToDepth(span.z); \
201 GLint s = FixedToInt(span.intTex[0]) & smask; \
202 GLint t = FixedToInt(span.intTex[1]) & tmask; \
203 GLint pos = (t << twidth_log2) + s; \
204 pos = pos + pos + pos; /* multiply by 3 */ \
205 rgb[i][RCOMP] = texture[pos+2]; \
206 rgb[i][GCOMP] = texture[pos+1]; \
207 rgb[i][BCOMP] = texture[pos+0]; \
209 span.array->mask[i] = 1; \
212 span.array->mask[i] = 0; \
214 span.intTex[0] += span.intTexStep[0]; \
215 span.intTex[1] += span.intTexStep[1]; \
216 span.z += span.zStep; \
218 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, span.array->mask);
220 #include "s_tritemp.h"
223 #if CHAN_TYPE != GL_FLOAT
232 const GLchan
*texture
;
233 GLfixed er
, eg
, eb
, ea
;
234 GLint tbytesline
, tsize
;
239 ilerp(GLint t
, GLint a
, GLint b
)
241 return a
+ ((t
* (b
- a
)) >> FIXED_SHIFT
);
245 ilerp_2d(GLint ia
, GLint ib
, GLint v00
, GLint v10
, GLint v01
, GLint v11
)
247 const GLint temp0
= ilerp(ia
, v00
, v10
);
248 const GLint temp1
= ilerp(ia
, v01
, v11
);
249 return ilerp(ib
, temp0
, temp1
);
253 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
254 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
258 affine_span(struct gl_context
*ctx
, SWspan
*span
,
259 struct affine_info
*info
)
261 GLchan sample
[4]; /* the filtered texture sample */
262 const GLuint texEnableSave
= ctx
->Texture
._EnabledCoordUnits
;
264 /* Instead of defining a function for each mode, a test is done
265 * between the outer and inner loops. This is to reduce code size
266 * and complexity. Observe that an optimizing compiler kills
267 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
270 #define NEAREST_RGB \
271 sample[RCOMP] = tex00[2]; \
272 sample[GCOMP] = tex00[1]; \
273 sample[BCOMP] = tex00[0]; \
274 sample[ACOMP] = CHAN_MAX;
277 sample[RCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
278 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
279 sample[BCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
280 sample[ACOMP] = CHAN_MAX;
282 #define NEAREST_RGBA \
283 sample[RCOMP] = tex00[3]; \
284 sample[GCOMP] = tex00[2]; \
285 sample[BCOMP] = tex00[1]; \
286 sample[ACOMP] = tex00[0];
288 #define LINEAR_RGBA \
289 sample[RCOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3]);\
290 sample[GCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
291 sample[BCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
292 sample[ACOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0])
295 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
296 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
297 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
298 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
301 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
302 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
303 >> (FIXED_SHIFT + 8); \
304 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
305 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
306 >> (FIXED_SHIFT + 8); \
307 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
308 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
309 >> (FIXED_SHIFT + 8); \
310 dest[ACOMP] = FixedToInt(span->alpha)
313 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
314 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
315 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
316 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
317 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
318 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
319 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
321 #define REPLACE COPY_CHAN4(dest, sample)
325 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
326 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
327 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
328 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
329 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
330 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
331 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
336 #define NEAREST_RGB_REPLACE \
338 dest[0] = sample[0]; \
339 dest[1] = sample[1]; \
340 dest[2] = sample[2]; \
341 dest[3] = FixedToInt(span->alpha);
343 #define NEAREST_RGBA_REPLACE \
344 dest[RCOMP] = tex00[3]; \
345 dest[GCOMP] = tex00[2]; \
346 dest[BCOMP] = tex00[1]; \
347 dest[ACOMP] = tex00[0]
349 #define SPAN_NEAREST(DO_TEX, COMPS) \
350 for (i = 0; i < span->end; i++) { \
351 /* Isn't it necessary to use FixedFloor below?? */ \
352 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
353 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
354 GLint pos = (t << info->twidth_log2) + s; \
355 const GLchan *tex00 = info->texture + COMPS * pos; \
357 span->red += span->redStep; \
358 span->green += span->greenStep; \
359 span->blue += span->blueStep; \
360 span->alpha += span->alphaStep; \
361 span->intTex[0] += span->intTexStep[0]; \
362 span->intTex[1] += span->intTexStep[1]; \
366 #define SPAN_LINEAR(DO_TEX, COMPS) \
367 for (i = 0; i < span->end; i++) { \
368 /* Isn't it necessary to use FixedFloor below?? */ \
369 const GLint s = FixedToInt(span->intTex[0]) & info->smask; \
370 const GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
371 const GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
372 const GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
373 const GLint pos = (t << info->twidth_log2) + s; \
374 const GLchan *tex00 = info->texture + COMPS * pos; \
375 const GLchan *tex10 = tex00 + info->tbytesline; \
376 const GLchan *tex01 = tex00 + COMPS; \
377 const GLchan *tex11 = tex10 + COMPS; \
378 if (t == info->tmask) { \
379 tex10 -= info->tsize; \
380 tex11 -= info->tsize; \
382 if (s == info->smask) { \
383 tex01 -= info->tbytesline; \
384 tex11 -= info->tbytesline; \
387 span->red += span->redStep; \
388 span->green += span->greenStep; \
389 span->blue += span->blueStep; \
390 span->alpha += span->alphaStep; \
391 span->intTex[0] += span->intTexStep[0]; \
392 span->intTex[1] += span->intTexStep[1]; \
398 GLchan
*dest
= span
->array
->rgba
[0];
400 /* Disable tex units so they're not re-applied in swrast_write_rgba_span */
401 ctx
->Texture
._EnabledCoordUnits
= 0x0;
403 span
->intTex
[0] -= FIXED_HALF
;
404 span
->intTex
[1] -= FIXED_HALF
;
405 switch (info
->filter
) {
407 switch (info
->format
) {
408 case MESA_FORMAT_RGB888
:
409 switch (info
->envmode
) {
411 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
415 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
418 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
421 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
424 _mesa_problem(ctx
, "bad tex env mode in SPAN_LINEAR");
428 case MESA_FORMAT_RGBA8888
:
429 switch(info
->envmode
) {
431 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
434 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
437 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
440 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
443 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
446 _mesa_problem(ctx
, "bad tex env mode (2) in SPAN_LINEAR");
454 span
->intTex
[0] -= FIXED_HALF
;
455 span
->intTex
[1] -= FIXED_HALF
;
456 switch (info
->format
) {
457 case MESA_FORMAT_RGB888
:
458 switch (info
->envmode
) {
460 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
464 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
467 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
470 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
473 _mesa_problem(ctx
, "bad tex env mode (3) in SPAN_LINEAR");
477 case MESA_FORMAT_RGBA8888
:
478 switch (info
->envmode
) {
480 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
483 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
486 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
489 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
492 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
495 _mesa_problem(ctx
, "bad tex env mode (4) in SPAN_LINEAR");
502 span
->interpMask
&= ~SPAN_RGBA
;
503 ASSERT(span
->arrayMask
& SPAN_RGBA
);
505 _swrast_write_rgba_span(ctx
, span
);
507 /* re-enable texture units */
508 ctx
->Texture
._EnabledCoordUnits
= texEnableSave
;
517 * Render an RGB/RGBA textured triangle without perspective correction.
519 #define NAME affine_textured_triangle
522 #define INTERP_ALPHA 1
523 #define INTERP_INT_TEX 1
524 #define S_SCALE twidth
525 #define T_SCALE theight
528 struct affine_info info; \
529 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
530 const struct gl_texture_object *obj = \
531 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
532 const struct gl_texture_image *texImg = \
533 obj->Image[0][obj->BaseLevel]; \
534 const GLfloat twidth = (GLfloat) texImg->Width; \
535 const GLfloat theight = (GLfloat) texImg->Height; \
536 info.texture = (const GLchan *) texImg->Data; \
537 info.twidth_log2 = texImg->WidthLog2; \
538 info.smask = texImg->Width - 1; \
539 info.tmask = texImg->Height - 1; \
540 info.format = texImg->TexFormat; \
541 info.filter = obj->MinFilter; \
542 info.envmode = unit->EnvMode; \
546 span.arrayMask |= SPAN_RGBA; \
548 if (info.envmode == GL_BLEND) { \
549 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
550 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
551 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
552 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
553 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
555 if (!info.texture) { \
556 /* this shouldn't happen */ \
560 switch (info.format) { \
561 case MESA_FORMAT_RGB888: \
562 info.tbytesline = texImg->Width * 3; \
564 case MESA_FORMAT_RGBA8888: \
565 info.tbytesline = texImg->Width * 4; \
568 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
571 info.tsize = texImg->Height * info.tbytesline;
573 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
575 #include "s_tritemp.h"
586 const GLchan
*texture
;
587 GLfixed er
, eg
, eb
, ea
; /* texture env color */
588 GLint tbytesline
, tsize
;
593 fast_persp_span(struct gl_context
*ctx
, SWspan
*span
,
594 struct persp_info
*info
)
596 GLchan sample
[4]; /* the filtered texture sample */
598 /* Instead of defining a function for each mode, a test is done
599 * between the outer and inner loops. This is to reduce code size
600 * and complexity. Observe that an optimizing compiler kills
601 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
603 #define SPAN_NEAREST(DO_TEX,COMP) \
604 for (i = 0; i < span->end; i++) { \
605 GLdouble invQ = tex_coord[2] ? \
606 (1.0 / tex_coord[2]) : 1.0; \
607 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
608 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
609 GLint s = IFLOOR(s_tmp) & info->smask; \
610 GLint t = IFLOOR(t_tmp) & info->tmask; \
611 GLint pos = (t << info->twidth_log2) + s; \
612 const GLchan *tex00 = info->texture + COMP * pos; \
614 span->red += span->redStep; \
615 span->green += span->greenStep; \
616 span->blue += span->blueStep; \
617 span->alpha += span->alphaStep; \
618 tex_coord[0] += tex_step[0]; \
619 tex_coord[1] += tex_step[1]; \
620 tex_coord[2] += tex_step[2]; \
624 #define SPAN_LINEAR(DO_TEX,COMP) \
625 for (i = 0; i < span->end; i++) { \
626 GLdouble invQ = tex_coord[2] ? \
627 (1.0 / tex_coord[2]) : 1.0; \
628 const GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
629 const GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
630 const GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
631 const GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
632 const GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
633 const GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
634 const GLfixed sf = s_fix & FIXED_FRAC_MASK; \
635 const GLfixed tf = t_fix & FIXED_FRAC_MASK; \
636 const GLint pos = (t << info->twidth_log2) + s; \
637 const GLchan *tex00 = info->texture + COMP * pos; \
638 const GLchan *tex10 = tex00 + info->tbytesline; \
639 const GLchan *tex01 = tex00 + COMP; \
640 const GLchan *tex11 = tex10 + COMP; \
641 if (t == info->tmask) { \
642 tex10 -= info->tsize; \
643 tex11 -= info->tsize; \
645 if (s == info->smask) { \
646 tex01 -= info->tbytesline; \
647 tex11 -= info->tbytesline; \
650 span->red += span->redStep; \
651 span->green += span->greenStep; \
652 span->blue += span->blueStep; \
653 span->alpha += span->alphaStep; \
654 tex_coord[0] += tex_step[0]; \
655 tex_coord[1] += tex_step[1]; \
656 tex_coord[2] += tex_step[2]; \
661 GLfloat tex_coord
[3], tex_step
[3];
662 GLchan
*dest
= span
->array
->rgba
[0];
664 const GLuint texEnableSave
= ctx
->Texture
._EnabledCoordUnits
;
665 ctx
->Texture
._EnabledCoordUnits
= 0;
667 tex_coord
[0] = span
->attrStart
[FRAG_ATTRIB_TEX0
][0] * (info
->smask
+ 1);
668 tex_step
[0] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][0] * (info
->smask
+ 1);
669 tex_coord
[1] = span
->attrStart
[FRAG_ATTRIB_TEX0
][1] * (info
->tmask
+ 1);
670 tex_step
[1] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][1] * (info
->tmask
+ 1);
671 /* span->attrStart[FRAG_ATTRIB_TEX0][2] only if 3D-texturing, here only 2D */
672 tex_coord
[2] = span
->attrStart
[FRAG_ATTRIB_TEX0
][3];
673 tex_step
[2] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][3];
675 switch (info
->filter
) {
677 switch (info
->format
) {
678 case MESA_FORMAT_RGB888
:
679 switch (info
->envmode
) {
681 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
685 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
688 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
691 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
694 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
698 case MESA_FORMAT_RGBA8888
:
699 switch(info
->envmode
) {
701 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
704 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
707 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
710 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
713 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
716 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
724 switch (info
->format
) {
725 case MESA_FORMAT_RGB888
:
726 switch (info
->envmode
) {
728 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
732 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
735 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
738 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
741 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
745 case MESA_FORMAT_RGBA8888
:
746 switch (info
->envmode
) {
748 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
751 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
754 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
757 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
760 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
763 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
771 ASSERT(span
->arrayMask
& SPAN_RGBA
);
772 _swrast_write_rgba_span(ctx
, span
);
778 ctx
->Texture
._EnabledCoordUnits
= texEnableSave
;
783 * Render an perspective corrected RGB/RGBA textured triangle.
784 * The Q (aka V in Mesa) coordinate must be zero such that the divide
785 * by interpolated Q/W comes out right.
788 #define NAME persp_textured_triangle
791 #define INTERP_ALPHA 1
792 #define INTERP_ATTRIBS 1
795 struct persp_info info; \
796 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
797 const struct gl_texture_object *obj = \
798 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
799 const struct gl_texture_image *texImg = \
800 obj->Image[0][obj->BaseLevel]; \
801 info.texture = (const GLchan *) texImg->Data; \
802 info.twidth_log2 = texImg->WidthLog2; \
803 info.smask = texImg->Width - 1; \
804 info.tmask = texImg->Height - 1; \
805 info.format = texImg->TexFormat; \
806 info.filter = obj->MinFilter; \
807 info.envmode = unit->EnvMode; \
812 if (info.envmode == GL_BLEND) { \
813 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
814 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
815 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
816 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
817 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
819 if (!info.texture) { \
820 /* this shouldn't happen */ \
824 switch (info.format) { \
825 case MESA_FORMAT_RGB888: \
826 info.tbytesline = texImg->Width * 3; \
828 case MESA_FORMAT_RGBA8888: \
829 info.tbytesline = texImg->Width * 4; \
832 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
835 info.tsize = texImg->Height * info.tbytesline;
837 #define RENDER_SPAN( span ) \
838 span.interpMask &= ~SPAN_RGBA; \
839 span.arrayMask |= SPAN_RGBA; \
840 fast_persp_span(ctx, &span, &info);
842 #include "s_tritemp.h"
844 #endif /*CHAN_TYPE != GL_FLOAT*/
849 * Render an RGBA triangle with arbitrary attributes.
851 #define NAME general_triangle
854 #define INTERP_ALPHA 1
855 #define INTERP_ATTRIBS 1
856 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
857 #include "s_tritemp.h"
863 * Special tri function for occlusion testing
865 #define NAME occlusion_zless_triangle
868 struct gl_renderbuffer *rb = ctx->DrawBuffer->_DepthBuffer; \
869 struct gl_query_object *q = ctx->Query.CurrentOcclusionObject; \
870 ASSERT(ctx->Depth.Test); \
871 ASSERT(!ctx->Depth.Mask); \
872 ASSERT(ctx->Depth.Func == GL_LESS); \
876 #define RENDER_SPAN( span ) \
877 if (rb->Format == MESA_FORMAT_Z16) { \
879 const GLushort *zRow = (const GLushort *) \
880 rb->GetPointer(ctx, rb, span.x, span.y); \
881 for (i = 0; i < span.end; i++) { \
882 GLuint z = FixedToDepth(span.z); \
886 span.z += span.zStep; \
891 const GLuint *zRow = (const GLuint *) \
892 rb->GetPointer(ctx, rb, span.x, span.y); \
893 for (i = 0; i < span.end; i++) { \
894 if ((GLuint)span.z < zRow[i]) { \
897 span.z += span.zStep; \
900 #include "s_tritemp.h"
905 nodraw_triangle( struct gl_context
*ctx
,
910 (void) (ctx
&& v0
&& v1
&& v2
);
915 * This is used when separate specular color is enabled, but not
916 * texturing. We add the specular color to the primary color,
917 * draw the triangle, then restore the original primary color.
918 * Inefficient, but seldom needed.
921 _swrast_add_spec_terms_triangle(struct gl_context
*ctx
, const SWvertex
*v0
,
922 const SWvertex
*v1
, const SWvertex
*v2
)
924 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
925 SWvertex
*ncv1
= (SWvertex
*)v1
;
926 SWvertex
*ncv2
= (SWvertex
*)v2
;
927 GLfloat rSum
, gSum
, bSum
;
930 /* save original colors */
931 COPY_CHAN4( cSave
[0], ncv0
->color
);
932 COPY_CHAN4( cSave
[1], ncv1
->color
);
933 COPY_CHAN4( cSave
[2], ncv2
->color
);
935 rSum
= CHAN_TO_FLOAT(ncv0
->color
[0]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][0];
936 gSum
= CHAN_TO_FLOAT(ncv0
->color
[1]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][1];
937 bSum
= CHAN_TO_FLOAT(ncv0
->color
[2]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][2];
938 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[0], rSum
);
939 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[1], gSum
);
940 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[2], bSum
);
942 rSum
= CHAN_TO_FLOAT(ncv1
->color
[0]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][0];
943 gSum
= CHAN_TO_FLOAT(ncv1
->color
[1]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][1];
944 bSum
= CHAN_TO_FLOAT(ncv1
->color
[2]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][2];
945 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[0], rSum
);
946 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[1], gSum
);
947 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[2], bSum
);
949 rSum
= CHAN_TO_FLOAT(ncv2
->color
[0]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][0];
950 gSum
= CHAN_TO_FLOAT(ncv2
->color
[1]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][1];
951 bSum
= CHAN_TO_FLOAT(ncv2
->color
[2]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][2];
952 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[0], rSum
);
953 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[1], gSum
);
954 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[2], bSum
);
956 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
957 /* restore original colors */
958 COPY_CHAN4( ncv0
->color
, cSave
[0] );
959 COPY_CHAN4( ncv1
->color
, cSave
[1] );
960 COPY_CHAN4( ncv2
->color
, cSave
[2] );
967 /* record the current triangle function name */
968 const char *_mesa_triFuncName
= NULL
;
970 #define USE(triFunc) \
972 _mesa_triFuncName = #triFunc; \
973 /*printf("%s\n", _mesa_triFuncName);*/ \
974 swrast->Triangle = triFunc; \
979 #define USE(triFunc) swrast->Triangle = triFunc;
987 * Determine which triangle rendering function to use given the current
990 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
991 * remove tests to this code.
994 _swrast_choose_triangle( struct gl_context
*ctx
)
996 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
998 if (ctx
->Polygon
.CullFlag
&&
999 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1000 USE(nodraw_triangle
);
1004 if (ctx
->RenderMode
==GL_RENDER
) {
1006 if (ctx
->Polygon
.SmoothFlag
) {
1007 _swrast_set_aa_triangle_function(ctx
);
1008 ASSERT(swrast
->Triangle
);
1012 /* special case for occlusion testing */
1013 if (ctx
->Query
.CurrentOcclusionObject
&&
1015 ctx
->Depth
.Mask
== GL_FALSE
&&
1016 ctx
->Depth
.Func
== GL_LESS
&&
1017 !ctx
->Stencil
._Enabled
) {
1018 if (ctx
->Color
.ColorMask
[0][0] == 0 &&
1019 ctx
->Color
.ColorMask
[0][1] == 0 &&
1020 ctx
->Color
.ColorMask
[0][2] == 0 &&
1021 ctx
->Color
.ColorMask
[0][3] == 0) {
1022 USE(occlusion_zless_triangle
);
1028 * XXX should examine swrast->_ActiveAttribMask to determine what
1029 * needs to be interpolated.
1031 if (ctx
->Texture
._EnabledCoordUnits
||
1032 ctx
->FragmentProgram
._Current
||
1033 ctx
->ATIFragmentShader
._Enabled
||
1034 NEED_SECONDARY_COLOR(ctx
) ||
1035 swrast
->_FogEnabled
) {
1036 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1037 const struct gl_texture_object
*texObj2D
;
1038 const struct gl_texture_image
*texImg
;
1039 GLenum minFilter
, magFilter
, envMode
;
1041 texObj2D
= ctx
->Texture
.Unit
[0].CurrentTex
[TEXTURE_2D_INDEX
];
1043 texImg
= texObj2D
? texObj2D
->Image
[0][texObj2D
->BaseLevel
] : NULL
;
1044 format
= texImg
? texImg
->TexFormat
: MESA_FORMAT_NONE
;
1045 minFilter
= texObj2D
? texObj2D
->MinFilter
: GL_NONE
;
1046 magFilter
= texObj2D
? texObj2D
->MagFilter
: GL_NONE
;
1047 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1049 /* First see if we can use an optimized 2-D texture function */
1050 if (ctx
->Texture
._EnabledCoordUnits
== 0x1
1051 && !ctx
->FragmentProgram
._Current
1052 && !ctx
->ATIFragmentShader
._Enabled
1053 && ctx
->Texture
._EnabledUnits
== 0x1
1054 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1055 && texObj2D
->WrapS
== GL_REPEAT
1056 && texObj2D
->WrapT
== GL_REPEAT
1057 && texObj2D
->_Swizzle
== SWIZZLE_NOOP
1058 && texImg
->_IsPowerOfTwo
1059 && texImg
->Border
== 0
1060 && texImg
->Width
== texImg
->RowStride
1061 && (format
== MESA_FORMAT_RGB888
|| format
== MESA_FORMAT_RGBA8888
)
1062 && minFilter
== magFilter
1063 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1064 && !swrast
->_FogEnabled
1065 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
1066 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE4_NV
) {
1067 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1068 if (minFilter
== GL_NEAREST
1069 && format
== MESA_FORMAT_RGB888
1070 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1071 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1072 && ctx
->Depth
.Func
== GL_LESS
1073 && ctx
->Depth
.Mask
== GL_TRUE
)
1074 || swrast
->_RasterMask
== TEXTURE_BIT
)
1075 && ctx
->Polygon
.StippleFlag
== GL_FALSE
1076 && ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
1077 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1078 USE(simple_z_textured_triangle
);
1081 USE(simple_textured_triangle
);
1086 USE(general_triangle
);
1088 if (format
== MESA_FORMAT_RGBA8888
&& !_mesa_little_endian()) {
1089 /* We only handle RGBA8888 correctly on little endian
1090 * in the optimized code above.
1092 USE(general_triangle
);
1095 USE(affine_textured_triangle
);
1102 USE(general_triangle
);
1104 USE(persp_textured_triangle
);
1109 /* general case textured triangles */
1110 USE(general_triangle
);
1114 ASSERT(!swrast
->_FogEnabled
);
1115 ASSERT(!NEED_SECONDARY_COLOR(ctx
));
1116 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1117 /* smooth shaded, no texturing, stippled or some raster ops */
1119 USE(general_triangle
);
1121 USE(smooth_rgba_triangle
);
1125 /* flat shaded, no texturing, stippled or some raster ops */
1127 USE(general_triangle
);
1129 USE(flat_rgba_triangle
);
1134 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1135 USE(_swrast_feedback_triangle
);
1138 /* GL_SELECT mode */
1139 USE(_swrast_select_triangle
);