17775526174430d0dae3125dc2b236cba7ff5148
[mesa.git] / src / mesa / swrast / s_triangle.c
1 /* $Id: s_triangle.c,v 1.55 2002/03/16 18:02:08 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 /*
29 * When the device driver doesn't implement triangle rasterization it
30 * can hook in _swrast_Triangle, which eventually calls one of these
31 * functions to draw triangles.
32 */
33
34 #include "glheader.h"
35 #include "context.h"
36 #include "colormac.h"
37 #include "macros.h"
38 #include "mem.h"
39 #include "mmath.h"
40 #include "texformat.h"
41 #include "teximage.h"
42 #include "texstate.h"
43
44 #include "s_aatriangle.h"
45 #include "s_context.h"
46 #include "s_depth.h"
47 #include "s_feedback.h"
48 #include "s_span.h"
49 #include "s_triangle.h"
50
51
52
53 GLboolean _mesa_cull_triangle( GLcontext *ctx,
54 const SWvertex *v0,
55 const SWvertex *v1,
56 const SWvertex *v2 )
57 {
58 GLfloat ex = v1->win[0] - v0->win[0];
59 GLfloat ey = v1->win[1] - v0->win[1];
60 GLfloat fx = v2->win[0] - v0->win[0];
61 GLfloat fy = v2->win[1] - v0->win[1];
62 GLfloat c = ex*fy-ey*fx;
63
64 if (c * SWRAST_CONTEXT(ctx)->_backface_sign > 0)
65 return 0;
66
67 return 1;
68 }
69
70
71
72 /*
73 * Render a flat-shaded color index triangle.
74 */
75 static void flat_ci_triangle( GLcontext *ctx,
76 const SWvertex *v0,
77 const SWvertex *v1,
78 const SWvertex *v2 )
79 {
80 #define INTERP_Z 1
81 #define INTERP_FOG 1
82
83 #define SETUP_CODE \
84 span.interpMask |= SPAN_INDEX; \
85 span.index = IntToFixed(v2->index); \
86 span.indexStep = 0;
87
88 #define RENDER_SPAN( span ) _mesa_write_index_span(ctx, &span, GL_POLYGON )
89
90 #include "s_tritemp.h"
91 }
92
93
94
95 /*
96 * Render a smooth-shaded color index triangle.
97 */
98 static void smooth_ci_triangle( GLcontext *ctx,
99 const SWvertex *v0,
100 const SWvertex *v1,
101 const SWvertex *v2 )
102 {
103 #define INTERP_Z 1
104 #define INTERP_FOG 1
105 #define INTERP_INDEX 1
106
107 #define RENDER_SPAN( span ) _mesa_write_index_span(ctx, &span, GL_POLYGON)
108
109 #include "s_tritemp.h"
110 }
111
112
113
114 /*
115 * Render a flat-shaded RGBA triangle.
116 */
117 static void flat_rgba_triangle( GLcontext *ctx,
118 const SWvertex *v0,
119 const SWvertex *v1,
120 const SWvertex *v2 )
121 {
122 #define INTERP_Z 1
123 #define INTERP_FOG 1
124 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
125
126 #define SETUP_CODE \
127 ASSERT(!ctx->Texture._ReallyEnabled); \
128 ASSERT(ctx->Light.ShadeModel==GL_FLAT); \
129 span.interpMask |= SPAN_RGBA; \
130 span.red = ChanToFixed(v2->color[0]); \
131 span.green = ChanToFixed(v2->color[1]); \
132 span.blue = ChanToFixed(v2->color[2]); \
133 span.alpha = ChanToFixed(v2->color[3]); \
134 span.redStep = 0; \
135 span.greenStep = 0; \
136 span.blueStep = 0; \
137 span.alphaStep = 0;
138
139 #define RENDER_SPAN( span ) _mesa_write_rgba_span(ctx, &span, GL_POLYGON )
140
141 #include "s_tritemp.h"
142 }
143
144
145
146 /*
147 * Render a smooth-shaded RGBA triangle.
148 */
149 static void smooth_rgba_triangle( GLcontext *ctx,
150 const SWvertex *v0,
151 const SWvertex *v1,
152 const SWvertex *v2 )
153 {
154
155 #define INTERP_Z 1
156 #define INTERP_FOG 1
157 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
158 #define INTERP_RGB 1
159 #define INTERP_ALPHA 1
160
161 #define SETUP_CODE \
162 { \
163 /* texturing must be off */ \
164 ASSERT(!ctx->Texture._ReallyEnabled); \
165 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH); \
166 }
167
168 #define RENDER_SPAN( span ) _mesa_write_rgba_span(ctx, &span, GL_POLYGON)
169
170 #include "s_tritemp.h"
171
172 }
173
174
175 /*
176 * Render an RGB, GL_DECAL, textured triangle.
177 * Interpolate S,T only w/out mipmapping or perspective correction.
178 *
179 * No fog.
180 */
181 static void simple_textured_triangle( GLcontext *ctx,
182 const SWvertex *v0,
183 const SWvertex *v1,
184 const SWvertex *v2 )
185 {
186 #define INTERP_INT_TEX 1
187 #define S_SCALE twidth
188 #define T_SCALE theight
189
190 #define SETUP_CODE \
191 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
192 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
193 const GLint b = obj->BaseLevel; \
194 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
195 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
196 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
197 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
198 const GLint smask = obj->Image[b]->Width - 1; \
199 const GLint tmask = obj->Image[b]->Height - 1; \
200 if (!texture) { \
201 /* this shouldn't happen */ \
202 return; \
203 }
204
205 #define RENDER_SPAN( span ) \
206 GLuint i; \
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 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 span.color.rgb[i][RCOMP] = texture[pos]; \
215 span.color.rgb[i][GCOMP] = texture[pos+1]; \
216 span.color.rgb[i][BCOMP] = texture[pos+2]; \
217 span.intTex[0] += span.intTexStep[0]; \
218 span.intTex[1] += span.intTexStep[1]; \
219 } \
220 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
221 (CONST GLchan (*)[3]) span.color.rgb, \
222 NULL );
223
224 #include "s_tritemp.h"
225 }
226
227
228 /*
229 * Render an RGB, GL_DECAL, textured triangle.
230 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
231 * perspective correction.
232 *
233 * No fog.
234 */
235 static void simple_z_textured_triangle( GLcontext *ctx,
236 const SWvertex *v0,
237 const SWvertex *v1,
238 const SWvertex *v2 )
239 {
240 #define INTERP_Z 1
241 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
242 #define INTERP_INT_TEX 1
243 #define S_SCALE twidth
244 #define T_SCALE theight
245
246 #define SETUP_CODE \
247 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
248 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
249 const GLint b = obj->BaseLevel; \
250 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
251 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
252 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
253 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
254 const GLint smask = obj->Image[b]->Width - 1; \
255 const GLint tmask = obj->Image[b]->Height - 1; \
256 if (!texture) { \
257 /* this shouldn't happen */ \
258 return; \
259 }
260
261 #define RENDER_SPAN( span ) \
262 GLuint i; \
263 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
264 span.intTex[1] -= FIXED_HALF; \
265 for (i = 0; i < span.end; i++) { \
266 const GLdepth z = FixedToDepth(span.z); \
267 if (z < zRow[i]) { \
268 GLint s = FixedToInt(span.intTex[0]) & smask; \
269 GLint t = FixedToInt(span.intTex[1]) & tmask; \
270 GLint pos = (t << twidth_log2) + s; \
271 pos = pos + pos + pos; /* multiply by 3 */ \
272 span.color.rgb[i][RCOMP] = texture[pos]; \
273 span.color.rgb[i][GCOMP] = texture[pos+1]; \
274 span.color.rgb[i][BCOMP] = texture[pos+2]; \
275 zRow[i] = z; \
276 span.mask[i] = 1; \
277 } \
278 else { \
279 span.mask[i] = 0; \
280 } \
281 span.intTex[0] += span.intTexStep[0]; \
282 span.intTex[1] += span.intTexStep[1]; \
283 span.z += span.zStep; \
284 } \
285 (*swrast->Driver.WriteRGBSpan)(ctx, span.end, span.x, span.y, \
286 (CONST GLchan (*)[3]) span.color.rgb, \
287 span.mask );
288
289 #include "s_tritemp.h"
290 }
291
292
293 #if CHAN_TYPE != GL_FLOAT
294
295 struct affine_info
296 {
297 GLenum filter;
298 GLenum format;
299 GLenum envmode;
300 GLint smask, tmask;
301 GLint twidth_log2;
302 const GLchan *texture;
303 GLfixed er, eg, eb, ea;
304 GLint tbytesline, tsize;
305 };
306
307
308 /* This function can handle GL_NEAREST or GL_LINEAR sampling of 2D RGB or RGBA
309 * textures with GL_REPLACE, GL_MODULATE, GL_BLEND, GL_DECAL or GL_ADD
310 * texture env modes.
311 */
312 static INLINE void
313 affine_span(GLcontext *ctx, struct sw_span *span,
314 struct affine_info *info)
315 {
316 GLchan sample[4]; /* the filtered texture sample */
317
318 /* Instead of defining a function for each mode, a test is done
319 * between the outer and inner loops. This is to reduce code size
320 * and complexity. Observe that an optimizing compiler kills
321 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
322 */
323
324 #define NEAREST_RGB \
325 sample[RCOMP] = tex00[RCOMP]; \
326 sample[GCOMP] = tex00[GCOMP]; \
327 sample[BCOMP] = tex00[BCOMP]; \
328 sample[ACOMP] = CHAN_MAX
329
330 #define LINEAR_RGB \
331 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
332 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
333 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
334 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
335 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
336 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
337 sample[ACOMP] = CHAN_MAX
338
339 #define NEAREST_RGBA COPY_CHAN4(sample, tex00)
340
341 #define LINEAR_RGBA \
342 sample[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
343 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT;\
344 sample[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
345 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT;\
346 sample[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
347 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT;\
348 sample[ACOMP] = (ti * (si * tex00[3] + sf * tex01[3]) + \
349 tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT
350
351 #define MODULATE \
352 dest[RCOMP] = span->red * (sample[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
353 dest[GCOMP] = span->green * (sample[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
354 dest[BCOMP] = span->blue * (sample[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
355 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
356
357 #define DECAL \
358 dest[RCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->red + \
359 ((sample[ACOMP] + 1) * sample[RCOMP] << FIXED_SHIFT)) \
360 >> (FIXED_SHIFT + 8); \
361 dest[GCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->green + \
362 ((sample[ACOMP] + 1) * sample[GCOMP] << FIXED_SHIFT)) \
363 >> (FIXED_SHIFT + 8); \
364 dest[BCOMP] = ((CHAN_MAX - sample[ACOMP]) * span->blue + \
365 ((sample[ACOMP] + 1) * sample[BCOMP] << FIXED_SHIFT)) \
366 >> (FIXED_SHIFT + 8); \
367 dest[ACOMP] = FixedToInt(span->alpha)
368
369 #define BLEND \
370 dest[RCOMP] = ((CHAN_MAX - sample[RCOMP]) * span->red \
371 + (sample[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
372 dest[GCOMP] = ((CHAN_MAX - sample[GCOMP]) * span->green \
373 + (sample[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
374 dest[BCOMP] = ((CHAN_MAX - sample[BCOMP]) * span->blue \
375 + (sample[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
376 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8)
377
378 #define REPLACE COPY_CHAN4(dest, sample)
379
380 #define ADD \
381 { \
382 GLint rSum = FixedToInt(span->red) + (GLint) sample[RCOMP]; \
383 GLint gSum = FixedToInt(span->green) + (GLint) sample[GCOMP]; \
384 GLint bSum = FixedToInt(span->blue) + (GLint) sample[BCOMP]; \
385 dest[RCOMP] = MIN2(rSum, CHAN_MAX); \
386 dest[GCOMP] = MIN2(gSum, CHAN_MAX); \
387 dest[BCOMP] = MIN2(bSum, CHAN_MAX); \
388 dest[ACOMP] = span->alpha * (sample[ACOMP] + 1) >> (FIXED_SHIFT + 8); \
389 }
390
391 /* shortcuts */
392
393 #define NEAREST_RGB_REPLACE \
394 NEAREST_RGB; \
395 dest[0] = sample[0]; \
396 dest[1] = sample[1]; \
397 dest[2] = sample[2]; \
398 dest[3] = FixedToInt(span->alpha);
399
400 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
401
402 #define SPAN_NEAREST(DO_TEX,COMP) \
403 for (i = 0; i < span->end; i++) { \
404 /* Isn't it necessary to use FixedFloor below?? */ \
405 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
406 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
407 GLint pos = (t << info->twidth_log2) + s; \
408 const GLchan *tex00 = info->texture + COMP * pos; \
409 DO_TEX; \
410 span->red += span->redStep; \
411 span->green += span->greenStep; \
412 span->blue += span->blueStep; \
413 span->alpha += span->alphaStep; \
414 span->intTex[0] += span->intTexStep[0]; \
415 span->intTex[1] += span->intTexStep[1]; \
416 dest += 4; \
417 }
418
419 #define SPAN_LINEAR(DO_TEX,COMP) \
420 for (i = 0; i < span->end; i++) { \
421 /* Isn't it necessary to use FixedFloor below?? */ \
422 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
423 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
424 GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
425 GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
426 GLfixed si = FIXED_FRAC_MASK - sf; \
427 GLfixed ti = FIXED_FRAC_MASK - tf; \
428 GLint pos = (t << info->twidth_log2) + s; \
429 const GLchan *tex00 = info->texture + COMP * pos; \
430 const GLchan *tex10 = tex00 + info->tbytesline; \
431 const GLchan *tex01 = tex00 + COMP; \
432 const GLchan *tex11 = tex10 + COMP; \
433 (void) ti; \
434 (void) si; \
435 if (t == info->tmask) { \
436 tex10 -= info->tsize; \
437 tex11 -= info->tsize; \
438 } \
439 if (s == info->smask) { \
440 tex01 -= info->tbytesline; \
441 tex11 -= info->tbytesline; \
442 } \
443 DO_TEX; \
444 span->red += span->redStep; \
445 span->green += span->greenStep; \
446 span->blue += span->blueStep; \
447 span->alpha += span->alphaStep; \
448 span->intTex[0] += span->intTexStep[0]; \
449 span->intTex[1] += span->intTexStep[1]; \
450 dest += 4; \
451 }
452
453
454 GLuint i;
455 GLchan *dest = span->color.rgba[0];
456
457 span->intTex[0] -= FIXED_HALF;
458 span->intTex[1] -= FIXED_HALF;
459 switch (info->filter) {
460 case GL_NEAREST:
461 switch (info->format) {
462 case GL_RGB:
463 switch (info->envmode) {
464 case GL_MODULATE:
465 SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
466 break;
467 case GL_DECAL:
468 case GL_REPLACE:
469 SPAN_NEAREST(NEAREST_RGB_REPLACE,3);
470 break;
471 case GL_BLEND:
472 SPAN_NEAREST(NEAREST_RGB;BLEND,3);
473 break;
474 case GL_ADD:
475 SPAN_NEAREST(NEAREST_RGB;ADD,3);
476 break;
477 default:
478 abort();
479 }
480 break;
481 case GL_RGBA:
482 switch(info->envmode) {
483 case GL_MODULATE:
484 SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
485 break;
486 case GL_DECAL:
487 SPAN_NEAREST(NEAREST_RGBA;DECAL,4);
488 break;
489 case GL_BLEND:
490 SPAN_NEAREST(NEAREST_RGBA;BLEND,4);
491 break;
492 case GL_ADD:
493 SPAN_NEAREST(NEAREST_RGBA;ADD,4);
494 break;
495 case GL_REPLACE:
496 SPAN_NEAREST(NEAREST_RGBA_REPLACE,4);
497 break;
498 default:
499 abort();
500 }
501 break;
502 }
503 break;
504
505 case GL_LINEAR:
506 span->intTex[0] -= FIXED_HALF;
507 span->intTex[1] -= FIXED_HALF;
508 switch (info->format) {
509 case GL_RGB:
510 switch (info->envmode) {
511 case GL_MODULATE:
512 SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
513 break;
514 case GL_DECAL:
515 case GL_REPLACE:
516 SPAN_LINEAR(LINEAR_RGB;REPLACE,3);
517 break;
518 case GL_BLEND:
519 SPAN_LINEAR(LINEAR_RGB;BLEND,3);
520 break;
521 case GL_ADD:
522 SPAN_LINEAR(LINEAR_RGB;ADD,3);
523 break;
524 default:
525 abort();
526 }
527 break;
528 case GL_RGBA:
529 switch (info->envmode) {
530 case GL_MODULATE:
531 SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
532 break;
533 case GL_DECAL:
534 SPAN_LINEAR(LINEAR_RGBA;DECAL,4);
535 break;
536 case GL_BLEND:
537 SPAN_LINEAR(LINEAR_RGBA;BLEND,4);
538 break;
539 case GL_ADD:
540 SPAN_LINEAR(LINEAR_RGBA;ADD,4);
541 break;
542 case GL_REPLACE:
543 SPAN_LINEAR(LINEAR_RGBA;REPLACE,4);
544 break;
545 default:
546 abort();
547 } break;
548 }
549 break;
550 }
551 ASSERT(span->interpMask & SPAN_RGBA); /* XXXX unset */
552 span->interpMask &= ~SPAN_RGBA;
553 ASSERT(span->arrayMask & SPAN_RGBA);
554 _mesa_write_rgba_span(ctx, span, GL_POLYGON);
555
556 #undef SPAN_NEAREST
557 #undef SPAN_LINEAR
558 }
559
560
561
562 /*
563 * Render an RGB/RGBA textured triangle without perspective correction.
564 */
565 static void affine_textured_triangle( GLcontext *ctx,
566 const SWvertex *v0,
567 const SWvertex *v1,
568 const SWvertex *v2 )
569 {
570 #define INTERP_Z 1
571 #define INTERP_FOG 1
572 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
573 #define INTERP_RGB 1
574 #define INTERP_ALPHA 1
575 #define INTERP_INT_TEX 1
576 #define S_SCALE twidth
577 #define T_SCALE theight
578
579 #define SETUP_CODE \
580 struct affine_info info; \
581 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
582 struct gl_texture_object *obj = unit->Current2D; \
583 const GLint b = obj->BaseLevel; \
584 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
585 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
586 info.texture = (const GLchan *) obj->Image[b]->Data; \
587 info.twidth_log2 = obj->Image[b]->WidthLog2; \
588 info.smask = obj->Image[b]->Width - 1; \
589 info.tmask = obj->Image[b]->Height - 1; \
590 info.format = obj->Image[b]->Format; \
591 info.filter = obj->MinFilter; \
592 info.envmode = unit->EnvMode; \
593 span.arrayMask |= SPAN_RGBA; \
594 \
595 if (info.envmode == GL_BLEND) { \
596 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
597 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
598 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
599 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
600 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
601 } \
602 if (!info.texture) { \
603 /* this shouldn't happen */ \
604 return; \
605 } \
606 \
607 switch (info.format) { \
608 case GL_ALPHA: \
609 case GL_LUMINANCE: \
610 case GL_INTENSITY: \
611 info.tbytesline = obj->Image[b]->Width; \
612 break; \
613 case GL_LUMINANCE_ALPHA: \
614 info.tbytesline = obj->Image[b]->Width * 2; \
615 break; \
616 case GL_RGB: \
617 info.tbytesline = obj->Image[b]->Width * 3; \
618 break; \
619 case GL_RGBA: \
620 info.tbytesline = obj->Image[b]->Width * 4; \
621 break; \
622 default: \
623 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
624 return; \
625 } \
626 info.tsize = obj->Image[b]->Height * info.tbytesline;
627
628 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
629
630 #include "s_tritemp.h"
631
632 }
633
634
635
636 struct persp_info
637 {
638 GLenum filter;
639 GLenum format;
640 GLenum envmode;
641 GLint smask, tmask;
642 GLint twidth_log2;
643 const GLchan *texture;
644 GLfixed er, eg, eb, ea; /* texture env color */
645 GLint tbytesline, tsize;
646 };
647
648
649 static INLINE void
650 fast_persp_span(GLcontext *ctx, struct sw_span *span,
651 struct persp_info *info)
652 {
653 GLchan sample[4]; /* the filtered texture sample */
654
655 /* Instead of defining a function for each mode, a test is done
656 * between the outer and inner loops. This is to reduce code size
657 * and complexity. Observe that an optimizing compiler kills
658 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
659 */
660 #define SPAN_NEAREST(DO_TEX,COMP) \
661 for (i = 0; i < span->end; i++) { \
662 GLdouble invQ = tex_coord[2] ? \
663 (1.0 / tex_coord[2]) : 1.0; \
664 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
665 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
666 GLint s = IFLOOR(s_tmp) & info->smask; \
667 GLint t = IFLOOR(t_tmp) & info->tmask; \
668 GLint pos = (t << info->twidth_log2) + s; \
669 const GLchan *tex00 = info->texture + COMP * pos; \
670 DO_TEX; \
671 span->red += span->redStep; \
672 span->green += span->greenStep; \
673 span->blue += span->blueStep; \
674 span->alpha += span->alphaStep; \
675 tex_coord[0] += tex_step[0]; \
676 tex_coord[1] += tex_step[1]; \
677 tex_coord[2] += tex_step[2]; \
678 dest += 4; \
679 }
680
681 #define SPAN_LINEAR(DO_TEX,COMP) \
682 for (i = 0; i < span->end; i++) { \
683 GLdouble invQ = tex_coord[2] ? \
684 (1.0 / tex_coord[2]) : 1.0; \
685 GLfloat s_tmp = (GLfloat) (tex_coord[0] * invQ); \
686 GLfloat t_tmp = (GLfloat) (tex_coord[1] * invQ); \
687 GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
688 GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
689 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
690 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
691 GLfixed sf = s_fix & FIXED_FRAC_MASK; \
692 GLfixed tf = t_fix & FIXED_FRAC_MASK; \
693 GLfixed si = FIXED_FRAC_MASK - sf; \
694 GLfixed ti = FIXED_FRAC_MASK - tf; \
695 GLint pos = (t << info->twidth_log2) + s; \
696 const GLchan *tex00 = info->texture + COMP * pos; \
697 const GLchan *tex10 = tex00 + info->tbytesline; \
698 const GLchan *tex01 = tex00 + COMP; \
699 const GLchan *tex11 = tex10 + COMP; \
700 (void) ti; \
701 (void) si; \
702 if (t == info->tmask) { \
703 tex10 -= info->tsize; \
704 tex11 -= info->tsize; \
705 } \
706 if (s == info->smask) { \
707 tex01 -= info->tbytesline; \
708 tex11 -= info->tbytesline; \
709 } \
710 DO_TEX; \
711 span->red += span->redStep; \
712 span->green += span->greenStep; \
713 span->blue += span->blueStep; \
714 span->alpha += span->alphaStep; \
715 tex_coord[0] += tex_step[0]; \
716 tex_coord[1] += tex_step[1]; \
717 tex_coord[2] += tex_step[2]; \
718 dest += 4; \
719 }
720
721 GLuint i;
722 GLfloat tex_coord[3], tex_step[3];
723 GLchan *dest = span->color.rgba[0];
724
725 tex_coord[0] = span->tex[0][0] * (info->smask + 1);
726 tex_step[0] = span->texStepX[0][0] * (info->smask + 1);
727 tex_coord[1] = span->tex[0][1] * (info->tmask + 1);
728 tex_step[1] = span->texStepX[0][1] * (info->tmask + 1);
729 /* span->tex[0][2] only if 3D-texturing, here only 2D */
730 tex_coord[2] = span->tex[0][3];
731 tex_step[2] = span->texStepX[0][3];
732
733 switch (info->filter) {
734 case GL_NEAREST:
735 switch (info->format) {
736 case GL_RGB:
737 switch (info->envmode) {
738 case GL_MODULATE:
739 SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
740 break;
741 case GL_DECAL:
742 case GL_REPLACE:
743 SPAN_NEAREST(NEAREST_RGB_REPLACE,3);
744 break;
745 case GL_BLEND:
746 SPAN_NEAREST(NEAREST_RGB;BLEND,3);
747 break;
748 case GL_ADD:
749 SPAN_NEAREST(NEAREST_RGB;ADD,3);
750 break;
751 default:
752 abort();
753 }
754 break;
755 case GL_RGBA:
756 switch(info->envmode) {
757 case GL_MODULATE:
758 SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
759 break;
760 case GL_DECAL:
761 SPAN_NEAREST(NEAREST_RGBA;DECAL,4);
762 break;
763 case GL_BLEND:
764 SPAN_NEAREST(NEAREST_RGBA;BLEND,4);
765 break;
766 case GL_ADD:
767 SPAN_NEAREST(NEAREST_RGBA;ADD,4);
768 break;
769 case GL_REPLACE:
770 SPAN_NEAREST(NEAREST_RGBA_REPLACE,4);
771 break;
772 default:
773 abort();
774 }
775 break;
776 }
777 break;
778
779 case GL_LINEAR:
780 switch (info->format) {
781 case GL_RGB:
782 switch (info->envmode) {
783 case GL_MODULATE:
784 SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
785 break;
786 case GL_DECAL:
787 case GL_REPLACE:
788 SPAN_LINEAR(LINEAR_RGB;REPLACE,3);
789 break;
790 case GL_BLEND:
791 SPAN_LINEAR(LINEAR_RGB;BLEND,3);
792 break;
793 case GL_ADD:
794 SPAN_LINEAR(LINEAR_RGB;ADD,3);
795 break;
796 default:
797 abort();
798 }
799 break;
800 case GL_RGBA:
801 switch (info->envmode) {
802 case GL_MODULATE:
803 SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
804 break;
805 case GL_DECAL:
806 SPAN_LINEAR(LINEAR_RGBA;DECAL,4);
807 break;
808 case GL_BLEND:
809 SPAN_LINEAR(LINEAR_RGBA;BLEND,4);
810 break;
811 case GL_ADD:
812 SPAN_LINEAR(LINEAR_RGBA;ADD,4);
813 break;
814 case GL_REPLACE:
815 SPAN_LINEAR(LINEAR_RGBA;REPLACE,4);
816 break;
817 default:
818 abort();
819 }
820 break;
821 }
822 break;
823 }
824
825 ASSERT(span->arrayMask & SPAN_RGBA);
826 _mesa_write_rgba_span(ctx, span, GL_POLYGON);
827
828
829 #undef SPAN_NEAREST
830 #undef SPAN_LINEAR
831 }
832
833
834 /*
835 * Render an perspective corrected RGB/RGBA textured triangle.
836 * The Q (aka V in Mesa) coordinate must be zero such that the divide
837 * by interpolated Q/W comes out right.
838 *
839 */
840 static void persp_textured_triangle( GLcontext *ctx,
841 const SWvertex *v0,
842 const SWvertex *v1,
843 const SWvertex *v2 )
844 {
845 #define INTERP_Z 1
846 #define INTERP_FOG 1
847 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
848 #define INTERP_RGB 1
849 #define INTERP_ALPHA 1
850 #define INTERP_TEX 1
851
852 #define SETUP_CODE \
853 struct persp_info info; \
854 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
855 const struct gl_texture_object *obj = unit->Current2D; \
856 const GLint b = obj->BaseLevel; \
857 info.texture = (const GLchan *) obj->Image[b]->Data; \
858 info.twidth_log2 = obj->Image[b]->WidthLog2; \
859 info.smask = obj->Image[b]->Width - 1; \
860 info.tmask = obj->Image[b]->Height - 1; \
861 info.format = obj->Image[b]->Format; \
862 info.filter = obj->MinFilter; \
863 info.envmode = unit->EnvMode; \
864 \
865 if (info.envmode == GL_BLEND) { \
866 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
867 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
868 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
869 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
870 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
871 } \
872 if (!info.texture) { \
873 /* this shouldn't happen */ \
874 return; \
875 } \
876 \
877 switch (info.format) { \
878 case GL_ALPHA: \
879 case GL_LUMINANCE: \
880 case GL_INTENSITY: \
881 info.tbytesline = obj->Image[b]->Width; \
882 break; \
883 case GL_LUMINANCE_ALPHA: \
884 info.tbytesline = obj->Image[b]->Width * 2; \
885 break; \
886 case GL_RGB: \
887 info.tbytesline = obj->Image[b]->Width * 3; \
888 break; \
889 case GL_RGBA: \
890 info.tbytesline = obj->Image[b]->Width * 4; \
891 break; \
892 default: \
893 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
894 return; \
895 } \
896 info.tsize = obj->Image[b]->Height * info.tbytesline;
897
898 #define RENDER_SPAN( span ) \
899 span.interpMask &= ~SPAN_RGBA; \
900 span.arrayMask |= SPAN_RGBA; \
901 fast_persp_span(ctx, &span, &info);
902
903 #include "s_tritemp.h"
904
905 }
906
907
908 #endif /* CHAN_BITS != GL_FLOAT */
909
910
911
912
913 /*
914 * Render a smooth-shaded, textured, RGBA triangle.
915 * Interpolate S,T,R with perspective correction, w/out mipmapping.
916 */
917 static void general_textured_triangle( GLcontext *ctx,
918 const SWvertex *v0,
919 const SWvertex *v1,
920 const SWvertex *v2 )
921 {
922 #define INTERP_Z 1
923 #define INTERP_FOG 1
924 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
925 #define INTERP_RGB 1
926 #define INTERP_SPEC 1
927 #define INTERP_ALPHA 1
928 #define INTERP_TEX 1
929
930 #define RENDER_SPAN( span ) _mesa_write_texture_span(ctx, &span, GL_POLYGON);
931
932 #include "s_tritemp.h"
933 }
934
935
936
937 /*
938 * This is the big one!
939 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates.
940 * Yup, it's slow.
941 */
942 static void
943 multitextured_triangle( GLcontext *ctx,
944 const SWvertex *v0,
945 const SWvertex *v1,
946 const SWvertex *v2 )
947 {
948
949 #define INTERP_Z 1
950 #define INTERP_FOG 1
951 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
952 #define INTERP_RGB 1
953 #define INTERP_ALPHA 1
954 #define INTERP_SPEC 1
955 #define INTERP_MULTITEX 1
956
957 #define RENDER_SPAN( span ) _mesa_write_texture_span(ctx, &span, GL_POLYGON);
958
959 #include "s_tritemp.h"
960
961 }
962
963
964 static void occlusion_zless_triangle( GLcontext *ctx,
965 const SWvertex *v0,
966 const SWvertex *v1,
967 const SWvertex *v2 )
968 {
969 if (ctx->OcclusionResult) {
970 return;
971 }
972
973 #define DO_OCCLUSION_TEST
974 #define INTERP_Z 1
975 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
976
977 #define RENDER_SPAN( span ) \
978 GLuint i; \
979 for (i = 0; i < span.end; i++) { \
980 GLdepth z = FixedToDepth(span.z); \
981 if (z < zRow[i]) { \
982 ctx->OcclusionResult = GL_TRUE; \
983 return; \
984 } \
985 span.z += span.zStep; \
986 }
987
988 #include "s_tritemp.h"
989 }
990
991 static void nodraw_triangle( GLcontext *ctx,
992 const SWvertex *v0,
993 const SWvertex *v1,
994 const SWvertex *v2 )
995 {
996 (void) (ctx && v0 && v1 && v2);
997 }
998
999
1000 /*
1001 * This is used when separate specular color is enabled, but not
1002 * texturing. We add the specular color to the primary color,
1003 * draw the triangle, then restore the original primary color.
1004 * Inefficient, but seldom needed.
1005 */
1006 void _swrast_add_spec_terms_triangle( GLcontext *ctx,
1007 const SWvertex *v0,
1008 const SWvertex *v1,
1009 const SWvertex *v2 )
1010 {
1011 SWvertex *ncv0 = (SWvertex *)v0; /* drop const qualifier */
1012 SWvertex *ncv1 = (SWvertex *)v1;
1013 SWvertex *ncv2 = (SWvertex *)v2;
1014 #if CHAN_TYPE == GL_FLOAT
1015 GLfloat rSum, gSum, bSum;
1016 #else
1017 GLint rSum, gSum, bSum;
1018 #endif
1019 GLchan c[3][4];
1020 /* save original colors */
1021 COPY_CHAN4( c[0], ncv0->color );
1022 COPY_CHAN4( c[1], ncv1->color );
1023 COPY_CHAN4( c[2], ncv2->color );
1024 /* sum v0 */
1025 rSum = ncv0->color[0] + ncv0->specular[0];
1026 gSum = ncv0->color[1] + ncv0->specular[1];
1027 bSum = ncv0->color[2] + ncv0->specular[2];
1028 ncv0->color[0] = MIN2(rSum, CHAN_MAX);
1029 ncv0->color[1] = MIN2(gSum, CHAN_MAX);
1030 ncv0->color[2] = MIN2(bSum, CHAN_MAX);
1031 /* sum v1 */
1032 rSum = ncv1->color[0] + ncv1->specular[0];
1033 gSum = ncv1->color[1] + ncv1->specular[1];
1034 bSum = ncv1->color[2] + ncv1->specular[2];
1035 ncv1->color[0] = MIN2(rSum, CHAN_MAX);
1036 ncv1->color[1] = MIN2(gSum, CHAN_MAX);
1037 ncv1->color[2] = MIN2(bSum, CHAN_MAX);
1038 /* sum v2 */
1039 rSum = ncv2->color[0] + ncv2->specular[0];
1040 gSum = ncv2->color[1] + ncv2->specular[1];
1041 bSum = ncv2->color[2] + ncv2->specular[2];
1042 ncv2->color[0] = MIN2(rSum, CHAN_MAX);
1043 ncv2->color[1] = MIN2(gSum, CHAN_MAX);
1044 ncv2->color[2] = MIN2(bSum, CHAN_MAX);
1045 /* draw */
1046 SWRAST_CONTEXT(ctx)->SpecTriangle( ctx, ncv0, ncv1, ncv2 );
1047 /* restore original colors */
1048 COPY_CHAN4( ncv0->color, c[0] );
1049 COPY_CHAN4( ncv1->color, c[1] );
1050 COPY_CHAN4( ncv2->color, c[2] );
1051 }
1052
1053
1054
1055 #ifdef DEBUG
1056
1057 /* record the current triangle function name */
1058 const char *_mesa_triFuncName = NULL;
1059
1060 #define USE(triFunc) \
1061 do { \
1062 _mesa_triFuncName = #triFunc; \
1063 /*printf("%s\n", _mesa_triFuncName);*/ \
1064 swrast->Triangle = triFunc; \
1065 } while (0)
1066
1067 #else
1068
1069 #define USE(triFunc) swrast->Triangle = triFunc;
1070
1071 #endif
1072
1073
1074
1075
1076 /*
1077 * Determine which triangle rendering function to use given the current
1078 * rendering context.
1079 *
1080 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1081 * remove tests to this code.
1082 */
1083 void
1084 _swrast_choose_triangle( GLcontext *ctx )
1085 {
1086 SWcontext *swrast = SWRAST_CONTEXT(ctx);
1087 const GLboolean rgbmode = ctx->Visual.rgbMode;
1088
1089 if (ctx->Polygon.CullFlag &&
1090 ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) {
1091 USE(nodraw_triangle);
1092 return;
1093 }
1094
1095 if (ctx->RenderMode==GL_RENDER) {
1096
1097 if (ctx->Polygon.SmoothFlag) {
1098 _mesa_set_aa_triangle_function(ctx);
1099 ASSERT(swrast->Triangle);
1100 return;
1101 }
1102
1103 if (ctx->Depth.OcclusionTest &&
1104 ctx->Depth.Test &&
1105 ctx->Depth.Mask == GL_FALSE &&
1106 ctx->Depth.Func == GL_LESS &&
1107 !ctx->Stencil.Enabled) {
1108 if ((rgbmode &&
1109 ctx->Color.ColorMask[0] == 0 &&
1110 ctx->Color.ColorMask[1] == 0 &&
1111 ctx->Color.ColorMask[2] == 0 &&
1112 ctx->Color.ColorMask[3] == 0)
1113 ||
1114 (!rgbmode && ctx->Color.IndexMask == 0)) {
1115 USE(occlusion_zless_triangle);
1116 return;
1117 }
1118 }
1119
1120 if (ctx->Texture._ReallyEnabled) {
1121 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1122 const struct gl_texture_object *texObj2D;
1123 const struct gl_texture_image *texImg;
1124 GLenum minFilter, magFilter, envMode;
1125 GLint format;
1126 texObj2D = ctx->Texture.Unit[0].Current2D;
1127 texImg = texObj2D ? texObj2D->Image[texObj2D->BaseLevel] : NULL;
1128 format = texImg ? texImg->TexFormat->MesaFormat : -1;
1129 minFilter = texObj2D ? texObj2D->MinFilter : (GLenum) 0;
1130 magFilter = texObj2D ? texObj2D->MagFilter : (GLenum) 0;
1131 envMode = ctx->Texture.Unit[0].EnvMode;
1132
1133 /* First see if we can used an optimized 2-D texture function */
1134 if (ctx->Texture._ReallyEnabled==TEXTURE0_2D
1135 && texObj2D->WrapS==GL_REPEAT
1136 && texObj2D->WrapT==GL_REPEAT
1137 && texImg->Border==0
1138 && (format == MESA_FORMAT_RGB || format == MESA_FORMAT_RGBA)
1139 && minFilter == magFilter
1140 && ctx->Light.Model.ColorControl == GL_SINGLE_COLOR
1141 && ctx->Texture.Unit[0].EnvMode != GL_COMBINE_EXT) {
1142 if (ctx->Hint.PerspectiveCorrection==GL_FASTEST) {
1143 if (minFilter == GL_NEAREST
1144 && format == MESA_FORMAT_RGB
1145 && (envMode == GL_REPLACE || envMode == GL_DECAL)
1146 && ((swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)
1147 && ctx->Depth.Func == GL_LESS
1148 && ctx->Depth.Mask == GL_TRUE)
1149 || swrast->_RasterMask == TEXTURE_BIT)
1150 && ctx->Polygon.StippleFlag == GL_FALSE) {
1151 if (swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)) {
1152 USE(simple_z_textured_triangle);
1153 }
1154 else {
1155 USE(simple_textured_triangle);
1156 }
1157 }
1158 else {
1159 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1160 USE(general_textured_triangle);
1161 #else
1162 USE(affine_textured_triangle);
1163 #endif
1164 }
1165 }
1166 else {
1167 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1168 USE(general_textured_triangle);
1169 #else
1170 USE(persp_textured_triangle);
1171 #endif
1172 }
1173 }
1174 else {
1175 /* general case textured triangles */
1176 if (ctx->Texture._ReallyEnabled > TEXTURE0_ANY) {
1177 USE(multitextured_triangle);
1178 }
1179 else {
1180 USE(general_textured_triangle);
1181 }
1182 }
1183 }
1184 else {
1185 ASSERT(!ctx->Texture._ReallyEnabled);
1186 if (ctx->Light.ShadeModel==GL_SMOOTH) {
1187 /* smooth shaded, no texturing, stippled or some raster ops */
1188 if (rgbmode) {
1189 USE(smooth_rgba_triangle);
1190 }
1191 else {
1192 USE(smooth_ci_triangle);
1193 }
1194 }
1195 else {
1196 /* flat shaded, no texturing, stippled or some raster ops */
1197 if (rgbmode) {
1198 USE(flat_rgba_triangle);
1199 }
1200 else {
1201 USE(flat_ci_triangle);
1202 }
1203 }
1204 }
1205 }
1206 else if (ctx->RenderMode==GL_FEEDBACK) {
1207 USE(_mesa_feedback_triangle);
1208 }
1209 else {
1210 /* GL_SELECT mode */
1211 USE(_mesa_select_triangle);
1212 }
1213 }