Add ATI_separate_stencil and use it in preference to EXT_stencil_two_side
[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 (0x%04x)", func );
113 return;
114 }
115
116 ref = CLAMP( ref, 0, stencilMax );
117
118 if (ctx->Extensions.ATI_separate_stencil) {
119 /* set both front and back state */
120 if (ctx->Stencil.Function[0] == func &&
121 ctx->Stencil.Function[1] == func &&
122 ctx->Stencil.ValueMask[0] == mask &&
123 ctx->Stencil.ValueMask[1] == mask &&
124 ctx->Stencil.Ref[0] == ref &&
125 ctx->Stencil.Ref[1] == ref)
126 return;
127 FLUSH_VERTICES(ctx, _NEW_STENCIL);
128 ctx->Stencil.Function[0] = ctx->Stencil.Function[1] = func;
129 ctx->Stencil.Ref[0] = ctx->Stencil.Ref[1] = ref;
130 ctx->Stencil.ValueMask[0] = ctx->Stencil.ValueMask[1] = mask;
131 if (ctx->Driver.StencilFuncSeparate) {
132 ctx->Driver.StencilFuncSeparate(ctx, GL_FRONT_AND_BACK,
133 func, ref, mask);
134 }
135 }
136 else {
137 /* only set active face state */
138 const GLint face = ctx->Stencil.ActiveFace;
139 if (ctx->Stencil.Function[face] == func &&
140 ctx->Stencil.ValueMask[face] == mask &&
141 ctx->Stencil.Ref[face] == ref)
142 return;
143 FLUSH_VERTICES(ctx, _NEW_STENCIL);
144 ctx->Stencil.Function[face] = func;
145 ctx->Stencil.Ref[face] = ref;
146 ctx->Stencil.ValueMask[face] = mask;
147 if (ctx->Driver.StencilFuncSeparate) {
148 ctx->Driver.StencilFuncSeparate(ctx, face ? GL_BACK : GL_FRONT,
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.ATI_separate_stencil) {
173 /* set both front and back state */
174 if (ctx->Stencil.WriteMask[0] == mask &&
175 ctx->Stencil.WriteMask[1] == mask)
176 return;
177 FLUSH_VERTICES(ctx, _NEW_STENCIL);
178 ctx->Stencil.WriteMask[0] = ctx->Stencil.WriteMask[1] = mask;
179 if (ctx->Driver.StencilMaskSeparate) {
180 ctx->Driver.StencilMaskSeparate(ctx, GL_FRONT_AND_BACK, mask);
181 }
182 }
183 else {
184 /* only set active face state */
185 const GLint face = ctx->Stencil.ActiveFace;
186 if (ctx->Stencil.WriteMask[face] == mask)
187 return;
188 FLUSH_VERTICES(ctx, _NEW_STENCIL);
189 ctx->Stencil.WriteMask[face] = mask;
190 if (ctx->Driver.StencilMaskSeparate) {
191 ctx->Driver.StencilMaskSeparate(ctx, face ? GL_BACK : GL_FRONT, 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.ATI_separate_stencil) {
273 /* set both front and back state */
274 if (ctx->Stencil.ZFailFunc[0] == zfail &&
275 ctx->Stencil.ZFailFunc[1] == zfail &&
276 ctx->Stencil.ZPassFunc[0] == zpass &&
277 ctx->Stencil.ZPassFunc[1] == zpass &&
278 ctx->Stencil.FailFunc[0] == fail &&
279 ctx->Stencil.FailFunc[1] == fail)
280 return;
281 FLUSH_VERTICES(ctx, _NEW_STENCIL);
282 ctx->Stencil.ZFailFunc[0] = ctx->Stencil.ZFailFunc[1] = zfail;
283 ctx->Stencil.ZPassFunc[0] = ctx->Stencil.ZPassFunc[1] = zpass;
284 ctx->Stencil.FailFunc[0] = ctx->Stencil.FailFunc[1] = fail;
285 if (ctx->Driver.StencilOpSeparate) {
286 ctx->Driver.StencilOpSeparate(ctx, GL_FRONT_AND_BACK,
287 fail, zfail, zpass);
288 }
289 }
290 else {
291 /* only set active face state */
292 const GLint face = ctx->Stencil.ActiveFace;
293 if (ctx->Stencil.ZFailFunc[face] == zfail &&
294 ctx->Stencil.ZPassFunc[face] == zpass &&
295 ctx->Stencil.FailFunc[face] == fail)
296 return;
297 FLUSH_VERTICES(ctx, _NEW_STENCIL);
298 ctx->Stencil.ZFailFunc[face] = zfail;
299 ctx->Stencil.ZPassFunc[face] = zpass;
300 ctx->Stencil.FailFunc[face] = fail;
301 if (ctx->Driver.StencilOpSeparate) {
302 ctx->Driver.StencilOpSeparate(ctx, face ? GL_BACK : GL_FRONT,
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_BACK) {
410 ctx->Stencil.FailFunc[0] = fail;
411 ctx->Stencil.ZFailFunc[0] = zfail;
412 ctx->Stencil.ZPassFunc[0] = zpass;
413 }
414 if (face != GL_FRONT) {
415 ctx->Stencil.FailFunc[1] = fail;
416 ctx->Stencil.ZFailFunc[1] = zfail;
417 ctx->Stencil.ZPassFunc[1] = zpass;
418 }
419 if (ctx->Driver.StencilOpSeparate) {
420 ctx->Driver.StencilOpSeparate(ctx, face, fail, zfail, zpass);
421 }
422 }
423
424
425 /* OpenGL 2.0 */
426 void GLAPIENTRY
427 _mesa_StencilFuncSeparate(GLenum face, GLenum func, GLint ref, GLuint mask)
428 {
429 GET_CURRENT_CONTEXT(ctx);
430 const GLint stencilMax = (1 << ctx->DrawBuffer->Visual.stencilBits) - 1;
431 ASSERT_OUTSIDE_BEGIN_END(ctx);
432
433 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
434 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilFuncSeparate(face)");
435 return;
436 }
437
438 switch (func) {
439 case GL_NEVER:
440 case GL_LESS:
441 case GL_LEQUAL:
442 case GL_GREATER:
443 case GL_GEQUAL:
444 case GL_EQUAL:
445 case GL_NOTEQUAL:
446 case GL_ALWAYS:
447 break;
448 default:
449 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilFuncSeparate(func)");
450 return;
451 }
452
453 ref = CLAMP(ref, 0, stencilMax);
454
455 FLUSH_VERTICES(ctx, _NEW_STENCIL);
456
457 if (face == GL_FRONT || face == GL_FRONT_AND_BACK) {
458 ctx->Stencil.Function[0] = func;
459 ctx->Stencil.Ref[0] = ref;
460 ctx->Stencil.ValueMask[0] = mask;
461 }
462 if (face == GL_BACK || face == GL_FRONT_AND_BACK) {
463 ctx->Stencil.Function[1] = func;
464 ctx->Stencil.Ref[1] = ref;
465 ctx->Stencil.ValueMask[1] = mask;
466 }
467 if (ctx->Driver.StencilFuncSeparate) {
468 ctx->Driver.StencilFuncSeparate(ctx, face, func, ref, mask);
469 }
470 }
471
472
473 /* OpenGL 2.0 */
474 void GLAPIENTRY
475 _mesa_StencilMaskSeparate(GLenum face, GLuint mask)
476 {
477 GET_CURRENT_CONTEXT(ctx);
478 ASSERT_OUTSIDE_BEGIN_END(ctx);
479
480 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
481 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilaMaskSeparate(face)");
482 return;
483 }
484
485 FLUSH_VERTICES(ctx, _NEW_STENCIL);
486
487 if (face != GL_BACK) {
488 ctx->Stencil.WriteMask[0] = mask;
489 }
490 if (face != GL_FRONT) {
491 ctx->Stencil.WriteMask[1] = mask;
492 }
493 if (ctx->Driver.StencilMaskSeparate) {
494 ctx->Driver.StencilMaskSeparate(ctx, face, mask);
495 }
496 }
497
498
499 /**
500 * Update derived stencil state.
501 */
502 void
503 _mesa_update_stencil(GLcontext *ctx)
504 {
505 if (ctx->Extensions.EXT_stencil_two_side) {
506 ctx->Stencil._TestTwoSide = ctx->Stencil.TestTwoSide;
507 }
508 else {
509 ctx->Stencil._TestTwoSide =
510 (ctx->Stencil.Function[0] != ctx->Stencil.Function[1] ||
511 ctx->Stencil.FailFunc[0] != ctx->Stencil.FailFunc[1] ||
512 ctx->Stencil.ZPassFunc[0] != ctx->Stencil.ZPassFunc[1] ||
513 ctx->Stencil.ZFailFunc[0] != ctx->Stencil.ZFailFunc[1] ||
514 ctx->Stencil.Ref[0] != ctx->Stencil.Ref[1] ||
515 ctx->Stencil.ValueMask[0] != ctx->Stencil.ValueMask[1] ||
516 ctx->Stencil.WriteMask[0] != ctx->Stencil.WriteMask[1]);
517 }
518 }
519
520
521 /**
522 * Initialize the context stipple state.
523 *
524 * \param ctx GL context.
525 *
526 * Initializes __GLcontextRec::Stencil attribute group.
527 */
528 void
529 _mesa_init_stencil(GLcontext *ctx)
530 {
531 ctx->Stencil.Enabled = GL_FALSE;
532 ctx->Stencil.TestTwoSide = GL_FALSE;
533 ctx->Stencil.ActiveFace = 0; /* 0 = GL_FRONT, 1 = GL_BACK */
534 ctx->Stencil.Function[0] = GL_ALWAYS;
535 ctx->Stencil.Function[1] = GL_ALWAYS;
536 ctx->Stencil.FailFunc[0] = GL_KEEP;
537 ctx->Stencil.FailFunc[1] = GL_KEEP;
538 ctx->Stencil.ZPassFunc[0] = GL_KEEP;
539 ctx->Stencil.ZPassFunc[1] = GL_KEEP;
540 ctx->Stencil.ZFailFunc[0] = GL_KEEP;
541 ctx->Stencil.ZFailFunc[1] = GL_KEEP;
542 ctx->Stencil.Ref[0] = 0;
543 ctx->Stencil.Ref[1] = 0;
544 ctx->Stencil.ValueMask[0] = ~0U;
545 ctx->Stencil.ValueMask[1] = ~0U;
546 ctx->Stencil.WriteMask[0] = ~0U;
547 ctx->Stencil.WriteMask[1] = ~0U;
548 ctx->Stencil.Clear = 0;
549 }