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.
26 * \file swrast/s_alpha.c
27 * \brief Functions to apply alpha test.
36 #include "s_context.h"
39 #define ALPHA_TEST(ALPHA, LOOP_CODE) \
41 switch (ctx->Color.AlphaFunc) { \
43 for (i = 0; i < n; i++) { \
44 mask[i] &= (ALPHA < ref); \
49 for (i = 0; i < n; i++) { \
50 mask[i] &= (ALPHA <= ref); \
55 for (i = 0; i < n; i++) { \
56 mask[i] &= (ALPHA >= ref); \
61 for (i = 0; i < n; i++) { \
62 mask[i] &= (ALPHA > ref); \
67 for (i = 0; i < n; i++) { \
68 mask[i] &= (ALPHA != ref); \
73 for (i = 0; i < n; i++) { \
74 mask[i] &= (ALPHA == ref); \
79 _mesa_problem(ctx, "Invalid alpha test in _swrast_alpha_test" ); \
87 * Perform the alpha test for an array of pixels.
88 * For pixels that fail the test, mask[i] will be set to 0.
89 * \return 0 if all pixels in the span failed the alpha test,
90 * 1 if one or more pixels passed the alpha test.
93 _swrast_alpha_test(const GLcontext
*ctx
, SWspan
*span
)
95 const GLuint n
= span
->end
;
96 GLubyte
*mask
= span
->array
->mask
;
99 if (ctx
->Color
.AlphaFunc
== GL_ALWAYS
) {
103 else if (ctx
->Color
.AlphaFunc
== GL_NEVER
) {
104 /* All pixels failed - caller should check for this return value and
107 span
->writeAll
= GL_FALSE
;
111 if (span
->arrayMask
& SPAN_RGBA
) {
112 /* Use array's alpha values */
113 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
114 GLubyte (*rgba
)[4] = span
->array
->color
.sz1
.rgba
;
116 CLAMPED_FLOAT_TO_UBYTE(ref
, ctx
->Color
.AlphaRef
);
117 ALPHA_TEST(rgba
[i
][ACOMP
], ;);
119 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
120 GLushort (*rgba
)[4] = span
->array
->color
.sz2
.rgba
;
122 CLAMPED_FLOAT_TO_USHORT(ref
, ctx
->Color
.AlphaRef
);
123 ALPHA_TEST(rgba
[i
][ACOMP
], ;);
126 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
127 const GLfloat ref
= ctx
->Color
.AlphaRef
;
128 ALPHA_TEST(rgba
[i
][ACOMP
], ;);
132 /* Interpolate alpha values */
133 ASSERT(span
->interpMask
& SPAN_RGBA
);
134 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
135 const GLfixed alphaStep
= span
->alphaStep
;
136 GLfixed alpha
= span
->alpha
;
138 CLAMPED_FLOAT_TO_UBYTE(ref
, ctx
->Color
.AlphaRef
);
139 ALPHA_TEST(FixedToInt(alpha
), alpha
+= alphaStep
);
141 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
142 const GLfixed alphaStep
= span
->alphaStep
;
143 GLfixed alpha
= span
->alpha
;
145 CLAMPED_FLOAT_TO_USHORT(ref
, ctx
->Color
.AlphaRef
);
146 ALPHA_TEST(FixedToInt(alpha
), alpha
+= alphaStep
);
149 const GLfloat alphaStep
= span
->alphaStep
;
150 GLfloat alpha
= span
->alpha
;
151 const GLfloat ref
= ctx
->Color
.AlphaRef
;
152 ALPHA_TEST(alpha
, alpha
+= alphaStep
);
156 span
->writeAll
= GL_FALSE
;
158 /* XXX examine mask[] values? */