c1aa1facf14e040559a4a7e356ede42b8cbfbc27
[mesa.git] / src / mesa / swrast / s_triangle.c
1 /* $Id: s_triangle.c,v 1.37 2001/09/13 22:12:54 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 /*
29 * When the device driver doesn't implement triangle rasterization it
30 * can hook in _swrast_Triangle, which eventually calls one of these
31 * functions to draw triangles.
32 */
33
34 #include "glheader.h"
35 #include "context.h"
36 #include "colormac.h"
37 #include "macros.h"
38 #include "mem.h"
39 #include "mmath.h"
40 #include "texformat.h"
41 #include "teximage.h"
42 #include "texstate.h"
43
44 #include "s_aatriangle.h"
45 #include "s_context.h"
46 #include "s_depth.h"
47 #include "s_feedback.h"
48 #include "s_span.h"
49 #include "s_triangle.h"
50 #include "s_trispan.h"
51
52
53
54 GLboolean _mesa_cull_triangle( GLcontext *ctx,
55 const SWvertex *v0,
56 const SWvertex *v1,
57 const SWvertex *v2 )
58 {
59 GLfloat ex = v1->win[0] - v0->win[0];
60 GLfloat ey = v1->win[1] - v0->win[1];
61 GLfloat fx = v2->win[0] - v0->win[0];
62 GLfloat fy = v2->win[1] - v0->win[1];
63 GLfloat c = ex*fy-ey*fx;
64
65 if (c * SWRAST_CONTEXT(ctx)->_backface_sign > 0)
66 return 0;
67
68 return 1;
69 }
70
71
72
73 /*
74 * Render a flat-shaded color index triangle.
75 */
76 static void flat_ci_triangle( GLcontext *ctx,
77 const SWvertex *v0,
78 const SWvertex *v1,
79 const SWvertex *v2 )
80 {
81 #define INTERP_Z 1
82 #define INTERP_FOG 1
83
84 #define RENDER_SPAN( span ) \
85 GLdepth zSpan[MAX_WIDTH]; \
86 GLfloat fogSpan[MAX_WIDTH]; \
87 GLuint i; \
88 for (i = 0; i < span.count; i++) { \
89 zSpan[i] = FixedToDepth(span.z); \
90 span.z += span.zStep; \
91 fogSpan[i] = span.fog; \
92 span.fog += span.fogStep; \
93 } \
94 _mesa_write_monoindex_span(ctx, span.count, span.x, span.y, \
95 zSpan, fogSpan, v0->index, NULL, GL_POLYGON );
96
97 #include "s_tritemp.h"
98 }
99
100
101
102 /*
103 * Render a smooth-shaded color index triangle.
104 */
105 static void smooth_ci_triangle( GLcontext *ctx,
106 const SWvertex *v0,
107 const SWvertex *v1,
108 const SWvertex *v2 )
109 {
110 #define INTERP_Z 1
111 #define INTERP_FOG 1
112 #define INTERP_INDEX 1
113
114 #define RENDER_SPAN( span ) \
115 GLdepth zSpan[MAX_WIDTH]; \
116 GLfloat fogSpan[MAX_WIDTH]; \
117 GLuint indexSpan[MAX_WIDTH]; \
118 GLuint i; \
119 for (i = 0; i < span.count; i++) { \
120 zSpan[i] = FixedToDepth(span.z); \
121 span.z += span.zStep; \
122 indexSpan[i] = FixedToInt(span.index); \
123 span.index += span.indexStep; \
124 fogSpan[i] = span.fog; \
125 span.fog += span.fogStep; \
126 } \
127 _mesa_write_index_span(ctx, span.count, span.x, span.y, \
128 zSpan, fogSpan, indexSpan, NULL, GL_POLYGON);
129
130 #include "s_tritemp.h"
131 }
132
133
134
135 /*
136 * Render a flat-shaded RGBA triangle.
137 */
138 static void flat_rgba_triangle( GLcontext *ctx,
139 const SWvertex *v0,
140 const SWvertex *v1,
141 const SWvertex *v2 )
142 {
143 #define INTERP_Z 1
144 #define INTERP_FOG 1
145 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
146
147 #define RENDER_SPAN( span ) \
148 GLdepth zSpan[MAX_WIDTH]; \
149 GLfloat fogSpan[MAX_WIDTH]; \
150 GLuint i; \
151 for (i = 0; i < span.count; i++) { \
152 zSpan[i] = FixedToDepth(span.z); \
153 span.z += span.zStep; \
154 fogSpan[i] = span.fog; \
155 span.fog += span.fogStep; \
156 } \
157 _mesa_write_monocolor_span(ctx, span.count, span.x, span.y, zSpan, \
158 fogSpan, v2->color, NULL, GL_POLYGON );
159
160 #include "s_tritemp.h"
161
162 ASSERT(!ctx->Texture._ReallyEnabled); /* texturing must be off */
163 ASSERT(ctx->Light.ShadeModel==GL_FLAT);
164 }
165
166
167
168 /*
169 * Render a smooth-shaded RGBA triangle.
170 */
171 static void smooth_rgba_triangle( GLcontext *ctx,
172 const SWvertex *v0,
173 const SWvertex *v1,
174 const SWvertex *v2 )
175 {
176
177 #define INTERP_Z 1
178 #define INTERP_FOG 1
179 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
180 #define INTERP_RGB 1
181 #define INTERP_ALPHA 1
182
183 #define RENDER_SPAN( span ) \
184 GLdepth zSpan[MAX_WIDTH]; \
185 GLchan rgbaSpan[MAX_WIDTH][4]; \
186 GLfloat fogSpan[MAX_WIDTH]; \
187 GLuint i; \
188 for (i = 0; i < span.count; i++) { \
189 rgbaSpan[i][RCOMP] = FixedToChan(span.red); \
190 rgbaSpan[i][GCOMP] = FixedToChan(span.green); \
191 rgbaSpan[i][BCOMP] = FixedToChan(span.blue); \
192 rgbaSpan[i][ACOMP] = FixedToChan(span.alpha); \
193 span.red += span.redStep; \
194 span.green += span.greenStep; \
195 span.blue += span.blueStep; \
196 span.alpha += span.alphaStep; \
197 zSpan[i] = FixedToDepth(span.z); \
198 span.z += span.zStep; \
199 fogSpan[i] = span.fog; \
200 span.fog += span.fogStep; \
201 } \
202 _mesa_write_rgba_span(ctx, span.count, span.x, span.y, \
203 (CONST GLdepth *) zSpan, \
204 fogSpan, rgbaSpan, NULL, GL_POLYGON);
205
206 #include "s_tritemp.h"
207
208 ASSERT(!ctx->Texture._ReallyEnabled); /* texturing must be off */
209 ASSERT(ctx->Light.ShadeModel==GL_SMOOTH);
210 }
211
212
213 /*
214 * Render an RGB, GL_DECAL, textured triangle.
215 * Interpolate S,T only w/out mipmapping or perspective correction.
216 *
217 * No fog.
218 */
219 static void simple_textured_triangle( GLcontext *ctx,
220 const SWvertex *v0,
221 const SWvertex *v1,
222 const SWvertex *v2 )
223 {
224 #define INTERP_INT_TEX 1
225 #define S_SCALE twidth
226 #define T_SCALE theight
227
228 #define SETUP_CODE \
229 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
230 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
231 GLint b = obj->BaseLevel; \
232 const GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
233 const GLfloat theight = (GLfloat) obj->Image[b]->Height; \
234 const GLint twidth_log2 = obj->Image[b]->WidthLog2; \
235 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
236 const GLint smask = obj->Image[b]->Width - 1; \
237 const GLint tmask = obj->Image[b]->Height - 1; \
238 if (!texture) { \
239 /* this shouldn't happen */ \
240 return; \
241 }
242
243 #define RENDER_SPAN( span ) \
244 GLchan rgbSpan[MAX_WIDTH][3]; \
245 GLuint i; \
246 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
247 span.intTex[1] -= FIXED_HALF; \
248 for (i = 0; i < span.count; i++) { \
249 GLint s = FixedToInt(span.intTex[0]) & smask; \
250 GLint t = FixedToInt(span.intTex[1]) & tmask; \
251 GLint pos = (t << twidth_log2) + s; \
252 pos = pos + pos + pos; /* multiply by 3 */ \
253 rgbSpan[i][RCOMP] = texture[pos]; \
254 rgbSpan[i][GCOMP] = texture[pos+1]; \
255 rgbSpan[i][BCOMP] = texture[pos+2]; \
256 span.intTex[0] += span.intTexStep[0]; \
257 span.intTex[1] += span.intTexStep[1]; \
258 } \
259 (*swrast->Driver.WriteRGBSpan)(ctx, span.count, span.x, span.y, \
260 (CONST GLchan (*)[3]) rgbSpan, NULL );
261
262 #include "s_tritemp.h"
263 }
264
265
266 /*
267 * Render an RGB, GL_DECAL, textured triangle.
268 * Interpolate S,T, GL_LESS depth test, w/out mipmapping or
269 * perspective correction.
270 *
271 * No fog.
272 */
273 static void simple_z_textured_triangle( GLcontext *ctx,
274 const SWvertex *v0,
275 const SWvertex *v1,
276 const SWvertex *v2 )
277 {
278 #define INTERP_Z 1
279 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
280 #define INTERP_INT_TEX 1
281 #define S_SCALE twidth
282 #define T_SCALE theight
283
284 #define SETUP_CODE \
285 SWcontext *swrast = SWRAST_CONTEXT(ctx); \
286 struct gl_texture_object *obj = ctx->Texture.Unit[0].Current2D; \
287 GLint b = obj->BaseLevel; \
288 GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
289 GLfloat theight = (GLfloat) obj->Image[b]->Height; \
290 GLint twidth_log2 = obj->Image[b]->WidthLog2; \
291 const GLchan *texture = (const GLchan *) obj->Image[b]->Data; \
292 GLint smask = obj->Image[b]->Width - 1; \
293 GLint tmask = obj->Image[b]->Height - 1; \
294 if (!texture) { \
295 /* this shouldn't happen */ \
296 return; \
297 }
298
299 #define RENDER_SPAN( span ) \
300 GLchan rgbSpan[MAX_WIDTH][3]; \
301 GLubyte mask[MAX_WIDTH]; \
302 GLuint i; \
303 span.intTex[0] -= FIXED_HALF; /* off-by-one error? */ \
304 span.intTex[1] -= FIXED_HALF; \
305 for (i = 0; i < span.count; i++) { \
306 const GLdepth z = FixedToDepth(span.z); \
307 if (z < zRow[i]) { \
308 GLint s = FixedToInt(span.intTex[0]) & smask; \
309 GLint t = FixedToInt(span.intTex[1]) & tmask; \
310 GLint pos = (t << twidth_log2) + s; \
311 pos = pos + pos + pos; /* multiply by 3 */ \
312 rgbSpan[i][RCOMP] = texture[pos]; \
313 rgbSpan[i][GCOMP] = texture[pos+1]; \
314 rgbSpan[i][BCOMP] = texture[pos+2]; \
315 zRow[i] = z; \
316 mask[i] = 1; \
317 } \
318 else { \
319 mask[i] = 0; \
320 } \
321 span.intTex[0] += span.intTexStep[0]; \
322 span.intTex[1] += span.intTexStep[1]; \
323 span.z += span.zStep; \
324 } \
325 (*swrast->Driver.WriteRGBSpan)(ctx, span.count, span.x, span.y, \
326 (CONST GLchan (*)[3]) rgbSpan, mask );
327
328 #include "s_tritemp.h"
329 }
330
331
332 #if CHAN_TYPE != GL_FLOAT
333
334 struct affine_info
335 {
336 GLenum filter;
337 GLenum format;
338 GLenum envmode;
339 GLint smask, tmask;
340 GLint twidth_log2;
341 const GLchan *texture;
342 GLchan er, eg, eb, ea;
343 GLint tbytesline, tsize;
344 GLint fixedToDepthShift;
345 };
346
347 static void
348 affine_span(GLcontext *ctx, struct triangle_span *span,
349 struct affine_info *info)
350 {
351 GLchan tmp_col[4];
352
353 /* Instead of defining a function for each mode, a test is done
354 * between the outer and inner loops. This is to reduce code size
355 * and complexity. Observe that an optimizing compiler kills
356 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
357 */
358
359 #define NEAREST_RGB \
360 tmp_col[RCOMP] = tex00[RCOMP]; \
361 tmp_col[GCOMP] = tex00[GCOMP]; \
362 tmp_col[BCOMP] = tex00[BCOMP]; \
363 tmp_col[ACOMP] = CHAN_MAX
364
365 #define LINEAR_RGB \
366 tmp_col[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
367 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT; \
368 tmp_col[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
369 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT; \
370 tmp_col[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
371 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT; \
372 tmp_col[ACOMP] = CHAN_MAX
373
374 #define NEAREST_RGBA COPY_CHAN4(tmp_col, tex00)
375
376 #define LINEAR_RGBA \
377 tmp_col[RCOMP] = (ti * (si * tex00[0] + sf * tex01[0]) + \
378 tf * (si * tex10[0] + sf * tex11[0])) >> 2 * FIXED_SHIFT;\
379 tmp_col[GCOMP] = (ti * (si * tex00[1] + sf * tex01[1]) + \
380 tf * (si * tex10[1] + sf * tex11[1])) >> 2 * FIXED_SHIFT;\
381 tmp_col[BCOMP] = (ti * (si * tex00[2] + sf * tex01[2]) + \
382 tf * (si * tex10[2] + sf * tex11[2])) >> 2 * FIXED_SHIFT;\
383 tmp_col[ACOMP] = (ti * (si * tex00[3] + sf * tex01[3]) + \
384 tf * (si * tex10[3] + sf * tex11[3])) >> 2 * FIXED_SHIFT
385
386 #define MODULATE \
387 dest[RCOMP] = span->red * (tmp_col[RCOMP] + 1u) >> (FIXED_SHIFT + 8); \
388 dest[GCOMP] = span->green * (tmp_col[GCOMP] + 1u) >> (FIXED_SHIFT + 8); \
389 dest[BCOMP] = span->blue * (tmp_col[BCOMP] + 1u) >> (FIXED_SHIFT + 8); \
390 dest[ACOMP] = span->alpha * (tmp_col[ACOMP] + 1u) >> (FIXED_SHIFT + 8)
391
392 #define DECAL \
393 dest[RCOMP] = ((CHAN_MAX - tmp_col[ACOMP]) * span->red + \
394 ((tmp_col[ACOMP] + 1) * tmp_col[RCOMP] << FIXED_SHIFT)) \
395 >> (FIXED_SHIFT + 8); \
396 dest[GCOMP] = ((CHAN_MAX - tmp_col[ACOMP]) * span->green + \
397 ((tmp_col[ACOMP] + 1) * tmp_col[GCOMP] << FIXED_SHIFT)) \
398 >> (FIXED_SHIFT + 8); \
399 dest[BCOMP] = ((CHAN_MAX - tmp_col[ACOMP]) * span->blue + \
400 ((tmp_col[ACOMP] + 1) * tmp_col[BCOMP] << FIXED_SHIFT)) \
401 >> (FIXED_SHIFT + 8); \
402 dest[ACOMP] = FixedToInt(span->alpha)
403
404 #define BLEND \
405 dest[RCOMP] = ((CHAN_MAX - tmp_col[RCOMP]) * span->red \
406 + (tmp_col[RCOMP] + 1) * info->er) >> (FIXED_SHIFT + 8); \
407 dest[GCOMP] = ((CHAN_MAX - tmp_col[GCOMP]) * span->green \
408 + (tmp_col[GCOMP] + 1) * info->eg) >> (FIXED_SHIFT + 8); \
409 dest[BCOMP] = ((CHAN_MAX - tmp_col[BCOMP]) * span->blue \
410 + (tmp_col[BCOMP] + 1) * info->eb) >> (FIXED_SHIFT + 8); \
411 dest[ACOMP] = span->alpha * (tmp_col[ACOMP] + 1) >> (FIXED_SHIFT + 8)
412
413 #define REPLACE COPY_CHAN4(dest, tmp_col)
414
415 #define I2CHAN_CLAMP(I) (GLchan) ((I) & CHAN_MAX)
416 /* equivalent to '(GLchan) MIN2((I),CHAN_MAX)' */
417
418 #define ADD \
419 dest[RCOMP] = MIN2(((span->red << 8) + \
420 (tmp_col[RCOMP] + 1) * info->er) \
421 >> (FIXED_SHIFT + 8), CHAN_MAX); \
422 dest[GCOMP] = MIN2(((span->green << 8) + \
423 (tmp_col[GCOMP] + 1) * info->eg) \
424 >> (FIXED_SHIFT + 8), CHAN_MAX); \
425 dest[RCOMP] = MIN2(((span->blue << 8) + \
426 (tmp_col[BCOMP] + 1) * info->eb) \
427 >> (FIXED_SHIFT + 8), CHAN_MAX); \
428 dest[ACOMP] = span->alpha * (tmp_col[ACOMP] + 1) >> (FIXED_SHIFT + 8)
429
430 /* shortcuts */
431
432 #define NEAREST_RGB_REPLACE NEAREST_RGB;REPLACE
433
434 #define NEAREST_RGBA_REPLACE COPY_CHAN4(dest, tex00)
435
436 #define SPAN_NEAREST(DO_TEX,COMP) \
437 for (i = 0; i < span->count; i++) { \
438 /* Isn't it necessary to use FixedFloor below?? */ \
439 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
440 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
441 GLint pos = (t << info->twidth_log2) + s; \
442 const GLchan *tex00 = info->texture + COMP * pos; \
443 zspan[i] = FixedToDepth(span->z); \
444 fogspan[i] = span->fog; \
445 DO_TEX; \
446 span->fog += span->fogStep; \
447 span->z += span->zStep; \
448 span->red += span->redStep; \
449 span->green += span->greenStep; \
450 span->blue += span->blueStep; \
451 span->alpha += span->alphaStep; \
452 span->intTex[0] += span->intTexStep[0]; \
453 span->intTex[1] += span->intTexStep[1]; \
454 dest += 4; \
455 }
456
457 #define SPAN_LINEAR(DO_TEX,COMP) \
458 for (i = 0; i < span->count; i++) { \
459 /* Isn't it necessary to use FixedFloor below?? */ \
460 GLint s = FixedToInt(span->intTex[0]) & info->smask; \
461 GLint t = FixedToInt(span->intTex[1]) & info->tmask; \
462 GLfixed sf = span->intTex[0] & FIXED_FRAC_MASK; \
463 GLfixed tf = span->intTex[1] & FIXED_FRAC_MASK; \
464 GLfixed si = FIXED_FRAC_MASK - sf; \
465 GLfixed ti = FIXED_FRAC_MASK - tf; \
466 GLint pos = (t << info->twidth_log2) + s; \
467 const GLchan *tex00 = info->texture + COMP * pos; \
468 const GLchan *tex10 = tex00 + info->tbytesline; \
469 const GLchan *tex01 = tex00 + COMP; \
470 const GLchan *tex11 = tex10 + COMP; \
471 (void) ti; \
472 (void) si; \
473 if (t == info->tmask) { \
474 tex10 -= info->tsize; \
475 tex11 -= info->tsize; \
476 } \
477 if (s == info->smask) { \
478 tex01 -= info->tbytesline; \
479 tex11 -= info->tbytesline; \
480 } \
481 zspan[i] = FixedToDepth(span->z); \
482 fogspan[i] = span->fog; \
483 DO_TEX; \
484 span->fog += span->fogStep; \
485 span->z += span->zStep; \
486 span->red += span->redStep; \
487 span->green += span->greenStep; \
488 span->blue += span->blueStep; \
489 span->alpha += span->alphaStep; \
490 span->intTex[0] += span->intTexStep[0]; \
491 span->intTex[1] += span->intTexStep[1]; \
492 dest += 4; \
493 }
494
495 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
496
497 GLuint i;
498 GLdepth zspan[MAX_WIDTH];
499 GLfloat fogspan[MAX_WIDTH];
500 GLchan rgba[MAX_WIDTH][4];
501 GLchan *dest = rgba[0];
502 const GLint fixedToDepthShift = info->fixedToDepthShift;
503
504 span->intTex[0] -= FIXED_HALF;
505 span->intTex[1] -= FIXED_HALF;
506 switch (info->filter) {
507 case GL_NEAREST:
508 switch (info->format) {
509 case GL_RGB:
510 switch (info->envmode) {
511 case GL_MODULATE:
512 SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
513 break;
514 case GL_DECAL:
515 case GL_REPLACE:
516 SPAN_NEAREST(NEAREST_RGB_REPLACE,3);
517 break;
518 case GL_BLEND:
519 SPAN_NEAREST(NEAREST_RGB;BLEND,3);
520 break;
521 case GL_ADD:
522 SPAN_NEAREST(NEAREST_RGB;ADD,3);
523 break;
524 default:
525 abort();
526 }
527 break;
528 case GL_RGBA:
529 switch(info->envmode) {
530 case GL_MODULATE:
531 SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
532 break;
533 case GL_DECAL:
534 SPAN_NEAREST(NEAREST_RGBA;DECAL,4);
535 break;
536 case GL_BLEND:
537 SPAN_NEAREST(NEAREST_RGBA;BLEND,4);
538 break;
539 case GL_ADD:
540 SPAN_NEAREST(NEAREST_RGBA;ADD,4);
541 break;
542 case GL_REPLACE:
543 SPAN_NEAREST(NEAREST_RGBA_REPLACE,4);
544 break;
545 default:
546 abort();
547 }
548 break;
549 }
550 break;
551
552 case GL_LINEAR:
553 span->intTex[0] -= FIXED_HALF;
554 span->intTex[1] -= FIXED_HALF;
555 switch (info->format) {
556 case GL_RGB:
557 switch (info->envmode) {
558 case GL_MODULATE:
559 SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
560 break;
561 case GL_DECAL:
562 case GL_REPLACE:
563 SPAN_LINEAR(LINEAR_RGB;REPLACE,3);
564 break;
565 case GL_BLEND:
566 SPAN_LINEAR(LINEAR_RGB;BLEND,3);
567 break;
568 case GL_ADD:
569 SPAN_LINEAR(LINEAR_RGB;ADD,3);
570 break;
571 default:
572 abort();
573 }
574 break;
575 case GL_RGBA:
576 switch (info->envmode) {
577 case GL_MODULATE:
578 SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
579 break;
580 case GL_DECAL:
581 SPAN_LINEAR(LINEAR_RGBA;DECAL,4);
582 break;
583 case GL_BLEND:
584 SPAN_LINEAR(LINEAR_RGBA;BLEND,4);
585 break;
586 case GL_ADD:
587 SPAN_LINEAR(LINEAR_RGBA;ADD,4);
588 break;
589 case GL_REPLACE:
590 SPAN_LINEAR(LINEAR_RGBA;REPLACE,4);
591 break;
592 default:
593 abort();
594 } break;
595 }
596 break;
597 }
598 _mesa_write_rgba_span(ctx, span->count, span->x, span->y,
599 zspan, fogspan, rgba, NULL, GL_POLYGON);
600
601 #undef SPAN_NEAREST
602 #undef SPAN_LINEAR
603 #undef FixedToDepth
604 }
605
606
607
608 /*
609 * Render an RGB/RGBA textured triangle without perspective correction.
610 */
611 static void affine_textured_triangle( GLcontext *ctx,
612 const SWvertex *v0,
613 const SWvertex *v1,
614 const SWvertex *v2 )
615 {
616 #define INTERP_Z 1
617 #define INTERP_FOG 1
618 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
619 #define INTERP_RGB 1
620 #define INTERP_ALPHA 1
621 #define INTERP_INT_TEX 1
622 #define S_SCALE twidth
623 #define T_SCALE theight
624
625 #define SETUP_CODE \
626 struct affine_info info; \
627 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
628 struct gl_texture_object *obj = unit->Current2D; \
629 GLint b = obj->BaseLevel; \
630 GLfloat twidth = (GLfloat) obj->Image[b]->Width; \
631 GLfloat theight = (GLfloat) obj->Image[b]->Height; \
632 info.fixedToDepthShift = ctx->Visual.depthBits <= 16 ? FIXED_SHIFT : 0;\
633 info.texture = (const GLchan *) obj->Image[b]->Data; \
634 info.twidth_log2 = obj->Image[b]->WidthLog2; \
635 info.smask = obj->Image[b]->Width - 1; \
636 info.tmask = obj->Image[b]->Height - 1; \
637 info.format = obj->Image[b]->Format; \
638 info.filter = obj->MinFilter; \
639 info.envmode = unit->EnvMode; \
640 \
641 if (info.envmode == GL_BLEND) { \
642 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
643 info.er = FloatToFixed(unit->EnvColor[RCOMP]); \
644 info.eg = FloatToFixed(unit->EnvColor[GCOMP]); \
645 info.eb = FloatToFixed(unit->EnvColor[BCOMP]); \
646 info.ea = FloatToFixed(unit->EnvColor[ACOMP]); \
647 } \
648 if (!info.texture) { \
649 /* this shouldn't happen */ \
650 return; \
651 } \
652 \
653 switch (info.format) { \
654 case GL_ALPHA: \
655 case GL_LUMINANCE: \
656 case GL_INTENSITY: \
657 info.tbytesline = obj->Image[b]->Width; \
658 break; \
659 case GL_LUMINANCE_ALPHA: \
660 info.tbytesline = obj->Image[b]->Width * 2; \
661 break; \
662 case GL_RGB: \
663 info.tbytesline = obj->Image[b]->Width * 3; \
664 break; \
665 case GL_RGBA: \
666 info.tbytesline = obj->Image[b]->Width * 4; \
667 break; \
668 default: \
669 _mesa_problem(NULL, "Bad texture format in affine_texture_triangle");\
670 return; \
671 } \
672 info.tsize = obj->Image[b]->Height * info.tbytesline;
673
674 #define RENDER_SPAN( span ) affine_span(ctx, &span, &info);
675
676 #include "s_tritemp.h"
677
678 }
679
680
681
682 struct persp_info
683 {
684 GLenum filter;
685 GLenum format;
686 GLenum envmode;
687 GLint smask, tmask;
688 GLint twidth_log2;
689 const GLchan *texture;
690 GLchan er, eg, eb, ea;
691 GLint tbytesline, tsize;
692 GLint fixedToDepthShift;
693 };
694
695
696 static void
697 fast_persp_span(GLcontext *ctx, struct triangle_span *span,
698 struct persp_info *info)
699 {
700 GLchan tmp_col[4];
701
702 /* Instead of defining a function for each mode, a test is done
703 * between the outer and inner loops. This is to reduce code size
704 * and complexity. Observe that an optimizing compiler kills
705 * unused variables (for instance tf,sf,ti,si in case of GL_NEAREST).
706 */
707 #define SPAN_NEAREST(DO_TEX,COMP) \
708 for (i = 0; i < span->count; i++) { \
709 GLdouble invQ = tex_coord[2] ? \
710 (1.0 / tex_coord[2]) : 1.0; \
711 GLfloat s_tmp = tex_coord[0] * invQ; \
712 GLfloat t_tmp = tex_coord[1] * invQ; \
713 GLint s = IFLOOR(s_tmp) & info->smask; \
714 GLint t = IFLOOR(t_tmp) & info->tmask; \
715 GLint pos = (t << info->twidth_log2) + s; \
716 const GLchan *tex00 = info->texture + COMP * pos; \
717 zspan[i] = FixedToDepth(span->z); \
718 fogspan[i] = span->fog; \
719 DO_TEX; \
720 span->fog += span->fogStep; \
721 span->z += span->zStep; \
722 span->red += span->redStep; \
723 span->green += span->greenStep; \
724 span->blue += span->blueStep; \
725 span->alpha += span->alphaStep; \
726 tex_coord[0] += tex_step[0]; \
727 tex_coord[1] += tex_step[1]; \
728 tex_coord[2] += tex_step[2]; \
729 dest += 4; \
730 }
731
732 #define SPAN_LINEAR(DO_TEX,COMP) \
733 for (i = 0; i < span->count; i++) { \
734 GLdouble invQ = tex_coord[2] ? \
735 (1.0 / tex_coord[2]) : 1.0; \
736 GLfloat s_tmp = tex_coord[0] * invQ; \
737 GLfloat t_tmp = tex_coord[1] * invQ; \
738 GLfixed s_fix = FloatToFixed(s_tmp) - FIXED_HALF; \
739 GLfixed t_fix = FloatToFixed(t_tmp) - FIXED_HALF; \
740 GLint s = FixedToInt(FixedFloor(s_fix)) & info->smask; \
741 GLint t = FixedToInt(FixedFloor(t_fix)) & info->tmask; \
742 GLfixed sf = s_fix & FIXED_FRAC_MASK; \
743 GLfixed tf = t_fix & FIXED_FRAC_MASK; \
744 GLfixed si = FIXED_FRAC_MASK - sf; \
745 GLfixed ti = FIXED_FRAC_MASK - tf; \
746 GLint pos = (t << info->twidth_log2) + s; \
747 const GLchan *tex00 = info->texture + COMP * pos; \
748 const GLchan *tex10 = tex00 + info->tbytesline; \
749 const GLchan *tex01 = tex00 + COMP; \
750 const GLchan *tex11 = tex10 + COMP; \
751 (void) ti; \
752 (void) si; \
753 if (t == info->tmask) { \
754 tex10 -= info->tsize; \
755 tex11 -= info->tsize; \
756 } \
757 if (s == info->smask) { \
758 tex01 -= info->tbytesline; \
759 tex11 -= info->tbytesline; \
760 } \
761 zspan[i] = FixedToDepth(span->z); \
762 fogspan[i] = span->fog; \
763 DO_TEX; \
764 span->fog += span->fogStep; \
765 span->z += span->zStep; \
766 span->red += span->redStep; \
767 span->green += span->greenStep; \
768 span->blue += span->blueStep; \
769 span->alpha += span->alphaStep; \
770 tex_coord[0] += tex_step[0]; \
771 tex_coord[1] += tex_step[1]; \
772 tex_coord[2] += tex_step[2]; \
773 dest += 4; \
774 }
775
776 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
777
778 GLuint i;
779 GLdepth zspan[MAX_WIDTH];
780 GLfloat tex_coord[3], tex_step[3];
781 GLfloat fogspan[MAX_WIDTH];
782 GLchan rgba[MAX_WIDTH][4];
783 GLchan *dest = rgba[0];
784 const GLint fixedToDepthShift = info->fixedToDepthShift;
785
786 tex_coord[0] = span->tex[0][0] * (info->smask + 1),
787 tex_step[0] = span->texStep[0][0] * (info->smask + 1);
788 tex_coord[1] = span->tex[0][1] * (info->tmask + 1),
789 tex_step[1] = span->texStep[0][1] * (info->tmask + 1);
790 /* span->tex[0][2] only if 3D-texturing, here only 2D */
791 tex_coord[2] = span->tex[0][3],
792 tex_step[2] = span->texStep[0][3];
793
794 switch (info->filter) {
795 case GL_NEAREST:
796 switch (info->format) {
797 case GL_RGB:
798 switch (info->envmode) {
799 case GL_MODULATE:
800 SPAN_NEAREST(NEAREST_RGB;MODULATE,3);
801 break;
802 case GL_DECAL:
803 case GL_REPLACE:
804 SPAN_NEAREST(NEAREST_RGB_REPLACE,3);
805 break;
806 case GL_BLEND:
807 SPAN_NEAREST(NEAREST_RGB;BLEND,3);
808 break;
809 case GL_ADD:
810 SPAN_NEAREST(NEAREST_RGB;ADD,3);
811 break;
812 default:
813 abort();
814 }
815 break;
816 case GL_RGBA:
817 switch(info->envmode) {
818 case GL_MODULATE:
819 SPAN_NEAREST(NEAREST_RGBA;MODULATE,4);
820 break;
821 case GL_DECAL:
822 SPAN_NEAREST(NEAREST_RGBA;DECAL,4);
823 break;
824 case GL_BLEND:
825 SPAN_NEAREST(NEAREST_RGBA;BLEND,4);
826 break;
827 case GL_ADD:
828 SPAN_NEAREST(NEAREST_RGBA;ADD,4);
829 break;
830 case GL_REPLACE:
831 SPAN_NEAREST(NEAREST_RGBA_REPLACE,4);
832 break;
833 default:
834 abort();
835 }
836 break;
837 }
838 break;
839
840 case GL_LINEAR:
841 switch (info->format) {
842 case GL_RGB:
843 switch (info->envmode) {
844 case GL_MODULATE:
845 SPAN_LINEAR(LINEAR_RGB;MODULATE,3);
846 break;
847 case GL_DECAL:
848 case GL_REPLACE:
849 SPAN_LINEAR(LINEAR_RGB;REPLACE,3);
850 break;
851 case GL_BLEND:
852 SPAN_LINEAR(LINEAR_RGB;BLEND,3);
853 break;
854 case GL_ADD:
855 SPAN_LINEAR(LINEAR_RGB;ADD,3);
856 break;
857 default:
858 abort();
859 }
860 break;
861 case GL_RGBA:
862 switch (info->envmode) {
863 case GL_MODULATE:
864 SPAN_LINEAR(LINEAR_RGBA;MODULATE,4);
865 break;
866 case GL_DECAL:
867 SPAN_LINEAR(LINEAR_RGBA;DECAL,4);
868 break;
869 case GL_BLEND:
870 SPAN_LINEAR(LINEAR_RGBA;BLEND,4);
871 break;
872 case GL_ADD:
873 SPAN_LINEAR(LINEAR_RGBA;ADD,4);
874 break;
875 case GL_REPLACE:
876 SPAN_LINEAR(LINEAR_RGBA;REPLACE,4);
877 break;
878 default:
879 abort();
880 }
881 break;
882 }
883 break;
884 }
885 /* This does not seem to be necessary, but I don't know !! */
886 /* span->tex[0][0] = tex_coord[0] / (info->smask + 1),
887 span->tex[0][1] = tex_coord[1] / (info->tmask + 1),*/
888 /* span->tex[0][2] only if 3D-texturing, here only 2D */
889 /* span->tex[0][3] = tex_coord[2]; */
890
891 _mesa_write_rgba_span(ctx, span->count, span->x, span->y,
892 zspan, fogspan, rgba, NULL, GL_POLYGON);
893
894
895 #undef SPAN_NEAREST
896 #undef SPAN_LINEAR
897 #undef FixedToDepth
898 }
899
900
901 /*
902 * Render an perspective corrected RGB/RGBA textured triangle.
903 * The Q (aka V in Mesa) coordinate must be zero such that the divide
904 * by interpolated Q/W comes out right.
905 *
906 */
907 static void persp_textured_triangle( GLcontext *ctx,
908 const SWvertex *v0,
909 const SWvertex *v1,
910 const SWvertex *v2 )
911 {
912 #define INTERP_Z 1
913 #define INTERP_FOG 1
914 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
915 #define INTERP_RGB 1
916 #define INTERP_ALPHA 1
917 #define INTERP_TEX 1
918
919 #define SETUP_CODE \
920 struct persp_info info; \
921 struct gl_texture_unit *unit = ctx->Texture.Unit+0; \
922 struct gl_texture_object *obj = unit->Current2D; \
923 GLint b = obj->BaseLevel; \
924 info.fixedToDepthShift = ctx->Visual.depthBits <= 16 ? FIXED_SHIFT : 0;\
925 info.texture = (const GLchan *) obj->Image[b]->Data; \
926 info.twidth_log2 = obj->Image[b]->WidthLog2; \
927 info.smask = obj->Image[b]->Width - 1; \
928 info.tmask = obj->Image[b]->Height - 1; \
929 info.format = obj->Image[b]->Format; \
930 info.filter = obj->MinFilter; \
931 info.envmode = unit->EnvMode; \
932 \
933 if (info.envmode == GL_BLEND) { \
934 /* potential off-by-one error here? (1.0f -> 2048 -> 0) */ \
935 info.er = FloatToFixed(unit->EnvColor[RCOMP]); \
936 info.eg = FloatToFixed(unit->EnvColor[GCOMP]); \
937 info.eb = FloatToFixed(unit->EnvColor[BCOMP]); \
938 info.ea = FloatToFixed(unit->EnvColor[ACOMP]); \
939 } \
940 if (!info.texture) { \
941 /* this shouldn't happen */ \
942 return; \
943 } \
944 \
945 switch (info.format) { \
946 case GL_ALPHA: \
947 case GL_LUMINANCE: \
948 case GL_INTENSITY: \
949 info.tbytesline = obj->Image[b]->Width; \
950 break; \
951 case GL_LUMINANCE_ALPHA: \
952 info.tbytesline = obj->Image[b]->Width * 2; \
953 break; \
954 case GL_RGB: \
955 info.tbytesline = obj->Image[b]->Width * 3; \
956 break; \
957 case GL_RGBA: \
958 info.tbytesline = obj->Image[b]->Width * 4; \
959 break; \
960 default: \
961 _mesa_problem(NULL, "Bad texture format in persp_textured_triangle");\
962 return; \
963 } \
964 info.tsize = obj->Image[b]->Height * info.tbytesline;
965
966 #define RENDER_SPAN( span ) fast_persp_span(ctx, &span, &info);
967
968 #include "s_tritemp.h"
969
970 }
971
972
973 #endif /* CHAN_BITS != GL_FLOAT */
974
975
976 /*
977 * Generate arrays of fragment colors, z, fog, texcoords, etc from a
978 * triangle span object. Then call the span/fragment processsing
979 * functions in s_span.[ch]. This is used by a bunch of the textured
980 * triangle functions.
981 */
982 static void
983 rasterize_span(GLcontext *ctx, const struct triangle_span *span)
984 {
985 DEFMARRAY(GLchan, rgba, MAX_WIDTH, 4);
986 DEFMARRAY(GLchan, spec, MAX_WIDTH, 4);
987 DEFARRAY(GLuint, index, MAX_WIDTH);
988 DEFARRAY(GLuint, z, MAX_WIDTH);
989 DEFARRAY(GLfloat, fog, MAX_WIDTH);
990 DEFARRAY(GLfloat, sTex, MAX_WIDTH);
991 DEFARRAY(GLfloat, tTex, MAX_WIDTH);
992 DEFARRAY(GLfloat, rTex, MAX_WIDTH);
993 DEFARRAY(GLfloat, lambda, MAX_WIDTH);
994 DEFMARRAY(GLfloat, msTex, MAX_TEXTURE_UNITS, MAX_WIDTH);
995 DEFMARRAY(GLfloat, mtTex, MAX_TEXTURE_UNITS, MAX_WIDTH);
996 DEFMARRAY(GLfloat, mrTex, MAX_TEXTURE_UNITS, MAX_WIDTH);
997 DEFMARRAY(GLfloat, mLambda, MAX_TEXTURE_UNITS, MAX_WIDTH);
998
999 CHECKARRAY(rgba, return);
1000 CHECKARRAY(spec, return);
1001 CHECKARRAY(index, return);
1002 CHECKARRAY(z, return);
1003 CHECKARRAY(fog, return);
1004 CHECKARRAY(sTex, return);
1005 CHECKARRAY(tTex, return);
1006 CHECKARRAY(rTex, return);
1007 CHECKARRAY(lambda, return);
1008 CHECKARRAY(msTex, return);
1009 CHECKARRAY(mtTex, return);
1010 CHECKARRAY(mrTex, return);
1011 CHECKARRAY(mLambda, return);
1012
1013 if (span->activeMask & SPAN_RGBA) {
1014 if (span->activeMask & SPAN_FLAT) {
1015 GLuint i;
1016 GLchan color[4];
1017 color[RCOMP] = FixedToChan(span->red);
1018 color[GCOMP] = FixedToChan(span->green);
1019 color[BCOMP] = FixedToChan(span->blue);
1020 color[ACOMP] = FixedToChan(span->alpha);
1021 for (i = 0; i < span->count; i++) {
1022 COPY_CHAN4(rgba[i], color);
1023 }
1024 }
1025 else {
1026 /* smooth interpolation */
1027 #if CHAN_TYPE == GL_FLOAT
1028 GLfloat r = span->red;
1029 GLfloat g = span->green;
1030 GLfloat b = span->blue;
1031 GLfloat a = span->alpha;
1032 #else
1033 GLfixed r = span->red;
1034 GLfixed g = span->green;
1035 GLfixed b = span->blue;
1036 GLfixed a = span->alpha;
1037 #endif
1038 GLuint i;
1039 for (i = 0; i < span->count; i++) {
1040 rgba[i][RCOMP] = FixedToChan(r);
1041 rgba[i][GCOMP] = FixedToChan(g);
1042 rgba[i][BCOMP] = FixedToChan(b);
1043 rgba[i][ACOMP] = FixedToChan(a);
1044 r += span->redStep;
1045 g += span->greenStep;
1046 b += span->blueStep;
1047 a += span->alphaStep;
1048 }
1049 }
1050 }
1051
1052 if (span->activeMask & SPAN_SPEC) {
1053 if (span->activeMask & SPAN_FLAT) {
1054 const GLchan r = FixedToChan(span->specRed);
1055 const GLchan g = FixedToChan(span->specGreen);
1056 const GLchan b = FixedToChan(span->specBlue);
1057 GLuint i;
1058 for (i = 0; i < span->count; i++) {
1059 spec[i][RCOMP] = r;
1060 spec[i][GCOMP] = g;
1061 spec[i][BCOMP] = b;
1062 }
1063 }
1064 else {
1065 /* smooth interpolation */
1066 #if CHAN_TYPE == GL_FLOAT
1067 GLfloat r = span->specRed;
1068 GLfloat g = span->specGreen;
1069 GLfloat b = span->specBlue;
1070 #else
1071 GLfixed r = span->specRed;
1072 GLfixed g = span->specGreen;
1073 GLfixed b = span->specBlue;
1074 #endif
1075 GLuint i;
1076 for (i = 0; i < span->count; i++) {
1077 spec[i][RCOMP] = FixedToChan(r);
1078 spec[i][GCOMP] = FixedToChan(g);
1079 spec[i][BCOMP] = FixedToChan(b);
1080 r += span->specRedStep;
1081 g += span->specGreenStep;
1082 b += span->specBlueStep;
1083 }
1084 }
1085 }
1086
1087 if (span->activeMask & SPAN_INDEX) {
1088 if (span->activeMask & SPAN_FLAT) {
1089 GLuint i;
1090 const GLint indx = FixedToInt(span->index);
1091 for (i = 0; i < span->count; i++) {
1092 index[i] = indx;
1093 }
1094 }
1095 else {
1096 /* smooth interpolation */
1097 GLuint i;
1098 GLfixed ind = span->index;
1099 for (i = 0; i < span->count; i++) {
1100 index[i] = FixedToInt(ind);
1101 ind += span->indexStep;
1102 }
1103 }
1104 }
1105
1106 if (span->activeMask & SPAN_Z) {
1107 if (ctx->Visual.depthBits <= 16) {
1108 GLuint i;
1109 GLfixed zval = span->z;
1110 for (i = 0; i < span->count; i++) {
1111 z[i] = FixedToInt(zval);
1112 zval += span->zStep;
1113 }
1114 }
1115 else {
1116 /* Deep Z buffer, no fixed->int shift */
1117 GLuint i;
1118 GLfixed zval = span->z;
1119 for (i = 0; i < span->count; i++) {
1120 z[i] = zval;
1121 zval += span->zStep;
1122 }
1123 }
1124 }
1125 if (span->activeMask & SPAN_FOG) {
1126 GLuint i;
1127 GLfloat f = span->fog;
1128 for (i = 0; i < span->count; i++) {
1129 fog[i] = f;
1130 f += span->fogStep;
1131 }
1132 }
1133 if (span->activeMask & SPAN_TEXTURE) {
1134 if (ctx->Texture._ReallyEnabled & ~TEXTURE0_ANY) {
1135 /* multitexture */
1136 if (span->activeMask & SPAN_LAMBDA) {
1137 /* with lambda */
1138 GLuint u;
1139 for (u = 0; u < MAX_TEXTURE_UNITS; u++) {
1140 if (ctx->Texture.Unit[u]._ReallyEnabled) {
1141 GLfloat s = span->tex[u][0];
1142 GLfloat t = span->tex[u][1];
1143 GLfloat r = span->tex[u][2];
1144 GLfloat q = span->tex[u][3];
1145 GLuint i;
1146 for (i = 0; i < span->count; i++) {
1147 const GLfloat invQ = (q == 0.0F) ? 1.0 : (1.0F / q);
1148 msTex[u][i] = s * invQ;
1149 mtTex[u][i] = t * invQ;
1150 mrTex[u][i] = r * invQ;
1151 mLambda[u][i] = log(span->rho[u] * invQ * invQ) * 1.442695F * 0.5F;
1152 s += span->texStep[u][0];
1153 t += span->texStep[u][1];
1154 r += span->texStep[u][2];
1155 q += span->texStep[u][3];
1156 }
1157 }
1158 }
1159 }
1160 else {
1161 /* without lambda */
1162 GLuint u;
1163 for (u = 0; u < MAX_TEXTURE_UNITS; u++) {
1164 if (ctx->Texture.Unit[u]._ReallyEnabled) {
1165 GLfloat s = span->tex[u][0];
1166 GLfloat t = span->tex[u][1];
1167 GLfloat r = span->tex[u][2];
1168 GLfloat q = span->tex[u][3];
1169 GLuint i;
1170 for (i = 0; i < span->count; i++) {
1171 const GLfloat invQ = (q == 0.0F) ? 1.0 : (1.0F / q);
1172 msTex[u][i] = s * invQ;
1173 mtTex[u][i] = t * invQ;
1174 mrTex[u][i] = r * invQ;
1175 s += span->texStep[u][0];
1176 t += span->texStep[u][1];
1177 r += span->texStep[u][2];
1178 q += span->texStep[u][3];
1179 }
1180 }
1181 }
1182 }
1183 }
1184 else {
1185 /* just texture unit 0 */
1186 if (span->activeMask & SPAN_LAMBDA) {
1187 /* with lambda */
1188 GLfloat s = span->tex[0][0];
1189 GLfloat t = span->tex[0][1];
1190 GLfloat r = span->tex[0][2];
1191 GLfloat q = span->tex[0][3];
1192 GLuint i;
1193 for (i = 0; i < span->count; i++) {
1194 const GLfloat invQ = (q == 0.0F) ? 1.0 : (1.0F / q);
1195 sTex[i] = s * invQ;
1196 tTex[i] = t * invQ;
1197 rTex[i] = r * invQ;
1198 lambda[i] = log(span->rho[0] * invQ * invQ) * 1.442695F * 0.5F;
1199 s += span->texStep[0][0];
1200 t += span->texStep[0][1];
1201 r += span->texStep[0][2];
1202 q += span->texStep[0][3];
1203 }
1204 }
1205 else {
1206 /* without lambda */
1207 GLfloat s = span->tex[0][0];
1208 GLfloat t = span->tex[0][1];
1209 GLfloat r = span->tex[0][2];
1210 GLfloat q = span->tex[0][3];
1211 GLuint i;
1212 for (i = 0; i < span->count; i++) {
1213 const GLfloat invQ = (q == 0.0F) ? 1.0 : (1.0F / q);
1214 sTex[i] = s * invQ;
1215 tTex[i] = t * invQ;
1216 rTex[i] = r * invQ;
1217 s += span->texStep[0][0];
1218 t += span->texStep[0][1];
1219 r += span->texStep[0][2];
1220 q += span->texStep[0][3];
1221 }
1222 }
1223 }
1224 }
1225 /* XXX keep this? */
1226 if (span->activeMask & SPAN_INT_TEXTURE) {
1227 GLint intTexcoord[MAX_WIDTH][2];
1228 GLfixed s = span->intTex[0];
1229 GLfixed t = span->intTex[1];
1230 GLuint i;
1231 for (i = 0; i < span->count; i++) {
1232 intTexcoord[i][0] = FixedToInt(s);
1233 intTexcoord[i][1] = FixedToInt(t);
1234 s += span->intTexStep[0];
1235 t += span->intTexStep[1];
1236 }
1237 }
1238
1239 /* examine activeMask and call a s_span.c function */
1240 if (span->activeMask & SPAN_TEXTURE) {
1241 const GLfloat *fogPtr;
1242 if (span->activeMask & SPAN_FOG)
1243 fogPtr = fog;
1244 else
1245 fogPtr = NULL;
1246
1247 if (ctx->Texture._ReallyEnabled & ~TEXTURE0_ANY) {
1248 if (span->activeMask & SPAN_SPEC) {
1249 _mesa_write_multitexture_span(ctx, span->count, span->x, span->y,
1250 z, fogPtr,
1251 (const GLfloat (*)[MAX_WIDTH]) msTex,
1252 (const GLfloat (*)[MAX_WIDTH]) mtTex,
1253 (const GLfloat (*)[MAX_WIDTH]) mrTex,
1254 (GLfloat (*)[MAX_WIDTH]) mLambda,
1255 rgba, (CONST GLchan (*)[4]) spec,
1256 NULL, GL_POLYGON );
1257 }
1258 else {
1259 _mesa_write_multitexture_span(ctx, span->count, span->x, span->y,
1260 z, fogPtr,
1261 (const GLfloat (*)[MAX_WIDTH]) msTex,
1262 (const GLfloat (*)[MAX_WIDTH]) mtTex,
1263 (const GLfloat (*)[MAX_WIDTH]) mrTex,
1264 (GLfloat (*)[MAX_WIDTH]) mLambda,
1265 rgba, NULL, NULL, GL_POLYGON);
1266 }
1267 }
1268 else {
1269 /* single texture */
1270 if (span->activeMask & SPAN_SPEC) {
1271 _mesa_write_texture_span(ctx, span->count, span->x, span->y,
1272 z, fogPtr, sTex, tTex, rTex, lambda,
1273 rgba, (CONST GLchan (*)[4]) spec,
1274 NULL, GL_POLYGON);
1275 }
1276 else {
1277 _mesa_write_texture_span(ctx, span->count, span->x, span->y,
1278 z, fogPtr, sTex, tTex, rTex, lambda,
1279 rgba, NULL, NULL, GL_POLYGON);
1280 }
1281 }
1282 }
1283 else {
1284 _mesa_problem(ctx, "rasterize_span() should only be used for texturing");
1285 }
1286
1287 UNDEFARRAY(rgba);
1288 UNDEFARRAY(spec);
1289 UNDEFARRAY(index);
1290 UNDEFARRAY(z);
1291 UNDEFARRAY(fog);
1292 UNDEFARRAY(sTex);
1293 UNDEFARRAY(tTex);
1294 UNDEFARRAY(rTex);
1295 UNDEFARRAY(lambda);
1296 UNDEFARRAY(msTex);
1297 UNDEFARRAY(mtTex);
1298 UNDEFARRAY(mrTex);
1299 UNDEFARRAY(mLambda);
1300 }
1301
1302
1303
1304
1305 /*
1306 * Render a smooth-shaded, textured, RGBA triangle.
1307 * Interpolate S,T,R with perspective correction, w/out mipmapping.
1308 */
1309 static void general_textured_triangle( GLcontext *ctx,
1310 const SWvertex *v0,
1311 const SWvertex *v1,
1312 const SWvertex *v2 )
1313 {
1314 #define INTERP_Z 1
1315 #define INTERP_FOG 1
1316 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1317 #define INTERP_RGB 1
1318 #define INTERP_ALPHA 1
1319 #define INTERP_TEX 1
1320
1321 #define SETUP_CODE \
1322 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1323 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1324 DEFARRAY(GLfloat, sSpan, MAX_WIDTH); /* mac 32k limitation */ \
1325 DEFARRAY(GLfloat, tSpan, MAX_WIDTH); /* mac 32k limitation */ \
1326 DEFARRAY(GLfloat, uSpan, MAX_WIDTH); /* mac 32k limitation */ \
1327 CHECKARRAY(sSpan, return); /* mac 32k limitation */ \
1328 CHECKARRAY(tSpan, return); /* mac 32k limitation */ \
1329 CHECKARRAY(uSpan, return); /* mac 32k limitation */ \
1330 span.texWidth[0] = (GLfloat) texImage->Width; \
1331 span.texHeight[0] = (GLfloat) texImage->Height; \
1332 (void) fixedToDepthShift;
1333
1334 #define RENDER_SPAN( span ) \
1335 GLdepth zSpan[MAX_WIDTH]; \
1336 GLfloat fogSpan[MAX_WIDTH]; \
1337 GLchan rgbaSpan[MAX_WIDTH][4]; \
1338 GLuint i; \
1339 /* NOTE: we could just call rasterize_span() here instead */ \
1340 for (i = 0; i < span.count; i++) { \
1341 GLdouble invQ = span.tex[0][3] ? (1.0 / span.tex[0][3]) : 1.0; \
1342 zSpan[i] = FixedToDepth(span.z); \
1343 span.z += span.zStep; \
1344 fogSpan[i] = span.fog; \
1345 span.fog += span.fogStep; \
1346 rgbaSpan[i][RCOMP] = FixedToChan(span.red); \
1347 rgbaSpan[i][GCOMP] = FixedToChan(span.green); \
1348 rgbaSpan[i][BCOMP] = FixedToChan(span.blue); \
1349 rgbaSpan[i][ACOMP] = FixedToChan(span.alpha); \
1350 span.red += span.redStep; \
1351 span.green += span.greenStep; \
1352 span.blue += span.blueStep; \
1353 span.alpha += span.alphaStep; \
1354 sSpan[i] = span.tex[0][0] * invQ; \
1355 tSpan[i] = span.tex[0][1] * invQ; \
1356 uSpan[i] = span.tex[0][2] * invQ; \
1357 span.tex[0][0] += span.texStep[0][0]; \
1358 span.tex[0][1] += span.texStep[0][1]; \
1359 span.tex[0][2] += span.texStep[0][2]; \
1360 span.tex[0][3] += span.texStep[0][3]; \
1361 } \
1362 _mesa_write_texture_span(ctx, span.count, span.x, span.y, \
1363 zSpan, fogSpan, sSpan, tSpan, uSpan, \
1364 NULL, rgbaSpan, NULL, NULL, GL_POLYGON );
1365
1366 #define CLEANUP_CODE \
1367 UNDEFARRAY(sSpan); /* mac 32k limitation */ \
1368 UNDEFARRAY(tSpan); \
1369 UNDEFARRAY(uSpan);
1370
1371 #include "s_tritemp.h"
1372 }
1373
1374
1375 /*
1376 * Render a smooth-shaded, textured, RGBA triangle with separate specular
1377 * color interpolation.
1378 * Interpolate texcoords with perspective correction, w/out mipmapping.
1379 */
1380 static void general_textured_spec_triangle( GLcontext *ctx,
1381 const SWvertex *v0,
1382 const SWvertex *v1,
1383 const SWvertex *v2 )
1384 {
1385 #define INTERP_Z 1
1386 #define INTERP_FOG 1
1387 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1388 #define INTERP_RGB 1
1389 #define INTERP_SPEC 1
1390 #define INTERP_ALPHA 1
1391 #define INTERP_TEX 1
1392
1393 #define SETUP_CODE \
1394 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1395 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1396 span.texWidth[0] = (GLfloat) texImage->Width; \
1397 span.texHeight[0] = (GLfloat) texImage->Height; \
1398 (void) fixedToDepthShift;
1399
1400 #define RENDER_SPAN( span ) rasterize_span(ctx, &span);
1401
1402 #include "s_tritemp.h"
1403 }
1404
1405
1406 /*
1407 * Render a smooth-shaded, textured, RGBA triangle.
1408 * Interpolate S,T,R with perspective correction and compute lambda for
1409 * each fragment. Lambda is used to determine whether to use the
1410 * minification or magnification filter. If minification and using
1411 * mipmaps, lambda is also used to select the texture level of detail.
1412 */
1413 static void lambda_textured_triangle( GLcontext *ctx,
1414 const SWvertex *v0,
1415 const SWvertex *v1,
1416 const SWvertex *v2 )
1417 {
1418 #define INTERP_Z 1
1419 #define INTERP_FOG 1
1420 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1421 #define INTERP_RGB 1
1422 #define INTERP_ALPHA 1
1423 #define INTERP_TEX 1
1424 #define INTERP_LAMBDA 1
1425
1426 #define SETUP_CODE \
1427 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1428 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1429 span.texWidth[0] = (GLfloat) texImage->Width; \
1430 span.texHeight[0] = (GLfloat) texImage->Height; \
1431 (void) fixedToDepthShift;
1432
1433 #define RENDER_SPAN( span ) rasterize_span(ctx, &span);
1434
1435 #include "s_tritemp.h"
1436 }
1437
1438
1439 /*
1440 * Render a smooth-shaded, textured, RGBA triangle with separate specular
1441 * interpolation.
1442 * Interpolate S,T,R with perspective correction and compute lambda for
1443 * each fragment. Lambda is used to determine whether to use the
1444 * minification or magnification filter. If minification and using
1445 * mipmaps, lambda is also used to select the texture level of detail.
1446 */
1447 static void lambda_textured_spec_triangle( GLcontext *ctx,
1448 const SWvertex *v0,
1449 const SWvertex *v1,
1450 const SWvertex *v2 )
1451 {
1452 #define INTERP_Z 1
1453 #define INTERP_FOG 1
1454 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1455 #define INTERP_RGB 1
1456 #define INTERP_SPEC 1
1457 #define INTERP_ALPHA 1
1458 #define INTERP_TEX 1
1459 #define INTERP_LAMBDA 1
1460
1461 #define SETUP_CODE \
1462 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current; \
1463 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];\
1464 span.texWidth[0] = (GLfloat) texImage->Width; \
1465 span.texHeight[0] = (GLfloat) texImage->Height; \
1466 (void) fixedToDepthShift;
1467
1468 #define RENDER_SPAN( span ) rasterize_span(ctx, &span);
1469
1470 #include "s_tritemp.h"
1471 }
1472
1473
1474 /*
1475 * This is the big one!
1476 * Interpolate Z, RGB, Alpha, specular, fog, and N sets of texture coordinates
1477 * with lambda (LOD).
1478 * Yup, it's slow.
1479 */
1480 static void
1481 lambda_multitextured_triangle( GLcontext *ctx,
1482 const SWvertex *v0,
1483 const SWvertex *v1,
1484 const SWvertex *v2 )
1485 {
1486
1487 #define INTERP_Z 1
1488 #define INTERP_FOG 1
1489 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1490 #define INTERP_RGB 1
1491 #define INTERP_ALPHA 1
1492 #define INTERP_SPEC 1
1493 #define INTERP_MULTITEX 1
1494 #define INTERP_LAMBDA 1
1495
1496 #define SETUP_CODE \
1497 GLuint u; \
1498 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) { \
1499 if (ctx->Texture.Unit[u]._ReallyEnabled) { \
1500 const struct gl_texture_object *texObj; \
1501 const struct gl_texture_image *texImage; \
1502 texObj = ctx->Texture.Unit[u]._Current; \
1503 texImage = texObj->Image[texObj->BaseLevel]; \
1504 span.texWidth[u] = (GLfloat) texImage->Width; \
1505 span.texHeight[u] = (GLfloat) texImage->Height; \
1506 } \
1507 } \
1508 (void) fixedToDepthShift;
1509
1510 #define RENDER_SPAN( span ) rasterize_span(ctx, &span);
1511
1512 #include "s_tritemp.h"
1513
1514 }
1515
1516
1517 static void occlusion_zless_triangle( GLcontext *ctx,
1518 const SWvertex *v0,
1519 const SWvertex *v1,
1520 const SWvertex *v2 )
1521 {
1522 if (ctx->OcclusionResult) {
1523 return;
1524 }
1525
1526 #define DO_OCCLUSION_TEST
1527 #define INTERP_Z 1
1528 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
1529
1530 #define RENDER_SPAN( span ) \
1531 GLuint i; \
1532 for (i = 0; i < span.count; i++) { \
1533 GLdepth z = FixedToDepth(span.z); \
1534 if (z < zRow[i]) { \
1535 ctx->OcclusionResult = GL_TRUE; \
1536 return; \
1537 } \
1538 span.z += span.zStep; \
1539 }
1540
1541 #include "s_tritemp.h"
1542 }
1543
1544 static void nodraw_triangle( GLcontext *ctx,
1545 const SWvertex *v0,
1546 const SWvertex *v1,
1547 const SWvertex *v2 )
1548 {
1549 (void) (ctx && v0 && v1 && v2);
1550 }
1551
1552 void _swrast_add_spec_terms_triangle( GLcontext *ctx,
1553 const SWvertex *v0,
1554 const SWvertex *v1,
1555 const SWvertex *v2 )
1556 {
1557 SWvertex *ncv0 = (SWvertex *)v0; /* drop const qualifier */
1558 SWvertex *ncv1 = (SWvertex *)v1;
1559 SWvertex *ncv2 = (SWvertex *)v2;
1560 GLchan c[3][4];
1561 COPY_CHAN4( c[0], ncv0->color );
1562 COPY_CHAN4( c[1], ncv1->color );
1563 COPY_CHAN4( c[2], ncv2->color );
1564 ACC_3V( ncv0->color, ncv0->specular );
1565 ACC_3V( ncv1->color, ncv1->specular );
1566 ACC_3V( ncv2->color, ncv2->specular );
1567 SWRAST_CONTEXT(ctx)->SpecTriangle( ctx, ncv0, ncv1, ncv2 );
1568 COPY_CHAN4( ncv0->color, c[0] );
1569 COPY_CHAN4( ncv1->color, c[1] );
1570 COPY_CHAN4( ncv2->color, c[2] );
1571 }
1572
1573
1574
1575 #ifdef DEBUG
1576
1577 /* record the current triangle function name */
1578 static const char *triFuncName = NULL;
1579
1580 #define USE(triFunc) \
1581 do { \
1582 triFuncName = #triFunc; \
1583 /*printf("%s\n", triFuncName);*/ \
1584 swrast->Triangle = triFunc; \
1585 } while (0)
1586
1587 #else
1588
1589 #define USE(triFunc) swrast->Triangle = triFunc;
1590
1591 #endif
1592
1593
1594
1595
1596 /*
1597 * Determine which triangle rendering function to use given the current
1598 * rendering context.
1599 *
1600 * Please update the summary flag _SWRAST_NEW_TRIANGLE if you add or
1601 * remove tests to this code.
1602 */
1603 void
1604 _swrast_choose_triangle( GLcontext *ctx )
1605 {
1606 SWcontext *swrast = SWRAST_CONTEXT(ctx);
1607 const GLboolean rgbmode = ctx->Visual.rgbMode;
1608
1609 if (ctx->Polygon.CullFlag &&
1610 ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK) {
1611 USE(nodraw_triangle);
1612 return;
1613 }
1614
1615 if (ctx->RenderMode==GL_RENDER) {
1616
1617 if (ctx->Polygon.SmoothFlag) {
1618 _mesa_set_aa_triangle_function(ctx);
1619 ASSERT(swrast->Triangle);
1620 return;
1621 }
1622
1623 if (ctx->Depth.OcclusionTest &&
1624 ctx->Depth.Test &&
1625 ctx->Depth.Mask == GL_FALSE &&
1626 ctx->Depth.Func == GL_LESS &&
1627 !ctx->Stencil.Enabled) {
1628 if ((rgbmode &&
1629 ctx->Color.ColorMask[0] == 0 &&
1630 ctx->Color.ColorMask[1] == 0 &&
1631 ctx->Color.ColorMask[2] == 0 &&
1632 ctx->Color.ColorMask[3] == 0)
1633 ||
1634 (!rgbmode && ctx->Color.IndexMask == 0)) {
1635 USE(occlusion_zless_triangle);
1636 return;
1637 }
1638 }
1639
1640 if (ctx->Texture._ReallyEnabled) {
1641 /* Ugh, we do a _lot_ of tests to pick the best textured tri func */
1642 const struct gl_texture_object *texObj2D;
1643 const struct gl_texture_image *texImg;
1644 GLenum minFilter, magFilter, envMode;
1645 GLint format;
1646 texObj2D = ctx->Texture.Unit[0].Current2D;
1647 texImg = texObj2D ? texObj2D->Image[texObj2D->BaseLevel] : NULL;
1648 format = texImg ? texImg->TexFormat->MesaFormat : -1;
1649 minFilter = texObj2D ? texObj2D->MinFilter : (GLenum) 0;
1650 magFilter = texObj2D ? texObj2D->MagFilter : (GLenum) 0;
1651 envMode = ctx->Texture.Unit[0].EnvMode;
1652
1653 /* First see if we can used an optimized 2-D texture function */
1654 if (ctx->Texture._ReallyEnabled==TEXTURE0_2D
1655 && texObj2D->WrapS==GL_REPEAT
1656 && texObj2D->WrapT==GL_REPEAT
1657 && texImg->Border==0
1658 && (format == MESA_FORMAT_RGB || format == MESA_FORMAT_RGBA)
1659 && minFilter == magFilter
1660 && ctx->Light.Model.ColorControl == GL_SINGLE_COLOR
1661 && ctx->Texture.Unit[0].EnvMode != GL_COMBINE_EXT) {
1662 if (ctx->Hint.PerspectiveCorrection==GL_FASTEST) {
1663 if (minFilter == GL_NEAREST
1664 && format == MESA_FORMAT_RGB
1665 && (envMode == GL_REPLACE || envMode == GL_DECAL)
1666 && ((swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)
1667 && ctx->Depth.Func == GL_LESS
1668 && ctx->Depth.Mask == GL_TRUE)
1669 || swrast->_RasterMask == TEXTURE_BIT)
1670 && ctx->Polygon.StippleFlag == GL_FALSE) {
1671 if (swrast->_RasterMask == (DEPTH_BIT | TEXTURE_BIT)) {
1672 USE(simple_z_textured_triangle);
1673 }
1674 else {
1675 USE(simple_textured_triangle);
1676 }
1677 }
1678 else {
1679 #if CHAN_TYPE == GL_FLOAT
1680 USE(general_textured_triangle);
1681 #else
1682 USE(affine_textured_triangle);
1683 #endif
1684 }
1685 }
1686 else {
1687 #if CHAN_TYPE == GL_FLOAT
1688 USE(general_textured_triangle);
1689 #else
1690 USE(persp_textured_triangle);
1691 #endif
1692 }
1693 }
1694 else {
1695 /* More complicated textures (mipmap, multi-tex, sep specular) */
1696 GLboolean needLambda;
1697 /* if mag filter != min filter we need to compute lambda */
1698 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current;
1699 if (obj && obj->MinFilter != obj->MagFilter)
1700 needLambda = GL_TRUE;
1701 else
1702 needLambda = GL_FALSE;
1703 if (ctx->Texture._ReallyEnabled > TEXTURE0_ANY) {
1704 USE(lambda_multitextured_triangle);
1705 }
1706 else if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
1707 /* separate specular color interpolation */
1708 if (needLambda) {
1709 USE(lambda_textured_spec_triangle);
1710 }
1711 else {
1712 USE(general_textured_spec_triangle);
1713 }
1714 }
1715 else {
1716 if (needLambda) {
1717 USE(lambda_textured_triangle);
1718 }
1719 else {
1720 USE(general_textured_triangle);
1721 }
1722 }
1723 }
1724 }
1725 else {
1726 ASSERT(!ctx->Texture._ReallyEnabled);
1727 if (ctx->Light.ShadeModel==GL_SMOOTH) {
1728 /* smooth shaded, no texturing, stippled or some raster ops */
1729 if (rgbmode) {
1730 USE(smooth_rgba_triangle);
1731 }
1732 else {
1733 USE(smooth_ci_triangle);
1734 }
1735 }
1736 else {
1737 /* flat shaded, no texturing, stippled or some raster ops */
1738 if (rgbmode) {
1739 USE(flat_rgba_triangle);
1740 }
1741 else {
1742 USE(flat_ci_triangle);
1743 }
1744 }
1745 }
1746 }
1747 else if (ctx->RenderMode==GL_FEEDBACK) {
1748 USE(_mesa_feedback_triangle);
1749 }
1750 else {
1751 /* GL_SELECT mode */
1752 USE(_mesa_select_triangle);
1753 }
1754 }