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"
55 * Set default fragment attributes for the span using the
56 * current raster values. Used prior to glDraw/CopyPixels
60 _swrast_span_default_attribs(struct gl_context
*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 UNCLAMPED_FLOAT_TO_CHAN(r
, ctx
->Current
.RasterColor
[0]);
84 UNCLAMPED_FLOAT_TO_CHAN(g
, ctx
->Current
.RasterColor
[1]);
85 UNCLAMPED_FLOAT_TO_CHAN(b
, ctx
->Current
.RasterColor
[2]);
86 UNCLAMPED_FLOAT_TO_CHAN(a
, ctx
->Current
.RasterColor
[3]);
87 #if CHAN_TYPE == GL_FLOAT
93 span
->red
= IntToFixed(r
);
94 span
->green
= IntToFixed(g
);
95 span
->blue
= IntToFixed(b
);
96 span
->alpha
= IntToFixed(a
);
102 span
->interpMask
|= SPAN_RGBA
;
104 COPY_4V(span
->attrStart
[FRAG_ATTRIB_COL0
], ctx
->Current
.RasterColor
);
105 ASSIGN_4V(span
->attrStepX
[FRAG_ATTRIB_COL0
], 0.0, 0.0, 0.0, 0.0);
106 ASSIGN_4V(span
->attrStepY
[FRAG_ATTRIB_COL0
], 0.0, 0.0, 0.0, 0.0);
108 /* Secondary color */
109 if (ctx
->Light
.Enabled
|| ctx
->Fog
.ColorSumEnabled
)
111 COPY_4V(span
->attrStart
[FRAG_ATTRIB_COL1
], ctx
->Current
.RasterSecondaryColor
);
112 ASSIGN_4V(span
->attrStepX
[FRAG_ATTRIB_COL1
], 0.0, 0.0, 0.0, 0.0);
113 ASSIGN_4V(span
->attrStepY
[FRAG_ATTRIB_COL1
], 0.0, 0.0, 0.0, 0.0);
118 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
119 GLfloat fogVal
; /* a coord or a blend factor */
120 if (swrast
->_PreferPixelFog
) {
121 /* fog blend factors will be computed from fog coordinates per pixel */
122 fogVal
= ctx
->Current
.RasterDistance
;
125 /* fog blend factor should be computed from fogcoord now */
126 fogVal
= _swrast_z_to_fogfactor(ctx
, ctx
->Current
.RasterDistance
);
128 span
->attrStart
[FRAG_ATTRIB_FOGC
][0] = fogVal
;
129 span
->attrStepX
[FRAG_ATTRIB_FOGC
][0] = 0.0;
130 span
->attrStepY
[FRAG_ATTRIB_FOGC
][0] = 0.0;
136 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
137 const GLuint attr
= FRAG_ATTRIB_TEX0
+ i
;
138 const GLfloat
*tc
= ctx
->Current
.RasterTexCoords
[i
];
139 if (ctx
->FragmentProgram
._Current
|| ctx
->ATIFragmentShader
._Enabled
) {
140 COPY_4V(span
->attrStart
[attr
], tc
);
142 else if (tc
[3] > 0.0F
) {
143 /* use (s/q, t/q, r/q, 1) */
144 span
->attrStart
[attr
][0] = tc
[0] / tc
[3];
145 span
->attrStart
[attr
][1] = tc
[1] / tc
[3];
146 span
->attrStart
[attr
][2] = tc
[2] / tc
[3];
147 span
->attrStart
[attr
][3] = 1.0;
150 ASSIGN_4V(span
->attrStart
[attr
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
152 ASSIGN_4V(span
->attrStepX
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
153 ASSIGN_4V(span
->attrStepY
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
160 * Interpolate the active attributes (and'd with attrMask) to
161 * fill in span->array->attribs[].
162 * Perspective correction will be done. The point/line/triangle function
163 * should have computed attrStart/Step values for FRAG_ATTRIB_WPOS[3]!
166 interpolate_active_attribs(struct gl_context
*ctx
, SWspan
*span
, GLbitfield attrMask
)
168 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
171 * Don't overwrite existing array values, such as colors that may have
172 * been produced by glDraw/CopyPixels.
174 attrMask
&= ~span
->arrayAttribs
;
177 if (attrMask
& (1 << attr
)) {
178 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
179 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
180 const GLfloat dv0dx
= span
->attrStepX
[attr
][0];
181 const GLfloat dv1dx
= span
->attrStepX
[attr
][1];
182 const GLfloat dv2dx
= span
->attrStepX
[attr
][2];
183 const GLfloat dv3dx
= span
->attrStepX
[attr
][3];
184 GLfloat v0
= span
->attrStart
[attr
][0] + span
->leftClip
* dv0dx
;
185 GLfloat v1
= span
->attrStart
[attr
][1] + span
->leftClip
* dv1dx
;
186 GLfloat v2
= span
->attrStart
[attr
][2] + span
->leftClip
* dv2dx
;
187 GLfloat v3
= span
->attrStart
[attr
][3] + span
->leftClip
* dv3dx
;
189 for (k
= 0; k
< span
->end
; k
++) {
190 const GLfloat invW
= 1.0f
/ w
;
191 span
->array
->attribs
[attr
][k
][0] = v0
* invW
;
192 span
->array
->attribs
[attr
][k
][1] = v1
* invW
;
193 span
->array
->attribs
[attr
][k
][2] = v2
* invW
;
194 span
->array
->attribs
[attr
][k
][3] = v3
* invW
;
201 ASSERT((span
->arrayAttribs
& (1 << attr
)) == 0);
202 span
->arrayAttribs
|= (1 << attr
);
209 * Interpolate primary colors to fill in the span->array->rgba8 (or rgb16)
213 interpolate_int_colors(struct gl_context
*ctx
, SWspan
*span
)
216 const GLuint n
= span
->end
;
219 ASSERT(!(span
->arrayMask
& SPAN_RGBA
));
222 switch (span
->array
->ChanType
) {
224 case GL_UNSIGNED_BYTE
:
226 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
227 if (span
->interpMask
& SPAN_FLAT
) {
229 color
[RCOMP
] = FixedToInt(span
->red
);
230 color
[GCOMP
] = FixedToInt(span
->green
);
231 color
[BCOMP
] = FixedToInt(span
->blue
);
232 color
[ACOMP
] = FixedToInt(span
->alpha
);
233 for (i
= 0; i
< n
; i
++) {
234 COPY_4UBV(rgba
[i
], color
);
238 GLfixed r
= span
->red
;
239 GLfixed g
= span
->green
;
240 GLfixed b
= span
->blue
;
241 GLfixed a
= span
->alpha
;
242 GLint dr
= span
->redStep
;
243 GLint dg
= span
->greenStep
;
244 GLint db
= span
->blueStep
;
245 GLint da
= span
->alphaStep
;
246 for (i
= 0; i
< n
; i
++) {
247 rgba
[i
][RCOMP
] = FixedToChan(r
);
248 rgba
[i
][GCOMP
] = FixedToChan(g
);
249 rgba
[i
][BCOMP
] = FixedToChan(b
);
250 rgba
[i
][ACOMP
] = FixedToChan(a
);
259 case GL_UNSIGNED_SHORT
:
261 GLushort (*rgba
)[4] = span
->array
->rgba16
;
262 if (span
->interpMask
& SPAN_FLAT
) {
264 color
[RCOMP
] = FixedToInt(span
->red
);
265 color
[GCOMP
] = FixedToInt(span
->green
);
266 color
[BCOMP
] = FixedToInt(span
->blue
);
267 color
[ACOMP
] = FixedToInt(span
->alpha
);
268 for (i
= 0; i
< n
; i
++) {
269 COPY_4V(rgba
[i
], color
);
273 GLushort (*rgba
)[4] = span
->array
->rgba16
;
275 GLint dr
, dg
, db
, da
;
281 dg
= span
->greenStep
;
283 da
= span
->alphaStep
;
284 for (i
= 0; i
< n
; i
++) {
285 rgba
[i
][RCOMP
] = FixedToChan(r
);
286 rgba
[i
][GCOMP
] = FixedToChan(g
);
287 rgba
[i
][BCOMP
] = FixedToChan(b
);
288 rgba
[i
][ACOMP
] = FixedToChan(a
);
299 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL0
);
302 _mesa_problem(ctx
, "bad datatype 0x%x in interpolate_int_colors",
303 span
->array
->ChanType
);
305 span
->arrayMask
|= SPAN_RGBA
;
310 * Populate the FRAG_ATTRIB_COL0 array.
313 interpolate_float_colors(SWspan
*span
)
315 GLfloat (*col0
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
316 const GLuint n
= span
->end
;
319 assert(!(span
->arrayAttribs
& FRAG_BIT_COL0
));
321 if (span
->arrayMask
& SPAN_RGBA
) {
322 /* convert array of int colors */
323 for (i
= 0; i
< n
; i
++) {
324 col0
[i
][0] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][0]);
325 col0
[i
][1] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][1]);
326 col0
[i
][2] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][2]);
327 col0
[i
][3] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][3]);
331 /* interpolate red/green/blue/alpha to get float colors */
332 ASSERT(span
->interpMask
& SPAN_RGBA
);
333 if (span
->interpMask
& SPAN_FLAT
) {
334 GLfloat r
= FixedToFloat(span
->red
);
335 GLfloat g
= FixedToFloat(span
->green
);
336 GLfloat b
= FixedToFloat(span
->blue
);
337 GLfloat a
= FixedToFloat(span
->alpha
);
338 for (i
= 0; i
< n
; i
++) {
339 ASSIGN_4V(col0
[i
], r
, g
, b
, a
);
343 GLfloat r
= FixedToFloat(span
->red
);
344 GLfloat g
= FixedToFloat(span
->green
);
345 GLfloat b
= FixedToFloat(span
->blue
);
346 GLfloat a
= FixedToFloat(span
->alpha
);
347 GLfloat dr
= FixedToFloat(span
->redStep
);
348 GLfloat dg
= FixedToFloat(span
->greenStep
);
349 GLfloat db
= FixedToFloat(span
->blueStep
);
350 GLfloat da
= FixedToFloat(span
->alphaStep
);
351 for (i
= 0; i
< n
; i
++) {
364 span
->arrayAttribs
|= FRAG_BIT_COL0
;
365 span
->array
->ChanType
= GL_FLOAT
;
371 * Fill in the span.zArray array from the span->z, zStep values.
374 _swrast_span_interpolate_z( const struct gl_context
*ctx
, SWspan
*span
)
376 const GLuint n
= span
->end
;
379 ASSERT(!(span
->arrayMask
& SPAN_Z
));
381 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
382 GLfixed zval
= span
->z
;
383 GLuint
*z
= span
->array
->z
;
384 for (i
= 0; i
< n
; i
++) {
385 z
[i
] = FixedToInt(zval
);
390 /* Deep Z buffer, no fixed->int shift */
391 GLuint zval
= span
->z
;
392 GLuint
*z
= span
->array
->z
;
393 for (i
= 0; i
< n
; i
++) {
398 span
->interpMask
&= ~SPAN_Z
;
399 span
->arrayMask
|= SPAN_Z
;
404 * Compute mipmap LOD from partial derivatives.
405 * This the ideal solution, as given in the OpenGL spec.
408 _swrast_compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
409 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
410 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
412 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
413 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
414 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
415 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
416 GLfloat x
= SQRTF(dudx
* dudx
+ dvdx
* dvdx
);
417 GLfloat y
= SQRTF(dudy
* dudy
+ dvdy
* dvdy
);
418 GLfloat rho
= MAX2(x
, y
);
419 GLfloat lambda
= LOG2(rho
);
425 * Compute mipmap LOD from partial derivatives.
426 * This is a faster approximation than above function.
430 _swrast_compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
431 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
432 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
434 GLfloat dsdx2
= (s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
;
435 GLfloat dtdx2
= (t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
;
436 GLfloat dsdy2
= (s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
;
437 GLfloat dtdy2
= (t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
;
438 GLfloat maxU
, maxV
, rho
, lambda
;
439 dsdx2
= FABSF(dsdx2
);
440 dsdy2
= FABSF(dsdy2
);
441 dtdx2
= FABSF(dtdx2
);
442 dtdy2
= FABSF(dtdy2
);
443 maxU
= MAX2(dsdx2
, dsdy2
) * texW
;
444 maxV
= MAX2(dtdx2
, dtdy2
) * texH
;
445 rho
= MAX2(maxU
, maxV
);
453 * Fill in the span.array->attrib[FRAG_ATTRIB_TEXn] arrays from the
454 * using the attrStart/Step values.
456 * This function only used during fixed-function fragment processing.
458 * Note: in the places where we divide by Q (or mult by invQ) we're
459 * really doing two things: perspective correction and texcoord
460 * projection. Remember, for texcoord (s,t,r,q) we need to index
461 * texels with (s/q, t/q, r/q).
464 interpolate_texcoords(struct gl_context
*ctx
, SWspan
*span
)
467 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
470 /* XXX CoordUnits vs. ImageUnits */
471 for (u
= 0; u
< maxUnit
; u
++) {
472 if (ctx
->Texture
._EnabledCoordUnits
& (1 << u
)) {
473 const GLuint attr
= FRAG_ATTRIB_TEX0
+ u
;
474 const struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[u
]._Current
;
476 GLboolean needLambda
;
477 GLfloat (*texcoord
)[4] = span
->array
->attribs
[attr
];
478 GLfloat
*lambda
= span
->array
->lambda
[u
];
479 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
480 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
481 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
482 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
483 const GLfloat drdx
= span
->attrStepX
[attr
][2];
484 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
485 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
486 GLfloat s
= span
->attrStart
[attr
][0] + span
->leftClip
* dsdx
;
487 GLfloat t
= span
->attrStart
[attr
][1] + span
->leftClip
* dtdx
;
488 GLfloat r
= span
->attrStart
[attr
][2] + span
->leftClip
* drdx
;
489 GLfloat q
= span
->attrStart
[attr
][3] + span
->leftClip
* dqdx
;
492 const struct gl_texture_image
*img
= obj
->Image
[0][obj
->BaseLevel
];
493 const struct swrast_texture_image
*swImg
=
494 swrast_texture_image_const(img
);
496 needLambda
= (obj
->Sampler
.MinFilter
!= obj
->Sampler
.MagFilter
)
497 || ctx
->FragmentProgram
._Current
;
498 /* LOD is calculated directly in the ansiotropic filter, we can
499 * skip the normal lambda function as the result is ignored.
501 if (obj
->Sampler
.MaxAnisotropy
> 1.0 &&
502 obj
->Sampler
.MinFilter
== GL_LINEAR_MIPMAP_LINEAR
) {
503 needLambda
= GL_FALSE
;
505 texW
= swImg
->WidthScale
;
506 texH
= swImg
->HeightScale
;
509 /* using a fragment program */
512 needLambda
= GL_FALSE
;
517 if (ctx
->FragmentProgram
._Current
518 || ctx
->ATIFragmentShader
._Enabled
) {
519 /* do perspective correction but don't divide s, t, r by q */
520 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
521 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3] + span
->leftClip
* dwdx
;
522 for (i
= 0; i
< span
->end
; i
++) {
523 const GLfloat invW
= 1.0F
/ w
;
524 texcoord
[i
][0] = s
* invW
;
525 texcoord
[i
][1] = t
* invW
;
526 texcoord
[i
][2] = r
* invW
;
527 texcoord
[i
][3] = q
* invW
;
528 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
529 dqdx
, dqdy
, texW
, texH
,
539 for (i
= 0; i
< span
->end
; i
++) {
540 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
541 texcoord
[i
][0] = s
* invQ
;
542 texcoord
[i
][1] = t
* invQ
;
543 texcoord
[i
][2] = r
* invQ
;
545 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
546 dqdx
, dqdy
, texW
, texH
,
554 span
->arrayMask
|= SPAN_LAMBDA
;
558 if (ctx
->FragmentProgram
._Current
||
559 ctx
->ATIFragmentShader
._Enabled
) {
560 /* do perspective correction but don't divide s, t, r by q */
561 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
562 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3] + span
->leftClip
* dwdx
;
563 for (i
= 0; i
< span
->end
; i
++) {
564 const GLfloat invW
= 1.0F
/ w
;
565 texcoord
[i
][0] = s
* invW
;
566 texcoord
[i
][1] = t
* invW
;
567 texcoord
[i
][2] = r
* invW
;
568 texcoord
[i
][3] = q
* invW
;
577 else if (dqdx
== 0.0F
) {
578 /* Ortho projection or polygon's parallel to window X axis */
579 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
580 for (i
= 0; i
< span
->end
; i
++) {
581 texcoord
[i
][0] = s
* invQ
;
582 texcoord
[i
][1] = t
* invQ
;
583 texcoord
[i
][2] = r
* invQ
;
592 for (i
= 0; i
< span
->end
; i
++) {
593 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
594 texcoord
[i
][0] = s
* invQ
;
595 texcoord
[i
][1] = t
* invQ
;
596 texcoord
[i
][2] = r
* invQ
;
612 * Fill in the arrays->attribs[FRAG_ATTRIB_WPOS] array.
615 interpolate_wpos(struct gl_context
*ctx
, SWspan
*span
)
617 GLfloat (*wpos
)[4] = span
->array
->attribs
[FRAG_ATTRIB_WPOS
];
619 const GLfloat zScale
= 1.0F
/ ctx
->DrawBuffer
->_DepthMaxF
;
622 if (span
->arrayMask
& SPAN_XY
) {
623 for (i
= 0; i
< span
->end
; i
++) {
624 wpos
[i
][0] = (GLfloat
) span
->array
->x
[i
];
625 wpos
[i
][1] = (GLfloat
) span
->array
->y
[i
];
629 for (i
= 0; i
< span
->end
; i
++) {
630 wpos
[i
][0] = (GLfloat
) span
->x
+ i
;
631 wpos
[i
][1] = (GLfloat
) span
->y
;
635 dw
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
636 w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3] + span
->leftClip
* dw
;
637 for (i
= 0; i
< span
->end
; i
++) {
638 wpos
[i
][2] = (GLfloat
) span
->array
->z
[i
] * zScale
;
646 * Apply the current polygon stipple pattern to a span of pixels.
649 stipple_polygon_span(struct gl_context
*ctx
, SWspan
*span
)
651 GLubyte
*mask
= span
->array
->mask
;
653 ASSERT(ctx
->Polygon
.StippleFlag
);
655 if (span
->arrayMask
& SPAN_XY
) {
656 /* arrays of x/y pixel coords */
658 for (i
= 0; i
< span
->end
; i
++) {
659 const GLint col
= span
->array
->x
[i
] % 32;
660 const GLint row
= span
->array
->y
[i
] % 32;
661 const GLuint stipple
= ctx
->PolygonStipple
[row
];
662 if (((1 << col
) & stipple
) == 0) {
668 /* horizontal span of pixels */
669 const GLuint highBit
= 1 << 31;
670 const GLuint stipple
= ctx
->PolygonStipple
[span
->y
% 32];
671 GLuint i
, m
= highBit
>> (GLuint
) (span
->x
% 32);
672 for (i
= 0; i
< span
->end
; i
++) {
673 if ((m
& stipple
) == 0) {
682 span
->writeAll
= GL_FALSE
;
687 * Clip a pixel span to the current buffer/window boundaries:
688 * DrawBuffer->_Xmin, _Xmax, _Ymin, _Ymax. This will accomplish
689 * window clipping and scissoring.
690 * Return: GL_TRUE some pixels still visible
691 * GL_FALSE nothing visible
694 clip_span( struct gl_context
*ctx
, SWspan
*span
)
696 const GLint xmin
= ctx
->DrawBuffer
->_Xmin
;
697 const GLint xmax
= ctx
->DrawBuffer
->_Xmax
;
698 const GLint ymin
= ctx
->DrawBuffer
->_Ymin
;
699 const GLint ymax
= ctx
->DrawBuffer
->_Ymax
;
703 if (span
->arrayMask
& SPAN_XY
) {
704 /* arrays of x/y pixel coords */
705 const GLint
*x
= span
->array
->x
;
706 const GLint
*y
= span
->array
->y
;
707 const GLint n
= span
->end
;
708 GLubyte
*mask
= span
->array
->mask
;
710 if (span
->arrayMask
& SPAN_MASK
) {
711 /* note: using & intead of && to reduce branches */
712 for (i
= 0; i
< n
; i
++) {
713 mask
[i
] &= (x
[i
] >= xmin
) & (x
[i
] < xmax
)
714 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
718 /* note: using & intead of && to reduce branches */
719 for (i
= 0; i
< n
; i
++) {
720 mask
[i
] = (x
[i
] >= xmin
) & (x
[i
] < xmax
)
721 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
724 return GL_TRUE
; /* some pixels visible */
727 /* horizontal span of pixels */
728 const GLint x
= span
->x
;
729 const GLint y
= span
->y
;
732 /* Trivial rejection tests */
733 if (y
< ymin
|| y
>= ymax
|| x
+ n
<= xmin
|| x
>= xmax
) {
735 return GL_FALSE
; /* all pixels clipped */
741 n
= span
->end
= xmax
- x
;
744 /* Clip to the left */
746 const GLint leftClip
= xmin
- x
;
749 ASSERT(leftClip
> 0);
750 ASSERT(x
+ n
> xmin
);
752 /* Clip 'leftClip' pixels from the left side.
753 * The span->leftClip field will be applied when we interpolate
754 * fragment attributes.
755 * For arrays of values, shift them left.
757 for (i
= 0; i
< FRAG_ATTRIB_MAX
; i
++) {
758 if (span
->interpMask
& (1 << i
)) {
760 for (j
= 0; j
< 4; j
++) {
761 span
->attrStart
[i
][j
] += leftClip
* span
->attrStepX
[i
][j
];
766 span
->red
+= leftClip
* span
->redStep
;
767 span
->green
+= leftClip
* span
->greenStep
;
768 span
->blue
+= leftClip
* span
->blueStep
;
769 span
->alpha
+= leftClip
* span
->alphaStep
;
770 span
->index
+= leftClip
* span
->indexStep
;
771 span
->z
+= leftClip
* span
->zStep
;
772 span
->intTex
[0] += leftClip
* span
->intTexStep
[0];
773 span
->intTex
[1] += leftClip
* span
->intTexStep
[1];
775 #define SHIFT_ARRAY(ARRAY, SHIFT, LEN) \
776 memcpy(ARRAY, ARRAY + (SHIFT), (LEN) * sizeof(ARRAY[0]))
778 for (i
= 0; i
< FRAG_ATTRIB_MAX
; i
++) {
779 if (span
->arrayAttribs
& (1 << i
)) {
780 /* shift array elements left by 'leftClip' */
781 SHIFT_ARRAY(span
->array
->attribs
[i
], leftClip
, n
- leftClip
);
785 SHIFT_ARRAY(span
->array
->mask
, leftClip
, n
- leftClip
);
786 SHIFT_ARRAY(span
->array
->rgba8
, leftClip
, n
- leftClip
);
787 SHIFT_ARRAY(span
->array
->rgba16
, leftClip
, n
- leftClip
);
788 SHIFT_ARRAY(span
->array
->x
, leftClip
, n
- leftClip
);
789 SHIFT_ARRAY(span
->array
->y
, leftClip
, n
- leftClip
);
790 SHIFT_ARRAY(span
->array
->z
, leftClip
, n
- leftClip
);
791 SHIFT_ARRAY(span
->array
->index
, leftClip
, n
- leftClip
);
792 for (i
= 0; i
< MAX_TEXTURE_COORD_UNITS
; i
++) {
793 SHIFT_ARRAY(span
->array
->lambda
[i
], leftClip
, n
- leftClip
);
795 SHIFT_ARRAY(span
->array
->coverage
, leftClip
, n
- leftClip
);
799 span
->leftClip
= leftClip
;
801 span
->end
-= leftClip
;
802 span
->writeAll
= GL_FALSE
;
805 ASSERT(span
->x
>= xmin
);
806 ASSERT(span
->x
+ span
->end
<= xmax
);
807 ASSERT(span
->y
>= ymin
);
808 ASSERT(span
->y
< ymax
);
810 return GL_TRUE
; /* some pixels visible */
816 * Add specular colors to primary colors.
817 * Only called during fixed-function operation.
818 * Result is float color array (FRAG_ATTRIB_COL0).
821 add_specular(struct gl_context
*ctx
, SWspan
*span
)
823 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
824 const GLubyte
*mask
= span
->array
->mask
;
825 GLfloat (*col0
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
826 GLfloat (*col1
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL1
];
829 ASSERT(!ctx
->FragmentProgram
._Current
);
830 ASSERT(span
->arrayMask
& SPAN_RGBA
);
831 ASSERT(swrast
->_ActiveAttribMask
& FRAG_BIT_COL1
);
832 (void) swrast
; /* silence warning */
834 if (span
->array
->ChanType
== GL_FLOAT
) {
835 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
836 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL0
);
840 /* need float colors */
841 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
842 interpolate_float_colors(span
);
846 if ((span
->arrayAttribs
& FRAG_BIT_COL1
) == 0) {
847 /* XXX could avoid this and interpolate COL1 in the loop below */
848 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL1
);
851 ASSERT(span
->arrayAttribs
& FRAG_BIT_COL0
);
852 ASSERT(span
->arrayAttribs
& FRAG_BIT_COL1
);
854 for (i
= 0; i
< span
->end
; i
++) {
856 col0
[i
][0] += col1
[i
][0];
857 col0
[i
][1] += col1
[i
][1];
858 col0
[i
][2] += col1
[i
][2];
862 span
->array
->ChanType
= GL_FLOAT
;
867 * Apply antialiasing coverage value to alpha values.
870 apply_aa_coverage(SWspan
*span
)
872 const GLfloat
*coverage
= span
->array
->coverage
;
874 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
875 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
876 for (i
= 0; i
< span
->end
; i
++) {
877 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
878 rgba
[i
][ACOMP
] = (GLubyte
) CLAMP(a
, 0.0, 255.0);
879 ASSERT(coverage
[i
] >= 0.0);
880 ASSERT(coverage
[i
] <= 1.0);
883 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
884 GLushort (*rgba
)[4] = span
->array
->rgba16
;
885 for (i
= 0; i
< span
->end
; i
++) {
886 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
887 rgba
[i
][ACOMP
] = (GLushort
) CLAMP(a
, 0.0, 65535.0);
891 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
892 for (i
= 0; i
< span
->end
; i
++) {
893 rgba
[i
][ACOMP
] = rgba
[i
][ACOMP
] * coverage
[i
];
901 * Clamp span's float colors to [0,1]
904 clamp_colors(SWspan
*span
)
906 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
908 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
909 for (i
= 0; i
< span
->end
; i
++) {
910 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0F
, 1.0F
);
911 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0F
, 1.0F
);
912 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0F
, 1.0F
);
913 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0F
, 1.0F
);
919 * Convert the span's color arrays to the given type.
920 * The only way 'output' can be greater than zero is when we have a fragment
921 * program that writes to gl_FragData[1] or higher.
922 * \param output which fragment program color output is being processed
925 convert_color_type(SWspan
*span
, GLenum newType
, GLuint output
)
929 if (output
> 0 || span
->array
->ChanType
== GL_FLOAT
) {
930 src
= span
->array
->attribs
[FRAG_ATTRIB_COL0
+ output
];
931 span
->array
->ChanType
= GL_FLOAT
;
933 else if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
934 src
= span
->array
->rgba8
;
937 ASSERT(span
->array
->ChanType
== GL_UNSIGNED_SHORT
);
938 src
= span
->array
->rgba16
;
941 if (newType
== GL_UNSIGNED_BYTE
) {
942 dst
= span
->array
->rgba8
;
944 else if (newType
== GL_UNSIGNED_SHORT
) {
945 dst
= span
->array
->rgba16
;
948 dst
= span
->array
->attribs
[FRAG_ATTRIB_COL0
];
951 _mesa_convert_colors(span
->array
->ChanType
, src
,
953 span
->end
, span
->array
->mask
);
955 span
->array
->ChanType
= newType
;
956 span
->array
->rgba
= dst
;
962 * Apply fragment shader, fragment program or normal texturing to span.
965 shade_texture_span(struct gl_context
*ctx
, SWspan
*span
)
967 if (ctx
->FragmentProgram
._Current
||
968 ctx
->ATIFragmentShader
._Enabled
) {
969 /* programmable shading */
970 if (span
->primitive
== GL_BITMAP
&& span
->array
->ChanType
!= GL_FLOAT
) {
971 convert_color_type(span
, GL_FLOAT
, 0);
974 span
->array
->rgba
= (void *) span
->array
->attribs
[FRAG_ATTRIB_COL0
];
977 if (span
->primitive
!= GL_POINT
||
978 (span
->interpMask
& SPAN_RGBA
) ||
979 ctx
->Point
.PointSprite
) {
980 /* for single-pixel points, we populated the arrays already */
981 interpolate_active_attribs(ctx
, span
, ~0);
983 span
->array
->ChanType
= GL_FLOAT
;
985 if (!(span
->arrayMask
& SPAN_Z
))
986 _swrast_span_interpolate_z (ctx
, span
);
989 if (inputsRead
& FRAG_BIT_WPOS
)
991 /* XXX always interpolate wpos so that DDX/DDY work */
993 interpolate_wpos(ctx
, span
);
995 /* Run fragment program/shader now */
996 if (ctx
->FragmentProgram
._Current
) {
997 _swrast_exec_fragment_program(ctx
, span
);
1000 ASSERT(ctx
->ATIFragmentShader
._Enabled
);
1001 _swrast_exec_fragment_shader(ctx
, span
);
1004 else if (ctx
->Texture
._EnabledCoordUnits
) {
1005 /* conventional texturing */
1008 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1009 interpolate_int_colors(ctx
, span
);
1012 if (!(span
->arrayMask
& SPAN_RGBA
))
1013 interpolate_int_colors(ctx
, span
);
1015 if ((span
->arrayAttribs
& FRAG_BITS_TEX_ANY
) == 0x0)
1016 interpolate_texcoords(ctx
, span
);
1018 _swrast_texture_span(ctx
, span
);
1025 * Apply all the per-fragment operations to a span.
1026 * This now includes texturing (_swrast_write_texture_span() is history).
1027 * This function may modify any of the array values in the span.
1028 * span->interpMask and span->arrayMask may be changed but will be restored
1029 * to their original values before returning.
1032 _swrast_write_rgba_span( struct gl_context
*ctx
, SWspan
*span
)
1034 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1035 const GLuint
*colorMask
= (GLuint
*) ctx
->Color
.ColorMask
;
1036 const GLbitfield origInterpMask
= span
->interpMask
;
1037 const GLbitfield origArrayMask
= span
->arrayMask
;
1038 const GLbitfield origArrayAttribs
= span
->arrayAttribs
;
1039 const GLenum origChanType
= span
->array
->ChanType
;
1040 void * const origRgba
= span
->array
->rgba
;
1041 const GLboolean shader
= (ctx
->FragmentProgram
._Current
1042 || ctx
->ATIFragmentShader
._Enabled
);
1043 const GLboolean shaderOrTexture
= shader
|| ctx
->Texture
._EnabledCoordUnits
;
1044 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1047 printf("%s() interp 0x%x array 0x%x\n", __FUNCTION__,
1048 span->interpMask, span->arrayMask);
1051 ASSERT(span
->primitive
== GL_POINT
||
1052 span
->primitive
== GL_LINE
||
1053 span
->primitive
== GL_POLYGON
||
1054 span
->primitive
== GL_BITMAP
);
1056 /* Fragment write masks */
1057 if (span
->arrayMask
& SPAN_MASK
) {
1058 /* mask was initialized by caller, probably glBitmap */
1059 span
->writeAll
= GL_FALSE
;
1062 memset(span
->array
->mask
, 1, span
->end
);
1063 span
->writeAll
= GL_TRUE
;
1066 /* Clip to window/scissor box */
1067 if (!clip_span(ctx
, span
)) {
1071 ASSERT(span
->end
<= MAX_WIDTH
);
1073 /* Depth bounds test */
1074 if (ctx
->Depth
.BoundsTest
&& fb
->Visual
.depthBits
> 0) {
1075 if (!_swrast_depth_bounds_test(ctx
, span
)) {
1081 /* Make sure all fragments are within window bounds */
1082 if (span
->arrayMask
& SPAN_XY
) {
1083 /* array of pixel locations */
1085 for (i
= 0; i
< span
->end
; i
++) {
1086 if (span
->array
->mask
[i
]) {
1087 assert(span
->array
->x
[i
] >= fb
->_Xmin
);
1088 assert(span
->array
->x
[i
] < fb
->_Xmax
);
1089 assert(span
->array
->y
[i
] >= fb
->_Ymin
);
1090 assert(span
->array
->y
[i
] < fb
->_Ymax
);
1096 /* Polygon Stippling */
1097 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
1098 stipple_polygon_span(ctx
, span
);
1101 /* This is the normal place to compute the fragment color/Z
1102 * from texturing or shading.
1104 if (shaderOrTexture
&& !swrast
->_DeferredTexture
) {
1105 shade_texture_span(ctx
, span
);
1108 /* Do the alpha test */
1109 if (ctx
->Color
.AlphaEnabled
) {
1110 if (!_swrast_alpha_test(ctx
, span
)) {
1111 /* all fragments failed test */
1116 /* Stencil and Z testing */
1117 if (ctx
->Stencil
._Enabled
|| ctx
->Depth
.Test
) {
1118 if (!(span
->arrayMask
& SPAN_Z
))
1119 _swrast_span_interpolate_z(ctx
, span
);
1121 if (ctx
->Transform
.DepthClamp
)
1122 _swrast_depth_clamp_span(ctx
, span
);
1124 if (ctx
->Stencil
._Enabled
) {
1125 /* Combined Z/stencil tests */
1126 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
1127 /* all fragments failed test */
1131 else if (fb
->Visual
.depthBits
> 0) {
1132 /* Just regular depth testing */
1133 ASSERT(ctx
->Depth
.Test
);
1134 ASSERT(span
->arrayMask
& SPAN_Z
);
1135 if (!_swrast_depth_test_span(ctx
, span
)) {
1136 /* all fragments failed test */
1142 if (ctx
->Query
.CurrentOcclusionObject
) {
1143 /* update count of 'passed' fragments */
1144 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
1146 for (i
= 0; i
< span
->end
; i
++)
1147 q
->Result
+= span
->array
->mask
[i
];
1150 /* We had to wait until now to check for glColorMask(0,0,0,0) because of
1151 * the occlusion test.
1153 if (fb
->_NumColorDrawBuffers
== 1 && colorMask
[0] == 0x0) {
1154 /* no colors to write */
1158 /* If we were able to defer fragment color computation to now, there's
1159 * a good chance that many fragments will have already been killed by
1160 * Z/stencil testing.
1162 if (shaderOrTexture
&& swrast
->_DeferredTexture
) {
1163 shade_texture_span(ctx
, span
);
1167 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1168 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL0
);
1171 if ((span
->arrayMask
& SPAN_RGBA
) == 0) {
1172 interpolate_int_colors(ctx
, span
);
1176 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1178 if (span
->primitive
== GL_BITMAP
|| !swrast
->SpecularVertexAdd
) {
1179 /* Add primary and specular (diffuse + specular) colors */
1181 if (ctx
->Fog
.ColorSumEnabled
||
1182 (ctx
->Light
.Enabled
&&
1183 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)) {
1184 add_specular(ctx
, span
);
1190 if (swrast
->_FogEnabled
) {
1191 _swrast_fog_rgba_span(ctx
, span
);
1194 /* Antialias coverage application */
1195 if (span
->arrayMask
& SPAN_COVERAGE
) {
1196 apply_aa_coverage(span
);
1199 /* Clamp color/alpha values over the range [0.0, 1.0] before storage */
1200 if (ctx
->Color
.ClampFragmentColor
== GL_TRUE
&&
1201 span
->array
->ChanType
== GL_FLOAT
) {
1206 * Write to renderbuffers.
1207 * Depending on glDrawBuffer() state and the which color outputs are
1208 * written by the fragment shader, we may either replicate one color to
1209 * all renderbuffers or write a different color to each renderbuffer.
1210 * multiFragOutputs=TRUE for the later case.
1213 const GLuint numBuffers
= fb
->_NumColorDrawBuffers
;
1214 const struct gl_fragment_program
*fp
= ctx
->FragmentProgram
._Current
;
1215 const GLboolean multiFragOutputs
=
1216 (fp
&& fp
->Base
.OutputsWritten
>= (1 << FRAG_RESULT_DATA0
));
1219 for (buf
= 0; buf
< numBuffers
; buf
++) {
1220 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[buf
];
1222 /* color[fragOutput] will be written to buffer[buf] */
1225 GLchan rgbaSave
[MAX_WIDTH
][4];
1226 const GLuint fragOutput
= multiFragOutputs
? buf
: 0;
1228 /* set span->array->rgba to colors for render buffer's datatype */
1229 if (rb
->DataType
!= span
->array
->ChanType
|| fragOutput
> 0) {
1230 convert_color_type(span
, rb
->DataType
, fragOutput
);
1233 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1234 span
->array
->rgba
= span
->array
->rgba8
;
1236 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1237 span
->array
->rgba
= (void *) span
->array
->rgba16
;
1240 span
->array
->rgba
= (void *)
1241 span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1245 if (!multiFragOutputs
&& numBuffers
> 1) {
1246 /* save colors for second, third renderbuffer writes */
1247 memcpy(rgbaSave
, span
->array
->rgba
,
1248 4 * span
->end
* sizeof(GLchan
));
1251 ASSERT(rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
||
1252 rb
->_BaseFormat
== GL_ALPHA
);
1254 if (ctx
->Color
.ColorLogicOpEnabled
) {
1255 _swrast_logicop_rgba_span(ctx
, rb
, span
);
1257 else if ((ctx
->Color
.BlendEnabled
>> buf
) & 1) {
1258 _swrast_blend_span(ctx
, rb
, span
);
1261 if (colorMask
[buf
] != 0xffffffff) {
1262 _swrast_mask_rgba_span(ctx
, rb
, span
, buf
);
1265 if (span
->arrayMask
& SPAN_XY
) {
1266 /* array of pixel coords */
1267 ASSERT(rb
->PutValues
);
1268 rb
->PutValues(ctx
, rb
, span
->end
,
1269 span
->array
->x
, span
->array
->y
,
1270 span
->array
->rgba
, span
->array
->mask
);
1273 /* horizontal run of pixels */
1275 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1277 span
->writeAll
? NULL
: span
->array
->mask
);
1280 if (!multiFragOutputs
&& numBuffers
> 1) {
1281 /* restore original span values */
1282 memcpy(span
->array
->rgba
, rgbaSave
,
1283 4 * span
->end
* sizeof(GLchan
));
1291 /* restore these values before returning */
1292 span
->interpMask
= origInterpMask
;
1293 span
->arrayMask
= origArrayMask
;
1294 span
->arrayAttribs
= origArrayAttribs
;
1295 span
->array
->ChanType
= origChanType
;
1296 span
->array
->rgba
= origRgba
;
1301 * Read RGBA pixels from a renderbuffer. Clipping will be done to prevent
1302 * reading ouside the buffer's boundaries.
1303 * \param dstType datatype for returned colors
1304 * \param rgba the returned colors
1307 _swrast_read_rgba_span( struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1308 GLuint n
, GLint x
, GLint y
, GLenum dstType
,
1311 const GLint bufWidth
= (GLint
) rb
->Width
;
1312 const GLint bufHeight
= (GLint
) rb
->Height
;
1314 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1315 /* completely above, below, or right */
1316 /* XXX maybe leave rgba values undefined? */
1317 memset(rgba
, 0, 4 * n
* sizeof(GLchan
));
1322 /* left edge clipping */
1324 length
= (GLint
) n
- skip
;
1326 /* completely left of window */
1329 if (length
> bufWidth
) {
1333 else if ((GLint
) (x
+ n
) > bufWidth
) {
1334 /* right edge clipping */
1336 length
= bufWidth
- x
;
1338 /* completely to right of window */
1350 ASSERT(rb
->_BaseFormat
== GL_RGBA
||
1351 rb
->_BaseFormat
== GL_RGB
||
1352 rb
->_BaseFormat
== GL_RG
||
1353 rb
->_BaseFormat
== GL_RED
||
1354 rb
->_BaseFormat
== GL_LUMINANCE
||
1355 rb
->_BaseFormat
== GL_INTENSITY
||
1356 rb
->_BaseFormat
== GL_LUMINANCE_ALPHA
||
1357 rb
->_BaseFormat
== GL_ALPHA
);
1359 if (rb
->DataType
== dstType
) {
1360 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
,
1361 (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(rb
->DataType
));
1364 GLuint temp
[MAX_WIDTH
* 4];
1365 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, temp
);
1366 _mesa_convert_colors(rb
->DataType
, temp
,
1367 dstType
, (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(dstType
),
1375 * Wrapper for gl_renderbuffer::GetValues() which does clipping to avoid
1376 * reading values outside the buffer bounds.
1377 * We can use this for reading any format/type of renderbuffer.
1378 * \param valueSize is the size in bytes of each value (pixel) put into the
1382 _swrast_get_values(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1383 GLuint count
, const GLint x
[], const GLint y
[],
1384 void *values
, GLuint valueSize
)
1386 GLuint i
, inCount
= 0, inStart
= 0;
1388 for (i
= 0; i
< count
; i
++) {
1389 if (x
[i
] >= 0 && y
[i
] >= 0 &&
1390 x
[i
] < (GLint
) rb
->Width
&& y
[i
] < (GLint
) rb
->Height
) {
1398 /* read [inStart, inStart + inCount) */
1399 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1400 (GLubyte
*) values
+ inStart
* valueSize
);
1406 /* read last values */
1407 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1408 (GLubyte
*) values
+ inStart
* valueSize
);
1414 * Wrapper for gl_renderbuffer::PutRow() which does clipping.
1415 * \param valueSize size of each value (pixel) in bytes
1418 _swrast_put_row(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1419 GLuint count
, GLint x
, GLint y
,
1420 const GLvoid
*values
, GLuint valueSize
)
1424 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1425 return; /* above or below */
1427 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1428 return; /* entirely left or right */
1430 if ((GLint
) (x
+ count
) > (GLint
) rb
->Width
) {
1432 GLint clip
= x
+ count
- rb
->Width
;
1443 rb
->PutRow(ctx
, rb
, count
, x
, y
,
1444 (const GLubyte
*) values
+ skip
* valueSize
, NULL
);
1449 * Wrapper for gl_renderbuffer::GetRow() which does clipping.
1450 * \param valueSize size of each value (pixel) in bytes
1453 _swrast_get_row(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1454 GLuint count
, GLint x
, GLint y
,
1455 GLvoid
*values
, GLuint valueSize
)
1459 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1460 return; /* above or below */
1462 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1463 return; /* entirely left or right */
1465 if (x
+ count
> rb
->Width
) {
1467 GLint clip
= x
+ count
- rb
->Width
;
1478 rb
->GetRow(ctx
, rb
, count
, x
, y
, (GLubyte
*) values
+ skip
* valueSize
);
1483 * Get RGBA pixels from the given renderbuffer.
1484 * Used by blending, logicop and masking functions.
1485 * \return pointer to the colors we read.
1488 _swrast_get_dest_rgba(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1491 const GLuint pixelSize
= RGBA_PIXEL_SIZE(span
->array
->ChanType
);
1494 /* Point rbPixels to a temporary space */
1495 rbPixels
= span
->array
->attribs
[FRAG_ATTRIB_MAX
- 1];
1497 /* Get destination values from renderbuffer */
1498 if (span
->arrayMask
& SPAN_XY
) {
1499 _swrast_get_values(ctx
, rb
, span
->end
, span
->array
->x
, span
->array
->y
,
1500 rbPixels
, pixelSize
);
1503 _swrast_get_row(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1504 rbPixels
, pixelSize
);