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
;
1153 span
->array
->rgba
= dst
;
1159 * Apply fragment shader, fragment program or normal texturing to span.
1162 shade_texture_span(GLcontext
*ctx
, SWspan
*span
)
1164 GLbitfield inputsRead
;
1166 /* Determine which fragment attributes are actually needed */
1167 if (ctx
->FragmentProgram
._Current
) {
1168 inputsRead
= ctx
->FragmentProgram
._Current
->Base
.InputsRead
;
1171 /* XXX we could be a bit smarter about this */
1175 if (ctx
->FragmentProgram
._Current
||
1176 ctx
->ATIFragmentShader
._Enabled
) {
1177 /* programmable shading */
1178 if (span
->primitive
== GL_BITMAP
&& span
->array
->ChanType
!= GL_FLOAT
) {
1179 convert_color_type(span
, GL_FLOAT
, 0);
1181 interpolate_active_attribs(ctx
, span
, ~0);
1182 span
->array
->ChanType
= GL_FLOAT
;
1184 if (!(span
->arrayMask
& SPAN_Z
))
1185 _swrast_span_interpolate_z (ctx
, span
);
1188 if (inputsRead
& FRAG_BIT_WPOS
)
1190 /* XXX always interpolate wpos so that DDX/DDY work */
1192 interpolate_wpos(ctx
, span
);
1194 /* Run fragment program/shader now */
1195 if (ctx
->FragmentProgram
._Current
) {
1196 _swrast_exec_fragment_program(ctx
, span
);
1199 ASSERT(ctx
->ATIFragmentShader
._Enabled
);
1200 _swrast_exec_fragment_shader(ctx
, span
);
1203 else if (ctx
->Texture
._EnabledUnits
) {
1204 /* conventional texturing */
1207 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1208 interpolate_int_colors(ctx
, span
);
1211 if (!(span
->arrayMask
& SPAN_RGBA
))
1212 interpolate_int_colors(ctx
, span
);
1214 if ((span
->arrayAttribs
& FRAG_BITS_TEX_ANY
) == 0x0)
1215 interpolate_texcoords(ctx
, span
);
1217 _swrast_texture_span(ctx
, span
);
1224 * Apply all the per-fragment operations to a span.
1225 * This now includes texturing (_swrast_write_texture_span() is history).
1226 * This function may modify any of the array values in the span.
1227 * span->interpMask and span->arrayMask may be changed but will be restored
1228 * to their original values before returning.
1231 _swrast_write_rgba_span( GLcontext
*ctx
, SWspan
*span
)
1233 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1234 const GLuint colorMask
= *((GLuint
*) ctx
->Color
.ColorMask
);
1235 const GLbitfield origInterpMask
= span
->interpMask
;
1236 const GLbitfield origArrayMask
= span
->arrayMask
;
1237 const GLbitfield origArrayAttribs
= span
->arrayAttribs
;
1238 const GLenum origChanType
= span
->array
->ChanType
;
1239 void * const origRgba
= span
->array
->rgba
;
1240 const GLboolean shader
= (ctx
->FragmentProgram
._Current
1241 || ctx
->ATIFragmentShader
._Enabled
);
1242 const GLboolean shaderOrTexture
= shader
|| ctx
->Texture
._EnabledUnits
;
1243 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1247 printf("%s() interp 0x%x array 0x%x\n", __FUNCTION__,
1248 span->interpMask, span->arrayMask);
1251 ASSERT(span
->primitive
== GL_POINT
||
1252 span
->primitive
== GL_LINE
||
1253 span
->primitive
== GL_POLYGON
||
1254 span
->primitive
== GL_BITMAP
);
1255 ASSERT(span
->end
<= MAX_WIDTH
);
1257 /* Fragment write masks */
1258 if (span
->arrayMask
& SPAN_MASK
) {
1259 /* mask was initialized by caller, probably glBitmap */
1260 span
->writeAll
= GL_FALSE
;
1263 _mesa_memset(span
->array
->mask
, 1, span
->end
);
1264 span
->writeAll
= GL_TRUE
;
1267 /* Clip to window/scissor box */
1268 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
1269 if (!clip_span(ctx
, span
)) {
1275 /* Make sure all fragments are within window bounds */
1276 if (span
->arrayMask
& SPAN_XY
) {
1278 for (i
= 0; i
< span
->end
; i
++) {
1279 if (span
->array
->mask
[i
]) {
1280 assert(span
->array
->x
[i
] >= fb
->_Xmin
);
1281 assert(span
->array
->x
[i
] < fb
->_Xmax
);
1282 assert(span
->array
->y
[i
] >= fb
->_Ymin
);
1283 assert(span
->array
->y
[i
] < fb
->_Ymax
);
1289 /* Polygon Stippling */
1290 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
1291 stipple_polygon_span(ctx
, span
);
1294 /* This is the normal place to compute the fragment color/Z
1295 * from texturing or shading.
1297 if (shaderOrTexture
&& !swrast
->_DeferredTexture
) {
1298 shade_texture_span(ctx
, span
);
1301 /* Do the alpha test */
1302 if (ctx
->Color
.AlphaEnabled
) {
1303 if (!_swrast_alpha_test(ctx
, span
)) {
1308 /* Stencil and Z testing */
1309 if (ctx
->Stencil
.Enabled
|| ctx
->Depth
.Test
) {
1310 if (!(span
->arrayMask
& SPAN_Z
))
1311 _swrast_span_interpolate_z(ctx
, span
);
1313 if (ctx
->Stencil
.Enabled
&& fb
->Visual
.stencilBits
> 0) {
1314 /* Combined Z/stencil tests */
1315 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
1319 else if (fb
->Visual
.depthBits
> 0) {
1320 /* Just regular depth testing */
1321 ASSERT(ctx
->Depth
.Test
);
1322 ASSERT(span
->arrayMask
& SPAN_Z
);
1323 if (!_swrast_depth_test_span(ctx
, span
)) {
1329 #if FEATURE_ARB_occlusion_query
1330 if (ctx
->Query
.CurrentOcclusionObject
) {
1331 /* update count of 'passed' fragments */
1332 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
1334 for (i
= 0; i
< span
->end
; i
++)
1335 q
->Result
+= span
->array
->mask
[i
];
1339 /* We had to wait until now to check for glColorMask(0,0,0,0) because of
1340 * the occlusion test.
1342 if (colorMask
== 0x0) {
1346 /* If we were able to defer fragment color computation to now, there's
1347 * a good chance that many fragments will have already been killed by
1348 * Z/stencil testing.
1350 if (shaderOrTexture
&& swrast
->_DeferredTexture
) {
1351 shade_texture_span(ctx
, span
);
1355 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1356 interpolate_int_colors(ctx
, span
);
1359 if ((span
->arrayMask
& SPAN_RGBA
) == 0) {
1360 interpolate_int_colors(ctx
, span
);
1364 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1367 /* Add base and specular colors */
1368 if (ctx
->Fog
.ColorSumEnabled
||
1369 (ctx
->Light
.Enabled
&&
1370 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)) {
1371 add_specular(ctx
, span
);
1376 if (swrast
->_FogEnabled
) {
1377 _swrast_fog_rgba_span(ctx
, span
);
1380 /* Antialias coverage application */
1381 if (span
->arrayMask
& SPAN_COVERAGE
) {
1382 apply_aa_coverage(span
);
1385 /* Clamp color/alpha values over the range [0.0, 1.0] before storage */
1386 if (ctx
->Color
.ClampFragmentColor
== GL_TRUE
&&
1387 span
->array
->ChanType
== GL_FLOAT
) {
1392 * Write to renderbuffers
1394 /* Loop over color outputs (GL_ARB_draw_buffers) written by frag prog */
1395 for (output
= 0; output
< swrast
->_NumColorOutputs
; output
++) {
1396 if (swrast
->_ColorOutputsMask
& (1 << output
)) {
1397 const GLuint numDrawBuffers
= fb
->_NumColorDrawBuffers
[output
];
1398 GLchan rgbaSave
[MAX_WIDTH
][4];
1401 ASSERT(numDrawBuffers
> 0);
1403 if (fb
->_ColorDrawBuffers
[output
][0]->DataType
1404 != span
->array
->ChanType
|| output
> 0) {
1405 convert_color_type(span
,
1406 fb
->_ColorDrawBuffers
[output
][0]->DataType
,
1410 if (numDrawBuffers
> 1) {
1411 /* save colors for second, third renderbuffer writes */
1412 _mesa_memcpy(rgbaSave
, span
->array
->rgba
,
1413 4 * span
->end
* sizeof(GLchan
));
1416 /* Loop over renderbuffers (i.e. GL_FRONT_AND_BACK) */
1417 for (buf
= 0; buf
< numDrawBuffers
; buf
++) {
1418 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[output
][buf
];
1419 ASSERT(rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
);
1421 if (ctx
->Color
._LogicOpEnabled
) {
1422 _swrast_logicop_rgba_span(ctx
, rb
, span
);
1424 else if (ctx
->Color
.BlendEnabled
) {
1425 _swrast_blend_span(ctx
, rb
, span
);
1428 if (colorMask
!= 0xffffffff) {
1429 _swrast_mask_rgba_span(ctx
, rb
, span
);
1432 if (span
->arrayMask
& SPAN_XY
) {
1433 /* array of pixel coords */
1434 ASSERT(rb
->PutValues
);
1435 rb
->PutValues(ctx
, rb
, span
->end
,
1436 span
->array
->x
, span
->array
->y
,
1437 span
->array
->rgba
, span
->array
->mask
);
1440 /* horizontal run of pixels */
1442 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1444 span
->writeAll
? NULL
: span
->array
->mask
);
1447 if (buf
+ 1 < numDrawBuffers
) {
1448 /* restore original span values */
1449 _mesa_memcpy(span
->array
->rgba
, rgbaSave
,
1450 4 * span
->end
* sizeof(GLchan
));
1453 } /* if output is written to */
1457 /* restore these values before returning */
1458 span
->interpMask
= origInterpMask
;
1459 span
->arrayMask
= origArrayMask
;
1460 span
->arrayAttribs
= origArrayAttribs
;
1461 span
->array
->ChanType
= origChanType
;
1462 span
->array
->rgba
= origRgba
;
1467 * Read RGBA pixels from a renderbuffer. Clipping will be done to prevent
1468 * reading ouside the buffer's boundaries.
1469 * \param dstType datatype for returned colors
1470 * \param rgba the returned colors
1473 _swrast_read_rgba_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1474 GLuint n
, GLint x
, GLint y
, GLenum dstType
,
1477 const GLint bufWidth
= (GLint
) rb
->Width
;
1478 const GLint bufHeight
= (GLint
) rb
->Height
;
1480 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1481 /* completely above, below, or right */
1482 /* XXX maybe leave rgba values undefined? */
1483 _mesa_bzero(rgba
, 4 * n
* sizeof(GLchan
));
1488 /* left edge clipping */
1490 length
= (GLint
) n
- skip
;
1492 /* completely left of window */
1495 if (length
> bufWidth
) {
1499 else if ((GLint
) (x
+ n
) > bufWidth
) {
1500 /* right edge clipping */
1502 length
= bufWidth
- x
;
1504 /* completely to right of window */
1516 ASSERT(rb
->_BaseFormat
== GL_RGB
|| rb
->_BaseFormat
== GL_RGBA
);
1518 if (rb
->DataType
== dstType
) {
1519 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
,
1520 (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(rb
->DataType
));
1523 GLuint temp
[MAX_WIDTH
* 4];
1524 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, temp
);
1525 _mesa_convert_colors(rb
->DataType
, temp
,
1526 dstType
, (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(dstType
),
1534 * Read CI pixels from a renderbuffer. Clipping will be done to prevent
1535 * reading ouside the buffer's boundaries.
1538 _swrast_read_index_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1539 GLuint n
, GLint x
, GLint y
, GLuint index
[] )
1541 const GLint bufWidth
= (GLint
) rb
->Width
;
1542 const GLint bufHeight
= (GLint
) rb
->Height
;
1544 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1545 /* completely above, below, or right */
1546 _mesa_bzero(index
, n
* sizeof(GLuint
));
1551 /* left edge clipping */
1553 length
= (GLint
) n
- skip
;
1555 /* completely left of window */
1558 if (length
> bufWidth
) {
1562 else if ((GLint
) (x
+ n
) > bufWidth
) {
1563 /* right edge clipping */
1565 length
= bufWidth
- x
;
1567 /* completely to right of window */
1578 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
1580 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1581 GLubyte index8
[MAX_WIDTH
];
1583 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index8
);
1584 for (i
= 0; i
< length
; i
++)
1585 index
[skip
+ i
] = index8
[i
];
1587 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1588 GLushort index16
[MAX_WIDTH
];
1590 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index16
);
1591 for (i
= 0; i
< length
; i
++)
1592 index
[skip
+ i
] = index16
[i
];
1594 else if (rb
->DataType
== GL_UNSIGNED_INT
) {
1595 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index
+ skip
);
1602 * Wrapper for gl_renderbuffer::GetValues() which does clipping to avoid
1603 * reading values outside the buffer bounds.
1604 * We can use this for reading any format/type of renderbuffer.
1605 * \param valueSize is the size in bytes of each value (pixel) put into the
1609 _swrast_get_values(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1610 GLuint count
, const GLint x
[], const GLint y
[],
1611 void *values
, GLuint valueSize
)
1613 GLuint i
, inCount
= 0, inStart
= 0;
1615 for (i
= 0; i
< count
; i
++) {
1616 if (x
[i
] >= 0 && y
[i
] >= 0 &&
1617 x
[i
] < (GLint
) rb
->Width
&& y
[i
] < (GLint
) rb
->Height
) {
1625 /* read [inStart, inStart + inCount) */
1626 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1627 (GLubyte
*) values
+ inStart
* valueSize
);
1633 /* read last values */
1634 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1635 (GLubyte
*) values
+ inStart
* valueSize
);
1641 * Wrapper for gl_renderbuffer::PutRow() which does clipping.
1642 * \param valueSize size of each value (pixel) in bytes
1645 _swrast_put_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1646 GLuint count
, GLint x
, GLint y
,
1647 const GLvoid
*values
, GLuint valueSize
)
1651 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1652 return; /* above or below */
1654 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1655 return; /* entirely left or right */
1657 if ((GLint
) (x
+ count
) > (GLint
) rb
->Width
) {
1659 GLint clip
= x
+ count
- rb
->Width
;
1670 rb
->PutRow(ctx
, rb
, count
, x
, y
,
1671 (const GLubyte
*) values
+ skip
* valueSize
, NULL
);
1676 * Wrapper for gl_renderbuffer::GetRow() which does clipping.
1677 * \param valueSize size of each value (pixel) in bytes
1680 _swrast_get_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1681 GLuint count
, GLint x
, GLint y
,
1682 GLvoid
*values
, GLuint valueSize
)
1686 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1687 return; /* above or below */
1689 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1690 return; /* entirely left or right */
1692 if (x
+ count
> rb
->Width
) {
1694 GLint clip
= x
+ count
- rb
->Width
;
1705 rb
->GetRow(ctx
, rb
, count
, x
, y
, (GLubyte
*) values
+ skip
* valueSize
);
1710 * Get RGBA pixels from the given renderbuffer. Put the pixel colors into
1711 * the span's specular color arrays. The specular color arrays should no
1712 * longer be needed by time this function is called.
1713 * Used by blending, logicop and masking functions.
1714 * \return pointer to the colors we read.
1717 _swrast_get_dest_rgba(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1720 const GLuint pixelSize
= RGBA_PIXEL_SIZE(span
->array
->ChanType
);
1724 * Point rbPixels to a temporary space (use specular color arrays).
1726 rbPixels
= span
->array
->attribs
[FRAG_ATTRIB_COL1
];
1728 /* Get destination values from renderbuffer */
1729 if (span
->arrayMask
& SPAN_XY
) {
1730 _swrast_get_values(ctx
, rb
, span
->end
, span
->array
->x
, span
->array
->y
,
1731 rbPixels
, pixelSize
);
1734 _swrast_get_row(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1735 rbPixels
, pixelSize
);