OpenGL 2.0's two-sided stencil feature wasn't implemented correctly.
[mesa.git] / src / mesa / main / stencil.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2005 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 /**
27 * \file stencil.c
28 * Stencil operations.
29 *
30 * Note: There's an incompatibility between GL_EXT_stencil_two_side and
31 * OpenGL 2.0's two-sided stencil feature.
32 *
33 * With GL_EXT_stencil_two_side, calling glStencilOp/Func/Mask() only the
34 * front OR back face state (as set by glActiveStencilFaceEXT) is set.
35 *
36 * But with OpenGL 2.0, calling glStencilOp/Func/Mask() sets BOTH the
37 * front AND back state.
38 *
39 * So either we advertise the GL_EXT_stencil_two_side extension, or OpenGL
40 * 2.0, but not both.
41 */
42
43
44 #include "glheader.h"
45 #include "imports.h"
46 #include "context.h"
47 #include "macros.h"
48 #include "stencil.h"
49 #include "mtypes.h"
50
51
52 /**
53 * Set the clear value for the stencil buffer.
54 *
55 * \param s clear value.
56 *
57 * \sa glClearStencil().
58 *
59 * Updates gl_stencil_attrib::Clear. On change
60 * flushes the vertices and notifies the driver via
61 * the dd_function_table::ClearStencil callback.
62 */
63 void GLAPIENTRY
64 _mesa_ClearStencil( GLint s )
65 {
66 GET_CURRENT_CONTEXT(ctx);
67 ASSERT_OUTSIDE_BEGIN_END(ctx);
68
69 if (ctx->Stencil.Clear == (GLuint) s)
70 return;
71
72 FLUSH_VERTICES(ctx, _NEW_STENCIL);
73 ctx->Stencil.Clear = (GLuint) s;
74
75 if (ctx->Driver.ClearStencil) {
76 ctx->Driver.ClearStencil( ctx, s );
77 }
78 }
79
80
81 /**
82 * Set the function and reference value for stencil testing.
83 *
84 * \param func test function.
85 * \param ref reference value.
86 * \param mask bitmask.
87 *
88 * \sa glStencilFunc().
89 *
90 * Verifies the parameters and updates the respective values in
91 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
92 * driver via the dd_function_table::StencilFunc callback.
93 */
94 void GLAPIENTRY
95 _mesa_StencilFunc( GLenum func, GLint ref, GLuint mask )
96 {
97 GET_CURRENT_CONTEXT(ctx);
98 GLint maxref;
99 ASSERT_OUTSIDE_BEGIN_END(ctx);
100
101 switch (func) {
102 case GL_NEVER:
103 case GL_LESS:
104 case GL_LEQUAL:
105 case GL_GREATER:
106 case GL_GEQUAL:
107 case GL_EQUAL:
108 case GL_NOTEQUAL:
109 case GL_ALWAYS:
110 break;
111 default:
112 _mesa_error( ctx, GL_INVALID_ENUM, "glStencilFunc" );
113 return;
114 }
115
116 maxref = (1 << STENCIL_BITS) - 1;
117 ref = CLAMP( ref, 0, maxref );
118
119 if (ctx->Extensions.EXT_stencil_two_side) {
120 /* only set active face state */
121 const GLint face = ctx->Stencil.ActiveFace;
122 if (ctx->Stencil.Function[face] == func &&
123 ctx->Stencil.ValueMask[face] == mask &&
124 ctx->Stencil.Ref[face] == ref)
125 return;
126 FLUSH_VERTICES(ctx, _NEW_STENCIL);
127 ctx->Stencil.Function[face] = func;
128 ctx->Stencil.Ref[face] = ref;
129 ctx->Stencil.ValueMask[face] = mask;
130 }
131 else {
132 /* set both front and back state */
133 if (ctx->Stencil.Function[0] == func &&
134 ctx->Stencil.Function[1] == func &&
135 ctx->Stencil.ValueMask[0] == mask &&
136 ctx->Stencil.ValueMask[1] == mask &&
137 ctx->Stencil.Ref[0] == ref &&
138 ctx->Stencil.Ref[1] == ref)
139 return;
140 FLUSH_VERTICES(ctx, _NEW_STENCIL);
141 ctx->Stencil.Function[0] = ctx->Stencil.Function[1] = func;
142 ctx->Stencil.Ref[0] = ctx->Stencil.Ref[1] = ref;
143 ctx->Stencil.ValueMask[0] = ctx->Stencil.ValueMask[1] = mask;
144 }
145
146 if (ctx->Driver.StencilFunc) {
147 ctx->Driver.StencilFunc( ctx, func, ref, mask );
148 }
149 }
150
151
152 /**
153 * Set the stencil writing mask.
154 *
155 * \param mask bit-mask to enable/disable writing of individual bits in the
156 * stencil planes.
157 *
158 * \sa glStencilMask().
159 *
160 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
161 * notifies the driver via the dd_function_table::StencilMask callback.
162 */
163 void GLAPIENTRY
164 _mesa_StencilMask( GLuint mask )
165 {
166 GET_CURRENT_CONTEXT(ctx);
167 ASSERT_OUTSIDE_BEGIN_END(ctx);
168
169 if (ctx->Extensions.EXT_stencil_two_side) {
170 /* only set active face state */
171 const GLint face = ctx->Stencil.ActiveFace;
172 if (ctx->Stencil.WriteMask[face] == mask)
173 return;
174 FLUSH_VERTICES(ctx, _NEW_STENCIL);
175 ctx->Stencil.WriteMask[face] = mask;
176 }
177 else {
178 /* set both front and back state */
179 if (ctx->Stencil.WriteMask[0] == mask &&
180 ctx->Stencil.WriteMask[1] == mask)
181 return;
182 FLUSH_VERTICES(ctx, _NEW_STENCIL);
183 ctx->Stencil.WriteMask[0] = ctx->Stencil.WriteMask[1] = mask;
184 }
185
186 if (ctx->Driver.StencilMask) {
187 ctx->Driver.StencilMask( ctx, mask );
188 }
189 }
190
191
192 /**
193 * Set the stencil test actions.
194 *
195 * \param fail action to take when stencil test fails.
196 * \param zfail action to take when stencil test passes, but depth test fails.
197 * \param zpass action to take when stencil test passes and the depth test
198 * passes (or depth testing is not enabled).
199 *
200 * \sa glStencilOp().
201 *
202 * Verifies the parameters and updates the respective fields in
203 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
204 * driver via the dd_function_table::StencilOp callback.
205 */
206 void GLAPIENTRY
207 _mesa_StencilOp(GLenum fail, GLenum zfail, GLenum zpass)
208 {
209 GET_CURRENT_CONTEXT(ctx);
210 ASSERT_OUTSIDE_BEGIN_END(ctx);
211
212 switch (fail) {
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 switch (zfail) {
231 case GL_KEEP:
232 case GL_ZERO:
233 case GL_REPLACE:
234 case GL_INCR:
235 case GL_DECR:
236 case GL_INVERT:
237 break;
238 case GL_INCR_WRAP_EXT:
239 case GL_DECR_WRAP_EXT:
240 if (ctx->Extensions.EXT_stencil_wrap) {
241 break;
242 }
243 /* FALL-THROUGH */
244 default:
245 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOp");
246 return;
247 }
248 switch (zpass) {
249 case GL_KEEP:
250 case GL_ZERO:
251 case GL_REPLACE:
252 case GL_INCR:
253 case GL_DECR:
254 case GL_INVERT:
255 break;
256 case GL_INCR_WRAP_EXT:
257 case GL_DECR_WRAP_EXT:
258 if (ctx->Extensions.EXT_stencil_wrap) {
259 break;
260 }
261 /* FALL-THROUGH */
262 default:
263 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOp");
264 return;
265 }
266
267 if (ctx->Extensions.EXT_stencil_two_side) {
268 /* only set active face state */
269 const GLint face = ctx->Stencil.ActiveFace;
270 if (ctx->Stencil.ZFailFunc[face] == zfail &&
271 ctx->Stencil.ZPassFunc[face] == zpass &&
272 ctx->Stencil.FailFunc[face] == fail)
273 return;
274 FLUSH_VERTICES(ctx, _NEW_STENCIL);
275 ctx->Stencil.ZFailFunc[face] = zfail;
276 ctx->Stencil.ZPassFunc[face] = zpass;
277 ctx->Stencil.FailFunc[face] = fail;
278 }
279 else {
280 /* set both front and back state */
281 if (ctx->Stencil.ZFailFunc[0] == zfail &&
282 ctx->Stencil.ZFailFunc[1] == zfail &&
283 ctx->Stencil.ZPassFunc[0] == zpass &&
284 ctx->Stencil.ZPassFunc[1] == zpass &&
285 ctx->Stencil.FailFunc[0] == fail &&
286 ctx->Stencil.FailFunc[1] == fail)
287 return;
288 FLUSH_VERTICES(ctx, _NEW_STENCIL);
289 ctx->Stencil.ZFailFunc[0] = ctx->Stencil.ZFailFunc[1] = zfail;
290 ctx->Stencil.ZPassFunc[0] = ctx->Stencil.ZPassFunc[1] = zpass;
291 ctx->Stencil.FailFunc[0] = ctx->Stencil.FailFunc[1] = fail;
292 }
293
294 if (ctx->Driver.StencilOp) {
295 ctx->Driver.StencilOp(ctx, fail, zfail, zpass);
296 }
297 }
298
299
300
301 #if _HAVE_FULL_GL
302 /* GL_EXT_stencil_two_side */
303 void GLAPIENTRY
304 _mesa_ActiveStencilFaceEXT(GLenum face)
305 {
306 GET_CURRENT_CONTEXT(ctx);
307 ASSERT_OUTSIDE_BEGIN_END(ctx);
308
309 if (!ctx->Extensions.EXT_stencil_two_side) {
310 _mesa_error(ctx, GL_INVALID_OPERATION, "glActiveStencilFaceEXT");
311 return;
312 }
313
314 if (face == GL_FRONT || face == GL_BACK) {
315 FLUSH_VERTICES(ctx, _NEW_STENCIL);
316 ctx->Stencil.ActiveFace = (face == GL_FRONT) ? 0 : 1;
317 }
318 else {
319 _mesa_error(ctx, GL_INVALID_ENUM, "glActiveStencilFaceEXT(face)");
320 return;
321 }
322
323 if (ctx->Driver.ActiveStencilFace) {
324 ctx->Driver.ActiveStencilFace(ctx, (GLuint) ctx->Stencil.ActiveFace);
325 }
326 }
327 #endif
328
329
330
331 /**
332 * OpenGL 2.0 function.
333 * \todo Make StencilOp() call this function. And eventually remove the
334 * ctx->Driver.StencilOp function and use ctx->Driver.StencilOpSeparate
335 * instead.
336 */
337 void GLAPIENTRY
338 _mesa_StencilOpSeparate(GLenum face, GLenum fail, GLenum zfail, GLenum zpass)
339 {
340 GET_CURRENT_CONTEXT(ctx);
341 ASSERT_OUTSIDE_BEGIN_END(ctx);
342
343 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
344 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(face)");
345 return;
346 }
347
348 switch (fail) {
349 case GL_KEEP:
350 case GL_ZERO:
351 case GL_REPLACE:
352 case GL_INCR:
353 case GL_DECR:
354 case GL_INVERT:
355 break;
356 case GL_INCR_WRAP_EXT:
357 case GL_DECR_WRAP_EXT:
358 if (ctx->Extensions.EXT_stencil_wrap) {
359 break;
360 }
361 /* FALL-THROUGH */
362 default:
363 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(fail)");
364 return;
365 }
366 switch (zfail) {
367 case GL_KEEP:
368 case GL_ZERO:
369 case GL_REPLACE:
370 case GL_INCR:
371 case GL_DECR:
372 case GL_INVERT:
373 break;
374 case GL_INCR_WRAP_EXT:
375 case GL_DECR_WRAP_EXT:
376 if (ctx->Extensions.EXT_stencil_wrap) {
377 break;
378 }
379 /* FALL-THROUGH */
380 default:
381 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(zfail)");
382 return;
383 }
384 switch (zpass) {
385 case GL_KEEP:
386 case GL_ZERO:
387 case GL_REPLACE:
388 case GL_INCR:
389 case GL_DECR:
390 case GL_INVERT:
391 break;
392 case GL_INCR_WRAP_EXT:
393 case GL_DECR_WRAP_EXT:
394 if (ctx->Extensions.EXT_stencil_wrap) {
395 break;
396 }
397 /* FALL-THROUGH */
398 default:
399 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(zpass)");
400 return;
401 }
402
403 FLUSH_VERTICES(ctx, _NEW_STENCIL);
404
405 if (face == GL_FRONT || face == GL_FRONT_AND_BACK) {
406 ctx->Stencil.FailFunc[0] = fail;
407 ctx->Stencil.ZFailFunc[0] = zfail;
408 ctx->Stencil.ZPassFunc[0] = zpass;
409 }
410 if (face == GL_BACK || face == GL_FRONT_AND_BACK) {
411 ctx->Stencil.FailFunc[1] = fail;
412 ctx->Stencil.ZFailFunc[1] = zfail;
413 ctx->Stencil.ZPassFunc[1] = zpass;
414 }
415
416 if (ctx->Driver.StencilOpSeparate) {
417 ctx->Driver.StencilOpSeparate(ctx, face, fail, zfail, zpass);
418 }
419 }
420
421
422 /* OpenGL 2.0 */
423 void GLAPIENTRY
424 _mesa_StencilFuncSeparate(GLenum face, GLenum func, GLint ref, GLuint mask)
425 {
426 GET_CURRENT_CONTEXT(ctx);
427 GLint maxref;
428 ASSERT_OUTSIDE_BEGIN_END(ctx);
429
430 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
431 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilFuncSeparate(face)");
432 return;
433 }
434
435 switch (func) {
436 case GL_NEVER:
437 case GL_LESS:
438 case GL_LEQUAL:
439 case GL_GREATER:
440 case GL_GEQUAL:
441 case GL_EQUAL:
442 case GL_NOTEQUAL:
443 case GL_ALWAYS:
444 break;
445 default:
446 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilFuncSeparate(func)");
447 return;
448 }
449
450 maxref = (1 << STENCIL_BITS) - 1;
451 ref = CLAMP(ref, 0, maxref);
452
453 FLUSH_VERTICES(ctx, _NEW_STENCIL);
454
455 if (face == GL_FRONT || face == GL_FRONT_AND_BACK) {
456 ctx->Stencil.Function[0] = func;
457 ctx->Stencil.Ref[0] = ref;
458 ctx->Stencil.ValueMask[0] = mask;
459 }
460 if (face == GL_BACK || face == GL_FRONT_AND_BACK) {
461 ctx->Stencil.Function[1] = func;
462 ctx->Stencil.Ref[1] = ref;
463 ctx->Stencil.ValueMask[1] = mask;
464 }
465
466 if (ctx->Driver.StencilFuncSeparate) {
467 ctx->Driver.StencilFuncSeparate(ctx, face, func, ref, mask);
468 }
469 }
470
471
472 /* OpenGL 2.0 */
473 void GLAPIENTRY
474 _mesa_StencilMaskSeparate(GLenum face, GLuint mask)
475 {
476 GET_CURRENT_CONTEXT(ctx);
477 ASSERT_OUTSIDE_BEGIN_END(ctx);
478
479 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
480 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilaMaskSeparate(face)");
481 return;
482 }
483
484 FLUSH_VERTICES(ctx, _NEW_STENCIL);
485
486 if (face == GL_FRONT || face == GL_FRONT_AND_BACK) {
487 ctx->Stencil.WriteMask[0] = mask;
488 }
489 if (face == GL_BACK || face == GL_FRONT_AND_BACK) {
490 ctx->Stencil.WriteMask[1] = mask;
491 }
492
493 if (ctx->Driver.StencilMaskSeparate) {
494 ctx->Driver.StencilMaskSeparate(ctx, face, mask);
495 }
496 }
497
498
499 /**
500 * Update derived stencil state.
501 */
502 void
503 _mesa_update_stencil(GLcontext *ctx)
504 {
505 if (ctx->Extensions.EXT_stencil_two_side) {
506 ctx->Stencil._TestTwoSide = ctx->Stencil.TestTwoSide;
507 }
508 else {
509 ctx->Stencil._TestTwoSide =
510 (ctx->Stencil.Function[0] != ctx->Stencil.Function[1] ||
511 ctx->Stencil.FailFunc[0] != ctx->Stencil.FailFunc[1] ||
512 ctx->Stencil.ZPassFunc[0] != ctx->Stencil.ZPassFunc[1] ||
513 ctx->Stencil.ZFailFunc[0] != ctx->Stencil.ZFailFunc[1] ||
514 ctx->Stencil.Ref[0] != ctx->Stencil.Ref[1] ||
515 ctx->Stencil.ValueMask[0] != ctx->Stencil.ValueMask[1] ||
516 ctx->Stencil.WriteMask[0] != ctx->Stencil.WriteMask[1]);
517 }
518 }
519
520
521 /**
522 * Initialize the context stipple state.
523 *
524 * \param ctx GL context.
525 *
526 * Initializes __GLcontextRec::Stencil attribute group.
527 */
528 void
529 _mesa_init_stencil(GLcontext *ctx)
530 {
531 ctx->Stencil.Enabled = GL_FALSE;
532 ctx->Stencil.TestTwoSide = GL_FALSE;
533 ctx->Stencil.ActiveFace = 0; /* 0 = GL_FRONT, 1 = GL_BACK */
534 ctx->Stencil.Function[0] = GL_ALWAYS;
535 ctx->Stencil.Function[1] = GL_ALWAYS;
536 ctx->Stencil.FailFunc[0] = GL_KEEP;
537 ctx->Stencil.FailFunc[1] = GL_KEEP;
538 ctx->Stencil.ZPassFunc[0] = GL_KEEP;
539 ctx->Stencil.ZPassFunc[1] = GL_KEEP;
540 ctx->Stencil.ZFailFunc[0] = GL_KEEP;
541 ctx->Stencil.ZFailFunc[1] = GL_KEEP;
542 ctx->Stencil.Ref[0] = 0;
543 ctx->Stencil.Ref[1] = 0;
544 ctx->Stencil.ValueMask[0] = ~0U;
545 ctx->Stencil.ValueMask[1] = ~0U;
546 ctx->Stencil.WriteMask[0] = ~0U;
547 ctx->Stencil.WriteMask[1] = ~0U;
548 ctx->Stencil.Clear = 0;
549 }