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 * THE AUTHORS 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
);
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
);
134 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
136 if (MESA_VERBOSE
& VERBOSE_API
)
137 _mesa_debug(ctx
, "glStencilFuncSeparateATI()\n");
139 if (!validate_stencil_func(ctx
, frontfunc
)) {
140 _mesa_error(ctx
, GL_INVALID_ENUM
,
141 "glStencilFuncSeparateATI(frontfunc)");
144 if (!validate_stencil_func(ctx
, backfunc
)) {
145 _mesa_error(ctx
, GL_INVALID_ENUM
,
146 "glStencilFuncSeparateATI(backfunc)");
150 ref
= CLAMP( ref
, 0, stencilMax
);
152 /* set both front and back state */
153 if (ctx
->Stencil
.Function
[0] == frontfunc
&&
154 ctx
->Stencil
.Function
[1] == backfunc
&&
155 ctx
->Stencil
.ValueMask
[0] == mask
&&
156 ctx
->Stencil
.ValueMask
[1] == mask
&&
157 ctx
->Stencil
.Ref
[0] == ref
&&
158 ctx
->Stencil
.Ref
[1] == ref
)
160 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
161 ctx
->Stencil
.Function
[0] = frontfunc
;
162 ctx
->Stencil
.Function
[1] = backfunc
;
163 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
164 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
165 if (ctx
->Driver
.StencilFuncSeparate
) {
166 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_FRONT
,
167 frontfunc
, ref
, mask
);
168 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_BACK
,
169 backfunc
, ref
, mask
);
175 * Set the function and reference value for stencil testing.
177 * \param func test function.
178 * \param ref reference value.
179 * \param mask bitmask.
181 * \sa glStencilFunc().
183 * Verifies the parameters and updates the respective values in
184 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies the
185 * driver via the dd_function_table::StencilFunc callback.
188 _mesa_StencilFunc( GLenum func
, GLint ref
, GLuint mask
)
190 GET_CURRENT_CONTEXT(ctx
);
191 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
192 const GLint face
= ctx
->Stencil
.ActiveFace
;
194 if (MESA_VERBOSE
& VERBOSE_API
)
195 _mesa_debug(ctx
, "glStencilFunc()\n");
197 if (!validate_stencil_func(ctx
, func
)) {
198 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFunc(func)");
202 ref
= CLAMP( ref
, 0, stencilMax
);
205 if (ctx
->Stencil
.Function
[face
] == func
&&
206 ctx
->Stencil
.ValueMask
[face
] == mask
&&
207 ctx
->Stencil
.Ref
[face
] == ref
)
209 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
210 ctx
->Stencil
.Function
[face
] = func
;
211 ctx
->Stencil
.Ref
[face
] = ref
;
212 ctx
->Stencil
.ValueMask
[face
] = mask
;
214 /* Only propagate the change to the driver if EXT_stencil_two_side
217 if (ctx
->Driver
.StencilFuncSeparate
&& ctx
->Stencil
.TestTwoSide
) {
218 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_BACK
, func
, ref
, mask
);
222 /* set both front and back state */
223 if (ctx
->Stencil
.Function
[0] == func
&&
224 ctx
->Stencil
.Function
[1] == func
&&
225 ctx
->Stencil
.ValueMask
[0] == mask
&&
226 ctx
->Stencil
.ValueMask
[1] == mask
&&
227 ctx
->Stencil
.Ref
[0] == ref
&&
228 ctx
->Stencil
.Ref
[1] == ref
)
230 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
231 ctx
->Stencil
.Function
[0] = ctx
->Stencil
.Function
[1] = func
;
232 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
233 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
234 if (ctx
->Driver
.StencilFuncSeparate
) {
235 ctx
->Driver
.StencilFuncSeparate(ctx
,
236 ((ctx
->Stencil
.TestTwoSide
)
237 ? GL_FRONT
: GL_FRONT_AND_BACK
),
245 * Set the stencil writing mask.
247 * \param mask bit-mask to enable/disable writing of individual bits in the
250 * \sa glStencilMask().
252 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
253 * notifies the driver via the dd_function_table::StencilMask callback.
256 _mesa_StencilMask( GLuint mask
)
258 GET_CURRENT_CONTEXT(ctx
);
259 const GLint face
= ctx
->Stencil
.ActiveFace
;
261 if (MESA_VERBOSE
& VERBOSE_API
)
262 _mesa_debug(ctx
, "glStencilMask()\n");
265 /* Only modify the EXT_stencil_two_side back-face state.
267 if (ctx
->Stencil
.WriteMask
[face
] == mask
)
269 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
270 ctx
->Stencil
.WriteMask
[face
] = mask
;
272 /* Only propagate the change to the driver if EXT_stencil_two_side
275 if (ctx
->Driver
.StencilMaskSeparate
&& ctx
->Stencil
.TestTwoSide
) {
276 ctx
->Driver
.StencilMaskSeparate(ctx
, GL_BACK
, mask
);
280 /* set both front and back state */
281 if (ctx
->Stencil
.WriteMask
[0] == mask
&&
282 ctx
->Stencil
.WriteMask
[1] == mask
)
284 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
285 ctx
->Stencil
.WriteMask
[0] = ctx
->Stencil
.WriteMask
[1] = mask
;
286 if (ctx
->Driver
.StencilMaskSeparate
) {
287 ctx
->Driver
.StencilMaskSeparate(ctx
,
288 ((ctx
->Stencil
.TestTwoSide
)
289 ? GL_FRONT
: GL_FRONT_AND_BACK
),
297 * Set the stencil test actions.
299 * \param fail action to take when stencil test fails.
300 * \param zfail action to take when stencil test passes, but depth test fails.
301 * \param zpass action to take when stencil test passes and the depth test
302 * passes (or depth testing is not enabled).
306 * Verifies the parameters and updates the respective fields in
307 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies the
308 * driver via the dd_function_table::StencilOp callback.
311 _mesa_StencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
)
313 GET_CURRENT_CONTEXT(ctx
);
314 const GLint face
= ctx
->Stencil
.ActiveFace
;
316 if (MESA_VERBOSE
& VERBOSE_API
)
317 _mesa_debug(ctx
, "glStencilOp()\n");
319 if (!validate_stencil_op(ctx
, fail
)) {
320 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(sfail)");
323 if (!validate_stencil_op(ctx
, zfail
)) {
324 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zfail)");
327 if (!validate_stencil_op(ctx
, zpass
)) {
328 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zpass)");
333 /* only set active face state */
334 if (ctx
->Stencil
.ZFailFunc
[face
] == zfail
&&
335 ctx
->Stencil
.ZPassFunc
[face
] == zpass
&&
336 ctx
->Stencil
.FailFunc
[face
] == fail
)
338 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
339 ctx
->Stencil
.ZFailFunc
[face
] = zfail
;
340 ctx
->Stencil
.ZPassFunc
[face
] = zpass
;
341 ctx
->Stencil
.FailFunc
[face
] = fail
;
343 /* Only propagate the change to the driver if EXT_stencil_two_side
346 if (ctx
->Driver
.StencilOpSeparate
&& ctx
->Stencil
.TestTwoSide
) {
347 ctx
->Driver
.StencilOpSeparate(ctx
, GL_BACK
, fail
, zfail
, zpass
);
351 /* set both front and back state */
352 if (ctx
->Stencil
.ZFailFunc
[0] == zfail
&&
353 ctx
->Stencil
.ZFailFunc
[1] == zfail
&&
354 ctx
->Stencil
.ZPassFunc
[0] == zpass
&&
355 ctx
->Stencil
.ZPassFunc
[1] == zpass
&&
356 ctx
->Stencil
.FailFunc
[0] == fail
&&
357 ctx
->Stencil
.FailFunc
[1] == fail
)
359 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
360 ctx
->Stencil
.ZFailFunc
[0] = ctx
->Stencil
.ZFailFunc
[1] = zfail
;
361 ctx
->Stencil
.ZPassFunc
[0] = ctx
->Stencil
.ZPassFunc
[1] = zpass
;
362 ctx
->Stencil
.FailFunc
[0] = ctx
->Stencil
.FailFunc
[1] = fail
;
363 if (ctx
->Driver
.StencilOpSeparate
) {
364 ctx
->Driver
.StencilOpSeparate(ctx
,
365 ((ctx
->Stencil
.TestTwoSide
)
366 ? GL_FRONT
: GL_FRONT_AND_BACK
),
374 /* GL_EXT_stencil_two_side */
376 _mesa_ActiveStencilFaceEXT(GLenum face
)
378 GET_CURRENT_CONTEXT(ctx
);
380 if (MESA_VERBOSE
& VERBOSE_API
)
381 _mesa_debug(ctx
, "glActiveStencilFaceEXT()\n");
383 if (!ctx
->Extensions
.EXT_stencil_two_side
) {
384 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveStencilFaceEXT");
388 if (face
== GL_FRONT
|| face
== GL_BACK
) {
389 ctx
->Stencil
.ActiveFace
= (face
== GL_FRONT
) ? 0 : 2;
392 _mesa_error(ctx
, GL_INVALID_ENUM
, "glActiveStencilFaceEXT(face)");
399 * OpenGL 2.0 function.
400 * \todo Make StencilOp() call this function. And eventually remove the
401 * ctx->Driver.StencilOp function and use ctx->Driver.StencilOpSeparate
405 _mesa_StencilOpSeparate(GLenum face
, GLenum sfail
, GLenum zfail
, GLenum zpass
)
407 GLboolean set
= GL_FALSE
;
408 GET_CURRENT_CONTEXT(ctx
);
410 if (MESA_VERBOSE
& VERBOSE_API
)
411 _mesa_debug(ctx
, "glStencilOpSeparate()\n");
413 if (!validate_stencil_op(ctx
, sfail
)) {
414 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(sfail)");
417 if (!validate_stencil_op(ctx
, zfail
)) {
418 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zfail)");
421 if (!validate_stencil_op(ctx
, zpass
)) {
422 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zpass)");
425 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
426 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(face)");
430 if (face
!= GL_BACK
) {
432 if (ctx
->Stencil
.ZFailFunc
[0] != zfail
||
433 ctx
->Stencil
.ZPassFunc
[0] != zpass
||
434 ctx
->Stencil
.FailFunc
[0] != sfail
){
435 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
436 ctx
->Stencil
.ZFailFunc
[0] = zfail
;
437 ctx
->Stencil
.ZPassFunc
[0] = zpass
;
438 ctx
->Stencil
.FailFunc
[0] = sfail
;
442 if (face
!= GL_FRONT
) {
444 if (ctx
->Stencil
.ZFailFunc
[1] != zfail
||
445 ctx
->Stencil
.ZPassFunc
[1] != zpass
||
446 ctx
->Stencil
.FailFunc
[1] != sfail
) {
447 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
448 ctx
->Stencil
.ZFailFunc
[1] = zfail
;
449 ctx
->Stencil
.ZPassFunc
[1] = zpass
;
450 ctx
->Stencil
.FailFunc
[1] = sfail
;
454 if (set
&& ctx
->Driver
.StencilOpSeparate
) {
455 ctx
->Driver
.StencilOpSeparate(ctx
, face
, sfail
, zfail
, zpass
);
462 _mesa_StencilFuncSeparate(GLenum face
, GLenum func
, GLint ref
, GLuint mask
)
464 GET_CURRENT_CONTEXT(ctx
);
465 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
467 if (MESA_VERBOSE
& VERBOSE_API
)
468 _mesa_debug(ctx
, "glStencilFuncSeparate()\n");
470 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
471 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(face)");
474 if (!validate_stencil_func(ctx
, func
)) {
475 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(func)");
479 ref
= CLAMP(ref
, 0, stencilMax
);
481 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
483 if (face
!= GL_BACK
) {
485 ctx
->Stencil
.Function
[0] = func
;
486 ctx
->Stencil
.Ref
[0] = ref
;
487 ctx
->Stencil
.ValueMask
[0] = mask
;
489 if (face
!= GL_FRONT
) {
491 ctx
->Stencil
.Function
[1] = func
;
492 ctx
->Stencil
.Ref
[1] = ref
;
493 ctx
->Stencil
.ValueMask
[1] = mask
;
495 if (ctx
->Driver
.StencilFuncSeparate
) {
496 ctx
->Driver
.StencilFuncSeparate(ctx
, face
, func
, ref
, mask
);
503 _mesa_StencilMaskSeparate(GLenum face
, GLuint mask
)
505 GET_CURRENT_CONTEXT(ctx
);
507 if (MESA_VERBOSE
& VERBOSE_API
)
508 _mesa_debug(ctx
, "glStencilMaskSeparate()\n");
510 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
511 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilaMaskSeparate(face)");
515 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
517 if (face
!= GL_BACK
) {
518 ctx
->Stencil
.WriteMask
[0] = mask
;
520 if (face
!= GL_FRONT
) {
521 ctx
->Stencil
.WriteMask
[1] = mask
;
523 if (ctx
->Driver
.StencilMaskSeparate
) {
524 ctx
->Driver
.StencilMaskSeparate(ctx
, face
, mask
);
530 * Update derived stencil state.
533 _mesa_update_stencil(struct gl_context
*ctx
)
535 const GLint face
= ctx
->Stencil
._BackFace
;
537 ctx
->Stencil
._Enabled
= (ctx
->Stencil
.Enabled
&&
538 ctx
->DrawBuffer
->Visual
.stencilBits
> 0);
540 ctx
->Stencil
._TestTwoSide
=
541 ctx
->Stencil
._Enabled
&&
542 (ctx
->Stencil
.Function
[0] != ctx
->Stencil
.Function
[face
] ||
543 ctx
->Stencil
.FailFunc
[0] != ctx
->Stencil
.FailFunc
[face
] ||
544 ctx
->Stencil
.ZPassFunc
[0] != ctx
->Stencil
.ZPassFunc
[face
] ||
545 ctx
->Stencil
.ZFailFunc
[0] != ctx
->Stencil
.ZFailFunc
[face
] ||
546 ctx
->Stencil
.Ref
[0] != ctx
->Stencil
.Ref
[face
] ||
547 ctx
->Stencil
.ValueMask
[0] != ctx
->Stencil
.ValueMask
[face
] ||
548 ctx
->Stencil
.WriteMask
[0] != ctx
->Stencil
.WriteMask
[face
]);
550 ctx
->Stencil
._WriteEnabled
=
551 ctx
->Stencil
._Enabled
&&
552 (ctx
->Stencil
.WriteMask
[0] != 0 ||
553 (ctx
->Stencil
._TestTwoSide
&& ctx
->Stencil
.WriteMask
[face
] != 0));
558 * Initialize the context stipple state.
560 * \param ctx GL context.
562 * Initializes __struct gl_contextRec::Stencil attribute group.
565 _mesa_init_stencil(struct gl_context
*ctx
)
567 ctx
->Stencil
.Enabled
= GL_FALSE
;
568 ctx
->Stencil
.TestTwoSide
= GL_FALSE
;
569 ctx
->Stencil
.ActiveFace
= 0; /* 0 = GL_FRONT, 2 = GL_BACK */
570 ctx
->Stencil
.Function
[0] = GL_ALWAYS
;
571 ctx
->Stencil
.Function
[1] = GL_ALWAYS
;
572 ctx
->Stencil
.Function
[2] = GL_ALWAYS
;
573 ctx
->Stencil
.FailFunc
[0] = GL_KEEP
;
574 ctx
->Stencil
.FailFunc
[1] = GL_KEEP
;
575 ctx
->Stencil
.FailFunc
[2] = GL_KEEP
;
576 ctx
->Stencil
.ZPassFunc
[0] = GL_KEEP
;
577 ctx
->Stencil
.ZPassFunc
[1] = GL_KEEP
;
578 ctx
->Stencil
.ZPassFunc
[2] = GL_KEEP
;
579 ctx
->Stencil
.ZFailFunc
[0] = GL_KEEP
;
580 ctx
->Stencil
.ZFailFunc
[1] = GL_KEEP
;
581 ctx
->Stencil
.ZFailFunc
[2] = GL_KEEP
;
582 ctx
->Stencil
.Ref
[0] = 0;
583 ctx
->Stencil
.Ref
[1] = 0;
584 ctx
->Stencil
.Ref
[2] = 0;
585 ctx
->Stencil
.ValueMask
[0] = ~0U;
586 ctx
->Stencil
.ValueMask
[1] = ~0U;
587 ctx
->Stencil
.ValueMask
[2] = ~0U;
588 ctx
->Stencil
.WriteMask
[0] = ~0U;
589 ctx
->Stencil
.WriteMask
[1] = ~0U;
590 ctx
->Stencil
.WriteMask
[2] = ~0U;
591 ctx
->Stencil
.Clear
= 0;
592 ctx
->Stencil
._BackFace
= 1;