merge from master
[mesa.git] / src / mesa / swrast / s_lines.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2005 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 GLint width, start;
67
68 ASSERT(span->end < MAX_WIDTH);
69
70 width = (GLint) CLAMP( ctx->Line._Width, MIN_LINE_WIDTH, MAX_LINE_WIDTH );
71
72 if (width & 1)
73 start = width / 2;
74 else
75 start = width / 2 - 1;
76
77 if (xMajor) {
78 GLint *y = span->array->y;
79 GLuint i;
80 GLint w;
81 for (w = 0; w < width; w++) {
82 if (w == 0) {
83 for (i = 0; i < span->end; i++)
84 y[i] -= start;
85 }
86 else {
87 for (i = 0; i < span->end; i++)
88 y[i]++;
89 }
90 if (ctx->Visual.rgbMode)
91 _swrast_write_rgba_span(ctx, span);
92 else
93 _swrast_write_index_span(ctx, span);
94 }
95 }
96 else {
97 GLint *x = span->array->x;
98 GLuint i;
99 GLint w;
100 for (w = 0; w < width; w++) {
101 if (w == 0) {
102 for (i = 0; i < span->end; i++)
103 x[i] -= start;
104 }
105 else {
106 for (i = 0; i < span->end; i++)
107 x[i]++;
108 }
109 if (ctx->Visual.rgbMode)
110 _swrast_write_rgba_span(ctx, span);
111 else
112 _swrast_write_index_span(ctx, span);
113 }
114 }
115 }
116
117
118
119 /**********************************************************************/
120 /***** Rasterization *****/
121 /**********************************************************************/
122
123 /* Simple color index line (no stipple, width=1, no Z, no fog, no tex)*/
124 #define NAME simple_ci_line
125 #define INTERP_INDEX
126 #define RENDER_SPAN(span) _swrast_write_index_span(ctx, &span)
127 #include "s_linetemp.h"
128
129 /* Simple RGBA index line (no stipple, width=1, no Z, no fog, no tex)*/
130 #define NAME simple_rgba_line
131 #define INTERP_RGBA
132 #define RENDER_SPAN(span) _swrast_write_rgba_span(ctx, &span);
133 #include "s_linetemp.h"
134
135
136 /* Z, fog, wide, stipple color index line */
137 #define NAME general_ci_line
138 #define INTERP_INDEX
139 #define INTERP_Z
140 #define INTERP_FOG
141 #define RENDER_SPAN(span) \
142 if (ctx->Line.StippleFlag) { \
143 span.arrayMask |= SPAN_MASK; \
144 compute_stipple_mask(ctx, span.end, span.array->mask); \
145 } \
146 if (ctx->Line._Width > 1.0) { \
147 draw_wide_line(ctx, &span, (GLboolean)(dx > dy)); \
148 } \
149 else { \
150 _swrast_write_index_span(ctx, &span); \
151 }
152 #include "s_linetemp.h"
153
154
155 /* Z, fog, wide, stipple RGBA line */
156 #define NAME general_rgba_line
157 #define INTERP_RGBA
158 #define INTERP_Z
159 #define INTERP_FOG
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 /* Single-texture line, w/ fog, Z, specular, etc. */
175 #define NAME textured_line
176 #define INTERP_RGBA
177 #define INTERP_Z
178 #define INTERP_FOG
179 #define INTERP_TEX
180 #define RENDER_SPAN(span) \
181 if (ctx->Line.StippleFlag) { \
182 span.arrayMask |= SPAN_MASK; \
183 compute_stipple_mask(ctx, span.end, span.array->mask); \
184 } \
185 if (ctx->Line._Width > 1.0) { \
186 draw_wide_line(ctx, &span, (GLboolean)(dx > dy)); \
187 } \
188 else { \
189 _swrast_write_rgba_span(ctx, &span); \
190 }
191 #include "s_linetemp.h"
192
193
194 /* Multi-texture or separate specular line, w/ fog, Z, specular, etc. */
195 #define NAME multitextured_line
196 #define INTERP_RGBA
197 #define INTERP_SPEC
198 #define INTERP_Z
199 #define INTERP_FOG
200 #define INTERP_MULTITEX
201 #define INTERP_VARYING
202 #define RENDER_SPAN(span) \
203 if (ctx->Line.StippleFlag) { \
204 span.arrayMask |= SPAN_MASK; \
205 compute_stipple_mask(ctx, span.end, span.array->mask); \
206 } \
207 if (ctx->Line._Width > 1.0) { \
208 draw_wide_line(ctx, &span, (GLboolean)(dx > dy)); \
209 } \
210 else { \
211 _swrast_write_rgba_span(ctx, &span); \
212 }
213 #include "s_linetemp.h"
214
215
216
217 void
218 _swrast_add_spec_terms_line( GLcontext *ctx,
219 const SWvertex *v0,
220 const SWvertex *v1 )
221 {
222 SWvertex *ncv0 = (SWvertex *)v0;
223 SWvertex *ncv1 = (SWvertex *)v1;
224 GLchan c[2][4];
225 COPY_CHAN4( c[0], ncv0->color );
226 COPY_CHAN4( c[1], ncv1->color );
227 ACC_3V( ncv0->color, ncv0->specular );
228 ACC_3V( ncv1->color, ncv1->specular );
229 SWRAST_CONTEXT(ctx)->SpecLine( ctx, ncv0, ncv1 );
230 COPY_CHAN4( ncv0->color, c[0] );
231 COPY_CHAN4( ncv1->color, c[1] );
232 }
233
234
235 #ifdef DEBUG
236 extern void
237 _mesa_print_line_function(GLcontext *ctx); /* silence compiler warning */
238 void
239 _mesa_print_line_function(GLcontext *ctx)
240 {
241 SWcontext *swrast = SWRAST_CONTEXT(ctx);
242
243 _mesa_printf("Line Func == ");
244 if (swrast->Line == simple_ci_line)
245 _mesa_printf("simple_ci_line\n");
246 else if (swrast->Line == simple_rgba_line)
247 _mesa_printf("simple_rgba_line\n");
248 else if (swrast->Line == general_ci_line)
249 _mesa_printf("general_ci_line\n");
250 else if (swrast->Line == general_rgba_line)
251 _mesa_printf("general_rgba_line\n");
252 else if (swrast->Line == textured_line)
253 _mesa_printf("textured_line\n");
254 else if (swrast->Line == multitextured_line)
255 _mesa_printf("multitextured_line\n");
256 else
257 _mesa_printf("Driver func %p\n", (void *(*)()) swrast->Line);
258 }
259 #endif
260
261
262
263 #ifdef DEBUG
264
265 /* record the current line function name */
266 static const char *lineFuncName = NULL;
267
268 #define USE(lineFunc) \
269 do { \
270 lineFuncName = #lineFunc; \
271 /*_mesa_printf("%s\n", lineFuncName);*/ \
272 swrast->Line = lineFunc; \
273 } while (0)
274
275 #else
276
277 #define USE(lineFunc) swrast->Line = lineFunc
278
279 #endif
280
281
282
283 /*
284 * Determine which line drawing function to use given the current
285 * rendering context.
286 *
287 * Please update the summary flag _SWRAST_NEW_LINE if you add or remove
288 * tests to this code.
289 */
290 void
291 _swrast_choose_line( GLcontext *ctx )
292 {
293 SWcontext *swrast = SWRAST_CONTEXT(ctx);
294 const GLboolean rgbmode = ctx->Visual.rgbMode;
295
296 if (ctx->RenderMode == GL_RENDER) {
297 if (ctx->Line.SmoothFlag) {
298 /* antialiased lines */
299 _swrast_choose_aa_line_function(ctx);
300 ASSERT(swrast->Line);
301 }
302 else if (ctx->Texture._EnabledCoordUnits
303 || ctx->FragmentProgram._Current) {
304 /* textured lines */
305 if (ctx->Texture._EnabledCoordUnits > 0x1
306 || NEED_SECONDARY_COLOR(ctx)
307 || ctx->FragmentProgram._Current) {
308 /* multi-texture and/or separate specular color */
309 USE(multitextured_line);
310 }
311 else {
312 USE(textured_line);
313 }
314 }
315 else if (ctx->Depth.Test || swrast->_FogEnabled || ctx->Line._Width != 1.0
316 || ctx->Line.StippleFlag) {
317 /* no texture, but Z, fog, width>1, stipple, etc. */
318 if (rgbmode)
319 USE(general_rgba_line);
320 else
321 USE(general_ci_line);
322 }
323 else {
324 /* simplest lines */
325 if (rgbmode)
326 USE(simple_rgba_line);
327 else
328 USE(simple_ci_line);
329 }
330 }
331 else if (ctx->RenderMode == GL_FEEDBACK) {
332 USE(_swrast_feedback_line);
333 }
334 else {
335 ASSERT(ctx->RenderMode == GL_SELECT);
336 USE(_swrast_select_line);
337 }
338
339 /*_mesa_print_line_function(ctx);*/
340 }