2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2007 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
42 * Also, note that GL_ATI_separate_stencil is different as well:
43 * glStencilFuncSeparateATI(GLenum frontfunc, GLenum backfunc, ...) vs.
44 * glStencilFuncSeparate(GLenum face, GLenum func, ...).
57 validate_stencil_op(GLcontext
*ctx
, GLenum op
)
67 case GL_INCR_WRAP_EXT
:
68 case GL_DECR_WRAP_EXT
:
69 if (ctx
->Extensions
.EXT_stencil_wrap
) {
80 validate_stencil_func(GLcontext
*ctx
, GLenum func
)
99 * Set the clear value for the stencil buffer.
101 * \param s clear value.
103 * \sa glClearStencil().
105 * Updates gl_stencil_attrib::Clear. On change
106 * flushes the vertices and notifies the driver via
107 * the dd_function_table::ClearStencil callback.
110 _mesa_ClearStencil( GLint s
)
112 GET_CURRENT_CONTEXT(ctx
);
113 ASSERT_OUTSIDE_BEGIN_END(ctx
);
115 if (ctx
->Stencil
.Clear
== (GLuint
) s
)
118 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
119 ctx
->Stencil
.Clear
= (GLuint
) s
;
121 if (ctx
->Driver
.ClearStencil
) {
122 ctx
->Driver
.ClearStencil( ctx
, s
);
128 * Set the function and reference value for stencil testing.
130 * \param frontfunc front test function.
131 * \param backfunc back test function.
132 * \param ref front and back reference value.
133 * \param mask front and back bitmask.
135 * \sa glStencilFunc().
137 * Verifies the parameters and updates the respective values in
138 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
139 * driver via the dd_function_table::StencilFunc callback.
142 _mesa_StencilFuncSeparateATI( GLenum frontfunc
, GLenum backfunc
, GLint ref
, GLuint mask
)
144 GET_CURRENT_CONTEXT(ctx
);
145 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
146 ASSERT_OUTSIDE_BEGIN_END(ctx
);
148 if (!validate_stencil_func(ctx
, frontfunc
)) {
149 _mesa_error(ctx
, GL_INVALID_ENUM
,
150 "glStencilFuncSeparateATI(frontfunc)");
153 if (!validate_stencil_func(ctx
, backfunc
)) {
154 _mesa_error(ctx
, GL_INVALID_ENUM
,
155 "glStencilFuncSeparateATI(backfunc)");
159 ref
= CLAMP( ref
, 0, stencilMax
);
161 /* set both front and back state */
162 if (ctx
->Stencil
.Function
[0] == frontfunc
&&
163 ctx
->Stencil
.Function
[1] == backfunc
&&
164 ctx
->Stencil
.ValueMask
[0] == mask
&&
165 ctx
->Stencil
.ValueMask
[1] == mask
&&
166 ctx
->Stencil
.Ref
[0] == ref
&&
167 ctx
->Stencil
.Ref
[1] == ref
)
169 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
170 ctx
->Stencil
.Function
[0] = frontfunc
;
171 ctx
->Stencil
.Function
[1] = backfunc
;
172 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
173 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
174 if (ctx
->Driver
.StencilFuncSeparate
) {
175 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_FRONT
,
176 frontfunc
, ref
, mask
);
177 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_BACK
,
178 backfunc
, ref
, mask
);
184 * Set the function and reference value for stencil testing.
186 * \param func test function.
187 * \param ref reference value.
188 * \param mask bitmask.
190 * \sa glStencilFunc().
192 * Verifies the parameters and updates the respective values in
193 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
194 * driver via the dd_function_table::StencilFunc callback.
197 _mesa_StencilFunc( GLenum func
, GLint ref
, GLuint mask
)
199 GET_CURRENT_CONTEXT(ctx
);
200 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
201 ASSERT_OUTSIDE_BEGIN_END(ctx
);
203 if (!validate_stencil_func(ctx
, func
)) {
204 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFunc(func)");
208 ref
= CLAMP( ref
, 0, stencilMax
);
210 if (ctx
->Extensions
.ATI_separate_stencil
) {
211 /* set both front and back state */
212 if (ctx
->Stencil
.Function
[0] == func
&&
213 ctx
->Stencil
.Function
[1] == func
&&
214 ctx
->Stencil
.ValueMask
[0] == mask
&&
215 ctx
->Stencil
.ValueMask
[1] == mask
&&
216 ctx
->Stencil
.Ref
[0] == ref
&&
217 ctx
->Stencil
.Ref
[1] == ref
)
219 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
220 ctx
->Stencil
.Function
[0] = ctx
->Stencil
.Function
[1] = func
;
221 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
222 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
223 if (ctx
->Driver
.StencilFuncSeparate
) {
224 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_FRONT_AND_BACK
,
229 /* only set active face state */
230 const GLint face
= ctx
->Stencil
.ActiveFace
;
231 if (ctx
->Stencil
.Function
[face
] == func
&&
232 ctx
->Stencil
.ValueMask
[face
] == mask
&&
233 ctx
->Stencil
.Ref
[face
] == ref
)
235 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
236 ctx
->Stencil
.Function
[face
] = func
;
237 ctx
->Stencil
.Ref
[face
] = ref
;
238 ctx
->Stencil
.ValueMask
[face
] = mask
;
239 if (ctx
->Driver
.StencilFuncSeparate
) {
240 ctx
->Driver
.StencilFuncSeparate(ctx
, face
? GL_BACK
: GL_FRONT
,
248 * Set the stencil writing mask.
250 * \param mask bit-mask to enable/disable writing of individual bits in the
253 * \sa glStencilMask().
255 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
256 * notifies the driver via the dd_function_table::StencilMask callback.
259 _mesa_StencilMask( GLuint mask
)
261 GET_CURRENT_CONTEXT(ctx
);
262 ASSERT_OUTSIDE_BEGIN_END(ctx
);
264 if (ctx
->Extensions
.ATI_separate_stencil
) {
265 /* set both front and back state */
266 if (ctx
->Stencil
.WriteMask
[0] == mask
&&
267 ctx
->Stencil
.WriteMask
[1] == mask
)
269 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
270 ctx
->Stencil
.WriteMask
[0] = ctx
->Stencil
.WriteMask
[1] = mask
;
271 if (ctx
->Driver
.StencilMaskSeparate
) {
272 ctx
->Driver
.StencilMaskSeparate(ctx
, GL_FRONT_AND_BACK
, mask
);
276 /* only set active face state */
277 const GLint face
= ctx
->Stencil
.ActiveFace
;
278 if (ctx
->Stencil
.WriteMask
[face
] == mask
)
280 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
281 ctx
->Stencil
.WriteMask
[face
] = mask
;
282 if (ctx
->Driver
.StencilMaskSeparate
) {
283 ctx
->Driver
.StencilMaskSeparate(ctx
, face
? GL_BACK
: GL_FRONT
, mask
);
290 * Set the stencil test actions.
292 * \param fail action to take when stencil test fails.
293 * \param zfail action to take when stencil test passes, but depth test fails.
294 * \param zpass action to take when stencil test passes and the depth test
295 * passes (or depth testing is not enabled).
299 * Verifies the parameters and updates the respective fields in
300 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
301 * driver via the dd_function_table::StencilOp callback.
304 _mesa_StencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
)
306 GET_CURRENT_CONTEXT(ctx
);
307 ASSERT_OUTSIDE_BEGIN_END(ctx
);
309 if (!validate_stencil_op(ctx
, fail
)) {
310 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(sfail)");
313 if (!validate_stencil_op(ctx
, zfail
)) {
314 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zfail)");
317 if (!validate_stencil_op(ctx
, zpass
)) {
318 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zpass)");
322 if (ctx
->Extensions
.ATI_separate_stencil
) {
323 /* set both front and back state */
324 if (ctx
->Stencil
.ZFailFunc
[0] == zfail
&&
325 ctx
->Stencil
.ZFailFunc
[1] == zfail
&&
326 ctx
->Stencil
.ZPassFunc
[0] == zpass
&&
327 ctx
->Stencil
.ZPassFunc
[1] == zpass
&&
328 ctx
->Stencil
.FailFunc
[0] == fail
&&
329 ctx
->Stencil
.FailFunc
[1] == fail
)
331 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
332 ctx
->Stencil
.ZFailFunc
[0] = ctx
->Stencil
.ZFailFunc
[1] = zfail
;
333 ctx
->Stencil
.ZPassFunc
[0] = ctx
->Stencil
.ZPassFunc
[1] = zpass
;
334 ctx
->Stencil
.FailFunc
[0] = ctx
->Stencil
.FailFunc
[1] = fail
;
335 if (ctx
->Driver
.StencilOpSeparate
) {
336 ctx
->Driver
.StencilOpSeparate(ctx
, GL_FRONT_AND_BACK
,
341 /* only set active face state */
342 const GLint face
= ctx
->Stencil
.ActiveFace
;
343 if (ctx
->Stencil
.ZFailFunc
[face
] == zfail
&&
344 ctx
->Stencil
.ZPassFunc
[face
] == zpass
&&
345 ctx
->Stencil
.FailFunc
[face
] == fail
)
347 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
348 ctx
->Stencil
.ZFailFunc
[face
] = zfail
;
349 ctx
->Stencil
.ZPassFunc
[face
] = zpass
;
350 ctx
->Stencil
.FailFunc
[face
] = fail
;
351 if (ctx
->Driver
.StencilOpSeparate
) {
352 ctx
->Driver
.StencilOpSeparate(ctx
, face
? GL_BACK
: GL_FRONT
,
361 /* GL_EXT_stencil_two_side */
363 _mesa_ActiveStencilFaceEXT(GLenum face
)
365 GET_CURRENT_CONTEXT(ctx
);
366 ASSERT_OUTSIDE_BEGIN_END(ctx
);
368 if (!ctx
->Extensions
.EXT_stencil_two_side
) {
369 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveStencilFaceEXT");
373 if (face
== GL_FRONT
|| face
== GL_BACK
) {
374 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
375 ctx
->Stencil
.ActiveFace
= (face
== GL_FRONT
) ? 0 : 1;
378 _mesa_error(ctx
, GL_INVALID_ENUM
, "glActiveStencilFaceEXT(face)");
386 * OpenGL 2.0 function.
387 * \todo Make StencilOp() call this function. And eventually remove the
388 * ctx->Driver.StencilOp function and use ctx->Driver.StencilOpSeparate
392 _mesa_StencilOpSeparate(GLenum face
, GLenum sfail
, GLenum zfail
, GLenum zpass
)
394 GLboolean set
= GL_FALSE
;
395 GET_CURRENT_CONTEXT(ctx
);
396 ASSERT_OUTSIDE_BEGIN_END(ctx
);
398 if (!validate_stencil_op(ctx
, sfail
)) {
399 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(sfail)");
402 if (!validate_stencil_op(ctx
, zfail
)) {
403 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zfail)");
406 if (!validate_stencil_op(ctx
, zpass
)) {
407 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zpass)");
410 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
411 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(face)");
415 if (face
!= GL_BACK
) {
417 if (ctx
->Stencil
.ZFailFunc
[0] != zfail
||
418 ctx
->Stencil
.ZPassFunc
[0] != zpass
||
419 ctx
->Stencil
.FailFunc
[0] != sfail
){
420 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
421 ctx
->Stencil
.ZFailFunc
[0] = zfail
;
422 ctx
->Stencil
.ZPassFunc
[0] = zpass
;
423 ctx
->Stencil
.FailFunc
[0] = sfail
;
427 if (face
!= GL_FRONT
) {
429 if (ctx
->Stencil
.ZFailFunc
[1] != zfail
||
430 ctx
->Stencil
.ZPassFunc
[1] != zpass
||
431 ctx
->Stencil
.FailFunc
[1] != sfail
) {
432 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
433 ctx
->Stencil
.ZFailFunc
[1] = zfail
;
434 ctx
->Stencil
.ZPassFunc
[1] = zpass
;
435 ctx
->Stencil
.FailFunc
[1] = sfail
;
439 if (set
&& ctx
->Driver
.StencilOpSeparate
) {
440 ctx
->Driver
.StencilOpSeparate(ctx
, face
, sfail
, zfail
, zpass
);
447 _mesa_StencilFuncSeparate(GLenum face
, GLenum func
, GLint ref
, GLuint mask
)
449 GET_CURRENT_CONTEXT(ctx
);
450 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
451 ASSERT_OUTSIDE_BEGIN_END(ctx
);
453 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
454 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(face)");
457 if (!validate_stencil_func(ctx
, func
)) {
458 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(func)");
462 ref
= CLAMP(ref
, 0, stencilMax
);
464 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
466 if (face
== GL_FRONT
|| face
== GL_FRONT_AND_BACK
) {
467 ctx
->Stencil
.Function
[0] = func
;
468 ctx
->Stencil
.Ref
[0] = ref
;
469 ctx
->Stencil
.ValueMask
[0] = mask
;
471 if (face
== GL_BACK
|| face
== GL_FRONT_AND_BACK
) {
472 ctx
->Stencil
.Function
[1] = func
;
473 ctx
->Stencil
.Ref
[1] = ref
;
474 ctx
->Stencil
.ValueMask
[1] = mask
;
476 if (ctx
->Driver
.StencilFuncSeparate
) {
477 ctx
->Driver
.StencilFuncSeparate(ctx
, face
, func
, ref
, mask
);
484 _mesa_StencilMaskSeparate(GLenum face
, GLuint mask
)
486 GET_CURRENT_CONTEXT(ctx
);
487 ASSERT_OUTSIDE_BEGIN_END(ctx
);
489 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
490 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilaMaskSeparate(face)");
494 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
496 if (face
!= GL_BACK
) {
497 ctx
->Stencil
.WriteMask
[0] = mask
;
499 if (face
!= GL_FRONT
) {
500 ctx
->Stencil
.WriteMask
[1] = mask
;
502 if (ctx
->Driver
.StencilMaskSeparate
) {
503 ctx
->Driver
.StencilMaskSeparate(ctx
, face
, mask
);
509 * Update derived stencil state.
512 _mesa_update_stencil(GLcontext
*ctx
)
514 if (ctx
->Extensions
.EXT_stencil_two_side
) {
515 ctx
->Stencil
._TestTwoSide
= ctx
->Stencil
.TestTwoSide
;
518 ctx
->Stencil
._TestTwoSide
=
519 (ctx
->Stencil
.Function
[0] != ctx
->Stencil
.Function
[1] ||
520 ctx
->Stencil
.FailFunc
[0] != ctx
->Stencil
.FailFunc
[1] ||
521 ctx
->Stencil
.ZPassFunc
[0] != ctx
->Stencil
.ZPassFunc
[1] ||
522 ctx
->Stencil
.ZFailFunc
[0] != ctx
->Stencil
.ZFailFunc
[1] ||
523 ctx
->Stencil
.Ref
[0] != ctx
->Stencil
.Ref
[1] ||
524 ctx
->Stencil
.ValueMask
[0] != ctx
->Stencil
.ValueMask
[1] ||
525 ctx
->Stencil
.WriteMask
[0] != ctx
->Stencil
.WriteMask
[1]);
531 * Initialize the context stipple state.
533 * \param ctx GL context.
535 * Initializes __GLcontextRec::Stencil attribute group.
538 _mesa_init_stencil(GLcontext
*ctx
)
540 ctx
->Stencil
.Enabled
= GL_FALSE
;
541 ctx
->Stencil
.TestTwoSide
= GL_FALSE
;
542 ctx
->Stencil
.ActiveFace
= 0; /* 0 = GL_FRONT, 1 = GL_BACK */
543 ctx
->Stencil
.Function
[0] = GL_ALWAYS
;
544 ctx
->Stencil
.Function
[1] = GL_ALWAYS
;
545 ctx
->Stencil
.FailFunc
[0] = GL_KEEP
;
546 ctx
->Stencil
.FailFunc
[1] = GL_KEEP
;
547 ctx
->Stencil
.ZPassFunc
[0] = GL_KEEP
;
548 ctx
->Stencil
.ZPassFunc
[1] = GL_KEEP
;
549 ctx
->Stencil
.ZFailFunc
[0] = GL_KEEP
;
550 ctx
->Stencil
.ZFailFunc
[1] = GL_KEEP
;
551 ctx
->Stencil
.Ref
[0] = 0;
552 ctx
->Stencil
.Ref
[1] = 0;
553 ctx
->Stencil
.ValueMask
[0] = ~0U;
554 ctx
->Stencil
.ValueMask
[1] = ~0U;
555 ctx
->Stencil
.WriteMask
[0] = ~0U;
556 ctx
->Stencil
.WriteMask
[1] = ~0U;
557 ctx
->Stencil
.Clear
= 0;