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.
35 validate_stencil_op(struct gl_context
*ctx
, GLenum op
)
54 validate_stencil_func(struct gl_context
*ctx
, GLenum func
)
73 * Set the clear value for the stencil buffer.
75 * \param s clear value.
77 * \sa glClearStencil().
79 * Updates gl_stencil_attrib::Clear. On change
80 * flushes the vertices and notifies the driver via
81 * the dd_function_table::ClearStencil callback.
84 _mesa_ClearStencil( GLint s
)
86 GET_CURRENT_CONTEXT(ctx
);
87 ASSERT_OUTSIDE_BEGIN_END(ctx
);
89 if (ctx
->Stencil
.Clear
== (GLuint
) s
)
92 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
93 ctx
->Stencil
.Clear
= (GLuint
) s
;
95 if (ctx
->Driver
.ClearStencil
) {
96 ctx
->Driver
.ClearStencil( ctx
, s
);
101 * Set the function and reference value for stencil testing.
103 * \param func test function.
104 * \param ref reference value.
105 * \param mask bitmask.
107 * \sa glStencilFunc().
109 * Verifies the parameters and updates the respective values in
110 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies the
111 * driver via the dd_function_table::StencilFunc callback.
114 _mesa_StencilFunc( GLenum func
, GLint ref
, GLuint mask
)
116 GET_CURRENT_CONTEXT(ctx
);
117 const GLint stencilMax
= (1 << ctx
->DrawBuffer
->Visual
.stencilBits
) - 1;
118 ASSERT_OUTSIDE_BEGIN_END(ctx
);
120 if (MESA_VERBOSE
& VERBOSE_API
)
121 _mesa_debug(ctx
, "glStencilFunc()\n");
123 if (!validate_stencil_func(ctx
, func
)) {
124 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilFunc(func)");
128 ref
= CLAMP( ref
, 0, stencilMax
);
130 /* set both front and back state */
131 if (ctx
->Stencil
.Function
== func
&&
132 ctx
->Stencil
.ValueMask
== mask
&&
133 ctx
->Stencil
.Ref
== ref
)
135 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
136 ctx
->Stencil
.Function
= func
;
137 ctx
->Stencil
.Ref
= ref
;
138 ctx
->Stencil
.ValueMask
= mask
;
143 * Set the stencil writing mask.
145 * \param mask bit-mask to enable/disable writing of individual bits in the
148 * \sa glStencilMask().
150 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
151 * notifies the driver via the dd_function_table::StencilMask callback.
154 _mesa_StencilMask( GLuint mask
)
156 GET_CURRENT_CONTEXT(ctx
);
158 if (MESA_VERBOSE
& VERBOSE_API
)
159 _mesa_debug(ctx
, "glStencilMask()\n");
161 ASSERT_OUTSIDE_BEGIN_END(ctx
);
163 /* set both front and back state */
164 if (ctx
->Stencil
.WriteMask
== mask
)
166 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
167 ctx
->Stencil
.WriteMask
= mask
;
172 * Set the stencil test actions.
174 * \param fail action to take when stencil test fails.
175 * \param zfail action to take when stencil test passes, but depth test fails.
176 * \param zpass action to take when stencil test passes and the depth test
177 * passes (or depth testing is not enabled).
181 * Verifies the parameters and updates the respective fields in
182 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies the
183 * driver via the dd_function_table::StencilOp callback.
186 _mesa_StencilOp(GLenum fail
, GLenum zfail
, GLenum zpass
)
188 GET_CURRENT_CONTEXT(ctx
);
190 if (MESA_VERBOSE
& VERBOSE_API
)
191 _mesa_debug(ctx
, "glStencilOp()\n");
193 ASSERT_OUTSIDE_BEGIN_END(ctx
);
195 if (!validate_stencil_op(ctx
, fail
)) {
196 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(sfail)");
199 if (!validate_stencil_op(ctx
, zfail
)) {
200 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zfail)");
203 if (!validate_stencil_op(ctx
, zpass
)) {
204 _mesa_error(ctx
, GL_INVALID_ENUM
, "glStencilOp(zpass)");
208 /* set both front and back state */
209 if (ctx
->Stencil
.ZFailFunc
== zfail
&&
210 ctx
->Stencil
.ZPassFunc
== zpass
&&
211 ctx
->Stencil
.FailFunc
== fail
)
213 FLUSH_VERTICES(ctx
, _NEW_STENCIL
);
214 ctx
->Stencil
.ZFailFunc
= zfail
;
215 ctx
->Stencil
.ZPassFunc
= zpass
;
216 ctx
->Stencil
.FailFunc
= fail
;
220 * Update derived stencil state.
223 _mesa_update_stencil(struct gl_context
*ctx
)
225 ctx
->Stencil
._Enabled
= (ctx
->Stencil
.Enabled
&&
226 ctx
->DrawBuffer
->Visual
.stencilBits
> 0);
231 * Initialize the context stipple state.
233 * \param ctx GL context.
235 * Initializes __struct gl_contextRec::Stencil attribute group.
238 _mesa_init_stencil(struct gl_context
*ctx
)
240 ctx
->Stencil
.Enabled
= GL_FALSE
;
241 ctx
->Stencil
.Function
= GL_ALWAYS
;
242 ctx
->Stencil
.FailFunc
= GL_KEEP
;
243 ctx
->Stencil
.ZPassFunc
= GL_KEEP
;
244 ctx
->Stencil
.ZFailFunc
= GL_KEEP
;
245 ctx
->Stencil
.Ref
= 0;
246 ctx
->Stencil
.ValueMask
= ~0U;
247 ctx
->Stencil
.WriteMask
= ~0U;
248 ctx
->Stencil
.Clear
= 0;