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