blob: bebb131a5d1e4a3e3fa27c7d2bf4bb7795d30e84 [file] [log] [blame]
Brian Paul77983742000-11-05 23:15:16 +00001/*
2 * Mesa 3-D graphics library
Gareth Hughes22144ab2001-03-12 00:48:37 +00003 *
Briandd34fe82007-02-05 10:10:01 -07004 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
Gareth Hughes22144ab2001-03-12 00:48:37 +00005 *
Brian Paul77983742000-11-05 23:15:16 +00006 * 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:
Gareth Hughes22144ab2001-03-12 00:48:37 +000012 *
Brian Paul77983742000-11-05 23:15:16 +000013 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
Gareth Hughes22144ab2001-03-12 00:48:37 +000015 *
Brian Paul77983742000-11-05 23:15:16 +000016 * 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
Kenneth Graunke3d8d5b22013-04-21 13:46:48 -070019 * 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.
Brian Paul77983742000-11-05 23:15:16 +000023 */
24
25
26/*
27 * Antialiased line template.
28 */
29
30
31/*
32 * Function to render each fragment in the AA line.
Brian Paula803b0c2004-12-18 22:03:07 +000033 * \param ix - integer fragment window X coordiante
34 * \param iy - integer fragment window Y coordiante
Brian Paul77983742000-11-05 23:15:16 +000035 */
36static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -040037NAME(plot)(struct gl_context *ctx, struct LineInfo *line, int ix, int iy)
Brian Paul77983742000-11-05 23:15:16 +000038{
Briandd34fe82007-02-05 10:10:01 -070039 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
Brian Paul77983742000-11-05 23:15:16 +000040 const GLfloat fx = (GLfloat) ix;
41 const GLfloat fy = (GLfloat) iy;
42 const GLfloat coverage = compute_coveragef(line, ix, iy);
Brian Paul77df8872002-08-07 00:45:07 +000043 const GLuint i = line->span.end;
Brian Paul77983742000-11-05 23:15:16 +000044
Briandd34fe82007-02-05 10:10:01 -070045 (void) swrast;
46
Matt Turner04aa8b52015-07-12 23:15:10 -070047 if (coverage == 0.0F)
Brian Paul77983742000-11-05 23:15:16 +000048 return;
49
Brian Paul77df8872002-08-07 00:45:07 +000050 line->span.end++;
51 line->span.array->coverage[i] = coverage;
52 line->span.array->x[i] = ix;
53 line->span.array->y[i] = iy;
Brian Paul733a4b62002-02-02 17:24:11 +000054
Brian Paul77983742000-11-05 23:15:16 +000055 /*
56 * Compute Z, color, texture coords, fog for the fragment by
57 * solving the plane equations at (ix,iy).
58 */
59#ifdef DO_Z
Brian Paul3e37baf2005-09-21 02:47:32 +000060 line->span.array->z[i] = (GLuint) solve_plane(fx, fy, line->zPlane);
Brian Paul77983742000-11-05 23:15:16 +000061#endif
Brian Paul77df8872002-08-07 00:45:07 +000062 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);
Brianeca456b2007-04-24 16:57:38 -060066#if defined(DO_ATTRIBS)
Brian0bdf2162007-04-23 21:21:52 -060067 ATTRIB_LOOP_BEGIN
68 GLfloat (*attribArray)[4] = line->span.array->attribs[attr];
Paul Berryeed6baf2013-02-23 09:00:58 -080069 if (attr >= VARYING_SLOT_TEX0 && attr < VARYING_SLOT_VAR0
Chad Versace1c0f1dd2012-01-25 19:38:10 -080070 && !_swrast_use_fragment_program(ctx)) {
Brian9e8a9612007-05-20 12:27:39 -060071 /* texcoord w/ divide by Q */
Paul Berryeed6baf2013-02-23 09:00:58 -080072 const GLuint unit = attr - VARYING_SLOT_TEX0;
Brian9e8a9612007-05-20 12:27:39 -060073 const GLfloat invQ = solve_plane_recip(fx, fy, line->attrPlane[attr][3]);
74 GLuint c;
75 for (c = 0; c < 3; c++) {
76 attribArray[i][c] = solve_plane(fx, fy, line->attrPlane[attr][c]) * invQ;
77 }
78 line->span.array->lambda[unit][i]
79 = compute_lambda(line->attrPlane[attr][0],
80 line->attrPlane[attr][1], invQ,
81 line->texWidth[attr], line->texHeight[attr]);
Brian Paul77983742000-11-05 23:15:16 +000082 }
Brian0bdf2162007-04-23 21:21:52 -060083 else {
Brian9e8a9612007-05-20 12:27:39 -060084 /* non-texture attrib */
85 const GLfloat invW = solve_plane_recip(fx, fy, line->wPlane);
86 GLuint c;
87 for (c = 0; c < 4; c++) {
88 attribArray[i][c] = solve_plane(fx, fy, line->attrPlane[attr][c]) * invW;
89 }
Brian0bdf2162007-04-23 21:21:52 -060090 }
91 ATTRIB_LOOP_END
Brian Paul77983742000-11-05 23:15:16 +000092#endif
93
Brian Paul47d88ef2012-02-20 11:07:00 -070094 if (line->span.end == SWRAST_MAX_WIDTH) {
Brian Paul45bc8872003-03-25 02:23:44 +000095 _swrast_write_rgba_span(ctx, &(line->span));
Brian Paul77df8872002-08-07 00:45:07 +000096 line->span.end = 0; /* reset counter */
Brian Paul733a4b62002-02-02 17:24:11 +000097 }
Brian Paul77983742000-11-05 23:15:16 +000098}
99
100
101
102/*
103 * Line setup
104 */
105static void
Kristian Høgsbergf9995b32010-10-12 12:26:10 -0400106NAME(line)(struct gl_context *ctx, const SWvertex *v0, const SWvertex *v1)
Brian Paul77983742000-11-05 23:15:16 +0000107{
108 SWcontext *swrast = SWRAST_CONTEXT(ctx);
Brian Paul77983742000-11-05 23:15:16 +0000109 GLfloat tStart, tEnd; /* segment start, end along line length */
110 GLboolean inSegment;
111 GLint iLen, i;
112
113 /* Init the LineInfo struct */
114 struct LineInfo line;
Paul Berryeed6baf2013-02-23 09:00:58 -0800115 line.x0 = v0->attrib[VARYING_SLOT_POS][0];
116 line.y0 = v0->attrib[VARYING_SLOT_POS][1];
117 line.x1 = v1->attrib[VARYING_SLOT_POS][0];
118 line.y1 = v1->attrib[VARYING_SLOT_POS][1];
Brian Paul77983742000-11-05 23:15:16 +0000119 line.dx = line.x1 - line.x0;
120 line.dy = line.y1 - line.y0;
Brian Paul56ccdf72012-09-03 12:19:15 -0600121 line.len = sqrtf(line.dx * line.dx + line.dy * line.dy);
Brianaf2aa8e2007-07-21 10:06:18 -0600122 line.halfWidth = 0.5F * CLAMP(ctx->Line.Width,
123 ctx->Const.MinLineWidthAA,
124 ctx->Const.MaxLineWidthAA);
Brian Pauldbed2022001-05-10 17:41:41 +0000125
Matt Turner04aa8b52015-07-12 23:15:10 -0700126 if (line.len == 0.0F || IS_INF_OR_NAN(line.len))
Brian Pauldbed2022001-05-10 17:41:41 +0000127 return;
128
Brianf4b103d2007-06-29 21:52:18 -0600129 INIT_SPAN(line.span, GL_LINE);
130 line.span.arrayMask = SPAN_XY | SPAN_COVERAGE;
Brianfcd7c372007-11-30 13:01:57 -0700131 line.span.facing = swrast->PointLineFacing;
Brian Pauldbed2022001-05-10 17:41:41 +0000132 line.xAdj = line.dx / line.len * line.halfWidth;
133 line.yAdj = line.dy / line.len * line.halfWidth;
Brian Paul77983742000-11-05 23:15:16 +0000134
135#ifdef DO_Z
Brian Paul77df8872002-08-07 00:45:07 +0000136 line.span.arrayMask |= SPAN_Z;
Brian Paul77983742000-11-05 23:15:16 +0000137 compute_plane(line.x0, line.y0, line.x1, line.y1,
Paul Berryeed6baf2013-02-23 09:00:58 -0800138 v0->attrib[VARYING_SLOT_POS][2], v1->attrib[VARYING_SLOT_POS][2], line.zPlane);
Brian Paul77983742000-11-05 23:15:16 +0000139#endif
Brian Paul77df8872002-08-07 00:45:07 +0000140 line.span.arrayMask |= SPAN_RGBA;
Brian Paul77983742000-11-05 23:15:16 +0000141 if (ctx->Light.ShadeModel == GL_SMOOTH) {
142 compute_plane(line.x0, line.y0, line.x1, line.y1,
143 v0->color[RCOMP], v1->color[RCOMP], line.rPlane);
144 compute_plane(line.x0, line.y0, line.x1, line.y1,
145 v0->color[GCOMP], v1->color[GCOMP], line.gPlane);
146 compute_plane(line.x0, line.y0, line.x1, line.y1,
147 v0->color[BCOMP], v1->color[BCOMP], line.bPlane);
148 compute_plane(line.x0, line.y0, line.x1, line.y1,
149 v0->color[ACOMP], v1->color[ACOMP], line.aPlane);
150 }
151 else {
Keith Whitwell58e99172001-01-05 02:26:48 +0000152 constant_plane(v1->color[RCOMP], line.rPlane);
153 constant_plane(v1->color[GCOMP], line.gPlane);
154 constant_plane(v1->color[BCOMP], line.bPlane);
155 constant_plane(v1->color[ACOMP], line.aPlane);
Brian Paul77983742000-11-05 23:15:16 +0000156 }
Brianeca456b2007-04-24 16:57:38 -0600157#if defined(DO_ATTRIBS)
Brian Paul77983742000-11-05 23:15:16 +0000158 {
Paul Berryeed6baf2013-02-23 09:00:58 -0800159 const GLfloat invW0 = v0->attrib[VARYING_SLOT_POS][3];
160 const GLfloat invW1 = v1->attrib[VARYING_SLOT_POS][3];
Brian9e8a9612007-05-20 12:27:39 -0600161 line.span.arrayMask |= SPAN_LAMBDA;
162 compute_plane(line.x0, line.y0, line.x1, line.y1, invW0, invW1, line.wPlane);
Brian0bdf2162007-04-23 21:21:52 -0600163 ATTRIB_LOOP_BEGIN
Brian9e8a9612007-05-20 12:27:39 -0600164 GLuint c;
Brian5237f862007-05-25 16:22:15 -0600165 if (swrast->_InterpMode[attr] == GL_FLAT) {
166 for (c = 0; c < 4; c++) {
167 constant_plane(v1->attrib[attr][c], line.attrPlane[attr][c]);
168 }
169 }
170 else {
171 for (c = 0; c < 4; c++) {
172 const GLfloat a0 = v0->attrib[attr][c] * invW0;
173 const GLfloat a1 = v1->attrib[attr][c] * invW1;
174 compute_plane(line.x0, line.y0, line.x1, line.y1, a0, a1,
175 line.attrPlane[attr][c]);
176 }
Brian9e8a9612007-05-20 12:27:39 -0600177 }
Brian Paul706400f2012-01-12 09:54:04 -0700178 line.span.arrayAttribs |= BITFIELD64_BIT(attr);
Paul Berryeed6baf2013-02-23 09:00:58 -0800179 if (attr >= VARYING_SLOT_TEX0 && attr < VARYING_SLOT_VAR0) {
180 const GLuint u = attr - VARYING_SLOT_TEX0;
Brian0bdf2162007-04-23 21:21:52 -0600181 const struct gl_texture_object *obj = ctx->Texture.Unit[u]._Current;
Brian Paul05279fa2015-01-02 16:56:12 -0700182 const struct gl_texture_image *texImage =
183 _mesa_base_tex_image(obj);
Brian0bdf2162007-04-23 21:21:52 -0600184 line.texWidth[attr] = (GLfloat) texImage->Width;
Brianeca456b2007-04-24 16:57:38 -0600185 line.texHeight[attr] = (GLfloat) texImage->Height;
Brian Paul77983742000-11-05 23:15:16 +0000186 }
Brian0bdf2162007-04-23 21:21:52 -0600187 ATTRIB_LOOP_END
Brian Paul77983742000-11-05 23:15:16 +0000188 }
189#endif
190
191 tStart = tEnd = 0.0;
192 inSegment = GL_FALSE;
193 iLen = (GLint) line.len;
194
195 if (ctx->Line.StippleFlag) {
196 for (i = 0; i < iLen; i++) {
197 const GLuint bit = (swrast->StippleCounter / ctx->Line.StippleFactor) & 0xf;
198 if ((1 << bit) & ctx->Line.StipplePattern) {
199 /* stipple bit is on */
200 const GLfloat t = (GLfloat) i / (GLfloat) line.len;
201 if (!inSegment) {
202 /* start new segment */
203 inSegment = GL_TRUE;
204 tStart = t;
205 }
206 else {
207 /* still in the segment, extend it */
208 tEnd = t;
209 }
210 }
211 else {
212 /* stipple bit is off */
213 if (inSegment && (tEnd > tStart)) {
214 /* draw the segment */
Brian Paul733a4b62002-02-02 17:24:11 +0000215 segment(ctx, &line, NAME(plot), tStart, tEnd);
Brian Paul77983742000-11-05 23:15:16 +0000216 inSegment = GL_FALSE;
217 }
218 else {
219 /* still between segments, do nothing */
220 }
221 }
222 swrast->StippleCounter++;
223 }
Brian Paul426628c2001-01-29 23:38:41 +0000224
225 if (inSegment) {
226 /* draw the final segment of the line */
Brian Paul733a4b62002-02-02 17:24:11 +0000227 segment(ctx, &line, NAME(plot), tStart, 1.0F);
Brian Paul426628c2001-01-29 23:38:41 +0000228 }
Brian Paul77983742000-11-05 23:15:16 +0000229 }
230 else {
231 /* non-stippled */
Brian Paul733a4b62002-02-02 17:24:11 +0000232 segment(ctx, &line, NAME(plot), 0.0, 1.0);
Brian Paul77983742000-11-05 23:15:16 +0000233 }
Brian Paul733a4b62002-02-02 17:24:11 +0000234
Brian Paul45bc8872003-03-25 02:23:44 +0000235 _swrast_write_rgba_span(ctx, &(line.span));
Brian Paul77983742000-11-05 23:15:16 +0000236}
237
238
239
240
241#undef DO_Z
Brianeca456b2007-04-24 16:57:38 -0600242#undef DO_ATTRIBS
Brian Paul77983742000-11-05 23:15:16 +0000243#undef NAME