17464310b1aacd17a22ac8705146e4d117ab43e2
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 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 validate_stencil_op(GLcontext
*ctx
, GLenum op
)
67 case GL_INCR_WRAP_EXT
:
68 case GL_DECR_WRAP_EXT
:
69 if (ctx
->Extensions
.EXT_stencil_wrap
) {
80 validate_stencil_func(GLcontext
*ctx
, GLenum func
)
99 * Set the clear value for the stencil buffer.
101 * \param s clear value.
103 * \sa glClearStencil().
105 * Updates gl_stencil_attrib::Clear. On change
106 * flushes the vertices and notifies the driver via
107 * the dd_function_table::ClearStencil callback.
110 _mesa_ClearStencil( GLint s
)
112 GET_CURRENT_CONTEXT(ctx
);
113 ASSERT_OUTSIDE_BEGIN_END(ctx
);
115 if (ctx
->Stencil
.Clear
== (GLuint
) s
)
118 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
119 ctx
->Stencil
.Clear
= (GLuint
) s
;
121 if (ctx
->Driver
.ClearStencil
) {
122 ctx
->Driver
.ClearStencil( ctx
, s
);
128 * Set the function and reference value for stencil testing.
130 * \param frontfunc front test function.
131 * \param backfunc back test function.
132 * \param ref front and back reference value.
133 * \param mask front and back bitmask.
135 * \sa glStencilFunc().
137 * Verifies the parameters and updates the respective values in
138 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
139 * driver via the dd_function_table::StencilFunc callback.
142 _mesa_StencilFuncSeparateATI( GLenum frontfunc
, GLenum backfunc
, GLint ref
, GLuint mask
)
144 GET_CURRENT_CONTEXT(ctx
);
145 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
146 ASSERT_OUTSIDE_BEGIN_END(ctx
);
148 if (!validate_stencil_func(ctx
, frontfunc
)) {
149 _mesa_error(ctx
, GL_INVALID_ENUM
,
150 "glStencilFuncSeparateATI(frontfunc)");
153 if (!validate_stencil_func(ctx
, backfunc
)) {
154 _mesa_error(ctx
, GL_INVALID_ENUM
,
155 "glStencilFuncSeparateATI(backfunc)");
159 ref
= CLAMP( ref
, 0, stencilMax
);
161 /* set both front and back state */
162 if (ctx
->Stencil
.Function
[0] == frontfunc
&&
163 ctx
->Stencil
.Function
[1] == backfunc
&&
164 ctx
->Stencil
.ValueMask
[0] == mask
&&
165 ctx
->Stencil
.ValueMask
[1] == mask
&&
166 ctx
->Stencil
.Ref
[0] == ref
&&
167 ctx
->Stencil
.Ref
[1] == ref
)
169 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
170 ctx
->Stencil
.Function
[0] = frontfunc
;
171 ctx
->Stencil
.Function
[1] = backfunc
;
172 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
173 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
174 if (ctx
->Driver
.StencilFuncSeparate
) {
175 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_FRONT
,
176 frontfunc
, ref
, mask
);
177 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_BACK
,
178 backfunc
, ref
, mask
);
184 _mesa_StencilOpSeparateATI(GLenum face
, GLenum sfail
, GLenum zfail
, GLenum zpass
)
186 GLboolean set
= GL_FALSE
;
187 GET_CURRENT_CONTEXT(ctx
);
188 ASSERT_OUTSIDE_BEGIN_END(ctx
);
190 if (!validate_stencil_op(ctx
, sfail
)) {
191 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparateATI(sfail)");
194 if (!validate_stencil_op(ctx
, zfail
)) {
195 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparateATI(zfail)");
198 if (!validate_stencil_op(ctx
, zpass
)) {
199 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparateATI(zpass)");
202 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
203 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparateATI(face)");
207 if (face
!= GL_BACK
) {
209 if (ctx
->Stencil
.ZFailFunc
[0] != zfail
||
210 ctx
->Stencil
.ZPassFunc
[0] != zpass
||
211 ctx
->Stencil
.FailFunc
[0] != sfail
){
212 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
213 ctx
->Stencil
.ZFailFunc
[0] = zfail
;
214 ctx
->Stencil
.ZPassFunc
[0] = zpass
;
215 ctx
->Stencil
.FailFunc
[0] = sfail
;
219 if (face
!= GL_FRONT
) {
221 if (ctx
->Stencil
.ZFailFunc
[1] != zfail
||
222 ctx
->Stencil
.ZPassFunc
[1] != zpass
||
223 ctx
->Stencil
.FailFunc
[1] != sfail
) {
224 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
225 ctx
->Stencil
.ZFailFunc
[1] = zfail
;
226 ctx
->Stencil
.ZPassFunc
[1] = zpass
;
227 ctx
->Stencil
.FailFunc
[1] = sfail
;
231 if (set
&& ctx
->Driver
.StencilOpSeparate
) {
232 ctx
->Driver
.StencilOpSeparate(ctx
, face
, sfail
, zfail
, zpass
);
239 * Set the function and reference value for stencil testing.
241 * \param func test function.
242 * \param ref reference value.
243 * \param mask bitmask.
245 * \sa glStencilFunc().
247 * Verifies the parameters and updates the respective values in
248 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
249 * driver via the dd_function_table::StencilFunc callback.
252 _mesa_StencilFunc( GLenum func
, GLint ref
, GLuint mask
)
254 GET_CURRENT_CONTEXT(ctx
);
255 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
256 ASSERT_OUTSIDE_BEGIN_END(ctx
);
258 if (!validate_stencil_func(ctx
, func
)) {
259 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFunc(func)");
263 ref
= CLAMP( ref
, 0, stencilMax
);
265 if (ctx
->Extensions
.ATI_separate_stencil
) {
266 /* set both front and back state */
267 if (ctx
->Stencil
.Function
[0] == func
&&
268 ctx
->Stencil
.Function
[1] == func
&&
269 ctx
->Stencil
.ValueMask
[0] == mask
&&
270 ctx
->Stencil
.ValueMask
[1] == mask
&&
271 ctx
->Stencil
.Ref
[0] == ref
&&
272 ctx
->Stencil
.Ref
[1] == ref
)
274 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
275 ctx
->Stencil
.Function
[0] = ctx
->Stencil
.Function
[1] = func
;
276 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
277 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
278 if (ctx
->Driver
.StencilFuncSeparate
) {
279 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_FRONT_AND_BACK
,
284 /* only set active face state */
285 const GLint face
= ctx
->Stencil
.ActiveFace
;
286 if (ctx
->Stencil
.Function
[face
] == func
&&
287 ctx
->Stencil
.ValueMask
[face
] == mask
&&
288 ctx
->Stencil
.Ref
[face
] == ref
)
290 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
291 ctx
->Stencil
.Function
[face
] = func
;
292 ctx
->Stencil
.Ref
[face
] = ref
;
293 ctx
->Stencil
.ValueMask
[face
] = mask
;
294 if (ctx
->Driver
.StencilFuncSeparate
) {
295 ctx
->Driver
.StencilFuncSeparate(ctx
, face
? GL_BACK
: GL_FRONT
,
303 * Set the stencil writing mask.
305 * \param mask bit-mask to enable/disable writing of individual bits in the
308 * \sa glStencilMask().
310 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
311 * notifies the driver via the dd_function_table::StencilMask callback.
314 _mesa_StencilMask( GLuint mask
)
316 GET_CURRENT_CONTEXT(ctx
);
317 ASSERT_OUTSIDE_BEGIN_END(ctx
);
319 if (ctx
->Extensions
.ATI_separate_stencil
) {
320 /* set both front and back state */
321 if (ctx
->Stencil
.WriteMask
[0] == mask
&&
322 ctx
->Stencil
.WriteMask
[1] == mask
)
324 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
325 ctx
->Stencil
.WriteMask
[0] = ctx
->Stencil
.WriteMask
[1] = mask
;
326 if (ctx
->Driver
.StencilMaskSeparate
) {
327 ctx
->Driver
.StencilMaskSeparate(ctx
, GL_FRONT_AND_BACK
, mask
);
331 /* only set active face state */
332 const GLint face
= ctx
->Stencil
.ActiveFace
;
333 if (ctx
->Stencil
.WriteMask
[face
] == mask
)
335 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
336 ctx
->Stencil
.WriteMask
[face
] = mask
;
337 if (ctx
->Driver
.StencilMaskSeparate
) {
338 ctx
->Driver
.StencilMaskSeparate(ctx
, face
? GL_BACK
: GL_FRONT
, mask
);
345 * Set the stencil test actions.
347 * \param fail action to take when stencil test fails.
348 * \param zfail action to take when stencil test passes, but depth test fails.
349 * \param zpass action to take when stencil test passes and the depth test
350 * passes (or depth testing is not enabled).
354 * Verifies the parameters and updates the respective fields in
355 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
356 * driver via the dd_function_table::StencilOp callback.
359 _mesa_StencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
)
361 GET_CURRENT_CONTEXT(ctx
);
362 ASSERT_OUTSIDE_BEGIN_END(ctx
);
364 if (!validate_stencil_op(ctx
, fail
)) {
365 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(sfail)");
368 if (!validate_stencil_op(ctx
, zfail
)) {
369 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zfail)");
372 if (!validate_stencil_op(ctx
, zpass
)) {
373 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zpass)");
377 if (ctx
->Extensions
.ATI_separate_stencil
) {
378 /* set both front and back state */
379 if (ctx
->Stencil
.ZFailFunc
[0] == zfail
&&
380 ctx
->Stencil
.ZFailFunc
[1] == zfail
&&
381 ctx
->Stencil
.ZPassFunc
[0] == zpass
&&
382 ctx
->Stencil
.ZPassFunc
[1] == zpass
&&
383 ctx
->Stencil
.FailFunc
[0] == fail
&&
384 ctx
->Stencil
.FailFunc
[1] == fail
)
386 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
387 ctx
->Stencil
.ZFailFunc
[0] = ctx
->Stencil
.ZFailFunc
[1] = zfail
;
388 ctx
->Stencil
.ZPassFunc
[0] = ctx
->Stencil
.ZPassFunc
[1] = zpass
;
389 ctx
->Stencil
.FailFunc
[0] = ctx
->Stencil
.FailFunc
[1] = fail
;
390 if (ctx
->Driver
.StencilOpSeparate
) {
391 ctx
->Driver
.StencilOpSeparate(ctx
, GL_FRONT_AND_BACK
,
396 /* only set active face state */
397 const GLint face
= ctx
->Stencil
.ActiveFace
;
398 if (ctx
->Stencil
.ZFailFunc
[face
] == zfail
&&
399 ctx
->Stencil
.ZPassFunc
[face
] == zpass
&&
400 ctx
->Stencil
.FailFunc
[face
] == fail
)
402 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
403 ctx
->Stencil
.ZFailFunc
[face
] = zfail
;
404 ctx
->Stencil
.ZPassFunc
[face
] = zpass
;
405 ctx
->Stencil
.FailFunc
[face
] = fail
;
406 if (ctx
->Driver
.StencilOpSeparate
) {
407 ctx
->Driver
.StencilOpSeparate(ctx
, face
? GL_BACK
: GL_FRONT
,
416 /* GL_EXT_stencil_two_side */
418 _mesa_ActiveStencilFaceEXT(GLenum face
)
420 GET_CURRENT_CONTEXT(ctx
);
421 ASSERT_OUTSIDE_BEGIN_END(ctx
);
423 if (!ctx
->Extensions
.EXT_stencil_two_side
) {
424 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveStencilFaceEXT");
428 if (face
== GL_FRONT
|| face
== GL_BACK
) {
429 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
430 ctx
->Stencil
.ActiveFace
= (face
== GL_FRONT
) ? 0 : 1;
433 _mesa_error(ctx
, GL_INVALID_ENUM
, "glActiveStencilFaceEXT(face)");
441 * OpenGL 2.0 function.
442 * \todo Make StencilOp() call this function. And eventually remove the
443 * ctx->Driver.StencilOp function and use ctx->Driver.StencilOpSeparate
447 _mesa_StencilOpSeparate(GLenum face
, GLenum fail
, GLenum zfail
, GLenum zpass
)
449 GET_CURRENT_CONTEXT(ctx
);
450 ASSERT_OUTSIDE_BEGIN_END(ctx
);
452 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
453 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(face)");
457 if (!validate_stencil_op(ctx
, fail
)) {
458 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(sfail)");
461 if (!validate_stencil_op(ctx
, zfail
)) {
462 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zfail)");
465 if (!validate_stencil_op(ctx
, zpass
)) {
466 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zpass)");
470 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
472 if (face
!= GL_BACK
) {
473 ctx
->Stencil
.FailFunc
[0] = fail
;
474 ctx
->Stencil
.ZFailFunc
[0] = zfail
;
475 ctx
->Stencil
.ZPassFunc
[0] = zpass
;
477 if (face
!= GL_FRONT
) {
478 ctx
->Stencil
.FailFunc
[1] = fail
;
479 ctx
->Stencil
.ZFailFunc
[1] = zfail
;
480 ctx
->Stencil
.ZPassFunc
[1] = zpass
;
482 if (ctx
->Driver
.StencilOpSeparate
) {
483 ctx
->Driver
.StencilOpSeparate(ctx
, face
, fail
, zfail
, zpass
);
490 _mesa_StencilFuncSeparate(GLenum face
, GLenum func
, GLint ref
, GLuint mask
)
492 GET_CURRENT_CONTEXT(ctx
);
493 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
494 ASSERT_OUTSIDE_BEGIN_END(ctx
);
496 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
497 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(face)");
500 if (!validate_stencil_func(ctx
, func
)) {
501 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(func)");
505 ref
= CLAMP(ref
, 0, stencilMax
);
507 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
509 if (face
== GL_FRONT
|| face
== GL_FRONT_AND_BACK
) {
510 ctx
->Stencil
.Function
[0] = func
;
511 ctx
->Stencil
.Ref
[0] = ref
;
512 ctx
->Stencil
.ValueMask
[0] = mask
;
514 if (face
== GL_BACK
|| face
== GL_FRONT_AND_BACK
) {
515 ctx
->Stencil
.Function
[1] = func
;
516 ctx
->Stencil
.Ref
[1] = ref
;
517 ctx
->Stencil
.ValueMask
[1] = mask
;
519 if (ctx
->Driver
.StencilFuncSeparate
) {
520 ctx
->Driver
.StencilFuncSeparate(ctx
, face
, func
, ref
, mask
);
527 _mesa_StencilMaskSeparate(GLenum face
, GLuint mask
)
529 GET_CURRENT_CONTEXT(ctx
);
530 ASSERT_OUTSIDE_BEGIN_END(ctx
);
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(GLcontext
*ctx
)
557 if (ctx
->Extensions
.EXT_stencil_two_side
) {
558 ctx
->Stencil
._TestTwoSide
= ctx
->Stencil
.TestTwoSide
;
561 ctx
->Stencil
._TestTwoSide
=
562 (ctx
->Stencil
.Function
[0] != ctx
->Stencil
.Function
[1] ||
563 ctx
->Stencil
.FailFunc
[0] != ctx
->Stencil
.FailFunc
[1] ||
564 ctx
->Stencil
.ZPassFunc
[0] != ctx
->Stencil
.ZPassFunc
[1] ||
565 ctx
->Stencil
.ZFailFunc
[0] != ctx
->Stencil
.ZFailFunc
[1] ||
566 ctx
->Stencil
.Ref
[0] != ctx
->Stencil
.Ref
[1] ||
567 ctx
->Stencil
.ValueMask
[0] != ctx
->Stencil
.ValueMask
[1] ||
568 ctx
->Stencil
.WriteMask
[0] != ctx
->Stencil
.WriteMask
[1]);
574 * Initialize the context stipple state.
576 * \param ctx GL context.
578 * Initializes __GLcontextRec::Stencil attribute group.
581 _mesa_init_stencil(GLcontext
*ctx
)
583 ctx
->Stencil
.Enabled
= GL_FALSE
;
584 ctx
->Stencil
.TestTwoSide
= GL_FALSE
;
585 ctx
->Stencil
.ActiveFace
= 0; /* 0 = GL_FRONT, 1 = GL_BACK */
586 ctx
->Stencil
.Function
[0] = GL_ALWAYS
;
587 ctx
->Stencil
.Function
[1] = GL_ALWAYS
;
588 ctx
->Stencil
.FailFunc
[0] = GL_KEEP
;
589 ctx
->Stencil
.FailFunc
[1] = GL_KEEP
;
590 ctx
->Stencil
.ZPassFunc
[0] = GL_KEEP
;
591 ctx
->Stencil
.ZPassFunc
[1] = GL_KEEP
;
592 ctx
->Stencil
.ZFailFunc
[0] = GL_KEEP
;
593 ctx
->Stencil
.ZFailFunc
[1] = GL_KEEP
;
594 ctx
->Stencil
.Ref
[0] = 0;
595 ctx
->Stencil
.Ref
[1] = 0;
596 ctx
->Stencil
.ValueMask
[0] = ~0U;
597 ctx
->Stencil
.ValueMask
[1] = ~0U;
598 ctx
->Stencil
.WriteMask
[0] = ~0U;
599 ctx
->Stencil
.WriteMask
[1] = ~0U;
600 ctx
->Stencil
.Clear
= 0;