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