2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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
);
112 ctx
->Stencil
.Clear
= (GLuint
) s
;
117 * Set the function and reference value for stencil testing.
119 * \param frontfunc front test function.
120 * \param backfunc back test function.
121 * \param ref front and back reference value.
122 * \param mask front and back bitmask.
124 * \sa glStencilFunc().
126 * Verifies the parameters and updates the respective values in
127 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies the
128 * driver via the dd_function_table::StencilFunc callback.
131 _mesa_StencilFuncSeparateATI( GLenum frontfunc
, GLenum backfunc
, GLint ref
, GLuint mask
)
133 GET_CURRENT_CONTEXT(ctx
);
135 if (MESA_VERBOSE
& VERBOSE_API
)
136 _mesa_debug(ctx
, "glStencilFuncSeparateATI()\n");
138 if (!validate_stencil_func(ctx
, frontfunc
)) {
139 _mesa_error(ctx
, GL_INVALID_ENUM
,
140 "glStencilFuncSeparateATI(frontfunc)");
143 if (!validate_stencil_func(ctx
, backfunc
)) {
144 _mesa_error(ctx
, GL_INVALID_ENUM
,
145 "glStencilFuncSeparateATI(backfunc)");
149 /* set both front and back state */
150 if (ctx
->Stencil
.Function
[0] == frontfunc
&&
151 ctx
->Stencil
.Function
[1] == backfunc
&&
152 ctx
->Stencil
.ValueMask
[0] == mask
&&
153 ctx
->Stencil
.ValueMask
[1] == mask
&&
154 ctx
->Stencil
.Ref
[0] == ref
&&
155 ctx
->Stencil
.Ref
[1] == ref
)
157 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
158 ctx
->Stencil
.Function
[0] = frontfunc
;
159 ctx
->Stencil
.Function
[1] = backfunc
;
160 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
161 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
162 if (ctx
->Driver
.StencilFuncSeparate
) {
163 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_FRONT
,
164 frontfunc
, ref
, mask
);
165 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_BACK
,
166 backfunc
, ref
, mask
);
172 * Set the function and reference value for stencil testing.
174 * \param func test function.
175 * \param ref reference value.
176 * \param mask bitmask.
178 * \sa glStencilFunc().
180 * Verifies the parameters and updates the respective values in
181 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies the
182 * driver via the dd_function_table::StencilFunc callback.
185 _mesa_StencilFunc( GLenum func
, GLint ref
, GLuint mask
)
187 GET_CURRENT_CONTEXT(ctx
);
188 const GLint face
= ctx
->Stencil
.ActiveFace
;
190 if (MESA_VERBOSE
& VERBOSE_API
)
191 _mesa_debug(ctx
, "glStencilFunc()\n");
193 if (!validate_stencil_func(ctx
, func
)) {
194 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFunc(func)");
199 if (ctx
->Stencil
.Function
[face
] == func
&&
200 ctx
->Stencil
.ValueMask
[face
] == mask
&&
201 ctx
->Stencil
.Ref
[face
] == ref
)
203 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
204 ctx
->Stencil
.Function
[face
] = func
;
205 ctx
->Stencil
.Ref
[face
] = ref
;
206 ctx
->Stencil
.ValueMask
[face
] = mask
;
208 /* Only propagate the change to the driver if EXT_stencil_two_side
211 if (ctx
->Driver
.StencilFuncSeparate
&& ctx
->Stencil
.TestTwoSide
) {
212 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_BACK
, func
, ref
, mask
);
216 /* set both front and back state */
217 if (ctx
->Stencil
.Function
[0] == func
&&
218 ctx
->Stencil
.Function
[1] == func
&&
219 ctx
->Stencil
.ValueMask
[0] == mask
&&
220 ctx
->Stencil
.ValueMask
[1] == mask
&&
221 ctx
->Stencil
.Ref
[0] == ref
&&
222 ctx
->Stencil
.Ref
[1] == ref
)
224 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
225 ctx
->Stencil
.Function
[0] = ctx
->Stencil
.Function
[1] = func
;
226 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
227 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
228 if (ctx
->Driver
.StencilFuncSeparate
) {
229 ctx
->Driver
.StencilFuncSeparate(ctx
,
230 ((ctx
->Stencil
.TestTwoSide
)
231 ? GL_FRONT
: GL_FRONT_AND_BACK
),
239 * Set the stencil writing mask.
241 * \param mask bit-mask to enable/disable writing of individual bits in the
244 * \sa glStencilMask().
246 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
247 * notifies the driver via the dd_function_table::StencilMask callback.
250 _mesa_StencilMask( GLuint mask
)
252 GET_CURRENT_CONTEXT(ctx
);
253 const GLint face
= ctx
->Stencil
.ActiveFace
;
255 if (MESA_VERBOSE
& VERBOSE_API
)
256 _mesa_debug(ctx
, "glStencilMask()\n");
259 /* Only modify the EXT_stencil_two_side back-face state.
261 if (ctx
->Stencil
.WriteMask
[face
] == mask
)
263 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
264 ctx
->Stencil
.WriteMask
[face
] = mask
;
266 /* Only propagate the change to the driver if EXT_stencil_two_side
269 if (ctx
->Driver
.StencilMaskSeparate
&& ctx
->Stencil
.TestTwoSide
) {
270 ctx
->Driver
.StencilMaskSeparate(ctx
, GL_BACK
, mask
);
274 /* set both front and back state */
275 if (ctx
->Stencil
.WriteMask
[0] == mask
&&
276 ctx
->Stencil
.WriteMask
[1] == mask
)
278 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
279 ctx
->Stencil
.WriteMask
[0] = ctx
->Stencil
.WriteMask
[1] = mask
;
280 if (ctx
->Driver
.StencilMaskSeparate
) {
281 ctx
->Driver
.StencilMaskSeparate(ctx
,
282 ((ctx
->Stencil
.TestTwoSide
)
283 ? GL_FRONT
: GL_FRONT_AND_BACK
),
291 * Set the stencil test actions.
293 * \param fail action to take when stencil test fails.
294 * \param zfail action to take when stencil test passes, but depth test fails.
295 * \param zpass action to take when stencil test passes and the depth test
296 * passes (or depth testing is not enabled).
300 * Verifies the parameters and updates the respective fields in
301 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies the
302 * driver via the dd_function_table::StencilOp callback.
305 _mesa_StencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
)
307 GET_CURRENT_CONTEXT(ctx
);
308 const GLint face
= ctx
->Stencil
.ActiveFace
;
310 if (MESA_VERBOSE
& VERBOSE_API
)
311 _mesa_debug(ctx
, "glStencilOp()\n");
313 if (!validate_stencil_op(ctx
, fail
)) {
314 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(sfail)");
317 if (!validate_stencil_op(ctx
, zfail
)) {
318 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zfail)");
321 if (!validate_stencil_op(ctx
, zpass
)) {
322 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zpass)");
327 /* only set active face state */
328 if (ctx
->Stencil
.ZFailFunc
[face
] == zfail
&&
329 ctx
->Stencil
.ZPassFunc
[face
] == zpass
&&
330 ctx
->Stencil
.FailFunc
[face
] == fail
)
332 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
333 ctx
->Stencil
.ZFailFunc
[face
] = zfail
;
334 ctx
->Stencil
.ZPassFunc
[face
] = zpass
;
335 ctx
->Stencil
.FailFunc
[face
] = fail
;
337 /* Only propagate the change to the driver if EXT_stencil_two_side
340 if (ctx
->Driver
.StencilOpSeparate
&& ctx
->Stencil
.TestTwoSide
) {
341 ctx
->Driver
.StencilOpSeparate(ctx
, GL_BACK
, fail
, zfail
, zpass
);
345 /* set both front and back state */
346 if (ctx
->Stencil
.ZFailFunc
[0] == zfail
&&
347 ctx
->Stencil
.ZFailFunc
[1] == zfail
&&
348 ctx
->Stencil
.ZPassFunc
[0] == zpass
&&
349 ctx
->Stencil
.ZPassFunc
[1] == zpass
&&
350 ctx
->Stencil
.FailFunc
[0] == fail
&&
351 ctx
->Stencil
.FailFunc
[1] == fail
)
353 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
354 ctx
->Stencil
.ZFailFunc
[0] = ctx
->Stencil
.ZFailFunc
[1] = zfail
;
355 ctx
->Stencil
.ZPassFunc
[0] = ctx
->Stencil
.ZPassFunc
[1] = zpass
;
356 ctx
->Stencil
.FailFunc
[0] = ctx
->Stencil
.FailFunc
[1] = fail
;
357 if (ctx
->Driver
.StencilOpSeparate
) {
358 ctx
->Driver
.StencilOpSeparate(ctx
,
359 ((ctx
->Stencil
.TestTwoSide
)
360 ? GL_FRONT
: GL_FRONT_AND_BACK
),
368 /* GL_EXT_stencil_two_side */
370 _mesa_ActiveStencilFaceEXT(GLenum face
)
372 GET_CURRENT_CONTEXT(ctx
);
374 if (MESA_VERBOSE
& VERBOSE_API
)
375 _mesa_debug(ctx
, "glActiveStencilFaceEXT()\n");
377 if (!ctx
->Extensions
.EXT_stencil_two_side
) {
378 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveStencilFaceEXT");
382 if (face
== GL_FRONT
|| face
== GL_BACK
) {
383 ctx
->Stencil
.ActiveFace
= (face
== GL_FRONT
) ? 0 : 2;
386 _mesa_error(ctx
, GL_INVALID_ENUM
, "glActiveStencilFaceEXT(face)");
393 * OpenGL 2.0 function.
394 * \todo Make StencilOp() call this function. And eventually remove the
395 * ctx->Driver.StencilOp function and use ctx->Driver.StencilOpSeparate
399 _mesa_StencilOpSeparate(GLenum face
, GLenum sfail
, GLenum zfail
, GLenum zpass
)
401 GLboolean set
= GL_FALSE
;
402 GET_CURRENT_CONTEXT(ctx
);
404 if (MESA_VERBOSE
& VERBOSE_API
)
405 _mesa_debug(ctx
, "glStencilOpSeparate()\n");
407 if (!validate_stencil_op(ctx
, sfail
)) {
408 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(sfail)");
411 if (!validate_stencil_op(ctx
, zfail
)) {
412 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zfail)");
415 if (!validate_stencil_op(ctx
, zpass
)) {
416 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zpass)");
419 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
420 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(face)");
424 if (face
!= GL_BACK
) {
426 if (ctx
->Stencil
.ZFailFunc
[0] != zfail
||
427 ctx
->Stencil
.ZPassFunc
[0] != zpass
||
428 ctx
->Stencil
.FailFunc
[0] != sfail
){
429 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
430 ctx
->Stencil
.ZFailFunc
[0] = zfail
;
431 ctx
->Stencil
.ZPassFunc
[0] = zpass
;
432 ctx
->Stencil
.FailFunc
[0] = sfail
;
436 if (face
!= GL_FRONT
) {
438 if (ctx
->Stencil
.ZFailFunc
[1] != zfail
||
439 ctx
->Stencil
.ZPassFunc
[1] != zpass
||
440 ctx
->Stencil
.FailFunc
[1] != sfail
) {
441 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
442 ctx
->Stencil
.ZFailFunc
[1] = zfail
;
443 ctx
->Stencil
.ZPassFunc
[1] = zpass
;
444 ctx
->Stencil
.FailFunc
[1] = sfail
;
448 if (set
&& ctx
->Driver
.StencilOpSeparate
) {
449 ctx
->Driver
.StencilOpSeparate(ctx
, face
, sfail
, zfail
, zpass
);
456 _mesa_StencilFuncSeparate(GLenum face
, GLenum func
, GLint ref
, GLuint mask
)
458 GET_CURRENT_CONTEXT(ctx
);
460 if (MESA_VERBOSE
& VERBOSE_API
)
461 _mesa_debug(ctx
, "glStencilFuncSeparate()\n");
463 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
464 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(face)");
467 if (!validate_stencil_func(ctx
, func
)) {
468 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(func)");
472 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
474 if (face
!= GL_BACK
) {
476 ctx
->Stencil
.Function
[0] = func
;
477 ctx
->Stencil
.Ref
[0] = ref
;
478 ctx
->Stencil
.ValueMask
[0] = mask
;
480 if (face
!= GL_FRONT
) {
482 ctx
->Stencil
.Function
[1] = func
;
483 ctx
->Stencil
.Ref
[1] = ref
;
484 ctx
->Stencil
.ValueMask
[1] = mask
;
486 if (ctx
->Driver
.StencilFuncSeparate
) {
487 ctx
->Driver
.StencilFuncSeparate(ctx
, face
, func
, ref
, mask
);
494 _mesa_StencilMaskSeparate(GLenum face
, GLuint mask
)
496 GET_CURRENT_CONTEXT(ctx
);
498 if (MESA_VERBOSE
& VERBOSE_API
)
499 _mesa_debug(ctx
, "glStencilMaskSeparate()\n");
501 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
502 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilaMaskSeparate(face)");
506 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
508 if (face
!= GL_BACK
) {
509 ctx
->Stencil
.WriteMask
[0] = mask
;
511 if (face
!= GL_FRONT
) {
512 ctx
->Stencil
.WriteMask
[1] = mask
;
514 if (ctx
->Driver
.StencilMaskSeparate
) {
515 ctx
->Driver
.StencilMaskSeparate(ctx
, face
, mask
);
521 * Update derived stencil state.
524 _mesa_update_stencil(struct gl_context
*ctx
)
526 const GLint face
= ctx
->Stencil
._BackFace
;
528 ctx
->Stencil
._Enabled
= (ctx
->Stencil
.Enabled
&&
529 ctx
->DrawBuffer
->Visual
.stencilBits
> 0);
531 ctx
->Stencil
._TestTwoSide
=
532 ctx
->Stencil
._Enabled
&&
533 (ctx
->Stencil
.Function
[0] != ctx
->Stencil
.Function
[face
] ||
534 ctx
->Stencil
.FailFunc
[0] != ctx
->Stencil
.FailFunc
[face
] ||
535 ctx
->Stencil
.ZPassFunc
[0] != ctx
->Stencil
.ZPassFunc
[face
] ||
536 ctx
->Stencil
.ZFailFunc
[0] != ctx
->Stencil
.ZFailFunc
[face
] ||
537 ctx
->Stencil
.Ref
[0] != ctx
->Stencil
.Ref
[face
] ||
538 ctx
->Stencil
.ValueMask
[0] != ctx
->Stencil
.ValueMask
[face
] ||
539 ctx
->Stencil
.WriteMask
[0] != ctx
->Stencil
.WriteMask
[face
]);
541 ctx
->Stencil
._WriteEnabled
=
542 ctx
->Stencil
._Enabled
&&
543 (ctx
->Stencil
.WriteMask
[0] != 0 ||
544 (ctx
->Stencil
._TestTwoSide
&& ctx
->Stencil
.WriteMask
[face
] != 0));
549 * Initialize the context stipple state.
551 * \param ctx GL context.
553 * Initializes __struct gl_contextRec::Stencil attribute group.
556 _mesa_init_stencil(struct gl_context
*ctx
)
558 ctx
->Stencil
.Enabled
= GL_FALSE
;
559 ctx
->Stencil
.TestTwoSide
= GL_FALSE
;
560 ctx
->Stencil
.ActiveFace
= 0; /* 0 = GL_FRONT, 2 = GL_BACK */
561 ctx
->Stencil
.Function
[0] = GL_ALWAYS
;
562 ctx
->Stencil
.Function
[1] = GL_ALWAYS
;
563 ctx
->Stencil
.Function
[2] = GL_ALWAYS
;
564 ctx
->Stencil
.FailFunc
[0] = GL_KEEP
;
565 ctx
->Stencil
.FailFunc
[1] = GL_KEEP
;
566 ctx
->Stencil
.FailFunc
[2] = GL_KEEP
;
567 ctx
->Stencil
.ZPassFunc
[0] = GL_KEEP
;
568 ctx
->Stencil
.ZPassFunc
[1] = GL_KEEP
;
569 ctx
->Stencil
.ZPassFunc
[2] = GL_KEEP
;
570 ctx
->Stencil
.ZFailFunc
[0] = GL_KEEP
;
571 ctx
->Stencil
.ZFailFunc
[1] = GL_KEEP
;
572 ctx
->Stencil
.ZFailFunc
[2] = GL_KEEP
;
573 ctx
->Stencil
.Ref
[0] = 0;
574 ctx
->Stencil
.Ref
[1] = 0;
575 ctx
->Stencil
.Ref
[2] = 0;
577 /* 4.1.4 Stencil Test section of the GL-ES 3.0 specification says:
579 * "In the initial state, [...] the front and back stencil mask are both
580 * set to the value 2^s − 1, where s is greater than or equal to the
581 * number of bits in the deepest stencil buffer* supported by the GL
584 * Since the maximum supported precision for stencil buffers is 8 bits,
585 * mask values should be initialized to 2^8 - 1 = 0xFF.
587 ctx
->Stencil
.ValueMask
[0] = 0xFF;
588 ctx
->Stencil
.ValueMask
[1] = 0xFF;
589 ctx
->Stencil
.ValueMask
[2] = 0xFF;
590 ctx
->Stencil
.WriteMask
[0] = 0xFF;
591 ctx
->Stencil
.WriteMask
[1] = 0xFF;
592 ctx
->Stencil
.WriteMask
[2] = 0xFF;
594 ctx
->Stencil
.Clear
= 0;
595 ctx
->Stencil
._BackFace
= 1;