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