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