2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 2009 VMware, Inc. 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 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
28 * \file swrast/s_span.c
29 * \brief Span processing functions used by all rasterization functions.
30 * This is where all the per-fragment tests are performed
35 #include "main/errors.h"
36 #include "main/glheader.h"
37 #include "main/format_pack.h"
38 #include "main/format_unpack.h"
39 #include "main/macros.h"
41 #include "main/image.h"
42 #include "main/samplerobj.h"
43 #include "main/state.h"
44 #include "main/stencil.h"
45 #include "main/teximage.h"
47 #include "s_atifragshader.h"
50 #include "s_context.h"
54 #include "s_masking.h"
55 #include "s_fragprog.h"
57 #include "s_stencil.h"
58 #include "s_texcombine.h"
63 * Set default fragment attributes for the span using the
64 * current raster values. Used prior to glDraw/CopyPixels
68 _swrast_span_default_attribs(struct gl_context
*ctx
, SWspan
*span
)
73 const GLfloat depthMax
= ctx
->DrawBuffer
->_DepthMaxF
;
74 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16)
75 span
->z
= FloatToFixed(ctx
->Current
.RasterPos
[2] * depthMax
+ 0.5F
);
77 GLfloat tmpf
= ctx
->Current
.RasterPos
[2] * depthMax
;
78 tmpf
= MIN2(tmpf
, depthMax
);
79 span
->z
= (GLint
)tmpf
;
82 span
->interpMask
|= SPAN_Z
;
85 /* W (for perspective correction) */
86 span
->attrStart
[VARYING_SLOT_POS
][3] = 1.0;
87 span
->attrStepX
[VARYING_SLOT_POS
][3] = 0.0;
88 span
->attrStepY
[VARYING_SLOT_POS
][3] = 0.0;
90 /* primary color, or color index */
91 UNCLAMPED_FLOAT_TO_CHAN(r
, ctx
->Current
.RasterColor
[0]);
92 UNCLAMPED_FLOAT_TO_CHAN(g
, ctx
->Current
.RasterColor
[1]);
93 UNCLAMPED_FLOAT_TO_CHAN(b
, ctx
->Current
.RasterColor
[2]);
94 UNCLAMPED_FLOAT_TO_CHAN(a
, ctx
->Current
.RasterColor
[3]);
95 #if CHAN_TYPE == GL_FLOAT
101 span
->red
= IntToFixed(r
);
102 span
->green
= IntToFixed(g
);
103 span
->blue
= IntToFixed(b
);
104 span
->alpha
= IntToFixed(a
);
110 span
->interpMask
|= SPAN_RGBA
;
112 COPY_4V(span
->attrStart
[VARYING_SLOT_COL0
], ctx
->Current
.RasterColor
);
113 ASSIGN_4V(span
->attrStepX
[VARYING_SLOT_COL0
], 0.0, 0.0, 0.0, 0.0);
114 ASSIGN_4V(span
->attrStepY
[VARYING_SLOT_COL0
], 0.0, 0.0, 0.0, 0.0);
116 /* Secondary color */
117 if (ctx
->Light
.Enabled
|| ctx
->Fog
.ColorSumEnabled
)
119 COPY_4V(span
->attrStart
[VARYING_SLOT_COL1
], ctx
->Current
.RasterSecondaryColor
);
120 ASSIGN_4V(span
->attrStepX
[VARYING_SLOT_COL1
], 0.0, 0.0, 0.0, 0.0);
121 ASSIGN_4V(span
->attrStepY
[VARYING_SLOT_COL1
], 0.0, 0.0, 0.0, 0.0);
126 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
127 GLfloat fogVal
; /* a coord or a blend factor */
128 if (swrast
->_PreferPixelFog
) {
129 /* fog blend factors will be computed from fog coordinates per pixel */
130 fogVal
= ctx
->Current
.RasterDistance
;
133 /* fog blend factor should be computed from fogcoord now */
134 fogVal
= _swrast_z_to_fogfactor(ctx
, ctx
->Current
.RasterDistance
);
136 span
->attrStart
[VARYING_SLOT_FOGC
][0] = fogVal
;
137 span
->attrStepX
[VARYING_SLOT_FOGC
][0] = 0.0;
138 span
->attrStepY
[VARYING_SLOT_FOGC
][0] = 0.0;
144 for (i
= 0; i
< ctx
->Const
.MaxTextureCoordUnits
; i
++) {
145 const GLuint attr
= VARYING_SLOT_TEX0
+ i
;
146 const GLfloat
*tc
= ctx
->Current
.RasterTexCoords
[i
];
147 if (_swrast_use_fragment_program(ctx
) ||
148 _mesa_ati_fragment_shader_enabled(ctx
)) {
149 COPY_4V(span
->attrStart
[attr
], tc
);
151 else if (tc
[3] > 0.0F
) {
152 /* use (s/q, t/q, r/q, 1) */
153 span
->attrStart
[attr
][0] = tc
[0] / tc
[3];
154 span
->attrStart
[attr
][1] = tc
[1] / tc
[3];
155 span
->attrStart
[attr
][2] = tc
[2] / tc
[3];
156 span
->attrStart
[attr
][3] = 1.0;
159 ASSIGN_4V(span
->attrStart
[attr
], 0.0F
, 0.0F
, 0.0F
, 1.0F
);
161 ASSIGN_4V(span
->attrStepX
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
162 ASSIGN_4V(span
->attrStepY
[attr
], 0.0F
, 0.0F
, 0.0F
, 0.0F
);
169 * Interpolate the active attributes (and'd with attrMask) to
170 * fill in span->array->attribs[].
171 * Perspective correction will be done. The point/line/triangle function
172 * should have computed attrStart/Step values for VARYING_SLOT_POS[3]!
175 interpolate_active_attribs(struct gl_context
*ctx
, SWspan
*span
,
176 GLbitfield64 attrMask
)
178 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
181 * Don't overwrite existing array values, such as colors that may have
182 * been produced by glDraw/CopyPixels.
184 attrMask
&= ~span
->arrayAttribs
;
187 if (attrMask
& BITFIELD64_BIT(attr
)) {
188 const GLfloat dwdx
= span
->attrStepX
[VARYING_SLOT_POS
][3];
189 GLfloat w
= span
->attrStart
[VARYING_SLOT_POS
][3];
190 const GLfloat dv0dx
= span
->attrStepX
[attr
][0];
191 const GLfloat dv1dx
= span
->attrStepX
[attr
][1];
192 const GLfloat dv2dx
= span
->attrStepX
[attr
][2];
193 const GLfloat dv3dx
= span
->attrStepX
[attr
][3];
194 GLfloat v0
= span
->attrStart
[attr
][0] + span
->leftClip
* dv0dx
;
195 GLfloat v1
= span
->attrStart
[attr
][1] + span
->leftClip
* dv1dx
;
196 GLfloat v2
= span
->attrStart
[attr
][2] + span
->leftClip
* dv2dx
;
197 GLfloat v3
= span
->attrStart
[attr
][3] + span
->leftClip
* dv3dx
;
199 for (k
= 0; k
< span
->end
; k
++) {
200 const GLfloat invW
= 1.0f
/ w
;
201 span
->array
->attribs
[attr
][k
][0] = v0
* invW
;
202 span
->array
->attribs
[attr
][k
][1] = v1
* invW
;
203 span
->array
->attribs
[attr
][k
][2] = v2
* invW
;
204 span
->array
->attribs
[attr
][k
][3] = v3
* invW
;
211 assert((span
->arrayAttribs
& BITFIELD64_BIT(attr
)) == 0);
212 span
->arrayAttribs
|= BITFIELD64_BIT(attr
);
219 * Interpolate primary colors to fill in the span->array->rgba8 (or rgb16)
223 interpolate_int_colors(struct gl_context
*ctx
, SWspan
*span
)
226 const GLuint n
= span
->end
;
229 assert(!(span
->arrayMask
& SPAN_RGBA
));
232 switch (span
->array
->ChanType
) {
234 case GL_UNSIGNED_BYTE
:
236 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
237 if (span
->interpMask
& SPAN_FLAT
) {
239 color
[RCOMP
] = FixedToInt(span
->red
);
240 color
[GCOMP
] = FixedToInt(span
->green
);
241 color
[BCOMP
] = FixedToInt(span
->blue
);
242 color
[ACOMP
] = FixedToInt(span
->alpha
);
243 for (i
= 0; i
< n
; i
++) {
244 COPY_4UBV(rgba
[i
], color
);
248 GLfixed r
= span
->red
;
249 GLfixed g
= span
->green
;
250 GLfixed b
= span
->blue
;
251 GLfixed a
= span
->alpha
;
252 GLint dr
= span
->redStep
;
253 GLint dg
= span
->greenStep
;
254 GLint db
= span
->blueStep
;
255 GLint da
= span
->alphaStep
;
256 for (i
= 0; i
< n
; i
++) {
257 rgba
[i
][RCOMP
] = FixedToChan(r
);
258 rgba
[i
][GCOMP
] = FixedToChan(g
);
259 rgba
[i
][BCOMP
] = FixedToChan(b
);
260 rgba
[i
][ACOMP
] = FixedToChan(a
);
269 case GL_UNSIGNED_SHORT
:
271 GLushort (*rgba
)[4] = span
->array
->rgba16
;
272 if (span
->interpMask
& SPAN_FLAT
) {
274 color
[RCOMP
] = FixedToInt(span
->red
);
275 color
[GCOMP
] = FixedToInt(span
->green
);
276 color
[BCOMP
] = FixedToInt(span
->blue
);
277 color
[ACOMP
] = FixedToInt(span
->alpha
);
278 for (i
= 0; i
< n
; i
++) {
279 COPY_4V(rgba
[i
], color
);
283 GLushort (*rgba
)[4] = span
->array
->rgba16
;
285 GLint dr
, dg
, db
, da
;
291 dg
= span
->greenStep
;
293 da
= span
->alphaStep
;
294 for (i
= 0; i
< n
; i
++) {
295 rgba
[i
][RCOMP
] = FixedToChan(r
);
296 rgba
[i
][GCOMP
] = FixedToChan(g
);
297 rgba
[i
][BCOMP
] = FixedToChan(b
);
298 rgba
[i
][ACOMP
] = FixedToChan(a
);
309 interpolate_active_attribs(ctx
, span
, VARYING_BIT_COL0
);
312 _mesa_problem(ctx
, "bad datatype 0x%x in interpolate_int_colors",
313 span
->array
->ChanType
);
315 span
->arrayMask
|= SPAN_RGBA
;
320 * Populate the VARYING_SLOT_COL0 array.
323 interpolate_float_colors(SWspan
*span
)
325 GLfloat (*col0
)[4] = span
->array
->attribs
[VARYING_SLOT_COL0
];
326 const GLuint n
= span
->end
;
329 assert(!(span
->arrayAttribs
& VARYING_BIT_COL0
));
331 if (span
->arrayMask
& SPAN_RGBA
) {
332 /* convert array of int colors */
333 for (i
= 0; i
< n
; i
++) {
334 col0
[i
][0] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][0]);
335 col0
[i
][1] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][1]);
336 col0
[i
][2] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][2]);
337 col0
[i
][3] = UBYTE_TO_FLOAT(span
->array
->rgba8
[i
][3]);
341 /* interpolate red/green/blue/alpha to get float colors */
342 assert(span
->interpMask
& SPAN_RGBA
);
343 if (span
->interpMask
& SPAN_FLAT
) {
344 GLfloat r
= FixedToFloat(span
->red
);
345 GLfloat g
= FixedToFloat(span
->green
);
346 GLfloat b
= FixedToFloat(span
->blue
);
347 GLfloat a
= FixedToFloat(span
->alpha
);
348 for (i
= 0; i
< n
; i
++) {
349 ASSIGN_4V(col0
[i
], r
, g
, b
, a
);
353 GLfloat r
= FixedToFloat(span
->red
);
354 GLfloat g
= FixedToFloat(span
->green
);
355 GLfloat b
= FixedToFloat(span
->blue
);
356 GLfloat a
= FixedToFloat(span
->alpha
);
357 GLfloat dr
= FixedToFloat(span
->redStep
);
358 GLfloat dg
= FixedToFloat(span
->greenStep
);
359 GLfloat db
= FixedToFloat(span
->blueStep
);
360 GLfloat da
= FixedToFloat(span
->alphaStep
);
361 for (i
= 0; i
< n
; i
++) {
374 span
->arrayAttribs
|= VARYING_BIT_COL0
;
375 span
->array
->ChanType
= GL_FLOAT
;
381 * Fill in the span.zArray array from the span->z, zStep values.
384 _swrast_span_interpolate_z( const struct gl_context
*ctx
, SWspan
*span
)
386 const GLuint n
= span
->end
;
389 assert(!(span
->arrayMask
& SPAN_Z
));
391 if (ctx
->DrawBuffer
->Visual
.depthBits
<= 16) {
392 GLfixed zval
= span
->z
;
393 GLuint
*z
= span
->array
->z
;
394 for (i
= 0; i
< n
; i
++) {
395 z
[i
] = FixedToInt(zval
);
400 /* Deep Z buffer, no fixed->int shift */
401 GLuint zval
= span
->z
;
402 GLuint
*z
= span
->array
->z
;
403 for (i
= 0; i
< n
; i
++) {
408 span
->interpMask
&= ~SPAN_Z
;
409 span
->arrayMask
|= SPAN_Z
;
414 * Compute mipmap LOD from partial derivatives.
415 * This the ideal solution, as given in the OpenGL spec.
418 _swrast_compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
419 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
420 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
422 GLfloat dudx
= texW
* ((s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
);
423 GLfloat dvdx
= texH
* ((t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
);
424 GLfloat dudy
= texW
* ((s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
);
425 GLfloat dvdy
= texH
* ((t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
);
426 GLfloat x
= sqrtf(dudx
* dudx
+ dvdx
* dvdx
);
427 GLfloat y
= sqrtf(dudy
* dudy
+ dvdy
* dvdy
);
428 GLfloat rho
= MAX2(x
, y
);
429 GLfloat lambda
= util_fast_log2(rho
);
435 * Compute mipmap LOD from partial derivatives.
436 * This is a faster approximation than above function.
440 _swrast_compute_lambda(GLfloat dsdx
, GLfloat dsdy
, GLfloat dtdx
, GLfloat dtdy
,
441 GLfloat dqdx
, GLfloat dqdy
, GLfloat texW
, GLfloat texH
,
442 GLfloat s
, GLfloat t
, GLfloat q
, GLfloat invQ
)
444 GLfloat dsdx2
= (s
+ dsdx
) / (q
+ dqdx
) - s
* invQ
;
445 GLfloat dtdx2
= (t
+ dtdx
) / (q
+ dqdx
) - t
* invQ
;
446 GLfloat dsdy2
= (s
+ dsdy
) / (q
+ dqdy
) - s
* invQ
;
447 GLfloat dtdy2
= (t
+ dtdy
) / (q
+ dqdy
) - t
* invQ
;
448 GLfloat maxU
, maxV
, rho
, lambda
;
449 dsdx2
= fabsf(dsdx2
);
450 dsdy2
= fabsf(dsdy2
);
451 dtdx2
= fabsf(dtdx2
);
452 dtdy2
= fabsf(dtdy2
);
453 maxU
= MAX2(dsdx2
, dsdy2
) * texW
;
454 maxV
= MAX2(dtdx2
, dtdy2
) * texH
;
455 rho
= MAX2(maxU
, maxV
);
456 lambda
= util_fast_log2(rho
);
463 * Fill in the span.array->attrib[VARYING_SLOT_TEXn] arrays from the
464 * using the attrStart/Step values.
466 * This function only used during fixed-function fragment processing.
468 * Note: in the places where we divide by Q (or mult by invQ) we're
469 * really doing two things: perspective correction and texcoord
470 * projection. Remember, for texcoord (s,t,r,q) we need to index
471 * texels with (s/q, t/q, r/q).
474 interpolate_texcoords(struct gl_context
*ctx
, SWspan
*span
)
477 = (ctx
->Texture
._EnabledCoordUnits
> 1) ? ctx
->Const
.MaxTextureUnits
: 1;
480 /* XXX CoordUnits vs. ImageUnits */
481 for (u
= 0; u
< maxUnit
; u
++) {
482 if (ctx
->Texture
._EnabledCoordUnits
& (1 << u
)) {
483 const GLuint attr
= VARYING_SLOT_TEX0
+ u
;
484 const struct gl_texture_object
*obj
= ctx
->Texture
.Unit
[u
]._Current
;
486 GLboolean needLambda
;
487 GLfloat (*texcoord
)[4] = span
->array
->attribs
[attr
];
488 GLfloat
*lambda
= span
->array
->lambda
[u
];
489 const GLfloat dsdx
= span
->attrStepX
[attr
][0];
490 const GLfloat dsdy
= span
->attrStepY
[attr
][0];
491 const GLfloat dtdx
= span
->attrStepX
[attr
][1];
492 const GLfloat dtdy
= span
->attrStepY
[attr
][1];
493 const GLfloat drdx
= span
->attrStepX
[attr
][2];
494 const GLfloat dqdx
= span
->attrStepX
[attr
][3];
495 const GLfloat dqdy
= span
->attrStepY
[attr
][3];
496 GLfloat s
= span
->attrStart
[attr
][0] + span
->leftClip
* dsdx
;
497 GLfloat t
= span
->attrStart
[attr
][1] + span
->leftClip
* dtdx
;
498 GLfloat r
= span
->attrStart
[attr
][2] + span
->leftClip
* drdx
;
499 GLfloat q
= span
->attrStart
[attr
][3] + span
->leftClip
* dqdx
;
502 const struct gl_texture_image
*img
= _mesa_base_tex_image(obj
);
503 const struct swrast_texture_image
*swImg
=
504 swrast_texture_image_const(img
);
505 const struct gl_sampler_object
*samp
= _mesa_get_samplerobj(ctx
, u
);
507 needLambda
= (samp
->MinFilter
!= samp
->MagFilter
)
508 || _swrast_use_fragment_program(ctx
);
509 /* LOD is calculated directly in the ansiotropic filter, we can
510 * skip the normal lambda function as the result is ignored.
512 if (samp
->MaxAnisotropy
> 1.0F
&&
513 samp
->MinFilter
== GL_LINEAR_MIPMAP_LINEAR
) {
514 needLambda
= GL_FALSE
;
516 texW
= swImg
->WidthScale
;
517 texH
= swImg
->HeightScale
;
520 /* using a fragment program */
523 needLambda
= GL_FALSE
;
528 if (_swrast_use_fragment_program(ctx
)
529 || _mesa_ati_fragment_shader_enabled(ctx
)) {
530 /* do perspective correction but don't divide s, t, r by q */
531 const GLfloat dwdx
= span
->attrStepX
[VARYING_SLOT_POS
][3];
532 GLfloat w
= span
->attrStart
[VARYING_SLOT_POS
][3] + span
->leftClip
* dwdx
;
533 for (i
= 0; i
< span
->end
; i
++) {
534 const GLfloat invW
= 1.0F
/ w
;
535 texcoord
[i
][0] = s
* invW
;
536 texcoord
[i
][1] = t
* invW
;
537 texcoord
[i
][2] = r
* invW
;
538 texcoord
[i
][3] = q
* invW
;
539 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
540 dqdx
, dqdy
, texW
, texH
,
550 for (i
= 0; i
< span
->end
; i
++) {
551 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
552 texcoord
[i
][0] = s
* invQ
;
553 texcoord
[i
][1] = t
* invQ
;
554 texcoord
[i
][2] = r
* invQ
;
556 lambda
[i
] = _swrast_compute_lambda(dsdx
, dsdy
, dtdx
, dtdy
,
557 dqdx
, dqdy
, texW
, texH
,
565 span
->arrayMask
|= SPAN_LAMBDA
;
569 if (_swrast_use_fragment_program(ctx
) ||
570 _mesa_ati_fragment_shader_enabled(ctx
)) {
571 /* do perspective correction but don't divide s, t, r by q */
572 const GLfloat dwdx
= span
->attrStepX
[VARYING_SLOT_POS
][3];
573 GLfloat w
= span
->attrStart
[VARYING_SLOT_POS
][3] + span
->leftClip
* dwdx
;
574 for (i
= 0; i
< span
->end
; i
++) {
575 const GLfloat invW
= 1.0F
/ w
;
576 texcoord
[i
][0] = s
* invW
;
577 texcoord
[i
][1] = t
* invW
;
578 texcoord
[i
][2] = r
* invW
;
579 texcoord
[i
][3] = q
* invW
;
588 else if (dqdx
== 0.0F
) {
589 /* Ortho projection or polygon's parallel to window X axis */
590 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
591 for (i
= 0; i
< span
->end
; i
++) {
592 texcoord
[i
][0] = s
* invQ
;
593 texcoord
[i
][1] = t
* invQ
;
594 texcoord
[i
][2] = r
* invQ
;
603 for (i
= 0; i
< span
->end
; i
++) {
604 const GLfloat invQ
= (q
== 0.0F
) ? 1.0F
: (1.0F
/ q
);
605 texcoord
[i
][0] = s
* invQ
;
606 texcoord
[i
][1] = t
* invQ
;
607 texcoord
[i
][2] = r
* invQ
;
623 * Fill in the arrays->attribs[VARYING_SLOT_POS] array.
626 interpolate_wpos(struct gl_context
*ctx
, SWspan
*span
)
628 GLfloat (*wpos
)[4] = span
->array
->attribs
[VARYING_SLOT_POS
];
630 const GLfloat zScale
= 1.0F
/ ctx
->DrawBuffer
->_DepthMaxF
;
633 if (span
->arrayMask
& SPAN_XY
) {
634 for (i
= 0; i
< span
->end
; i
++) {
635 wpos
[i
][0] = (GLfloat
) span
->array
->x
[i
];
636 wpos
[i
][1] = (GLfloat
) span
->array
->y
[i
];
640 for (i
= 0; i
< span
->end
; i
++) {
641 wpos
[i
][0] = (GLfloat
) span
->x
+ i
;
642 wpos
[i
][1] = (GLfloat
) span
->y
;
646 dw
= span
->attrStepX
[VARYING_SLOT_POS
][3];
647 w
= span
->attrStart
[VARYING_SLOT_POS
][3] + span
->leftClip
* dw
;
648 for (i
= 0; i
< span
->end
; i
++) {
649 wpos
[i
][2] = (GLfloat
) span
->array
->z
[i
] * zScale
;
657 * Apply the current polygon stipple pattern to a span of pixels.
660 stipple_polygon_span(struct gl_context
*ctx
, SWspan
*span
)
662 GLubyte
*mask
= span
->array
->mask
;
664 assert(ctx
->Polygon
.StippleFlag
);
666 if (span
->arrayMask
& SPAN_XY
) {
667 /* arrays of x/y pixel coords */
669 for (i
= 0; i
< span
->end
; i
++) {
670 const GLint col
= span
->array
->x
[i
] % 32;
671 const GLint row
= span
->array
->y
[i
] % 32;
672 const GLuint stipple
= ctx
->PolygonStipple
[row
];
673 if (((1 << col
) & stipple
) == 0) {
679 /* horizontal span of pixels */
680 const GLuint highBit
= 1 << 31;
681 const GLuint stipple
= ctx
->PolygonStipple
[span
->y
% 32];
682 GLuint i
, m
= highBit
>> (GLuint
) (span
->x
% 32);
683 for (i
= 0; i
< span
->end
; i
++) {
684 if ((m
& stipple
) == 0) {
693 span
->writeAll
= GL_FALSE
;
698 * Clip a pixel span to the current buffer/window boundaries:
699 * DrawBuffer->_Xmin, _Xmax, _Ymin, _Ymax. This will accomplish
700 * window clipping and scissoring.
701 * Return: GL_TRUE some pixels still visible
702 * GL_FALSE nothing visible
705 clip_span( struct gl_context
*ctx
, SWspan
*span
)
707 const GLint xmin
= ctx
->DrawBuffer
->_Xmin
;
708 const GLint xmax
= ctx
->DrawBuffer
->_Xmax
;
709 const GLint ymin
= ctx
->DrawBuffer
->_Ymin
;
710 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
;
722 if (span
->arrayMask
& SPAN_MASK
) {
723 /* note: using & intead of && to reduce branches */
724 for (i
= 0; i
< n
; i
++) {
725 mask
[i
] &= (x
[i
] >= xmin
) & (x
[i
] < xmax
)
726 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
731 /* note: using & intead of && to reduce branches */
732 for (i
= 0; i
< n
; i
++) {
733 mask
[i
] = (x
[i
] >= xmin
) & (x
[i
] < xmax
)
734 & (y
[i
] >= ymin
) & (y
[i
] < ymax
);
741 /* horizontal span of pixels */
742 const GLint x
= span
->x
;
743 const GLint y
= span
->y
;
746 /* Trivial rejection tests */
747 if (y
< ymin
|| y
>= ymax
|| x
+ n
<= xmin
|| x
>= xmax
) {
749 return GL_FALSE
; /* all pixels clipped */
755 n
= span
->end
= xmax
- x
;
758 /* Clip to the left */
760 const GLint leftClip
= xmin
- x
;
763 assert(leftClip
> 0);
764 assert(x
+ n
> xmin
);
766 /* Clip 'leftClip' pixels from the left side.
767 * The span->leftClip field will be applied when we interpolate
768 * fragment attributes.
769 * For arrays of values, shift them left.
771 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
772 if (span
->interpMask
& (1u << i
)) {
774 for (j
= 0; j
< 4; j
++) {
775 span
->attrStart
[i
][j
] += leftClip
* span
->attrStepX
[i
][j
];
780 span
->red
+= leftClip
* span
->redStep
;
781 span
->green
+= leftClip
* span
->greenStep
;
782 span
->blue
+= leftClip
* span
->blueStep
;
783 span
->alpha
+= leftClip
* span
->alphaStep
;
784 span
->index
+= leftClip
* span
->indexStep
;
785 span
->z
+= leftClip
* span
->zStep
;
786 span
->intTex
[0] += leftClip
* span
->intTexStep
[0];
787 span
->intTex
[1] += leftClip
* span
->intTexStep
[1];
789 #define SHIFT_ARRAY(ARRAY, SHIFT, LEN) \
790 memmove(ARRAY, ARRAY + (SHIFT), (LEN) * sizeof(ARRAY[0]))
792 for (i
= 0; i
< VARYING_SLOT_MAX
; i
++) {
793 if (span
->arrayAttribs
& BITFIELD64_BIT(i
)) {
794 /* shift array elements left by 'leftClip' */
795 SHIFT_ARRAY(span
->array
->attribs
[i
], leftClip
, n
- leftClip
);
799 SHIFT_ARRAY(span
->array
->mask
, leftClip
, n
- leftClip
);
800 SHIFT_ARRAY(span
->array
->rgba8
, leftClip
, n
- leftClip
);
801 SHIFT_ARRAY(span
->array
->rgba16
, leftClip
, n
- leftClip
);
802 SHIFT_ARRAY(span
->array
->x
, leftClip
, n
- leftClip
);
803 SHIFT_ARRAY(span
->array
->y
, leftClip
, n
- leftClip
);
804 SHIFT_ARRAY(span
->array
->z
, leftClip
, n
- leftClip
);
805 SHIFT_ARRAY(span
->array
->index
, leftClip
, n
- leftClip
);
806 for (i
= 0; i
< MAX_TEXTURE_COORD_UNITS
; i
++) {
807 SHIFT_ARRAY(span
->array
->lambda
[i
], leftClip
, n
- leftClip
);
809 SHIFT_ARRAY(span
->array
->coverage
, leftClip
, n
- leftClip
);
813 span
->leftClip
= leftClip
;
815 span
->end
-= leftClip
;
816 span
->writeAll
= GL_FALSE
;
819 assert(span
->x
>= xmin
);
820 assert(span
->x
+ span
->end
<= xmax
);
821 assert(span
->y
>= ymin
);
822 assert(span
->y
< ymax
);
824 return GL_TRUE
; /* some pixels visible */
830 * Add specular colors to primary colors.
831 * Only called during fixed-function operation.
832 * Result is float color array (VARYING_SLOT_COL0).
835 add_specular(struct gl_context
*ctx
, SWspan
*span
)
837 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
838 const GLubyte
*mask
= span
->array
->mask
;
839 GLfloat (*col0
)[4] = span
->array
->attribs
[VARYING_SLOT_COL0
];
840 GLfloat (*col1
)[4] = span
->array
->attribs
[VARYING_SLOT_COL1
];
843 assert(!_swrast_use_fragment_program(ctx
));
844 assert(span
->arrayMask
& SPAN_RGBA
);
845 assert(swrast
->_ActiveAttribMask
& VARYING_BIT_COL1
);
846 (void) swrast
; /* silence warning */
848 if (span
->array
->ChanType
== GL_FLOAT
) {
849 if ((span
->arrayAttribs
& VARYING_BIT_COL0
) == 0) {
850 interpolate_active_attribs(ctx
, span
, VARYING_BIT_COL0
);
854 /* need float colors */
855 if ((span
->arrayAttribs
& VARYING_BIT_COL0
) == 0) {
856 interpolate_float_colors(span
);
860 if ((span
->arrayAttribs
& VARYING_BIT_COL1
) == 0) {
861 /* XXX could avoid this and interpolate COL1 in the loop below */
862 interpolate_active_attribs(ctx
, span
, VARYING_BIT_COL1
);
865 assert(span
->arrayAttribs
& VARYING_BIT_COL0
);
866 assert(span
->arrayAttribs
& VARYING_BIT_COL1
);
868 for (i
= 0; i
< span
->end
; i
++) {
870 col0
[i
][0] += col1
[i
][0];
871 col0
[i
][1] += col1
[i
][1];
872 col0
[i
][2] += col1
[i
][2];
876 span
->array
->ChanType
= GL_FLOAT
;
881 * Apply antialiasing coverage value to alpha values.
884 apply_aa_coverage(SWspan
*span
)
886 const GLfloat
*coverage
= span
->array
->coverage
;
888 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
889 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
890 for (i
= 0; i
< span
->end
; i
++) {
891 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
892 rgba
[i
][ACOMP
] = (GLubyte
) CLAMP(a
, 0.0F
, 255.0F
);
893 assert(coverage
[i
] >= 0.0F
);
894 assert(coverage
[i
] <= 1.0F
);
897 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
898 GLushort (*rgba
)[4] = span
->array
->rgba16
;
899 for (i
= 0; i
< span
->end
; i
++) {
900 const GLfloat a
= rgba
[i
][ACOMP
] * coverage
[i
];
901 rgba
[i
][ACOMP
] = (GLushort
) CLAMP(a
, 0.0F
, 65535.0F
);
905 GLfloat (*rgba
)[4] = span
->array
->attribs
[VARYING_SLOT_COL0
];
906 for (i
= 0; i
< span
->end
; i
++) {
907 rgba
[i
][ACOMP
] = rgba
[i
][ACOMP
] * coverage
[i
];
915 * Clamp span's float colors to [0,1]
918 clamp_colors(SWspan
*span
)
920 GLfloat (*rgba
)[4] = span
->array
->attribs
[VARYING_SLOT_COL0
];
922 assert(span
->array
->ChanType
== GL_FLOAT
);
923 for (i
= 0; i
< span
->end
; i
++) {
924 rgba
[i
][RCOMP
] = CLAMP(rgba
[i
][RCOMP
], 0.0F
, 1.0F
);
925 rgba
[i
][GCOMP
] = CLAMP(rgba
[i
][GCOMP
], 0.0F
, 1.0F
);
926 rgba
[i
][BCOMP
] = CLAMP(rgba
[i
][BCOMP
], 0.0F
, 1.0F
);
927 rgba
[i
][ACOMP
] = CLAMP(rgba
[i
][ACOMP
], 0.0F
, 1.0F
);
933 * Convert the span's color arrays to the given type.
934 * The only way 'output' can be greater than zero is when we have a fragment
935 * program that writes to gl_FragData[1] or higher.
936 * \param output which fragment program color output is being processed
939 convert_color_type(SWspan
*span
, GLenum srcType
, GLenum newType
, GLuint output
)
943 if (output
> 0 || srcType
== GL_FLOAT
) {
944 src
= span
->array
->attribs
[VARYING_SLOT_COL0
+ output
];
945 span
->array
->ChanType
= GL_FLOAT
;
947 else if (srcType
== GL_UNSIGNED_BYTE
) {
948 src
= span
->array
->rgba8
;
951 assert(srcType
== GL_UNSIGNED_SHORT
);
952 src
= span
->array
->rgba16
;
955 if (newType
== GL_UNSIGNED_BYTE
) {
956 dst
= span
->array
->rgba8
;
958 else if (newType
== GL_UNSIGNED_SHORT
) {
959 dst
= span
->array
->rgba16
;
962 dst
= span
->array
->attribs
[VARYING_SLOT_COL0
];
965 _mesa_convert_colors(span
->array
->ChanType
, src
,
967 span
->end
, span
->array
->mask
);
969 span
->array
->ChanType
= newType
;
970 span
->array
->rgba
= dst
;
976 * Apply fragment shader, fragment program or normal texturing to span.
979 shade_texture_span(struct gl_context
*ctx
, SWspan
*span
)
981 if (_swrast_use_fragment_program(ctx
) ||
982 _mesa_ati_fragment_shader_enabled(ctx
)) {
983 /* programmable shading */
984 if (span
->primitive
== GL_BITMAP
&& span
->array
->ChanType
!= GL_FLOAT
) {
985 convert_color_type(span
, span
->array
->ChanType
, GL_FLOAT
, 0);
988 span
->array
->rgba
= (void *) span
->array
->attribs
[VARYING_SLOT_COL0
];
991 if (span
->primitive
!= GL_POINT
||
992 (span
->interpMask
& SPAN_RGBA
) ||
993 ctx
->Point
.PointSprite
) {
994 /* for single-pixel points, we populated the arrays already */
995 interpolate_active_attribs(ctx
, span
, ~0);
997 span
->array
->ChanType
= GL_FLOAT
;
999 if (!(span
->arrayMask
& SPAN_Z
))
1000 _swrast_span_interpolate_z (ctx
, span
);
1003 if (inputsRead
& VARYING_BIT_POS
)
1005 /* XXX always interpolate wpos so that DDX/DDY work */
1007 interpolate_wpos(ctx
, span
);
1009 /* Run fragment program/shader now */
1010 if (_swrast_use_fragment_program(ctx
)) {
1011 _swrast_exec_fragment_program(ctx
, span
);
1014 assert(_mesa_ati_fragment_shader_enabled(ctx
));
1015 _swrast_exec_fragment_shader(ctx
, span
);
1018 else if (ctx
->Texture
._EnabledCoordUnits
) {
1019 /* conventional texturing */
1022 if ((span
->arrayAttribs
& VARYING_BIT_COL0
) == 0) {
1023 interpolate_int_colors(ctx
, span
);
1026 if (!(span
->arrayMask
& SPAN_RGBA
))
1027 interpolate_int_colors(ctx
, span
);
1029 if ((span
->arrayAttribs
& VARYING_BITS_TEX_ANY
) == 0x0)
1030 interpolate_texcoords(ctx
, span
);
1032 _swrast_texture_span(ctx
, span
);
1037 /** Put colors at x/y locations into a renderbuffer */
1039 put_values(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1041 GLuint count
, const GLint x
[], const GLint y
[],
1042 const void *values
, const GLubyte
*mask
)
1044 mesa_pack_ubyte_rgba_func pack_ubyte
= NULL
;
1045 mesa_pack_float_rgba_func pack_float
= NULL
;
1048 if (datatype
== GL_UNSIGNED_BYTE
)
1049 pack_ubyte
= _mesa_get_pack_ubyte_rgba_function(rb
->Format
);
1051 pack_float
= _mesa_get_pack_float_rgba_function(rb
->Format
);
1053 for (i
= 0; i
< count
; i
++) {
1055 GLubyte
*dst
= _swrast_pixel_address(rb
, x
[i
], y
[i
]);
1057 if (datatype
== GL_UNSIGNED_BYTE
) {
1058 pack_ubyte((const GLubyte
*) values
+ 4 * i
, dst
);
1061 assert(datatype
== GL_FLOAT
);
1062 pack_float((const GLfloat
*) values
+ 4 * i
, dst
);
1069 /** Put row of colors into renderbuffer */
1071 _swrast_put_row(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1073 GLuint count
, GLint x
, GLint y
,
1074 const void *values
, const GLubyte
*mask
)
1076 GLubyte
*dst
= _swrast_pixel_address(rb
, x
, y
);
1079 if (datatype
== GL_UNSIGNED_BYTE
) {
1080 _mesa_pack_ubyte_rgba_row(rb
->Format
, count
,
1081 (const GLubyte (*)[4]) values
, dst
);
1084 assert(datatype
== GL_FLOAT
);
1085 _mesa_pack_float_rgba_row(rb
->Format
, count
,
1086 (const GLfloat (*)[4]) values
, dst
);
1090 const GLuint bpp
= _mesa_get_format_bytes(rb
->Format
);
1091 GLuint i
, runLen
, runStart
;
1092 /* We can't pass a 'mask' array to the _mesa_pack_rgba_row() functions
1093 * so look for runs where mask=1...
1095 runLen
= runStart
= 0;
1096 for (i
= 0; i
< count
; i
++) {
1103 if (!mask
[i
] || i
== count
- 1) {
1104 /* might be the end of a run of pixels */
1106 if (datatype
== GL_UNSIGNED_BYTE
) {
1107 _mesa_pack_ubyte_rgba_row(rb
->Format
, runLen
,
1108 (const GLubyte (*)[4]) values
+ runStart
,
1109 dst
+ runStart
* bpp
);
1112 assert(datatype
== GL_FLOAT
);
1113 _mesa_pack_float_rgba_row(rb
->Format
, runLen
,
1114 (const GLfloat (*)[4]) values
+ runStart
,
1115 dst
+ runStart
* bpp
);
1127 * Apply all the per-fragment operations to a span.
1128 * This now includes texturing (_swrast_write_texture_span() is history).
1129 * This function may modify any of the array values in the span.
1130 * span->interpMask and span->arrayMask may be changed but will be restored
1131 * to their original values before returning.
1134 _swrast_write_rgba_span( struct gl_context
*ctx
, SWspan
*span
)
1136 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1137 const GLbitfield origInterpMask
= span
->interpMask
;
1138 const GLbitfield origArrayMask
= span
->arrayMask
;
1139 const GLbitfield64 origArrayAttribs
= span
->arrayAttribs
;
1140 const GLenum origChanType
= span
->array
->ChanType
;
1141 void * const origRgba
= span
->array
->rgba
;
1142 const GLboolean shader
= (_swrast_use_fragment_program(ctx
)
1143 || _mesa_ati_fragment_shader_enabled(ctx
));
1144 const GLboolean shaderOrTexture
= shader
|| ctx
->Texture
._EnabledCoordUnits
;
1145 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1148 printf("%s() interp 0x%x array 0x%x\n", __func__,
1149 span->interpMask, span->arrayMask);
1152 assert(span
->primitive
== GL_POINT
||
1153 span
->primitive
== GL_LINE
||
1154 span
->primitive
== GL_POLYGON
||
1155 span
->primitive
== GL_BITMAP
);
1157 /* Fragment write masks */
1158 if (span
->arrayMask
& SPAN_MASK
) {
1159 /* mask was initialized by caller, probably glBitmap */
1160 span
->writeAll
= GL_FALSE
;
1163 memset(span
->array
->mask
, 1, span
->end
);
1164 span
->writeAll
= GL_TRUE
;
1167 /* Clip to window/scissor box */
1168 if (!clip_span(ctx
, span
)) {
1172 assert(span
->end
<= SWRAST_MAX_WIDTH
);
1174 /* Depth bounds test */
1175 if (ctx
->Depth
.BoundsTest
&& fb
->Visual
.depthBits
> 0) {
1176 if (!_swrast_depth_bounds_test(ctx
, span
)) {
1182 /* Make sure all fragments are within window bounds */
1183 if (span
->arrayMask
& SPAN_XY
) {
1184 /* array of pixel locations */
1186 for (i
= 0; i
< span
->end
; i
++) {
1187 if (span
->array
->mask
[i
]) {
1188 assert(span
->array
->x
[i
] >= fb
->_Xmin
);
1189 assert(span
->array
->x
[i
] < fb
->_Xmax
);
1190 assert(span
->array
->y
[i
] >= fb
->_Ymin
);
1191 assert(span
->array
->y
[i
] < fb
->_Ymax
);
1197 /* Polygon Stippling */
1198 if (ctx
->Polygon
.StippleFlag
&& span
->primitive
== GL_POLYGON
) {
1199 stipple_polygon_span(ctx
, span
);
1202 /* This is the normal place to compute the fragment color/Z
1203 * from texturing or shading.
1205 if (shaderOrTexture
&& !swrast
->_DeferredTexture
) {
1206 shade_texture_span(ctx
, span
);
1209 /* Do the alpha test */
1210 if (ctx
->Color
.AlphaEnabled
) {
1211 if (!_swrast_alpha_test(ctx
, span
)) {
1212 /* all fragments failed test */
1217 /* Stencil and Z testing */
1218 if (_mesa_stencil_is_enabled(ctx
) || ctx
->Depth
.Test
) {
1219 if (!(span
->arrayMask
& SPAN_Z
))
1220 _swrast_span_interpolate_z(ctx
, span
);
1222 if (ctx
->Transform
.DepthClampNear
&& ctx
->Transform
.DepthClampFar
)
1223 _swrast_depth_clamp_span(ctx
, span
);
1225 if (_mesa_stencil_is_enabled(ctx
)) {
1226 /* Combined Z/stencil tests */
1227 if (!_swrast_stencil_and_ztest_span(ctx
, span
)) {
1228 /* all fragments failed test */
1232 else if (fb
->Visual
.depthBits
> 0) {
1233 /* Just regular depth testing */
1234 assert(ctx
->Depth
.Test
);
1235 assert(span
->arrayMask
& SPAN_Z
);
1236 if (!_swrast_depth_test_span(ctx
, span
)) {
1237 /* all fragments failed test */
1243 if (ctx
->Query
.CurrentOcclusionObject
) {
1244 /* update count of 'passed' fragments */
1245 struct gl_query_object
*q
= ctx
->Query
.CurrentOcclusionObject
;
1247 for (i
= 0; i
< span
->end
; i
++)
1248 q
->Result
+= span
->array
->mask
[i
];
1251 /* We had to wait until now to check for glColorMask(0,0,0,0) because of
1252 * the occlusion test.
1254 if (fb
->_NumColorDrawBuffers
== 1 &&
1255 !GET_COLORMASK(ctx
->Color
.ColorMask
, 0)) {
1256 /* no colors to write */
1260 /* If we were able to defer fragment color computation to now, there's
1261 * a good chance that many fragments will have already been killed by
1262 * Z/stencil testing.
1264 if (shaderOrTexture
&& swrast
->_DeferredTexture
) {
1265 shade_texture_span(ctx
, span
);
1269 if ((span
->arrayAttribs
& VARYING_BIT_COL0
) == 0) {
1270 interpolate_active_attribs(ctx
, span
, VARYING_BIT_COL0
);
1273 if ((span
->arrayMask
& SPAN_RGBA
) == 0) {
1274 interpolate_int_colors(ctx
, span
);
1278 assert(span
->arrayMask
& SPAN_RGBA
);
1280 if (span
->primitive
== GL_BITMAP
|| !swrast
->SpecularVertexAdd
) {
1281 /* Add primary and specular (diffuse + specular) colors */
1283 if (ctx
->Fog
.ColorSumEnabled
||
1284 (ctx
->Light
.Enabled
&&
1285 ctx
->Light
.Model
.ColorControl
== GL_SEPARATE_SPECULAR_COLOR
)) {
1286 add_specular(ctx
, span
);
1292 if (swrast
->_FogEnabled
) {
1293 _swrast_fog_rgba_span(ctx
, span
);
1296 /* Antialias coverage application */
1297 if (span
->arrayMask
& SPAN_COVERAGE
) {
1298 apply_aa_coverage(span
);
1301 /* Clamp color/alpha values over the range [0.0, 1.0] before storage */
1302 if (ctx
->Color
.ClampFragmentColor
== GL_TRUE
&&
1303 span
->array
->ChanType
== GL_FLOAT
) {
1308 * Write to renderbuffers.
1309 * Depending on glDrawBuffer() state and the which color outputs are
1310 * written by the fragment shader, we may either replicate one color to
1311 * all renderbuffers or write a different color to each renderbuffer.
1312 * multiFragOutputs=TRUE for the later case.
1315 const GLuint numBuffers
= fb
->_NumColorDrawBuffers
;
1316 const struct gl_program
*fp
= ctx
->FragmentProgram
._Current
;
1317 const GLboolean multiFragOutputs
=
1318 _swrast_use_fragment_program(ctx
)
1319 && fp
->info
.outputs_written
>= (1 << FRAG_RESULT_DATA0
);
1320 /* Save srcColorType because convert_color_type() can change it */
1321 const GLenum srcColorType
= span
->array
->ChanType
;
1324 for (buf
= 0; buf
< numBuffers
; buf
++) {
1325 struct gl_renderbuffer
*rb
= fb
->_ColorDrawBuffers
[buf
];
1327 /* color[fragOutput] will be written to buffer[buf] */
1330 /* re-use one of the attribute array buffers for rgbaSave */
1331 GLchan (*rgbaSave
)[4] = (GLchan (*)[4]) span
->array
->attribs
[0];
1332 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
1333 const GLenum dstColorType
= srb
->ColorType
;
1335 assert(dstColorType
== GL_UNSIGNED_BYTE
||
1336 dstColorType
== GL_FLOAT
);
1338 /* set span->array->rgba to colors for renderbuffer's datatype */
1339 if (srcColorType
!= dstColorType
) {
1340 convert_color_type(span
, srcColorType
, dstColorType
,
1341 multiFragOutputs
? buf
: 0);
1344 if (srcColorType
== GL_UNSIGNED_BYTE
) {
1345 span
->array
->rgba
= span
->array
->rgba8
;
1348 span
->array
->rgba
= (void *)
1349 span
->array
->attribs
[VARYING_SLOT_COL0
];
1353 if (!multiFragOutputs
&& numBuffers
> 1) {
1354 /* save colors for second, third renderbuffer writes */
1355 memcpy(rgbaSave
, span
->array
->rgba
,
1356 4 * span
->end
* sizeof(GLchan
));
1359 assert(rb
->_BaseFormat
== GL_RGBA
||
1360 rb
->_BaseFormat
== GL_RGB
||
1361 rb
->_BaseFormat
== GL_RED
||
1362 rb
->_BaseFormat
== GL_RG
||
1363 rb
->_BaseFormat
== GL_ALPHA
);
1365 if (ctx
->Color
.ColorLogicOpEnabled
) {
1366 _swrast_logicop_rgba_span(ctx
, rb
, span
);
1368 else if ((ctx
->Color
.BlendEnabled
>> buf
) & 1) {
1369 _swrast_blend_span(ctx
, rb
, span
);
1372 if (GET_COLORMASK(ctx
->Color
.ColorMask
, buf
) != 0xf) {
1373 _swrast_mask_rgba_span(ctx
, rb
, span
, buf
);
1376 if (span
->arrayMask
& SPAN_XY
) {
1377 /* array of pixel coords */
1379 span
->array
->ChanType
, span
->end
,
1380 span
->array
->x
, span
->array
->y
,
1381 span
->array
->rgba
, span
->array
->mask
);
1384 /* horizontal run of pixels */
1385 _swrast_put_row(ctx
, rb
,
1386 span
->array
->ChanType
,
1387 span
->end
, span
->x
, span
->y
,
1389 span
->writeAll
? NULL
: span
->array
->mask
);
1392 if (!multiFragOutputs
&& numBuffers
> 1) {
1393 /* restore original span values */
1394 memcpy(span
->array
->rgba
, rgbaSave
,
1395 4 * span
->end
* sizeof(GLchan
));
1403 /* restore these values before returning */
1404 span
->interpMask
= origInterpMask
;
1405 span
->arrayMask
= origArrayMask
;
1406 span
->arrayAttribs
= origArrayAttribs
;
1407 span
->array
->ChanType
= origChanType
;
1408 span
->array
->rgba
= origRgba
;
1413 * Read float RGBA pixels from a renderbuffer. Clipping will be done to
1414 * prevent reading ouside the buffer's boundaries.
1415 * \param rgba the returned colors
1418 _swrast_read_rgba_span( struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1419 GLuint n
, GLint x
, GLint y
,
1422 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
1423 GLenum dstType
= GL_FLOAT
;
1424 const GLint bufWidth
= (GLint
) rb
->Width
;
1425 const GLint bufHeight
= (GLint
) rb
->Height
;
1427 if (y
< 0 || y
>= bufHeight
|| x
+ (GLint
) n
< 0 || x
>= bufWidth
) {
1428 /* completely above, below, or right */
1429 /* XXX maybe leave rgba values undefined? */
1430 memset(rgba
, 0, 4 * n
* sizeof(GLchan
));
1437 /* left edge clipping */
1439 length
= (GLint
) n
- skip
;
1441 /* completely left of window */
1444 if (length
> bufWidth
) {
1448 else if ((GLint
) (x
+ n
) > bufWidth
) {
1449 /* right edge clipping */
1451 length
= bufWidth
- x
;
1453 /* completely to right of window */
1464 assert(rb
->_BaseFormat
== GL_RGBA
||
1465 rb
->_BaseFormat
== GL_RGB
||
1466 rb
->_BaseFormat
== GL_RG
||
1467 rb
->_BaseFormat
== GL_RED
||
1468 rb
->_BaseFormat
== GL_LUMINANCE
||
1469 rb
->_BaseFormat
== GL_INTENSITY
||
1470 rb
->_BaseFormat
== GL_LUMINANCE_ALPHA
||
1471 rb
->_BaseFormat
== GL_ALPHA
);
1474 (void) srb
; /* silence unused var warning */
1476 src
= _swrast_pixel_address(rb
, x
+ skip
, y
);
1478 if (dstType
== GL_UNSIGNED_BYTE
) {
1479 _mesa_unpack_ubyte_rgba_row(rb
->Format
, length
, src
,
1480 (GLubyte (*)[4]) rgba
+ skip
);
1482 else if (dstType
== GL_FLOAT
) {
1483 _mesa_unpack_rgba_row(rb
->Format
, length
, src
,
1484 (GLfloat (*)[4]) rgba
+ skip
);
1487 _mesa_problem(ctx
, "unexpected type in _swrast_read_rgba_span()");
1494 * Get colors at x/y positions with clipping.
1495 * \param type type of values to return
1498 get_values(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1499 GLuint count
, const GLint x
[], const GLint y
[],
1500 void *values
, GLenum type
)
1504 for (i
= 0; i
< count
; i
++) {
1505 if (x
[i
] >= 0 && y
[i
] >= 0 &&
1506 x
[i
] < (GLint
) rb
->Width
&& y
[i
] < (GLint
) rb
->Height
) {
1508 const GLubyte
*src
= _swrast_pixel_address(rb
, x
[i
], y
[i
]);
1510 if (type
== GL_UNSIGNED_BYTE
) {
1511 _mesa_unpack_ubyte_rgba_row(rb
->Format
, 1, src
,
1512 (GLubyte (*)[4]) values
+ i
);
1514 else if (type
== GL_FLOAT
) {
1515 _mesa_unpack_rgba_row(rb
->Format
, 1, src
,
1516 (GLfloat (*)[4]) values
+ i
);
1519 _mesa_problem(ctx
, "unexpected type in get_values()");
1527 * Get row of colors with clipping.
1528 * \param type type of values to return
1531 get_row(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1532 GLuint count
, GLint x
, GLint y
,
1533 GLvoid
*values
, GLenum type
)
1538 if (y
< 0 || y
>= (GLint
) rb
->Height
)
1539 return; /* above or below */
1541 if (x
+ (GLint
) count
<= 0 || x
>= (GLint
) rb
->Width
)
1542 return; /* entirely left or right */
1544 if (x
+ count
> rb
->Width
) {
1546 GLint clip
= x
+ count
- rb
->Width
;
1557 src
= _swrast_pixel_address(rb
, x
, y
);
1559 if (type
== GL_UNSIGNED_BYTE
) {
1560 _mesa_unpack_ubyte_rgba_row(rb
->Format
, count
, src
,
1561 (GLubyte (*)[4]) values
+ skip
);
1563 else if (type
== GL_FLOAT
) {
1564 _mesa_unpack_rgba_row(rb
->Format
, count
, src
,
1565 (GLfloat (*)[4]) values
+ skip
);
1568 _mesa_problem(ctx
, "unexpected type in get_row()");
1574 * Get RGBA pixels from the given renderbuffer.
1575 * Used by blending, logicop and masking functions.
1576 * \return pointer to the colors we read.
1579 _swrast_get_dest_rgba(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
1584 /* Point rbPixels to a temporary space */
1585 rbPixels
= span
->array
->attribs
[VARYING_SLOT_MAX
- 1];
1587 /* Get destination values from renderbuffer */
1588 if (span
->arrayMask
& SPAN_XY
) {
1589 get_values(ctx
, rb
, span
->end
, span
->array
->x
, span
->array
->y
,
1590 rbPixels
, span
->array
->ChanType
);
1593 get_row(ctx
, rb
, span
->end
, span
->x
, span
->y
,
1594 rbPixels
, span
->array
->ChanType
);