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