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 some conflict 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 * Also, note that GL_ATI_separate_stencil is different as well:
40 * glStencilFuncSeparateATI(GLenum frontfunc, GLenum backfunc, ...) vs.
41 * glStencilFuncSeparate(GLenum face, GLenum func, ...).
43 * This problem is solved by keeping three sets of stencil state:
44 * state[0] = GL_FRONT state.
45 * state[1] = OpenGL 2.0 / GL_ATI_separate_stencil GL_BACK state.
46 * state[2] = GL_EXT_stencil_two_side GL_BACK state.
59 validate_stencil_op(struct gl_context
*ctx
, GLenum op
)
69 case GL_INCR_WRAP_EXT
:
70 case GL_DECR_WRAP_EXT
:
71 if (ctx
->Extensions
.EXT_stencil_wrap
) {
82 validate_stencil_func(struct gl_context
*ctx
, GLenum func
)
101 * Set the clear value for the stencil buffer.
103 * \param s clear value.
105 * \sa glClearStencil().
107 * Updates gl_stencil_attrib::Clear. On change
108 * flushes the vertices and notifies the driver via
109 * the dd_function_table::ClearStencil callback.
112 _mesa_ClearStencil( GLint s
)
114 GET_CURRENT_CONTEXT(ctx
);
115 ASSERT_OUTSIDE_BEGIN_END(ctx
);
117 if (ctx
->Stencil
.Clear
== (GLuint
) s
)
120 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
121 ctx
->Stencil
.Clear
= (GLuint
) s
;
123 if (ctx
->Driver
.ClearStencil
) {
124 ctx
->Driver
.ClearStencil( ctx
, s
);
130 * Set the function and reference value for stencil testing.
132 * \param frontfunc front test function.
133 * \param backfunc back test function.
134 * \param ref front and back reference value.
135 * \param mask front and back bitmask.
137 * \sa glStencilFunc().
139 * Verifies the parameters and updates the respective values in
140 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies the
141 * driver via the dd_function_table::StencilFunc callback.
144 _mesa_StencilFuncSeparateATI( GLenum frontfunc
, GLenum backfunc
, GLint ref
, GLuint mask
)
146 GET_CURRENT_CONTEXT(ctx
);
147 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
148 ASSERT_OUTSIDE_BEGIN_END(ctx
);
150 if (MESA_VERBOSE
& VERBOSE_API
)
151 _mesa_debug(ctx
, "glStencilFuncSeparateATI()\n");
153 if (!validate_stencil_func(ctx
, frontfunc
)) {
154 _mesa_error(ctx
, GL_INVALID_ENUM
,
155 "glStencilFuncSeparateATI(frontfunc)");
158 if (!validate_stencil_func(ctx
, backfunc
)) {
159 _mesa_error(ctx
, GL_INVALID_ENUM
,
160 "glStencilFuncSeparateATI(backfunc)");
164 ref
= CLAMP( ref
, 0, stencilMax
);
166 /* set both front and back state */
167 if (ctx
->Stencil
.Function
[0] == frontfunc
&&
168 ctx
->Stencil
.Function
[1] == backfunc
&&
169 ctx
->Stencil
.ValueMask
[0] == mask
&&
170 ctx
->Stencil
.ValueMask
[1] == mask
&&
171 ctx
->Stencil
.Ref
[0] == ref
&&
172 ctx
->Stencil
.Ref
[1] == ref
)
174 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
175 ctx
->Stencil
.Function
[0] = frontfunc
;
176 ctx
->Stencil
.Function
[1] = backfunc
;
177 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
178 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
179 if (ctx
->Driver
.StencilFuncSeparate
) {
180 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_FRONT
,
181 frontfunc
, ref
, mask
);
182 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_BACK
,
183 backfunc
, ref
, mask
);
189 * Set the function and reference value for stencil testing.
191 * \param func test function.
192 * \param ref reference value.
193 * \param mask bitmask.
195 * \sa glStencilFunc().
197 * Verifies the parameters and updates the respective values in
198 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies the
199 * driver via the dd_function_table::StencilFunc callback.
202 _mesa_StencilFunc( GLenum func
, GLint ref
, GLuint mask
)
204 GET_CURRENT_CONTEXT(ctx
);
205 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
206 const GLint face
= ctx
->Stencil
.ActiveFace
;
207 ASSERT_OUTSIDE_BEGIN_END(ctx
);
209 if (MESA_VERBOSE
& VERBOSE_API
)
210 _mesa_debug(ctx
, "glStencilFunc()\n");
212 if (!validate_stencil_func(ctx
, func
)) {
213 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFunc(func)");
217 ref
= CLAMP( ref
, 0, stencilMax
);
220 if (ctx
->Stencil
.Function
[face
] == func
&&
221 ctx
->Stencil
.ValueMask
[face
] == mask
&&
222 ctx
->Stencil
.Ref
[face
] == ref
)
224 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
225 ctx
->Stencil
.Function
[face
] = func
;
226 ctx
->Stencil
.Ref
[face
] = ref
;
227 ctx
->Stencil
.ValueMask
[face
] = mask
;
229 /* Only propagate the change to the driver if EXT_stencil_two_side
232 if (ctx
->Driver
.StencilFuncSeparate
&& ctx
->Stencil
.TestTwoSide
) {
233 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_BACK
, func
, ref
, mask
);
237 /* set both front and back state */
238 if (ctx
->Stencil
.Function
[0] == func
&&
239 ctx
->Stencil
.Function
[1] == func
&&
240 ctx
->Stencil
.ValueMask
[0] == mask
&&
241 ctx
->Stencil
.ValueMask
[1] == mask
&&
242 ctx
->Stencil
.Ref
[0] == ref
&&
243 ctx
->Stencil
.Ref
[1] == ref
)
245 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
246 ctx
->Stencil
.Function
[0] = ctx
->Stencil
.Function
[1] = func
;
247 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
248 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
249 if (ctx
->Driver
.StencilFuncSeparate
) {
250 ctx
->Driver
.StencilFuncSeparate(ctx
,
251 ((ctx
->Stencil
.TestTwoSide
)
252 ? GL_FRONT
: GL_FRONT_AND_BACK
),
260 * Set the stencil writing mask.
262 * \param mask bit-mask to enable/disable writing of individual bits in the
265 * \sa glStencilMask().
267 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
268 * notifies the driver via the dd_function_table::StencilMask callback.
271 _mesa_StencilMask( GLuint mask
)
273 GET_CURRENT_CONTEXT(ctx
);
274 const GLint face
= ctx
->Stencil
.ActiveFace
;
276 if (MESA_VERBOSE
& VERBOSE_API
)
277 _mesa_debug(ctx
, "glStencilMask()\n");
279 ASSERT_OUTSIDE_BEGIN_END(ctx
);
282 /* Only modify the EXT_stencil_two_side back-face state.
284 if (ctx
->Stencil
.WriteMask
[face
] == mask
)
286 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
287 ctx
->Stencil
.WriteMask
[face
] = mask
;
289 /* Only propagate the change to the driver if EXT_stencil_two_side
292 if (ctx
->Driver
.StencilMaskSeparate
&& ctx
->Stencil
.TestTwoSide
) {
293 ctx
->Driver
.StencilMaskSeparate(ctx
, GL_BACK
, mask
);
297 /* set both front and back state */
298 if (ctx
->Stencil
.WriteMask
[0] == mask
&&
299 ctx
->Stencil
.WriteMask
[1] == mask
)
301 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
302 ctx
->Stencil
.WriteMask
[0] = ctx
->Stencil
.WriteMask
[1] = mask
;
303 if (ctx
->Driver
.StencilMaskSeparate
) {
304 ctx
->Driver
.StencilMaskSeparate(ctx
,
305 ((ctx
->Stencil
.TestTwoSide
)
306 ? GL_FRONT
: GL_FRONT_AND_BACK
),
314 * Set the stencil test actions.
316 * \param fail action to take when stencil test fails.
317 * \param zfail action to take when stencil test passes, but depth test fails.
318 * \param zpass action to take when stencil test passes and the depth test
319 * passes (or depth testing is not enabled).
323 * Verifies the parameters and updates the respective fields in
324 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies the
325 * driver via the dd_function_table::StencilOp callback.
328 _mesa_StencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
)
330 GET_CURRENT_CONTEXT(ctx
);
331 const GLint face
= ctx
->Stencil
.ActiveFace
;
333 if (MESA_VERBOSE
& VERBOSE_API
)
334 _mesa_debug(ctx
, "glStencilOp()\n");
336 ASSERT_OUTSIDE_BEGIN_END(ctx
);
338 if (!validate_stencil_op(ctx
, fail
)) {
339 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(sfail)");
342 if (!validate_stencil_op(ctx
, zfail
)) {
343 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zfail)");
346 if (!validate_stencil_op(ctx
, zpass
)) {
347 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zpass)");
352 /* only set active face state */
353 if (ctx
->Stencil
.ZFailFunc
[face
] == zfail
&&
354 ctx
->Stencil
.ZPassFunc
[face
] == zpass
&&
355 ctx
->Stencil
.FailFunc
[face
] == fail
)
357 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
358 ctx
->Stencil
.ZFailFunc
[face
] = zfail
;
359 ctx
->Stencil
.ZPassFunc
[face
] = zpass
;
360 ctx
->Stencil
.FailFunc
[face
] = fail
;
362 /* Only propagate the change to the driver if EXT_stencil_two_side
365 if (ctx
->Driver
.StencilOpSeparate
&& ctx
->Stencil
.TestTwoSide
) {
366 ctx
->Driver
.StencilOpSeparate(ctx
, GL_BACK
, fail
, zfail
, zpass
);
370 /* set both front and back state */
371 if (ctx
->Stencil
.ZFailFunc
[0] == zfail
&&
372 ctx
->Stencil
.ZFailFunc
[1] == zfail
&&
373 ctx
->Stencil
.ZPassFunc
[0] == zpass
&&
374 ctx
->Stencil
.ZPassFunc
[1] == zpass
&&
375 ctx
->Stencil
.FailFunc
[0] == fail
&&
376 ctx
->Stencil
.FailFunc
[1] == fail
)
378 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
379 ctx
->Stencil
.ZFailFunc
[0] = ctx
->Stencil
.ZFailFunc
[1] = zfail
;
380 ctx
->Stencil
.ZPassFunc
[0] = ctx
->Stencil
.ZPassFunc
[1] = zpass
;
381 ctx
->Stencil
.FailFunc
[0] = ctx
->Stencil
.FailFunc
[1] = fail
;
382 if (ctx
->Driver
.StencilOpSeparate
) {
383 ctx
->Driver
.StencilOpSeparate(ctx
,
384 ((ctx
->Stencil
.TestTwoSide
)
385 ? GL_FRONT
: GL_FRONT_AND_BACK
),
394 /* GL_EXT_stencil_two_side */
396 _mesa_ActiveStencilFaceEXT(GLenum face
)
398 GET_CURRENT_CONTEXT(ctx
);
399 ASSERT_OUTSIDE_BEGIN_END(ctx
);
401 if (MESA_VERBOSE
& VERBOSE_API
)
402 _mesa_debug(ctx
, "glActiveStencilFaceEXT()\n");
404 if (!ctx
->Extensions
.EXT_stencil_two_side
) {
405 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveStencilFaceEXT");
409 if (face
== GL_FRONT
|| face
== GL_BACK
) {
410 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
411 ctx
->Stencil
.ActiveFace
= (face
== GL_FRONT
) ? 0 : 2;
414 _mesa_error(ctx
, GL_INVALID_ENUM
, "glActiveStencilFaceEXT(face)");
422 * OpenGL 2.0 function.
423 * \todo Make StencilOp() call this function. And eventually remove the
424 * ctx->Driver.StencilOp function and use ctx->Driver.StencilOpSeparate
428 _mesa_StencilOpSeparate(GLenum face
, GLenum sfail
, GLenum zfail
, GLenum zpass
)
430 GLboolean set
= GL_FALSE
;
431 GET_CURRENT_CONTEXT(ctx
);
432 ASSERT_OUTSIDE_BEGIN_END(ctx
);
434 if (MESA_VERBOSE
& VERBOSE_API
)
435 _mesa_debug(ctx
, "glStencilOpSeparate()\n");
437 if (!validate_stencil_op(ctx
, sfail
)) {
438 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(sfail)");
441 if (!validate_stencil_op(ctx
, zfail
)) {
442 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zfail)");
445 if (!validate_stencil_op(ctx
, zpass
)) {
446 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zpass)");
449 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
450 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(face)");
454 if (face
!= GL_BACK
) {
456 if (ctx
->Stencil
.ZFailFunc
[0] != zfail
||
457 ctx
->Stencil
.ZPassFunc
[0] != zpass
||
458 ctx
->Stencil
.FailFunc
[0] != sfail
){
459 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
460 ctx
->Stencil
.ZFailFunc
[0] = zfail
;
461 ctx
->Stencil
.ZPassFunc
[0] = zpass
;
462 ctx
->Stencil
.FailFunc
[0] = sfail
;
466 if (face
!= GL_FRONT
) {
468 if (ctx
->Stencil
.ZFailFunc
[1] != zfail
||
469 ctx
->Stencil
.ZPassFunc
[1] != zpass
||
470 ctx
->Stencil
.FailFunc
[1] != sfail
) {
471 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
472 ctx
->Stencil
.ZFailFunc
[1] = zfail
;
473 ctx
->Stencil
.ZPassFunc
[1] = zpass
;
474 ctx
->Stencil
.FailFunc
[1] = sfail
;
478 if (set
&& ctx
->Driver
.StencilOpSeparate
) {
479 ctx
->Driver
.StencilOpSeparate(ctx
, face
, sfail
, zfail
, zpass
);
486 _mesa_StencilFuncSeparate(GLenum face
, GLenum func
, GLint ref
, GLuint mask
)
488 GET_CURRENT_CONTEXT(ctx
);
489 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
490 ASSERT_OUTSIDE_BEGIN_END(ctx
);
492 if (MESA_VERBOSE
& VERBOSE_API
)
493 _mesa_debug(ctx
, "glStencilFuncSeparate()\n");
495 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
496 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(face)");
499 if (!validate_stencil_func(ctx
, func
)) {
500 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(func)");
504 ref
= CLAMP(ref
, 0, stencilMax
);
506 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
508 if (face
!= GL_BACK
) {
510 ctx
->Stencil
.Function
[0] = func
;
511 ctx
->Stencil
.Ref
[0] = ref
;
512 ctx
->Stencil
.ValueMask
[0] = mask
;
514 if (face
!= GL_FRONT
) {
516 ctx
->Stencil
.Function
[1] = func
;
517 ctx
->Stencil
.Ref
[1] = ref
;
518 ctx
->Stencil
.ValueMask
[1] = mask
;
520 if (ctx
->Driver
.StencilFuncSeparate
) {
521 ctx
->Driver
.StencilFuncSeparate(ctx
, face
, func
, ref
, mask
);
528 _mesa_StencilMaskSeparate(GLenum face
, GLuint mask
)
530 GET_CURRENT_CONTEXT(ctx
);
531 ASSERT_OUTSIDE_BEGIN_END(ctx
);
533 if (MESA_VERBOSE
& VERBOSE_API
)
534 _mesa_debug(ctx
, "glStencilMaskSeparate()\n");
536 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
537 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilaMaskSeparate(face)");
541 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
543 if (face
!= GL_BACK
) {
544 ctx
->Stencil
.WriteMask
[0] = mask
;
546 if (face
!= GL_FRONT
) {
547 ctx
->Stencil
.WriteMask
[1] = mask
;
549 if (ctx
->Driver
.StencilMaskSeparate
) {
550 ctx
->Driver
.StencilMaskSeparate(ctx
, face
, mask
);
556 * Update derived stencil state.
559 _mesa_update_stencil(struct gl_context
*ctx
)
561 const GLint face
= ctx
->Stencil
._BackFace
;
563 ctx
->Stencil
._Enabled
= (ctx
->Stencil
.Enabled
&&
564 ctx
->DrawBuffer
->Visual
.stencilBits
> 0);
566 ctx
->Stencil
._TestTwoSide
=
567 ctx
->Stencil
._Enabled
&&
568 (ctx
->Stencil
.Function
[0] != ctx
->Stencil
.Function
[face
] ||
569 ctx
->Stencil
.FailFunc
[0] != ctx
->Stencil
.FailFunc
[face
] ||
570 ctx
->Stencil
.ZPassFunc
[0] != ctx
->Stencil
.ZPassFunc
[face
] ||
571 ctx
->Stencil
.ZFailFunc
[0] != ctx
->Stencil
.ZFailFunc
[face
] ||
572 ctx
->Stencil
.Ref
[0] != ctx
->Stencil
.Ref
[face
] ||
573 ctx
->Stencil
.ValueMask
[0] != ctx
->Stencil
.ValueMask
[face
] ||
574 ctx
->Stencil
.WriteMask
[0] != ctx
->Stencil
.WriteMask
[face
]);
579 * Initialize the context stipple state.
581 * \param ctx GL context.
583 * Initializes __struct gl_contextRec::Stencil attribute group.
586 _mesa_init_stencil(struct gl_context
*ctx
)
588 ctx
->Stencil
.Enabled
= GL_FALSE
;
589 ctx
->Stencil
.TestTwoSide
= GL_FALSE
;
590 ctx
->Stencil
.ActiveFace
= 0; /* 0 = GL_FRONT, 2 = GL_BACK */
591 ctx
->Stencil
.Function
[0] = GL_ALWAYS
;
592 ctx
->Stencil
.Function
[1] = GL_ALWAYS
;
593 ctx
->Stencil
.Function
[2] = GL_ALWAYS
;
594 ctx
->Stencil
.FailFunc
[0] = GL_KEEP
;
595 ctx
->Stencil
.FailFunc
[1] = GL_KEEP
;
596 ctx
->Stencil
.FailFunc
[2] = GL_KEEP
;
597 ctx
->Stencil
.ZPassFunc
[0] = GL_KEEP
;
598 ctx
->Stencil
.ZPassFunc
[1] = GL_KEEP
;
599 ctx
->Stencil
.ZPassFunc
[2] = GL_KEEP
;
600 ctx
->Stencil
.ZFailFunc
[0] = GL_KEEP
;
601 ctx
->Stencil
.ZFailFunc
[1] = GL_KEEP
;
602 ctx
->Stencil
.ZFailFunc
[2] = GL_KEEP
;
603 ctx
->Stencil
.Ref
[0] = 0;
604 ctx
->Stencil
.Ref
[1] = 0;
605 ctx
->Stencil
.Ref
[2] = 0;
606 ctx
->Stencil
.ValueMask
[0] = ~0U;
607 ctx
->Stencil
.ValueMask
[1] = ~0U;
608 ctx
->Stencil
.ValueMask
[2] = ~0U;
609 ctx
->Stencil
.WriteMask
[0] = ~0U;
610 ctx
->Stencil
.WriteMask
[1] = ~0U;
611 ctx
->Stencil
.WriteMask
[2] = ~0U;
612 ctx
->Stencil
.Clear
= 0;
613 ctx
->Stencil
._BackFace
= 1;