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_nvfragprog.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
->fog
= _swrast_z_to_fogfactor(ctx
, ctx
->Current
.RasterDistance
);
79 span
->fogStep
= span
->dfogdx
= span
->dfogdy
= 0.0F
;
80 span
->interpMask
|= SPAN_FOG
;
85 * Init span's rgba or index interpolation values to the RasterPos color.
86 * Used during setup for glDraw/CopyPixels.
89 _swrast_span_default_color( GLcontext
*ctx
, SWspan
*span
)
91 if (ctx
->Visual
.rgbMode
) {
93 UNCLAMPED_FLOAT_TO_CHAN(r
, ctx
->Current
.RasterColor
[0]);
94 UNCLAMPED_FLOAT_TO_CHAN(g
, ctx
->Current
.RasterColor
[1]);
95 UNCLAMPED_FLOAT_TO_CHAN(b
, ctx
->Current
.RasterColor
[2]);
96 UNCLAMPED_FLOAT_TO_CHAN(a
, ctx
->Current
.RasterColor
[3]);
97 #if CHAN_TYPE == GL_FLOAT
103 span
->red
= IntToFixed(r
);
104 span
->green
= IntToFixed(g
);
105 span
->blue
= IntToFixed(b
);
106 span
->alpha
= IntToFixed(a
);
112 span
->interpMask
|= SPAN_RGBA
;
115 span
->index
= FloatToFixed(ctx
->Current
.RasterIndex
);
117 span
->interpMask
|= SPAN_INDEX
;
123 * Init span's texcoord interpolation values to the RasterPos texcoords.
124 * Used during setup for glDraw/CopyPixels.
127 _swrast_span_default_texcoords( GLcontext
*ctx
, SWspan
*span
)
130 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
131 const GLfloat
*tc
= ctx
->Current
.RasterTexCoords
[i
];
132 if (ctx
->FragmentProgram
._Current
|| ctx
->ATIFragmentShader
._Enabled
) {
133 COPY_4V(span
->tex
[i
], tc
);
135 else if (tc
[3] > 0.0F
) {
136 /* use (s/q, t/q, r/q, 1) */
137 span
->tex
[i
][0] = tc
[0] / tc
[3];
138 span
->tex
[i
][1] = tc
[1] / tc
[3];
139 span
->tex
[i
][2] = tc
[2] / tc
[3];
140 span
->tex
[i
][3] = 1.0;
143 ASSIGN_4V(span
->tex
[i
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
145 ASSIGN_4V(span
->texStepX
[i
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
146 ASSIGN_4V(span
->texStepY
[i
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
148 span
->interpMask
|= SPAN_TEXTURE
;
153 * Interpolate primary colors to fill in the span->array->color array.
156 interpolate_colors(SWspan
*span
)
158 const GLuint n
= span
->end
;
161 ASSERT((span
->interpMask
& SPAN_RGBA
) &&
162 !(span
->arrayMask
& SPAN_RGBA
));
164 switch (span
->array
->ChanType
) {
166 case GL_UNSIGNED_BYTE
:
168 GLubyte (*rgba
)[4] = span
->array
->color
.sz1
.rgba
;
169 if (span
->interpMask
& SPAN_FLAT
) {
171 color
[RCOMP
] = FixedToInt(span
->red
);
172 color
[GCOMP
] = FixedToInt(span
->green
);
173 color
[BCOMP
] = FixedToInt(span
->blue
);
174 color
[ACOMP
] = FixedToInt(span
->alpha
);
175 for (i
= 0; i
< n
; i
++) {
176 COPY_4UBV(rgba
[i
], color
);
180 GLfixed r
= span
->red
;
181 GLfixed g
= span
->green
;
182 GLfixed b
= span
->blue
;
183 GLfixed a
= span
->alpha
;
184 GLint dr
= span
->redStep
;
185 GLint dg
= span
->greenStep
;
186 GLint db
= span
->blueStep
;
187 GLint da
= span
->alphaStep
;
188 for (i
= 0; i
< n
; i
++) {
189 rgba
[i
][RCOMP
] = FixedToChan(r
);
190 rgba
[i
][GCOMP
] = FixedToChan(g
);
191 rgba
[i
][BCOMP
] = FixedToChan(b
);
192 rgba
[i
][ACOMP
] = FixedToChan(a
);
201 case GL_UNSIGNED_SHORT
:
203 GLushort (*rgba
)[4] = span
->array
->color
.sz2
.rgba
;
204 if (span
->interpMask
& SPAN_FLAT
) {
206 color
[RCOMP
] = FixedToInt(span
->red
);
207 color
[GCOMP
] = FixedToInt(span
->green
);
208 color
[BCOMP
] = FixedToInt(span
->blue
);
209 color
[ACOMP
] = FixedToInt(span
->alpha
);
210 for (i
= 0; i
< n
; i
++) {
211 COPY_4V(rgba
[i
], color
);
215 GLushort (*rgba
)[4] = span
->array
->color
.sz2
.rgba
;
217 GLint dr
, dg
, db
, da
;
223 dg
= span
->greenStep
;
225 da
= span
->alphaStep
;
226 for (i
= 0; i
< n
; i
++) {
227 rgba
[i
][RCOMP
] = FixedToChan(r
);
228 rgba
[i
][GCOMP
] = FixedToChan(g
);
229 rgba
[i
][BCOMP
] = FixedToChan(b
);
230 rgba
[i
][ACOMP
] = FixedToChan(a
);
242 GLfloat (*rgba
)[4] = span
->array
->color
.sz4
.rgba
;
243 GLfloat r
, g
, b
, a
, dr
, dg
, db
, da
;
248 if (span
->interpMask
& SPAN_FLAT
) {
249 dr
= dg
= db
= da
= 0.0;
253 dg
= span
->greenStep
;
255 da
= span
->alphaStep
;
257 for (i
= 0; i
< n
; i
++) {
270 _mesa_problem(NULL
, "bad datatype in interpolate_colors");
272 span
->arrayMask
|= SPAN_RGBA
;
277 * Interpolate specular/secondary colors.
280 interpolate_specular(SWspan
*span
)
282 const GLuint n
= span
->end
;
285 switch (span
->array
->ChanType
) {
287 case GL_UNSIGNED_BYTE
:
289 GLubyte (*spec
)[4] = span
->array
->color
.sz1
.spec
;
290 if (span
->interpMask
& SPAN_FLAT
) {
292 color
[RCOMP
] = FixedToInt(span
->specRed
);
293 color
[GCOMP
] = FixedToInt(span
->specGreen
);
294 color
[BCOMP
] = FixedToInt(span
->specBlue
);
296 for (i
= 0; i
< n
; i
++) {
297 COPY_4UBV(spec
[i
], color
);
301 GLfixed r
= span
->specRed
;
302 GLfixed g
= span
->specGreen
;
303 GLfixed b
= span
->specBlue
;
304 GLint dr
= span
->specRedStep
;
305 GLint dg
= span
->specGreenStep
;
306 GLint db
= span
->specBlueStep
;
307 for (i
= 0; i
< n
; i
++) {
308 spec
[i
][RCOMP
] = CLAMP(FixedToChan(r
), 0, 255);
309 spec
[i
][GCOMP
] = CLAMP(FixedToChan(g
), 0, 255);
310 spec
[i
][BCOMP
] = CLAMP(FixedToChan(b
), 0, 255);
319 case GL_UNSIGNED_SHORT
:
321 GLushort (*spec
)[4] = span
->array
->color
.sz2
.spec
;
322 if (span
->interpMask
& SPAN_FLAT
) {
324 color
[RCOMP
] = FixedToInt(span
->specRed
);
325 color
[GCOMP
] = FixedToInt(span
->specGreen
);
326 color
[BCOMP
] = FixedToInt(span
->specBlue
);
328 for (i
= 0; i
< n
; i
++) {
329 COPY_4V(spec
[i
], color
);
333 GLfixed r
= FloatToFixed(span
->specRed
);
334 GLfixed g
= FloatToFixed(span
->specGreen
);
335 GLfixed b
= FloatToFixed(span
->specBlue
);
336 GLint dr
= FloatToFixed(span
->specRedStep
);
337 GLint dg
= FloatToFixed(span
->specGreenStep
);
338 GLint db
= FloatToFixed(span
->specBlueStep
);
339 for (i
= 0; i
< n
; i
++) {
340 spec
[i
][RCOMP
] = FixedToInt(r
);
341 spec
[i
][GCOMP
] = FixedToInt(g
);
342 spec
[i
][BCOMP
] = FixedToInt(b
);
354 GLfloat (*spec
)[4] = span
->array
->color
.sz4
.spec
;
356 GLfloat r
= CHAN_TO_FLOAT(FixedToChan(span
->specRed
));
357 GLfloat g
= CHAN_TO_FLOAT(FixedToChan(span
->specGreen
));
358 GLfloat b
= CHAN_TO_FLOAT(FixedToChan(span
->specBlue
));
360 GLfloat r
= span
->specRed
;
361 GLfloat g
= span
->specGreen
;
362 GLfloat b
= span
->specBlue
;
365 if (span
->interpMask
& SPAN_FLAT
) {
370 dr
= CHAN_TO_FLOAT(FixedToChan(span
->specRedStep
));
371 dg
= CHAN_TO_FLOAT(FixedToChan(span
->specGreenStep
));
372 db
= CHAN_TO_FLOAT(FixedToChan(span
->specBlueStep
));
374 dr
= span
->specRedStep
;
375 dg
= span
->specGreenStep
;
376 db
= span
->specBlueStep
;
379 for (i
= 0; i
< n
; i
++) {
383 spec
[i
][ACOMP
] = 0.0F
;
391 _mesa_problem(NULL
, "bad datatype in interpolate_specular");
393 span
->arrayMask
|= SPAN_SPEC
;
397 /* Fill in the span.color.index array from the interpolation values */
399 interpolate_indexes(GLcontext
*ctx
, SWspan
*span
)
401 GLfixed index
= span
->index
;
402 const GLint indexStep
= span
->indexStep
;
403 const GLuint n
= span
->end
;
404 GLuint
*indexes
= span
->array
->index
;
407 ASSERT((span
->interpMask
& SPAN_INDEX
) &&
408 !(span
->arrayMask
& SPAN_INDEX
));
410 if ((span
->interpMask
& SPAN_FLAT
) || (indexStep
== 0)) {
412 index
= FixedToInt(index
);
413 for (i
= 0; i
< n
; i
++) {
419 for (i
= 0; i
< n
; i
++) {
420 indexes
[i
] = FixedToInt(index
);
424 span
->arrayMask
|= SPAN_INDEX
;
425 span
->interpMask
&= ~SPAN_INDEX
;
429 /* Fill in the span.array.fog values from the interpolation values */
431 interpolate_fog(const GLcontext
*ctx
, SWspan
*span
)
433 GLfloat
*fog
= span
->array
->fog
;
434 const GLfloat fogStep
= span
->fogStep
;
435 GLfloat fogCoord
= span
->fog
;
436 const GLuint haveW
= (span
->interpMask
& SPAN_W
);
437 const GLfloat wStep
= haveW
? span
->dwdx
: 0.0F
;
438 GLfloat w
= haveW
? span
->w
: 1.0F
;
440 for (i
= 0; i
< span
->end
; i
++) {
441 fog
[i
] = fogCoord
/ w
;
445 span
->arrayMask
|= SPAN_FOG
;
449 /* Fill in the span.zArray array from the interpolation values */
451 _swrast_span_interpolate_z( const GLcontext
*ctx
, SWspan
*span
)
453 const GLuint n
= span
->end
;
456 ASSERT((span
->interpMask
& SPAN_Z
) &&
457 !(span
->arrayMask
& SPAN_Z
));
459 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
460 GLfixed zval
= span
->z
;
461 GLuint
*z
= span
->array
->z
;
462 for (i
= 0; i
< n
; i
++) {
463 z
[i
] = FixedToInt(zval
);
468 /* Deep Z buffer, no fixed->int shift */
469 GLuint zval
= span
->z
;
470 GLuint
*z
= span
->array
->z
;
471 for (i
= 0; i
< n
; i
++) {
476 span
->interpMask
&= ~SPAN_Z
;
477 span
->arrayMask
|= SPAN_Z
;
482 * This the ideal solution, as given in the OpenGL spec.
486 compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
487 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
488 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
490 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
491 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
492 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
493 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
494 GLfloat x
= SQRTF(dudx
* dudx
+ dvdx
* dvdx
);
495 GLfloat y
= SQRTF(dudy
* dudy
+ dvdy
* dvdy
);
496 GLfloat rho
= MAX2(x
, y
);
497 GLfloat lambda
= LOG2(rho
);
504 * This is a faster approximation
507 _swrast_compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
508 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
509 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
511 GLfloat dsdx2
= (s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
;
512 GLfloat dtdx2
= (t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
;
513 GLfloat dsdy2
= (s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
;
514 GLfloat dtdy2
= (t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
;
515 GLfloat maxU
, maxV
, rho
, lambda
;
516 dsdx2
= FABSF(dsdx2
);
517 dsdy2
= FABSF(dsdy2
);
518 dtdx2
= FABSF(dtdx2
);
519 dtdy2
= FABSF(dtdy2
);
520 maxU
= MAX2(dsdx2
, dsdy2
) * texW
;
521 maxV
= MAX2(dtdx2
, dtdy2
) * texH
;
522 rho
= MAX2(maxU
, maxV
);
529 * Fill in the span.texcoords array from the interpolation values.
530 * Note: in the places where we divide by Q (or mult by invQ) we're
531 * really doing two things: perspective correction and texcoord
532 * projection. Remember, for texcoord (s,t,r,q) we need to index
533 * texels with (s/q, t/q, r/q).
534 * If we're using a fragment program, we never do the division
535 * for texcoord projection. That's done by the TXP instruction
536 * or user-written code.
539 interpolate_texcoords(GLcontext
*ctx
, SWspan
*span
)
541 ASSERT(span
->interpMask
& SPAN_TEXTURE
);
542 ASSERT(!(span
->arrayMask
& SPAN_TEXTURE
));
544 if (ctx
->Texture
._EnabledCoordUnits
> 1) {
547 span
->arrayMask
|= SPAN_TEXTURE
;
548 /* XXX CoordUnits vs. ImageUnits */
549 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
550 if (ctx
->Texture
._EnabledCoordUnits
& (1 << u
)) {
551 const struct gl_texture_object
*obj
=ctx
->Texture
.Unit
[u
]._Current
;
553 GLboolean needLambda
;
555 const struct gl_texture_image
*img
= obj
->Image
[0][obj
->BaseLevel
];
556 needLambda
= (obj
->MinFilter
!= obj
->MagFilter
)
557 || ctx
->FragmentProgram
._Current
;
558 texW
= img
->WidthScale
;
559 texH
= img
->HeightScale
;
562 /* using a fragment program */
565 needLambda
= GL_FALSE
;
568 GLfloat (*texcoord
)[4] = span
->array
->texcoords
[u
];
569 GLfloat
*lambda
= span
->array
->lambda
[u
];
570 const GLfloat dsdx
= span
->texStepX
[u
][0];
571 const GLfloat dsdy
= span
->texStepY
[u
][0];
572 const GLfloat dtdx
= span
->texStepX
[u
][1];
573 const GLfloat dtdy
= span
->texStepY
[u
][1];
574 const GLfloat drdx
= span
->texStepX
[u
][2];
575 const GLfloat dqdx
= span
->texStepX
[u
][3];
576 const GLfloat dqdy
= span
->texStepY
[u
][3];
577 GLfloat s
= span
->tex
[u
][0];
578 GLfloat t
= span
->tex
[u
][1];
579 GLfloat r
= span
->tex
[u
][2];
580 GLfloat q
= span
->tex
[u
][3];
582 if (ctx
->FragmentProgram
._Current
583 || ctx
->ATIFragmentShader
._Enabled
) {
584 /* do perspective correction but don't divide s, t, r by q */
585 const GLfloat dwdx
= span
->dwdx
;
587 for (i
= 0; i
< span
->end
; i
++) {
588 const GLfloat invW
= 1.0F
/ w
;
589 texcoord
[i
][0] = s
* invW
;
590 texcoord
[i
][1] = t
* invW
;
591 texcoord
[i
][2] = r
* invW
;
592 texcoord
[i
][3] = q
* invW
;
593 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
594 dqdx
, dqdy
, texW
, texH
,
605 for (i
= 0; i
< span
->end
; i
++) {
606 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
607 texcoord
[i
][0] = s
* invQ
;
608 texcoord
[i
][1] = t
* invQ
;
609 texcoord
[i
][2] = r
* invQ
;
611 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
612 dqdx
, dqdy
, texW
, texH
,
620 span
->arrayMask
|= SPAN_LAMBDA
;
623 GLfloat (*texcoord
)[4] = span
->array
->texcoords
[u
];
624 GLfloat
*lambda
= span
->array
->lambda
[u
];
625 const GLfloat dsdx
= span
->texStepX
[u
][0];
626 const GLfloat dtdx
= span
->texStepX
[u
][1];
627 const GLfloat drdx
= span
->texStepX
[u
][2];
628 const GLfloat dqdx
= span
->texStepX
[u
][3];
629 GLfloat s
= span
->tex
[u
][0];
630 GLfloat t
= span
->tex
[u
][1];
631 GLfloat r
= span
->tex
[u
][2];
632 GLfloat q
= span
->tex
[u
][3];
634 if (ctx
->FragmentProgram
._Current
||
635 ctx
->ATIFragmentShader
._Enabled
) {
636 /* do perspective correction but don't divide s, t, r by q */
637 const GLfloat dwdx
= span
->dwdx
;
639 for (i
= 0; i
< span
->end
; i
++) {
640 const GLfloat invW
= 1.0F
/ w
;
641 texcoord
[i
][0] = s
* invW
;
642 texcoord
[i
][1] = t
* invW
;
643 texcoord
[i
][2] = r
* invW
;
644 texcoord
[i
][3] = q
* invW
;
653 else if (dqdx
== 0.0F
) {
654 /* Ortho projection or polygon's parallel to window X axis */
655 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
656 for (i
= 0; i
< span
->end
; i
++) {
657 texcoord
[i
][0] = s
* invQ
;
658 texcoord
[i
][1] = t
* invQ
;
659 texcoord
[i
][2] = r
* invQ
;
668 for (i
= 0; i
< span
->end
; i
++) {
669 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
670 texcoord
[i
][0] = s
* invQ
;
671 texcoord
[i
][1] = t
* invQ
;
672 texcoord
[i
][2] = r
* invQ
;
687 const struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[0]._Current
;
689 GLboolean needLambda
;
691 const struct gl_texture_image
*img
= obj
->Image
[0][obj
->BaseLevel
];
692 needLambda
= (obj
->MinFilter
!= obj
->MagFilter
)
693 || ctx
->FragmentProgram
._Current
;
694 texW
= (GLfloat
) img
->WidthScale
;
695 texH
= (GLfloat
) img
->HeightScale
;
698 needLambda
= GL_FALSE
;
701 span
->arrayMask
|= SPAN_TEXTURE
;
703 /* just texture unit 0, with lambda */
704 GLfloat (*texcoord
)[4] = span
->array
->texcoords
[0];
705 GLfloat
*lambda
= span
->array
->lambda
[0];
706 const GLfloat dsdx
= span
->texStepX
[0][0];
707 const GLfloat dsdy
= span
->texStepY
[0][0];
708 const GLfloat dtdx
= span
->texStepX
[0][1];
709 const GLfloat dtdy
= span
->texStepY
[0][1];
710 const GLfloat drdx
= span
->texStepX
[0][2];
711 const GLfloat dqdx
= span
->texStepX
[0][3];
712 const GLfloat dqdy
= span
->texStepY
[0][3];
713 GLfloat s
= span
->tex
[0][0];
714 GLfloat t
= span
->tex
[0][1];
715 GLfloat r
= span
->tex
[0][2];
716 GLfloat q
= span
->tex
[0][3];
718 if (ctx
->FragmentProgram
._Current
719 || ctx
->ATIFragmentShader
._Enabled
) {
720 /* do perspective correction but don't divide s, t, r by q */
721 const GLfloat dwdx
= span
->dwdx
;
723 for (i
= 0; i
< span
->end
; i
++) {
724 const GLfloat invW
= 1.0F
/ w
;
725 texcoord
[i
][0] = s
* invW
;
726 texcoord
[i
][1] = t
* invW
;
727 texcoord
[i
][2] = r
* invW
;
728 texcoord
[i
][3] = q
* invW
;
729 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
730 dqdx
, dqdy
, texW
, texH
,
741 for (i
= 0; i
< span
->end
; i
++) {
742 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
743 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
744 dqdx
, dqdy
, texW
, texH
,
746 texcoord
[i
][0] = s
* invQ
;
747 texcoord
[i
][1] = t
* invQ
;
748 texcoord
[i
][2] = r
* invQ
;
756 span
->arrayMask
|= SPAN_LAMBDA
;
759 /* just texture 0, without lambda */
760 GLfloat (*texcoord
)[4] = span
->array
->texcoords
[0];
761 const GLfloat dsdx
= span
->texStepX
[0][0];
762 const GLfloat dtdx
= span
->texStepX
[0][1];
763 const GLfloat drdx
= span
->texStepX
[0][2];
764 const GLfloat dqdx
= span
->texStepX
[0][3];
765 GLfloat s
= span
->tex
[0][0];
766 GLfloat t
= span
->tex
[0][1];
767 GLfloat r
= span
->tex
[0][2];
768 GLfloat q
= span
->tex
[0][3];
770 if (ctx
->FragmentProgram
._Current
771 || ctx
->ATIFragmentShader
._Enabled
) {
772 /* do perspective correction but don't divide s, t, r by q */
773 const GLfloat dwdx
= span
->dwdx
;
775 for (i
= 0; i
< span
->end
; i
++) {
776 const GLfloat invW
= 1.0F
/ w
;
777 texcoord
[i
][0] = s
* invW
;
778 texcoord
[i
][1] = t
* invW
;
779 texcoord
[i
][2] = r
* invW
;
780 texcoord
[i
][3] = q
* invW
;
788 else if (dqdx
== 0.0F
) {
789 /* Ortho projection or polygon's parallel to window X axis */
790 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
791 for (i
= 0; i
< span
->end
; i
++) {
792 texcoord
[i
][0] = s
* invQ
;
793 texcoord
[i
][1] = t
* invQ
;
794 texcoord
[i
][2] = r
* invQ
;
802 for (i
= 0; i
< span
->end
; i
++) {
803 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
804 texcoord
[i
][0] = s
* invQ
;
805 texcoord
[i
][1] = t
* invQ
;
806 texcoord
[i
][2] = r
* invQ
;
820 * Fill in the span.varying array from the interpolation values.
823 interpolate_varying(GLcontext
*ctx
, SWspan
*span
)
827 ASSERT(span
->interpMask
& SPAN_VARYING
);
828 ASSERT(!(span
->arrayMask
& SPAN_VARYING
));
830 span
->arrayMask
|= SPAN_VARYING
;
832 for (i
= 0; i
< MAX_VARYING_VECTORS
; i
++) {
833 for (j
= 0; j
< VARYINGS_PER_VECTOR
; j
++) {
834 const GLfloat dvdx
= span
->varStepX
[i
][j
];
835 GLfloat v
= span
->var
[i
][j
];
836 const GLfloat dwdx
= span
->dwdx
;
840 for (k
= 0; k
< span
->end
; k
++) {
841 GLfloat invW
= 1.0f
/ w
;
842 span
->array
->varying
[k
][i
][j
] = v
* invW
;
852 * Apply the current polygon stipple pattern to a span of pixels.
855 stipple_polygon_span( GLcontext
*ctx
, SWspan
*span
)
857 const GLuint highbit
= 0x80000000;
858 const GLuint stipple
= ctx
->PolygonStipple
[span
->y
% 32];
859 GLubyte
*mask
= span
->array
->mask
;
862 ASSERT(ctx
->Polygon
.StippleFlag
);
863 ASSERT((span
->arrayMask
& SPAN_XY
) == 0);
865 m
= highbit
>> (GLuint
) (span
->x
% 32);
867 for (i
= 0; i
< span
->end
; i
++) {
868 if ((m
& stipple
) == 0) {
876 span
->writeAll
= GL_FALSE
;
881 * Clip a pixel span to the current buffer/window boundaries:
882 * DrawBuffer->_Xmin, _Xmax, _Ymin, _Ymax. This will accomplish
883 * window clipping and scissoring.
884 * Return: GL_TRUE some pixels still visible
885 * GL_FALSE nothing visible
888 clip_span( GLcontext
*ctx
, SWspan
*span
)
890 const GLint xmin
= ctx
->DrawBuffer
->_Xmin
;
891 const GLint xmax
= ctx
->DrawBuffer
->_Xmax
;
892 const GLint ymin
= ctx
->DrawBuffer
->_Ymin
;
893 const GLint ymax
= ctx
->DrawBuffer
->_Ymax
;
895 if (span
->arrayMask
& SPAN_XY
) {
896 /* arrays of x/y pixel coords */
897 const GLint
*x
= span
->array
->x
;
898 const GLint
*y
= span
->array
->y
;
899 const GLint n
= span
->end
;
900 GLubyte
*mask
= span
->array
->mask
;
902 if (span
->arrayMask
& SPAN_MASK
) {
903 /* note: using & intead of && to reduce branches */
904 for (i
= 0; i
< n
; i
++) {
905 mask
[i
] &= (x
[i
] >= xmin
) & (x
[i
] < xmax
)
906 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
910 /* note: using & intead of && to reduce branches */
911 for (i
= 0; i
< n
; i
++) {
912 mask
[i
] = (x
[i
] >= xmin
) & (x
[i
] < xmax
)
913 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
916 return GL_TRUE
; /* some pixels visible */
919 /* horizontal span of pixels */
920 const GLint x
= span
->x
;
921 const GLint y
= span
->y
;
922 const GLint n
= span
->end
;
924 /* Trivial rejection tests */
925 if (y
< ymin
|| y
>= ymax
|| x
+ n
<= xmin
|| x
>= xmax
) {
927 return GL_FALSE
; /* all pixels clipped */
930 /* Clip to the left */
932 ASSERT(x
+ n
> xmin
);
933 span
->writeAll
= GL_FALSE
;
934 _mesa_bzero(span
->array
->mask
, (xmin
- x
) * sizeof(GLubyte
));
940 span
->end
= xmax
- x
;
943 return GL_TRUE
; /* some pixels visible */
949 * Apply all the per-fragment opertions to a span of color index fragments
950 * and write them to the enabled color drawbuffers.
951 * The 'span' parameter can be considered to be const. Note that
952 * span->interpMask and span->arrayMask may be changed but will be restored
953 * to their original values before returning.
956 _swrast_write_index_span( GLcontext
*ctx
, SWspan
*span
)
958 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
959 const GLbitfield origInterpMask
= span
->interpMask
;
960 const GLbitfield origArrayMask
= span
->arrayMask
;
962 ASSERT(span
->end
<= MAX_WIDTH
);
963 ASSERT(span
->primitive
== GL_POINT
|| span
->primitive
== GL_LINE
||
964 span
->primitive
== GL_POLYGON
|| span
->primitive
== GL_BITMAP
);
965 ASSERT((span
->interpMask
| span
->arrayMask
) & SPAN_INDEX
);
966 ASSERT((span
->interpMask
& span
->arrayMask
) == 0);
968 if (span
->arrayMask
& SPAN_MASK
) {
969 /* mask was initialized by caller, probably glBitmap */
970 span
->writeAll
= GL_FALSE
;
973 _mesa_memset(span
->array
->mask
, 1, span
->end
);
974 span
->writeAll
= GL_TRUE
;
978 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
979 if (!clip_span(ctx
, span
)) {
984 /* Depth bounds test */
985 if (ctx
->Depth
.BoundsTest
&& ctx
->DrawBuffer
->Visual
.depthBits
> 0) {
986 if (!_swrast_depth_bounds_test(ctx
, span
)) {
992 /* Make sure all fragments are within window bounds */
993 if (span
->arrayMask
& SPAN_XY
) {
995 for (i
= 0; i
< span
->end
; i
++) {
996 if (span
->array
->mask
[i
]) {
997 assert(span
->array
->x
[i
] >= ctx
->DrawBuffer
->_Xmin
);
998 assert(span
->array
->x
[i
] < ctx
->DrawBuffer
->_Xmax
);
999 assert(span
->array
->y
[i
] >= ctx
->DrawBuffer
->_Ymin
);
1000 assert(span
->array
->y
[i
] < ctx
->DrawBuffer
->_Ymax
);
1006 /* Polygon Stippling */
1007 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
1008 stipple_polygon_span(ctx
, span
);
1011 /* Stencil and Z testing */
1012 if (ctx
->Depth
.Test
|| ctx
->Stencil
.Enabled
) {
1013 if (span
->interpMask
& SPAN_Z
)
1014 _swrast_span_interpolate_z(ctx
, span
);
1016 if (ctx
->Stencil
.Enabled
) {
1017 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
1018 span
->arrayMask
= origArrayMask
;
1023 ASSERT(ctx
->Depth
.Test
);
1024 if (!_swrast_depth_test_span(ctx
, span
)) {
1025 span
->interpMask
= origInterpMask
;
1026 span
->arrayMask
= origArrayMask
;
1032 #if FEATURE_ARB_occlusion_query
1033 if (ctx
->Query
.CurrentOcclusionObject
) {
1034 /* update count of 'passed' fragments */
1035 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
1037 for (i
= 0; i
< span
->end
; i
++)
1038 q
->Result
+= span
->array
->mask
[i
];
1042 /* we have to wait until after occlusion to do this test */
1043 if (ctx
->Color
.DrawBuffer
== GL_NONE
|| ctx
->Color
.IndexMask
== 0) {
1044 /* write no pixels */
1045 span
->arrayMask
= origArrayMask
;
1049 /* Interpolate the color indexes if needed */
1050 if (swrast
->_FogEnabled
||
1051 ctx
->Color
.IndexLogicOpEnabled
||
1052 ctx
->Color
.IndexMask
!= 0xffffffff ||
1053 (span
->arrayMask
& SPAN_COVERAGE
)) {
1054 if (span
->interpMask
& SPAN_INDEX
) {
1055 interpolate_indexes(ctx
, span
);
1060 if (swrast
->_FogEnabled
) {
1061 _swrast_fog_ci_span(ctx
, span
);
1064 /* Antialias coverage application */
1065 if (span
->arrayMask
& SPAN_COVERAGE
) {
1066 const GLfloat
*coverage
= span
->array
->coverage
;
1067 GLuint
*index
= span
->array
->index
;
1069 for (i
= 0; i
< span
->end
; i
++) {
1070 ASSERT(coverage
[i
] < 16);
1071 index
[i
] = (index
[i
] & ~0xf) | ((GLuint
) coverage
[i
]);
1076 * Write to renderbuffers
1079 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1080 const GLuint output
= 0; /* only frag progs can write to other outputs */
1081 const GLuint numDrawBuffers
= fb
->_NumColorDrawBuffers
[output
];
1082 GLuint indexSave
[MAX_WIDTH
];
1085 if (numDrawBuffers
> 1) {
1086 /* save indexes for second, third renderbuffer writes */
1087 _mesa_memcpy(indexSave
, span
->array
->index
,
1088 span
->end
* sizeof(indexSave
[0]));
1091 for (buf
= 0; buf
< fb
->_NumColorDrawBuffers
[output
]; buf
++) {
1092 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[output
][buf
];
1093 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
1095 if (ctx
->Color
.IndexLogicOpEnabled
) {
1096 _swrast_logicop_ci_span(ctx
, rb
, span
);
1099 if (ctx
->Color
.IndexMask
!= 0xffffffff) {
1100 _swrast_mask_ci_span(ctx
, rb
, span
);
1103 if ((span
->interpMask
& SPAN_INDEX
) && span
->indexStep
== 0) {
1104 /* all fragments have same color index */
1110 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1111 index8
= FixedToInt(span
->index
);
1114 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1115 index16
= FixedToInt(span
->index
);
1119 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
1120 index32
= FixedToInt(span
->index
);
1124 if (span
->arrayMask
& SPAN_XY
) {
1125 rb
->PutMonoValues(ctx
, rb
, span
->end
, span
->array
->x
,
1126 span
->array
->y
, value
, span
->array
->mask
);
1129 rb
->PutMonoRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1130 value
, span
->array
->mask
);
1134 /* each fragment is a different color */
1135 GLubyte index8
[MAX_WIDTH
];
1136 GLushort index16
[MAX_WIDTH
];
1139 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1141 for (k
= 0; k
< span
->end
; k
++) {
1142 index8
[k
] = (GLubyte
) span
->array
->index
[k
];
1146 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1148 for (k
= 0; k
< span
->end
; k
++) {
1149 index16
[k
] = (GLushort
) span
->array
->index
[k
];
1154 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
1155 values
= span
->array
->index
;
1158 if (span
->arrayMask
& SPAN_XY
) {
1159 rb
->PutValues(ctx
, rb
, span
->end
,
1160 span
->array
->x
, span
->array
->y
,
1161 values
, span
->array
->mask
);
1164 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1165 values
, span
->array
->mask
);
1169 if (buf
+ 1 < numDrawBuffers
) {
1170 /* restore original span values */
1171 _mesa_memcpy(span
->array
->index
, indexSave
,
1172 span
->end
* sizeof(indexSave
[0]));
1177 span
->interpMask
= origInterpMask
;
1178 span
->arrayMask
= origArrayMask
;
1183 * Add specular color to base color. This is used only when
1184 * GL_LIGHT_MODEL_COLOR_CONTROL = GL_SEPARATE_SPECULAR_COLOR.
1187 add_specular(GLcontext
*ctx
, SWspan
*span
)
1189 switch (span
->array
->ChanType
) {
1190 case GL_UNSIGNED_BYTE
:
1192 GLubyte (*rgba
)[4] = span
->array
->color
.sz1
.rgba
;
1193 GLubyte (*spec
)[4] = span
->array
->color
.sz1
.spec
;
1195 for (i
= 0; i
< span
->end
; i
++) {
1196 GLint r
= rgba
[i
][RCOMP
] + spec
[i
][RCOMP
];
1197 GLint g
= rgba
[i
][GCOMP
] + spec
[i
][GCOMP
];
1198 GLint b
= rgba
[i
][BCOMP
] + spec
[i
][BCOMP
];
1199 GLint a
= rgba
[i
][ACOMP
] + spec
[i
][ACOMP
];
1200 rgba
[i
][RCOMP
] = MIN2(r
, 255);
1201 rgba
[i
][GCOMP
] = MIN2(g
, 255);
1202 rgba
[i
][BCOMP
] = MIN2(b
, 255);
1203 rgba
[i
][ACOMP
] = MIN2(a
, 255);
1207 case GL_UNSIGNED_SHORT
:
1209 GLushort (*rgba
)[4] = span
->array
->color
.sz2
.rgba
;
1210 GLushort (*spec
)[4] = span
->array
->color
.sz2
.spec
;
1212 for (i
= 0; i
< span
->end
; i
++) {
1213 GLint r
= rgba
[i
][RCOMP
] + spec
[i
][RCOMP
];
1214 GLint g
= rgba
[i
][GCOMP
] + spec
[i
][GCOMP
];
1215 GLint b
= rgba
[i
][BCOMP
] + spec
[i
][BCOMP
];
1216 GLint a
= rgba
[i
][ACOMP
] + spec
[i
][ACOMP
];
1217 rgba
[i
][RCOMP
] = MIN2(r
, 65535);
1218 rgba
[i
][GCOMP
] = MIN2(g
, 65535);
1219 rgba
[i
][BCOMP
] = MIN2(b
, 65535);
1220 rgba
[i
][ACOMP
] = MIN2(a
, 65535);
1226 GLfloat (*rgba
)[4] = span
->array
->color
.sz4
.rgba
;
1227 GLfloat (*spec
)[4] = span
->array
->color
.sz4
.spec
;
1229 for (i
= 0; i
< span
->end
; i
++) {
1230 rgba
[i
][RCOMP
] += spec
[i
][RCOMP
];
1231 rgba
[i
][GCOMP
] += spec
[i
][GCOMP
];
1232 rgba
[i
][BCOMP
] += spec
[i
][BCOMP
];
1233 rgba
[i
][ACOMP
] += spec
[i
][ACOMP
];
1238 _mesa_problem(ctx
, "Invalid datatype in add_specular");
1244 * Apply antialiasing coverage value to alpha values.
1247 apply_aa_coverage(SWspan
*span
)
1249 const GLfloat
*coverage
= span
->array
->coverage
;
1251 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1252 GLubyte (*rgba
)[4] = span
->array
->color
.sz1
.rgba
;
1253 for (i
= 0; i
< span
->end
; i
++) {
1254 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
1255 rgba
[i
][ACOMP
] = (GLubyte
) CLAMP(a
, 0.0, 255.0);
1256 ASSERT(coverage
[i
] >= 0.0);
1257 ASSERT(coverage
[i
] <= 1.0);
1260 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
1261 GLushort (*rgba
)[4] = span
->array
->color
.sz2
.rgba
;
1262 for (i
= 0; i
< span
->end
; i
++) {
1263 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
1264 rgba
[i
][ACOMP
] = (GLushort
) CLAMP(a
, 0.0, 65535.0);
1268 GLfloat (*rgba
)[4] = span
->array
->color
.sz4
.rgba
;
1269 for (i
= 0; i
< span
->end
; i
++) {
1270 rgba
[i
][ACOMP
] = rgba
[i
][ACOMP
] * coverage
[i
];
1277 * Clamp span's float colors to [0,1]
1280 clamp_colors(SWspan
*span
)
1282 GLfloat (*rgba
)[4] = span
->array
->color
.sz4
.rgba
;
1284 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
1285 for (i
= 0; i
< span
->end
; i
++) {
1286 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0F
, 1.0F
);
1287 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0F
, 1.0F
);
1288 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0F
, 1.0F
);
1289 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0F
, 1.0F
);
1295 * Convert the span's color arrays to the given type.
1298 convert_color_type(GLcontext
*ctx
, SWspan
*span
, GLenum newType
)
1301 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1302 src
= span
->array
->color
.sz1
.rgba
;
1304 else if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1305 src
= span
->array
->color
.sz2
.rgba
;
1308 src
= span
->array
->color
.sz4
.rgba
;
1310 if (newType
== GL_UNSIGNED_BYTE
) {
1311 dst
= span
->array
->color
.sz1
.rgba
;
1313 else if (newType
== GL_UNSIGNED_BYTE
) {
1314 dst
= span
->array
->color
.sz2
.rgba
;
1317 dst
= span
->array
->color
.sz4
.rgba
;
1320 _mesa_convert_colors(span
->array
->ChanType
, src
,
1322 span
->end
, span
->array
->mask
);
1324 span
->array
->ChanType
= newType
;
1330 * Apply fragment shader, fragment program or normal texturing to span.
1333 shade_texture_span(GLcontext
*ctx
, SWspan
*span
)
1335 /* Now we need the rgba array, fill it in if needed */
1336 if (span
->interpMask
& SPAN_RGBA
)
1337 interpolate_colors(span
);
1339 if (ctx
->Texture
._EnabledCoordUnits
&& (span
->interpMask
& SPAN_TEXTURE
))
1340 interpolate_texcoords(ctx
, span
);
1342 if (ctx
->FragmentProgram
._Current
||
1343 ctx
->ATIFragmentShader
._Enabled
) {
1345 /* use float colors if running a fragment program or shader */
1346 const GLenum oldType
= span
->array
->ChanType
;
1347 const GLenum newType
= GL_FLOAT
;
1348 if (oldType
!= newType
) {
1349 GLvoid
*src
= (oldType
== GL_UNSIGNED_BYTE
)
1350 ? (GLvoid
*) span
->array
->color
.sz1
.rgba
1351 : (GLvoid
*) span
->array
->color
.sz2
.rgba
;
1352 _mesa_convert_colors(oldType
, src
,
1353 newType
, span
->array
->color
.sz4
.rgba
,
1354 span
->end
, span
->array
->mask
);
1355 span
->array
->ChanType
= newType
;
1358 /* fragment programs/shaders may need specular, fog and Z coords */
1359 if (span
->interpMask
& SPAN_SPEC
)
1360 interpolate_specular(span
);
1362 if (span
->interpMask
& SPAN_FOG
)
1363 interpolate_fog(ctx
, span
);
1365 if (span
->interpMask
& SPAN_Z
)
1366 _swrast_span_interpolate_z (ctx
, span
);
1368 if (ctx
->ShaderObjects
.Linked
&& span
->interpMask
& SPAN_VARYING
)
1369 interpolate_varying(ctx
, span
);
1371 /* Run fragment program/shader now */
1372 if (ctx
->FragmentProgram
._Current
) {
1373 _swrast_exec_fragment_program(ctx
, span
);
1376 ASSERT(ctx
->ATIFragmentShader
._Enabled
);
1377 _swrast_exec_fragment_shader(ctx
, span
);
1380 else if (ctx
->Texture
._EnabledUnits
&& (span
->arrayMask
& SPAN_TEXTURE
)) {
1381 /* conventional texturing */
1382 _swrast_texture_span(ctx
, span
);
1389 * Apply all the per-fragment operations to a span.
1390 * This now includes texturing (_swrast_write_texture_span() is history).
1391 * This function may modify any of the array values in the span.
1392 * span->interpMask and span->arrayMask may be changed but will be restored
1393 * to their original values before returning.
1396 _swrast_write_rgba_span( GLcontext
*ctx
, SWspan
*span
)
1398 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1399 const GLuint colorMask
= *((GLuint
*) ctx
->Color
.ColorMask
);
1400 const GLbitfield origInterpMask
= span
->interpMask
;
1401 const GLbitfield origArrayMask
= span
->arrayMask
;
1402 const GLenum chanType
= span
->array
->ChanType
;
1403 const GLboolean shader
= (ctx
->FragmentProgram
._Current
1404 || ctx
->ATIFragmentShader
._Enabled
);
1405 const GLboolean shaderOrTexture
= shader
|| ctx
->Texture
._EnabledUnits
;
1406 GLboolean deferredTexture
;
1409 printf("%s() interp 0x%x array 0x%x\n", __FUNCTION__,
1410 span->interpMask, span->arrayMask);
1413 ASSERT(span
->primitive
== GL_POINT
||
1414 span
->primitive
== GL_LINE
||
1415 span
->primitive
== GL_POLYGON
||
1416 span
->primitive
== GL_BITMAP
);
1417 ASSERT(span
->end
<= MAX_WIDTH
);
1418 ASSERT((span
->interpMask
& span
->arrayMask
) == 0);
1419 ASSERT((span
->interpMask
& SPAN_RGBA
) ^ (span
->arrayMask
& SPAN_RGBA
));
1421 /* check for conditions that prevent deferred shading */
1422 if (ctx
->Color
.AlphaEnabled
) {
1423 /* alpha test depends on post-texture/shader colors */
1424 deferredTexture
= GL_FALSE
;
1426 else if (shaderOrTexture
) {
1427 if (ctx
->FragmentProgram
._Current
) {
1428 if (ctx
->FragmentProgram
.Current
->Base
.OutputsWritten
1429 & (1 << FRAG_RESULT_DEPR
)) {
1430 /* Z comes from fragment program/shader */
1431 deferredTexture
= GL_FALSE
;
1434 deferredTexture
= GL_TRUE
;
1438 /* ATI frag shader or conventional texturing */
1439 deferredTexture
= GL_TRUE
;
1443 /* no texturing or shadering */
1444 deferredTexture
= GL_FALSE
;
1447 /* Fragment write masks */
1448 if (span
->arrayMask
& SPAN_MASK
) {
1449 /* mask was initialized by caller, probably glBitmap */
1450 span
->writeAll
= GL_FALSE
;
1453 _mesa_memset(span
->array
->mask
, 1, span
->end
);
1454 span
->writeAll
= GL_TRUE
;
1457 /* Clip to window/scissor box */
1458 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
1459 if (!clip_span(ctx
, span
)) {
1465 /* Make sure all fragments are within window bounds */
1466 if (span
->arrayMask
& SPAN_XY
) {
1468 for (i
= 0; i
< span
->end
; i
++) {
1469 if (span
->array
->mask
[i
]) {
1470 assert(span
->array
->x
[i
] >= ctx
->DrawBuffer
->_Xmin
);
1471 assert(span
->array
->x
[i
] < ctx
->DrawBuffer
->_Xmax
);
1472 assert(span
->array
->y
[i
] >= ctx
->DrawBuffer
->_Ymin
);
1473 assert(span
->array
->y
[i
] < ctx
->DrawBuffer
->_Ymax
);
1479 /* Polygon Stippling */
1480 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
1481 stipple_polygon_span(ctx
, span
);
1484 /* This is the normal place to compute the resulting fragment color/Z.
1485 * As an optimization, we try to defer this until after Z/stencil
1486 * testing in order to try to avoid computing colors that we won't
1489 if (shaderOrTexture
&& !deferredTexture
) {
1490 shade_texture_span(ctx
, span
);
1493 /* Do the alpha test */
1494 if (ctx
->Color
.AlphaEnabled
) {
1495 if (!_swrast_alpha_test(ctx
, span
)) {
1500 /* Stencil and Z testing */
1501 if (ctx
->Stencil
.Enabled
|| ctx
->Depth
.Test
) {
1502 if (span
->interpMask
& SPAN_Z
)
1503 _swrast_span_interpolate_z(ctx
, span
);
1505 if (ctx
->Stencil
.Enabled
&& ctx
->DrawBuffer
->Visual
.stencilBits
> 0) {
1506 /* Combined Z/stencil tests */
1507 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
1511 else if (ctx
->DrawBuffer
->Visual
.depthBits
> 0) {
1512 /* Just regular depth testing */
1513 ASSERT(ctx
->Depth
.Test
);
1514 ASSERT(span
->arrayMask
& SPAN_Z
);
1515 if (!_swrast_depth_test_span(ctx
, span
)) {
1521 #if FEATURE_ARB_occlusion_query
1522 if (ctx
->Query
.CurrentOcclusionObject
) {
1523 /* update count of 'passed' fragments */
1524 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
1526 for (i
= 0; i
< span
->end
; i
++)
1527 q
->Result
+= span
->array
->mask
[i
];
1531 /* We had to wait until now to check for glColorMask(0,0,0,0) because of
1532 * the occlusion test.
1534 if (colorMask
== 0x0) {
1538 /* If we were able to defer fragment color computation to now, there's
1539 * a good chance that many fragments will have already been killed by
1540 * Z/stencil testing.
1542 if (deferredTexture
) {
1543 ASSERT(shaderOrTexture
);
1544 shade_texture_span(ctx
, span
);
1547 if ((span
->arrayMask
& SPAN_RGBA
) == 0) {
1548 interpolate_colors(span
);
1551 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1554 /* Add base and specular colors */
1555 if (ctx
->Fog
.ColorSumEnabled
||
1556 (ctx
->Light
.Enabled
&&
1557 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)) {
1558 if (span
->interpMask
& SPAN_SPEC
) {
1559 interpolate_specular(span
);
1561 if (span
->arrayMask
& SPAN_SPEC
) {
1562 add_specular(ctx
, span
);
1565 /* We probably added the base/specular colors during the
1573 if (swrast
->_FogEnabled
) {
1574 _swrast_fog_rgba_span(ctx
, span
);
1577 /* Antialias coverage application */
1578 if (span
->arrayMask
& SPAN_COVERAGE
) {
1579 apply_aa_coverage(span
);
1582 /* Clamp color/alpha values over the range [0.0, 1.0] before storage */
1583 if (ctx
->Color
.ClampFragmentColor
== GL_TRUE
&&
1584 span
->array
->ChanType
== GL_FLOAT
) {
1589 * Write to renderbuffers
1592 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1593 const GLuint output
= 0; /* only frag progs can write to other outputs */
1594 const GLuint numDrawBuffers
= fb
->_NumColorDrawBuffers
[output
];
1595 GLchan rgbaSave
[MAX_WIDTH
][4];
1598 if (numDrawBuffers
> 0) {
1599 if (fb
->_ColorDrawBuffers
[output
][0]->DataType
1600 != span
->array
->ChanType
) {
1601 convert_color_type(ctx
, span
,
1602 fb
->_ColorDrawBuffers
[output
][0]->DataType
);
1606 if (numDrawBuffers
> 1) {
1607 /* save colors for second, third renderbuffer writes */
1608 _mesa_memcpy(rgbaSave
, span
->array
->rgba
,
1609 4 * span
->end
* sizeof(GLchan
));
1612 for (buf
= 0; buf
< numDrawBuffers
; buf
++) {
1613 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[output
][buf
];
1614 ASSERT(rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
);
1616 if (ctx
->Color
._LogicOpEnabled
) {
1617 _swrast_logicop_rgba_span(ctx
, rb
, span
);
1619 else if (ctx
->Color
.BlendEnabled
) {
1620 _swrast_blend_span(ctx
, rb
, span
);
1623 if (colorMask
!= 0xffffffff) {
1624 _swrast_mask_rgba_span(ctx
, rb
, span
);
1627 if (span
->arrayMask
& SPAN_XY
) {
1628 /* array of pixel coords */
1629 ASSERT(rb
->PutValues
);
1630 rb
->PutValues(ctx
, rb
, span
->end
,
1631 span
->array
->x
, span
->array
->y
,
1632 span
->array
->rgba
, span
->array
->mask
);
1635 /* horizontal run of pixels */
1637 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
, span
->array
->rgba
,
1638 span
->writeAll
? NULL
: span
->array
->mask
);
1641 if (buf
+ 1 < numDrawBuffers
) {
1642 /* restore original span values */
1643 _mesa_memcpy(span
->array
->rgba
, rgbaSave
,
1644 4 * span
->end
* sizeof(GLchan
));
1651 /* restore these values before returning */
1652 span
->interpMask
= origInterpMask
;
1653 span
->arrayMask
= origArrayMask
;
1654 span
->array
->ChanType
= chanType
;
1659 * Read RGBA pixels from frame buffer. Clipping will be done to prevent
1660 * reading ouside the buffer's boundaries.
1661 * \param type datatype for returned colors
1662 * \param rgba the returned colors
1665 _swrast_read_rgba_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1666 GLuint n
, GLint x
, GLint y
, GLenum dstType
,
1669 const GLint bufWidth
= (GLint
) rb
->Width
;
1670 const GLint bufHeight
= (GLint
) rb
->Height
;
1672 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1673 /* completely above, below, or right */
1674 /* XXX maybe leave rgba values undefined? */
1675 _mesa_bzero(rgba
, 4 * n
* sizeof(GLchan
));
1680 /* left edge clipping */
1682 length
= (GLint
) n
- skip
;
1684 /* completely left of window */
1687 if (length
> bufWidth
) {
1691 else if ((GLint
) (x
+ n
) > bufWidth
) {
1692 /* right edge clipping */
1694 length
= bufWidth
- x
;
1696 /* completely to right of window */
1708 ASSERT(rb
->_BaseFormat
== GL_RGB
|| rb
->_BaseFormat
== GL_RGBA
);
1710 if (rb
->DataType
== dstType
) {
1711 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
,
1712 (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(rb
->DataType
));
1715 GLuint temp
[MAX_WIDTH
* 4];
1716 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, temp
);
1717 _mesa_convert_colors(rb
->DataType
, temp
,
1718 dstType
, (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(dstType
),
1726 * Read CI pixels from frame buffer. Clipping will be done to prevent
1727 * reading ouside the buffer's boundaries.
1730 _swrast_read_index_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1731 GLuint n
, GLint x
, GLint y
, GLuint index
[] )
1733 const GLint bufWidth
= (GLint
) rb
->Width
;
1734 const GLint bufHeight
= (GLint
) rb
->Height
;
1736 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1737 /* completely above, below, or right */
1738 _mesa_bzero(index
, n
* sizeof(GLuint
));
1743 /* left edge clipping */
1745 length
= (GLint
) n
- skip
;
1747 /* completely left of window */
1750 if (length
> bufWidth
) {
1754 else if ((GLint
) (x
+ n
) > bufWidth
) {
1755 /* right edge clipping */
1757 length
= bufWidth
- x
;
1759 /* completely to right of window */
1770 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
1772 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1773 GLubyte index8
[MAX_WIDTH
];
1775 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index8
);
1776 for (i
= 0; i
< length
; i
++)
1777 index
[skip
+ i
] = index8
[i
];
1779 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1780 GLushort index16
[MAX_WIDTH
];
1782 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index16
);
1783 for (i
= 0; i
< length
; i
++)
1784 index
[skip
+ i
] = index16
[i
];
1786 else if (rb
->DataType
== GL_UNSIGNED_INT
) {
1787 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index
+ skip
);
1794 * Wrapper for gl_renderbuffer::GetValues() which does clipping to avoid
1795 * reading values outside the buffer bounds.
1796 * We can use this for reading any format/type of renderbuffer.
1797 * \param valueSize is the size in bytes of each value (pixel) put into the
1801 _swrast_get_values(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1802 GLuint count
, const GLint x
[], const GLint y
[],
1803 void *values
, GLuint valueSize
)
1805 GLuint i
, inCount
= 0, inStart
= 0;
1807 for (i
= 0; i
< count
; i
++) {
1808 if (x
[i
] >= 0 && y
[i
] >= 0 && x
[i
] < rb
->Width
&& y
[i
] < rb
->Height
) {
1816 /* read [inStart, inStart + inCount) */
1817 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1818 (GLubyte
*) values
+ inStart
* valueSize
);
1824 /* read last values */
1825 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1826 (GLubyte
*) values
+ inStart
* valueSize
);
1832 * Wrapper for gl_renderbuffer::PutRow() which does clipping.
1833 * \param valueSize size of each value (pixel) in bytes
1836 _swrast_put_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1837 GLuint count
, GLint x
, GLint y
,
1838 const GLvoid
*values
, GLuint valueSize
)
1842 if (y
< 0 || y
>= rb
->Height
)
1843 return; /* above or below */
1845 if (x
+ (GLint
) count
<= 0 || x
>= rb
->Width
)
1846 return; /* entirely left or right */
1848 if (x
+ count
> rb
->Width
) {
1850 GLint clip
= x
+ count
- rb
->Width
;
1861 rb
->PutRow(ctx
, rb
, count
, x
, y
,
1862 (const GLubyte
*) values
+ skip
* valueSize
, NULL
);
1867 * Wrapper for gl_renderbuffer::GetRow() which does clipping.
1868 * \param valueSize size of each value (pixel) in bytes
1871 _swrast_get_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1872 GLuint count
, GLint x
, GLint y
,
1873 GLvoid
*values
, GLuint valueSize
)
1877 if (y
< 0 || y
>= rb
->Height
)
1878 return; /* above or below */
1880 if (x
+ (GLint
) count
<= 0 || x
>= rb
->Width
)
1881 return; /* entirely left or right */
1883 if (x
+ count
> rb
->Width
) {
1885 GLint clip
= x
+ count
- rb
->Width
;
1896 rb
->GetRow(ctx
, rb
, count
, x
, y
, (GLubyte
*) values
+ skip
* valueSize
);
1901 * Get RGBA pixels from the given renderbuffer. Put the pixel colors into
1902 * the span's specular color arrays. The specular color arrays should no
1903 * longer be needed by time this function is called.
1904 * Used by blending, logicop and masking functions.
1905 * \return pointer to the colors we read.
1908 _swrast_get_dest_rgba(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1911 const GLuint pixelSize
= RGBA_PIXEL_SIZE(span
->array
->ChanType
);
1915 * Determine pixel size (in bytes).
1916 * Point rbPixels to a temporary space (use specular color arrays).
1918 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1919 rbPixels
= span
->array
->color
.sz1
.spec
;
1921 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
1922 rbPixels
= span
->array
->color
.sz2
.spec
;
1925 rbPixels
= span
->array
->color
.sz4
.spec
;
1928 /* Get destination values from renderbuffer */
1929 if (span
->arrayMask
& SPAN_XY
) {
1930 _swrast_get_values(ctx
, rb
, span
->end
, span
->array
->x
, span
->array
->y
,
1931 rbPixels
, pixelSize
);
1934 _swrast_get_row(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1935 rbPixels
, pixelSize
);