Merge branch '965-glsl'
[mesa.git] / src / mesa / swrast / s_lines.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.1
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
26 #include "glheader.h"
27 #include "context.h"
28 #include "colormac.h"
29 #include "macros.h"
30 #include "s_aaline.h"
31 #include "s_context.h"
32 #include "s_depth.h"
33 #include "s_feedback.h"
34 #include "s_lines.h"
35 #include "s_span.h"
36
37
38 /*
39 * Init the mask[] array to implement a line stipple.
40 */
41 static void
42 compute_stipple_mask( GLcontext *ctx, GLuint len, GLubyte mask[] )
43 {
44 SWcontext *swrast = SWRAST_CONTEXT(ctx);
45 GLuint i;
46
47 for (i = 0; i < len; i++) {
48 GLuint bit = (swrast->StippleCounter / ctx->Line.StippleFactor) & 0xf;
49 if ((1 << bit) & ctx->Line.StipplePattern) {
50 mask[i] = GL_TRUE;
51 }
52 else {
53 mask[i] = GL_FALSE;
54 }
55 swrast->StippleCounter++;
56 }
57 }
58
59
60 /*
61 * To draw a wide line we can simply redraw the span N times, side by side.
62 */
63 static void
64 draw_wide_line( GLcontext *ctx, SWspan *span, GLboolean xMajor )
65 {
66 const GLint width = (GLint) CLAMP(ctx->Line.Width,
67 ctx->Const.MinLineWidth,
68 ctx->Const.MaxLineWidth);
69 GLint start;
70
71 ASSERT(span->end < MAX_WIDTH);
72
73 if (width & 1)
74 start = width / 2;
75 else
76 start = width / 2 - 1;
77
78 if (xMajor) {
79 GLint *y = span->array->y;
80 GLuint i;
81 GLint w;
82 for (w = 0; w < width; w++) {
83 if (w == 0) {
84 for (i = 0; i < span->end; i++)
85 y[i] -= start;
86 }
87 else {
88 for (i = 0; i < span->end; i++)
89 y[i]++;
90 }
91 if (ctx->Visual.rgbMode)
92 _swrast_write_rgba_span(ctx, span);
93 else
94 _swrast_write_index_span(ctx, span);
95 }
96 }
97 else {
98 GLint *x = span->array->x;
99 GLuint i;
100 GLint w;
101 for (w = 0; w < width; w++) {
102 if (w == 0) {
103 for (i = 0; i < span->end; i++)
104 x[i] -= start;
105 }
106 else {
107 for (i = 0; i < span->end; i++)
108 x[i]++;
109 }
110 if (ctx->Visual.rgbMode)
111 _swrast_write_rgba_span(ctx, span);
112 else
113 _swrast_write_index_span(ctx, span);
114 }
115 }
116 }
117
118
119
120 /**********************************************************************/
121 /***** Rasterization *****/
122 /**********************************************************************/
123
124 /* Simple color index line (no stipple, width=1, no Z, no fog, no tex)*/
125 #define NAME simple_no_z_ci_line
126 #define INTERP_INDEX
127 #define RENDER_SPAN(span) _swrast_write_index_span(ctx, &span)
128 #include "s_linetemp.h"
129
130 /* Simple RGBA index line (no stipple, width=1, no Z, no fog, no tex)*/
131 #define NAME simple_no_z_rgba_line
132 #define INTERP_RGBA
133 #define RENDER_SPAN(span) _swrast_write_rgba_span(ctx, &span);
134 #include "s_linetemp.h"
135
136
137 /* Z, fog, wide, stipple color index line */
138 #define NAME ci_line
139 #define INTERP_INDEX
140 #define INTERP_Z
141 #define INTERP_ATTRIBS /* for fog */
142 #define RENDER_SPAN(span) \
143 if (ctx->Line.StippleFlag) { \
144 span.arrayMask |= SPAN_MASK; \
145 compute_stipple_mask(ctx, span.end, span.array->mask); \
146 } \
147 if (ctx->Line.Width > 1.0) { \
148 draw_wide_line(ctx, &span, (GLboolean)(dx > dy)); \
149 } \
150 else { \
151 _swrast_write_index_span(ctx, &span); \
152 }
153 #include "s_linetemp.h"
154
155
156 /* Z, fog, wide, stipple RGBA line */
157 #define NAME rgba_line
158 #define INTERP_RGBA
159 #define INTERP_Z
160 #define RENDER_SPAN(span) \
161 if (ctx->Line.StippleFlag) { \
162 span.arrayMask |= SPAN_MASK; \
163 compute_stipple_mask(ctx, span.end, span.array->mask); \
164 } \
165 if (ctx->Line.Width > 1.0) { \
166 draw_wide_line(ctx, &span, (GLboolean)(dx > dy)); \
167 } \
168 else { \
169 _swrast_write_rgba_span(ctx, &span); \
170 }
171 #include "s_linetemp.h"
172
173
174 /* General-purpose line (any/all features). */
175 #define NAME general_line
176 #define INTERP_RGBA
177 #define INTERP_Z
178 #define INTERP_ATTRIBS
179 #define RENDER_SPAN(span) \
180 if (ctx->Line.StippleFlag) { \
181 span.arrayMask |= SPAN_MASK; \
182 compute_stipple_mask(ctx, span.end, span.array->mask); \
183 } \
184 if (ctx->Line.Width > 1.0) { \
185 draw_wide_line(ctx, &span, (GLboolean)(dx > dy)); \
186 } \
187 else { \
188 _swrast_write_rgba_span(ctx, &span); \
189 }
190 #include "s_linetemp.h"
191
192
193
194 void
195 _swrast_add_spec_terms_line(GLcontext *ctx,
196 const SWvertex *v0, const SWvertex *v1)
197 {
198 SWvertex *ncv0 = (SWvertex *)v0;
199 SWvertex *ncv1 = (SWvertex *)v1;
200 GLfloat rSum, gSum, bSum;
201 GLchan cSave[2][4];
202
203 /* save original colors */
204 COPY_CHAN4(cSave[0], ncv0->color);
205 COPY_CHAN4(cSave[1], ncv1->color);
206 /* sum v0 */
207 rSum = CHAN_TO_FLOAT(ncv0->color[0]) + ncv0->attrib[FRAG_ATTRIB_COL1][0];
208 gSum = CHAN_TO_FLOAT(ncv0->color[1]) + ncv0->attrib[FRAG_ATTRIB_COL1][1];
209 bSum = CHAN_TO_FLOAT(ncv0->color[2]) + ncv0->attrib[FRAG_ATTRIB_COL1][2];
210 UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[0], rSum);
211 UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[1], gSum);
212 UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[2], bSum);
213 /* sum v1 */
214 rSum = CHAN_TO_FLOAT(ncv1->color[0]) + ncv1->attrib[FRAG_ATTRIB_COL1][0];
215 gSum = CHAN_TO_FLOAT(ncv1->color[1]) + ncv1->attrib[FRAG_ATTRIB_COL1][1];
216 bSum = CHAN_TO_FLOAT(ncv1->color[2]) + ncv1->attrib[FRAG_ATTRIB_COL1][2];
217 UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[0], rSum);
218 UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[1], gSum);
219 UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[2], bSum);
220 /* draw */
221 SWRAST_CONTEXT(ctx)->SpecLine( ctx, ncv0, ncv1 );
222 /* restore original colors */
223 COPY_CHAN4( ncv0->attrib[FRAG_ATTRIB_COL0], cSave[0] );
224 COPY_CHAN4( ncv1->attrib[FRAG_ATTRIB_COL0], cSave[1] );
225 }
226
227
228
229 #ifdef DEBUG
230
231 /* record the current line function name */
232 static const char *lineFuncName = NULL;
233
234 #define USE(lineFunc) \
235 do { \
236 lineFuncName = #lineFunc; \
237 /*_mesa_printf("%s\n", lineFuncName);*/ \
238 swrast->Line = lineFunc; \
239 } while (0)
240
241 #else
242
243 #define USE(lineFunc) swrast->Line = lineFunc
244
245 #endif
246
247
248
249 /**
250 * Determine which line drawing function to use given the current
251 * rendering context.
252 *
253 * Please update the summary flag _SWRAST_NEW_LINE if you add or remove
254 * tests to this code.
255 */
256 void
257 _swrast_choose_line( GLcontext *ctx )
258 {
259 SWcontext *swrast = SWRAST_CONTEXT(ctx);
260 const GLboolean rgbmode = ctx->Visual.rgbMode;
261 GLboolean specular = (ctx->Fog.ColorSumEnabled ||
262 (ctx->Light.Enabled &&
263 ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR));
264
265 if (ctx->RenderMode == GL_RENDER) {
266 if (ctx->Line.SmoothFlag) {
267 /* antialiased lines */
268 _swrast_choose_aa_line_function(ctx);
269 ASSERT(swrast->Line);
270 }
271 else if (ctx->Texture._EnabledCoordUnits
272 || ctx->FragmentProgram._Current
273 || swrast->_FogEnabled
274 || specular) {
275 USE(general_line);
276 }
277 else if (ctx->Depth.Test
278 || ctx->Line.Width != 1.0
279 || ctx->Line.StippleFlag) {
280 /* no texture, but Z, fog, width>1, stipple, etc. */
281 if (rgbmode)
282 #if CHAN_BITS == 32
283 USE(general_line);
284 #else
285 USE(rgba_line);
286 #endif
287 else
288 USE(ci_line);
289 }
290 else {
291 ASSERT(!ctx->Depth.Test);
292 ASSERT(ctx->Line.Width == 1.0);
293 /* simple lines */
294 if (rgbmode)
295 USE(simple_no_z_rgba_line);
296 else
297 USE(simple_no_z_ci_line);
298 }
299 }
300 else if (ctx->RenderMode == GL_FEEDBACK) {
301 USE(_swrast_feedback_line);
302 }
303 else {
304 ASSERT(ctx->RenderMode == GL_SELECT);
305 USE(_swrast_select_line);
306 }
307 }