s_linetemp.h revision 9ab512ad8cf3a12f4f7f8494fa99bc9389f217db
1/* 2 * Mesa 3-D graphics library 3 * Version: 6.5.3 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_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 * INTERP_VARYING - if defined, interpolate GLSL varyings 41 * 42 * When one can directly address pixels in the color buffer the following 43 * macros can be defined and used to directly compute pixel addresses during 44 * rasterization (see pixelPtr): 45 * PIXEL_TYPE - the datatype of a pixel (GLubyte, GLushort, GLuint) 46 * BYTES_PER_ROW - number of bytes per row in the color buffer 47 * PIXEL_ADDRESS(X,Y) - returns the address of pixel at (X,Y) where 48 * Y==0 at bottom of screen and increases upward. 49 * 50 * Similarly, for direct depth buffer access, this type is used for depth 51 * buffer addressing: 52 * DEPTH_TYPE - either GLushort or GLuint 53 * 54 * Optionally, one may provide one-time setup code 55 * SETUP_CODE - code which is to be executed once per line 56 * 57 * To actually "plot" each pixel the PLOT macro must be defined... 58 * PLOT(X,Y) - code to plot a pixel. Example: 59 * if (Z < *zPtr) { 60 * *zPtr = Z; 61 * color = pack_rgb( FixedToInt(r0), FixedToInt(g0), 62 * FixedToInt(b0) ); 63 * put_pixel( X, Y, color ); 64 * } 65 * 66 * This code was designed for the origin to be in the lower-left corner. 67 * 68 */ 69 70 71static void 72NAME( GLcontext *ctx, const SWvertex *vert0, const SWvertex *vert1 ) 73{ 74 SWspan span; 75 GLuint interpFlags = 0; 76 GLint x0 = (GLint) vert0->win[0]; 77 GLint x1 = (GLint) vert1->win[0]; 78 GLint y0 = (GLint) vert0->win[1]; 79 GLint y1 = (GLint) vert1->win[1]; 80 GLint dx, dy; 81 GLint numPixels; 82 GLint xstep, ystep; 83#if defined(DEPTH_TYPE) 84 const GLint depthBits = ctx->Visual.depthBits; 85 const GLint fixedToDepthShift = depthBits <= 16 ? FIXED_SHIFT : 0; 86 struct gl_renderbuffer *zrb = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer; 87#define FixedToDepth(F) ((F) >> fixedToDepthShift) 88 GLint zPtrXstep, zPtrYstep; 89 DEPTH_TYPE *zPtr; 90#elif defined(INTERP_Z) 91 const GLint depthBits = ctx->Visual.depthBits; 92#endif 93#ifdef PIXEL_ADDRESS 94 PIXEL_TYPE *pixelPtr; 95 GLint pixelXstep, pixelYstep; 96#endif 97 98#ifdef SETUP_CODE 99 SETUP_CODE 100#endif 101 102 /* Cull primitives with malformed coordinates. 103 */ 104 { 105 GLfloat tmp = vert0->win[0] + vert0->win[1] 106 + vert1->win[0] + vert1->win[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->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]); 122 */ 123 124/* 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 129 * necessary. 130 */ 131#ifdef CLIP_HACK 132 { 133 GLint w = ctx->DrawBuffer->Width; 134 GLint h = ctx->DrawBuffer->Height; 135 if ((x0==w) | (x1==w)) { 136 if ((x0==w) & (x1==w)) 137 return; 138 x0 -= x0==w; 139 x1 -= x1==w; 140 } 141 if ((y0==h) | (y1==h)) { 142 if ((y0==h) & (y1==h)) 143 return; 144 y0 -= y0==h; 145 y1 -= y1==h; 146 } 147 } 148#endif 149 150 dx = x1 - x0; 151 dy = y1 - y0; 152 if (dx == 0 && dy == 0) 153 return; 154 155#ifdef DEPTH_TYPE 156 zPtr = (DEPTH_TYPE *) zrb->GetPointer(ctx, zrb, x0, y0); 157#endif 158#ifdef PIXEL_ADDRESS 159 pixelPtr = (PIXEL_TYPE *) PIXEL_ADDRESS(x0,y0); 160#endif 161 162 if (dx<0) { 163 dx = -dx; /* make positive */ 164 xstep = -1; 165#ifdef DEPTH_TYPE 166 zPtrXstep = -((GLint)sizeof(DEPTH_TYPE)); 167#endif 168#ifdef PIXEL_ADDRESS 169 pixelXstep = -((GLint)sizeof(PIXEL_TYPE)); 170#endif 171 } 172 else { 173 xstep = 1; 174#ifdef DEPTH_TYPE 175 zPtrXstep = ((GLint)sizeof(DEPTH_TYPE)); 176#endif 177#ifdef PIXEL_ADDRESS 178 pixelXstep = ((GLint)sizeof(PIXEL_TYPE)); 179#endif 180 } 181 182 if (dy<0) { 183 dy = -dy; /* make positive */ 184 ystep = -1; 185#ifdef DEPTH_TYPE 186 zPtrYstep = -((GLint) (ctx->DrawBuffer->Width * sizeof(DEPTH_TYPE))); 187#endif 188#ifdef PIXEL_ADDRESS 189 pixelYstep = BYTES_PER_ROW; 190#endif 191 } 192 else { 193 ystep = 1; 194#ifdef DEPTH_TYPE 195 zPtrYstep = (GLint) (ctx->DrawBuffer->Width * sizeof(DEPTH_TYPE)); 196#endif 197#ifdef PIXEL_ADDRESS 198 pixelYstep = -(BYTES_PER_ROW); 199#endif 200 } 201 202 ASSERT(dx >= 0); 203 ASSERT(dy >= 0); 204 205 numPixels = MAX2(dx, dy); 206 207 /* 208 * Span setup: compute start and step values for all interpolated values. 209 */ 210#ifdef INTERP_RGBA 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; 221 } 222 else { 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]); 227 span.redStep = 0; 228 span.greenStep = 0; 229 span.blueStep = 0; 230 span.alphaStep = 0; 231 } 232#endif 233#ifdef INTERP_SPEC 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; 242 } 243 else { 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; 250 } 251#endif 252#ifdef INTERP_INDEX 253 interpFlags |= SPAN_INDEX; 254 if (ctx->Light.ShadeModel == GL_SMOOTH) { 255 span.index = FloatToFixed(vert0->index); 256 span.indexStep = FloatToFixed(vert1->index - vert0->index) / numPixels; 257 } 258 else { 259 span.index = FloatToFixed(vert1->index); 260 span.indexStep = 0; 261 } 262#endif 263#if defined(INTERP_Z) || defined(DEPTH_TYPE) 264 interpFlags |= SPAN_Z; 265 { 266 if (depthBits <= 16) { 267 span.z = FloatToFixed(vert0->win[2]) + FIXED_HALF; 268 span.zStep = FloatToFixed(vert1->win[2] - vert0->win[2]) / numPixels; 269 } 270 else { 271 /* don't use fixed point */ 272 span.z = (GLint) vert0->win[2]; 273 span.zStep = (GLint) ((vert1->win[2] - vert0->win[2]) / numPixels); 274 } 275 } 276#endif 277#ifdef INTERP_FOG 278 interpFlags |= SPAN_FOG; 279 span.attrStart[FRAG_ATTRIB_FOGC][0] = vert0->fog; 280 span.attrStepX[FRAG_ATTRIB_FOGC][0] = (vert1->fog - vert0->fog) / numPixels; 281#endif 282#ifdef INTERP_TEX 283 interpFlags |= SPAN_TEXTURE; 284 { 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.attrStart[FRAG_ATTRIB_TEX0][0] = invw0 * vert0->texcoord[0][0]; 290 span.attrStart[FRAG_ATTRIB_TEX0][1] = invw0 * vert0->texcoord[0][1]; 291 span.attrStart[FRAG_ATTRIB_TEX0][2] = invw0 * vert0->texcoord[0][2]; 292 span.attrStart[FRAG_ATTRIB_TEX0][3] = invw0 * vert0->texcoord[0][3]; 293 ds = (invw1 * vert1->texcoord[0][0]) - span.attrStart[FRAG_ATTRIB_TEX0][0]; 294 dt = (invw1 * vert1->texcoord[0][1]) - span.attrStart[FRAG_ATTRIB_TEX0][1]; 295 dr = (invw1 * vert1->texcoord[0][2]) - span.attrStart[FRAG_ATTRIB_TEX0][2]; 296 dq = (invw1 * vert1->texcoord[0][3]) - span.attrStart[FRAG_ATTRIB_TEX0][3]; 297 span.attrStepX[FRAG_ATTRIB_TEX0][0] = ds * invLen; 298 span.attrStepX[FRAG_ATTRIB_TEX0][1] = dt * invLen; 299 span.attrStepX[FRAG_ATTRIB_TEX0][2] = dr * invLen; 300 span.attrStepX[FRAG_ATTRIB_TEX0][3] = dq * invLen; 301 span.attrStepY[FRAG_ATTRIB_TEX0][0] = 0.0F; 302 span.attrStepY[FRAG_ATTRIB_TEX0][1] = 0.0F; 303 span.attrStepY[FRAG_ATTRIB_TEX0][2] = 0.0F; 304 span.attrStepY[FRAG_ATTRIB_TEX0][3] = 0.0F; 305 } 306#endif 307#ifdef INTERP_MULTITEX 308 interpFlags |= SPAN_TEXTURE; 309 { 310 const GLfloat invLen = 1.0F / numPixels; 311 GLuint u; 312 for (u = 0; u < ctx->Const.MaxTextureUnits; u++) { 313 if (ctx->Texture.Unit[u]._ReallyEnabled) { 314 const GLuint attr = FRAG_ATTRIB_TEX0 + u; 315 const GLfloat invw0 = vert0->win[3]; 316 const GLfloat invw1 = vert1->win[3]; 317 GLfloat ds, dt, dr, dq; 318 span.attrStart[attr][0] = invw0 * vert0->texcoord[u][0]; 319 span.attrStart[attr][1] = invw0 * vert0->texcoord[u][1]; 320 span.attrStart[attr][2] = invw0 * vert0->texcoord[u][2]; 321 span.attrStart[attr][3] = invw0 * vert0->texcoord[u][3]; 322 ds = (invw1 * vert1->texcoord[u][0]) - span.attrStart[attr][0]; 323 dt = (invw1 * vert1->texcoord[u][1]) - span.attrStart[attr][1]; 324 dr = (invw1 * vert1->texcoord[u][2]) - span.attrStart[attr][2]; 325 dq = (invw1 * vert1->texcoord[u][3]) - span.attrStart[attr][3]; 326 span.attrStepX[attr][0] = ds * invLen; 327 span.attrStepX[attr][1] = dt * invLen; 328 span.attrStepX[attr][2] = dr * invLen; 329 span.attrStepX[attr][3] = dq * invLen; 330 span.attrStepY[attr][0] = 0.0F; 331 span.attrStepY[attr][1] = 0.0F; 332 span.attrStepY[attr][2] = 0.0F; 333 span.attrStepY[attr][3] = 0.0F; 334 } 335 } 336 } 337#endif 338#ifdef INTERP_VARYING 339 interpFlags |= SPAN_VARYING; 340 { 341 const GLfloat invLen = 1.0F / numPixels; 342 const GLbitfield inputsUsed = ctx->FragmentProgram._Current ? 343 ctx->FragmentProgram._Current->Base.InputsRead : 0x0; 344 const GLfloat invw0 = vert0->win[3]; 345 const GLfloat invw1 = vert1->win[3]; 346 GLuint v; 347 for (v = 0; v < MAX_VARYING; v++) { 348 if (inputsUsed & FRAG_BIT_VAR(v)) { 349 GLuint attr = FRAG_ATTRIB_VAR0 + v; 350 GLfloat ds, dt, dr, dq; 351 span.attrStart[attr][0] = invw0 * vert0->varying[v][0]; 352 span.attrStart[attr][1] = invw0 * vert0->varying[v][1]; 353 span.attrStart[attr][2] = invw0 * vert0->varying[v][2]; 354 span.attrStart[attr][3] = invw0 * vert0->varying[v][3]; 355 ds = (invw1 * vert1->varying[v][0]) - span.attrStart[attr][0]; 356 dt = (invw1 * vert1->varying[v][1]) - span.attrStart[attr][1]; 357 dr = (invw1 * vert1->varying[v][2]) - span.attrStart[attr][2]; 358 dq = (invw1 * vert1->varying[v][3]) - span.attrStart[attr][3]; 359 span.attrStepX[attr][0] = ds * invLen; 360 span.attrStepX[attr][1] = dt * invLen; 361 span.attrStepX[attr][2] = dr * invLen; 362 span.attrStepX[attr][3] = dq * invLen; 363 span.attrStepY[attr][0] = 0.0F; 364 span.attrStepY[attr][1] = 0.0F; 365 span.attrStepY[attr][2] = 0.0F; 366 span.attrStepY[attr][3] = 0.0F; 367 } 368 } 369 } 370#endif 371 372 INIT_SPAN(span, GL_LINE, numPixels, interpFlags, SPAN_XY); 373 374 /* Need these for fragment prog texcoord interpolation */ 375 span.attrStart[FRAG_ATTRIB_WPOS][3] = 1.0F; 376 span.attrStepX[FRAG_ATTRIB_WPOS][3] = 0.0F; 377 span.attrStepY[FRAG_ATTRIB_WPOS][3] = 0.0F; 378 379 /* 380 * Draw 381 */ 382 383 if (dx > dy) { 384 /*** X-major line ***/ 385 GLint i; 386 GLint errorInc = dy+dy; 387 GLint error = errorInc-dx; 388 GLint errorDec = error-dx; 389 390 for (i = 0; i < dx; i++) { 391#ifdef DEPTH_TYPE 392 GLuint Z = FixedToDepth(span.z); 393#endif 394#ifdef PLOT 395 PLOT( x0, y0 ); 396#else 397 span.array->x[i] = x0; 398 span.array->y[i] = y0; 399#endif 400 x0 += xstep; 401#ifdef DEPTH_TYPE 402 zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrXstep); 403 span.z += span.zStep; 404#endif 405#ifdef PIXEL_ADDRESS 406 pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelXstep); 407#endif 408 if (error<0) { 409 error += errorInc; 410 } 411 else { 412 error += errorDec; 413 y0 += ystep; 414#ifdef DEPTH_TYPE 415 zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrYstep); 416#endif 417#ifdef PIXEL_ADDRESS 418 pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelYstep); 419#endif 420 } 421 } 422 } 423 else { 424 /*** Y-major line ***/ 425 GLint i; 426 GLint errorInc = dx+dx; 427 GLint error = errorInc-dy; 428 GLint errorDec = error-dy; 429 430 for (i=0;i<dy;i++) { 431#ifdef DEPTH_TYPE 432 GLuint Z = FixedToDepth(span.z); 433#endif 434#ifdef PLOT 435 PLOT( x0, y0 ); 436#else 437 span.array->x[i] = x0; 438 span.array->y[i] = y0; 439#endif 440 y0 += ystep; 441#ifdef DEPTH_TYPE 442 zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrYstep); 443 span.z += span.zStep; 444#endif 445#ifdef PIXEL_ADDRESS 446 pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelYstep); 447#endif 448 if (error<0) { 449 error += errorInc; 450 } 451 else { 452 error += errorDec; 453 x0 += xstep; 454#ifdef DEPTH_TYPE 455 zPtr = (DEPTH_TYPE *) ((GLubyte*) zPtr + zPtrXstep); 456#endif 457#ifdef PIXEL_ADDRESS 458 pixelPtr = (PIXEL_TYPE*) ((GLubyte*) pixelPtr + pixelXstep); 459#endif 460 } 461 } 462 } 463 464#ifdef RENDER_SPAN 465 RENDER_SPAN( span ); 466#endif 467 468 (void)span; 469 470} 471 472 473#undef NAME 474#undef INTERP_Z 475#undef INTERP_FOG 476#undef INTERP_RGBA 477#undef INTERP_SPEC 478#undef INTERP_TEX 479#undef INTERP_MULTITEX 480#undef INTERP_INDEX 481#undef PIXEL_ADDRESS 482#undef PIXEL_TYPE 483#undef DEPTH_TYPE 484#undef BYTES_PER_ROW 485#undef SETUP_CODE 486#undef PLOT 487#undef CLIP_HACK 488#undef FixedToDepth 489#undef RENDER_SPAN 490