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