Replace ctx->Driver.StencilOp/Func/Mask() functions with
[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 GLint maxref;
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 maxref = (1 << STENCIL_BITS) - 1;
117 ref = CLAMP( ref, 0, maxref );
118
119 if (ctx->Extensions.EXT_stencil_two_side) {
120 /* only set active face state */
121 const GLint face = ctx->Stencil.ActiveFace;
122 if (ctx->Stencil.Function[face] == func &&
123 ctx->Stencil.ValueMask[face] == mask &&
124 ctx->Stencil.Ref[face] == ref)
125 return;
126 FLUSH_VERTICES(ctx, _NEW_STENCIL);
127 ctx->Stencil.Function[face] = func;
128 ctx->Stencil.Ref[face] = ref;
129 ctx->Stencil.ValueMask[face] = mask;
130 if (ctx->Driver.StencilFuncSeparate) {
131 ctx->Driver.StencilFuncSeparate(ctx, face ? GL_BACK : GL_FRONT,
132 func, ref, mask);
133 }
134 }
135 else {
136 /* set both front and back state */
137 if (ctx->Stencil.Function[0] == func &&
138 ctx->Stencil.Function[1] == func &&
139 ctx->Stencil.ValueMask[0] == mask &&
140 ctx->Stencil.ValueMask[1] == mask &&
141 ctx->Stencil.Ref[0] == ref &&
142 ctx->Stencil.Ref[1] == ref)
143 return;
144 FLUSH_VERTICES(ctx, _NEW_STENCIL);
145 ctx->Stencil.Function[0] = ctx->Stencil.Function[1] = func;
146 ctx->Stencil.Ref[0] = ctx->Stencil.Ref[1] = ref;
147 ctx->Stencil.ValueMask[0] = ctx->Stencil.ValueMask[1] = mask;
148 if (ctx->Driver.StencilFuncSeparate) {
149 ctx->Driver.StencilFuncSeparate(ctx, GL_FRONT_AND_BACK,
150 func, ref, mask);
151 }
152 }
153 }
154
155
156 /**
157 * Set the stencil writing mask.
158 *
159 * \param mask bit-mask to enable/disable writing of individual bits in the
160 * stencil planes.
161 *
162 * \sa glStencilMask().
163 *
164 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
165 * notifies the driver via the dd_function_table::StencilMask callback.
166 */
167 void GLAPIENTRY
168 _mesa_StencilMask( GLuint mask )
169 {
170 GET_CURRENT_CONTEXT(ctx);
171 ASSERT_OUTSIDE_BEGIN_END(ctx);
172
173 if (ctx->Extensions.EXT_stencil_two_side) {
174 /* only set active face state */
175 const GLint face = ctx->Stencil.ActiveFace;
176 if (ctx->Stencil.WriteMask[face] == mask)
177 return;
178 FLUSH_VERTICES(ctx, _NEW_STENCIL);
179 ctx->Stencil.WriteMask[face] = mask;
180 if (ctx->Driver.StencilMaskSeparate) {
181 ctx->Driver.StencilMaskSeparate(ctx, face ? GL_BACK : GL_FRONT, mask);
182 }
183 }
184 else {
185 /* set both front and back state */
186 if (ctx->Stencil.WriteMask[0] == mask &&
187 ctx->Stencil.WriteMask[1] == mask)
188 return;
189 FLUSH_VERTICES(ctx, _NEW_STENCIL);
190 ctx->Stencil.WriteMask[0] = ctx->Stencil.WriteMask[1] = mask;
191 if (ctx->Driver.StencilMaskSeparate) {
192 ctx->Driver.StencilMaskSeparate(ctx, GL_FRONT_AND_BACK, mask);
193 }
194 }
195 }
196
197
198 /**
199 * Set the stencil test actions.
200 *
201 * \param fail action to take when stencil test fails.
202 * \param zfail action to take when stencil test passes, but depth test fails.
203 * \param zpass action to take when stencil test passes and the depth test
204 * passes (or depth testing is not enabled).
205 *
206 * \sa glStencilOp().
207 *
208 * Verifies the parameters and updates the respective fields in
209 * __GLcontextRec::Stencil. On change flushes the vertices and notifies the
210 * driver via the dd_function_table::StencilOp callback.
211 */
212 void GLAPIENTRY
213 _mesa_StencilOp(GLenum fail, GLenum zfail, GLenum zpass)
214 {
215 GET_CURRENT_CONTEXT(ctx);
216 ASSERT_OUTSIDE_BEGIN_END(ctx);
217
218 switch (fail) {
219 case GL_KEEP:
220 case GL_ZERO:
221 case GL_REPLACE:
222 case GL_INCR:
223 case GL_DECR:
224 case GL_INVERT:
225 break;
226 case GL_INCR_WRAP_EXT:
227 case GL_DECR_WRAP_EXT:
228 if (ctx->Extensions.EXT_stencil_wrap) {
229 break;
230 }
231 /* FALL-THROUGH */
232 default:
233 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOp");
234 return;
235 }
236 switch (zfail) {
237 case GL_KEEP:
238 case GL_ZERO:
239 case GL_REPLACE:
240 case GL_INCR:
241 case GL_DECR:
242 case GL_INVERT:
243 break;
244 case GL_INCR_WRAP_EXT:
245 case GL_DECR_WRAP_EXT:
246 if (ctx->Extensions.EXT_stencil_wrap) {
247 break;
248 }
249 /* FALL-THROUGH */
250 default:
251 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOp");
252 return;
253 }
254 switch (zpass) {
255 case GL_KEEP:
256 case GL_ZERO:
257 case GL_REPLACE:
258 case GL_INCR:
259 case GL_DECR:
260 case GL_INVERT:
261 break;
262 case GL_INCR_WRAP_EXT:
263 case GL_DECR_WRAP_EXT:
264 if (ctx->Extensions.EXT_stencil_wrap) {
265 break;
266 }
267 /* FALL-THROUGH */
268 default:
269 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOp");
270 return;
271 }
272
273 if (ctx->Extensions.EXT_stencil_two_side) {
274 /* only set active face state */
275 const GLint face = ctx->Stencil.ActiveFace;
276 if (ctx->Stencil.ZFailFunc[face] == zfail &&
277 ctx->Stencil.ZPassFunc[face] == zpass &&
278 ctx->Stencil.FailFunc[face] == fail)
279 return;
280 FLUSH_VERTICES(ctx, _NEW_STENCIL);
281 ctx->Stencil.ZFailFunc[face] = zfail;
282 ctx->Stencil.ZPassFunc[face] = zpass;
283 ctx->Stencil.FailFunc[face] = fail;
284 if (ctx->Driver.StencilOpSeparate) {
285 ctx->Driver.StencilOpSeparate(ctx, face ? GL_BACK : GL_FRONT,
286 fail, zfail, zpass);
287 }
288 }
289 else {
290 /* set both front and back state */
291 if (ctx->Stencil.ZFailFunc[0] == zfail &&
292 ctx->Stencil.ZFailFunc[1] == zfail &&
293 ctx->Stencil.ZPassFunc[0] == zpass &&
294 ctx->Stencil.ZPassFunc[1] == zpass &&
295 ctx->Stencil.FailFunc[0] == fail &&
296 ctx->Stencil.FailFunc[1] == fail)
297 return;
298 FLUSH_VERTICES(ctx, _NEW_STENCIL);
299 ctx->Stencil.ZFailFunc[0] = ctx->Stencil.ZFailFunc[1] = zfail;
300 ctx->Stencil.ZPassFunc[0] = ctx->Stencil.ZPassFunc[1] = zpass;
301 ctx->Stencil.FailFunc[0] = ctx->Stencil.FailFunc[1] = fail;
302 if (ctx->Driver.StencilOpSeparate) {
303 ctx->Driver.StencilOpSeparate(ctx, GL_FRONT_AND_BACK,
304 fail, zfail, zpass);
305 }
306 }
307 }
308
309
310
311 #if _HAVE_FULL_GL
312 /* GL_EXT_stencil_two_side */
313 void GLAPIENTRY
314 _mesa_ActiveStencilFaceEXT(GLenum face)
315 {
316 GET_CURRENT_CONTEXT(ctx);
317 ASSERT_OUTSIDE_BEGIN_END(ctx);
318
319 if (!ctx->Extensions.EXT_stencil_two_side) {
320 _mesa_error(ctx, GL_INVALID_OPERATION, "glActiveStencilFaceEXT");
321 return;
322 }
323
324 if (face == GL_FRONT || face == GL_BACK) {
325 FLUSH_VERTICES(ctx, _NEW_STENCIL);
326 ctx->Stencil.ActiveFace = (face == GL_FRONT) ? 0 : 1;
327 }
328 else {
329 _mesa_error(ctx, GL_INVALID_ENUM, "glActiveStencilFaceEXT(face)");
330 }
331 }
332 #endif
333
334
335
336 /**
337 * OpenGL 2.0 function.
338 * \todo Make StencilOp() call this function. And eventually remove the
339 * ctx->Driver.StencilOp function and use ctx->Driver.StencilOpSeparate
340 * instead.
341 */
342 void GLAPIENTRY
343 _mesa_StencilOpSeparate(GLenum face, GLenum fail, GLenum zfail, GLenum zpass)
344 {
345 GET_CURRENT_CONTEXT(ctx);
346 ASSERT_OUTSIDE_BEGIN_END(ctx);
347
348 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
349 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(face)");
350 return;
351 }
352
353 switch (fail) {
354 case GL_KEEP:
355 case GL_ZERO:
356 case GL_REPLACE:
357 case GL_INCR:
358 case GL_DECR:
359 case GL_INVERT:
360 break;
361 case GL_INCR_WRAP_EXT:
362 case GL_DECR_WRAP_EXT:
363 if (ctx->Extensions.EXT_stencil_wrap) {
364 break;
365 }
366 /* FALL-THROUGH */
367 default:
368 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(fail)");
369 return;
370 }
371 switch (zfail) {
372 case GL_KEEP:
373 case GL_ZERO:
374 case GL_REPLACE:
375 case GL_INCR:
376 case GL_DECR:
377 case GL_INVERT:
378 break;
379 case GL_INCR_WRAP_EXT:
380 case GL_DECR_WRAP_EXT:
381 if (ctx->Extensions.EXT_stencil_wrap) {
382 break;
383 }
384 /* FALL-THROUGH */
385 default:
386 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(zfail)");
387 return;
388 }
389 switch (zpass) {
390 case GL_KEEP:
391 case GL_ZERO:
392 case GL_REPLACE:
393 case GL_INCR:
394 case GL_DECR:
395 case GL_INVERT:
396 break;
397 case GL_INCR_WRAP_EXT:
398 case GL_DECR_WRAP_EXT:
399 if (ctx->Extensions.EXT_stencil_wrap) {
400 break;
401 }
402 /* FALL-THROUGH */
403 default:
404 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(zpass)");
405 return;
406 }
407
408 FLUSH_VERTICES(ctx, _NEW_STENCIL);
409
410 if (face == GL_FRONT || face == GL_FRONT_AND_BACK) {
411 ctx->Stencil.FailFunc[0] = fail;
412 ctx->Stencil.ZFailFunc[0] = zfail;
413 ctx->Stencil.ZPassFunc[0] = zpass;
414 }
415 if (face == GL_BACK || face == GL_FRONT_AND_BACK) {
416 ctx->Stencil.FailFunc[1] = fail;
417 ctx->Stencil.ZFailFunc[1] = zfail;
418 ctx->Stencil.ZPassFunc[1] = zpass;
419 }
420
421 if (ctx->Driver.StencilOpSeparate) {
422 ctx->Driver.StencilOpSeparate(ctx, face, fail, zfail, zpass);
423 }
424 }
425
426
427 /* OpenGL 2.0 */
428 void GLAPIENTRY
429 _mesa_StencilFuncSeparate(GLenum face, GLenum func, GLint ref, GLuint mask)
430 {
431 GET_CURRENT_CONTEXT(ctx);
432 GLint maxref;
433 ASSERT_OUTSIDE_BEGIN_END(ctx);
434
435 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
436 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilFuncSeparate(face)");
437 return;
438 }
439
440 switch (func) {
441 case GL_NEVER:
442 case GL_LESS:
443 case GL_LEQUAL:
444 case GL_GREATER:
445 case GL_GEQUAL:
446 case GL_EQUAL:
447 case GL_NOTEQUAL:
448 case GL_ALWAYS:
449 break;
450 default:
451 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilFuncSeparate(func)");
452 return;
453 }
454
455 maxref = (1 << STENCIL_BITS) - 1;
456 ref = CLAMP(ref, 0, maxref);
457
458 FLUSH_VERTICES(ctx, _NEW_STENCIL);
459
460 if (face == GL_FRONT || face == GL_FRONT_AND_BACK) {
461 ctx->Stencil.Function[0] = func;
462 ctx->Stencil.Ref[0] = ref;
463 ctx->Stencil.ValueMask[0] = mask;
464 }
465 if (face == GL_BACK || face == GL_FRONT_AND_BACK) {
466 ctx->Stencil.Function[1] = func;
467 ctx->Stencil.Ref[1] = ref;
468 ctx->Stencil.ValueMask[1] = mask;
469 }
470
471 if (ctx->Driver.StencilFuncSeparate) {
472 ctx->Driver.StencilFuncSeparate(ctx, face, func, ref, mask);
473 }
474 }
475
476
477 /* OpenGL 2.0 */
478 void GLAPIENTRY
479 _mesa_StencilMaskSeparate(GLenum face, GLuint mask)
480 {
481 GET_CURRENT_CONTEXT(ctx);
482 ASSERT_OUTSIDE_BEGIN_END(ctx);
483
484 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
485 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilaMaskSeparate(face)");
486 return;
487 }
488
489 FLUSH_VERTICES(ctx, _NEW_STENCIL);
490
491 if (face == GL_FRONT || face == GL_FRONT_AND_BACK) {
492 ctx->Stencil.WriteMask[0] = mask;
493 }
494 if (face == GL_BACK || face == GL_FRONT_AND_BACK) {
495 ctx->Stencil.WriteMask[1] = mask;
496 }
497
498 if (ctx->Driver.StencilMaskSeparate) {
499 ctx->Driver.StencilMaskSeparate(ctx, face, mask);
500 }
501 }
502
503
504 /**
505 * Update derived stencil state.
506 */
507 void
508 _mesa_update_stencil(GLcontext *ctx)
509 {
510 if (ctx->Extensions.EXT_stencil_two_side) {
511 ctx->Stencil._TestTwoSide = ctx->Stencil.TestTwoSide;
512 }
513 else {
514 ctx->Stencil._TestTwoSide =
515 (ctx->Stencil.Function[0] != ctx->Stencil.Function[1] ||
516 ctx->Stencil.FailFunc[0] != ctx->Stencil.FailFunc[1] ||
517 ctx->Stencil.ZPassFunc[0] != ctx->Stencil.ZPassFunc[1] ||
518 ctx->Stencil.ZFailFunc[0] != ctx->Stencil.ZFailFunc[1] ||
519 ctx->Stencil.Ref[0] != ctx->Stencil.Ref[1] ||
520 ctx->Stencil.ValueMask[0] != ctx->Stencil.ValueMask[1] ||
521 ctx->Stencil.WriteMask[0] != ctx->Stencil.WriteMask[1]);
522 }
523 }
524
525
526 /**
527 * Initialize the context stipple state.
528 *
529 * \param ctx GL context.
530 *
531 * Initializes __GLcontextRec::Stencil attribute group.
532 */
533 void
534 _mesa_init_stencil(GLcontext *ctx)
535 {
536 ctx->Stencil.Enabled = GL_FALSE;
537 ctx->Stencil.TestTwoSide = GL_FALSE;
538 ctx->Stencil.ActiveFace = 0; /* 0 = GL_FRONT, 1 = GL_BACK */
539 ctx->Stencil.Function[0] = GL_ALWAYS;
540 ctx->Stencil.Function[1] = GL_ALWAYS;
541 ctx->Stencil.FailFunc[0] = GL_KEEP;
542 ctx->Stencil.FailFunc[1] = GL_KEEP;
543 ctx->Stencil.ZPassFunc[0] = GL_KEEP;
544 ctx->Stencil.ZPassFunc[1] = GL_KEEP;
545 ctx->Stencil.ZFailFunc[0] = GL_KEEP;
546 ctx->Stencil.ZFailFunc[1] = GL_KEEP;
547 ctx->Stencil.Ref[0] = 0;
548 ctx->Stencil.Ref[1] = 0;
549 ctx->Stencil.ValueMask[0] = ~0U;
550 ctx->Stencil.ValueMask[1] = ~0U;
551 ctx->Stencil.WriteMask[0] = ~0U;
552 ctx->Stencil.WriteMask[1] = ~0U;
553 ctx->Stencil.Clear = 0;
554 }