Sync with trunk r63637.
[reactos.git] / dll / opengl / mesa / swrast / s_aaline.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.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 #include <precomp.h>
26
27 #define SUB_PIXEL 4
28
29
30 /*
31 * Info about the AA line we're rendering
32 */
33 struct LineInfo
34 {
35 GLfloat x0, y0; /* start */
36 GLfloat x1, y1; /* end */
37 GLfloat dx, dy; /* direction vector */
38 GLfloat len; /* length */
39 GLfloat halfWidth; /* half of line width */
40 GLfloat xAdj, yAdj; /* X and Y adjustment for quad corners around line */
41 /* for coverage computation */
42 GLfloat qx0, qy0; /* quad vertices */
43 GLfloat qx1, qy1;
44 GLfloat qx2, qy2;
45 GLfloat qx3, qy3;
46 GLfloat ex0, ey0; /* quad edge vectors */
47 GLfloat ex1, ey1;
48 GLfloat ex2, ey2;
49 GLfloat ex3, ey3;
50
51 /* DO_Z */
52 GLfloat zPlane[4];
53 /* DO_RGBA - always enabled */
54 GLfloat rPlane[4], gPlane[4], bPlane[4], aPlane[4];
55 /* DO_ATTRIBS */
56 GLfloat wPlane[4];
57 GLfloat attrPlane[FRAG_ATTRIB_MAX][4][4];
58 GLfloat lambda[FRAG_ATTRIB_MAX];
59 GLfloat texWidth[FRAG_ATTRIB_MAX];
60 GLfloat texHeight[FRAG_ATTRIB_MAX];
61
62 SWspan span;
63 };
64
65
66
67 /*
68 * Compute the equation of a plane used to interpolate line fragment data
69 * such as color, Z, texture coords, etc.
70 * Input: (x0, y0) and (x1,y1) are the endpoints of the line.
71 * z0, and z1 are the end point values to interpolate.
72 * Output: plane - the plane equation.
73 *
74 * Note: we don't really have enough parameters to specify a plane.
75 * We take the endpoints of the line and compute a plane such that
76 * the cross product of the line vector and the plane normal is
77 * parallel to the projection plane.
78 */
79 static void
80 compute_plane(GLfloat x0, GLfloat y0, GLfloat x1, GLfloat y1,
81 GLfloat z0, GLfloat z1, GLfloat plane[4])
82 {
83 #if 0
84 /* original */
85 const GLfloat px = x1 - x0;
86 const GLfloat py = y1 - y0;
87 const GLfloat pz = z1 - z0;
88 const GLfloat qx = -py;
89 const GLfloat qy = px;
90 const GLfloat qz = 0;
91 const GLfloat a = py * qz - pz * qy;
92 const GLfloat b = pz * qx - px * qz;
93 const GLfloat c = px * qy - py * qx;
94 const GLfloat d = -(a * x0 + b * y0 + c * z0);
95 plane[0] = a;
96 plane[1] = b;
97 plane[2] = c;
98 plane[3] = d;
99 #else
100 /* simplified */
101 const GLfloat px = x1 - x0;
102 const GLfloat py = y1 - y0;
103 const GLfloat pz = z0 - z1;
104 const GLfloat a = pz * px;
105 const GLfloat b = pz * py;
106 const GLfloat c = px * px + py * py;
107 const GLfloat d = -(a * x0 + b * y0 + c * z0);
108 if (a == 0.0 && b == 0.0 && c == 0.0 && d == 0.0) {
109 plane[0] = 0.0;
110 plane[1] = 0.0;
111 plane[2] = 1.0;
112 plane[3] = 0.0;
113 }
114 else {
115 plane[0] = a;
116 plane[1] = b;
117 plane[2] = c;
118 plane[3] = d;
119 }
120 #endif
121 }
122
123
124 static inline void
125 constant_plane(GLfloat value, GLfloat plane[4])
126 {
127 plane[0] = 0.0;
128 plane[1] = 0.0;
129 plane[2] = -1.0;
130 plane[3] = value;
131 }
132
133
134 static inline GLfloat
135 solve_plane(GLfloat x, GLfloat y, const GLfloat plane[4])
136 {
137 const GLfloat z = (plane[3] + plane[0] * x + plane[1] * y) / -plane[2];
138 return z;
139 }
140
141 #define SOLVE_PLANE(X, Y, PLANE) \
142 ((PLANE[3] + PLANE[0] * (X) + PLANE[1] * (Y)) / -PLANE[2])
143
144
145 /*
146 * Return 1 / solve_plane().
147 */
148 static inline GLfloat
149 solve_plane_recip(GLfloat x, GLfloat y, const GLfloat plane[4])
150 {
151 const GLfloat denom = plane[3] + plane[0] * x + plane[1] * y;
152 if (denom == 0.0)
153 return 0.0;
154 else
155 return -plane[2] / denom;
156 }
157
158
159 /*
160 * Solve plane and return clamped GLchan value.
161 */
162 static inline GLchan
163 solve_plane_chan(GLfloat x, GLfloat y, const GLfloat plane[4])
164 {
165 const GLfloat z = (plane[3] + plane[0] * x + plane[1] * y) / -plane[2];
166 #if CHAN_TYPE == GL_FLOAT
167 return CLAMP(z, 0.0F, CHAN_MAXF);
168 #else
169 if (z < 0)
170 return 0;
171 else if (z > CHAN_MAX)
172 return CHAN_MAX;
173 return (GLchan) IROUND_POS(z);
174 #endif
175 }
176
177
178 /*
179 * Compute mipmap level of detail.
180 */
181 static inline GLfloat
182 compute_lambda(const GLfloat sPlane[4], const GLfloat tPlane[4],
183 GLfloat invQ, GLfloat width, GLfloat height)
184 {
185 GLfloat dudx = sPlane[0] / sPlane[2] * invQ * width;
186 GLfloat dudy = sPlane[1] / sPlane[2] * invQ * width;
187 GLfloat dvdx = tPlane[0] / tPlane[2] * invQ * height;
188 GLfloat dvdy = tPlane[1] / tPlane[2] * invQ * height;
189 GLfloat r1 = dudx * dudx + dudy * dudy;
190 GLfloat r2 = dvdx * dvdx + dvdy * dvdy;
191 GLfloat rho2 = r1 + r2;
192 /* return log base 2 of rho */
193 if (rho2 == 0.0F)
194 return 0.0;
195 else
196 return (GLfloat) (LOGF(rho2) * 1.442695 * 0.5);/* 1.442695 = 1/log(2) */
197 }
198
199
200
201
202 /*
203 * Fill in the samples[] array with the (x,y) subpixel positions of
204 * xSamples * ySamples sample positions.
205 * Note that the four corner samples are put into the first four
206 * positions of the array. This allows us to optimize for the common
207 * case of all samples being inside the polygon.
208 */
209 static void
210 make_sample_table(GLint xSamples, GLint ySamples, GLfloat samples[][2])
211 {
212 const GLfloat dx = 1.0F / (GLfloat) xSamples;
213 const GLfloat dy = 1.0F / (GLfloat) ySamples;
214 GLint x, y;
215 GLint i;
216
217 i = 4;
218 for (x = 0; x < xSamples; x++) {
219 for (y = 0; y < ySamples; y++) {
220 GLint j;
221 if (x == 0 && y == 0) {
222 /* lower left */
223 j = 0;
224 }
225 else if (x == xSamples - 1 && y == 0) {
226 /* lower right */
227 j = 1;
228 }
229 else if (x == 0 && y == ySamples - 1) {
230 /* upper left */
231 j = 2;
232 }
233 else if (x == xSamples - 1 && y == ySamples - 1) {
234 /* upper right */
235 j = 3;
236 }
237 else {
238 j = i++;
239 }
240 samples[j][0] = x * dx + 0.5F * dx;
241 samples[j][1] = y * dy + 0.5F * dy;
242 }
243 }
244 }
245
246
247
248 /*
249 * Compute how much of the given pixel's area is inside the rectangle
250 * defined by vertices v0, v1, v2, v3.
251 * Vertices MUST be specified in counter-clockwise order.
252 * Return: coverage in [0, 1].
253 */
254 static GLfloat
255 compute_coveragef(const struct LineInfo *info,
256 GLint winx, GLint winy)
257 {
258 static GLfloat samples[SUB_PIXEL * SUB_PIXEL][2];
259 static GLboolean haveSamples = GL_FALSE;
260 const GLfloat x = (GLfloat) winx;
261 const GLfloat y = (GLfloat) winy;
262 GLint stop = 4, i;
263 GLfloat insideCount = SUB_PIXEL * SUB_PIXEL;
264
265 if (!haveSamples) {
266 make_sample_table(SUB_PIXEL, SUB_PIXEL, samples);
267 haveSamples = GL_TRUE;
268 }
269
270 #if 0 /*DEBUG*/
271 {
272 const GLfloat area = dx0 * dy1 - dx1 * dy0;
273 assert(area >= 0.0);
274 }
275 #endif
276
277 for (i = 0; i < stop; i++) {
278 const GLfloat sx = x + samples[i][0];
279 const GLfloat sy = y + samples[i][1];
280 const GLfloat fx0 = sx - info->qx0;
281 const GLfloat fy0 = sy - info->qy0;
282 const GLfloat fx1 = sx - info->qx1;
283 const GLfloat fy1 = sy - info->qy1;
284 const GLfloat fx2 = sx - info->qx2;
285 const GLfloat fy2 = sy - info->qy2;
286 const GLfloat fx3 = sx - info->qx3;
287 const GLfloat fy3 = sy - info->qy3;
288 /* cross product determines if sample is inside or outside each edge */
289 GLfloat cross0 = (info->ex0 * fy0 - info->ey0 * fx0);
290 GLfloat cross1 = (info->ex1 * fy1 - info->ey1 * fx1);
291 GLfloat cross2 = (info->ex2 * fy2 - info->ey2 * fx2);
292 GLfloat cross3 = (info->ex3 * fy3 - info->ey3 * fx3);
293 /* Check if the sample is exactly on an edge. If so, let cross be a
294 * positive or negative value depending on the direction of the edge.
295 */
296 if (cross0 == 0.0F)
297 cross0 = info->ex0 + info->ey0;
298 if (cross1 == 0.0F)
299 cross1 = info->ex1 + info->ey1;
300 if (cross2 == 0.0F)
301 cross2 = info->ex2 + info->ey2;
302 if (cross3 == 0.0F)
303 cross3 = info->ex3 + info->ey3;
304 if (cross0 < 0.0F || cross1 < 0.0F || cross2 < 0.0F || cross3 < 0.0F) {
305 /* point is outside quadrilateral */
306 insideCount -= 1.0F;
307 stop = SUB_PIXEL * SUB_PIXEL;
308 }
309 }
310 if (stop == 4)
311 return 1.0F;
312 else
313 return insideCount * (1.0F / (SUB_PIXEL * SUB_PIXEL));
314 }
315
316
317 typedef void (*plot_func)(struct gl_context *ctx, struct LineInfo *line,
318 int ix, int iy);
319
320
321
322 /*
323 * Draw an AA line segment (called many times per line when stippling)
324 */
325 static void
326 segment(struct gl_context *ctx,
327 struct LineInfo *line,
328 plot_func plot,
329 GLfloat t0, GLfloat t1)
330 {
331 const GLfloat absDx = (line->dx < 0.0F) ? -line->dx : line->dx;
332 const GLfloat absDy = (line->dy < 0.0F) ? -line->dy : line->dy;
333 /* compute the actual segment's endpoints */
334 const GLfloat x0 = line->x0 + t0 * line->dx;
335 const GLfloat y0 = line->y0 + t0 * line->dy;
336 const GLfloat x1 = line->x0 + t1 * line->dx;
337 const GLfloat y1 = line->y0 + t1 * line->dy;
338
339 /* compute vertices of the line-aligned quadrilateral */
340 line->qx0 = x0 - line->yAdj;
341 line->qy0 = y0 + line->xAdj;
342 line->qx1 = x0 + line->yAdj;
343 line->qy1 = y0 - line->xAdj;
344 line->qx2 = x1 + line->yAdj;
345 line->qy2 = y1 - line->xAdj;
346 line->qx3 = x1 - line->yAdj;
347 line->qy3 = y1 + line->xAdj;
348 /* compute the quad's edge vectors (for coverage calc) */
349 line->ex0 = line->qx1 - line->qx0;
350 line->ey0 = line->qy1 - line->qy0;
351 line->ex1 = line->qx2 - line->qx1;
352 line->ey1 = line->qy2 - line->qy1;
353 line->ex2 = line->qx3 - line->qx2;
354 line->ey2 = line->qy3 - line->qy2;
355 line->ex3 = line->qx0 - line->qx3;
356 line->ey3 = line->qy0 - line->qy3;
357
358 if (absDx > absDy) {
359 /* X-major line */
360 GLfloat dydx = line->dy / line->dx;
361 GLfloat xLeft, xRight, yBot, yTop;
362 GLint ix, ixRight;
363 if (x0 < x1) {
364 xLeft = x0 - line->halfWidth;
365 xRight = x1 + line->halfWidth;
366 if (line->dy >= 0.0) {
367 yBot = y0 - 3.0F * line->halfWidth;
368 yTop = y0 + line->halfWidth;
369 }
370 else {
371 yBot = y0 - line->halfWidth;
372 yTop = y0 + 3.0F * line->halfWidth;
373 }
374 }
375 else {
376 xLeft = x1 - line->halfWidth;
377 xRight = x0 + line->halfWidth;
378 if (line->dy <= 0.0) {
379 yBot = y1 - 3.0F * line->halfWidth;
380 yTop = y1 + line->halfWidth;
381 }
382 else {
383 yBot = y1 - line->halfWidth;
384 yTop = y1 + 3.0F * line->halfWidth;
385 }
386 }
387
388 /* scan along the line, left-to-right */
389 ixRight = (GLint) (xRight + 1.0F);
390
391 /*printf("avg span height: %g\n", yTop - yBot);*/
392 for (ix = (GLint) xLeft; ix < ixRight; ix++) {
393 const GLint iyBot = (GLint) yBot;
394 const GLint iyTop = (GLint) (yTop + 1.0F);
395 GLint iy;
396 /* scan across the line, bottom-to-top */
397 for (iy = iyBot; iy < iyTop; iy++) {
398 (*plot)(ctx, line, ix, iy);
399 }
400 yBot += dydx;
401 yTop += dydx;
402 }
403 }
404 else {
405 /* Y-major line */
406 GLfloat dxdy = line->dx / line->dy;
407 GLfloat yBot, yTop, xLeft, xRight;
408 GLint iy, iyTop;
409 if (y0 < y1) {
410 yBot = y0 - line->halfWidth;
411 yTop = y1 + line->halfWidth;
412 if (line->dx >= 0.0) {
413 xLeft = x0 - 3.0F * line->halfWidth;
414 xRight = x0 + line->halfWidth;
415 }
416 else {
417 xLeft = x0 - line->halfWidth;
418 xRight = x0 + 3.0F * line->halfWidth;
419 }
420 }
421 else {
422 yBot = y1 - line->halfWidth;
423 yTop = y0 + line->halfWidth;
424 if (line->dx <= 0.0) {
425 xLeft = x1 - 3.0F * line->halfWidth;
426 xRight = x1 + line->halfWidth;
427 }
428 else {
429 xLeft = x1 - line->halfWidth;
430 xRight = x1 + 3.0F * line->halfWidth;
431 }
432 }
433
434 /* scan along the line, bottom-to-top */
435 iyTop = (GLint) (yTop + 1.0F);
436
437 /*printf("avg span width: %g\n", xRight - xLeft);*/
438 for (iy = (GLint) yBot; iy < iyTop; iy++) {
439 const GLint ixLeft = (GLint) xLeft;
440 const GLint ixRight = (GLint) (xRight + 1.0F);
441 GLint ix;
442 /* scan across the line, left-to-right */
443 for (ix = ixLeft; ix < ixRight; ix++) {
444 (*plot)(ctx, line, ix, iy);
445 }
446 xLeft += dxdy;
447 xRight += dxdy;
448 }
449 }
450 }
451
452
453 #define NAME(x) aa_rgba_##x
454 #define DO_Z
455 #include "s_aalinetemp.h"
456
457
458 #define NAME(x) aa_general_rgba_##x
459 #define DO_Z
460 #define DO_ATTRIBS
461 #include "s_aalinetemp.h"
462
463
464
465 void
466 _swrast_choose_aa_line_function(struct gl_context *ctx)
467 {
468 SWcontext *swrast = SWRAST_CONTEXT(ctx);
469
470 ASSERT(ctx->Line.SmoothFlag);
471
472 if (ctx->Texture._EnabledCoord
473 || swrast->_FogEnabled) {
474 swrast->Line = aa_general_rgba_line;
475 }
476 else {
477 swrast->Line = aa_rgba_line;
478 }
479 }