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
;
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
)
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
;
146 if (ctx
->Driver
.StencilFunc
) {
147 ctx
->Driver
.StencilFunc( ctx
, func
, ref
, mask
);
153 * Set the stencil writing mask.
155 * \param mask bit-mask to enable/disable writing of individual bits in the
158 * \sa glStencilMask().
160 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
161 * notifies the driver via the dd_function_table::StencilMask callback.
164 _mesa_StencilMask( GLuint mask
)
166 GET_CURRENT_CONTEXT(ctx
);
167 ASSERT_OUTSIDE_BEGIN_END(ctx
);
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
)
174 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
175 ctx
->Stencil
.WriteMask
[face
] = mask
;
178 /* set both front and back state */
179 if (ctx
->Stencil
.WriteMask
[0] == mask
&&
180 ctx
->Stencil
.WriteMask
[1] == mask
)
182 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
183 ctx
->Stencil
.WriteMask
[0] = ctx
->Stencil
.WriteMask
[1] = mask
;
186 if (ctx
->Driver
.StencilMask
) {
187 ctx
->Driver
.StencilMask( ctx
, mask
);
193 * Set the stencil test actions.
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).
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.
207 _mesa_StencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
)
209 GET_CURRENT_CONTEXT(ctx
);
210 ASSERT_OUTSIDE_BEGIN_END(ctx
);
220 case GL_INCR_WRAP_EXT
:
221 case GL_DECR_WRAP_EXT
:
222 if (ctx
->Extensions
.EXT_stencil_wrap
) {
227 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp");
238 case GL_INCR_WRAP_EXT
:
239 case GL_DECR_WRAP_EXT
:
240 if (ctx
->Extensions
.EXT_stencil_wrap
) {
245 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp");
256 case GL_INCR_WRAP_EXT
:
257 case GL_DECR_WRAP_EXT
:
258 if (ctx
->Extensions
.EXT_stencil_wrap
) {
263 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp");
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
)
274 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
275 ctx
->Stencil
.ZFailFunc
[face
] = zfail
;
276 ctx
->Stencil
.ZPassFunc
[face
] = zpass
;
277 ctx
->Stencil
.FailFunc
[face
] = fail
;
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
)
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
;
294 if (ctx
->Driver
.StencilOp
) {
295 ctx
->Driver
.StencilOp(ctx
, fail
, zfail
, zpass
);
302 /* GL_EXT_stencil_two_side */
304 _mesa_ActiveStencilFaceEXT(GLenum face
)
306 GET_CURRENT_CONTEXT(ctx
);
307 ASSERT_OUTSIDE_BEGIN_END(ctx
);
309 if (!ctx
->Extensions
.EXT_stencil_two_side
) {
310 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveStencilFaceEXT");
314 if (face
== GL_FRONT
|| face
== GL_BACK
) {
315 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
316 ctx
->Stencil
.ActiveFace
= (face
== GL_FRONT
) ? 0 : 1;
319 _mesa_error(ctx
, GL_INVALID_ENUM
, "glActiveStencilFaceEXT(face)");
323 if (ctx
->Driver
.ActiveStencilFace
) {
324 ctx
->Driver
.ActiveStencilFace(ctx
, (GLuint
) ctx
->Stencil
.ActiveFace
);
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
338 _mesa_StencilOpSeparate(GLenum face
, GLenum fail
, GLenum zfail
, GLenum zpass
)
340 GET_CURRENT_CONTEXT(ctx
);
341 ASSERT_OUTSIDE_BEGIN_END(ctx
);
343 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
344 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(face)");
356 case GL_INCR_WRAP_EXT
:
357 case GL_DECR_WRAP_EXT
:
358 if (ctx
->Extensions
.EXT_stencil_wrap
) {
363 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(fail)");
374 case GL_INCR_WRAP_EXT
:
375 case GL_DECR_WRAP_EXT
:
376 if (ctx
->Extensions
.EXT_stencil_wrap
) {
381 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zfail)");
392 case GL_INCR_WRAP_EXT
:
393 case GL_DECR_WRAP_EXT
:
394 if (ctx
->Extensions
.EXT_stencil_wrap
) {
399 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zpass)");
403 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
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
;
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
;
416 if (ctx
->Driver
.StencilOpSeparate
) {
417 ctx
->Driver
.StencilOpSeparate(ctx
, face
, fail
, zfail
, zpass
);
424 _mesa_StencilFuncSeparate(GLenum face
, GLenum func
, GLint ref
, GLuint mask
)
426 GET_CURRENT_CONTEXT(ctx
);
428 ASSERT_OUTSIDE_BEGIN_END(ctx
);
430 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
431 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(face)");
446 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(func)");
450 maxref
= (1 << STENCIL_BITS
) - 1;
451 ref
= CLAMP(ref
, 0, maxref
);
453 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
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
;
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
;
466 if (ctx
->Driver
.StencilFuncSeparate
) {
467 ctx
->Driver
.StencilFuncSeparate(ctx
, face
, func
, ref
, mask
);
474 _mesa_StencilMaskSeparate(GLenum face
, GLuint mask
)
476 GET_CURRENT_CONTEXT(ctx
);
477 ASSERT_OUTSIDE_BEGIN_END(ctx
);
479 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
480 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilaMaskSeparate(face)");
484 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
486 if (face
== GL_FRONT
|| face
== GL_FRONT_AND_BACK
) {
487 ctx
->Stencil
.WriteMask
[0] = mask
;
489 if (face
== GL_BACK
|| face
== GL_FRONT_AND_BACK
) {
490 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;