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