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"
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 */
266 const GLuint texEnableSave
= ctx
->Texture
._EnabledUnits
;
268 /* Disable tex units so they're not re-applied in swrast_write_rgba_span */
269 ctx
->Texture
._EnabledUnits
= 0x0;
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 span
->intTex
[0] -= FIXED_HALF
;
400 span
->intTex
[1] -= FIXED_HALF
;
401 switch (info
->filter
) {
403 switch (info
->format
) {
405 switch (info
->envmode
) {
407 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
411 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
414 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
417 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
420 _mesa_problem(ctx
, "bad tex env mode in SPAN_LINEAR");
425 switch(info
->envmode
) {
427 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
430 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
433 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
436 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
439 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
442 _mesa_problem(ctx
, "bad tex env mode (2) in SPAN_LINEAR");
450 span
->intTex
[0] -= FIXED_HALF
;
451 span
->intTex
[1] -= FIXED_HALF
;
452 switch (info
->format
) {
454 switch (info
->envmode
) {
456 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
460 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
463 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
466 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
469 _mesa_problem(ctx
, "bad tex env mode (3) in SPAN_LINEAR");
474 switch (info
->envmode
) {
476 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
479 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
482 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
485 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
488 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
491 _mesa_problem(ctx
, "bad tex env mode (4) in SPAN_LINEAR");
498 span
->interpMask
&= ~SPAN_RGBA
;
499 ASSERT(span
->arrayMask
& SPAN_RGBA
);
501 _swrast_write_rgba_span(ctx
, span
);
503 /* re-enable texture units */
504 ctx
->Texture
._EnabledUnits
= texEnableSave
;
513 * Render an RGB/RGBA textured triangle without perspective correction.
515 #define NAME affine_textured_triangle
518 #define INTERP_ALPHA 1
519 #define INTERP_INT_TEX 1
520 #define S_SCALE twidth
521 #define T_SCALE theight
524 struct affine_info info; \
525 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
526 struct gl_texture_object *obj = unit->Current2D; \
527 const GLint b = obj->BaseLevel; \
528 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
529 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
530 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
531 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
532 info.smask = obj->Image[0][b]->Width - 1; \
533 info.tmask = obj->Image[0][b]->Height - 1; \
534 info.format = obj->Image[0][b]->_BaseFormat; \
535 info.filter = obj->MinFilter; \
536 info.envmode = unit->EnvMode; \
537 span.arrayMask |= SPAN_RGBA; \
539 if (info.envmode == GL_BLEND) { \
540 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
541 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
542 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
543 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
544 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
546 if (!info.texture) { \
547 /* this shouldn't happen */ \
551 switch (info.format) { \
555 info.tbytesline = obj->Image[0][b]->Width; \
557 case GL_LUMINANCE_ALPHA: \
558 info.tbytesline = obj->Image[0][b]->Width * 2; \
561 info.tbytesline = obj->Image[0][b]->Width * 3; \
564 info.tbytesline = obj->Image[0][b]->Width * 4; \
567 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
570 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
572 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
574 #include "s_tritemp.h"
585 const GLchan
*texture
;
586 GLfixed er
, eg
, eb
, ea
; /* texture env color */
587 GLint tbytesline
, tsize
;
592 fast_persp_span(GLcontext
*ctx
, SWspan
*span
,
593 struct persp_info
*info
)
595 GLchan sample
[4]; /* the filtered texture sample */
597 /* Instead of defining a function for each mode, a test is done
598 * between the outer and inner loops. This is to reduce code size
599 * and complexity. Observe that an optimizing compiler kills
600 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
602 #define SPAN_NEAREST(DO_TEX,COMP) \
603 for (i = 0; i < span->end; i++) { \
604 GLdouble invQ = tex_coord[2] ? \
605 (1.0 / tex_coord[2]) : 1.0; \
606 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
607 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
608 GLint s = IFLOOR(s_tmp) & info->smask; \
609 GLint t = IFLOOR(t_tmp) & info->tmask; \
610 GLint pos = (t << info->twidth_log2) + s; \
611 const GLchan *tex00 = info->texture + COMP * pos; \
613 span->red += span->redStep; \
614 span->green += span->greenStep; \
615 span->blue += span->blueStep; \
616 span->alpha += span->alphaStep; \
617 tex_coord[0] += tex_step[0]; \
618 tex_coord[1] += tex_step[1]; \
619 tex_coord[2] += tex_step[2]; \
623 #define SPAN_LINEAR(DO_TEX,COMP) \
624 for (i = 0; i < span->end; i++) { \
625 GLdouble invQ = tex_coord[2] ? \
626 (1.0 / tex_coord[2]) : 1.0; \
627 const GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
628 const GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
629 const GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
630 const GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
631 const GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
632 const GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
633 const GLfixed sf = s_fix & FIXED_FRAC_MASK; \
634 const GLfixed tf = t_fix & FIXED_FRAC_MASK; \
635 const GLint pos = (t << info->twidth_log2) + s; \
636 const GLchan *tex00 = info->texture + COMP * pos; \
637 const GLchan *tex10 = tex00 + info->tbytesline; \
638 const GLchan *tex01 = tex00 + COMP; \
639 const GLchan *tex11 = tex10 + COMP; \
640 if (t == info->tmask) { \
641 tex10 -= info->tsize; \
642 tex11 -= info->tsize; \
644 if (s == info->smask) { \
645 tex01 -= info->tbytesline; \
646 tex11 -= info->tbytesline; \
649 span->red += span->redStep; \
650 span->green += span->greenStep; \
651 span->blue += span->blueStep; \
652 span->alpha += span->alphaStep; \
653 tex_coord[0] += tex_step[0]; \
654 tex_coord[1] += tex_step[1]; \
655 tex_coord[2] += tex_step[2]; \
660 GLfloat tex_coord
[3], tex_step
[3];
661 GLchan
*dest
= span
->array
->rgba
[0];
663 const GLuint savedTexEnable
= ctx
->Texture
._EnabledUnits
;
664 ctx
->Texture
._EnabledUnits
= 0;
666 tex_coord
[0] = span
->attrStart
[FRAG_ATTRIB_TEX0
][0] * (info
->smask
+ 1);
667 tex_step
[0] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][0] * (info
->smask
+ 1);
668 tex_coord
[1] = span
->attrStart
[FRAG_ATTRIB_TEX0
][1] * (info
->tmask
+ 1);
669 tex_step
[1] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][1] * (info
->tmask
+ 1);
670 /* span->attrStart[FRAG_ATTRIB_TEX0][2] only if 3D-texturing, here only 2D */
671 tex_coord
[2] = span
->attrStart
[FRAG_ATTRIB_TEX0
][3];
672 tex_step
[2] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][3];
674 switch (info
->filter
) {
676 switch (info
->format
) {
678 switch (info
->envmode
) {
680 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
684 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
687 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
690 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
693 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
698 switch(info
->envmode
) {
700 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
703 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
706 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
709 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
712 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
715 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
723 switch (info
->format
) {
725 switch (info
->envmode
) {
727 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
731 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
734 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
737 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
740 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
745 switch (info
->envmode
) {
747 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
750 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
753 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
756 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
759 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
762 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
770 ASSERT(span
->arrayMask
& SPAN_RGBA
);
771 _swrast_write_rgba_span(ctx
, span
);
777 ctx
->Texture
._EnabledUnits
= savedTexEnable
;
782 * Render an perspective corrected RGB/RGBA textured triangle.
783 * The Q (aka V in Mesa) coordinate must be zero such that the divide
784 * by interpolated Q/W comes out right.
787 #define NAME persp_textured_triangle
790 #define INTERP_ALPHA 1
791 #define INTERP_ATTRIBS 1
794 struct persp_info info; \
795 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
796 const struct gl_texture_object *obj = unit->Current2D; \
797 const GLint b = obj->BaseLevel; \
798 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
799 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
800 info.smask = obj->Image[0][b]->Width - 1; \
801 info.tmask = obj->Image[0][b]->Height - 1; \
802 info.format = obj->Image[0][b]->_BaseFormat; \
803 info.filter = obj->MinFilter; \
804 info.envmode = unit->EnvMode; \
806 if (info.envmode == GL_BLEND) { \
807 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
808 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
809 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
810 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
811 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
813 if (!info.texture) { \
814 /* this shouldn't happen */ \
818 switch (info.format) { \
822 info.tbytesline = obj->Image[0][b]->Width; \
824 case GL_LUMINANCE_ALPHA: \
825 info.tbytesline = obj->Image[0][b]->Width * 2; \
828 info.tbytesline = obj->Image[0][b]->Width * 3; \
831 info.tbytesline = obj->Image[0][b]->Width * 4; \
834 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
837 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
839 #define RENDER_SPAN( span ) \
840 span.interpMask &= ~SPAN_RGBA; \
841 span.arrayMask |= SPAN_RGBA; \
842 fast_persp_span(ctx, &span, &info);
844 #include "s_tritemp.h"
846 #endif /*CHAN_TYPE != GL_FLOAT*/
851 * Render an RGBA triangle with arbitrary attributes.
853 #define NAME general_triangle
856 #define INTERP_ALPHA 1
857 #define INTERP_ATTRIBS 1
858 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
859 #include "s_tritemp.h"
865 * Special tri function for occlusion testing
867 #define NAME occlusion_zless_triangle
870 struct gl_renderbuffer *rb = ctx->DrawBuffer->_DepthBuffer; \
871 struct gl_query_object *q = ctx->Query.CurrentOcclusionObject; \
872 ASSERT(ctx->Depth.Test); \
873 ASSERT(!ctx->Depth.Mask); \
874 ASSERT(ctx->Depth.Func == GL_LESS); \
878 #define RENDER_SPAN( span ) \
879 if (rb->DepthBits <= 16) { \
881 const GLushort *zRow = (const GLushort *) \
882 rb->GetPointer(ctx, rb, span.x, span.y); \
883 for (i = 0; i < span.end; i++) { \
884 GLuint z = FixedToDepth(span.z); \
888 span.z += span.zStep; \
893 const GLuint *zRow = (const GLuint *) \
894 rb->GetPointer(ctx, rb, span.x, span.y); \
895 for (i = 0; i < span.end; i++) { \
896 if ((GLuint)span.z < zRow[i]) { \
899 span.z += span.zStep; \
902 #include "s_tritemp.h"
907 nodraw_triangle( GLcontext
*ctx
,
912 (void) (ctx
&& v0
&& v1
&& v2
);
917 * This is used when separate specular color is enabled, but not
918 * texturing. We add the specular color to the primary color,
919 * draw the triangle, then restore the original primary color.
920 * Inefficient, but seldom needed.
923 _swrast_add_spec_terms_triangle(GLcontext
*ctx
, const SWvertex
*v0
,
924 const SWvertex
*v1
, const SWvertex
*v2
)
926 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
927 SWvertex
*ncv1
= (SWvertex
*)v1
;
928 SWvertex
*ncv2
= (SWvertex
*)v2
;
929 GLfloat rSum
, gSum
, bSum
;
932 /* save original colors */
933 COPY_CHAN4( cSave
[0], ncv0
->color
);
934 COPY_CHAN4( cSave
[1], ncv1
->color
);
935 COPY_CHAN4( cSave
[2], ncv2
->color
);
937 rSum
= CHAN_TO_FLOAT(ncv0
->color
[0]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][0];
938 gSum
= CHAN_TO_FLOAT(ncv0
->color
[1]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][1];
939 bSum
= CHAN_TO_FLOAT(ncv0
->color
[2]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][2];
940 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[0], rSum
);
941 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[1], gSum
);
942 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[2], bSum
);
944 rSum
= CHAN_TO_FLOAT(ncv1
->color
[0]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][0];
945 gSum
= CHAN_TO_FLOAT(ncv1
->color
[1]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][1];
946 bSum
= CHAN_TO_FLOAT(ncv1
->color
[2]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][2];
947 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[0], rSum
);
948 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[1], gSum
);
949 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[2], bSum
);
951 rSum
= CHAN_TO_FLOAT(ncv2
->color
[0]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][0];
952 gSum
= CHAN_TO_FLOAT(ncv2
->color
[1]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][1];
953 bSum
= CHAN_TO_FLOAT(ncv2
->color
[2]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][2];
954 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[0], rSum
);
955 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[1], gSum
);
956 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[2], bSum
);
958 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
959 /* restore original colors */
960 COPY_CHAN4( ncv0
->color
, cSave
[0] );
961 COPY_CHAN4( ncv1
->color
, cSave
[1] );
962 COPY_CHAN4( ncv2
->color
, cSave
[2] );
969 /* record the current triangle function name */
970 const char *_mesa_triFuncName
= NULL
;
972 #define USE(triFunc) \
974 _mesa_triFuncName = #triFunc; \
975 /*printf("%s\n", _mesa_triFuncName);*/ \
976 swrast->Triangle = triFunc; \
981 #define USE(triFunc) swrast->Triangle = triFunc;
989 * Determine which triangle rendering function to use given the current
992 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
993 * remove tests to this code.
996 _swrast_choose_triangle( GLcontext
*ctx
)
998 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
999 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
1001 if (ctx
->Polygon
.CullFlag
&&
1002 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1003 USE(nodraw_triangle
);
1007 if (ctx
->RenderMode
==GL_RENDER
) {
1009 if (ctx
->Polygon
.SmoothFlag
) {
1010 _swrast_set_aa_triangle_function(ctx
);
1011 ASSERT(swrast
->Triangle
);
1015 /* special case for occlusion testing */
1016 if (ctx
->Query
.CurrentOcclusionObject
&&
1018 ctx
->Depth
.Mask
== GL_FALSE
&&
1019 ctx
->Depth
.Func
== GL_LESS
&&
1020 !ctx
->Stencil
.Enabled
) {
1022 ctx
->Color
.ColorMask
[0] == 0 &&
1023 ctx
->Color
.ColorMask
[1] == 0 &&
1024 ctx
->Color
.ColorMask
[2] == 0 &&
1025 ctx
->Color
.ColorMask
[3] == 0)
1027 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1028 USE(occlusion_zless_triangle
);
1039 * XXX should examine swrast->_ActiveAttribMask to determine what
1040 * needs to be interpolated.
1042 if (ctx
->Texture
._EnabledCoordUnits
||
1043 ctx
->FragmentProgram
._Current
||
1044 ctx
->ATIFragmentShader
._Enabled
||
1045 NEED_SECONDARY_COLOR(ctx
) ||
1046 swrast
->_FogEnabled
) {
1047 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1048 const struct gl_texture_object
*texObj2D
;
1049 const struct gl_texture_image
*texImg
;
1050 GLenum minFilter
, magFilter
, envMode
;
1052 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1053 texImg
= texObj2D
? texObj2D
->Image
[0][texObj2D
->BaseLevel
] : NULL
;
1054 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1055 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1056 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1057 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1059 /* First see if we can use an optimized 2-D texture function */
1060 if (ctx
->Texture
._EnabledCoordUnits
== 0x1
1061 && !ctx
->FragmentProgram
._Current
1062 && !ctx
->ATIFragmentShader
._Enabled
1063 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1064 && texObj2D
->WrapS
== GL_REPEAT
1065 && texObj2D
->WrapT
== GL_REPEAT
1066 && texImg
->_IsPowerOfTwo
1067 && texImg
->Border
== 0
1068 && texImg
->Width
== texImg
->RowStride
1069 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1070 && minFilter
== magFilter
1071 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1072 && !swrast
->_FogEnabled
1073 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1074 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1075 if (minFilter
== GL_NEAREST
1076 && format
== MESA_FORMAT_RGB
1077 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1078 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1079 && ctx
->Depth
.Func
== GL_LESS
1080 && ctx
->Depth
.Mask
== GL_TRUE
)
1081 || swrast
->_RasterMask
== TEXTURE_BIT
)
1082 && ctx
->Polygon
.StippleFlag
== GL_FALSE
1083 && ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
1084 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1085 USE(simple_z_textured_triangle
);
1088 USE(simple_textured_triangle
);
1093 USE(general_triangle
);
1095 USE(affine_textured_triangle
);
1101 USE(general_triangle
);
1103 USE(persp_textured_triangle
);
1108 /* general case textured triangles */
1109 USE(general_triangle
);
1113 ASSERT(!swrast
->_FogEnabled
);
1114 ASSERT(!NEED_SECONDARY_COLOR(ctx
));
1115 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1116 /* smooth shaded, no texturing, stippled or some raster ops */
1118 USE(general_triangle
);
1120 USE(smooth_rgba_triangle
);
1124 /* flat shaded, no texturing, stippled or some raster ops */
1126 USE(general_triangle
);
1128 USE(flat_rgba_triangle
);
1133 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1134 USE(_swrast_feedback_triangle
);
1137 /* GL_SELECT mode */
1138 USE(_swrast_select_triangle
);