2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. 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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
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
35 #include "main/glheader.h"
36 #include "main/colormac.h"
37 #include "main/format_pack.h"
38 #include "main/format_unpack.h"
39 #include "main/macros.h"
40 #include "main/imports.h"
41 #include "main/image.h"
42 #include "main/samplerobj.h"
43 #include "main/teximage.h"
45 #include "s_atifragshader.h"
48 #include "s_context.h"
52 #include "s_masking.h"
53 #include "s_fragprog.h"
55 #include "s_stencil.h"
56 #include "s_texcombine.h"
61 * Set default fragment attributes for the span using the
62 * current raster values. Used prior to glDraw/CopyPixels
66 _swrast_span_default_attribs(struct gl_context
*ctx
, SWspan
*span
)
71 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
72 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16)
73 span
->z
= FloatToFixed(ctx
->Current
.RasterPos
[2] * depthMax
+ 0.5F
);
75 GLfloat tmpf
= ctx
->Current
.RasterPos
[2] * depthMax
;
76 tmpf
= MIN2(tmpf
, depthMax
);
77 span
->z
= (GLint
)tmpf
;
80 span
->interpMask
|= SPAN_Z
;
83 /* W (for perspective correction) */
84 span
->attrStart
[VARYING_SLOT_POS
][3] = 1.0;
85 span
->attrStepX
[VARYING_SLOT_POS
][3] = 0.0;
86 span
->attrStepY
[VARYING_SLOT_POS
][3] = 0.0;
88 /* primary color, or color index */
89 UNCLAMPED_FLOAT_TO_CHAN(r
, ctx
->Current
.RasterColor
[0]);
90 UNCLAMPED_FLOAT_TO_CHAN(g
, ctx
->Current
.RasterColor
[1]);
91 UNCLAMPED_FLOAT_TO_CHAN(b
, ctx
->Current
.RasterColor
[2]);
92 UNCLAMPED_FLOAT_TO_CHAN(a
, ctx
->Current
.RasterColor
[3]);
93 #if CHAN_TYPE == GL_FLOAT
99 span
->red
= IntToFixed(r
);
100 span
->green
= IntToFixed(g
);
101 span
->blue
= IntToFixed(b
);
102 span
->alpha
= IntToFixed(a
);
108 span
->interpMask
|= SPAN_RGBA
;
110 COPY_4V(span
->attrStart
[VARYING_SLOT_COL0
], ctx
->Current
.RasterColor
);
111 ASSIGN_4V(span
->attrStepX
[VARYING_SLOT_COL0
], 0.0, 0.0, 0.0, 0.0);
112 ASSIGN_4V(span
->attrStepY
[VARYING_SLOT_COL0
], 0.0, 0.0, 0.0, 0.0);
114 /* Secondary color */
115 if (ctx
->Light
.Enabled
|| ctx
->Fog
.ColorSumEnabled
)
117 COPY_4V(span
->attrStart
[VARYING_SLOT_COL1
], ctx
->Current
.RasterSecondaryColor
);
118 ASSIGN_4V(span
->attrStepX
[VARYING_SLOT_COL1
], 0.0, 0.0, 0.0, 0.0);
119 ASSIGN_4V(span
->attrStepY
[VARYING_SLOT_COL1
], 0.0, 0.0, 0.0, 0.0);
124 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
125 GLfloat fogVal
; /* a coord or a blend factor */
126 if (swrast
->_PreferPixelFog
) {
127 /* fog blend factors will be computed from fog coordinates per pixel */
128 fogVal
= ctx
->Current
.RasterDistance
;
131 /* fog blend factor should be computed from fogcoord now */
132 fogVal
= _swrast_z_to_fogfactor(ctx
, ctx
->Current
.RasterDistance
);
134 span
->attrStart
[VARYING_SLOT_FOGC
][0] = fogVal
;
135 span
->attrStepX
[VARYING_SLOT_FOGC
][0] = 0.0;
136 span
->attrStepY
[VARYING_SLOT_FOGC
][0] = 0.0;
142 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
143 const GLuint attr
= VARYING_SLOT_TEX0
+ i
;
144 const GLfloat
*tc
= ctx
->Current
.RasterTexCoords
[i
];
145 if (_swrast_use_fragment_program(ctx
) ||
146 ctx
->ATIFragmentShader
._Enabled
) {
147 COPY_4V(span
->attrStart
[attr
], tc
);
149 else if (tc
[3] > 0.0F
) {
150 /* use (s/q, t/q, r/q, 1) */
151 span
->attrStart
[attr
][0] = tc
[0] / tc
[3];
152 span
->attrStart
[attr
][1] = tc
[1] / tc
[3];
153 span
->attrStart
[attr
][2] = tc
[2] / tc
[3];
154 span
->attrStart
[attr
][3] = 1.0;
157 ASSIGN_4V(span
->attrStart
[attr
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
159 ASSIGN_4V(span
->attrStepX
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
160 ASSIGN_4V(span
->attrStepY
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
167 * Interpolate the active attributes (and'd with attrMask) to
168 * fill in span->array->attribs[].
169 * Perspective correction will be done. The point/line/triangle function
170 * should have computed attrStart/Step values for VARYING_SLOT_POS[3]!
173 interpolate_active_attribs(struct gl_context
*ctx
, SWspan
*span
,
174 GLbitfield64 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
& BITFIELD64_BIT(attr
)) {
186 const GLfloat dwdx
= span
->attrStepX
[VARYING_SLOT_POS
][3];
187 GLfloat w
= span
->attrStart
[VARYING_SLOT_POS
][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
& BITFIELD64_BIT(attr
)) == 0);
210 span
->arrayAttribs
|= BITFIELD64_BIT(attr
);
217 * Interpolate primary colors to fill in the span->array->rgba8 (or rgb16)
221 interpolate_int_colors(struct gl_context
*ctx
, SWspan
*span
)
224 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
, VARYING_BIT_COL0
);
310 _mesa_problem(ctx
, "bad datatype 0x%x in interpolate_int_colors",
311 span
->array
->ChanType
);
313 span
->arrayMask
|= SPAN_RGBA
;
318 * Populate the VARYING_SLOT_COL0 array.
321 interpolate_float_colors(SWspan
*span
)
323 GLfloat (*col0
)[4] = span
->array
->attribs
[VARYING_SLOT_COL0
];
324 const GLuint n
= span
->end
;
327 assert(!(span
->arrayAttribs
& VARYING_BIT_COL0
));
329 if (span
->arrayMask
& SPAN_RGBA
) {
330 /* convert array of int colors */
331 for (i
= 0; i
< n
; i
++) {
332 col0
[i
][0] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][0]);
333 col0
[i
][1] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][1]);
334 col0
[i
][2] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][2]);
335 col0
[i
][3] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][3]);
339 /* interpolate red/green/blue/alpha to get float colors */
340 assert(span
->interpMask
& SPAN_RGBA
);
341 if (span
->interpMask
& SPAN_FLAT
) {
342 GLfloat r
= FixedToFloat(span
->red
);
343 GLfloat g
= FixedToFloat(span
->green
);
344 GLfloat b
= FixedToFloat(span
->blue
);
345 GLfloat a
= FixedToFloat(span
->alpha
);
346 for (i
= 0; i
< n
; i
++) {
347 ASSIGN_4V(col0
[i
], r
, g
, b
, a
);
351 GLfloat r
= FixedToFloat(span
->red
);
352 GLfloat g
= FixedToFloat(span
->green
);
353 GLfloat b
= FixedToFloat(span
->blue
);
354 GLfloat a
= FixedToFloat(span
->alpha
);
355 GLfloat dr
= FixedToFloat(span
->redStep
);
356 GLfloat dg
= FixedToFloat(span
->greenStep
);
357 GLfloat db
= FixedToFloat(span
->blueStep
);
358 GLfloat da
= FixedToFloat(span
->alphaStep
);
359 for (i
= 0; i
< n
; i
++) {
372 span
->arrayAttribs
|= VARYING_BIT_COL0
;
373 span
->array
->ChanType
= GL_FLOAT
;
379 * Fill in the span.zArray array from the span->z, zStep values.
382 _swrast_span_interpolate_z( const struct gl_context
*ctx
, SWspan
*span
)
384 const GLuint n
= span
->end
;
387 assert(!(span
->arrayMask
& SPAN_Z
));
389 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
390 GLfixed zval
= span
->z
;
391 GLuint
*z
= span
->array
->z
;
392 for (i
= 0; i
< n
; i
++) {
393 z
[i
] = FixedToInt(zval
);
398 /* Deep Z buffer, no fixed->int shift */
399 GLuint zval
= span
->z
;
400 GLuint
*z
= span
->array
->z
;
401 for (i
= 0; i
< n
; i
++) {
406 span
->interpMask
&= ~SPAN_Z
;
407 span
->arrayMask
|= SPAN_Z
;
412 * Compute mipmap LOD from partial derivatives.
413 * This the ideal solution, as given in the OpenGL spec.
416 _swrast_compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
417 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
418 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
420 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
421 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
422 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
423 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
424 GLfloat x
= sqrtf(dudx
* dudx
+ dvdx
* dvdx
);
425 GLfloat y
= sqrtf(dudy
* dudy
+ dvdy
* dvdy
);
426 GLfloat rho
= MAX2(x
, y
);
427 GLfloat lambda
= LOG2(rho
);
433 * Compute mipmap LOD from partial derivatives.
434 * This is a faster approximation than above function.
438 _swrast_compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
439 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
440 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
442 GLfloat dsdx2
= (s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
;
443 GLfloat dtdx2
= (t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
;
444 GLfloat dsdy2
= (s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
;
445 GLfloat dtdy2
= (t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
;
446 GLfloat maxU
, maxV
, rho
, lambda
;
447 dsdx2
= fabsf(dsdx2
);
448 dsdy2
= fabsf(dsdy2
);
449 dtdx2
= fabsf(dtdx2
);
450 dtdy2
= fabsf(dtdy2
);
451 maxU
= MAX2(dsdx2
, dsdy2
) * texW
;
452 maxV
= MAX2(dtdx2
, dtdy2
) * texH
;
453 rho
= MAX2(maxU
, maxV
);
461 * Fill in the span.array->attrib[VARYING_SLOT_TEXn] arrays from the
462 * using the attrStart/Step values.
464 * This function only used during fixed-function fragment processing.
466 * Note: in the places where we divide by Q (or mult by invQ) we're
467 * really doing two things: perspective correction and texcoord
468 * projection. Remember, for texcoord (s,t,r,q) we need to index
469 * texels with (s/q, t/q, r/q).
472 interpolate_texcoords(struct gl_context
*ctx
, SWspan
*span
)
475 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
478 /* XXX CoordUnits vs. ImageUnits */
479 for (u
= 0; u
< maxUnit
; u
++) {
480 if (ctx
->Texture
._EnabledCoordUnits
& (1 << u
)) {
481 const GLuint attr
= VARYING_SLOT_TEX0
+ u
;
482 const struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[u
]._Current
;
484 GLboolean needLambda
;
485 GLfloat (*texcoord
)[4] = span
->array
->attribs
[attr
];
486 GLfloat
*lambda
= span
->array
->lambda
[u
];
487 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
488 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
489 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
490 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
491 const GLfloat drdx
= span
->attrStepX
[attr
][2];
492 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
493 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
494 GLfloat s
= span
->attrStart
[attr
][0] + span
->leftClip
* dsdx
;
495 GLfloat t
= span
->attrStart
[attr
][1] + span
->leftClip
* dtdx
;
496 GLfloat r
= span
->attrStart
[attr
][2] + span
->leftClip
* drdx
;
497 GLfloat q
= span
->attrStart
[attr
][3] + span
->leftClip
* dqdx
;
500 const struct gl_texture_image
*img
= _mesa_base_tex_image(obj
);
501 const struct swrast_texture_image
*swImg
=
502 swrast_texture_image_const(img
);
503 const struct gl_sampler_object
*samp
= _mesa_get_samplerobj(ctx
, u
);
505 needLambda
= (samp
->MinFilter
!= samp
->MagFilter
)
506 || _swrast_use_fragment_program(ctx
);
507 /* LOD is calculated directly in the ansiotropic filter, we can
508 * skip the normal lambda function as the result is ignored.
510 if (samp
->MaxAnisotropy
> 1.0 &&
511 samp
->MinFilter
== GL_LINEAR_MIPMAP_LINEAR
) {
512 needLambda
= GL_FALSE
;
514 texW
= swImg
->WidthScale
;
515 texH
= swImg
->HeightScale
;
518 /* using a fragment program */
521 needLambda
= GL_FALSE
;
526 if (_swrast_use_fragment_program(ctx
)
527 || ctx
->ATIFragmentShader
._Enabled
) {
528 /* do perspective correction but don't divide s, t, r by q */
529 const GLfloat dwdx
= span
->attrStepX
[VARYING_SLOT_POS
][3];
530 GLfloat w
= span
->attrStart
[VARYING_SLOT_POS
][3] + span
->leftClip
* dwdx
;
531 for (i
= 0; i
< span
->end
; i
++) {
532 const GLfloat invW
= 1.0F
/ w
;
533 texcoord
[i
][0] = s
* invW
;
534 texcoord
[i
][1] = t
* invW
;
535 texcoord
[i
][2] = r
* invW
;
536 texcoord
[i
][3] = q
* invW
;
537 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
538 dqdx
, dqdy
, texW
, texH
,
548 for (i
= 0; i
< span
->end
; i
++) {
549 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
550 texcoord
[i
][0] = s
* invQ
;
551 texcoord
[i
][1] = t
* invQ
;
552 texcoord
[i
][2] = r
* invQ
;
554 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
555 dqdx
, dqdy
, texW
, texH
,
563 span
->arrayMask
|= SPAN_LAMBDA
;
567 if (_swrast_use_fragment_program(ctx
) ||
568 ctx
->ATIFragmentShader
._Enabled
) {
569 /* do perspective correction but don't divide s, t, r by q */
570 const GLfloat dwdx
= span
->attrStepX
[VARYING_SLOT_POS
][3];
571 GLfloat w
= span
->attrStart
[VARYING_SLOT_POS
][3] + span
->leftClip
* dwdx
;
572 for (i
= 0; i
< span
->end
; i
++) {
573 const GLfloat invW
= 1.0F
/ w
;
574 texcoord
[i
][0] = s
* invW
;
575 texcoord
[i
][1] = t
* invW
;
576 texcoord
[i
][2] = r
* invW
;
577 texcoord
[i
][3] = q
* invW
;
586 else if (dqdx
== 0.0F
) {
587 /* Ortho projection or polygon's parallel to window X axis */
588 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
589 for (i
= 0; i
< span
->end
; i
++) {
590 texcoord
[i
][0] = s
* invQ
;
591 texcoord
[i
][1] = t
* invQ
;
592 texcoord
[i
][2] = r
* invQ
;
601 for (i
= 0; i
< span
->end
; i
++) {
602 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
603 texcoord
[i
][0] = s
* invQ
;
604 texcoord
[i
][1] = t
* invQ
;
605 texcoord
[i
][2] = r
* invQ
;
621 * Fill in the arrays->attribs[VARYING_SLOT_POS] array.
624 interpolate_wpos(struct gl_context
*ctx
, SWspan
*span
)
626 GLfloat (*wpos
)[4] = span
->array
->attribs
[VARYING_SLOT_POS
];
628 const GLfloat zScale
= 1.0F
/ ctx
->DrawBuffer
->_DepthMaxF
;
631 if (span
->arrayMask
& SPAN_XY
) {
632 for (i
= 0; i
< span
->end
; i
++) {
633 wpos
[i
][0] = (GLfloat
) span
->array
->x
[i
];
634 wpos
[i
][1] = (GLfloat
) span
->array
->y
[i
];
638 for (i
= 0; i
< span
->end
; i
++) {
639 wpos
[i
][0] = (GLfloat
) span
->x
+ i
;
640 wpos
[i
][1] = (GLfloat
) span
->y
;
644 dw
= span
->attrStepX
[VARYING_SLOT_POS
][3];
645 w
= span
->attrStart
[VARYING_SLOT_POS
][3] + span
->leftClip
* dw
;
646 for (i
= 0; i
< span
->end
; i
++) {
647 wpos
[i
][2] = (GLfloat
) span
->array
->z
[i
] * zScale
;
655 * Apply the current polygon stipple pattern to a span of pixels.
658 stipple_polygon_span(struct gl_context
*ctx
, SWspan
*span
)
660 GLubyte
*mask
= span
->array
->mask
;
662 assert(ctx
->Polygon
.StippleFlag
);
664 if (span
->arrayMask
& SPAN_XY
) {
665 /* arrays of x/y pixel coords */
667 for (i
= 0; i
< span
->end
; i
++) {
668 const GLint col
= span
->array
->x
[i
] % 32;
669 const GLint row
= span
->array
->y
[i
] % 32;
670 const GLuint stipple
= ctx
->PolygonStipple
[row
];
671 if (((1 << col
) & stipple
) == 0) {
677 /* horizontal span of pixels */
678 const GLuint highBit
= 1 << 31;
679 const GLuint stipple
= ctx
->PolygonStipple
[span
->y
% 32];
680 GLuint i
, m
= highBit
>> (GLuint
) (span
->x
% 32);
681 for (i
= 0; i
< span
->end
; i
++) {
682 if ((m
& stipple
) == 0) {
691 span
->writeAll
= GL_FALSE
;
696 * Clip a pixel span to the current buffer/window boundaries:
697 * DrawBuffer->_Xmin, _Xmax, _Ymin, _Ymax. This will accomplish
698 * window clipping and scissoring.
699 * Return: GL_TRUE some pixels still visible
700 * GL_FALSE nothing visible
703 clip_span( struct gl_context
*ctx
, SWspan
*span
)
705 const GLint xmin
= ctx
->DrawBuffer
->_Xmin
;
706 const GLint xmax
= ctx
->DrawBuffer
->_Xmax
;
707 const GLint ymin
= ctx
->DrawBuffer
->_Ymin
;
708 const GLint ymax
= ctx
->DrawBuffer
->_Ymax
;
712 if (span
->arrayMask
& SPAN_XY
) {
713 /* arrays of x/y pixel coords */
714 const GLint
*x
= span
->array
->x
;
715 const GLint
*y
= span
->array
->y
;
716 const GLint n
= span
->end
;
717 GLubyte
*mask
= span
->array
->mask
;
720 if (span
->arrayMask
& SPAN_MASK
) {
721 /* note: using & intead of && to reduce branches */
722 for (i
= 0; i
< n
; i
++) {
723 mask
[i
] &= (x
[i
] >= xmin
) & (x
[i
] < xmax
)
724 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
729 /* note: using & intead of && to reduce branches */
730 for (i
= 0; i
< n
; i
++) {
731 mask
[i
] = (x
[i
] >= xmin
) & (x
[i
] < xmax
)
732 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
739 /* horizontal span of pixels */
740 const GLint x
= span
->x
;
741 const GLint y
= span
->y
;
744 /* Trivial rejection tests */
745 if (y
< ymin
|| y
>= ymax
|| x
+ n
<= xmin
|| x
>= xmax
) {
747 return GL_FALSE
; /* all pixels clipped */
753 n
= span
->end
= xmax
- x
;
756 /* Clip to the left */
758 const GLint leftClip
= xmin
- x
;
761 assert(leftClip
> 0);
762 assert(x
+ n
> xmin
);
764 /* Clip 'leftClip' pixels from the left side.
765 * The span->leftClip field will be applied when we interpolate
766 * fragment attributes.
767 * For arrays of values, shift them left.
769 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
770 if (span
->interpMask
& (1 << i
)) {
772 for (j
= 0; j
< 4; j
++) {
773 span
->attrStart
[i
][j
] += leftClip
* span
->attrStepX
[i
][j
];
778 span
->red
+= leftClip
* span
->redStep
;
779 span
->green
+= leftClip
* span
->greenStep
;
780 span
->blue
+= leftClip
* span
->blueStep
;
781 span
->alpha
+= leftClip
* span
->alphaStep
;
782 span
->index
+= leftClip
* span
->indexStep
;
783 span
->z
+= leftClip
* span
->zStep
;
784 span
->intTex
[0] += leftClip
* span
->intTexStep
[0];
785 span
->intTex
[1] += leftClip
* span
->intTexStep
[1];
787 #define SHIFT_ARRAY(ARRAY, SHIFT, LEN) \
788 memmove(ARRAY, ARRAY + (SHIFT), (LEN) * sizeof(ARRAY[0]))
790 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
791 if (span
->arrayAttribs
& BITFIELD64_BIT(i
)) {
792 /* shift array elements left by 'leftClip' */
793 SHIFT_ARRAY(span
->array
->attribs
[i
], leftClip
, n
- leftClip
);
797 SHIFT_ARRAY(span
->array
->mask
, leftClip
, n
- leftClip
);
798 SHIFT_ARRAY(span
->array
->rgba8
, leftClip
, n
- leftClip
);
799 SHIFT_ARRAY(span
->array
->rgba16
, leftClip
, n
- leftClip
);
800 SHIFT_ARRAY(span
->array
->x
, leftClip
, n
- leftClip
);
801 SHIFT_ARRAY(span
->array
->y
, leftClip
, n
- leftClip
);
802 SHIFT_ARRAY(span
->array
->z
, leftClip
, n
- leftClip
);
803 SHIFT_ARRAY(span
->array
->index
, leftClip
, n
- leftClip
);
804 for (i
= 0; i
< MAX_TEXTURE_COORD_UNITS
; i
++) {
805 SHIFT_ARRAY(span
->array
->lambda
[i
], leftClip
, n
- leftClip
);
807 SHIFT_ARRAY(span
->array
->coverage
, leftClip
, n
- leftClip
);
811 span
->leftClip
= leftClip
;
813 span
->end
-= leftClip
;
814 span
->writeAll
= GL_FALSE
;
817 assert(span
->x
>= xmin
);
818 assert(span
->x
+ span
->end
<= xmax
);
819 assert(span
->y
>= ymin
);
820 assert(span
->y
< ymax
);
822 return GL_TRUE
; /* some pixels visible */
828 * Add specular colors to primary colors.
829 * Only called during fixed-function operation.
830 * Result is float color array (VARYING_SLOT_COL0).
833 add_specular(struct gl_context
*ctx
, SWspan
*span
)
835 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
836 const GLubyte
*mask
= span
->array
->mask
;
837 GLfloat (*col0
)[4] = span
->array
->attribs
[VARYING_SLOT_COL0
];
838 GLfloat (*col1
)[4] = span
->array
->attribs
[VARYING_SLOT_COL1
];
841 assert(!_swrast_use_fragment_program(ctx
));
842 assert(span
->arrayMask
& SPAN_RGBA
);
843 assert(swrast
->_ActiveAttribMask
& VARYING_BIT_COL1
);
844 (void) swrast
; /* silence warning */
846 if (span
->array
->ChanType
== GL_FLOAT
) {
847 if ((span
->arrayAttribs
& VARYING_BIT_COL0
) == 0) {
848 interpolate_active_attribs(ctx
, span
, VARYING_BIT_COL0
);
852 /* need float colors */
853 if ((span
->arrayAttribs
& VARYING_BIT_COL0
) == 0) {
854 interpolate_float_colors(span
);
858 if ((span
->arrayAttribs
& VARYING_BIT_COL1
) == 0) {
859 /* XXX could avoid this and interpolate COL1 in the loop below */
860 interpolate_active_attribs(ctx
, span
, VARYING_BIT_COL1
);
863 assert(span
->arrayAttribs
& VARYING_BIT_COL0
);
864 assert(span
->arrayAttribs
& VARYING_BIT_COL1
);
866 for (i
= 0; i
< span
->end
; i
++) {
868 col0
[i
][0] += col1
[i
][0];
869 col0
[i
][1] += col1
[i
][1];
870 col0
[i
][2] += col1
[i
][2];
874 span
->array
->ChanType
= GL_FLOAT
;
879 * Apply antialiasing coverage value to alpha values.
882 apply_aa_coverage(SWspan
*span
)
884 const GLfloat
*coverage
= span
->array
->coverage
;
886 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
887 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
888 for (i
= 0; i
< span
->end
; i
++) {
889 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
890 rgba
[i
][ACOMP
] = (GLubyte
) CLAMP(a
, 0.0, 255.0);
891 assert(coverage
[i
] >= 0.0);
892 assert(coverage
[i
] <= 1.0);
895 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
896 GLushort (*rgba
)[4] = span
->array
->rgba16
;
897 for (i
= 0; i
< span
->end
; i
++) {
898 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
899 rgba
[i
][ACOMP
] = (GLushort
) CLAMP(a
, 0.0, 65535.0);
903 GLfloat (*rgba
)[4] = span
->array
->attribs
[VARYING_SLOT_COL0
];
904 for (i
= 0; i
< span
->end
; i
++) {
905 rgba
[i
][ACOMP
] = rgba
[i
][ACOMP
] * coverage
[i
];
913 * Clamp span's float colors to [0,1]
916 clamp_colors(SWspan
*span
)
918 GLfloat (*rgba
)[4] = span
->array
->attribs
[VARYING_SLOT_COL0
];
920 assert(span
->array
->ChanType
== GL_FLOAT
);
921 for (i
= 0; i
< span
->end
; i
++) {
922 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0F
, 1.0F
);
923 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0F
, 1.0F
);
924 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0F
, 1.0F
);
925 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0F
, 1.0F
);
931 * Convert the span's color arrays to the given type.
932 * The only way 'output' can be greater than zero is when we have a fragment
933 * program that writes to gl_FragData[1] or higher.
934 * \param output which fragment program color output is being processed
937 convert_color_type(SWspan
*span
, GLenum srcType
, GLenum newType
, GLuint output
)
941 if (output
> 0 || srcType
== GL_FLOAT
) {
942 src
= span
->array
->attribs
[VARYING_SLOT_COL0
+ output
];
943 span
->array
->ChanType
= GL_FLOAT
;
945 else if (srcType
== GL_UNSIGNED_BYTE
) {
946 src
= span
->array
->rgba8
;
949 assert(srcType
== GL_UNSIGNED_SHORT
);
950 src
= span
->array
->rgba16
;
953 if (newType
== GL_UNSIGNED_BYTE
) {
954 dst
= span
->array
->rgba8
;
956 else if (newType
== GL_UNSIGNED_SHORT
) {
957 dst
= span
->array
->rgba16
;
960 dst
= span
->array
->attribs
[VARYING_SLOT_COL0
];
963 _mesa_convert_colors(span
->array
->ChanType
, src
,
965 span
->end
, span
->array
->mask
);
967 span
->array
->ChanType
= newType
;
968 span
->array
->rgba
= dst
;
974 * Apply fragment shader, fragment program or normal texturing to span.
977 shade_texture_span(struct gl_context
*ctx
, SWspan
*span
)
979 if (_swrast_use_fragment_program(ctx
) ||
980 ctx
->ATIFragmentShader
._Enabled
) {
981 /* programmable shading */
982 if (span
->primitive
== GL_BITMAP
&& span
->array
->ChanType
!= GL_FLOAT
) {
983 convert_color_type(span
, span
->array
->ChanType
, GL_FLOAT
, 0);
986 span
->array
->rgba
= (void *) span
->array
->attribs
[VARYING_SLOT_COL0
];
989 if (span
->primitive
!= GL_POINT
||
990 (span
->interpMask
& SPAN_RGBA
) ||
991 ctx
->Point
.PointSprite
) {
992 /* for single-pixel points, we populated the arrays already */
993 interpolate_active_attribs(ctx
, span
, ~0);
995 span
->array
->ChanType
= GL_FLOAT
;
997 if (!(span
->arrayMask
& SPAN_Z
))
998 _swrast_span_interpolate_z (ctx
, span
);
1001 if (inputsRead
& VARYING_BIT_POS
)
1003 /* XXX always interpolate wpos so that DDX/DDY work */
1005 interpolate_wpos(ctx
, span
);
1007 /* Run fragment program/shader now */
1008 if (_swrast_use_fragment_program(ctx
)) {
1009 _swrast_exec_fragment_program(ctx
, span
);
1012 assert(ctx
->ATIFragmentShader
._Enabled
);
1013 _swrast_exec_fragment_shader(ctx
, span
);
1016 else if (ctx
->Texture
._EnabledCoordUnits
) {
1017 /* conventional texturing */
1020 if ((span
->arrayAttribs
& VARYING_BIT_COL0
) == 0) {
1021 interpolate_int_colors(ctx
, span
);
1024 if (!(span
->arrayMask
& SPAN_RGBA
))
1025 interpolate_int_colors(ctx
, span
);
1027 if ((span
->arrayAttribs
& VARYING_BITS_TEX_ANY
) == 0x0)
1028 interpolate_texcoords(ctx
, span
);
1030 _swrast_texture_span(ctx
, span
);
1035 /** Put colors at x/y locations into a renderbuffer */
1037 put_values(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1039 GLuint count
, const GLint x
[], const GLint y
[],
1040 const void *values
, const GLubyte
*mask
)
1042 gl_pack_ubyte_rgba_func pack_ubyte
= NULL
;
1043 gl_pack_float_rgba_func pack_float
= NULL
;
1046 if (datatype
== GL_UNSIGNED_BYTE
)
1047 pack_ubyte
= _mesa_get_pack_ubyte_rgba_function(rb
->Format
);
1049 pack_float
= _mesa_get_pack_float_rgba_function(rb
->Format
);
1051 for (i
= 0; i
< count
; i
++) {
1053 GLubyte
*dst
= _swrast_pixel_address(rb
, x
[i
], y
[i
]);
1055 if (datatype
== GL_UNSIGNED_BYTE
) {
1056 pack_ubyte((const GLubyte
*) values
+ 4 * i
, dst
);
1059 assert(datatype
== GL_FLOAT
);
1060 pack_float((const GLfloat
*) values
+ 4 * i
, dst
);
1067 /** Put row of colors into renderbuffer */
1069 _swrast_put_row(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1071 GLuint count
, GLint x
, GLint y
,
1072 const void *values
, const GLubyte
*mask
)
1074 GLubyte
*dst
= _swrast_pixel_address(rb
, x
, y
);
1077 if (datatype
== GL_UNSIGNED_BYTE
) {
1078 _mesa_pack_ubyte_rgba_row(rb
->Format
, count
,
1079 (const GLubyte (*)[4]) values
, dst
);
1082 assert(datatype
== GL_FLOAT
);
1083 _mesa_pack_float_rgba_row(rb
->Format
, count
,
1084 (const GLfloat (*)[4]) values
, dst
);
1088 const GLuint bpp
= _mesa_get_format_bytes(rb
->Format
);
1089 GLuint i
, runLen
, runStart
;
1090 /* We can't pass a 'mask' array to the _mesa_pack_rgba_row() functions
1091 * so look for runs where mask=1...
1093 runLen
= runStart
= 0;
1094 for (i
= 0; i
< count
; i
++) {
1101 if (!mask
[i
] || i
== count
- 1) {
1102 /* might be the end of a run of pixels */
1104 if (datatype
== GL_UNSIGNED_BYTE
) {
1105 _mesa_pack_ubyte_rgba_row(rb
->Format
, runLen
,
1106 (const GLubyte (*)[4]) values
+ runStart
,
1107 dst
+ runStart
* bpp
);
1110 assert(datatype
== GL_FLOAT
);
1111 _mesa_pack_float_rgba_row(rb
->Format
, runLen
,
1112 (const GLfloat (*)[4]) values
+ runStart
,
1113 dst
+ runStart
* bpp
);
1125 * Apply all the per-fragment operations to a span.
1126 * This now includes texturing (_swrast_write_texture_span() is history).
1127 * This function may modify any of the array values in the span.
1128 * span->interpMask and span->arrayMask may be changed but will be restored
1129 * to their original values before returning.
1132 _swrast_write_rgba_span( struct gl_context
*ctx
, SWspan
*span
)
1134 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1135 const GLuint
*colorMask
= (GLuint
*) ctx
->Color
.ColorMask
;
1136 const GLbitfield origInterpMask
= span
->interpMask
;
1137 const GLbitfield origArrayMask
= span
->arrayMask
;
1138 const GLbitfield64 origArrayAttribs
= span
->arrayAttribs
;
1139 const GLenum origChanType
= span
->array
->ChanType
;
1140 void * const origRgba
= span
->array
->rgba
;
1141 const GLboolean shader
= (_swrast_use_fragment_program(ctx
)
1142 || ctx
->ATIFragmentShader
._Enabled
);
1143 const GLboolean shaderOrTexture
= shader
|| ctx
->Texture
._EnabledCoordUnits
;
1144 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1147 printf("%s() interp 0x%x array 0x%x\n", __FUNCTION__,
1148 span->interpMask, span->arrayMask);
1151 assert(span
->primitive
== GL_POINT
||
1152 span
->primitive
== GL_LINE
||
1153 span
->primitive
== GL_POLYGON
||
1154 span
->primitive
== GL_BITMAP
);
1156 /* Fragment write masks */
1157 if (span
->arrayMask
& SPAN_MASK
) {
1158 /* mask was initialized by caller, probably glBitmap */
1159 span
->writeAll
= GL_FALSE
;
1162 memset(span
->array
->mask
, 1, span
->end
);
1163 span
->writeAll
= GL_TRUE
;
1166 /* Clip to window/scissor box */
1167 if (!clip_span(ctx
, span
)) {
1171 assert(span
->end
<= SWRAST_MAX_WIDTH
);
1173 /* Depth bounds test */
1174 if (ctx
->Depth
.BoundsTest
&& fb
->Visual
.depthBits
> 0) {
1175 if (!_swrast_depth_bounds_test(ctx
, span
)) {
1181 /* Make sure all fragments are within window bounds */
1182 if (span
->arrayMask
& SPAN_XY
) {
1183 /* array of pixel locations */
1185 for (i
= 0; i
< span
->end
; i
++) {
1186 if (span
->array
->mask
[i
]) {
1187 assert(span
->array
->x
[i
] >= fb
->_Xmin
);
1188 assert(span
->array
->x
[i
] < fb
->_Xmax
);
1189 assert(span
->array
->y
[i
] >= fb
->_Ymin
);
1190 assert(span
->array
->y
[i
] < fb
->_Ymax
);
1196 /* Polygon Stippling */
1197 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
1198 stipple_polygon_span(ctx
, span
);
1201 /* This is the normal place to compute the fragment color/Z
1202 * from texturing or shading.
1204 if (shaderOrTexture
&& !swrast
->_DeferredTexture
) {
1205 shade_texture_span(ctx
, span
);
1208 /* Do the alpha test */
1209 if (ctx
->Color
.AlphaEnabled
) {
1210 if (!_swrast_alpha_test(ctx
, span
)) {
1211 /* all fragments failed test */
1216 /* Stencil and Z testing */
1217 if (ctx
->Stencil
._Enabled
|| ctx
->Depth
.Test
) {
1218 if (!(span
->arrayMask
& SPAN_Z
))
1219 _swrast_span_interpolate_z(ctx
, span
);
1221 if (ctx
->Transform
.DepthClamp
)
1222 _swrast_depth_clamp_span(ctx
, span
);
1224 if (ctx
->Stencil
._Enabled
) {
1225 /* Combined Z/stencil tests */
1226 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
1227 /* all fragments failed test */
1231 else if (fb
->Visual
.depthBits
> 0) {
1232 /* Just regular depth testing */
1233 assert(ctx
->Depth
.Test
);
1234 assert(span
->arrayMask
& SPAN_Z
);
1235 if (!_swrast_depth_test_span(ctx
, span
)) {
1236 /* all fragments failed test */
1242 if (ctx
->Query
.CurrentOcclusionObject
) {
1243 /* update count of 'passed' fragments */
1244 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
1246 for (i
= 0; i
< span
->end
; i
++)
1247 q
->Result
+= span
->array
->mask
[i
];
1250 /* We had to wait until now to check for glColorMask(0,0,0,0) because of
1251 * the occlusion test.
1253 if (fb
->_NumColorDrawBuffers
== 1 && colorMask
[0] == 0x0) {
1254 /* no colors to write */
1258 /* If we were able to defer fragment color computation to now, there's
1259 * a good chance that many fragments will have already been killed by
1260 * Z/stencil testing.
1262 if (shaderOrTexture
&& swrast
->_DeferredTexture
) {
1263 shade_texture_span(ctx
, span
);
1267 if ((span
->arrayAttribs
& VARYING_BIT_COL0
) == 0) {
1268 interpolate_active_attribs(ctx
, span
, VARYING_BIT_COL0
);
1271 if ((span
->arrayMask
& SPAN_RGBA
) == 0) {
1272 interpolate_int_colors(ctx
, span
);
1276 assert(span
->arrayMask
& SPAN_RGBA
);
1278 if (span
->primitive
== GL_BITMAP
|| !swrast
->SpecularVertexAdd
) {
1279 /* Add primary and specular (diffuse + specular) colors */
1281 if (ctx
->Fog
.ColorSumEnabled
||
1282 (ctx
->Light
.Enabled
&&
1283 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)) {
1284 add_specular(ctx
, span
);
1290 if (swrast
->_FogEnabled
) {
1291 _swrast_fog_rgba_span(ctx
, span
);
1294 /* Antialias coverage application */
1295 if (span
->arrayMask
& SPAN_COVERAGE
) {
1296 apply_aa_coverage(span
);
1299 /* Clamp color/alpha values over the range [0.0, 1.0] before storage */
1300 if (ctx
->Color
.ClampFragmentColor
== GL_TRUE
&&
1301 span
->array
->ChanType
== GL_FLOAT
) {
1306 * Write to renderbuffers.
1307 * Depending on glDrawBuffer() state and the which color outputs are
1308 * written by the fragment shader, we may either replicate one color to
1309 * all renderbuffers or write a different color to each renderbuffer.
1310 * multiFragOutputs=TRUE for the later case.
1313 const GLuint numBuffers
= fb
->_NumColorDrawBuffers
;
1314 const struct gl_fragment_program
*fp
= ctx
->FragmentProgram
._Current
;
1315 const GLboolean multiFragOutputs
=
1316 _swrast_use_fragment_program(ctx
)
1317 && fp
->Base
.OutputsWritten
>= (1 << FRAG_RESULT_DATA0
);
1318 /* Save srcColorType because convert_color_type() can change it */
1319 const GLenum srcColorType
= span
->array
->ChanType
;
1322 for (buf
= 0; buf
< numBuffers
; buf
++) {
1323 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[buf
];
1325 /* color[fragOutput] will be written to buffer[buf] */
1328 /* re-use one of the attribute array buffers for rgbaSave */
1329 GLchan (*rgbaSave
)[4] = (GLchan (*)[4]) span
->array
->attribs
[0];
1330 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
1331 const GLenum dstColorType
= srb
->ColorType
;
1333 assert(dstColorType
== GL_UNSIGNED_BYTE
||
1334 dstColorType
== GL_FLOAT
);
1336 /* set span->array->rgba to colors for renderbuffer's datatype */
1337 if (srcColorType
!= dstColorType
) {
1338 convert_color_type(span
, srcColorType
, dstColorType
,
1339 multiFragOutputs
? buf
: 0);
1342 if (srcColorType
== GL_UNSIGNED_BYTE
) {
1343 span
->array
->rgba
= span
->array
->rgba8
;
1346 span
->array
->rgba
= (void *)
1347 span
->array
->attribs
[VARYING_SLOT_COL0
];
1351 if (!multiFragOutputs
&& numBuffers
> 1) {
1352 /* save colors for second, third renderbuffer writes */
1353 memcpy(rgbaSave
, span
->array
->rgba
,
1354 4 * span
->end
* sizeof(GLchan
));
1357 assert(rb
->_BaseFormat
== GL_RGBA
||
1358 rb
->_BaseFormat
== GL_RGB
||
1359 rb
->_BaseFormat
== GL_RED
||
1360 rb
->_BaseFormat
== GL_RG
||
1361 rb
->_BaseFormat
== GL_ALPHA
);
1363 if (ctx
->Color
.ColorLogicOpEnabled
) {
1364 _swrast_logicop_rgba_span(ctx
, rb
, span
);
1366 else if ((ctx
->Color
.BlendEnabled
>> buf
) & 1) {
1367 _swrast_blend_span(ctx
, rb
, span
);
1370 if (colorMask
[buf
] != 0xffffffff) {
1371 _swrast_mask_rgba_span(ctx
, rb
, span
, buf
);
1374 if (span
->arrayMask
& SPAN_XY
) {
1375 /* array of pixel coords */
1377 span
->array
->ChanType
, span
->end
,
1378 span
->array
->x
, span
->array
->y
,
1379 span
->array
->rgba
, span
->array
->mask
);
1382 /* horizontal run of pixels */
1383 _swrast_put_row(ctx
, rb
,
1384 span
->array
->ChanType
,
1385 span
->end
, span
->x
, span
->y
,
1387 span
->writeAll
? NULL
: span
->array
->mask
);
1390 if (!multiFragOutputs
&& numBuffers
> 1) {
1391 /* restore original span values */
1392 memcpy(span
->array
->rgba
, rgbaSave
,
1393 4 * span
->end
* sizeof(GLchan
));
1401 /* restore these values before returning */
1402 span
->interpMask
= origInterpMask
;
1403 span
->arrayMask
= origArrayMask
;
1404 span
->arrayAttribs
= origArrayAttribs
;
1405 span
->array
->ChanType
= origChanType
;
1406 span
->array
->rgba
= origRgba
;
1411 * Read float RGBA pixels from a renderbuffer. Clipping will be done to
1412 * prevent reading ouside the buffer's boundaries.
1413 * \param rgba the returned colors
1416 _swrast_read_rgba_span( struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1417 GLuint n
, GLint x
, GLint y
,
1420 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
1421 GLenum dstType
= GL_FLOAT
;
1422 const GLint bufWidth
= (GLint
) rb
->Width
;
1423 const GLint bufHeight
= (GLint
) rb
->Height
;
1425 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1426 /* completely above, below, or right */
1427 /* XXX maybe leave rgba values undefined? */
1428 memset(rgba
, 0, 4 * n
* sizeof(GLchan
));
1435 /* left edge clipping */
1437 length
= (GLint
) n
- skip
;
1439 /* completely left of window */
1442 if (length
> bufWidth
) {
1446 else if ((GLint
) (x
+ n
) > bufWidth
) {
1447 /* right edge clipping */
1449 length
= bufWidth
- x
;
1451 /* completely to right of window */
1462 assert(rb
->_BaseFormat
== GL_RGBA
||
1463 rb
->_BaseFormat
== GL_RGB
||
1464 rb
->_BaseFormat
== GL_RG
||
1465 rb
->_BaseFormat
== GL_RED
||
1466 rb
->_BaseFormat
== GL_LUMINANCE
||
1467 rb
->_BaseFormat
== GL_INTENSITY
||
1468 rb
->_BaseFormat
== GL_LUMINANCE_ALPHA
||
1469 rb
->_BaseFormat
== GL_ALPHA
);
1472 (void) srb
; /* silence unused var warning */
1474 src
= _swrast_pixel_address(rb
, x
+ skip
, y
);
1476 if (dstType
== GL_UNSIGNED_BYTE
) {
1477 _mesa_unpack_ubyte_rgba_row(rb
->Format
, length
, src
,
1478 (GLubyte (*)[4]) rgba
+ skip
);
1480 else if (dstType
== GL_FLOAT
) {
1481 _mesa_unpack_rgba_row(rb
->Format
, length
, src
,
1482 (GLfloat (*)[4]) rgba
+ skip
);
1485 _mesa_problem(ctx
, "unexpected type in _swrast_read_rgba_span()");
1492 * Get colors at x/y positions with clipping.
1493 * \param type type of values to return
1496 get_values(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1497 GLuint count
, const GLint x
[], const GLint y
[],
1498 void *values
, GLenum type
)
1502 for (i
= 0; i
< count
; i
++) {
1503 if (x
[i
] >= 0 && y
[i
] >= 0 &&
1504 x
[i
] < (GLint
) rb
->Width
&& y
[i
] < (GLint
) rb
->Height
) {
1506 const GLubyte
*src
= _swrast_pixel_address(rb
, x
[i
], y
[i
]);
1508 if (type
== GL_UNSIGNED_BYTE
) {
1509 _mesa_unpack_ubyte_rgba_row(rb
->Format
, 1, src
,
1510 (GLubyte (*)[4]) values
+ i
);
1512 else if (type
== GL_FLOAT
) {
1513 _mesa_unpack_rgba_row(rb
->Format
, 1, src
,
1514 (GLfloat (*)[4]) values
+ i
);
1517 _mesa_problem(ctx
, "unexpected type in get_values()");
1525 * Get row of colors with clipping.
1526 * \param type type of values to return
1529 get_row(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1530 GLuint count
, GLint x
, GLint y
,
1531 GLvoid
*values
, GLenum type
)
1536 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1537 return; /* above or below */
1539 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1540 return; /* entirely left or right */
1542 if (x
+ count
> rb
->Width
) {
1544 GLint clip
= x
+ count
- rb
->Width
;
1555 src
= _swrast_pixel_address(rb
, x
, y
);
1557 if (type
== GL_UNSIGNED_BYTE
) {
1558 _mesa_unpack_ubyte_rgba_row(rb
->Format
, count
, src
,
1559 (GLubyte (*)[4]) values
+ skip
);
1561 else if (type
== GL_FLOAT
) {
1562 _mesa_unpack_rgba_row(rb
->Format
, count
, src
,
1563 (GLfloat (*)[4]) values
+ skip
);
1566 _mesa_problem(ctx
, "unexpected type in get_row()");
1572 * Get RGBA pixels from the given renderbuffer.
1573 * Used by blending, logicop and masking functions.
1574 * \return pointer to the colors we read.
1577 _swrast_get_dest_rgba(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1582 /* Point rbPixels to a temporary space */
1583 rbPixels
= span
->array
->attribs
[VARYING_SLOT_MAX
- 1];
1585 /* Get destination values from renderbuffer */
1586 if (span
->arrayMask
& SPAN_XY
) {
1587 get_values(ctx
, rb
, span
->end
, span
->array
->x
, span
->array
->y
,
1588 rbPixels
, span
->array
->ChanType
);
1591 get_row(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1592 rbPixels
, span
->array
->ChanType
);