7 * Mesa 3-D graphics library
10 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
42 * Set the clear value for the stencil buffer.
44 * \param s clear value.
46 * \sa glClearStencil().
48 * Updates gl_stencil_attrib::Clear. On change
49 * flushes the vertices and notifies the driver via
50 * the dd_function_table::ClearStencil callback.
53 _mesa_ClearStencil( GLint s
)
55 GET_CURRENT_CONTEXT(ctx
);
56 ASSERT_OUTSIDE_BEGIN_END(ctx
);
58 if (ctx
->Stencil
.Clear
== (GLstencil
) s
)
61 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
62 ctx
->Stencil
.Clear
= (GLstencil
) s
;
64 if (ctx
->Driver
.ClearStencil
) {
65 (*ctx
->Driver
.ClearStencil
)( ctx
, s
);
71 * Set the function and reference value for stencil testing.
73 * \param func test function.
74 * \param ref reference value.
75 * \param mask bitmask.
77 * \sa glStencilFunc().
79 * Verifies the parameters and updates the respective values in
80 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
81 * driver via the dd_function_table::StencilFunc callback.
84 _mesa_StencilFunc( GLenum func
, GLint ref
, GLuint mask
)
86 GET_CURRENT_CONTEXT(ctx
);
87 const GLint face
= ctx
->Stencil
.ActiveFace
;
89 ASSERT_OUTSIDE_BEGIN_END(ctx
);
102 _mesa_error( ctx
, GL_INVALID_ENUM
, "glStencilFunc" );
106 maxref
= (1 << STENCIL_BITS
) - 1;
107 ref
= (GLstencil
) CLAMP( ref
, 0, maxref
);
109 if (ctx
->Stencil
.Function
[face
] == func
&&
110 ctx
->Stencil
.ValueMask
[face
] == (GLstencil
) mask
&&
111 ctx
->Stencil
.Ref
[face
] == ref
)
114 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
115 ctx
->Stencil
.Function
[face
] = func
;
116 ctx
->Stencil
.Ref
[face
] = ref
;
117 ctx
->Stencil
.ValueMask
[face
] = (GLstencil
) mask
;
119 if (ctx
->Driver
.StencilFunc
) {
120 (*ctx
->Driver
.StencilFunc
)( ctx
, func
, ref
, mask
);
126 * Set the stencil writing mask.
128 * \param mask bit-mask to enable/disable writing of individual bits in the
131 * \sa glStencilMask().
133 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
134 * notifies the driver via the dd_function_table::StencilMask callback.
137 _mesa_StencilMask( GLuint mask
)
139 GET_CURRENT_CONTEXT(ctx
);
140 const GLint face
= ctx
->Stencil
.ActiveFace
;
141 ASSERT_OUTSIDE_BEGIN_END(ctx
);
143 if (ctx
->Stencil
.WriteMask
[face
] == (GLstencil
) mask
)
146 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
147 ctx
->Stencil
.WriteMask
[face
] = (GLstencil
) mask
;
149 if (ctx
->Driver
.StencilMask
) {
150 (*ctx
->Driver
.StencilMask
)( ctx
, mask
);
156 * Set the stencil test actions.
158 * \param fail action to take when stencil test fails.
159 * \param zfail action to take when stencil test passes, but the depth test fails.
160 * \param zpass action to take when stencil test passes and the depth test
161 * passes (or depth testing is not enabled).
165 * Verifies the parameters and updates the respective fields in
166 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
167 * driver via the dd_function_table::StencilOp callback.
170 _mesa_StencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
)
172 GET_CURRENT_CONTEXT(ctx
);
173 const GLint face
= ctx
->Stencil
.ActiveFace
;
174 ASSERT_OUTSIDE_BEGIN_END(ctx
);
184 case GL_INCR_WRAP_EXT
:
185 case GL_DECR_WRAP_EXT
:
186 if (ctx
->Extensions
.EXT_stencil_wrap
) {
191 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp");
202 case GL_INCR_WRAP_EXT
:
203 case GL_DECR_WRAP_EXT
:
204 if (ctx
->Extensions
.EXT_stencil_wrap
) {
209 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp");
220 case GL_INCR_WRAP_EXT
:
221 case GL_DECR_WRAP_EXT
:
222 if (ctx
->Extensions
.EXT_stencil_wrap
) {
227 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp");
231 if (ctx
->Stencil
.ZFailFunc
[face
] == zfail
&&
232 ctx
->Stencil
.ZPassFunc
[face
] == zpass
&&
233 ctx
->Stencil
.FailFunc
[face
] == fail
)
236 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
237 ctx
->Stencil
.ZFailFunc
[face
] = zfail
;
238 ctx
->Stencil
.ZPassFunc
[face
] = zpass
;
239 ctx
->Stencil
.FailFunc
[face
] = fail
;
241 if (ctx
->Driver
.StencilOp
) {
242 (*ctx
->Driver
.StencilOp
)(ctx
, fail
, zfail
, zpass
);
249 /* GL_EXT_stencil_two_side */
251 _mesa_ActiveStencilFaceEXT(GLenum face
)
253 GET_CURRENT_CONTEXT(ctx
);
254 ASSERT_OUTSIDE_BEGIN_END(ctx
);
256 if (face
== GL_FRONT
|| face
== GL_BACK
) {
257 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
258 ctx
->Stencil
.ActiveFace
= (face
== GL_FRONT
) ? 0 : 1;
261 if (ctx
->Driver
.ActiveStencilFace
) {
262 (*ctx
->Driver
.ActiveStencilFace
)( ctx
, (GLuint
) ctx
->Stencil
.ActiveFace
);
270 * OpenGL 2.0 function.
271 * \todo Make StencilOp() call this function. And eventually remove the
272 * ctx->Driver.StencilOp function and use ctx->Driver.StencilOpSeparate
276 _mesa_StencilOpSeparate(GLenum face
, GLenum fail
, GLenum zfail
, GLenum zpass
)
278 GET_CURRENT_CONTEXT(ctx
);
279 ASSERT_OUTSIDE_BEGIN_END(ctx
);
281 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
282 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(face)");
294 case GL_INCR_WRAP_EXT
:
295 case GL_DECR_WRAP_EXT
:
296 if (ctx
->Extensions
.EXT_stencil_wrap
) {
301 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(fail)");
312 case GL_INCR_WRAP_EXT
:
313 case GL_DECR_WRAP_EXT
:
314 if (ctx
->Extensions
.EXT_stencil_wrap
) {
319 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zfail)");
330 case GL_INCR_WRAP_EXT
:
331 case GL_DECR_WRAP_EXT
:
332 if (ctx
->Extensions
.EXT_stencil_wrap
) {
337 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOpSeparate(zpass)");
341 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
343 if (face
== GL_FRONT
|| face
== GL_FRONT_AND_BACK
) {
344 ctx
->Stencil
.FailFunc
[0] = fail
;
345 ctx
->Stencil
.ZFailFunc
[0] = zfail
;
346 ctx
->Stencil
.ZPassFunc
[0] = zpass
;
348 if (face
== GL_BACK
|| face
== GL_FRONT_AND_BACK
) {
349 ctx
->Stencil
.FailFunc
[1] = fail
;
350 ctx
->Stencil
.ZFailFunc
[1] = zfail
;
351 ctx
->Stencil
.ZPassFunc
[1] = zpass
;
354 if (ctx
->Driver
.StencilOpSeparate
) {
355 ctx
->Driver
.StencilOpSeparate(ctx
, face
, fail
, zfail
, zpass
);
362 _mesa_StencilFuncSeparate(GLenum face
, GLenum func
, GLint ref
, GLuint mask
)
364 GET_CURRENT_CONTEXT(ctx
);
366 ASSERT_OUTSIDE_BEGIN_END(ctx
);
368 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
369 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(face)");
384 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFuncSeparate(func)");
388 maxref
= (1 << STENCIL_BITS
) - 1;
389 ref
= (GLstencil
) CLAMP(ref
, 0, maxref
);
391 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
393 if (face
== GL_FRONT
|| face
== GL_FRONT_AND_BACK
) {
394 ctx
->Stencil
.Function
[0] = func
;
395 ctx
->Stencil
.Ref
[0] = ref
;
396 ctx
->Stencil
.ValueMask
[0] = (GLstencil
) mask
;
398 if (face
== GL_BACK
|| face
== GL_FRONT_AND_BACK
) {
399 ctx
->Stencil
.Function
[1] = func
;
400 ctx
->Stencil
.Ref
[1] = ref
;
401 ctx
->Stencil
.ValueMask
[1] = (GLstencil
) mask
;
404 if (ctx
->Driver
.StencilFuncSeparate
) {
405 ctx
->Driver
.StencilFuncSeparate(ctx
, face
, func
, ref
, mask
);
412 _mesa_StencilMaskSeparate(GLenum face
, GLuint mask
)
414 GET_CURRENT_CONTEXT(ctx
);
415 ASSERT_OUTSIDE_BEGIN_END(ctx
);
417 if (face
!= GL_FRONT
&& face
!= GL_BACK
&& face
!= GL_FRONT_AND_BACK
) {
418 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilaMaskSeparate(face)");
422 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
424 if (face
== GL_FRONT
|| face
== GL_FRONT_AND_BACK
) {
425 ctx
->Stencil
.WriteMask
[0] = (GLstencil
) mask
;
427 if (face
== GL_BACK
|| face
== GL_FRONT_AND_BACK
) {
428 ctx
->Stencil
.WriteMask
[1] = (GLstencil
) mask
;
431 if (ctx
->Driver
.StencilMaskSeparate
) {
432 ctx
->Driver
.StencilMaskSeparate(ctx
, face
, mask
);
438 * Initialize the context stipple state.
440 * \param ctx GL context.
442 * Initializes __GLcontextRec::Stencil attribute group.
444 void _mesa_init_stencil( GLcontext
* ctx
)
448 ctx
->Stencil
.Enabled
= GL_FALSE
;
449 ctx
->Stencil
.TestTwoSide
= GL_FALSE
;
450 ctx
->Stencil
.ActiveFace
= 0; /* 0 = GL_FRONT, 1 = GL_BACK */
451 ctx
->Stencil
.Function
[0] = GL_ALWAYS
;
452 ctx
->Stencil
.Function
[1] = GL_ALWAYS
;
453 ctx
->Stencil
.FailFunc
[0] = GL_KEEP
;
454 ctx
->Stencil
.FailFunc
[1] = GL_KEEP
;
455 ctx
->Stencil
.ZPassFunc
[0] = GL_KEEP
;
456 ctx
->Stencil
.ZPassFunc
[1] = GL_KEEP
;
457 ctx
->Stencil
.ZFailFunc
[0] = GL_KEEP
;
458 ctx
->Stencil
.ZFailFunc
[1] = GL_KEEP
;
459 ctx
->Stencil
.Ref
[0] = 0;
460 ctx
->Stencil
.Ref
[1] = 0;
461 ctx
->Stencil
.ValueMask
[0] = STENCIL_MAX
;
462 ctx
->Stencil
.ValueMask
[1] = STENCIL_MAX
;
463 ctx
->Stencil
.WriteMask
[0] = STENCIL_MAX
;
464 ctx
->Stencil
.WriteMask
[1] = STENCIL_MAX
;
465 ctx
->Stencil
.Clear
= 0;