Klaus's latest patches and some clean-up
[mesa.git] / src / mesa / swrast / s_alpha.c
1 /* $Id: s_alpha.c,v 1.5 2002/01/21 18:12:34 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 4.1
6 *
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 #include "glheader.h"
29 #include "context.h"
30 #include "colormac.h"
31 #include "macros.h"
32 #include "mmath.h"
33
34 #include "s_alpha.h"
35
36
37 /*
38 * Apply the alpha test to a span of pixels.
39 * In: rgba - array of pixels
40 * In/Out: span -
41 * Return: 0 = all pixels in the span failed the alpha test.
42 * 1 = one or more pixels passed the alpha test.
43 */
44 GLint
45 _mesa_alpha_test( const GLcontext *ctx, struct sw_span *span,
46 CONST GLchan rgba[][4])
47 {
48 GLuint i;
49 const GLchan ref = ctx->Color.AlphaRef;
50 GLubyte *mask = span->mask;
51
52 ASSERT (span->filledMask == GL_TRUE);
53 ASSERT (span->filledAlpha == GL_TRUE);
54
55 SW_SPAN_SET_FLAG(span->testedAlpha);
56
57
58 /* switch cases ordered from most frequent to less frequent */
59 switch (ctx->Color.AlphaFunc) {
60 case GL_LESS:
61 for (i=span->start; i<span->end; i++) {
62 mask[i] &= (rgba[i][ACOMP] < ref);
63 }
64 break;
65 case GL_LEQUAL:
66 for (i=span->start; i<span->end; i++)
67 mask[i] &= (rgba[i][ACOMP] <= ref);
68 break;
69 case GL_GEQUAL:
70 for (i=span->start; i<span->end; i++) {
71 mask[i] &= (rgba[i][ACOMP] >= ref);
72 }
73 break;
74 case GL_GREATER:
75 for (i=span->start; i<span->end; i++) {
76 mask[i] &= (rgba[i][ACOMP] > ref);
77 }
78 break;
79 case GL_NOTEQUAL:
80 for (i=span->start; i<span->end; i++) {
81 mask[i] &= (rgba[i][ACOMP] != ref);
82 }
83 break;
84 case GL_EQUAL:
85 for (i=span->start; i<span->end; i++) {
86 mask[i] &= (rgba[i][ACOMP] == ref);
87 }
88 break;
89 case GL_ALWAYS:
90 /* do nothing */
91 return 1;
92 case GL_NEVER:
93 /* caller should check for zero! */
94 return 0;
95 default:
96 _mesa_problem( ctx, "Invalid alpha test in gl_alpha_test" );
97 return 0;
98 }
99
100 while ((span->start <= span->end) &&
101 (mask[span->start] == 0))
102 span->start ++;
103
104 while ((span->end >= span->start) &&
105 (mask[span->end] == 0))
106 span->end --;
107
108 span->writeAll = GL_FALSE;
109
110 if (span->start >= span->end)
111 return 0;
112 else
113 return 1;
114 }
115
116
117 /*
118 * Apply the alpha test to a span of pixels.
119 * In: rgba - array of pixels
120 * In/Out: mask - current pixel mask. Pixels which fail the alpha test
121 * will set the corresponding mask flag to 0.
122 * Return: 0 = all pixels in the span failed the alpha test.
123 * 1 = one or more pixels passed the alpha test.
124 */
125 GLint
126 _old_alpha_test( const GLcontext *ctx,
127 GLuint n, CONST GLchan rgba[][4], GLubyte mask[] )
128 {
129 GLuint i;
130 const GLchan ref = ctx->Color.AlphaRef;
131
132 /* switch cases ordered from most frequent to less frequent */
133 switch (ctx->Color.AlphaFunc) {
134 case GL_LESS:
135 for (i=0;i<n;i++) {
136 mask[i] &= (rgba[i][ACOMP] < ref);
137 }
138 return 1;
139 case GL_LEQUAL:
140 for (i=0;i<n;i++)
141 mask[i] &= (rgba[i][ACOMP] <= ref);
142 return 1;
143 case GL_GEQUAL:
144 for (i=0;i<n;i++) {
145 mask[i] &= (rgba[i][ACOMP] >= ref);
146 }
147 return 1;
148 case GL_GREATER:
149 for (i=0;i<n;i++) {
150 mask[i] &= (rgba[i][ACOMP] > ref);
151 }
152 return 1;
153 case GL_NOTEQUAL:
154 for (i=0;i<n;i++) {
155 mask[i] &= (rgba[i][ACOMP] != ref);
156 }
157 return 1;
158 case GL_EQUAL:
159 for (i=0;i<n;i++) {
160 mask[i] &= (rgba[i][ACOMP] == ref);
161 }
162 return 1;
163 case GL_ALWAYS:
164 /* do nothing */
165 return 1;
166 case GL_NEVER:
167 /* caller should check for zero! */
168 return 0;
169 default:
170 _mesa_problem( ctx, "Invalid alpha test in gl_alpha_test" );
171 return 0;
172 }
173 /* Never get here */
174 /*return 1;*/
175 }