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