257f06c24ddde76e03172da68485107cdd4a51a7
[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 = IntToFixed(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_FOG 1
802 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
803 #define INTERP_RGB 1
804 #define INTERP_ALPHA 1
805 #define INTERP_TEX 1
806
807 #define SETUP_CODE \
808 struct persp_info info; \
809 const struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
810 const struct gl_texture_object *obj = unit->Current2D; \
811 const GLint b = obj->BaseLevel; \
812 info.texture = (const GLchan *) obj->Image[0][b]->Data; \
813 info.twidth_log2 = obj->Image[0][b]->WidthLog2; \
814 info.smask = obj->Image[0][b]->Width - 1; \
815 info.tmask = obj->Image[0][b]->Height - 1; \
816 info.format = obj->Image[0][b]->Format; \
817 info.filter = obj->MinFilter; \
818 info.envmode = unit->EnvMode; \
819 \
820 if (info.envmode == GL_BLEND) { \
821 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
822 info.er = FloatToFixed(unit->EnvColor[RCOMP] * CHAN_MAXF); \
823 info.eg = FloatToFixed(unit->EnvColor[GCOMP] * CHAN_MAXF); \
824 info.eb = FloatToFixed(unit->EnvColor[BCOMP] * CHAN_MAXF); \
825 info.ea = FloatToFixed(unit->EnvColor[ACOMP] * CHAN_MAXF); \
826 } \
827 if (!info.texture) { \
828 /* this shouldn't happen */ \
829 return; \
830 } \
831 \
832 switch (info.format) { \
833 case GL_ALPHA: \
834 case GL_LUMINANCE: \
835 case GL_INTENSITY: \
836 info.tbytesline = obj->Image[0][b]->Width; \
837 break; \
838 case GL_LUMINANCE_ALPHA: \
839 info.tbytesline = obj->Image[0][b]->Width * 2; \
840 break; \
841 case GL_RGB: \
842 info.tbytesline = obj->Image[0][b]->Width * 3; \
843 break; \
844 case GL_RGBA: \
845 info.tbytesline = obj->Image[0][b]->Width * 4; \
846 break; \
847 default: \
848 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
849 return; \
850 } \
851 info.tsize = obj->Image[0][b]->Height * info.tbytesline;
852
853 #define RENDER_SPAN( span ) \
854 span.interpMask &= ~SPAN_RGBA; \
855 span.arrayMask |= SPAN_RGBA; \
856 fast_persp_span(ctx, &span, &info);
857
858 #include "s_tritemp.h"
859
860
861 #endif /* CHAN_BITS != GL_FLOAT */
862
863
864
865
866 /*
867 * Render a smooth-shaded, textured, RGBA triangle.
868 * Interpolate S,T,R with perspective correction, w/out mipmapping.
869 */
870 #define NAME general_textured_triangle
871 #define INTERP_Z 1
872 #define INTERP_W 1
873 #define INTERP_FOG 1
874 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
875 #define INTERP_RGB 1
876 #define INTERP_SPEC 1
877 #define INTERP_ALPHA 1
878 #define INTERP_TEX 1
879 #define RENDER_SPAN( span ) _swrast_write_texture_span(ctx, &span);
880 #include "s_tritemp.h"
881
882
883
884 /*
885 * This is the big one!
886 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates.
887 * Yup, it's slow.
888 */
889 #define NAME multitextured_triangle
890 #define INTERP_Z 1
891 #define INTERP_W 1
892 #define INTERP_FOG 1
893 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
894 #define INTERP_RGB 1
895 #define INTERP_ALPHA 1
896 #define INTERP_SPEC 1
897 #define INTERP_MULTITEX 1
898 #define RENDER_SPAN( span ) _swrast_write_texture_span(ctx, &span);
899 #include "s_tritemp.h"
900
901
902
903 /*
904 * Special tri function for occlusion testing
905 */
906 #define NAME occlusion_zless_triangle
907 #define DO_OCCLUSION_TEST
908 #define INTERP_Z 1
909 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
910 #define SETUP_CODE \
911 if (ctx->OcclusionResult && !ctx->Occlusion.Active) { \
912 return; \
913 }
914 #define RENDER_SPAN( span ) \
915 GLuint i; \
916 for (i = 0; i < span.end; i++) { \
917 GLdepth z = FixedToDepth(span.z); \
918 if (z < zRow[i]) { \
919 ctx->OcclusionResult = GL_TRUE; \
920 ctx->Occlusion.PassedCounter++; \
921 } \
922 span.z += span.zStep; \
923 }
924 #include "s_tritemp.h"
925
926
927
928 static void
929 nodraw_triangle( GLcontext *ctx,
930 const SWvertex *v0,
931 const SWvertex *v1,
932 const SWvertex *v2 )
933 {
934 (void) (ctx && v0 && v1 && v2);
935 }
936
937
938 /*
939 * This is used when separate specular color is enabled, but not
940 * texturing. We add the specular color to the primary color,
941 * draw the triangle, then restore the original primary color.
942 * Inefficient, but seldom needed.
943 */
944 void _swrast_add_spec_terms_triangle( GLcontext *ctx,
945 const SWvertex *v0,
946 const SWvertex *v1,
947 const SWvertex *v2 )
948 {
949 SWvertex *ncv0 = (SWvertex *)v0; /* drop const qualifier */
950 SWvertex *ncv1 = (SWvertex *)v1;
951 SWvertex *ncv2 = (SWvertex *)v2;
952 #if CHAN_TYPE == GL_FLOAT
953 GLfloat rSum, gSum, bSum;
954 #else
955 GLint rSum, gSum, bSum;
956 #endif
957 GLchan c[3][4];
958 /* save original colors */
959 COPY_CHAN4( c[0], ncv0->color );
960 COPY_CHAN4( c[1], ncv1->color );
961 COPY_CHAN4( c[2], ncv2->color );
962 /* sum v0 */
963 rSum = ncv0->color[0] + ncv0->specular[0];
964 gSum = ncv0->color[1] + ncv0->specular[1];
965 bSum = ncv0->color[2] + ncv0->specular[2];
966 ncv0->color[0] = MIN2(rSum, CHAN_MAX);
967 ncv0->color[1] = MIN2(gSum, CHAN_MAX);
968 ncv0->color[2] = MIN2(bSum, CHAN_MAX);
969 /* sum v1 */
970 rSum = ncv1->color[0] + ncv1->specular[0];
971 gSum = ncv1->color[1] + ncv1->specular[1];
972 bSum = ncv1->color[2] + ncv1->specular[2];
973 ncv1->color[0] = MIN2(rSum, CHAN_MAX);
974 ncv1->color[1] = MIN2(gSum, CHAN_MAX);
975 ncv1->color[2] = MIN2(bSum, CHAN_MAX);
976 /* sum v2 */
977 rSum = ncv2->color[0] + ncv2->specular[0];
978 gSum = ncv2->color[1] + ncv2->specular[1];
979 bSum = ncv2->color[2] + ncv2->specular[2];
980 ncv2->color[0] = MIN2(rSum, CHAN_MAX);
981 ncv2->color[1] = MIN2(gSum, CHAN_MAX);
982 ncv2->color[2] = MIN2(bSum, CHAN_MAX);
983 /* draw */
984 SWRAST_CONTEXT(ctx)->SpecTriangle( ctx, ncv0, ncv1, ncv2 );
985 /* restore original colors */
986 COPY_CHAN4( ncv0->color, c[0] );
987 COPY_CHAN4( ncv1->color, c[1] );
988 COPY_CHAN4( ncv2->color, c[2] );
989 }
990
991
992
993 #ifdef DEBUG
994
995 /* record the current triangle function name */
996 const char *_mesa_triFuncName = NULL;
997
998 #define USE(triFunc) \
999 do { \
1000 _mesa_triFuncName = #triFunc; \
1001 /*printf("%s\n", _mesa_triFuncName);*/ \
1002 swrast->Triangle = triFunc; \
1003 } while (0)
1004
1005 #else
1006
1007 #define USE(triFunc) swrast->Triangle = triFunc;
1008
1009 #endif
1010
1011
1012
1013
1014 /*
1015 * Determine which triangle rendering function to use given the current
1016 * rendering context.
1017 *
1018 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1019 * remove tests to this code.
1020 */
1021 void
1022 _swrast_choose_triangle( GLcontext *ctx )
1023 {
1024 SWcontext *swrast = SWRAST_CONTEXT(ctx);
1025 const GLboolean rgbmode = ctx->Visual.rgbMode;
1026
1027 if (ctx->Polygon.CullFlag &&
1028 ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) {
1029 USE(nodraw_triangle);
1030 return;
1031 }
1032
1033 if (ctx->RenderMode==GL_RENDER) {
1034
1035 if (ctx->Polygon.SmoothFlag) {
1036 _swrast_set_aa_triangle_function(ctx);
1037 ASSERT(swrast->Triangle);
1038 return;
1039 }
1040
1041 /* special case for occlusion testing */
1042 if ((ctx->Depth.OcclusionTest || ctx->Occlusion.Active) &&
1043 ctx->Depth.Test &&
1044 ctx->Depth.Mask == GL_FALSE &&
1045 ctx->Depth.Func == GL_LESS &&
1046 !ctx->Stencil.Enabled) {
1047 if ((rgbmode &&
1048 ctx->Color.ColorMask[0] == 0 &&
1049 ctx->Color.ColorMask[1] == 0 &&
1050 ctx->Color.ColorMask[2] == 0 &&
1051 ctx->Color.ColorMask[3] == 0)
1052 ||
1053 (!rgbmode && ctx->Color.IndexMask == 0)) {
1054 USE(occlusion_zless_triangle);
1055 return;
1056 }
1057 }
1058
1059 if (ctx->Texture._EnabledCoordUnits || ctx->FragmentProgram.Enabled) {
1060 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1061 const struct gl_texture_object *texObj2D;
1062 const struct gl_texture_image *texImg;
1063 GLenum minFilter, magFilter, envMode;
1064 GLint format;
1065 texObj2D = ctx->Texture.Unit[0].Current2D;
1066 texImg = texObj2D ? texObj2D->Image[0][texObj2D->BaseLevel] : NULL;
1067 format = texImg ? texImg->TexFormat->MesaFormat : -1;
1068 minFilter = texObj2D ? texObj2D->MinFilter : (GLenum) 0;
1069 magFilter = texObj2D ? texObj2D->MagFilter : (GLenum) 0;
1070 envMode = ctx->Texture.Unit[0].EnvMode;
1071
1072 /* First see if we can use an optimized 2-D texture function */
1073 if (ctx->Texture._EnabledCoordUnits == 1
1074 && !ctx->FragmentProgram.Enabled
1075 && ctx->Texture.Unit[0]._ReallyEnabled == TEXTURE_2D_BIT
1076 && texObj2D->WrapS==GL_REPEAT
1077 && texObj2D->WrapT==GL_REPEAT
1078 && texObj2D->_IsPowerOfTwo
1079 && texImg->Border==0
1080 && texImg->Width == texImg->RowStride
1081 && (format == MESA_FORMAT_RGB || format == MESA_FORMAT_RGBA)
1082 && minFilter == magFilter
1083 && ctx->Light.Model.ColorControl == GL_SINGLE_COLOR
1084 && ctx->Texture.Unit[0].EnvMode != GL_COMBINE_EXT) {
1085 if (ctx->Hint.PerspectiveCorrection==GL_FASTEST) {
1086 if (minFilter == GL_NEAREST
1087 && format == MESA_FORMAT_RGB
1088 && (envMode == GL_REPLACE || envMode == GL_DECAL)
1089 && ((swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)
1090 && ctx->Depth.Func == GL_LESS
1091 && ctx->Depth.Mask == GL_TRUE)
1092 || swrast->_RasterMask == TEXTURE_BIT)
1093 && ctx->Polygon.StippleFlag == GL_FALSE) {
1094 if (swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)) {
1095 USE(simple_z_textured_triangle);
1096 }
1097 else {
1098 USE(simple_textured_triangle);
1099 }
1100 }
1101 else {
1102 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1103 USE(general_textured_triangle);
1104 #else
1105 USE(affine_textured_triangle);
1106 #endif
1107 }
1108 }
1109 else {
1110 #if (CHAN_BITS == 16 || CHAN_BITS == 32)
1111 USE(general_textured_triangle);
1112 #else
1113 USE(persp_textured_triangle);
1114 #endif
1115 }
1116 }
1117 else {
1118 /* general case textured triangles */
1119 if (ctx->Texture._EnabledCoordUnits > 1) {
1120 USE(multitextured_triangle);
1121 }
1122 else {
1123 USE(general_textured_triangle);
1124 }
1125 }
1126 }
1127 else {
1128 ASSERT(!ctx->Texture._EnabledCoordUnits);
1129 if (ctx->Light.ShadeModel==GL_SMOOTH) {
1130 /* smooth shaded, no texturing, stippled or some raster ops */
1131 if (rgbmode) {
1132 USE(smooth_rgba_triangle);
1133 }
1134 else {
1135 USE(smooth_ci_triangle);
1136 }
1137 }
1138 else {
1139 /* flat shaded, no texturing, stippled or some raster ops */
1140 if (rgbmode) {
1141 USE(flat_rgba_triangle);
1142 }
1143 else {
1144 USE(flat_ci_triangle);
1145 }
1146 }
1147 }
1148 }
1149 else if (ctx->RenderMode==GL_FEEDBACK) {
1150 USE(_swrast_feedback_triangle);
1151 }
1152 else {
1153 /* GL_SELECT mode */
1154 USE(_swrast_select_triangle);
1155 }
1156 }