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] = _swrast_z_to_fogfactor(ctx
, ctx
->Current
.RasterDistance
);
79 span
->attrStepX
[FRAG_ATTRIB_FOGC
][0] = 0.0;
80 span
->attrStepY
[FRAG_ATTRIB_FOGC
][0] = 0.0;
81 span
->interpMask
|= SPAN_FOG
;
86 * Init span's rgba or index interpolation values to the RasterPos color.
87 * Used during setup for glDraw/CopyPixels.
90 _swrast_span_default_color( GLcontext
*ctx
, SWspan
*span
)
92 if (ctx
->Visual
.rgbMode
) {
94 UNCLAMPED_FLOAT_TO_CHAN(r
, ctx
->Current
.RasterColor
[0]);
95 UNCLAMPED_FLOAT_TO_CHAN(g
, ctx
->Current
.RasterColor
[1]);
96 UNCLAMPED_FLOAT_TO_CHAN(b
, ctx
->Current
.RasterColor
[2]);
97 UNCLAMPED_FLOAT_TO_CHAN(a
, ctx
->Current
.RasterColor
[3]);
98 #if CHAN_TYPE == GL_FLOAT
104 span
->red
= IntToFixed(r
);
105 span
->green
= IntToFixed(g
);
106 span
->blue
= IntToFixed(b
);
107 span
->alpha
= IntToFixed(a
);
113 span
->interpMask
|= SPAN_RGBA
;
116 span
->index
= FloatToFixed(ctx
->Current
.RasterIndex
);
118 span
->interpMask
|= SPAN_INDEX
;
124 * Init span's texcoord interpolation values to the RasterPos texcoords.
125 * Used during setup for glDraw/CopyPixels.
128 _swrast_span_default_texcoords( GLcontext
*ctx
, SWspan
*span
)
131 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
132 const GLfloat
*tc
= ctx
->Current
.RasterTexCoords
[i
];
133 if (ctx
->FragmentProgram
._Current
|| ctx
->ATIFragmentShader
._Enabled
) {
134 COPY_4V(span
->tex
[i
], tc
);
136 else if (tc
[3] > 0.0F
) {
137 /* use (s/q, t/q, r/q, 1) */
138 span
->tex
[i
][0] = tc
[0] / tc
[3];
139 span
->tex
[i
][1] = tc
[1] / tc
[3];
140 span
->tex
[i
][2] = tc
[2] / tc
[3];
141 span
->tex
[i
][3] = 1.0;
144 ASSIGN_4V(span
->tex
[i
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
146 ASSIGN_4V(span
->texStepX
[i
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
147 ASSIGN_4V(span
->texStepY
[i
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
149 span
->interpMask
|= SPAN_TEXTURE
;
154 * Interpolate primary colors to fill in the span->array->color array.
157 interpolate_colors(SWspan
*span
)
159 const GLuint n
= span
->end
;
162 ASSERT((span
->interpMask
& SPAN_RGBA
) &&
163 !(span
->arrayMask
& SPAN_RGBA
));
165 switch (span
->array
->ChanType
) {
167 case GL_UNSIGNED_BYTE
:
169 GLubyte (*rgba
)[4] = span
->array
->color
.sz1
.rgba
;
170 if (span
->interpMask
& SPAN_FLAT
) {
172 color
[RCOMP
] = FixedToInt(span
->red
);
173 color
[GCOMP
] = FixedToInt(span
->green
);
174 color
[BCOMP
] = FixedToInt(span
->blue
);
175 color
[ACOMP
] = FixedToInt(span
->alpha
);
176 for (i
= 0; i
< n
; i
++) {
177 COPY_4UBV(rgba
[i
], color
);
181 GLfixed r
= span
->red
;
182 GLfixed g
= span
->green
;
183 GLfixed b
= span
->blue
;
184 GLfixed a
= span
->alpha
;
185 GLint dr
= span
->redStep
;
186 GLint dg
= span
->greenStep
;
187 GLint db
= span
->blueStep
;
188 GLint da
= span
->alphaStep
;
189 for (i
= 0; i
< n
; i
++) {
190 rgba
[i
][RCOMP
] = FixedToChan(r
);
191 rgba
[i
][GCOMP
] = FixedToChan(g
);
192 rgba
[i
][BCOMP
] = FixedToChan(b
);
193 rgba
[i
][ACOMP
] = FixedToChan(a
);
202 case GL_UNSIGNED_SHORT
:
204 GLushort (*rgba
)[4] = span
->array
->color
.sz2
.rgba
;
205 if (span
->interpMask
& SPAN_FLAT
) {
207 color
[RCOMP
] = FixedToInt(span
->red
);
208 color
[GCOMP
] = FixedToInt(span
->green
);
209 color
[BCOMP
] = FixedToInt(span
->blue
);
210 color
[ACOMP
] = FixedToInt(span
->alpha
);
211 for (i
= 0; i
< n
; i
++) {
212 COPY_4V(rgba
[i
], color
);
216 GLushort (*rgba
)[4] = span
->array
->color
.sz2
.rgba
;
218 GLint dr
, dg
, db
, da
;
224 dg
= span
->greenStep
;
226 da
= span
->alphaStep
;
227 for (i
= 0; i
< n
; i
++) {
228 rgba
[i
][RCOMP
] = FixedToChan(r
);
229 rgba
[i
][GCOMP
] = FixedToChan(g
);
230 rgba
[i
][BCOMP
] = FixedToChan(b
);
231 rgba
[i
][ACOMP
] = FixedToChan(a
);
243 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
244 GLfloat r
, g
, b
, a
, dr
, dg
, db
, da
;
249 if (span
->interpMask
& SPAN_FLAT
) {
250 dr
= dg
= db
= da
= 0.0;
254 dg
= span
->greenStep
;
256 da
= span
->alphaStep
;
258 for (i
= 0; i
< n
; i
++) {
271 _mesa_problem(NULL
, "bad datatype in interpolate_colors");
273 span
->arrayMask
|= SPAN_RGBA
;
278 * Interpolate specular/secondary colors.
281 interpolate_specular(SWspan
*span
)
283 const GLuint n
= span
->end
;
286 switch (span
->array
->ChanType
) {
288 case GL_UNSIGNED_BYTE
:
290 GLubyte (*spec
)[4] = span
->array
->color
.sz1
.spec
;
291 if (span
->interpMask
& SPAN_FLAT
) {
293 color
[RCOMP
] = FixedToInt(span
->specRed
);
294 color
[GCOMP
] = FixedToInt(span
->specGreen
);
295 color
[BCOMP
] = FixedToInt(span
->specBlue
);
297 for (i
= 0; i
< n
; i
++) {
298 COPY_4UBV(spec
[i
], color
);
302 GLfixed r
= span
->specRed
;
303 GLfixed g
= span
->specGreen
;
304 GLfixed b
= span
->specBlue
;
305 GLint dr
= span
->specRedStep
;
306 GLint dg
= span
->specGreenStep
;
307 GLint db
= span
->specBlueStep
;
308 for (i
= 0; i
< n
; i
++) {
309 spec
[i
][RCOMP
] = CLAMP(FixedToChan(r
), 0, 255);
310 spec
[i
][GCOMP
] = CLAMP(FixedToChan(g
), 0, 255);
311 spec
[i
][BCOMP
] = CLAMP(FixedToChan(b
), 0, 255);
320 case GL_UNSIGNED_SHORT
:
322 GLushort (*spec
)[4] = span
->array
->color
.sz2
.spec
;
323 if (span
->interpMask
& SPAN_FLAT
) {
325 color
[RCOMP
] = FixedToInt(span
->specRed
);
326 color
[GCOMP
] = FixedToInt(span
->specGreen
);
327 color
[BCOMP
] = FixedToInt(span
->specBlue
);
329 for (i
= 0; i
< n
; i
++) {
330 COPY_4V(spec
[i
], color
);
334 GLfixed r
= FloatToFixed(span
->specRed
);
335 GLfixed g
= FloatToFixed(span
->specGreen
);
336 GLfixed b
= FloatToFixed(span
->specBlue
);
337 GLint dr
= FloatToFixed(span
->specRedStep
);
338 GLint dg
= FloatToFixed(span
->specGreenStep
);
339 GLint db
= FloatToFixed(span
->specBlueStep
);
340 for (i
= 0; i
< n
; i
++) {
341 spec
[i
][RCOMP
] = FixedToInt(r
);
342 spec
[i
][GCOMP
] = FixedToInt(g
);
343 spec
[i
][BCOMP
] = FixedToInt(b
);
355 GLfloat (*spec
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL1
];
357 GLfloat r
= CHAN_TO_FLOAT(FixedToChan(span
->specRed
));
358 GLfloat g
= CHAN_TO_FLOAT(FixedToChan(span
->specGreen
));
359 GLfloat b
= CHAN_TO_FLOAT(FixedToChan(span
->specBlue
));
361 GLfloat r
= span
->specRed
;
362 GLfloat g
= span
->specGreen
;
363 GLfloat b
= span
->specBlue
;
366 if (span
->interpMask
& SPAN_FLAT
) {
371 dr
= CHAN_TO_FLOAT(FixedToChan(span
->specRedStep
));
372 dg
= CHAN_TO_FLOAT(FixedToChan(span
->specGreenStep
));
373 db
= CHAN_TO_FLOAT(FixedToChan(span
->specBlueStep
));
375 dr
= span
->specRedStep
;
376 dg
= span
->specGreenStep
;
377 db
= span
->specBlueStep
;
380 for (i
= 0; i
< n
; i
++) {
384 spec
[i
][ACOMP
] = 0.0F
;
392 _mesa_problem(NULL
, "bad datatype in interpolate_specular");
394 span
->arrayMask
|= SPAN_SPEC
;
398 /* Fill in the span.color.index array from the interpolation values */
400 interpolate_indexes(GLcontext
*ctx
, SWspan
*span
)
402 GLfixed index
= span
->index
;
403 const GLint indexStep
= span
->indexStep
;
404 const GLuint n
= span
->end
;
405 GLuint
*indexes
= span
->array
->index
;
408 ASSERT((span
->interpMask
& SPAN_INDEX
) &&
409 !(span
->arrayMask
& SPAN_INDEX
));
411 if ((span
->interpMask
& SPAN_FLAT
) || (indexStep
== 0)) {
413 index
= FixedToInt(index
);
414 for (i
= 0; i
< n
; i
++) {
420 for (i
= 0; i
< n
; i
++) {
421 indexes
[i
] = FixedToInt(index
);
425 span
->arrayMask
|= SPAN_INDEX
;
426 span
->interpMask
&= ~SPAN_INDEX
;
430 /* Fill in the span.array.fog values from the interpolation values */
432 interpolate_fog(const GLcontext
*ctx
, SWspan
*span
)
434 GLfloat (*fog
)[4] = span
->array
->attribs
[FRAG_ATTRIB_FOGC
];
435 const GLfloat fogStep
= span
->attrStepX
[FRAG_ATTRIB_FOGC
][0];
436 GLfloat fogCoord
= span
->attrStart
[FRAG_ATTRIB_FOGC
][0];
437 const GLuint haveW
= (span
->interpMask
& SPAN_W
);
438 const GLfloat wStep
= haveW
? span
->dwdx
: 0.0F
;
439 GLfloat w
= haveW
? span
->w
: 1.0F
;
441 for (i
= 0; i
< span
->end
; i
++) {
442 fog
[i
][0] = fogCoord
/ w
;
446 span
->arrayMask
|= SPAN_FOG
;
450 /* Fill in the span.zArray array from the interpolation values */
452 _swrast_span_interpolate_z( const GLcontext
*ctx
, SWspan
*span
)
454 const GLuint n
= span
->end
;
457 ASSERT((span
->interpMask
& SPAN_Z
) &&
458 !(span
->arrayMask
& SPAN_Z
));
460 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
461 GLfixed zval
= span
->z
;
462 GLuint
*z
= span
->array
->z
;
463 for (i
= 0; i
< n
; i
++) {
464 z
[i
] = FixedToInt(zval
);
469 /* Deep Z buffer, no fixed->int shift */
470 GLuint zval
= span
->z
;
471 GLuint
*z
= span
->array
->z
;
472 for (i
= 0; i
< n
; i
++) {
477 span
->interpMask
&= ~SPAN_Z
;
478 span
->arrayMask
|= SPAN_Z
;
483 * This the ideal solution, as given in the OpenGL spec.
487 compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
488 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
489 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
491 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
492 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
493 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
494 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
495 GLfloat x
= SQRTF(dudx
* dudx
+ dvdx
* dvdx
);
496 GLfloat y
= SQRTF(dudy
* dudy
+ dvdy
* dvdy
);
497 GLfloat rho
= MAX2(x
, y
);
498 GLfloat lambda
= LOG2(rho
);
505 * This is a faster approximation
508 _swrast_compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
509 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
510 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
512 GLfloat dsdx2
= (s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
;
513 GLfloat dtdx2
= (t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
;
514 GLfloat dsdy2
= (s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
;
515 GLfloat dtdy2
= (t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
;
516 GLfloat maxU
, maxV
, rho
, lambda
;
517 dsdx2
= FABSF(dsdx2
);
518 dsdy2
= FABSF(dsdy2
);
519 dtdx2
= FABSF(dtdx2
);
520 dtdy2
= FABSF(dtdy2
);
521 maxU
= MAX2(dsdx2
, dsdy2
) * texW
;
522 maxV
= MAX2(dtdx2
, dtdy2
) * texH
;
523 rho
= MAX2(maxU
, maxV
);
530 * Fill in the span.texcoords array from the interpolation values.
531 * Note: in the places where we divide by Q (or mult by invQ) we're
532 * really doing two things: perspective correction and texcoord
533 * projection. Remember, for texcoord (s,t,r,q) we need to index
534 * texels with (s/q, t/q, r/q).
535 * If we're using a fragment program, we never do the division
536 * for texcoord projection. That's done by the TXP instruction
537 * or user-written code.
540 interpolate_texcoords(GLcontext
*ctx
, SWspan
*span
)
542 ASSERT(span
->interpMask
& SPAN_TEXTURE
);
543 ASSERT(!(span
->arrayMask
& SPAN_TEXTURE
));
545 if (ctx
->Texture
._EnabledCoordUnits
> 1) {
548 span
->arrayMask
|= SPAN_TEXTURE
;
549 /* XXX CoordUnits vs. ImageUnits */
550 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
551 if (ctx
->Texture
._EnabledCoordUnits
& (1 << u
)) {
552 const struct gl_texture_object
*obj
=ctx
->Texture
.Unit
[u
]._Current
;
554 GLboolean needLambda
;
556 const struct gl_texture_image
*img
= obj
->Image
[0][obj
->BaseLevel
];
557 needLambda
= (obj
->MinFilter
!= obj
->MagFilter
)
558 || ctx
->FragmentProgram
._Current
;
559 texW
= img
->WidthScale
;
560 texH
= img
->HeightScale
;
563 /* using a fragment program */
566 needLambda
= GL_FALSE
;
569 GLfloat (*texcoord
)[4] = span
->array
->attribs
[FRAG_ATTRIB_TEX0
+ u
];
570 GLfloat
*lambda
= span
->array
->lambda
[u
];
571 const GLfloat dsdx
= span
->texStepX
[u
][0];
572 const GLfloat dsdy
= span
->texStepY
[u
][0];
573 const GLfloat dtdx
= span
->texStepX
[u
][1];
574 const GLfloat dtdy
= span
->texStepY
[u
][1];
575 const GLfloat drdx
= span
->texStepX
[u
][2];
576 const GLfloat dqdx
= span
->texStepX
[u
][3];
577 const GLfloat dqdy
= span
->texStepY
[u
][3];
578 GLfloat s
= span
->tex
[u
][0];
579 GLfloat t
= span
->tex
[u
][1];
580 GLfloat r
= span
->tex
[u
][2];
581 GLfloat q
= span
->tex
[u
][3];
583 if (ctx
->FragmentProgram
._Current
584 || ctx
->ATIFragmentShader
._Enabled
) {
585 /* do perspective correction but don't divide s, t, r by q */
586 const GLfloat dwdx
= span
->dwdx
;
588 for (i
= 0; i
< span
->end
; i
++) {
589 const GLfloat invW
= 1.0F
/ w
;
590 texcoord
[i
][0] = s
* invW
;
591 texcoord
[i
][1] = t
* invW
;
592 texcoord
[i
][2] = r
* invW
;
593 texcoord
[i
][3] = q
* invW
;
594 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
595 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
->attribs
[FRAG_ATTRIB_TEX0
+ 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
->attribs
[FRAG_ATTRIB_TEX0
];
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
->attribs
[FRAG_ATTRIB_TEX0
];
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 arrays->attribs[FRAG_ATTRIB_VARx] arrays from the
821 * interpolation values.
822 * XXX since interpolants/arrays are getting uniformed, we might merge
823 * this with interpolate_texcoords(), interpolate_Fog(), etc. someday.
826 interpolate_varying(GLcontext
*ctx
, SWspan
*span
)
829 const GLbitfield inputsUsed
= ctx
->FragmentProgram
._Current
->Base
.InputsRead
;
831 ASSERT(span
->interpMask
& SPAN_VARYING
);
832 ASSERT(!(span
->arrayMask
& SPAN_VARYING
));
834 span
->arrayMask
|= SPAN_VARYING
;
836 for (var
= 0; var
< MAX_VARYING
; var
++) {
837 if (inputsUsed
& FRAG_BIT_VAR(var
)) {
838 const GLuint attr
= FRAG_ATTRIB_VAR0
+ var
;
840 for (j
= 0; j
< 4; j
++) {
841 const GLfloat dvdx
= span
->attrStepX
[attr
][j
];
842 GLfloat v
= span
->attrStart
[attr
][j
];
843 const GLfloat dwdx
= span
->dwdx
;
846 for (k
= 0; k
< span
->end
; k
++) {
847 GLfloat invW
= 1.0f
/ w
;
848 span
->array
->attribs
[attr
][k
][j
] = v
* invW
;
859 * Fill in the arrays->attribs[FRAG_ATTRIB_WPOS] array.
862 interpolate_wpos(GLcontext
*ctx
, SWspan
*span
)
864 GLfloat (*wpos
)[4] = span
->array
->attribs
[FRAG_ATTRIB_WPOS
];
866 if (span
->arrayMask
& SPAN_XY
) {
867 for (i
= 0; i
< span
->end
; i
++) {
868 wpos
[i
][0] = (GLfloat
) span
->array
->x
[i
];
869 wpos
[i
][1] = (GLfloat
) span
->array
->y
[i
];
873 for (i
= 0; i
< span
->end
; i
++) {
874 wpos
[i
][0] = (GLfloat
) span
->x
+ i
;
875 wpos
[i
][1] = (GLfloat
) span
->y
;
878 for (i
= 0; i
< span
->end
; i
++) {
879 wpos
[i
][2] = (GLfloat
) span
->array
->z
[i
] / ctx
->DrawBuffer
->_DepthMaxF
;
880 wpos
[i
][3] = span
->w
+ i
* span
->dwdx
;
886 * Apply the current polygon stipple pattern to a span of pixels.
889 stipple_polygon_span(GLcontext
*ctx
, SWspan
*span
)
891 GLubyte
*mask
= span
->array
->mask
;
893 ASSERT(ctx
->Polygon
.StippleFlag
);
895 if (span
->arrayMask
& SPAN_XY
) {
896 /* arrays of x/y pixel coords */
898 for (i
= 0; i
< span
->end
; i
++) {
899 const GLint col
= span
->array
->x
[i
] % 32;
900 const GLint row
= span
->array
->y
[i
] % 32;
901 const GLuint stipple
= ctx
->PolygonStipple
[row
];
902 if (((1 << col
) & stipple
) == 0) {
908 /* horizontal span of pixels */
909 const GLuint highBit
= 1 << 31;
910 const GLuint stipple
= ctx
->PolygonStipple
[span
->y
% 32];
911 GLuint i
, m
= highBit
>> (GLuint
) (span
->x
% 32);
912 for (i
= 0; i
< span
->end
; i
++) {
913 if ((m
& stipple
) == 0) {
922 span
->writeAll
= GL_FALSE
;
927 * Clip a pixel span to the current buffer/window boundaries:
928 * DrawBuffer->_Xmin, _Xmax, _Ymin, _Ymax. This will accomplish
929 * window clipping and scissoring.
930 * Return: GL_TRUE some pixels still visible
931 * GL_FALSE nothing visible
934 clip_span( GLcontext
*ctx
, SWspan
*span
)
936 const GLint xmin
= ctx
->DrawBuffer
->_Xmin
;
937 const GLint xmax
= ctx
->DrawBuffer
->_Xmax
;
938 const GLint ymin
= ctx
->DrawBuffer
->_Ymin
;
939 const GLint ymax
= ctx
->DrawBuffer
->_Ymax
;
941 if (span
->arrayMask
& SPAN_XY
) {
942 /* arrays of x/y pixel coords */
943 const GLint
*x
= span
->array
->x
;
944 const GLint
*y
= span
->array
->y
;
945 const GLint n
= span
->end
;
946 GLubyte
*mask
= span
->array
->mask
;
948 if (span
->arrayMask
& SPAN_MASK
) {
949 /* note: using & intead of && to reduce branches */
950 for (i
= 0; i
< n
; i
++) {
951 mask
[i
] &= (x
[i
] >= xmin
) & (x
[i
] < xmax
)
952 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
956 /* note: using & intead of && to reduce branches */
957 for (i
= 0; i
< n
; i
++) {
958 mask
[i
] = (x
[i
] >= xmin
) & (x
[i
] < xmax
)
959 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
962 return GL_TRUE
; /* some pixels visible */
965 /* horizontal span of pixels */
966 const GLint x
= span
->x
;
967 const GLint y
= span
->y
;
968 const GLint n
= span
->end
;
970 /* Trivial rejection tests */
971 if (y
< ymin
|| y
>= ymax
|| x
+ n
<= xmin
|| x
>= xmax
) {
973 return GL_FALSE
; /* all pixels clipped */
976 /* Clip to the left */
978 ASSERT(x
+ n
> xmin
);
979 span
->writeAll
= GL_FALSE
;
980 _mesa_bzero(span
->array
->mask
, (xmin
- x
) * sizeof(GLubyte
));
986 span
->end
= xmax
- x
;
989 return GL_TRUE
; /* some pixels visible */
995 * Apply all the per-fragment opertions to a span of color index fragments
996 * and write them to the enabled color drawbuffers.
997 * The 'span' parameter can be considered to be const. Note that
998 * span->interpMask and span->arrayMask may be changed but will be restored
999 * to their original values before returning.
1002 _swrast_write_index_span( GLcontext
*ctx
, SWspan
*span
)
1004 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1005 const GLbitfield origInterpMask
= span
->interpMask
;
1006 const GLbitfield origArrayMask
= span
->arrayMask
;
1008 ASSERT(span
->end
<= MAX_WIDTH
);
1009 ASSERT(span
->primitive
== GL_POINT
|| span
->primitive
== GL_LINE
||
1010 span
->primitive
== GL_POLYGON
|| span
->primitive
== GL_BITMAP
);
1011 ASSERT((span
->interpMask
| span
->arrayMask
) & SPAN_INDEX
);
1012 ASSERT((span
->interpMask
& span
->arrayMask
) == 0);
1014 if (span
->arrayMask
& SPAN_MASK
) {
1015 /* mask was initialized by caller, probably glBitmap */
1016 span
->writeAll
= GL_FALSE
;
1019 _mesa_memset(span
->array
->mask
, 1, span
->end
);
1020 span
->writeAll
= GL_TRUE
;
1024 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
1025 if (!clip_span(ctx
, span
)) {
1030 /* Depth bounds test */
1031 if (ctx
->Depth
.BoundsTest
&& ctx
->DrawBuffer
->Visual
.depthBits
> 0) {
1032 if (!_swrast_depth_bounds_test(ctx
, span
)) {
1038 /* Make sure all fragments are within window bounds */
1039 if (span
->arrayMask
& SPAN_XY
) {
1041 for (i
= 0; i
< span
->end
; i
++) {
1042 if (span
->array
->mask
[i
]) {
1043 assert(span
->array
->x
[i
] >= ctx
->DrawBuffer
->_Xmin
);
1044 assert(span
->array
->x
[i
] < ctx
->DrawBuffer
->_Xmax
);
1045 assert(span
->array
->y
[i
] >= ctx
->DrawBuffer
->_Ymin
);
1046 assert(span
->array
->y
[i
] < ctx
->DrawBuffer
->_Ymax
);
1052 /* Polygon Stippling */
1053 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
1054 stipple_polygon_span(ctx
, span
);
1057 /* Stencil and Z testing */
1058 if (ctx
->Depth
.Test
|| ctx
->Stencil
.Enabled
) {
1059 if (span
->interpMask
& SPAN_Z
)
1060 _swrast_span_interpolate_z(ctx
, span
);
1062 if (ctx
->Stencil
.Enabled
) {
1063 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
1064 span
->arrayMask
= origArrayMask
;
1069 ASSERT(ctx
->Depth
.Test
);
1070 if (!_swrast_depth_test_span(ctx
, span
)) {
1071 span
->interpMask
= origInterpMask
;
1072 span
->arrayMask
= origArrayMask
;
1078 #if FEATURE_ARB_occlusion_query
1079 if (ctx
->Query
.CurrentOcclusionObject
) {
1080 /* update count of 'passed' fragments */
1081 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
1083 for (i
= 0; i
< span
->end
; i
++)
1084 q
->Result
+= span
->array
->mask
[i
];
1088 /* we have to wait until after occlusion to do this test */
1089 if (ctx
->Color
.DrawBuffer
== GL_NONE
|| ctx
->Color
.IndexMask
== 0) {
1090 /* write no pixels */
1091 span
->arrayMask
= origArrayMask
;
1095 /* Interpolate the color indexes if needed */
1096 if (swrast
->_FogEnabled
||
1097 ctx
->Color
.IndexLogicOpEnabled
||
1098 ctx
->Color
.IndexMask
!= 0xffffffff ||
1099 (span
->arrayMask
& SPAN_COVERAGE
)) {
1100 if (span
->interpMask
& SPAN_INDEX
) {
1101 interpolate_indexes(ctx
, span
);
1106 if (swrast
->_FogEnabled
) {
1107 _swrast_fog_ci_span(ctx
, span
);
1110 /* Antialias coverage application */
1111 if (span
->arrayMask
& SPAN_COVERAGE
) {
1112 const GLfloat
*coverage
= span
->array
->coverage
;
1113 GLuint
*index
= span
->array
->index
;
1115 for (i
= 0; i
< span
->end
; i
++) {
1116 ASSERT(coverage
[i
] < 16);
1117 index
[i
] = (index
[i
] & ~0xf) | ((GLuint
) coverage
[i
]);
1122 * Write to renderbuffers
1125 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1126 const GLuint output
= 0; /* only frag progs can write to other outputs */
1127 const GLuint numDrawBuffers
= fb
->_NumColorDrawBuffers
[output
];
1128 GLuint indexSave
[MAX_WIDTH
];
1131 if (numDrawBuffers
> 1) {
1132 /* save indexes for second, third renderbuffer writes */
1133 _mesa_memcpy(indexSave
, span
->array
->index
,
1134 span
->end
* sizeof(indexSave
[0]));
1137 for (buf
= 0; buf
< fb
->_NumColorDrawBuffers
[output
]; buf
++) {
1138 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[output
][buf
];
1139 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
1141 if (ctx
->Color
.IndexLogicOpEnabled
) {
1142 _swrast_logicop_ci_span(ctx
, rb
, span
);
1145 if (ctx
->Color
.IndexMask
!= 0xffffffff) {
1146 _swrast_mask_ci_span(ctx
, rb
, span
);
1149 if ((span
->interpMask
& SPAN_INDEX
) && span
->indexStep
== 0) {
1150 /* all fragments have same color index */
1156 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1157 index8
= FixedToInt(span
->index
);
1160 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1161 index16
= FixedToInt(span
->index
);
1165 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
1166 index32
= FixedToInt(span
->index
);
1170 if (span
->arrayMask
& SPAN_XY
) {
1171 rb
->PutMonoValues(ctx
, rb
, span
->end
, span
->array
->x
,
1172 span
->array
->y
, value
, span
->array
->mask
);
1175 rb
->PutMonoRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1176 value
, span
->array
->mask
);
1180 /* each fragment is a different color */
1181 GLubyte index8
[MAX_WIDTH
];
1182 GLushort index16
[MAX_WIDTH
];
1185 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1187 for (k
= 0; k
< span
->end
; k
++) {
1188 index8
[k
] = (GLubyte
) span
->array
->index
[k
];
1192 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1194 for (k
= 0; k
< span
->end
; k
++) {
1195 index16
[k
] = (GLushort
) span
->array
->index
[k
];
1200 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
1201 values
= span
->array
->index
;
1204 if (span
->arrayMask
& SPAN_XY
) {
1205 rb
->PutValues(ctx
, rb
, span
->end
,
1206 span
->array
->x
, span
->array
->y
,
1207 values
, span
->array
->mask
);
1210 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1211 values
, span
->array
->mask
);
1215 if (buf
+ 1 < numDrawBuffers
) {
1216 /* restore original span values */
1217 _mesa_memcpy(span
->array
->index
, indexSave
,
1218 span
->end
* sizeof(indexSave
[0]));
1223 span
->interpMask
= origInterpMask
;
1224 span
->arrayMask
= origArrayMask
;
1229 * Add specular color to base color. This is used only when
1230 * GL_LIGHT_MODEL_COLOR_CONTROL = GL_SEPARATE_SPECULAR_COLOR.
1233 add_specular(GLcontext
*ctx
, SWspan
*span
)
1235 switch (span
->array
->ChanType
) {
1236 case GL_UNSIGNED_BYTE
:
1238 GLubyte (*rgba
)[4] = span
->array
->color
.sz1
.rgba
;
1239 GLubyte (*spec
)[4] = span
->array
->color
.sz1
.spec
;
1241 for (i
= 0; i
< span
->end
; i
++) {
1242 GLint r
= rgba
[i
][RCOMP
] + spec
[i
][RCOMP
];
1243 GLint g
= rgba
[i
][GCOMP
] + spec
[i
][GCOMP
];
1244 GLint b
= rgba
[i
][BCOMP
] + spec
[i
][BCOMP
];
1245 GLint a
= rgba
[i
][ACOMP
] + spec
[i
][ACOMP
];
1246 rgba
[i
][RCOMP
] = MIN2(r
, 255);
1247 rgba
[i
][GCOMP
] = MIN2(g
, 255);
1248 rgba
[i
][BCOMP
] = MIN2(b
, 255);
1249 rgba
[i
][ACOMP
] = MIN2(a
, 255);
1253 case GL_UNSIGNED_SHORT
:
1255 GLushort (*rgba
)[4] = span
->array
->color
.sz2
.rgba
;
1256 GLushort (*spec
)[4] = span
->array
->color
.sz2
.spec
;
1258 for (i
= 0; i
< span
->end
; i
++) {
1259 GLint r
= rgba
[i
][RCOMP
] + spec
[i
][RCOMP
];
1260 GLint g
= rgba
[i
][GCOMP
] + spec
[i
][GCOMP
];
1261 GLint b
= rgba
[i
][BCOMP
] + spec
[i
][BCOMP
];
1262 GLint a
= rgba
[i
][ACOMP
] + spec
[i
][ACOMP
];
1263 rgba
[i
][RCOMP
] = MIN2(r
, 65535);
1264 rgba
[i
][GCOMP
] = MIN2(g
, 65535);
1265 rgba
[i
][BCOMP
] = MIN2(b
, 65535);
1266 rgba
[i
][ACOMP
] = MIN2(a
, 65535);
1272 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1273 GLfloat (*spec
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL1
];
1275 for (i
= 0; i
< span
->end
; i
++) {
1276 rgba
[i
][RCOMP
] += spec
[i
][RCOMP
];
1277 rgba
[i
][GCOMP
] += spec
[i
][GCOMP
];
1278 rgba
[i
][BCOMP
] += spec
[i
][BCOMP
];
1279 rgba
[i
][ACOMP
] += spec
[i
][ACOMP
];
1284 _mesa_problem(ctx
, "Invalid datatype in add_specular");
1290 * Apply antialiasing coverage value to alpha values.
1293 apply_aa_coverage(SWspan
*span
)
1295 const GLfloat
*coverage
= span
->array
->coverage
;
1297 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1298 GLubyte (*rgba
)[4] = span
->array
->color
.sz1
.rgba
;
1299 for (i
= 0; i
< span
->end
; i
++) {
1300 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
1301 rgba
[i
][ACOMP
] = (GLubyte
) CLAMP(a
, 0.0, 255.0);
1302 ASSERT(coverage
[i
] >= 0.0);
1303 ASSERT(coverage
[i
] <= 1.0);
1306 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
1307 GLushort (*rgba
)[4] = span
->array
->color
.sz2
.rgba
;
1308 for (i
= 0; i
< span
->end
; i
++) {
1309 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
1310 rgba
[i
][ACOMP
] = (GLushort
) CLAMP(a
, 0.0, 65535.0);
1314 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1315 for (i
= 0; i
< span
->end
; i
++) {
1316 rgba
[i
][ACOMP
] = rgba
[i
][ACOMP
] * coverage
[i
];
1323 * Clamp span's float colors to [0,1]
1326 clamp_colors(SWspan
*span
)
1328 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1330 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
1331 for (i
= 0; i
< span
->end
; i
++) {
1332 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0F
, 1.0F
);
1333 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0F
, 1.0F
);
1334 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0F
, 1.0F
);
1335 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0F
, 1.0F
);
1341 * Convert the span's color arrays to the given type.
1344 convert_color_type(SWspan
*span
, GLenum newType
)
1347 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1348 src
= span
->array
->color
.sz1
.rgba
;
1350 else if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1351 src
= span
->array
->color
.sz2
.rgba
;
1354 src
= span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1356 if (newType
== GL_UNSIGNED_BYTE
) {
1357 dst
= span
->array
->color
.sz1
.rgba
;
1359 else if (newType
== GL_UNSIGNED_BYTE
) {
1360 dst
= span
->array
->color
.sz2
.rgba
;
1363 dst
= span
->array
->attribs
[FRAG_ATTRIB_COL0
];
1366 _mesa_convert_colors(span
->array
->ChanType
, src
,
1368 span
->end
, span
->array
->mask
);
1370 span
->array
->ChanType
= newType
;
1376 * Apply fragment shader, fragment program or normal texturing to span.
1379 shade_texture_span(GLcontext
*ctx
, SWspan
*span
)
1381 /* Now we need the rgba array, fill it in if needed */
1382 if (span
->interpMask
& SPAN_RGBA
)
1383 interpolate_colors(span
);
1385 if (ctx
->Texture
._EnabledCoordUnits
&& (span
->interpMask
& SPAN_TEXTURE
))
1386 interpolate_texcoords(ctx
, span
);
1388 if (ctx
->FragmentProgram
._Current
||
1389 ctx
->ATIFragmentShader
._Enabled
) {
1391 /* use float colors if running a fragment program or shader */
1392 const GLenum oldType
= span
->array
->ChanType
;
1393 const GLenum newType
= GL_FLOAT
;
1394 if (oldType
!= newType
) {
1395 GLvoid
*src
= (oldType
== GL_UNSIGNED_BYTE
)
1396 ? (GLvoid
*) span
->array
->color
.sz1
.rgba
1397 : (GLvoid
*) span
->array
->color
.sz2
.rgba
;
1398 _mesa_convert_colors(oldType
, src
,
1399 newType
, span
->array
->attribs
[FRAG_ATTRIB_COL0
],
1400 span
->end
, span
->array
->mask
);
1401 span
->array
->ChanType
= newType
;
1404 /* fragment programs/shaders may need specular, fog and Z coords */
1405 if (span
->interpMask
& SPAN_SPEC
)
1406 interpolate_specular(span
);
1408 if (span
->interpMask
& SPAN_FOG
)
1409 interpolate_fog(ctx
, span
);
1411 if (span
->interpMask
& SPAN_Z
)
1412 _swrast_span_interpolate_z (ctx
, span
);
1414 if (ctx
->Shader
.CurrentProgram
&& span
->interpMask
& SPAN_VARYING
)
1415 interpolate_varying(ctx
, span
);
1417 if (ctx
->FragmentProgram
._Current
&&
1418 (ctx
->FragmentProgram
._Current
->Base
.InputsRead
& FRAG_BIT_WPOS
))
1419 interpolate_wpos(ctx
, span
);
1421 /* Run fragment program/shader now */
1422 if (ctx
->FragmentProgram
._Current
) {
1423 _swrast_exec_fragment_program(ctx
, span
);
1426 ASSERT(ctx
->ATIFragmentShader
._Enabled
);
1427 _swrast_exec_fragment_shader(ctx
, span
);
1430 else if (ctx
->Texture
._EnabledUnits
&& (span
->arrayMask
& SPAN_TEXTURE
)) {
1431 /* conventional texturing */
1432 _swrast_texture_span(ctx
, span
);
1439 * Apply all the per-fragment operations to a span.
1440 * This now includes texturing (_swrast_write_texture_span() is history).
1441 * This function may modify any of the array values in the span.
1442 * span->interpMask and span->arrayMask may be changed but will be restored
1443 * to their original values before returning.
1446 _swrast_write_rgba_span( GLcontext
*ctx
, SWspan
*span
)
1448 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1449 const GLuint colorMask
= *((GLuint
*) ctx
->Color
.ColorMask
);
1450 const GLbitfield origInterpMask
= span
->interpMask
;
1451 const GLbitfield origArrayMask
= span
->arrayMask
;
1452 const GLenum chanType
= span
->array
->ChanType
;
1453 const GLboolean shader
= (ctx
->FragmentProgram
._Current
1454 || ctx
->ATIFragmentShader
._Enabled
);
1455 const GLboolean shaderOrTexture
= shader
|| ctx
->Texture
._EnabledUnits
;
1456 GLboolean deferredTexture
;
1459 printf("%s() interp 0x%x array 0x%x\n", __FUNCTION__,
1460 span->interpMask, span->arrayMask);
1463 ASSERT(span
->primitive
== GL_POINT
||
1464 span
->primitive
== GL_LINE
||
1465 span
->primitive
== GL_POLYGON
||
1466 span
->primitive
== GL_BITMAP
);
1467 ASSERT(span
->end
<= MAX_WIDTH
);
1468 ASSERT((span
->interpMask
& span
->arrayMask
) == 0);
1469 ASSERT((span
->interpMask
& SPAN_RGBA
) ^ (span
->arrayMask
& SPAN_RGBA
));
1471 /* check for conditions that prevent deferred shading */
1472 if (ctx
->Color
.AlphaEnabled
) {
1473 /* alpha test depends on post-texture/shader colors */
1474 deferredTexture
= GL_FALSE
;
1476 else if (shaderOrTexture
) {
1477 if (ctx
->FragmentProgram
._Current
) {
1478 if (ctx
->FragmentProgram
.Current
->Base
.OutputsWritten
1479 & (1 << FRAG_RESULT_DEPR
)) {
1480 /* Z comes from fragment program/shader */
1481 deferredTexture
= GL_FALSE
;
1484 deferredTexture
= GL_TRUE
;
1488 /* ATI frag shader or conventional texturing */
1489 deferredTexture
= GL_TRUE
;
1493 /* no texturing or shadering */
1494 deferredTexture
= GL_FALSE
;
1497 /* Fragment write masks */
1498 if (span
->arrayMask
& SPAN_MASK
) {
1499 /* mask was initialized by caller, probably glBitmap */
1500 span
->writeAll
= GL_FALSE
;
1503 _mesa_memset(span
->array
->mask
, 1, span
->end
);
1504 span
->writeAll
= GL_TRUE
;
1507 /* Clip to window/scissor box */
1508 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
1509 if (!clip_span(ctx
, span
)) {
1515 /* Make sure all fragments are within window bounds */
1516 if (span
->arrayMask
& SPAN_XY
) {
1518 for (i
= 0; i
< span
->end
; i
++) {
1519 if (span
->array
->mask
[i
]) {
1520 assert(span
->array
->x
[i
] >= ctx
->DrawBuffer
->_Xmin
);
1521 assert(span
->array
->x
[i
] < ctx
->DrawBuffer
->_Xmax
);
1522 assert(span
->array
->y
[i
] >= ctx
->DrawBuffer
->_Ymin
);
1523 assert(span
->array
->y
[i
] < ctx
->DrawBuffer
->_Ymax
);
1529 /* Polygon Stippling */
1530 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
1531 stipple_polygon_span(ctx
, span
);
1534 /* This is the normal place to compute the resulting fragment color/Z.
1535 * As an optimization, we try to defer this until after Z/stencil
1536 * testing in order to try to avoid computing colors that we won't
1539 if (shaderOrTexture
&& !deferredTexture
) {
1540 shade_texture_span(ctx
, span
);
1543 /* Do the alpha test */
1544 if (ctx
->Color
.AlphaEnabled
) {
1545 if (!_swrast_alpha_test(ctx
, span
)) {
1550 /* Stencil and Z testing */
1551 if (ctx
->Stencil
.Enabled
|| ctx
->Depth
.Test
) {
1552 if (span
->interpMask
& SPAN_Z
)
1553 _swrast_span_interpolate_z(ctx
, span
);
1555 if (ctx
->Stencil
.Enabled
&& ctx
->DrawBuffer
->Visual
.stencilBits
> 0) {
1556 /* Combined Z/stencil tests */
1557 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
1561 else if (ctx
->DrawBuffer
->Visual
.depthBits
> 0) {
1562 /* Just regular depth testing */
1563 ASSERT(ctx
->Depth
.Test
);
1564 ASSERT(span
->arrayMask
& SPAN_Z
);
1565 if (!_swrast_depth_test_span(ctx
, span
)) {
1571 #if FEATURE_ARB_occlusion_query
1572 if (ctx
->Query
.CurrentOcclusionObject
) {
1573 /* update count of 'passed' fragments */
1574 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
1576 for (i
= 0; i
< span
->end
; i
++)
1577 q
->Result
+= span
->array
->mask
[i
];
1581 /* We had to wait until now to check for glColorMask(0,0,0,0) because of
1582 * the occlusion test.
1584 if (colorMask
== 0x0) {
1588 /* If we were able to defer fragment color computation to now, there's
1589 * a good chance that many fragments will have already been killed by
1590 * Z/stencil testing.
1592 if (deferredTexture
) {
1593 ASSERT(shaderOrTexture
);
1594 shade_texture_span(ctx
, span
);
1597 if ((span
->arrayMask
& SPAN_RGBA
) == 0) {
1598 interpolate_colors(span
);
1601 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1604 /* Add base and specular colors */
1605 if (ctx
->Fog
.ColorSumEnabled
||
1606 (ctx
->Light
.Enabled
&&
1607 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)) {
1608 if (span
->interpMask
& SPAN_SPEC
) {
1609 interpolate_specular(span
);
1611 if (span
->arrayMask
& SPAN_SPEC
) {
1612 add_specular(ctx
, span
);
1615 /* We probably added the base/specular colors during the
1623 if (swrast
->_FogEnabled
) {
1624 _swrast_fog_rgba_span(ctx
, span
);
1627 /* Antialias coverage application */
1628 if (span
->arrayMask
& SPAN_COVERAGE
) {
1629 apply_aa_coverage(span
);
1632 /* Clamp color/alpha values over the range [0.0, 1.0] before storage */
1633 if (ctx
->Color
.ClampFragmentColor
== GL_TRUE
&&
1634 span
->array
->ChanType
== GL_FLOAT
) {
1639 * Write to renderbuffers
1642 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1643 const GLuint output
= 0; /* only frag progs can write to other outputs */
1644 const GLuint numDrawBuffers
= fb
->_NumColorDrawBuffers
[output
];
1645 GLchan rgbaSave
[MAX_WIDTH
][4];
1648 if (numDrawBuffers
> 0) {
1649 if (fb
->_ColorDrawBuffers
[output
][0]->DataType
1650 != span
->array
->ChanType
) {
1651 convert_color_type(span
,
1652 fb
->_ColorDrawBuffers
[output
][0]->DataType
);
1656 if (numDrawBuffers
> 1) {
1657 /* save colors for second, third renderbuffer writes */
1658 _mesa_memcpy(rgbaSave
, span
->array
->rgba
,
1659 4 * span
->end
* sizeof(GLchan
));
1662 for (buf
= 0; buf
< numDrawBuffers
; buf
++) {
1663 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[output
][buf
];
1664 ASSERT(rb
->_BaseFormat
== GL_RGBA
|| rb
->_BaseFormat
== GL_RGB
);
1666 if (ctx
->Color
._LogicOpEnabled
) {
1667 _swrast_logicop_rgba_span(ctx
, rb
, span
);
1669 else if (ctx
->Color
.BlendEnabled
) {
1670 _swrast_blend_span(ctx
, rb
, span
);
1673 if (colorMask
!= 0xffffffff) {
1674 _swrast_mask_rgba_span(ctx
, rb
, span
);
1677 if (span
->arrayMask
& SPAN_XY
) {
1678 /* array of pixel coords */
1679 ASSERT(rb
->PutValues
);
1680 rb
->PutValues(ctx
, rb
, span
->end
,
1681 span
->array
->x
, span
->array
->y
,
1682 span
->array
->rgba
, span
->array
->mask
);
1685 /* horizontal run of pixels */
1687 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
, span
->array
->rgba
,
1688 span
->writeAll
? NULL
: span
->array
->mask
);
1691 if (buf
+ 1 < numDrawBuffers
) {
1692 /* restore original span values */
1693 _mesa_memcpy(span
->array
->rgba
, rgbaSave
,
1694 4 * span
->end
* sizeof(GLchan
));
1701 /* restore these values before returning */
1702 span
->interpMask
= origInterpMask
;
1703 span
->arrayMask
= origArrayMask
;
1704 span
->array
->ChanType
= chanType
;
1709 * Read RGBA pixels from a renderbuffer. Clipping will be done to prevent
1710 * reading ouside the buffer's boundaries.
1711 * \param dstType datatype for returned colors
1712 * \param rgba the returned colors
1715 _swrast_read_rgba_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1716 GLuint n
, GLint x
, GLint y
, GLenum dstType
,
1719 const GLint bufWidth
= (GLint
) rb
->Width
;
1720 const GLint bufHeight
= (GLint
) rb
->Height
;
1722 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1723 /* completely above, below, or right */
1724 /* XXX maybe leave rgba values undefined? */
1725 _mesa_bzero(rgba
, 4 * n
* sizeof(GLchan
));
1730 /* left edge clipping */
1732 length
= (GLint
) n
- skip
;
1734 /* completely left of window */
1737 if (length
> bufWidth
) {
1741 else if ((GLint
) (x
+ n
) > bufWidth
) {
1742 /* right edge clipping */
1744 length
= bufWidth
- x
;
1746 /* completely to right of window */
1758 ASSERT(rb
->_BaseFormat
== GL_RGB
|| rb
->_BaseFormat
== GL_RGBA
);
1760 if (rb
->DataType
== dstType
) {
1761 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
,
1762 (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(rb
->DataType
));
1765 GLuint temp
[MAX_WIDTH
* 4];
1766 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, temp
);
1767 _mesa_convert_colors(rb
->DataType
, temp
,
1768 dstType
, (GLubyte
*) rgba
+ skip
* RGBA_PIXEL_SIZE(dstType
),
1776 * Read CI pixels from a renderbuffer. Clipping will be done to prevent
1777 * reading ouside the buffer's boundaries.
1780 _swrast_read_index_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1781 GLuint n
, GLint x
, GLint y
, GLuint index
[] )
1783 const GLint bufWidth
= (GLint
) rb
->Width
;
1784 const GLint bufHeight
= (GLint
) rb
->Height
;
1786 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1787 /* completely above, below, or right */
1788 _mesa_bzero(index
, n
* sizeof(GLuint
));
1793 /* left edge clipping */
1795 length
= (GLint
) n
- skip
;
1797 /* completely left of window */
1800 if (length
> bufWidth
) {
1804 else if ((GLint
) (x
+ n
) > bufWidth
) {
1805 /* right edge clipping */
1807 length
= bufWidth
- x
;
1809 /* completely to right of window */
1820 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
1822 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1823 GLubyte index8
[MAX_WIDTH
];
1825 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index8
);
1826 for (i
= 0; i
< length
; i
++)
1827 index
[skip
+ i
] = index8
[i
];
1829 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1830 GLushort index16
[MAX_WIDTH
];
1832 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index16
);
1833 for (i
= 0; i
< length
; i
++)
1834 index
[skip
+ i
] = index16
[i
];
1836 else if (rb
->DataType
== GL_UNSIGNED_INT
) {
1837 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index
+ skip
);
1844 * Wrapper for gl_renderbuffer::GetValues() which does clipping to avoid
1845 * reading values outside the buffer bounds.
1846 * We can use this for reading any format/type of renderbuffer.
1847 * \param valueSize is the size in bytes of each value (pixel) put into the
1851 _swrast_get_values(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1852 GLuint count
, const GLint x
[], const GLint y
[],
1853 void *values
, GLuint valueSize
)
1855 GLuint i
, inCount
= 0, inStart
= 0;
1857 for (i
= 0; i
< count
; i
++) {
1858 if (x
[i
] >= 0 && y
[i
] >= 0 &&
1859 x
[i
] < (GLint
) rb
->Width
&& y
[i
] < (GLint
) rb
->Height
) {
1867 /* read [inStart, inStart + inCount) */
1868 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1869 (GLubyte
*) values
+ inStart
* valueSize
);
1875 /* read last values */
1876 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1877 (GLubyte
*) values
+ inStart
* valueSize
);
1883 * Wrapper for gl_renderbuffer::PutRow() which does clipping.
1884 * \param valueSize size of each value (pixel) in bytes
1887 _swrast_put_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1888 GLuint count
, GLint x
, GLint y
,
1889 const GLvoid
*values
, GLuint valueSize
)
1893 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1894 return; /* above or below */
1896 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1897 return; /* entirely left or right */
1899 if ((GLint
) (x
+ count
) > (GLint
) rb
->Width
) {
1901 GLint clip
= x
+ count
- rb
->Width
;
1912 rb
->PutRow(ctx
, rb
, count
, x
, y
,
1913 (const GLubyte
*) values
+ skip
* valueSize
, NULL
);
1918 * Wrapper for gl_renderbuffer::GetRow() which does clipping.
1919 * \param valueSize size of each value (pixel) in bytes
1922 _swrast_get_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1923 GLuint count
, GLint x
, GLint y
,
1924 GLvoid
*values
, GLuint valueSize
)
1928 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1929 return; /* above or below */
1931 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1932 return; /* entirely left or right */
1934 if (x
+ count
> rb
->Width
) {
1936 GLint clip
= x
+ count
- rb
->Width
;
1947 rb
->GetRow(ctx
, rb
, count
, x
, y
, (GLubyte
*) values
+ skip
* valueSize
);
1952 * Get RGBA pixels from the given renderbuffer. Put the pixel colors into
1953 * the span's specular color arrays. The specular color arrays should no
1954 * longer be needed by time this function is called.
1955 * Used by blending, logicop and masking functions.
1956 * \return pointer to the colors we read.
1959 _swrast_get_dest_rgba(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1962 const GLuint pixelSize
= RGBA_PIXEL_SIZE(span
->array
->ChanType
);
1966 * Determine pixel size (in bytes).
1967 * Point rbPixels to a temporary space (use specular color arrays).
1969 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
1970 rbPixels
= span
->array
->color
.sz1
.spec
;
1972 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
1973 rbPixels
= span
->array
->color
.sz2
.spec
;
1976 rbPixels
= span
->array
->attribs
[FRAG_ATTRIB_COL1
];
1979 /* Get destination values from renderbuffer */
1980 if (span
->arrayMask
& SPAN_XY
) {
1981 _swrast_get_values(ctx
, rb
, span
->end
, span
->array
->x
, span
->array
->y
,
1982 rbPixels
, pixelSize
);
1985 _swrast_get_row(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1986 rbPixels
, pixelSize
);