s_span.c revision 60121c41ce9ee50ff8d8476d4eb04867adf9b8c5
1/* 2 * Mesa 3-D graphics library 3 * Version: 7.5 4 * 5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved. 6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved. 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a 9 * copy of this software and associated documentation files (the "Software"), 10 * to deal in the Software without restriction, including without limitation 11 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 * and/or sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included 16 * in all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 26 27/** 28 * \file swrast/s_span.c 29 * \brief Span processing functions used by all rasterization functions. 30 * This is where all the per-fragment tests are performed 31 * \author Brian Paul 32 */ 33 34#include "main/glheader.h" 35#include "main/colormac.h" 36#include "main/context.h" 37#include "main/macros.h" 38#include "main/imports.h" 39#include "main/image.h" 40 41#include "s_atifragshader.h" 42#include "s_alpha.h" 43#include "s_blend.h" 44#include "s_context.h" 45#include "s_depth.h" 46#include "s_fog.h" 47#include "s_logic.h" 48#include "s_masking.h" 49#include "s_fragprog.h" 50#include "s_span.h" 51#include "s_stencil.h" 52#include "s_texcombine.h" 53 54 55/** 56 * Set default fragment attributes for the span using the 57 * current raster values. Used prior to glDraw/CopyPixels 58 * and glBitmap. 59 */ 60void 61_swrast_span_default_attribs(GLcontext *ctx, SWspan *span) 62{ 63 GLchan r, g, b, a; 64 /* Z*/ 65 { 66 const GLfloat depthMax = ctx->DrawBuffer->_DepthMaxF; 67 if (ctx->DrawBuffer->Visual.depthBits <= 16) 68 span->z = FloatToFixed(ctx->Current.RasterPos[2] * depthMax + 0.5F); 69 else { 70 GLfloat tmpf = ctx->Current.RasterPos[2] * depthMax; 71 tmpf = MIN2(tmpf, depthMax); 72 span->z = (GLint)tmpf; 73 } 74 span->zStep = 0; 75 span->interpMask |= SPAN_Z; 76 } 77 78 /* W (for perspective correction) */ 79 span->attrStart[FRAG_ATTRIB_WPOS][3] = 1.0; 80 span->attrStepX[FRAG_ATTRIB_WPOS][3] = 0.0; 81 span->attrStepY[FRAG_ATTRIB_WPOS][3] = 0.0; 82 83 /* primary color, or color index */ 84 UNCLAMPED_FLOAT_TO_CHAN(r, ctx->Current.RasterColor[0]); 85 UNCLAMPED_FLOAT_TO_CHAN(g, ctx->Current.RasterColor[1]); 86 UNCLAMPED_FLOAT_TO_CHAN(b, ctx->Current.RasterColor[2]); 87 UNCLAMPED_FLOAT_TO_CHAN(a, ctx->Current.RasterColor[3]); 88#if CHAN_TYPE == GL_FLOAT 89 span->red = r; 90 span->green = g; 91 span->blue = b; 92 span->alpha = a; 93#else 94 span->red = IntToFixed(r); 95 span->green = IntToFixed(g); 96 span->blue = IntToFixed(b); 97 span->alpha = IntToFixed(a); 98#endif 99 span->redStep = 0; 100 span->greenStep = 0; 101 span->blueStep = 0; 102 span->alphaStep = 0; 103 span->interpMask |= SPAN_RGBA; 104 105 COPY_4V(span->attrStart[FRAG_ATTRIB_COL0], ctx->Current.RasterColor); 106 ASSIGN_4V(span->attrStepX[FRAG_ATTRIB_COL0], 0.0, 0.0, 0.0, 0.0); 107 ASSIGN_4V(span->attrStepY[FRAG_ATTRIB_COL0], 0.0, 0.0, 0.0, 0.0); 108 109 /* Secondary color */ 110 if (ctx->Light.Enabled || ctx->Fog.ColorSumEnabled) 111 { 112 COPY_4V(span->attrStart[FRAG_ATTRIB_COL1], ctx->Current.RasterSecondaryColor); 113 ASSIGN_4V(span->attrStepX[FRAG_ATTRIB_COL1], 0.0, 0.0, 0.0, 0.0); 114 ASSIGN_4V(span->attrStepY[FRAG_ATTRIB_COL1], 0.0, 0.0, 0.0, 0.0); 115 } 116 117 /* fog */ 118 { 119 const SWcontext *swrast = SWRAST_CONTEXT(ctx); 120 GLfloat fogVal; /* a coord or a blend factor */ 121 if (swrast->_PreferPixelFog) { 122 /* fog blend factors will be computed from fog coordinates per pixel */ 123 fogVal = ctx->Current.RasterDistance; 124 } 125 else { 126 /* fog blend factor should be computed from fogcoord now */ 127 fogVal = _swrast_z_to_fogfactor(ctx, ctx->Current.RasterDistance); 128 } 129 span->attrStart[FRAG_ATTRIB_FOGC][0] = fogVal; 130 span->attrStepX[FRAG_ATTRIB_FOGC][0] = 0.0; 131 span->attrStepY[FRAG_ATTRIB_FOGC][0] = 0.0; 132 } 133 134 /* texcoords */ 135 { 136 GLuint i; 137 for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) { 138 const GLuint attr = FRAG_ATTRIB_TEX0 + i; 139 const GLfloat *tc = ctx->Current.RasterTexCoords[i]; 140 if (ctx->FragmentProgram._Current || ctx->ATIFragmentShader._Enabled) { 141 COPY_4V(span->attrStart[attr], tc); 142 } 143 else if (tc[3] > 0.0F) { 144 /* use (s/q, t/q, r/q, 1) */ 145 span->attrStart[attr][0] = tc[0] / tc[3]; 146 span->attrStart[attr][1] = tc[1] / tc[3]; 147 span->attrStart[attr][2] = tc[2] / tc[3]; 148 span->attrStart[attr][3] = 1.0; 149 } 150 else { 151 ASSIGN_4V(span->attrStart[attr], 0.0F, 0.0F, 0.0F, 1.0F); 152 } 153 ASSIGN_4V(span->attrStepX[attr], 0.0F, 0.0F, 0.0F, 0.0F); 154 ASSIGN_4V(span->attrStepY[attr], 0.0F, 0.0F, 0.0F, 0.0F); 155 } 156 } 157} 158 159 160/** 161 * Interpolate the active attributes (and'd with attrMask) to 162 * fill in span->array->attribs[]. 163 * Perspective correction will be done. The point/line/triangle function 164 * should have computed attrStart/Step values for FRAG_ATTRIB_WPOS[3]! 165 */ 166static INLINE void 167interpolate_active_attribs(GLcontext *ctx, SWspan *span, GLbitfield attrMask) 168{ 169 const SWcontext *swrast = SWRAST_CONTEXT(ctx); 170 171 /* 172 * Don't overwrite existing array values, such as colors that may have 173 * been produced by glDraw/CopyPixels. 174 */ 175 attrMask &= ~span->arrayAttribs; 176 177 ATTRIB_LOOP_BEGIN 178 if (attrMask & (1 << attr)) { 179 const GLfloat dwdx = span->attrStepX[FRAG_ATTRIB_WPOS][3]; 180 GLfloat w = span->attrStart[FRAG_ATTRIB_WPOS][3]; 181 const GLfloat dv0dx = span->attrStepX[attr][0]; 182 const GLfloat dv1dx = span->attrStepX[attr][1]; 183 const GLfloat dv2dx = span->attrStepX[attr][2]; 184 const GLfloat dv3dx = span->attrStepX[attr][3]; 185 GLfloat v0 = span->attrStart[attr][0] + span->leftClip * dv0dx; 186 GLfloat v1 = span->attrStart[attr][1] + span->leftClip * dv1dx; 187 GLfloat v2 = span->attrStart[attr][2] + span->leftClip * dv2dx; 188 GLfloat v3 = span->attrStart[attr][3] + span->leftClip * dv3dx; 189 GLuint k; 190 for (k = 0; k < span->end; k++) { 191 const GLfloat invW = 1.0f / w; 192 span->array->attribs[attr][k][0] = v0 * invW; 193 span->array->attribs[attr][k][1] = v1 * invW; 194 span->array->attribs[attr][k][2] = v2 * invW; 195 span->array->attribs[attr][k][3] = v3 * invW; 196 v0 += dv0dx; 197 v1 += dv1dx; 198 v2 += dv2dx; 199 v3 += dv3dx; 200 w += dwdx; 201 } 202 ASSERT((span->arrayAttribs & (1 << attr)) == 0); 203 span->arrayAttribs |= (1 << attr); 204 } 205 ATTRIB_LOOP_END 206} 207 208 209/** 210 * Interpolate primary colors to fill in the span->array->rgba8 (or rgb16) 211 * color array. 212 */ 213static INLINE void 214interpolate_int_colors(GLcontext *ctx, SWspan *span) 215{ 216 const GLuint n = span->end; 217 GLuint i; 218 219#if CHAN_BITS != 32 220 ASSERT(!(span->arrayMask & SPAN_RGBA)); 221#endif 222 223 switch (span->array->ChanType) { 224#if CHAN_BITS != 32 225 case GL_UNSIGNED_BYTE: 226 { 227 GLubyte (*rgba)[4] = span->array->rgba8; 228 if (span->interpMask & SPAN_FLAT) { 229 GLubyte color[4]; 230 color[RCOMP] = FixedToInt(span->red); 231 color[GCOMP] = FixedToInt(span->green); 232 color[BCOMP] = FixedToInt(span->blue); 233 color[ACOMP] = FixedToInt(span->alpha); 234 for (i = 0; i < n; i++) { 235 COPY_4UBV(rgba[i], color); 236 } 237 } 238 else { 239 GLfixed r = span->red; 240 GLfixed g = span->green; 241 GLfixed b = span->blue; 242 GLfixed a = span->alpha; 243 GLint dr = span->redStep; 244 GLint dg = span->greenStep; 245 GLint db = span->blueStep; 246 GLint da = span->alphaStep; 247 for (i = 0; i < n; i++) { 248 rgba[i][RCOMP] = FixedToChan(r); 249 rgba[i][GCOMP] = FixedToChan(g); 250 rgba[i][BCOMP] = FixedToChan(b); 251 rgba[i][ACOMP] = FixedToChan(a); 252 r += dr; 253 g += dg; 254 b += db; 255 a += da; 256 } 257 } 258 } 259 break; 260 case GL_UNSIGNED_SHORT: 261 { 262 GLushort (*rgba)[4] = span->array->rgba16; 263 if (span->interpMask & SPAN_FLAT) { 264 GLushort color[4]; 265 color[RCOMP] = FixedToInt(span->red); 266 color[GCOMP] = FixedToInt(span->green); 267 color[BCOMP] = FixedToInt(span->blue); 268 color[ACOMP] = FixedToInt(span->alpha); 269 for (i = 0; i < n; i++) { 270 COPY_4V(rgba[i], color); 271 } 272 } 273 else { 274 GLushort (*rgba)[4] = span->array->rgba16; 275 GLfixed r, g, b, a; 276 GLint dr, dg, db, da; 277 r = span->red; 278 g = span->green; 279 b = span->blue; 280 a = span->alpha; 281 dr = span->redStep; 282 dg = span->greenStep; 283 db = span->blueStep; 284 da = span->alphaStep; 285 for (i = 0; i < n; i++) { 286 rgba[i][RCOMP] = FixedToChan(r); 287 rgba[i][GCOMP] = FixedToChan(g); 288 rgba[i][BCOMP] = FixedToChan(b); 289 rgba[i][ACOMP] = FixedToChan(a); 290 r += dr; 291 g += dg; 292 b += db; 293 a += da; 294 } 295 } 296 } 297 break; 298#endif 299 case GL_FLOAT: 300 interpolate_active_attribs(ctx, span, FRAG_BIT_COL0); 301 break; 302 default: 303 _mesa_problem(NULL, "bad datatype in interpolate_int_colors"); 304 } 305 span->arrayMask |= SPAN_RGBA; 306} 307 308 309/** 310 * Populate the FRAG_ATTRIB_COL0 array. 311 */ 312static INLINE void 313interpolate_float_colors(SWspan *span) 314{ 315 GLfloat (*col0)[4] = span->array->attribs[FRAG_ATTRIB_COL0]; 316 const GLuint n = span->end; 317 GLuint i; 318 319 assert(!(span->arrayAttribs & FRAG_BIT_COL0)); 320 321 if (span->arrayMask & SPAN_RGBA) { 322 /* convert array of int colors */ 323 for (i = 0; i < n; i++) { 324 col0[i][0] = UBYTE_TO_FLOAT(span->array->rgba8[i][0]); 325 col0[i][1] = UBYTE_TO_FLOAT(span->array->rgba8[i][1]); 326 col0[i][2] = UBYTE_TO_FLOAT(span->array->rgba8[i][2]); 327 col0[i][3] = UBYTE_TO_FLOAT(span->array->rgba8[i][3]); 328 } 329 } 330 else { 331 /* interpolate red/green/blue/alpha to get float colors */ 332 ASSERT(span->interpMask & SPAN_RGBA); 333 if (span->interpMask & SPAN_FLAT) { 334 GLfloat r = FixedToFloat(span->red); 335 GLfloat g = FixedToFloat(span->green); 336 GLfloat b = FixedToFloat(span->blue); 337 GLfloat a = FixedToFloat(span->alpha); 338 for (i = 0; i < n; i++) { 339 ASSIGN_4V(col0[i], r, g, b, a); 340 } 341 } 342 else { 343 GLfloat r = FixedToFloat(span->red); 344 GLfloat g = FixedToFloat(span->green); 345 GLfloat b = FixedToFloat(span->blue); 346 GLfloat a = FixedToFloat(span->alpha); 347 GLfloat dr = FixedToFloat(span->redStep); 348 GLfloat dg = FixedToFloat(span->greenStep); 349 GLfloat db = FixedToFloat(span->blueStep); 350 GLfloat da = FixedToFloat(span->alphaStep); 351 for (i = 0; i < n; i++) { 352 col0[i][0] = r; 353 col0[i][1] = g; 354 col0[i][2] = b; 355 col0[i][3] = a; 356 r += dr; 357 g += dg; 358 b += db; 359 a += da; 360 } 361 } 362 } 363 364 span->arrayAttribs |= FRAG_BIT_COL0; 365 span->array->ChanType = GL_FLOAT; 366} 367 368 369 370/** 371 * Fill in the span.zArray array from the span->z, zStep values. 372 */ 373void 374_swrast_span_interpolate_z( const GLcontext *ctx, SWspan *span ) 375{ 376 const GLuint n = span->end; 377 GLuint i; 378 379 ASSERT(!(span->arrayMask & SPAN_Z)); 380 381 if (ctx->DrawBuffer->Visual.depthBits <= 16) { 382 GLfixed zval = span->z; 383 GLuint *z = span->array->z; 384 for (i = 0; i < n; i++) { 385 z[i] = FixedToInt(zval); 386 zval += span->zStep; 387 } 388 } 389 else { 390 /* Deep Z buffer, no fixed->int shift */ 391 GLuint zval = span->z; 392 GLuint *z = span->array->z; 393 for (i = 0; i < n; i++) { 394 z[i] = zval; 395 zval += span->zStep; 396 } 397 } 398 span->interpMask &= ~SPAN_Z; 399 span->arrayMask |= SPAN_Z; 400} 401 402 403/** 404 * Compute mipmap LOD from partial derivatives. 405 * This the ideal solution, as given in the OpenGL spec. 406 */ 407GLfloat 408_swrast_compute_lambda(GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy, 409 GLfloat dqdx, GLfloat dqdy, GLfloat texW, GLfloat texH, 410 GLfloat s, GLfloat t, GLfloat q, GLfloat invQ) 411{ 412 GLfloat dudx = texW * ((s + dsdx) / (q + dqdx) - s * invQ); 413 GLfloat dvdx = texH * ((t + dtdx) / (q + dqdx) - t * invQ); 414 GLfloat dudy = texW * ((s + dsdy) / (q + dqdy) - s * invQ); 415 GLfloat dvdy = texH * ((t + dtdy) / (q + dqdy) - t * invQ); 416 GLfloat x = SQRTF(dudx * dudx + dvdx * dvdx); 417 GLfloat y = SQRTF(dudy * dudy + dvdy * dvdy); 418 GLfloat rho = MAX2(x, y); 419 GLfloat lambda = LOG2(rho); 420 return lambda; 421} 422 423 424/** 425 * Compute mipmap LOD from partial derivatives. 426 * This is a faster approximation than above function. 427 */ 428#if 0 429GLfloat 430_swrast_compute_lambda(GLfloat dsdx, GLfloat dsdy, GLfloat dtdx, GLfloat dtdy, 431 GLfloat dqdx, GLfloat dqdy, GLfloat texW, GLfloat texH, 432 GLfloat s, GLfloat t, GLfloat q, GLfloat invQ) 433{ 434 GLfloat dsdx2 = (s + dsdx) / (q + dqdx) - s * invQ; 435 GLfloat dtdx2 = (t + dtdx) / (q + dqdx) - t * invQ; 436 GLfloat dsdy2 = (s + dsdy) / (q + dqdy) - s * invQ; 437 GLfloat dtdy2 = (t + dtdy) / (q + dqdy) - t * invQ; 438 GLfloat maxU, maxV, rho, lambda; 439 dsdx2 = FABSF(dsdx2); 440 dsdy2 = FABSF(dsdy2); 441 dtdx2 = FABSF(dtdx2); 442 dtdy2 = FABSF(dtdy2); 443 maxU = MAX2(dsdx2, dsdy2) * texW; 444 maxV = MAX2(dtdx2, dtdy2) * texH; 445 rho = MAX2(maxU, maxV); 446 lambda = LOG2(rho); 447 return lambda; 448} 449#endif 450 451 452/** 453 * Fill in the span.array->attrib[FRAG_ATTRIB_TEXn] arrays from the 454 * using the attrStart/Step values. 455 * 456 * This function only used during fixed-function fragment processing. 457 * 458 * Note: in the places where we divide by Q (or mult by invQ) we're 459 * really doing two things: perspective correction and texcoord 460 * projection. Remember, for texcoord (s,t,r,q) we need to index 461 * texels with (s/q, t/q, r/q). 462 */ 463static void 464interpolate_texcoords(GLcontext *ctx, SWspan *span) 465{ 466 const GLuint maxUnit 467 = (ctx->Texture._EnabledCoordUnits > 1) ? ctx->Const.MaxTextureUnits : 1; 468 GLuint u; 469 470 /* XXX CoordUnits vs. ImageUnits */ 471 for (u = 0; u < maxUnit; u++) { 472 if (ctx->Texture._EnabledCoordUnits & (1 << u)) { 473 const GLuint attr = FRAG_ATTRIB_TEX0 + u; 474 const struct gl_texture_object *obj = ctx->Texture.Unit[u]._Current; 475 GLfloat texW, texH; 476 GLboolean needLambda; 477 GLfloat (*texcoord)[4] = span->array->attribs[attr]; 478 GLfloat *lambda = span->array->lambda[u]; 479 const GLfloat dsdx = span->attrStepX[attr][0]; 480 const GLfloat dsdy = span->attrStepY[attr][0]; 481 const GLfloat dtdx = span->attrStepX[attr][1]; 482 const GLfloat dtdy = span->attrStepY[attr][1]; 483 const GLfloat drdx = span->attrStepX[attr][2]; 484 const GLfloat dqdx = span->attrStepX[attr][3]; 485 const GLfloat dqdy = span->attrStepY[attr][3]; 486 GLfloat s = span->attrStart[attr][0] + span->leftClip * dsdx; 487 GLfloat t = span->attrStart[attr][1] + span->leftClip * dtdx; 488 GLfloat r = span->attrStart[attr][2] + span->leftClip * drdx; 489 GLfloat q = span->attrStart[attr][3] + span->leftClip * dqdx; 490 491 if (obj) { 492 const struct gl_texture_image *img = obj->Image[0][obj->BaseLevel]; 493 needLambda = (obj->MinFilter != obj->MagFilter) 494 || ctx->FragmentProgram._Current; 495 texW = img->WidthScale; 496 texH = img->HeightScale; 497 } 498 else { 499 /* using a fragment program */ 500 texW = 1.0; 501 texH = 1.0; 502 needLambda = GL_FALSE; 503 } 504 505 if (needLambda) { 506 GLuint i; 507 if (ctx->FragmentProgram._Current 508 || ctx->ATIFragmentShader._Enabled) { 509 /* do perspective correction but don't divide s, t, r by q */ 510 const GLfloat dwdx = span->attrStepX[FRAG_ATTRIB_WPOS][3]; 511 GLfloat w = span->attrStart[FRAG_ATTRIB_WPOS][3] + span->leftClip * dwdx; 512 for (i = 0; i < span->end; i++) { 513 const GLfloat invW = 1.0F / w; 514 texcoord[i][0] = s * invW; 515 texcoord[i][1] = t * invW; 516 texcoord[i][2] = r * invW; 517 texcoord[i][3] = q * invW; 518 lambda[i] = _swrast_compute_lambda(dsdx, dsdy, dtdx, dtdy, 519 dqdx, dqdy, texW, texH, 520 s, t, q, invW); 521 s += dsdx; 522 t += dtdx; 523 r += drdx; 524 q += dqdx; 525 w += dwdx; 526 } 527 } 528 else { 529 for (i = 0; i < span->end; i++) { 530 const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q); 531 texcoord[i][0] = s * invQ; 532 texcoord[i][1] = t * invQ; 533 texcoord[i][2] = r * invQ; 534 texcoord[i][3] = q; 535 lambda[i] = _swrast_compute_lambda(dsdx, dsdy, dtdx, dtdy, 536 dqdx, dqdy, texW, texH, 537 s, t, q, invQ); 538 s += dsdx; 539 t += dtdx; 540 r += drdx; 541 q += dqdx; 542 } 543 } 544 span->arrayMask |= SPAN_LAMBDA; 545 } 546 else { 547 GLuint i; 548 if (ctx->FragmentProgram._Current || 549 ctx->ATIFragmentShader._Enabled) { 550 /* do perspective correction but don't divide s, t, r by q */ 551 const GLfloat dwdx = span->attrStepX[FRAG_ATTRIB_WPOS][3]; 552 GLfloat w = span->attrStart[FRAG_ATTRIB_WPOS][3] + span->leftClip * dwdx; 553 for (i = 0; i < span->end; i++) { 554 const GLfloat invW = 1.0F / w; 555 texcoord[i][0] = s * invW; 556 texcoord[i][1] = t * invW; 557 texcoord[i][2] = r * invW; 558 texcoord[i][3] = q * invW; 559 lambda[i] = 0.0; 560 s += dsdx; 561 t += dtdx; 562 r += drdx; 563 q += dqdx; 564 w += dwdx; 565 } 566 } 567 else if (dqdx == 0.0F) { 568 /* Ortho projection or polygon's parallel to window X axis */ 569 const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q); 570 for (i = 0; i < span->end; i++) { 571 texcoord[i][0] = s * invQ; 572 texcoord[i][1] = t * invQ; 573 texcoord[i][2] = r * invQ; 574 texcoord[i][3] = q; 575 lambda[i] = 0.0; 576 s += dsdx; 577 t += dtdx; 578 r += drdx; 579 } 580 } 581 else { 582 for (i = 0; i < span->end; i++) { 583 const GLfloat invQ = (q == 0.0F) ? 1.0F : (1.0F / q); 584 texcoord[i][0] = s * invQ; 585 texcoord[i][1] = t * invQ; 586 texcoord[i][2] = r * invQ; 587 texcoord[i][3] = q; 588 lambda[i] = 0.0; 589 s += dsdx; 590 t += dtdx; 591 r += drdx; 592 q += dqdx; 593 } 594 } 595 } /* lambda */ 596 } /* if */ 597 } /* for */ 598} 599 600 601/** 602 * Fill in the arrays->attribs[FRAG_ATTRIB_WPOS] array. 603 */ 604static INLINE void 605interpolate_wpos(GLcontext *ctx, SWspan *span) 606{ 607 GLfloat (*wpos)[4] = span->array->attribs[FRAG_ATTRIB_WPOS]; 608 GLuint i; 609 const GLfloat zScale = 1.0F / ctx->DrawBuffer->_DepthMaxF; 610 GLfloat w, dw; 611 612 if (span->arrayMask & SPAN_XY) { 613 for (i = 0; i < span->end; i++) { 614 wpos[i][0] = (GLfloat) span->array->x[i]; 615 wpos[i][1] = (GLfloat) span->array->y[i]; 616 } 617 } 618 else { 619 for (i = 0; i < span->end; i++) { 620 wpos[i][0] = (GLfloat) span->x + i; 621 wpos[i][1] = (GLfloat) span->y; 622 } 623 } 624 625 dw = span->attrStepX[FRAG_ATTRIB_WPOS][3]; 626 w = span->attrStart[FRAG_ATTRIB_WPOS][3] + span->leftClip * dw; 627 for (i = 0; i < span->end; i++) { 628 wpos[i][2] = (GLfloat) span->array->z[i] * zScale; 629 wpos[i][3] = w; 630 w += dw; 631 } 632} 633 634 635/** 636 * Apply the current polygon stipple pattern to a span of pixels. 637 */ 638static INLINE void 639stipple_polygon_span(GLcontext *ctx, SWspan *span) 640{ 641 GLubyte *mask = span->array->mask; 642 643 ASSERT(ctx->Polygon.StippleFlag); 644 645 if (span->arrayMask & SPAN_XY) { 646 /* arrays of x/y pixel coords */ 647 GLuint i; 648 for (i = 0; i < span->end; i++) { 649 const GLint col = span->array->x[i] % 32; 650 const GLint row = span->array->y[i] % 32; 651 const GLuint stipple = ctx->PolygonStipple[row]; 652 if (((1 << col) & stipple) == 0) { 653 mask[i] = 0; 654 } 655 } 656 } 657 else { 658 /* horizontal span of pixels */ 659 const GLuint highBit = 1 << 31; 660 const GLuint stipple = ctx->PolygonStipple[span->y % 32]; 661 GLuint i, m = highBit >> (GLuint) (span->x % 32); 662 for (i = 0; i < span->end; i++) { 663 if ((m & stipple) == 0) { 664 mask[i] = 0; 665 } 666 m = m >> 1; 667 if (m == 0) { 668 m = highBit; 669 } 670 } 671 } 672 span->writeAll = GL_FALSE; 673} 674 675 676/** 677 * Clip a pixel span to the current buffer/window boundaries: 678 * DrawBuffer->_Xmin, _Xmax, _Ymin, _Ymax. This will accomplish 679 * window clipping and scissoring. 680 * Return: GL_TRUE some pixels still visible 681 * GL_FALSE nothing visible 682 */ 683static INLINE GLuint 684clip_span( GLcontext *ctx, SWspan *span ) 685{ 686 const GLint xmin = ctx->DrawBuffer->_Xmin; 687 const GLint xmax = ctx->DrawBuffer->_Xmax; 688 const GLint ymin = ctx->DrawBuffer->_Ymin; 689 const GLint ymax = ctx->DrawBuffer->_Ymax; 690 691 span->leftClip = 0; 692 693 if (span->arrayMask & SPAN_XY) { 694 /* arrays of x/y pixel coords */ 695 const GLint *x = span->array->x; 696 const GLint *y = span->array->y; 697 const GLint n = span->end; 698 GLubyte *mask = span->array->mask; 699 GLint i; 700 if (span->arrayMask & SPAN_MASK) { 701 /* note: using & intead of && to reduce branches */ 702 for (i = 0; i < n; i++) { 703 mask[i] &= (x[i] >= xmin) & (x[i] < xmax) 704 & (y[i] >= ymin) & (y[i] < ymax); 705 } 706 } 707 else { 708 /* note: using & intead of && to reduce branches */ 709 for (i = 0; i < n; i++) { 710 mask[i] = (x[i] >= xmin) & (x[i] < xmax) 711 & (y[i] >= ymin) & (y[i] < ymax); 712 } 713 } 714 return GL_TRUE; /* some pixels visible */ 715 } 716 else { 717 /* horizontal span of pixels */ 718 const GLint x = span->x; 719 const GLint y = span->y; 720 GLint n = span->end; 721 722 /* Trivial rejection tests */ 723 if (y < ymin || y >= ymax || x + n <= xmin || x >= xmax) { 724 span->end = 0; 725 return GL_FALSE; /* all pixels clipped */ 726 } 727 728 /* Clip to right */ 729 if (x + n > xmax) { 730 ASSERT(x < xmax); 731 n = span->end = xmax - x; 732 } 733 734 /* Clip to the left */ 735 if (x < xmin) { 736 const GLint leftClip = xmin - x; 737 GLuint i; 738 739 ASSERT(leftClip > 0); 740 ASSERT(x + n > xmin); 741 742 /* Clip 'leftClip' pixels from the left side. 743 * The span->leftClip field will be applied when we interpolate 744 * fragment attributes. 745 * For arrays of values, shift them left. 746 */ 747 for (i = 0; i < FRAG_ATTRIB_MAX; i++) { 748 if (span->interpMask & (1 << i)) { 749 GLuint j; 750 for (j = 0; j < 4; j++) { 751 span->attrStart[i][j] += leftClip * span->attrStepX[i][j]; 752 } 753 } 754 } 755 756 span->red += leftClip * span->redStep; 757 span->green += leftClip * span->greenStep; 758 span->blue += leftClip * span->blueStep; 759 span->alpha += leftClip * span->alphaStep; 760 span->index += leftClip * span->indexStep; 761 span->z += leftClip * span->zStep; 762 span->intTex[0] += leftClip * span->intTexStep[0]; 763 span->intTex[1] += leftClip * span->intTexStep[1]; 764 765#define SHIFT_ARRAY(ARRAY, SHIFT, LEN) \ 766 memcpy(ARRAY, ARRAY + (SHIFT), (LEN) * sizeof(ARRAY[0])) 767 768 for (i = 0; i < FRAG_ATTRIB_MAX; i++) { 769 if (span->arrayAttribs & (1 << i)) { 770 /* shift array elements left by 'leftClip' */ 771 SHIFT_ARRAY(span->array->attribs[i], leftClip, n - leftClip); 772 } 773 } 774 775 SHIFT_ARRAY(span->array->mask, leftClip, n - leftClip); 776 SHIFT_ARRAY(span->array->rgba8, leftClip, n - leftClip); 777 SHIFT_ARRAY(span->array->rgba16, leftClip, n - leftClip); 778 SHIFT_ARRAY(span->array->x, leftClip, n - leftClip); 779 SHIFT_ARRAY(span->array->y, leftClip, n - leftClip); 780 SHIFT_ARRAY(span->array->z, leftClip, n - leftClip); 781 SHIFT_ARRAY(span->array->index, leftClip, n - leftClip); 782 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) { 783 SHIFT_ARRAY(span->array->lambda[i], leftClip, n - leftClip); 784 } 785 SHIFT_ARRAY(span->array->coverage, leftClip, n - leftClip); 786 787#undef SHIFT_ARRAY 788 789 span->leftClip = leftClip; 790 span->x = xmin; 791 span->end -= leftClip; 792 span->writeAll = GL_FALSE; 793 } 794 795 ASSERT(span->x >= xmin); 796 ASSERT(span->x + span->end <= xmax); 797 ASSERT(span->y >= ymin); 798 ASSERT(span->y < ymax); 799 800 return GL_TRUE; /* some pixels visible */ 801 } 802} 803 804 805/** 806 * Add specular colors to primary colors. 807 * Only called during fixed-function operation. 808 * Result is float color array (FRAG_ATTRIB_COL0). 809 */ 810static INLINE void 811add_specular(GLcontext *ctx, SWspan *span) 812{ 813 const SWcontext *swrast = SWRAST_CONTEXT(ctx); 814 const GLubyte *mask = span->array->mask; 815 GLfloat (*col0)[4] = span->array->attribs[FRAG_ATTRIB_COL0]; 816 GLfloat (*col1)[4] = span->array->attribs[FRAG_ATTRIB_COL1]; 817 GLuint i; 818 819 ASSERT(!ctx->FragmentProgram._Current); 820 ASSERT(span->arrayMask & SPAN_RGBA); 821 ASSERT(swrast->_ActiveAttribMask & FRAG_BIT_COL1); 822 (void) swrast; /* silence warning */ 823 824 if (span->array->ChanType == GL_FLOAT) { 825 if ((span->arrayAttribs & FRAG_BIT_COL0) == 0) { 826 interpolate_active_attribs(ctx, span, FRAG_BIT_COL0); 827 } 828 } 829 else { 830 /* need float colors */ 831 if ((span->arrayAttribs & FRAG_BIT_COL0) == 0) { 832 interpolate_float_colors(span); 833 } 834 } 835 836 if ((span->arrayAttribs & FRAG_BIT_COL1) == 0) { 837 /* XXX could avoid this and interpolate COL1 in the loop below */ 838 interpolate_active_attribs(ctx, span, FRAG_BIT_COL1); 839 } 840 841 ASSERT(span->arrayAttribs & FRAG_BIT_COL0); 842 ASSERT(span->arrayAttribs & FRAG_BIT_COL1); 843 844 for (i = 0; i < span->end; i++) { 845 if (mask[i]) { 846 col0[i][0] += col1[i][0]; 847 col0[i][1] += col1[i][1]; 848 col0[i][2] += col1[i][2]; 849 } 850 } 851 852 span->array->ChanType = GL_FLOAT; 853} 854 855 856/** 857 * Apply antialiasing coverage value to alpha values. 858 */ 859static INLINE void 860apply_aa_coverage(SWspan *span) 861{ 862 const GLfloat *coverage = span->array->coverage; 863 GLuint i; 864 if (span->array->ChanType == GL_UNSIGNED_BYTE) { 865 GLubyte (*rgba)[4] = span->array->rgba8; 866 for (i = 0; i < span->end; i++) { 867 const GLfloat a = rgba[i][ACOMP] * coverage[i]; 868 rgba[i][ACOMP] = (GLubyte) CLAMP(a, 0.0, 255.0); 869 ASSERT(coverage[i] >= 0.0); 870 ASSERT(coverage[i] <= 1.0); 871 } 872 } 873 else if (span->array->ChanType == GL_UNSIGNED_SHORT) { 874 GLushort (*rgba)[4] = span->array->rgba16; 875 for (i = 0; i < span->end; i++) { 876 const GLfloat a = rgba[i][ACOMP] * coverage[i]; 877 rgba[i][ACOMP] = (GLushort) CLAMP(a, 0.0, 65535.0); 878 } 879 } 880 else { 881 GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL0]; 882 for (i = 0; i < span->end; i++) { 883 rgba[i][ACOMP] = rgba[i][ACOMP] * coverage[i]; 884 /* clamp later */ 885 } 886 } 887} 888 889 890/** 891 * Clamp span's float colors to [0,1] 892 */ 893static INLINE void 894clamp_colors(SWspan *span) 895{ 896 GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL0]; 897 GLuint i; 898 ASSERT(span->array->ChanType == GL_FLOAT); 899 for (i = 0; i < span->end; i++) { 900 rgba[i][RCOMP] = CLAMP(rgba[i][RCOMP], 0.0F, 1.0F); 901 rgba[i][GCOMP] = CLAMP(rgba[i][GCOMP], 0.0F, 1.0F); 902 rgba[i][BCOMP] = CLAMP(rgba[i][BCOMP], 0.0F, 1.0F); 903 rgba[i][ACOMP] = CLAMP(rgba[i][ACOMP], 0.0F, 1.0F); 904 } 905} 906 907 908/** 909 * Convert the span's color arrays to the given type. 910 * The only way 'output' can be greater than zero is when we have a fragment 911 * program that writes to gl_FragData[1] or higher. 912 * \param output which fragment program color output is being processed 913 */ 914static INLINE void 915convert_color_type(SWspan *span, GLenum newType, GLuint output) 916{ 917 GLvoid *src, *dst; 918 919 if (output > 0 || span->array->ChanType == GL_FLOAT) { 920 src = span->array->attribs[FRAG_ATTRIB_COL0 + output]; 921 span->array->ChanType = GL_FLOAT; 922 } 923 else if (span->array->ChanType == GL_UNSIGNED_BYTE) { 924 src = span->array->rgba8; 925 } 926 else { 927 ASSERT(span->array->ChanType == GL_UNSIGNED_SHORT); 928 src = span->array->rgba16; 929 } 930 931 if (newType == GL_UNSIGNED_BYTE) { 932 dst = span->array->rgba8; 933 } 934 else if (newType == GL_UNSIGNED_SHORT) { 935 dst = span->array->rgba16; 936 } 937 else { 938 dst = span->array->attribs[FRAG_ATTRIB_COL0]; 939 } 940 941 _mesa_convert_colors(span->array->ChanType, src, 942 newType, dst, 943 span->end, span->array->mask); 944 945 span->array->ChanType = newType; 946 span->array->rgba = dst; 947} 948 949 950 951/** 952 * Apply fragment shader, fragment program or normal texturing to span. 953 */ 954static INLINE void 955shade_texture_span(GLcontext *ctx, SWspan *span) 956{ 957 GLbitfield inputsRead; 958 959 /* Determine which fragment attributes are actually needed */ 960 if (ctx->FragmentProgram._Current) { 961 inputsRead = ctx->FragmentProgram._Current->Base.InputsRead; 962 } 963 else { 964 /* XXX we could be a bit smarter about this */ 965 inputsRead = ~0; 966 } 967 968 if (ctx->FragmentProgram._Current || 969 ctx->ATIFragmentShader._Enabled) { 970 /* programmable shading */ 971 if (span->primitive == GL_BITMAP && span->array->ChanType != GL_FLOAT) { 972 convert_color_type(span, GL_FLOAT, 0); 973 } 974 if (span->primitive != GL_POINT || 975 (span->interpMask & SPAN_RGBA) || 976 ctx->Point.PointSprite) { 977 /* for single-pixel points, we populated the arrays already */ 978 interpolate_active_attribs(ctx, span, ~0); 979 } 980 span->array->ChanType = GL_FLOAT; 981 982 if (!(span->arrayMask & SPAN_Z)) 983 _swrast_span_interpolate_z (ctx, span); 984 985#if 0 986 if (inputsRead & FRAG_BIT_WPOS) 987#else 988 /* XXX always interpolate wpos so that DDX/DDY work */ 989#endif 990 interpolate_wpos(ctx, span); 991 992 /* Run fragment program/shader now */ 993 if (ctx->FragmentProgram._Current) { 994 _swrast_exec_fragment_program(ctx, span); 995 } 996 else { 997 ASSERT(ctx->ATIFragmentShader._Enabled); 998 _swrast_exec_fragment_shader(ctx, span); 999 } 1000 } 1001 else if (ctx->Texture._EnabledCoordUnits) { 1002 /* conventional texturing */ 1003 1004#if CHAN_BITS == 32 1005 if ((span->arrayAttribs & FRAG_BIT_COL0) == 0) { 1006 interpolate_int_colors(ctx, span); 1007 } 1008#else 1009 if (!(span->arrayMask & SPAN_RGBA)) 1010 interpolate_int_colors(ctx, span); 1011#endif 1012 if ((span->arrayAttribs & FRAG_BITS_TEX_ANY) == 0x0) 1013 interpolate_texcoords(ctx, span); 1014 1015 _swrast_texture_span(ctx, span); 1016 } 1017} 1018 1019 1020 1021/** 1022 * Apply all the per-fragment operations to a span. 1023 * This now includes texturing (_swrast_write_texture_span() is history). 1024 * This function may modify any of the array values in the span. 1025 * span->interpMask and span->arrayMask may be changed but will be restored 1026 * to their original values before returning. 1027 */ 1028void 1029_swrast_write_rgba_span( GLcontext *ctx, SWspan *span) 1030{ 1031 const SWcontext *swrast = SWRAST_CONTEXT(ctx); 1032 const GLuint *colorMask = (GLuint *) ctx->Color.ColorMask; 1033 const GLbitfield origInterpMask = span->interpMask; 1034 const GLbitfield origArrayMask = span->arrayMask; 1035 const GLbitfield origArrayAttribs = span->arrayAttribs; 1036 const GLenum origChanType = span->array->ChanType; 1037 void * const origRgba = span->array->rgba; 1038 const GLboolean shader = (ctx->FragmentProgram._Current 1039 || ctx->ATIFragmentShader._Enabled); 1040 const GLboolean shaderOrTexture = shader || ctx->Texture._EnabledCoordUnits; 1041 struct gl_framebuffer *fb = ctx->DrawBuffer; 1042 1043 /* 1044 printf("%s() interp 0x%x array 0x%x\n", __FUNCTION__, 1045 span->interpMask, span->arrayMask); 1046 */ 1047 1048 ASSERT(span->primitive == GL_POINT || 1049 span->primitive == GL_LINE || 1050 span->primitive == GL_POLYGON || 1051 span->primitive == GL_BITMAP); 1052 1053 /* Fragment write masks */ 1054 if (span->arrayMask & SPAN_MASK) { 1055 /* mask was initialized by caller, probably glBitmap */ 1056 span->writeAll = GL_FALSE; 1057 } 1058 else { 1059 memset(span->array->mask, 1, span->end); 1060 span->writeAll = GL_TRUE; 1061 } 1062 1063 /* Clip to window/scissor box */ 1064 if (!clip_span(ctx, span)) { 1065 return; 1066 } 1067 1068 ASSERT(span->end <= MAX_WIDTH); 1069 1070 /* Depth bounds test */ 1071 if (ctx->Depth.BoundsTest && fb->Visual.depthBits > 0) { 1072 if (!_swrast_depth_bounds_test(ctx, span)) { 1073 return; 1074 } 1075 } 1076 1077#ifdef DEBUG 1078 /* Make sure all fragments are within window bounds */ 1079 if (span->arrayMask & SPAN_XY) { 1080 /* array of pixel locations */ 1081 GLuint i; 1082 for (i = 0; i < span->end; i++) { 1083 if (span->array->mask[i]) { 1084 assert(span->array->x[i] >= fb->_Xmin); 1085 assert(span->array->x[i] < fb->_Xmax); 1086 assert(span->array->y[i] >= fb->_Ymin); 1087 assert(span->array->y[i] < fb->_Ymax); 1088 } 1089 } 1090 } 1091#endif 1092 1093 /* Polygon Stippling */ 1094 if (ctx->Polygon.StippleFlag && span->primitive == GL_POLYGON) { 1095 stipple_polygon_span(ctx, span); 1096 } 1097 1098 /* This is the normal place to compute the fragment color/Z 1099 * from texturing or shading. 1100 */ 1101 if (shaderOrTexture && !swrast->_DeferredTexture) { 1102 shade_texture_span(ctx, span); 1103 } 1104 1105 /* Do the alpha test */ 1106 if (ctx->Color.AlphaEnabled) { 1107 if (!_swrast_alpha_test(ctx, span)) { 1108 /* all fragments failed test */ 1109 goto end; 1110 } 1111 } 1112 1113 /* Stencil and Z testing */ 1114 if (ctx->Stencil._Enabled || ctx->Depth.Test) { 1115 if (!(span->arrayMask & SPAN_Z)) 1116 _swrast_span_interpolate_z(ctx, span); 1117 1118 if (ctx->Transform.DepthClamp) 1119 _swrast_depth_clamp_span(ctx, span); 1120 1121 if (ctx->Stencil._Enabled) { 1122 /* Combined Z/stencil tests */ 1123 if (!_swrast_stencil_and_ztest_span(ctx, span)) { 1124 /* all fragments failed test */ 1125 goto end; 1126 } 1127 } 1128 else if (fb->Visual.depthBits > 0) { 1129 /* Just regular depth testing */ 1130 ASSERT(ctx->Depth.Test); 1131 ASSERT(span->arrayMask & SPAN_Z); 1132 if (!_swrast_depth_test_span(ctx, span)) { 1133 /* all fragments failed test */ 1134 goto end; 1135 } 1136 } 1137 } 1138 1139 if (ctx->Query.CurrentOcclusionObject) { 1140 /* update count of 'passed' fragments */ 1141 struct gl_query_object *q = ctx->Query.CurrentOcclusionObject; 1142 GLuint i; 1143 for (i = 0; i < span->end; i++) 1144 q->Result += span->array->mask[i]; 1145 } 1146 1147 /* We had to wait until now to check for glColorMask(0,0,0,0) because of 1148 * the occlusion test. 1149 */ 1150 if (fb->_NumColorDrawBuffers == 1 && colorMask[0] == 0x0) { 1151 /* no colors to write */ 1152 goto end; 1153 } 1154 1155 /* If we were able to defer fragment color computation to now, there's 1156 * a good chance that many fragments will have already been killed by 1157 * Z/stencil testing. 1158 */ 1159 if (shaderOrTexture && swrast->_DeferredTexture) { 1160 shade_texture_span(ctx, span); 1161 } 1162 1163#if CHAN_BITS == 32 1164 if ((span->arrayAttribs & FRAG_BIT_COL0) == 0) { 1165 interpolate_active_attribs(ctx, span, FRAG_BIT_COL0); 1166 } 1167#else 1168 if ((span->arrayMask & SPAN_RGBA) == 0) { 1169 interpolate_int_colors(ctx, span); 1170 } 1171#endif 1172 1173 ASSERT(span->arrayMask & SPAN_RGBA); 1174 1175 if (span->primitive == GL_BITMAP || !swrast->SpecularVertexAdd) { 1176 /* Add primary and specular (diffuse + specular) colors */ 1177 if (!shader) { 1178 if (ctx->Fog.ColorSumEnabled || 1179 (ctx->Light.Enabled && 1180 ctx->Light.Model.ColorControl == GL_SEPARATE_SPECULAR_COLOR)) { 1181 add_specular(ctx, span); 1182 } 1183 } 1184 } 1185 1186 /* Fog */ 1187 if (swrast->_FogEnabled) { 1188 _swrast_fog_rgba_span(ctx, span); 1189 } 1190 1191 /* Antialias coverage application */ 1192 if (span->arrayMask & SPAN_COVERAGE) { 1193 apply_aa_coverage(span); 1194 } 1195 1196 /* Clamp color/alpha values over the range [0.0, 1.0] before storage */ 1197 if (ctx->Color.ClampFragmentColor == GL_TRUE && 1198 span->array->ChanType == GL_FLOAT) { 1199 clamp_colors(span); 1200 } 1201 1202 /* 1203 * Write to renderbuffers. 1204 * Depending on glDrawBuffer() state and the which color outputs are 1205 * written by the fragment shader, we may either replicate one color to 1206 * all renderbuffers or write a different color to each renderbuffer. 1207 * multiFragOutputs=TRUE for the later case. 1208 */ 1209 { 1210 const GLuint numBuffers = fb->_NumColorDrawBuffers; 1211 const struct gl_fragment_program *fp = ctx->FragmentProgram._Current; 1212 const GLboolean multiFragOutputs = 1213 (fp && fp->Base.OutputsWritten >= (1 << FRAG_RESULT_DATA0)); 1214 GLuint buf; 1215 1216 for (buf = 0; buf < numBuffers; buf++) { 1217 struct gl_renderbuffer *rb = fb->_ColorDrawBuffers[buf]; 1218 1219 /* color[fragOutput] will be written to buffer[buf] */ 1220 1221 if (rb) { 1222 GLchan rgbaSave[MAX_WIDTH][4]; 1223 const GLuint fragOutput = multiFragOutputs ? buf : 0; 1224 1225 if (rb->DataType != span->array->ChanType || fragOutput > 0) { 1226 convert_color_type(span, rb->DataType, fragOutput); 1227 } 1228 1229 if (!multiFragOutputs && numBuffers > 1) { 1230 /* save colors for second, third renderbuffer writes */ 1231 memcpy(rgbaSave, span->array->rgba, 1232 4 * span->end * sizeof(GLchan)); 1233 } 1234 1235 ASSERT(rb->_BaseFormat == GL_RGBA || rb->_BaseFormat == GL_RGB); 1236 1237 if (ctx->Color._LogicOpEnabled) { 1238 _swrast_logicop_rgba_span(ctx, rb, span); 1239 } 1240 else if ((ctx->Color.BlendEnabled >> buf) & 1) { 1241 _swrast_blend_span(ctx, rb, span); 1242 } 1243 1244 if (colorMask[buf] != 0xffffffff) { 1245 _swrast_mask_rgba_span(ctx, rb, span, buf); 1246 } 1247 1248 if (span->arrayMask & SPAN_XY) { 1249 /* array of pixel coords */ 1250 ASSERT(rb->PutValues); 1251 rb->PutValues(ctx, rb, span->end, 1252 span->array->x, span->array->y, 1253 span->array->rgba, span->array->mask); 1254 } 1255 else { 1256 /* horizontal run of pixels */ 1257 ASSERT(rb->PutRow); 1258 rb->PutRow(ctx, rb, span->end, span->x, span->y, 1259 span->array->rgba, 1260 span->writeAll ? NULL: span->array->mask); 1261 } 1262 1263 if (!multiFragOutputs && numBuffers > 1) { 1264 /* restore original span values */ 1265 memcpy(span->array->rgba, rgbaSave, 1266 4 * span->end * sizeof(GLchan)); 1267 } 1268 1269 } /* if rb */ 1270 } /* for buf */ 1271 } 1272 1273end: 1274 /* restore these values before returning */ 1275 span->interpMask = origInterpMask; 1276 span->arrayMask = origArrayMask; 1277 span->arrayAttribs = origArrayAttribs; 1278 span->array->ChanType = origChanType; 1279 span->array->rgba = origRgba; 1280} 1281 1282 1283/** 1284 * Read RGBA pixels from a renderbuffer. Clipping will be done to prevent 1285 * reading ouside the buffer's boundaries. 1286 * \param dstType datatype for returned colors 1287 * \param rgba the returned colors 1288 */ 1289void 1290_swrast_read_rgba_span( GLcontext *ctx, struct gl_renderbuffer *rb, 1291 GLuint n, GLint x, GLint y, GLenum dstType, 1292 GLvoid *rgba) 1293{ 1294 const GLint bufWidth = (GLint) rb->Width; 1295 const GLint bufHeight = (GLint) rb->Height; 1296 1297 if (y < 0 || y >= bufHeight || x + (GLint) n < 0 || x >= bufWidth) { 1298 /* completely above, below, or right */ 1299 /* XXX maybe leave rgba values undefined? */ 1300 memset(rgba, 0, 4 * n * sizeof(GLchan)); 1301 } 1302 else { 1303 GLint skip, length; 1304 if (x < 0) { 1305 /* left edge clipping */ 1306 skip = -x; 1307 length = (GLint) n - skip; 1308 if (length < 0) { 1309 /* completely left of window */ 1310 return; 1311 } 1312 if (length > bufWidth) { 1313 length = bufWidth; 1314 } 1315 } 1316 else if ((GLint) (x + n) > bufWidth) { 1317 /* right edge clipping */ 1318 skip = 0; 1319 length = bufWidth - x; 1320 if (length < 0) { 1321 /* completely to right of window */ 1322 return; 1323 } 1324 } 1325 else { 1326 /* no clipping */ 1327 skip = 0; 1328 length = (GLint) n; 1329 } 1330 1331 ASSERT(rb); 1332 ASSERT(rb->GetRow); 1333 ASSERT(rb->_BaseFormat == GL_RGB || rb->_BaseFormat == GL_RGBA); 1334 1335 if (rb->DataType == dstType) { 1336 rb->GetRow(ctx, rb, length, x + skip, y, 1337 (GLubyte *) rgba + skip * RGBA_PIXEL_SIZE(rb->DataType)); 1338 } 1339 else { 1340 GLuint temp[MAX_WIDTH * 4]; 1341 rb->GetRow(ctx, rb, length, x + skip, y, temp); 1342 _mesa_convert_colors(rb->DataType, temp, 1343 dstType, (GLubyte *) rgba + skip * RGBA_PIXEL_SIZE(dstType), 1344 length, NULL); 1345 } 1346 } 1347} 1348 1349 1350/** 1351 * Wrapper for gl_renderbuffer::GetValues() which does clipping to avoid 1352 * reading values outside the buffer bounds. 1353 * We can use this for reading any format/type of renderbuffer. 1354 * \param valueSize is the size in bytes of each value (pixel) put into the 1355 * values array. 1356 */ 1357void 1358_swrast_get_values(GLcontext *ctx, struct gl_renderbuffer *rb, 1359 GLuint count, const GLint x[], const GLint y[], 1360 void *values, GLuint valueSize) 1361{ 1362 GLuint i, inCount = 0, inStart = 0; 1363 1364 for (i = 0; i < count; i++) { 1365 if (x[i] >= 0 && y[i] >= 0 && 1366 x[i] < (GLint) rb->Width && y[i] < (GLint) rb->Height) { 1367 /* inside */ 1368 if (inCount == 0) 1369 inStart = i; 1370 inCount++; 1371 } 1372 else { 1373 if (inCount > 0) { 1374 /* read [inStart, inStart + inCount) */ 1375 rb->GetValues(ctx, rb, inCount, x + inStart, y + inStart, 1376 (GLubyte *) values + inStart * valueSize); 1377 inCount = 0; 1378 } 1379 } 1380 } 1381 if (inCount > 0) { 1382 /* read last values */ 1383 rb->GetValues(ctx, rb, inCount, x + inStart, y + inStart, 1384 (GLubyte *) values + inStart * valueSize); 1385 } 1386} 1387 1388 1389/** 1390 * Wrapper for gl_renderbuffer::PutRow() which does clipping. 1391 * \param valueSize size of each value (pixel) in bytes 1392 */ 1393void 1394_swrast_put_row(GLcontext *ctx, struct gl_renderbuffer *rb, 1395 GLuint count, GLint x, GLint y, 1396 const GLvoid *values, GLuint valueSize) 1397{ 1398 GLint skip = 0; 1399 1400 if (y < 0 || y >= (GLint) rb->Height) 1401 return; /* above or below */ 1402 1403 if (x + (GLint) count <= 0 || x >= (GLint) rb->Width) 1404 return; /* entirely left or right */ 1405 1406 if ((GLint) (x + count) > (GLint) rb->Width) { 1407 /* right clip */ 1408 GLint clip = x + count - rb->Width; 1409 count -= clip; 1410 } 1411 1412 if (x < 0) { 1413 /* left clip */ 1414 skip = -x; 1415 x = 0; 1416 count -= skip; 1417 } 1418 1419 rb->PutRow(ctx, rb, count, x, y, 1420 (const GLubyte *) values + skip * valueSize, NULL); 1421} 1422 1423 1424/** 1425 * Wrapper for gl_renderbuffer::GetRow() which does clipping. 1426 * \param valueSize size of each value (pixel) in bytes 1427 */ 1428void 1429_swrast_get_row(GLcontext *ctx, struct gl_renderbuffer *rb, 1430 GLuint count, GLint x, GLint y, 1431 GLvoid *values, GLuint valueSize) 1432{ 1433 GLint skip = 0; 1434 1435 if (y < 0 || y >= (GLint) rb->Height) 1436 return; /* above or below */ 1437 1438 if (x + (GLint) count <= 0 || x >= (GLint) rb->Width) 1439 return; /* entirely left or right */ 1440 1441 if (x + count > rb->Width) { 1442 /* right clip */ 1443 GLint clip = x + count - rb->Width; 1444 count -= clip; 1445 } 1446 1447 if (x < 0) { 1448 /* left clip */ 1449 skip = -x; 1450 x = 0; 1451 count -= skip; 1452 } 1453 1454 rb->GetRow(ctx, rb, count, x, y, (GLubyte *) values + skip * valueSize); 1455} 1456 1457 1458/** 1459 * Get RGBA pixels from the given renderbuffer. 1460 * Used by blending, logicop and masking functions. 1461 * \return pointer to the colors we read. 1462 */ 1463void * 1464_swrast_get_dest_rgba(GLcontext *ctx, struct gl_renderbuffer *rb, 1465 SWspan *span) 1466{ 1467 const GLuint pixelSize = RGBA_PIXEL_SIZE(span->array->ChanType); 1468 void *rbPixels; 1469 1470 /* Point rbPixels to a temporary space */ 1471 rbPixels = span->array->attribs[FRAG_ATTRIB_MAX - 1]; 1472 1473 /* Get destination values from renderbuffer */ 1474 if (span->arrayMask & SPAN_XY) { 1475 _swrast_get_values(ctx, rb, span->end, span->array->x, span->array->y, 1476 rbPixels, pixelSize); 1477 } 1478 else { 1479 _swrast_get_row(ctx, rb, span->end, span->x, span->y, 1480 rbPixels, pixelSize); 1481 } 1482 1483 return rbPixels; 1484} 1485