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/mtypes.h"
38 #include "main/state.h"
39 #include "program/prog_instruction.h"
41 #include "s_aatriangle.h"
42 #include "s_context.h"
43 #include "s_feedback.h"
44 #include "s_fragprog.h"
46 #include "s_triangle.h"
50 * Test if a triangle should be culled. Used for feedback and selection mode.
51 * \return GL_TRUE if the triangle is to be culled, GL_FALSE otherwise.
54 _swrast_culltriangle( struct gl_context
*ctx
,
59 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
60 GLfloat ex
= v1
->attrib
[FRAG_ATTRIB_WPOS
][0] - v0
->attrib
[FRAG_ATTRIB_WPOS
][0];
61 GLfloat ey
= v1
->attrib
[FRAG_ATTRIB_WPOS
][1] - v0
->attrib
[FRAG_ATTRIB_WPOS
][1];
62 GLfloat fx
= v2
->attrib
[FRAG_ATTRIB_WPOS
][0] - v0
->attrib
[FRAG_ATTRIB_WPOS
][0];
63 GLfloat fy
= v2
->attrib
[FRAG_ATTRIB_WPOS
][1] - v0
->attrib
[FRAG_ATTRIB_WPOS
][1];
64 GLfloat c
= ex
*fy
-ey
*fx
;
66 if (c
* swrast
->_BackfaceSign
* swrast
->_BackfaceCullSign
<= 0.0F
)
75 * Render a flat-shaded RGBA triangle.
77 #define NAME flat_rgba_triangle
80 ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
81 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
82 span.interpMask |= SPAN_RGBA; \
83 span.red = ChanToFixed(v2->color[0]); \
84 span.green = ChanToFixed(v2->color[1]); \
85 span.blue = ChanToFixed(v2->color[2]); \
86 span.alpha = ChanToFixed(v2->color[3]); \
91 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
92 #include "s_tritemp.h"
97 * Render a smooth-shaded RGBA triangle.
99 #define NAME smooth_rgba_triangle
102 #define INTERP_ALPHA 1
105 /* texturing must be off */ \
106 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \
107 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
109 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
110 #include "s_tritemp.h"
115 * Render an RGB, GL_DECAL, textured triangle.
116 * Interpolate S,T only w/out mipmapping or perspective correction.
118 * No fog. No depth testing.
120 #define NAME simple_textured_triangle
121 #define INTERP_INT_TEX 1
122 #define S_SCALE twidth
123 #define T_SCALE theight
126 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
127 const struct gl_texture_object *obj = \
128 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
129 const struct gl_texture_image *texImg = \
130 obj->Image[0][obj->BaseLevel]; \
131 const struct swrast_texture_image *swImg = \
132 swrast_texture_image_const(texImg); \
133 const GLfloat twidth = (GLfloat) texImg->Width; \
134 const GLfloat theight = (GLfloat) texImg->Height; \
135 const GLint twidth_log2 = texImg->WidthLog2; \
136 const GLubyte *texture = (const GLubyte *) swImg->Map; \
137 const GLint smask = texImg->Width - 1; \
138 const GLint tmask = texImg->Height - 1; \
139 ASSERT(texImg->TexFormat == MESA_FORMAT_RGB888); \
140 if (!rb || !texture) { \
144 #define RENDER_SPAN( span ) \
146 GLubyte rgba[MAX_WIDTH][4]; \
147 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
148 span.intTex[1] -= FIXED_HALF; \
149 for (i = 0; i < span.end; i++) { \
150 GLint s = FixedToInt(span.intTex[0]) & smask; \
151 GLint t = FixedToInt(span.intTex[1]) & tmask; \
152 GLint pos = (t << twidth_log2) + s; \
153 pos = pos + pos + pos; /* multiply by 3 */ \
154 rgba[i][RCOMP] = texture[pos+2]; \
155 rgba[i][GCOMP] = texture[pos+1]; \
156 rgba[i][BCOMP] = texture[pos+0]; \
157 rgba[i][ACOMP] = 0xff; \
158 span.intTex[0] += span.intTexStep[0]; \
159 span.intTex[1] += span.intTexStep[1]; \
161 _swrast_put_row(ctx, rb, GL_UNSIGNED_BYTE, span.end, \
162 span.x, span.y, rgba, 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 const struct gl_texture_object *obj = \
186 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
187 const struct gl_texture_image *texImg = \
188 obj->Image[0][obj->BaseLevel]; \
189 const struct swrast_texture_image *swImg = \
190 swrast_texture_image_const(texImg); \
191 const GLfloat twidth = (GLfloat) texImg->Width; \
192 const GLfloat theight = (GLfloat) texImg->Height; \
193 const GLint twidth_log2 = texImg->WidthLog2; \
194 const GLubyte *texture = (const GLubyte *) swImg->Map; \
195 const GLint smask = texImg->Width - 1; \
196 const GLint tmask = texImg->Height - 1; \
197 ASSERT(texImg->TexFormat == MESA_FORMAT_RGB888); \
198 if (!rb || !texture) { \
202 #define RENDER_SPAN( span ) \
204 GLubyte rgba[MAX_WIDTH][4]; \
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 rgba[i][RCOMP] = texture[pos+2]; \
215 rgba[i][GCOMP] = texture[pos+1]; \
216 rgba[i][BCOMP] = texture[pos+0]; \
217 rgba[i][ACOMP] = 0xff; \
219 span.array->mask[i] = 1; \
222 span.array->mask[i] = 0; \
224 span.intTex[0] += span.intTexStep[0]; \
225 span.intTex[1] += span.intTexStep[1]; \
226 span.z += span.zStep; \
228 _swrast_put_row(ctx, rb, GL_UNSIGNED_BYTE, \
229 span.end, span.x, span.y, rgba, span.array->mask);
231 #include "s_tritemp.h"
234 #if CHAN_TYPE != GL_FLOAT
243 const GLchan
*texture
;
244 GLfixed er
, eg
, eb
, ea
;
245 GLint tbytesline
, tsize
;
250 ilerp(GLint t
, GLint a
, GLint b
)
252 return a
+ ((t
* (b
- a
)) >> FIXED_SHIFT
);
256 ilerp_2d(GLint ia
, GLint ib
, GLint v00
, GLint v10
, GLint v01
, GLint v11
)
258 const GLint temp0
= ilerp(ia
, v00
, v10
);
259 const GLint temp1
= ilerp(ia
, v01
, v11
);
260 return ilerp(ib
, temp0
, temp1
);
264 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
265 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
269 affine_span(struct gl_context
*ctx
, SWspan
*span
,
270 struct affine_info
*info
)
272 GLchan sample
[4]; /* the filtered texture sample */
273 const GLuint texEnableSave
= ctx
->Texture
._EnabledCoordUnits
;
275 /* Instead of defining a function for each mode, a test is done
276 * between the outer and inner loops. This is to reduce code size
277 * and complexity. Observe that an optimizing compiler kills
278 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
281 #define NEAREST_RGB \
282 sample[RCOMP] = tex00[2]; \
283 sample[GCOMP] = tex00[1]; \
284 sample[BCOMP] = tex00[0]; \
285 sample[ACOMP] = CHAN_MAX;
288 sample[RCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
289 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
290 sample[BCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
291 sample[ACOMP] = CHAN_MAX;
293 #define NEAREST_RGBA \
294 sample[RCOMP] = tex00[3]; \
295 sample[GCOMP] = tex00[2]; \
296 sample[BCOMP] = tex00[1]; \
297 sample[ACOMP] = tex00[0];
299 #define LINEAR_RGBA \
300 sample[RCOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3]);\
301 sample[GCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
302 sample[BCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
303 sample[ACOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0])
306 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
307 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
308 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
309 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
312 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
313 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
314 >> (FIXED_SHIFT + 8); \
315 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
316 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
317 >> (FIXED_SHIFT + 8); \
318 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
319 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
320 >> (FIXED_SHIFT + 8); \
321 dest[ACOMP] = FixedToInt(span->alpha)
324 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
325 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
326 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
327 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
328 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
329 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
330 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
332 #define REPLACE COPY_CHAN4(dest, sample)
336 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
337 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
338 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
339 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
340 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
341 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
342 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
347 #define NEAREST_RGB_REPLACE \
349 dest[0] = sample[0]; \
350 dest[1] = sample[1]; \
351 dest[2] = sample[2]; \
352 dest[3] = FixedToInt(span->alpha);
354 #define NEAREST_RGBA_REPLACE \
355 dest[RCOMP] = tex00[3]; \
356 dest[GCOMP] = tex00[2]; \
357 dest[BCOMP] = tex00[1]; \
358 dest[ACOMP] = tex00[0]
360 #define SPAN_NEAREST(DO_TEX, COMPS) \
361 for (i = 0; i < span->end; i++) { \
362 /* Isn't it necessary to use FixedFloor below?? */ \
363 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
364 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
365 GLint pos = (t << info->twidth_log2) + s; \
366 const GLchan *tex00 = info->texture + COMPS * pos; \
368 span->red += span->redStep; \
369 span->green += span->greenStep; \
370 span->blue += span->blueStep; \
371 span->alpha += span->alphaStep; \
372 span->intTex[0] += span->intTexStep[0]; \
373 span->intTex[1] += span->intTexStep[1]; \
377 #define SPAN_LINEAR(DO_TEX, COMPS) \
378 for (i = 0; i < span->end; i++) { \
379 /* Isn't it necessary to use FixedFloor below?? */ \
380 const GLint s = FixedToInt(span->intTex[0]) & info->smask; \
381 const GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
382 const GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
383 const GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
384 const GLint pos = (t << info->twidth_log2) + s; \
385 const GLchan *tex00 = info->texture + COMPS * pos; \
386 const GLchan *tex10 = tex00 + info->tbytesline; \
387 const GLchan *tex01 = tex00 + COMPS; \
388 const GLchan *tex11 = tex10 + COMPS; \
389 if (t == info->tmask) { \
390 tex10 -= info->tsize; \
391 tex11 -= info->tsize; \
393 if (s == info->smask) { \
394 tex01 -= info->tbytesline; \
395 tex11 -= info->tbytesline; \
398 span->red += span->redStep; \
399 span->green += span->greenStep; \
400 span->blue += span->blueStep; \
401 span->alpha += span->alphaStep; \
402 span->intTex[0] += span->intTexStep[0]; \
403 span->intTex[1] += span->intTexStep[1]; \
409 GLchan
*dest
= span
->array
->rgba
[0];
411 /* Disable tex units so they're not re-applied in swrast_write_rgba_span */
412 ctx
->Texture
._EnabledCoordUnits
= 0x0;
414 span
->intTex
[0] -= FIXED_HALF
;
415 span
->intTex
[1] -= FIXED_HALF
;
416 switch (info
->filter
) {
418 switch (info
->format
) {
419 case MESA_FORMAT_RGB888
:
420 switch (info
->envmode
) {
422 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
426 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
429 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
432 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
435 _mesa_problem(ctx
, "bad tex env mode in SPAN_LINEAR");
439 case MESA_FORMAT_RGBA8888
:
440 switch(info
->envmode
) {
442 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
445 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
448 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
451 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
454 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
457 _mesa_problem(ctx
, "bad tex env mode (2) in SPAN_LINEAR");
465 span
->intTex
[0] -= FIXED_HALF
;
466 span
->intTex
[1] -= FIXED_HALF
;
467 switch (info
->format
) {
468 case MESA_FORMAT_RGB888
:
469 switch (info
->envmode
) {
471 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
475 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
478 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
481 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
484 _mesa_problem(ctx
, "bad tex env mode (3) in SPAN_LINEAR");
488 case MESA_FORMAT_RGBA8888
:
489 switch (info
->envmode
) {
491 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
494 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
497 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
500 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
503 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
506 _mesa_problem(ctx
, "bad tex env mode (4) in SPAN_LINEAR");
513 span
->interpMask
&= ~SPAN_RGBA
;
514 ASSERT(span
->arrayMask
& SPAN_RGBA
);
516 _swrast_write_rgba_span(ctx
, span
);
518 /* re-enable texture units */
519 ctx
->Texture
._EnabledCoordUnits
= texEnableSave
;
528 * Render an RGB/RGBA textured triangle without perspective correction.
530 #define NAME affine_textured_triangle
533 #define INTERP_ALPHA 1
534 #define INTERP_INT_TEX 1
535 #define S_SCALE twidth
536 #define T_SCALE theight
539 struct affine_info info; \
540 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
541 const struct gl_texture_object *obj = \
542 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
543 const struct gl_texture_image *texImg = \
544 obj->Image[0][obj->BaseLevel]; \
545 const struct swrast_texture_image *swImg = \
546 swrast_texture_image_const(texImg); \
547 const GLfloat twidth = (GLfloat) texImg->Width; \
548 const GLfloat theight = (GLfloat) texImg->Height; \
549 info.texture = (const GLchan *) swImg->Map; \
550 info.twidth_log2 = texImg->WidthLog2; \
551 info.smask = texImg->Width - 1; \
552 info.tmask = texImg->Height - 1; \
553 info.format = texImg->TexFormat; \
554 info.filter = obj->Sampler.MinFilter; \
555 info.envmode = unit->EnvMode; \
559 span.arrayMask |= SPAN_RGBA; \
561 if (info.envmode == GL_BLEND) { \
562 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
563 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
564 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
565 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
566 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
568 if (!info.texture) { \
569 /* this shouldn't happen */ \
573 switch (info.format) { \
574 case MESA_FORMAT_RGB888: \
575 info.tbytesline = texImg->Width * 3; \
577 case MESA_FORMAT_RGBA8888: \
578 info.tbytesline = texImg->Width * 4; \
581 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
584 info.tsize = texImg->Height * info.tbytesline;
586 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
588 #include "s_tritemp.h"
599 const GLchan
*texture
;
600 GLfixed er
, eg
, eb
, ea
; /* texture env color */
601 GLint tbytesline
, tsize
;
606 fast_persp_span(struct gl_context
*ctx
, SWspan
*span
,
607 struct persp_info
*info
)
609 GLchan sample
[4]; /* the filtered texture sample */
611 /* Instead of defining a function for each mode, a test is done
612 * between the outer and inner loops. This is to reduce code size
613 * and complexity. Observe that an optimizing compiler kills
614 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
616 #define SPAN_NEAREST(DO_TEX,COMP) \
617 for (i = 0; i < span->end; i++) { \
618 GLdouble invQ = tex_coord[2] ? \
619 (1.0 / tex_coord[2]) : 1.0; \
620 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
621 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
622 GLint s = IFLOOR(s_tmp) & info->smask; \
623 GLint t = IFLOOR(t_tmp) & info->tmask; \
624 GLint pos = (t << info->twidth_log2) + s; \
625 const GLchan *tex00 = info->texture + COMP * pos; \
627 span->red += span->redStep; \
628 span->green += span->greenStep; \
629 span->blue += span->blueStep; \
630 span->alpha += span->alphaStep; \
631 tex_coord[0] += tex_step[0]; \
632 tex_coord[1] += tex_step[1]; \
633 tex_coord[2] += tex_step[2]; \
637 #define SPAN_LINEAR(DO_TEX,COMP) \
638 for (i = 0; i < span->end; i++) { \
639 GLdouble invQ = tex_coord[2] ? \
640 (1.0 / tex_coord[2]) : 1.0; \
641 const GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
642 const GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
643 const GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
644 const GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
645 const GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
646 const GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
647 const GLfixed sf = s_fix & FIXED_FRAC_MASK; \
648 const GLfixed tf = t_fix & FIXED_FRAC_MASK; \
649 const GLint pos = (t << info->twidth_log2) + s; \
650 const GLchan *tex00 = info->texture + COMP * pos; \
651 const GLchan *tex10 = tex00 + info->tbytesline; \
652 const GLchan *tex01 = tex00 + COMP; \
653 const GLchan *tex11 = tex10 + COMP; \
654 if (t == info->tmask) { \
655 tex10 -= info->tsize; \
656 tex11 -= info->tsize; \
658 if (s == info->smask) { \
659 tex01 -= info->tbytesline; \
660 tex11 -= info->tbytesline; \
663 span->red += span->redStep; \
664 span->green += span->greenStep; \
665 span->blue += span->blueStep; \
666 span->alpha += span->alphaStep; \
667 tex_coord[0] += tex_step[0]; \
668 tex_coord[1] += tex_step[1]; \
669 tex_coord[2] += tex_step[2]; \
674 GLfloat tex_coord
[3], tex_step
[3];
675 GLchan
*dest
= span
->array
->rgba
[0];
677 const GLuint texEnableSave
= ctx
->Texture
._EnabledCoordUnits
;
678 ctx
->Texture
._EnabledCoordUnits
= 0;
680 tex_coord
[0] = span
->attrStart
[FRAG_ATTRIB_TEX0
][0] * (info
->smask
+ 1);
681 tex_step
[0] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][0] * (info
->smask
+ 1);
682 tex_coord
[1] = span
->attrStart
[FRAG_ATTRIB_TEX0
][1] * (info
->tmask
+ 1);
683 tex_step
[1] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][1] * (info
->tmask
+ 1);
684 /* span->attrStart[FRAG_ATTRIB_TEX0][2] only if 3D-texturing, here only 2D */
685 tex_coord
[2] = span
->attrStart
[FRAG_ATTRIB_TEX0
][3];
686 tex_step
[2] = span
->attrStepX
[FRAG_ATTRIB_TEX0
][3];
688 switch (info
->filter
) {
690 switch (info
->format
) {
691 case MESA_FORMAT_RGB888
:
692 switch (info
->envmode
) {
694 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
698 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
701 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
704 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
707 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
711 case MESA_FORMAT_RGBA8888
:
712 switch(info
->envmode
) {
714 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
717 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
720 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
723 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
726 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
729 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
737 switch (info
->format
) {
738 case MESA_FORMAT_RGB888
:
739 switch (info
->envmode
) {
741 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
745 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
748 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
751 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
754 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
758 case MESA_FORMAT_RGBA8888
:
759 switch (info
->envmode
) {
761 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
764 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
767 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
770 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
773 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
776 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
784 ASSERT(span
->arrayMask
& SPAN_RGBA
);
785 _swrast_write_rgba_span(ctx
, span
);
791 ctx
->Texture
._EnabledCoordUnits
= texEnableSave
;
796 * Render an perspective corrected RGB/RGBA textured triangle.
797 * The Q (aka V in Mesa) coordinate must be zero such that the divide
798 * by interpolated Q/W comes out right.
801 #define NAME persp_textured_triangle
804 #define INTERP_ALPHA 1
805 #define INTERP_ATTRIBS 1
808 struct persp_info info; \
809 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
810 const struct gl_texture_object *obj = \
811 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
812 const struct gl_texture_image *texImg = \
813 obj->Image[0][obj->BaseLevel]; \
814 const struct swrast_texture_image *swImg = \
815 swrast_texture_image_const(texImg); \
816 info.texture = (const GLchan *) swImg->Map; \
817 info.twidth_log2 = texImg->WidthLog2; \
818 info.smask = texImg->Width - 1; \
819 info.tmask = texImg->Height - 1; \
820 info.format = texImg->TexFormat; \
821 info.filter = obj->Sampler.MinFilter; \
822 info.envmode = unit->EnvMode; \
827 if (info.envmode == GL_BLEND) { \
828 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
829 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
830 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
831 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
832 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
834 if (!info.texture) { \
835 /* this shouldn't happen */ \
839 switch (info.format) { \
840 case MESA_FORMAT_RGB888: \
841 info.tbytesline = texImg->Width * 3; \
843 case MESA_FORMAT_RGBA8888: \
844 info.tbytesline = texImg->Width * 4; \
847 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
850 info.tsize = texImg->Height * info.tbytesline;
852 #define RENDER_SPAN( span ) \
853 span.interpMask &= ~SPAN_RGBA; \
854 span.arrayMask |= SPAN_RGBA; \
855 fast_persp_span(ctx, &span, &info);
857 #include "s_tritemp.h"
859 #endif /*CHAN_TYPE != GL_FLOAT*/
864 * Render an RGBA triangle with arbitrary attributes.
866 #define NAME general_triangle
869 #define INTERP_ALPHA 1
870 #define INTERP_ATTRIBS 1
871 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
872 #include "s_tritemp.h"
877 nodraw_triangle( struct gl_context
*ctx
,
882 (void) (ctx
&& v0
&& v1
&& v2
);
887 * This is used when separate specular color is enabled, but not
888 * texturing. We add the specular color to the primary color,
889 * draw the triangle, then restore the original primary color.
890 * Inefficient, but seldom needed.
893 _swrast_add_spec_terms_triangle(struct gl_context
*ctx
, const SWvertex
*v0
,
894 const SWvertex
*v1
, const SWvertex
*v2
)
896 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
897 SWvertex
*ncv1
= (SWvertex
*)v1
;
898 SWvertex
*ncv2
= (SWvertex
*)v2
;
899 GLfloat rSum
, gSum
, bSum
;
902 /* save original colors */
903 COPY_CHAN4( cSave
[0], ncv0
->color
);
904 COPY_CHAN4( cSave
[1], ncv1
->color
);
905 COPY_CHAN4( cSave
[2], ncv2
->color
);
907 rSum
= CHAN_TO_FLOAT(ncv0
->color
[0]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][0];
908 gSum
= CHAN_TO_FLOAT(ncv0
->color
[1]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][1];
909 bSum
= CHAN_TO_FLOAT(ncv0
->color
[2]) + ncv0
->attrib
[FRAG_ATTRIB_COL1
][2];
910 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[0], rSum
);
911 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[1], gSum
);
912 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[2], bSum
);
914 rSum
= CHAN_TO_FLOAT(ncv1
->color
[0]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][0];
915 gSum
= CHAN_TO_FLOAT(ncv1
->color
[1]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][1];
916 bSum
= CHAN_TO_FLOAT(ncv1
->color
[2]) + ncv1
->attrib
[FRAG_ATTRIB_COL1
][2];
917 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[0], rSum
);
918 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[1], gSum
);
919 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[2], bSum
);
921 rSum
= CHAN_TO_FLOAT(ncv2
->color
[0]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][0];
922 gSum
= CHAN_TO_FLOAT(ncv2
->color
[1]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][1];
923 bSum
= CHAN_TO_FLOAT(ncv2
->color
[2]) + ncv2
->attrib
[FRAG_ATTRIB_COL1
][2];
924 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[0], rSum
);
925 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[1], gSum
);
926 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[2], bSum
);
928 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
929 /* restore original colors */
930 COPY_CHAN4( ncv0
->color
, cSave
[0] );
931 COPY_CHAN4( ncv1
->color
, cSave
[1] );
932 COPY_CHAN4( ncv2
->color
, cSave
[2] );
939 /* record the current triangle function name */
940 const char *_mesa_triFuncName
= NULL
;
942 #define USE(triFunc) \
944 _mesa_triFuncName = #triFunc; \
945 /*printf("%s\n", _mesa_triFuncName);*/ \
946 swrast->Triangle = triFunc; \
951 #define USE(triFunc) swrast->Triangle = triFunc;
959 * Determine which triangle rendering function to use given the current
962 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
963 * remove tests to this code.
966 _swrast_choose_triangle( struct gl_context
*ctx
)
968 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
970 if (ctx
->Polygon
.CullFlag
&&
971 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
972 USE(nodraw_triangle
);
976 if (ctx
->RenderMode
==GL_RENDER
) {
978 if (ctx
->Polygon
.SmoothFlag
) {
979 _swrast_set_aa_triangle_function(ctx
);
980 ASSERT(swrast
->Triangle
);
985 * XXX should examine swrast->_ActiveAttribMask to determine what
986 * needs to be interpolated.
988 if (ctx
->Texture
._EnabledCoordUnits
||
989 _swrast_use_fragment_program(ctx
) ||
990 _mesa_need_secondary_color(ctx
) ||
991 swrast
->_FogEnabled
) {
992 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
993 const struct gl_texture_object
*texObj2D
;
994 const struct gl_texture_image
*texImg
;
995 const struct swrast_texture_image
*swImg
;
996 GLenum minFilter
, magFilter
, envMode
;
998 texObj2D
= ctx
->Texture
.Unit
[0].CurrentTex
[TEXTURE_2D_INDEX
];
1000 texImg
= texObj2D
? texObj2D
->Image
[0][texObj2D
->BaseLevel
] : NULL
;
1001 swImg
= swrast_texture_image_const(texImg
);
1003 format
= texImg
? texImg
->TexFormat
: MESA_FORMAT_NONE
;
1004 minFilter
= texObj2D
? texObj2D
->Sampler
.MinFilter
: GL_NONE
;
1005 magFilter
= texObj2D
? texObj2D
->Sampler
.MagFilter
: GL_NONE
;
1006 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1008 /* First see if we can use an optimized 2-D texture function */
1009 if (ctx
->Texture
._EnabledCoordUnits
== 0x1
1010 && !_swrast_use_fragment_program(ctx
)
1011 && ctx
->Texture
._EnabledUnits
== 0x1
1012 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1013 && texObj2D
->Sampler
.WrapS
== GL_REPEAT
1014 && texObj2D
->Sampler
.WrapT
== GL_REPEAT
1015 && swImg
->_IsPowerOfTwo
1016 && texImg
->Border
== 0
1017 && texImg
->Width
== swImg
->RowStride
1018 && (format
== MESA_FORMAT_RGB888
|| format
== MESA_FORMAT_RGBA8888
)
1019 && minFilter
== magFilter
1020 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1021 && !swrast
->_FogEnabled
1022 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
1023 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE4_NV
) {
1024 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1025 if (minFilter
== GL_NEAREST
1026 && format
== MESA_FORMAT_RGB888
1027 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1028 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1029 && ctx
->Depth
.Func
== GL_LESS
1030 && ctx
->Depth
.Mask
== GL_TRUE
)
1031 || swrast
->_RasterMask
== TEXTURE_BIT
)
1032 && ctx
->Polygon
.StippleFlag
== GL_FALSE
1033 && ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
1034 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1035 USE(simple_z_textured_triangle
);
1038 USE(simple_textured_triangle
);
1043 USE(general_triangle
);
1045 if (format
== MESA_FORMAT_RGBA8888
&& !_mesa_little_endian()) {
1046 /* We only handle RGBA8888 correctly on little endian
1047 * in the optimized code above.
1049 USE(general_triangle
);
1052 USE(affine_textured_triangle
);
1059 USE(general_triangle
);
1061 USE(persp_textured_triangle
);
1066 /* general case textured triangles */
1067 USE(general_triangle
);
1071 ASSERT(!swrast
->_FogEnabled
);
1072 ASSERT(!_mesa_need_secondary_color(ctx
));
1073 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1074 /* smooth shaded, no texturing, stippled or some raster ops */
1076 USE(general_triangle
);
1078 USE(smooth_rgba_triangle
);
1082 /* flat shaded, no texturing, stippled or some raster ops */
1084 USE(general_triangle
);
1086 USE(flat_rgba_triangle
);
1091 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1092 USE(_swrast_feedback_triangle
);
1095 /* GL_SELECT mode */
1096 USE(_swrast_select_triangle
);