2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 * \file swrast/s_span.c
29 * \brief Span processing functions used by all rasterization functions.
30 * This is where all the per-fragment tests are performed
34 #include "main/glheader.h"
35 #include "main/colormac.h"
36 #include "main/context.h"
37 #include "main/macros.h"
38 #include "main/imports.h"
39 #include "main/image.h"
41 #include "s_atifragshader.h"
44 #include "s_context.h"
48 #include "s_masking.h"
49 #include "s_fragprog.h"
51 #include "s_stencil.h"
52 #include "s_texcombine.h"
56 * Set default fragment attributes for the span using the
57 * current raster values. Used prior to glDraw/CopyPixels
61 _swrast_span_default_attribs(GLcontext
*ctx
, SWspan
*span
)
65 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
66 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16)
67 span
->z
= FloatToFixed(ctx
->Current
.RasterPos
[2] * depthMax
+ 0.5F
);
69 GLfloat tmpf
= ctx
->Current
.RasterPos
[2] * depthMax
;
70 tmpf
= MIN2(tmpf
, depthMax
);
71 span
->z
= (GLint
)tmpf
;
74 span
->interpMask
|= SPAN_Z
;
77 /* W (for perspective correction) */
78 span
->attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0;
79 span
->attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0;
80 span
->attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0;
82 /* primary color, or color index */
83 if (ctx
->Visual
.rgbMode
) {
85 UNCLAMPED_FLOAT_TO_CHAN(r
, ctx
->Current
.RasterColor
[0]);
86 UNCLAMPED_FLOAT_TO_CHAN(g
, ctx
->Current
.RasterColor
[1]);
87 UNCLAMPED_FLOAT_TO_CHAN(b
, ctx
->Current
.RasterColor
[2]);
88 UNCLAMPED_FLOAT_TO_CHAN(a
, ctx
->Current
.RasterColor
[3]);
89 #if CHAN_TYPE == GL_FLOAT
95 span
->red
= IntToFixed(r
);
96 span
->green
= IntToFixed(g
);
97 span
->blue
= IntToFixed(b
);
98 span
->alpha
= IntToFixed(a
);
104 span
->interpMask
|= SPAN_RGBA
;
106 COPY_4V(span
->attrStart
[FRAG_ATTRIB_COL0
], ctx
->Current
.RasterColor
);
107 ASSIGN_4V(span
->attrStepX
[FRAG_ATTRIB_COL0
], 0.0, 0.0, 0.0, 0.0);
108 ASSIGN_4V(span
->attrStepY
[FRAG_ATTRIB_COL0
], 0.0, 0.0, 0.0, 0.0);
111 span
->index
= FloatToFixed(ctx
->Current
.RasterIndex
);
113 span
->interpMask
|= SPAN_INDEX
;
116 /* Secondary color */
117 if (ctx
->Visual
.rgbMode
&& (ctx
->Light
.Enabled
|| ctx
->Fog
.ColorSumEnabled
))
119 COPY_4V(span
->attrStart
[FRAG_ATTRIB_COL1
], ctx
->Current
.RasterSecondaryColor
);
120 ASSIGN_4V(span
->attrStepX
[FRAG_ATTRIB_COL1
], 0.0, 0.0, 0.0, 0.0);
121 ASSIGN_4V(span
->attrStepY
[FRAG_ATTRIB_COL1
], 0.0, 0.0, 0.0, 0.0);
126 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
127 GLfloat fogVal
; /* a coord or a blend factor */
128 if (swrast
->_PreferPixelFog
) {
129 /* fog blend factors will be computed from fog coordinates per pixel */
130 fogVal
= ctx
->Current
.RasterDistance
;
133 /* fog blend factor should be computed from fogcoord now */
134 fogVal
= _swrast_z_to_fogfactor(ctx
, ctx
->Current
.RasterDistance
);
136 span
->attrStart
[FRAG_ATTRIB_FOGC
][0] = fogVal
;
137 span
->attrStepX
[FRAG_ATTRIB_FOGC
][0] = 0.0;
138 span
->attrStepY
[FRAG_ATTRIB_FOGC
][0] = 0.0;
144 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
145 const GLuint attr
= FRAG_ATTRIB_TEX0
+ i
;
146 const GLfloat
*tc
= ctx
->Current
.RasterTexCoords
[i
];
147 if (ctx
->FragmentProgram
._Current
|| ctx
->ATIFragmentShader
._Enabled
) {
148 COPY_4V(span
->attrStart
[attr
], tc
);
150 else if (tc
[3] > 0.0F
) {
151 /* use (s/q, t/q, r/q, 1) */
152 span
->attrStart
[attr
][0] = tc
[0] / tc
[3];
153 span
->attrStart
[attr
][1] = tc
[1] / tc
[3];
154 span
->attrStart
[attr
][2] = tc
[2] / tc
[3];
155 span
->attrStart
[attr
][3] = 1.0;
158 ASSIGN_4V(span
->attrStart
[attr
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
160 ASSIGN_4V(span
->attrStepX
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
161 ASSIGN_4V(span
->attrStepY
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
168 * Interpolate the active attributes (and'd with attrMask) to
169 * fill in span->array->attribs[].
170 * Perspective correction will be done. The point/line/triangle function
171 * should have computed attrStart/Step values for FRAG_ATTRIB_WPOS[3]!
174 interpolate_active_attribs(GLcontext
*ctx
, SWspan
*span
, GLbitfield attrMask
)
176 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
179 * Don't overwrite existing array values, such as colors that may have
180 * been produced by glDraw/CopyPixels.
182 attrMask
&= ~span
->arrayAttribs
;
185 if (attrMask
& (1 << attr
)) {
186 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
187 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
188 const GLfloat dv0dx
= span
->attrStepX
[attr
][0];
189 const GLfloat dv1dx
= span
->attrStepX
[attr
][1];
190 const GLfloat dv2dx
= span
->attrStepX
[attr
][2];
191 const GLfloat dv3dx
= span
->attrStepX
[attr
][3];
192 GLfloat v0
= span
->attrStart
[attr
][0] + span
->leftClip
* dv0dx
;
193 GLfloat v1
= span
->attrStart
[attr
][1] + span
->leftClip
* dv1dx
;
194 GLfloat v2
= span
->attrStart
[attr
][2] + span
->leftClip
* dv2dx
;
195 GLfloat v3
= span
->attrStart
[attr
][3] + span
->leftClip
* dv3dx
;
197 for (k
= 0; k
< span
->end
; k
++) {
198 const GLfloat invW
= 1.0f
/ w
;
199 span
->array
->attribs
[attr
][k
][0] = v0
* invW
;
200 span
->array
->attribs
[attr
][k
][1] = v1
* invW
;
201 span
->array
->attribs
[attr
][k
][2] = v2
* invW
;
202 span
->array
->attribs
[attr
][k
][3] = v3
* invW
;
209 ASSERT((span
->arrayAttribs
& (1 << attr
)) == 0);
210 span
->arrayAttribs
|= (1 << attr
);
217 * Interpolate primary colors to fill in the span->array->rgba8 (or rgb16)
221 interpolate_int_colors(GLcontext
*ctx
, SWspan
*span
)
223 const GLuint n
= span
->end
;
227 ASSERT(!(span
->arrayMask
& SPAN_RGBA
));
230 switch (span
->array
->ChanType
) {
232 case GL_UNSIGNED_BYTE
:
234 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
235 if (span
->interpMask
& SPAN_FLAT
) {
237 color
[RCOMP
] = FixedToInt(span
->red
);
238 color
[GCOMP
] = FixedToInt(span
->green
);
239 color
[BCOMP
] = FixedToInt(span
->blue
);
240 color
[ACOMP
] = FixedToInt(span
->alpha
);
241 for (i
= 0; i
< n
; i
++) {
242 COPY_4UBV(rgba
[i
], color
);
246 GLfixed r
= span
->red
;
247 GLfixed g
= span
->green
;
248 GLfixed b
= span
->blue
;
249 GLfixed a
= span
->alpha
;
250 GLint dr
= span
->redStep
;
251 GLint dg
= span
->greenStep
;
252 GLint db
= span
->blueStep
;
253 GLint da
= span
->alphaStep
;
254 for (i
= 0; i
< n
; i
++) {
255 rgba
[i
][RCOMP
] = FixedToChan(r
);
256 rgba
[i
][GCOMP
] = FixedToChan(g
);
257 rgba
[i
][BCOMP
] = FixedToChan(b
);
258 rgba
[i
][ACOMP
] = FixedToChan(a
);
267 case GL_UNSIGNED_SHORT
:
269 GLushort (*rgba
)[4] = span
->array
->rgba16
;
270 if (span
->interpMask
& SPAN_FLAT
) {
272 color
[RCOMP
] = FixedToInt(span
->red
);
273 color
[GCOMP
] = FixedToInt(span
->green
);
274 color
[BCOMP
] = FixedToInt(span
->blue
);
275 color
[ACOMP
] = FixedToInt(span
->alpha
);
276 for (i
= 0; i
< n
; i
++) {
277 COPY_4V(rgba
[i
], color
);
281 GLushort (*rgba
)[4] = span
->array
->rgba16
;
283 GLint dr
, dg
, db
, da
;
289 dg
= span
->greenStep
;
291 da
= span
->alphaStep
;
292 for (i
= 0; i
< n
; i
++) {
293 rgba
[i
][RCOMP
] = FixedToChan(r
);
294 rgba
[i
][GCOMP
] = FixedToChan(g
);
295 rgba
[i
][BCOMP
] = FixedToChan(b
);
296 rgba
[i
][ACOMP
] = FixedToChan(a
);
307 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL0
);
310 _mesa_problem(NULL
, "bad datatype in interpolate_int_colors");
312 span
->arrayMask
|= SPAN_RGBA
;
317 * Populate the FRAG_ATTRIB_COL0 array.
320 interpolate_float_colors(SWspan
*span
)
322 GLfloat (*col0
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
323 const GLuint n
= span
->end
;
326 assert(!(span
->arrayAttribs
& FRAG_BIT_COL0
));
328 if (span
->arrayMask
& SPAN_RGBA
) {
329 /* convert array of int colors */
330 for (i
= 0; i
< n
; i
++) {
331 col0
[i
][0] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][0]);
332 col0
[i
][1] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][1]);
333 col0
[i
][2] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][2]);
334 col0
[i
][3] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][3]);
338 /* interpolate red/green/blue/alpha to get float colors */
339 ASSERT(span
->interpMask
& SPAN_RGBA
);
340 if (span
->interpMask
& SPAN_FLAT
) {
341 GLfloat r
= FixedToFloat(span
->red
);
342 GLfloat g
= FixedToFloat(span
->green
);
343 GLfloat b
= FixedToFloat(span
->blue
);
344 GLfloat a
= FixedToFloat(span
->alpha
);
345 for (i
= 0; i
< n
; i
++) {
346 ASSIGN_4V(col0
[i
], r
, g
, b
, a
);
350 GLfloat r
= FixedToFloat(span
->red
);
351 GLfloat g
= FixedToFloat(span
->green
);
352 GLfloat b
= FixedToFloat(span
->blue
);
353 GLfloat a
= FixedToFloat(span
->alpha
);
354 GLfloat dr
= FixedToFloat(span
->redStep
);
355 GLfloat dg
= FixedToFloat(span
->greenStep
);
356 GLfloat db
= FixedToFloat(span
->blueStep
);
357 GLfloat da
= FixedToFloat(span
->alphaStep
);
358 for (i
= 0; i
< n
; i
++) {
371 span
->arrayAttribs
|= FRAG_BIT_COL0
;
372 span
->array
->ChanType
= GL_FLOAT
;
377 /* Fill in the span.color.index array from the interpolation values */
379 interpolate_indexes(GLcontext
*ctx
, SWspan
*span
)
381 GLfixed index
= span
->index
;
382 const GLint indexStep
= span
->indexStep
;
383 const GLuint n
= span
->end
;
384 GLuint
*indexes
= span
->array
->index
;
388 ASSERT(!(span
->arrayMask
& SPAN_INDEX
));
390 if ((span
->interpMask
& SPAN_FLAT
) || (indexStep
== 0)) {
392 index
= FixedToInt(index
);
393 for (i
= 0; i
< n
; i
++) {
399 for (i
= 0; i
< n
; i
++) {
400 indexes
[i
] = FixedToInt(index
);
404 span
->arrayMask
|= SPAN_INDEX
;
405 span
->interpMask
&= ~SPAN_INDEX
;
410 * Fill in the span.zArray array from the span->z, zStep values.
413 _swrast_span_interpolate_z( const GLcontext
*ctx
, SWspan
*span
)
415 const GLuint n
= span
->end
;
418 ASSERT(!(span
->arrayMask
& SPAN_Z
));
420 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
421 GLfixed zval
= span
->z
;
422 GLuint
*z
= span
->array
->z
;
423 for (i
= 0; i
< n
; i
++) {
424 z
[i
] = FixedToInt(zval
);
429 /* Deep Z buffer, no fixed->int shift */
430 GLuint zval
= span
->z
;
431 GLuint
*z
= span
->array
->z
;
432 for (i
= 0; i
< n
; i
++) {
437 span
->interpMask
&= ~SPAN_Z
;
438 span
->arrayMask
|= SPAN_Z
;
443 * Compute mipmap LOD from partial derivatives.
444 * This the ideal solution, as given in the OpenGL spec.
447 _swrast_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
);
464 * Compute mipmap LOD from partial derivatives.
465 * 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
);
492 * Fill in the span.array->attrib[FRAG_ATTRIB_TEXn] arrays from the
493 * using the attrStart/Step values.
495 * This function only used during fixed-function fragment processing.
497 * Note: in the places where we divide by Q (or mult by invQ) we're
498 * really doing two things: perspective correction and texcoord
499 * projection. Remember, for texcoord (s,t,r,q) we need to index
500 * texels with (s/q, t/q, r/q).
503 interpolate_texcoords(GLcontext
*ctx
, SWspan
*span
)
506 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
509 /* XXX CoordUnits vs. ImageUnits */
510 for (u
= 0; u
< maxUnit
; u
++) {
511 if (ctx
->Texture
._EnabledCoordUnits
& (1 << u
)) {
512 const GLuint attr
= FRAG_ATTRIB_TEX0
+ u
;
513 const struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[u
]._Current
;
515 GLboolean needLambda
;
516 GLfloat (*texcoord
)[4] = span
->array
->attribs
[attr
];
517 GLfloat
*lambda
= span
->array
->lambda
[u
];
518 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
519 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
520 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
521 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
522 const GLfloat drdx
= span
->attrStepX
[attr
][2];
523 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
524 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
525 GLfloat s
= span
->attrStart
[attr
][0] + span
->leftClip
* dsdx
;
526 GLfloat t
= span
->attrStart
[attr
][1] + span
->leftClip
* dtdx
;
527 GLfloat r
= span
->attrStart
[attr
][2] + span
->leftClip
* drdx
;
528 GLfloat q
= span
->attrStart
[attr
][3] + span
->leftClip
* dqdx
;
531 const struct gl_texture_image
*img
= obj
->Image
[0][obj
->BaseLevel
];
532 needLambda
= (obj
->MinFilter
!= obj
->MagFilter
)
533 || ctx
->FragmentProgram
._Current
;
534 texW
= img
->WidthScale
;
535 texH
= img
->HeightScale
;
538 /* using a fragment program */
541 needLambda
= GL_FALSE
;
546 if (ctx
->FragmentProgram
._Current
547 || ctx
->ATIFragmentShader
._Enabled
) {
548 /* do perspective correction but don't divide s, t, r by q */
549 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
550 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3] + span
->leftClip
* dwdx
;
551 for (i
= 0; i
< span
->end
; i
++) {
552 const GLfloat invW
= 1.0F
/ w
;
553 texcoord
[i
][0] = s
* invW
;
554 texcoord
[i
][1] = t
* invW
;
555 texcoord
[i
][2] = r
* invW
;
556 texcoord
[i
][3] = q
* invW
;
557 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
558 dqdx
, dqdy
, texW
, texH
,
568 for (i
= 0; i
< span
->end
; i
++) {
569 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
570 texcoord
[i
][0] = s
* invQ
;
571 texcoord
[i
][1] = t
* invQ
;
572 texcoord
[i
][2] = r
* invQ
;
574 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
575 dqdx
, dqdy
, texW
, texH
,
583 span
->arrayMask
|= SPAN_LAMBDA
;
587 if (ctx
->FragmentProgram
._Current
||
588 ctx
->ATIFragmentShader
._Enabled
) {
589 /* do perspective correction but don't divide s, t, r by q */
590 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
591 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3] + span
->leftClip
* dwdx
;
592 for (i
= 0; i
< span
->end
; i
++) {
593 const GLfloat invW
= 1.0F
/ w
;
594 texcoord
[i
][0] = s
* invW
;
595 texcoord
[i
][1] = t
* invW
;
596 texcoord
[i
][2] = r
* invW
;
597 texcoord
[i
][3] = q
* invW
;
606 else if (dqdx
== 0.0F
) {
607 /* Ortho projection or polygon's parallel to window X axis */
608 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
609 for (i
= 0; i
< span
->end
; i
++) {
610 texcoord
[i
][0] = s
* invQ
;
611 texcoord
[i
][1] = t
* invQ
;
612 texcoord
[i
][2] = r
* invQ
;
621 for (i
= 0; i
< span
->end
; i
++) {
622 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
623 texcoord
[i
][0] = s
* invQ
;
624 texcoord
[i
][1] = t
* invQ
;
625 texcoord
[i
][2] = r
* invQ
;
641 * Fill in the arrays->attribs[FRAG_ATTRIB_WPOS] array.
644 interpolate_wpos(GLcontext
*ctx
, SWspan
*span
)
646 GLfloat (*wpos
)[4] = span
->array
->attribs
[FRAG_ATTRIB_WPOS
];
648 const GLfloat zScale
= 1.0F
/ ctx
->DrawBuffer
->_DepthMaxF
;
651 if (span
->arrayMask
& SPAN_XY
) {
652 for (i
= 0; i
< span
->end
; i
++) {
653 wpos
[i
][0] = (GLfloat
) span
->array
->x
[i
];
654 wpos
[i
][1] = (GLfloat
) span
->array
->y
[i
];
658 for (i
= 0; i
< span
->end
; i
++) {
659 wpos
[i
][0] = (GLfloat
) span
->x
+ i
;
660 wpos
[i
][1] = (GLfloat
) span
->y
;
664 dw
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
665 w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3] + span
->leftClip
* dw
;
666 for (i
= 0; i
< span
->end
; i
++) {
667 wpos
[i
][2] = (GLfloat
) span
->array
->z
[i
] * zScale
;
675 * Apply the current polygon stipple pattern to a span of pixels.
678 stipple_polygon_span(GLcontext
*ctx
, SWspan
*span
)
680 GLubyte
*mask
= span
->array
->mask
;
682 ASSERT(ctx
->Polygon
.StippleFlag
);
684 if (span
->arrayMask
& SPAN_XY
) {
685 /* arrays of x/y pixel coords */
687 for (i
= 0; i
< span
->end
; i
++) {
688 const GLint col
= span
->array
->x
[i
] % 32;
689 const GLint row
= span
->array
->y
[i
] % 32;
690 const GLuint stipple
= ctx
->PolygonStipple
[row
];
691 if (((1 << col
) & stipple
) == 0) {
697 /* horizontal span of pixels */
698 const GLuint highBit
= 1 << 31;
699 const GLuint stipple
= ctx
->PolygonStipple
[span
->y
% 32];
700 GLuint i
, m
= highBit
>> (GLuint
) (span
->x
% 32);
701 for (i
= 0; i
< span
->end
; i
++) {
702 if ((m
& stipple
) == 0) {
711 span
->writeAll
= GL_FALSE
;
716 * Clip a pixel span to the current buffer/window boundaries:
717 * DrawBuffer->_Xmin, _Xmax, _Ymin, _Ymax. This will accomplish
718 * window clipping and scissoring.
719 * Return: GL_TRUE some pixels still visible
720 * GL_FALSE nothing visible
723 clip_span( GLcontext
*ctx
, SWspan
*span
)
725 const GLint xmin
= ctx
->DrawBuffer
->_Xmin
;
726 const GLint xmax
= ctx
->DrawBuffer
->_Xmax
;
727 const GLint ymin
= ctx
->DrawBuffer
->_Ymin
;
728 const GLint ymax
= ctx
->DrawBuffer
->_Ymax
;
732 if (span
->arrayMask
& SPAN_XY
) {
733 /* arrays of x/y pixel coords */
734 const GLint
*x
= span
->array
->x
;
735 const GLint
*y
= span
->array
->y
;
736 const GLint n
= span
->end
;
737 GLubyte
*mask
= span
->array
->mask
;
739 if (span
->arrayMask
& SPAN_MASK
) {
740 /* note: using & intead of && to reduce branches */
741 for (i
= 0; i
< n
; i
++) {
742 mask
[i
] &= (x
[i
] >= xmin
) & (x
[i
] < xmax
)
743 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
747 /* note: using & intead of && to reduce branches */
748 for (i
= 0; i
< n
; i
++) {
749 mask
[i
] = (x
[i
] >= xmin
) & (x
[i
] < xmax
)
750 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
753 return GL_TRUE
; /* some pixels visible */
756 /* horizontal span of pixels */
757 const GLint x
= span
->x
;
758 const GLint y
= span
->y
;
761 /* Trivial rejection tests */
762 if (y
< ymin
|| y
>= ymax
|| x
+ n
<= xmin
|| x
>= xmax
) {
764 return GL_FALSE
; /* all pixels clipped */
770 n
= span
->end
= xmax
- x
;
773 /* Clip to the left */
775 const GLint leftClip
= xmin
- x
;
778 ASSERT(leftClip
> 0);
779 ASSERT(x
+ n
> xmin
);
781 /* Clip 'leftClip' pixels from the left side.
782 * The span->leftClip field will be applied when we interpolate
783 * fragment attributes.
784 * For arrays of values, shift them left.
786 for (i
= 0; i
< FRAG_ATTRIB_MAX
; i
++) {
787 if (span
->interpMask
& (1 << i
)) {
789 for (j
= 0; j
< 4; j
++) {
790 span
->attrStart
[i
][j
] += leftClip
* span
->attrStepX
[i
][j
];
795 span
->red
+= leftClip
* span
->redStep
;
796 span
->green
+= leftClip
* span
->greenStep
;
797 span
->blue
+= leftClip
* span
->blueStep
;
798 span
->alpha
+= leftClip
* span
->alphaStep
;
799 span
->index
+= leftClip
* span
->indexStep
;
800 span
->z
+= leftClip
* span
->zStep
;
801 span
->intTex
[0] += leftClip
* span
->intTexStep
[0];
802 span
->intTex
[1] += leftClip
* span
->intTexStep
[1];
804 #define SHIFT_ARRAY(ARRAY, SHIFT, LEN) \
805 memcpy(ARRAY, ARRAY + (SHIFT), (LEN) * sizeof(ARRAY[0]))
807 for (i
= 0; i
< FRAG_ATTRIB_MAX
; i
++) {
808 if (span
->arrayAttribs
& (1 << i
)) {
809 /* shift array elements left by 'leftClip' */
810 SHIFT_ARRAY(span
->array
->attribs
[i
], leftClip
, n
- leftClip
);
814 SHIFT_ARRAY(span
->array
->mask
, leftClip
, n
- leftClip
);
815 SHIFT_ARRAY(span
->array
->rgba8
, leftClip
, n
- leftClip
);
816 SHIFT_ARRAY(span
->array
->rgba16
, leftClip
, n
- leftClip
);
817 SHIFT_ARRAY(span
->array
->x
, leftClip
, n
- leftClip
);
818 SHIFT_ARRAY(span
->array
->y
, leftClip
, n
- leftClip
);
819 SHIFT_ARRAY(span
->array
->z
, leftClip
, n
- leftClip
);
820 SHIFT_ARRAY(span
->array
->index
, leftClip
, n
- leftClip
);
821 for (i
= 0; i
< MAX_TEXTURE_COORD_UNITS
; i
++) {
822 SHIFT_ARRAY(span
->array
->lambda
[i
], leftClip
, n
- leftClip
);
824 SHIFT_ARRAY(span
->array
->coverage
, leftClip
, n
- leftClip
);
828 span
->leftClip
= leftClip
;
830 span
->end
-= leftClip
;
831 span
->writeAll
= GL_FALSE
;
834 ASSERT(span
->x
>= xmin
);
835 ASSERT(span
->x
+ span
->end
<= xmax
);
836 ASSERT(span
->y
>= ymin
);
837 ASSERT(span
->y
< ymax
);
839 return GL_TRUE
; /* some pixels visible */
845 * Apply all the per-fragment opertions to a span of color index fragments
846 * and write them to the enabled color drawbuffers.
847 * The 'span' parameter can be considered to be const. Note that
848 * span->interpMask and span->arrayMask may be changed but will be restored
849 * to their original values before returning.
852 _swrast_write_index_span( GLcontext
*ctx
, SWspan
*span
)
854 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
855 const GLbitfield origInterpMask
= span
->interpMask
;
856 const GLbitfield origArrayMask
= span
->arrayMask
;
857 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
859 ASSERT(span
->end
<= MAX_WIDTH
);
860 ASSERT(span
->primitive
== GL_POINT
|| span
->primitive
== GL_LINE
||
861 span
->primitive
== GL_POLYGON
|| span
->primitive
== GL_BITMAP
);
862 ASSERT((span
->interpMask
| span
->arrayMask
) & SPAN_INDEX
);
864 ASSERT((span->interpMask & span->arrayMask) == 0);
867 if (span
->arrayMask
& SPAN_MASK
) {
868 /* mask was initialized by caller, probably glBitmap */
869 span
->writeAll
= GL_FALSE
;
872 _mesa_memset(span
->array
->mask
, 1, span
->end
);
873 span
->writeAll
= GL_TRUE
;
877 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
878 if (!clip_span(ctx
, span
)) {
883 if (!(span
->arrayMask
& SPAN_MASK
)) {
884 /* post-clip sanity check */
885 assert(span
->x
>= 0);
886 assert(span
->y
>= 0);
889 /* Depth bounds test */
890 if (ctx
->Depth
.BoundsTest
&& fb
->Visual
.depthBits
> 0) {
891 if (!_swrast_depth_bounds_test(ctx
, span
)) {
897 /* Make sure all fragments are within window bounds */
898 if (span
->arrayMask
& SPAN_XY
) {
900 for (i
= 0; i
< span
->end
; i
++) {
901 if (span
->array
->mask
[i
]) {
902 assert(span
->array
->x
[i
] >= fb
->_Xmin
);
903 assert(span
->array
->x
[i
] < fb
->_Xmax
);
904 assert(span
->array
->y
[i
] >= fb
->_Ymin
);
905 assert(span
->array
->y
[i
] < fb
->_Ymax
);
911 /* Polygon Stippling */
912 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
913 stipple_polygon_span(ctx
, span
);
916 /* Stencil and Z testing */
917 if (ctx
->Stencil
._Enabled
|| ctx
->Depth
.Test
) {
918 if (!(span
->arrayMask
& SPAN_Z
))
919 _swrast_span_interpolate_z(ctx
, span
);
921 if (ctx
->Transform
.DepthClamp
)
922 _swrast_depth_clamp_span(ctx
, span
);
924 if (ctx
->Stencil
._Enabled
) {
925 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
926 span
->arrayMask
= origArrayMask
;
931 ASSERT(ctx
->Depth
.Test
);
932 if (!_swrast_depth_test_span(ctx
, span
)) {
933 span
->interpMask
= origInterpMask
;
934 span
->arrayMask
= origArrayMask
;
940 if (ctx
->Query
.CurrentOcclusionObject
) {
941 /* update count of 'passed' fragments */
942 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
944 for (i
= 0; i
< span
->end
; i
++)
945 q
->Result
+= span
->array
->mask
[i
];
948 /* we have to wait until after occlusion to do this test */
949 if (ctx
->Color
.IndexMask
== 0) {
950 /* write no pixels */
951 span
->arrayMask
= origArrayMask
;
955 /* Interpolate the color indexes if needed */
956 if (swrast
->_FogEnabled
||
957 ctx
->Color
.IndexLogicOpEnabled
||
958 ctx
->Color
.IndexMask
!= 0xffffffff ||
959 (span
->arrayMask
& SPAN_COVERAGE
)) {
960 if (!(span
->arrayMask
& SPAN_INDEX
) /*span->interpMask & SPAN_INDEX*/) {
961 interpolate_indexes(ctx
, span
);
966 if (swrast
->_FogEnabled
) {
967 _swrast_fog_ci_span(ctx
, span
);
970 /* Antialias coverage application */
971 if (span
->arrayMask
& SPAN_COVERAGE
) {
972 const GLfloat
*coverage
= span
->array
->coverage
;
973 GLuint
*index
= span
->array
->index
;
975 for (i
= 0; i
< span
->end
; i
++) {
976 ASSERT(coverage
[i
] < 16);
977 index
[i
] = (index
[i
] & ~0xf) | ((GLuint
) coverage
[i
]);
982 * Write to renderbuffers
985 const GLuint numBuffers
= fb
->_NumColorDrawBuffers
;
988 for (buf
= 0; buf
< numBuffers
; buf
++) {
989 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[buf
];
990 GLuint indexSave
[MAX_WIDTH
];
992 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
994 if (numBuffers
> 1) {
995 /* save indexes for second, third renderbuffer writes */
996 _mesa_memcpy(indexSave
, span
->array
->index
,
997 span
->end
* sizeof(indexSave
[0]));
1000 if (ctx
->Color
.IndexLogicOpEnabled
) {
1001 _swrast_logicop_ci_span(ctx
, rb
, span
);
1004 if (ctx
->Color
.IndexMask
!= 0xffffffff) {
1005 _swrast_mask_ci_span(ctx
, rb
, span
);
1008 if (!(span
->arrayMask
& SPAN_INDEX
) && span
->indexStep
== 0) {
1009 /* all fragments have same color index */
1015 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1016 index8
= FixedToInt(span
->index
);
1019 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1020 index16
= FixedToInt(span
->index
);
1024 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
1025 index32
= FixedToInt(span
->index
);
1029 if (span
->arrayMask
& SPAN_XY
) {
1030 rb
->PutMonoValues(ctx
, rb
, span
->end
, span
->array
->x
,
1031 span
->array
->y
, value
, span
->array
->mask
);
1034 rb
->PutMonoRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1035 value
, span
->array
->mask
);
1039 /* each fragment is a different color */
1040 GLubyte index8
[MAX_WIDTH
];
1041 GLushort index16
[MAX_WIDTH
];
1044 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1046 for (k
= 0; k
< span
->end
; k
++) {
1047 index8
[k
] = (GLubyte
) span
->array
->index
[k
];
1051 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1053 for (k
= 0; k
< span
->end
; k
++) {
1054 index16
[k
] = (GLushort
) span
->array
->index
[k
];
1059 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
1060 values
= span
->array
->index
;
1063 if (span
->arrayMask
& SPAN_XY
) {
1064 rb
->PutValues(ctx
, rb
, span
->end
,
1065 span
->array
->x
, span
->array
->y
,
1066 values
, span
->array
->mask
);
1069 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1070 values
, span
->array
->mask
);
1074 if (buf
+ 1 < numBuffers
) {
1075 /* restore original span values */
1076 _mesa_memcpy(span
->array
->index
, indexSave
,
1077 span
->end
* sizeof(indexSave
[0]));
1082 span
->interpMask
= origInterpMask
;
1083 span
->arrayMask
= origArrayMask
;
1088 * Add specular colors to primary colors.
1089 * Only called during fixed-function operation.
1090 * Result is float color array (FRAG_ATTRIB_COL0).
1093 add_specular(GLcontext
*ctx
, SWspan
*span
)
1095 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1096 const GLubyte
*mask
= span
->array
->mask
;
1097 GLfloat (*col0
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1098 GLfloat (*col1
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL1
];
1101 ASSERT(!ctx
->FragmentProgram
._Current
);
1102 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1103 ASSERT(swrast
->_ActiveAttribMask
& FRAG_BIT_COL1
);
1104 (void) swrast
; /* silence warning */
1106 if (span
->array
->ChanType
== GL_FLOAT
) {
1107 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1108 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL0
);
1112 /* need float colors */
1113 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1114 interpolate_float_colors(span
);
1118 if ((span
->arrayAttribs
& FRAG_BIT_COL1
) == 0) {
1119 /* XXX could avoid this and interpolate COL1 in the loop below */
1120 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL1
);
1123 ASSERT(span
->arrayAttribs
& FRAG_BIT_COL0
);
1124 ASSERT(span
->arrayAttribs
& FRAG_BIT_COL1
);
1126 for (i
= 0; i
< span
->end
; i
++) {
1128 col0
[i
][0] += col1
[i
][0];
1129 col0
[i
][1] += col1
[i
][1];
1130 col0
[i
][2] += col1
[i
][2];
1134 span
->array
->ChanType
= GL_FLOAT
;
1139 * Apply antialiasing coverage value to alpha values.
1142 apply_aa_coverage(SWspan
*span
)
1144 const GLfloat
*coverage
= span
->array
->coverage
;
1146 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1147 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
1148 for (i
= 0; i
< span
->end
; i
++) {
1149 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
1150 rgba
[i
][ACOMP
] = (GLubyte
) CLAMP(a
, 0.0, 255.0);
1151 ASSERT(coverage
[i
] >= 0.0);
1152 ASSERT(coverage
[i
] <= 1.0);
1155 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
1156 GLushort (*rgba
)[4] = span
->array
->rgba16
;
1157 for (i
= 0; i
< span
->end
; i
++) {
1158 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
1159 rgba
[i
][ACOMP
] = (GLushort
) CLAMP(a
, 0.0, 65535.0);
1163 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1164 for (i
= 0; i
< span
->end
; i
++) {
1165 rgba
[i
][ACOMP
] = rgba
[i
][ACOMP
] * coverage
[i
];
1173 * Clamp span's float colors to [0,1]
1176 clamp_colors(SWspan
*span
)
1178 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1180 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
1181 for (i
= 0; i
< span
->end
; i
++) {
1182 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0F
, 1.0F
);
1183 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0F
, 1.0F
);
1184 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0F
, 1.0F
);
1185 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0F
, 1.0F
);
1191 * Convert the span's color arrays to the given type.
1192 * The only way 'output' can be greater than zero is when we have a fragment
1193 * program that writes to gl_FragData[1] or higher.
1194 * \param output which fragment program color output is being processed
1197 convert_color_type(SWspan
*span
, GLenum newType
, GLuint output
)
1201 if (output
> 0 || span
->array
->ChanType
== GL_FLOAT
) {
1202 src
= span
->array
->attribs
[FRAG_ATTRIB_COL0
+ output
];
1203 span
->array
->ChanType
= GL_FLOAT
;
1205 else if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1206 src
= span
->array
->rgba8
;
1209 ASSERT(span
->array
->ChanType
== GL_UNSIGNED_SHORT
);
1210 src
= span
->array
->rgba16
;
1213 if (newType
== GL_UNSIGNED_BYTE
) {
1214 dst
= span
->array
->rgba8
;
1216 else if (newType
== GL_UNSIGNED_SHORT
) {
1217 dst
= span
->array
->rgba16
;
1220 dst
= span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1223 _mesa_convert_colors(span
->array
->ChanType
, src
,
1225 span
->end
, span
->array
->mask
);
1227 span
->array
->ChanType
= newType
;
1228 span
->array
->rgba
= dst
;
1234 * Apply fragment shader, fragment program or normal texturing to span.
1237 shade_texture_span(GLcontext
*ctx
, SWspan
*span
)
1239 GLbitfield inputsRead
;
1241 /* Determine which fragment attributes are actually needed */
1242 if (ctx
->FragmentProgram
._Current
) {
1243 inputsRead
= ctx
->FragmentProgram
._Current
->Base
.InputsRead
;
1246 /* XXX we could be a bit smarter about this */
1250 if (ctx
->FragmentProgram
._Current
||
1251 ctx
->ATIFragmentShader
._Enabled
) {
1252 /* programmable shading */
1253 if (span
->primitive
== GL_BITMAP
&& span
->array
->ChanType
!= GL_FLOAT
) {
1254 convert_color_type(span
, GL_FLOAT
, 0);
1256 if (span
->primitive
!= GL_POINT
||
1257 (span
->interpMask
& SPAN_RGBA
) ||
1258 ctx
->Point
.PointSprite
) {
1259 /* for single-pixel points, we populated the arrays already */
1260 interpolate_active_attribs(ctx
, span
, ~0);
1262 span
->array
->ChanType
= GL_FLOAT
;
1264 if (!(span
->arrayMask
& SPAN_Z
))
1265 _swrast_span_interpolate_z (ctx
, span
);
1268 if (inputsRead
& FRAG_BIT_WPOS
)
1270 /* XXX always interpolate wpos so that DDX/DDY work */
1272 interpolate_wpos(ctx
, span
);
1274 /* Run fragment program/shader now */
1275 if (ctx
->FragmentProgram
._Current
) {
1276 _swrast_exec_fragment_program(ctx
, span
);
1279 ASSERT(ctx
->ATIFragmentShader
._Enabled
);
1280 _swrast_exec_fragment_shader(ctx
, span
);
1283 else if (ctx
->Texture
._EnabledCoordUnits
) {
1284 /* conventional texturing */
1287 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1288 interpolate_int_colors(ctx
, span
);
1291 if (!(span
->arrayMask
& SPAN_RGBA
))
1292 interpolate_int_colors(ctx
, span
);
1294 if ((span
->arrayAttribs
& FRAG_BITS_TEX_ANY
) == 0x0)
1295 interpolate_texcoords(ctx
, span
);
1297 _swrast_texture_span(ctx
, span
);
1304 * Apply all the per-fragment operations to a span.
1305 * This now includes texturing (_swrast_write_texture_span() is history).
1306 * This function may modify any of the array values in the span.
1307 * span->interpMask and span->arrayMask may be changed but will be restored
1308 * to their original values before returning.
1311 _swrast_write_rgba_span( GLcontext
*ctx
, SWspan
*span
)
1313 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1314 const GLuint
*colorMask
= (GLuint
*) ctx
->Color
.ColorMask
;
1315 const GLbitfield origInterpMask
= span
->interpMask
;
1316 const GLbitfield origArrayMask
= span
->arrayMask
;
1317 const GLbitfield origArrayAttribs
= span
->arrayAttribs
;
1318 const GLenum origChanType
= span
->array
->ChanType
;
1319 void * const origRgba
= span
->array
->rgba
;
1320 const GLboolean shader
= (ctx
->FragmentProgram
._Current
1321 || ctx
->ATIFragmentShader
._Enabled
);
1322 const GLboolean shaderOrTexture
= shader
|| ctx
->Texture
._EnabledCoordUnits
;
1323 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1326 printf("%s() interp 0x%x array 0x%x\n", __FUNCTION__,
1327 span->interpMask, span->arrayMask);
1330 ASSERT(span
->primitive
== GL_POINT
||
1331 span
->primitive
== GL_LINE
||
1332 span
->primitive
== GL_POLYGON
||
1333 span
->primitive
== GL_BITMAP
);
1335 /* Fragment write masks */
1336 if (span
->arrayMask
& SPAN_MASK
) {
1337 /* mask was initialized by caller, probably glBitmap */
1338 span
->writeAll
= GL_FALSE
;
1341 _mesa_memset(span
->array
->mask
, 1, span
->end
);
1342 span
->writeAll
= GL_TRUE
;
1345 /* Clip to window/scissor box */
1346 if (!clip_span(ctx
, span
)) {
1350 ASSERT(span
->end
<= MAX_WIDTH
);
1352 /* Depth bounds test */
1353 if (ctx
->Depth
.BoundsTest
&& fb
->Visual
.depthBits
> 0) {
1354 if (!_swrast_depth_bounds_test(ctx
, span
)) {
1360 /* Make sure all fragments are within window bounds */
1361 if (span
->arrayMask
& SPAN_XY
) {
1362 /* array of pixel locations */
1364 for (i
= 0; i
< span
->end
; i
++) {
1365 if (span
->array
->mask
[i
]) {
1366 assert(span
->array
->x
[i
] >= fb
->_Xmin
);
1367 assert(span
->array
->x
[i
] < fb
->_Xmax
);
1368 assert(span
->array
->y
[i
] >= fb
->_Ymin
);
1369 assert(span
->array
->y
[i
] < fb
->_Ymax
);
1375 /* Polygon Stippling */
1376 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
1377 stipple_polygon_span(ctx
, span
);
1380 /* This is the normal place to compute the fragment color/Z
1381 * from texturing or shading.
1383 if (shaderOrTexture
&& !swrast
->_DeferredTexture
) {
1384 shade_texture_span(ctx
, span
);
1387 /* Do the alpha test */
1388 if (ctx
->Color
.AlphaEnabled
) {
1389 if (!_swrast_alpha_test(ctx
, span
)) {
1390 /* all fragments failed test */
1395 /* Stencil and Z testing */
1396 if (ctx
->Stencil
._Enabled
|| ctx
->Depth
.Test
) {
1397 if (!(span
->arrayMask
& SPAN_Z
))
1398 _swrast_span_interpolate_z(ctx
, span
);
1400 if (ctx
->Transform
.DepthClamp
)
1401 _swrast_depth_clamp_span(ctx
, span
);
1403 if (ctx
->Stencil
._Enabled
) {
1404 /* Combined Z/stencil tests */
1405 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
1406 /* all fragments failed test */
1410 else if (fb
->Visual
.depthBits
> 0) {
1411 /* Just regular depth testing */
1412 ASSERT(ctx
->Depth
.Test
);
1413 ASSERT(span
->arrayMask
& SPAN_Z
);
1414 if (!_swrast_depth_test_span(ctx
, span
)) {
1415 /* all fragments failed test */
1421 if (ctx
->Query
.CurrentOcclusionObject
) {
1422 /* update count of 'passed' fragments */
1423 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
1425 for (i
= 0; i
< span
->end
; i
++)
1426 q
->Result
+= span
->array
->mask
[i
];
1429 /* We had to wait until now to check for glColorMask(0,0,0,0) because of
1430 * the occlusion test.
1432 if (fb
->_NumColorDrawBuffers
== 1 && colorMask
[0] == 0x0) {
1433 /* no colors to write */
1437 /* If we were able to defer fragment color computation to now, there's
1438 * a good chance that many fragments will have already been killed by
1439 * Z/stencil testing.
1441 if (shaderOrTexture
&& swrast
->_DeferredTexture
) {
1442 shade_texture_span(ctx
, span
);
1446 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1447 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL0
);
1450 if ((span
->arrayMask
& SPAN_RGBA
) == 0) {
1451 interpolate_int_colors(ctx
, span
);
1455 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1457 if (span
->primitive
== GL_BITMAP
|| !swrast
->SpecularVertexAdd
) {
1458 /* Add primary and specular (diffuse + specular) colors */
1460 if (ctx
->Fog
.ColorSumEnabled
||
1461 (ctx
->Light
.Enabled
&&
1462 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)) {
1463 add_specular(ctx
, span
);
1469 if (swrast
->_FogEnabled
) {
1470 _swrast_fog_rgba_span(ctx
, span
);
1473 /* Antialias coverage application */
1474 if (span
->arrayMask
& SPAN_COVERAGE
) {
1475 apply_aa_coverage(span
);
1478 /* Clamp color/alpha values over the range [0.0, 1.0] before storage */
1479 if (ctx
->Color
.ClampFragmentColor
== GL_TRUE
&&
1480 span
->array
->ChanType
== GL_FLOAT
) {
1485 * Write to renderbuffers.
1486 * Depending on glDrawBuffer() state and the which color outputs are
1487 * written by the fragment shader, we may either replicate one color to
1488 * all renderbuffers or write a different color to each renderbuffer.
1489 * multiFragOutputs=TRUE for the later case.
1492 const GLuint numBuffers
= fb
->_NumColorDrawBuffers
;
1493 const struct gl_fragment_program
*fp
= ctx
->FragmentProgram
._Current
;
1494 const GLboolean multiFragOutputs
=
1495 (fp
&& fp
->Base
.OutputsWritten
>= (1 << FRAG_RESULT_DATA0
));
1498 for (buf
= 0; buf
< numBuffers
; buf
++) {
1499 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[buf
];
1501 /* color[fragOutput] will be written to buffer[buf] */
1504 GLchan rgbaSave
[MAX_WIDTH
][4];
1505 const GLuint fragOutput
= multiFragOutputs
? buf
: 0;
1507 if (rb
->DataType
!= span
->array
->ChanType
|| fragOutput
> 0) {
1508 convert_color_type(span
, rb
->DataType
, fragOutput
);
1511 if (!multiFragOutputs
&& numBuffers
> 1) {
1512 /* save colors for second, third renderbuffer writes */
1513 _mesa_memcpy(rgbaSave
, span
->array
->rgba
,
1514 4 * span
->end
* sizeof(GLchan
));
1517 ASSERT(rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
);
1519 if (ctx
->Color
._LogicOpEnabled
) {
1520 _swrast_logicop_rgba_span(ctx
, rb
, span
);
1522 else if ((ctx
->Color
.BlendEnabled
>> buf
) & 1) {
1523 _swrast_blend_span(ctx
, rb
, span
);
1526 if (colorMask
[buf
] != 0xffffffff) {
1527 _swrast_mask_rgba_span(ctx
, rb
, span
, buf
);
1530 if (span
->arrayMask
& SPAN_XY
) {
1531 /* array of pixel coords */
1532 ASSERT(rb
->PutValues
);
1533 rb
->PutValues(ctx
, rb
, span
->end
,
1534 span
->array
->x
, span
->array
->y
,
1535 span
->array
->rgba
, span
->array
->mask
);
1538 /* horizontal run of pixels */
1540 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1542 span
->writeAll
? NULL
: span
->array
->mask
);
1545 if (!multiFragOutputs
&& numBuffers
> 1) {
1546 /* restore original span values */
1547 _mesa_memcpy(span
->array
->rgba
, rgbaSave
,
1548 4 * span
->end
* sizeof(GLchan
));
1556 /* restore these values before returning */
1557 span
->interpMask
= origInterpMask
;
1558 span
->arrayMask
= origArrayMask
;
1559 span
->arrayAttribs
= origArrayAttribs
;
1560 span
->array
->ChanType
= origChanType
;
1561 span
->array
->rgba
= origRgba
;
1566 * Read RGBA pixels from a renderbuffer. Clipping will be done to prevent
1567 * reading ouside the buffer's boundaries.
1568 * \param dstType datatype for returned colors
1569 * \param rgba the returned colors
1572 _swrast_read_rgba_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1573 GLuint n
, GLint x
, GLint y
, GLenum dstType
,
1576 const GLint bufWidth
= (GLint
) rb
->Width
;
1577 const GLint bufHeight
= (GLint
) rb
->Height
;
1579 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1580 /* completely above, below, or right */
1581 /* XXX maybe leave rgba values undefined? */
1582 _mesa_bzero(rgba
, 4 * n
* sizeof(GLchan
));
1587 /* left edge clipping */
1589 length
= (GLint
) n
- skip
;
1591 /* completely left of window */
1594 if (length
> bufWidth
) {
1598 else if ((GLint
) (x
+ n
) > bufWidth
) {
1599 /* right edge clipping */
1601 length
= bufWidth
- x
;
1603 /* completely to right of window */
1615 ASSERT(rb
->_BaseFormat
== GL_RGB
|| rb
->_BaseFormat
== GL_RGBA
);
1617 if (rb
->DataType
== dstType
) {
1618 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
,
1619 (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(rb
->DataType
));
1622 GLuint temp
[MAX_WIDTH
* 4];
1623 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, temp
);
1624 _mesa_convert_colors(rb
->DataType
, temp
,
1625 dstType
, (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(dstType
),
1633 * Read CI pixels from a renderbuffer. Clipping will be done to prevent
1634 * reading ouside the buffer's boundaries.
1637 _swrast_read_index_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1638 GLuint n
, GLint x
, GLint y
, GLuint index
[] )
1640 const GLint bufWidth
= (GLint
) rb
->Width
;
1641 const GLint bufHeight
= (GLint
) rb
->Height
;
1643 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1644 /* completely above, below, or right */
1645 _mesa_bzero(index
, n
* sizeof(GLuint
));
1650 /* left edge clipping */
1652 length
= (GLint
) n
- skip
;
1654 /* completely left of window */
1657 if (length
> bufWidth
) {
1661 else if ((GLint
) (x
+ n
) > bufWidth
) {
1662 /* right edge clipping */
1664 length
= bufWidth
- x
;
1666 /* completely to right of window */
1677 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
1679 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1680 GLubyte index8
[MAX_WIDTH
];
1682 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index8
);
1683 for (i
= 0; i
< length
; i
++)
1684 index
[skip
+ i
] = index8
[i
];
1686 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1687 GLushort index16
[MAX_WIDTH
];
1689 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index16
);
1690 for (i
= 0; i
< length
; i
++)
1691 index
[skip
+ i
] = index16
[i
];
1693 else if (rb
->DataType
== GL_UNSIGNED_INT
) {
1694 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index
+ skip
);
1701 * Wrapper for gl_renderbuffer::GetValues() which does clipping to avoid
1702 * reading values outside the buffer bounds.
1703 * We can use this for reading any format/type of renderbuffer.
1704 * \param valueSize is the size in bytes of each value (pixel) put into the
1708 _swrast_get_values(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1709 GLuint count
, const GLint x
[], const GLint y
[],
1710 void *values
, GLuint valueSize
)
1712 GLuint i
, inCount
= 0, inStart
= 0;
1714 for (i
= 0; i
< count
; i
++) {
1715 if (x
[i
] >= 0 && y
[i
] >= 0 &&
1716 x
[i
] < (GLint
) rb
->Width
&& y
[i
] < (GLint
) rb
->Height
) {
1724 /* read [inStart, inStart + inCount) */
1725 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1726 (GLubyte
*) values
+ inStart
* valueSize
);
1732 /* read last values */
1733 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1734 (GLubyte
*) values
+ inStart
* valueSize
);
1740 * Wrapper for gl_renderbuffer::PutRow() which does clipping.
1741 * \param valueSize size of each value (pixel) in bytes
1744 _swrast_put_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1745 GLuint count
, GLint x
, GLint y
,
1746 const GLvoid
*values
, GLuint valueSize
)
1750 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1751 return; /* above or below */
1753 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1754 return; /* entirely left or right */
1756 if ((GLint
) (x
+ count
) > (GLint
) rb
->Width
) {
1758 GLint clip
= x
+ count
- rb
->Width
;
1769 rb
->PutRow(ctx
, rb
, count
, x
, y
,
1770 (const GLubyte
*) values
+ skip
* valueSize
, NULL
);
1775 * Wrapper for gl_renderbuffer::GetRow() which does clipping.
1776 * \param valueSize size of each value (pixel) in bytes
1779 _swrast_get_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1780 GLuint count
, GLint x
, GLint y
,
1781 GLvoid
*values
, GLuint valueSize
)
1785 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1786 return; /* above or below */
1788 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1789 return; /* entirely left or right */
1791 if (x
+ count
> rb
->Width
) {
1793 GLint clip
= x
+ count
- rb
->Width
;
1804 rb
->GetRow(ctx
, rb
, count
, x
, y
, (GLubyte
*) values
+ skip
* valueSize
);
1809 * Get RGBA pixels from the given renderbuffer.
1810 * Used by blending, logicop and masking functions.
1811 * \return pointer to the colors we read.
1814 _swrast_get_dest_rgba(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1817 const GLuint pixelSize
= RGBA_PIXEL_SIZE(span
->array
->ChanType
);
1820 /* Point rbPixels to a temporary space */
1821 rbPixels
= span
->array
->attribs
[FRAG_ATTRIB_MAX
- 1];
1823 /* Get destination values from renderbuffer */
1824 if (span
->arrayMask
& SPAN_XY
) {
1825 _swrast_get_values(ctx
, rb
, span
->end
, span
->array
->x
, span
->array
->y
,
1826 rbPixels
, pixelSize
);
1829 _swrast_get_row(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1830 rbPixels
, pixelSize
);