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/macros.h"
37 #include "main/imports.h"
38 #include "main/image.h"
40 #include "s_atifragshader.h"
43 #include "s_context.h"
47 #include "s_masking.h"
48 #include "s_fragprog.h"
50 #include "s_stencil.h"
51 #include "s_texcombine.h"
56 * Set default fragment attributes for the span using the
57 * current raster values. Used prior to glDraw/CopyPixels
61 _swrast_span_default_attribs(struct gl_context
*ctx
, SWspan
*span
)
66 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
67 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16)
68 span
->z
= FloatToFixed(ctx
->Current
.RasterPos
[2] * depthMax
+ 0.5F
);
70 GLfloat tmpf
= ctx
->Current
.RasterPos
[2] * depthMax
;
71 tmpf
= MIN2(tmpf
, depthMax
);
72 span
->z
= (GLint
)tmpf
;
75 span
->interpMask
|= SPAN_Z
;
78 /* W (for perspective correction) */
79 span
->attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0;
80 span
->attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0;
81 span
->attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0;
83 /* primary color, or color index */
84 UNCLAMPED_FLOAT_TO_CHAN(r
, ctx
->Current
.RasterColor
[0]);
85 UNCLAMPED_FLOAT_TO_CHAN(g
, ctx
->Current
.RasterColor
[1]);
86 UNCLAMPED_FLOAT_TO_CHAN(b
, ctx
->Current
.RasterColor
[2]);
87 UNCLAMPED_FLOAT_TO_CHAN(a
, ctx
->Current
.RasterColor
[3]);
88 #if CHAN_TYPE == GL_FLOAT
94 span
->red
= IntToFixed(r
);
95 span
->green
= IntToFixed(g
);
96 span
->blue
= IntToFixed(b
);
97 span
->alpha
= IntToFixed(a
);
103 span
->interpMask
|= SPAN_RGBA
;
105 COPY_4V(span
->attrStart
[FRAG_ATTRIB_COL0
], ctx
->Current
.RasterColor
);
106 ASSIGN_4V(span
->attrStepX
[FRAG_ATTRIB_COL0
], 0.0, 0.0, 0.0, 0.0);
107 ASSIGN_4V(span
->attrStepY
[FRAG_ATTRIB_COL0
], 0.0, 0.0, 0.0, 0.0);
109 /* Secondary color */
110 if (ctx
->Light
.Enabled
|| ctx
->Fog
.ColorSumEnabled
)
112 COPY_4V(span
->attrStart
[FRAG_ATTRIB_COL1
], ctx
->Current
.RasterSecondaryColor
);
113 ASSIGN_4V(span
->attrStepX
[FRAG_ATTRIB_COL1
], 0.0, 0.0, 0.0, 0.0);
114 ASSIGN_4V(span
->attrStepY
[FRAG_ATTRIB_COL1
], 0.0, 0.0, 0.0, 0.0);
119 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
120 GLfloat fogVal
; /* a coord or a blend factor */
121 if (swrast
->_PreferPixelFog
) {
122 /* fog blend factors will be computed from fog coordinates per pixel */
123 fogVal
= ctx
->Current
.RasterDistance
;
126 /* fog blend factor should be computed from fogcoord now */
127 fogVal
= _swrast_z_to_fogfactor(ctx
, ctx
->Current
.RasterDistance
);
129 span
->attrStart
[FRAG_ATTRIB_FOGC
][0] = fogVal
;
130 span
->attrStepX
[FRAG_ATTRIB_FOGC
][0] = 0.0;
131 span
->attrStepY
[FRAG_ATTRIB_FOGC
][0] = 0.0;
137 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
138 const GLuint attr
= FRAG_ATTRIB_TEX0
+ i
;
139 const GLfloat
*tc
= ctx
->Current
.RasterTexCoords
[i
];
140 if (ctx
->FragmentProgram
._Current
|| ctx
->ATIFragmentShader
._Enabled
) {
141 COPY_4V(span
->attrStart
[attr
], tc
);
143 else if (tc
[3] > 0.0F
) {
144 /* use (s/q, t/q, r/q, 1) */
145 span
->attrStart
[attr
][0] = tc
[0] / tc
[3];
146 span
->attrStart
[attr
][1] = tc
[1] / tc
[3];
147 span
->attrStart
[attr
][2] = tc
[2] / tc
[3];
148 span
->attrStart
[attr
][3] = 1.0;
151 ASSIGN_4V(span
->attrStart
[attr
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
153 ASSIGN_4V(span
->attrStepX
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
154 ASSIGN_4V(span
->attrStepY
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
161 * Interpolate the active attributes (and'd with attrMask) to
162 * fill in span->array->attribs[].
163 * Perspective correction will be done. The point/line/triangle function
164 * should have computed attrStart/Step values for FRAG_ATTRIB_WPOS[3]!
167 interpolate_active_attribs(struct gl_context
*ctx
, SWspan
*span
,
168 GLbitfield64 attrMask
)
170 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
173 * Don't overwrite existing array values, such as colors that may have
174 * been produced by glDraw/CopyPixels.
176 attrMask
&= ~span
->arrayAttribs
;
179 if (attrMask
& BITFIELD64_BIT(attr
)) {
180 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
181 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
182 const GLfloat dv0dx
= span
->attrStepX
[attr
][0];
183 const GLfloat dv1dx
= span
->attrStepX
[attr
][1];
184 const GLfloat dv2dx
= span
->attrStepX
[attr
][2];
185 const GLfloat dv3dx
= span
->attrStepX
[attr
][3];
186 GLfloat v0
= span
->attrStart
[attr
][0] + span
->leftClip
* dv0dx
;
187 GLfloat v1
= span
->attrStart
[attr
][1] + span
->leftClip
* dv1dx
;
188 GLfloat v2
= span
->attrStart
[attr
][2] + span
->leftClip
* dv2dx
;
189 GLfloat v3
= span
->attrStart
[attr
][3] + span
->leftClip
* dv3dx
;
191 for (k
= 0; k
< span
->end
; k
++) {
192 const GLfloat invW
= 1.0f
/ w
;
193 span
->array
->attribs
[attr
][k
][0] = v0
* invW
;
194 span
->array
->attribs
[attr
][k
][1] = v1
* invW
;
195 span
->array
->attribs
[attr
][k
][2] = v2
* invW
;
196 span
->array
->attribs
[attr
][k
][3] = v3
* invW
;
203 ASSERT((span
->arrayAttribs
& BITFIELD64_BIT(attr
)) == 0);
204 span
->arrayAttribs
|= BITFIELD64_BIT(attr
);
211 * Interpolate primary colors to fill in the span->array->rgba8 (or rgb16)
215 interpolate_int_colors(struct gl_context
*ctx
, SWspan
*span
)
218 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(ctx
, "bad datatype 0x%x in interpolate_int_colors",
305 span
->array
->ChanType
);
307 span
->arrayMask
|= SPAN_RGBA
;
312 * Populate the FRAG_ATTRIB_COL0 array.
315 interpolate_float_colors(SWspan
*span
)
317 GLfloat (*col0
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
318 const GLuint n
= span
->end
;
321 assert(!(span
->arrayAttribs
& FRAG_BIT_COL0
));
323 if (span
->arrayMask
& SPAN_RGBA
) {
324 /* convert array of int colors */
325 for (i
= 0; i
< n
; i
++) {
326 col0
[i
][0] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][0]);
327 col0
[i
][1] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][1]);
328 col0
[i
][2] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][2]);
329 col0
[i
][3] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][3]);
333 /* interpolate red/green/blue/alpha to get float colors */
334 ASSERT(span
->interpMask
& SPAN_RGBA
);
335 if (span
->interpMask
& SPAN_FLAT
) {
336 GLfloat r
= FixedToFloat(span
->red
);
337 GLfloat g
= FixedToFloat(span
->green
);
338 GLfloat b
= FixedToFloat(span
->blue
);
339 GLfloat a
= FixedToFloat(span
->alpha
);
340 for (i
= 0; i
< n
; i
++) {
341 ASSIGN_4V(col0
[i
], r
, g
, b
, a
);
345 GLfloat r
= FixedToFloat(span
->red
);
346 GLfloat g
= FixedToFloat(span
->green
);
347 GLfloat b
= FixedToFloat(span
->blue
);
348 GLfloat a
= FixedToFloat(span
->alpha
);
349 GLfloat dr
= FixedToFloat(span
->redStep
);
350 GLfloat dg
= FixedToFloat(span
->greenStep
);
351 GLfloat db
= FixedToFloat(span
->blueStep
);
352 GLfloat da
= FixedToFloat(span
->alphaStep
);
353 for (i
= 0; i
< n
; i
++) {
366 span
->arrayAttribs
|= FRAG_BIT_COL0
;
367 span
->array
->ChanType
= GL_FLOAT
;
373 * Fill in the span.zArray array from the span->z, zStep values.
376 _swrast_span_interpolate_z( const struct gl_context
*ctx
, SWspan
*span
)
378 const GLuint n
= span
->end
;
381 ASSERT(!(span
->arrayMask
& SPAN_Z
));
383 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
384 GLfixed zval
= span
->z
;
385 GLuint
*z
= span
->array
->z
;
386 for (i
= 0; i
< n
; i
++) {
387 z
[i
] = FixedToInt(zval
);
392 /* Deep Z buffer, no fixed->int shift */
393 GLuint zval
= span
->z
;
394 GLuint
*z
= span
->array
->z
;
395 for (i
= 0; i
< n
; i
++) {
400 span
->interpMask
&= ~SPAN_Z
;
401 span
->arrayMask
|= SPAN_Z
;
406 * Compute mipmap LOD from partial derivatives.
407 * This the ideal solution, as given in the OpenGL spec.
410 _swrast_compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
411 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
412 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
414 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
415 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
416 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
417 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
418 GLfloat x
= SQRTF(dudx
* dudx
+ dvdx
* dvdx
);
419 GLfloat y
= SQRTF(dudy
* dudy
+ dvdy
* dvdy
);
420 GLfloat rho
= MAX2(x
, y
);
421 GLfloat lambda
= LOG2(rho
);
427 * Compute mipmap LOD from partial derivatives.
428 * This is a faster approximation than above function.
432 _swrast_compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
433 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
434 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
436 GLfloat dsdx2
= (s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
;
437 GLfloat dtdx2
= (t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
;
438 GLfloat dsdy2
= (s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
;
439 GLfloat dtdy2
= (t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
;
440 GLfloat maxU
, maxV
, rho
, lambda
;
441 dsdx2
= FABSF(dsdx2
);
442 dsdy2
= FABSF(dsdy2
);
443 dtdx2
= FABSF(dtdx2
);
444 dtdy2
= FABSF(dtdy2
);
445 maxU
= MAX2(dsdx2
, dsdy2
) * texW
;
446 maxV
= MAX2(dtdx2
, dtdy2
) * texH
;
447 rho
= MAX2(maxU
, maxV
);
455 * Fill in the span.array->attrib[FRAG_ATTRIB_TEXn] arrays from the
456 * using the attrStart/Step values.
458 * This function only used during fixed-function fragment processing.
460 * Note: in the places where we divide by Q (or mult by invQ) we're
461 * really doing two things: perspective correction and texcoord
462 * projection. Remember, for texcoord (s,t,r,q) we need to index
463 * texels with (s/q, t/q, r/q).
466 interpolate_texcoords(struct gl_context
*ctx
, SWspan
*span
)
469 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
472 /* XXX CoordUnits vs. ImageUnits */
473 for (u
= 0; u
< maxUnit
; u
++) {
474 if (ctx
->Texture
._EnabledCoordUnits
& (1 << u
)) {
475 const GLuint attr
= FRAG_ATTRIB_TEX0
+ u
;
476 const struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[u
]._Current
;
478 GLboolean needLambda
;
479 GLfloat (*texcoord
)[4] = span
->array
->attribs
[attr
];
480 GLfloat
*lambda
= span
->array
->lambda
[u
];
481 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
482 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
483 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
484 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
485 const GLfloat drdx
= span
->attrStepX
[attr
][2];
486 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
487 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
488 GLfloat s
= span
->attrStart
[attr
][0] + span
->leftClip
* dsdx
;
489 GLfloat t
= span
->attrStart
[attr
][1] + span
->leftClip
* dtdx
;
490 GLfloat r
= span
->attrStart
[attr
][2] + span
->leftClip
* drdx
;
491 GLfloat q
= span
->attrStart
[attr
][3] + span
->leftClip
* dqdx
;
494 const struct gl_texture_image
*img
= obj
->Image
[0][obj
->BaseLevel
];
495 const struct swrast_texture_image
*swImg
=
496 swrast_texture_image_const(img
);
498 needLambda
= (obj
->Sampler
.MinFilter
!= obj
->Sampler
.MagFilter
)
499 || ctx
->FragmentProgram
._Current
;
500 /* LOD is calculated directly in the ansiotropic filter, we can
501 * skip the normal lambda function as the result is ignored.
503 if (obj
->Sampler
.MaxAnisotropy
> 1.0 &&
504 obj
->Sampler
.MinFilter
== GL_LINEAR_MIPMAP_LINEAR
) {
505 needLambda
= GL_FALSE
;
507 texW
= swImg
->WidthScale
;
508 texH
= swImg
->HeightScale
;
511 /* using a fragment program */
514 needLambda
= GL_FALSE
;
519 if (ctx
->FragmentProgram
._Current
520 || ctx
->ATIFragmentShader
._Enabled
) {
521 /* do perspective correction but don't divide s, t, r by q */
522 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
523 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3] + span
->leftClip
* dwdx
;
524 for (i
= 0; i
< span
->end
; i
++) {
525 const GLfloat invW
= 1.0F
/ w
;
526 texcoord
[i
][0] = s
* invW
;
527 texcoord
[i
][1] = t
* invW
;
528 texcoord
[i
][2] = r
* invW
;
529 texcoord
[i
][3] = q
* invW
;
530 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
531 dqdx
, dqdy
, texW
, texH
,
541 for (i
= 0; i
< span
->end
; i
++) {
542 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
543 texcoord
[i
][0] = s
* invQ
;
544 texcoord
[i
][1] = t
* invQ
;
545 texcoord
[i
][2] = r
* invQ
;
547 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
548 dqdx
, dqdy
, texW
, texH
,
556 span
->arrayMask
|= SPAN_LAMBDA
;
560 if (ctx
->FragmentProgram
._Current
||
561 ctx
->ATIFragmentShader
._Enabled
) {
562 /* do perspective correction but don't divide s, t, r by q */
563 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
564 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3] + span
->leftClip
* dwdx
;
565 for (i
= 0; i
< span
->end
; i
++) {
566 const GLfloat invW
= 1.0F
/ w
;
567 texcoord
[i
][0] = s
* invW
;
568 texcoord
[i
][1] = t
* invW
;
569 texcoord
[i
][2] = r
* invW
;
570 texcoord
[i
][3] = q
* invW
;
579 else if (dqdx
== 0.0F
) {
580 /* Ortho projection or polygon's parallel to window X axis */
581 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
582 for (i
= 0; i
< span
->end
; i
++) {
583 texcoord
[i
][0] = s
* invQ
;
584 texcoord
[i
][1] = t
* invQ
;
585 texcoord
[i
][2] = r
* invQ
;
594 for (i
= 0; i
< span
->end
; i
++) {
595 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
596 texcoord
[i
][0] = s
* invQ
;
597 texcoord
[i
][1] = t
* invQ
;
598 texcoord
[i
][2] = r
* invQ
;
614 * Fill in the arrays->attribs[FRAG_ATTRIB_WPOS] array.
617 interpolate_wpos(struct gl_context
*ctx
, SWspan
*span
)
619 GLfloat (*wpos
)[4] = span
->array
->attribs
[FRAG_ATTRIB_WPOS
];
621 const GLfloat zScale
= 1.0F
/ ctx
->DrawBuffer
->_DepthMaxF
;
624 if (span
->arrayMask
& SPAN_XY
) {
625 for (i
= 0; i
< span
->end
; i
++) {
626 wpos
[i
][0] = (GLfloat
) span
->array
->x
[i
];
627 wpos
[i
][1] = (GLfloat
) span
->array
->y
[i
];
631 for (i
= 0; i
< span
->end
; i
++) {
632 wpos
[i
][0] = (GLfloat
) span
->x
+ i
;
633 wpos
[i
][1] = (GLfloat
) span
->y
;
637 dw
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
638 w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3] + span
->leftClip
* dw
;
639 for (i
= 0; i
< span
->end
; i
++) {
640 wpos
[i
][2] = (GLfloat
) span
->array
->z
[i
] * zScale
;
648 * Apply the current polygon stipple pattern to a span of pixels.
651 stipple_polygon_span(struct gl_context
*ctx
, SWspan
*span
)
653 GLubyte
*mask
= span
->array
->mask
;
655 ASSERT(ctx
->Polygon
.StippleFlag
);
657 if (span
->arrayMask
& SPAN_XY
) {
658 /* arrays of x/y pixel coords */
660 for (i
= 0; i
< span
->end
; i
++) {
661 const GLint col
= span
->array
->x
[i
] % 32;
662 const GLint row
= span
->array
->y
[i
] % 32;
663 const GLuint stipple
= ctx
->PolygonStipple
[row
];
664 if (((1 << col
) & stipple
) == 0) {
670 /* horizontal span of pixels */
671 const GLuint highBit
= 1 << 31;
672 const GLuint stipple
= ctx
->PolygonStipple
[span
->y
% 32];
673 GLuint i
, m
= highBit
>> (GLuint
) (span
->x
% 32);
674 for (i
= 0; i
< span
->end
; i
++) {
675 if ((m
& stipple
) == 0) {
684 span
->writeAll
= GL_FALSE
;
689 * Clip a pixel span to the current buffer/window boundaries:
690 * DrawBuffer->_Xmin, _Xmax, _Ymin, _Ymax. This will accomplish
691 * window clipping and scissoring.
692 * Return: GL_TRUE some pixels still visible
693 * GL_FALSE nothing visible
696 clip_span( struct gl_context
*ctx
, SWspan
*span
)
698 const GLint xmin
= ctx
->DrawBuffer
->_Xmin
;
699 const GLint xmax
= ctx
->DrawBuffer
->_Xmax
;
700 const GLint ymin
= ctx
->DrawBuffer
->_Ymin
;
701 const GLint ymax
= ctx
->DrawBuffer
->_Ymax
;
705 if (span
->arrayMask
& SPAN_XY
) {
706 /* arrays of x/y pixel coords */
707 const GLint
*x
= span
->array
->x
;
708 const GLint
*y
= span
->array
->y
;
709 const GLint n
= span
->end
;
710 GLubyte
*mask
= span
->array
->mask
;
713 if (span
->arrayMask
& SPAN_MASK
) {
714 /* note: using & intead of && to reduce branches */
715 for (i
= 0; i
< n
; i
++) {
716 mask
[i
] &= (x
[i
] >= xmin
) & (x
[i
] < xmax
)
717 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
722 /* note: using & intead of && to reduce branches */
723 for (i
= 0; i
< n
; i
++) {
724 mask
[i
] = (x
[i
] >= xmin
) & (x
[i
] < xmax
)
725 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
732 /* horizontal span of pixels */
733 const GLint x
= span
->x
;
734 const GLint y
= span
->y
;
737 /* Trivial rejection tests */
738 if (y
< ymin
|| y
>= ymax
|| x
+ n
<= xmin
|| x
>= xmax
) {
740 return GL_FALSE
; /* all pixels clipped */
746 n
= span
->end
= xmax
- x
;
749 /* Clip to the left */
751 const GLint leftClip
= xmin
- x
;
754 ASSERT(leftClip
> 0);
755 ASSERT(x
+ n
> xmin
);
757 /* Clip 'leftClip' pixels from the left side.
758 * The span->leftClip field will be applied when we interpolate
759 * fragment attributes.
760 * For arrays of values, shift them left.
762 for (i
= 0; i
< FRAG_ATTRIB_MAX
; i
++) {
763 if (span
->interpMask
& (1 << i
)) {
765 for (j
= 0; j
< 4; j
++) {
766 span
->attrStart
[i
][j
] += leftClip
* span
->attrStepX
[i
][j
];
771 span
->red
+= leftClip
* span
->redStep
;
772 span
->green
+= leftClip
* span
->greenStep
;
773 span
->blue
+= leftClip
* span
->blueStep
;
774 span
->alpha
+= leftClip
* span
->alphaStep
;
775 span
->index
+= leftClip
* span
->indexStep
;
776 span
->z
+= leftClip
* span
->zStep
;
777 span
->intTex
[0] += leftClip
* span
->intTexStep
[0];
778 span
->intTex
[1] += leftClip
* span
->intTexStep
[1];
780 #define SHIFT_ARRAY(ARRAY, SHIFT, LEN) \
781 memmove(ARRAY, ARRAY + (SHIFT), (LEN) * sizeof(ARRAY[0]))
783 for (i
= 0; i
< FRAG_ATTRIB_MAX
; i
++) {
784 if (span
->arrayAttribs
& (1 << i
)) {
785 /* shift array elements left by 'leftClip' */
786 SHIFT_ARRAY(span
->array
->attribs
[i
], leftClip
, n
- leftClip
);
790 SHIFT_ARRAY(span
->array
->mask
, leftClip
, n
- leftClip
);
791 SHIFT_ARRAY(span
->array
->rgba8
, leftClip
, n
- leftClip
);
792 SHIFT_ARRAY(span
->array
->rgba16
, leftClip
, n
- leftClip
);
793 SHIFT_ARRAY(span
->array
->x
, leftClip
, n
- leftClip
);
794 SHIFT_ARRAY(span
->array
->y
, leftClip
, n
- leftClip
);
795 SHIFT_ARRAY(span
->array
->z
, leftClip
, n
- leftClip
);
796 SHIFT_ARRAY(span
->array
->index
, leftClip
, n
- leftClip
);
797 for (i
= 0; i
< MAX_TEXTURE_COORD_UNITS
; i
++) {
798 SHIFT_ARRAY(span
->array
->lambda
[i
], leftClip
, n
- leftClip
);
800 SHIFT_ARRAY(span
->array
->coverage
, leftClip
, n
- leftClip
);
804 span
->leftClip
= leftClip
;
806 span
->end
-= leftClip
;
807 span
->writeAll
= GL_FALSE
;
810 ASSERT(span
->x
>= xmin
);
811 ASSERT(span
->x
+ span
->end
<= xmax
);
812 ASSERT(span
->y
>= ymin
);
813 ASSERT(span
->y
< ymax
);
815 return GL_TRUE
; /* some pixels visible */
821 * Add specular colors to primary colors.
822 * Only called during fixed-function operation.
823 * Result is float color array (FRAG_ATTRIB_COL0).
826 add_specular(struct gl_context
*ctx
, SWspan
*span
)
828 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
829 const GLubyte
*mask
= span
->array
->mask
;
830 GLfloat (*col0
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
831 GLfloat (*col1
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL1
];
834 ASSERT(!ctx
->FragmentProgram
._Current
);
835 ASSERT(span
->arrayMask
& SPAN_RGBA
);
836 ASSERT(swrast
->_ActiveAttribMask
& FRAG_BIT_COL1
);
837 (void) swrast
; /* silence warning */
839 if (span
->array
->ChanType
== GL_FLOAT
) {
840 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
841 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL0
);
845 /* need float colors */
846 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
847 interpolate_float_colors(span
);
851 if ((span
->arrayAttribs
& FRAG_BIT_COL1
) == 0) {
852 /* XXX could avoid this and interpolate COL1 in the loop below */
853 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL1
);
856 ASSERT(span
->arrayAttribs
& FRAG_BIT_COL0
);
857 ASSERT(span
->arrayAttribs
& FRAG_BIT_COL1
);
859 for (i
= 0; i
< span
->end
; i
++) {
861 col0
[i
][0] += col1
[i
][0];
862 col0
[i
][1] += col1
[i
][1];
863 col0
[i
][2] += col1
[i
][2];
867 span
->array
->ChanType
= GL_FLOAT
;
872 * Apply antialiasing coverage value to alpha values.
875 apply_aa_coverage(SWspan
*span
)
877 const GLfloat
*coverage
= span
->array
->coverage
;
879 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
880 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
881 for (i
= 0; i
< span
->end
; i
++) {
882 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
883 rgba
[i
][ACOMP
] = (GLubyte
) CLAMP(a
, 0.0, 255.0);
884 ASSERT(coverage
[i
] >= 0.0);
885 ASSERT(coverage
[i
] <= 1.0);
888 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
889 GLushort (*rgba
)[4] = span
->array
->rgba16
;
890 for (i
= 0; i
< span
->end
; i
++) {
891 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
892 rgba
[i
][ACOMP
] = (GLushort
) CLAMP(a
, 0.0, 65535.0);
896 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
897 for (i
= 0; i
< span
->end
; i
++) {
898 rgba
[i
][ACOMP
] = rgba
[i
][ACOMP
] * coverage
[i
];
906 * Clamp span's float colors to [0,1]
909 clamp_colors(SWspan
*span
)
911 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
913 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
914 for (i
= 0; i
< span
->end
; i
++) {
915 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0F
, 1.0F
);
916 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0F
, 1.0F
);
917 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0F
, 1.0F
);
918 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0F
, 1.0F
);
924 * Convert the span's color arrays to the given type.
925 * The only way 'output' can be greater than zero is when we have a fragment
926 * program that writes to gl_FragData[1] or higher.
927 * \param output which fragment program color output is being processed
930 convert_color_type(SWspan
*span
, GLenum newType
, GLuint output
)
934 if (output
> 0 || span
->array
->ChanType
== GL_FLOAT
) {
935 src
= span
->array
->attribs
[FRAG_ATTRIB_COL0
+ output
];
936 span
->array
->ChanType
= GL_FLOAT
;
938 else if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
939 src
= span
->array
->rgba8
;
942 ASSERT(span
->array
->ChanType
== GL_UNSIGNED_SHORT
);
943 src
= span
->array
->rgba16
;
946 if (newType
== GL_UNSIGNED_BYTE
) {
947 dst
= span
->array
->rgba8
;
949 else if (newType
== GL_UNSIGNED_SHORT
) {
950 dst
= span
->array
->rgba16
;
953 dst
= span
->array
->attribs
[FRAG_ATTRIB_COL0
];
956 _mesa_convert_colors(span
->array
->ChanType
, src
,
958 span
->end
, span
->array
->mask
);
960 span
->array
->ChanType
= newType
;
961 span
->array
->rgba
= dst
;
967 * Apply fragment shader, fragment program or normal texturing to span.
970 shade_texture_span(struct gl_context
*ctx
, SWspan
*span
)
972 /* This is a hack to work around drivers such as i965 that:
974 * - Set _MaintainTexEnvProgram to generate GLSL IR for
975 * fixed-function fragment processing.
976 * - Don't call _mesa_ir_link_shader to generate Mesa IR from
978 * - May use swrast to handle glDrawPixels.
980 * Since _mesa_ir_link_shader is never called, there is no Mesa IR
981 * to execute. Instead do regular fixed-function processing.
983 * It is also worth noting that the software fixed-function path is
984 * much faster than the software shader path.
986 const bool use_fragment_program
=
987 ctx
->FragmentProgram
._Current
988 && ctx
->FragmentProgram
._Current
!= ctx
->FragmentProgram
._TexEnvProgram
;
990 if (use_fragment_program
||
991 ctx
->ATIFragmentShader
._Enabled
) {
992 /* programmable shading */
993 if (span
->primitive
== GL_BITMAP
&& span
->array
->ChanType
!= GL_FLOAT
) {
994 convert_color_type(span
, GL_FLOAT
, 0);
997 span
->array
->rgba
= (void *) span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1000 if (span
->primitive
!= GL_POINT
||
1001 (span
->interpMask
& SPAN_RGBA
) ||
1002 ctx
->Point
.PointSprite
) {
1003 /* for single-pixel points, we populated the arrays already */
1004 interpolate_active_attribs(ctx
, span
, ~0);
1006 span
->array
->ChanType
= GL_FLOAT
;
1008 if (!(span
->arrayMask
& SPAN_Z
))
1009 _swrast_span_interpolate_z (ctx
, span
);
1012 if (inputsRead
& FRAG_BIT_WPOS
)
1014 /* XXX always interpolate wpos so that DDX/DDY work */
1016 interpolate_wpos(ctx
, span
);
1018 /* Run fragment program/shader now */
1019 if (use_fragment_program
) {
1020 _swrast_exec_fragment_program(ctx
, span
);
1023 ASSERT(ctx
->ATIFragmentShader
._Enabled
);
1024 _swrast_exec_fragment_shader(ctx
, span
);
1027 else if (ctx
->Texture
._EnabledCoordUnits
) {
1028 /* conventional texturing */
1031 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1032 interpolate_int_colors(ctx
, span
);
1035 if (!(span
->arrayMask
& SPAN_RGBA
))
1036 interpolate_int_colors(ctx
, span
);
1038 if ((span
->arrayAttribs
& FRAG_BITS_TEX_ANY
) == 0x0)
1039 interpolate_texcoords(ctx
, span
);
1041 _swrast_texture_span(ctx
, span
);
1048 * Apply all the per-fragment operations to a span.
1049 * This now includes texturing (_swrast_write_texture_span() is history).
1050 * This function may modify any of the array values in the span.
1051 * span->interpMask and span->arrayMask may be changed but will be restored
1052 * to their original values before returning.
1055 _swrast_write_rgba_span( struct gl_context
*ctx
, SWspan
*span
)
1057 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1058 const GLuint
*colorMask
= (GLuint
*) ctx
->Color
.ColorMask
;
1059 const GLbitfield origInterpMask
= span
->interpMask
;
1060 const GLbitfield origArrayMask
= span
->arrayMask
;
1061 const GLbitfield64 origArrayAttribs
= span
->arrayAttribs
;
1062 const GLenum origChanType
= span
->array
->ChanType
;
1063 void * const origRgba
= span
->array
->rgba
;
1064 const GLboolean shader
= (ctx
->FragmentProgram
._Current
1065 || ctx
->ATIFragmentShader
._Enabled
);
1066 const GLboolean shaderOrTexture
= shader
|| ctx
->Texture
._EnabledCoordUnits
;
1067 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1070 printf("%s() interp 0x%x array 0x%x\n", __FUNCTION__,
1071 span->interpMask, span->arrayMask);
1074 ASSERT(span
->primitive
== GL_POINT
||
1075 span
->primitive
== GL_LINE
||
1076 span
->primitive
== GL_POLYGON
||
1077 span
->primitive
== GL_BITMAP
);
1079 /* Fragment write masks */
1080 if (span
->arrayMask
& SPAN_MASK
) {
1081 /* mask was initialized by caller, probably glBitmap */
1082 span
->writeAll
= GL_FALSE
;
1085 memset(span
->array
->mask
, 1, span
->end
);
1086 span
->writeAll
= GL_TRUE
;
1089 /* Clip to window/scissor box */
1090 if (!clip_span(ctx
, span
)) {
1094 ASSERT(span
->end
<= MAX_WIDTH
);
1096 /* Depth bounds test */
1097 if (ctx
->Depth
.BoundsTest
&& fb
->Visual
.depthBits
> 0) {
1098 if (!_swrast_depth_bounds_test(ctx
, span
)) {
1104 /* Make sure all fragments are within window bounds */
1105 if (span
->arrayMask
& SPAN_XY
) {
1106 /* array of pixel locations */
1108 for (i
= 0; i
< span
->end
; i
++) {
1109 if (span
->array
->mask
[i
]) {
1110 assert(span
->array
->x
[i
] >= fb
->_Xmin
);
1111 assert(span
->array
->x
[i
] < fb
->_Xmax
);
1112 assert(span
->array
->y
[i
] >= fb
->_Ymin
);
1113 assert(span
->array
->y
[i
] < fb
->_Ymax
);
1119 /* Polygon Stippling */
1120 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
1121 stipple_polygon_span(ctx
, span
);
1124 /* This is the normal place to compute the fragment color/Z
1125 * from texturing or shading.
1127 if (shaderOrTexture
&& !swrast
->_DeferredTexture
) {
1128 shade_texture_span(ctx
, span
);
1131 /* Do the alpha test */
1132 if (ctx
->Color
.AlphaEnabled
) {
1133 if (!_swrast_alpha_test(ctx
, span
)) {
1134 /* all fragments failed test */
1139 /* Stencil and Z testing */
1140 if (ctx
->Stencil
._Enabled
|| ctx
->Depth
.Test
) {
1141 if (!(span
->arrayMask
& SPAN_Z
))
1142 _swrast_span_interpolate_z(ctx
, span
);
1144 if (ctx
->Transform
.DepthClamp
)
1145 _swrast_depth_clamp_span(ctx
, span
);
1147 if (ctx
->Stencil
._Enabled
) {
1148 /* Combined Z/stencil tests */
1149 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
1150 /* all fragments failed test */
1154 else if (fb
->Visual
.depthBits
> 0) {
1155 /* Just regular depth testing */
1156 ASSERT(ctx
->Depth
.Test
);
1157 ASSERT(span
->arrayMask
& SPAN_Z
);
1158 if (!_swrast_depth_test_span(ctx
, span
)) {
1159 /* all fragments failed test */
1165 if (ctx
->Query
.CurrentOcclusionObject
) {
1166 /* update count of 'passed' fragments */
1167 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
1169 for (i
= 0; i
< span
->end
; i
++)
1170 q
->Result
+= span
->array
->mask
[i
];
1173 /* We had to wait until now to check for glColorMask(0,0,0,0) because of
1174 * the occlusion test.
1176 if (fb
->_NumColorDrawBuffers
== 1 && colorMask
[0] == 0x0) {
1177 /* no colors to write */
1181 /* If we were able to defer fragment color computation to now, there's
1182 * a good chance that many fragments will have already been killed by
1183 * Z/stencil testing.
1185 if (shaderOrTexture
&& swrast
->_DeferredTexture
) {
1186 shade_texture_span(ctx
, span
);
1190 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1191 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL0
);
1194 if ((span
->arrayMask
& SPAN_RGBA
) == 0) {
1195 interpolate_int_colors(ctx
, span
);
1199 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1201 if (span
->primitive
== GL_BITMAP
|| !swrast
->SpecularVertexAdd
) {
1202 /* Add primary and specular (diffuse + specular) colors */
1204 if (ctx
->Fog
.ColorSumEnabled
||
1205 (ctx
->Light
.Enabled
&&
1206 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)) {
1207 add_specular(ctx
, span
);
1213 if (swrast
->_FogEnabled
) {
1214 _swrast_fog_rgba_span(ctx
, span
);
1217 /* Antialias coverage application */
1218 if (span
->arrayMask
& SPAN_COVERAGE
) {
1219 apply_aa_coverage(span
);
1222 /* Clamp color/alpha values over the range [0.0, 1.0] before storage */
1223 if (ctx
->Color
.ClampFragmentColor
== GL_TRUE
&&
1224 span
->array
->ChanType
== GL_FLOAT
) {
1229 * Write to renderbuffers.
1230 * Depending on glDrawBuffer() state and the which color outputs are
1231 * written by the fragment shader, we may either replicate one color to
1232 * all renderbuffers or write a different color to each renderbuffer.
1233 * multiFragOutputs=TRUE for the later case.
1236 const GLuint numBuffers
= fb
->_NumColorDrawBuffers
;
1237 const struct gl_fragment_program
*fp
= ctx
->FragmentProgram
._Current
;
1238 const GLboolean multiFragOutputs
=
1239 (fp
&& fp
->Base
.OutputsWritten
>= (1 << FRAG_RESULT_DATA0
));
1242 for (buf
= 0; buf
< numBuffers
; buf
++) {
1243 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[buf
];
1245 /* color[fragOutput] will be written to buffer[buf] */
1248 GLchan rgbaSave
[MAX_WIDTH
][4];
1249 const GLuint fragOutput
= multiFragOutputs
? buf
: 0;
1251 /* set span->array->rgba to colors for render buffer's datatype */
1252 if (rb
->DataType
!= span
->array
->ChanType
|| fragOutput
> 0) {
1253 convert_color_type(span
, rb
->DataType
, fragOutput
);
1256 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1257 span
->array
->rgba
= span
->array
->rgba8
;
1259 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1260 span
->array
->rgba
= (void *) span
->array
->rgba16
;
1263 span
->array
->rgba
= (void *)
1264 span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1268 if (!multiFragOutputs
&& numBuffers
> 1) {
1269 /* save colors for second, third renderbuffer writes */
1270 memcpy(rgbaSave
, span
->array
->rgba
,
1271 4 * span
->end
* sizeof(GLchan
));
1274 ASSERT(rb
->_BaseFormat
== GL_RGBA
||
1275 rb
->_BaseFormat
== GL_RGB
||
1276 rb
->_BaseFormat
== GL_RED
||
1277 rb
->_BaseFormat
== GL_RG
||
1278 rb
->_BaseFormat
== GL_ALPHA
);
1280 if (ctx
->Color
.ColorLogicOpEnabled
) {
1281 _swrast_logicop_rgba_span(ctx
, rb
, span
);
1283 else if ((ctx
->Color
.BlendEnabled
>> buf
) & 1) {
1284 _swrast_blend_span(ctx
, rb
, span
);
1287 if (colorMask
[buf
] != 0xffffffff) {
1288 _swrast_mask_rgba_span(ctx
, rb
, span
, buf
);
1291 if (span
->arrayMask
& SPAN_XY
) {
1292 /* array of pixel coords */
1293 ASSERT(rb
->PutValues
);
1294 rb
->PutValues(ctx
, rb
, span
->end
,
1295 span
->array
->x
, span
->array
->y
,
1296 span
->array
->rgba
, span
->array
->mask
);
1299 /* horizontal run of pixels */
1301 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1303 span
->writeAll
? NULL
: span
->array
->mask
);
1306 if (!multiFragOutputs
&& numBuffers
> 1) {
1307 /* restore original span values */
1308 memcpy(span
->array
->rgba
, rgbaSave
,
1309 4 * span
->end
* sizeof(GLchan
));
1317 /* restore these values before returning */
1318 span
->interpMask
= origInterpMask
;
1319 span
->arrayMask
= origArrayMask
;
1320 span
->arrayAttribs
= origArrayAttribs
;
1321 span
->array
->ChanType
= origChanType
;
1322 span
->array
->rgba
= origRgba
;
1327 * Read RGBA pixels from a renderbuffer. Clipping will be done to prevent
1328 * reading ouside the buffer's boundaries.
1329 * \param dstType datatype for returned colors
1330 * \param rgba the returned colors
1333 _swrast_read_rgba_span( struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1334 GLuint n
, GLint x
, GLint y
, GLenum dstType
,
1337 const GLint bufWidth
= (GLint
) rb
->Width
;
1338 const GLint bufHeight
= (GLint
) rb
->Height
;
1340 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1341 /* completely above, below, or right */
1342 /* XXX maybe leave rgba values undefined? */
1343 memset(rgba
, 0, 4 * n
* sizeof(GLchan
));
1348 /* left edge clipping */
1350 length
= (GLint
) n
- skip
;
1352 /* completely left of window */
1355 if (length
> bufWidth
) {
1359 else if ((GLint
) (x
+ n
) > bufWidth
) {
1360 /* right edge clipping */
1362 length
= bufWidth
- x
;
1364 /* completely to right of window */
1376 ASSERT(rb
->_BaseFormat
== GL_RGBA
||
1377 rb
->_BaseFormat
== GL_RGB
||
1378 rb
->_BaseFormat
== GL_RG
||
1379 rb
->_BaseFormat
== GL_RED
||
1380 rb
->_BaseFormat
== GL_LUMINANCE
||
1381 rb
->_BaseFormat
== GL_INTENSITY
||
1382 rb
->_BaseFormat
== GL_LUMINANCE_ALPHA
||
1383 rb
->_BaseFormat
== GL_ALPHA
);
1385 if (rb
->DataType
== dstType
) {
1386 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
,
1387 (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(rb
->DataType
));
1390 GLuint temp
[MAX_WIDTH
* 4];
1391 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, temp
);
1392 _mesa_convert_colors(rb
->DataType
, temp
,
1393 dstType
, (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(dstType
),
1401 * Wrapper for gl_renderbuffer::GetValues() which does clipping to avoid
1402 * reading values outside the buffer bounds.
1403 * We can use this for reading any format/type of renderbuffer.
1404 * \param valueSize is the size in bytes of each value (pixel) put into the
1408 _swrast_get_values(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1409 GLuint count
, const GLint x
[], const GLint y
[],
1410 void *values
, GLuint valueSize
)
1412 GLuint i
, inCount
= 0, inStart
= 0;
1414 for (i
= 0; i
< count
; i
++) {
1415 if (x
[i
] >= 0 && y
[i
] >= 0 &&
1416 x
[i
] < (GLint
) rb
->Width
&& y
[i
] < (GLint
) rb
->Height
) {
1424 /* read [inStart, inStart + inCount) */
1425 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1426 (GLubyte
*) values
+ inStart
* valueSize
);
1432 /* read last values */
1433 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1434 (GLubyte
*) values
+ inStart
* valueSize
);
1440 * Wrapper for gl_renderbuffer::GetRow() which does clipping.
1441 * \param valueSize size of each value (pixel) in bytes
1444 _swrast_get_row(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1445 GLuint count
, GLint x
, GLint y
,
1446 GLvoid
*values
, GLuint valueSize
)
1450 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1451 return; /* above or below */
1453 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1454 return; /* entirely left or right */
1456 if (x
+ count
> rb
->Width
) {
1458 GLint clip
= x
+ count
- rb
->Width
;
1469 rb
->GetRow(ctx
, rb
, count
, x
, y
, (GLubyte
*) values
+ skip
* valueSize
);
1474 * Get RGBA pixels from the given renderbuffer.
1475 * Used by blending, logicop and masking functions.
1476 * \return pointer to the colors we read.
1479 _swrast_get_dest_rgba(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1482 const GLuint pixelSize
= RGBA_PIXEL_SIZE(span
->array
->ChanType
);
1485 /* Point rbPixels to a temporary space */
1486 rbPixels
= span
->array
->attribs
[FRAG_ATTRIB_MAX
- 1];
1488 /* Get destination values from renderbuffer */
1489 if (span
->arrayMask
& SPAN_XY
) {
1490 _swrast_get_values(ctx
, rb
, span
->end
, span
->array
->x
, span
->array
->y
,
1491 rbPixels
, pixelSize
);
1494 _swrast_get_row(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1495 rbPixels
, pixelSize
);