Sync with trunk r63647.
[reactos.git] / dll / opengl / mesa / swrast / s_alpha.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25 /**
26 * \file swrast/s_alpha.c
27 * \brief Functions to apply alpha test.
28 */
29
30 #include <precomp.h>
31
32 #define ALPHA_TEST(ALPHA, LOOP_CODE) \
33 do { \
34 switch (ctx->Color.AlphaFunc) { \
35 case GL_LESS: \
36 for (i = 0; i < n; i++) { \
37 mask[i] &= (ALPHA < ref); \
38 LOOP_CODE; \
39 } \
40 break; \
41 case GL_LEQUAL: \
42 for (i = 0; i < n; i++) { \
43 mask[i] &= (ALPHA <= ref); \
44 LOOP_CODE; \
45 } \
46 break; \
47 case GL_GEQUAL: \
48 for (i = 0; i < n; i++) { \
49 mask[i] &= (ALPHA >= ref); \
50 LOOP_CODE; \
51 } \
52 break; \
53 case GL_GREATER: \
54 for (i = 0; i < n; i++) { \
55 mask[i] &= (ALPHA > ref); \
56 LOOP_CODE; \
57 } \
58 break; \
59 case GL_NOTEQUAL: \
60 for (i = 0; i < n; i++) { \
61 mask[i] &= (ALPHA != ref); \
62 LOOP_CODE; \
63 } \
64 break; \
65 case GL_EQUAL: \
66 for (i = 0; i < n; i++) { \
67 mask[i] &= (ALPHA == ref); \
68 LOOP_CODE; \
69 } \
70 break; \
71 default: \
72 _mesa_problem(ctx, "Invalid alpha test in _swrast_alpha_test" ); \
73 return 0; \
74 } \
75 } while (0)
76
77
78
79 /**
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.
84 */
85 GLint
86 _swrast_alpha_test(const struct gl_context *ctx, SWspan *span)
87 {
88 const GLuint n = span->end;
89 GLubyte *mask = span->array->mask;
90 GLuint i;
91
92 if (ctx->Color.AlphaFunc == GL_ALWAYS) {
93 /* do nothing */
94 return 1;
95 }
96 else if (ctx->Color.AlphaFunc == GL_NEVER) {
97 /* All pixels failed - caller should check for this return value and
98 * act accordingly.
99 */
100 span->writeAll = GL_FALSE;
101 return 0;
102 }
103
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;
108 GLubyte ref;
109 CLAMPED_FLOAT_TO_UBYTE(ref, ctx->Color.AlphaRef);
110 ALPHA_TEST(rgba[i][ACOMP], ;);
111 }
112 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
113 GLushort (*rgba)[4] = span->array->rgba16;
114 GLushort ref;
115 CLAMPED_FLOAT_TO_USHORT(ref, ctx->Color.AlphaRef);
116 ALPHA_TEST(rgba[i][ACOMP], ;);
117 }
118 else {
119 GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL];
120 const GLfloat ref = ctx->Color.AlphaRef;
121 ALPHA_TEST(rgba[i][ACOMP], ;);
122 }
123 }
124 else {
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;
130 GLubyte ref;
131 CLAMPED_FLOAT_TO_UBYTE(ref, ctx->Color.AlphaRef);
132 ALPHA_TEST(FixedToInt(alpha), alpha += alphaStep);
133 }
134 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
135 const GLfixed alphaStep = span->alphaStep;
136 GLfixed alpha = span->alpha;
137 GLushort ref;
138 CLAMPED_FLOAT_TO_USHORT(ref, ctx->Color.AlphaRef);
139 ALPHA_TEST(FixedToInt(alpha), alpha += alphaStep);
140 }
141 else {
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);
146 }
147 }
148
149 span->writeAll = GL_FALSE;
150
151 /* XXX examine mask[] values? */
152 return 1;
153 }