2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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 "main/samplerobj.h"
40 #include "main/teximage.h"
41 #include "program/prog_instruction.h"
43 #include "s_aatriangle.h"
44 #include "s_context.h"
45 #include "s_feedback.h"
47 #include "s_triangle.h"
51 * Test if a triangle should be culled. Used for feedback and selection mode.
52 * \return GL_TRUE if the triangle is to be culled, GL_FALSE otherwise.
55 _swrast_culltriangle( struct gl_context
*ctx
,
60 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
61 GLfloat ex
= v1
->attrib
[VARYING_SLOT_POS
][0] - v0
->attrib
[VARYING_SLOT_POS
][0];
62 GLfloat ey
= v1
->attrib
[VARYING_SLOT_POS
][1] - v0
->attrib
[VARYING_SLOT_POS
][1];
63 GLfloat fx
= v2
->attrib
[VARYING_SLOT_POS
][0] - v0
->attrib
[VARYING_SLOT_POS
][0];
64 GLfloat fy
= v2
->attrib
[VARYING_SLOT_POS
][1] - v0
->attrib
[VARYING_SLOT_POS
][1];
65 GLfloat c
= ex
*fy
-ey
*fx
;
67 if (c
* swrast
->_BackfaceSign
* swrast
->_BackfaceCullSign
<= 0.0F
)
76 * Render a flat-shaded RGBA triangle.
78 #define NAME flat_rgba_triangle
81 ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
82 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
83 span.interpMask |= SPAN_RGBA; \
84 span.red = ChanToFixed(v2->color[0]); \
85 span.green = ChanToFixed(v2->color[1]); \
86 span.blue = ChanToFixed(v2->color[2]); \
87 span.alpha = ChanToFixed(v2->color[3]); \
92 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
93 #include "s_tritemp.h"
98 * Render a smooth-shaded RGBA triangle.
100 #define NAME smooth_rgba_triangle
103 #define INTERP_ALPHA 1
106 /* texturing must be off */ \
107 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \
108 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
110 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
111 #include "s_tritemp.h"
116 * Render an RGB, GL_DECAL, textured triangle.
117 * Interpolate S,T only w/out mipmapping or perspective correction.
119 * No fog. No depth testing.
121 #define NAME simple_textured_triangle
122 #define INTERP_INT_TEX 1
123 #define S_SCALE twidth
124 #define T_SCALE theight
127 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
128 const struct gl_texture_object *obj = \
129 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
130 const struct gl_texture_image *texImg = \
131 _mesa_base_tex_image(obj); \
132 const struct swrast_texture_image *swImg = \
133 swrast_texture_image_const(texImg); \
134 const GLfloat twidth = (GLfloat) texImg->Width; \
135 const GLfloat theight = (GLfloat) texImg->Height; \
136 const GLint twidth_log2 = texImg->WidthLog2; \
137 const GLubyte *texture = (const GLubyte *) swImg->ImageSlices[0]; \
138 const GLint smask = texImg->Width - 1; \
139 const GLint tmask = texImg->Height - 1; \
140 ASSERT(texImg->TexFormat == MESA_FORMAT_BGR_UNORM8); \
141 if (!rb || !texture) { \
145 #define RENDER_SPAN( span ) \
147 GLubyte (*rgba)[4] = swrast->SpanArrays->rgba8; \
148 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
149 span.intTex[1] -= FIXED_HALF; \
150 for (i = 0; i < span.end; i++) { \
151 GLint s = FixedToInt(span.intTex[0]) & smask; \
152 GLint t = FixedToInt(span.intTex[1]) & tmask; \
153 GLint pos = (t << twidth_log2) + s; \
154 pos = pos + pos + pos; /* multiply by 3 */ \
155 rgba[i][RCOMP] = texture[pos+2]; \
156 rgba[i][GCOMP] = texture[pos+1]; \
157 rgba[i][BCOMP] = texture[pos+0]; \
158 rgba[i][ACOMP] = 0xff; \
159 span.intTex[0] += span.intTexStep[0]; \
160 span.intTex[1] += span.intTexStep[1]; \
162 _swrast_put_row(ctx, rb, GL_UNSIGNED_BYTE, span.end, \
163 span.x, span.y, rgba, NULL);
165 #include "s_tritemp.h"
170 * Render an RGB, GL_DECAL, textured triangle.
171 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
172 * perspective correction.
173 * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
177 #define NAME simple_z_textured_triangle
179 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
180 #define INTERP_INT_TEX 1
181 #define S_SCALE twidth
182 #define T_SCALE theight
185 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0]; \
186 const struct gl_texture_object *obj = \
187 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
188 const struct gl_texture_image *texImg = \
189 _mesa_base_tex_image(obj); \
190 const struct swrast_texture_image *swImg = \
191 swrast_texture_image_const(texImg); \
192 const GLfloat twidth = (GLfloat) texImg->Width; \
193 const GLfloat theight = (GLfloat) texImg->Height; \
194 const GLint twidth_log2 = texImg->WidthLog2; \
195 const GLubyte *texture = (const GLubyte *) swImg->ImageSlices[0]; \
196 const GLint smask = texImg->Width - 1; \
197 const GLint tmask = texImg->Height - 1; \
198 ASSERT(texImg->TexFormat == MESA_FORMAT_BGR_UNORM8); \
199 if (!rb || !texture) { \
203 #define RENDER_SPAN( span ) \
205 GLubyte (*rgba)[4] = swrast->SpanArrays->rgba8; \
206 GLubyte *mask = swrast->SpanArrays->mask; \
207 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
208 span.intTex[1] -= FIXED_HALF; \
209 for (i = 0; i < span.end; i++) { \
210 const GLuint z = FixedToDepth(span.z); \
212 GLint s = FixedToInt(span.intTex[0]) & smask; \
213 GLint t = FixedToInt(span.intTex[1]) & tmask; \
214 GLint pos = (t << twidth_log2) + s; \
215 pos = pos + pos + pos; /* multiply by 3 */ \
216 rgba[i][RCOMP] = texture[pos+2]; \
217 rgba[i][GCOMP] = texture[pos+1]; \
218 rgba[i][BCOMP] = texture[pos+0]; \
219 rgba[i][ACOMP] = 0xff; \
226 span.intTex[0] += span.intTexStep[0]; \
227 span.intTex[1] += span.intTexStep[1]; \
228 span.z += span.zStep; \
230 _swrast_put_row(ctx, rb, GL_UNSIGNED_BYTE, \
231 span.end, span.x, span.y, rgba, mask);
233 #include "s_tritemp.h"
236 #if CHAN_TYPE != GL_FLOAT
245 const GLchan
*texture
;
246 GLfixed er
, eg
, eb
, ea
;
247 GLint tbytesline
, tsize
;
252 ilerp(GLint t
, GLint a
, GLint b
)
254 return a
+ ((t
* (b
- a
)) >> FIXED_SHIFT
);
258 ilerp_2d(GLint ia
, GLint ib
, GLint v00
, GLint v10
, GLint v01
, GLint v11
)
260 const GLint temp0
= ilerp(ia
, v00
, v10
);
261 const GLint temp1
= ilerp(ia
, v01
, v11
);
262 return ilerp(ib
, temp0
, temp1
);
266 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
267 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
271 affine_span(struct gl_context
*ctx
, SWspan
*span
,
272 struct affine_info
*info
)
274 GLchan sample
[4]; /* the filtered texture sample */
275 const GLuint texEnableSave
= ctx
->Texture
._EnabledCoordUnits
;
277 /* Instead of defining a function for each mode, a test is done
278 * between the outer and inner loops. This is to reduce code size
279 * and complexity. Observe that an optimizing compiler kills
280 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
283 #define NEAREST_RGB \
284 sample[RCOMP] = tex00[2]; \
285 sample[GCOMP] = tex00[1]; \
286 sample[BCOMP] = tex00[0]; \
287 sample[ACOMP] = CHAN_MAX;
290 sample[RCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
291 sample[GCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
292 sample[BCOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0]);\
293 sample[ACOMP] = CHAN_MAX;
295 #define NEAREST_RGBA \
296 sample[RCOMP] = tex00[3]; \
297 sample[GCOMP] = tex00[2]; \
298 sample[BCOMP] = tex00[1]; \
299 sample[ACOMP] = tex00[0];
301 #define LINEAR_RGBA \
302 sample[RCOMP] = ilerp_2d(sf, tf, tex00[3], tex01[3], tex10[3], tex11[3]);\
303 sample[GCOMP] = ilerp_2d(sf, tf, tex00[2], tex01[2], tex10[2], tex11[2]);\
304 sample[BCOMP] = ilerp_2d(sf, tf, tex00[1], tex01[1], tex10[1], tex11[1]);\
305 sample[ACOMP] = ilerp_2d(sf, tf, tex00[0], tex01[0], tex10[0], tex11[0])
308 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
309 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
310 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
311 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
314 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
315 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
316 >> (FIXED_SHIFT + 8); \
317 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
318 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
319 >> (FIXED_SHIFT + 8); \
320 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
321 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
322 >> (FIXED_SHIFT + 8); \
323 dest[ACOMP] = FixedToInt(span->alpha)
326 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
327 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
328 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
329 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
330 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
331 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
332 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
334 #define REPLACE COPY_CHAN4(dest, sample)
338 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
339 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
340 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
341 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
342 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
343 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
344 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
349 #define NEAREST_RGB_REPLACE \
351 dest[0] = sample[0]; \
352 dest[1] = sample[1]; \
353 dest[2] = sample[2]; \
354 dest[3] = FixedToInt(span->alpha);
356 #define NEAREST_RGBA_REPLACE \
357 dest[RCOMP] = tex00[3]; \
358 dest[GCOMP] = tex00[2]; \
359 dest[BCOMP] = tex00[1]; \
360 dest[ACOMP] = tex00[0]
362 #define SPAN_NEAREST(DO_TEX, COMPS) \
363 for (i = 0; i < span->end; i++) { \
364 /* Isn't it necessary to use FixedFloor below?? */ \
365 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
366 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
367 GLint pos = (t << info->twidth_log2) + s; \
368 const GLchan *tex00 = info->texture + COMPS * pos; \
370 span->red += span->redStep; \
371 span->green += span->greenStep; \
372 span->blue += span->blueStep; \
373 span->alpha += span->alphaStep; \
374 span->intTex[0] += span->intTexStep[0]; \
375 span->intTex[1] += span->intTexStep[1]; \
379 #define SPAN_LINEAR(DO_TEX, COMPS) \
380 for (i = 0; i < span->end; i++) { \
381 /* Isn't it necessary to use FixedFloor below?? */ \
382 const GLint s = FixedToInt(span->intTex[0]) & info->smask; \
383 const GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
384 const GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
385 const GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
386 const GLint pos = (t << info->twidth_log2) + s; \
387 const GLchan *tex00 = info->texture + COMPS * pos; \
388 const GLchan *tex10 = tex00 + info->tbytesline; \
389 const GLchan *tex01 = tex00 + COMPS; \
390 const GLchan *tex11 = tex10 + COMPS; \
391 if (t == info->tmask) { \
392 tex10 -= info->tsize; \
393 tex11 -= info->tsize; \
395 if (s == info->smask) { \
396 tex01 -= info->tbytesline; \
397 tex11 -= info->tbytesline; \
400 span->red += span->redStep; \
401 span->green += span->greenStep; \
402 span->blue += span->blueStep; \
403 span->alpha += span->alphaStep; \
404 span->intTex[0] += span->intTexStep[0]; \
405 span->intTex[1] += span->intTexStep[1]; \
411 GLchan
*dest
= span
->array
->rgba
[0];
413 /* Disable tex units so they're not re-applied in swrast_write_rgba_span */
414 ctx
->Texture
._EnabledCoordUnits
= 0x0;
416 span
->intTex
[0] -= FIXED_HALF
;
417 span
->intTex
[1] -= FIXED_HALF
;
418 switch (info
->filter
) {
420 switch (info
->format
) {
421 case MESA_FORMAT_BGR_UNORM8
:
422 switch (info
->envmode
) {
424 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
428 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
431 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
434 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
437 _mesa_problem(ctx
, "bad tex env mode in SPAN_LINEAR");
441 case MESA_FORMAT_A8B8G8R8_UNORM
:
442 switch(info
->envmode
) {
444 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
447 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
450 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
453 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
456 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
459 _mesa_problem(ctx
, "bad tex env mode (2) in SPAN_LINEAR");
467 span
->intTex
[0] -= FIXED_HALF
;
468 span
->intTex
[1] -= FIXED_HALF
;
469 switch (info
->format
) {
470 case MESA_FORMAT_BGR_UNORM8
:
471 switch (info
->envmode
) {
473 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
477 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
480 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
483 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
486 _mesa_problem(ctx
, "bad tex env mode (3) in SPAN_LINEAR");
490 case MESA_FORMAT_A8B8G8R8_UNORM
:
491 switch (info
->envmode
) {
493 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
496 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
499 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
502 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
505 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
508 _mesa_problem(ctx
, "bad tex env mode (4) in SPAN_LINEAR");
515 span
->interpMask
&= ~SPAN_RGBA
;
516 ASSERT(span
->arrayMask
& SPAN_RGBA
);
518 _swrast_write_rgba_span(ctx
, span
);
520 /* re-enable texture units */
521 ctx
->Texture
._EnabledCoordUnits
= texEnableSave
;
530 * Render an RGB/RGBA textured triangle without perspective correction.
532 #define NAME affine_textured_triangle
535 #define INTERP_ALPHA 1
536 #define INTERP_INT_TEX 1
537 #define S_SCALE twidth
538 #define T_SCALE theight
541 struct affine_info info; \
542 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
543 const struct gl_texture_object *obj = \
544 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
545 const struct gl_texture_image *texImg = \
546 _mesa_base_tex_image(obj); \
547 const struct swrast_texture_image *swImg = \
548 swrast_texture_image_const(texImg); \
549 const GLfloat twidth = (GLfloat) texImg->Width; \
550 const GLfloat theight = (GLfloat) texImg->Height; \
551 info.texture = (const GLchan *) swImg->ImageSlices[0]; \
552 info.twidth_log2 = texImg->WidthLog2; \
553 info.smask = texImg->Width - 1; \
554 info.tmask = texImg->Height - 1; \
555 info.format = texImg->TexFormat; \
556 info.filter = obj->Sampler.MinFilter; \
557 info.envmode = unit->EnvMode; \
561 span.arrayMask |= SPAN_RGBA; \
563 if (info.envmode == GL_BLEND) { \
564 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
565 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
566 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
567 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
568 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
570 if (!info.texture) { \
571 /* this shouldn't happen */ \
575 switch (info.format) { \
576 case MESA_FORMAT_BGR_UNORM8: \
577 info.tbytesline = texImg->Width * 3; \
579 case MESA_FORMAT_A8B8G8R8_UNORM: \
580 info.tbytesline = texImg->Width * 4; \
583 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
586 info.tsize = texImg->Height * info.tbytesline;
588 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
590 #include "s_tritemp.h"
601 const GLchan
*texture
;
602 GLfixed er
, eg
, eb
, ea
; /* texture env color */
603 GLint tbytesline
, tsize
;
608 fast_persp_span(struct gl_context
*ctx
, SWspan
*span
,
609 struct persp_info
*info
)
611 GLchan sample
[4]; /* the filtered texture sample */
613 /* Instead of defining a function for each mode, a test is done
614 * between the outer and inner loops. This is to reduce code size
615 * and complexity. Observe that an optimizing compiler kills
616 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
618 #define SPAN_NEAREST(DO_TEX,COMP) \
619 for (i = 0; i < span->end; i++) { \
620 GLdouble invQ = tex_coord[2] ? \
621 (1.0 / tex_coord[2]) : 1.0; \
622 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
623 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
624 GLint s = IFLOOR(s_tmp) & info->smask; \
625 GLint t = IFLOOR(t_tmp) & info->tmask; \
626 GLint pos = (t << info->twidth_log2) + s; \
627 const GLchan *tex00 = info->texture + COMP * pos; \
629 span->red += span->redStep; \
630 span->green += span->greenStep; \
631 span->blue += span->blueStep; \
632 span->alpha += span->alphaStep; \
633 tex_coord[0] += tex_step[0]; \
634 tex_coord[1] += tex_step[1]; \
635 tex_coord[2] += tex_step[2]; \
639 #define SPAN_LINEAR(DO_TEX,COMP) \
640 for (i = 0; i < span->end; i++) { \
641 GLdouble invQ = tex_coord[2] ? \
642 (1.0 / tex_coord[2]) : 1.0; \
643 const GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
644 const GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
645 const GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
646 const GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
647 const GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
648 const GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
649 const GLfixed sf = s_fix & FIXED_FRAC_MASK; \
650 const GLfixed tf = t_fix & FIXED_FRAC_MASK; \
651 const GLint pos = (t << info->twidth_log2) + s; \
652 const GLchan *tex00 = info->texture + COMP * pos; \
653 const GLchan *tex10 = tex00 + info->tbytesline; \
654 const GLchan *tex01 = tex00 + COMP; \
655 const GLchan *tex11 = tex10 + COMP; \
656 if (t == info->tmask) { \
657 tex10 -= info->tsize; \
658 tex11 -= info->tsize; \
660 if (s == info->smask) { \
661 tex01 -= info->tbytesline; \
662 tex11 -= info->tbytesline; \
665 span->red += span->redStep; \
666 span->green += span->greenStep; \
667 span->blue += span->blueStep; \
668 span->alpha += span->alphaStep; \
669 tex_coord[0] += tex_step[0]; \
670 tex_coord[1] += tex_step[1]; \
671 tex_coord[2] += tex_step[2]; \
676 GLfloat tex_coord
[3], tex_step
[3];
677 GLchan
*dest
= span
->array
->rgba
[0];
679 const GLuint texEnableSave
= ctx
->Texture
._EnabledCoordUnits
;
680 ctx
->Texture
._EnabledCoordUnits
= 0;
682 tex_coord
[0] = span
->attrStart
[VARYING_SLOT_TEX0
][0] * (info
->smask
+ 1);
683 tex_step
[0] = span
->attrStepX
[VARYING_SLOT_TEX0
][0] * (info
->smask
+ 1);
684 tex_coord
[1] = span
->attrStart
[VARYING_SLOT_TEX0
][1] * (info
->tmask
+ 1);
685 tex_step
[1] = span
->attrStepX
[VARYING_SLOT_TEX0
][1] * (info
->tmask
+ 1);
686 /* span->attrStart[VARYING_SLOT_TEX0][2] only if 3D-texturing, here only 2D */
687 tex_coord
[2] = span
->attrStart
[VARYING_SLOT_TEX0
][3];
688 tex_step
[2] = span
->attrStepX
[VARYING_SLOT_TEX0
][3];
690 switch (info
->filter
) {
692 switch (info
->format
) {
693 case MESA_FORMAT_BGR_UNORM8
:
694 switch (info
->envmode
) {
696 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
700 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
703 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
706 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
709 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
713 case MESA_FORMAT_A8B8G8R8_UNORM
:
714 switch(info
->envmode
) {
716 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
719 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
722 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
725 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
728 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
731 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
739 switch (info
->format
) {
740 case MESA_FORMAT_BGR_UNORM8
:
741 switch (info
->envmode
) {
743 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
747 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
750 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
753 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
756 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
760 case MESA_FORMAT_A8B8G8R8_UNORM
:
761 switch (info
->envmode
) {
763 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
766 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
769 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
772 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
775 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
778 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
786 ASSERT(span
->arrayMask
& SPAN_RGBA
);
787 _swrast_write_rgba_span(ctx
, span
);
793 ctx
->Texture
._EnabledCoordUnits
= texEnableSave
;
798 * Render an perspective corrected RGB/RGBA textured triangle.
799 * The Q (aka V in Mesa) coordinate must be zero such that the divide
800 * by interpolated Q/W comes out right.
803 #define NAME persp_textured_triangle
806 #define INTERP_ALPHA 1
807 #define INTERP_ATTRIBS 1
810 struct persp_info info; \
811 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
812 const struct gl_texture_object *obj = \
813 ctx->Texture.Unit[0].CurrentTex[TEXTURE_2D_INDEX]; \
814 const struct gl_texture_image *texImg = \
815 _mesa_base_tex_image(obj); \
816 const struct swrast_texture_image *swImg = \
817 swrast_texture_image_const(texImg); \
818 info.texture = (const GLchan *) swImg->ImageSlices[0]; \
819 info.twidth_log2 = texImg->WidthLog2; \
820 info.smask = texImg->Width - 1; \
821 info.tmask = texImg->Height - 1; \
822 info.format = texImg->TexFormat; \
823 info.filter = obj->Sampler.MinFilter; \
824 info.envmode = unit->EnvMode; \
829 if (info.envmode == GL_BLEND) { \
830 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
831 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
832 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
833 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
834 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
836 if (!info.texture) { \
837 /* this shouldn't happen */ \
841 switch (info.format) { \
842 case MESA_FORMAT_BGR_UNORM8: \
843 info.tbytesline = texImg->Width * 3; \
845 case MESA_FORMAT_A8B8G8R8_UNORM: \
846 info.tbytesline = texImg->Width * 4; \
849 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
852 info.tsize = texImg->Height * info.tbytesline;
854 #define RENDER_SPAN( span ) \
855 span.interpMask &= ~SPAN_RGBA; \
856 span.arrayMask |= SPAN_RGBA; \
857 fast_persp_span(ctx, &span, &info);
859 #include "s_tritemp.h"
861 #endif /*CHAN_TYPE != GL_FLOAT*/
866 * Render an RGBA triangle with arbitrary attributes.
868 #define NAME general_triangle
871 #define INTERP_ALPHA 1
872 #define INTERP_ATTRIBS 1
873 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
874 #include "s_tritemp.h"
880 * Special tri function for occlusion testing
882 #define NAME occlusion_zless_16_triangle
885 struct gl_renderbuffer *rb = \
886 ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer; \
887 struct gl_query_object *q = ctx->Query.CurrentOcclusionObject; \
888 ASSERT(ctx->Depth.Test); \
889 ASSERT(!ctx->Depth.Mask); \
890 ASSERT(ctx->Depth.Func == GL_LESS); \
891 assert(rb->Format == MESA_FORMAT_Z_UNORM16); \
895 #define RENDER_SPAN( span ) \
898 const GLushort *zRow = (const GLushort *) \
899 _swrast_pixel_address(rb, span.x, span.y); \
900 for (i = 0; i < span.end; i++) { \
901 GLuint z = FixedToDepth(span.z); \
905 span.z += span.zStep; \
908 #include "s_tritemp.h"
913 nodraw_triangle( struct gl_context
*ctx
,
918 (void) (ctx
&& v0
&& v1
&& v2
);
923 * This is used when separate specular color is enabled, but not
924 * texturing. We add the specular color to the primary color,
925 * draw the triangle, then restore the original primary color.
926 * Inefficient, but seldom needed.
929 _swrast_add_spec_terms_triangle(struct gl_context
*ctx
, const SWvertex
*v0
,
930 const SWvertex
*v1
, const SWvertex
*v2
)
932 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
933 SWvertex
*ncv1
= (SWvertex
*)v1
;
934 SWvertex
*ncv2
= (SWvertex
*)v2
;
935 GLfloat rSum
, gSum
, bSum
;
938 /* save original colors */
939 COPY_CHAN4( cSave
[0], ncv0
->color
);
940 COPY_CHAN4( cSave
[1], ncv1
->color
);
941 COPY_CHAN4( cSave
[2], ncv2
->color
);
943 rSum
= CHAN_TO_FLOAT(ncv0
->color
[0]) + ncv0
->attrib
[VARYING_SLOT_COL1
][0];
944 gSum
= CHAN_TO_FLOAT(ncv0
->color
[1]) + ncv0
->attrib
[VARYING_SLOT_COL1
][1];
945 bSum
= CHAN_TO_FLOAT(ncv0
->color
[2]) + ncv0
->attrib
[VARYING_SLOT_COL1
][2];
946 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[0], rSum
);
947 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[1], gSum
);
948 UNCLAMPED_FLOAT_TO_CHAN(ncv0
->color
[2], bSum
);
950 rSum
= CHAN_TO_FLOAT(ncv1
->color
[0]) + ncv1
->attrib
[VARYING_SLOT_COL1
][0];
951 gSum
= CHAN_TO_FLOAT(ncv1
->color
[1]) + ncv1
->attrib
[VARYING_SLOT_COL1
][1];
952 bSum
= CHAN_TO_FLOAT(ncv1
->color
[2]) + ncv1
->attrib
[VARYING_SLOT_COL1
][2];
953 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[0], rSum
);
954 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[1], gSum
);
955 UNCLAMPED_FLOAT_TO_CHAN(ncv1
->color
[2], bSum
);
957 rSum
= CHAN_TO_FLOAT(ncv2
->color
[0]) + ncv2
->attrib
[VARYING_SLOT_COL1
][0];
958 gSum
= CHAN_TO_FLOAT(ncv2
->color
[1]) + ncv2
->attrib
[VARYING_SLOT_COL1
][1];
959 bSum
= CHAN_TO_FLOAT(ncv2
->color
[2]) + ncv2
->attrib
[VARYING_SLOT_COL1
][2];
960 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[0], rSum
);
961 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[1], gSum
);
962 UNCLAMPED_FLOAT_TO_CHAN(ncv2
->color
[2], bSum
);
964 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
965 /* restore original colors */
966 COPY_CHAN4( ncv0
->color
, cSave
[0] );
967 COPY_CHAN4( ncv1
->color
, cSave
[1] );
968 COPY_CHAN4( ncv2
->color
, cSave
[2] );
975 /* record the current triangle function name */
976 const char *_mesa_triFuncName
= NULL
;
978 #define USE(triFunc) \
980 _mesa_triFuncName = #triFunc; \
981 /*printf("%s\n", _mesa_triFuncName);*/ \
982 swrast->Triangle = triFunc; \
987 #define USE(triFunc) swrast->Triangle = triFunc;
995 * Determine which triangle rendering function to use given the current
998 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
999 * remove tests to this code.
1002 _swrast_choose_triangle( struct gl_context
*ctx
)
1004 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1006 if (ctx
->Polygon
.CullFlag
&&
1007 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1008 USE(nodraw_triangle
);
1012 if (ctx
->RenderMode
==GL_RENDER
) {
1013 struct gl_renderbuffer
*depthRb
=
1014 ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
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 depthRb
->Format
== MESA_FORMAT_Z_UNORM16
) {
1030 if (ctx
->Color
.ColorMask
[0][0] == 0 &&
1031 ctx
->Color
.ColorMask
[0][1] == 0 &&
1032 ctx
->Color
.ColorMask
[0][2] == 0 &&
1033 ctx
->Color
.ColorMask
[0][3] == 0) {
1034 USE(occlusion_zless_16_triangle
);
1040 * XXX should examine swrast->_ActiveAttribMask to determine what
1041 * needs to be interpolated.
1043 if (ctx
->Texture
._EnabledCoordUnits
||
1044 _swrast_use_fragment_program(ctx
) ||
1045 ctx
->ATIFragmentShader
._Enabled
||
1046 _mesa_need_secondary_color(ctx
) ||
1047 swrast
->_FogEnabled
) {
1048 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1049 const struct gl_texture_object
*texObj2D
;
1050 const struct gl_sampler_object
*samp
;
1051 const struct gl_texture_image
*texImg
;
1052 const struct swrast_texture_image
*swImg
;
1053 GLenum minFilter
, magFilter
, envMode
;
1055 texObj2D
= ctx
->Texture
.Unit
[0].CurrentTex
[TEXTURE_2D_INDEX
];
1056 if (ctx
->Texture
.Unit
[0].Sampler
)
1057 samp
= ctx
->Texture
.Unit
[0].Sampler
;
1059 samp
= &texObj2D
->Sampler
;
1063 texImg
= texObj2D
? _mesa_base_tex_image(texObj2D
) : NULL
;
1064 swImg
= swrast_texture_image_const(texImg
);
1066 format
= texImg
? texImg
->TexFormat
: MESA_FORMAT_NONE
;
1067 minFilter
= texObj2D
? samp
->MinFilter
: GL_NONE
;
1068 magFilter
= texObj2D
? samp
->MagFilter
: GL_NONE
;
1069 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1071 /* First see if we can use an optimized 2-D texture function */
1072 if (ctx
->Texture
._EnabledCoordUnits
== 0x1
1073 && !_swrast_use_fragment_program(ctx
)
1074 && !ctx
->ATIFragmentShader
._Enabled
1075 && ctx
->Texture
._MaxEnabledTexImageUnit
== 0
1076 && ctx
->Texture
.Unit
[0]._Current
->Target
== GL_TEXTURE_2D
1077 && samp
->WrapS
== GL_REPEAT
1078 && samp
->WrapT
== GL_REPEAT
1079 && texObj2D
->_Swizzle
== SWIZZLE_NOOP
1080 && swImg
->_IsPowerOfTwo
1081 && texImg
->Border
== 0
1082 && (_mesa_format_row_stride(format
, texImg
->Width
) ==
1084 && (format
== MESA_FORMAT_BGR_UNORM8
|| format
== MESA_FORMAT_A8B8G8R8_UNORM
)
1085 && minFilter
== magFilter
1086 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1087 && !swrast
->_FogEnabled
1088 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
1089 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE4_NV
) {
1090 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1091 if (minFilter
== GL_NEAREST
1092 && format
== MESA_FORMAT_BGR_UNORM8
1093 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1094 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1095 && ctx
->Depth
.Func
== GL_LESS
1096 && ctx
->Depth
.Mask
== GL_TRUE
)
1097 || swrast
->_RasterMask
== TEXTURE_BIT
)
1098 && ctx
->Polygon
.StippleFlag
== GL_FALSE
1099 && ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
1100 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1101 USE(simple_z_textured_triangle
);
1104 USE(simple_textured_triangle
);
1109 USE(general_triangle
);
1111 if (format
== MESA_FORMAT_A8B8G8R8_UNORM
&& !_mesa_little_endian()) {
1112 /* We only handle RGBA8888 correctly on little endian
1113 * in the optimized code above.
1115 USE(general_triangle
);
1118 USE(affine_textured_triangle
);
1125 USE(general_triangle
);
1127 USE(persp_textured_triangle
);
1132 /* general case textured triangles */
1133 USE(general_triangle
);
1137 ASSERT(!swrast
->_FogEnabled
);
1138 ASSERT(!_mesa_need_secondary_color(ctx
));
1139 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1140 /* smooth shaded, no texturing, stippled or some raster ops */
1142 USE(general_triangle
);
1144 USE(smooth_rgba_triangle
);
1148 /* flat shaded, no texturing, stippled or some raster ops */
1150 USE(general_triangle
);
1152 USE(flat_rgba_triangle
);
1157 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1158 USE(_swrast_feedback_triangle
);
1161 /* GL_SELECT mode */
1162 USE(_swrast_select_triangle
);