612ad38dcc1472ea2862b008fffed0e919ad56df
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 if (MESA_VERBOSE
& VERBOSE_API
)
113 _mesa_debug(ctx
, "glClearStencil(%d)\n", s
);
115 ctx
->Stencil
.Clear
= (GLuint
) s
;
120 * Set the function and reference value for stencil testing.
122 * \param frontfunc front test function.
123 * \param backfunc back test function.
124 * \param ref front and back reference value.
125 * \param mask front and back bitmask.
127 * \sa glStencilFunc().
129 * Verifies the parameters and updates the respective values in
130 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies
131 * the driver via the dd_function_table::StencilFunc callback.
134 _mesa_StencilFuncSeparateATI( GLenum frontfunc
, GLenum backfunc
, GLint ref
, GLuint mask
)
136 GET_CURRENT_CONTEXT(ctx
);
138 if (MESA_VERBOSE
& VERBOSE_API
)
139 _mesa_debug(ctx
, "glStencilFuncSeparateATI()\n");
141 if (!validate_stencil_func(ctx
, frontfunc
)) {
142 _mesa_error(ctx
, GL_INVALID_ENUM
,
143 "glStencilFuncSeparateATI(frontfunc)");
146 if (!validate_stencil_func(ctx
, backfunc
)) {
147 _mesa_error(ctx
, GL_INVALID_ENUM
,
148 "glStencilFuncSeparateATI(backfunc)");
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
185 * the driver via the dd_function_table::StencilFunc callback.
188 stencil_func(struct gl_context
*ctx
, GLenum func
, GLint ref
, GLuint mask
)
190 const GLint face
= ctx
->Stencil
.ActiveFace
;
193 if (ctx
->Stencil
.Function
[face
] == func
&&
194 ctx
->Stencil
.ValueMask
[face
] == mask
&&
195 ctx
->Stencil
.Ref
[face
] == ref
)
197 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
198 ctx
->Stencil
.Function
[face
] = func
;
199 ctx
->Stencil
.Ref
[face
] = ref
;
200 ctx
->Stencil
.ValueMask
[face
] = mask
;
202 /* Only propagate the change to the driver if EXT_stencil_two_side
205 if (ctx
->Driver
.StencilFuncSeparate
&& ctx
->Stencil
.TestTwoSide
) {
206 ctx
->Driver
.StencilFuncSeparate(ctx
, GL_BACK
, func
, ref
, mask
);
210 /* set both front and back state */
211 if (ctx
->Stencil
.Function
[0] == func
&&
212 ctx
->Stencil
.Function
[1] == func
&&
213 ctx
->Stencil
.ValueMask
[0] == mask
&&
214 ctx
->Stencil
.ValueMask
[1] == mask
&&
215 ctx
->Stencil
.Ref
[0] == ref
&&
216 ctx
->Stencil
.Ref
[1] == ref
)
218 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
219 ctx
->Stencil
.Function
[0] = ctx
->Stencil
.Function
[1] = func
;
220 ctx
->Stencil
.Ref
[0] = ctx
->Stencil
.Ref
[1] = ref
;
221 ctx
->Stencil
.ValueMask
[0] = ctx
->Stencil
.ValueMask
[1] = mask
;
222 if (ctx
->Driver
.StencilFuncSeparate
) {
223 ctx
->Driver
.StencilFuncSeparate(ctx
,
224 ((ctx
->Stencil
.TestTwoSide
)
225 ? GL_FRONT
: GL_FRONT_AND_BACK
),
233 _mesa_StencilFunc_no_error(GLenum func
, GLint ref
, GLuint mask
)
235 GET_CURRENT_CONTEXT(ctx
);
236 stencil_func(ctx
, func
, ref
, mask
);
241 _mesa_StencilFunc(GLenum func
, GLint ref
, GLuint mask
)
243 GET_CURRENT_CONTEXT(ctx
);
245 if (MESA_VERBOSE
& VERBOSE_API
)
246 _mesa_debug(ctx
, "glStencilFunc()\n");
248 if (!validate_stencil_func(ctx
, func
)) {
249 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFunc(func)");
253 stencil_func(ctx
, func
, ref
, mask
);
258 * Set the stencil writing mask.
260 * \param mask bit-mask to enable/disable writing of individual bits in the
263 * \sa glStencilMask().
265 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
266 * notifies the driver via the dd_function_table::StencilMask callback.
269 _mesa_StencilMask( GLuint mask
)
271 GET_CURRENT_CONTEXT(ctx
);
272 const GLint face
= ctx
->Stencil
.ActiveFace
;
274 if (MESA_VERBOSE
& VERBOSE_API
)
275 _mesa_debug(ctx
, "glStencilMask()\n");
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
321 * the driver via the dd_function_table::StencilOp callback.
324 stencil_op(struct gl_context
*ctx
, GLenum fail
, GLenum zfail
, GLenum zpass
)
326 const GLint face
= ctx
->Stencil
.ActiveFace
;
329 /* only set active face state */
330 if (ctx
->Stencil
.ZFailFunc
[face
] == zfail
&&
331 ctx
->Stencil
.ZPassFunc
[face
] == zpass
&&
332 ctx
->Stencil
.FailFunc
[face
] == fail
)
334 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
335 ctx
->Stencil
.ZFailFunc
[face
] = zfail
;
336 ctx
->Stencil
.ZPassFunc
[face
] = zpass
;
337 ctx
->Stencil
.FailFunc
[face
] = fail
;
339 /* Only propagate the change to the driver if EXT_stencil_two_side
342 if (ctx
->Driver
.StencilOpSeparate
&& ctx
->Stencil
.TestTwoSide
) {
343 ctx
->Driver
.StencilOpSeparate(ctx
, GL_BACK
, fail
, zfail
, zpass
);
347 /* set both front and back state */
348 if (ctx
->Stencil
.ZFailFunc
[0] == zfail
&&
349 ctx
->Stencil
.ZFailFunc
[1] == zfail
&&
350 ctx
->Stencil
.ZPassFunc
[0] == zpass
&&
351 ctx
->Stencil
.ZPassFunc
[1] == zpass
&&
352 ctx
->Stencil
.FailFunc
[0] == fail
&&
353 ctx
->Stencil
.FailFunc
[1] == fail
)
355 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
356 ctx
->Stencil
.ZFailFunc
[0] = ctx
->Stencil
.ZFailFunc
[1] = zfail
;
357 ctx
->Stencil
.ZPassFunc
[0] = ctx
->Stencil
.ZPassFunc
[1] = zpass
;
358 ctx
->Stencil
.FailFunc
[0] = ctx
->Stencil
.FailFunc
[1] = fail
;
359 if (ctx
->Driver
.StencilOpSeparate
) {
360 ctx
->Driver
.StencilOpSeparate(ctx
,
361 ((ctx
->Stencil
.TestTwoSide
)
362 ? GL_FRONT
: GL_FRONT_AND_BACK
),
370 _mesa_StencilOp_no_error(GLenum fail
, GLenum zfail
, GLenum zpass
)
372 GET_CURRENT_CONTEXT(ctx
);
373 stencil_op(ctx
, fail
, zfail
, zpass
);
378 _mesa_StencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
)
380 GET_CURRENT_CONTEXT(ctx
);
382 if (MESA_VERBOSE
& VERBOSE_API
)
383 _mesa_debug(ctx
, "glStencilOp()\n");
385 if (!validate_stencil_op(ctx
, fail
)) {
386 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(sfail)");
390 if (!validate_stencil_op(ctx
, zfail
)) {
391 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zfail)");
395 if (!validate_stencil_op(ctx
, zpass
)) {
396 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zpass)");
400 stencil_op(ctx
, fail
, zfail
, zpass
);
404 /* GL_EXT_stencil_two_side */
406 _mesa_ActiveStencilFaceEXT(GLenum face
)
408 GET_CURRENT_CONTEXT(ctx
);
410 if (MESA_VERBOSE
& VERBOSE_API
)
411 _mesa_debug(ctx
, "glActiveStencilFaceEXT()\n");
413 if (!ctx
->Extensions
.EXT_stencil_two_side
) {
414 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glActiveStencilFaceEXT");
418 if (face
== GL_FRONT
|| face
== GL_BACK
) {
419 ctx
->Stencil
.ActiveFace
= (face
== GL_FRONT
) ? 0 : 2;
422 _mesa_error(ctx
, GL_INVALID_ENUM
, "glActiveStencilFaceEXT(face)");
428 stencil_op_separate(struct gl_context
*ctx
, GLenum face
, GLenum sfail
,
429 GLenum zfail
, GLenum zpass
)
431 GLboolean set
= GL_FALSE
;
433 if (face
!= GL_BACK
) {
435 if (ctx
->Stencil
.ZFailFunc
[0] != zfail
||
436 ctx
->Stencil
.ZPassFunc
[0] != zpass
||
437 ctx
->Stencil
.FailFunc
[0] != sfail
){
438 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
439 ctx
->Stencil
.ZFailFunc
[0] = zfail
;
440 ctx
->Stencil
.ZPassFunc
[0] = zpass
;
441 ctx
->Stencil
.FailFunc
[0] = sfail
;
446 if (face
!= GL_FRONT
) {
448 if (ctx
->Stencil
.ZFailFunc
[1] != zfail
||
449 ctx
->Stencil
.ZPassFunc
[1] != zpass
||
450 ctx
->Stencil
.FailFunc
[1] != sfail
) {
451 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
452 ctx
->Stencil
.ZFailFunc
[1] = zfail
;
453 ctx
->Stencil
.ZPassFunc
[1] = zpass
;
454 ctx
->Stencil
.FailFunc
[1] = sfail
;
459 if (set
&& ctx
->Driver
.StencilOpSeparate
) {
460 ctx
->Driver
.StencilOpSeparate(ctx
, face
, sfail
, zfail
, zpass
);
466 _mesa_StencilOpSeparate_no_error(GLenum face
, GLenum sfail
, GLenum zfail
,
469 GET_CURRENT_CONTEXT(ctx
);
470 stencil_op_separate(ctx
, face
, sfail
, zfail
, zpass
);
475 _mesa_StencilOpSeparate(GLenum face
, GLenum sfail
, GLenum zfail
, GLenum zpass
)
477 GET_CURRENT_CONTEXT(ctx
);
479 if (MESA_VERBOSE
& VERBOSE_API
)
480 _mesa_debug(ctx
, "glStencilOpSeparate()\n");
482 if (!validate_stencil_op(ctx
, sfail
)) {
483 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(sfail)");
487 if (!validate_stencil_op(ctx
, zfail
)) {
488 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zfail)");
492 if (!validate_stencil_op(ctx
, zpass
)) {
493 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zpass)");
497 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
498 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(face)");
502 stencil_op_separate(ctx
, face
, sfail
, zfail
, zpass
);
507 stencil_func_separate(struct gl_context
*ctx
, GLenum face
, GLenum func
,
508 GLint ref
, GLuint mask
)
510 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
512 if (face
!= GL_BACK
) {
514 ctx
->Stencil
.Function
[0] = func
;
515 ctx
->Stencil
.Ref
[0] = ref
;
516 ctx
->Stencil
.ValueMask
[0] = mask
;
519 if (face
!= GL_FRONT
) {
521 ctx
->Stencil
.Function
[1] = func
;
522 ctx
->Stencil
.Ref
[1] = ref
;
523 ctx
->Stencil
.ValueMask
[1] = mask
;
526 if (ctx
->Driver
.StencilFuncSeparate
) {
527 ctx
->Driver
.StencilFuncSeparate(ctx
, face
, func
, ref
, mask
);
534 _mesa_StencilFuncSeparate_no_error(GLenum face
, GLenum func
, GLint ref
,
537 GET_CURRENT_CONTEXT(ctx
);
538 stencil_func_separate(ctx
, face
, func
, ref
, mask
);
543 _mesa_StencilFuncSeparate(GLenum face
, GLenum func
, GLint ref
, GLuint mask
)
545 GET_CURRENT_CONTEXT(ctx
);
547 if (MESA_VERBOSE
& VERBOSE_API
)
548 _mesa_debug(ctx
, "glStencilFuncSeparate()\n");
550 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
551 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(face)");
555 if (!validate_stencil_func(ctx
, func
)) {
556 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(func)");
560 stencil_func_separate(ctx
, face
, func
, ref
, mask
);
565 stencil_mask_separate(struct gl_context
*ctx
, GLenum face
, GLuint mask
)
567 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
569 if (face
!= GL_BACK
) {
570 ctx
->Stencil
.WriteMask
[0] = mask
;
573 if (face
!= GL_FRONT
) {
574 ctx
->Stencil
.WriteMask
[1] = mask
;
577 if (ctx
->Driver
.StencilMaskSeparate
) {
578 ctx
->Driver
.StencilMaskSeparate(ctx
, face
, mask
);
585 _mesa_StencilMaskSeparate_no_error(GLenum face
, GLuint mask
)
587 GET_CURRENT_CONTEXT(ctx
);
588 stencil_mask_separate(ctx
, face
, mask
);
593 _mesa_StencilMaskSeparate(GLenum face
, GLuint mask
)
595 GET_CURRENT_CONTEXT(ctx
);
597 if (MESA_VERBOSE
& VERBOSE_API
)
598 _mesa_debug(ctx
, "glStencilMaskSeparate()\n");
600 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
601 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilaMaskSeparate(face)");
605 stencil_mask_separate(ctx
, face
, mask
);
610 * Initialize the context stipple state.
612 * \param ctx GL context.
614 * Initializes __struct gl_contextRec::Stencil attribute group.
617 _mesa_init_stencil(struct gl_context
*ctx
)
619 ctx
->Stencil
.Enabled
= GL_FALSE
;
620 ctx
->Stencil
.TestTwoSide
= GL_FALSE
;
621 ctx
->Stencil
.ActiveFace
= 0; /* 0 = GL_FRONT, 2 = GL_BACK */
622 ctx
->Stencil
.Function
[0] = GL_ALWAYS
;
623 ctx
->Stencil
.Function
[1] = GL_ALWAYS
;
624 ctx
->Stencil
.Function
[2] = GL_ALWAYS
;
625 ctx
->Stencil
.FailFunc
[0] = GL_KEEP
;
626 ctx
->Stencil
.FailFunc
[1] = GL_KEEP
;
627 ctx
->Stencil
.FailFunc
[2] = GL_KEEP
;
628 ctx
->Stencil
.ZPassFunc
[0] = GL_KEEP
;
629 ctx
->Stencil
.ZPassFunc
[1] = GL_KEEP
;
630 ctx
->Stencil
.ZPassFunc
[2] = GL_KEEP
;
631 ctx
->Stencil
.ZFailFunc
[0] = GL_KEEP
;
632 ctx
->Stencil
.ZFailFunc
[1] = GL_KEEP
;
633 ctx
->Stencil
.ZFailFunc
[2] = GL_KEEP
;
634 ctx
->Stencil
.Ref
[0] = 0;
635 ctx
->Stencil
.Ref
[1] = 0;
636 ctx
->Stencil
.Ref
[2] = 0;
638 /* 4.1.4 Stencil Test section of the GL-ES 3.0 specification says:
640 * "In the initial state, [...] the front and back stencil mask are both
641 * set to the value 2^s − 1, where s is greater than or equal to the
642 * number of bits in the deepest stencil buffer* supported by the GL
645 * Since the maximum supported precision for stencil buffers is 8 bits,
646 * mask values should be initialized to 2^8 - 1 = 0xFF.
648 ctx
->Stencil
.ValueMask
[0] = 0xFF;
649 ctx
->Stencil
.ValueMask
[1] = 0xFF;
650 ctx
->Stencil
.ValueMask
[2] = 0xFF;
651 ctx
->Stencil
.WriteMask
[0] = 0xFF;
652 ctx
->Stencil
.WriteMask
[1] = 0xFF;
653 ctx
->Stencil
.WriteMask
[2] = 0xFF;
655 ctx
->Stencil
.Clear
= 0;
656 ctx
->Stencil
._BackFace
= 1;