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