replace STENCIL_BITS with stencilMax value
[mesa.git] / src / mesa / main / stencil.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5
4 *
5 * Copyright (C) 1999-2005 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
26 /**
27 * \file stencil.c
28 * Stencil operations.
29 *
30 * Note: There's an incompatibility between GL_EXT_stencil_two_side and
31 * OpenGL 2.0's two-sided stencil feature.
32 *
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.
35 *
36 * But with OpenGL 2.0, calling glStencilOp/Func/Mask() sets BOTH the
37 * front AND back state.
38 *
39 * So either we advertise the GL_EXT_stencil_two_side extension, or OpenGL
40 * 2.0, but not both.
41 */
42
43
44 #include "glheader.h"
45 #include "imports.h"
46 #include "context.h"
47 #include "macros.h"
48 #include "stencil.h"
49 #include "mtypes.h"
50
51
52 /**
53 * Set the clear value for the stencil buffer.
54 *
55 * \param s clear value.
56 *
57 * \sa glClearStencil().
58 *
59 * Updates gl_stencil_attrib::Clear. On change
60 * flushes the vertices and notifies the driver via
61 * the dd_function_table::ClearStencil callback.
62 */
63 void GLAPIENTRY
64 _mesa_ClearStencil( GLint s )
65 {
66 GET_CURRENT_CONTEXT(ctx);
67 ASSERT_OUTSIDE_BEGIN_END(ctx);
68
69 if (ctx->Stencil.Clear == (GLuint) s)
70 return;
71
72 FLUSH_VERTICES(ctx, _NEW_STENCIL);
73 ctx->Stencil.Clear = (GLuint) s;
74
75 if (ctx->Driver.ClearStencil) {
76 ctx->Driver.ClearStencil( ctx, s );
77 }
78 }
79
80
81 /**
82 * Set the function and reference value for stencil testing.
83 *
84 * \param func test function.
85 * \param ref reference value.
86 * \param mask bitmask.
87 *
88 * \sa glStencilFunc().
89 *
90 * Verifies the parameters and updates the respective values in
91 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
92 * driver via the dd_function_table::StencilFunc callback.
93 */
94 void GLAPIENTRY
95 _mesa_StencilFunc( GLenum func, GLint ref, GLuint mask )
96 {
97 GET_CURRENT_CONTEXT(ctx);
98 const GLint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
99 ASSERT_OUTSIDE_BEGIN_END(ctx);
100
101 switch (func) {
102 case GL_NEVER:
103 case GL_LESS:
104 case GL_LEQUAL:
105 case GL_GREATER:
106 case GL_GEQUAL:
107 case GL_EQUAL:
108 case GL_NOTEQUAL:
109 case GL_ALWAYS:
110 break;
111 default:
112 _mesa_error( ctx, GL_INVALID_ENUM, "glStencilFunc" );
113 return;
114 }
115
116 ref = CLAMP( ref, 0, stencilMax );
117
118 if (ctx->Extensions.EXT_stencil_two_side) {
119 /* only set active face state */
120 const GLint face = ctx->Stencil.ActiveFace;
121 if (ctx->Stencil.Function[face] == func &&
122 ctx->Stencil.ValueMask[face] == mask &&
123 ctx->Stencil.Ref[face] == ref)
124 return;
125 FLUSH_VERTICES(ctx, _NEW_STENCIL);
126 ctx->Stencil.Function[face] = func;
127 ctx->Stencil.Ref[face] = ref;
128 ctx->Stencil.ValueMask[face] = mask;
129 if (ctx->Driver.StencilFuncSeparate) {
130 ctx->Driver.StencilFuncSeparate(ctx, face ? GL_BACK : GL_FRONT,
131 func, ref, mask);
132 }
133 }
134 else {
135 /* set both front and back state */
136 if (ctx->Stencil.Function[0] == func &&
137 ctx->Stencil.Function[1] == func &&
138 ctx->Stencil.ValueMask[0] == mask &&
139 ctx->Stencil.ValueMask[1] == mask &&
140 ctx->Stencil.Ref[0] == ref &&
141 ctx->Stencil.Ref[1] == ref)
142 return;
143 FLUSH_VERTICES(ctx, _NEW_STENCIL);
144 ctx->Stencil.Function[0] = ctx->Stencil.Function[1] = func;
145 ctx->Stencil.Ref[0] = ctx->Stencil.Ref[1] = ref;
146 ctx->Stencil.ValueMask[0] = ctx->Stencil.ValueMask[1] = mask;
147 if (ctx->Driver.StencilFuncSeparate) {
148 ctx->Driver.StencilFuncSeparate(ctx, GL_FRONT_AND_BACK,
149 func, ref, mask);
150 }
151 }
152 }
153
154
155 /**
156 * Set the stencil writing mask.
157 *
158 * \param mask bit-mask to enable/disable writing of individual bits in the
159 * stencil planes.
160 *
161 * \sa glStencilMask().
162 *
163 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
164 * notifies the driver via the dd_function_table::StencilMask callback.
165 */
166 void GLAPIENTRY
167 _mesa_StencilMask( GLuint mask )
168 {
169 GET_CURRENT_CONTEXT(ctx);
170 ASSERT_OUTSIDE_BEGIN_END(ctx);
171
172 if (ctx->Extensions.EXT_stencil_two_side) {
173 /* only set active face state */
174 const GLint face = ctx->Stencil.ActiveFace;
175 if (ctx->Stencil.WriteMask[face] == mask)
176 return;
177 FLUSH_VERTICES(ctx, _NEW_STENCIL);
178 ctx->Stencil.WriteMask[face] = mask;
179 if (ctx->Driver.StencilMaskSeparate) {
180 ctx->Driver.StencilMaskSeparate(ctx, face ? GL_BACK : GL_FRONT, mask);
181 }
182 }
183 else {
184 /* set both front and back state */
185 if (ctx->Stencil.WriteMask[0] == mask &&
186 ctx->Stencil.WriteMask[1] == mask)
187 return;
188 FLUSH_VERTICES(ctx, _NEW_STENCIL);
189 ctx->Stencil.WriteMask[0] = ctx->Stencil.WriteMask[1] = mask;
190 if (ctx->Driver.StencilMaskSeparate) {
191 ctx->Driver.StencilMaskSeparate(ctx, GL_FRONT_AND_BACK, mask);
192 }
193 }
194 }
195
196
197 /**
198 * Set the stencil test actions.
199 *
200 * \param fail action to take when stencil test fails.
201 * \param zfail action to take when stencil test passes, but depth test fails.
202 * \param zpass action to take when stencil test passes and the depth test
203 * passes (or depth testing is not enabled).
204 *
205 * \sa glStencilOp().
206 *
207 * Verifies the parameters and updates the respective fields in
208 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
209 * driver via the dd_function_table::StencilOp callback.
210 */
211 void GLAPIENTRY
212 _mesa_StencilOp(GLenum fail, GLenum zfail, GLenum zpass)
213 {
214 GET_CURRENT_CONTEXT(ctx);
215 ASSERT_OUTSIDE_BEGIN_END(ctx);
216
217 switch (fail) {
218 case GL_KEEP:
219 case GL_ZERO:
220 case GL_REPLACE:
221 case GL_INCR:
222 case GL_DECR:
223 case GL_INVERT:
224 break;
225 case GL_INCR_WRAP_EXT:
226 case GL_DECR_WRAP_EXT:
227 if (ctx->Extensions.EXT_stencil_wrap) {
228 break;
229 }
230 /* FALL-THROUGH */
231 default:
232 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOp");
233 return;
234 }
235 switch (zfail) {
236 case GL_KEEP:
237 case GL_ZERO:
238 case GL_REPLACE:
239 case GL_INCR:
240 case GL_DECR:
241 case GL_INVERT:
242 break;
243 case GL_INCR_WRAP_EXT:
244 case GL_DECR_WRAP_EXT:
245 if (ctx->Extensions.EXT_stencil_wrap) {
246 break;
247 }
248 /* FALL-THROUGH */
249 default:
250 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOp");
251 return;
252 }
253 switch (zpass) {
254 case GL_KEEP:
255 case GL_ZERO:
256 case GL_REPLACE:
257 case GL_INCR:
258 case GL_DECR:
259 case GL_INVERT:
260 break;
261 case GL_INCR_WRAP_EXT:
262 case GL_DECR_WRAP_EXT:
263 if (ctx->Extensions.EXT_stencil_wrap) {
264 break;
265 }
266 /* FALL-THROUGH */
267 default:
268 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOp");
269 return;
270 }
271
272 if (ctx->Extensions.EXT_stencil_two_side) {
273 /* only set active face state */
274 const GLint face = ctx->Stencil.ActiveFace;
275 if (ctx->Stencil.ZFailFunc[face] == zfail &&
276 ctx->Stencil.ZPassFunc[face] == zpass &&
277 ctx->Stencil.FailFunc[face] == fail)
278 return;
279 FLUSH_VERTICES(ctx, _NEW_STENCIL);
280 ctx->Stencil.ZFailFunc[face] = zfail;
281 ctx->Stencil.ZPassFunc[face] = zpass;
282 ctx->Stencil.FailFunc[face] = fail;
283 if (ctx->Driver.StencilOpSeparate) {
284 ctx->Driver.StencilOpSeparate(ctx, face ? GL_BACK : GL_FRONT,
285 fail, zfail, zpass);
286 }
287 }
288 else {
289 /* set both front and back state */
290 if (ctx->Stencil.ZFailFunc[0] == zfail &&
291 ctx->Stencil.ZFailFunc[1] == zfail &&
292 ctx->Stencil.ZPassFunc[0] == zpass &&
293 ctx->Stencil.ZPassFunc[1] == zpass &&
294 ctx->Stencil.FailFunc[0] == fail &&
295 ctx->Stencil.FailFunc[1] == fail)
296 return;
297 FLUSH_VERTICES(ctx, _NEW_STENCIL);
298 ctx->Stencil.ZFailFunc[0] = ctx->Stencil.ZFailFunc[1] = zfail;
299 ctx->Stencil.ZPassFunc[0] = ctx->Stencil.ZPassFunc[1] = zpass;
300 ctx->Stencil.FailFunc[0] = ctx->Stencil.FailFunc[1] = fail;
301 if (ctx->Driver.StencilOpSeparate) {
302 ctx->Driver.StencilOpSeparate(ctx, GL_FRONT_AND_BACK,
303 fail, zfail, zpass);
304 }
305 }
306 }
307
308
309
310 #if _HAVE_FULL_GL
311 /* GL_EXT_stencil_two_side */
312 void GLAPIENTRY
313 _mesa_ActiveStencilFaceEXT(GLenum face)
314 {
315 GET_CURRENT_CONTEXT(ctx);
316 ASSERT_OUTSIDE_BEGIN_END(ctx);
317
318 if (!ctx->Extensions.EXT_stencil_two_side) {
319 _mesa_error(ctx, GL_INVALID_OPERATION, "glActiveStencilFaceEXT");
320 return;
321 }
322
323 if (face == GL_FRONT || face == GL_BACK) {
324 FLUSH_VERTICES(ctx, _NEW_STENCIL);
325 ctx->Stencil.ActiveFace = (face == GL_FRONT) ? 0 : 1;
326 }
327 else {
328 _mesa_error(ctx, GL_INVALID_ENUM, "glActiveStencilFaceEXT(face)");
329 }
330 }
331 #endif
332
333
334
335 /**
336 * OpenGL 2.0 function.
337 * \todo Make StencilOp() call this function. And eventually remove the
338 * ctx->Driver.StencilOp function and use ctx->Driver.StencilOpSeparate
339 * instead.
340 */
341 void GLAPIENTRY
342 _mesa_StencilOpSeparate(GLenum face, GLenum fail, GLenum zfail, GLenum zpass)
343 {
344 GET_CURRENT_CONTEXT(ctx);
345 ASSERT_OUTSIDE_BEGIN_END(ctx);
346
347 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
348 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(face)");
349 return;
350 }
351
352 switch (fail) {
353 case GL_KEEP:
354 case GL_ZERO:
355 case GL_REPLACE:
356 case GL_INCR:
357 case GL_DECR:
358 case GL_INVERT:
359 break;
360 case GL_INCR_WRAP_EXT:
361 case GL_DECR_WRAP_EXT:
362 if (ctx->Extensions.EXT_stencil_wrap) {
363 break;
364 }
365 /* FALL-THROUGH */
366 default:
367 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(fail)");
368 return;
369 }
370 switch (zfail) {
371 case GL_KEEP:
372 case GL_ZERO:
373 case GL_REPLACE:
374 case GL_INCR:
375 case GL_DECR:
376 case GL_INVERT:
377 break;
378 case GL_INCR_WRAP_EXT:
379 case GL_DECR_WRAP_EXT:
380 if (ctx->Extensions.EXT_stencil_wrap) {
381 break;
382 }
383 /* FALL-THROUGH */
384 default:
385 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(zfail)");
386 return;
387 }
388 switch (zpass) {
389 case GL_KEEP:
390 case GL_ZERO:
391 case GL_REPLACE:
392 case GL_INCR:
393 case GL_DECR:
394 case GL_INVERT:
395 break;
396 case GL_INCR_WRAP_EXT:
397 case GL_DECR_WRAP_EXT:
398 if (ctx->Extensions.EXT_stencil_wrap) {
399 break;
400 }
401 /* FALL-THROUGH */
402 default:
403 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(zpass)");
404 return;
405 }
406
407 FLUSH_VERTICES(ctx, _NEW_STENCIL);
408
409 if (face == GL_FRONT || face == GL_FRONT_AND_BACK) {
410 ctx->Stencil.FailFunc[0] = fail;
411 ctx->Stencil.ZFailFunc[0] = zfail;
412 ctx->Stencil.ZPassFunc[0] = zpass;
413 }
414 if (face == GL_BACK || face == GL_FRONT_AND_BACK) {
415 ctx->Stencil.FailFunc[1] = fail;
416 ctx->Stencil.ZFailFunc[1] = zfail;
417 ctx->Stencil.ZPassFunc[1] = zpass;
418 }
419
420 if (ctx->Driver.StencilOpSeparate) {
421 ctx->Driver.StencilOpSeparate(ctx, face, fail, zfail, zpass);
422 }
423 }
424
425
426 /* OpenGL 2.0 */
427 void GLAPIENTRY
428 _mesa_StencilFuncSeparate(GLenum face, GLenum func, GLint ref, GLuint mask)
429 {
430 GET_CURRENT_CONTEXT(ctx);
431 const GLint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
432 ASSERT_OUTSIDE_BEGIN_END(ctx);
433
434 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
435 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilFuncSeparate(face)");
436 return;
437 }
438
439 switch (func) {
440 case GL_NEVER:
441 case GL_LESS:
442 case GL_LEQUAL:
443 case GL_GREATER:
444 case GL_GEQUAL:
445 case GL_EQUAL:
446 case GL_NOTEQUAL:
447 case GL_ALWAYS:
448 break;
449 default:
450 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilFuncSeparate(func)");
451 return;
452 }
453
454 ref = CLAMP(ref, 0, stencilMax);
455
456 FLUSH_VERTICES(ctx, _NEW_STENCIL);
457
458 if (face == GL_FRONT || face == GL_FRONT_AND_BACK) {
459 ctx->Stencil.Function[0] = func;
460 ctx->Stencil.Ref[0] = ref;
461 ctx->Stencil.ValueMask[0] = mask;
462 }
463 if (face == GL_BACK || face == GL_FRONT_AND_BACK) {
464 ctx->Stencil.Function[1] = func;
465 ctx->Stencil.Ref[1] = ref;
466 ctx->Stencil.ValueMask[1] = mask;
467 }
468
469 if (ctx->Driver.StencilFuncSeparate) {
470 ctx->Driver.StencilFuncSeparate(ctx, face, func, ref, mask);
471 }
472 }
473
474
475 /* OpenGL 2.0 */
476 void GLAPIENTRY
477 _mesa_StencilMaskSeparate(GLenum face, GLuint mask)
478 {
479 GET_CURRENT_CONTEXT(ctx);
480 ASSERT_OUTSIDE_BEGIN_END(ctx);
481
482 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
483 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilaMaskSeparate(face)");
484 return;
485 }
486
487 FLUSH_VERTICES(ctx, _NEW_STENCIL);
488
489 if (face == GL_FRONT || face == GL_FRONT_AND_BACK) {
490 ctx->Stencil.WriteMask[0] = mask;
491 }
492 if (face == GL_BACK || face == GL_FRONT_AND_BACK) {
493 ctx->Stencil.WriteMask[1] = mask;
494 }
495
496 if (ctx->Driver.StencilMaskSeparate) {
497 ctx->Driver.StencilMaskSeparate(ctx, face, mask);
498 }
499 }
500
501
502 /**
503 * Update derived stencil state.
504 */
505 void
506 _mesa_update_stencil(GLcontext *ctx)
507 {
508 if (ctx->Extensions.EXT_stencil_two_side) {
509 ctx->Stencil._TestTwoSide = ctx->Stencil.TestTwoSide;
510 }
511 else {
512 ctx->Stencil._TestTwoSide =
513 (ctx->Stencil.Function[0] != ctx->Stencil.Function[1] ||
514 ctx->Stencil.FailFunc[0] != ctx->Stencil.FailFunc[1] ||
515 ctx->Stencil.ZPassFunc[0] != ctx->Stencil.ZPassFunc[1] ||
516 ctx->Stencil.ZFailFunc[0] != ctx->Stencil.ZFailFunc[1] ||
517 ctx->Stencil.Ref[0] != ctx->Stencil.Ref[1] ||
518 ctx->Stencil.ValueMask[0] != ctx->Stencil.ValueMask[1] ||
519 ctx->Stencil.WriteMask[0] != ctx->Stencil.WriteMask[1]);
520 }
521 }
522
523
524 /**
525 * Initialize the context stipple state.
526 *
527 * \param ctx GL context.
528 *
529 * Initializes __GLcontextRec::Stencil attribute group.
530 */
531 void
532 _mesa_init_stencil(GLcontext *ctx)
533 {
534 ctx->Stencil.Enabled = GL_FALSE;
535 ctx->Stencil.TestTwoSide = GL_FALSE;
536 ctx->Stencil.ActiveFace = 0; /* 0 = GL_FRONT, 1 = GL_BACK */
537 ctx->Stencil.Function[0] = GL_ALWAYS;
538 ctx->Stencil.Function[1] = GL_ALWAYS;
539 ctx->Stencil.FailFunc[0] = GL_KEEP;
540 ctx->Stencil.FailFunc[1] = GL_KEEP;
541 ctx->Stencil.ZPassFunc[0] = GL_KEEP;
542 ctx->Stencil.ZPassFunc[1] = GL_KEEP;
543 ctx->Stencil.ZFailFunc[0] = GL_KEEP;
544 ctx->Stencil.ZFailFunc[1] = GL_KEEP;
545 ctx->Stencil.Ref[0] = 0;
546 ctx->Stencil.Ref[1] = 0;
547 ctx->Stencil.ValueMask[0] = ~0U;
548 ctx->Stencil.ValueMask[1] = ~0U;
549 ctx->Stencil.WriteMask[0] = ~0U;
550 ctx->Stencil.WriteMask[1] = ~0U;
551 ctx->Stencil.Clear = 0;
552 }