2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * \file swrast/s_span.c
28 * \brief Span processing functions used by all rasterization functions.
29 * This is where all the per-fragment tests are performed
33 #include "main/glheader.h"
34 #include "main/colormac.h"
35 #include "main/context.h"
36 #include "main/macros.h"
37 #include "main/imports.h"
40 #include "s_atifragshader.h"
43 #include "s_context.h"
47 #include "s_masking.h"
48 #include "s_fragprog.h"
50 #include "s_stencil.h"
51 #include "s_texcombine.h"
55 * Set default fragment attributes for the span using the
56 * current raster values. Used prior to glDraw/CopyPixels
60 _swrast_span_default_attribs(GLcontext
*ctx
, SWspan
*span
)
64 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
65 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16)
66 span
->z
= FloatToFixed(ctx
->Current
.RasterPos
[2] * depthMax
+ 0.5F
);
68 GLfloat tmpf
= ctx
->Current
.RasterPos
[2] * depthMax
;
69 tmpf
= MIN2(tmpf
, depthMax
);
70 span
->z
= (GLint
)tmpf
;
73 span
->interpMask
|= SPAN_Z
;
76 /* W (for perspective correction) */
77 span
->attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0;
78 span
->attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0;
79 span
->attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0;
81 /* primary color, or color index */
82 if (ctx
->Visual
.rgbMode
) {
84 UNCLAMPED_FLOAT_TO_CHAN(r
, ctx
->Current
.RasterColor
[0]);
85 UNCLAMPED_FLOAT_TO_CHAN(g
, ctx
->Current
.RasterColor
[1]);
86 UNCLAMPED_FLOAT_TO_CHAN(b
, ctx
->Current
.RasterColor
[2]);
87 UNCLAMPED_FLOAT_TO_CHAN(a
, ctx
->Current
.RasterColor
[3]);
88 #if CHAN_TYPE == GL_FLOAT
94 span
->red
= IntToFixed(r
);
95 span
->green
= IntToFixed(g
);
96 span
->blue
= IntToFixed(b
);
97 span
->alpha
= IntToFixed(a
);
103 span
->interpMask
|= SPAN_RGBA
;
105 COPY_4V(span
->attrStart
[FRAG_ATTRIB_COL0
], ctx
->Current
.RasterColor
);
106 ASSIGN_4V(span
->attrStepX
[FRAG_ATTRIB_COL0
], 0.0, 0.0, 0.0, 0.0);
107 ASSIGN_4V(span
->attrStepY
[FRAG_ATTRIB_COL0
], 0.0, 0.0, 0.0, 0.0);
110 span
->index
= FloatToFixed(ctx
->Current
.RasterIndex
);
112 span
->interpMask
|= SPAN_INDEX
;
115 /* Secondary color */
116 if (ctx
->Visual
.rgbMode
&& (ctx
->Light
.Enabled
|| ctx
->Fog
.ColorSumEnabled
))
118 COPY_4V(span
->attrStart
[FRAG_ATTRIB_COL1
], ctx
->Current
.RasterSecondaryColor
);
119 ASSIGN_4V(span
->attrStepX
[FRAG_ATTRIB_COL1
], 0.0, 0.0, 0.0, 0.0);
120 ASSIGN_4V(span
->attrStepY
[FRAG_ATTRIB_COL1
], 0.0, 0.0, 0.0, 0.0);
125 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
126 GLfloat fogVal
; /* a coord or a blend factor */
127 if (swrast
->_PreferPixelFog
) {
128 /* fog blend factors will be computed from fog coordinates per pixel */
129 fogVal
= ctx
->Current
.RasterDistance
;
132 /* fog blend factor should be computed from fogcoord now */
133 fogVal
= _swrast_z_to_fogfactor(ctx
, ctx
->Current
.RasterDistance
);
135 span
->attrStart
[FRAG_ATTRIB_FOGC
][0] = fogVal
;
136 span
->attrStepX
[FRAG_ATTRIB_FOGC
][0] = 0.0;
137 span
->attrStepY
[FRAG_ATTRIB_FOGC
][0] = 0.0;
143 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
144 const GLuint attr
= FRAG_ATTRIB_TEX0
+ i
;
145 const GLfloat
*tc
= ctx
->Current
.RasterTexCoords
[i
];
146 if (ctx
->FragmentProgram
._Current
|| ctx
->ATIFragmentShader
._Enabled
) {
147 COPY_4V(span
->attrStart
[attr
], tc
);
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;
157 ASSIGN_4V(span
->attrStart
[attr
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
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
);
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 FRAG_ATTRIB_WPOS[3]!
173 interpolate_active_attribs(GLcontext
*ctx
, SWspan
*span
, GLbitfield attrMask
)
175 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
178 * Don't overwrite existing array values, such as colors that may have
179 * been produced by glDraw/CopyPixels.
181 attrMask
&= ~span
->arrayAttribs
;
184 if (attrMask
& (1 << attr
)) {
185 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
186 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
187 const GLfloat dv0dx
= span
->attrStepX
[attr
][0];
188 const GLfloat dv1dx
= span
->attrStepX
[attr
][1];
189 const GLfloat dv2dx
= span
->attrStepX
[attr
][2];
190 const GLfloat dv3dx
= span
->attrStepX
[attr
][3];
191 GLfloat v0
= span
->attrStart
[attr
][0];
192 GLfloat v1
= span
->attrStart
[attr
][1];
193 GLfloat v2
= span
->attrStart
[attr
][2];
194 GLfloat v3
= span
->attrStart
[attr
][3];
196 for (k
= 0; k
< span
->end
; k
++) {
197 const GLfloat invW
= 1.0f
/ w
;
198 span
->array
->attribs
[attr
][k
][0] = v0
* invW
;
199 span
->array
->attribs
[attr
][k
][1] = v1
* invW
;
200 span
->array
->attribs
[attr
][k
][2] = v2
* invW
;
201 span
->array
->attribs
[attr
][k
][3] = v3
* invW
;
208 ASSERT((span
->arrayAttribs
& (1 << attr
)) == 0);
209 span
->arrayAttribs
|= (1 << attr
);
216 * Interpolate primary colors to fill in the span->array->rgba8 (or rgb16)
220 interpolate_int_colors(GLcontext
*ctx
, SWspan
*span
)
222 const GLuint n
= span
->end
;
226 ASSERT(!(span
->arrayMask
& SPAN_RGBA
));
229 switch (span
->array
->ChanType
) {
231 case GL_UNSIGNED_BYTE
:
233 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
234 if (span
->interpMask
& SPAN_FLAT
) {
236 color
[RCOMP
] = FixedToInt(span
->red
);
237 color
[GCOMP
] = FixedToInt(span
->green
);
238 color
[BCOMP
] = FixedToInt(span
->blue
);
239 color
[ACOMP
] = FixedToInt(span
->alpha
);
240 for (i
= 0; i
< n
; i
++) {
241 COPY_4UBV(rgba
[i
], color
);
245 GLfixed r
= span
->red
;
246 GLfixed g
= span
->green
;
247 GLfixed b
= span
->blue
;
248 GLfixed a
= span
->alpha
;
249 GLint dr
= span
->redStep
;
250 GLint dg
= span
->greenStep
;
251 GLint db
= span
->blueStep
;
252 GLint da
= span
->alphaStep
;
253 for (i
= 0; i
< n
; i
++) {
254 rgba
[i
][RCOMP
] = FixedToChan(r
);
255 rgba
[i
][GCOMP
] = FixedToChan(g
);
256 rgba
[i
][BCOMP
] = FixedToChan(b
);
257 rgba
[i
][ACOMP
] = FixedToChan(a
);
266 case GL_UNSIGNED_SHORT
:
268 GLushort (*rgba
)[4] = span
->array
->rgba16
;
269 if (span
->interpMask
& SPAN_FLAT
) {
271 color
[RCOMP
] = FixedToInt(span
->red
);
272 color
[GCOMP
] = FixedToInt(span
->green
);
273 color
[BCOMP
] = FixedToInt(span
->blue
);
274 color
[ACOMP
] = FixedToInt(span
->alpha
);
275 for (i
= 0; i
< n
; i
++) {
276 COPY_4V(rgba
[i
], color
);
280 GLushort (*rgba
)[4] = span
->array
->rgba16
;
282 GLint dr
, dg
, db
, da
;
288 dg
= span
->greenStep
;
290 da
= span
->alphaStep
;
291 for (i
= 0; i
< n
; i
++) {
292 rgba
[i
][RCOMP
] = FixedToChan(r
);
293 rgba
[i
][GCOMP
] = FixedToChan(g
);
294 rgba
[i
][BCOMP
] = FixedToChan(b
);
295 rgba
[i
][ACOMP
] = FixedToChan(a
);
306 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL0
);
309 _mesa_problem(NULL
, "bad datatype in interpolate_int_colors");
311 span
->arrayMask
|= SPAN_RGBA
;
316 * Populate the FRAG_ATTRIB_COL0 array.
319 interpolate_float_colors(SWspan
*span
)
321 GLfloat (*col0
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
322 const GLuint n
= span
->end
;
325 assert(!(span
->arrayAttribs
& FRAG_BIT_COL0
));
327 if (span
->arrayMask
& SPAN_RGBA
) {
328 /* convert array of int colors */
329 for (i
= 0; i
< n
; i
++) {
330 col0
[i
][0] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][0]);
331 col0
[i
][1] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][1]);
332 col0
[i
][2] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][2]);
333 col0
[i
][3] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][3]);
337 /* interpolate red/green/blue/alpha to get float colors */
338 ASSERT(span
->interpMask
& SPAN_RGBA
);
339 if (span
->interpMask
& SPAN_FLAT
) {
340 GLfloat r
= FixedToFloat(span
->red
);
341 GLfloat g
= FixedToFloat(span
->green
);
342 GLfloat b
= FixedToFloat(span
->blue
);
343 GLfloat a
= FixedToFloat(span
->alpha
);
344 for (i
= 0; i
< n
; i
++) {
345 ASSIGN_4V(col0
[i
], r
, g
, b
, a
);
349 GLfloat r
= FixedToFloat(span
->red
);
350 GLfloat g
= FixedToFloat(span
->green
);
351 GLfloat b
= FixedToFloat(span
->blue
);
352 GLfloat a
= FixedToFloat(span
->alpha
);
353 GLfloat dr
= FixedToFloat(span
->redStep
);
354 GLfloat dg
= FixedToFloat(span
->greenStep
);
355 GLfloat db
= FixedToFloat(span
->blueStep
);
356 GLfloat da
= FixedToFloat(span
->alphaStep
);
357 for (i
= 0; i
< n
; i
++) {
370 span
->arrayAttribs
|= FRAG_BIT_COL0
;
371 span
->array
->ChanType
= GL_FLOAT
;
376 /* Fill in the span.color.index array from the interpolation values */
378 interpolate_indexes(GLcontext
*ctx
, SWspan
*span
)
380 GLfixed index
= span
->index
;
381 const GLint indexStep
= span
->indexStep
;
382 const GLuint n
= span
->end
;
383 GLuint
*indexes
= span
->array
->index
;
387 ASSERT(!(span
->arrayMask
& SPAN_INDEX
));
389 if ((span
->interpMask
& SPAN_FLAT
) || (indexStep
== 0)) {
391 index
= FixedToInt(index
);
392 for (i
= 0; i
< n
; i
++) {
398 for (i
= 0; i
< n
; i
++) {
399 indexes
[i
] = FixedToInt(index
);
403 span
->arrayMask
|= SPAN_INDEX
;
404 span
->interpMask
&= ~SPAN_INDEX
;
409 * Fill in the span.zArray array from the span->z, zStep values.
412 _swrast_span_interpolate_z( const GLcontext
*ctx
, SWspan
*span
)
414 const GLuint n
= span
->end
;
417 ASSERT(!(span
->arrayMask
& SPAN_Z
));
419 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
420 GLfixed zval
= span
->z
;
421 GLuint
*z
= span
->array
->z
;
422 for (i
= 0; i
< n
; i
++) {
423 z
[i
] = FixedToInt(zval
);
428 /* Deep Z buffer, no fixed->int shift */
429 GLuint zval
= span
->z
;
430 GLuint
*z
= span
->array
->z
;
431 for (i
= 0; i
< n
; i
++) {
436 span
->interpMask
&= ~SPAN_Z
;
437 span
->arrayMask
|= SPAN_Z
;
442 * Compute mipmap LOD from partial derivatives.
443 * This the ideal solution, as given in the OpenGL spec.
447 compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
448 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
449 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
451 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
452 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
453 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
454 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
455 GLfloat x
= SQRTF(dudx
* dudx
+ dvdx
* dvdx
);
456 GLfloat y
= SQRTF(dudy
* dudy
+ dvdy
* dvdy
);
457 GLfloat rho
= MAX2(x
, y
);
458 GLfloat lambda
= LOG2(rho
);
465 * Compute mipmap LOD from partial derivatives.
466 * This is a faster approximation than above function.
469 _swrast_compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
470 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
471 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
473 GLfloat dsdx2
= (s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
;
474 GLfloat dtdx2
= (t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
;
475 GLfloat dsdy2
= (s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
;
476 GLfloat dtdy2
= (t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
;
477 GLfloat maxU
, maxV
, rho
, lambda
;
478 dsdx2
= FABSF(dsdx2
);
479 dsdy2
= FABSF(dsdy2
);
480 dtdx2
= FABSF(dtdx2
);
481 dtdy2
= FABSF(dtdy2
);
482 maxU
= MAX2(dsdx2
, dsdy2
) * texW
;
483 maxV
= MAX2(dtdx2
, dtdy2
) * texH
;
484 rho
= MAX2(maxU
, maxV
);
491 * Fill in the span.array->attrib[FRAG_ATTRIB_TEXn] arrays from the
492 * using the attrStart/Step values.
494 * This function only used during fixed-function fragment processing.
496 * Note: in the places where we divide by Q (or mult by invQ) we're
497 * really doing two things: perspective correction and texcoord
498 * projection. Remember, for texcoord (s,t,r,q) we need to index
499 * texels with (s/q, t/q, r/q).
502 interpolate_texcoords(GLcontext
*ctx
, SWspan
*span
)
505 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
508 /* XXX CoordUnits vs. ImageUnits */
509 for (u
= 0; u
< maxUnit
; u
++) {
510 if (ctx
->Texture
._EnabledCoordUnits
& (1 << u
)) {
511 const GLuint attr
= FRAG_ATTRIB_TEX0
+ u
;
512 const struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[u
]._Current
;
514 GLboolean needLambda
;
515 GLfloat (*texcoord
)[4] = span
->array
->attribs
[attr
];
516 GLfloat
*lambda
= span
->array
->lambda
[u
];
517 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
518 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
519 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
520 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
521 const GLfloat drdx
= span
->attrStepX
[attr
][2];
522 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
523 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
524 GLfloat s
= span
->attrStart
[attr
][0];
525 GLfloat t
= span
->attrStart
[attr
][1];
526 GLfloat r
= span
->attrStart
[attr
][2];
527 GLfloat q
= span
->attrStart
[attr
][3];
530 const struct gl_texture_image
*img
= obj
->Image
[0][obj
->BaseLevel
];
531 needLambda
= (obj
->MinFilter
!= obj
->MagFilter
)
532 || ctx
->FragmentProgram
._Current
;
533 texW
= img
->WidthScale
;
534 texH
= img
->HeightScale
;
537 /* using a fragment program */
540 needLambda
= GL_FALSE
;
545 if (ctx
->FragmentProgram
._Current
546 || ctx
->ATIFragmentShader
._Enabled
) {
547 /* do perspective correction but don't divide s, t, r by q */
548 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
549 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
550 for (i
= 0; i
< span
->end
; i
++) {
551 const GLfloat invW
= 1.0F
/ w
;
552 texcoord
[i
][0] = s
* invW
;
553 texcoord
[i
][1] = t
* invW
;
554 texcoord
[i
][2] = r
* invW
;
555 texcoord
[i
][3] = q
* invW
;
556 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
557 dqdx
, dqdy
, texW
, texH
,
567 for (i
= 0; i
< span
->end
; i
++) {
568 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
569 texcoord
[i
][0] = s
* invQ
;
570 texcoord
[i
][1] = t
* invQ
;
571 texcoord
[i
][2] = r
* invQ
;
573 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
574 dqdx
, dqdy
, texW
, texH
,
582 span
->arrayMask
|= SPAN_LAMBDA
;
586 if (ctx
->FragmentProgram
._Current
||
587 ctx
->ATIFragmentShader
._Enabled
) {
588 /* do perspective correction but don't divide s, t, r by q */
589 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
590 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
591 for (i
= 0; i
< span
->end
; i
++) {
592 const GLfloat invW
= 1.0F
/ w
;
593 texcoord
[i
][0] = s
* invW
;
594 texcoord
[i
][1] = t
* invW
;
595 texcoord
[i
][2] = r
* invW
;
596 texcoord
[i
][3] = q
* invW
;
605 else if (dqdx
== 0.0F
) {
606 /* Ortho projection or polygon's parallel to window X axis */
607 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
608 for (i
= 0; i
< span
->end
; i
++) {
609 texcoord
[i
][0] = s
* invQ
;
610 texcoord
[i
][1] = t
* invQ
;
611 texcoord
[i
][2] = r
* invQ
;
620 for (i
= 0; i
< span
->end
; i
++) {
621 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
622 texcoord
[i
][0] = s
* invQ
;
623 texcoord
[i
][1] = t
* invQ
;
624 texcoord
[i
][2] = r
* invQ
;
640 * Fill in the arrays->attribs[FRAG_ATTRIB_WPOS] array.
643 interpolate_wpos(GLcontext
*ctx
, SWspan
*span
)
645 GLfloat (*wpos
)[4] = span
->array
->attribs
[FRAG_ATTRIB_WPOS
];
647 const GLfloat zScale
= 1.0 / ctx
->DrawBuffer
->_DepthMaxF
;
650 if (span
->arrayMask
& SPAN_XY
) {
651 for (i
= 0; i
< span
->end
; i
++) {
652 wpos
[i
][0] = (GLfloat
) span
->array
->x
[i
];
653 wpos
[i
][1] = (GLfloat
) span
->array
->y
[i
];
657 for (i
= 0; i
< span
->end
; i
++) {
658 wpos
[i
][0] = (GLfloat
) span
->x
+ i
;
659 wpos
[i
][1] = (GLfloat
) span
->y
;
663 w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
664 dw
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
665 for (i
= 0; i
< span
->end
; i
++) {
666 wpos
[i
][2] = (GLfloat
) span
->array
->z
[i
] * zScale
;
674 * Apply the current polygon stipple pattern to a span of pixels.
677 stipple_polygon_span(GLcontext
*ctx
, SWspan
*span
)
679 GLubyte
*mask
= span
->array
->mask
;
681 ASSERT(ctx
->Polygon
.StippleFlag
);
683 if (span
->arrayMask
& SPAN_XY
) {
684 /* arrays of x/y pixel coords */
686 for (i
= 0; i
< span
->end
; i
++) {
687 const GLint col
= span
->array
->x
[i
] % 32;
688 const GLint row
= span
->array
->y
[i
] % 32;
689 const GLuint stipple
= ctx
->PolygonStipple
[row
];
690 if (((1 << col
) & stipple
) == 0) {
696 /* horizontal span of pixels */
697 const GLuint highBit
= 1 << 31;
698 const GLuint stipple
= ctx
->PolygonStipple
[span
->y
% 32];
699 GLuint i
, m
= highBit
>> (GLuint
) (span
->x
% 32);
700 for (i
= 0; i
< span
->end
; i
++) {
701 if ((m
& stipple
) == 0) {
710 span
->writeAll
= GL_FALSE
;
715 * Clip a pixel span to the current buffer/window boundaries:
716 * DrawBuffer->_Xmin, _Xmax, _Ymin, _Ymax. This will accomplish
717 * window clipping and scissoring.
718 * Return: GL_TRUE some pixels still visible
719 * GL_FALSE nothing visible
722 clip_span( GLcontext
*ctx
, SWspan
*span
)
724 const GLint xmin
= ctx
->DrawBuffer
->_Xmin
;
725 const GLint xmax
= ctx
->DrawBuffer
->_Xmax
;
726 const GLint ymin
= ctx
->DrawBuffer
->_Ymin
;
727 const GLint ymax
= ctx
->DrawBuffer
->_Ymax
;
729 if (span
->arrayMask
& SPAN_XY
) {
730 /* arrays of x/y pixel coords */
731 const GLint
*x
= span
->array
->x
;
732 const GLint
*y
= span
->array
->y
;
733 const GLint n
= span
->end
;
734 GLubyte
*mask
= span
->array
->mask
;
736 if (span
->arrayMask
& SPAN_MASK
) {
737 /* note: using & intead of && to reduce branches */
738 for (i
= 0; i
< n
; i
++) {
739 mask
[i
] &= (x
[i
] >= xmin
) & (x
[i
] < xmax
)
740 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
744 /* note: using & intead of && to reduce branches */
745 for (i
= 0; i
< n
; i
++) {
746 mask
[i
] = (x
[i
] >= xmin
) & (x
[i
] < xmax
)
747 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
750 return GL_TRUE
; /* some pixels visible */
753 /* horizontal span of pixels */
754 const GLint x
= span
->x
;
755 const GLint y
= span
->y
;
756 const GLint n
= span
->end
;
758 /* Trivial rejection tests */
759 if (y
< ymin
|| y
>= ymax
|| x
+ n
<= xmin
|| x
>= xmax
) {
761 return GL_FALSE
; /* all pixels clipped */
764 /* Clip to the left */
766 ASSERT(x
+ n
> xmin
);
767 span
->writeAll
= GL_FALSE
;
768 _mesa_bzero(span
->array
->mask
, (xmin
- x
) * sizeof(GLubyte
));
774 span
->end
= xmax
- x
;
777 return GL_TRUE
; /* some pixels visible */
783 * Apply all the per-fragment opertions to a span of color index fragments
784 * and write them to the enabled color drawbuffers.
785 * The 'span' parameter can be considered to be const. Note that
786 * span->interpMask and span->arrayMask may be changed but will be restored
787 * to their original values before returning.
790 _swrast_write_index_span( GLcontext
*ctx
, SWspan
*span
)
792 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
793 const GLbitfield origInterpMask
= span
->interpMask
;
794 const GLbitfield origArrayMask
= span
->arrayMask
;
795 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
797 ASSERT(span
->end
<= MAX_WIDTH
);
798 ASSERT(span
->primitive
== GL_POINT
|| span
->primitive
== GL_LINE
||
799 span
->primitive
== GL_POLYGON
|| span
->primitive
== GL_BITMAP
);
800 ASSERT((span
->interpMask
| span
->arrayMask
) & SPAN_INDEX
);
802 ASSERT((span->interpMask & span->arrayMask) == 0);
805 if (span
->arrayMask
& SPAN_MASK
) {
806 /* mask was initialized by caller, probably glBitmap */
807 span
->writeAll
= GL_FALSE
;
810 _mesa_memset(span
->array
->mask
, 1, span
->end
);
811 span
->writeAll
= GL_TRUE
;
815 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
816 if (!clip_span(ctx
, span
)) {
821 /* Depth bounds test */
822 if (ctx
->Depth
.BoundsTest
&& fb
->Visual
.depthBits
> 0) {
823 if (!_swrast_depth_bounds_test(ctx
, span
)) {
829 /* Make sure all fragments are within window bounds */
830 if (span
->arrayMask
& SPAN_XY
) {
832 for (i
= 0; i
< span
->end
; i
++) {
833 if (span
->array
->mask
[i
]) {
834 assert(span
->array
->x
[i
] >= fb
->_Xmin
);
835 assert(span
->array
->x
[i
] < fb
->_Xmax
);
836 assert(span
->array
->y
[i
] >= fb
->_Ymin
);
837 assert(span
->array
->y
[i
] < fb
->_Ymax
);
843 /* Polygon Stippling */
844 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
845 stipple_polygon_span(ctx
, span
);
848 /* Stencil and Z testing */
849 if (ctx
->Depth
.Test
|| ctx
->Stencil
.Enabled
) {
850 if (!(span
->arrayMask
& SPAN_Z
))
851 _swrast_span_interpolate_z(ctx
, span
);
853 if (ctx
->Stencil
.Enabled
) {
854 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
855 span
->arrayMask
= origArrayMask
;
860 ASSERT(ctx
->Depth
.Test
);
861 if (!_swrast_depth_test_span(ctx
, span
)) {
862 span
->interpMask
= origInterpMask
;
863 span
->arrayMask
= origArrayMask
;
869 #if FEATURE_ARB_occlusion_query
870 if (ctx
->Query
.CurrentOcclusionObject
) {
871 /* update count of 'passed' fragments */
872 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
874 for (i
= 0; i
< span
->end
; i
++)
875 q
->Result
+= span
->array
->mask
[i
];
879 /* we have to wait until after occlusion to do this test */
880 if (ctx
->Color
.IndexMask
== 0) {
881 /* write no pixels */
882 span
->arrayMask
= origArrayMask
;
886 /* Interpolate the color indexes if needed */
887 if (swrast
->_FogEnabled
||
888 ctx
->Color
.IndexLogicOpEnabled
||
889 ctx
->Color
.IndexMask
!= 0xffffffff ||
890 (span
->arrayMask
& SPAN_COVERAGE
)) {
891 if (!(span
->arrayMask
& SPAN_INDEX
) /*span->interpMask & SPAN_INDEX*/) {
892 interpolate_indexes(ctx
, span
);
897 if (swrast
->_FogEnabled
) {
898 _swrast_fog_ci_span(ctx
, span
);
901 /* Antialias coverage application */
902 if (span
->arrayMask
& SPAN_COVERAGE
) {
903 const GLfloat
*coverage
= span
->array
->coverage
;
904 GLuint
*index
= span
->array
->index
;
906 for (i
= 0; i
< span
->end
; i
++) {
907 ASSERT(coverage
[i
] < 16);
908 index
[i
] = (index
[i
] & ~0xf) | ((GLuint
) coverage
[i
]);
913 * Write to renderbuffers
916 const GLuint numBuffers
= fb
->_NumColorDrawBuffers
;
919 for (buf
= 0; buf
< numBuffers
; buf
++) {
920 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[buf
];
921 GLuint indexSave
[MAX_WIDTH
];
923 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
925 if (numBuffers
> 1) {
926 /* save indexes for second, third renderbuffer writes */
927 _mesa_memcpy(indexSave
, span
->array
->index
,
928 span
->end
* sizeof(indexSave
[0]));
931 if (ctx
->Color
.IndexLogicOpEnabled
) {
932 _swrast_logicop_ci_span(ctx
, rb
, span
);
935 if (ctx
->Color
.IndexMask
!= 0xffffffff) {
936 _swrast_mask_ci_span(ctx
, rb
, span
);
939 if (!(span
->arrayMask
& SPAN_INDEX
) && span
->indexStep
== 0) {
940 /* all fragments have same color index */
946 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
947 index8
= FixedToInt(span
->index
);
950 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
951 index16
= FixedToInt(span
->index
);
955 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
956 index32
= FixedToInt(span
->index
);
960 if (span
->arrayMask
& SPAN_XY
) {
961 rb
->PutMonoValues(ctx
, rb
, span
->end
, span
->array
->x
,
962 span
->array
->y
, value
, span
->array
->mask
);
965 rb
->PutMonoRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
966 value
, span
->array
->mask
);
970 /* each fragment is a different color */
971 GLubyte index8
[MAX_WIDTH
];
972 GLushort index16
[MAX_WIDTH
];
975 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
977 for (k
= 0; k
< span
->end
; k
++) {
978 index8
[k
] = (GLubyte
) span
->array
->index
[k
];
982 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
984 for (k
= 0; k
< span
->end
; k
++) {
985 index16
[k
] = (GLushort
) span
->array
->index
[k
];
990 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
991 values
= span
->array
->index
;
994 if (span
->arrayMask
& SPAN_XY
) {
995 rb
->PutValues(ctx
, rb
, span
->end
,
996 span
->array
->x
, span
->array
->y
,
997 values
, span
->array
->mask
);
1000 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1001 values
, span
->array
->mask
);
1005 if (buf
+ 1 < numBuffers
) {
1006 /* restore original span values */
1007 _mesa_memcpy(span
->array
->index
, indexSave
,
1008 span
->end
* sizeof(indexSave
[0]));
1013 span
->interpMask
= origInterpMask
;
1014 span
->arrayMask
= origArrayMask
;
1019 * Add specular colors to primary colors.
1020 * Only called during fixed-function operation.
1021 * Result is float color array (FRAG_ATTRIB_COL0).
1024 add_specular(GLcontext
*ctx
, SWspan
*span
)
1026 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1027 const GLubyte
*mask
= span
->array
->mask
;
1028 GLfloat (*col0
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1029 GLfloat (*col1
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL1
];
1032 ASSERT(!ctx
->FragmentProgram
._Current
);
1033 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1034 ASSERT(swrast
->_ActiveAttribMask
& FRAG_BIT_COL1
);
1036 if (span
->array
->ChanType
== GL_FLOAT
) {
1037 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1038 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL0
);
1042 /* need float colors */
1043 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1044 interpolate_float_colors(span
);
1048 if ((span
->arrayAttribs
& FRAG_BIT_COL1
) == 0) {
1049 /* XXX could avoid this and interpolate COL1 in the loop below */
1050 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL1
);
1053 ASSERT(span
->arrayAttribs
& FRAG_BIT_COL0
);
1054 ASSERT(span
->arrayAttribs
& FRAG_BIT_COL1
);
1056 for (i
= 0; i
< span
->end
; i
++) {
1058 col0
[i
][0] += col1
[i
][0];
1059 col0
[i
][1] += col1
[i
][1];
1060 col0
[i
][2] += col1
[i
][2];
1064 span
->array
->ChanType
= GL_FLOAT
;
1069 * Apply antialiasing coverage value to alpha values.
1072 apply_aa_coverage(SWspan
*span
)
1074 const GLfloat
*coverage
= span
->array
->coverage
;
1076 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1077 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
1078 for (i
= 0; i
< span
->end
; i
++) {
1079 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
1080 rgba
[i
][ACOMP
] = (GLubyte
) CLAMP(a
, 0.0, 255.0);
1081 ASSERT(coverage
[i
] >= 0.0);
1082 ASSERT(coverage
[i
] <= 1.0);
1085 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
1086 GLushort (*rgba
)[4] = span
->array
->rgba16
;
1087 for (i
= 0; i
< span
->end
; i
++) {
1088 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
1089 rgba
[i
][ACOMP
] = (GLushort
) CLAMP(a
, 0.0, 65535.0);
1093 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1094 for (i
= 0; i
< span
->end
; i
++) {
1095 rgba
[i
][ACOMP
] = rgba
[i
][ACOMP
] * coverage
[i
];
1103 * Clamp span's float colors to [0,1]
1106 clamp_colors(SWspan
*span
)
1108 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1110 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
1111 for (i
= 0; i
< span
->end
; i
++) {
1112 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0F
, 1.0F
);
1113 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0F
, 1.0F
);
1114 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0F
, 1.0F
);
1115 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0F
, 1.0F
);
1121 * Convert the span's color arrays to the given type.
1122 * The only way 'output' can be greater than zero is when we have a fragment
1123 * program that writes to gl_FragData[1] or higher.
1124 * \param output which fragment program color output is being processed
1127 convert_color_type(SWspan
*span
, GLenum newType
, GLuint output
)
1131 if (output
> 0 || span
->array
->ChanType
== GL_FLOAT
) {
1132 src
= span
->array
->attribs
[FRAG_ATTRIB_COL0
+ output
];
1133 span
->array
->ChanType
= GL_FLOAT
;
1135 else if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1136 src
= span
->array
->rgba8
;
1139 ASSERT(span
->array
->ChanType
== GL_UNSIGNED_SHORT
);
1140 src
= span
->array
->rgba16
;
1143 if (newType
== GL_UNSIGNED_BYTE
) {
1144 dst
= span
->array
->rgba8
;
1146 else if (newType
== GL_UNSIGNED_SHORT
) {
1147 dst
= span
->array
->rgba16
;
1150 dst
= span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1153 _mesa_convert_colors(span
->array
->ChanType
, src
,
1155 span
->end
, span
->array
->mask
);
1157 span
->array
->ChanType
= newType
;
1158 span
->array
->rgba
= dst
;
1164 * Apply fragment shader, fragment program or normal texturing to span.
1167 shade_texture_span(GLcontext
*ctx
, SWspan
*span
)
1169 GLbitfield inputsRead
;
1171 /* Determine which fragment attributes are actually needed */
1172 if (ctx
->FragmentProgram
._Current
) {
1173 inputsRead
= ctx
->FragmentProgram
._Current
->Base
.InputsRead
;
1176 /* XXX we could be a bit smarter about this */
1180 if (ctx
->FragmentProgram
._Current
||
1181 ctx
->ATIFragmentShader
._Enabled
) {
1182 /* programmable shading */
1183 if (span
->primitive
== GL_BITMAP
&& span
->array
->ChanType
!= GL_FLOAT
) {
1184 convert_color_type(span
, GL_FLOAT
, 0);
1186 if (span
->primitive
!= GL_POINT
||
1187 (span
->interpMask
& SPAN_RGBA
) ||
1188 ctx
->Point
.PointSprite
) {
1189 /* for single-pixel points, we populated the arrays already */
1190 interpolate_active_attribs(ctx
, span
, ~0);
1192 span
->array
->ChanType
= GL_FLOAT
;
1194 if (!(span
->arrayMask
& SPAN_Z
))
1195 _swrast_span_interpolate_z (ctx
, span
);
1198 if (inputsRead
& FRAG_BIT_WPOS
)
1200 /* XXX always interpolate wpos so that DDX/DDY work */
1202 interpolate_wpos(ctx
, span
);
1204 /* Run fragment program/shader now */
1205 if (ctx
->FragmentProgram
._Current
) {
1206 _swrast_exec_fragment_program(ctx
, span
);
1209 ASSERT(ctx
->ATIFragmentShader
._Enabled
);
1210 _swrast_exec_fragment_shader(ctx
, span
);
1213 else if (ctx
->Texture
._EnabledUnits
) {
1214 /* conventional texturing */
1217 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1218 interpolate_int_colors(ctx
, span
);
1221 if (!(span
->arrayMask
& SPAN_RGBA
))
1222 interpolate_int_colors(ctx
, span
);
1224 if ((span
->arrayAttribs
& FRAG_BITS_TEX_ANY
) == 0x0)
1225 interpolate_texcoords(ctx
, span
);
1227 _swrast_texture_span(ctx
, span
);
1234 * Apply all the per-fragment operations to a span.
1235 * This now includes texturing (_swrast_write_texture_span() is history).
1236 * This function may modify any of the array values in the span.
1237 * span->interpMask and span->arrayMask may be changed but will be restored
1238 * to their original values before returning.
1241 _swrast_write_rgba_span( GLcontext
*ctx
, SWspan
*span
)
1243 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1244 const GLuint colorMask
= *((GLuint
*) ctx
->Color
.ColorMask
);
1245 const GLbitfield origInterpMask
= span
->interpMask
;
1246 const GLbitfield origArrayMask
= span
->arrayMask
;
1247 const GLbitfield origArrayAttribs
= span
->arrayAttribs
;
1248 const GLenum origChanType
= span
->array
->ChanType
;
1249 void * const origRgba
= span
->array
->rgba
;
1250 const GLboolean shader
= (ctx
->FragmentProgram
._Current
1251 || ctx
->ATIFragmentShader
._Enabled
);
1252 const GLboolean shaderOrTexture
= shader
|| ctx
->Texture
._EnabledUnits
;
1253 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1256 printf("%s() interp 0x%x array 0x%x\n", __FUNCTION__,
1257 span->interpMask, span->arrayMask);
1260 ASSERT(span
->primitive
== GL_POINT
||
1261 span
->primitive
== GL_LINE
||
1262 span
->primitive
== GL_POLYGON
||
1263 span
->primitive
== GL_BITMAP
);
1264 ASSERT(span
->end
<= MAX_WIDTH
);
1266 /* Fragment write masks */
1267 if (span
->arrayMask
& SPAN_MASK
) {
1268 /* mask was initialized by caller, probably glBitmap */
1269 span
->writeAll
= GL_FALSE
;
1272 _mesa_memset(span
->array
->mask
, 1, span
->end
);
1273 span
->writeAll
= GL_TRUE
;
1276 /* Clip to window/scissor box */
1277 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
1278 if (!clip_span(ctx
, span
)) {
1284 /* Make sure all fragments are within window bounds */
1285 if (span
->arrayMask
& SPAN_XY
) {
1287 for (i
= 0; i
< span
->end
; i
++) {
1288 if (span
->array
->mask
[i
]) {
1289 assert(span
->array
->x
[i
] >= fb
->_Xmin
);
1290 assert(span
->array
->x
[i
] < fb
->_Xmax
);
1291 assert(span
->array
->y
[i
] >= fb
->_Ymin
);
1292 assert(span
->array
->y
[i
] < fb
->_Ymax
);
1298 /* Polygon Stippling */
1299 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
1300 stipple_polygon_span(ctx
, span
);
1303 /* This is the normal place to compute the fragment color/Z
1304 * from texturing or shading.
1306 if (shaderOrTexture
&& !swrast
->_DeferredTexture
) {
1307 shade_texture_span(ctx
, span
);
1310 /* Do the alpha test */
1311 if (ctx
->Color
.AlphaEnabled
) {
1312 if (!_swrast_alpha_test(ctx
, span
)) {
1317 /* Stencil and Z testing */
1318 if (ctx
->Stencil
.Enabled
|| ctx
->Depth
.Test
) {
1319 if (!(span
->arrayMask
& SPAN_Z
))
1320 _swrast_span_interpolate_z(ctx
, span
);
1322 if (ctx
->Stencil
.Enabled
&& fb
->Visual
.stencilBits
> 0) {
1323 /* Combined Z/stencil tests */
1324 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
1328 else if (fb
->Visual
.depthBits
> 0) {
1329 /* Just regular depth testing */
1330 ASSERT(ctx
->Depth
.Test
);
1331 ASSERT(span
->arrayMask
& SPAN_Z
);
1332 if (!_swrast_depth_test_span(ctx
, span
)) {
1338 #if FEATURE_ARB_occlusion_query
1339 if (ctx
->Query
.CurrentOcclusionObject
) {
1340 /* update count of 'passed' fragments */
1341 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
1343 for (i
= 0; i
< span
->end
; i
++)
1344 q
->Result
+= span
->array
->mask
[i
];
1348 /* We had to wait until now to check for glColorMask(0,0,0,0) because of
1349 * the occlusion test.
1351 if (colorMask
== 0x0) {
1355 /* If we were able to defer fragment color computation to now, there's
1356 * a good chance that many fragments will have already been killed by
1357 * Z/stencil testing.
1359 if (shaderOrTexture
&& swrast
->_DeferredTexture
) {
1360 shade_texture_span(ctx
, span
);
1364 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1365 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL0
);
1368 if ((span
->arrayMask
& SPAN_RGBA
) == 0) {
1369 interpolate_int_colors(ctx
, span
);
1373 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1376 /* Add base and specular colors */
1377 if (ctx
->Fog
.ColorSumEnabled
||
1378 (ctx
->Light
.Enabled
&&
1379 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)) {
1380 add_specular(ctx
, span
);
1385 if (swrast
->_FogEnabled
) {
1386 _swrast_fog_rgba_span(ctx
, span
);
1389 /* Antialias coverage application */
1390 if (span
->arrayMask
& SPAN_COVERAGE
) {
1391 apply_aa_coverage(span
);
1394 /* Clamp color/alpha values over the range [0.0, 1.0] before storage */
1395 if (ctx
->Color
.ClampFragmentColor
== GL_TRUE
&&
1396 span
->array
->ChanType
== GL_FLOAT
) {
1401 * Write to renderbuffers
1404 const GLuint numBuffers
= fb
->_NumColorDrawBuffers
;
1405 const GLboolean multiFragOutputs
= numBuffers
> 1;
1408 for (buf
= 0; buf
< numBuffers
; buf
++) {
1409 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[buf
];
1411 /* color[fragOutput] will be written to buffer[buf] */
1414 GLchan rgbaSave
[MAX_WIDTH
][4];
1415 const GLuint fragOutput
= multiFragOutputs
? buf
: 0;
1417 if (rb
->DataType
!= span
->array
->ChanType
|| fragOutput
> 0) {
1418 convert_color_type(span
, rb
->DataType
, fragOutput
);
1421 if (!multiFragOutputs
&& numBuffers
> 1) {
1422 /* save colors for second, third renderbuffer writes */
1423 _mesa_memcpy(rgbaSave
, span
->array
->rgba
,
1424 4 * span
->end
* sizeof(GLchan
));
1427 ASSERT(rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
);
1429 if (ctx
->Color
._LogicOpEnabled
) {
1430 _swrast_logicop_rgba_span(ctx
, rb
, span
);
1432 else if (ctx
->Color
.BlendEnabled
) {
1433 _swrast_blend_span(ctx
, rb
, span
);
1436 if (colorMask
!= 0xffffffff) {
1437 _swrast_mask_rgba_span(ctx
, rb
, span
);
1440 if (span
->arrayMask
& SPAN_XY
) {
1441 /* array of pixel coords */
1442 ASSERT(rb
->PutValues
);
1443 rb
->PutValues(ctx
, rb
, span
->end
,
1444 span
->array
->x
, span
->array
->y
,
1445 span
->array
->rgba
, span
->array
->mask
);
1448 /* horizontal run of pixels */
1450 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1452 span
->writeAll
? NULL
: span
->array
->mask
);
1455 if (!multiFragOutputs
&& numBuffers
> 1) {
1456 /* restore original span values */
1457 _mesa_memcpy(span
->array
->rgba
, rgbaSave
,
1458 4 * span
->end
* sizeof(GLchan
));
1466 /* restore these values before returning */
1467 span
->interpMask
= origInterpMask
;
1468 span
->arrayMask
= origArrayMask
;
1469 span
->arrayAttribs
= origArrayAttribs
;
1470 span
->array
->ChanType
= origChanType
;
1471 span
->array
->rgba
= origRgba
;
1476 * Read RGBA pixels from a renderbuffer. Clipping will be done to prevent
1477 * reading ouside the buffer's boundaries.
1478 * \param dstType datatype for returned colors
1479 * \param rgba the returned colors
1482 _swrast_read_rgba_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1483 GLuint n
, GLint x
, GLint y
, GLenum dstType
,
1486 const GLint bufWidth
= (GLint
) rb
->Width
;
1487 const GLint bufHeight
= (GLint
) rb
->Height
;
1489 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1490 /* completely above, below, or right */
1491 /* XXX maybe leave rgba values undefined? */
1492 _mesa_bzero(rgba
, 4 * n
* sizeof(GLchan
));
1497 /* left edge clipping */
1499 length
= (GLint
) n
- skip
;
1501 /* completely left of window */
1504 if (length
> bufWidth
) {
1508 else if ((GLint
) (x
+ n
) > bufWidth
) {
1509 /* right edge clipping */
1511 length
= bufWidth
- x
;
1513 /* completely to right of window */
1525 ASSERT(rb
->_BaseFormat
== GL_RGB
|| rb
->_BaseFormat
== GL_RGBA
);
1527 if (rb
->DataType
== dstType
) {
1528 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
,
1529 (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(rb
->DataType
));
1532 GLuint temp
[MAX_WIDTH
* 4];
1533 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, temp
);
1534 _mesa_convert_colors(rb
->DataType
, temp
,
1535 dstType
, (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(dstType
),
1543 * Read CI pixels from a renderbuffer. Clipping will be done to prevent
1544 * reading ouside the buffer's boundaries.
1547 _swrast_read_index_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1548 GLuint n
, GLint x
, GLint y
, GLuint index
[] )
1550 const GLint bufWidth
= (GLint
) rb
->Width
;
1551 const GLint bufHeight
= (GLint
) rb
->Height
;
1553 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1554 /* completely above, below, or right */
1555 _mesa_bzero(index
, n
* sizeof(GLuint
));
1560 /* left edge clipping */
1562 length
= (GLint
) n
- skip
;
1564 /* completely left of window */
1567 if (length
> bufWidth
) {
1571 else if ((GLint
) (x
+ n
) > bufWidth
) {
1572 /* right edge clipping */
1574 length
= bufWidth
- x
;
1576 /* completely to right of window */
1587 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
1589 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1590 GLubyte index8
[MAX_WIDTH
];
1592 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index8
);
1593 for (i
= 0; i
< length
; i
++)
1594 index
[skip
+ i
] = index8
[i
];
1596 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1597 GLushort index16
[MAX_WIDTH
];
1599 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index16
);
1600 for (i
= 0; i
< length
; i
++)
1601 index
[skip
+ i
] = index16
[i
];
1603 else if (rb
->DataType
== GL_UNSIGNED_INT
) {
1604 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index
+ skip
);
1611 * Wrapper for gl_renderbuffer::GetValues() which does clipping to avoid
1612 * reading values outside the buffer bounds.
1613 * We can use this for reading any format/type of renderbuffer.
1614 * \param valueSize is the size in bytes of each value (pixel) put into the
1618 _swrast_get_values(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1619 GLuint count
, const GLint x
[], const GLint y
[],
1620 void *values
, GLuint valueSize
)
1622 GLuint i
, inCount
= 0, inStart
= 0;
1624 for (i
= 0; i
< count
; i
++) {
1625 if (x
[i
] >= 0 && y
[i
] >= 0 &&
1626 x
[i
] < (GLint
) rb
->Width
&& y
[i
] < (GLint
) rb
->Height
) {
1634 /* read [inStart, inStart + inCount) */
1635 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1636 (GLubyte
*) values
+ inStart
* valueSize
);
1642 /* read last values */
1643 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1644 (GLubyte
*) values
+ inStart
* valueSize
);
1650 * Wrapper for gl_renderbuffer::PutRow() which does clipping.
1651 * \param valueSize size of each value (pixel) in bytes
1654 _swrast_put_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1655 GLuint count
, GLint x
, GLint y
,
1656 const GLvoid
*values
, GLuint valueSize
)
1660 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1661 return; /* above or below */
1663 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1664 return; /* entirely left or right */
1666 if ((GLint
) (x
+ count
) > (GLint
) rb
->Width
) {
1668 GLint clip
= x
+ count
- rb
->Width
;
1679 rb
->PutRow(ctx
, rb
, count
, x
, y
,
1680 (const GLubyte
*) values
+ skip
* valueSize
, NULL
);
1685 * Wrapper for gl_renderbuffer::GetRow() which does clipping.
1686 * \param valueSize size of each value (pixel) in bytes
1689 _swrast_get_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1690 GLuint count
, GLint x
, GLint y
,
1691 GLvoid
*values
, GLuint valueSize
)
1695 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1696 return; /* above or below */
1698 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1699 return; /* entirely left or right */
1701 if (x
+ count
> rb
->Width
) {
1703 GLint clip
= x
+ count
- rb
->Width
;
1714 rb
->GetRow(ctx
, rb
, count
, x
, y
, (GLubyte
*) values
+ skip
* valueSize
);
1719 * Get RGBA pixels from the given renderbuffer. Put the pixel colors into
1720 * the span's specular color arrays. The specular color arrays should no
1721 * longer be needed by time this function is called.
1722 * Used by blending, logicop and masking functions.
1723 * \return pointer to the colors we read.
1726 _swrast_get_dest_rgba(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1729 const GLuint pixelSize
= RGBA_PIXEL_SIZE(span
->array
->ChanType
);
1733 * Point rbPixels to a temporary space (use specular color arrays).
1735 rbPixels
= span
->array
->attribs
[FRAG_ATTRIB_COL1
];
1737 /* Get destination values from renderbuffer */
1738 if (span
->arrayMask
& SPAN_XY
) {
1739 _swrast_get_values(ctx
, rb
, span
->end
, span
->array
->x
, span
->array
->y
,
1740 rbPixels
, pixelSize
);
1743 _swrast_get_row(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1744 rbPixels
, pixelSize
);