2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * When the device driver doesn't implement triangle rasterization it
28 * can hook in _swrast_Triangle, which eventually calls one of these
29 * functions to draw triangles.
32 #include "main/glheader.h"
33 #include "main/context.h"
34 #include "main/colormac.h"
35 #include "main/imports.h"
36 #include "main/macros.h"
37 #include "main/texformat.h"
38 #include "shader/prog_instruction.h"
40 #include "s_aatriangle.h"
41 #include "s_context.h"
42 #include "s_feedback.h"
44 #include "s_triangle.h"
48 * 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( GLcontext
*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 smooth or flat-shaded color index triangle.
75 #define NAME ci_triangle
77 #define INTERP_ATTRIBS 1 /* just for fog */
78 #define INTERP_INDEX 1
79 #define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
80 #include "s_tritemp.h"
85 * Render a flat-shaded RGBA triangle.
87 #define NAME flat_rgba_triangle
90 ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
91 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
92 span.interpMask |= SPAN_RGBA; \
93 span.red = ChanToFixed(v2->color[0]); \
94 span.green = ChanToFixed(v2->color[1]); \
95 span.blue = ChanToFixed(v2->color[2]); \
96 span.alpha = ChanToFixed(v2->color[3]); \
101 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
102 #include "s_tritemp.h"
107 * Render a smooth-shaded RGBA triangle.
109 #define NAME smooth_rgba_triangle
112 #define INTERP_ALPHA 1
115 /* texturing must be off */ \
116 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \
117 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
119 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
120 #include "s_tritemp.h"
125 * Render an RGB, GL_DECAL, textured triangle.
126 * Interpolate S,T only w/out mipmapping or perspective correction.
128 * No fog. No depth testing.
130 #define NAME simple_textured_triangle
131 #define INTERP_INT_TEX 1
132 #define S_SCALE twidth
133 #define T_SCALE theight
136 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
137 struct gl_texture_object *obj = \
138 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
139 const GLint b = obj->BaseLevel; \
140 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
141 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
142 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
143 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
144 const GLint smask = obj->Image[0][b]->Width - 1; \
145 const GLint tmask = obj->Image[0][b]->Height - 1; \
146 if (!rb || !texture) { \
150 #define RENDER_SPAN( span ) \
152 GLchan rgb[MAX_WIDTH][3]; \
153 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
154 span.intTex[1] -= FIXED_HALF; \
155 for (i = 0; i < span.end; i++) { \
156 GLint s = FixedToInt(span.intTex[0]) & smask; \
157 GLint t = FixedToInt(span.intTex[1]) & tmask; \
158 GLint pos = (t << twidth_log2) + s; \
159 pos = pos + pos + pos; /* multiply by 3 */ \
160 rgb[i][RCOMP] = texture[pos]; \
161 rgb[i][GCOMP] = texture[pos+1]; \
162 rgb[i][BCOMP] = texture[pos+2]; \
163 span.intTex[0] += span.intTexStep[0]; \
164 span.intTex[1] += span.intTexStep[1]; \
166 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, NULL);
168 #include "s_tritemp.h"
173 * Render an RGB, GL_DECAL, textured triangle.
174 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
175 * perspective correction.
176 * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
180 #define NAME simple_z_textured_triangle
182 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
183 #define INTERP_INT_TEX 1
184 #define S_SCALE twidth
185 #define T_SCALE theight
188 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
189 struct gl_texture_object *obj = \
190 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
191 const GLint b = obj->BaseLevel; \
192 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
193 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
194 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
195 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
196 const GLint smask = obj->Image[0][b]->Width - 1; \
197 const GLint tmask = obj->Image[0][b]->Height - 1; \
198 if (!rb || !texture) { \
202 #define RENDER_SPAN( span ) \
204 GLchan rgb[MAX_WIDTH][3]; \
205 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
206 span.intTex[1] -= FIXED_HALF; \
207 for (i = 0; i < span.end; i++) { \
208 const GLuint z = FixedToDepth(span.z); \
210 GLint s = FixedToInt(span.intTex[0]) & smask; \
211 GLint t = FixedToInt(span.intTex[1]) & tmask; \
212 GLint pos = (t << twidth_log2) + s; \
213 pos = pos + pos + pos; /* multiply by 3 */ \
214 rgb[i][RCOMP] = texture[pos]; \
215 rgb[i][GCOMP] = texture[pos+1]; \
216 rgb[i][BCOMP] = texture[pos+2]; \
218 span.array->mask[i] = 1; \
221 span.array->mask[i] = 0; \
223 span.intTex[0] += span.intTexStep[0]; \
224 span.intTex[1] += span.intTexStep[1]; \
225 span.z += span.zStep; \
227 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, rgb, span.array->mask);
229 #include "s_tritemp.h"
232 #if CHAN_TYPE != GL_FLOAT
241 const GLchan
*texture
;
242 GLfixed er
, eg
, eb
, ea
;
243 GLint tbytesline
, tsize
;
248 ilerp(GLint t
, GLint a
, GLint b
)
250 return a
+ ((t
* (b
- a
)) >> FIXED_SHIFT
);
254 ilerp_2d(GLint ia
, GLint ib
, GLint v00
, GLint v10
, GLint v01
, GLint v11
)
256 const GLint temp0
= ilerp(ia
, v00
, v10
);
257 const GLint temp1
= ilerp(ia
, v01
, v11
);
258 return ilerp(ib
, temp0
, temp1
);
262 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
263 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
267 affine_span(GLcontext
*ctx
, SWspan
*span
,
268 struct affine_info
*info
)
270 GLchan sample
[4]; /* the filtered texture sample */
271 const GLuint texEnableSave
= ctx
->Texture
._EnabledCoordUnits
;
273 /* Instead of defining a function for each mode, a test is done
274 * between the outer and inner loops. This is to reduce code size
275 * and complexity. Observe that an optimizing compiler kills
276 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
279 #define NEAREST_RGB \
280 sample[RCOMP] = tex00[RCOMP]; \
281 sample[GCOMP] = tex00[GCOMP]; \
282 sample[BCOMP] = tex00[BCOMP]; \
283 sample[ACOMP] = CHAN_MAX
286 sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
287 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
288 sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
289 sample[ACOMP] = CHAN_MAX;
291 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
293 #define LINEAR_RGBA \
294 sample[RCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
295 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
296 sample[BCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
297 sample[ACOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3])
300 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
301 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
302 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
303 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
306 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
307 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
308 >> (FIXED_SHIFT + 8); \
309 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
310 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
311 >> (FIXED_SHIFT + 8); \
312 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
313 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
314 >> (FIXED_SHIFT + 8); \
315 dest[ACOMP] = FixedToInt(span->alpha)
318 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
319 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
320 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
321 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
322 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
323 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
324 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
326 #define REPLACE COPY_CHAN4(dest, sample)
330 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
331 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
332 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
333 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
334 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
335 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
336 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
341 #define NEAREST_RGB_REPLACE \
343 dest[0] = sample[0]; \
344 dest[1] = sample[1]; \
345 dest[2] = sample[2]; \
346 dest[3] = FixedToInt(span->alpha);
348 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
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
) {
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");
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
) {
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");
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 struct gl_texture_object *obj = \
532 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
533 const GLint b = obj->BaseLevel; \
534 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
535 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
536 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
537 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
538 info.smask = obj->Image[0][b]->Width - 1; \
539 info.tmask = obj->Image[0][b]->Height - 1; \
540 info.format = obj->Image[0][b]->_BaseFormat; \
541 info.filter = obj->MinFilter; \
542 info.envmode = unit->EnvMode; \
543 span.arrayMask |= SPAN_RGBA; \
545 if (info.envmode == GL_BLEND) { \
546 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
547 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
548 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
549 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
550 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
552 if (!info.texture) { \
553 /* this shouldn't happen */ \
557 switch (info.format) { \
561 info.tbytesline = obj->Image[0][b]->Width; \
563 case GL_LUMINANCE_ALPHA: \
564 info.tbytesline = obj->Image[0][b]->Width * 2; \
567 info.tbytesline = obj->Image[0][b]->Width * 3; \
570 info.tbytesline = obj->Image[0][b]->Width * 4; \
573 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
576 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
578 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
580 #include "s_tritemp.h"
591 const GLchan
*texture
;
592 GLfixed er
, eg
, eb
, ea
; /* texture env color */
593 GLint tbytesline
, tsize
;
598 fast_persp_span(GLcontext
*ctx
, SWspan
*span
,
599 struct persp_info
*info
)
601 GLchan sample
[4]; /* the filtered texture sample */
603 /* Instead of defining a function for each mode, a test is done
604 * between the outer and inner loops. This is to reduce code size
605 * and complexity. Observe that an optimizing compiler kills
606 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
608 #define SPAN_NEAREST(DO_TEX,COMP) \
609 for (i = 0; i < span->end; i++) { \
610 GLdouble invQ = tex_coord[2] ? \
611 (1.0 / tex_coord[2]) : 1.0; \
612 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
613 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
614 GLint s = IFLOOR(s_tmp) & info->smask; \
615 GLint t = IFLOOR(t_tmp) & info->tmask; \
616 GLint pos = (t << info->twidth_log2) + s; \
617 const GLchan *tex00 = info->texture + COMP * pos; \
619 span->red += span->redStep; \
620 span->green += span->greenStep; \
621 span->blue += span->blueStep; \
622 span->alpha += span->alphaStep; \
623 tex_coord[0] += tex_step[0]; \
624 tex_coord[1] += tex_step[1]; \
625 tex_coord[2] += tex_step[2]; \
629 #define SPAN_LINEAR(DO_TEX,COMP) \
630 for (i = 0; i < span->end; i++) { \
631 GLdouble invQ = tex_coord[2] ? \
632 (1.0 / tex_coord[2]) : 1.0; \
633 const GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
634 const GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
635 const GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
636 const GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
637 const GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
638 const GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
639 const GLfixed sf = s_fix & FIXED_FRAC_MASK; \
640 const GLfixed tf = t_fix & FIXED_FRAC_MASK; \
641 const GLint pos = (t << info->twidth_log2) + s; \
642 const GLchan *tex00 = info->texture + COMP * pos; \
643 const GLchan *tex10 = tex00 + info->tbytesline; \
644 const GLchan *tex01 = tex00 + COMP; \
645 const GLchan *tex11 = tex10 + COMP; \
646 if (t == info->tmask) { \
647 tex10 -= info->tsize; \
648 tex11 -= info->tsize; \
650 if (s == info->smask) { \
651 tex01 -= info->tbytesline; \
652 tex11 -= info->tbytesline; \
655 span->red += span->redStep; \
656 span->green += span->greenStep; \
657 span->blue += span->blueStep; \
658 span->alpha += span->alphaStep; \
659 tex_coord[0] += tex_step[0]; \
660 tex_coord[1] += tex_step[1]; \
661 tex_coord[2] += tex_step[2]; \
666 GLfloat tex_coord
[3], tex_step
[3];
667 GLchan
*dest
= span
->array
->rgba
[0];
669 const GLuint texEnableSave
= ctx
->Texture
._EnabledCoordUnits
;
670 ctx
->Texture
._EnabledCoordUnits
= 0;
672 tex_coord
[0] = span
->attrStart
[FRAG_ATTRIB_TEX0
][0] * (info
->smask
+ 1);
673 tex_step
[0] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][0] * (info
->smask
+ 1);
674 tex_coord
[1] = span
->attrStart
[FRAG_ATTRIB_TEX0
][1] * (info
->tmask
+ 1);
675 tex_step
[1] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][1] * (info
->tmask
+ 1);
676 /* span->attrStart[FRAG_ATTRIB_TEX0][2] only if 3D-texturing, here only 2D */
677 tex_coord
[2] = span
->attrStart
[FRAG_ATTRIB_TEX0
][3];
678 tex_step
[2] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][3];
680 switch (info
->filter
) {
682 switch (info
->format
) {
684 switch (info
->envmode
) {
686 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
690 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
693 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
696 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
699 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
704 switch(info
->envmode
) {
706 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
709 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
712 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
715 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
718 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
721 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
729 switch (info
->format
) {
731 switch (info
->envmode
) {
733 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
737 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
740 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
743 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
746 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
751 switch (info
->envmode
) {
753 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
756 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
759 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
762 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
765 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
768 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
776 ASSERT(span
->arrayMask
& SPAN_RGBA
);
777 _swrast_write_rgba_span(ctx
, span
);
783 ctx
->Texture
._EnabledCoordUnits
= texEnableSave
;
788 * Render an perspective corrected RGB/RGBA textured triangle.
789 * The Q (aka V in Mesa) coordinate must be zero such that the divide
790 * by interpolated Q/W comes out right.
793 #define NAME persp_textured_triangle
796 #define INTERP_ALPHA 1
797 #define INTERP_ATTRIBS 1
800 struct persp_info info; \
801 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
802 struct gl_texture_object *obj = \
803 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
804 const GLint b = obj->BaseLevel; \
805 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
806 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
807 info.smask = obj->Image[0][b]->Width - 1; \
808 info.tmask = obj->Image[0][b]->Height - 1; \
809 info.format = obj->Image[0][b]->_BaseFormat; \
810 info.filter = obj->MinFilter; \
811 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) { \
829 info.tbytesline = obj->Image[0][b]->Width; \
831 case GL_LUMINANCE_ALPHA: \
832 info.tbytesline = obj->Image[0][b]->Width * 2; \
835 info.tbytesline = obj->Image[0][b]->Width * 3; \
838 info.tbytesline = obj->Image[0][b]->Width * 4; \
841 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
844 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
846 #define RENDER_SPAN( span ) \
847 span.interpMask &= ~SPAN_RGBA; \
848 span.arrayMask |= SPAN_RGBA; \
849 fast_persp_span(ctx, &span, &info);
851 #include "s_tritemp.h"
853 #endif /*CHAN_TYPE != GL_FLOAT*/
858 * Render an RGBA triangle with arbitrary attributes.
860 #define NAME general_triangle
863 #define INTERP_ALPHA 1
864 #define INTERP_ATTRIBS 1
865 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
866 #include "s_tritemp.h"
872 * Special tri function for occlusion testing
874 #define NAME occlusion_zless_triangle
877 struct gl_renderbuffer *rb = ctx->DrawBuffer->_DepthBuffer; \
878 struct gl_query_object *q = ctx->Query.CurrentOcclusionObject; \
879 ASSERT(ctx->Depth.Test); \
880 ASSERT(!ctx->Depth.Mask); \
881 ASSERT(ctx->Depth.Func == GL_LESS); \
885 #define RENDER_SPAN( span ) \
886 if (rb->DepthBits <= 16) { \
888 const GLushort *zRow = (const GLushort *) \
889 rb->GetPointer(ctx, rb, span.x, span.y); \
890 for (i = 0; i < span.end; i++) { \
891 GLuint z = FixedToDepth(span.z); \
895 span.z += span.zStep; \
900 const GLuint *zRow = (const GLuint *) \
901 rb->GetPointer(ctx, rb, span.x, span.y); \
902 for (i = 0; i < span.end; i++) { \
903 if ((GLuint)span.z < zRow[i]) { \
906 span.z += span.zStep; \
909 #include "s_tritemp.h"
914 nodraw_triangle( GLcontext
*ctx
,
919 (void) (ctx
&& v0
&& v1
&& v2
);
924 * This is used when separate specular color is enabled, but not
925 * texturing. We add the specular color to the primary color,
926 * draw the triangle, then restore the original primary color.
927 * Inefficient, but seldom needed.
930 _swrast_add_spec_terms_triangle(GLcontext
*ctx
, const SWvertex
*v0
,
931 const SWvertex
*v1
, const SWvertex
*v2
)
933 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
934 SWvertex
*ncv1
= (SWvertex
*)v1
;
935 SWvertex
*ncv2
= (SWvertex
*)v2
;
936 GLfloat rSum
, gSum
, bSum
;
939 /* save original colors */
940 COPY_CHAN4( cSave
[0], ncv0
->color
);
941 COPY_CHAN4( cSave
[1], ncv1
->color
);
942 COPY_CHAN4( cSave
[2], ncv2
->color
);
944 rSum
= CHAN_TO_FLOAT(ncv0
->color
[0]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][0];
945 gSum
= CHAN_TO_FLOAT(ncv0
->color
[1]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][1];
946 bSum
= CHAN_TO_FLOAT(ncv0
->color
[2]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][2];
947 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[0], rSum
);
948 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[1], gSum
);
949 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[2], bSum
);
951 rSum
= CHAN_TO_FLOAT(ncv1
->color
[0]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][0];
952 gSum
= CHAN_TO_FLOAT(ncv1
->color
[1]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][1];
953 bSum
= CHAN_TO_FLOAT(ncv1
->color
[2]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][2];
954 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[0], rSum
);
955 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[1], gSum
);
956 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[2], bSum
);
958 rSum
= CHAN_TO_FLOAT(ncv2
->color
[0]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][0];
959 gSum
= CHAN_TO_FLOAT(ncv2
->color
[1]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][1];
960 bSum
= CHAN_TO_FLOAT(ncv2
->color
[2]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][2];
961 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[0], rSum
);
962 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[1], gSum
);
963 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[2], bSum
);
965 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
966 /* restore original colors */
967 COPY_CHAN4( ncv0
->color
, cSave
[0] );
968 COPY_CHAN4( ncv1
->color
, cSave
[1] );
969 COPY_CHAN4( ncv2
->color
, cSave
[2] );
976 /* record the current triangle function name */
977 const char *_mesa_triFuncName
= NULL
;
979 #define USE(triFunc) \
981 _mesa_triFuncName = #triFunc; \
982 /*printf("%s\n", _mesa_triFuncName);*/ \
983 swrast->Triangle = triFunc; \
988 #define USE(triFunc) swrast->Triangle = triFunc;
996 * Determine which triangle rendering function to use given the current
999 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1000 * remove tests to this code.
1003 _swrast_choose_triangle( GLcontext
*ctx
)
1005 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1006 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
1008 if (ctx
->Polygon
.CullFlag
&&
1009 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1010 USE(nodraw_triangle
);
1014 if (ctx
->RenderMode
==GL_RENDER
) {
1016 if (ctx
->Polygon
.SmoothFlag
) {
1017 _swrast_set_aa_triangle_function(ctx
);
1018 ASSERT(swrast
->Triangle
);
1022 /* special case for occlusion testing */
1023 if (ctx
->Query
.CurrentOcclusionObject
&&
1025 ctx
->Depth
.Mask
== GL_FALSE
&&
1026 ctx
->Depth
.Func
== GL_LESS
&&
1027 !ctx
->Stencil
._Enabled
) {
1029 ctx
->Color
.ColorMask
[0] == 0 &&
1030 ctx
->Color
.ColorMask
[1] == 0 &&
1031 ctx
->Color
.ColorMask
[2] == 0 &&
1032 ctx
->Color
.ColorMask
[3] == 0)
1034 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1035 USE(occlusion_zless_triangle
);
1046 * XXX should examine swrast->_ActiveAttribMask to determine what
1047 * needs to be interpolated.
1049 if (ctx
->Texture
._EnabledCoordUnits
||
1050 ctx
->FragmentProgram
._Current
||
1051 ctx
->ATIFragmentShader
._Enabled
||
1052 NEED_SECONDARY_COLOR(ctx
) ||
1053 swrast
->_FogEnabled
) {
1054 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1055 const struct gl_texture_object
*texObj2D
;
1056 const struct gl_texture_image
*texImg
;
1057 GLenum minFilter
, magFilter
, envMode
;
1059 texObj2D
= ctx
->Texture
.Unit
[0].CurrentTex
[TEXTURE_2D_INDEX
];
1061 texImg
= texObj2D
? texObj2D
->Image
[0][texObj2D
->BaseLevel
] : NULL
;
1062 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1063 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1064 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1065 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1067 /* First see if we can use an optimized 2-D texture function */
1068 if (ctx
->Texture
._EnabledCoordUnits
== 0x1
1069 && !ctx
->FragmentProgram
._Current
1070 && !ctx
->ATIFragmentShader
._Enabled
1071 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1072 && texObj2D
->WrapS
== GL_REPEAT
1073 && texObj2D
->WrapT
== GL_REPEAT
1074 && texObj2D
->_Swizzle
== SWIZZLE_NOOP
1075 && texImg
->_IsPowerOfTwo
1076 && texImg
->Border
== 0
1077 && texImg
->Width
== texImg
->RowStride
1078 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1079 && minFilter
== magFilter
1080 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1081 && !swrast
->_FogEnabled
1082 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
1083 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE4_NV
) {
1084 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1085 if (minFilter
== GL_NEAREST
1086 && format
== MESA_FORMAT_RGB
1087 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1088 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1089 && ctx
->Depth
.Func
== GL_LESS
1090 && ctx
->Depth
.Mask
== GL_TRUE
)
1091 || swrast
->_RasterMask
== TEXTURE_BIT
)
1092 && ctx
->Polygon
.StippleFlag
== GL_FALSE
1093 && ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
1094 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1095 USE(simple_z_textured_triangle
);
1098 USE(simple_textured_triangle
);
1103 USE(general_triangle
);
1105 USE(affine_textured_triangle
);
1111 USE(general_triangle
);
1113 USE(persp_textured_triangle
);
1118 /* general case textured triangles */
1119 USE(general_triangle
);
1123 ASSERT(!swrast
->_FogEnabled
);
1124 ASSERT(!NEED_SECONDARY_COLOR(ctx
));
1125 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1126 /* smooth shaded, no texturing, stippled or some raster ops */
1128 USE(general_triangle
);
1130 USE(smooth_rgba_triangle
);
1134 /* flat shaded, no texturing, stippled or some raster ops */
1136 USE(general_triangle
);
1138 USE(flat_rgba_triangle
);
1143 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1144 USE(_swrast_feedback_triangle
);
1147 /* GL_SELECT mode */
1148 USE(_swrast_select_triangle
);