New implementation of antialiased lines. Mesa should now pass the remaining
[mesa.git] / src / mesa / swrast / s_aalinetemp.h
1 /* $Id: s_aalinetemp.h,v 1.1 2000/11/05 23:15:16 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 line template.
30 */
31
32
33 /*
34 * Function to render each fragment in the AA line.
35 */
36 static void
37 NAME(plot)(GLcontext *ctx, const struct LineInfo *line,
38 struct pixel_buffer *pb, int ix, int iy)
39 {
40 const GLfloat fx = (GLfloat) ix;
41 const GLfloat fy = (GLfloat) iy;
42 const GLfloat coverage = compute_coveragef(line, ix, iy);
43 GLdepth z;
44 GLfloat fog;
45 GLchan red, green, blue, alpha;
46 GLint frac, indx, index;
47 GLchan specRed, specGreen, specBlue;
48 GLfloat tex[MAX_TEXTURE_UNITS][4], lambda[MAX_TEXTURE_UNITS];
49
50 if (coverage == 0.0)
51 return;
52
53 /*
54 * Compute Z, color, texture coords, fog for the fragment by
55 * solving the plane equations at (ix,iy).
56 */
57 #ifdef DO_Z
58 z = (GLdepth) solve_plane(fx, fy, line->zPlane);
59 #else
60 z = 0.0;
61 #endif
62 #ifdef DO_FOG
63 fog = solve_plane(fx, fy, line->fPlane);
64 #else
65 fog = 0.0;
66 #endif
67 #ifdef DO_RGBA
68 red = solve_plane_chan(fx, fy, line->rPlane);
69 green = solve_plane_chan(fx, fy, line->gPlane);
70 blue = solve_plane_chan(fx, fy, line->bPlane);
71 alpha = (GLchan) (solve_plane_chan(fx, fy, line->aPlane) * coverage);;
72 #else
73 (void) red;
74 (void) green;
75 (void) blue;
76 (void) alpha;
77 #endif
78 #ifdef DO_INDEX
79 frac = (GLint) (15.0 * coverage);
80 indx = (GLint) solve_plane(fx, fy, line->iPlane);
81 index = (indx & ~0xf) | frac;
82 #else
83 (void) frac;
84 (void) indx;
85 (void) index;
86 #endif
87 #ifdef DO_SPEC
88 specRed = solve_plane_chan(fx, fy, line->srPlane);
89 specGreen = solve_plane_chan(fx, fy, line->sgPlane);
90 specBlue = solve_plane_chan(fx, fy, line->sbPlane);
91 #else
92 (void) specRed;
93 (void) specGreen;
94 (void) specBlue;
95 #endif
96 #ifdef DO_TEX
97 {
98 GLfloat invQ = solve_plane_recip(fx, fy, line->vPlane[0]);
99 tex[0][0] = solve_plane(fx, fy, line->sPlane[0]) * invQ;
100 tex[0][1] = solve_plane(fx, fy, line->tPlane[0]) * invQ;
101 tex[0][2] = solve_plane(fx, fy, line->uPlane[0]) * invQ;
102 lambda[0] = compute_lambda(line->sPlane[0], line->tPlane[0], invQ,
103 line->texWidth[0], line->texHeight[0]);
104 }
105 #elif defined(DO_MULTITEX)
106 {
107 GLuint unit;
108 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
109 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
110 GLfloat invQ = solve_plane_recip(fx, fy, line->vPlane[unit]);
111 tex[unit][0] = solve_plane(fx, fy, line->sPlane[unit]) * invQ;
112 tex[unit][1] = solve_plane(fx, fy, line->tPlane[unit]) * invQ;
113 tex[unit][2] = solve_plane(fx, fy, line->uPlane[unit]) * invQ;
114 lambda[unit] = compute_lambda(line->sPlane[unit],
115 line->tPlane[unit], invQ,
116 line->texWidth[unit], line->texHeight[unit]);
117 }
118 }
119 }
120 #else
121 (void) tex[0][0];
122 (void) lambda[0];
123 #endif
124
125
126 #if defined(DO_MULTITEX)
127 #if defined(DO_SPEC)
128 PB_WRITE_MULTITEX_SPEC_PIXEL(pb, ix, iy, z, fog, red, green, blue, alpha,
129 specRed, specGreen, specBlue, tex);
130 #else
131 PB_WRITE_MULTITEX_PIXEL(pb, ix, iy, z, fog, red, green, blue, alpha, texcoords);
132 #endif
133 #elif defined(DO_TEX)
134 PB_WRITE_TEX_PIXEL(pb, ix, iy, z, fog, red, green, blue, alpha,
135 tex[0][0], tex[0][1], tex[0][2]);
136 #elif defined(DO_RGBA)
137 PB_WRITE_RGBA_PIXEL(pb, ix, iy, z, fog, red, green, blue, alpha);
138 #elif defined(DO_INDEX)
139 PB_WRITE_CI_PIXEL(pb, ix, iy, z, fog, index);
140 #endif
141
142 PB_CHECK_FLUSH(ctx, pb);
143 }
144
145
146
147 /*
148 * Line setup
149 */
150 static void
151 NAME(line)(GLcontext *ctx, SWvertex *v0, SWvertex *v1)
152 {
153 SWcontext *swrast = SWRAST_CONTEXT(ctx);
154 struct pixel_buffer *pb = SWRAST_CONTEXT(ctx)->PB;
155 GLfloat tStart, tEnd; /* segment start, end along line length */
156 GLboolean inSegment;
157 GLint iLen, i;
158
159 /* Init the LineInfo struct */
160 struct LineInfo line;
161 line.x0 = v0->win[0];
162 line.y0 = v0->win[1];
163 line.x1 = v1->win[0];
164 line.y1 = v1->win[1];
165 line.dx = line.x1 - line.x0;
166 line.dy = line.y1 - line.y0;
167 line.len = sqrt(line.dx * line.dx + line.dy * line.dy);
168 line.halfWidth = 0.5F * ctx->Line.Width;
169 line.xAdj = line.dx / line.len * line.halfWidth;
170 line.yAdj = line.dy / line.len * line.halfWidth;
171
172 #ifdef DO_Z
173 compute_plane(line.x0, line.y0, line.x1, line.y1,
174 v0->win[2], v1->win[2], line.zPlane);
175 #endif
176 #ifdef DO_FOG
177 compute_plane(line.x0, line.y0, line.x1, line.y1,
178 v0->fog, v1->fog, line.fPlane);
179 #endif
180 #ifdef DO_RGBA
181 if (ctx->Light.ShadeModel == GL_SMOOTH) {
182 compute_plane(line.x0, line.y0, line.x1, line.y1,
183 v0->color[RCOMP], v1->color[RCOMP], line.rPlane);
184 compute_plane(line.x0, line.y0, line.x1, line.y1,
185 v0->color[GCOMP], v1->color[GCOMP], line.gPlane);
186 compute_plane(line.x0, line.y0, line.x1, line.y1,
187 v0->color[BCOMP], v1->color[BCOMP], line.bPlane);
188 compute_plane(line.x0, line.y0, line.x1, line.y1,
189 v0->color[ACOMP], v1->color[ACOMP], line.aPlane);
190 }
191 else {
192 constant_plane(v0->color[RCOMP], line.rPlane);
193 constant_plane(v0->color[GCOMP], line.gPlane);
194 constant_plane(v0->color[BCOMP], line.bPlane);
195 constant_plane(v0->color[ACOMP], line.aPlane);
196 }
197 #endif
198 #ifdef DO_SPEC
199 if (ctx->Light.ShadeModel == GL_SMOOTH) {
200 compute_plane(line.x0, line.y0, line.x1, line.y1,
201 v0->specular[RCOMP], v1->specular[RCOMP], line.srPlane);
202 compute_plane(line.x0, line.y0, line.x1, line.y1,
203 v0->specular[GCOMP], v1->specular[GCOMP], line.sgPlane);
204 compute_plane(line.x0, line.y0, line.x1, line.y1,
205 v0->specular[BCOMP], v1->specular[BCOMP], line.sbPlane);
206 }
207 else {
208 constant_plane(v0->specular[RCOMP], line.srPlane);
209 constant_plane(v0->specular[GCOMP], line.sgPlane);
210 constant_plane(v0->specular[BCOMP], line.sbPlane);
211 }
212 #endif
213 #ifdef DO_INDEX
214 if (ctx->Light.ShadeModel == GL_SMOOTH) {
215 compute_plane(line.x0, line.y0, line.x1, line.y1,
216 v0->index, v1->index, line.iPlane);
217 }
218 else {
219 constant_plane(v0->index, line.iPlane);
220 }
221 #endif
222 #ifdef DO_TEX
223 {
224 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current;
225 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];
226 const GLfloat invW0 = v0->win[3];
227 const GLfloat invW1 = v1->win[3];
228 const GLfloat s0 = v0->texcoord[0][0] * invW0;
229 const GLfloat s1 = v1->texcoord[0][0] * invW1;
230 const GLfloat t0 = v0->texcoord[0][1] * invW0;
231 const GLfloat t1 = v1->texcoord[0][1] * invW0;
232 const GLfloat r0 = v0->texcoord[0][2] * invW0;
233 const GLfloat r1 = v1->texcoord[0][2] * invW0;
234 const GLfloat q0 = v0->texcoord[0][3] * invW0;
235 const GLfloat q1 = v1->texcoord[0][3] * invW0;
236 compute_plane(line.x0, line.y0, line.x1, line.y1, s0, s1, line.sPlane[0]);
237 compute_plane(line.x0, line.y0, line.x1, line.y1, t0, t1, line.tPlane[0]);
238 compute_plane(line.x0, line.y0, line.x1, line.y1, r0, r1, line.uPlane[0]);
239 compute_plane(line.x0, line.y0, line.x1, line.y1, q0, q1, line.vPlane[0]);
240 line.texWidth[0] = (GLfloat) texImage->Width;
241 line.texHeight[0] = (GLfloat) texImage->Height;
242 }
243 #elif defined(DO_MULITEX)
244 {
245 GLuint u;
246 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
247 if (ctx->Texture.Unit[u].ReallyEnabled) {
248 const struct gl_texture_object *obj = ctx->Texture.Unit[u].Current;
249 const struct gl_texture_image *texImage = obj->Image[obj->BaseLevel];
250 const GLfloat invW0 = v0->win[3];
251 const GLfloat invW1 = v1->win[3];
252 GLfloat (*texCoord)[4] = VB->TexCoordPtr[u]->data;
253 const GLfloat s0 = v0->texcoord[u][0] * invW0;
254 const GLfloat s1 = v1->texcoord[u][0] * invW1;
255 const GLfloat t0 = v0->texcoord[u][1] * invW0;
256 const GLfloat t1 = v1->texcoord[u][1] * invW0;
257 const GLfloat r0 = v0->texcoord[u][2] * invW0;
258 const GLfloat r1 = v1->texcoord[u][2] * invW0;
259 const GLfloat q0 = v0->texcoord[u][3] * invW0;
260 const GLfloat q1 = v1->texcoord[u][3] * invW0;
261 compute_plane(line.x0, line.y0, line.x1, line.y1, s0, s1, line.sPlane[u]);
262 compute_plane(line.x0, line.y0, line.x1, line.y1, t0, t1, line.tPlane[u]);
263 compute_plane(line.x0, line.y0, line.x1, line.y1, u0, u1, line.uPlane[u]);
264 compute_plane(line.x0, line.y0, line.x1, line.y1, v0, v1, line.vPlane[u]);
265 line.texWidth[u] = (GLfloat) texImage->Width;
266 line.texHeight[u] = (GLfloat) texImage->Height;
267 }
268 }
269 }
270 #endif
271
272 tStart = tEnd = 0.0;
273 inSegment = GL_FALSE;
274 iLen = (GLint) line.len;
275
276 if (ctx->Line.StippleFlag) {
277 for (i = 0; i < iLen; i++) {
278 const GLuint bit = (swrast->StippleCounter / ctx->Line.StippleFactor) & 0xf;
279 if ((1 << bit) & ctx->Line.StipplePattern) {
280 /* stipple bit is on */
281 const GLfloat t = (GLfloat) i / (GLfloat) line.len;
282 if (!inSegment) {
283 /* start new segment */
284 inSegment = GL_TRUE;
285 tStart = t;
286 }
287 else {
288 /* still in the segment, extend it */
289 tEnd = t;
290 }
291 }
292 else {
293 /* stipple bit is off */
294 if (inSegment && (tEnd > tStart)) {
295 /* draw the segment */
296 segment(ctx, &line, NAME(plot), pb, tStart, tEnd);
297 inSegment = GL_FALSE;
298 }
299 else {
300 /* still between segments, do nothing */
301 }
302 }
303 swrast->StippleCounter++;
304 }
305 }
306 else {
307 /* non-stippled */
308 segment(ctx, &line, NAME(plot), pb, 0.0, 1.0);
309 }
310 }
311
312
313
314
315 #undef DO_Z
316 #undef DO_FOG
317 #undef DO_RGBA
318 #undef DO_INDEX
319 #undef DO_SPEC
320 #undef DO_TEX
321 #undef DO_MULTITEX
322 #undef NAME