2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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 * Init span's Z interpolation values to the RasterPos Z.
56 * Used during setup for glDraw/CopyPixels.
59 _swrast_span_default_z( GLcontext
*ctx
, SWspan
*span
)
61 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
62 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16)
63 span
->z
= FloatToFixed(ctx
->Current
.RasterPos
[2] * depthMax
+ 0.5F
);
65 span
->z
= (GLint
) (ctx
->Current
.RasterPos
[2] * depthMax
+ 0.5F
);
67 span
->interpMask
|= SPAN_Z
;
72 * Init span's fog interpolation values to the RasterPos fog.
73 * Used during setup for glDraw/CopyPixels.
76 _swrast_span_default_fog( GLcontext
*ctx
, SWspan
*span
)
78 span
->attrStart
[FRAG_ATTRIB_FOGC
][0]
79 = _swrast_z_to_fogfactor(ctx
, ctx
->Current
.RasterDistance
);
80 span
->attrStepX
[FRAG_ATTRIB_FOGC
][0] = 0.0;
81 span
->attrStepY
[FRAG_ATTRIB_FOGC
][0] = 0.0;
82 span
->interpMask
|= SPAN_FOG
;
87 * Init span's rgba or index interpolation values to the RasterPos color.
88 * Used during setup for glDraw/CopyPixels.
91 _swrast_span_default_color( GLcontext
*ctx
, SWspan
*span
)
93 if (ctx
->Visual
.rgbMode
) {
95 UNCLAMPED_FLOAT_TO_CHAN(r
, ctx
->Current
.RasterColor
[0]);
96 UNCLAMPED_FLOAT_TO_CHAN(g
, ctx
->Current
.RasterColor
[1]);
97 UNCLAMPED_FLOAT_TO_CHAN(b
, ctx
->Current
.RasterColor
[2]);
98 UNCLAMPED_FLOAT_TO_CHAN(a
, ctx
->Current
.RasterColor
[3]);
99 #if CHAN_TYPE == GL_FLOAT
105 span
->red
= IntToFixed(r
);
106 span
->green
= IntToFixed(g
);
107 span
->blue
= IntToFixed(b
);
108 span
->alpha
= IntToFixed(a
);
114 span
->interpMask
|= SPAN_RGBA
;
117 span
->index
= FloatToFixed(ctx
->Current
.RasterIndex
);
119 span
->interpMask
|= SPAN_INDEX
;
125 * Init span's texcoord interpolation values to the RasterPos texcoords.
126 * Used during setup for glDraw/CopyPixels.
129 _swrast_span_default_texcoords( GLcontext
*ctx
, SWspan
*span
)
132 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
133 const GLuint attr
= FRAG_ATTRIB_TEX0
+ i
;
134 const GLfloat
*tc
= ctx
->Current
.RasterTexCoords
[i
];
135 if (ctx
->FragmentProgram
._Current
|| ctx
->ATIFragmentShader
._Enabled
) {
136 COPY_4V(span
->attrStart
[attr
], tc
);
138 else if (tc
[3] > 0.0F
) {
139 /* use (s/q, t/q, r/q, 1) */
140 span
->attrStart
[attr
][0] = tc
[0] / tc
[3];
141 span
->attrStart
[attr
][1] = tc
[1] / tc
[3];
142 span
->attrStart
[attr
][2] = tc
[2] / tc
[3];
143 span
->attrStart
[attr
][3] = 1.0;
146 ASSIGN_4V(span
->attrStart
[attr
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
148 ASSIGN_4V(span
->attrStepX
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
149 ASSIGN_4V(span
->attrStepY
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
151 span
->interpMask
|= SPAN_TEXTURE
;
156 * Interpolate primary colors to fill in the span->array->color array.
159 interpolate_colors(SWspan
*span
)
161 const GLuint n
= span
->end
;
164 ASSERT((span
->interpMask
& SPAN_RGBA
) &&
165 !(span
->arrayMask
& SPAN_RGBA
));
167 switch (span
->array
->ChanType
) {
169 case GL_UNSIGNED_BYTE
:
171 GLubyte (*rgba
)[4] = span
->array
->color
.sz1
.rgba
;
172 if (span
->interpMask
& SPAN_FLAT
) {
174 color
[RCOMP
] = FixedToInt(span
->red
);
175 color
[GCOMP
] = FixedToInt(span
->green
);
176 color
[BCOMP
] = FixedToInt(span
->blue
);
177 color
[ACOMP
] = FixedToInt(span
->alpha
);
178 for (i
= 0; i
< n
; i
++) {
179 COPY_4UBV(rgba
[i
], color
);
183 GLfixed r
= span
->red
;
184 GLfixed g
= span
->green
;
185 GLfixed b
= span
->blue
;
186 GLfixed a
= span
->alpha
;
187 GLint dr
= span
->redStep
;
188 GLint dg
= span
->greenStep
;
189 GLint db
= span
->blueStep
;
190 GLint da
= span
->alphaStep
;
191 for (i
= 0; i
< n
; i
++) {
192 rgba
[i
][RCOMP
] = FixedToChan(r
);
193 rgba
[i
][GCOMP
] = FixedToChan(g
);
194 rgba
[i
][BCOMP
] = FixedToChan(b
);
195 rgba
[i
][ACOMP
] = FixedToChan(a
);
204 case GL_UNSIGNED_SHORT
:
206 GLushort (*rgba
)[4] = span
->array
->color
.sz2
.rgba
;
207 if (span
->interpMask
& SPAN_FLAT
) {
209 color
[RCOMP
] = FixedToInt(span
->red
);
210 color
[GCOMP
] = FixedToInt(span
->green
);
211 color
[BCOMP
] = FixedToInt(span
->blue
);
212 color
[ACOMP
] = FixedToInt(span
->alpha
);
213 for (i
= 0; i
< n
; i
++) {
214 COPY_4V(rgba
[i
], color
);
218 GLushort (*rgba
)[4] = span
->array
->color
.sz2
.rgba
;
220 GLint dr
, dg
, db
, da
;
226 dg
= span
->greenStep
;
228 da
= span
->alphaStep
;
229 for (i
= 0; i
< n
; i
++) {
230 rgba
[i
][RCOMP
] = FixedToChan(r
);
231 rgba
[i
][GCOMP
] = FixedToChan(g
);
232 rgba
[i
][BCOMP
] = FixedToChan(b
);
233 rgba
[i
][ACOMP
] = FixedToChan(a
);
245 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
246 GLfloat r
, g
, b
, a
, dr
, dg
, db
, da
;
251 if (span
->interpMask
& SPAN_FLAT
) {
252 dr
= dg
= db
= da
= 0.0;
256 dg
= span
->greenStep
;
258 da
= span
->alphaStep
;
260 for (i
= 0; i
< n
; i
++) {
273 _mesa_problem(NULL
, "bad datatype in interpolate_colors");
275 span
->arrayMask
|= SPAN_RGBA
;
280 * Interpolate specular/secondary colors.
283 interpolate_specular(SWspan
*span
)
285 const GLuint n
= span
->end
;
288 switch (span
->array
->ChanType
) {
290 case GL_UNSIGNED_BYTE
:
292 GLubyte (*spec
)[4] = span
->array
->color
.sz1
.spec
;
293 if (span
->interpMask
& SPAN_FLAT
) {
295 color
[RCOMP
] = FixedToInt(span
->specRed
);
296 color
[GCOMP
] = FixedToInt(span
->specGreen
);
297 color
[BCOMP
] = FixedToInt(span
->specBlue
);
299 for (i
= 0; i
< n
; i
++) {
300 COPY_4UBV(spec
[i
], color
);
304 GLfixed r
= span
->specRed
;
305 GLfixed g
= span
->specGreen
;
306 GLfixed b
= span
->specBlue
;
307 GLint dr
= span
->specRedStep
;
308 GLint dg
= span
->specGreenStep
;
309 GLint db
= span
->specBlueStep
;
310 for (i
= 0; i
< n
; i
++) {
311 spec
[i
][RCOMP
] = CLAMP(FixedToChan(r
), 0, 255);
312 spec
[i
][GCOMP
] = CLAMP(FixedToChan(g
), 0, 255);
313 spec
[i
][BCOMP
] = CLAMP(FixedToChan(b
), 0, 255);
322 case GL_UNSIGNED_SHORT
:
324 GLushort (*spec
)[4] = span
->array
->color
.sz2
.spec
;
325 if (span
->interpMask
& SPAN_FLAT
) {
327 color
[RCOMP
] = FixedToInt(span
->specRed
);
328 color
[GCOMP
] = FixedToInt(span
->specGreen
);
329 color
[BCOMP
] = FixedToInt(span
->specBlue
);
331 for (i
= 0; i
< n
; i
++) {
332 COPY_4V(spec
[i
], color
);
336 GLfixed r
= FloatToFixed(span
->specRed
);
337 GLfixed g
= FloatToFixed(span
->specGreen
);
338 GLfixed b
= FloatToFixed(span
->specBlue
);
339 GLint dr
= FloatToFixed(span
->specRedStep
);
340 GLint dg
= FloatToFixed(span
->specGreenStep
);
341 GLint db
= FloatToFixed(span
->specBlueStep
);
342 for (i
= 0; i
< n
; i
++) {
343 spec
[i
][RCOMP
] = FixedToInt(r
);
344 spec
[i
][GCOMP
] = FixedToInt(g
);
345 spec
[i
][BCOMP
] = FixedToInt(b
);
357 GLfloat (*spec
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL1
];
359 GLfloat r
= CHAN_TO_FLOAT(FixedToChan(span
->specRed
));
360 GLfloat g
= CHAN_TO_FLOAT(FixedToChan(span
->specGreen
));
361 GLfloat b
= CHAN_TO_FLOAT(FixedToChan(span
->specBlue
));
363 GLfloat r
= span
->specRed
;
364 GLfloat g
= span
->specGreen
;
365 GLfloat b
= span
->specBlue
;
368 if (span
->interpMask
& SPAN_FLAT
) {
373 dr
= CHAN_TO_FLOAT(FixedToChan(span
->specRedStep
));
374 dg
= CHAN_TO_FLOAT(FixedToChan(span
->specGreenStep
));
375 db
= CHAN_TO_FLOAT(FixedToChan(span
->specBlueStep
));
377 dr
= span
->specRedStep
;
378 dg
= span
->specGreenStep
;
379 db
= span
->specBlueStep
;
382 for (i
= 0; i
< n
; i
++) {
386 spec
[i
][ACOMP
] = 0.0F
;
394 _mesa_problem(NULL
, "bad datatype in interpolate_specular");
396 span
->arrayMask
|= SPAN_SPEC
;
400 /* Fill in the span.color.index array from the interpolation values */
402 interpolate_indexes(GLcontext
*ctx
, SWspan
*span
)
404 GLfixed index
= span
->index
;
405 const GLint indexStep
= span
->indexStep
;
406 const GLuint n
= span
->end
;
407 GLuint
*indexes
= span
->array
->index
;
410 ASSERT((span
->interpMask
& SPAN_INDEX
) &&
411 !(span
->arrayMask
& SPAN_INDEX
));
413 if ((span
->interpMask
& SPAN_FLAT
) || (indexStep
== 0)) {
415 index
= FixedToInt(index
);
416 for (i
= 0; i
< n
; i
++) {
422 for (i
= 0; i
< n
; i
++) {
423 indexes
[i
] = FixedToInt(index
);
427 span
->arrayMask
|= SPAN_INDEX
;
428 span
->interpMask
&= ~SPAN_INDEX
;
432 /* Fill in the span.array.fog values from the interpolation values */
434 interpolate_fog(const GLcontext
*ctx
, SWspan
*span
)
436 GLfloat (*fog
)[4] = span
->array
->attribs
[FRAG_ATTRIB_FOGC
];
437 const GLfloat fogStep
= span
->attrStepX
[FRAG_ATTRIB_FOGC
][0];
438 GLfloat fogCoord
= span
->attrStart
[FRAG_ATTRIB_FOGC
][0];
439 const GLuint haveW
= (span
->interpMask
& SPAN_W
);
440 const GLfloat wStep
= haveW
? span
->attrStepX
[FRAG_ATTRIB_WPOS
][3] : 0.0F
;
441 GLfloat w
= haveW
? span
->attrStart
[FRAG_ATTRIB_WPOS
][3] : 1.0F
;
443 for (i
= 0; i
< span
->end
; i
++) {
444 fog
[i
][0] = fogCoord
/ w
;
448 span
->arrayMask
|= SPAN_FOG
;
452 /* Fill in the span.zArray array from the interpolation values */
454 _swrast_span_interpolate_z( const GLcontext
*ctx
, SWspan
*span
)
456 const GLuint n
= span
->end
;
459 ASSERT((span
->interpMask
& SPAN_Z
) &&
460 !(span
->arrayMask
& SPAN_Z
));
462 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
463 GLfixed zval
= span
->z
;
464 GLuint
*z
= span
->array
->z
;
465 for (i
= 0; i
< n
; i
++) {
466 z
[i
] = FixedToInt(zval
);
471 /* Deep Z buffer, no fixed->int shift */
472 GLuint zval
= span
->z
;
473 GLuint
*z
= span
->array
->z
;
474 for (i
= 0; i
< n
; i
++) {
479 span
->interpMask
&= ~SPAN_Z
;
480 span
->arrayMask
|= SPAN_Z
;
485 * This the ideal solution, as given in the OpenGL spec.
489 compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
490 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
491 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
493 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
494 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
495 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
496 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
497 GLfloat x
= SQRTF(dudx
* dudx
+ dvdx
* dvdx
);
498 GLfloat y
= SQRTF(dudy
* dudy
+ dvdy
* dvdy
);
499 GLfloat rho
= MAX2(x
, y
);
500 GLfloat lambda
= LOG2(rho
);
507 * This is a faster approximation
510 _swrast_compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
511 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
512 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
514 GLfloat dsdx2
= (s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
;
515 GLfloat dtdx2
= (t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
;
516 GLfloat dsdy2
= (s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
;
517 GLfloat dtdy2
= (t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
;
518 GLfloat maxU
, maxV
, rho
, lambda
;
519 dsdx2
= FABSF(dsdx2
);
520 dsdy2
= FABSF(dsdy2
);
521 dtdx2
= FABSF(dtdx2
);
522 dtdy2
= FABSF(dtdy2
);
523 maxU
= MAX2(dsdx2
, dsdy2
) * texW
;
524 maxV
= MAX2(dtdx2
, dtdy2
) * texH
;
525 rho
= MAX2(maxU
, maxV
);
532 * Fill in the span.texcoords array from the interpolation values.
533 * Note: in the places where we divide by Q (or mult by invQ) we're
534 * really doing two things: perspective correction and texcoord
535 * projection. Remember, for texcoord (s,t,r,q) we need to index
536 * texels with (s/q, t/q, r/q).
537 * If we're using a fragment program, we never do the division
538 * for texcoord projection. That's done by the TXP instruction
539 * or user-written code.
542 interpolate_texcoords(GLcontext
*ctx
, SWspan
*span
)
545 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
548 ASSERT(span
->interpMask
& SPAN_TEXTURE
);
549 ASSERT(!(span
->arrayMask
& SPAN_TEXTURE
));
551 span
->arrayMask
|= SPAN_TEXTURE
;
553 /* XXX CoordUnits vs. ImageUnits */
554 for (u
= 0; u
< maxUnit
; u
++) {
555 if (ctx
->Texture
._EnabledCoordUnits
& (1 << u
)) {
556 const GLuint attr
= FRAG_ATTRIB_TEX0
+ u
;
557 const struct gl_texture_object
*obj
=ctx
->Texture
.Unit
[u
]._Current
;
559 GLboolean needLambda
;
560 GLfloat (*texcoord
)[4] = span
->array
->attribs
[attr
];
561 GLfloat
*lambda
= span
->array
->lambda
[u
];
562 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
563 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
564 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
565 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
566 const GLfloat drdx
= span
->attrStepX
[attr
][2];
567 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
568 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
569 GLfloat s
= span
->attrStart
[attr
][0];
570 GLfloat t
= span
->attrStart
[attr
][1];
571 GLfloat r
= span
->attrStart
[attr
][2];
572 GLfloat q
= span
->attrStart
[attr
][3];
575 const struct gl_texture_image
*img
= obj
->Image
[0][obj
->BaseLevel
];
576 needLambda
= (obj
->MinFilter
!= obj
->MagFilter
)
577 || ctx
->FragmentProgram
._Current
;
578 texW
= img
->WidthScale
;
579 texH
= img
->HeightScale
;
582 /* using a fragment program */
585 needLambda
= GL_FALSE
;
590 if (ctx
->FragmentProgram
._Current
591 || ctx
->ATIFragmentShader
._Enabled
) {
592 /* do perspective correction but don't divide s, t, r by q */
593 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
594 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
595 for (i
= 0; i
< span
->end
; i
++) {
596 const GLfloat invW
= 1.0F
/ w
;
597 texcoord
[i
][0] = s
* invW
;
598 texcoord
[i
][1] = t
* invW
;
599 texcoord
[i
][2] = r
* invW
;
600 texcoord
[i
][3] = q
* invW
;
601 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
602 dqdx
, dqdy
, texW
, texH
,
612 for (i
= 0; i
< span
->end
; i
++) {
613 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
614 texcoord
[i
][0] = s
* invQ
;
615 texcoord
[i
][1] = t
* invQ
;
616 texcoord
[i
][2] = r
* invQ
;
618 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
619 dqdx
, dqdy
, texW
, texH
,
627 span
->arrayMask
|= SPAN_LAMBDA
;
631 if (ctx
->FragmentProgram
._Current
||
632 ctx
->ATIFragmentShader
._Enabled
) {
633 /* do perspective correction but don't divide s, t, r by q */
634 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
635 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
636 for (i
= 0; i
< span
->end
; i
++) {
637 const GLfloat invW
= 1.0F
/ w
;
638 texcoord
[i
][0] = s
* invW
;
639 texcoord
[i
][1] = t
* invW
;
640 texcoord
[i
][2] = r
* invW
;
641 texcoord
[i
][3] = q
* invW
;
650 else if (dqdx
== 0.0F
) {
651 /* Ortho projection or polygon's parallel to window X axis */
652 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
653 for (i
= 0; i
< span
->end
; i
++) {
654 texcoord
[i
][0] = s
* invQ
;
655 texcoord
[i
][1] = t
* invQ
;
656 texcoord
[i
][2] = r
* invQ
;
665 for (i
= 0; i
< span
->end
; i
++) {
666 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
667 texcoord
[i
][0] = s
* invQ
;
668 texcoord
[i
][1] = t
* invQ
;
669 texcoord
[i
][2] = r
* invQ
;
686 * Fill in the arrays->attribs[FRAG_ATTRIB_VARx] arrays from the
687 * interpolation values.
688 * XXX since interpolants/arrays are getting uniformed, we might merge
689 * this with interpolate_texcoords(), interpolate_Fog(), etc. someday.
692 interpolate_varying(GLcontext
*ctx
, SWspan
*span
)
695 const GLbitfield inputsUsed
= ctx
->FragmentProgram
._Current
->Base
.InputsRead
;
697 ASSERT(span
->interpMask
& SPAN_VARYING
);
698 ASSERT(!(span
->arrayMask
& SPAN_VARYING
));
700 span
->arrayMask
|= SPAN_VARYING
;
702 for (var
= 0; var
< MAX_VARYING
; var
++) {
703 if (inputsUsed
& FRAG_BIT_VAR(var
)) {
704 const GLuint attr
= FRAG_ATTRIB_VAR0
+ var
;
705 const GLfloat dwdx
= span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
706 GLfloat w
= span
->attrStart
[FRAG_ATTRIB_WPOS
][3];
707 const GLfloat dv0dx
= span
->attrStepX
[attr
][0];
708 const GLfloat dv1dx
= span
->attrStepX
[attr
][1];
709 const GLfloat dv2dx
= span
->attrStepX
[attr
][2];
710 const GLfloat dv3dx
= span
->attrStepX
[attr
][3];
711 GLfloat v0
= span
->attrStart
[attr
][0];
712 GLfloat v1
= span
->attrStart
[attr
][1];
713 GLfloat v2
= span
->attrStart
[attr
][2];
714 GLfloat v3
= span
->attrStart
[attr
][3];
716 for (k
= 0; k
< span
->end
; k
++) {
717 GLfloat invW
= 1.0f
/ w
;
718 span
->array
->attribs
[attr
][k
][0] = v0
* invW
;
719 span
->array
->attribs
[attr
][k
][1] = v1
* invW
;
720 span
->array
->attribs
[attr
][k
][2] = v2
* invW
;
721 span
->array
->attribs
[attr
][k
][3] = v3
* invW
;
734 * Fill in the arrays->attribs[FRAG_ATTRIB_WPOS] array.
737 interpolate_wpos(GLcontext
*ctx
, SWspan
*span
)
739 GLfloat (*wpos
)[4] = span
->array
->attribs
[FRAG_ATTRIB_WPOS
];
741 if (span
->arrayMask
& SPAN_XY
) {
742 for (i
= 0; i
< span
->end
; i
++) {
743 wpos
[i
][0] = (GLfloat
) span
->array
->x
[i
];
744 wpos
[i
][1] = (GLfloat
) span
->array
->y
[i
];
748 for (i
= 0; i
< span
->end
; i
++) {
749 wpos
[i
][0] = (GLfloat
) span
->x
+ i
;
750 wpos
[i
][1] = (GLfloat
) span
->y
;
753 for (i
= 0; i
< span
->end
; i
++) {
754 wpos
[i
][2] = (GLfloat
) span
->array
->z
[i
] / ctx
->DrawBuffer
->_DepthMaxF
;
755 wpos
[i
][3] = span
->attrStart
[FRAG_ATTRIB_WPOS
][3]
756 + i
* span
->attrStepX
[FRAG_ATTRIB_WPOS
][3];
762 * Apply the current polygon stipple pattern to a span of pixels.
765 stipple_polygon_span(GLcontext
*ctx
, SWspan
*span
)
767 GLubyte
*mask
= span
->array
->mask
;
769 ASSERT(ctx
->Polygon
.StippleFlag
);
771 if (span
->arrayMask
& SPAN_XY
) {
772 /* arrays of x/y pixel coords */
774 for (i
= 0; i
< span
->end
; i
++) {
775 const GLint col
= span
->array
->x
[i
] % 32;
776 const GLint row
= span
->array
->y
[i
] % 32;
777 const GLuint stipple
= ctx
->PolygonStipple
[row
];
778 if (((1 << col
) & stipple
) == 0) {
784 /* horizontal span of pixels */
785 const GLuint highBit
= 1 << 31;
786 const GLuint stipple
= ctx
->PolygonStipple
[span
->y
% 32];
787 GLuint i
, m
= highBit
>> (GLuint
) (span
->x
% 32);
788 for (i
= 0; i
< span
->end
; i
++) {
789 if ((m
& stipple
) == 0) {
798 span
->writeAll
= GL_FALSE
;
803 * Clip a pixel span to the current buffer/window boundaries:
804 * DrawBuffer->_Xmin, _Xmax, _Ymin, _Ymax. This will accomplish
805 * window clipping and scissoring.
806 * Return: GL_TRUE some pixels still visible
807 * GL_FALSE nothing visible
810 clip_span( GLcontext
*ctx
, SWspan
*span
)
812 const GLint xmin
= ctx
->DrawBuffer
->_Xmin
;
813 const GLint xmax
= ctx
->DrawBuffer
->_Xmax
;
814 const GLint ymin
= ctx
->DrawBuffer
->_Ymin
;
815 const GLint ymax
= ctx
->DrawBuffer
->_Ymax
;
817 if (span
->arrayMask
& SPAN_XY
) {
818 /* arrays of x/y pixel coords */
819 const GLint
*x
= span
->array
->x
;
820 const GLint
*y
= span
->array
->y
;
821 const GLint n
= span
->end
;
822 GLubyte
*mask
= span
->array
->mask
;
824 if (span
->arrayMask
& SPAN_MASK
) {
825 /* note: using & intead of && to reduce branches */
826 for (i
= 0; i
< n
; i
++) {
827 mask
[i
] &= (x
[i
] >= xmin
) & (x
[i
] < xmax
)
828 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
832 /* note: using & intead of && to reduce branches */
833 for (i
= 0; i
< n
; i
++) {
834 mask
[i
] = (x
[i
] >= xmin
) & (x
[i
] < xmax
)
835 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
838 return GL_TRUE
; /* some pixels visible */
841 /* horizontal span of pixels */
842 const GLint x
= span
->x
;
843 const GLint y
= span
->y
;
844 const GLint n
= span
->end
;
846 /* Trivial rejection tests */
847 if (y
< ymin
|| y
>= ymax
|| x
+ n
<= xmin
|| x
>= xmax
) {
849 return GL_FALSE
; /* all pixels clipped */
852 /* Clip to the left */
854 ASSERT(x
+ n
> xmin
);
855 span
->writeAll
= GL_FALSE
;
856 _mesa_bzero(span
->array
->mask
, (xmin
- x
) * sizeof(GLubyte
));
862 span
->end
= xmax
- x
;
865 return GL_TRUE
; /* some pixels visible */
871 * Apply all the per-fragment opertions to a span of color index fragments
872 * and write them to the enabled color drawbuffers.
873 * The 'span' parameter can be considered to be const. Note that
874 * span->interpMask and span->arrayMask may be changed but will be restored
875 * to their original values before returning.
878 _swrast_write_index_span( GLcontext
*ctx
, SWspan
*span
)
880 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
881 const GLbitfield origInterpMask
= span
->interpMask
;
882 const GLbitfield origArrayMask
= span
->arrayMask
;
884 ASSERT(span
->end
<= MAX_WIDTH
);
885 ASSERT(span
->primitive
== GL_POINT
|| span
->primitive
== GL_LINE
||
886 span
->primitive
== GL_POLYGON
|| span
->primitive
== GL_BITMAP
);
887 ASSERT((span
->interpMask
| span
->arrayMask
) & SPAN_INDEX
);
888 ASSERT((span
->interpMask
& span
->arrayMask
) == 0);
890 if (span
->arrayMask
& SPAN_MASK
) {
891 /* mask was initialized by caller, probably glBitmap */
892 span
->writeAll
= GL_FALSE
;
895 _mesa_memset(span
->array
->mask
, 1, span
->end
);
896 span
->writeAll
= GL_TRUE
;
900 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
901 if (!clip_span(ctx
, span
)) {
906 /* Depth bounds test */
907 if (ctx
->Depth
.BoundsTest
&& ctx
->DrawBuffer
->Visual
.depthBits
> 0) {
908 if (!_swrast_depth_bounds_test(ctx
, span
)) {
914 /* Make sure all fragments are within window bounds */
915 if (span
->arrayMask
& SPAN_XY
) {
917 for (i
= 0; i
< span
->end
; i
++) {
918 if (span
->array
->mask
[i
]) {
919 assert(span
->array
->x
[i
] >= ctx
->DrawBuffer
->_Xmin
);
920 assert(span
->array
->x
[i
] < ctx
->DrawBuffer
->_Xmax
);
921 assert(span
->array
->y
[i
] >= ctx
->DrawBuffer
->_Ymin
);
922 assert(span
->array
->y
[i
] < ctx
->DrawBuffer
->_Ymax
);
928 /* Polygon Stippling */
929 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
930 stipple_polygon_span(ctx
, span
);
933 /* Stencil and Z testing */
934 if (ctx
->Depth
.Test
|| ctx
->Stencil
.Enabled
) {
935 if (span
->interpMask
& SPAN_Z
)
936 _swrast_span_interpolate_z(ctx
, span
);
938 if (ctx
->Stencil
.Enabled
) {
939 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
940 span
->arrayMask
= origArrayMask
;
945 ASSERT(ctx
->Depth
.Test
);
946 if (!_swrast_depth_test_span(ctx
, span
)) {
947 span
->interpMask
= origInterpMask
;
948 span
->arrayMask
= origArrayMask
;
954 #if FEATURE_ARB_occlusion_query
955 if (ctx
->Query
.CurrentOcclusionObject
) {
956 /* update count of 'passed' fragments */
957 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
959 for (i
= 0; i
< span
->end
; i
++)
960 q
->Result
+= span
->array
->mask
[i
];
964 /* we have to wait until after occlusion to do this test */
965 if (ctx
->Color
.DrawBuffer
== GL_NONE
|| ctx
->Color
.IndexMask
== 0) {
966 /* write no pixels */
967 span
->arrayMask
= origArrayMask
;
971 /* Interpolate the color indexes if needed */
972 if (swrast
->_FogEnabled
||
973 ctx
->Color
.IndexLogicOpEnabled
||
974 ctx
->Color
.IndexMask
!= 0xffffffff ||
975 (span
->arrayMask
& SPAN_COVERAGE
)) {
976 if (span
->interpMask
& SPAN_INDEX
) {
977 interpolate_indexes(ctx
, span
);
982 if (swrast
->_FogEnabled
) {
983 _swrast_fog_ci_span(ctx
, span
);
986 /* Antialias coverage application */
987 if (span
->arrayMask
& SPAN_COVERAGE
) {
988 const GLfloat
*coverage
= span
->array
->coverage
;
989 GLuint
*index
= span
->array
->index
;
991 for (i
= 0; i
< span
->end
; i
++) {
992 ASSERT(coverage
[i
] < 16);
993 index
[i
] = (index
[i
] & ~0xf) | ((GLuint
) coverage
[i
]);
998 * Write to renderbuffers
1001 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1002 const GLuint output
= 0; /* only frag progs can write to other outputs */
1003 const GLuint numDrawBuffers
= fb
->_NumColorDrawBuffers
[output
];
1004 GLuint indexSave
[MAX_WIDTH
];
1007 if (numDrawBuffers
> 1) {
1008 /* save indexes for second, third renderbuffer writes */
1009 _mesa_memcpy(indexSave
, span
->array
->index
,
1010 span
->end
* sizeof(indexSave
[0]));
1013 for (buf
= 0; buf
< fb
->_NumColorDrawBuffers
[output
]; buf
++) {
1014 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[output
][buf
];
1015 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
1017 if (ctx
->Color
.IndexLogicOpEnabled
) {
1018 _swrast_logicop_ci_span(ctx
, rb
, span
);
1021 if (ctx
->Color
.IndexMask
!= 0xffffffff) {
1022 _swrast_mask_ci_span(ctx
, rb
, span
);
1025 if ((span
->interpMask
& SPAN_INDEX
) && span
->indexStep
== 0) {
1026 /* all fragments have same color index */
1032 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1033 index8
= FixedToInt(span
->index
);
1036 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1037 index16
= FixedToInt(span
->index
);
1041 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
1042 index32
= FixedToInt(span
->index
);
1046 if (span
->arrayMask
& SPAN_XY
) {
1047 rb
->PutMonoValues(ctx
, rb
, span
->end
, span
->array
->x
,
1048 span
->array
->y
, value
, span
->array
->mask
);
1051 rb
->PutMonoRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1052 value
, span
->array
->mask
);
1056 /* each fragment is a different color */
1057 GLubyte index8
[MAX_WIDTH
];
1058 GLushort index16
[MAX_WIDTH
];
1061 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1063 for (k
= 0; k
< span
->end
; k
++) {
1064 index8
[k
] = (GLubyte
) span
->array
->index
[k
];
1068 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1070 for (k
= 0; k
< span
->end
; k
++) {
1071 index16
[k
] = (GLushort
) span
->array
->index
[k
];
1076 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
1077 values
= span
->array
->index
;
1080 if (span
->arrayMask
& SPAN_XY
) {
1081 rb
->PutValues(ctx
, rb
, span
->end
,
1082 span
->array
->x
, span
->array
->y
,
1083 values
, span
->array
->mask
);
1086 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1087 values
, span
->array
->mask
);
1091 if (buf
+ 1 < numDrawBuffers
) {
1092 /* restore original span values */
1093 _mesa_memcpy(span
->array
->index
, indexSave
,
1094 span
->end
* sizeof(indexSave
[0]));
1099 span
->interpMask
= origInterpMask
;
1100 span
->arrayMask
= origArrayMask
;
1105 * Add specular color to base color. This is used only when
1106 * GL_LIGHT_MODEL_COLOR_CONTROL = GL_SEPARATE_SPECULAR_COLOR.
1109 add_specular(GLcontext
*ctx
, SWspan
*span
)
1111 switch (span
->array
->ChanType
) {
1112 case GL_UNSIGNED_BYTE
:
1114 GLubyte (*rgba
)[4] = span
->array
->color
.sz1
.rgba
;
1115 GLubyte (*spec
)[4] = span
->array
->color
.sz1
.spec
;
1117 for (i
= 0; i
< span
->end
; i
++) {
1118 GLint r
= rgba
[i
][RCOMP
] + spec
[i
][RCOMP
];
1119 GLint g
= rgba
[i
][GCOMP
] + spec
[i
][GCOMP
];
1120 GLint b
= rgba
[i
][BCOMP
] + spec
[i
][BCOMP
];
1121 GLint a
= rgba
[i
][ACOMP
] + spec
[i
][ACOMP
];
1122 rgba
[i
][RCOMP
] = MIN2(r
, 255);
1123 rgba
[i
][GCOMP
] = MIN2(g
, 255);
1124 rgba
[i
][BCOMP
] = MIN2(b
, 255);
1125 rgba
[i
][ACOMP
] = MIN2(a
, 255);
1129 case GL_UNSIGNED_SHORT
:
1131 GLushort (*rgba
)[4] = span
->array
->color
.sz2
.rgba
;
1132 GLushort (*spec
)[4] = span
->array
->color
.sz2
.spec
;
1134 for (i
= 0; i
< span
->end
; i
++) {
1135 GLint r
= rgba
[i
][RCOMP
] + spec
[i
][RCOMP
];
1136 GLint g
= rgba
[i
][GCOMP
] + spec
[i
][GCOMP
];
1137 GLint b
= rgba
[i
][BCOMP
] + spec
[i
][BCOMP
];
1138 GLint a
= rgba
[i
][ACOMP
] + spec
[i
][ACOMP
];
1139 rgba
[i
][RCOMP
] = MIN2(r
, 65535);
1140 rgba
[i
][GCOMP
] = MIN2(g
, 65535);
1141 rgba
[i
][BCOMP
] = MIN2(b
, 65535);
1142 rgba
[i
][ACOMP
] = MIN2(a
, 65535);
1148 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1149 GLfloat (*spec
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL1
];
1151 for (i
= 0; i
< span
->end
; i
++) {
1152 rgba
[i
][RCOMP
] += spec
[i
][RCOMP
];
1153 rgba
[i
][GCOMP
] += spec
[i
][GCOMP
];
1154 rgba
[i
][BCOMP
] += spec
[i
][BCOMP
];
1155 rgba
[i
][ACOMP
] += spec
[i
][ACOMP
];
1160 _mesa_problem(ctx
, "Invalid datatype in add_specular");
1166 * Apply antialiasing coverage value to alpha values.
1169 apply_aa_coverage(SWspan
*span
)
1171 const GLfloat
*coverage
= span
->array
->coverage
;
1173 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1174 GLubyte (*rgba
)[4] = span
->array
->color
.sz1
.rgba
;
1175 for (i
= 0; i
< span
->end
; i
++) {
1176 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
1177 rgba
[i
][ACOMP
] = (GLubyte
) CLAMP(a
, 0.0, 255.0);
1178 ASSERT(coverage
[i
] >= 0.0);
1179 ASSERT(coverage
[i
] <= 1.0);
1182 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
1183 GLushort (*rgba
)[4] = span
->array
->color
.sz2
.rgba
;
1184 for (i
= 0; i
< span
->end
; i
++) {
1185 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
1186 rgba
[i
][ACOMP
] = (GLushort
) CLAMP(a
, 0.0, 65535.0);
1190 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1191 for (i
= 0; i
< span
->end
; i
++) {
1192 rgba
[i
][ACOMP
] = rgba
[i
][ACOMP
] * coverage
[i
];
1199 * Clamp span's float colors to [0,1]
1202 clamp_colors(SWspan
*span
)
1204 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1206 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
1207 for (i
= 0; i
< span
->end
; i
++) {
1208 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0F
, 1.0F
);
1209 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0F
, 1.0F
);
1210 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0F
, 1.0F
);
1211 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0F
, 1.0F
);
1217 * Convert the span's color arrays to the given type.
1220 convert_color_type(SWspan
*span
, GLenum newType
)
1223 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1224 src
= span
->array
->color
.sz1
.rgba
;
1226 else if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1227 src
= span
->array
->color
.sz2
.rgba
;
1230 src
= span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1232 if (newType
== GL_UNSIGNED_BYTE
) {
1233 dst
= span
->array
->color
.sz1
.rgba
;
1235 else if (newType
== GL_UNSIGNED_BYTE
) {
1236 dst
= span
->array
->color
.sz2
.rgba
;
1239 dst
= span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1242 _mesa_convert_colors(span
->array
->ChanType
, src
,
1244 span
->end
, span
->array
->mask
);
1246 span
->array
->ChanType
= newType
;
1252 * Apply fragment shader, fragment program or normal texturing to span.
1255 shade_texture_span(GLcontext
*ctx
, SWspan
*span
)
1257 /* Now we need the rgba array, fill it in if needed */
1258 if (span
->interpMask
& SPAN_RGBA
)
1259 interpolate_colors(span
);
1261 if (ctx
->Texture
._EnabledCoordUnits
&& (span
->interpMask
& SPAN_TEXTURE
))
1262 interpolate_texcoords(ctx
, span
);
1264 if (ctx
->FragmentProgram
._Current
||
1265 ctx
->ATIFragmentShader
._Enabled
) {
1267 /* use float colors if running a fragment program or shader */
1268 const GLenum oldType
= span
->array
->ChanType
;
1269 const GLenum newType
= GL_FLOAT
;
1270 if (oldType
!= newType
) {
1271 GLvoid
*src
= (oldType
== GL_UNSIGNED_BYTE
)
1272 ? (GLvoid
*) span
->array
->color
.sz1
.rgba
1273 : (GLvoid
*) span
->array
->color
.sz2
.rgba
;
1274 _mesa_convert_colors(oldType
, src
,
1275 newType
, span
->array
->attribs
[FRAG_ATTRIB_COL0
],
1276 span
->end
, span
->array
->mask
);
1277 span
->array
->ChanType
= newType
;
1280 /* fragment programs/shaders may need specular, fog and Z coords */
1281 if (span
->interpMask
& SPAN_SPEC
)
1282 interpolate_specular(span
);
1284 if (span
->interpMask
& SPAN_FOG
)
1285 interpolate_fog(ctx
, span
);
1287 if (span
->interpMask
& SPAN_Z
)
1288 _swrast_span_interpolate_z (ctx
, span
);
1290 if (ctx
->Shader
.CurrentProgram
&& span
->interpMask
& SPAN_VARYING
)
1291 interpolate_varying(ctx
, span
);
1293 if (ctx
->FragmentProgram
._Current
&&
1294 (ctx
->FragmentProgram
._Current
->Base
.InputsRead
& FRAG_BIT_WPOS
))
1295 interpolate_wpos(ctx
, span
);
1297 /* Run fragment program/shader now */
1298 if (ctx
->FragmentProgram
._Current
) {
1299 _swrast_exec_fragment_program(ctx
, span
);
1302 ASSERT(ctx
->ATIFragmentShader
._Enabled
);
1303 _swrast_exec_fragment_shader(ctx
, span
);
1306 else if (ctx
->Texture
._EnabledUnits
&& (span
->arrayMask
& SPAN_TEXTURE
)) {
1307 /* conventional texturing */
1308 _swrast_texture_span(ctx
, span
);
1315 * Apply all the per-fragment operations to a span.
1316 * This now includes texturing (_swrast_write_texture_span() is history).
1317 * This function may modify any of the array values in the span.
1318 * span->interpMask and span->arrayMask may be changed but will be restored
1319 * to their original values before returning.
1322 _swrast_write_rgba_span( GLcontext
*ctx
, SWspan
*span
)
1324 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1325 const GLuint colorMask
= *((GLuint
*) ctx
->Color
.ColorMask
);
1326 const GLbitfield origInterpMask
= span
->interpMask
;
1327 const GLbitfield origArrayMask
= span
->arrayMask
;
1328 const GLenum chanType
= span
->array
->ChanType
;
1329 const GLboolean shader
= (ctx
->FragmentProgram
._Current
1330 || ctx
->ATIFragmentShader
._Enabled
);
1331 const GLboolean shaderOrTexture
= shader
|| ctx
->Texture
._EnabledUnits
;
1332 GLboolean deferredTexture
;
1335 printf("%s() interp 0x%x array 0x%x\n", __FUNCTION__,
1336 span->interpMask, span->arrayMask);
1339 ASSERT(span
->primitive
== GL_POINT
||
1340 span
->primitive
== GL_LINE
||
1341 span
->primitive
== GL_POLYGON
||
1342 span
->primitive
== GL_BITMAP
);
1343 ASSERT(span
->end
<= MAX_WIDTH
);
1344 ASSERT((span
->interpMask
& span
->arrayMask
) == 0);
1345 ASSERT((span
->interpMask
& SPAN_RGBA
) ^ (span
->arrayMask
& SPAN_RGBA
));
1347 /* check for conditions that prevent deferred shading */
1348 if (ctx
->Color
.AlphaEnabled
) {
1349 /* alpha test depends on post-texture/shader colors */
1350 deferredTexture
= GL_FALSE
;
1352 else if (shaderOrTexture
) {
1353 if (ctx
->FragmentProgram
._Current
) {
1354 if (ctx
->FragmentProgram
._Current
->Base
.OutputsWritten
1355 & (1 << FRAG_RESULT_DEPR
)) {
1356 /* Z comes from fragment program/shader */
1357 deferredTexture
= GL_FALSE
;
1360 deferredTexture
= GL_TRUE
;
1364 /* ATI frag shader or conventional texturing */
1365 deferredTexture
= GL_TRUE
;
1369 /* no texturing or shadering */
1370 deferredTexture
= GL_FALSE
;
1373 /* Fragment write masks */
1374 if (span
->arrayMask
& SPAN_MASK
) {
1375 /* mask was initialized by caller, probably glBitmap */
1376 span
->writeAll
= GL_FALSE
;
1379 _mesa_memset(span
->array
->mask
, 1, span
->end
);
1380 span
->writeAll
= GL_TRUE
;
1383 /* Clip to window/scissor box */
1384 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
1385 if (!clip_span(ctx
, span
)) {
1391 /* Make sure all fragments are within window bounds */
1392 if (span
->arrayMask
& SPAN_XY
) {
1394 for (i
= 0; i
< span
->end
; i
++) {
1395 if (span
->array
->mask
[i
]) {
1396 assert(span
->array
->x
[i
] >= ctx
->DrawBuffer
->_Xmin
);
1397 assert(span
->array
->x
[i
] < ctx
->DrawBuffer
->_Xmax
);
1398 assert(span
->array
->y
[i
] >= ctx
->DrawBuffer
->_Ymin
);
1399 assert(span
->array
->y
[i
] < ctx
->DrawBuffer
->_Ymax
);
1405 /* Polygon Stippling */
1406 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
1407 stipple_polygon_span(ctx
, span
);
1410 /* This is the normal place to compute the resulting fragment color/Z.
1411 * As an optimization, we try to defer this until after Z/stencil
1412 * testing in order to try to avoid computing colors that we won't
1415 if (shaderOrTexture
&& !deferredTexture
) {
1416 shade_texture_span(ctx
, span
);
1419 /* Do the alpha test */
1420 if (ctx
->Color
.AlphaEnabled
) {
1421 if (!_swrast_alpha_test(ctx
, span
)) {
1426 /* Stencil and Z testing */
1427 if (ctx
->Stencil
.Enabled
|| ctx
->Depth
.Test
) {
1428 if (span
->interpMask
& SPAN_Z
)
1429 _swrast_span_interpolate_z(ctx
, span
);
1431 if (ctx
->Stencil
.Enabled
&& ctx
->DrawBuffer
->Visual
.stencilBits
> 0) {
1432 /* Combined Z/stencil tests */
1433 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
1437 else if (ctx
->DrawBuffer
->Visual
.depthBits
> 0) {
1438 /* Just regular depth testing */
1439 ASSERT(ctx
->Depth
.Test
);
1440 ASSERT(span
->arrayMask
& SPAN_Z
);
1441 if (!_swrast_depth_test_span(ctx
, span
)) {
1447 #if FEATURE_ARB_occlusion_query
1448 if (ctx
->Query
.CurrentOcclusionObject
) {
1449 /* update count of 'passed' fragments */
1450 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
1452 for (i
= 0; i
< span
->end
; i
++)
1453 q
->Result
+= span
->array
->mask
[i
];
1457 /* We had to wait until now to check for glColorMask(0,0,0,0) because of
1458 * the occlusion test.
1460 if (colorMask
== 0x0) {
1464 /* If we were able to defer fragment color computation to now, there's
1465 * a good chance that many fragments will have already been killed by
1466 * Z/stencil testing.
1468 if (deferredTexture
) {
1469 ASSERT(shaderOrTexture
);
1470 shade_texture_span(ctx
, span
);
1473 if ((span
->arrayMask
& SPAN_RGBA
) == 0) {
1474 interpolate_colors(span
);
1477 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1480 /* Add base and specular colors */
1481 if (ctx
->Fog
.ColorSumEnabled
||
1482 (ctx
->Light
.Enabled
&&
1483 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)) {
1484 if (span
->interpMask
& SPAN_SPEC
) {
1485 interpolate_specular(span
);
1487 if (span
->arrayMask
& SPAN_SPEC
) {
1488 add_specular(ctx
, span
);
1491 /* We probably added the base/specular colors during the
1499 if (swrast
->_FogEnabled
) {
1500 _swrast_fog_rgba_span(ctx
, span
);
1503 /* Antialias coverage application */
1504 if (span
->arrayMask
& SPAN_COVERAGE
) {
1505 apply_aa_coverage(span
);
1508 /* Clamp color/alpha values over the range [0.0, 1.0] before storage */
1509 if (ctx
->Color
.ClampFragmentColor
== GL_TRUE
&&
1510 span
->array
->ChanType
== GL_FLOAT
) {
1515 * Write to renderbuffers
1518 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1519 const GLuint output
= 0; /* only frag progs can write to other outputs */
1520 const GLuint numDrawBuffers
= fb
->_NumColorDrawBuffers
[output
];
1521 GLchan rgbaSave
[MAX_WIDTH
][4];
1524 if (numDrawBuffers
> 0) {
1525 if (fb
->_ColorDrawBuffers
[output
][0]->DataType
1526 != span
->array
->ChanType
) {
1527 convert_color_type(span
,
1528 fb
->_ColorDrawBuffers
[output
][0]->DataType
);
1532 if (numDrawBuffers
> 1) {
1533 /* save colors for second, third renderbuffer writes */
1534 _mesa_memcpy(rgbaSave
, span
->array
->rgba
,
1535 4 * span
->end
* sizeof(GLchan
));
1538 for (buf
= 0; buf
< numDrawBuffers
; buf
++) {
1539 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[output
][buf
];
1540 ASSERT(rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
);
1542 if (ctx
->Color
._LogicOpEnabled
) {
1543 _swrast_logicop_rgba_span(ctx
, rb
, span
);
1545 else if (ctx
->Color
.BlendEnabled
) {
1546 _swrast_blend_span(ctx
, rb
, span
);
1549 if (colorMask
!= 0xffffffff) {
1550 _swrast_mask_rgba_span(ctx
, rb
, span
);
1553 if (span
->arrayMask
& SPAN_XY
) {
1554 /* array of pixel coords */
1555 ASSERT(rb
->PutValues
);
1556 rb
->PutValues(ctx
, rb
, span
->end
,
1557 span
->array
->x
, span
->array
->y
,
1558 span
->array
->rgba
, span
->array
->mask
);
1561 /* horizontal run of pixels */
1563 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
, span
->array
->rgba
,
1564 span
->writeAll
? NULL
: span
->array
->mask
);
1567 if (buf
+ 1 < numDrawBuffers
) {
1568 /* restore original span values */
1569 _mesa_memcpy(span
->array
->rgba
, rgbaSave
,
1570 4 * span
->end
* sizeof(GLchan
));
1577 /* restore these values before returning */
1578 span
->interpMask
= origInterpMask
;
1579 span
->arrayMask
= origArrayMask
;
1580 span
->array
->ChanType
= chanType
;
1585 * Read RGBA pixels from a renderbuffer. Clipping will be done to prevent
1586 * reading ouside the buffer's boundaries.
1587 * \param dstType datatype for returned colors
1588 * \param rgba the returned colors
1591 _swrast_read_rgba_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1592 GLuint n
, GLint x
, GLint y
, GLenum dstType
,
1595 const GLint bufWidth
= (GLint
) rb
->Width
;
1596 const GLint bufHeight
= (GLint
) rb
->Height
;
1598 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1599 /* completely above, below, or right */
1600 /* XXX maybe leave rgba values undefined? */
1601 _mesa_bzero(rgba
, 4 * n
* sizeof(GLchan
));
1606 /* left edge clipping */
1608 length
= (GLint
) n
- skip
;
1610 /* completely left of window */
1613 if (length
> bufWidth
) {
1617 else if ((GLint
) (x
+ n
) > bufWidth
) {
1618 /* right edge clipping */
1620 length
= bufWidth
- x
;
1622 /* completely to right of window */
1634 ASSERT(rb
->_BaseFormat
== GL_RGB
|| rb
->_BaseFormat
== GL_RGBA
);
1636 if (rb
->DataType
== dstType
) {
1637 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
,
1638 (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(rb
->DataType
));
1641 GLuint temp
[MAX_WIDTH
* 4];
1642 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, temp
);
1643 _mesa_convert_colors(rb
->DataType
, temp
,
1644 dstType
, (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(dstType
),
1652 * Read CI pixels from a renderbuffer. Clipping will be done to prevent
1653 * reading ouside the buffer's boundaries.
1656 _swrast_read_index_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1657 GLuint n
, GLint x
, GLint y
, GLuint index
[] )
1659 const GLint bufWidth
= (GLint
) rb
->Width
;
1660 const GLint bufHeight
= (GLint
) rb
->Height
;
1662 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1663 /* completely above, below, or right */
1664 _mesa_bzero(index
, n
* sizeof(GLuint
));
1669 /* left edge clipping */
1671 length
= (GLint
) n
- skip
;
1673 /* completely left of window */
1676 if (length
> bufWidth
) {
1680 else if ((GLint
) (x
+ n
) > bufWidth
) {
1681 /* right edge clipping */
1683 length
= bufWidth
- x
;
1685 /* completely to right of window */
1696 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
1698 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1699 GLubyte index8
[MAX_WIDTH
];
1701 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index8
);
1702 for (i
= 0; i
< length
; i
++)
1703 index
[skip
+ i
] = index8
[i
];
1705 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1706 GLushort index16
[MAX_WIDTH
];
1708 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index16
);
1709 for (i
= 0; i
< length
; i
++)
1710 index
[skip
+ i
] = index16
[i
];
1712 else if (rb
->DataType
== GL_UNSIGNED_INT
) {
1713 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index
+ skip
);
1720 * Wrapper for gl_renderbuffer::GetValues() which does clipping to avoid
1721 * reading values outside the buffer bounds.
1722 * We can use this for reading any format/type of renderbuffer.
1723 * \param valueSize is the size in bytes of each value (pixel) put into the
1727 _swrast_get_values(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1728 GLuint count
, const GLint x
[], const GLint y
[],
1729 void *values
, GLuint valueSize
)
1731 GLuint i
, inCount
= 0, inStart
= 0;
1733 for (i
= 0; i
< count
; i
++) {
1734 if (x
[i
] >= 0 && y
[i
] >= 0 &&
1735 x
[i
] < (GLint
) rb
->Width
&& y
[i
] < (GLint
) rb
->Height
) {
1743 /* read [inStart, inStart + inCount) */
1744 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1745 (GLubyte
*) values
+ inStart
* valueSize
);
1751 /* read last values */
1752 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1753 (GLubyte
*) values
+ inStart
* valueSize
);
1759 * Wrapper for gl_renderbuffer::PutRow() which does clipping.
1760 * \param valueSize size of each value (pixel) in bytes
1763 _swrast_put_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1764 GLuint count
, GLint x
, GLint y
,
1765 const GLvoid
*values
, GLuint valueSize
)
1769 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1770 return; /* above or below */
1772 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1773 return; /* entirely left or right */
1775 if ((GLint
) (x
+ count
) > (GLint
) rb
->Width
) {
1777 GLint clip
= x
+ count
- rb
->Width
;
1788 rb
->PutRow(ctx
, rb
, count
, x
, y
,
1789 (const GLubyte
*) values
+ skip
* valueSize
, NULL
);
1794 * Wrapper for gl_renderbuffer::GetRow() which does clipping.
1795 * \param valueSize size of each value (pixel) in bytes
1798 _swrast_get_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1799 GLuint count
, GLint x
, GLint y
,
1800 GLvoid
*values
, GLuint valueSize
)
1804 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1805 return; /* above or below */
1807 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1808 return; /* entirely left or right */
1810 if (x
+ count
> rb
->Width
) {
1812 GLint clip
= x
+ count
- rb
->Width
;
1823 rb
->GetRow(ctx
, rb
, count
, x
, y
, (GLubyte
*) values
+ skip
* valueSize
);
1828 * Get RGBA pixels from the given renderbuffer. Put the pixel colors into
1829 * the span's specular color arrays. The specular color arrays should no
1830 * longer be needed by time this function is called.
1831 * Used by blending, logicop and masking functions.
1832 * \return pointer to the colors we read.
1835 _swrast_get_dest_rgba(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1838 const GLuint pixelSize
= RGBA_PIXEL_SIZE(span
->array
->ChanType
);
1842 * Determine pixel size (in bytes).
1843 * Point rbPixels to a temporary space (use specular color arrays).
1845 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1846 rbPixels
= span
->array
->color
.sz1
.spec
;
1848 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
1849 rbPixels
= span
->array
->color
.sz2
.spec
;
1852 rbPixels
= span
->array
->attribs
[FRAG_ATTRIB_COL1
];
1855 /* Get destination values from renderbuffer */
1856 if (span
->arrayMask
& SPAN_XY
) {
1857 _swrast_get_values(ctx
, rb
, span
->end
, span
->array
->x
, span
->array
->y
,
1858 rbPixels
, pixelSize
);
1861 _swrast_get_row(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1862 rbPixels
, pixelSize
);