fixed some divide by zero problems found w/ conform
[mesa.git] / src / mesa / swrast / s_aatriangle.c
1 /* $Id: s_aatriangle.c,v 1.14 2001/05/10 17:41:41 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 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 rasterizers
30 */
31
32
33 #include "mmath.h"
34 #include "s_aatriangle.h"
35 #include "s_context.h"
36 #include "s_span.h"
37
38
39 /*
40 * Compute coefficients of a plane using the X,Y coords of the v0, v1, v2
41 * vertices and the given Z values.
42 */
43 static INLINE void
44 compute_plane(const GLfloat v0[], const GLfloat v1[], const GLfloat v2[],
45 GLfloat z0, GLfloat z1, GLfloat z2, GLfloat plane[4])
46 {
47 const GLfloat px = v1[0] - v0[0];
48 const GLfloat py = v1[1] - v0[1];
49 const GLfloat pz = z1 - z0;
50
51 const GLfloat qx = v2[0] - v0[0];
52 const GLfloat qy = v2[1] - v0[1];
53 const GLfloat qz = z2 - z0;
54
55 const GLfloat a = py * qz - pz * qy;
56 const GLfloat b = pz * qx - px * qz;
57 const GLfloat c = px * qy - py * qx;
58 const GLfloat d = -(a * v0[0] + b * v0[1] + c * z0);
59
60 plane[0] = a;
61 plane[1] = b;
62 plane[2] = c;
63 plane[3] = d;
64 }
65
66
67 /*
68 * Compute coefficients of a plane with a constant Z value.
69 */
70 static INLINE void
71 constant_plane(GLfloat value, GLfloat plane[4])
72 {
73 plane[0] = 0.0;
74 plane[1] = 0.0;
75 plane[2] = -1.0;
76 plane[3] = value;
77 }
78
79 #define CONSTANT_PLANE(VALUE, PLANE) \
80 do { \
81 PLANE[0] = 0.0F; \
82 PLANE[1] = 0.0F; \
83 PLANE[2] = -1.0F; \
84 PLANE[3] = VALUE; \
85 } while (0)
86
87
88
89 /*
90 * Solve plane equation for Z at (X,Y).
91 */
92 static INLINE GLfloat
93 solve_plane(GLfloat x, GLfloat y, const GLfloat plane[4])
94 {
95 const GLfloat z = (plane[3] + plane[0] * x + plane[1] * y) / -plane[2];
96 return z;
97 }
98
99
100 #define SOLVE_PLANE(X, Y, PLANE) \
101 ((PLANE[3] + PLANE[0] * (X) + PLANE[1] * (Y)) / -PLANE[2])
102
103
104 /*
105 * Return 1 / solve_plane().
106 */
107 static INLINE GLfloat
108 solve_plane_recip(GLfloat x, GLfloat y, const GLfloat plane[4])
109 {
110 const GLfloat denom = plane[3] + plane[0] * x + plane[1] * y;
111 if (denom == 0.0F)
112 return 0.0F;
113 else
114 return -plane[2] / denom;
115 }
116
117
118
119 /*
120 * Solve plane and return clamped GLchan value.
121 */
122 static INLINE GLchan
123 solve_plane_chan(GLfloat x, GLfloat y, const GLfloat plane[4])
124 {
125 GLfloat z = (plane[3] + plane[0] * x + plane[1] * y) / -plane[2] + 0.5F;
126 if (z < 0.0F)
127 return 0;
128 else if (z > CHAN_MAXF)
129 return (GLchan) CHAN_MAXF;
130 return (GLchan) (GLint) z;
131 }
132
133
134
135 /*
136 * Compute how much (area) of the given pixel is inside the triangle.
137 * Vertices MUST be specified in counter-clockwise order.
138 * Return: coverage in [0, 1].
139 */
140 static GLfloat
141 compute_coveragef(const GLfloat v0[3], const GLfloat v1[3],
142 const GLfloat v2[3], GLint winx, GLint winy)
143 {
144 #define B 0.125
145 static const GLfloat samples[16][2] = {
146 /* start with the four corners */
147 { 0.00+B, 0.00+B },
148 { 0.75+B, 0.00+B },
149 { 0.00+B, 0.75+B },
150 { 0.75+B, 0.75+B },
151 /* continue with interior samples */
152 { 0.25+B, 0.00+B },
153 { 0.50+B, 0.00+B },
154 { 0.00+B, 0.25+B },
155 { 0.25+B, 0.25+B },
156 { 0.50+B, 0.25+B },
157 { 0.75+B, 0.25+B },
158 { 0.00+B, 0.50+B },
159 { 0.25+B, 0.50+B },
160 { 0.50+B, 0.50+B },
161 { 0.75+B, 0.50+B },
162 { 0.25+B, 0.75+B },
163 { 0.50+B, 0.75+B }
164 };
165 const GLfloat x = (GLfloat) winx;
166 const GLfloat y = (GLfloat) winy;
167 const GLfloat dx0 = v1[0] - v0[0];
168 const GLfloat dy0 = v1[1] - v0[1];
169 const GLfloat dx1 = v2[0] - v1[0];
170 const GLfloat dy1 = v2[1] - v1[1];
171 const GLfloat dx2 = v0[0] - v2[0];
172 const GLfloat dy2 = v0[1] - v2[1];
173 GLint stop = 4, i;
174 GLfloat insideCount = 16.0F;
175
176 #ifdef DEBUG
177 {
178 const GLfloat area = dx0 * dy1 - dx1 * dy0;
179 assert(area >= 0.0);
180 }
181 #endif
182
183 for (i = 0; i < stop; i++) {
184 const GLfloat sx = x + samples[i][0];
185 const GLfloat sy = y + samples[i][1];
186 const GLfloat fx0 = sx - v0[0];
187 const GLfloat fy0 = sy - v0[1];
188 const GLfloat fx1 = sx - v1[0];
189 const GLfloat fy1 = sy - v1[1];
190 const GLfloat fx2 = sx - v2[0];
191 const GLfloat fy2 = sy - v2[1];
192 /* cross product determines if sample is inside or outside each edge */
193 GLfloat cross0 = (dx0 * fy0 - dy0 * fx0);
194 GLfloat cross1 = (dx1 * fy1 - dy1 * fx1);
195 GLfloat cross2 = (dx2 * fy2 - dy2 * fx2);
196 /* Check if the sample is exactly on an edge. If so, let cross be a
197 * positive or negative value depending on the direction of the edge.
198 */
199 if (cross0 == 0.0F)
200 cross0 = dx0 + dy0;
201 if (cross1 == 0.0F)
202 cross1 = dx1 + dy1;
203 if (cross2 == 0.0F)
204 cross2 = dx2 + dy2;
205 if (cross0 < 0.0F || cross1 < 0.0F || cross2 < 0.0F) {
206 /* point is outside triangle */
207 insideCount -= 1.0F;
208 stop = 16;
209 }
210 }
211 if (stop == 4)
212 return 1.0F;
213 else
214 return insideCount * (1.0F / 16.0F);
215 }
216
217
218
219 /*
220 * Compute how much (area) of the given pixel is inside the triangle.
221 * Vertices MUST be specified in counter-clockwise order.
222 * Return: coverage in [0, 15].
223 */
224 static GLint
225 compute_coveragei(const GLfloat v0[3], const GLfloat v1[3],
226 const GLfloat v2[3], GLint winx, GLint winy)
227 {
228 /* NOTE: 15 samples instead of 16.
229 * A better sample distribution could be used.
230 */
231 static const GLfloat samples[15][2] = {
232 /* start with the four corners */
233 { 0.00+B, 0.00+B },
234 { 0.75+B, 0.00+B },
235 { 0.00+B, 0.75+B },
236 { 0.75+B, 0.75+B },
237 /* continue with interior samples */
238 { 0.25+B, 0.00+B },
239 { 0.50+B, 0.00+B },
240 { 0.00+B, 0.25+B },
241 { 0.25+B, 0.25+B },
242 { 0.50+B, 0.25+B },
243 { 0.75+B, 0.25+B },
244 { 0.00+B, 0.50+B },
245 { 0.25+B, 0.50+B },
246 /*{ 0.50, 0.50 },*/
247 { 0.75+B, 0.50+B },
248 { 0.25+B, 0.75+B },
249 { 0.50+B, 0.75+B }
250 };
251 const GLfloat x = (GLfloat) winx;
252 const GLfloat y = (GLfloat) winy;
253 const GLfloat dx0 = v1[0] - v0[0];
254 const GLfloat dy0 = v1[1] - v0[1];
255 const GLfloat dx1 = v2[0] - v1[0];
256 const GLfloat dy1 = v2[1] - v1[1];
257 const GLfloat dx2 = v0[0] - v2[0];
258 const GLfloat dy2 = v0[1] - v2[1];
259 GLint stop = 4, i;
260 GLint insideCount = 15;
261
262 #ifdef DEBUG
263 {
264 const GLfloat area = dx0 * dy1 - dx1 * dy0;
265 assert(area >= 0.0);
266 }
267 #endif
268
269 for (i = 0; i < stop; i++) {
270 const GLfloat sx = x + samples[i][0];
271 const GLfloat sy = y + samples[i][1];
272 const GLfloat fx0 = sx - v0[0];
273 const GLfloat fy0 = sy - v0[1];
274 const GLfloat fx1 = sx - v1[0];
275 const GLfloat fy1 = sy - v1[1];
276 const GLfloat fx2 = sx - v2[0];
277 const GLfloat fy2 = sy - v2[1];
278 /* cross product determines if sample is inside or outside each edge */
279 GLfloat cross0 = (dx0 * fy0 - dy0 * fx0);
280 GLfloat cross1 = (dx1 * fy1 - dy1 * fx1);
281 GLfloat cross2 = (dx2 * fy2 - dy2 * fx2);
282 /* Check if the sample is exactly on an edge. If so, let cross be a
283 * positive or negative value depending on the direction of the edge.
284 */
285 if (cross0 == 0.0F)
286 cross0 = dx0 + dy0;
287 if (cross1 == 0.0F)
288 cross1 = dx1 + dy1;
289 if (cross2 == 0.0F)
290 cross2 = dx2 + dy2;
291 if (cross0 < 0.0F || cross1 < 0.0F || cross2 < 0.0F) {
292 /* point is outside triangle */
293 insideCount--;
294 stop = 15;
295 }
296 }
297 if (stop == 4)
298 return 15;
299 else
300 return insideCount;
301 }
302
303
304
305 static void
306 rgba_aa_tri(GLcontext *ctx,
307 const SWvertex *v0,
308 const SWvertex *v1,
309 const SWvertex *v2)
310 {
311 #define DO_Z
312 #define DO_RGBA
313 #include "s_aatritemp.h"
314 }
315
316
317 static void
318 index_aa_tri(GLcontext *ctx,
319 const SWvertex *v0,
320 const SWvertex *v1,
321 const SWvertex *v2)
322 {
323 #define DO_Z
324 #define DO_INDEX
325 #include "s_aatritemp.h"
326 }
327
328
329 /*
330 * Compute mipmap level of detail.
331 */
332 static INLINE GLfloat
333 compute_lambda(const GLfloat sPlane[4], const GLfloat tPlane[4],
334 GLfloat invQ, GLfloat width, GLfloat height)
335 {
336 GLfloat dudx = sPlane[0] / sPlane[2] * invQ * width;
337 GLfloat dudy = sPlane[1] / sPlane[2] * invQ * width;
338 GLfloat dvdx = tPlane[0] / tPlane[2] * invQ * height;
339 GLfloat dvdy = tPlane[1] / tPlane[2] * invQ * height;
340 GLfloat r1 = dudx * dudx + dudy * dudy;
341 GLfloat r2 = dvdx * dvdx + dvdy * dvdy;
342 GLfloat rho2 = r1 + r2;
343 /* return log base 2 of rho */
344 if (rho2 == 0.0F)
345 return 0.0;
346 else
347 return log(rho2) * 1.442695 * 0.5; /* 1.442695 = 1/log(2) */
348 }
349
350
351 static void
352 tex_aa_tri(GLcontext *ctx,
353 const SWvertex *v0,
354 const SWvertex *v1,
355 const SWvertex *v2)
356 {
357 #define DO_Z
358 #define DO_RGBA
359 #define DO_TEX
360 #include "s_aatritemp.h"
361 }
362
363
364 static void
365 spec_tex_aa_tri(GLcontext *ctx,
366 const SWvertex *v0,
367 const SWvertex *v1,
368 const SWvertex *v2)
369 {
370 #define DO_Z
371 #define DO_RGBA
372 #define DO_TEX
373 #define DO_SPEC
374 #include "s_aatritemp.h"
375 }
376
377
378 static void
379 multitex_aa_tri(GLcontext *ctx,
380 const SWvertex *v0,
381 const SWvertex *v1,
382 const SWvertex *v2)
383 {
384 #define DO_Z
385 #define DO_RGBA
386 #define DO_MULTITEX
387 #include "s_aatritemp.h"
388 }
389
390 static void
391 spec_multitex_aa_tri(GLcontext *ctx,
392 const SWvertex *v0,
393 const SWvertex *v1,
394 const SWvertex *v2)
395 {
396 #define DO_Z
397 #define DO_RGBA
398 #define DO_MULTITEX
399 #define DO_SPEC
400 #include "s_aatritemp.h"
401 }
402
403
404 /*
405 * Examine GL state and set swrast->Triangle to an
406 * appropriate antialiased triangle rasterizer function.
407 */
408 void
409 _mesa_set_aa_triangle_function(GLcontext *ctx)
410 {
411 SWcontext *swrast = SWRAST_CONTEXT(ctx);
412 ASSERT(ctx->Polygon.SmoothFlag);
413
414 if (ctx->Texture._ReallyEnabled) {
415 if (ctx->_TriangleCaps & DD_SEPARATE_SPECULAR) {
416 if (swrast->_MultiTextureEnabled) {
417 SWRAST_CONTEXT(ctx)->Triangle = spec_multitex_aa_tri;
418 }
419 else {
420 SWRAST_CONTEXT(ctx)->Triangle = spec_tex_aa_tri;
421 }
422 }
423 else {
424 if (swrast->_MultiTextureEnabled) {
425 SWRAST_CONTEXT(ctx)->Triangle = multitex_aa_tri;
426 }
427 else {
428 SWRAST_CONTEXT(ctx)->Triangle = tex_aa_tri;
429 }
430 }
431 }
432 else if (ctx->Visual.rgbMode) {
433 SWRAST_CONTEXT(ctx)->Triangle = rgba_aa_tri;
434 }
435 else {
436 SWRAST_CONTEXT(ctx)->Triangle = index_aa_tri;
437 }
438
439 ASSERT(SWRAST_CONTEXT(ctx)->Triangle);
440 }