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