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