| 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 |
|
| 70 |
static void |
| 71 |
NAME( 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 |
struct gl_renderbuffer *zrb = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer; |
| 86 |
#define FixedToDepth(F) ((F) >> fixedToDepthShift) |
| 87 |
GLint zPtrXstep, zPtrYstep; |
| 88 |
DEPTH_TYPE *zPtr; |
| 89 |
#elif defined(INTERP_Z) |
| 90 |
const GLint depthBits = ctx->Visual.depthBits; |
| 91 |
#endif |
| 92 |
#ifdef PIXEL_ADDRESS |
| 93 |
PIXEL_TYPE *pixelPtr; |
| 94 |
GLint pixelXstep, pixelYstep; |
| 95 |
#endif |
| 96 |
|
| 97 |
#ifdef SETUP_CODE |
| 98 |
SETUP_CODE |
| 99 |
#endif |
| 100 |
|
| 101 |
/* Cull primitives with malformed coordinates. |
| 102 |
*/ |
| 103 |
{ |
| 104 |
GLfloat tmp = vert0->win[0] + vert0->win[1] |
| 105 |
+ vert1->win[0] + vert1->win[1]; |
| 106 |
if (IS_INF_OR_NAN(tmp)) |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
/* |
| 111 |
printf("%s():\n", __FUNCTION__); |
| 112 |
printf(" (%f, %f, %f) -> (%f, %f, %f)\n", |
| 113 |
vert0->win[0], vert0->win[1], vert0->win[2], |
| 114 |
vert1->win[0], vert1->win[1], vert1->win[2]); |
| 115 |
printf(" (%d, %d, %d) -> (%d, %d, %d)\n", |
| 116 |
vert0->color[0], vert0->color[1], vert0->color[2], |
| 117 |
vert1->color[0], vert1->color[1], vert1->color[2]); |
| 118 |
printf(" (%d, %d, %d) -> (%d, %d, %d)\n", |
| 119 |
vert0->specular[0], vert0->specular[1], vert0->specular[2], |
| 120 |
vert1->specular[0], vert1->specular[1], vert1->specular[2]); |
| 121 |
*/ |
| 122 |
|
| 123 |
/* |
| 124 |
* Despite being clipped to the view volume, the line's window coordinates |
| 125 |
* may just lie outside the window bounds. That is, if the legal window |
| 126 |
* coordinates are [0,W-1][0,H-1], it's possible for x==W and/or y==H. |
| 127 |
* This quick and dirty code nudges the endpoints inside the window if |
| 128 |
* necessary. |
| 129 |
*/ |
| 130 |
#ifdef CLIP_HACK |
| 131 |
{ |
| 132 |
GLint w = ctx->DrawBuffer->Width; |
| 133 |
GLint h = ctx->DrawBuffer->Height; |
| 134 |
if ((x0==w) | (x1==w)) { |
| 135 |
if ((x0==w) & (x1==w)) |
| 136 |
return; |
| 137 |
x0 -= x0==w; |
| 138 |
x1 -= x1==w; |
| 139 |
} |
| 140 |
if ((y0==h) | (y1==h)) { |
| 141 |
if ((y0==h) & (y1==h)) |
| 142 |
return; |
| 143 |
y0 -= y0==h; |
| 144 |
y1 -= y1==h; |
| 145 |
} |
| 146 |
} |
| 147 |
#endif |
| 148 |
|
| 149 |
dx = x1 - x0; |
| 150 |
dy = y1 - y0; |
| 151 |
if (dx == 0 && dy == 0) |
| 152 |
return; |
| 153 |
|
| 154 |
#ifdef DEPTH_TYPE |
| 155 |
zPtr = (DEPTH_TYPE *) zrb->GetPointer(ctx, zrb, x0, y0); |
| 156 |
#endif |
| 157 |
#ifdef PIXEL_ADDRESS |
| 158 |
pixelPtr = (PIXEL_TYPE *) PIXEL_ADDRESS(x0,y0); |
| 159 |
#endif |
| 160 |
|
| 161 |
if (dx<0) { |
| 162 |
dx = -dx; /* make positive */ |
| 163 |
xstep = -1; |
| 164 |
#ifdef DEPTH_TYPE |
| 165 |
zPtrXstep = -((GLint)sizeof(DEPTH_TYPE)); |
| 166 |
#endif |
| 167 |
#ifdef PIXEL_ADDRESS |
| 168 |
pixelXstep = -((GLint)sizeof(PIXEL_TYPE)); |
| 169 |
#endif |
| 170 |
} |
| 171 |
else { |
| 172 |
xstep = 1; |
| 173 |
#ifdef DEPTH_TYPE |
| 174 |
zPtrXstep = ((GLint)sizeof(DEPTH_TYPE)); |
| 175 |
#endif |
| 176 |
#ifdef PIXEL_ADDRESS |
| 177 |
pixelXstep = ((GLint)sizeof(PIXEL_TYPE)); |
| 178 |
#endif |
| 179 |
} |
| 180 |
|
| 181 |
if (dy<0) { |
| 182 |
dy = -dy; /* make positive */ |
| 183 |
ystep = -1; |
| 184 |
#ifdef DEPTH_TYPE |
| 185 |
zPtrYstep = -((GLint) (ctx->DrawBuffer->Width * sizeof(DEPTH_TYPE))); |
| 186 |
#endif |
| 187 |
#ifdef PIXEL_ADDRESS |
| 188 |
pixelYstep = BYTES_PER_ROW; |
| 189 |
#endif |
| 190 |
} |
| 191 |
else { |
| 192 |
ystep = 1; |
| 193 |
#ifdef DEPTH_TYPE |
| 194 |
zPtrYstep = (GLint) (ctx->DrawBuffer->Width * sizeof(DEPTH_TYPE)); |
| 195 |
#endif |
| 196 |
#ifdef PIXEL_ADDRESS |
| 197 |
pixelYstep = -(BYTES_PER_ROW); |
| 198 |
#endif |
| 199 |
} |
| 200 |
|
| 201 |
ASSERT(dx >= 0); |
| 202 |
ASSERT(dy >= 0); |
| 203 |
|
| 204 |
numPixels = MAX2(dx, dy); |
| 205 |
|
| 206 |
/* |
| 207 |
* Span setup: compute start and step values for all interpolated values. |
| 208 |
*/ |
| 209 |
#ifdef INTERP_RGBA |
| 210 |
interpFlags |= SPAN_RGBA; |
| 211 |
if (ctx->Light.ShadeModel == GL_SMOOTH) { |
| 212 |
span.red = ChanToFixed(vert0->color[0]); |
| 213 |
span.green = ChanToFixed(vert0->color[1]); |
| 214 |
span.blue = ChanToFixed(vert0->color[2]); |
| 215 |
span.alpha = ChanToFixed(vert0->color[3]); |
| 216 |
span.redStep = (ChanToFixed(vert1->color[0]) - span.red ) / numPixels; |
| 217 |
span.greenStep = (ChanToFixed(vert1->color[1]) - span.green) / numPixels; |
| 218 |
span.blueStep = (ChanToFixed(vert1->color[2]) - span.blue ) / numPixels; |
| 219 |
span.alphaStep = (ChanToFixed(vert1->color[3]) - span.alpha) / numPixels; |
| 220 |
} |
| 221 |
else { |
| 222 |
span.red = ChanToFixed(vert1->color[0]); |
| 223 |
span.green = ChanToFixed(vert1->color[1]); |
| 224 |
span.blue = ChanToFixed(vert1->color[2]); |
| 225 |
span.alpha = ChanToFixed(vert1->color[3]); |
| 226 |
span.redStep = 0; |
| 227 |
span.greenStep = 0; |
| 228 |
span.blueStep = 0; |
| 229 |
span.alphaStep = 0; |
| 230 |
} |
| 231 |
#endif |
| 232 |
#ifdef INTERP_SPEC |
| 233 |
interpFlags |= SPAN_SPEC; |
| 234 |
if (ctx->Light.ShadeModel == GL_SMOOTH) { |
| 235 |
span.specRed = ChanToFixed(vert0->specular[0]); |
| 236 |
span.specGreen = ChanToFixed(vert0->specular[1]); |
| 237 |
span.specBlue = ChanToFixed(vert0->specular[2]); |
| 238 |
span.specRedStep = (ChanToFixed(vert1->specular[0]) - span.specRed) / numPixels; |
| 239 |
span.specGreenStep = (ChanToFixed(vert1->specular[1]) - span.specBlue) / numPixels; |
| 240 |
span.specBlueStep = (ChanToFixed(vert1->specular[2]) - span.specGreen) / numPixels; |
| 241 |
} |
| 242 |
else { |
| 243 |
span.specRed = ChanToFixed(vert1->specular[0]); |
| 244 |
span.specGreen = ChanToFixed(vert1->specular[1]); |
| 245 |
span.specBlue = ChanToFixed(vert1->specular[2]); |
| 246 |
span.specRedStep = 0; |
| 247 |
span.specGreenStep = 0; |
| 248 |
span.specBlueStep = 0; |
| 249 |
} |
| 250 |
#endif |
| 251 |
#ifdef INTERP_INDEX |
| 252 |
interpFlags |= SPAN_INDEX; |
| 253 |
if (ctx->Light.ShadeModel == GL_SMOOTH) { |
| 254 |
span.index = FloatToFixed(vert0->index); |
| 255 |
span.indexStep = FloatToFixed(vert1->index - vert0->index) / numPixels; |
| 256 |
} |
| 257 |
else { |
| 258 |
span.index = FloatToFixed(vert1->index); |
| 259 |
span.indexStep = 0; |
| 260 |
} |
| 261 |
#endif |
| 262 |
#if defined(INTERP_Z) || defined(DEPTH_TYPE) |
| 263 |
interpFlags |= SPAN_Z; |
| 264 |
{ |
| 265 |
if (depthBits <= 16) { |
| 266 |
span.z = FloatToFixed(vert0->win[2]) + FIXED_HALF; |
| 267 |
span.zStep = FloatToFixed(vert1->win[2] - vert0->win[2]) / numPixels; |
| 268 |
} |
| 269 |
else { |
| 270 |
/* don't use fixed point */ |
| 271 |
span.z = (GLint) vert0->win[2]; |
| 272 |
span.zStep = (GLint) ((vert1->win[2] - vert0->win[2]) / numPixels); |
| 273 |
} |
| 274 |
} |
| 275 |
#endif |
| 276 |
#ifdef INTERP_FOG |
| 277 |
interpFlags |= SPAN_FOG; |
| 278 |
span.fog = vert0->fog; |
| 279 |
span.fogStep = (vert1->fog - vert0->fog) / numPixels; |
| 280 |
#endif |
| 281 |
#ifdef INTERP_TEX |
| 282 |
interpFlags |= SPAN_TEXTURE; |
| 283 |
{ |
| 284 |
const GLfloat invw0 = vert0->win[3]; |
| 285 |
const GLfloat invw1 = vert1->win[3]; |
| 286 |
const GLfloat invLen = 1.0F / numPixels; |
| 287 |
GLfloat ds, dt, dr, dq; |
| 288 |
span.tex[0][0] = invw0 * vert0->texcoord[0][0]; |
| 289 |
span.tex[0][1] = invw0 * vert0->texcoord[0][1]; |
| 290 |
span.tex[0][2] = invw0 * vert0->texcoord[0][2]; |
| 291 |
span.tex[0][3] = invw0 * vert0->texcoord[0][3]; |
| 292 |
ds = (invw1 * vert1->texcoord[0][0]) - span.tex[0][0]; |
| 293 |
dt = (invw1 * vert1->texcoord[0][1]) - span.tex[0][1]; |
| 294 |
dr = (invw1 * vert1->texcoord[0][2]) - span.tex[0][2]; |
| 295 |
dq = (invw1 * vert1->texcoord[0][3]) - span.tex[0][3]; |
| 296 |
span.texStepX[0][0] = ds * invLen; |
| 297 |
span.texStepX[0][1] = dt * invLen; |
| 298 |
span.texStepX[0][2] = dr * invLen; |
| 299 |
span.texStepX[0][3] = dq * invLen; |
| 300 |
span.texStepY[0][0] = 0.0F; |
| 301 |
span.texStepY[0][1] = 0.0F; |
| 302 |
span.texStepY[0][2] = 0.0F; |
| 303 |
span.texStepY[0][3] = 0.0F; |
| 304 |
} |
| 305 |
#endif |
| 306 |
#ifdef INTERP_MULTITEX |
| 307 |
interpFlags |= SPAN_TEXTURE; |
| 308 |
{ |
| 309 |
const GLfloat invLen = 1.0F / numPixels; |
| 310 |
GLuint u; |
| 311 |
for (u = 0; u < ctx->Const.MaxTextureUnits; u++) { |
| 312 |
if (ctx->Texture.Unit[u]._ReallyEnabled) { |
| 313 |
const GLfloat invw0 = vert0->win[3]; |
| 314 |
const GLfloat invw1 = vert1->win[3]; |
| 315 |
GLfloat ds, dt, dr, dq; |
| 316 |
span.tex[u][0] = invw0 * vert0->texcoord[u][0]; |
| 317 |
span.tex[u][1] = invw0 * vert0->texcoord[u][1]; |
| 318 |
span.tex[u][2] = invw0 * vert0->texcoord[u][2]; |
| 319 |
span.tex[u][3] = invw0 * vert0->texcoord[u][3]; |
| 320 |
ds = (invw1 * vert1->texcoord[u][0]) - span.tex[u][0]; |
| 321 |
dt = (invw1 * vert1->texcoord[u][1]) - span.tex[u][1]; |
| 322 |
dr = (invw1 * vert1->texcoord[u][2]) - span.tex[u][2]; |
| 323 |
dq = (invw1 * vert1->texcoord[u][3]) - span.tex[u][3]; |
| 324 |
span.texStepX[u][0] = ds * invLen; |
| 325 |
span.texStepX[u][1] = dt * invLen; |
| 326 |
span.texStepX[u][2] = dr * invLen; |
| 327 |
span.texStepX[u][3] = dq * invLen; |
| 328 |
span.texStepY[u][0] = 0.0F; |
| 329 |
span.texStepY[u][1] = 0.0F; |
| 330 |
span.texStepY[u][2] = 0.0F; |
| 331 |
span.texStepY[u][3] = 0.0F; |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
#endif |
| 336 |
|
| 337 |
INIT_SPAN(span, GL_LINE, numPixels, interpFlags, SPAN_XY); |
| 338 |
|
| 339 |
/* Need these for fragment prog texcoord interpolation */ |
| 340 |
span.w = 1.0F; |
| 341 |
span.dwdx = 0.0F; |
| 342 |
span.dwdy = 0.0F; |
| 343 |
|
| 344 |
/* |
| 345 |
* Draw |
| 346 |
*/ |
| 347 |
|
| 348 |
if (dx > dy) { |
| 349 |
/*** X-major line ***/ |
| 350 |
GLint i; |
| 351 |
GLint errorInc = dy+dy; |
| 352 |
GLint error = errorInc-dx; |
| 353 |
GLint errorDec = error-dx; |
| 354 |
|
| 355 |
for (i = 0; i < dx; i++) { |
| 356 |
#ifdef DEPTH_TYPE |
| 357 |
GLdepth Z = FixedToDepth(span.z); |
| 358 |
#endif |
| 359 |
#ifdef PLOT |
| 360 |
PLOT( x0, y0 ); |
| 361 |
#else |
| 362 |
span.array->x[i] = x0; |
| 363 |
span.array->y[i] = y0; |
| 364 |
#endif |
| 365 |
x0 += xstep; |
| 366 |
#ifdef DEPTH_TYPE |
| 367 |
zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrXstep); |
| 368 |
span.z += span.zStep; |
| 369 |
#endif |
| 370 |
#ifdef PIXEL_ADDRESS |
| 371 |
pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelXstep); |
| 372 |
#endif |
| 373 |
if (error<0) { |
| 374 |
error += errorInc; |
| 375 |
} |
| 376 |
else { |
| 377 |
error += errorDec; |
| 378 |
y0 += ystep; |
| 379 |
#ifdef DEPTH_TYPE |
| 380 |
zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrYstep); |
| 381 |
#endif |
| 382 |
#ifdef PIXEL_ADDRESS |
| 383 |
pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelYstep); |
| 384 |
#endif |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
else { |
| 389 |
/*** Y-major line ***/ |
| 390 |
GLint i; |
| 391 |
GLint errorInc = dx+dx; |
| 392 |
GLint error = errorInc-dy; |
| 393 |
GLint errorDec = error-dy; |
| 394 |
|
| 395 |
for (i=0;i<dy;i++) { |
| 396 |
#ifdef DEPTH_TYPE |
| 397 |
GLdepth Z = FixedToDepth(span.z); |
| 398 |
#endif |
| 399 |
#ifdef PLOT |
| 400 |
PLOT( x0, y0 ); |
| 401 |
#else |
| 402 |
span.array->x[i] = x0; |
| 403 |
span.array->y[i] = y0; |
| 404 |
#endif |
| 405 |
y0 += ystep; |
| 406 |
#ifdef DEPTH_TYPE |
| 407 |
zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrYstep); |
| 408 |
span.z += span.zStep; |
| 409 |
#endif |
| 410 |
#ifdef PIXEL_ADDRESS |
| 411 |
pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelYstep); |
| 412 |
#endif |
| 413 |
if (error<0) { |
| 414 |
error += errorInc; |
| 415 |
} |
| 416 |
else { |
| 417 |
error += errorDec; |
| 418 |
x0 += xstep; |
| 419 |
#ifdef DEPTH_TYPE |
| 420 |
zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrXstep); |
| 421 |
#endif |
| 422 |
#ifdef PIXEL_ADDRESS |
| 423 |
pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelXstep); |
| 424 |
#endif |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
#ifdef RENDER_SPAN |
| 430 |
RENDER_SPAN( span ); |
| 431 |
#endif |
| 432 |
|
| 433 |
(void)span; |
| 434 |
|
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
#undef NAME |
| 439 |
#undef INTERP_Z |
| 440 |
#undef INTERP_FOG |
| 441 |
#undef INTERP_RGBA |
| 442 |
#undef INTERP_SPEC |
| 443 |
#undef INTERP_TEX |
| 444 |
#undef INTERP_MULTITEX |
| 445 |
#undef INTERP_INDEX |
| 446 |
#undef PIXEL_ADDRESS |
| 447 |
#undef PIXEL_TYPE |
| 448 |
#undef DEPTH_TYPE |
| 449 |
#undef BYTES_PER_ROW |
| 450 |
#undef SETUP_CODE |
| 451 |
#undef PLOT |
| 452 |
#undef CLIP_HACK |
| 453 |
#undef FixedToDepth |
| 454 |
#undef RENDER_SPAN |