Consolidate texObj->Pos/Neg/X/Y/Z and texObj->Image into a single
[mesa.git] / src / mesa / swrast / s_aalinetemp.h
1
2 /*
3 * Mesa 3-D graphics library
4 * Version: 5.1
5 *
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /*
28 * Antialiased line template.
29 */
30
31
32 /*
33 * Function to render each fragment in the AA line.
34 */
35 static void
36 NAME(plot)(GLcontext *ctx, struct LineInfo *line, int ix, int iy)
37 {
38 const GLfloat fx = (GLfloat) ix;
39 const GLfloat fy = (GLfloat) iy;
40 const GLfloat coverage = compute_coveragef(line, ix, iy);
41 const GLuint i = line->span.end;
42
43 if (coverage == 0.0)
44 return;
45
46 line->span.end++;
47 line->span.array->coverage[i] = coverage;
48 line->span.array->x[i] = ix;
49 line->span.array->y[i] = iy;
50
51 /*
52 * Compute Z, color, texture coords, fog for the fragment by
53 * solving the plane equations at (ix,iy).
54 */
55 #ifdef DO_Z
56 line->span.array->z[i] = (GLdepth) IROUND(solve_plane(fx, fy, line->zPlane));
57 #endif
58 #ifdef DO_FOG
59 line->span.array->fog[i] = solve_plane(fx, fy, line->fPlane);
60 #endif
61 #ifdef DO_RGBA
62 line->span.array->rgba[i][RCOMP] = solve_plane_chan(fx, fy, line->rPlane);
63 line->span.array->rgba[i][GCOMP] = solve_plane_chan(fx, fy, line->gPlane);
64 line->span.array->rgba[i][BCOMP] = solve_plane_chan(fx, fy, line->bPlane);
65 line->span.array->rgba[i][ACOMP] = solve_plane_chan(fx, fy, line->aPlane);
66 #endif
67 #ifdef DO_INDEX
68 line->span.array->index[i] = (GLint) solve_plane(fx, fy, line->iPlane);
69 #endif
70 #ifdef DO_SPEC
71 line->span.array->spec[i][RCOMP] = solve_plane_chan(fx, fy, line->srPlane);
72 line->span.array->spec[i][GCOMP] = solve_plane_chan(fx, fy, line->sgPlane);
73 line->span.array->spec[i][BCOMP] = solve_plane_chan(fx, fy, line->sbPlane);
74 #endif
75 #ifdef DO_TEX
76 {
77 const GLfloat invQ = solve_plane_recip(fx, fy, line->vPlane[0]);
78 line->span.array->texcoords[0][i][0] = solve_plane(fx, fy, line->sPlane[0]) * invQ;
79 line->span.array->texcoords[0][i][1] = solve_plane(fx, fy, line->tPlane[0]) * invQ;
80 line->span.array->texcoords[0][i][2] = solve_plane(fx, fy, line->uPlane[0]) * invQ;
81 line->span.array->lambda[0][i] = compute_lambda(line->sPlane[0], line->tPlane[0], invQ,
82 line->texWidth[0], line->texHeight[0]);
83 }
84 #elif defined(DO_MULTITEX)
85 {
86 GLuint unit;
87 for (unit = 0; unit < ctx->Const.MaxTextureUnits; unit++) {
88 if (ctx->Texture.Unit[unit]._ReallyEnabled) {
89 const GLfloat invQ = solve_plane_recip(fx, fy, line->vPlane[unit]);
90 line->span.array->texcoords[unit][i][0] = solve_plane(fx, fy, line->sPlane[unit]) * invQ;
91 line->span.array->texcoords[unit][i][1] = solve_plane(fx, fy, line->tPlane[unit]) * invQ;
92 line->span.array->texcoords[unit][i][2] = solve_plane(fx, fy, line->uPlane[unit]) * invQ;
93 line->span.array->lambda[unit][i] = compute_lambda(line->sPlane[unit],
94 line->tPlane[unit], invQ,
95 line->texWidth[unit], line->texHeight[unit]);
96 }
97 }
98 }
99 #endif
100
101 if (line->span.end == MAX_WIDTH) {
102 #if defined(DO_TEX) || defined(DO_MULTITEX)
103 _swrast_write_texture_span(ctx, &(line->span));
104 #elif defined(DO_RGBA)
105 _swrast_write_rgba_span(ctx, &(line->span));
106 #else
107 _swrast_write_index_span(ctx, &(line->span));
108 #endif
109 line->span.end = 0; /* reset counter */
110 }
111 }
112
113
114
115 /*
116 * Line setup
117 */
118 static void
119 NAME(line)(GLcontext *ctx, const SWvertex *v0, const SWvertex *v1)
120 {
121 SWcontext *swrast = SWRAST_CONTEXT(ctx);
122 GLfloat tStart, tEnd; /* segment start, end along line length */
123 GLboolean inSegment;
124 GLint iLen, i;
125
126 /* Init the LineInfo struct */
127 struct LineInfo line;
128 line.x0 = v0->win[0];
129 line.y0 = v0->win[1];
130 line.x1 = v1->win[0];
131 line.y1 = v1->win[1];
132 line.dx = line.x1 - line.x0;
133 line.dy = line.y1 - line.y0;
134 line.len = SQRTF(line.dx * line.dx + line.dy * line.dy);
135 line.halfWidth = 0.5F * ctx->Line.Width;
136
137 if (line.len == 0.0 || IS_INF_OR_NAN(line.len))
138 return;
139
140 INIT_SPAN(line.span, GL_LINE, 0, 0, SPAN_XY | SPAN_COVERAGE);
141
142 line.xAdj = line.dx / line.len * line.halfWidth;
143 line.yAdj = line.dy / line.len * line.halfWidth;
144
145 #ifdef DO_Z
146 line.span.arrayMask |= SPAN_Z;
147 compute_plane(line.x0, line.y0, line.x1, line.y1,
148 v0->win[2], v1->win[2], line.zPlane);
149 #endif
150 #ifdef DO_FOG
151 line.span.arrayMask |= SPAN_FOG;
152 compute_plane(line.x0, line.y0, line.x1, line.y1,
153 v0->fog, v1->fog, line.fPlane);
154 #endif
155 #ifdef DO_RGBA
156 line.span.arrayMask |= SPAN_RGBA;
157 if (ctx->Light.ShadeModel == GL_SMOOTH) {
158 compute_plane(line.x0, line.y0, line.x1, line.y1,
159 v0->color[RCOMP], v1->color[RCOMP], line.rPlane);
160 compute_plane(line.x0, line.y0, line.x1, line.y1,
161 v0->color[GCOMP], v1->color[GCOMP], line.gPlane);
162 compute_plane(line.x0, line.y0, line.x1, line.y1,
163 v0->color[BCOMP], v1->color[BCOMP], line.bPlane);
164 compute_plane(line.x0, line.y0, line.x1, line.y1,
165 v0->color[ACOMP], v1->color[ACOMP], line.aPlane);
166 }
167 else {
168 constant_plane(v1->color[RCOMP], line.rPlane);
169 constant_plane(v1->color[GCOMP], line.gPlane);
170 constant_plane(v1->color[BCOMP], line.bPlane);
171 constant_plane(v1->color[ACOMP], line.aPlane);
172 }
173 #endif
174 #ifdef DO_SPEC
175 line.span.arrayMask |= SPAN_SPEC;
176 if (ctx->Light.ShadeModel == GL_SMOOTH) {
177 compute_plane(line.x0, line.y0, line.x1, line.y1,
178 v0->specular[RCOMP], v1->specular[RCOMP], line.srPlane);
179 compute_plane(line.x0, line.y0, line.x1, line.y1,
180 v0->specular[GCOMP], v1->specular[GCOMP], line.sgPlane);
181 compute_plane(line.x0, line.y0, line.x1, line.y1,
182 v0->specular[BCOMP], v1->specular[BCOMP], line.sbPlane);
183 }
184 else {
185 constant_plane(v1->specular[RCOMP], line.srPlane);
186 constant_plane(v1->specular[GCOMP], line.sgPlane);
187 constant_plane(v1->specular[BCOMP], line.sbPlane);
188 }
189 #endif
190 #ifdef DO_INDEX
191 line.span.arrayMask |= SPAN_INDEX;
192 if (ctx->Light.ShadeModel == GL_SMOOTH) {
193 compute_plane(line.x0, line.y0, line.x1, line.y1,
194 (GLfloat) v0->index, (GLfloat) v1->index, line.iPlane);
195 }
196 else {
197 constant_plane((GLfloat) v1->index, line.iPlane);
198 }
199 #endif
200 #ifdef DO_TEX
201 {
202 const struct gl_texture_object *obj = ctx->Texture.Unit[0]._Current;
203 const struct gl_texture_image *texImage = obj->Image[0][obj->BaseLevel];
204 const GLfloat invW0 = v0->win[3];
205 const GLfloat invW1 = v1->win[3];
206 const GLfloat s0 = v0->texcoord[0][0] * invW0;
207 const GLfloat s1 = v1->texcoord[0][0] * invW1;
208 const GLfloat t0 = v0->texcoord[0][1] * invW0;
209 const GLfloat t1 = v1->texcoord[0][1] * invW0;
210 const GLfloat r0 = v0->texcoord[0][2] * invW0;
211 const GLfloat r1 = v1->texcoord[0][2] * invW0;
212 const GLfloat q0 = v0->texcoord[0][3] * invW0;
213 const GLfloat q1 = v1->texcoord[0][3] * invW0;
214 line.span.arrayMask |= (SPAN_TEXTURE | SPAN_LAMBDA);
215 compute_plane(line.x0, line.y0, line.x1, line.y1, s0, s1, line.sPlane[0]);
216 compute_plane(line.x0, line.y0, line.x1, line.y1, t0, t1, line.tPlane[0]);
217 compute_plane(line.x0, line.y0, line.x1, line.y1, r0, r1, line.uPlane[0]);
218 compute_plane(line.x0, line.y0, line.x1, line.y1, q0, q1, line.vPlane[0]);
219 line.texWidth[0] = (GLfloat) texImage->Width;
220 line.texHeight[0] = (GLfloat) texImage->Height;
221 }
222 #elif defined(DO_MULTITEX)
223 {
224 GLuint u;
225 line.span.arrayMask |= (SPAN_TEXTURE | SPAN_LAMBDA);
226 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
227 if (ctx->Texture.Unit[u]._ReallyEnabled) {
228 const struct gl_texture_object *obj = ctx->Texture.Unit[u]._Current;
229 const struct gl_texture_image *texImage = obj->Image[0][obj->BaseLevel];
230 const GLfloat invW0 = v0->win[3];
231 const GLfloat invW1 = v1->win[3];
232 const GLfloat s0 = v0->texcoord[u][0] * invW0;
233 const GLfloat s1 = v1->texcoord[u][0] * invW1;
234 const GLfloat t0 = v0->texcoord[u][1] * invW0;
235 const GLfloat t1 = v1->texcoord[u][1] * invW0;
236 const GLfloat r0 = v0->texcoord[u][2] * invW0;
237 const GLfloat r1 = v1->texcoord[u][2] * invW0;
238 const GLfloat q0 = v0->texcoord[u][3] * invW0;
239 const GLfloat q1 = v1->texcoord[u][3] * invW0;
240 compute_plane(line.x0, line.y0, line.x1, line.y1, s0, s1, line.sPlane[u]);
241 compute_plane(line.x0, line.y0, line.x1, line.y1, t0, t1, line.tPlane[u]);
242 compute_plane(line.x0, line.y0, line.x1, line.y1, r0, r1, line.uPlane[u]);
243 compute_plane(line.x0, line.y0, line.x1, line.y1, q0, q1, line.vPlane[u]);
244 line.texWidth[u] = (GLfloat) texImage->Width;
245 line.texHeight[u] = (GLfloat) texImage->Height;
246 }
247 }
248 }
249 #endif
250
251 tStart = tEnd = 0.0;
252 inSegment = GL_FALSE;
253 iLen = (GLint) line.len;
254
255 if (ctx->Line.StippleFlag) {
256 for (i = 0; i < iLen; i++) {
257 const GLuint bit = (swrast->StippleCounter / ctx->Line.StippleFactor) & 0xf;
258 if ((1 << bit) & ctx->Line.StipplePattern) {
259 /* stipple bit is on */
260 const GLfloat t = (GLfloat) i / (GLfloat) line.len;
261 if (!inSegment) {
262 /* start new segment */
263 inSegment = GL_TRUE;
264 tStart = t;
265 }
266 else {
267 /* still in the segment, extend it */
268 tEnd = t;
269 }
270 }
271 else {
272 /* stipple bit is off */
273 if (inSegment && (tEnd > tStart)) {
274 /* draw the segment */
275 segment(ctx, &line, NAME(plot), tStart, tEnd);
276 inSegment = GL_FALSE;
277 }
278 else {
279 /* still between segments, do nothing */
280 }
281 }
282 swrast->StippleCounter++;
283 }
284
285 if (inSegment) {
286 /* draw the final segment of the line */
287 segment(ctx, &line, NAME(plot), tStart, 1.0F);
288 }
289 }
290 else {
291 /* non-stippled */
292 segment(ctx, &line, NAME(plot), 0.0, 1.0);
293 }
294
295 #if defined(DO_TEX) || defined(DO_MULTITEX)
296 _swrast_write_texture_span(ctx, &(line.span));
297 #elif defined(DO_RGBA)
298 _swrast_write_rgba_span(ctx, &(line.span));
299 #else
300 _swrast_write_index_span(ctx, &(line.span));
301 #endif
302 }
303
304
305
306
307 #undef DO_Z
308 #undef DO_FOG
309 #undef DO_RGBA
310 #undef DO_INDEX
311 #undef DO_SPEC
312 #undef DO_TEX
313 #undef DO_MULTITEX
314 #undef NAME