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