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