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