mesa: Restore 78-column wrapping of license text in C-style comments.
[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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 #include "main/glheader.h"
28 #include "main/context.h"
29 #include "main/colormac.h"
30 #include "main/macros.h"
31 #include "s_aaline.h"
32 #include "s_context.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( struct gl_context *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( struct gl_context *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 < SWRAST_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 _swrast_write_rgba_span(ctx, span);
92 }
93 }
94 else {
95 GLint *x = span->array->x;
96 GLuint i;
97 GLint w;
98 for (w = 0; w < width; w++) {
99 if (w == 0) {
100 for (i = 0; i < span->end; i++)
101 x[i] -= start;
102 }
103 else {
104 for (i = 0; i < span->end; i++)
105 x[i]++;
106 }
107 _swrast_write_rgba_span(ctx, span);
108 }
109 }
110 }
111
112
113
114 /**********************************************************************/
115 /***** Rasterization *****/
116 /**********************************************************************/
117
118 /* Simple RGBA index line (no stipple, width=1, no Z, no fog, no tex)*/
119 #define NAME simple_no_z_rgba_line
120 #define INTERP_RGBA
121 #define RENDER_SPAN(span) _swrast_write_rgba_span(ctx, &span);
122 #include "s_linetemp.h"
123
124
125 /* Z, fog, wide, stipple RGBA line */
126 #define NAME rgba_line
127 #define INTERP_RGBA
128 #define INTERP_Z
129 #define RENDER_SPAN(span) \
130 if (ctx->Line.StippleFlag) { \
131 span.arrayMask |= SPAN_MASK; \
132 compute_stipple_mask(ctx, span.end, span.array->mask); \
133 } \
134 if (ctx->Line.Width > 1.0) { \
135 draw_wide_line(ctx, &span, (GLboolean)(dx > dy)); \
136 } \
137 else { \
138 _swrast_write_rgba_span(ctx, &span); \
139 }
140 #include "s_linetemp.h"
141
142
143 /* General-purpose line (any/all features). */
144 #define NAME general_line
145 #define INTERP_RGBA
146 #define INTERP_Z
147 #define INTERP_ATTRIBS
148 #define RENDER_SPAN(span) \
149 if (ctx->Line.StippleFlag) { \
150 span.arrayMask |= SPAN_MASK; \
151 compute_stipple_mask(ctx, span.end, span.array->mask); \
152 } \
153 if (ctx->Line.Width > 1.0) { \
154 draw_wide_line(ctx, &span, (GLboolean)(dx > dy)); \
155 } \
156 else { \
157 _swrast_write_rgba_span(ctx, &span); \
158 }
159 #include "s_linetemp.h"
160
161
162
163 void
164 _swrast_add_spec_terms_line(struct gl_context *ctx,
165 const SWvertex *v0, const SWvertex *v1)
166 {
167 SWvertex *ncv0 = (SWvertex *)v0;
168 SWvertex *ncv1 = (SWvertex *)v1;
169 GLfloat rSum, gSum, bSum;
170 GLchan cSave[2][4];
171
172 /* save original colors */
173 COPY_CHAN4(cSave[0], ncv0->color);
174 COPY_CHAN4(cSave[1], ncv1->color);
175 /* sum v0 */
176 rSum = CHAN_TO_FLOAT(ncv0->color[0]) + ncv0->attrib[VARYING_SLOT_COL1][0];
177 gSum = CHAN_TO_FLOAT(ncv0->color[1]) + ncv0->attrib[VARYING_SLOT_COL1][1];
178 bSum = CHAN_TO_FLOAT(ncv0->color[2]) + ncv0->attrib[VARYING_SLOT_COL1][2];
179 UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[0], rSum);
180 UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[1], gSum);
181 UNCLAMPED_FLOAT_TO_CHAN(ncv0->color[2], bSum);
182 /* sum v1 */
183 rSum = CHAN_TO_FLOAT(ncv1->color[0]) + ncv1->attrib[VARYING_SLOT_COL1][0];
184 gSum = CHAN_TO_FLOAT(ncv1->color[1]) + ncv1->attrib[VARYING_SLOT_COL1][1];
185 bSum = CHAN_TO_FLOAT(ncv1->color[2]) + ncv1->attrib[VARYING_SLOT_COL1][2];
186 UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[0], rSum);
187 UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[1], gSum);
188 UNCLAMPED_FLOAT_TO_CHAN(ncv1->color[2], bSum);
189 /* draw */
190 SWRAST_CONTEXT(ctx)->SpecLine( ctx, ncv0, ncv1 );
191 /* restore original colors */
192 COPY_CHAN4(ncv0->color, cSave[0]);
193 COPY_CHAN4(ncv1->color, cSave[1]);
194 }
195
196
197
198 #ifdef DEBUG
199
200 /* record the current line function name */
201 static const char *lineFuncName = NULL;
202
203 #define USE(lineFunc) \
204 do { \
205 lineFuncName = #lineFunc; \
206 /*printf("%s\n", lineFuncName);*/ \
207 swrast->Line = lineFunc; \
208 } while (0)
209
210 #else
211
212 #define USE(lineFunc) swrast->Line = lineFunc
213
214 #endif
215
216
217
218 /**
219 * Determine which line drawing function to use given the current
220 * rendering context.
221 *
222 * Please update the summary flag _SWRAST_NEW_LINE if you add or remove
223 * tests to this code.
224 */
225 void
226 _swrast_choose_line( struct gl_context *ctx )
227 {
228 SWcontext *swrast = SWRAST_CONTEXT(ctx);
229 GLboolean specular = (ctx->Fog.ColorSumEnabled ||
230 (ctx->Light.Enabled &&
231 ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR));
232
233 if (ctx->RenderMode == GL_RENDER) {
234 if (ctx->Line.SmoothFlag) {
235 /* antialiased lines */
236 _swrast_choose_aa_line_function(ctx);
237 ASSERT(swrast->Line);
238 }
239 else if (ctx->Texture._EnabledCoordUnits
240 || _swrast_use_fragment_program(ctx)
241 || swrast->_FogEnabled
242 || specular) {
243 USE(general_line);
244 }
245 else if (ctx->Depth.Test
246 || ctx->Line.Width != 1.0
247 || ctx->Line.StippleFlag) {
248 /* no texture, but Z, fog, width>1, stipple, etc. */
249 #if CHAN_BITS == 32
250 USE(general_line);
251 #else
252 USE(rgba_line);
253 #endif
254 }
255 else {
256 ASSERT(!ctx->Depth.Test);
257 ASSERT(ctx->Line.Width == 1.0);
258 /* simple lines */
259 USE(simple_no_z_rgba_line);
260 }
261 }
262 else if (ctx->RenderMode == GL_FEEDBACK) {
263 USE(_swrast_feedback_line);
264 }
265 else {
266 ASSERT(ctx->RenderMode == GL_SELECT);
267 USE(_swrast_select_line);
268 }
269 }