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