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