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
39 #include "s_atifragshader.h"
41 #include "s_arbshader.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
, struct sw_span
*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
, struct sw_span
*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
, struct sw_span
*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
, struct sw_span
*span
)
130 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
131 const GLfloat
*tc
= ctx
->Current
.RasterTexCoords
[i
];
132 if (ctx
->FragmentProgram
._Active
|| 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
;
152 /* Fill in the span.color.rgba array from the interpolation values */
154 interpolate_colors(GLcontext
*ctx
, struct sw_span
*span
)
156 const GLuint n
= span
->end
;
157 GLchan (*rgba
)[4] = span
->array
->rgba
;
161 ASSERT((span
->interpMask
& SPAN_RGBA
) &&
162 !(span
->arrayMask
& SPAN_RGBA
));
164 if (span
->interpMask
& SPAN_FLAT
) {
167 color
[RCOMP
] = FixedToChan(span
->red
);
168 color
[GCOMP
] = FixedToChan(span
->green
);
169 color
[BCOMP
] = FixedToChan(span
->blue
);
170 color
[ACOMP
] = FixedToChan(span
->alpha
);
171 for (i
= 0; i
< n
; i
++) {
172 COPY_CHAN4(span
->array
->rgba
[i
], color
);
177 #if CHAN_TYPE == GL_FLOAT
178 GLfloat r
= span
->red
;
179 GLfloat g
= span
->green
;
180 GLfloat b
= span
->blue
;
181 GLfloat a
= span
->alpha
;
182 const GLfloat dr
= span
->redStep
;
183 const GLfloat dg
= span
->greenStep
;
184 const GLfloat db
= span
->blueStep
;
185 const GLfloat da
= span
->alphaStep
;
187 GLfixed r
= span
->red
;
188 GLfixed g
= span
->green
;
189 GLfixed b
= span
->blue
;
190 GLfixed a
= span
->alpha
;
191 const GLint dr
= span
->redStep
;
192 const GLint dg
= span
->greenStep
;
193 const GLint db
= span
->blueStep
;
194 const GLint da
= span
->alphaStep
;
196 for (i
= 0; i
< n
; i
++) {
197 rgba
[i
][RCOMP
] = FixedToChan(r
);
198 rgba
[i
][GCOMP
] = FixedToChan(g
);
199 rgba
[i
][BCOMP
] = FixedToChan(b
);
200 rgba
[i
][ACOMP
] = FixedToChan(a
);
207 span
->arrayMask
|= SPAN_RGBA
;
211 /* Fill in the span.color.index array from the interpolation values */
213 interpolate_indexes(GLcontext
*ctx
, struct sw_span
*span
)
215 GLfixed index
= span
->index
;
216 const GLint indexStep
= span
->indexStep
;
217 const GLuint n
= span
->end
;
218 GLuint
*indexes
= span
->array
->index
;
221 ASSERT((span
->interpMask
& SPAN_INDEX
) &&
222 !(span
->arrayMask
& SPAN_INDEX
));
224 if ((span
->interpMask
& SPAN_FLAT
) || (indexStep
== 0)) {
226 index
= FixedToInt(index
);
227 for (i
= 0; i
< n
; i
++) {
233 for (i
= 0; i
< n
; i
++) {
234 indexes
[i
] = FixedToInt(index
);
238 span
->arrayMask
|= SPAN_INDEX
;
239 span
->interpMask
&= ~SPAN_INDEX
;
243 /* Fill in the span.->array->spec array from the interpolation values */
245 interpolate_specular(GLcontext
*ctx
, struct sw_span
*span
)
248 if (span
->interpMask
& SPAN_FLAT
) {
250 const GLchan r
= FixedToChan(span
->specRed
);
251 const GLchan g
= FixedToChan(span
->specGreen
);
252 const GLchan b
= FixedToChan(span
->specBlue
);
254 for (i
= 0; i
< span
->end
; i
++) {
255 span
->array
->spec
[i
][RCOMP
] = r
;
256 span
->array
->spec
[i
][GCOMP
] = g
;
257 span
->array
->spec
[i
][BCOMP
] = b
;
262 #if CHAN_TYPE == GL_FLOAT
263 GLfloat r
= span
->specRed
;
264 GLfloat g
= span
->specGreen
;
265 GLfloat b
= span
->specBlue
;
267 GLfixed r
= span
->specRed
;
268 GLfixed g
= span
->specGreen
;
269 GLfixed b
= span
->specBlue
;
272 for (i
= 0; i
< span
->end
; i
++) {
273 span
->array
->spec
[i
][RCOMP
] = FixedToChan(r
);
274 span
->array
->spec
[i
][GCOMP
] = FixedToChan(g
);
275 span
->array
->spec
[i
][BCOMP
] = FixedToChan(b
);
276 r
+= span
->specRedStep
;
277 g
+= span
->specGreenStep
;
278 b
+= span
->specBlueStep
;
281 span
->arrayMask
|= SPAN_SPEC
;
285 /* Fill in the span.array.fog values from the interpolation values */
287 interpolate_fog(const GLcontext
*ctx
, struct sw_span
*span
)
289 GLfloat
*fog
= span
->array
->fog
;
290 const GLfloat fogStep
= span
->fogStep
;
291 GLfloat fogCoord
= span
->fog
;
292 const GLuint haveW
= (span
->interpMask
& SPAN_W
);
293 const GLfloat wStep
= haveW
? span
->dwdx
: 0.0F
;
294 GLfloat w
= haveW
? span
->w
: 1.0F
;
296 for (i
= 0; i
< span
->end
; i
++) {
297 fog
[i
] = fogCoord
/ w
;
301 span
->arrayMask
|= SPAN_FOG
;
305 /* Fill in the span.zArray array from the interpolation values */
307 _swrast_span_interpolate_z( const GLcontext
*ctx
, struct sw_span
*span
)
309 const GLuint n
= span
->end
;
312 ASSERT((span
->interpMask
& SPAN_Z
) &&
313 !(span
->arrayMask
& SPAN_Z
));
315 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
316 GLfixed zval
= span
->z
;
317 GLuint
*z
= span
->array
->z
;
318 for (i
= 0; i
< n
; i
++) {
319 z
[i
] = FixedToInt(zval
);
324 /* Deep Z buffer, no fixed->int shift */
325 GLuint zval
= span
->z
;
326 GLuint
*z
= span
->array
->z
;
327 for (i
= 0; i
< n
; i
++) {
332 span
->interpMask
&= ~SPAN_Z
;
333 span
->arrayMask
|= SPAN_Z
;
338 * This the ideal solution, as given in the OpenGL spec.
342 compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
343 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
344 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
346 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
347 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
348 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
349 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
350 GLfloat x
= SQRTF(dudx
* dudx
+ dvdx
* dvdx
);
351 GLfloat y
= SQRTF(dudy
* dudy
+ dvdy
* dvdy
);
352 GLfloat rho
= MAX2(x
, y
);
353 GLfloat lambda
= LOG2(rho
);
360 * This is a faster approximation
363 _swrast_compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
364 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
365 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
367 GLfloat dsdx2
= (s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
;
368 GLfloat dtdx2
= (t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
;
369 GLfloat dsdy2
= (s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
;
370 GLfloat dtdy2
= (t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
;
371 GLfloat maxU
, maxV
, rho
, lambda
;
372 dsdx2
= FABSF(dsdx2
);
373 dsdy2
= FABSF(dsdy2
);
374 dtdx2
= FABSF(dtdx2
);
375 dtdy2
= FABSF(dtdy2
);
376 maxU
= MAX2(dsdx2
, dsdy2
) * texW
;
377 maxV
= MAX2(dtdx2
, dtdy2
) * texH
;
378 rho
= MAX2(maxU
, maxV
);
385 * Fill in the span.texcoords array from the interpolation values.
386 * Note: in the places where we divide by Q (or mult by invQ) we're
387 * really doing two things: perspective correction and texcoord
388 * projection. Remember, for texcoord (s,t,r,q) we need to index
389 * texels with (s/q, t/q, r/q).
390 * If we're using a fragment program, we never do the division
391 * for texcoord projection. That's done by the TXP instruction
392 * or user-written code.
395 interpolate_texcoords(GLcontext
*ctx
, struct sw_span
*span
)
397 ASSERT(span
->interpMask
& SPAN_TEXTURE
);
398 ASSERT(!(span
->arrayMask
& SPAN_TEXTURE
));
400 if (ctx
->Texture
._EnabledCoordUnits
> 1) {
403 span
->arrayMask
|= SPAN_TEXTURE
;
404 for (u
= 0; u
< ctx
->Const
.MaxTextureUnits
; u
++) {
405 if (ctx
->Texture
._EnabledCoordUnits
& (1 << u
)) {
406 const struct gl_texture_object
*obj
=ctx
->Texture
.Unit
[u
]._Current
;
408 GLboolean needLambda
;
410 const struct gl_texture_image
*img
= obj
->Image
[0][obj
->BaseLevel
];
411 needLambda
= (obj
->MinFilter
!= obj
->MagFilter
)
412 || ctx
->FragmentProgram
._Active
;
413 texW
= img
->WidthScale
;
414 texH
= img
->HeightScale
;
417 /* using a fragment program */
420 needLambda
= GL_FALSE
;
423 GLfloat (*texcoord
)[4] = span
->array
->texcoords
[u
];
424 GLfloat
*lambda
= span
->array
->lambda
[u
];
425 const GLfloat dsdx
= span
->texStepX
[u
][0];
426 const GLfloat dsdy
= span
->texStepY
[u
][0];
427 const GLfloat dtdx
= span
->texStepX
[u
][1];
428 const GLfloat dtdy
= span
->texStepY
[u
][1];
429 const GLfloat drdx
= span
->texStepX
[u
][2];
430 const GLfloat dqdx
= span
->texStepX
[u
][3];
431 const GLfloat dqdy
= span
->texStepY
[u
][3];
432 GLfloat s
= span
->tex
[u
][0];
433 GLfloat t
= span
->tex
[u
][1];
434 GLfloat r
= span
->tex
[u
][2];
435 GLfloat q
= span
->tex
[u
][3];
437 if (ctx
->FragmentProgram
._Active
|| ctx
->ATIFragmentShader
._Enabled
) {
438 /* do perspective correction but don't divide s, t, r by q */
439 const GLfloat dwdx
= span
->dwdx
;
441 for (i
= 0; i
< span
->end
; i
++) {
442 const GLfloat invW
= 1.0F
/ w
;
443 texcoord
[i
][0] = s
* invW
;
444 texcoord
[i
][1] = t
* invW
;
445 texcoord
[i
][2] = r
* invW
;
446 texcoord
[i
][3] = q
* invW
;
447 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
448 dqdx
, dqdy
, texW
, texH
,
459 for (i
= 0; i
< span
->end
; i
++) {
460 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
461 texcoord
[i
][0] = s
* invQ
;
462 texcoord
[i
][1] = t
* invQ
;
463 texcoord
[i
][2] = r
* invQ
;
465 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
466 dqdx
, dqdy
, texW
, texH
,
474 span
->arrayMask
|= SPAN_LAMBDA
;
477 GLfloat (*texcoord
)[4] = span
->array
->texcoords
[u
];
478 GLfloat
*lambda
= span
->array
->lambda
[u
];
479 const GLfloat dsdx
= span
->texStepX
[u
][0];
480 const GLfloat dtdx
= span
->texStepX
[u
][1];
481 const GLfloat drdx
= span
->texStepX
[u
][2];
482 const GLfloat dqdx
= span
->texStepX
[u
][3];
483 GLfloat s
= span
->tex
[u
][0];
484 GLfloat t
= span
->tex
[u
][1];
485 GLfloat r
= span
->tex
[u
][2];
486 GLfloat q
= span
->tex
[u
][3];
488 if (ctx
->FragmentProgram
._Active
|| ctx
->ATIFragmentShader
._Enabled
) {
489 /* do perspective correction but don't divide s, t, r by q */
490 const GLfloat dwdx
= span
->dwdx
;
492 for (i
= 0; i
< span
->end
; i
++) {
493 const GLfloat invW
= 1.0F
/ w
;
494 texcoord
[i
][0] = s
* invW
;
495 texcoord
[i
][1] = t
* invW
;
496 texcoord
[i
][2] = r
* invW
;
497 texcoord
[i
][3] = q
* invW
;
506 else if (dqdx
== 0.0F
) {
507 /* Ortho projection or polygon's parallel to window X axis */
508 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
509 for (i
= 0; i
< span
->end
; i
++) {
510 texcoord
[i
][0] = s
* invQ
;
511 texcoord
[i
][1] = t
* invQ
;
512 texcoord
[i
][2] = r
* invQ
;
521 for (i
= 0; i
< span
->end
; i
++) {
522 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
523 texcoord
[i
][0] = s
* invQ
;
524 texcoord
[i
][1] = t
* invQ
;
525 texcoord
[i
][2] = r
* invQ
;
540 const struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[0]._Current
;
542 GLboolean needLambda
;
544 const struct gl_texture_image
*img
= obj
->Image
[0][obj
->BaseLevel
];
545 needLambda
= (obj
->MinFilter
!= obj
->MagFilter
)
546 || ctx
->FragmentProgram
._Active
;
547 texW
= (GLfloat
) img
->WidthScale
;
548 texH
= (GLfloat
) img
->HeightScale
;
551 needLambda
= GL_FALSE
;
554 span
->arrayMask
|= SPAN_TEXTURE
;
556 /* just texture unit 0, with lambda */
557 GLfloat (*texcoord
)[4] = span
->array
->texcoords
[0];
558 GLfloat
*lambda
= span
->array
->lambda
[0];
559 const GLfloat dsdx
= span
->texStepX
[0][0];
560 const GLfloat dsdy
= span
->texStepY
[0][0];
561 const GLfloat dtdx
= span
->texStepX
[0][1];
562 const GLfloat dtdy
= span
->texStepY
[0][1];
563 const GLfloat drdx
= span
->texStepX
[0][2];
564 const GLfloat dqdx
= span
->texStepX
[0][3];
565 const GLfloat dqdy
= span
->texStepY
[0][3];
566 GLfloat s
= span
->tex
[0][0];
567 GLfloat t
= span
->tex
[0][1];
568 GLfloat r
= span
->tex
[0][2];
569 GLfloat q
= span
->tex
[0][3];
571 if (ctx
->FragmentProgram
._Active
|| ctx
->ATIFragmentShader
._Enabled
) {
572 /* do perspective correction but don't divide s, t, r by q */
573 const GLfloat dwdx
= span
->dwdx
;
575 for (i
= 0; i
< span
->end
; i
++) {
576 const GLfloat invW
= 1.0F
/ w
;
577 texcoord
[i
][0] = s
* invW
;
578 texcoord
[i
][1] = t
* invW
;
579 texcoord
[i
][2] = r
* invW
;
580 texcoord
[i
][3] = q
* invW
;
581 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
582 dqdx
, dqdy
, texW
, texH
,
593 for (i
= 0; i
< span
->end
; i
++) {
594 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
595 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
596 dqdx
, dqdy
, texW
, texH
,
598 texcoord
[i
][0] = s
* invQ
;
599 texcoord
[i
][1] = t
* invQ
;
600 texcoord
[i
][2] = r
* invQ
;
608 span
->arrayMask
|= SPAN_LAMBDA
;
611 /* just texture 0, without lambda */
612 GLfloat (*texcoord
)[4] = span
->array
->texcoords
[0];
613 const GLfloat dsdx
= span
->texStepX
[0][0];
614 const GLfloat dtdx
= span
->texStepX
[0][1];
615 const GLfloat drdx
= span
->texStepX
[0][2];
616 const GLfloat dqdx
= span
->texStepX
[0][3];
617 GLfloat s
= span
->tex
[0][0];
618 GLfloat t
= span
->tex
[0][1];
619 GLfloat r
= span
->tex
[0][2];
620 GLfloat q
= span
->tex
[0][3];
622 if (ctx
->FragmentProgram
._Active
|| ctx
->ATIFragmentShader
._Enabled
) {
623 /* do perspective correction but don't divide s, t, r by q */
624 const GLfloat dwdx
= span
->dwdx
;
626 for (i
= 0; i
< span
->end
; i
++) {
627 const GLfloat invW
= 1.0F
/ w
;
628 texcoord
[i
][0] = s
* invW
;
629 texcoord
[i
][1] = t
* invW
;
630 texcoord
[i
][2] = r
* invW
;
631 texcoord
[i
][3] = q
* invW
;
639 else if (dqdx
== 0.0F
) {
640 /* Ortho projection or polygon's parallel to window X axis */
641 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
642 for (i
= 0; i
< span
->end
; i
++) {
643 texcoord
[i
][0] = s
* invQ
;
644 texcoord
[i
][1] = t
* invQ
;
645 texcoord
[i
][2] = r
* invQ
;
653 for (i
= 0; i
< span
->end
; i
++) {
654 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
655 texcoord
[i
][0] = s
* invQ
;
656 texcoord
[i
][1] = t
* invQ
;
657 texcoord
[i
][2] = r
* invQ
;
671 * Apply the current polygon stipple pattern to a span of pixels.
674 stipple_polygon_span( GLcontext
*ctx
, struct sw_span
*span
)
676 const GLuint highbit
= 0x80000000;
677 const GLuint stipple
= ctx
->PolygonStipple
[span
->y
% 32];
678 GLubyte
*mask
= span
->array
->mask
;
681 ASSERT(ctx
->Polygon
.StippleFlag
);
682 ASSERT((span
->arrayMask
& SPAN_XY
) == 0);
684 m
= highbit
>> (GLuint
) (span
->x
% 32);
686 for (i
= 0; i
< span
->end
; i
++) {
687 if ((m
& stipple
) == 0) {
695 span
->writeAll
= GL_FALSE
;
700 * Clip a pixel span to the current buffer/window boundaries:
701 * DrawBuffer->_Xmin, _Xmax, _Ymin, _Ymax. This will accomplish
702 * window clipping and scissoring.
703 * Return: GL_TRUE some pixels still visible
704 * GL_FALSE nothing visible
707 clip_span( GLcontext
*ctx
, struct sw_span
*span
)
709 const GLint xmin
= ctx
->DrawBuffer
->_Xmin
;
710 const GLint xmax
= ctx
->DrawBuffer
->_Xmax
;
711 const GLint ymin
= ctx
->DrawBuffer
->_Ymin
;
712 const GLint ymax
= ctx
->DrawBuffer
->_Ymax
;
714 if (span
->arrayMask
& SPAN_XY
) {
715 /* arrays of x/y pixel coords */
716 const GLint
*x
= span
->array
->x
;
717 const GLint
*y
= span
->array
->y
;
718 const GLint n
= span
->end
;
719 GLubyte
*mask
= span
->array
->mask
;
721 if (span
->arrayMask
& SPAN_MASK
) {
722 /* note: using & intead of && to reduce branches */
723 for (i
= 0; i
< n
; i
++) {
724 mask
[i
] &= (x
[i
] >= xmin
) & (x
[i
] < xmax
)
725 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
729 /* note: using & intead of && to reduce branches */
730 for (i
= 0; i
< n
; i
++) {
731 mask
[i
] = (x
[i
] >= xmin
) & (x
[i
] < xmax
)
732 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
735 return GL_TRUE
; /* some pixels visible */
738 /* horizontal span of pixels */
739 const GLint x
= span
->x
;
740 const GLint y
= span
->y
;
741 const GLint n
= span
->end
;
743 /* Trivial rejection tests */
744 if (y
< ymin
|| y
>= ymax
|| x
+ n
<= xmin
|| x
>= xmax
) {
746 return GL_FALSE
; /* all pixels clipped */
749 /* Clip to the left */
751 ASSERT(x
+ n
> xmin
);
752 span
->writeAll
= GL_FALSE
;
753 _mesa_bzero(span
->array
->mask
, (xmin
- x
) * sizeof(GLubyte
));
759 span
->end
= xmax
- x
;
762 return GL_TRUE
; /* some pixels visible */
768 * Apply all the per-fragment opertions to a span of color index fragments
769 * and write them to the enabled color drawbuffers.
770 * The 'span' parameter can be considered to be const. Note that
771 * span->interpMask and span->arrayMask may be changed but will be restored
772 * to their original values before returning.
775 _swrast_write_index_span( GLcontext
*ctx
, struct sw_span
*span
)
777 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
778 const struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
779 const GLuint output
= 0;
780 const GLbitfield origInterpMask
= span
->interpMask
;
781 const GLbitfield origArrayMask
= span
->arrayMask
;
784 ASSERT(span
->end
<= MAX_WIDTH
);
785 ASSERT(span
->primitive
== GL_POINT
|| span
->primitive
== GL_LINE
||
786 span
->primitive
== GL_POLYGON
|| span
->primitive
== GL_BITMAP
);
787 ASSERT((span
->interpMask
| span
->arrayMask
) & SPAN_INDEX
);
788 ASSERT((span
->interpMask
& span
->arrayMask
) == 0);
790 if (span
->arrayMask
& SPAN_MASK
) {
791 /* mask was initialized by caller, probably glBitmap */
792 span
->writeAll
= GL_FALSE
;
795 _mesa_memset(span
->array
->mask
, 1, span
->end
);
796 span
->writeAll
= GL_TRUE
;
800 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
801 if (!clip_span(ctx
, span
)) {
806 /* Depth bounds test */
807 if (ctx
->Depth
.BoundsTest
&& ctx
->DrawBuffer
->Visual
.depthBits
> 0) {
808 if (!_swrast_depth_bounds_test(ctx
, span
)) {
814 /* Make sure all fragments are within window bounds */
815 if (span
->arrayMask
& SPAN_XY
) {
817 for (i
= 0; i
< span
->end
; i
++) {
818 if (span
->array
->mask
[i
]) {
819 assert(span
->array
->x
[i
] >= ctx
->DrawBuffer
->_Xmin
);
820 assert(span
->array
->x
[i
] < ctx
->DrawBuffer
->_Xmax
);
821 assert(span
->array
->y
[i
] >= ctx
->DrawBuffer
->_Ymin
);
822 assert(span
->array
->y
[i
] < ctx
->DrawBuffer
->_Ymax
);
828 /* Polygon Stippling */
829 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
830 stipple_polygon_span(ctx
, span
);
833 /* Stencil and Z testing */
834 if (ctx
->Depth
.Test
|| ctx
->Stencil
.Enabled
) {
835 if (span
->interpMask
& SPAN_Z
)
836 _swrast_span_interpolate_z(ctx
, span
);
838 if (ctx
->Stencil
.Enabled
) {
839 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
840 span
->arrayMask
= origArrayMask
;
845 ASSERT(ctx
->Depth
.Test
);
846 if (!_swrast_depth_test_span(ctx
, span
)) {
847 span
->interpMask
= origInterpMask
;
848 span
->arrayMask
= origArrayMask
;
854 #if FEATURE_ARB_occlusion_query
855 if (ctx
->Query
.CurrentOcclusionObject
) {
856 /* update count of 'passed' fragments */
857 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
859 for (i
= 0; i
< span
->end
; i
++)
860 q
->Result
+= span
->array
->mask
[i
];
864 /* we have to wait until after occlusion to do this test */
865 if (ctx
->Color
.DrawBuffer
== GL_NONE
|| ctx
->Color
.IndexMask
== 0) {
866 /* write no pixels */
867 span
->arrayMask
= origArrayMask
;
871 /* Interpolate the color indexes if needed */
872 if (swrast
->_FogEnabled
||
873 ctx
->Color
.IndexLogicOpEnabled
||
874 ctx
->Color
.IndexMask
!= 0xffffffff ||
875 (span
->arrayMask
& SPAN_COVERAGE
)) {
876 if (span
->interpMask
& SPAN_INDEX
) {
877 interpolate_indexes(ctx
, span
);
882 if (swrast
->_FogEnabled
) {
883 _swrast_fog_ci_span(ctx
, span
);
886 /* Antialias coverage application */
887 if (span
->arrayMask
& SPAN_COVERAGE
) {
888 const GLfloat
*coverage
= span
->array
->coverage
;
889 GLuint
*index
= span
->array
->index
;
891 for (i
= 0; i
< span
->end
; i
++) {
892 ASSERT(coverage
[i
] < 16);
893 index
[i
] = (index
[i
] & ~0xf) | ((GLuint
) coverage
[i
]);
897 /* Loop over drawing buffers */
898 for (buf
= 0; buf
< fb
->_NumColorDrawBuffers
[output
]; buf
++) {
899 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[output
][buf
];
900 GLuint indexTemp
[MAX_WIDTH
], *index32
;
902 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
904 if (ctx
->Color
.IndexLogicOpEnabled
||
905 ctx
->Color
.IndexMask
!= 0xffffffff) {
906 /* make copy of incoming indexes */
907 MEMCPY(indexTemp
, span
->array
->index
, span
->end
* sizeof(GLuint
));
909 if (ctx
->Color
.IndexLogicOpEnabled
) {
910 _swrast_logicop_ci_span(ctx
, rb
, span
, indexTemp
);
913 if (ctx
->Color
.IndexMask
!= 0xffffffff) {
914 _swrast_mask_ci_span(ctx
, rb
, span
, indexTemp
);
919 index32
= span
->array
->index
;
922 if ((span
->interpMask
& SPAN_INDEX
) && span
->indexStep
== 0) {
923 /* all fragments have same color index */
929 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
930 index8
= FixedToInt(span
->index
);
933 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
934 index16
= FixedToInt(span
->index
);
938 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
939 index32
= FixedToInt(span
->index
);
943 if (span
->arrayMask
& SPAN_XY
) {
944 rb
->PutMonoValues(ctx
, rb
, span
->end
, span
->array
->x
,
945 span
->array
->y
, value
, span
->array
->mask
);
948 rb
->PutMonoRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
949 value
, span
->array
->mask
);
953 /* each fragment is a different color */
954 GLubyte index8
[MAX_WIDTH
];
955 GLushort index16
[MAX_WIDTH
];
958 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
960 for (k
= 0; k
< span
->end
; k
++) {
961 index8
[k
] = (GLubyte
) index32
[k
];
965 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
967 for (k
= 0; k
< span
->end
; k
++) {
968 index16
[k
] = (GLushort
) index32
[k
];
973 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
977 if (span
->arrayMask
& SPAN_XY
) {
978 rb
->PutValues(ctx
, rb
, span
->end
, span
->array
->x
, span
->array
->y
,
979 values
, span
->array
->mask
);
982 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
,
983 values
, span
->array
->mask
);
988 span
->interpMask
= origInterpMask
;
989 span
->arrayMask
= origArrayMask
;
994 * Add specular color to base color. This is used only when
995 * GL_LIGHT_MODEL_COLOR_CONTROL = GL_SEPARATE_SPECULAR_COLOR.
998 add_colors(GLuint n
, GLchan rgba
[][4], GLchan specular
[][4] )
1001 for (i
= 0; i
< n
; i
++) {
1002 #if CHAN_TYPE == GL_FLOAT
1004 rgba
[i
][RCOMP
] += specular
[i
][RCOMP
];
1005 rgba
[i
][GCOMP
] += specular
[i
][GCOMP
];
1006 rgba
[i
][BCOMP
] += specular
[i
][BCOMP
];
1008 GLint r
= rgba
[i
][RCOMP
] + specular
[i
][RCOMP
];
1009 GLint g
= rgba
[i
][GCOMP
] + specular
[i
][GCOMP
];
1010 GLint b
= rgba
[i
][BCOMP
] + specular
[i
][BCOMP
];
1011 rgba
[i
][RCOMP
] = (GLchan
) MIN2(r
, CHAN_MAX
);
1012 rgba
[i
][GCOMP
] = (GLchan
) MIN2(g
, CHAN_MAX
);
1013 rgba
[i
][BCOMP
] = (GLchan
) MIN2(b
, CHAN_MAX
);
1020 * XXX merge this code into the _swrast_write_rgba_span() routine!
1022 * Draw to more than one RGBA color buffer (or none).
1023 * All fragment operations, up to (but not) blending/logicop should
1024 * have been done first.
1027 multi_write_rgba_span( GLcontext
*ctx
, struct sw_span
*span
)
1029 const GLuint colorMask
= *((GLuint
*) ctx
->Color
.ColorMask
);
1030 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1031 const GLuint output
= 0;
1034 ASSERT(span
->end
< MAX_WIDTH
);
1035 ASSERT(colorMask
!= 0x0);
1037 for (i
= 0; i
< fb
->_NumColorDrawBuffers
[output
]; i
++) {
1038 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[output
][i
];
1039 GLchan rgbaTmp
[MAX_WIDTH
][4];
1041 /* make copy of incoming colors */
1042 MEMCPY( rgbaTmp
, span
->array
->rgba
, 4 * span
->end
* sizeof(GLchan
) );
1044 if (ctx
->Color
._LogicOpEnabled
) {
1045 _swrast_logicop_rgba_span(ctx
, rb
, span
, rgbaTmp
);
1047 else if (ctx
->Color
.BlendEnabled
) {
1048 _swrast_blend_span(ctx
, rb
, span
, rgbaTmp
);
1051 if (colorMask
!= 0xffffffff) {
1052 _swrast_mask_rgba_span(ctx
, rb
, span
, rgbaTmp
);
1055 if (span
->arrayMask
& SPAN_XY
) {
1056 /* array of pixel coords */
1057 ASSERT(rb
->PutValues
);
1058 rb
->PutValues(ctx
, rb
, span
->end
, span
->array
->x
,
1059 span
->array
->y
, rgbaTmp
, span
->array
->mask
);
1062 /* horizontal run of pixels */
1064 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
, rgbaTmp
,
1072 * Apply all the per-fragment operations to a span.
1073 * This now includes texturing (_swrast_write_texture_span() is history).
1074 * This function may modify any of the array values in the span.
1075 * span->interpMask and span->arrayMask may be changed but will be restored
1076 * to their original values before returning.
1079 _swrast_write_rgba_span( GLcontext
*ctx
, struct sw_span
*span
)
1081 const GLuint colorMask
= *((GLuint
*) ctx
->Color
.ColorMask
);
1082 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1083 const GLbitfield origInterpMask
= span
->interpMask
;
1084 const GLbitfield origArrayMask
= span
->arrayMask
;
1085 const GLboolean deferredTexture
= !(ctx
->Color
.AlphaEnabled
||
1086 ctx
->FragmentProgram
._Active
||
1087 ctx
->ShaderObjects
.CurrentProgram
);
1089 ASSERT(span
->primitive
== GL_POINT
|| span
->primitive
== GL_LINE
||
1090 span
->primitive
== GL_POLYGON
|| span
->primitive
== GL_BITMAP
);
1091 ASSERT(span
->end
<= MAX_WIDTH
);
1092 ASSERT((span
->interpMask
& span
->arrayMask
) == 0);
1095 printf("%s() interp 0x%x array 0x%x\n", __FUNCTION__,
1096 span->interpMask, span->arrayMask);
1099 if (span
->arrayMask
& SPAN_MASK
) {
1100 /* mask was initialized by caller, probably glBitmap */
1101 span
->writeAll
= GL_FALSE
;
1104 _mesa_memset(span
->array
->mask
, 1, span
->end
);
1105 span
->writeAll
= GL_TRUE
;
1108 /* Clip to window/scissor box */
1109 if ((swrast
->_RasterMask
& CLIP_BIT
) || (span
->primitive
!= GL_POLYGON
)) {
1110 if (!clip_span(ctx
, span
)) {
1116 /* Make sure all fragments are within window bounds */
1117 if (span
->arrayMask
& SPAN_XY
) {
1119 for (i
= 0; i
< span
->end
; i
++) {
1120 if (span
->array
->mask
[i
]) {
1121 assert(span
->array
->x
[i
] >= ctx
->DrawBuffer
->_Xmin
);
1122 assert(span
->array
->x
[i
] < ctx
->DrawBuffer
->_Xmax
);
1123 assert(span
->array
->y
[i
] >= ctx
->DrawBuffer
->_Ymin
);
1124 assert(span
->array
->y
[i
] < ctx
->DrawBuffer
->_Ymax
);
1130 /* Polygon Stippling */
1131 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
1132 stipple_polygon_span(ctx
, span
);
1135 /* Interpolate texcoords? */
1136 if (ctx
->Texture
._EnabledCoordUnits
1137 && (span
->interpMask
& SPAN_TEXTURE
)
1138 && (span
->arrayMask
& SPAN_TEXTURE
) == 0) {
1139 interpolate_texcoords(ctx
, span
);
1142 /* This is the normal place to compute the resulting fragment color/Z.
1143 * As an optimization, we try to defer this until after Z/stencil
1144 * testing in order to try to avoid computing colors that we won't
1147 if (!deferredTexture
) {
1148 /* Now we need the rgba array, fill it in if needed */
1149 if ((span
->interpMask
& SPAN_RGBA
) && (span
->arrayMask
& SPAN_RGBA
) == 0)
1150 interpolate_colors(ctx
, span
);
1152 if (span
->interpMask
& SPAN_SPEC
)
1153 interpolate_specular(ctx
, span
);
1155 if (span
->interpMask
& SPAN_FOG
)
1156 interpolate_fog(ctx
, span
);
1158 /* Compute fragment colors with fragment program or texture lookups */
1159 if (ctx
->ShaderObjects
.CurrentProgram
!= NULL
) {
1160 if (span
->interpMask
& SPAN_Z
)
1161 _swrast_span_interpolate_z (ctx
, span
);
1162 _swrast_exec_arbshader (ctx
, span
);
1164 else if (ctx
->FragmentProgram
._Active
) {
1165 /* frag prog may need Z values */
1166 if (span
->interpMask
& SPAN_Z
)
1167 _swrast_span_interpolate_z(ctx
, span
);
1168 _swrast_exec_fragment_program( ctx
, span
);
1170 else if (ctx
->ATIFragmentShader
._Enabled
)
1171 _swrast_exec_fragment_shader( ctx
, span
);
1172 else if (ctx
->Texture
._EnabledUnits
&& (span
->arrayMask
& SPAN_TEXTURE
))
1173 _swrast_texture_span( ctx
, span
);
1175 /* Do the alpha test */
1176 if (ctx
->Color
.AlphaEnabled
) {
1177 if (!_swrast_alpha_test(ctx
, span
)) {
1178 span
->arrayMask
= origArrayMask
;
1184 /* Stencil and Z testing */
1185 if (ctx
->Stencil
.Enabled
|| ctx
->Depth
.Test
) {
1186 if (span
->interpMask
& SPAN_Z
)
1187 _swrast_span_interpolate_z(ctx
, span
);
1189 if (ctx
->Stencil
.Enabled
&& ctx
->DrawBuffer
->Visual
.stencilBits
> 0) {
1190 /* Combined Z/stencil tests */
1191 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
1192 span
->interpMask
= origInterpMask
;
1193 span
->arrayMask
= origArrayMask
;
1197 else if (ctx
->DrawBuffer
->Visual
.depthBits
> 0) {
1198 /* Just regular depth testing */
1199 ASSERT(ctx
->Depth
.Test
);
1200 ASSERT(span
->arrayMask
& SPAN_Z
);
1201 if (!_swrast_depth_test_span(ctx
, span
)) {
1202 span
->interpMask
= origInterpMask
;
1203 span
->arrayMask
= origArrayMask
;
1209 #if FEATURE_ARB_occlusion_query
1210 if (ctx
->Query
.CurrentOcclusionObject
) {
1211 /* update count of 'passed' fragments */
1212 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
1214 for (i
= 0; i
< span
->end
; i
++)
1215 q
->Result
+= span
->array
->mask
[i
];
1219 /* We had to wait until now to check for glColorMask(0,0,0,0) because of
1220 * the occlusion test.
1222 if (colorMask
== 0x0) {
1223 span
->interpMask
= origInterpMask
;
1224 span
->arrayMask
= origArrayMask
;
1228 /* If we were able to defer fragment color computation to now, there's
1229 * a good chance that many fragments will have already been killed by
1230 * Z/stencil testing.
1232 if (deferredTexture
) {
1233 /* Now we need the rgba array, fill it in if needed */
1234 if ((span
->interpMask
& SPAN_RGBA
) && (span
->arrayMask
& SPAN_RGBA
) == 0)
1235 interpolate_colors(ctx
, span
);
1237 if (span
->interpMask
& SPAN_SPEC
)
1238 interpolate_specular(ctx
, span
);
1240 if (span
->interpMask
& SPAN_FOG
)
1241 interpolate_fog(ctx
, span
);
1243 if (ctx
->ShaderObjects
.CurrentProgram
!= NULL
) {
1244 if (span
->interpMask
& SPAN_Z
)
1245 _swrast_span_interpolate_z (ctx
, span
);
1246 _swrast_exec_arbshader (ctx
, span
);
1248 else if (ctx
->FragmentProgram
._Active
)
1249 _swrast_exec_fragment_program( ctx
, span
);
1250 else if (ctx
->ATIFragmentShader
._Enabled
)
1251 _swrast_exec_fragment_shader( ctx
, span
);
1252 else if (ctx
->Texture
._EnabledUnits
&& (span
->arrayMask
& SPAN_TEXTURE
))
1253 _swrast_texture_span( ctx
, span
);
1256 ASSERT(span
->arrayMask
& SPAN_RGBA
);
1258 if (!ctx
->FragmentProgram
._Enabled
) {
1259 /* Add base and specular colors */
1260 if (ctx
->Fog
.ColorSumEnabled
||
1261 (ctx
->Light
.Enabled
&&
1262 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)) {
1263 if (span
->interpMask
& SPAN_SPEC
) {
1264 interpolate_specular(ctx
, span
);
1266 if (span
->arrayMask
& SPAN_SPEC
) {
1267 add_colors( span
->end
, span
->array
->rgba
, span
->array
->spec
);
1270 /* We probably added the base/specular colors during the
1278 if (swrast
->_FogEnabled
) {
1279 _swrast_fog_rgba_span(ctx
, span
);
1282 /* Antialias coverage application */
1283 if (span
->arrayMask
& SPAN_COVERAGE
) {
1284 GLchan (*rgba
)[4] = span
->array
->rgba
;
1285 GLfloat
*coverage
= span
->array
->coverage
;
1287 for (i
= 0; i
< span
->end
; i
++) {
1288 rgba
[i
][ACOMP
] = (GLchan
) (rgba
[i
][ACOMP
] * coverage
[i
]);
1292 /* Clamp color/alpha values over the range [0.0, 1.0] before storage */
1293 #if CHAN_TYPE == GL_FLOAT
1294 if (ctx
->Color
.ClampFragmentColor
) {
1295 GLchan (*rgba
)[4] = span
->array
->rgba
;
1297 for (i
= 0; i
< span
->end
; i
++) {
1298 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0, CHAN_MAXF
);
1299 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0, CHAN_MAXF
);
1300 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0, CHAN_MAXF
);
1301 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0, CHAN_MAXF
);
1306 if (swrast
->_RasterMask
& MULTI_DRAW_BIT
) {
1307 /* need to do blend/logicop separately for each color buffer */
1308 multi_write_rgba_span(ctx
, span
);
1311 /* normal: write to exactly one buffer */
1312 struct gl_renderbuffer
*rb
= ctx
->DrawBuffer
->_ColorDrawBuffers
[0][0];
1314 if (ctx
->Color
._LogicOpEnabled
) {
1315 _swrast_logicop_rgba_span(ctx
, rb
, span
, span
->array
->rgba
);
1317 else if (ctx
->Color
.BlendEnabled
) {
1318 _swrast_blend_span(ctx
, rb
, span
, span
->array
->rgba
);
1321 /* Color component masking */
1322 if (colorMask
!= 0xffffffff) {
1323 _swrast_mask_rgba_span(ctx
, rb
, span
, span
->array
->rgba
);
1326 /* Finally, write the pixels to a color buffer */
1327 if (span
->arrayMask
& SPAN_XY
) {
1328 /* array of pixel coords */
1329 ASSERT(rb
->PutValues
);
1330 ASSERT(rb
->_BaseFormat
== GL_RGB
|| rb
->_BaseFormat
== GL_RGBA
);
1331 /* XXX check datatype */
1332 rb
->PutValues(ctx
, rb
, span
->end
, span
->array
->x
, span
->array
->y
,
1333 span
->array
->rgba
, span
->array
->mask
);
1336 /* horizontal run of pixels */
1338 ASSERT(rb
->_BaseFormat
== GL_RGB
|| rb
->_BaseFormat
== GL_RGBA
);
1339 /* XXX check datatype */
1340 rb
->PutRow(ctx
, rb
, span
->end
, span
->x
, span
->y
, span
->array
->rgba
,
1341 span
->writeAll
? NULL
: span
->array
->mask
);
1345 span
->interpMask
= origInterpMask
;
1346 span
->arrayMask
= origArrayMask
;
1352 * Read RGBA pixels from frame buffer. Clipping will be done to prevent
1353 * reading ouside the buffer's boundaries.
1356 _swrast_read_rgba_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1357 GLuint n
, GLint x
, GLint y
, GLchan rgba
[][4] )
1359 const GLint bufWidth
= (GLint
) rb
->Width
;
1360 const GLint bufHeight
= (GLint
) rb
->Height
;
1362 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1363 /* completely above, below, or right */
1364 /* XXX maybe leave rgba values undefined? */
1365 _mesa_bzero(rgba
, 4 * n
* sizeof(GLchan
));
1370 /* left edge clipping */
1372 length
= (GLint
) n
- skip
;
1374 /* completely left of window */
1377 if (length
> bufWidth
) {
1381 else if ((GLint
) (x
+ n
) > bufWidth
) {
1382 /* right edge clipping */
1384 length
= bufWidth
- x
;
1386 /* completely to right of window */
1398 ASSERT(rb
->_BaseFormat
== GL_RGB
|| rb
->_BaseFormat
== GL_RGBA
);
1399 ASSERT(rb
->DataType
== GL_UNSIGNED_BYTE
);
1400 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, rgba
+ skip
);
1406 * Read CI pixels from frame buffer. Clipping will be done to prevent
1407 * reading ouside the buffer's boundaries.
1410 _swrast_read_index_span( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1411 GLuint n
, GLint x
, GLint y
, GLuint index
[] )
1413 const GLint bufWidth
= (GLint
) rb
->Width
;
1414 const GLint bufHeight
= (GLint
) rb
->Height
;
1416 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1417 /* completely above, below, or right */
1418 _mesa_bzero(index
, n
* sizeof(GLuint
));
1423 /* left edge clipping */
1425 length
= (GLint
) n
- skip
;
1427 /* completely left of window */
1430 if (length
> bufWidth
) {
1434 else if ((GLint
) (x
+ n
) > bufWidth
) {
1435 /* right edge clipping */
1437 length
= bufWidth
- x
;
1439 /* completely to right of window */
1450 ASSERT(rb
->_BaseFormat
== GL_COLOR_INDEX
);
1452 if (rb
->DataType
== GL_UNSIGNED_BYTE
) {
1453 GLubyte index8
[MAX_WIDTH
];
1455 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index8
);
1456 for (i
= 0; i
< length
; i
++)
1457 index
[skip
+ i
] = index8
[i
];
1459 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1460 GLushort index16
[MAX_WIDTH
];
1462 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index16
);
1463 for (i
= 0; i
< length
; i
++)
1464 index
[skip
+ i
] = index16
[i
];
1466 else if (rb
->DataType
== GL_UNSIGNED_INT
) {
1467 rb
->GetRow(ctx
, rb
, length
, x
+ skip
, y
, index
+ skip
);
1474 * Wrapper for gl_renderbuffer::GetValues() which does clipping to avoid
1475 * reading values outside the buffer bounds.
1476 * We can use this for reading any format/type of renderbuffer.
1477 * \param valueSize is the size in bytes of each value put into the
1481 _swrast_get_values(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1482 GLuint count
, const GLint x
[], const GLint y
[],
1483 void *values
, GLuint valueSize
)
1485 GLuint i
, inCount
= 0, inStart
= 0;
1487 for (i
= 0; i
< count
; i
++) {
1488 if (x
[i
] >= 0 && y
[i
] >= 0 && x
[i
] < rb
->Width
&& y
[i
] < rb
->Height
) {
1496 /* read [inStart, inStart + inCount) */
1497 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1498 (GLubyte
*) values
+ inStart
* valueSize
);
1504 /* read last values */
1505 rb
->GetValues(ctx
, rb
, inCount
, x
+ inStart
, y
+ inStart
,
1506 (GLubyte
*) values
+ inStart
* valueSize
);
1512 * Wrapper for gl_renderbuffer::PutRow() which does clipping.
1515 _swrast_put_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1516 GLuint count
, GLint x
, GLint y
,
1517 const GLvoid
*values
, GLuint valueSize
)
1521 if (y
< 0 || y
>= rb
->Height
)
1522 return; /* above or below */
1524 if (x
+ (GLint
) count
<= 0 || x
>= rb
->Width
)
1525 return; /* entirely left or right */
1527 if (x
+ count
> rb
->Width
) {
1529 GLint clip
= x
+ count
- rb
->Width
;
1540 rb
->PutRow(ctx
, rb
, count
, x
, y
,
1541 (const GLubyte
*) values
+ skip
* valueSize
, NULL
);
1546 * Wrapper for gl_renderbuffer::GetRow() which does clipping.
1549 _swrast_get_row(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1550 GLuint count
, GLint x
, GLint y
,
1551 GLvoid
*values
, GLuint valueSize
)
1555 if (y
< 0 || y
>= rb
->Height
)
1556 return; /* above or below */
1558 if (x
+ (GLint
) count
<= 0 || x
>= rb
->Width
)
1559 return; /* entirely left or right */
1561 if (x
+ count
> rb
->Width
) {
1563 GLint clip
= x
+ count
- rb
->Width
;
1574 rb
->GetRow(ctx
, rb
, count
, x
, y
, (GLubyte
*) values
+ skip
* valueSize
);