2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
30 * Note: There's some conflict between GL_EXT_stencil_two_side and
31 * OpenGL 2.0's two-sided stencil feature.
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.
36 * But with OpenGL 2.0, calling glStencilOp/Func/Mask() sets BOTH the
37 * front AND back state.
39 * Also, note that GL_ATI_separate_stencil is different as well:
40 * glStencilFuncSeparateATI(GLenum frontfunc, GLenum backfunc, ...) vs.
41 * glStencilFuncSeparate(GLenum face, GLenum func, ...).
43 * This problem is solved by keeping three sets of stencil state:
44 * state[0] = GL_FRONT state.
45 * state[1] = OpenGL 2.0 / GL_ATI_separate_stencil GL_BACK state.
46 * state[2] = GL_EXT_stencil_two_side GL_BACK state.
59 validate_stencil_op(struct gl_context
*ctx
, GLenum op
)
78 validate_stencil_func(struct gl_context
*ctx
, GLenum func
)
97 * Set the clear value for the stencil buffer.
99 * \param s clear value.
101 * \sa glClearStencil().
103 * Updates gl_stencil_attrib::Clear. On change
104 * flushes the vertices and notifies the driver via
105 * the dd_function_table::ClearStencil callback.
108 _mesa_ClearStencil( GLint s
)
110 GET_CURRENT_CONTEXT(ctx
);
112 if (MESA_VERBOSE
& VERBOSE_API
)
113 _mesa_debug(ctx
, "glClearStencil(%d)\n", s
);
115 ctx
->Stencil
.Clear
= (GLuint
) s
;
120 * Set the function and reference value for stencil testing.
122 * \param frontfunc front test function.
123 * \param backfunc back test function.
124 * \param ref front and back reference value.
125 * \param mask front and back bitmask.
127 * \sa glStencilFunc().
129 * Verifies the parameters and updates the respective values in
130 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies
131 * the driver via the dd_function_table::StencilFunc callback.
134 _mesa_StencilFuncSeparateATI( GLenum frontfunc
, GLenum backfunc
, GLint ref
, GLuint mask
)
136 GET_CURRENT_CONTEXT(ctx
);
138 if (MESA_VERBOSE
& VERBOSE_API
)
139 _mesa_debug(ctx
, "glStencilFuncSeparateATI()\n");
141 if (!validate_stencil_func(ctx
, frontfunc
)) {
142 _mesa_error(ctx
, GL_INVALID_ENUM
,
143 "glStencilFuncSeparateATI(frontfunc)");
146 if (!validate_stencil_func(ctx
, backfunc
)) {
147 _mesa_error(ctx
, GL_INVALID_ENUM
,
148 "glStencilFuncSeparateATI(backfunc)");
152 /* set both front and back state */
153 if (ctx
->Stencil
.Function
[0] == frontfunc
&&
154 ctx
->Stencil
.Function
[1] == backfunc
&&
155 ctx
->Stencil
.ValueMask
[0] == mask
&&
156 ctx
->Stencil
.ValueMask
[1] == mask
&&
157 ctx
->Stencil
.Ref
[0] == ref
&&
158 ctx
->Stencil
.Ref
[1] == ref
)
160 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
161 ctx
->Stencil
.Function
[0] = frontfunc
;
162 ctx
->Stencil
.Function
[1] = backfunc
;
163 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
164 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
165 if (ctx
->Driver
.StencilFuncSeparate
) {
166 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_FRONT
,
167 frontfunc
, ref
, mask
);
168 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_BACK
,
169 backfunc
, ref
, mask
);
175 * Set the function and reference value for stencil testing.
177 * \param func test function.
178 * \param ref reference value.
179 * \param mask bitmask.
181 * \sa glStencilFunc().
183 * Verifies the parameters and updates the respective values in
184 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies
185 * the driver via the dd_function_table::StencilFunc callback.
188 stencil_func(struct gl_context
*ctx
, GLenum func
, GLint ref
, GLuint mask
)
190 const GLint face
= ctx
->Stencil
.ActiveFace
;
193 if (ctx
->Stencil
.Function
[face
] == func
&&
194 ctx
->Stencil
.ValueMask
[face
] == mask
&&
195 ctx
->Stencil
.Ref
[face
] == ref
)
197 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
198 ctx
->Stencil
.Function
[face
] = func
;
199 ctx
->Stencil
.Ref
[face
] = ref
;
200 ctx
->Stencil
.ValueMask
[face
] = mask
;
202 /* Only propagate the change to the driver if EXT_stencil_two_side
205 if (ctx
->Driver
.StencilFuncSeparate
&& ctx
->Stencil
.TestTwoSide
) {
206 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_BACK
, func
, ref
, mask
);
210 /* set both front and back state */
211 if (ctx
->Stencil
.Function
[0] == func
&&
212 ctx
->Stencil
.Function
[1] == func
&&
213 ctx
->Stencil
.ValueMask
[0] == mask
&&
214 ctx
->Stencil
.ValueMask
[1] == mask
&&
215 ctx
->Stencil
.Ref
[0] == ref
&&
216 ctx
->Stencil
.Ref
[1] == ref
)
218 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
219 ctx
->Stencil
.Function
[0] = ctx
->Stencil
.Function
[1] = func
;
220 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
221 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
222 if (ctx
->Driver
.StencilFuncSeparate
) {
223 ctx
->Driver
.StencilFuncSeparate(ctx
,
224 ((ctx
->Stencil
.TestTwoSide
)
225 ? GL_FRONT
: GL_FRONT_AND_BACK
),
233 _mesa_StencilFunc(GLenum func
, GLint ref
, GLuint mask
)
235 GET_CURRENT_CONTEXT(ctx
);
237 if (MESA_VERBOSE
& VERBOSE_API
)
238 _mesa_debug(ctx
, "glStencilFunc()\n");
240 if (!validate_stencil_func(ctx
, func
)) {
241 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFunc(func)");
245 stencil_func(ctx
, func
, ref
, mask
);
250 * Set the stencil writing mask.
252 * \param mask bit-mask to enable/disable writing of individual bits in the
255 * \sa glStencilMask().
257 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
258 * notifies the driver via the dd_function_table::StencilMask callback.
261 _mesa_StencilMask( GLuint mask
)
263 GET_CURRENT_CONTEXT(ctx
);
264 const GLint face
= ctx
->Stencil
.ActiveFace
;
266 if (MESA_VERBOSE
& VERBOSE_API
)
267 _mesa_debug(ctx
, "glStencilMask()\n");
270 /* Only modify the EXT_stencil_two_side back-face state.
272 if (ctx
->Stencil
.WriteMask
[face
] == mask
)
274 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
275 ctx
->Stencil
.WriteMask
[face
] = mask
;
277 /* Only propagate the change to the driver if EXT_stencil_two_side
280 if (ctx
->Driver
.StencilMaskSeparate
&& ctx
->Stencil
.TestTwoSide
) {
281 ctx
->Driver
.StencilMaskSeparate(ctx
, GL_BACK
, mask
);
285 /* set both front and back state */
286 if (ctx
->Stencil
.WriteMask
[0] == mask
&&
287 ctx
->Stencil
.WriteMask
[1] == mask
)
289 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
290 ctx
->Stencil
.WriteMask
[0] = ctx
->Stencil
.WriteMask
[1] = mask
;
291 if (ctx
->Driver
.StencilMaskSeparate
) {
292 ctx
->Driver
.StencilMaskSeparate(ctx
,
293 ((ctx
->Stencil
.TestTwoSide
)
294 ? GL_FRONT
: GL_FRONT_AND_BACK
),
302 * Set the stencil test actions.
304 * \param fail action to take when stencil test fails.
305 * \param zfail action to take when stencil test passes, but depth test fails.
306 * \param zpass action to take when stencil test passes and the depth test
307 * passes (or depth testing is not enabled).
311 * Verifies the parameters and updates the respective fields in
312 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies
313 * the driver via the dd_function_table::StencilOp callback.
316 _mesa_StencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
)
318 GET_CURRENT_CONTEXT(ctx
);
319 const GLint face
= ctx
->Stencil
.ActiveFace
;
321 if (MESA_VERBOSE
& VERBOSE_API
)
322 _mesa_debug(ctx
, "glStencilOp()\n");
324 if (!validate_stencil_op(ctx
, fail
)) {
325 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(sfail)");
328 if (!validate_stencil_op(ctx
, zfail
)) {
329 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zfail)");
332 if (!validate_stencil_op(ctx
, zpass
)) {
333 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zpass)");
338 /* only set active face state */
339 if (ctx
->Stencil
.ZFailFunc
[face
] == zfail
&&
340 ctx
->Stencil
.ZPassFunc
[face
] == zpass
&&
341 ctx
->Stencil
.FailFunc
[face
] == fail
)
343 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
344 ctx
->Stencil
.ZFailFunc
[face
] = zfail
;
345 ctx
->Stencil
.ZPassFunc
[face
] = zpass
;
346 ctx
->Stencil
.FailFunc
[face
] = fail
;
348 /* Only propagate the change to the driver if EXT_stencil_two_side
351 if (ctx
->Driver
.StencilOpSeparate
&& ctx
->Stencil
.TestTwoSide
) {
352 ctx
->Driver
.StencilOpSeparate(ctx
, GL_BACK
, fail
, zfail
, zpass
);
356 /* set both front and back state */
357 if (ctx
->Stencil
.ZFailFunc
[0] == zfail
&&
358 ctx
->Stencil
.ZFailFunc
[1] == zfail
&&
359 ctx
->Stencil
.ZPassFunc
[0] == zpass
&&
360 ctx
->Stencil
.ZPassFunc
[1] == zpass
&&
361 ctx
->Stencil
.FailFunc
[0] == fail
&&
362 ctx
->Stencil
.FailFunc
[1] == fail
)
364 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
365 ctx
->Stencil
.ZFailFunc
[0] = ctx
->Stencil
.ZFailFunc
[1] = zfail
;
366 ctx
->Stencil
.ZPassFunc
[0] = ctx
->Stencil
.ZPassFunc
[1] = zpass
;
367 ctx
->Stencil
.FailFunc
[0] = ctx
->Stencil
.FailFunc
[1] = fail
;
368 if (ctx
->Driver
.StencilOpSeparate
) {
369 ctx
->Driver
.StencilOpSeparate(ctx
,
370 ((ctx
->Stencil
.TestTwoSide
)
371 ? GL_FRONT
: GL_FRONT_AND_BACK
),
379 /* GL_EXT_stencil_two_side */
381 _mesa_ActiveStencilFaceEXT(GLenum face
)
383 GET_CURRENT_CONTEXT(ctx
);
385 if (MESA_VERBOSE
& VERBOSE_API
)
386 _mesa_debug(ctx
, "glActiveStencilFaceEXT()\n");
388 if (!ctx
->Extensions
.EXT_stencil_two_side
) {
389 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveStencilFaceEXT");
393 if (face
== GL_FRONT
|| face
== GL_BACK
) {
394 ctx
->Stencil
.ActiveFace
= (face
== GL_FRONT
) ? 0 : 2;
397 _mesa_error(ctx
, GL_INVALID_ENUM
, "glActiveStencilFaceEXT(face)");
403 stencil_op_separate(struct gl_context
*ctx
, GLenum face
, GLenum sfail
,
404 GLenum zfail
, GLenum zpass
)
406 GLboolean set
= GL_FALSE
;
408 if (face
!= GL_BACK
) {
410 if (ctx
->Stencil
.ZFailFunc
[0] != zfail
||
411 ctx
->Stencil
.ZPassFunc
[0] != zpass
||
412 ctx
->Stencil
.FailFunc
[0] != sfail
){
413 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
414 ctx
->Stencil
.ZFailFunc
[0] = zfail
;
415 ctx
->Stencil
.ZPassFunc
[0] = zpass
;
416 ctx
->Stencil
.FailFunc
[0] = sfail
;
421 if (face
!= GL_FRONT
) {
423 if (ctx
->Stencil
.ZFailFunc
[1] != zfail
||
424 ctx
->Stencil
.ZPassFunc
[1] != zpass
||
425 ctx
->Stencil
.FailFunc
[1] != sfail
) {
426 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
427 ctx
->Stencil
.ZFailFunc
[1] = zfail
;
428 ctx
->Stencil
.ZPassFunc
[1] = zpass
;
429 ctx
->Stencil
.FailFunc
[1] = sfail
;
434 if (set
&& ctx
->Driver
.StencilOpSeparate
) {
435 ctx
->Driver
.StencilOpSeparate(ctx
, face
, sfail
, zfail
, zpass
);
441 _mesa_StencilOpSeparate_no_error(GLenum face
, GLenum sfail
, GLenum zfail
,
444 GET_CURRENT_CONTEXT(ctx
);
445 stencil_op_separate(ctx
, face
, sfail
, zfail
, zpass
);
450 _mesa_StencilOpSeparate(GLenum face
, GLenum sfail
, GLenum zfail
, GLenum zpass
)
452 GET_CURRENT_CONTEXT(ctx
);
454 if (MESA_VERBOSE
& VERBOSE_API
)
455 _mesa_debug(ctx
, "glStencilOpSeparate()\n");
457 if (!validate_stencil_op(ctx
, sfail
)) {
458 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(sfail)");
462 if (!validate_stencil_op(ctx
, zfail
)) {
463 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zfail)");
467 if (!validate_stencil_op(ctx
, zpass
)) {
468 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zpass)");
472 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
473 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(face)");
477 stencil_op_separate(ctx
, face
, sfail
, zfail
, zpass
);
482 stencil_func_separate(struct gl_context
*ctx
, GLenum face
, GLenum func
,
483 GLint ref
, GLuint mask
)
485 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
487 if (face
!= GL_BACK
) {
489 ctx
->Stencil
.Function
[0] = func
;
490 ctx
->Stencil
.Ref
[0] = ref
;
491 ctx
->Stencil
.ValueMask
[0] = mask
;
494 if (face
!= GL_FRONT
) {
496 ctx
->Stencil
.Function
[1] = func
;
497 ctx
->Stencil
.Ref
[1] = ref
;
498 ctx
->Stencil
.ValueMask
[1] = mask
;
501 if (ctx
->Driver
.StencilFuncSeparate
) {
502 ctx
->Driver
.StencilFuncSeparate(ctx
, face
, func
, ref
, mask
);
509 _mesa_StencilFuncSeparate_no_error(GLenum face
, GLenum func
, GLint ref
,
512 GET_CURRENT_CONTEXT(ctx
);
513 stencil_func_separate(ctx
, face
, func
, ref
, mask
);
518 _mesa_StencilFuncSeparate(GLenum face
, GLenum func
, GLint ref
, GLuint mask
)
520 GET_CURRENT_CONTEXT(ctx
);
522 if (MESA_VERBOSE
& VERBOSE_API
)
523 _mesa_debug(ctx
, "glStencilFuncSeparate()\n");
525 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
526 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(face)");
530 if (!validate_stencil_func(ctx
, func
)) {
531 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(func)");
535 stencil_func_separate(ctx
, face
, func
, ref
, mask
);
540 stencil_mask_separate(struct gl_context
*ctx
, GLenum face
, GLuint mask
)
542 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
544 if (face
!= GL_BACK
) {
545 ctx
->Stencil
.WriteMask
[0] = mask
;
548 if (face
!= GL_FRONT
) {
549 ctx
->Stencil
.WriteMask
[1] = mask
;
552 if (ctx
->Driver
.StencilMaskSeparate
) {
553 ctx
->Driver
.StencilMaskSeparate(ctx
, face
, mask
);
560 _mesa_StencilMaskSeparate_no_error(GLenum face
, GLuint mask
)
562 GET_CURRENT_CONTEXT(ctx
);
563 stencil_mask_separate(ctx
, face
, mask
);
568 _mesa_StencilMaskSeparate(GLenum face
, GLuint mask
)
570 GET_CURRENT_CONTEXT(ctx
);
572 if (MESA_VERBOSE
& VERBOSE_API
)
573 _mesa_debug(ctx
, "glStencilMaskSeparate()\n");
575 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
576 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilaMaskSeparate(face)");
580 stencil_mask_separate(ctx
, face
, mask
);
585 * Update derived stencil state.
588 _mesa_update_stencil(struct gl_context
*ctx
)
590 const GLint face
= ctx
->Stencil
._BackFace
;
592 ctx
->Stencil
._Enabled
= (ctx
->Stencil
.Enabled
&&
593 ctx
->DrawBuffer
->Visual
.stencilBits
> 0);
595 ctx
->Stencil
._TestTwoSide
=
596 ctx
->Stencil
._Enabled
&&
597 (ctx
->Stencil
.Function
[0] != ctx
->Stencil
.Function
[face
] ||
598 ctx
->Stencil
.FailFunc
[0] != ctx
->Stencil
.FailFunc
[face
] ||
599 ctx
->Stencil
.ZPassFunc
[0] != ctx
->Stencil
.ZPassFunc
[face
] ||
600 ctx
->Stencil
.ZFailFunc
[0] != ctx
->Stencil
.ZFailFunc
[face
] ||
601 ctx
->Stencil
.Ref
[0] != ctx
->Stencil
.Ref
[face
] ||
602 ctx
->Stencil
.ValueMask
[0] != ctx
->Stencil
.ValueMask
[face
] ||
603 ctx
->Stencil
.WriteMask
[0] != ctx
->Stencil
.WriteMask
[face
]);
605 ctx
->Stencil
._WriteEnabled
=
606 ctx
->Stencil
._Enabled
&&
607 (ctx
->Stencil
.WriteMask
[0] != 0 ||
608 (ctx
->Stencil
._TestTwoSide
&& ctx
->Stencil
.WriteMask
[face
] != 0));
613 * Initialize the context stipple state.
615 * \param ctx GL context.
617 * Initializes __struct gl_contextRec::Stencil attribute group.
620 _mesa_init_stencil(struct gl_context
*ctx
)
622 ctx
->Stencil
.Enabled
= GL_FALSE
;
623 ctx
->Stencil
.TestTwoSide
= GL_FALSE
;
624 ctx
->Stencil
.ActiveFace
= 0; /* 0 = GL_FRONT, 2 = GL_BACK */
625 ctx
->Stencil
.Function
[0] = GL_ALWAYS
;
626 ctx
->Stencil
.Function
[1] = GL_ALWAYS
;
627 ctx
->Stencil
.Function
[2] = GL_ALWAYS
;
628 ctx
->Stencil
.FailFunc
[0] = GL_KEEP
;
629 ctx
->Stencil
.FailFunc
[1] = GL_KEEP
;
630 ctx
->Stencil
.FailFunc
[2] = GL_KEEP
;
631 ctx
->Stencil
.ZPassFunc
[0] = GL_KEEP
;
632 ctx
->Stencil
.ZPassFunc
[1] = GL_KEEP
;
633 ctx
->Stencil
.ZPassFunc
[2] = GL_KEEP
;
634 ctx
->Stencil
.ZFailFunc
[0] = GL_KEEP
;
635 ctx
->Stencil
.ZFailFunc
[1] = GL_KEEP
;
636 ctx
->Stencil
.ZFailFunc
[2] = GL_KEEP
;
637 ctx
->Stencil
.Ref
[0] = 0;
638 ctx
->Stencil
.Ref
[1] = 0;
639 ctx
->Stencil
.Ref
[2] = 0;
641 /* 4.1.4 Stencil Test section of the GL-ES 3.0 specification says:
643 * "In the initial state, [...] the front and back stencil mask are both
644 * set to the value 2^s − 1, where s is greater than or equal to the
645 * number of bits in the deepest stencil buffer* supported by the GL
648 * Since the maximum supported precision for stencil buffers is 8 bits,
649 * mask values should be initialized to 2^8 - 1 = 0xFF.
651 ctx
->Stencil
.ValueMask
[0] = 0xFF;
652 ctx
->Stencil
.ValueMask
[1] = 0xFF;
653 ctx
->Stencil
.ValueMask
[2] = 0xFF;
654 ctx
->Stencil
.WriteMask
[0] = 0xFF;
655 ctx
->Stencil
.WriteMask
[1] = 0xFF;
656 ctx
->Stencil
.WriteMask
[2] = 0xFF;
658 ctx
->Stencil
.Clear
= 0;
659 ctx
->Stencil
._BackFace
= 1;