2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * \file swrast/s_span.c
28 * \brief Span processing functions used by all rasterization functions.
29 * This is where all the per-fragment tests are performed
40 #include "s_atifragshader.h"
43 #include "s_context.h"
47 #include "s_masking.h"
48 #include "s_fragprog.h"
50 #include "s_stencil.h"
51 #include "s_texcombine.h"
55 * Set default fragment attributes for the span using the
56 * current raster values. Used prior to glDraw/CopyPixels
60 _swrast_span_default_attribs(GLcontext
*ctx
, SWspan
*span
)
64 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
65 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16)
66 span
->z
= FloatToFixed(ctx
->Current
.RasterPos
[2] * depthMax
+ 0.5F
);
68 span
->z
= (GLint
) (ctx
->Current
.RasterPos
[2] * depthMax
+ 0.5F
);
70 span
->interpMask
|= SPAN_Z
;
73 /* W (for perspective correction) */
74 span
->attrStart
[FRAG_ATTRIB_WPOS
][3] = 1.0;
75 span
->attrStepX
[FRAG_ATTRIB_WPOS
][3] = 0.0;
76 span
->attrStepY
[FRAG_ATTRIB_WPOS
][3] = 0.0;
78 /* primary color, or color index */
79 if (ctx
->Visual
.rgbMode
) {
81 UNCLAMPED_FLOAT_TO_CHAN(r
, ctx
->Current
.RasterColor
[0]);
82 UNCLAMPED_FLOAT_TO_CHAN(g
, ctx
->Current
.RasterColor
[1]);
83 UNCLAMPED_FLOAT_TO_CHAN(b
, ctx
->Current
.RasterColor
[2]);
84 UNCLAMPED_FLOAT_TO_CHAN(a
, ctx
->Current
.RasterColor
[3]);
85 #if CHAN_TYPE == GL_FLOAT
91 span
->red
= IntToFixed(r
);
92 span
->green
= IntToFixed(g
);
93 span
->blue
= IntToFixed(b
);
94 span
->alpha
= IntToFixed(a
);
100 span
->interpMask
|= SPAN_RGBA
;
102 COPY_4V(span
->attrStart
[FRAG_ATTRIB_COL0
], ctx
->Current
.RasterColor
);
103 ASSIGN_4V(span
->attrStepX
[FRAG_ATTRIB_COL0
], 0.0, 0.0, 0.0, 0.0);
104 ASSIGN_4V(span
->attrStepY
[FRAG_ATTRIB_COL0
], 0.0, 0.0, 0.0, 0.0);
107 span
->index
= FloatToFixed(ctx
->Current
.RasterIndex
);
109 span
->interpMask
|= SPAN_INDEX
;
112 /* Secondary color */
113 if (ctx
->Visual
.rgbMode
&& (ctx
->Light
.Enabled
|| ctx
->Fog
.ColorSumEnabled
))
115 COPY_4V(span
->attrStart
[FRAG_ATTRIB_COL1
], ctx
->Current
.RasterSecondaryColor
);
116 ASSIGN_4V(span
->attrStepX
[FRAG_ATTRIB_COL1
], 0.0, 0.0, 0.0, 0.0);
117 ASSIGN_4V(span
->attrStepY
[FRAG_ATTRIB_COL1
], 0.0, 0.0, 0.0, 0.0);
122 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
123 GLfloat fogVal
; /* a coord or a blend factor */
124 if (swrast
->_PreferPixelFog
) {
125 /* fog blend factors will be computed from fog coordinates per pixel */
126 fogVal
= ctx
->Current
.RasterDistance
;
129 /* fog blend factor should be computed from fogcoord now */
130 fogVal
= _swrast_z_to_fogfactor(ctx
, ctx
->Current
.RasterDistance
);
132 span
->attrStart
[FRAG_ATTRIB_FOGC
][0] = fogVal
;
133 span
->attrStepX
[FRAG_ATTRIB_FOGC
][0] = 0.0;
134 span
->attrStepY
[FRAG_ATTRIB_FOGC
][0] = 0.0;
140 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
141 const GLuint attr
= FRAG_ATTRIB_TEX0
+ i
;
142 const GLfloat
*tc
= ctx
->Current
.RasterTexCoords
[i
];
143 if (ctx
->FragmentProgram
._Current
|| ctx
->ATIFragmentShader
._Enabled
) {
144 COPY_4V(span
->attrStart
[attr
], tc
);
146 else if (tc
[3] > 0.0F
) {
147 /* use (s/q, t/q, r/q, 1) */
148 span
->attrStart
[attr
][0] = tc
[0] / tc
[3];
149 span
->attrStart
[attr
][1] = tc
[1] / tc
[3];
150 span
->attrStart
[attr
][2] = tc
[2] / tc
[3];
151 span
->attrStart
[attr
][3] = 1.0;
154 ASSIGN_4V(span
->attrStart
[attr
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
156 ASSIGN_4V(span
->attrStepX
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
157 ASSIGN_4V(span
->attrStepY
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
164 * Interpolate the active attributes (and'd with attrMask) to
165 * fill in span->array->attribs[].
166 * Perspective correction will be done. The point/line/triangle function
167 * should have computed attrStart/Step values for FRAG_ATTRIB_WPOS[3]!
170 interpolate_active_attribs(GLcontext
*ctx
, SWspan
*span
, GLbitfield attrMask
)
172 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
175 if (attrMask
& (1 << attr
)) {
176 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
177 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
178 const GLfloat dv0dx
= span
->attrStepX
[attr
][0];
179 const GLfloat dv1dx
= span
->attrStepX
[attr
][1];
180 const GLfloat dv2dx
= span
->attrStepX
[attr
][2];
181 const GLfloat dv3dx
= span
->attrStepX
[attr
][3];
182 GLfloat v0
= span
->attrStart
[attr
][0];
183 GLfloat v1
= span
->attrStart
[attr
][1];
184 GLfloat v2
= span
->attrStart
[attr
][2];
185 GLfloat v3
= span
->attrStart
[attr
][3];
187 for (k
= 0; k
< span
->end
; k
++) {
188 const GLfloat invW
= 1.0f
/ w
;
189 span
->array
->attribs
[attr
][k
][0] = v0
* invW
;
190 span
->array
->attribs
[attr
][k
][1] = v1
* invW
;
191 span
->array
->attribs
[attr
][k
][2] = v2
* invW
;
192 span
->array
->attribs
[attr
][k
][3] = v3
* invW
;
199 span
->arrayAttribs
|= (1 << attr
);
206 * Interpolate primary colors to fill in the span->array->rgba8 (or rgb16)
210 interpolate_int_colors(GLcontext
*ctx
, SWspan
*span
)
212 const GLuint n
= span
->end
;
216 ASSERT(!(span
->arrayMask
& SPAN_RGBA
));
219 switch (span
->array
->ChanType
) {
221 case GL_UNSIGNED_BYTE
:
223 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
224 if (span
->interpMask
& SPAN_FLAT
) {
226 color
[RCOMP
] = FixedToInt(span
->red
);
227 color
[GCOMP
] = FixedToInt(span
->green
);
228 color
[BCOMP
] = FixedToInt(span
->blue
);
229 color
[ACOMP
] = FixedToInt(span
->alpha
);
230 for (i
= 0; i
< n
; i
++) {
231 COPY_4UBV(rgba
[i
], color
);
235 GLfixed r
= span
->red
;
236 GLfixed g
= span
->green
;
237 GLfixed b
= span
->blue
;
238 GLfixed a
= span
->alpha
;
239 GLint dr
= span
->redStep
;
240 GLint dg
= span
->greenStep
;
241 GLint db
= span
->blueStep
;
242 GLint da
= span
->alphaStep
;
243 for (i
= 0; i
< n
; i
++) {
244 rgba
[i
][RCOMP
] = FixedToChan(r
);
245 rgba
[i
][GCOMP
] = FixedToChan(g
);
246 rgba
[i
][BCOMP
] = FixedToChan(b
);
247 rgba
[i
][ACOMP
] = FixedToChan(a
);
256 case GL_UNSIGNED_SHORT
:
258 GLushort (*rgba
)[4] = span
->array
->rgba16
;
259 if (span
->interpMask
& SPAN_FLAT
) {
261 color
[RCOMP
] = FixedToInt(span
->red
);
262 color
[GCOMP
] = FixedToInt(span
->green
);
263 color
[BCOMP
] = FixedToInt(span
->blue
);
264 color
[ACOMP
] = FixedToInt(span
->alpha
);
265 for (i
= 0; i
< n
; i
++) {
266 COPY_4V(rgba
[i
], color
);
270 GLushort (*rgba
)[4] = span
->array
->rgba16
;
272 GLint dr
, dg
, db
, da
;
278 dg
= span
->greenStep
;
280 da
= span
->alphaStep
;
281 for (i
= 0; i
< n
; i
++) {
282 rgba
[i
][RCOMP
] = FixedToChan(r
);
283 rgba
[i
][GCOMP
] = FixedToChan(g
);
284 rgba
[i
][BCOMP
] = FixedToChan(b
);
285 rgba
[i
][ACOMP
] = FixedToChan(a
);
296 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL0
);
299 _mesa_problem(NULL
, "bad datatype in interpolate_int_colors");
301 span
->arrayMask
|= SPAN_RGBA
;
306 * Populate the FRAG_ATTRIB_COL0 array.
309 interpolate_float_colors(SWspan
*span
)
311 GLfloat (*col0
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
312 const GLuint n
= span
->end
;
315 assert(!(span
->arrayAttribs
& FRAG_BIT_COL0
));
317 if (span
->arrayMask
& SPAN_RGBA
) {
318 /* convert array of int colors */
319 for (i
= 0; i
< n
; i
++) {
320 col0
[i
][0] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][0]);
321 col0
[i
][1] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][1]);
322 col0
[i
][2] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][2]);
323 col0
[i
][3] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][3]);
327 /* interpolate red/green/blue/alpha to get float colors */
328 ASSERT(span
->interpMask
& SPAN_RGBA
);
329 if (span
->interpMask
& SPAN_FLAT
) {
330 GLfloat r
= FixedToFloat(span
->red
);
331 GLfloat g
= FixedToFloat(span
->green
);
332 GLfloat b
= FixedToFloat(span
->blue
);
333 GLfloat a
= FixedToFloat(span
->alpha
);
334 for (i
= 0; i
< n
; i
++) {
335 ASSIGN_4V(col0
[i
], r
, g
, b
, a
);
339 GLfloat r
= FixedToFloat(span
->red
);
340 GLfloat g
= FixedToFloat(span
->green
);
341 GLfloat b
= FixedToFloat(span
->blue
);
342 GLfloat a
= FixedToFloat(span
->alpha
);
343 GLfloat dr
= FixedToFloat(span
->redStep
);
344 GLfloat dg
= FixedToFloat(span
->greenStep
);
345 GLfloat db
= FixedToFloat(span
->blueStep
);
346 GLfloat da
= FixedToFloat(span
->alphaStep
);
347 for (i
= 0; i
< n
; i
++) {
360 span
->arrayAttribs
|= FRAG_BIT_COL0
;
361 span
->array
->ChanType
= GL_FLOAT
;
366 /* Fill in the span.color.index array from the interpolation values */
368 interpolate_indexes(GLcontext
*ctx
, SWspan
*span
)
370 GLfixed index
= span
->index
;
371 const GLint indexStep
= span
->indexStep
;
372 const GLuint n
= span
->end
;
373 GLuint
*indexes
= span
->array
->index
;
377 ASSERT(!(span
->arrayMask
& SPAN_INDEX
));
379 if ((span
->interpMask
& SPAN_FLAT
) || (indexStep
== 0)) {
381 index
= FixedToInt(index
);
382 for (i
= 0; i
< n
; i
++) {
388 for (i
= 0; i
< n
; i
++) {
389 indexes
[i
] = FixedToInt(index
);
393 span
->arrayMask
|= SPAN_INDEX
;
394 span
->interpMask
&= ~SPAN_INDEX
;
399 * Fill in the span.zArray array from the span->z, zStep values.
402 _swrast_span_interpolate_z( const GLcontext
*ctx
, SWspan
*span
)
404 const GLuint n
= span
->end
;
407 ASSERT(!(span
->arrayMask
& SPAN_Z
));
409 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
410 GLfixed zval
= span
->z
;
411 GLuint
*z
= span
->array
->z
;
412 for (i
= 0; i
< n
; i
++) {
413 z
[i
] = FixedToInt(zval
);
418 /* Deep Z buffer, no fixed->int shift */
419 GLuint zval
= span
->z
;
420 GLuint
*z
= span
->array
->z
;
421 for (i
= 0; i
< n
; i
++) {
426 span
->interpMask
&= ~SPAN_Z
;
427 span
->arrayMask
|= SPAN_Z
;
432 * Compute mipmap LOD from partial derivatives.
433 * This the ideal solution, as given in the OpenGL spec.
437 compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
438 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
439 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
441 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
442 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
443 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
444 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
445 GLfloat x
= SQRTF(dudx
* dudx
+ dvdx
* dvdx
);
446 GLfloat y
= SQRTF(dudy
* dudy
+ dvdy
* dvdy
);
447 GLfloat rho
= MAX2(x
, y
);
448 GLfloat lambda
= LOG2(rho
);
455 * Compute mipmap LOD from partial derivatives.
456 * This is a faster approximation than above function.
459 _swrast_compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
460 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
461 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
463 GLfloat dsdx2
= (s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
;
464 GLfloat dtdx2
= (t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
;
465 GLfloat dsdy2
= (s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
;
466 GLfloat dtdy2
= (t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
;
467 GLfloat maxU
, maxV
, rho
, lambda
;
468 dsdx2
= FABSF(dsdx2
);
469 dsdy2
= FABSF(dsdy2
);
470 dtdx2
= FABSF(dtdx2
);
471 dtdy2
= FABSF(dtdy2
);
472 maxU
= MAX2(dsdx2
, dsdy2
) * texW
;
473 maxV
= MAX2(dtdx2
, dtdy2
) * texH
;
474 rho
= MAX2(maxU
, maxV
);
481 * Fill in the span.array->attrib[FRAG_ATTRIB_TEXn] arrays from the
482 * using the attrStart/Step values.
484 * This function only used during fixed-function fragment processing.
486 * Note: in the places where we divide by Q (or mult by invQ) we're
487 * really doing two things: perspective correction and texcoord
488 * projection. Remember, for texcoord (s,t,r,q) we need to index
489 * texels with (s/q, t/q, r/q).
492 interpolate_texcoords(GLcontext
*ctx
, SWspan
*span
)
495 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
498 /* XXX CoordUnits vs. ImageUnits */
499 for (u
= 0; u
< maxUnit
; u
++) {
500 if (ctx
->Texture
._EnabledCoordUnits
& (1 << u
)) {
501 const GLuint attr
= FRAG_ATTRIB_TEX0
+ u
;
502 const struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[u
]._Current
;
504 GLboolean needLambda
;
505 GLfloat (*texcoord
)[4] = span
->array
->attribs
[attr
];
506 GLfloat
*lambda
= span
->array
->lambda
[u
];
507 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
508 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
509 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
510 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
511 const GLfloat drdx
= span
->attrStepX
[attr
][2];
512 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
513 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
514 GLfloat s
= span
->attrStart
[attr
][0];
515 GLfloat t
= span
->attrStart
[attr
][1];
516 GLfloat r
= span
->attrStart
[attr
][2];
517 GLfloat q
= span
->attrStart
[attr
][3];
520 const struct gl_texture_image
*img
= obj
->Image
[0][obj
->BaseLevel
];
521 needLambda
= (obj
->MinFilter
!= obj
->MagFilter
)
522 || ctx
->FragmentProgram
._Current
;
523 texW
= img
->WidthScale
;
524 texH
= img
->HeightScale
;
527 /* using a fragment program */
530 needLambda
= GL_FALSE
;
535 if (ctx
->FragmentProgram
._Current
536 || ctx
->ATIFragmentShader
._Enabled
) {
537 /* do perspective correction but don't divide s, t, r by q */
538 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
539 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
540 for (i
= 0; i
< span
->end
; i
++) {
541 const GLfloat invW
= 1.0F
/ w
;
542 texcoord
[i
][0] = s
* invW
;
543 texcoord
[i
][1] = t
* invW
;
544 texcoord
[i
][2] = r
* invW
;
545 texcoord
[i
][3] = q
* invW
;
546 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
547 dqdx
, dqdy
, texW
, texH
,
557 for (i
= 0; i
< span
->end
; i
++) {
558 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
559 texcoord
[i
][0] = s
* invQ
;
560 texcoord
[i
][1] = t
* invQ
;
561 texcoord
[i
][2] = r
* invQ
;
563 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
564 dqdx
, dqdy
, texW
, texH
,
572 span
->arrayMask
|= SPAN_LAMBDA
;
576 if (ctx
->FragmentProgram
._Current
||
577 ctx
->ATIFragmentShader
._Enabled
) {
578 /* do perspective correction but don't divide s, t, r by q */
579 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
580 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
581 for (i
= 0; i
< span
->end
; i
++) {
582 const GLfloat invW
= 1.0F
/ w
;
583 texcoord
[i
][0] = s
* invW
;
584 texcoord
[i
][1] = t
* invW
;
585 texcoord
[i
][2] = r
* invW
;
586 texcoord
[i
][3] = q
* invW
;
595 else if (dqdx
== 0.0F
) {
596 /* Ortho projection or polygon's parallel to window X axis */
597 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
598 for (i
= 0; i
< span
->end
; i
++) {
599 texcoord
[i
][0] = s
* invQ
;
600 texcoord
[i
][1] = t
* invQ
;
601 texcoord
[i
][2] = r
* invQ
;
610 for (i
= 0; i
< span
->end
; i
++) {
611 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
612 texcoord
[i
][0] = s
* invQ
;
613 texcoord
[i
][1] = t
* invQ
;
614 texcoord
[i
][2] = r
* invQ
;
630 * Fill in the arrays->attribs[FRAG_ATTRIB_WPOS] array.
633 interpolate_wpos(GLcontext
*ctx
, SWspan
*span
)
635 GLfloat (*wpos
)[4] = span
->array
->attribs
[FRAG_ATTRIB_WPOS
];
637 const GLfloat zScale
= 1.0 / ctx
->DrawBuffer
->_DepthMaxF
;
640 if (span
->arrayMask
& SPAN_XY
) {
641 for (i
= 0; i
< span
->end
; i
++) {
642 wpos
[i
][0] = (GLfloat
) span
->array
->x
[i
];
643 wpos
[i
][1] = (GLfloat
) span
->array
->y
[i
];
647 for (i
= 0; i
< span
->end
; i
++) {
648 wpos
[i
][0] = (GLfloat
) span
->x
+ i
;
649 wpos
[i
][1] = (GLfloat
) span
->y
;
653 w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
654 dw
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
655 for (i
= 0; i
< span
->end
; i
++) {
656 wpos
[i
][2] = (GLfloat
) span
->array
->z
[i
] * zScale
;
664 * Apply the current polygon stipple pattern to a span of pixels.
667 stipple_polygon_span(GLcontext
*ctx
, SWspan
*span
)
669 GLubyte
*mask
= span
->array
->mask
;
671 ASSERT(ctx
->Polygon
.StippleFlag
);
673 if (span
->arrayMask
& SPAN_XY
) {
674 /* arrays of x/y pixel coords */
676 for (i
= 0; i
< span
->end
; i
++) {
677 const GLint col
= span
->array
->x
[i
] % 32;
678 const GLint row
= span
->array
->y
[i
] % 32;
679 const GLuint stipple
= ctx
->PolygonStipple
[row
];
680 if (((1 << col
) & stipple
) == 0) {
686 /* horizontal span of pixels */
687 const GLuint highBit
= 1 << 31;
688 const GLuint stipple
= ctx
->PolygonStipple
[span
->y
% 32];
689 GLuint i
, m
= highBit
>> (GLuint
) (span
->x
% 32);
690 for (i
= 0; i
< span
->end
; i
++) {
691 if ((m
& stipple
) == 0) {
700 span
->writeAll
= GL_FALSE
;
705 * Clip a pixel span to the current buffer/window boundaries:
706 * DrawBuffer->_Xmin, _Xmax, _Ymin, _Ymax. This will accomplish
707 * window clipping and scissoring.
708 * Return: GL_TRUE some pixels still visible
709 * GL_FALSE nothing visible
712 clip_span( GLcontext
*ctx
, SWspan
*span
)
714 const GLint xmin
= ctx
->DrawBuffer
->_Xmin
;
715 const GLint xmax
= ctx
->DrawBuffer
->_Xmax
;
716 const GLint ymin
= ctx
->DrawBuffer
->_Ymin
;
717 const GLint ymax
= ctx
->DrawBuffer
->_Ymax
;
719 if (span
->arrayMask
& SPAN_XY
) {
720 /* arrays of x/y pixel coords */
721 const GLint
*x
= span
->array
->x
;
722 const GLint
*y
= span
->array
->y
;
723 const GLint n
= span
->end
;
724 GLubyte
*mask
= span
->array
->mask
;
726 if (span
->arrayMask
& SPAN_MASK
) {
727 /* note: using & intead of && to reduce branches */
728 for (i
= 0; i
< n
; i
++) {
729 mask
[i
] &= (x
[i
] >= xmin
) & (x
[i
] < xmax
)
730 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
734 /* note: using & intead of && to reduce branches */
735 for (i
= 0; i
< n
; i
++) {
736 mask
[i
] = (x
[i
] >= xmin
) & (x
[i
] < xmax
)
737 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
740 return GL_TRUE
; /* some pixels visible */
743 /* horizontal span of pixels */
744 const GLint x
= span
->x
;
745 const GLint y
= span
->y
;
746 const GLint n
= span
->end
;
748 /* Trivial rejection tests */
749 if (y
< ymin
|| y
>= ymax
|| x
+ n
<= xmin
|| x
>= xmax
) {
751 return GL_FALSE
; /* all pixels clipped */
754 /* Clip to the left */
756 ASSERT(x
+ n
> xmin
);
757 span
->writeAll
= GL_FALSE
;
758 _mesa_bzero(span
->array
->mask
, (xmin
- x
) * sizeof(GLubyte
));
764 span
->end
= xmax
- x
;
767 return GL_TRUE
; /* some pixels visible */
773 * Apply all the per-fragment opertions to a span of color index fragments
774 * and write them to the enabled color drawbuffers.
775 * The 'span' parameter can be considered to be const. Note that
776 * span->interpMask and span->arrayMask may be changed but will be restored
777 * to their original values before returning.
780 _swrast_write_index_span( GLcontext
*ctx
, SWspan
*span
)
782 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
783 const GLbitfield origInterpMask
= span
->interpMask
;
784 const GLbitfield origArrayMask
= span
->arrayMask
;
786 ASSERT(span
->end
<= MAX_WIDTH
);
787 ASSERT(span
->primitive
== GL_POINT
|| span
->primitive
== GL_LINE
||
788 span
->primitive
== GL_POLYGON
|| span
->primitive
== GL_BITMAP
);
789 ASSERT((span
->interpMask
| span
->arrayMask
) & SPAN_INDEX
);
791 ASSERT((span->interpMask & span->arrayMask) == 0);
794 if (span
->arrayMask
& SPAN_MASK
) {
795 /* mask was initialized by caller, probably glBitmap */
796 span
->writeAll
= GL_FALSE
;
799 _mesa_memset(span
->array
->mask
, 1, span
->end
);
800 span
->writeAll
= GL_TRUE
;
804 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
805 if (!clip_span(ctx
, span
)) {
810 /* Depth bounds test */
811 if (ctx
->Depth
.BoundsTest
&& ctx
->DrawBuffer
->Visual
.depthBits
> 0) {
812 if (!_swrast_depth_bounds_test(ctx
, span
)) {
818 /* Make sure all fragments are within window bounds */
819 if (span
->arrayMask
& SPAN_XY
) {
821 for (i
= 0; i
< span
->end
; i
++) {
822 if (span
->array
->mask
[i
]) {
823 assert(span
->array
->x
[i
] >= ctx
->DrawBuffer
->_Xmin
);
824 assert(span
->array
->x
[i
] < ctx
->DrawBuffer
->_Xmax
);
825 assert(span
->array
->y
[i
] >= ctx
->DrawBuffer
->_Ymin
);
826 assert(span
->array
->y
[i
] < ctx
->DrawBuffer
->_Ymax
);
832 /* Polygon Stippling */
833 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
834 stipple_polygon_span(ctx
, span
);
837 /* Stencil and Z testing */
838 if (ctx
->Depth
.Test
|| ctx
->Stencil
.Enabled
) {
839 if (!(span
->arrayMask
& SPAN_Z
))
840 _swrast_span_interpolate_z(ctx
, span
);
842 if (ctx
->Stencil
.Enabled
) {
843 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
844 span
->arrayMask
= origArrayMask
;
849 ASSERT(ctx
->Depth
.Test
);
850 if (!_swrast_depth_test_span(ctx
, span
)) {
851 span
->interpMask
= origInterpMask
;
852 span
->arrayMask
= origArrayMask
;
858 #if FEATURE_ARB_occlusion_query
859 if (ctx
->Query
.CurrentOcclusionObject
) {
860 /* update count of 'passed' fragments */
861 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
863 for (i
= 0; i
< span
->end
; i
++)
864 q
->Result
+= span
->array
->mask
[i
];
868 /* we have to wait until after occlusion to do this test */
869 if (ctx
->Color
.DrawBuffer
== GL_NONE
|| ctx
->Color
.IndexMask
== 0) {
870 /* write no pixels */
871 span
->arrayMask
= origArrayMask
;
875 /* Interpolate the color indexes if needed */
876 if (swrast
->_FogEnabled
||
877 ctx
->Color
.IndexLogicOpEnabled
||
878 ctx
->Color
.IndexMask
!= 0xffffffff ||
879 (span
->arrayMask
& SPAN_COVERAGE
)) {
880 if (!(span
->arrayMask
& SPAN_INDEX
) /*span->interpMask & SPAN_INDEX*/) {
881 interpolate_indexes(ctx
, span
);
886 if (swrast
->_FogEnabled
) {
887 _swrast_fog_ci_span(ctx
, span
);
890 /* Antialias coverage application */
891 if (span
->arrayMask
& SPAN_COVERAGE
) {
892 const GLfloat
*coverage
= span
->array
->coverage
;
893 GLuint
*index
= span
->array
->index
;
895 for (i
= 0; i
< span
->end
; i
++) {
896 ASSERT(coverage
[i
] < 16);
897 index
[i
] = (index
[i
] & ~0xf) | ((GLuint
) coverage
[i
]);
902 * Write to renderbuffers
905 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
906 const GLuint output
= 0; /* only frag progs can write to other outputs */
907 const GLuint numDrawBuffers
= fb
->_NumColorDrawBuffers
[output
];
908 GLuint indexSave
[MAX_WIDTH
];
911 if (numDrawBuffers
> 1) {
912 /* save indexes for second, third renderbuffer writes */
913 _mesa_memcpy(indexSave
, span
->array
->index
,
914 span
->end
* sizeof(indexSave
[0]));
917 for (buf
= 0; buf
< fb
->_NumColorDrawBuffers
[output
]; buf
++) {
918 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[output
][buf
];
919 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
921 if (ctx
->Color
.IndexLogicOpEnabled
) {
922 _swrast_logicop_ci_span(ctx
, rb
, span
);
925 if (ctx
->Color
.IndexMask
!= 0xffffffff) {
926 _swrast_mask_ci_span(ctx
, rb
, span
);
929 if (!(span
->arrayMask
& SPAN_INDEX
) && span
->indexStep
== 0) {
930 /* all fragments have same color index */
936 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
937 index8
= FixedToInt(span
->index
);
940 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
941 index16
= FixedToInt(span
->index
);
945 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
946 index32
= FixedToInt(span
->index
);
950 if (span
->arrayMask
& SPAN_XY
) {
951 rb
->PutMonoValues(ctx
, rb
, span
->end
, span
->array
->x
,
952 span
->array
->y
, value
, span
->array
->mask
);
955 rb
->PutMonoRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
956 value
, span
->array
->mask
);
960 /* each fragment is a different color */
961 GLubyte index8
[MAX_WIDTH
];
962 GLushort index16
[MAX_WIDTH
];
965 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
967 for (k
= 0; k
< span
->end
; k
++) {
968 index8
[k
] = (GLubyte
) span
->array
->index
[k
];
972 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
974 for (k
= 0; k
< span
->end
; k
++) {
975 index16
[k
] = (GLushort
) span
->array
->index
[k
];
980 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
981 values
= span
->array
->index
;
984 if (span
->arrayMask
& SPAN_XY
) {
985 rb
->PutValues(ctx
, rb
, span
->end
,
986 span
->array
->x
, span
->array
->y
,
987 values
, span
->array
->mask
);
990 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
991 values
, span
->array
->mask
);
995 if (buf
+ 1 < numDrawBuffers
) {
996 /* restore original span values */
997 _mesa_memcpy(span
->array
->index
, indexSave
,
998 span
->end
* sizeof(indexSave
[0]));
1003 span
->interpMask
= origInterpMask
;
1004 span
->arrayMask
= origArrayMask
;
1009 * Add specular colors to primary colors.
1010 * Only called during fixed-function operation.
1011 * Result is float color array (FRAG_ATTRIB_COL0).
1014 add_specular(GLcontext
*ctx
, SWspan
*span
)
1016 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1017 const GLubyte
*mask
= span
->array
->mask
;
1018 GLfloat (*col0
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1019 GLfloat (*col1
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL1
];
1022 ASSERT(!ctx
->FragmentProgram
._Current
);
1023 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1024 ASSERT(swrast
->_ActiveAttribMask
& FRAG_BIT_COL1
);
1026 if (span
->array
->ChanType
== GL_FLOAT
) {
1027 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1028 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL0
);
1032 /* need float colors */
1033 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1034 interpolate_float_colors(span
);
1038 if ((span
->arrayAttribs
& FRAG_BIT_COL1
) == 0) {
1039 /* XXX could avoid this and interpolate COL1 in the loop below */
1040 interpolate_active_attribs(ctx
, span
, FRAG_BIT_COL1
);
1043 ASSERT(span
->arrayAttribs
& FRAG_BIT_COL0
);
1044 ASSERT(span
->arrayAttribs
& FRAG_BIT_COL1
);
1046 for (i
= 0; i
< span
->end
; i
++) {
1048 col0
[i
][0] += col1
[i
][0];
1049 col0
[i
][1] += col1
[i
][1];
1050 col0
[i
][2] += col1
[i
][2];
1054 span
->array
->ChanType
= GL_FLOAT
;
1059 * Apply antialiasing coverage value to alpha values.
1062 apply_aa_coverage(SWspan
*span
)
1064 const GLfloat
*coverage
= span
->array
->coverage
;
1066 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1067 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
1068 for (i
= 0; i
< span
->end
; i
++) {
1069 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
1070 rgba
[i
][ACOMP
] = (GLubyte
) CLAMP(a
, 0.0, 255.0);
1071 ASSERT(coverage
[i
] >= 0.0);
1072 ASSERT(coverage
[i
] <= 1.0);
1075 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
1076 GLushort (*rgba
)[4] = span
->array
->rgba16
;
1077 for (i
= 0; i
< span
->end
; i
++) {
1078 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
1079 rgba
[i
][ACOMP
] = (GLushort
) CLAMP(a
, 0.0, 65535.0);
1083 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1084 for (i
= 0; i
< span
->end
; i
++) {
1085 rgba
[i
][ACOMP
] = rgba
[i
][ACOMP
] * coverage
[i
];
1093 * Clamp span's float colors to [0,1]
1096 clamp_colors(SWspan
*span
)
1098 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1100 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
1101 for (i
= 0; i
< span
->end
; i
++) {
1102 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0F
, 1.0F
);
1103 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0F
, 1.0F
);
1104 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0F
, 1.0F
);
1105 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0F
, 1.0F
);
1111 * Convert the span's color arrays to the given type.
1112 * The only way 'output' can be greater than one is when we have a fragment
1113 * program that writes to gl_FragData[1] or higher.
1114 * \param output which fragment program color output is being processed
1117 convert_color_type(SWspan
*span
, GLenum newType
, GLuint output
)
1121 if (output
> 0 || span
->array
->ChanType
== GL_FLOAT
) {
1122 src
= span
->array
->attribs
[FRAG_ATTRIB_COL0
+ output
];
1123 span
->array
->ChanType
= GL_FLOAT
;
1125 else if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1126 src
= span
->array
->rgba8
;
1129 ASSERT(span
->array
->ChanType
== GL_UNSIGNED_SHORT
);
1130 src
= span
->array
->rgba16
;
1133 if (newType
== GL_UNSIGNED_BYTE
) {
1134 dst
= span
->array
->rgba8
;
1136 else if (newType
== GL_UNSIGNED_SHORT
) {
1137 dst
= span
->array
->rgba16
;
1140 dst
= span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1143 _mesa_convert_colors(span
->array
->ChanType
, src
,
1145 span
->end
, span
->array
->mask
);
1147 span
->array
->ChanType
= newType
;
1153 * Apply fragment shader, fragment program or normal texturing to span.
1156 shade_texture_span(GLcontext
*ctx
, SWspan
*span
)
1158 GLbitfield inputsRead
;
1160 /* Determine which fragment attributes are actually needed */
1161 if (ctx
->FragmentProgram
._Current
) {
1162 inputsRead
= ctx
->FragmentProgram
._Current
->Base
.InputsRead
;
1165 /* XXX we could be a bit smarter about this */
1169 if (ctx
->FragmentProgram
._Current
||
1170 ctx
->ATIFragmentShader
._Enabled
) {
1171 /* programmable shading */
1172 span
->array
->ChanType
= GL_FLOAT
;
1174 interpolate_active_attribs(ctx
, span
, ~0);
1176 if (!(span
->arrayMask
& SPAN_Z
))
1177 _swrast_span_interpolate_z (ctx
, span
);
1180 if (inputsRead
& FRAG_BIT_WPOS
)
1182 /* XXX always interpolate wpos so that DDX/DDY work */
1184 interpolate_wpos(ctx
, span
);
1186 /* Run fragment program/shader now */
1187 if (ctx
->FragmentProgram
._Current
) {
1188 _swrast_exec_fragment_program(ctx
, span
);
1191 ASSERT(ctx
->ATIFragmentShader
._Enabled
);
1192 _swrast_exec_fragment_shader(ctx
, span
);
1195 else if (ctx
->Texture
._EnabledUnits
) {
1196 /* conventional texturing */
1199 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1200 interpolate_int_colors(ctx
, span
);
1203 if (!(span
->arrayMask
& SPAN_RGBA
))
1204 interpolate_int_colors(ctx
, span
);
1206 if ((span
->arrayAttribs
& FRAG_BITS_TEX_ANY
) == 0x0)
1207 interpolate_texcoords(ctx
, span
);
1209 _swrast_texture_span(ctx
, span
);
1216 * Apply all the per-fragment operations to a span.
1217 * This now includes texturing (_swrast_write_texture_span() is history).
1218 * This function may modify any of the array values in the span.
1219 * span->interpMask and span->arrayMask may be changed but will be restored
1220 * to their original values before returning.
1223 _swrast_write_rgba_span( GLcontext
*ctx
, SWspan
*span
)
1225 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1226 const GLuint colorMask
= *((GLuint
*) ctx
->Color
.ColorMask
);
1227 const GLbitfield origInterpMask
= span
->interpMask
;
1228 const GLbitfield origArrayMask
= span
->arrayMask
;
1229 const GLbitfield origArrayAttribs
= span
->arrayAttribs
;
1230 const GLenum chanType
= span
->array
->ChanType
;
1231 const GLboolean shader
= (ctx
->FragmentProgram
._Current
1232 || ctx
->ATIFragmentShader
._Enabled
);
1233 const GLboolean shaderOrTexture
= shader
|| ctx
->Texture
._EnabledUnits
;
1234 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1238 printf("%s() interp 0x%x array 0x%x\n", __FUNCTION__,
1239 span->interpMask, span->arrayMask);
1242 ASSERT(span
->primitive
== GL_POINT
||
1243 span
->primitive
== GL_LINE
||
1244 span
->primitive
== GL_POLYGON
||
1245 span
->primitive
== GL_BITMAP
);
1246 ASSERT(span
->end
<= MAX_WIDTH
);
1248 /* Fragment write masks */
1249 if (span
->arrayMask
& SPAN_MASK
) {
1250 /* mask was initialized by caller, probably glBitmap */
1251 span
->writeAll
= GL_FALSE
;
1254 _mesa_memset(span
->array
->mask
, 1, span
->end
);
1255 span
->writeAll
= GL_TRUE
;
1258 /* Clip to window/scissor box */
1259 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
1260 if (!clip_span(ctx
, span
)) {
1266 /* Make sure all fragments are within window bounds */
1267 if (span
->arrayMask
& SPAN_XY
) {
1269 for (i
= 0; i
< span
->end
; i
++) {
1270 if (span
->array
->mask
[i
]) {
1271 assert(span
->array
->x
[i
] >= fb
->_Xmin
);
1272 assert(span
->array
->x
[i
] < fb
->_Xmax
);
1273 assert(span
->array
->y
[i
] >= fb
->_Ymin
);
1274 assert(span
->array
->y
[i
] < fb
->_Ymax
);
1280 /* Polygon Stippling */
1281 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
1282 stipple_polygon_span(ctx
, span
);
1285 /* This is the normal place to compute the fragment color/Z
1286 * from texturing or shading.
1288 if (shaderOrTexture
&& !swrast
->_DeferredTexture
) {
1289 shade_texture_span(ctx
, span
);
1292 /* Do the alpha test */
1293 if (ctx
->Color
.AlphaEnabled
) {
1294 if (!_swrast_alpha_test(ctx
, span
)) {
1299 /* Stencil and Z testing */
1300 if (ctx
->Stencil
.Enabled
|| ctx
->Depth
.Test
) {
1301 if (!(span
->arrayMask
& SPAN_Z
))
1302 _swrast_span_interpolate_z(ctx
, span
);
1304 if (ctx
->Stencil
.Enabled
&& fb
->Visual
.stencilBits
> 0) {
1305 /* Combined Z/stencil tests */
1306 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
1310 else if (fb
->Visual
.depthBits
> 0) {
1311 /* Just regular depth testing */
1312 ASSERT(ctx
->Depth
.Test
);
1313 ASSERT(span
->arrayMask
& SPAN_Z
);
1314 if (!_swrast_depth_test_span(ctx
, span
)) {
1320 #if FEATURE_ARB_occlusion_query
1321 if (ctx
->Query
.CurrentOcclusionObject
) {
1322 /* update count of 'passed' fragments */
1323 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
1325 for (i
= 0; i
< span
->end
; i
++)
1326 q
->Result
+= span
->array
->mask
[i
];
1330 /* We had to wait until now to check for glColorMask(0,0,0,0) because of
1331 * the occlusion test.
1333 if (colorMask
== 0x0) {
1337 /* If we were able to defer fragment color computation to now, there's
1338 * a good chance that many fragments will have already been killed by
1339 * Z/stencil testing.
1341 if (shaderOrTexture
&& swrast
->_DeferredTexture
) {
1342 shade_texture_span(ctx
, span
);
1346 if ((span
->arrayAttribs
& FRAG_BIT_COL0
) == 0) {
1347 interpolate_int_colors(ctx
, span
);
1350 if ((span
->arrayMask
& SPAN_RGBA
) == 0) {
1351 interpolate_int_colors(ctx
, span
);
1355 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1358 /* Add base and specular colors */
1359 if (ctx
->Fog
.ColorSumEnabled
||
1360 (ctx
->Light
.Enabled
&&
1361 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)) {
1362 add_specular(ctx
, span
);
1367 if (swrast
->_FogEnabled
) {
1368 _swrast_fog_rgba_span(ctx
, span
);
1371 /* Antialias coverage application */
1372 if (span
->arrayMask
& SPAN_COVERAGE
) {
1373 apply_aa_coverage(span
);
1376 /* Clamp color/alpha values over the range [0.0, 1.0] before storage */
1377 if (ctx
->Color
.ClampFragmentColor
== GL_TRUE
&&
1378 span
->array
->ChanType
== GL_FLOAT
) {
1383 * Write to renderbuffers
1385 /* Loop over color outputs (GL_ARB_draw_buffers) written by frag prog */
1386 for (output
= 0; output
< swrast
->_NumColorOutputs
; output
++) {
1387 if (swrast
->_ColorOutputsMask
& (1 << output
)) {
1388 const GLuint numDrawBuffers
= fb
->_NumColorDrawBuffers
[output
];
1389 GLchan rgbaSave
[MAX_WIDTH
][4];
1392 ASSERT(numDrawBuffers
> 0);
1394 if (fb
->_ColorDrawBuffers
[output
][0]->DataType
1395 != span
->array
->ChanType
|| output
> 0) {
1396 convert_color_type(span
,
1397 fb
->_ColorDrawBuffers
[output
][0]->DataType
,
1401 if (numDrawBuffers
> 1) {
1402 /* save colors for second, third renderbuffer writes */
1403 _mesa_memcpy(rgbaSave
, span
->array
->rgba
,
1404 4 * span
->end
* sizeof(GLchan
));
1407 /* Loop over renderbuffers (i.e. GL_FRONT_AND_BACK) */
1408 for (buf
= 0; buf
< numDrawBuffers
; buf
++) {
1409 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[output
][buf
];
1410 ASSERT(rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
);
1412 if (ctx
->Color
._LogicOpEnabled
) {
1413 _swrast_logicop_rgba_span(ctx
, rb
, span
);
1415 else if (ctx
->Color
.BlendEnabled
) {
1416 _swrast_blend_span(ctx
, rb
, span
);
1419 if (colorMask
!= 0xffffffff) {
1420 _swrast_mask_rgba_span(ctx
, rb
, span
);
1423 if (span
->arrayMask
& SPAN_XY
) {
1424 /* array of pixel coords */
1425 ASSERT(rb
->PutValues
);
1426 rb
->PutValues(ctx
, rb
, span
->end
,
1427 span
->array
->x
, span
->array
->y
,
1428 span
->array
->rgba
, span
->array
->mask
);
1431 /* horizontal run of pixels */
1433 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1435 span
->writeAll
? NULL
: span
->array
->mask
);
1438 if (buf
+ 1 < numDrawBuffers
) {
1439 /* restore original span values */
1440 _mesa_memcpy(span
->array
->rgba
, rgbaSave
,
1441 4 * span
->end
* sizeof(GLchan
));
1444 } /* if output is written to */
1448 /* restore these values before returning */
1449 span
->interpMask
= origInterpMask
;
1450 span
->arrayMask
= origArrayMask
;
1451 span
->arrayAttribs
= origArrayAttribs
;
1452 span
->array
->ChanType
= chanType
;
1457 * Read RGBA pixels from a renderbuffer. Clipping will be done to prevent
1458 * reading ouside the buffer's boundaries.
1459 * \param dstType datatype for returned colors
1460 * \param rgba the returned colors
1463 _swrast_read_rgba_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1464 GLuint n
, GLint x
, GLint y
, GLenum dstType
,
1467 const GLint bufWidth
= (GLint
) rb
->Width
;
1468 const GLint bufHeight
= (GLint
) rb
->Height
;
1470 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1471 /* completely above, below, or right */
1472 /* XXX maybe leave rgba values undefined? */
1473 _mesa_bzero(rgba
, 4 * n
* sizeof(GLchan
));
1478 /* left edge clipping */
1480 length
= (GLint
) n
- skip
;
1482 /* completely left of window */
1485 if (length
> bufWidth
) {
1489 else if ((GLint
) (x
+ n
) > bufWidth
) {
1490 /* right edge clipping */
1492 length
= bufWidth
- x
;
1494 /* completely to right of window */
1506 ASSERT(rb
->_BaseFormat
== GL_RGB
|| rb
->_BaseFormat
== GL_RGBA
);
1508 if (rb
->DataType
== dstType
) {
1509 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
,
1510 (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(rb
->DataType
));
1513 GLuint temp
[MAX_WIDTH
* 4];
1514 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, temp
);
1515 _mesa_convert_colors(rb
->DataType
, temp
,
1516 dstType
, (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(dstType
),
1524 * Read CI pixels from a renderbuffer. Clipping will be done to prevent
1525 * reading ouside the buffer's boundaries.
1528 _swrast_read_index_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1529 GLuint n
, GLint x
, GLint y
, GLuint index
[] )
1531 const GLint bufWidth
= (GLint
) rb
->Width
;
1532 const GLint bufHeight
= (GLint
) rb
->Height
;
1534 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1535 /* completely above, below, or right */
1536 _mesa_bzero(index
, n
* sizeof(GLuint
));
1541 /* left edge clipping */
1543 length
= (GLint
) n
- skip
;
1545 /* completely left of window */
1548 if (length
> bufWidth
) {
1552 else if ((GLint
) (x
+ n
) > bufWidth
) {
1553 /* right edge clipping */
1555 length
= bufWidth
- x
;
1557 /* completely to right of window */
1568 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
1570 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1571 GLubyte index8
[MAX_WIDTH
];
1573 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index8
);
1574 for (i
= 0; i
< length
; i
++)
1575 index
[skip
+ i
] = index8
[i
];
1577 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1578 GLushort index16
[MAX_WIDTH
];
1580 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index16
);
1581 for (i
= 0; i
< length
; i
++)
1582 index
[skip
+ i
] = index16
[i
];
1584 else if (rb
->DataType
== GL_UNSIGNED_INT
) {
1585 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index
+ skip
);
1592 * Wrapper for gl_renderbuffer::GetValues() which does clipping to avoid
1593 * reading values outside the buffer bounds.
1594 * We can use this for reading any format/type of renderbuffer.
1595 * \param valueSize is the size in bytes of each value (pixel) put into the
1599 _swrast_get_values(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1600 GLuint count
, const GLint x
[], const GLint y
[],
1601 void *values
, GLuint valueSize
)
1603 GLuint i
, inCount
= 0, inStart
= 0;
1605 for (i
= 0; i
< count
; i
++) {
1606 if (x
[i
] >= 0 && y
[i
] >= 0 &&
1607 x
[i
] < (GLint
) rb
->Width
&& y
[i
] < (GLint
) rb
->Height
) {
1615 /* read [inStart, inStart + inCount) */
1616 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1617 (GLubyte
*) values
+ inStart
* valueSize
);
1623 /* read last values */
1624 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1625 (GLubyte
*) values
+ inStart
* valueSize
);
1631 * Wrapper for gl_renderbuffer::PutRow() which does clipping.
1632 * \param valueSize size of each value (pixel) in bytes
1635 _swrast_put_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1636 GLuint count
, GLint x
, GLint y
,
1637 const GLvoid
*values
, GLuint valueSize
)
1641 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1642 return; /* above or below */
1644 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1645 return; /* entirely left or right */
1647 if ((GLint
) (x
+ count
) > (GLint
) rb
->Width
) {
1649 GLint clip
= x
+ count
- rb
->Width
;
1660 rb
->PutRow(ctx
, rb
, count
, x
, y
,
1661 (const GLubyte
*) values
+ skip
* valueSize
, NULL
);
1666 * Wrapper for gl_renderbuffer::GetRow() which does clipping.
1667 * \param valueSize size of each value (pixel) in bytes
1670 _swrast_get_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1671 GLuint count
, GLint x
, GLint y
,
1672 GLvoid
*values
, GLuint valueSize
)
1676 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1677 return; /* above or below */
1679 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1680 return; /* entirely left or right */
1682 if (x
+ count
> rb
->Width
) {
1684 GLint clip
= x
+ count
- rb
->Width
;
1695 rb
->GetRow(ctx
, rb
, count
, x
, y
, (GLubyte
*) values
+ skip
* valueSize
);
1700 * Get RGBA pixels from the given renderbuffer. Put the pixel colors into
1701 * the span's specular color arrays. The specular color arrays should no
1702 * longer be needed by time this function is called.
1703 * Used by blending, logicop and masking functions.
1704 * \return pointer to the colors we read.
1707 _swrast_get_dest_rgba(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1710 const GLuint pixelSize
= RGBA_PIXEL_SIZE(span
->array
->ChanType
);
1714 * Point rbPixels to a temporary space (use specular color arrays).
1716 rbPixels
= span
->array
->attribs
[FRAG_ATTRIB_COL1
];
1718 /* Get destination values from renderbuffer */
1719 if (span
->arrayMask
& SPAN_XY
) {
1720 _swrast_get_values(ctx
, rb
, span
->end
, span
->array
->x
, span
->array
->y
,
1721 rbPixels
, pixelSize
);
1724 _swrast_get_row(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1725 rbPixels
, pixelSize
);