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
)
78 validate_stencil_func(struct gl_context
*ctx
, GLenum func
)
97 * Set the clear value for the stencil buffer.
99 * \param s clear value.
101 * \sa glClearStencil().
103 * Updates gl_stencil_attrib::Clear. On change
104 * flushes the vertices and notifies the driver via
105 * the dd_function_table::ClearStencil callback.
108 _mesa_ClearStencil( GLint s
)
110 GET_CURRENT_CONTEXT(ctx
);
111 ASSERT_OUTSIDE_BEGIN_END(ctx
);
113 if (ctx
->Stencil
.Clear
== (GLuint
) s
)
116 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
117 ctx
->Stencil
.Clear
= (GLuint
) s
;
119 if (ctx
->Driver
.ClearStencil
) {
120 ctx
->Driver
.ClearStencil( ctx
, s
);
126 * Set the function and reference value for stencil testing.
128 * \param frontfunc front test function.
129 * \param backfunc back test function.
130 * \param ref front and back reference value.
131 * \param mask front and back bitmask.
133 * \sa glStencilFunc().
135 * Verifies the parameters and updates the respective values in
136 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies the
137 * driver via the dd_function_table::StencilFunc callback.
140 _mesa_StencilFuncSeparateATI( GLenum frontfunc
, GLenum backfunc
, GLint ref
, GLuint mask
)
142 GET_CURRENT_CONTEXT(ctx
);
143 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
144 ASSERT_OUTSIDE_BEGIN_END(ctx
);
146 if (MESA_VERBOSE
& VERBOSE_API
)
147 _mesa_debug(ctx
, "glStencilFuncSeparateATI()\n");
149 if (!validate_stencil_func(ctx
, frontfunc
)) {
150 _mesa_error(ctx
, GL_INVALID_ENUM
,
151 "glStencilFuncSeparateATI(frontfunc)");
154 if (!validate_stencil_func(ctx
, backfunc
)) {
155 _mesa_error(ctx
, GL_INVALID_ENUM
,
156 "glStencilFuncSeparateATI(backfunc)");
160 ref
= CLAMP( ref
, 0, stencilMax
);
162 /* set both front and back state */
163 if (ctx
->Stencil
.Function
[0] == frontfunc
&&
164 ctx
->Stencil
.Function
[1] == backfunc
&&
165 ctx
->Stencil
.ValueMask
[0] == mask
&&
166 ctx
->Stencil
.ValueMask
[1] == mask
&&
167 ctx
->Stencil
.Ref
[0] == ref
&&
168 ctx
->Stencil
.Ref
[1] == ref
)
170 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
171 ctx
->Stencil
.Function
[0] = frontfunc
;
172 ctx
->Stencil
.Function
[1] = backfunc
;
173 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
174 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
175 if (ctx
->Driver
.StencilFuncSeparate
) {
176 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_FRONT
,
177 frontfunc
, ref
, mask
);
178 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_BACK
,
179 backfunc
, ref
, mask
);
185 * Set the function and reference value for stencil testing.
187 * \param func test function.
188 * \param ref reference value.
189 * \param mask bitmask.
191 * \sa glStencilFunc().
193 * Verifies the parameters and updates the respective values in
194 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies the
195 * driver via the dd_function_table::StencilFunc callback.
198 _mesa_StencilFunc( GLenum func
, GLint ref
, GLuint mask
)
200 GET_CURRENT_CONTEXT(ctx
);
201 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
202 const GLint face
= ctx
->Stencil
.ActiveFace
;
203 ASSERT_OUTSIDE_BEGIN_END(ctx
);
205 if (MESA_VERBOSE
& VERBOSE_API
)
206 _mesa_debug(ctx
, "glStencilFunc()\n");
208 if (!validate_stencil_func(ctx
, func
)) {
209 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFunc(func)");
213 ref
= CLAMP( ref
, 0, stencilMax
);
216 if (ctx
->Stencil
.Function
[face
] == func
&&
217 ctx
->Stencil
.ValueMask
[face
] == mask
&&
218 ctx
->Stencil
.Ref
[face
] == ref
)
220 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
221 ctx
->Stencil
.Function
[face
] = func
;
222 ctx
->Stencil
.Ref
[face
] = ref
;
223 ctx
->Stencil
.ValueMask
[face
] = mask
;
225 /* Only propagate the change to the driver if EXT_stencil_two_side
228 if (ctx
->Driver
.StencilFuncSeparate
&& ctx
->Stencil
.TestTwoSide
) {
229 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_BACK
, func
, ref
, mask
);
233 /* set both front and back state */
234 if (ctx
->Stencil
.Function
[0] == func
&&
235 ctx
->Stencil
.Function
[1] == func
&&
236 ctx
->Stencil
.ValueMask
[0] == mask
&&
237 ctx
->Stencil
.ValueMask
[1] == mask
&&
238 ctx
->Stencil
.Ref
[0] == ref
&&
239 ctx
->Stencil
.Ref
[1] == ref
)
241 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
242 ctx
->Stencil
.Function
[0] = ctx
->Stencil
.Function
[1] = func
;
243 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
244 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
245 if (ctx
->Driver
.StencilFuncSeparate
) {
246 ctx
->Driver
.StencilFuncSeparate(ctx
,
247 ((ctx
->Stencil
.TestTwoSide
)
248 ? GL_FRONT
: GL_FRONT_AND_BACK
),
256 * Set the stencil writing mask.
258 * \param mask bit-mask to enable/disable writing of individual bits in the
261 * \sa glStencilMask().
263 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
264 * notifies the driver via the dd_function_table::StencilMask callback.
267 _mesa_StencilMask( GLuint mask
)
269 GET_CURRENT_CONTEXT(ctx
);
270 const GLint face
= ctx
->Stencil
.ActiveFace
;
272 if (MESA_VERBOSE
& VERBOSE_API
)
273 _mesa_debug(ctx
, "glStencilMask()\n");
275 ASSERT_OUTSIDE_BEGIN_END(ctx
);
278 /* Only modify the EXT_stencil_two_side back-face state.
280 if (ctx
->Stencil
.WriteMask
[face
] == mask
)
282 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
283 ctx
->Stencil
.WriteMask
[face
] = mask
;
285 /* Only propagate the change to the driver if EXT_stencil_two_side
288 if (ctx
->Driver
.StencilMaskSeparate
&& ctx
->Stencil
.TestTwoSide
) {
289 ctx
->Driver
.StencilMaskSeparate(ctx
, GL_BACK
, mask
);
293 /* set both front and back state */
294 if (ctx
->Stencil
.WriteMask
[0] == mask
&&
295 ctx
->Stencil
.WriteMask
[1] == mask
)
297 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
298 ctx
->Stencil
.WriteMask
[0] = ctx
->Stencil
.WriteMask
[1] = mask
;
299 if (ctx
->Driver
.StencilMaskSeparate
) {
300 ctx
->Driver
.StencilMaskSeparate(ctx
,
301 ((ctx
->Stencil
.TestTwoSide
)
302 ? GL_FRONT
: GL_FRONT_AND_BACK
),
310 * Set the stencil test actions.
312 * \param fail action to take when stencil test fails.
313 * \param zfail action to take when stencil test passes, but depth test fails.
314 * \param zpass action to take when stencil test passes and the depth test
315 * passes (or depth testing is not enabled).
319 * Verifies the parameters and updates the respective fields in
320 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies the
321 * driver via the dd_function_table::StencilOp callback.
324 _mesa_StencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
)
326 GET_CURRENT_CONTEXT(ctx
);
327 const GLint face
= ctx
->Stencil
.ActiveFace
;
329 if (MESA_VERBOSE
& VERBOSE_API
)
330 _mesa_debug(ctx
, "glStencilOp()\n");
332 ASSERT_OUTSIDE_BEGIN_END(ctx
);
334 if (!validate_stencil_op(ctx
, fail
)) {
335 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(sfail)");
338 if (!validate_stencil_op(ctx
, zfail
)) {
339 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zfail)");
342 if (!validate_stencil_op(ctx
, zpass
)) {
343 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zpass)");
348 /* only set active face state */
349 if (ctx
->Stencil
.ZFailFunc
[face
] == zfail
&&
350 ctx
->Stencil
.ZPassFunc
[face
] == zpass
&&
351 ctx
->Stencil
.FailFunc
[face
] == fail
)
353 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
354 ctx
->Stencil
.ZFailFunc
[face
] = zfail
;
355 ctx
->Stencil
.ZPassFunc
[face
] = zpass
;
356 ctx
->Stencil
.FailFunc
[face
] = fail
;
358 /* Only propagate the change to the driver if EXT_stencil_two_side
361 if (ctx
->Driver
.StencilOpSeparate
&& ctx
->Stencil
.TestTwoSide
) {
362 ctx
->Driver
.StencilOpSeparate(ctx
, GL_BACK
, fail
, zfail
, zpass
);
366 /* set both front and back state */
367 if (ctx
->Stencil
.ZFailFunc
[0] == zfail
&&
368 ctx
->Stencil
.ZFailFunc
[1] == zfail
&&
369 ctx
->Stencil
.ZPassFunc
[0] == zpass
&&
370 ctx
->Stencil
.ZPassFunc
[1] == zpass
&&
371 ctx
->Stencil
.FailFunc
[0] == fail
&&
372 ctx
->Stencil
.FailFunc
[1] == fail
)
374 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
375 ctx
->Stencil
.ZFailFunc
[0] = ctx
->Stencil
.ZFailFunc
[1] = zfail
;
376 ctx
->Stencil
.ZPassFunc
[0] = ctx
->Stencil
.ZPassFunc
[1] = zpass
;
377 ctx
->Stencil
.FailFunc
[0] = ctx
->Stencil
.FailFunc
[1] = fail
;
378 if (ctx
->Driver
.StencilOpSeparate
) {
379 ctx
->Driver
.StencilOpSeparate(ctx
,
380 ((ctx
->Stencil
.TestTwoSide
)
381 ? GL_FRONT
: GL_FRONT_AND_BACK
),
390 /* GL_EXT_stencil_two_side */
392 _mesa_ActiveStencilFaceEXT(GLenum face
)
394 GET_CURRENT_CONTEXT(ctx
);
395 ASSERT_OUTSIDE_BEGIN_END(ctx
);
397 if (MESA_VERBOSE
& VERBOSE_API
)
398 _mesa_debug(ctx
, "glActiveStencilFaceEXT()\n");
400 if (!ctx
->Extensions
.EXT_stencil_two_side
) {
401 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveStencilFaceEXT");
405 if (face
== GL_FRONT
|| face
== GL_BACK
) {
406 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
407 ctx
->Stencil
.ActiveFace
= (face
== GL_FRONT
) ? 0 : 2;
410 _mesa_error(ctx
, GL_INVALID_ENUM
, "glActiveStencilFaceEXT(face)");
418 * OpenGL 2.0 function.
419 * \todo Make StencilOp() call this function. And eventually remove the
420 * ctx->Driver.StencilOp function and use ctx->Driver.StencilOpSeparate
424 _mesa_StencilOpSeparate(GLenum face
, GLenum sfail
, GLenum zfail
, GLenum zpass
)
426 GLboolean set
= GL_FALSE
;
427 GET_CURRENT_CONTEXT(ctx
);
428 ASSERT_OUTSIDE_BEGIN_END(ctx
);
430 if (MESA_VERBOSE
& VERBOSE_API
)
431 _mesa_debug(ctx
, "glStencilOpSeparate()\n");
433 if (!validate_stencil_op(ctx
, sfail
)) {
434 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(sfail)");
437 if (!validate_stencil_op(ctx
, zfail
)) {
438 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zfail)");
441 if (!validate_stencil_op(ctx
, zpass
)) {
442 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zpass)");
445 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
446 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(face)");
450 if (face
!= GL_BACK
) {
452 if (ctx
->Stencil
.ZFailFunc
[0] != zfail
||
453 ctx
->Stencil
.ZPassFunc
[0] != zpass
||
454 ctx
->Stencil
.FailFunc
[0] != sfail
){
455 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
456 ctx
->Stencil
.ZFailFunc
[0] = zfail
;
457 ctx
->Stencil
.ZPassFunc
[0] = zpass
;
458 ctx
->Stencil
.FailFunc
[0] = sfail
;
462 if (face
!= GL_FRONT
) {
464 if (ctx
->Stencil
.ZFailFunc
[1] != zfail
||
465 ctx
->Stencil
.ZPassFunc
[1] != zpass
||
466 ctx
->Stencil
.FailFunc
[1] != sfail
) {
467 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
468 ctx
->Stencil
.ZFailFunc
[1] = zfail
;
469 ctx
->Stencil
.ZPassFunc
[1] = zpass
;
470 ctx
->Stencil
.FailFunc
[1] = sfail
;
474 if (set
&& ctx
->Driver
.StencilOpSeparate
) {
475 ctx
->Driver
.StencilOpSeparate(ctx
, face
, sfail
, zfail
, zpass
);
482 _mesa_StencilFuncSeparate(GLenum face
, GLenum func
, GLint ref
, GLuint mask
)
484 GET_CURRENT_CONTEXT(ctx
);
485 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
486 ASSERT_OUTSIDE_BEGIN_END(ctx
);
488 if (MESA_VERBOSE
& VERBOSE_API
)
489 _mesa_debug(ctx
, "glStencilFuncSeparate()\n");
491 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
492 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(face)");
495 if (!validate_stencil_func(ctx
, func
)) {
496 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(func)");
500 ref
= CLAMP(ref
, 0, stencilMax
);
502 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
504 if (face
!= GL_BACK
) {
506 ctx
->Stencil
.Function
[0] = func
;
507 ctx
->Stencil
.Ref
[0] = ref
;
508 ctx
->Stencil
.ValueMask
[0] = mask
;
510 if (face
!= GL_FRONT
) {
512 ctx
->Stencil
.Function
[1] = func
;
513 ctx
->Stencil
.Ref
[1] = ref
;
514 ctx
->Stencil
.ValueMask
[1] = mask
;
516 if (ctx
->Driver
.StencilFuncSeparate
) {
517 ctx
->Driver
.StencilFuncSeparate(ctx
, face
, func
, ref
, mask
);
524 _mesa_StencilMaskSeparate(GLenum face
, GLuint mask
)
526 GET_CURRENT_CONTEXT(ctx
);
527 ASSERT_OUTSIDE_BEGIN_END(ctx
);
529 if (MESA_VERBOSE
& VERBOSE_API
)
530 _mesa_debug(ctx
, "glStencilMaskSeparate()\n");
532 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
533 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilaMaskSeparate(face)");
537 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
539 if (face
!= GL_BACK
) {
540 ctx
->Stencil
.WriteMask
[0] = mask
;
542 if (face
!= GL_FRONT
) {
543 ctx
->Stencil
.WriteMask
[1] = mask
;
545 if (ctx
->Driver
.StencilMaskSeparate
) {
546 ctx
->Driver
.StencilMaskSeparate(ctx
, face
, mask
);
552 * Update derived stencil state.
555 _mesa_update_stencil(struct gl_context
*ctx
)
557 const GLint face
= ctx
->Stencil
._BackFace
;
559 ctx
->Stencil
._Enabled
= (ctx
->Stencil
.Enabled
&&
560 ctx
->DrawBuffer
->Visual
.stencilBits
> 0);
562 ctx
->Stencil
._TestTwoSide
=
563 ctx
->Stencil
._Enabled
&&
564 (ctx
->Stencil
.Function
[0] != ctx
->Stencil
.Function
[face
] ||
565 ctx
->Stencil
.FailFunc
[0] != ctx
->Stencil
.FailFunc
[face
] ||
566 ctx
->Stencil
.ZPassFunc
[0] != ctx
->Stencil
.ZPassFunc
[face
] ||
567 ctx
->Stencil
.ZFailFunc
[0] != ctx
->Stencil
.ZFailFunc
[face
] ||
568 ctx
->Stencil
.Ref
[0] != ctx
->Stencil
.Ref
[face
] ||
569 ctx
->Stencil
.ValueMask
[0] != ctx
->Stencil
.ValueMask
[face
] ||
570 ctx
->Stencil
.WriteMask
[0] != ctx
->Stencil
.WriteMask
[face
]);
575 * Initialize the context stipple state.
577 * \param ctx GL context.
579 * Initializes __struct gl_contextRec::Stencil attribute group.
582 _mesa_init_stencil(struct gl_context
*ctx
)
584 ctx
->Stencil
.Enabled
= GL_FALSE
;
585 ctx
->Stencil
.TestTwoSide
= GL_FALSE
;
586 ctx
->Stencil
.ActiveFace
= 0; /* 0 = GL_FRONT, 2 = GL_BACK */
587 ctx
->Stencil
.Function
[0] = GL_ALWAYS
;
588 ctx
->Stencil
.Function
[1] = GL_ALWAYS
;
589 ctx
->Stencil
.Function
[2] = GL_ALWAYS
;
590 ctx
->Stencil
.FailFunc
[0] = GL_KEEP
;
591 ctx
->Stencil
.FailFunc
[1] = GL_KEEP
;
592 ctx
->Stencil
.FailFunc
[2] = GL_KEEP
;
593 ctx
->Stencil
.ZPassFunc
[0] = GL_KEEP
;
594 ctx
->Stencil
.ZPassFunc
[1] = GL_KEEP
;
595 ctx
->Stencil
.ZPassFunc
[2] = GL_KEEP
;
596 ctx
->Stencil
.ZFailFunc
[0] = GL_KEEP
;
597 ctx
->Stencil
.ZFailFunc
[1] = GL_KEEP
;
598 ctx
->Stencil
.ZFailFunc
[2] = GL_KEEP
;
599 ctx
->Stencil
.Ref
[0] = 0;
600 ctx
->Stencil
.Ref
[1] = 0;
601 ctx
->Stencil
.Ref
[2] = 0;
602 ctx
->Stencil
.ValueMask
[0] = ~0U;
603 ctx
->Stencil
.ValueMask
[1] = ~0U;
604 ctx
->Stencil
.ValueMask
[2] = ~0U;
605 ctx
->Stencil
.WriteMask
[0] = ~0U;
606 ctx
->Stencil
.WriteMask
[1] = ~0U;
607 ctx
->Stencil
.WriteMask
[2] = ~0U;
608 ctx
->Stencil
.Clear
= 0;
609 ctx
->Stencil
._BackFace
= 1;