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.
32 #define ALPHA_TEST(ALPHA, LOOP_CODE) \
34 switch (ctx->Color.AlphaFunc) { \
36 for (i = 0; i < n; i++) { \
37 mask[i] &= (ALPHA < ref); \
42 for (i = 0; i < n; i++) { \
43 mask[i] &= (ALPHA <= ref); \
48 for (i = 0; i < n; i++) { \
49 mask[i] &= (ALPHA >= ref); \
54 for (i = 0; i < n; i++) { \
55 mask[i] &= (ALPHA > ref); \
60 for (i = 0; i < n; i++) { \
61 mask[i] &= (ALPHA != ref); \
66 for (i = 0; i < n; i++) { \
67 mask[i] &= (ALPHA == ref); \
72 _mesa_problem(ctx, "Invalid alpha test in _swrast_alpha_test" ); \
80 * Perform the alpha test for an array of pixels.
81 * For pixels that fail the test, mask[i] will be set to 0.
82 * \return 0 if all pixels in the span failed the alpha test,
83 * 1 if one or more pixels passed the alpha test.
86 _swrast_alpha_test(const struct gl_context
*ctx
, SWspan
*span
)
88 const GLuint n
= span
->end
;
89 GLubyte
*mask
= span
->array
->mask
;
92 if (ctx
->Color
.AlphaFunc
== GL_ALWAYS
) {
96 else if (ctx
->Color
.AlphaFunc
== GL_NEVER
) {
97 /* All pixels failed - caller should check for this return value and
100 span
->writeAll
= GL_FALSE
;
104 if (span
->arrayMask
& SPAN_RGBA
) {
105 /* Use array's alpha values */
106 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
107 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
109 CLAMPED_FLOAT_TO_UBYTE(ref
, ctx
->Color
.AlphaRef
);
110 ALPHA_TEST(rgba
[i
][ACOMP
], ;);
112 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
113 GLushort (*rgba
)[4] = span
->array
->rgba16
;
115 CLAMPED_FLOAT_TO_USHORT(ref
, ctx
->Color
.AlphaRef
);
116 ALPHA_TEST(rgba
[i
][ACOMP
], ;);
119 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL
];
120 const GLfloat ref
= ctx
->Color
.AlphaRef
;
121 ALPHA_TEST(rgba
[i
][ACOMP
], ;);
125 /* Interpolate alpha values */
126 ASSERT(span
->interpMask
& SPAN_RGBA
);
127 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
128 const GLfixed alphaStep
= span
->alphaStep
;
129 GLfixed alpha
= span
->alpha
;
131 CLAMPED_FLOAT_TO_UBYTE(ref
, ctx
->Color
.AlphaRef
);
132 ALPHA_TEST(FixedToInt(alpha
), alpha
+= alphaStep
);
134 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
135 const GLfixed alphaStep
= span
->alphaStep
;
136 GLfixed alpha
= span
->alpha
;
138 CLAMPED_FLOAT_TO_USHORT(ref
, ctx
->Color
.AlphaRef
);
139 ALPHA_TEST(FixedToInt(alpha
), alpha
+= alphaStep
);
142 const GLfloat alphaStep
= FixedToFloat(span
->alphaStep
);
143 GLfloat alpha
= FixedToFloat(span
->alpha
);
144 const GLfloat ref
= ctx
->Color
.AlphaRef
;
145 ALPHA_TEST(alpha
, alpha
+= alphaStep
);
149 span
->writeAll
= GL_FALSE
;
151 /* XXX examine mask[] values? */