2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 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 * Set the clear value for the stencil buffer.
59 * \param s clear value.
61 * \sa glClearStencil().
63 * Updates gl_stencil_attrib::Clear. On change
64 * flushes the vertices and notifies the driver via
65 * the dd_function_table::ClearStencil callback.
68 _mesa_ClearStencil( GLint s
)
70 GET_CURRENT_CONTEXT(ctx
);
71 ASSERT_OUTSIDE_BEGIN_END(ctx
);
73 if (ctx
->Stencil
.Clear
== (GLuint
) s
)
76 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
77 ctx
->Stencil
.Clear
= (GLuint
) s
;
79 if (ctx
->Driver
.ClearStencil
) {
80 ctx
->Driver
.ClearStencil( ctx
, s
);
86 * Set the function and reference value for stencil testing.
88 * \param frontfunc front test function.
89 * \param backfunc back test function.
90 * \param ref front and back reference value.
91 * \param mask front and back bitmask.
93 * \sa glStencilFunc().
95 * Verifies the parameters and updates the respective values in
96 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
97 * driver via the dd_function_table::StencilFunc callback.
100 _mesa_StencilFuncSeparateATI( GLenum frontfunc
, GLenum backfunc
, GLint ref
, GLuint mask
)
102 GET_CURRENT_CONTEXT(ctx
);
103 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
104 ASSERT_OUTSIDE_BEGIN_END(ctx
);
117 _mesa_error( ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparateATI (0x%04x)", frontfunc
);
132 _mesa_error( ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparateATI (0x%04x)", backfunc
);
136 ref
= CLAMP( ref
, 0, stencilMax
);
138 /* set both front and back state */
139 if (ctx
->Stencil
.Function
[0] == frontfunc
&&
140 ctx
->Stencil
.Function
[1] == backfunc
&&
141 ctx
->Stencil
.ValueMask
[0] == mask
&&
142 ctx
->Stencil
.ValueMask
[1] == mask
&&
143 ctx
->Stencil
.Ref
[0] == ref
&&
144 ctx
->Stencil
.Ref
[1] == ref
)
146 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
147 ctx
->Stencil
.Function
[0] = frontfunc
;
148 ctx
->Stencil
.Function
[1] = backfunc
;
149 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
150 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
151 if (ctx
->Driver
.StencilFuncSeparate
) {
152 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_FRONT
,
153 frontfunc
, ref
, mask
);
154 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_BACK
,
155 backfunc
, ref
, mask
);
161 * Set the function and reference value for stencil testing.
163 * \param func test function.
164 * \param ref reference value.
165 * \param mask bitmask.
167 * \sa glStencilFunc().
169 * Verifies the parameters and updates the respective values in
170 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
171 * driver via the dd_function_table::StencilFunc callback.
174 _mesa_StencilFunc( GLenum func
, GLint ref
, GLuint mask
)
176 GET_CURRENT_CONTEXT(ctx
);
177 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
178 ASSERT_OUTSIDE_BEGIN_END(ctx
);
191 _mesa_error( ctx
, GL_INVALID_ENUM
, "glStencilFunc (0x%04x)", func
);
195 ref
= CLAMP( ref
, 0, stencilMax
);
197 if (ctx
->Extensions
.ATI_separate_stencil
) {
198 /* set both front and back state */
199 if (ctx
->Stencil
.Function
[0] == func
&&
200 ctx
->Stencil
.Function
[1] == func
&&
201 ctx
->Stencil
.ValueMask
[0] == mask
&&
202 ctx
->Stencil
.ValueMask
[1] == mask
&&
203 ctx
->Stencil
.Ref
[0] == ref
&&
204 ctx
->Stencil
.Ref
[1] == ref
)
206 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
207 ctx
->Stencil
.Function
[0] = ctx
->Stencil
.Function
[1] = func
;
208 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
209 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
210 if (ctx
->Driver
.StencilFuncSeparate
) {
211 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_FRONT_AND_BACK
,
216 /* only set active face state */
217 const GLint face
= ctx
->Stencil
.ActiveFace
;
218 if (ctx
->Stencil
.Function
[face
] == func
&&
219 ctx
->Stencil
.ValueMask
[face
] == mask
&&
220 ctx
->Stencil
.Ref
[face
] == ref
)
222 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
223 ctx
->Stencil
.Function
[face
] = func
;
224 ctx
->Stencil
.Ref
[face
] = ref
;
225 ctx
->Stencil
.ValueMask
[face
] = mask
;
226 if (ctx
->Driver
.StencilFuncSeparate
) {
227 ctx
->Driver
.StencilFuncSeparate(ctx
, face
? GL_BACK
: GL_FRONT
,
235 * Set the stencil writing mask.
237 * \param mask bit-mask to enable/disable writing of individual bits in the
240 * \sa glStencilMask().
242 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
243 * notifies the driver via the dd_function_table::StencilMask callback.
246 _mesa_StencilMask( GLuint mask
)
248 GET_CURRENT_CONTEXT(ctx
);
249 ASSERT_OUTSIDE_BEGIN_END(ctx
);
251 if (ctx
->Extensions
.ATI_separate_stencil
) {
252 /* set both front and back state */
253 if (ctx
->Stencil
.WriteMask
[0] == mask
&&
254 ctx
->Stencil
.WriteMask
[1] == mask
)
256 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
257 ctx
->Stencil
.WriteMask
[0] = ctx
->Stencil
.WriteMask
[1] = mask
;
258 if (ctx
->Driver
.StencilMaskSeparate
) {
259 ctx
->Driver
.StencilMaskSeparate(ctx
, GL_FRONT_AND_BACK
, mask
);
263 /* only set active face state */
264 const GLint face
= ctx
->Stencil
.ActiveFace
;
265 if (ctx
->Stencil
.WriteMask
[face
] == mask
)
267 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
268 ctx
->Stencil
.WriteMask
[face
] = mask
;
269 if (ctx
->Driver
.StencilMaskSeparate
) {
270 ctx
->Driver
.StencilMaskSeparate(ctx
, face
? GL_BACK
: GL_FRONT
, mask
);
277 * Set the stencil test actions.
279 * \param fail action to take when stencil test fails.
280 * \param zfail action to take when stencil test passes, but depth test fails.
281 * \param zpass action to take when stencil test passes and the depth test
282 * passes (or depth testing is not enabled).
286 * Verifies the parameters and updates the respective fields in
287 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
288 * driver via the dd_function_table::StencilOp callback.
291 _mesa_StencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
)
293 GET_CURRENT_CONTEXT(ctx
);
294 ASSERT_OUTSIDE_BEGIN_END(ctx
);
304 case GL_INCR_WRAP_EXT
:
305 case GL_DECR_WRAP_EXT
:
306 if (ctx
->Extensions
.EXT_stencil_wrap
) {
311 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp");
322 case GL_INCR_WRAP_EXT
:
323 case GL_DECR_WRAP_EXT
:
324 if (ctx
->Extensions
.EXT_stencil_wrap
) {
329 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp");
340 case GL_INCR_WRAP_EXT
:
341 case GL_DECR_WRAP_EXT
:
342 if (ctx
->Extensions
.EXT_stencil_wrap
) {
347 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp");
351 if (ctx
->Extensions
.ATI_separate_stencil
) {
352 /* set both front and back state */
353 if (ctx
->Stencil
.ZFailFunc
[0] == zfail
&&
354 ctx
->Stencil
.ZFailFunc
[1] == zfail
&&
355 ctx
->Stencil
.ZPassFunc
[0] == zpass
&&
356 ctx
->Stencil
.ZPassFunc
[1] == zpass
&&
357 ctx
->Stencil
.FailFunc
[0] == fail
&&
358 ctx
->Stencil
.FailFunc
[1] == fail
)
360 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
361 ctx
->Stencil
.ZFailFunc
[0] = ctx
->Stencil
.ZFailFunc
[1] = zfail
;
362 ctx
->Stencil
.ZPassFunc
[0] = ctx
->Stencil
.ZPassFunc
[1] = zpass
;
363 ctx
->Stencil
.FailFunc
[0] = ctx
->Stencil
.FailFunc
[1] = fail
;
364 if (ctx
->Driver
.StencilOpSeparate
) {
365 ctx
->Driver
.StencilOpSeparate(ctx
, GL_FRONT_AND_BACK
,
370 /* only set active face state */
371 const GLint face
= ctx
->Stencil
.ActiveFace
;
372 if (ctx
->Stencil
.ZFailFunc
[face
] == zfail
&&
373 ctx
->Stencil
.ZPassFunc
[face
] == zpass
&&
374 ctx
->Stencil
.FailFunc
[face
] == fail
)
376 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
377 ctx
->Stencil
.ZFailFunc
[face
] = zfail
;
378 ctx
->Stencil
.ZPassFunc
[face
] = zpass
;
379 ctx
->Stencil
.FailFunc
[face
] = fail
;
380 if (ctx
->Driver
.StencilOpSeparate
) {
381 ctx
->Driver
.StencilOpSeparate(ctx
, face
? GL_BACK
: GL_FRONT
,
390 /* GL_EXT_stencil_two_side */
392 _mesa_ActiveStencilFaceEXT(GLenum face
)
394 GET_CURRENT_CONTEXT(ctx
);
395 ASSERT_OUTSIDE_BEGIN_END(ctx
);
397 if (!ctx
->Extensions
.EXT_stencil_two_side
) {
398 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveStencilFaceEXT");
402 if (face
== GL_FRONT
|| face
== GL_BACK
) {
403 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
404 ctx
->Stencil
.ActiveFace
= (face
== GL_FRONT
) ? 0 : 1;
407 _mesa_error(ctx
, GL_INVALID_ENUM
, "glActiveStencilFaceEXT(face)");
415 * OpenGL 2.0 function.
416 * \todo Make StencilOp() call this function. And eventually remove the
417 * ctx->Driver.StencilOp function and use ctx->Driver.StencilOpSeparate
421 _mesa_StencilOpSeparate(GLenum face
, GLenum fail
, GLenum zfail
, GLenum zpass
)
423 GET_CURRENT_CONTEXT(ctx
);
424 ASSERT_OUTSIDE_BEGIN_END(ctx
);
426 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
427 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(face)");
439 case GL_INCR_WRAP_EXT
:
440 case GL_DECR_WRAP_EXT
:
441 if (ctx
->Extensions
.EXT_stencil_wrap
) {
446 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(fail)");
457 case GL_INCR_WRAP_EXT
:
458 case GL_DECR_WRAP_EXT
:
459 if (ctx
->Extensions
.EXT_stencil_wrap
) {
464 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zfail)");
475 case GL_INCR_WRAP_EXT
:
476 case GL_DECR_WRAP_EXT
:
477 if (ctx
->Extensions
.EXT_stencil_wrap
) {
482 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zpass)");
486 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
488 if (face
!= GL_BACK
) {
489 ctx
->Stencil
.FailFunc
[0] = fail
;
490 ctx
->Stencil
.ZFailFunc
[0] = zfail
;
491 ctx
->Stencil
.ZPassFunc
[0] = zpass
;
493 if (face
!= GL_FRONT
) {
494 ctx
->Stencil
.FailFunc
[1] = fail
;
495 ctx
->Stencil
.ZFailFunc
[1] = zfail
;
496 ctx
->Stencil
.ZPassFunc
[1] = zpass
;
498 if (ctx
->Driver
.StencilOpSeparate
) {
499 ctx
->Driver
.StencilOpSeparate(ctx
, face
, fail
, zfail
, zpass
);
506 _mesa_StencilFuncSeparate(GLenum face
, GLenum func
, GLint ref
, GLuint mask
)
508 GET_CURRENT_CONTEXT(ctx
);
509 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
510 ASSERT_OUTSIDE_BEGIN_END(ctx
);
512 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
513 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(face)");
528 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(func)");
532 ref
= CLAMP(ref
, 0, stencilMax
);
534 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
536 if (face
== GL_FRONT
|| face
== GL_FRONT_AND_BACK
) {
537 ctx
->Stencil
.Function
[0] = func
;
538 ctx
->Stencil
.Ref
[0] = ref
;
539 ctx
->Stencil
.ValueMask
[0] = mask
;
541 if (face
== GL_BACK
|| face
== GL_FRONT_AND_BACK
) {
542 ctx
->Stencil
.Function
[1] = func
;
543 ctx
->Stencil
.Ref
[1] = ref
;
544 ctx
->Stencil
.ValueMask
[1] = mask
;
546 if (ctx
->Driver
.StencilFuncSeparate
) {
547 ctx
->Driver
.StencilFuncSeparate(ctx
, face
, func
, ref
, mask
);
554 _mesa_StencilMaskSeparate(GLenum face
, GLuint mask
)
556 GET_CURRENT_CONTEXT(ctx
);
557 ASSERT_OUTSIDE_BEGIN_END(ctx
);
559 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
560 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilaMaskSeparate(face)");
564 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
566 if (face
!= GL_BACK
) {
567 ctx
->Stencil
.WriteMask
[0] = mask
;
569 if (face
!= GL_FRONT
) {
570 ctx
->Stencil
.WriteMask
[1] = mask
;
572 if (ctx
->Driver
.StencilMaskSeparate
) {
573 ctx
->Driver
.StencilMaskSeparate(ctx
, face
, mask
);
579 * Update derived stencil state.
582 _mesa_update_stencil(GLcontext
*ctx
)
584 if (ctx
->Extensions
.EXT_stencil_two_side
) {
585 ctx
->Stencil
._TestTwoSide
= ctx
->Stencil
.TestTwoSide
;
588 ctx
->Stencil
._TestTwoSide
=
589 (ctx
->Stencil
.Function
[0] != ctx
->Stencil
.Function
[1] ||
590 ctx
->Stencil
.FailFunc
[0] != ctx
->Stencil
.FailFunc
[1] ||
591 ctx
->Stencil
.ZPassFunc
[0] != ctx
->Stencil
.ZPassFunc
[1] ||
592 ctx
->Stencil
.ZFailFunc
[0] != ctx
->Stencil
.ZFailFunc
[1] ||
593 ctx
->Stencil
.Ref
[0] != ctx
->Stencil
.Ref
[1] ||
594 ctx
->Stencil
.ValueMask
[0] != ctx
->Stencil
.ValueMask
[1] ||
595 ctx
->Stencil
.WriteMask
[0] != ctx
->Stencil
.WriteMask
[1]);
601 * Initialize the context stipple state.
603 * \param ctx GL context.
605 * Initializes __GLcontextRec::Stencil attribute group.
608 _mesa_init_stencil(GLcontext
*ctx
)
610 ctx
->Stencil
.Enabled
= GL_FALSE
;
611 ctx
->Stencil
.TestTwoSide
= GL_FALSE
;
612 ctx
->Stencil
.ActiveFace
= 0; /* 0 = GL_FRONT, 1 = GL_BACK */
613 ctx
->Stencil
.Function
[0] = GL_ALWAYS
;
614 ctx
->Stencil
.Function
[1] = GL_ALWAYS
;
615 ctx
->Stencil
.FailFunc
[0] = GL_KEEP
;
616 ctx
->Stencil
.FailFunc
[1] = GL_KEEP
;
617 ctx
->Stencil
.ZPassFunc
[0] = GL_KEEP
;
618 ctx
->Stencil
.ZPassFunc
[1] = GL_KEEP
;
619 ctx
->Stencil
.ZFailFunc
[0] = GL_KEEP
;
620 ctx
->Stencil
.ZFailFunc
[1] = GL_KEEP
;
621 ctx
->Stencil
.Ref
[0] = 0;
622 ctx
->Stencil
.Ref
[1] = 0;
623 ctx
->Stencil
.ValueMask
[0] = ~0U;
624 ctx
->Stencil
.ValueMask
[1] = ~0U;
625 ctx
->Stencil
.WriteMask
[0] = ~0U;
626 ctx
->Stencil
.WriteMask
[1] = ~0U;
627 ctx
->Stencil
.Clear
= 0;