s_linetemp.h revision 69a5896238c3553422fcf80feebe5fa39e636006
1/*
2 * Mesa 3-D graphics library
3 * Version:  6.3
4 *
5 * Copyright (C) 1999-2004  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_FOG      - if defined, interpolate FOG values
35 *    INTERP_RGBA     - if defined, interpolate RGBA values
36 *    INTERP_SPEC     - if defined, interpolate specular RGB values
37 *    INTERP_INDEX    - if defined, interpolate color index values
38 *    INTERP_TEX      - if defined, interpolate unit 0 texcoords
39 *    INTERP_MULTITEX - if defined, interpolate multi-texcoords
40 *
41 * When one can directly address pixels in the color buffer the following
42 * macros can be defined and used to directly compute pixel addresses during
43 * rasterization (see pixelPtr):
44 *    PIXEL_TYPE          - the datatype of a pixel (GLubyte, GLushort, GLuint)
45 *    BYTES_PER_ROW       - number of bytes per row in the color buffer
46 *    PIXEL_ADDRESS(X,Y)  - returns the address of pixel at (X,Y) where
47 *                          Y==0 at bottom of screen and increases upward.
48 *
49 * Similarly, for direct depth buffer access, this type is used for depth
50 * buffer addressing:
51 *    DEPTH_TYPE          - either GLushort or GLuint
52 *
53 * Optionally, one may provide one-time setup code
54 *    SETUP_CODE    - code which is to be executed once per line
55 *
56 * To actually "plot" each pixel the PLOT macro must be defined...
57 *    PLOT(X,Y) - code to plot a pixel.  Example:
58 *                if (Z < *zPtr) {
59 *                   *zPtr = Z;
60 *                   color = pack_rgb( FixedToInt(r0), FixedToInt(g0),
61 *                                     FixedToInt(b0) );
62 *                   put_pixel( X, Y, color );
63 *                }
64 *
65 * This code was designed for the origin to be in the lower-left corner.
66 *
67 */
68
69
70static void
71NAME( GLcontext *ctx, const SWvertex *vert0, const SWvertex *vert1 )
72{
73   struct sw_span span;
74   GLuint interpFlags = 0;
75   GLint x0 = (GLint) vert0->win[0];
76   GLint x1 = (GLint) vert1->win[0];
77   GLint y0 = (GLint) vert0->win[1];
78   GLint y1 = (GLint) vert1->win[1];
79   GLint dx, dy;
80   GLint numPixels;
81   GLint xstep, ystep;
82#if defined(DEPTH_TYPE)
83   const GLint depthBits = ctx->Visual.depthBits;
84   const GLint fixedToDepthShift = depthBits <= 16 ? FIXED_SHIFT : 0;
85#define FixedToDepth(F)  ((F) >> fixedToDepthShift)
86   GLint zPtrXstep, zPtrYstep;
87   DEPTH_TYPE *zPtr;
88#elif defined(INTERP_Z)
89   const GLint depthBits = 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   /* Cull primitives with malformed coordinates.
101    */
102   {
103      GLfloat tmp = vert0->win[0] + vert0->win[1]
104                  + vert1->win[0] + vert1->win[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->win[0], vert0->win[1], vert0->win[2],
113          vert1->win[0], vert1->win[1], vert1->win[2]);
114   printf(" (%d, %d, %d) -> (%d, %d, %d)\n",
115          vert0->color[0], vert0->color[1], vert0->color[2],
116          vert1->color[0], vert1->color[1], vert1->color[2]);
117   printf(" (%d, %d, %d) -> (%d, %d, %d)\n",
118          vert0->specular[0], vert0->specular[1], vert0->specular[2],
119          vert1->specular[0], vert1->specular[1], vert1->specular[2]);
120   */
121
122/*
123 * Despite being clipped to the view volume, the line's window coordinates
124 * may just lie outside the window bounds.  That is, if the legal window
125 * coordinates are [0,W-1][0,H-1], it's possible for x==W and/or y==H.
126 * This quick and dirty code nudges the endpoints inside the window if
127 * necessary.
128 */
129#ifdef CLIP_HACK
130   {
131      GLint w = ctx->DrawBuffer->Width;
132      GLint h = ctx->DrawBuffer->Height;
133      if ((x0==w) | (x1==w)) {
134         if ((x0==w) & (x1==w))
135           return;
136         x0 -= x0==w;
137         x1 -= x1==w;
138      }
139      if ((y0==h) | (y1==h)) {
140         if ((y0==h) & (y1==h))
141           return;
142         y0 -= y0==h;
143         y1 -= y1==h;
144      }
145   }
146#endif
147
148   dx = x1 - x0;
149   dy = y1 - y0;
150   if (dx == 0 && dy == 0)
151      return;
152
153#ifdef DEPTH_TYPE
154   zPtr = (DEPTH_TYPE *) _swrast_zbuffer_address(ctx, x0, y0);
155#endif
156#ifdef PIXEL_ADDRESS
157   pixelPtr = (PIXEL_TYPE *) PIXEL_ADDRESS(x0,y0);
158#endif
159
160   if (dx<0) {
161      dx = -dx;   /* make positive */
162      xstep = -1;
163#ifdef DEPTH_TYPE
164      zPtrXstep = -((GLint)sizeof(DEPTH_TYPE));
165#endif
166#ifdef PIXEL_ADDRESS
167      pixelXstep = -((GLint)sizeof(PIXEL_TYPE));
168#endif
169   }
170   else {
171      xstep = 1;
172#ifdef DEPTH_TYPE
173      zPtrXstep = ((GLint)sizeof(DEPTH_TYPE));
174#endif
175#ifdef PIXEL_ADDRESS
176      pixelXstep = ((GLint)sizeof(PIXEL_TYPE));
177#endif
178   }
179
180   if (dy<0) {
181      dy = -dy;   /* make positive */
182      ystep = -1;
183#ifdef DEPTH_TYPE
184      zPtrYstep = -((GLint) (ctx->DrawBuffer->Width * sizeof(DEPTH_TYPE)));
185#endif
186#ifdef PIXEL_ADDRESS
187      pixelYstep = BYTES_PER_ROW;
188#endif
189   }
190   else {
191      ystep = 1;
192#ifdef DEPTH_TYPE
193      zPtrYstep = (GLint) (ctx->DrawBuffer->Width * sizeof(DEPTH_TYPE));
194#endif
195#ifdef PIXEL_ADDRESS
196      pixelYstep = -(BYTES_PER_ROW);
197#endif
198   }
199
200   ASSERT(dx >= 0);
201   ASSERT(dy >= 0);
202
203   numPixels = MAX2(dx, dy);
204
205   /*
206    * Span setup: compute start and step values for all interpolated values.
207    */
208#ifdef INTERP_RGBA
209   interpFlags |= SPAN_RGBA;
210   if (ctx->Light.ShadeModel == GL_SMOOTH) {
211      span.red   = ChanToFixed(vert0->color[0]);
212      span.green = ChanToFixed(vert0->color[1]);
213      span.blue  = ChanToFixed(vert0->color[2]);
214      span.alpha = ChanToFixed(vert0->color[3]);
215      span.redStep   = (ChanToFixed(vert1->color[0]) - span.red  ) / numPixels;
216      span.greenStep = (ChanToFixed(vert1->color[1]) - span.green) / numPixels;
217      span.blueStep  = (ChanToFixed(vert1->color[2]) - span.blue ) / numPixels;
218      span.alphaStep = (ChanToFixed(vert1->color[3]) - span.alpha) / numPixels;
219   }
220   else {
221      span.red   = ChanToFixed(vert1->color[0]);
222      span.green = ChanToFixed(vert1->color[1]);
223      span.blue  = ChanToFixed(vert1->color[2]);
224      span.alpha = ChanToFixed(vert1->color[3]);
225      span.redStep   = 0;
226      span.greenStep = 0;
227      span.blueStep  = 0;
228      span.alphaStep = 0;
229   }
230#endif
231#ifdef INTERP_SPEC
232   interpFlags |= SPAN_SPEC;
233   if (ctx->Light.ShadeModel == GL_SMOOTH) {
234      span.specRed       = ChanToFixed(vert0->specular[0]);
235      span.specGreen     = ChanToFixed(vert0->specular[1]);
236      span.specBlue      = ChanToFixed(vert0->specular[2]);
237      span.specRedStep   = (ChanToFixed(vert1->specular[0]) - span.specRed) / numPixels;
238      span.specGreenStep = (ChanToFixed(vert1->specular[1]) - span.specBlue) / numPixels;
239      span.specBlueStep  = (ChanToFixed(vert1->specular[2]) - span.specGreen) / numPixels;
240   }
241   else {
242      span.specRed       = ChanToFixed(vert1->specular[0]);
243      span.specGreen     = ChanToFixed(vert1->specular[1]);
244      span.specBlue      = ChanToFixed(vert1->specular[2]);
245      span.specRedStep   = 0;
246      span.specGreenStep = 0;
247      span.specBlueStep  = 0;
248   }
249#endif
250#ifdef INTERP_INDEX
251   interpFlags |= SPAN_INDEX;
252   if (ctx->Light.ShadeModel == GL_SMOOTH) {
253      span.index = FloatToFixed(vert0->index);
254      span.indexStep = FloatToFixed(vert1->index - vert0->index) / numPixels;
255   }
256   else {
257      span.index = FloatToFixed(vert1->index);
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->win[2]) + FIXED_HALF;
266         span.zStep = FloatToFixed(vert1->win[2] - vert0->win[2]) / numPixels;
267      }
268      else {
269         /* don't use fixed point */
270         span.z = (GLint) vert0->win[2];
271         span.zStep = (GLint) ((vert1->win[2] - vert0->win[2]) / numPixels);
272      }
273   }
274#endif
275#ifdef INTERP_FOG
276   interpFlags |= SPAN_FOG;
277   span.fog = vert0->fog;
278   span.fogStep = (vert1->fog - vert0->fog) / numPixels;
279#endif
280#ifdef INTERP_TEX
281   interpFlags |= SPAN_TEXTURE;
282   {
283      const GLfloat invw0 = vert0->win[3];
284      const GLfloat invw1 = vert1->win[3];
285      const GLfloat invLen = 1.0F / numPixels;
286      GLfloat ds, dt, dr, dq;
287      span.tex[0][0] = invw0 * vert0->texcoord[0][0];
288      span.tex[0][1] = invw0 * vert0->texcoord[0][1];
289      span.tex[0][2] = invw0 * vert0->texcoord[0][2];
290      span.tex[0][3] = invw0 * vert0->texcoord[0][3];
291      ds = (invw1 * vert1->texcoord[0][0]) - span.tex[0][0];
292      dt = (invw1 * vert1->texcoord[0][1]) - span.tex[0][1];
293      dr = (invw1 * vert1->texcoord[0][2]) - span.tex[0][2];
294      dq = (invw1 * vert1->texcoord[0][3]) - span.tex[0][3];
295      span.texStepX[0][0] = ds * invLen;
296      span.texStepX[0][1] = dt * invLen;
297      span.texStepX[0][2] = dr * invLen;
298      span.texStepX[0][3] = dq * invLen;
299      span.texStepY[0][0] = 0.0F;
300      span.texStepY[0][1] = 0.0F;
301      span.texStepY[0][2] = 0.0F;
302      span.texStepY[0][3] = 0.0F;
303   }
304#endif
305#ifdef INTERP_MULTITEX
306   interpFlags |= SPAN_TEXTURE;
307   {
308      const GLfloat invLen = 1.0F / numPixels;
309      GLuint u;
310      for (u = 0; u < ctx->Const.MaxTextureUnits; u++) {
311         if (ctx->Texture.Unit[u]._ReallyEnabled) {
312            const GLfloat invw0 = vert0->win[3];
313            const GLfloat invw1 = vert1->win[3];
314            GLfloat ds, dt, dr, dq;
315            span.tex[u][0] = invw0 * vert0->texcoord[u][0];
316            span.tex[u][1] = invw0 * vert0->texcoord[u][1];
317            span.tex[u][2] = invw0 * vert0->texcoord[u][2];
318            span.tex[u][3] = invw0 * vert0->texcoord[u][3];
319            ds = (invw1 * vert1->texcoord[u][0]) - span.tex[u][0];
320            dt = (invw1 * vert1->texcoord[u][1]) - span.tex[u][1];
321            dr = (invw1 * vert1->texcoord[u][2]) - span.tex[u][2];
322            dq = (invw1 * vert1->texcoord[u][3]) - span.tex[u][3];
323            span.texStepX[u][0] = ds * invLen;
324            span.texStepX[u][1] = dt * invLen;
325            span.texStepX[u][2] = dr * invLen;
326            span.texStepX[u][3] = dq * invLen;
327            span.texStepY[u][0] = 0.0F;
328            span.texStepY[u][1] = 0.0F;
329            span.texStepY[u][2] = 0.0F;
330            span.texStepY[u][3] = 0.0F;
331	 }
332      }
333   }
334#endif
335
336   INIT_SPAN(span, GL_LINE, numPixels, interpFlags, SPAN_XY);
337
338   /* Need these for fragment prog texcoord interpolation */
339   span.w = 1.0F;
340   span.dwdx = 0.0F;
341   span.dwdy = 0.0F;
342
343   /*
344    * Draw
345    */
346
347   if (dx > dy) {
348      /*** X-major line ***/
349      GLint i;
350      GLint errorInc = dy+dy;
351      GLint error = errorInc-dx;
352      GLint errorDec = error-dx;
353
354      for (i = 0; i < dx; i++) {
355#ifdef DEPTH_TYPE
356         GLdepth Z = FixedToDepth(span.z);
357#endif
358#ifdef PLOT
359         PLOT( x0, y0 );
360#else
361         span.array->x[i] = x0;
362         span.array->y[i] = y0;
363#endif
364         x0 += xstep;
365#ifdef DEPTH_TYPE
366         zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrXstep);
367         span.z += span.zStep;
368#endif
369#ifdef PIXEL_ADDRESS
370         pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelXstep);
371#endif
372         if (error<0) {
373            error += errorInc;
374         }
375         else {
376            error += errorDec;
377            y0 += ystep;
378#ifdef DEPTH_TYPE
379            zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrYstep);
380#endif
381#ifdef PIXEL_ADDRESS
382            pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelYstep);
383#endif
384         }
385      }
386   }
387   else {
388      /*** Y-major line ***/
389      GLint i;
390      GLint errorInc = dx+dx;
391      GLint error = errorInc-dy;
392      GLint errorDec = error-dy;
393
394      for (i=0;i<dy;i++) {
395#ifdef DEPTH_TYPE
396         GLdepth Z = FixedToDepth(span.z);
397#endif
398#ifdef PLOT
399         PLOT( x0, y0 );
400#else
401         span.array->x[i] = x0;
402         span.array->y[i] = y0;
403#endif
404         y0 += ystep;
405#ifdef DEPTH_TYPE
406         zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrYstep);
407         span.z += span.zStep;
408#endif
409#ifdef PIXEL_ADDRESS
410         pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelYstep);
411#endif
412         if (error<0) {
413            error += errorInc;
414         }
415         else {
416            error += errorDec;
417            x0 += xstep;
418#ifdef DEPTH_TYPE
419            zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrXstep);
420#endif
421#ifdef PIXEL_ADDRESS
422            pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelXstep);
423#endif
424         }
425      }
426   }
427
428#ifdef RENDER_SPAN
429   RENDER_SPAN( span );
430#endif
431
432   (void)span;
433
434}
435
436
437#undef NAME
438#undef INTERP_Z
439#undef INTERP_FOG
440#undef INTERP_RGBA
441#undef INTERP_SPEC
442#undef INTERP_TEX
443#undef INTERP_MULTITEX
444#undef INTERP_INDEX
445#undef PIXEL_ADDRESS
446#undef PIXEL_TYPE
447#undef DEPTH_TYPE
448#undef BYTES_PER_ROW
449#undef SETUP_CODE
450#undef PLOT
451#undef CLIP_HACK
452#undef FixedToDepth
453#undef RENDER_SPAN
454