swrast: Remove support for rendering lines into a color-index buffer
[mesa.git] / src / mesa / swrast / s_linetemp.h
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.0
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 /*
27 * Line Rasterizer Template
28 *
29 * This file is #include'd to generate custom line rasterizers.
30 *
31 * The following macros may be defined to indicate what auxillary information
32 * must be interplated along the line:
33 * INTERP_Z - if defined, interpolate Z values
34 * INTERP_ATTRIBS - if defined, interpolate attribs (texcoords, varying, etc)
35 *
36 * When one can directly address pixels in the color buffer the following
37 * macros can be defined and used to directly compute pixel addresses during
38 * rasterization (see pixelPtr):
39 * PIXEL_TYPE - the datatype of a pixel (GLubyte, GLushort, GLuint)
40 * BYTES_PER_ROW - number of bytes per row in the color buffer
41 * PIXEL_ADDRESS(X,Y) - returns the address of pixel at (X,Y) where
42 * Y==0 at bottom of screen and increases upward.
43 *
44 * Similarly, for direct depth buffer access, this type is used for depth
45 * buffer addressing:
46 * DEPTH_TYPE - either GLushort or GLuint
47 *
48 * Optionally, one may provide one-time setup code
49 * SETUP_CODE - code which is to be executed once per line
50 *
51 * To actually "plot" each pixel the PLOT macro must be defined...
52 * PLOT(X,Y) - code to plot a pixel. Example:
53 * if (Z < *zPtr) {
54 * *zPtr = Z;
55 * color = pack_rgb( FixedToInt(r0), FixedToInt(g0),
56 * FixedToInt(b0) );
57 * put_pixel( X, Y, color );
58 * }
59 *
60 * This code was designed for the origin to be in the lower-left corner.
61 *
62 */
63
64
65 static void
66 NAME( GLcontext *ctx, const SWvertex *vert0, const SWvertex *vert1 )
67 {
68 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
69 SWspan span;
70 GLuint interpFlags = 0;
71 GLint x0 = (GLint) vert0->attrib[FRAG_ATTRIB_WPOS][0];
72 GLint x1 = (GLint) vert1->attrib[FRAG_ATTRIB_WPOS][0];
73 GLint y0 = (GLint) vert0->attrib[FRAG_ATTRIB_WPOS][1];
74 GLint y1 = (GLint) vert1->attrib[FRAG_ATTRIB_WPOS][1];
75 GLint dx, dy;
76 GLint numPixels;
77 GLint xstep, ystep;
78 #if defined(DEPTH_TYPE)
79 const GLint depthBits = ctx->DrawBuffer->Visual.depthBits;
80 const GLint fixedToDepthShift = depthBits <= 16 ? FIXED_SHIFT : 0;
81 struct gl_renderbuffer *zrb = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
82 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
83 GLint zPtrXstep, zPtrYstep;
84 DEPTH_TYPE *zPtr;
85 #elif defined(INTERP_Z)
86 const GLint depthBits = ctx->DrawBuffer->Visual.depthBits;
87 /*ctx->Visual.depthBits;*/
88 #endif
89 #ifdef PIXEL_ADDRESS
90 PIXEL_TYPE *pixelPtr;
91 GLint pixelXstep, pixelYstep;
92 #endif
93
94 #ifdef SETUP_CODE
95 SETUP_CODE
96 #endif
97
98 (void) swrast;
99
100 /* Cull primitives with malformed coordinates.
101 */
102 {
103 GLfloat tmp = vert0->attrib[FRAG_ATTRIB_WPOS][0] + vert0->attrib[FRAG_ATTRIB_WPOS][1]
104 + vert1->attrib[FRAG_ATTRIB_WPOS][0] + vert1->attrib[FRAG_ATTRIB_WPOS][1];
105 if (IS_INF_OR_NAN(tmp))
106 return;
107 }
108
109 /*
110 printf("%s():\n", __FUNCTION__);
111 printf(" (%f, %f, %f) -> (%f, %f, %f)\n",
112 vert0->attrib[FRAG_ATTRIB_WPOS][0],
113 vert0->attrib[FRAG_ATTRIB_WPOS][1],
114 vert0->attrib[FRAG_ATTRIB_WPOS][2],
115 vert1->attrib[FRAG_ATTRIB_WPOS][0],
116 vert1->attrib[FRAG_ATTRIB_WPOS][1],
117 vert1->attrib[FRAG_ATTRIB_WPOS][2]);
118 printf(" (%d, %d, %d) -> (%d, %d, %d)\n",
119 vert0->color[0], vert0->color[1], vert0->color[2],
120 vert1->color[0], vert1->color[1], vert1->color[2]);
121 printf(" (%d, %d, %d) -> (%d, %d, %d)\n",
122 vert0->specular[0], vert0->specular[1], vert0->specular[2],
123 vert1->specular[0], vert1->specular[1], vert1->specular[2]);
124 */
125
126 /*
127 * Despite being clipped to the view volume, the line's window coordinates
128 * may just lie outside the window bounds. That is, if the legal window
129 * coordinates are [0,W-1][0,H-1], it's possible for x==W and/or y==H.
130 * This quick and dirty code nudges the endpoints inside the window if
131 * necessary.
132 */
133 #ifdef CLIP_HACK
134 {
135 GLint w = ctx->DrawBuffer->Width;
136 GLint h = ctx->DrawBuffer->Height;
137 if ((x0==w) | (x1==w)) {
138 if ((x0==w) & (x1==w))
139 return;
140 x0 -= x0==w;
141 x1 -= x1==w;
142 }
143 if ((y0==h) | (y1==h)) {
144 if ((y0==h) & (y1==h))
145 return;
146 y0 -= y0==h;
147 y1 -= y1==h;
148 }
149 }
150 #endif
151
152 dx = x1 - x0;
153 dy = y1 - y0;
154 if (dx == 0 && dy == 0)
155 return;
156
157 /*
158 printf("%s %d,%d %g %g %g %g %g %g %g %g\n", __FUNCTION__, dx, dy,
159 vert0->attrib[FRAG_ATTRIB_COL1][0],
160 vert0->attrib[FRAG_ATTRIB_COL1][1],
161 vert0->attrib[FRAG_ATTRIB_COL1][2],
162 vert0->attrib[FRAG_ATTRIB_COL1][3],
163 vert1->attrib[FRAG_ATTRIB_COL1][0],
164 vert1->attrib[FRAG_ATTRIB_COL1][1],
165 vert1->attrib[FRAG_ATTRIB_COL1][2],
166 vert1->attrib[FRAG_ATTRIB_COL1][3]);
167 */
168
169 #ifdef DEPTH_TYPE
170 zPtr = (DEPTH_TYPE *) zrb->GetPointer(ctx, zrb, x0, y0);
171 #endif
172 #ifdef PIXEL_ADDRESS
173 pixelPtr = (PIXEL_TYPE *) PIXEL_ADDRESS(x0,y0);
174 #endif
175
176 if (dx<0) {
177 dx = -dx; /* make positive */
178 xstep = -1;
179 #ifdef DEPTH_TYPE
180 zPtrXstep = -((GLint)sizeof(DEPTH_TYPE));
181 #endif
182 #ifdef PIXEL_ADDRESS
183 pixelXstep = -((GLint)sizeof(PIXEL_TYPE));
184 #endif
185 }
186 else {
187 xstep = 1;
188 #ifdef DEPTH_TYPE
189 zPtrXstep = ((GLint)sizeof(DEPTH_TYPE));
190 #endif
191 #ifdef PIXEL_ADDRESS
192 pixelXstep = ((GLint)sizeof(PIXEL_TYPE));
193 #endif
194 }
195
196 if (dy<0) {
197 dy = -dy; /* make positive */
198 ystep = -1;
199 #ifdef DEPTH_TYPE
200 zPtrYstep = -((GLint) (ctx->DrawBuffer->Width * sizeof(DEPTH_TYPE)));
201 #endif
202 #ifdef PIXEL_ADDRESS
203 pixelYstep = BYTES_PER_ROW;
204 #endif
205 }
206 else {
207 ystep = 1;
208 #ifdef DEPTH_TYPE
209 zPtrYstep = (GLint) (ctx->DrawBuffer->Width * sizeof(DEPTH_TYPE));
210 #endif
211 #ifdef PIXEL_ADDRESS
212 pixelYstep = -(BYTES_PER_ROW);
213 #endif
214 }
215
216 ASSERT(dx >= 0);
217 ASSERT(dy >= 0);
218
219 numPixels = MAX2(dx, dy);
220
221 /*
222 * Span setup: compute start and step values for all interpolated values.
223 */
224 interpFlags |= SPAN_RGBA;
225 if (ctx->Light.ShadeModel == GL_SMOOTH) {
226 span.red = ChanToFixed(vert0->color[0]);
227 span.green = ChanToFixed(vert0->color[1]);
228 span.blue = ChanToFixed(vert0->color[2]);
229 span.alpha = ChanToFixed(vert0->color[3]);
230 span.redStep = (ChanToFixed(vert1->color[0]) - span.red ) / numPixels;
231 span.greenStep = (ChanToFixed(vert1->color[1]) - span.green) / numPixels;
232 span.blueStep = (ChanToFixed(vert1->color[2]) - span.blue ) / numPixels;
233 span.alphaStep = (ChanToFixed(vert1->color[3]) - span.alpha) / numPixels;
234 }
235 else {
236 span.red = ChanToFixed(vert1->color[0]);
237 span.green = ChanToFixed(vert1->color[1]);
238 span.blue = ChanToFixed(vert1->color[2]);
239 span.alpha = ChanToFixed(vert1->color[3]);
240 span.redStep = 0;
241 span.greenStep = 0;
242 span.blueStep = 0;
243 span.alphaStep = 0;
244 }
245 #if defined(INTERP_Z) || defined(DEPTH_TYPE)
246 interpFlags |= SPAN_Z;
247 {
248 if (depthBits <= 16) {
249 span.z = FloatToFixed(vert0->attrib[FRAG_ATTRIB_WPOS][2]) + FIXED_HALF;
250 span.zStep = FloatToFixed( vert1->attrib[FRAG_ATTRIB_WPOS][2]
251 - vert0->attrib[FRAG_ATTRIB_WPOS][2]) / numPixels;
252 }
253 else {
254 /* don't use fixed point */
255 span.z = (GLuint) vert0->attrib[FRAG_ATTRIB_WPOS][2];
256 span.zStep = (GLint) (( vert1->attrib[FRAG_ATTRIB_WPOS][2]
257 - vert0->attrib[FRAG_ATTRIB_WPOS][2]) / numPixels);
258 }
259 }
260 #endif
261 #if defined(INTERP_ATTRIBS)
262 {
263 const GLfloat invLen = 1.0F / numPixels;
264 const GLfloat invw0 = vert0->attrib[FRAG_ATTRIB_WPOS][3];
265 const GLfloat invw1 = vert1->attrib[FRAG_ATTRIB_WPOS][3];
266
267 span.attrStart[FRAG_ATTRIB_WPOS][3] = invw0;
268 span.attrStepX[FRAG_ATTRIB_WPOS][3] = (invw1 - invw0) * invLen;
269 span.attrStepY[FRAG_ATTRIB_WPOS][3] = 0.0;
270
271 ATTRIB_LOOP_BEGIN
272 if (swrast->_InterpMode[attr] == GL_FLAT) {
273 COPY_4V(span.attrStart[attr], vert1->attrib[attr]);
274 ASSIGN_4V(span.attrStepX[attr], 0.0, 0.0, 0.0, 0.0);
275 }
276 else {
277 GLuint c;
278 for (c = 0; c < 4; c++) {
279 float da;
280 span.attrStart[attr][c] = invw0 * vert0->attrib[attr][c];
281 da = (invw1 * vert1->attrib[attr][c]) - span.attrStart[attr][c];
282 span.attrStepX[attr][c] = da * invLen;
283 }
284 }
285 ASSIGN_4V(span.attrStepY[attr], 0.0, 0.0, 0.0, 0.0);
286 ATTRIB_LOOP_END
287 }
288 #endif
289
290 INIT_SPAN(span, GL_LINE);
291 span.end = numPixels;
292 span.interpMask = interpFlags;
293 span.arrayMask = SPAN_XY;
294
295 span.facing = swrast->PointLineFacing;
296
297
298 /*
299 * Draw
300 */
301
302 if (dx > dy) {
303 /*** X-major line ***/
304 GLint i;
305 GLint errorInc = dy+dy;
306 GLint error = errorInc-dx;
307 GLint errorDec = error-dx;
308
309 for (i = 0; i < dx; i++) {
310 #ifdef DEPTH_TYPE
311 GLuint Z = FixedToDepth(span.z);
312 #endif
313 #ifdef PLOT
314 PLOT( x0, y0 );
315 #else
316 span.array->x[i] = x0;
317 span.array->y[i] = y0;
318 #endif
319 x0 += xstep;
320 #ifdef DEPTH_TYPE
321 zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrXstep);
322 span.z += span.zStep;
323 #endif
324 #ifdef PIXEL_ADDRESS
325 pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelXstep);
326 #endif
327 if (error < 0) {
328 error += errorInc;
329 }
330 else {
331 error += errorDec;
332 y0 += ystep;
333 #ifdef DEPTH_TYPE
334 zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrYstep);
335 #endif
336 #ifdef PIXEL_ADDRESS
337 pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelYstep);
338 #endif
339 }
340 }
341 }
342 else {
343 /*** Y-major line ***/
344 GLint i;
345 GLint errorInc = dx+dx;
346 GLint error = errorInc-dy;
347 GLint errorDec = error-dy;
348
349 for (i=0;i<dy;i++) {
350 #ifdef DEPTH_TYPE
351 GLuint Z = FixedToDepth(span.z);
352 #endif
353 #ifdef PLOT
354 PLOT( x0, y0 );
355 #else
356 span.array->x[i] = x0;
357 span.array->y[i] = y0;
358 #endif
359 y0 += ystep;
360 #ifdef DEPTH_TYPE
361 zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrYstep);
362 span.z += span.zStep;
363 #endif
364 #ifdef PIXEL_ADDRESS
365 pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelYstep);
366 #endif
367 if (error<0) {
368 error += errorInc;
369 }
370 else {
371 error += errorDec;
372 x0 += xstep;
373 #ifdef DEPTH_TYPE
374 zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrXstep);
375 #endif
376 #ifdef PIXEL_ADDRESS
377 pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelXstep);
378 #endif
379 }
380 }
381 }
382
383 #ifdef RENDER_SPAN
384 RENDER_SPAN( span );
385 #endif
386
387 (void)span;
388
389 }
390
391
392 #undef NAME
393 #undef INTERP_Z
394 #undef INTERP_ATTRIBS
395 #undef PIXEL_ADDRESS
396 #undef PIXEL_TYPE
397 #undef DEPTH_TYPE
398 #undef BYTES_PER_ROW
399 #undef SETUP_CODE
400 #undef PLOT
401 #undef CLIP_HACK
402 #undef FixedToDepth
403 #undef RENDER_SPAN