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