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
);
98 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
99 ASSERT_OUTSIDE_BEGIN_END(ctx
);
112 _mesa_error( ctx
, GL_INVALID_ENUM
, "glStencilFunc (0x%04x)", func
);
116 ref
= CLAMP( ref
, 0, stencilMax
);
118 if (ctx
->Extensions
.ATI_separate_stencil
) {
119 /* set both front and back state */
120 if (ctx
->Stencil
.Function
[0] == func
&&
121 ctx
->Stencil
.Function
[1] == func
&&
122 ctx
->Stencil
.ValueMask
[0] == mask
&&
123 ctx
->Stencil
.ValueMask
[1] == mask
&&
124 ctx
->Stencil
.Ref
[0] == ref
&&
125 ctx
->Stencil
.Ref
[1] == ref
)
127 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
128 ctx
->Stencil
.Function
[0] = ctx
->Stencil
.Function
[1] = func
;
129 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
130 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
131 if (ctx
->Driver
.StencilFuncSeparate
) {
132 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_FRONT_AND_BACK
,
137 /* only set active face state */
138 const GLint face
= ctx
->Stencil
.ActiveFace
;
139 if (ctx
->Stencil
.Function
[face
] == func
&&
140 ctx
->Stencil
.ValueMask
[face
] == mask
&&
141 ctx
->Stencil
.Ref
[face
] == ref
)
143 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
144 ctx
->Stencil
.Function
[face
] = func
;
145 ctx
->Stencil
.Ref
[face
] = ref
;
146 ctx
->Stencil
.ValueMask
[face
] = mask
;
147 if (ctx
->Driver
.StencilFuncSeparate
) {
148 ctx
->Driver
.StencilFuncSeparate(ctx
, face
? GL_BACK
: GL_FRONT
,
156 * Set the stencil writing mask.
158 * \param mask bit-mask to enable/disable writing of individual bits in the
161 * \sa glStencilMask().
163 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
164 * notifies the driver via the dd_function_table::StencilMask callback.
167 _mesa_StencilMask( GLuint mask
)
169 GET_CURRENT_CONTEXT(ctx
);
170 ASSERT_OUTSIDE_BEGIN_END(ctx
);
172 if (ctx
->Extensions
.ATI_separate_stencil
) {
173 /* set both front and back state */
174 if (ctx
->Stencil
.WriteMask
[0] == mask
&&
175 ctx
->Stencil
.WriteMask
[1] == mask
)
177 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
178 ctx
->Stencil
.WriteMask
[0] = ctx
->Stencil
.WriteMask
[1] = mask
;
179 if (ctx
->Driver
.StencilMaskSeparate
) {
180 ctx
->Driver
.StencilMaskSeparate(ctx
, GL_FRONT_AND_BACK
, mask
);
184 /* only set active face state */
185 const GLint face
= ctx
->Stencil
.ActiveFace
;
186 if (ctx
->Stencil
.WriteMask
[face
] == mask
)
188 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
189 ctx
->Stencil
.WriteMask
[face
] = mask
;
190 if (ctx
->Driver
.StencilMaskSeparate
) {
191 ctx
->Driver
.StencilMaskSeparate(ctx
, face
? GL_BACK
: GL_FRONT
, mask
);
198 * Set the stencil test actions.
200 * \param fail action to take when stencil test fails.
201 * \param zfail action to take when stencil test passes, but depth test fails.
202 * \param zpass action to take when stencil test passes and the depth test
203 * passes (or depth testing is not enabled).
207 * Verifies the parameters and updates the respective fields in
208 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
209 * driver via the dd_function_table::StencilOp callback.
212 _mesa_StencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
)
214 GET_CURRENT_CONTEXT(ctx
);
215 ASSERT_OUTSIDE_BEGIN_END(ctx
);
225 case GL_INCR_WRAP_EXT
:
226 case GL_DECR_WRAP_EXT
:
227 if (ctx
->Extensions
.EXT_stencil_wrap
) {
232 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp");
243 case GL_INCR_WRAP_EXT
:
244 case GL_DECR_WRAP_EXT
:
245 if (ctx
->Extensions
.EXT_stencil_wrap
) {
250 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp");
261 case GL_INCR_WRAP_EXT
:
262 case GL_DECR_WRAP_EXT
:
263 if (ctx
->Extensions
.EXT_stencil_wrap
) {
268 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp");
272 if (ctx
->Extensions
.ATI_separate_stencil
) {
273 /* set both front and back state */
274 if (ctx
->Stencil
.ZFailFunc
[0] == zfail
&&
275 ctx
->Stencil
.ZFailFunc
[1] == zfail
&&
276 ctx
->Stencil
.ZPassFunc
[0] == zpass
&&
277 ctx
->Stencil
.ZPassFunc
[1] == zpass
&&
278 ctx
->Stencil
.FailFunc
[0] == fail
&&
279 ctx
->Stencil
.FailFunc
[1] == fail
)
281 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
282 ctx
->Stencil
.ZFailFunc
[0] = ctx
->Stencil
.ZFailFunc
[1] = zfail
;
283 ctx
->Stencil
.ZPassFunc
[0] = ctx
->Stencil
.ZPassFunc
[1] = zpass
;
284 ctx
->Stencil
.FailFunc
[0] = ctx
->Stencil
.FailFunc
[1] = fail
;
285 if (ctx
->Driver
.StencilOpSeparate
) {
286 ctx
->Driver
.StencilOpSeparate(ctx
, GL_FRONT_AND_BACK
,
291 /* only set active face state */
292 const GLint face
= ctx
->Stencil
.ActiveFace
;
293 if (ctx
->Stencil
.ZFailFunc
[face
] == zfail
&&
294 ctx
->Stencil
.ZPassFunc
[face
] == zpass
&&
295 ctx
->Stencil
.FailFunc
[face
] == fail
)
297 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
298 ctx
->Stencil
.ZFailFunc
[face
] = zfail
;
299 ctx
->Stencil
.ZPassFunc
[face
] = zpass
;
300 ctx
->Stencil
.FailFunc
[face
] = fail
;
301 if (ctx
->Driver
.StencilOpSeparate
) {
302 ctx
->Driver
.StencilOpSeparate(ctx
, face
? GL_BACK
: GL_FRONT
,
311 /* GL_EXT_stencil_two_side */
313 _mesa_ActiveStencilFaceEXT(GLenum face
)
315 GET_CURRENT_CONTEXT(ctx
);
316 ASSERT_OUTSIDE_BEGIN_END(ctx
);
318 if (!ctx
->Extensions
.EXT_stencil_two_side
) {
319 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveStencilFaceEXT");
323 if (face
== GL_FRONT
|| face
== GL_BACK
) {
324 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
325 ctx
->Stencil
.ActiveFace
= (face
== GL_FRONT
) ? 0 : 1;
328 _mesa_error(ctx
, GL_INVALID_ENUM
, "glActiveStencilFaceEXT(face)");
336 * OpenGL 2.0 function.
337 * \todo Make StencilOp() call this function. And eventually remove the
338 * ctx->Driver.StencilOp function and use ctx->Driver.StencilOpSeparate
342 _mesa_StencilOpSeparate(GLenum face
, GLenum fail
, GLenum zfail
, GLenum zpass
)
344 GET_CURRENT_CONTEXT(ctx
);
345 ASSERT_OUTSIDE_BEGIN_END(ctx
);
347 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
348 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(face)");
360 case GL_INCR_WRAP_EXT
:
361 case GL_DECR_WRAP_EXT
:
362 if (ctx
->Extensions
.EXT_stencil_wrap
) {
367 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(fail)");
378 case GL_INCR_WRAP_EXT
:
379 case GL_DECR_WRAP_EXT
:
380 if (ctx
->Extensions
.EXT_stencil_wrap
) {
385 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zfail)");
396 case GL_INCR_WRAP_EXT
:
397 case GL_DECR_WRAP_EXT
:
398 if (ctx
->Extensions
.EXT_stencil_wrap
) {
403 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zpass)");
407 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
409 if (face
!= GL_BACK
) {
410 ctx
->Stencil
.FailFunc
[0] = fail
;
411 ctx
->Stencil
.ZFailFunc
[0] = zfail
;
412 ctx
->Stencil
.ZPassFunc
[0] = zpass
;
414 if (face
!= GL_FRONT
) {
415 ctx
->Stencil
.FailFunc
[1] = fail
;
416 ctx
->Stencil
.ZFailFunc
[1] = zfail
;
417 ctx
->Stencil
.ZPassFunc
[1] = zpass
;
419 if (ctx
->Driver
.StencilOpSeparate
) {
420 ctx
->Driver
.StencilOpSeparate(ctx
, face
, fail
, zfail
, zpass
);
427 _mesa_StencilFuncSeparate(GLenum face
, GLenum func
, GLint ref
, GLuint mask
)
429 GET_CURRENT_CONTEXT(ctx
);
430 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
431 ASSERT_OUTSIDE_BEGIN_END(ctx
);
433 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
434 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(face)");
449 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(func)");
453 ref
= CLAMP(ref
, 0, stencilMax
);
455 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
457 if (face
== GL_FRONT
|| face
== GL_FRONT_AND_BACK
) {
458 ctx
->Stencil
.Function
[0] = func
;
459 ctx
->Stencil
.Ref
[0] = ref
;
460 ctx
->Stencil
.ValueMask
[0] = mask
;
462 if (face
== GL_BACK
|| face
== GL_FRONT_AND_BACK
) {
463 ctx
->Stencil
.Function
[1] = func
;
464 ctx
->Stencil
.Ref
[1] = ref
;
465 ctx
->Stencil
.ValueMask
[1] = mask
;
467 if (ctx
->Driver
.StencilFuncSeparate
) {
468 ctx
->Driver
.StencilFuncSeparate(ctx
, face
, func
, ref
, mask
);
475 _mesa_StencilMaskSeparate(GLenum face
, GLuint mask
)
477 GET_CURRENT_CONTEXT(ctx
);
478 ASSERT_OUTSIDE_BEGIN_END(ctx
);
480 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
481 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilaMaskSeparate(face)");
485 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
487 if (face
!= GL_BACK
) {
488 ctx
->Stencil
.WriteMask
[0] = mask
;
490 if (face
!= GL_FRONT
) {
491 ctx
->Stencil
.WriteMask
[1] = mask
;
493 if (ctx
->Driver
.StencilMaskSeparate
) {
494 ctx
->Driver
.StencilMaskSeparate(ctx
, face
, mask
);
500 * Update derived stencil state.
503 _mesa_update_stencil(GLcontext
*ctx
)
505 if (ctx
->Extensions
.EXT_stencil_two_side
) {
506 ctx
->Stencil
._TestTwoSide
= ctx
->Stencil
.TestTwoSide
;
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]);
522 * Initialize the context stipple state.
524 * \param ctx GL context.
526 * Initializes __GLcontextRec::Stencil attribute group.
529 _mesa_init_stencil(GLcontext
*ctx
)
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;