Debian Codegraph @

nx-X11/extras/Mesa/src/mesa/swrast/s_span.c

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