1 /* $Id: s_linetemp.h,v 1.18 2003/03/25 02:23:46 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Line Rasterizer Template
31 * This file is #include'd to generate custom line rasterizers.
33 * The following macros may be defined to indicate what auxillary information
34 * must be interplated along the line:
35 * INTERP_Z - if defined, interpolate Z values
36 * INTERP_FOG - if defined, interpolate FOG values
37 * INTERP_RGBA - if defined, interpolate RGBA values
38 * INTERP_SPEC - if defined, interpolate specular RGB values
39 * INTERP_INDEX - if defined, interpolate color index values
40 * INTERP_TEX - if defined, interpolate unit 0 texcoords
41 * INTERP_MULTITEX - if defined, interpolate multi-texcoords
43 * When one can directly address pixels in the color buffer the following
44 * macros can be defined and used to directly compute pixel addresses during
45 * rasterization (see pixelPtr):
46 * PIXEL_TYPE - the datatype of a pixel (GLubyte, GLushort, GLuint)
47 * BYTES_PER_ROW - number of bytes per row in the color buffer
48 * PIXEL_ADDRESS(X,Y) - returns the address of pixel at (X,Y) where
49 * Y==0 at bottom of screen and increases upward.
51 * Similarly, for direct depth buffer access, this type is used for depth
53 * DEPTH_TYPE - either GLushort or GLuint
55 * Optionally, one may provide one-time setup code
56 * SETUP_CODE - code which is to be executed once per line
58 * To actually "plot" each pixel the PLOT macro must be defined...
59 * PLOT(X,Y) - code to plot a pixel. Example:
62 * color = pack_rgb( FixedToInt(r0), FixedToInt(g0),
64 * put_pixel( X, Y, color );
67 * This code was designed for the origin to be in the lower-left corner.
73 NAME( GLcontext
*ctx
, const SWvertex
*vert0
, const SWvertex
*vert1
)
76 GLuint interpFlags
= 0;
77 GLint x0
= (GLint
) vert0
->win
[0];
78 GLint x1
= (GLint
) vert1
->win
[0];
79 GLint y0
= (GLint
) vert0
->win
[1];
80 GLint y1
= (GLint
) vert1
->win
[1];
84 #if defined(DEPTH_TYPE)
85 const GLint depthBits
= ctx
->Visual
.depthBits
;
86 const GLint fixedToDepthShift
= depthBits
<= 16 ? FIXED_SHIFT
: 0;
87 #define FixedToDepth(F) ((F) >> fixedToDepthShift)
88 GLint zPtrXstep
, zPtrYstep
;
90 #elif defined(INTERP_Z)
91 const GLint depthBits
= ctx
->Visual
.depthBits
;
95 GLint pixelXstep
, pixelYstep
;
102 /* Cull primitives with malformed coordinates.
105 GLfloat tmp
= vert0
->win
[0] + vert0
->win
[1]
106 + vert1
->win
[0] + vert1
->win
[1];
107 if (IS_INF_OR_NAN(tmp
))
112 printf("%s():\n", __FUNCTION__);
113 printf(" (%f, %f, %f) -> (%f, %f, %f)\n",
114 vert0->win[0], vert0->win[1], vert0->win[2],
115 vert1->win[0], vert1->win[1], vert1->win[2]);
116 printf(" (%d, %d, %d) -> (%d, %d, %d)\n",
117 vert0->color[0], vert0->color[1], vert0->color[2],
118 vert1->color[0], vert1->color[1], vert1->color[2]);
119 printf(" (%d, %d, %d) -> (%d, %d, %d)\n",
120 vert0->specular[0], vert0->specular[1], vert0->specular[2],
121 vert1->specular[0], vert1->specular[1], vert1->specular[2]);
125 * Despite being clipped to the view volume, the line's window coordinates
126 * may just lie outside the window bounds. That is, if the legal window
127 * coordinates are [0,W-1][0,H-1], it's possible for x==W and/or y==H.
128 * This quick and dirty code nudges the endpoints inside the window if
133 GLint w
= ctx
->DrawBuffer
->Width
;
134 GLint h
= ctx
->DrawBuffer
->Height
;
135 if ((x0
==w
) | (x1
==w
)) {
136 if ((x0
==w
) & (x1
==w
))
141 if ((y0
==h
) | (y1
==h
)) {
142 if ((y0
==h
) & (y1
==h
))
152 if (dx
== 0 && dy
== 0)
156 zPtr
= (DEPTH_TYPE
*) _swrast_zbuffer_address(ctx
, x0
, y0
);
159 pixelPtr
= (PIXEL_TYPE
*) PIXEL_ADDRESS(x0
,y0
);
163 dx
= -dx
; /* make positive */
166 zPtrXstep
= -((GLint
)sizeof(DEPTH_TYPE
));
169 pixelXstep
= -((GLint
)sizeof(PIXEL_TYPE
));
175 zPtrXstep
= ((GLint
)sizeof(DEPTH_TYPE
));
178 pixelXstep
= ((GLint
)sizeof(PIXEL_TYPE
));
183 dy
= -dy
; /* make positive */
186 zPtrYstep
= -((GLint
) (ctx
->DrawBuffer
->Width
* sizeof(DEPTH_TYPE
)));
189 pixelYstep
= BYTES_PER_ROW
;
195 zPtrYstep
= (GLint
) (ctx
->DrawBuffer
->Width
* sizeof(DEPTH_TYPE
));
198 pixelYstep
= -(BYTES_PER_ROW
);
205 numPixels
= MAX2(dx
, dy
);
208 * Span setup: compute start and step values for all interpolated values.
211 interpFlags
|= SPAN_RGBA
;
212 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
213 span
.red
= ChanToFixed(vert0
->color
[0]);
214 span
.green
= ChanToFixed(vert0
->color
[1]);
215 span
.blue
= ChanToFixed(vert0
->color
[2]);
216 span
.alpha
= ChanToFixed(vert0
->color
[3]);
217 span
.redStep
= (ChanToFixed(vert1
->color
[0]) - span
.red
) / numPixels
;
218 span
.greenStep
= (ChanToFixed(vert1
->color
[1]) - span
.green
) / numPixels
;
219 span
.blueStep
= (ChanToFixed(vert1
->color
[2]) - span
.blue
) / numPixels
;
220 span
.alphaStep
= (ChanToFixed(vert1
->color
[3]) - span
.alpha
) / numPixels
;
223 span
.red
= ChanToFixed(vert1
->color
[0]);
224 span
.green
= ChanToFixed(vert1
->color
[1]);
225 span
.blue
= ChanToFixed(vert1
->color
[2]);
226 span
.alpha
= ChanToFixed(vert1
->color
[3]);
234 interpFlags
|= SPAN_SPEC
;
235 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
236 span
.specRed
= ChanToFixed(vert0
->specular
[0]);
237 span
.specGreen
= ChanToFixed(vert0
->specular
[1]);
238 span
.specBlue
= ChanToFixed(vert0
->specular
[2]);
239 span
.specRedStep
= (ChanToFixed(vert1
->specular
[0]) - span
.specRed
) / numPixels
;
240 span
.specGreenStep
= (ChanToFixed(vert1
->specular
[1]) - span
.specBlue
) / numPixels
;
241 span
.specBlueStep
= (ChanToFixed(vert1
->specular
[2]) - span
.specGreen
) / numPixels
;
244 span
.specRed
= ChanToFixed(vert1
->specular
[0]);
245 span
.specGreen
= ChanToFixed(vert1
->specular
[1]);
246 span
.specBlue
= ChanToFixed(vert1
->specular
[2]);
247 span
.specRedStep
= 0;
248 span
.specGreenStep
= 0;
249 span
.specBlueStep
= 0;
253 interpFlags
|= SPAN_INDEX
;
254 if (ctx
->Light
.ShadeModel
== GL_SMOOTH
) {
255 span
.index
= IntToFixed(vert0
->index
);
256 span
.indexStep
= IntToFixed(vert1
->index
- vert0
->index
) / numPixels
;
259 span
.index
= IntToFixed(vert1
->index
);
263 #if defined(INTERP_Z) || defined(DEPTH_TYPE)
264 interpFlags
|= SPAN_Z
;
266 if (depthBits
<= 16) {
267 span
.z
= FloatToFixed(vert0
->win
[2]) + FIXED_HALF
;
268 span
.zStep
= FloatToFixed(vert1
->win
[2] - vert0
->win
[2]) / numPixels
;
271 /* don't use fixed point */
272 span
.z
= (GLint
) vert0
->win
[2];
273 span
.zStep
= (GLint
) ((vert1
->win
[2] - vert0
->win
[2]) / numPixels
);
278 interpFlags
|= SPAN_FOG
;
279 span
.fog
= vert0
->fog
;
280 span
.fogStep
= (vert1
->fog
- vert0
->fog
) / numPixels
;
283 interpFlags
|= SPAN_TEXTURE
;
285 const GLfloat invw0
= vert0
->win
[3];
286 const GLfloat invw1
= vert1
->win
[3];
287 const GLfloat invLen
= 1.0F
/ numPixels
;
288 GLfloat ds
, dt
, dr
, dq
;
289 span
.tex
[0][0] = invw0
* vert0
->texcoord
[0][0];
290 span
.tex
[0][1] = invw0
* vert0
->texcoord
[0][1];
291 span
.tex
[0][2] = invw0
* vert0
->texcoord
[0][2];
292 span
.tex
[0][3] = invw0
* vert0
->texcoord
[0][3];
293 ds
= (invw1
* vert1
->texcoord
[0][0]) - span
.tex
[0][0];
294 dt
= (invw1
* vert1
->texcoord
[0][1]) - span
.tex
[0][1];
295 dr
= (invw1
* vert1
->texcoord
[0][2]) - span
.tex
[0][2];
296 dq
= (invw1
* vert1
->texcoord
[0][3]) - span
.tex
[0][3];
297 span
.texStepX
[0][0] = ds
* invLen
;
298 span
.texStepX
[0][1] = dt
* invLen
;
299 span
.texStepX
[0][2] = dr
* invLen
;
300 span
.texStepX
[0][3] = dq
* invLen
;
301 span
.texStepY
[0][0] = 0.0F
;
302 span
.texStepY
[0][1] = 0.0F
;
303 span
.texStepY
[0][2] = 0.0F
;
304 span
.texStepY
[0][3] = 0.0F
;
307 #ifdef INTERP_MULTITEX
308 interpFlags
|= SPAN_TEXTURE
;
310 const GLfloat invLen
= 1.0F
/ numPixels
;
312 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
313 if (ctx
->Texture
.Unit
[u
]._ReallyEnabled
) {
314 const GLfloat invw0
= vert0
->win
[3];
315 const GLfloat invw1
= vert1
->win
[3];
316 GLfloat ds
, dt
, dr
, dq
;
317 span
.tex
[u
][0] = invw0
* vert0
->texcoord
[u
][0];
318 span
.tex
[u
][1] = invw0
* vert0
->texcoord
[u
][1];
319 span
.tex
[u
][2] = invw0
* vert0
->texcoord
[u
][2];
320 span
.tex
[u
][3] = invw0
* vert0
->texcoord
[u
][3];
321 ds
= (invw1
* vert1
->texcoord
[u
][0]) - span
.tex
[u
][0];
322 dt
= (invw1
* vert1
->texcoord
[u
][1]) - span
.tex
[u
][1];
323 dr
= (invw1
* vert1
->texcoord
[u
][2]) - span
.tex
[u
][2];
324 dq
= (invw1
* vert1
->texcoord
[u
][3]) - span
.tex
[u
][3];
325 span
.texStepX
[u
][0] = ds
* invLen
;
326 span
.texStepX
[u
][1] = dt
* invLen
;
327 span
.texStepX
[u
][2] = dr
* invLen
;
328 span
.texStepX
[u
][3] = dq
* invLen
;
329 span
.texStepY
[u
][0] = 0.0F
;
330 span
.texStepY
[u
][1] = 0.0F
;
331 span
.texStepY
[u
][2] = 0.0F
;
332 span
.texStepY
[u
][3] = 0.0F
;
338 INIT_SPAN(span
, GL_LINE
, numPixels
, interpFlags
, SPAN_XY
);
345 /*** X-major line ***/
347 GLint errorInc
= dy
+dy
;
348 GLint error
= errorInc
-dx
;
349 GLint errorDec
= error
-dx
;
351 for (i
= 0; i
< dx
; i
++) {
353 GLdepth Z
= FixedToDepth(span
.z
);
358 span
.array
->x
[i
] = x0
;
359 span
.array
->y
[i
] = y0
;
363 zPtr
= (DEPTH_TYPE
*) ((GLubyte
*) zPtr
+ zPtrXstep
);
364 span
.z
+= span
.zStep
;
367 pixelPtr
= (PIXEL_TYPE
*) ((GLubyte
*) pixelPtr
+ pixelXstep
);
376 zPtr
= (DEPTH_TYPE
*) ((GLubyte
*) zPtr
+ zPtrYstep
);
379 pixelPtr
= (PIXEL_TYPE
*) ((GLubyte
*) pixelPtr
+ pixelYstep
);
385 /*** Y-major line ***/
387 GLint errorInc
= dx
+dx
;
388 GLint error
= errorInc
-dy
;
389 GLint errorDec
= error
-dy
;
393 GLdepth Z
= FixedToDepth(span
.z
);
398 span
.array
->x
[i
] = x0
;
399 span
.array
->y
[i
] = y0
;
403 zPtr
= (DEPTH_TYPE
*) ((GLubyte
*) zPtr
+ zPtrYstep
);
404 span
.z
+= span
.zStep
;
407 pixelPtr
= (PIXEL_TYPE
*) ((GLubyte
*) pixelPtr
+ pixelYstep
);
416 zPtr
= (DEPTH_TYPE
*) ((GLubyte
*) zPtr
+ zPtrXstep
);
419 pixelPtr
= (PIXEL_TYPE
*) ((GLubyte
*) pixelPtr
+ pixelXstep
);
438 #undef INTERP_MULTITEX