s_aatritemp.h revision a852378a6289d154364dde440f89a39bbfc33e2d
1/* $Id: s_aatritemp.h,v 1.4 2000/11/19 23:10:26 brianp Exp $ */
2
3/*
4 * Mesa 3-D graphics library
5 * Version:  3.5
6 *
7 * Copyright (C) 1999-2000  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 * Antialiased Triangle Rasterizer Template
30 *
31 * This file is #include'd to generate custom AA triangle rasterizers.
32 * NOTE: this code hasn't been optimized yet.  That'll come after it
33 * works correctly.
34 *
35 * The following macros may be defined to indicate what auxillary information
36 * must be copmuted across the triangle:
37 *    DO_Z         - if defined, compute Z values
38 *    DO_RGBA      - if defined, compute RGBA values
39 *    DO_INDEX     - if defined, compute color index values
40 *    DO_SPEC      - if defined, compute specular RGB values
41 *    DO_TEX       - if defined, compute unit 0 STRQ texcoords
42 *    DO_MULTITEX  - if defined, compute all unit's STRQ texcoords
43 */
44
45/*void triangle( GLcontext *ctx, GLuint v0, GLuint v1, GLuint v2, GLuint pv )*/
46{
47   const GLfloat *p0 = v0->win;
48   const GLfloat *p1 = v1->win;
49   const GLfloat *p2 = v2->win;
50   const SWvertex *vMin, *vMid, *vMax;
51   GLint iyMin, iyMax;
52   GLfloat yMin, yMax;
53   GLboolean ltor;
54   GLfloat majDx, majDy;
55#ifdef DO_Z
56   GLfloat zPlane[4];                                       /* Z (depth) */
57   GLdepth z[MAX_WIDTH];
58   GLfloat fogPlane[4];
59   GLfixed fog[MAX_WIDTH];
60#endif
61#ifdef DO_RGBA
62   GLfloat rPlane[4], gPlane[4], bPlane[4], aPlane[4];      /* color */
63   GLchan rgba[MAX_WIDTH][4];
64#endif
65#ifdef DO_INDEX
66   GLfloat iPlane[4];                                       /* color index */
67   GLuint index[MAX_WIDTH];
68#endif
69#ifdef DO_SPEC
70   GLfloat srPlane[4], sgPlane[4], sbPlane[4];              /* spec color */
71   GLchan spec[MAX_WIDTH][4];
72#endif
73#ifdef DO_TEX
74   GLfloat sPlane[4], tPlane[4], uPlane[4], vPlane[4];
75   GLfloat texWidth, texHeight;
76   GLfloat s[MAX_WIDTH], t[MAX_WIDTH], u[MAX_WIDTH];
77   GLfloat lambda[MAX_WIDTH];
78#elif defined(DO_MULTITEX)
79   GLfloat sPlane[MAX_TEXTURE_UNITS][4];
80   GLfloat tPlane[MAX_TEXTURE_UNITS][4];
81   GLfloat uPlane[MAX_TEXTURE_UNITS][4];
82   GLfloat vPlane[MAX_TEXTURE_UNITS][4];
83   GLfloat texWidth[MAX_TEXTURE_UNITS], texHeight[MAX_TEXTURE_UNITS];
84   GLfloat s[MAX_TEXTURE_UNITS][MAX_WIDTH];
85   GLfloat t[MAX_TEXTURE_UNITS][MAX_WIDTH];
86   GLfloat u[MAX_TEXTURE_UNITS][MAX_WIDTH];
87   GLfloat lambda[MAX_TEXTURE_UNITS][MAX_WIDTH];
88#endif
89   GLfloat bf = SWRAST_CONTEXT(ctx)->_backface_sign;
90
91   /* determine bottom to top order of vertices */
92   {
93      GLfloat y0 = v0->win[1];
94      GLfloat y1 = v1->win[1];
95      GLfloat y2 = v2->win[1];
96      if (y0 <= y1) {
97	 if (y1 <= y2) {
98	    vMin = v0;   vMid = v1;   vMax = v2;   /* y0<=y1<=y2 */
99	 }
100	 else if (y2 <= y0) {
101	    vMin = v2;   vMid = v0;   vMax = v1;   /* y2<=y0<=y1 */
102	 }
103	 else {
104	    vMin = v0;   vMid = v2;   vMax = v1;  bf = -bf; /* y0<=y2<=y1 */
105	 }
106      }
107      else {
108	 if (y0 <= y2) {
109	    vMin = v1;   vMid = v0;   vMax = v2;  bf = -bf; /* y1<=y0<=y2 */
110	 }
111	 else if (y2 <= y1) {
112	    vMin = v2;   vMid = v1;   vMax = v0;  bf = -bf; /* y2<=y1<=y0 */
113	 }
114	 else {
115	    vMin = v1;   vMid = v2;   vMax = v0;   /* y1<=y2<=y0 */
116	 }
117      }
118   }
119
120   majDx = vMax->win[0] - vMin->win[0];
121   majDy = vMax->win[1] - vMin->win[1];
122
123   {
124      const GLfloat botDx = vMid->win[0] - vMin->win[0];
125      const GLfloat botDy = vMid->win[1] - vMin->win[1];
126      const GLfloat area = majDx * botDy - botDx * majDy;
127      ltor = (GLboolean) (area < 0.0F);
128      /* Do backface culling */
129      if (area * bf < 0 || area * area < .0025)
130	 return;
131   }
132
133#ifndef DO_OCCLUSION_TEST
134   ctx->OcclusionResult = GL_TRUE;
135#endif
136
137   /* plane setup */
138#ifdef DO_Z
139   compute_plane(p0, p1, p2, p0[2], p1[2], p2[2], zPlane);
140   compute_plane(p0, p1, p2,
141		 v0->fog,
142		 v1->fog,
143		 v2->fog,
144		 fogPlane);
145#endif
146#ifdef DO_RGBA
147   if (ctx->Light.ShadeModel == GL_SMOOTH) {
148      compute_plane(p0, p1, p2, v0->color[0], v1->color[0], v2->color[0], rPlane);
149      compute_plane(p0, p1, p2, v0->color[1], v1->color[1], v2->color[1], gPlane);
150      compute_plane(p0, p1, p2, v0->color[2], v1->color[2], v2->color[2], bPlane);
151      compute_plane(p0, p1, p2, v0->color[3], v1->color[3], v2->color[3], aPlane);
152   }
153   else {
154      constant_plane(v0->color[RCOMP], rPlane);
155      constant_plane(v0->color[GCOMP], gPlane);
156      constant_plane(v0->color[BCOMP], bPlane);
157      constant_plane(v0->color[ACOMP], aPlane);
158   }
159#endif
160#ifdef DO_INDEX
161   if (ctx->Light.ShadeModel == GL_SMOOTH) {
162      compute_plane(p0, p1, p2, v0->index,
163                    v1->index, v2->index, iPlane);
164   }
165   else {
166      constant_plane(v0->index, iPlane);
167   }
168#endif
169#ifdef DO_SPEC
170   {
171      compute_plane(p0, p1, p2, v0->specular[0], v1->specular[0], v2->specular[0],srPlane);
172      compute_plane(p0, p1, p2, v0->specular[1], v1->specular[1], v2->specular[1],sgPlane);
173      compute_plane(p0, p1, p2, v0->specular[2], v1->specular[2], v2->specular[2],sbPlane);
174   }
175#endif
176#ifdef DO_TEX
177   {
178      const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current;
179      const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];
180      const GLfloat invW0 = v0->win[3];
181      const GLfloat invW1 = v1->win[3];
182      const GLfloat invW2 = v2->win[3];
183      const GLfloat s0 = v0->texcoord[0][0] * invW0;
184      const GLfloat s1 = v1->texcoord[0][0] * invW1;
185      const GLfloat s2 = v2->texcoord[0][0] * invW2;
186      const GLfloat t0 = v0->texcoord[0][1] * invW0;
187      const GLfloat t1 = v1->texcoord[0][1] * invW1;
188      const GLfloat t2 = v2->texcoord[0][1] * invW2;
189      const GLfloat r0 = v0->texcoord[0][2] * invW0;
190      const GLfloat r1 = v1->texcoord[0][2] * invW1;
191      const GLfloat r2 = v2->texcoord[0][2] * invW2;
192      const GLfloat q0 = v0->texcoord[0][3] * invW0;
193      const GLfloat q1 = v1->texcoord[0][3] * invW1;
194      const GLfloat q2 = v2->texcoord[0][3] * invW2;
195      compute_plane(p0, p1, p2, s0, s1, s2, sPlane);
196      compute_plane(p0, p1, p2, t0, t1, t2, tPlane);
197      compute_plane(p0, p1, p2, r0, r1, r2, uPlane);
198      compute_plane(p0, p1, p2, q0, q1, q2, vPlane);
199      texWidth = (GLfloat) texImage->Width;
200      texHeight = (GLfloat) texImage->Height;
201   }
202#elif defined(DO_MULTITEX)
203   {
204      GLuint u;
205      for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
206         if (ctx->Texture.Unit[u]._ReallyEnabled) {
207            const struct gl_texture_object *obj = ctx->Texture.Unit[u]._Current;
208            const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];
209            const GLfloat invW0 = v0->win[3];
210            const GLfloat invW1 = v1->win[3];
211            const GLfloat invW2 = v2->win[3];
212            const GLfloat s0 = v0->texcoord[u][0] * invW0;
213            const GLfloat s1 = v1->texcoord[u][0] * invW1;
214            const GLfloat s2 = v2->texcoord[u][0] * invW2;
215            const GLfloat t0 = v0->texcoord[u][1] * invW0;
216            const GLfloat t1 = v1->texcoord[u][1] * invW1;
217            const GLfloat t2 = v2->texcoord[u][1] * invW2;
218            const GLfloat r0 = v0->texcoord[u][2] * invW0;
219            const GLfloat r1 = v1->texcoord[u][2] * invW1;
220            const GLfloat r2 = v2->texcoord[u][2] * invW2;
221            const GLfloat q0 = v0->texcoord[u][3] * invW0;
222            const GLfloat q1 = v1->texcoord[u][3] * invW1;
223            const GLfloat q2 = v2->texcoord[u][3] * invW2;
224            compute_plane(p0, p1, p2, s0, s1, s2, sPlane[u]);
225            compute_plane(p0, p1, p2, t0, t1, t2, tPlane[u]);
226            compute_plane(p0, p1, p2, r0, r1, r2, uPlane[u]);
227            compute_plane(p0, p1, p2, q0, q1, q2, vPlane[u]);
228            texWidth[u]  = (GLfloat) texImage->Width;
229            texHeight[u] = (GLfloat) texImage->Height;
230         }
231      }
232   }
233#endif
234
235   yMin = vMin->win[1];
236   yMax = vMax->win[1];
237   iyMin = (int) yMin;
238   iyMax = (int) yMax + 1;
239
240   if (ltor) {
241      /* scan left to right */
242      const float *pMin = vMin->win;
243      const float *pMid = vMid->win;
244      const float *pMax = vMax->win;
245      const float dxdy = majDx / majDy;
246      const float xAdj = dxdy < 0.0F ? -dxdy : 0.0F;
247      float x = vMin->win[0] - (yMin - iyMin) * dxdy;
248      int iy;
249      for (iy = iyMin; iy < iyMax; iy++, x += dxdy) {
250         GLint ix, startX = (GLint) (x - xAdj);
251         GLuint count, n;
252         GLfloat coverage = 0.0F;
253         /* skip over fragments with zero coverage */
254         while (startX < MAX_WIDTH) {
255            coverage = compute_coveragef(pMin, pMid, pMax, startX, iy);
256            if (coverage > 0.0F)
257               break;
258            startX++;
259         }
260
261         /* enter interior of triangle */
262         ix = startX;
263         count = 0;
264         while (coverage > 0.0F) {
265#ifdef DO_Z
266            z[count] = (GLdepth) solve_plane(ix, iy, zPlane);
267	    fog[count] = FloatToFixed(solve_plane(ix, iy, fogPlane));
268#endif
269#ifdef DO_RGBA
270            rgba[count][RCOMP] = solve_plane_chan(ix, iy, rPlane);
271            rgba[count][GCOMP] = solve_plane_chan(ix, iy, gPlane);
272            rgba[count][BCOMP] = solve_plane_chan(ix, iy, bPlane);
273            rgba[count][ACOMP] = (GLchan) (solve_plane_chan(ix, iy, aPlane) * coverage);
274#endif
275#ifdef DO_INDEX
276            {
277               GLint frac = compute_coveragei(pMin, pMid, pMax, ix, iy);
278               GLint indx = (GLint) solve_plane(ix, iy, iPlane);
279               index[count] = (indx & ~0xf) | frac;
280            }
281#endif
282#ifdef DO_SPEC
283            spec[count][RCOMP] = solve_plane_chan(ix, iy, srPlane);
284            spec[count][GCOMP] = solve_plane_chan(ix, iy, sgPlane);
285            spec[count][BCOMP] = solve_plane_chan(ix, iy, sbPlane);
286#endif
287#ifdef DO_TEX
288            {
289               GLfloat invQ = solve_plane_recip(ix, iy, vPlane);
290               s[count] = solve_plane(ix, iy, sPlane) * invQ;
291               t[count] = solve_plane(ix, iy, tPlane) * invQ;
292               u[count] = solve_plane(ix, iy, uPlane) * invQ;
293               lambda[count] = compute_lambda(sPlane, tPlane, invQ,
294                                                 texWidth, texHeight);
295            }
296#elif defined(DO_MULTITEX)
297            {
298               GLuint unit;
299               for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
300                  if (ctx->Texture.Unit[unit]._ReallyEnabled) {
301                     GLfloat invQ = solve_plane_recip(ix, iy, vPlane[unit]);
302                     s[unit][count] = solve_plane(ix, iy, sPlane[unit]) * invQ;
303                     t[unit][count] = solve_plane(ix, iy, tPlane[unit]) * invQ;
304                     u[unit][count] = solve_plane(ix, iy, uPlane[unit]) * invQ;
305                     lambda[unit][count] = compute_lambda(sPlane[unit],
306                          tPlane[unit], invQ, texWidth[unit], texHeight[unit]);
307                  }
308               }
309            }
310#endif
311            ix++;
312            count++;
313            coverage = compute_coveragef(pMin, pMid, pMax, ix, iy);
314         }
315
316         n = (GLuint) ix - (GLuint) startX;
317#ifdef DO_MULTITEX
318#  ifdef DO_SPEC
319         gl_write_multitexture_span(ctx, n, startX, iy, z, fog,
320                                    (const GLfloat (*)[MAX_WIDTH]) s,
321                                    (const GLfloat (*)[MAX_WIDTH]) t,
322                                    (const GLfloat (*)[MAX_WIDTH]) u,
323                                    (GLfloat (*)[MAX_WIDTH]) lambda,
324                                    rgba, (const GLchan (*)[4]) spec,
325                                    GL_POLYGON);
326#  else
327         gl_write_multitexture_span(ctx, n, startX, iy, z, fog,
328                                    (const GLfloat (*)[MAX_WIDTH]) s,
329                                    (const GLfloat (*)[MAX_WIDTH]) t,
330                                    (const GLfloat (*)[MAX_WIDTH]) u,
331                                    lambda, rgba, NULL, GL_POLYGON);
332#  endif
333#elif defined(DO_TEX)
334#  ifdef DO_SPEC
335         gl_write_texture_span(ctx, n, startX, iy, z, fog,
336                               s, t, u, lambda, rgba,
337                               (const GLchan (*)[4]) spec, GL_POLYGON);
338#  else
339         gl_write_texture_span(ctx, n, startX, iy, z, fog,
340                               s, t, u, lambda,
341                               rgba, NULL, GL_POLYGON);
342#  endif
343#elif defined(DO_RGBA)
344         gl_write_rgba_span(ctx, n, startX, iy, z, fog, rgba, GL_POLYGON);
345#elif defined(DO_INDEX)
346         gl_write_index_span(ctx, n, startX, iy, z, fog, index, GL_POLYGON);
347#endif
348      }
349   }
350   else {
351      /* scan right to left */
352      const GLfloat *pMin = vMin->win;
353      const GLfloat *pMid = vMid->win;
354      const GLfloat *pMax = vMax->win;
355      const GLfloat dxdy = majDx / majDy;
356      const GLfloat xAdj = dxdy > 0 ? dxdy : 0.0F;
357      GLfloat x = vMin->win[0] - (yMin - iyMin) * dxdy;
358      GLint iy;
359      for (iy = iyMin; iy < iyMax; iy++, x += dxdy) {
360         GLint ix, left, startX = (GLint) (x + xAdj);
361         GLuint count, n;
362         GLfloat coverage = 0.0F;
363         /* skip fragments with zero coverage */
364         while (startX >= 0) {
365            coverage = compute_coveragef(pMin, pMax, pMid, startX, iy);
366            if (coverage > 0.0F)
367               break;
368            startX--;
369         }
370
371         /* enter interior of triangle */
372         ix = startX;
373         count = 0;
374         while (coverage > 0.0F) {
375#ifdef DO_Z
376            z[ix] = (GLdepth) solve_plane(ix, iy, zPlane);
377            fog[ix] = FloatToFixed(solve_plane(ix, iy, fogPlane));
378#endif
379#ifdef DO_RGBA
380            rgba[ix][RCOMP] = solve_plane_chan(ix, iy, rPlane);
381            rgba[ix][GCOMP] = solve_plane_chan(ix, iy, gPlane);
382            rgba[ix][BCOMP] = solve_plane_chan(ix, iy, bPlane);
383            rgba[ix][ACOMP] = (GLchan) (solve_plane_chan(ix, iy, aPlane) * coverage);
384#endif
385#ifdef DO_INDEX
386            {
387               GLint frac = compute_coveragei(pMin, pMax, pMid, ix, iy);
388               GLint indx = (GLint) solve_plane(ix, iy, iPlane);
389               index[ix] = (indx & ~0xf) | frac;
390            }
391#endif
392#ifdef DO_SPEC
393            spec[ix][RCOMP] = solve_plane_chan(ix, iy, srPlane);
394            spec[ix][GCOMP] = solve_plane_chan(ix, iy, sgPlane);
395            spec[ix][BCOMP] = solve_plane_chan(ix, iy, sbPlane);
396#endif
397#ifdef DO_TEX
398            {
399               GLfloat invQ = solve_plane_recip(ix, iy, vPlane);
400               s[ix] = solve_plane(ix, iy, sPlane) * invQ;
401               t[ix] = solve_plane(ix, iy, tPlane) * invQ;
402               u[ix] = solve_plane(ix, iy, uPlane) * invQ;
403               lambda[ix] = compute_lambda(sPlane, tPlane, invQ,
404                                              texWidth, texHeight);
405            }
406#elif defined(DO_MULTITEX)
407            {
408               GLuint unit;
409               for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
410                  if (ctx->Texture.Unit[unit]._ReallyEnabled) {
411                     GLfloat invQ = solve_plane_recip(ix, iy, vPlane[unit]);
412                     s[unit][ix] = solve_plane(ix, iy, sPlane[unit]) * invQ;
413                     t[unit][ix] = solve_plane(ix, iy, tPlane[unit]) * invQ;
414                     u[unit][ix] = solve_plane(ix, iy, uPlane[unit]) * invQ;
415                     lambda[unit][ix] = compute_lambda(sPlane[unit],
416                         tPlane[unit], invQ, texWidth[unit], texHeight[unit]);
417                  }
418               }
419            }
420#endif
421            ix--;
422            count++;
423            coverage = compute_coveragef(pMin, pMax, pMid, ix, iy);
424         }
425
426         n = (GLuint) startX - (GLuint) ix;
427         left = ix + 1;
428#ifdef DO_MULTITEX
429         {
430            GLuint unit;
431            for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
432               if (ctx->Texture.Unit[unit]._ReallyEnabled) {
433                  GLint j;
434                  for (j = 0; j < n; j++) {
435                     s[unit][j] = s[unit][j + left];
436                     t[unit][j] = t[unit][j + left];
437                     u[unit][j] = u[unit][j + left];
438                     lambda[unit][j] = lambda[unit][j + left];
439                  }
440               }
441            }
442         }
443#  ifdef DO_SPEC
444         gl_write_multitexture_span(ctx, n, left, iy, z + left, fog + left,
445                                    (const GLfloat (*)[MAX_WIDTH]) s,
446                                    (const GLfloat (*)[MAX_WIDTH]) t,
447                                    (const GLfloat (*)[MAX_WIDTH]) u,
448                                    lambda, rgba + left,
449                                    (const GLchan (*)[4]) (spec + left),
450                                    GL_POLYGON);
451#  else
452         gl_write_multitexture_span(ctx, n, left, iy, z + left, fog + left,
453                                    (const GLfloat (*)[MAX_WIDTH]) s,
454                                    (const GLfloat (*)[MAX_WIDTH]) t,
455                                    (const GLfloat (*)[MAX_WIDTH]) u,
456                                    lambda,
457                                    rgba + left, NULL, GL_POLYGON);
458#  endif
459#elif defined(DO_TEX)
460#  ifdef DO_SPEC
461         gl_write_texture_span(ctx, n, left, iy, z + left, fog + left,
462                               s + left, t + left, u + left,
463                               lambda + left, rgba + left,
464                               (const GLchan (*)[4]) (spec + left),
465                               GL_POLYGON);
466#  else
467         gl_write_texture_span(ctx, n, left, iy, z + left, fog + left,
468                               s + left, t + left,
469                               u + left, lambda + left,
470                               rgba + left, NULL, GL_POLYGON);
471#  endif
472#elif defined(DO_RGBA)
473         gl_write_rgba_span(ctx, n, left, iy, z + left, fog + left,
474                            rgba + left, GL_POLYGON);
475#elif defined(DO_INDEX)
476         gl_write_index_span(ctx, n, left, iy, z + left, fog + left,
477                             index + left, GL_POLYGON);
478#endif
479      }
480   }
481}
482
483
484#ifdef DO_Z
485#undef DO_Z
486#endif
487
488#ifdef DO_RGBA
489#undef DO_RGBA
490#endif
491
492#ifdef DO_INDEX
493#undef DO_INDEX
494#endif
495
496#ifdef DO_SPEC
497#undef DO_SPEC
498#endif
499
500#ifdef DO_TEX
501#undef DO_TEX
502#endif
503
504#ifdef DO_MULTITEX
505#undef DO_MULTITEX
506#endif
507
508#ifdef DO_OCCLUSION_TEST
509#undef DO_OCCLUSION_TEST
510#endif
511