Some initial work for OpenGL 2.0: glStencilFunc/Op/MaskSeparate() functions.
[mesa.git] / src / mesa / main / stencil.c
1 /**
2 * \file stencil.c
3 * Stencil operations.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 * Version: 6.3
9 *
10 * Copyright (C) 1999-2005 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
248 #if _HAVE_FULL_GL
249 /* GL_EXT_stencil_two_side */
250 void GLAPIENTRY
251 _mesa_ActiveStencilFaceEXT(GLenum face)
252 {
253 GET_CURRENT_CONTEXT(ctx);
254 ASSERT_OUTSIDE_BEGIN_END(ctx);
255
256 if (face == GL_FRONT || face == GL_BACK) {
257 FLUSH_VERTICES(ctx, _NEW_STENCIL);
258 ctx->Stencil.ActiveFace = (face == GL_FRONT) ? 0 : 1;
259 }
260
261 if (ctx->Driver.ActiveStencilFace) {
262 (*ctx->Driver.ActiveStencilFace)( ctx, (GLuint) ctx->Stencil.ActiveFace );
263 }
264 }
265 #endif
266
267
268
269 /**
270 * OpenGL 2.0 function.
271 * \todo Make StencilOp() call this function. And eventually remove the
272 * ctx->Driver.StencilOp function and use ctx->Driver.StencilOpSeparate
273 * instead.
274 */
275 void GLAPIENTRY
276 _mesa_StencilOpSeparate(GLenum face, GLenum fail, GLenum zfail, GLenum zpass)
277 {
278 GET_CURRENT_CONTEXT(ctx);
279 ASSERT_OUTSIDE_BEGIN_END(ctx);
280
281 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
282 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(face)");
283 return;
284 }
285
286 switch (fail) {
287 case GL_KEEP:
288 case GL_ZERO:
289 case GL_REPLACE:
290 case GL_INCR:
291 case GL_DECR:
292 case GL_INVERT:
293 break;
294 case GL_INCR_WRAP_EXT:
295 case GL_DECR_WRAP_EXT:
296 if (ctx->Extensions.EXT_stencil_wrap) {
297 break;
298 }
299 /* FALL-THROUGH */
300 default:
301 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(fail)");
302 return;
303 }
304 switch (zfail) {
305 case GL_KEEP:
306 case GL_ZERO:
307 case GL_REPLACE:
308 case GL_INCR:
309 case GL_DECR:
310 case GL_INVERT:
311 break;
312 case GL_INCR_WRAP_EXT:
313 case GL_DECR_WRAP_EXT:
314 if (ctx->Extensions.EXT_stencil_wrap) {
315 break;
316 }
317 /* FALL-THROUGH */
318 default:
319 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(zfail)");
320 return;
321 }
322 switch (zpass) {
323 case GL_KEEP:
324 case GL_ZERO:
325 case GL_REPLACE:
326 case GL_INCR:
327 case GL_DECR:
328 case GL_INVERT:
329 break;
330 case GL_INCR_WRAP_EXT:
331 case GL_DECR_WRAP_EXT:
332 if (ctx->Extensions.EXT_stencil_wrap) {
333 break;
334 }
335 /* FALL-THROUGH */
336 default:
337 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(zpass)");
338 return;
339 }
340
341 FLUSH_VERTICES(ctx, _NEW_STENCIL);
342
343 if (face == GL_FRONT || face == GL_FRONT_AND_BACK) {
344 ctx->Stencil.FailFunc[0] = fail;
345 ctx->Stencil.ZFailFunc[0] = zfail;
346 ctx->Stencil.ZPassFunc[0] = zpass;
347 }
348 if (face == GL_BACK || face == GL_FRONT_AND_BACK) {
349 ctx->Stencil.FailFunc[1] = fail;
350 ctx->Stencil.ZFailFunc[1] = zfail;
351 ctx->Stencil.ZPassFunc[1] = zpass;
352 }
353
354 if (ctx->Driver.StencilOpSeparate) {
355 ctx->Driver.StencilOpSeparate(ctx, face, fail, zfail, zpass);
356 }
357 }
358
359
360 /* OpenGL 2.0 */
361 void GLAPIENTRY
362 _mesa_StencilFuncSeparate(GLenum face, GLenum func, GLint ref, GLuint mask)
363 {
364 GET_CURRENT_CONTEXT(ctx);
365 GLint maxref;
366 ASSERT_OUTSIDE_BEGIN_END(ctx);
367
368 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
369 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilFuncSeparate(face)");
370 return;
371 }
372
373 switch (func) {
374 case GL_NEVER:
375 case GL_LESS:
376 case GL_LEQUAL:
377 case GL_GREATER:
378 case GL_GEQUAL:
379 case GL_EQUAL:
380 case GL_NOTEQUAL:
381 case GL_ALWAYS:
382 break;
383 default:
384 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilFuncSeparate(func)");
385 return;
386 }
387
388 maxref = (1 << STENCIL_BITS) - 1;
389 ref = (GLstencil) CLAMP(ref, 0, maxref);
390
391 FLUSH_VERTICES(ctx, _NEW_STENCIL);
392
393 if (face == GL_FRONT || face == GL_FRONT_AND_BACK) {
394 ctx->Stencil.Function[0] = func;
395 ctx->Stencil.Ref[0] = ref;
396 ctx->Stencil.ValueMask[0] = (GLstencil) mask;
397 }
398 if (face == GL_BACK || face == GL_FRONT_AND_BACK) {
399 ctx->Stencil.Function[1] = func;
400 ctx->Stencil.Ref[1] = ref;
401 ctx->Stencil.ValueMask[1] = (GLstencil) mask;
402 }
403
404 if (ctx->Driver.StencilFuncSeparate) {
405 ctx->Driver.StencilFuncSeparate(ctx, face, func, ref, mask);
406 }
407 }
408
409
410 /* OpenGL 2.0 */
411 void GLAPIENTRY
412 _mesa_StencilMaskSeparate(GLenum face, GLuint mask)
413 {
414 GET_CURRENT_CONTEXT(ctx);
415 ASSERT_OUTSIDE_BEGIN_END(ctx);
416
417 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
418 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilaMaskSeparate(face)");
419 return;
420 }
421
422 FLUSH_VERTICES(ctx, _NEW_STENCIL);
423
424 if (face == GL_FRONT || face == GL_FRONT_AND_BACK) {
425 ctx->Stencil.WriteMask[0] = (GLstencil) mask;
426 }
427 if (face == GL_BACK || face == GL_FRONT_AND_BACK) {
428 ctx->Stencil.WriteMask[1] = (GLstencil) mask;
429 }
430
431 if (ctx->Driver.StencilMaskSeparate) {
432 ctx->Driver.StencilMaskSeparate(ctx, face, mask);
433 }
434 }
435
436
437 /**
438 * Initialize the context stipple state.
439 *
440 * \param ctx GL context.
441 *
442 * Initializes __GLcontextRec::Stencil attribute group.
443 */
444 void _mesa_init_stencil( GLcontext * ctx )
445 {
446
447 /* Stencil group */
448 ctx->Stencil.Enabled = GL_FALSE;
449 ctx->Stencil.TestTwoSide = GL_FALSE;
450 ctx->Stencil.ActiveFace = 0; /* 0 = GL_FRONT, 1 = GL_BACK */
451 ctx->Stencil.Function[0] = GL_ALWAYS;
452 ctx->Stencil.Function[1] = GL_ALWAYS;
453 ctx->Stencil.FailFunc[0] = GL_KEEP;
454 ctx->Stencil.FailFunc[1] = GL_KEEP;
455 ctx->Stencil.ZPassFunc[0] = GL_KEEP;
456 ctx->Stencil.ZPassFunc[1] = GL_KEEP;
457 ctx->Stencil.ZFailFunc[0] = GL_KEEP;
458 ctx->Stencil.ZFailFunc[1] = GL_KEEP;
459 ctx->Stencil.Ref[0] = 0;
460 ctx->Stencil.Ref[1] = 0;
461 ctx->Stencil.ValueMask[0] = STENCIL_MAX;
462 ctx->Stencil.ValueMask[1] = STENCIL_MAX;
463 ctx->Stencil.WriteMask[0] = STENCIL_MAX;
464 ctx->Stencil.WriteMask[1] = STENCIL_MAX;
465 ctx->Stencil.Clear = 0;
466 }