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