2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 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.
37 #include "texformat.h"
39 #include "s_aatriangle.h"
40 #include "s_context.h"
42 #include "s_feedback.h"
44 #include "s_triangle.h"
48 * Just used for feedback mode.
51 _swrast_culltriangle( GLcontext
*ctx
,
56 GLfloat ex
= v1
->win
[0] - v0
->win
[0];
57 GLfloat ey
= v1
->win
[1] - v0
->win
[1];
58 GLfloat fx
= v2
->win
[0] - v0
->win
[0];
59 GLfloat fy
= v2
->win
[1] - v0
->win
[1];
60 GLfloat c
= ex
*fy
-ey
*fx
;
62 if (c
* SWRAST_CONTEXT(ctx
)->_BackfaceSign
> 0)
71 * Render a flat-shaded color index triangle.
73 #define NAME flat_ci_triangle
77 span.interpMask |= SPAN_INDEX; \
78 span.index = FloatToFixed(v2->index);\
80 #define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
81 #include "s_tritemp.h"
86 * Render a smooth-shaded color index triangle.
88 #define NAME smooth_ci_triangle
91 #define INTERP_INDEX 1
92 #define RENDER_SPAN( span ) _swrast_write_index_span(ctx, &span);
93 #include "s_tritemp.h"
98 * Render a flat-shaded RGBA triangle.
100 #define NAME flat_rgba_triangle
104 ASSERT(ctx->Texture._EnabledCoordUnits == 0);\
105 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
106 span.interpMask |= SPAN_RGBA; \
107 span.red = ChanToFixed(v2->color[0]); \
108 span.green = ChanToFixed(v2->color[1]); \
109 span.blue = ChanToFixed(v2->color[2]); \
110 span.alpha = ChanToFixed(v2->color[3]); \
112 span.greenStep = 0; \
115 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
116 #include "s_tritemp.h"
121 * Render a smooth-shaded RGBA triangle.
123 #define NAME smooth_rgba_triangle
127 #define INTERP_ALPHA 1
130 /* texturing must be off */ \
131 ASSERT(ctx->Texture._EnabledCoordUnits == 0); \
132 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
134 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
135 #include "s_tritemp.h"
140 * Render an RGB, GL_DECAL, textured triangle.
141 * Interpolate S,T only w/out mipmapping or perspective correction.
145 #define NAME simple_textured_triangle
146 #define INTERP_INT_TEX 1
147 #define S_SCALE twidth
148 #define T_SCALE theight
151 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
152 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];\
153 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
154 const GLint b = obj->BaseLevel; \
155 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
156 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
157 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
158 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
159 const GLint smask = obj->Image[0][b]->Width - 1; \
160 const GLint tmask = obj->Image[0][b]->Height - 1; \
162 /* this shouldn't happen */ \
166 #define RENDER_SPAN( span ) \
168 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
169 span.intTex[1] -= FIXED_HALF; \
170 for (i = 0; i < span.end; i++) { \
171 GLint s = FixedToInt(span.intTex[0]) & smask; \
172 GLint t = FixedToInt(span.intTex[1]) & tmask; \
173 GLint pos = (t << twidth_log2) + s; \
174 pos = pos + pos + pos; /* multiply by 3 */ \
175 span.array->rgb[i][RCOMP] = texture[pos]; \
176 span.array->rgb[i][GCOMP] = texture[pos+1]; \
177 span.array->rgb[i][BCOMP] = texture[pos+2]; \
178 span.intTex[0] += span.intTexStep[0]; \
179 span.intTex[1] += span.intTexStep[1]; \
181 if (swrast->Driver.WriteRGBSpan) \
182 swrast->Driver.WriteRGBSpan(ctx, rb, span.end, span.x, span.y, \
183 (CONST GLchan (*)[3]) span.array->rgb,\
186 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, span.array->rgb, NULL);
188 #include "s_tritemp.h"
193 * Render an RGB, GL_DECAL, textured triangle.
194 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
195 * perspective correction.
196 * Depth buffer bits must be <= sizeof(DEFAULT_SOFTWARE_DEPTH_TYPE)
200 #define NAME simple_z_textured_triangle
202 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
203 #define INTERP_INT_TEX 1
204 #define S_SCALE twidth
205 #define T_SCALE theight
208 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
209 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0][0];\
210 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
211 const GLint b = obj->BaseLevel; \
212 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
213 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
214 const GLint twidth_log2 = obj->Image[0][b]->WidthLog2; \
215 const GLchan *texture = (const GLchan *) obj->Image[0][b]->Data; \
216 const GLint smask = obj->Image[0][b]->Width - 1; \
217 const GLint tmask = obj->Image[0][b]->Height - 1; \
219 /* this shouldn't happen */ \
223 #define RENDER_SPAN( span ) \
225 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
226 span.intTex[1] -= FIXED_HALF; \
227 for (i = 0; i < span.end; i++) { \
228 const GLdepth z = FixedToDepth(span.z); \
230 GLint s = FixedToInt(span.intTex[0]) & smask; \
231 GLint t = FixedToInt(span.intTex[1]) & tmask; \
232 GLint pos = (t << twidth_log2) + s; \
233 pos = pos + pos + pos; /* multiply by 3 */ \
234 span.array->rgb[i][RCOMP] = texture[pos]; \
235 span.array->rgb[i][GCOMP] = texture[pos+1]; \
236 span.array->rgb[i][BCOMP] = texture[pos+2]; \
238 span.array->mask[i] = 1; \
241 span.array->mask[i] = 0; \
243 span.intTex[0] += span.intTexStep[0]; \
244 span.intTex[1] += span.intTexStep[1]; \
245 span.z += span.zStep; \
247 if (swrast->Driver.WriteRGBSpan) \
248 swrast->Driver.WriteRGBSpan(ctx, rb, span.end, span.x, span.y, \
249 (CONST GLchan (*)[3]) span.array->rgb,\
250 span.array->mask ); \
252 rb->PutRowRGB(ctx, rb, span.end, span.x, span.y, span.array->rgb, NULL);
254 #include "s_tritemp.h"
258 #if CHAN_TYPE != GL_FLOAT
267 const GLchan
*texture
;
268 GLfixed er
, eg
, eb
, ea
;
269 GLint tbytesline
, tsize
;
273 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
274 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
278 affine_span(GLcontext
*ctx
, struct sw_span
*span
,
279 struct affine_info
*info
)
281 GLchan sample
[4]; /* the filtered texture sample */
283 /* Instead of defining a function for each mode, a test is done
284 * between the outer and inner loops. This is to reduce code size
285 * and complexity. Observe that an optimizing compiler kills
286 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
289 #define NEAREST_RGB \
290 sample[RCOMP] = tex00[RCOMP]; \
291 sample[GCOMP] = tex00[GCOMP]; \
292 sample[BCOMP] = tex00[BCOMP]; \
293 sample[ACOMP] = CHAN_MAX
296 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
297 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
298 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
299 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
300 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
301 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
302 sample[ACOMP] = CHAN_MAX
304 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
306 #define LINEAR_RGBA \
307 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
308 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT;\
309 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
310 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT;\
311 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
312 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT;\
313 sample[ACOMP] = (ti * (si * tex00[3] + sf * tex01[3]) + \
314 tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT
317 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
318 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
319 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
320 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
323 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
324 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
325 >> (FIXED_SHIFT + 8); \
326 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
327 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
328 >> (FIXED_SHIFT + 8); \
329 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
330 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
331 >> (FIXED_SHIFT + 8); \
332 dest[ACOMP] = FixedToInt(span->alpha)
335 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
336 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
337 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
338 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
339 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
340 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
341 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
343 #define REPLACE COPY_CHAN4(dest, sample)
347 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
348 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
349 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
350 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
351 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
352 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
353 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
358 #define NEAREST_RGB_REPLACE \
360 dest[0] = sample[0]; \
361 dest[1] = sample[1]; \
362 dest[2] = sample[2]; \
363 dest[3] = FixedToInt(span->alpha);
365 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
367 #define SPAN_NEAREST(DO_TEX,COMP) \
368 for (i = 0; i < span->end; i++) { \
369 /* Isn't it necessary to use FixedFloor below?? */ \
370 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
371 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
372 GLint pos = (t << info->twidth_log2) + s; \
373 const GLchan *tex00 = info->texture + COMP * pos; \
375 span->red += span->redStep; \
376 span->green += span->greenStep; \
377 span->blue += span->blueStep; \
378 span->alpha += span->alphaStep; \
379 span->intTex[0] += span->intTexStep[0]; \
380 span->intTex[1] += span->intTexStep[1]; \
384 #define SPAN_LINEAR(DO_TEX,COMP) \
385 for (i = 0; i < span->end; i++) { \
386 /* Isn't it necessary to use FixedFloor below?? */ \
387 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
388 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
389 GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
390 GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
391 GLfixed si = FIXED_FRAC_MASK - sf; \
392 GLfixed ti = FIXED_FRAC_MASK - tf; \
393 GLint pos = (t << info->twidth_log2) + s; \
394 const GLchan *tex00 = info->texture + COMP * pos; \
395 const GLchan *tex10 = tex00 + info->tbytesline; \
396 const GLchan *tex01 = tex00 + COMP; \
397 const GLchan *tex11 = tex10 + COMP; \
400 if (t == info->tmask) { \
401 tex10 -= info->tsize; \
402 tex11 -= info->tsize; \
404 if (s == info->smask) { \
405 tex01 -= info->tbytesline; \
406 tex11 -= info->tbytesline; \
409 span->red += span->redStep; \
410 span->green += span->greenStep; \
411 span->blue += span->blueStep; \
412 span->alpha += span->alphaStep; \
413 span->intTex[0] += span->intTexStep[0]; \
414 span->intTex[1] += span->intTexStep[1]; \
420 GLchan
*dest
= span
->array
->rgba
[0];
422 span
->intTex
[0] -= FIXED_HALF
;
423 span
->intTex
[1] -= FIXED_HALF
;
424 switch (info
->filter
) {
426 switch (info
->format
) {
428 switch (info
->envmode
) {
430 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
434 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
437 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
440 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
443 _mesa_problem(ctx
, "bad tex env mode in SPAN_LINEAR");
448 switch(info
->envmode
) {
450 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
453 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
456 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
459 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
462 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
465 _mesa_problem(ctx
, "bad tex env mode (2) in SPAN_LINEAR");
473 span
->intTex
[0] -= FIXED_HALF
;
474 span
->intTex
[1] -= FIXED_HALF
;
475 switch (info
->format
) {
477 switch (info
->envmode
) {
479 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
483 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
486 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
489 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
492 _mesa_problem(ctx
, "bad tex env mode (3) in SPAN_LINEAR");
497 switch (info
->envmode
) {
499 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
502 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
505 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
508 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
511 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
514 _mesa_problem(ctx
, "bad tex env mode (4) in SPAN_LINEAR");
521 span
->interpMask
&= ~SPAN_RGBA
;
522 ASSERT(span
->arrayMask
& SPAN_RGBA
);
523 _swrast_write_rgba_span(ctx
, span
);
532 * Render an RGB/RGBA textured triangle without perspective correction.
534 #define NAME affine_textured_triangle
538 #define INTERP_ALPHA 1
539 #define INTERP_INT_TEX 1
540 #define S_SCALE twidth
541 #define T_SCALE theight
544 struct affine_info info; \
545 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
546 struct gl_texture_object *obj = unit->Current2D; \
547 const GLint b = obj->BaseLevel; \
548 const GLfloat twidth = (GLfloat) obj->Image[0][b]->Width; \
549 const GLfloat theight = (GLfloat) obj->Image[0][b]->Height; \
550 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
551 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
552 info.smask = obj->Image[0][b]->Width - 1; \
553 info.tmask = obj->Image[0][b]->Height - 1; \
554 info.format = obj->Image[0][b]->Format; \
555 info.filter = obj->MinFilter; \
556 info.envmode = unit->EnvMode; \
557 span.arrayMask |= SPAN_RGBA; \
559 if (info.envmode == GL_BLEND) { \
560 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
561 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
562 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
563 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
564 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
566 if (!info.texture) { \
567 /* this shouldn't happen */ \
571 switch (info.format) { \
575 info.tbytesline = obj->Image[0][b]->Width; \
577 case GL_LUMINANCE_ALPHA: \
578 info.tbytesline = obj->Image[0][b]->Width * 2; \
581 info.tbytesline = obj->Image[0][b]->Width * 3; \
584 info.tbytesline = obj->Image[0][b]->Width * 4; \
587 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
590 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
592 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
594 #include "s_tritemp.h"
605 const GLchan
*texture
;
606 GLfixed er
, eg
, eb
, ea
; /* texture env color */
607 GLint tbytesline
, tsize
;
612 fast_persp_span(GLcontext
*ctx
, struct sw_span
*span
,
613 struct persp_info
*info
)
615 GLchan sample
[4]; /* the filtered texture sample */
617 /* Instead of defining a function for each mode, a test is done
618 * between the outer and inner loops. This is to reduce code size
619 * and complexity. Observe that an optimizing compiler kills
620 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
622 #define SPAN_NEAREST(DO_TEX,COMP) \
623 for (i = 0; i < span->end; i++) { \
624 GLdouble invQ = tex_coord[2] ? \
625 (1.0 / tex_coord[2]) : 1.0; \
626 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
627 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
628 GLint s = IFLOOR(s_tmp) & info->smask; \
629 GLint t = IFLOOR(t_tmp) & info->tmask; \
630 GLint pos = (t << info->twidth_log2) + s; \
631 const GLchan *tex00 = info->texture + COMP * pos; \
633 span->red += span->redStep; \
634 span->green += span->greenStep; \
635 span->blue += span->blueStep; \
636 span->alpha += span->alphaStep; \
637 tex_coord[0] += tex_step[0]; \
638 tex_coord[1] += tex_step[1]; \
639 tex_coord[2] += tex_step[2]; \
643 #define SPAN_LINEAR(DO_TEX,COMP) \
644 for (i = 0; i < span->end; i++) { \
645 GLdouble invQ = tex_coord[2] ? \
646 (1.0 / tex_coord[2]) : 1.0; \
647 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
648 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
649 GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
650 GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
651 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
652 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
653 GLfixed sf = s_fix & FIXED_FRAC_MASK; \
654 GLfixed tf = t_fix & FIXED_FRAC_MASK; \
655 GLfixed si = FIXED_FRAC_MASK - sf; \
656 GLfixed ti = FIXED_FRAC_MASK - tf; \
657 GLint pos = (t << info->twidth_log2) + s; \
658 const GLchan *tex00 = info->texture + COMP * pos; \
659 const GLchan *tex10 = tex00 + info->tbytesline; \
660 const GLchan *tex01 = tex00 + COMP; \
661 const GLchan *tex11 = tex10 + COMP; \
664 if (t == info->tmask) { \
665 tex10 -= info->tsize; \
666 tex11 -= info->tsize; \
668 if (s == info->smask) { \
669 tex01 -= info->tbytesline; \
670 tex11 -= info->tbytesline; \
673 span->red += span->redStep; \
674 span->green += span->greenStep; \
675 span->blue += span->blueStep; \
676 span->alpha += span->alphaStep; \
677 tex_coord[0] += tex_step[0]; \
678 tex_coord[1] += tex_step[1]; \
679 tex_coord[2] += tex_step[2]; \
684 GLfloat tex_coord
[3], tex_step
[3];
685 GLchan
*dest
= span
->array
->rgba
[0];
687 const GLuint savedTexEnable
= ctx
->Texture
._EnabledUnits
;
688 ctx
->Texture
._EnabledUnits
= 0;
690 tex_coord
[0] = span
->tex
[0][0] * (info
->smask
+ 1);
691 tex_step
[0] = span
->texStepX
[0][0] * (info
->smask
+ 1);
692 tex_coord
[1] = span
->tex
[0][1] * (info
->tmask
+ 1);
693 tex_step
[1] = span
->texStepX
[0][1] * (info
->tmask
+ 1);
694 /* span->tex[0][2] only if 3D-texturing, here only 2D */
695 tex_coord
[2] = span
->tex
[0][3];
696 tex_step
[2] = span
->texStepX
[0][3];
698 switch (info
->filter
) {
700 switch (info
->format
) {
702 switch (info
->envmode
) {
704 SPAN_NEAREST(NEAREST_RGB
;MODULATE
,3);
708 SPAN_NEAREST(NEAREST_RGB_REPLACE
,3);
711 SPAN_NEAREST(NEAREST_RGB
;BLEND
,3);
714 SPAN_NEAREST(NEAREST_RGB
;ADD
,3);
717 _mesa_problem(ctx
, "bad tex env mode (5) in SPAN_LINEAR");
722 switch(info
->envmode
) {
724 SPAN_NEAREST(NEAREST_RGBA
;MODULATE
,4);
727 SPAN_NEAREST(NEAREST_RGBA
;DECAL
,4);
730 SPAN_NEAREST(NEAREST_RGBA
;BLEND
,4);
733 SPAN_NEAREST(NEAREST_RGBA
;ADD
,4);
736 SPAN_NEAREST(NEAREST_RGBA_REPLACE
,4);
739 _mesa_problem(ctx
, "bad tex env mode (6) in SPAN_LINEAR");
747 switch (info
->format
) {
749 switch (info
->envmode
) {
751 SPAN_LINEAR(LINEAR_RGB
;MODULATE
,3);
755 SPAN_LINEAR(LINEAR_RGB
;REPLACE
,3);
758 SPAN_LINEAR(LINEAR_RGB
;BLEND
,3);
761 SPAN_LINEAR(LINEAR_RGB
;ADD
,3);
764 _mesa_problem(ctx
, "bad tex env mode (7) in SPAN_LINEAR");
769 switch (info
->envmode
) {
771 SPAN_LINEAR(LINEAR_RGBA
;MODULATE
,4);
774 SPAN_LINEAR(LINEAR_RGBA
;DECAL
,4);
777 SPAN_LINEAR(LINEAR_RGBA
;BLEND
,4);
780 SPAN_LINEAR(LINEAR_RGBA
;ADD
,4);
783 SPAN_LINEAR(LINEAR_RGBA
;REPLACE
,4);
786 _mesa_problem(ctx
, "bad tex env mode (8) in SPAN_LINEAR");
794 ASSERT(span
->arrayMask
& SPAN_RGBA
);
795 _swrast_write_rgba_span(ctx
, span
);
801 ctx
->Texture
._EnabledUnits
= savedTexEnable
;
806 * Render an perspective corrected RGB/RGBA textured triangle.
807 * The Q (aka V in Mesa) coordinate must be zero such that the divide
808 * by interpolated Q/W comes out right.
811 #define NAME persp_textured_triangle
816 #define INTERP_ALPHA 1
820 struct persp_info info; \
821 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
822 const struct gl_texture_object *obj = unit->Current2D; \
823 const GLint b = obj->BaseLevel; \
824 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
825 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
826 info.smask = obj->Image[0][b]->Width - 1; \
827 info.tmask = obj->Image[0][b]->Height - 1; \
828 info.format = obj->Image[0][b]->Format; \
829 info.filter = obj->MinFilter; \
830 info.envmode = unit->EnvMode; \
832 if (info.envmode == GL_BLEND) { \
833 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
834 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
835 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
836 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
837 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
839 if (!info.texture) { \
840 /* this shouldn't happen */ \
844 switch (info.format) { \
848 info.tbytesline = obj->Image[0][b]->Width; \
850 case GL_LUMINANCE_ALPHA: \
851 info.tbytesline = obj->Image[0][b]->Width * 2; \
854 info.tbytesline = obj->Image[0][b]->Width * 3; \
857 info.tbytesline = obj->Image[0][b]->Width * 4; \
860 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
863 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
865 #define RENDER_SPAN( span ) \
866 span.interpMask &= ~SPAN_RGBA; \
867 span.arrayMask |= SPAN_RGBA; \
868 fast_persp_span(ctx, &span, &info);
870 #include "s_tritemp.h"
873 #endif /* CHAN_BITS != GL_FLOAT */
879 * Render a smooth-shaded, textured, RGBA triangle.
880 * Interpolate S,T,R with perspective correction, w/out mipmapping.
882 #define NAME general_textured_triangle
887 #define INTERP_SPEC 1
888 #define INTERP_ALPHA 1
890 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
891 #include "s_tritemp.h"
896 * This is the big one!
897 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates.
900 #define NAME multitextured_triangle
905 #define INTERP_ALPHA 1
906 #define INTERP_SPEC 1
907 #define INTERP_MULTITEX 1
908 #define RENDER_SPAN( span ) _swrast_write_rgba_span(ctx, &span);
909 #include "s_tritemp.h"
914 * Special tri function for occlusion testing
916 #define NAME occlusion_zless_triangle
919 struct gl_renderbuffer *rb \
920 = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer; \
921 ASSERT(ctx->Depth.Test); \
922 ASSERT(!ctx->Depth.Mask); \
923 ASSERT(ctx->Depth.Func == GL_LESS); \
924 if (ctx->OcclusionResult && !ctx->Occlusion.Active) { \
927 #define RENDER_SPAN( span ) \
928 if (ctx->Visual.depthBits <= 16) { \
930 const GLushort *zRow = (const GLushort *) \
931 rb->GetPointer(ctx, rb, span.x, span.y); \
932 for (i = 0; i < span.end; i++) { \
933 GLdepth z = FixedToDepth(span.z); \
935 ctx->OcclusionResult = GL_TRUE; \
936 ctx->Occlusion.PassedCounter++; \
938 span.z += span.zStep; \
943 const GLuint *zRow = (const GLuint *) \
944 rb->GetPointer(ctx, rb, span.x, span.y); \
945 for (i = 0; i < span.end; i++) { \
946 if ((GLuint)span.z < zRow[i]) { \
947 ctx->OcclusionResult = GL_TRUE; \
948 ctx->Occlusion.PassedCounter++; \
950 span.z += span.zStep; \
953 #include "s_tritemp.h"
958 nodraw_triangle( GLcontext
*ctx
,
963 (void) (ctx
&& v0
&& v1
&& v2
);
968 * This is used when separate specular color is enabled, but not
969 * texturing. We add the specular color to the primary color,
970 * draw the triangle, then restore the original primary color.
971 * Inefficient, but seldom needed.
973 void _swrast_add_spec_terms_triangle( GLcontext
*ctx
,
978 SWvertex
*ncv0
= (SWvertex
*)v0
; /* drop const qualifier */
979 SWvertex
*ncv1
= (SWvertex
*)v1
;
980 SWvertex
*ncv2
= (SWvertex
*)v2
;
981 #if CHAN_TYPE == GL_FLOAT
982 GLfloat rSum
, gSum
, bSum
;
984 GLint rSum
, gSum
, bSum
;
987 /* save original colors */
988 COPY_CHAN4( c
[0], ncv0
->color
);
989 COPY_CHAN4( c
[1], ncv1
->color
);
990 COPY_CHAN4( c
[2], ncv2
->color
);
992 rSum
= ncv0
->color
[0] + ncv0
->specular
[0];
993 gSum
= ncv0
->color
[1] + ncv0
->specular
[1];
994 bSum
= ncv0
->color
[2] + ncv0
->specular
[2];
995 ncv0
->color
[0] = MIN2(rSum
, CHAN_MAX
);
996 ncv0
->color
[1] = MIN2(gSum
, CHAN_MAX
);
997 ncv0
->color
[2] = MIN2(bSum
, CHAN_MAX
);
999 rSum
= ncv1
->color
[0] + ncv1
->specular
[0];
1000 gSum
= ncv1
->color
[1] + ncv1
->specular
[1];
1001 bSum
= ncv1
->color
[2] + ncv1
->specular
[2];
1002 ncv1
->color
[0] = MIN2(rSum
, CHAN_MAX
);
1003 ncv1
->color
[1] = MIN2(gSum
, CHAN_MAX
);
1004 ncv1
->color
[2] = MIN2(bSum
, CHAN_MAX
);
1006 rSum
= ncv2
->color
[0] + ncv2
->specular
[0];
1007 gSum
= ncv2
->color
[1] + ncv2
->specular
[1];
1008 bSum
= ncv2
->color
[2] + ncv2
->specular
[2];
1009 ncv2
->color
[0] = MIN2(rSum
, CHAN_MAX
);
1010 ncv2
->color
[1] = MIN2(gSum
, CHAN_MAX
);
1011 ncv2
->color
[2] = MIN2(bSum
, CHAN_MAX
);
1013 SWRAST_CONTEXT(ctx
)->SpecTriangle( ctx
, ncv0
, ncv1
, ncv2
);
1014 /* restore original colors */
1015 COPY_CHAN4( ncv0
->color
, c
[0] );
1016 COPY_CHAN4( ncv1
->color
, c
[1] );
1017 COPY_CHAN4( ncv2
->color
, c
[2] );
1024 /* record the current triangle function name */
1025 const char *_mesa_triFuncName
= NULL
;
1027 #define USE(triFunc) \
1029 _mesa_triFuncName = #triFunc; \
1030 /*printf("%s\n", _mesa_triFuncName);*/ \
1031 swrast->Triangle = triFunc; \
1036 #define USE(triFunc) swrast->Triangle = triFunc;
1044 * Determine which triangle rendering function to use given the current
1045 * rendering context.
1047 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1048 * remove tests to this code.
1051 _swrast_choose_triangle( GLcontext
*ctx
)
1053 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1054 const GLboolean rgbmode
= ctx
->Visual
.rgbMode
;
1056 if (ctx
->Polygon
.CullFlag
&&
1057 ctx
->Polygon
.CullFaceMode
== GL_FRONT_AND_BACK
) {
1058 USE(nodraw_triangle
);
1062 if (ctx
->RenderMode
==GL_RENDER
) {
1064 if (ctx
->Polygon
.SmoothFlag
) {
1065 _swrast_set_aa_triangle_function(ctx
);
1066 ASSERT(swrast
->Triangle
);
1070 /* special case for occlusion testing */
1071 if ((ctx
->Depth
.OcclusionTest
|| ctx
->Occlusion
.Active
) &&
1073 ctx
->Depth
.Mask
== GL_FALSE
&&
1074 ctx
->Depth
.Func
== GL_LESS
&&
1075 !ctx
->Stencil
.Enabled
) {
1077 ctx
->Color
.ColorMask
[0] == 0 &&
1078 ctx
->Color
.ColorMask
[1] == 0 &&
1079 ctx
->Color
.ColorMask
[2] == 0 &&
1080 ctx
->Color
.ColorMask
[3] == 0)
1082 (!rgbmode
&& ctx
->Color
.IndexMask
== 0)) {
1083 USE(occlusion_zless_triangle
);
1088 if (ctx
->Texture
._EnabledCoordUnits
|| ctx
->FragmentProgram
._Active
) {
1089 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1090 const struct gl_texture_object
*texObj2D
;
1091 const struct gl_texture_image
*texImg
;
1092 GLenum minFilter
, magFilter
, envMode
;
1094 texObj2D
= ctx
->Texture
.Unit
[0].Current2D
;
1095 texImg
= texObj2D
? texObj2D
->Image
[0][texObj2D
->BaseLevel
] : NULL
;
1096 format
= texImg
? texImg
->TexFormat
->MesaFormat
: -1;
1097 minFilter
= texObj2D
? texObj2D
->MinFilter
: (GLenum
) 0;
1098 magFilter
= texObj2D
? texObj2D
->MagFilter
: (GLenum
) 0;
1099 envMode
= ctx
->Texture
.Unit
[0].EnvMode
;
1101 /* First see if we can use an optimized 2-D texture function */
1102 if (ctx
->Texture
._EnabledCoordUnits
== 0x1
1103 && !ctx
->FragmentProgram
._Active
1104 && ctx
->Texture
.Unit
[0]._ReallyEnabled
== TEXTURE_2D_BIT
1105 && texObj2D
->WrapS
== GL_REPEAT
1106 && texObj2D
->WrapT
== GL_REPEAT
1107 && texObj2D
->_IsPowerOfTwo
1108 && texImg
->Border
== 0
1109 && texImg
->Width
== texImg
->RowStride
1110 && (format
== MESA_FORMAT_RGB
|| format
== MESA_FORMAT_RGBA
)
1111 && minFilter
== magFilter
1112 && ctx
->Light
.Model
.ColorControl
== GL_SINGLE_COLOR
1113 && ctx
->Texture
.Unit
[0].EnvMode
!= GL_COMBINE_EXT
) {
1114 if (ctx
->Hint
.PerspectiveCorrection
==GL_FASTEST
) {
1115 if (minFilter
== GL_NEAREST
1116 && format
== MESA_FORMAT_RGB
1117 && (envMode
== GL_REPLACE
|| envMode
== GL_DECAL
)
1118 && ((swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)
1119 && ctx
->Depth
.Func
== GL_LESS
1120 && ctx
->Depth
.Mask
== GL_TRUE
)
1121 || swrast
->_RasterMask
== TEXTURE_BIT
)
1122 && ctx
->Polygon
.StippleFlag
== GL_FALSE
1123 && ctx
->Visual
.depthBits
<= 16) {
1124 if (swrast
->_RasterMask
== (DEPTH_BIT
| TEXTURE_BIT
)) {
1125 USE(simple_z_textured_triangle
);
1128 USE(simple_textured_triangle
);
1132 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1133 USE(general_textured_triangle
);
1135 USE(affine_textured_triangle
);
1140 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1141 USE(general_textured_triangle
);
1143 USE(persp_textured_triangle
);
1148 /* general case textured triangles */
1149 if (ctx
->Texture
._EnabledCoordUnits
> 1) {
1150 USE(multitextured_triangle
);
1153 USE(general_textured_triangle
);
1158 ASSERT(!ctx
->Texture
._EnabledCoordUnits
);
1159 if (ctx
->Light
.ShadeModel
==GL_SMOOTH
) {
1160 /* smooth shaded, no texturing, stippled or some raster ops */
1162 USE(smooth_rgba_triangle
);
1165 USE(smooth_ci_triangle
);
1169 /* flat shaded, no texturing, stippled or some raster ops */
1171 USE(flat_rgba_triangle
);
1174 USE(flat_ci_triangle
);
1179 else if (ctx
->RenderMode
==GL_FEEDBACK
) {
1180 USE(_swrast_feedback_triangle
);
1183 /* GL_SELECT mode */
1184 USE(_swrast_select_triangle
);