test version of startmenu root with big icons
[reactos.git] / reactos / lib / mesa32 / src / main / stencil.c
1 /**
2 * \file stencil.c
3 * Stencil operations.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 * Version: 4.1
9 *
10 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31 #include "glheader.h"
32 #include "imports.h"
33 #include "context.h"
34 #include "depth.h"
35 #include "macros.h"
36 #include "stencil.h"
37 #include "mtypes.h"
38 #include "enable.h"
39
40
41 /**
42 * Set the clear value for the stencil buffer.
43 *
44 * \param s clear value.
45 *
46 * \sa glClearStencil().
47 *
48 * Updates gl_stencil_attrib::Clear. On change
49 * flushes the vertices and notifies the driver via
50 * the dd_function_table::ClearStencil callback.
51 */
52 void GLAPIENTRY
53 _mesa_ClearStencil( GLint s )
54 {
55 GET_CURRENT_CONTEXT(ctx);
56 ASSERT_OUTSIDE_BEGIN_END(ctx);
57
58 if (ctx->Stencil.Clear == (GLstencil) s)
59 return;
60
61 FLUSH_VERTICES(ctx, _NEW_STENCIL);
62 ctx->Stencil.Clear = (GLstencil) s;
63
64 if (ctx->Driver.ClearStencil) {
65 (*ctx->Driver.ClearStencil)( ctx, s );
66 }
67 }
68
69
70 /**
71 * Set the function and reference value for stencil testing.
72 *
73 * \param func test function.
74 * \param ref reference value.
75 * \param mask bitmask.
76 *
77 * \sa glStencilFunc().
78 *
79 * Verifies the parameters and updates the respective values in
80 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
81 * driver via the dd_function_table::StencilFunc callback.
82 */
83 void GLAPIENTRY
84 _mesa_StencilFunc( GLenum func, GLint ref, GLuint mask )
85 {
86 GET_CURRENT_CONTEXT(ctx);
87 const GLint face = ctx->Stencil.ActiveFace;
88 GLint maxref;
89 ASSERT_OUTSIDE_BEGIN_END(ctx);
90
91 switch (func) {
92 case GL_NEVER:
93 case GL_LESS:
94 case GL_LEQUAL:
95 case GL_GREATER:
96 case GL_GEQUAL:
97 case GL_EQUAL:
98 case GL_NOTEQUAL:
99 case GL_ALWAYS:
100 break;
101 default:
102 _mesa_error( ctx, GL_INVALID_ENUM, "glStencilFunc" );
103 return;
104 }
105
106 maxref = (1 << STENCIL_BITS) - 1;
107 ref = (GLstencil) CLAMP( ref, 0, maxref );
108
109 if (ctx->Stencil.Function[face] == func &&
110 ctx->Stencil.ValueMask[face] == (GLstencil) mask &&
111 ctx->Stencil.Ref[face] == ref)
112 return;
113
114 FLUSH_VERTICES(ctx, _NEW_STENCIL);
115 ctx->Stencil.Function[face] = func;
116 ctx->Stencil.Ref[face] = ref;
117 ctx->Stencil.ValueMask[face] = (GLstencil) mask;
118
119 if (ctx->Driver.StencilFunc) {
120 (*ctx->Driver.StencilFunc)( ctx, func, ref, mask );
121 }
122 }
123
124
125 /**
126 * Set the stencil writing mask.
127 *
128 * \param mask bit-mask to enable/disable writing of individual bits in the
129 * stencil planes.
130 *
131 * \sa glStencilMask().
132 *
133 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
134 * notifies the driver via the dd_function_table::StencilMask callback.
135 */
136 void GLAPIENTRY
137 _mesa_StencilMask( GLuint mask )
138 {
139 GET_CURRENT_CONTEXT(ctx);
140 const GLint face = ctx->Stencil.ActiveFace;
141 ASSERT_OUTSIDE_BEGIN_END(ctx);
142
143 if (ctx->Stencil.WriteMask[face] == (GLstencil) mask)
144 return;
145
146 FLUSH_VERTICES(ctx, _NEW_STENCIL);
147 ctx->Stencil.WriteMask[face] = (GLstencil) mask;
148
149 if (ctx->Driver.StencilMask) {
150 (*ctx->Driver.StencilMask)( ctx, mask );
151 }
152 }
153
154
155 /**
156 * Set the stencil test actions.
157 *
158 * \param fail action to take when stencil test fails.
159 * \param zfail action to take when stencil test passes, but the depth test fails.
160 * \param zpass action to take when stencil test passes and the depth test
161 * passes (or depth testing is not enabled).
162 *
163 * \sa glStencilOp().
164 *
165 * Verifies the parameters and updates the respective fields in
166 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
167 * driver via the dd_function_table::StencilOp callback.
168 */
169 void GLAPIENTRY
170 _mesa_StencilOp(GLenum fail, GLenum zfail, GLenum zpass)
171 {
172 GET_CURRENT_CONTEXT(ctx);
173 const GLint face = ctx->Stencil.ActiveFace;
174 ASSERT_OUTSIDE_BEGIN_END(ctx);
175
176 switch (fail) {
177 case GL_KEEP:
178 case GL_ZERO:
179 case GL_REPLACE:
180 case GL_INCR:
181 case GL_DECR:
182 case GL_INVERT:
183 break;
184 case GL_INCR_WRAP_EXT:
185 case GL_DECR_WRAP_EXT:
186 if (ctx->Extensions.EXT_stencil_wrap) {
187 break;
188 }
189 /* FALL-THROUGH */
190 default:
191 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOp");
192 return;
193 }
194 switch (zfail) {
195 case GL_KEEP:
196 case GL_ZERO:
197 case GL_REPLACE:
198 case GL_INCR:
199 case GL_DECR:
200 case GL_INVERT:
201 break;
202 case GL_INCR_WRAP_EXT:
203 case GL_DECR_WRAP_EXT:
204 if (ctx->Extensions.EXT_stencil_wrap) {
205 break;
206 }
207 /* FALL-THROUGH */
208 default:
209 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOp");
210 return;
211 }
212 switch (zpass) {
213 case GL_KEEP:
214 case GL_ZERO:
215 case GL_REPLACE:
216 case GL_INCR:
217 case GL_DECR:
218 case GL_INVERT:
219 break;
220 case GL_INCR_WRAP_EXT:
221 case GL_DECR_WRAP_EXT:
222 if (ctx->Extensions.EXT_stencil_wrap) {
223 break;
224 }
225 /* FALL-THROUGH */
226 default:
227 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOp");
228 return;
229 }
230
231 if (ctx->Stencil.ZFailFunc[face] == zfail &&
232 ctx->Stencil.ZPassFunc[face] == zpass &&
233 ctx->Stencil.FailFunc[face] == fail)
234 return;
235
236 FLUSH_VERTICES(ctx, _NEW_STENCIL);
237 ctx->Stencil.ZFailFunc[face] = zfail;
238 ctx->Stencil.ZPassFunc[face] = zpass;
239 ctx->Stencil.FailFunc[face] = fail;
240
241 if (ctx->Driver.StencilOp) {
242 (*ctx->Driver.StencilOp)(ctx, fail, zfail, zpass);
243 }
244 }
245
246
247 #if _HAVE_FULL_GL
248 /* GL_EXT_stencil_two_side */
249 void GLAPIENTRY
250 _mesa_ActiveStencilFaceEXT(GLenum face)
251 {
252 GET_CURRENT_CONTEXT(ctx);
253 ASSERT_OUTSIDE_BEGIN_END(ctx);
254
255 if (face == GL_FRONT || face == GL_BACK) {
256 FLUSH_VERTICES(ctx, _NEW_STENCIL);
257 ctx->Stencil.ActiveFace = (face == GL_FRONT) ? 0 : 1;
258 }
259
260 if (ctx->Driver.ActiveStencilFace) {
261 (*ctx->Driver.ActiveStencilFace)( ctx, (GLuint) ctx->Stencil.ActiveFace );
262 }
263 }
264 #endif
265
266
267 /**
268 * Initialize the context stipple state.
269 *
270 * \param ctx GL context.
271 *
272 * Initializes __GLcontextRec::Stencil attribute group.
273 */
274 void _mesa_init_stencil( GLcontext * ctx )
275 {
276
277 /* Stencil group */
278 ctx->Stencil.Enabled = GL_FALSE;
279 ctx->Stencil.TestTwoSide = GL_FALSE;
280 ctx->Stencil.ActiveFace = 0; /* 0 = GL_FRONT, 1 = GL_BACK */
281 ctx->Stencil.Function[0] = GL_ALWAYS;
282 ctx->Stencil.Function[1] = GL_ALWAYS;
283 ctx->Stencil.FailFunc[0] = GL_KEEP;
284 ctx->Stencil.FailFunc[1] = GL_KEEP;
285 ctx->Stencil.ZPassFunc[0] = GL_KEEP;
286 ctx->Stencil.ZPassFunc[1] = GL_KEEP;
287 ctx->Stencil.ZFailFunc[0] = GL_KEEP;
288 ctx->Stencil.ZFailFunc[1] = GL_KEEP;
289 ctx->Stencil.Ref[0] = 0;
290 ctx->Stencil.Ref[1] = 0;
291 ctx->Stencil.ValueMask[0] = STENCIL_MAX;
292 ctx->Stencil.ValueMask[1] = STENCIL_MAX;
293 ctx->Stencil.WriteMask[0] = STENCIL_MAX;
294 ctx->Stencil.WriteMask[1] = STENCIL_MAX;
295 ctx->Stencil.Clear = 0;
296 }