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