2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 * Line Rasterizer Template
29 * This file is #include'd to generate custom line rasterizers.
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)
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.
46 * Similarly, for direct depth buffer access, this type is used for depth
48 * DEPTH_TYPE - either GLushort or GLuint
50 * Optionally, one may provide one-time setup code
51 * SETUP_CODE - code which is to be executed once per line
53 * To actually "plot" each pixel the PLOT macro must be defined...
54 * PLOT(X,Y) - code to plot a pixel. Example:
57 * color = pack_rgb( FixedToInt(r0), FixedToInt(g0),
59 * put_pixel( X, Y, color );
62 * This code was designed for the origin to be in the lower-left corner.
68 NAME( GLcontext
*ctx
, const SWvertex
*vert0
, const SWvertex
*vert1
)
70 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
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];
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
;
87 #elif defined(INTERP_Z)
88 const GLint depthBits
= ctx
->DrawBuffer
->Visual
.depthBits
;
89 /*ctx->Visual.depthBits;*/
93 GLint pixelXstep
, pixelYstep
;
102 /* Cull primitives with malformed coordinates.
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
))
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]);
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
137 GLint w
= ctx
->DrawBuffer
->Width
;
138 GLint h
= ctx
->DrawBuffer
->Height
;
139 if ((x0
==w
) | (x1
==w
)) {
140 if ((x0
==w
) & (x1
==w
))
145 if ((y0
==h
) | (y1
==h
)) {
146 if ((y0
==h
) & (y1
==h
))
156 if (dx
== 0 && dy
== 0)
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]);
172 zPtr
= (DEPTH_TYPE
*) zrb
->GetPointer(ctx
, zrb
, x0
, y0
);
175 pixelPtr
= (PIXEL_TYPE
*) PIXEL_ADDRESS(x0
,y0
);
179 dx
= -dx
; /* make positive */
182 zPtrXstep
= -((GLint
)sizeof(DEPTH_TYPE
));
185 pixelXstep
= -((GLint
)sizeof(PIXEL_TYPE
));
191 zPtrXstep
= ((GLint
)sizeof(DEPTH_TYPE
));
194 pixelXstep
= ((GLint
)sizeof(PIXEL_TYPE
));
199 dy
= -dy
; /* make positive */
202 zPtrYstep
= -((GLint
) (ctx
->DrawBuffer
->Width
* sizeof(DEPTH_TYPE
)));
205 pixelYstep
= BYTES_PER_ROW
;
211 zPtrYstep
= (GLint
) (ctx
->DrawBuffer
->Width
* sizeof(DEPTH_TYPE
));
214 pixelYstep
= -(BYTES_PER_ROW
);
221 numPixels
= MAX2(dx
, dy
);
224 * Span setup: compute start and step values for all interpolated values.
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
;
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]);
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
;
257 span
.index
= FloatToFixed(vert1
->attrib
[FRAG_ATTRIB_CI
][0]);
261 #if defined(INTERP_Z) || defined(DEPTH_TYPE)
262 interpFlags
|= SPAN_Z
;
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
;
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
);
277 #if defined(INTERP_ATTRIBS)
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];
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;
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);
294 for (c
= 0; c
< 4; c
++) {
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
;
301 ASSIGN_4V(span
.attrStepY
[attr
], 0.0, 0.0, 0.0, 0.0);
306 INIT_SPAN(span
, GL_LINE
);
307 span
.end
= numPixels
;
308 span
.interpMask
= interpFlags
;
309 span
.arrayMask
= SPAN_XY
;
311 span
.facing
= swrast
->PointLineFacing
;
319 /*** X-major line ***/
321 GLint errorInc
= dy
+dy
;
322 GLint error
= errorInc
-dx
;
323 GLint errorDec
= error
-dx
;
325 for (i
= 0; i
< dx
; i
++) {
327 GLuint Z
= FixedToDepth(span
.z
);
332 span
.array
->x
[i
] = x0
;
333 span
.array
->y
[i
] = y0
;
337 zPtr
= (DEPTH_TYPE
*) ((GLubyte
*) zPtr
+ zPtrXstep
);
338 span
.z
+= span
.zStep
;
341 pixelPtr
= (PIXEL_TYPE
*) ((GLubyte
*) pixelPtr
+ pixelXstep
);
350 zPtr
= (DEPTH_TYPE
*) ((GLubyte
*) zPtr
+ zPtrYstep
);
353 pixelPtr
= (PIXEL_TYPE
*) ((GLubyte
*) pixelPtr
+ pixelYstep
);
359 /*** Y-major line ***/
361 GLint errorInc
= dx
+dx
;
362 GLint error
= errorInc
-dy
;
363 GLint errorDec
= error
-dy
;
367 GLuint Z
= FixedToDepth(span
.z
);
372 span
.array
->x
[i
] = x0
;
373 span
.array
->y
[i
] = y0
;
377 zPtr
= (DEPTH_TYPE
*) ((GLubyte
*) zPtr
+ zPtrYstep
);
378 span
.z
+= span
.zStep
;
381 pixelPtr
= (PIXEL_TYPE
*) ((GLubyte
*) pixelPtr
+ pixelYstep
);
390 zPtr
= (DEPTH_TYPE
*) ((GLubyte
*) zPtr
+ zPtrXstep
);
393 pixelPtr
= (PIXEL_TYPE
*) ((GLubyte
*) pixelPtr
+ pixelXstep
);
411 #undef INTERP_ATTRIBS