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
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 span
->z
= (GLint
) (ctx
->Current
.RasterPos
[2] * depthMax
+ 0.5F
);
70 span
->interpMask
|= SPAN_Z
;
73 /* W (for perspective correction) */
74 span
->attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0;
75 span
->attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0;
76 span
->attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0;
78 /* primary color, or color index */
79 if (ctx
->Visual
.rgbMode
) {
81 UNCLAMPED_FLOAT_TO_CHAN(r
, ctx
->Current
.RasterColor
[0]);
82 UNCLAMPED_FLOAT_TO_CHAN(g
, ctx
->Current
.RasterColor
[1]);
83 UNCLAMPED_FLOAT_TO_CHAN(b
, ctx
->Current
.RasterColor
[2]);
84 UNCLAMPED_FLOAT_TO_CHAN(a
, ctx
->Current
.RasterColor
[3]);
85 #if CHAN_TYPE == GL_FLOAT
91 span
->red
= IntToFixed(r
);
92 span
->green
= IntToFixed(g
);
93 span
->blue
= IntToFixed(b
);
94 span
->alpha
= IntToFixed(a
);
100 span
->interpMask
|= SPAN_RGBA
;
102 COPY_4V(span
->attrStart
[FRAG_ATTRIB_COL0
], ctx
->Current
.RasterColor
);
103 ASSIGN_4V(span
->attrStepX
[FRAG_ATTRIB_COL0
], 0.0, 0.0, 0.0, 0.0);
104 ASSIGN_4V(span
->attrStepY
[FRAG_ATTRIB_COL0
], 0.0, 0.0, 0.0, 0.0);
107 span
->index
= FloatToFixed(ctx
->Current
.RasterIndex
);
109 span
->interpMask
|= SPAN_INDEX
;
112 /* Secondary color */
113 if (ctx
->Visual
.rgbMode
&& (ctx
->Light
.Enabled
|| ctx
->Fog
.ColorSumEnabled
))
115 COPY_4V(span
->attrStart
[FRAG_ATTRIB_COL1
], ctx
->Current
.RasterSecondaryColor
);
116 ASSIGN_4V(span
->attrStepX
[FRAG_ATTRIB_COL1
], 0.0, 0.0, 0.0, 0.0);
117 ASSIGN_4V(span
->attrStepY
[FRAG_ATTRIB_COL1
], 0.0, 0.0, 0.0, 0.0);
122 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
123 GLfloat fogVal
; /* a coord or a blend factor */
124 if (swrast
->_PreferPixelFog
) {
125 /* fog blend factors will be computed from fog coordinates per pixel */
126 fogVal
= ctx
->Current
.RasterDistance
;
129 /* fog blend factor should be computed from fogcoord now */
130 fogVal
= _swrast_z_to_fogfactor(ctx
, ctx
->Current
.RasterDistance
);
132 span
->attrStart
[FRAG_ATTRIB_FOGC
][0] = fogVal
;
133 span
->attrStepX
[FRAG_ATTRIB_FOGC
][0] = 0.0;
134 span
->attrStepY
[FRAG_ATTRIB_FOGC
][0] = 0.0;
140 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
141 const GLuint attr
= FRAG_ATTRIB_TEX0
+ i
;
142 const GLfloat
*tc
= ctx
->Current
.RasterTexCoords
[i
];
143 if (ctx
->FragmentProgram
._Current
|| ctx
->ATIFragmentShader
._Enabled
) {
144 COPY_4V(span
->attrStart
[attr
], tc
);
146 else if (tc
[3] > 0.0F
) {
147 /* use (s/q, t/q, r/q, 1) */
148 span
->attrStart
[attr
][0] = tc
[0] / tc
[3];
149 span
->attrStart
[attr
][1] = tc
[1] / tc
[3];
150 span
->attrStart
[attr
][2] = tc
[2] / tc
[3];
151 span
->attrStart
[attr
][3] = 1.0;
154 ASSIGN_4V(span
->attrStart
[attr
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
156 ASSIGN_4V(span
->attrStepX
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
157 ASSIGN_4V(span
->attrStepY
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
164 * Interpolate the active attributes (and'd with attrMask) to
165 * fill in span->array->attribs[].
166 * Perspective correction will be done. The point/line/triangle function
167 * should have computed attrStart/Step values for FRAG_ATTRIB_WPOS[3]!
170 interpolate_active_attribs(GLcontext
*ctx
, SWspan
*span
, GLbitfield attrMask
)
172 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
174 /* for glDraw/CopyPixels() we may have turned off some bits in
175 * the _ActiveAttribMask - be sure to obey that mask now.
177 attrMask
&= swrast
->_ActiveAttribMask
;
180 if (attrMask
& (1 << attr
)) {
181 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
182 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
183 const GLfloat dv0dx
= span
->attrStepX
[attr
][0];
184 const GLfloat dv1dx
= span
->attrStepX
[attr
][1];
185 const GLfloat dv2dx
= span
->attrStepX
[attr
][2];
186 const GLfloat dv3dx
= span
->attrStepX
[attr
][3];
187 GLfloat v0
= span
->attrStart
[attr
][0];
188 GLfloat v1
= span
->attrStart
[attr
][1];
189 GLfloat v2
= span
->attrStart
[attr
][2];
190 GLfloat v3
= span
->attrStart
[attr
][3];
192 for (k
= 0; k
< span
->end
; k
++) {
193 const GLfloat invW
= 1.0f
/ w
;
194 span
->array
->attribs
[attr
][k
][0] = v0
* invW
;
195 span
->array
->attribs
[attr
][k
][1] = v1
* invW
;
196 span
->array
->attribs
[attr
][k
][2] = v2
* invW
;
197 span
->array
->attribs
[attr
][k
][3] = v3
* invW
;
204 span
->arrayAttribs
|= (1 << attr
);
211 * Interpolate primary colors to fill in the span->array->rgba8 (or rgb16)
215 interpolate_int_colors(GLcontext
*ctx
, SWspan
*span
)
217 const GLuint n
= span
->end
;
221 ASSERT(!(span
->arrayMask
& SPAN_RGBA
));
224 switch (span
->array
->ChanType
) {
226 case GL_UNSIGNED_BYTE
:
228 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
229 if (span
->interpMask
& SPAN_FLAT
) {
231 color
[RCOMP
] = FixedToInt(span
->red
);
232 color
[GCOMP
] = FixedToInt(span
->green
);
233 color
[BCOMP
] = FixedToInt(span
->blue
);
234 color
[ACOMP
] = FixedToInt(span
->alpha
);
235 for (i
= 0; i
< n
; i
++) {
236 COPY_4UBV(rgba
[i
], color
);
240 GLfixed r
= span
->red
;
241 GLfixed g
= span
->green
;
242 GLfixed b
= span
->blue
;
243 GLfixed a
= span
->alpha
;
244 GLint dr
= span
->redStep
;
245 GLint dg
= span
->greenStep
;
246 GLint db
= span
->blueStep
;
247 GLint da
= span
->alphaStep
;
248 for (i
= 0; i
< n
; i
++) {
249 rgba
[i
][RCOMP
] = FixedToChan(r
);
250 rgba
[i
][GCOMP
] = FixedToChan(g
);
251 rgba
[i
][BCOMP
] = FixedToChan(b
);
252 rgba
[i
][ACOMP
] = FixedToChan(a
);
261 case GL_UNSIGNED_SHORT
:
263 GLushort (*rgba
)[4] = span
->array
->rgba16
;
264 if (span
->interpMask
& SPAN_FLAT
) {
266 color
[RCOMP
] = FixedToInt(span
->red
);
267 color
[GCOMP
] = FixedToInt(span
->green
);
268 color
[BCOMP
] = FixedToInt(span
->blue
);
269 color
[ACOMP
] = FixedToInt(span
->alpha
);
270 for (i
= 0; i
< n
; i
++) {
271 COPY_4V(rgba
[i
], color
);
275 GLushort (*rgba
)[4] = span
->array
->rgba16
;
277 GLint dr
, dg
, db
, da
;
283 dg
= span
->greenStep
;
285 da
= span
->alphaStep
;
286 for (i
= 0; i
< n
; i
++) {
287 rgba
[i
][RCOMP
] = FixedToChan(r
);
288 rgba
[i
][GCOMP
] = FixedToChan(g
);
289 rgba
[i
][BCOMP
] = FixedToChan(b
);
290 rgba
[i
][ACOMP
] = FixedToChan(a
);
301 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL0
);
304 _mesa_problem(NULL
, "bad datatype in interpolate_int_colors");
306 span
->arrayMask
|= SPAN_RGBA
;
311 * Populate the FRAG_ATTRIB_COL0 array.
314 interpolate_float_colors(SWspan
*span
)
316 GLfloat (*col0
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
317 const GLuint n
= span
->end
;
320 assert(!(span
->arrayAttribs
& FRAG_BIT_COL0
));
322 if (span
->arrayMask
& SPAN_RGBA
) {
323 /* convert array of int colors */
324 for (i
= 0; i
< n
; i
++) {
325 col0
[i
][0] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][0]);
326 col0
[i
][1] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][1]);
327 col0
[i
][2] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][2]);
328 col0
[i
][3] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][3]);
332 /* interpolate red/green/blue/alpha to get float colors */
333 ASSERT(span
->interpMask
& SPAN_RGBA
);
334 if (span
->interpMask
& SPAN_FLAT
) {
335 GLfloat r
= FixedToFloat(span
->red
);
336 GLfloat g
= FixedToFloat(span
->green
);
337 GLfloat b
= FixedToFloat(span
->blue
);
338 GLfloat a
= FixedToFloat(span
->alpha
);
339 for (i
= 0; i
< n
; i
++) {
340 ASSIGN_4V(col0
[i
], r
, g
, b
, a
);
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 GLfloat dr
= FixedToFloat(span
->redStep
);
349 GLfloat dg
= FixedToFloat(span
->greenStep
);
350 GLfloat db
= FixedToFloat(span
->blueStep
);
351 GLfloat da
= FixedToFloat(span
->alphaStep
);
352 for (i
= 0; i
< n
; i
++) {
365 span
->arrayAttribs
|= FRAG_BIT_COL0
;
366 span
->array
->ChanType
= GL_FLOAT
;
371 /* Fill in the span.color.index array from the interpolation values */
373 interpolate_indexes(GLcontext
*ctx
, SWspan
*span
)
375 GLfixed index
= span
->index
;
376 const GLint indexStep
= span
->indexStep
;
377 const GLuint n
= span
->end
;
378 GLuint
*indexes
= span
->array
->index
;
382 ASSERT(!(span
->arrayMask
& SPAN_INDEX
));
384 if ((span
->interpMask
& SPAN_FLAT
) || (indexStep
== 0)) {
386 index
= FixedToInt(index
);
387 for (i
= 0; i
< n
; i
++) {
393 for (i
= 0; i
< n
; i
++) {
394 indexes
[i
] = FixedToInt(index
);
398 span
->arrayMask
|= SPAN_INDEX
;
399 span
->interpMask
&= ~SPAN_INDEX
;
404 * Fill in the span.zArray array from the span->z, zStep values.
407 _swrast_span_interpolate_z( const GLcontext
*ctx
, SWspan
*span
)
409 const GLuint n
= span
->end
;
412 ASSERT(!(span
->arrayMask
& SPAN_Z
));
414 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
415 GLfixed zval
= span
->z
;
416 GLuint
*z
= span
->array
->z
;
417 for (i
= 0; i
< n
; i
++) {
418 z
[i
] = FixedToInt(zval
);
423 /* Deep Z buffer, no fixed->int shift */
424 GLuint zval
= span
->z
;
425 GLuint
*z
= span
->array
->z
;
426 for (i
= 0; i
< n
; i
++) {
431 span
->interpMask
&= ~SPAN_Z
;
432 span
->arrayMask
|= SPAN_Z
;
437 * Compute mipmap LOD from partial derivatives.
438 * This the ideal solution, as given in the OpenGL spec.
442 compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
443 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
444 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
446 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
447 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
448 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
449 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
450 GLfloat x
= SQRTF(dudx
* dudx
+ dvdx
* dvdx
);
451 GLfloat y
= SQRTF(dudy
* dudy
+ dvdy
* dvdy
);
452 GLfloat rho
= MAX2(x
, y
);
453 GLfloat lambda
= LOG2(rho
);
460 * Compute mipmap LOD from partial derivatives.
461 * This is a faster approximation than above function.
464 _swrast_compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
465 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
466 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
468 GLfloat dsdx2
= (s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
;
469 GLfloat dtdx2
= (t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
;
470 GLfloat dsdy2
= (s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
;
471 GLfloat dtdy2
= (t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
;
472 GLfloat maxU
, maxV
, rho
, lambda
;
473 dsdx2
= FABSF(dsdx2
);
474 dsdy2
= FABSF(dsdy2
);
475 dtdx2
= FABSF(dtdx2
);
476 dtdy2
= FABSF(dtdy2
);
477 maxU
= MAX2(dsdx2
, dsdy2
) * texW
;
478 maxV
= MAX2(dtdx2
, dtdy2
) * texH
;
479 rho
= MAX2(maxU
, maxV
);
486 * Fill in the span.array->attrib[FRAG_ATTRIB_TEXn] arrays from the
487 * using the attrStart/Step values.
489 * This function only used during fixed-function fragment processing.
491 * Note: in the places where we divide by Q (or mult by invQ) we're
492 * really doing two things: perspective correction and texcoord
493 * projection. Remember, for texcoord (s,t,r,q) we need to index
494 * texels with (s/q, t/q, r/q).
497 interpolate_texcoords(GLcontext
*ctx
, SWspan
*span
)
500 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
503 /* XXX CoordUnits vs. ImageUnits */
504 for (u
= 0; u
< maxUnit
; u
++) {
505 if (ctx
->Texture
._EnabledCoordUnits
& (1 << u
)) {
506 const GLuint attr
= FRAG_ATTRIB_TEX0
+ u
;
507 const struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[u
]._Current
;
509 GLboolean needLambda
;
510 GLfloat (*texcoord
)[4] = span
->array
->attribs
[attr
];
511 GLfloat
*lambda
= span
->array
->lambda
[u
];
512 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
513 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
514 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
515 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
516 const GLfloat drdx
= span
->attrStepX
[attr
][2];
517 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
518 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
519 GLfloat s
= span
->attrStart
[attr
][0];
520 GLfloat t
= span
->attrStart
[attr
][1];
521 GLfloat r
= span
->attrStart
[attr
][2];
522 GLfloat q
= span
->attrStart
[attr
][3];
525 const struct gl_texture_image
*img
= obj
->Image
[0][obj
->BaseLevel
];
526 needLambda
= (obj
->MinFilter
!= obj
->MagFilter
)
527 || ctx
->FragmentProgram
._Current
;
528 texW
= img
->WidthScale
;
529 texH
= img
->HeightScale
;
532 /* using a fragment program */
535 needLambda
= GL_FALSE
;
540 if (ctx
->FragmentProgram
._Current
541 || ctx
->ATIFragmentShader
._Enabled
) {
542 /* do perspective correction but don't divide s, t, r by q */
543 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
544 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
545 for (i
= 0; i
< span
->end
; i
++) {
546 const GLfloat invW
= 1.0F
/ w
;
547 texcoord
[i
][0] = s
* invW
;
548 texcoord
[i
][1] = t
* invW
;
549 texcoord
[i
][2] = r
* invW
;
550 texcoord
[i
][3] = q
* invW
;
551 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
552 dqdx
, dqdy
, texW
, texH
,
562 for (i
= 0; i
< span
->end
; i
++) {
563 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
564 texcoord
[i
][0] = s
* invQ
;
565 texcoord
[i
][1] = t
* invQ
;
566 texcoord
[i
][2] = r
* invQ
;
568 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
569 dqdx
, dqdy
, texW
, texH
,
577 span
->arrayMask
|= SPAN_LAMBDA
;
581 if (ctx
->FragmentProgram
._Current
||
582 ctx
->ATIFragmentShader
._Enabled
) {
583 /* do perspective correction but don't divide s, t, r by q */
584 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
585 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
586 for (i
= 0; i
< span
->end
; i
++) {
587 const GLfloat invW
= 1.0F
/ w
;
588 texcoord
[i
][0] = s
* invW
;
589 texcoord
[i
][1] = t
* invW
;
590 texcoord
[i
][2] = r
* invW
;
591 texcoord
[i
][3] = q
* invW
;
600 else if (dqdx
== 0.0F
) {
601 /* Ortho projection or polygon's parallel to window X axis */
602 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
603 for (i
= 0; i
< span
->end
; i
++) {
604 texcoord
[i
][0] = s
* invQ
;
605 texcoord
[i
][1] = t
* invQ
;
606 texcoord
[i
][2] = r
* invQ
;
615 for (i
= 0; i
< span
->end
; i
++) {
616 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
617 texcoord
[i
][0] = s
* invQ
;
618 texcoord
[i
][1] = t
* invQ
;
619 texcoord
[i
][2] = r
* invQ
;
635 * Fill in the arrays->attribs[FRAG_ATTRIB_WPOS] array.
638 interpolate_wpos(GLcontext
*ctx
, SWspan
*span
)
640 GLfloat (*wpos
)[4] = span
->array
->attribs
[FRAG_ATTRIB_WPOS
];
642 const GLfloat zScale
= 1.0 / ctx
->DrawBuffer
->_DepthMaxF
;
645 if (span
->arrayMask
& SPAN_XY
) {
646 for (i
= 0; i
< span
->end
; i
++) {
647 wpos
[i
][0] = (GLfloat
) span
->array
->x
[i
];
648 wpos
[i
][1] = (GLfloat
) span
->array
->y
[i
];
652 for (i
= 0; i
< span
->end
; i
++) {
653 wpos
[i
][0] = (GLfloat
) span
->x
+ i
;
654 wpos
[i
][1] = (GLfloat
) span
->y
;
658 w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
659 dw
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
660 for (i
= 0; i
< span
->end
; i
++) {
661 wpos
[i
][2] = (GLfloat
) span
->array
->z
[i
] * zScale
;
669 * Apply the current polygon stipple pattern to a span of pixels.
672 stipple_polygon_span(GLcontext
*ctx
, SWspan
*span
)
674 GLubyte
*mask
= span
->array
->mask
;
676 ASSERT(ctx
->Polygon
.StippleFlag
);
678 if (span
->arrayMask
& SPAN_XY
) {
679 /* arrays of x/y pixel coords */
681 for (i
= 0; i
< span
->end
; i
++) {
682 const GLint col
= span
->array
->x
[i
] % 32;
683 const GLint row
= span
->array
->y
[i
] % 32;
684 const GLuint stipple
= ctx
->PolygonStipple
[row
];
685 if (((1 << col
) & stipple
) == 0) {
691 /* horizontal span of pixels */
692 const GLuint highBit
= 1 << 31;
693 const GLuint stipple
= ctx
->PolygonStipple
[span
->y
% 32];
694 GLuint i
, m
= highBit
>> (GLuint
) (span
->x
% 32);
695 for (i
= 0; i
< span
->end
; i
++) {
696 if ((m
& stipple
) == 0) {
705 span
->writeAll
= GL_FALSE
;
710 * Clip a pixel span to the current buffer/window boundaries:
711 * DrawBuffer->_Xmin, _Xmax, _Ymin, _Ymax. This will accomplish
712 * window clipping and scissoring.
713 * Return: GL_TRUE some pixels still visible
714 * GL_FALSE nothing visible
717 clip_span( GLcontext
*ctx
, SWspan
*span
)
719 const GLint xmin
= ctx
->DrawBuffer
->_Xmin
;
720 const GLint xmax
= ctx
->DrawBuffer
->_Xmax
;
721 const GLint ymin
= ctx
->DrawBuffer
->_Ymin
;
722 const GLint ymax
= ctx
->DrawBuffer
->_Ymax
;
724 if (span
->arrayMask
& SPAN_XY
) {
725 /* arrays of x/y pixel coords */
726 const GLint
*x
= span
->array
->x
;
727 const GLint
*y
= span
->array
->y
;
728 const GLint n
= span
->end
;
729 GLubyte
*mask
= span
->array
->mask
;
731 if (span
->arrayMask
& SPAN_MASK
) {
732 /* note: using & intead of && to reduce branches */
733 for (i
= 0; i
< n
; i
++) {
734 mask
[i
] &= (x
[i
] >= xmin
) & (x
[i
] < xmax
)
735 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
739 /* note: using & intead of && to reduce branches */
740 for (i
= 0; i
< n
; i
++) {
741 mask
[i
] = (x
[i
] >= xmin
) & (x
[i
] < xmax
)
742 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
745 return GL_TRUE
; /* some pixels visible */
748 /* horizontal span of pixels */
749 const GLint x
= span
->x
;
750 const GLint y
= span
->y
;
751 const GLint n
= span
->end
;
753 /* Trivial rejection tests */
754 if (y
< ymin
|| y
>= ymax
|| x
+ n
<= xmin
|| x
>= xmax
) {
756 return GL_FALSE
; /* all pixels clipped */
759 /* Clip to the left */
761 ASSERT(x
+ n
> xmin
);
762 span
->writeAll
= GL_FALSE
;
763 _mesa_bzero(span
->array
->mask
, (xmin
- x
) * sizeof(GLubyte
));
769 span
->end
= xmax
- x
;
772 return GL_TRUE
; /* some pixels visible */
778 * Apply all the per-fragment opertions to a span of color index fragments
779 * and write them to the enabled color drawbuffers.
780 * The 'span' parameter can be considered to be const. Note that
781 * span->interpMask and span->arrayMask may be changed but will be restored
782 * to their original values before returning.
785 _swrast_write_index_span( GLcontext
*ctx
, SWspan
*span
)
787 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
788 const GLbitfield origInterpMask
= span
->interpMask
;
789 const GLbitfield origArrayMask
= span
->arrayMask
;
791 ASSERT(span
->end
<= MAX_WIDTH
);
792 ASSERT(span
->primitive
== GL_POINT
|| span
->primitive
== GL_LINE
||
793 span
->primitive
== GL_POLYGON
|| span
->primitive
== GL_BITMAP
);
794 ASSERT((span
->interpMask
| span
->arrayMask
) & SPAN_INDEX
);
796 ASSERT((span->interpMask & span->arrayMask) == 0);
799 if (span
->arrayMask
& SPAN_MASK
) {
800 /* mask was initialized by caller, probably glBitmap */
801 span
->writeAll
= GL_FALSE
;
804 _mesa_memset(span
->array
->mask
, 1, span
->end
);
805 span
->writeAll
= GL_TRUE
;
809 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
810 if (!clip_span(ctx
, span
)) {
815 /* Depth bounds test */
816 if (ctx
->Depth
.BoundsTest
&& ctx
->DrawBuffer
->Visual
.depthBits
> 0) {
817 if (!_swrast_depth_bounds_test(ctx
, span
)) {
823 /* Make sure all fragments are within window bounds */
824 if (span
->arrayMask
& SPAN_XY
) {
826 for (i
= 0; i
< span
->end
; i
++) {
827 if (span
->array
->mask
[i
]) {
828 assert(span
->array
->x
[i
] >= ctx
->DrawBuffer
->_Xmin
);
829 assert(span
->array
->x
[i
] < ctx
->DrawBuffer
->_Xmax
);
830 assert(span
->array
->y
[i
] >= ctx
->DrawBuffer
->_Ymin
);
831 assert(span
->array
->y
[i
] < ctx
->DrawBuffer
->_Ymax
);
837 /* Polygon Stippling */
838 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
839 stipple_polygon_span(ctx
, span
);
842 /* Stencil and Z testing */
843 if (ctx
->Depth
.Test
|| ctx
->Stencil
.Enabled
) {
844 if (!(span
->arrayMask
& SPAN_Z
))
845 _swrast_span_interpolate_z(ctx
, span
);
847 if (ctx
->Stencil
.Enabled
) {
848 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
849 span
->arrayMask
= origArrayMask
;
854 ASSERT(ctx
->Depth
.Test
);
855 if (!_swrast_depth_test_span(ctx
, span
)) {
856 span
->interpMask
= origInterpMask
;
857 span
->arrayMask
= origArrayMask
;
863 #if FEATURE_ARB_occlusion_query
864 if (ctx
->Query
.CurrentOcclusionObject
) {
865 /* update count of 'passed' fragments */
866 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
868 for (i
= 0; i
< span
->end
; i
++)
869 q
->Result
+= span
->array
->mask
[i
];
873 /* we have to wait until after occlusion to do this test */
874 if (ctx
->Color
.DrawBuffer
== GL_NONE
|| ctx
->Color
.IndexMask
== 0) {
875 /* write no pixels */
876 span
->arrayMask
= origArrayMask
;
880 /* Interpolate the color indexes if needed */
881 if (swrast
->_FogEnabled
||
882 ctx
->Color
.IndexLogicOpEnabled
||
883 ctx
->Color
.IndexMask
!= 0xffffffff ||
884 (span
->arrayMask
& SPAN_COVERAGE
)) {
885 if (!(span
->arrayMask
& SPAN_INDEX
) /*span->interpMask & SPAN_INDEX*/) {
886 interpolate_indexes(ctx
, span
);
891 if (swrast
->_FogEnabled
) {
892 _swrast_fog_ci_span(ctx
, span
);
895 /* Antialias coverage application */
896 if (span
->arrayMask
& SPAN_COVERAGE
) {
897 const GLfloat
*coverage
= span
->array
->coverage
;
898 GLuint
*index
= span
->array
->index
;
900 for (i
= 0; i
< span
->end
; i
++) {
901 ASSERT(coverage
[i
] < 16);
902 index
[i
] = (index
[i
] & ~0xf) | ((GLuint
) coverage
[i
]);
907 * Write to renderbuffers
910 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
911 const GLuint output
= 0; /* only frag progs can write to other outputs */
912 const GLuint numDrawBuffers
= fb
->_NumColorDrawBuffers
[output
];
913 GLuint indexSave
[MAX_WIDTH
];
916 if (numDrawBuffers
> 1) {
917 /* save indexes for second, third renderbuffer writes */
918 _mesa_memcpy(indexSave
, span
->array
->index
,
919 span
->end
* sizeof(indexSave
[0]));
922 for (buf
= 0; buf
< fb
->_NumColorDrawBuffers
[output
]; buf
++) {
923 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[output
][buf
];
924 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
926 if (ctx
->Color
.IndexLogicOpEnabled
) {
927 _swrast_logicop_ci_span(ctx
, rb
, span
);
930 if (ctx
->Color
.IndexMask
!= 0xffffffff) {
931 _swrast_mask_ci_span(ctx
, rb
, span
);
934 if (!(span
->arrayMask
& SPAN_INDEX
) && span
->indexStep
== 0) {
935 /* all fragments have same color index */
941 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
942 index8
= FixedToInt(span
->index
);
945 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
946 index16
= FixedToInt(span
->index
);
950 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
951 index32
= FixedToInt(span
->index
);
955 if (span
->arrayMask
& SPAN_XY
) {
956 rb
->PutMonoValues(ctx
, rb
, span
->end
, span
->array
->x
,
957 span
->array
->y
, value
, span
->array
->mask
);
960 rb
->PutMonoRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
961 value
, span
->array
->mask
);
965 /* each fragment is a different color */
966 GLubyte index8
[MAX_WIDTH
];
967 GLushort index16
[MAX_WIDTH
];
970 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
972 for (k
= 0; k
< span
->end
; k
++) {
973 index8
[k
] = (GLubyte
) span
->array
->index
[k
];
977 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
979 for (k
= 0; k
< span
->end
; k
++) {
980 index16
[k
] = (GLushort
) span
->array
->index
[k
];
985 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
986 values
= span
->array
->index
;
989 if (span
->arrayMask
& SPAN_XY
) {
990 rb
->PutValues(ctx
, rb
, span
->end
,
991 span
->array
->x
, span
->array
->y
,
992 values
, span
->array
->mask
);
995 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
996 values
, span
->array
->mask
);
1000 if (buf
+ 1 < numDrawBuffers
) {
1001 /* restore original span values */
1002 _mesa_memcpy(span
->array
->index
, indexSave
,
1003 span
->end
* sizeof(indexSave
[0]));
1008 span
->interpMask
= origInterpMask
;
1009 span
->arrayMask
= origArrayMask
;
1014 * Add specular colors to primary colors.
1015 * Only called during fixed-function operation.
1016 * Result is float color array (FRAG_ATTRIB_COL0).
1019 add_specular(GLcontext
*ctx
, SWspan
*span
)
1021 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1022 const GLubyte
*mask
= span
->array
->mask
;
1023 GLfloat (*col0
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1024 GLfloat (*col1
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL1
];
1027 ASSERT(!ctx
->FragmentProgram
._Current
);
1028 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1029 ASSERT(swrast
->_ActiveAttribMask
& FRAG_BIT_COL1
);
1031 if (span
->array
->ChanType
== GL_FLOAT
) {
1032 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1033 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL0
);
1037 /* need float colors */
1038 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1039 interpolate_float_colors(span
);
1043 if ((span
->arrayAttribs
& FRAG_BIT_COL1
) == 0) {
1044 /* XXX could avoid this and interpolate COL1 in the loop below */
1045 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL1
);
1048 ASSERT(span
->arrayAttribs
& FRAG_BIT_COL0
);
1049 ASSERT(span
->arrayAttribs
& FRAG_BIT_COL1
);
1051 for (i
= 0; i
< span
->end
; i
++) {
1053 col0
[i
][0] += col1
[i
][0];
1054 col0
[i
][1] += col1
[i
][1];
1055 col0
[i
][2] += col1
[i
][2];
1059 span
->array
->ChanType
= GL_FLOAT
;
1064 * Apply antialiasing coverage value to alpha values.
1067 apply_aa_coverage(SWspan
*span
)
1069 const GLfloat
*coverage
= span
->array
->coverage
;
1071 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1072 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
1073 for (i
= 0; i
< span
->end
; i
++) {
1074 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
1075 rgba
[i
][ACOMP
] = (GLubyte
) CLAMP(a
, 0.0, 255.0);
1076 ASSERT(coverage
[i
] >= 0.0);
1077 ASSERT(coverage
[i
] <= 1.0);
1080 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
1081 GLushort (*rgba
)[4] = span
->array
->rgba16
;
1082 for (i
= 0; i
< span
->end
; i
++) {
1083 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
1084 rgba
[i
][ACOMP
] = (GLushort
) CLAMP(a
, 0.0, 65535.0);
1088 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1089 for (i
= 0; i
< span
->end
; i
++) {
1090 rgba
[i
][ACOMP
] = rgba
[i
][ACOMP
] * coverage
[i
];
1098 * Clamp span's float colors to [0,1]
1101 clamp_colors(SWspan
*span
)
1103 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1105 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
1106 for (i
= 0; i
< span
->end
; i
++) {
1107 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0F
, 1.0F
);
1108 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0F
, 1.0F
);
1109 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0F
, 1.0F
);
1110 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0F
, 1.0F
);
1116 * Convert the span's color arrays to the given type.
1117 * The only way 'output' can be greater than one is when we have a fragment
1118 * program that writes to gl_FragData[1] or higher.
1119 * \param output which fragment program color output is being processed
1122 convert_color_type(SWspan
*span
, GLenum newType
, GLuint output
)
1126 if (output
> 0 || span
->array
->ChanType
== GL_FLOAT
) {
1127 src
= span
->array
->attribs
[FRAG_ATTRIB_COL0
+ output
];
1128 span
->array
->ChanType
= GL_FLOAT
;
1130 else if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1131 src
= span
->array
->rgba8
;
1134 ASSERT(span
->array
->ChanType
== GL_UNSIGNED_SHORT
);
1135 src
= span
->array
->rgba16
;
1138 if (newType
== GL_UNSIGNED_BYTE
) {
1139 dst
= span
->array
->rgba8
;
1141 else if (newType
== GL_UNSIGNED_SHORT
) {
1142 dst
= span
->array
->rgba16
;
1145 dst
= span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1148 _mesa_convert_colors(span
->array
->ChanType
, src
,
1150 span
->end
, span
->array
->mask
);
1152 span
->array
->ChanType
= newType
;
1158 * Apply fragment shader, fragment program or normal texturing to span.
1161 shade_texture_span(GLcontext
*ctx
, SWspan
*span
)
1163 GLbitfield inputsRead
;
1165 /* Determine which fragment attributes are actually needed */
1166 if (ctx
->FragmentProgram
._Current
) {
1167 inputsRead
= ctx
->FragmentProgram
._Current
->Base
.InputsRead
;
1170 /* XXX we could be a bit smarter about this */
1174 if (ctx
->FragmentProgram
._Current
||
1175 ctx
->ATIFragmentShader
._Enabled
) {
1176 /* programmable shading */
1177 if (span
->primitive
== GL_BITMAP
&& span
->array
->ChanType
!= GL_FLOAT
) {
1178 convert_color_type(span
, GL_FLOAT
, 0);
1180 interpolate_active_attribs(ctx
, span
, ~0);
1181 span
->array
->ChanType
= GL_FLOAT
;
1183 if (!(span
->arrayMask
& SPAN_Z
))
1184 _swrast_span_interpolate_z (ctx
, span
);
1187 if (inputsRead
& FRAG_BIT_WPOS
)
1189 /* XXX always interpolate wpos so that DDX/DDY work */
1191 interpolate_wpos(ctx
, span
);
1193 /* Run fragment program/shader now */
1194 if (ctx
->FragmentProgram
._Current
) {
1195 _swrast_exec_fragment_program(ctx
, span
);
1198 ASSERT(ctx
->ATIFragmentShader
._Enabled
);
1199 _swrast_exec_fragment_shader(ctx
, span
);
1202 else if (ctx
->Texture
._EnabledUnits
) {
1203 /* conventional texturing */
1206 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1207 interpolate_int_colors(ctx
, span
);
1210 if (!(span
->arrayMask
& SPAN_RGBA
))
1211 interpolate_int_colors(ctx
, span
);
1213 if ((span
->arrayAttribs
& FRAG_BITS_TEX_ANY
) == 0x0)
1214 interpolate_texcoords(ctx
, span
);
1216 _swrast_texture_span(ctx
, span
);
1223 * Apply all the per-fragment operations to a span.
1224 * This now includes texturing (_swrast_write_texture_span() is history).
1225 * This function may modify any of the array values in the span.
1226 * span->interpMask and span->arrayMask may be changed but will be restored
1227 * to their original values before returning.
1230 _swrast_write_rgba_span( GLcontext
*ctx
, SWspan
*span
)
1232 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1233 const GLuint colorMask
= *((GLuint
*) ctx
->Color
.ColorMask
);
1234 const GLbitfield origInterpMask
= span
->interpMask
;
1235 const GLbitfield origArrayMask
= span
->arrayMask
;
1236 const GLbitfield origArrayAttribs
= span
->arrayAttribs
;
1237 const GLenum chanType
= span
->array
->ChanType
;
1238 const GLboolean shader
= (ctx
->FragmentProgram
._Current
1239 || ctx
->ATIFragmentShader
._Enabled
);
1240 const GLboolean shaderOrTexture
= shader
|| ctx
->Texture
._EnabledUnits
;
1241 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1245 printf("%s() interp 0x%x array 0x%x\n", __FUNCTION__,
1246 span->interpMask, span->arrayMask);
1249 ASSERT(span
->primitive
== GL_POINT
||
1250 span
->primitive
== GL_LINE
||
1251 span
->primitive
== GL_POLYGON
||
1252 span
->primitive
== GL_BITMAP
);
1253 ASSERT(span
->end
<= MAX_WIDTH
);
1255 /* Fragment write masks */
1256 if (span
->arrayMask
& SPAN_MASK
) {
1257 /* mask was initialized by caller, probably glBitmap */
1258 span
->writeAll
= GL_FALSE
;
1261 _mesa_memset(span
->array
->mask
, 1, span
->end
);
1262 span
->writeAll
= GL_TRUE
;
1265 /* Clip to window/scissor box */
1266 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
1267 if (!clip_span(ctx
, span
)) {
1273 /* Make sure all fragments are within window bounds */
1274 if (span
->arrayMask
& SPAN_XY
) {
1276 for (i
= 0; i
< span
->end
; i
++) {
1277 if (span
->array
->mask
[i
]) {
1278 assert(span
->array
->x
[i
] >= fb
->_Xmin
);
1279 assert(span
->array
->x
[i
] < fb
->_Xmax
);
1280 assert(span
->array
->y
[i
] >= fb
->_Ymin
);
1281 assert(span
->array
->y
[i
] < fb
->_Ymax
);
1287 /* Polygon Stippling */
1288 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
1289 stipple_polygon_span(ctx
, span
);
1292 /* This is the normal place to compute the fragment color/Z
1293 * from texturing or shading.
1295 if (shaderOrTexture
&& !swrast
->_DeferredTexture
) {
1296 shade_texture_span(ctx
, span
);
1299 /* Do the alpha test */
1300 if (ctx
->Color
.AlphaEnabled
) {
1301 if (!_swrast_alpha_test(ctx
, span
)) {
1306 /* Stencil and Z testing */
1307 if (ctx
->Stencil
.Enabled
|| ctx
->Depth
.Test
) {
1308 if (!(span
->arrayMask
& SPAN_Z
))
1309 _swrast_span_interpolate_z(ctx
, span
);
1311 if (ctx
->Stencil
.Enabled
&& fb
->Visual
.stencilBits
> 0) {
1312 /* Combined Z/stencil tests */
1313 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
1317 else if (fb
->Visual
.depthBits
> 0) {
1318 /* Just regular depth testing */
1319 ASSERT(ctx
->Depth
.Test
);
1320 ASSERT(span
->arrayMask
& SPAN_Z
);
1321 if (!_swrast_depth_test_span(ctx
, span
)) {
1327 #if FEATURE_ARB_occlusion_query
1328 if (ctx
->Query
.CurrentOcclusionObject
) {
1329 /* update count of 'passed' fragments */
1330 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
1332 for (i
= 0; i
< span
->end
; i
++)
1333 q
->Result
+= span
->array
->mask
[i
];
1337 /* We had to wait until now to check for glColorMask(0,0,0,0) because of
1338 * the occlusion test.
1340 if (colorMask
== 0x0) {
1344 /* If we were able to defer fragment color computation to now, there's
1345 * a good chance that many fragments will have already been killed by
1346 * Z/stencil testing.
1348 if (shaderOrTexture
&& swrast
->_DeferredTexture
) {
1349 shade_texture_span(ctx
, span
);
1353 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1354 interpolate_int_colors(ctx
, span
);
1357 if ((span
->arrayMask
& SPAN_RGBA
) == 0) {
1358 interpolate_int_colors(ctx
, span
);
1362 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1365 /* Add base and specular colors */
1366 if (ctx
->Fog
.ColorSumEnabled
||
1367 (ctx
->Light
.Enabled
&&
1368 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)) {
1369 add_specular(ctx
, span
);
1374 if (swrast
->_FogEnabled
) {
1375 _swrast_fog_rgba_span(ctx
, span
);
1378 /* Antialias coverage application */
1379 if (span
->arrayMask
& SPAN_COVERAGE
) {
1380 apply_aa_coverage(span
);
1383 /* Clamp color/alpha values over the range [0.0, 1.0] before storage */
1384 if (ctx
->Color
.ClampFragmentColor
== GL_TRUE
&&
1385 span
->array
->ChanType
== GL_FLOAT
) {
1390 * Write to renderbuffers
1392 /* Loop over color outputs (GL_ARB_draw_buffers) written by frag prog */
1393 for (output
= 0; output
< swrast
->_NumColorOutputs
; output
++) {
1394 if (swrast
->_ColorOutputsMask
& (1 << output
)) {
1395 const GLuint numDrawBuffers
= fb
->_NumColorDrawBuffers
[output
];
1396 GLchan rgbaSave
[MAX_WIDTH
][4];
1399 ASSERT(numDrawBuffers
> 0);
1401 if (fb
->_ColorDrawBuffers
[output
][0]->DataType
1402 != span
->array
->ChanType
|| output
> 0) {
1403 convert_color_type(span
,
1404 fb
->_ColorDrawBuffers
[output
][0]->DataType
,
1408 if (numDrawBuffers
> 1) {
1409 /* save colors for second, third renderbuffer writes */
1410 _mesa_memcpy(rgbaSave
, span
->array
->rgba
,
1411 4 * span
->end
* sizeof(GLchan
));
1414 /* Loop over renderbuffers (i.e. GL_FRONT_AND_BACK) */
1415 for (buf
= 0; buf
< numDrawBuffers
; buf
++) {
1416 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[output
][buf
];
1417 ASSERT(rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
);
1419 if (ctx
->Color
._LogicOpEnabled
) {
1420 _swrast_logicop_rgba_span(ctx
, rb
, span
);
1422 else if (ctx
->Color
.BlendEnabled
) {
1423 _swrast_blend_span(ctx
, rb
, span
);
1426 if (colorMask
!= 0xffffffff) {
1427 _swrast_mask_rgba_span(ctx
, rb
, span
);
1430 if (span
->arrayMask
& SPAN_XY
) {
1431 /* array of pixel coords */
1432 ASSERT(rb
->PutValues
);
1433 rb
->PutValues(ctx
, rb
, span
->end
,
1434 span
->array
->x
, span
->array
->y
,
1435 span
->array
->rgba
, span
->array
->mask
);
1438 /* horizontal run of pixels */
1440 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1442 span
->writeAll
? NULL
: span
->array
->mask
);
1445 if (buf
+ 1 < numDrawBuffers
) {
1446 /* restore original span values */
1447 _mesa_memcpy(span
->array
->rgba
, rgbaSave
,
1448 4 * span
->end
* sizeof(GLchan
));
1451 } /* if output is written to */
1455 /* restore these values before returning */
1456 span
->interpMask
= origInterpMask
;
1457 span
->arrayMask
= origArrayMask
;
1458 span
->arrayAttribs
= origArrayAttribs
;
1459 span
->array
->ChanType
= chanType
;
1464 * Read RGBA pixels from a renderbuffer. Clipping will be done to prevent
1465 * reading ouside the buffer's boundaries.
1466 * \param dstType datatype for returned colors
1467 * \param rgba the returned colors
1470 _swrast_read_rgba_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1471 GLuint n
, GLint x
, GLint y
, GLenum dstType
,
1474 const GLint bufWidth
= (GLint
) rb
->Width
;
1475 const GLint bufHeight
= (GLint
) rb
->Height
;
1477 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1478 /* completely above, below, or right */
1479 /* XXX maybe leave rgba values undefined? */
1480 _mesa_bzero(rgba
, 4 * n
* sizeof(GLchan
));
1485 /* left edge clipping */
1487 length
= (GLint
) n
- skip
;
1489 /* completely left of window */
1492 if (length
> bufWidth
) {
1496 else if ((GLint
) (x
+ n
) > bufWidth
) {
1497 /* right edge clipping */
1499 length
= bufWidth
- x
;
1501 /* completely to right of window */
1513 ASSERT(rb
->_BaseFormat
== GL_RGB
|| rb
->_BaseFormat
== GL_RGBA
);
1515 if (rb
->DataType
== dstType
) {
1516 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
,
1517 (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(rb
->DataType
));
1520 GLuint temp
[MAX_WIDTH
* 4];
1521 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, temp
);
1522 _mesa_convert_colors(rb
->DataType
, temp
,
1523 dstType
, (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(dstType
),
1531 * Read CI pixels from a renderbuffer. Clipping will be done to prevent
1532 * reading ouside the buffer's boundaries.
1535 _swrast_read_index_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1536 GLuint n
, GLint x
, GLint y
, GLuint index
[] )
1538 const GLint bufWidth
= (GLint
) rb
->Width
;
1539 const GLint bufHeight
= (GLint
) rb
->Height
;
1541 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1542 /* completely above, below, or right */
1543 _mesa_bzero(index
, n
* sizeof(GLuint
));
1548 /* left edge clipping */
1550 length
= (GLint
) n
- skip
;
1552 /* completely left of window */
1555 if (length
> bufWidth
) {
1559 else if ((GLint
) (x
+ n
) > bufWidth
) {
1560 /* right edge clipping */
1562 length
= bufWidth
- x
;
1564 /* completely to right of window */
1575 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
1577 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1578 GLubyte index8
[MAX_WIDTH
];
1580 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index8
);
1581 for (i
= 0; i
< length
; i
++)
1582 index
[skip
+ i
] = index8
[i
];
1584 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1585 GLushort index16
[MAX_WIDTH
];
1587 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index16
);
1588 for (i
= 0; i
< length
; i
++)
1589 index
[skip
+ i
] = index16
[i
];
1591 else if (rb
->DataType
== GL_UNSIGNED_INT
) {
1592 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index
+ skip
);
1599 * Wrapper for gl_renderbuffer::GetValues() which does clipping to avoid
1600 * reading values outside the buffer bounds.
1601 * We can use this for reading any format/type of renderbuffer.
1602 * \param valueSize is the size in bytes of each value (pixel) put into the
1606 _swrast_get_values(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1607 GLuint count
, const GLint x
[], const GLint y
[],
1608 void *values
, GLuint valueSize
)
1610 GLuint i
, inCount
= 0, inStart
= 0;
1612 for (i
= 0; i
< count
; i
++) {
1613 if (x
[i
] >= 0 && y
[i
] >= 0 &&
1614 x
[i
] < (GLint
) rb
->Width
&& y
[i
] < (GLint
) rb
->Height
) {
1622 /* read [inStart, inStart + inCount) */
1623 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1624 (GLubyte
*) values
+ inStart
* valueSize
);
1630 /* read last values */
1631 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1632 (GLubyte
*) values
+ inStart
* valueSize
);
1638 * Wrapper for gl_renderbuffer::PutRow() which does clipping.
1639 * \param valueSize size of each value (pixel) in bytes
1642 _swrast_put_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1643 GLuint count
, GLint x
, GLint y
,
1644 const GLvoid
*values
, GLuint valueSize
)
1648 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1649 return; /* above or below */
1651 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1652 return; /* entirely left or right */
1654 if ((GLint
) (x
+ count
) > (GLint
) rb
->Width
) {
1656 GLint clip
= x
+ count
- rb
->Width
;
1667 rb
->PutRow(ctx
, rb
, count
, x
, y
,
1668 (const GLubyte
*) values
+ skip
* valueSize
, NULL
);
1673 * Wrapper for gl_renderbuffer::GetRow() which does clipping.
1674 * \param valueSize size of each value (pixel) in bytes
1677 _swrast_get_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1678 GLuint count
, GLint x
, GLint y
,
1679 GLvoid
*values
, GLuint valueSize
)
1683 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1684 return; /* above or below */
1686 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1687 return; /* entirely left or right */
1689 if (x
+ count
> rb
->Width
) {
1691 GLint clip
= x
+ count
- rb
->Width
;
1702 rb
->GetRow(ctx
, rb
, count
, x
, y
, (GLubyte
*) values
+ skip
* valueSize
);
1707 * Get RGBA pixels from the given renderbuffer. Put the pixel colors into
1708 * the span's specular color arrays. The specular color arrays should no
1709 * longer be needed by time this function is called.
1710 * Used by blending, logicop and masking functions.
1711 * \return pointer to the colors we read.
1714 _swrast_get_dest_rgba(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1717 const GLuint pixelSize
= RGBA_PIXEL_SIZE(span
->array
->ChanType
);
1721 * Point rbPixels to a temporary space (use specular color arrays).
1723 rbPixels
= span
->array
->attribs
[FRAG_ATTRIB_COL1
];
1725 /* Get destination values from renderbuffer */
1726 if (span
->arrayMask
& SPAN_XY
) {
1727 _swrast_get_values(ctx
, rb
, span
->end
, span
->array
->x
, span
->array
->y
,
1728 rbPixels
, pixelSize
);
1731 _swrast_get_row(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1732 rbPixels
, pixelSize
);