2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
30 * Note: There's an incompatibility 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 * So either we advertise the GL_EXT_stencil_two_side extension, or OpenGL
53 * Set the clear value for the stencil buffer.
55 * \param s clear value.
57 * \sa glClearStencil().
59 * Updates gl_stencil_attrib::Clear. On change
60 * flushes the vertices and notifies the driver via
61 * the dd_function_table::ClearStencil callback.
64 _mesa_ClearStencil( GLint s
)
66 GET_CURRENT_CONTEXT(ctx
);
67 ASSERT_OUTSIDE_BEGIN_END(ctx
);
69 if (ctx
->Stencil
.Clear
== (GLuint
) s
)
72 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
73 ctx
->Stencil
.Clear
= (GLuint
) s
;
75 if (ctx
->Driver
.ClearStencil
) {
76 ctx
->Driver
.ClearStencil( ctx
, s
);
82 * Set the function and reference value for stencil testing.
84 * \param func test function.
85 * \param ref reference value.
86 * \param mask bitmask.
88 * \sa glStencilFunc().
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.
95 _mesa_StencilFunc( GLenum func
, GLint ref
, GLuint mask
)
97 GET_CURRENT_CONTEXT(ctx
);
99 ASSERT_OUTSIDE_BEGIN_END(ctx
);
112 _mesa_error( ctx
, GL_INVALID_ENUM
, "glStencilFunc" );
116 maxref
= (1 << STENCIL_BITS
) - 1;
117 ref
= CLAMP( ref
, 0, maxref
);
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
)
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 if (ctx
->Driver
.StencilFuncSeparate
) {
131 ctx
->Driver
.StencilFuncSeparate(ctx
, face
? GL_BACK
: GL_FRONT
,
136 /* set both front and back state */
137 if (ctx
->Stencil
.Function
[0] == func
&&
138 ctx
->Stencil
.Function
[1] == func
&&
139 ctx
->Stencil
.ValueMask
[0] == mask
&&
140 ctx
->Stencil
.ValueMask
[1] == mask
&&
141 ctx
->Stencil
.Ref
[0] == ref
&&
142 ctx
->Stencil
.Ref
[1] == ref
)
144 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
145 ctx
->Stencil
.Function
[0] = ctx
->Stencil
.Function
[1] = func
;
146 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
147 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
148 if (ctx
->Driver
.StencilFuncSeparate
) {
149 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_FRONT_AND_BACK
,
157 * Set the stencil writing mask.
159 * \param mask bit-mask to enable/disable writing of individual bits in the
162 * \sa glStencilMask().
164 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
165 * notifies the driver via the dd_function_table::StencilMask callback.
168 _mesa_StencilMask( GLuint mask
)
170 GET_CURRENT_CONTEXT(ctx
);
171 ASSERT_OUTSIDE_BEGIN_END(ctx
);
173 if (ctx
->Extensions
.EXT_stencil_two_side
) {
174 /* only set active face state */
175 const GLint face
= ctx
->Stencil
.ActiveFace
;
176 if (ctx
->Stencil
.WriteMask
[face
] == mask
)
178 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
179 ctx
->Stencil
.WriteMask
[face
] = mask
;
180 if (ctx
->Driver
.StencilMaskSeparate
) {
181 ctx
->Driver
.StencilMaskSeparate(ctx
, face
? GL_BACK
: GL_FRONT
, mask
);
185 /* set both front and back state */
186 if (ctx
->Stencil
.WriteMask
[0] == mask
&&
187 ctx
->Stencil
.WriteMask
[1] == mask
)
189 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
190 ctx
->Stencil
.WriteMask
[0] = ctx
->Stencil
.WriteMask
[1] = mask
;
191 if (ctx
->Driver
.StencilMaskSeparate
) {
192 ctx
->Driver
.StencilMaskSeparate(ctx
, GL_FRONT_AND_BACK
, mask
);
199 * Set the stencil test actions.
201 * \param fail action to take when stencil test fails.
202 * \param zfail action to take when stencil test passes, but depth test fails.
203 * \param zpass action to take when stencil test passes and the depth test
204 * passes (or depth testing is not enabled).
208 * Verifies the parameters and updates the respective fields in
209 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
210 * driver via the dd_function_table::StencilOp callback.
213 _mesa_StencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
)
215 GET_CURRENT_CONTEXT(ctx
);
216 ASSERT_OUTSIDE_BEGIN_END(ctx
);
226 case GL_INCR_WRAP_EXT
:
227 case GL_DECR_WRAP_EXT
:
228 if (ctx
->Extensions
.EXT_stencil_wrap
) {
233 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp");
244 case GL_INCR_WRAP_EXT
:
245 case GL_DECR_WRAP_EXT
:
246 if (ctx
->Extensions
.EXT_stencil_wrap
) {
251 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp");
262 case GL_INCR_WRAP_EXT
:
263 case GL_DECR_WRAP_EXT
:
264 if (ctx
->Extensions
.EXT_stencil_wrap
) {
269 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp");
273 if (ctx
->Extensions
.EXT_stencil_two_side
) {
274 /* only set active face state */
275 const GLint face
= ctx
->Stencil
.ActiveFace
;
276 if (ctx
->Stencil
.ZFailFunc
[face
] == zfail
&&
277 ctx
->Stencil
.ZPassFunc
[face
] == zpass
&&
278 ctx
->Stencil
.FailFunc
[face
] == fail
)
280 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
281 ctx
->Stencil
.ZFailFunc
[face
] = zfail
;
282 ctx
->Stencil
.ZPassFunc
[face
] = zpass
;
283 ctx
->Stencil
.FailFunc
[face
] = fail
;
284 if (ctx
->Driver
.StencilOpSeparate
) {
285 ctx
->Driver
.StencilOpSeparate(ctx
, face
? GL_BACK
: GL_FRONT
,
290 /* set both front and back state */
291 if (ctx
->Stencil
.ZFailFunc
[0] == zfail
&&
292 ctx
->Stencil
.ZFailFunc
[1] == zfail
&&
293 ctx
->Stencil
.ZPassFunc
[0] == zpass
&&
294 ctx
->Stencil
.ZPassFunc
[1] == zpass
&&
295 ctx
->Stencil
.FailFunc
[0] == fail
&&
296 ctx
->Stencil
.FailFunc
[1] == fail
)
298 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
299 ctx
->Stencil
.ZFailFunc
[0] = ctx
->Stencil
.ZFailFunc
[1] = zfail
;
300 ctx
->Stencil
.ZPassFunc
[0] = ctx
->Stencil
.ZPassFunc
[1] = zpass
;
301 ctx
->Stencil
.FailFunc
[0] = ctx
->Stencil
.FailFunc
[1] = fail
;
302 if (ctx
->Driver
.StencilOpSeparate
) {
303 ctx
->Driver
.StencilOpSeparate(ctx
, GL_FRONT_AND_BACK
,
312 /* GL_EXT_stencil_two_side */
314 _mesa_ActiveStencilFaceEXT(GLenum face
)
316 GET_CURRENT_CONTEXT(ctx
);
317 ASSERT_OUTSIDE_BEGIN_END(ctx
);
319 if (!ctx
->Extensions
.EXT_stencil_two_side
) {
320 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveStencilFaceEXT");
324 if (face
== GL_FRONT
|| face
== GL_BACK
) {
325 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
326 ctx
->Stencil
.ActiveFace
= (face
== GL_FRONT
) ? 0 : 1;
329 _mesa_error(ctx
, GL_INVALID_ENUM
, "glActiveStencilFaceEXT(face)");
337 * OpenGL 2.0 function.
338 * \todo Make StencilOp() call this function. And eventually remove the
339 * ctx->Driver.StencilOp function and use ctx->Driver.StencilOpSeparate
343 _mesa_StencilOpSeparate(GLenum face
, GLenum fail
, GLenum zfail
, GLenum zpass
)
345 GET_CURRENT_CONTEXT(ctx
);
346 ASSERT_OUTSIDE_BEGIN_END(ctx
);
348 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
349 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(face)");
361 case GL_INCR_WRAP_EXT
:
362 case GL_DECR_WRAP_EXT
:
363 if (ctx
->Extensions
.EXT_stencil_wrap
) {
368 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(fail)");
379 case GL_INCR_WRAP_EXT
:
380 case GL_DECR_WRAP_EXT
:
381 if (ctx
->Extensions
.EXT_stencil_wrap
) {
386 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zfail)");
397 case GL_INCR_WRAP_EXT
:
398 case GL_DECR_WRAP_EXT
:
399 if (ctx
->Extensions
.EXT_stencil_wrap
) {
404 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zpass)");
408 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
410 if (face
== GL_FRONT
|| face
== GL_FRONT_AND_BACK
) {
411 ctx
->Stencil
.FailFunc
[0] = fail
;
412 ctx
->Stencil
.ZFailFunc
[0] = zfail
;
413 ctx
->Stencil
.ZPassFunc
[0] = zpass
;
415 if (face
== GL_BACK
|| face
== GL_FRONT_AND_BACK
) {
416 ctx
->Stencil
.FailFunc
[1] = fail
;
417 ctx
->Stencil
.ZFailFunc
[1] = zfail
;
418 ctx
->Stencil
.ZPassFunc
[1] = zpass
;
421 if (ctx
->Driver
.StencilOpSeparate
) {
422 ctx
->Driver
.StencilOpSeparate(ctx
, face
, fail
, zfail
, zpass
);
429 _mesa_StencilFuncSeparate(GLenum face
, GLenum func
, GLint ref
, GLuint mask
)
431 GET_CURRENT_CONTEXT(ctx
);
433 ASSERT_OUTSIDE_BEGIN_END(ctx
);
435 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
436 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(face)");
451 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(func)");
455 maxref
= (1 << STENCIL_BITS
) - 1;
456 ref
= CLAMP(ref
, 0, maxref
);
458 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
460 if (face
== GL_FRONT
|| face
== GL_FRONT_AND_BACK
) {
461 ctx
->Stencil
.Function
[0] = func
;
462 ctx
->Stencil
.Ref
[0] = ref
;
463 ctx
->Stencil
.ValueMask
[0] = mask
;
465 if (face
== GL_BACK
|| face
== GL_FRONT_AND_BACK
) {
466 ctx
->Stencil
.Function
[1] = func
;
467 ctx
->Stencil
.Ref
[1] = ref
;
468 ctx
->Stencil
.ValueMask
[1] = mask
;
471 if (ctx
->Driver
.StencilFuncSeparate
) {
472 ctx
->Driver
.StencilFuncSeparate(ctx
, face
, func
, ref
, mask
);
479 _mesa_StencilMaskSeparate(GLenum face
, GLuint mask
)
481 GET_CURRENT_CONTEXT(ctx
);
482 ASSERT_OUTSIDE_BEGIN_END(ctx
);
484 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
485 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilaMaskSeparate(face)");
489 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
491 if (face
== GL_FRONT
|| face
== GL_FRONT_AND_BACK
) {
492 ctx
->Stencil
.WriteMask
[0] = mask
;
494 if (face
== GL_BACK
|| face
== GL_FRONT_AND_BACK
) {
495 ctx
->Stencil
.WriteMask
[1] = mask
;
498 if (ctx
->Driver
.StencilMaskSeparate
) {
499 ctx
->Driver
.StencilMaskSeparate(ctx
, face
, mask
);
505 * Update derived stencil state.
508 _mesa_update_stencil(GLcontext
*ctx
)
510 if (ctx
->Extensions
.EXT_stencil_two_side
) {
511 ctx
->Stencil
._TestTwoSide
= ctx
->Stencil
.TestTwoSide
;
514 ctx
->Stencil
._TestTwoSide
=
515 (ctx
->Stencil
.Function
[0] != ctx
->Stencil
.Function
[1] ||
516 ctx
->Stencil
.FailFunc
[0] != ctx
->Stencil
.FailFunc
[1] ||
517 ctx
->Stencil
.ZPassFunc
[0] != ctx
->Stencil
.ZPassFunc
[1] ||
518 ctx
->Stencil
.ZFailFunc
[0] != ctx
->Stencil
.ZFailFunc
[1] ||
519 ctx
->Stencil
.Ref
[0] != ctx
->Stencil
.Ref
[1] ||
520 ctx
->Stencil
.ValueMask
[0] != ctx
->Stencil
.ValueMask
[1] ||
521 ctx
->Stencil
.WriteMask
[0] != ctx
->Stencil
.WriteMask
[1]);
527 * Initialize the context stipple state.
529 * \param ctx GL context.
531 * Initializes __GLcontextRec::Stencil attribute group.
534 _mesa_init_stencil(GLcontext
*ctx
)
536 ctx
->Stencil
.Enabled
= GL_FALSE
;
537 ctx
->Stencil
.TestTwoSide
= GL_FALSE
;
538 ctx
->Stencil
.ActiveFace
= 0; /* 0 = GL_FRONT, 1 = GL_BACK */
539 ctx
->Stencil
.Function
[0] = GL_ALWAYS
;
540 ctx
->Stencil
.Function
[1] = GL_ALWAYS
;
541 ctx
->Stencil
.FailFunc
[0] = GL_KEEP
;
542 ctx
->Stencil
.FailFunc
[1] = GL_KEEP
;
543 ctx
->Stencil
.ZPassFunc
[0] = GL_KEEP
;
544 ctx
->Stencil
.ZPassFunc
[1] = GL_KEEP
;
545 ctx
->Stencil
.ZFailFunc
[0] = GL_KEEP
;
546 ctx
->Stencil
.ZFailFunc
[1] = GL_KEEP
;
547 ctx
->Stencil
.Ref
[0] = 0;
548 ctx
->Stencil
.Ref
[1] = 0;
549 ctx
->Stencil
.ValueMask
[0] = ~0U;
550 ctx
->Stencil
.ValueMask
[1] = ~0U;
551 ctx
->Stencil
.WriteMask
[0] = ~0U;
552 ctx
->Stencil
.WriteMask
[1] = ~0U;
553 ctx
->Stencil
.Clear
= 0;