mesa: Update todo regarding StencilOp and StencilOpSeparate.
[mesa.git] / src / mesa / main / stencil.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * \file stencil.c
28 * Stencil operations.
29 *
30 * Note: There's some conflict 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 * Also, note that GL_ATI_separate_stencil is different as well:
40 * glStencilFuncSeparateATI(GLenum frontfunc, GLenum backfunc, ...) vs.
41 * glStencilFuncSeparate(GLenum face, GLenum func, ...).
42 *
43 * This problem is solved by keeping three sets of stencil state:
44 * state[0] = GL_FRONT state.
45 * state[1] = OpenGL 2.0 / GL_ATI_separate_stencil GL_BACK state.
46 * state[2] = GL_EXT_stencil_two_side GL_BACK state.
47 */
48
49
50 #include "glheader.h"
51 #include "imports.h"
52 #include "context.h"
53 #include "macros.h"
54 #include "stencil.h"
55 #include "mtypes.h"
56
57
58 static GLboolean
59 validate_stencil_op(struct gl_context *ctx, GLenum op)
60 {
61 switch (op) {
62 case GL_KEEP:
63 case GL_ZERO:
64 case GL_REPLACE:
65 case GL_INCR:
66 case GL_DECR:
67 case GL_INVERT:
68 case GL_INCR_WRAP:
69 case GL_DECR_WRAP:
70 return GL_TRUE;
71 default:
72 return GL_FALSE;
73 }
74 }
75
76
77 static GLboolean
78 validate_stencil_func(struct gl_context *ctx, GLenum func)
79 {
80 switch (func) {
81 case GL_NEVER:
82 case GL_LESS:
83 case GL_LEQUAL:
84 case GL_GREATER:
85 case GL_GEQUAL:
86 case GL_EQUAL:
87 case GL_NOTEQUAL:
88 case GL_ALWAYS:
89 return GL_TRUE;
90 default:
91 return GL_FALSE;
92 }
93 }
94
95
96 /**
97 * Set the clear value for the stencil buffer.
98 *
99 * \param s clear value.
100 *
101 * \sa glClearStencil().
102 *
103 * Updates gl_stencil_attrib::Clear. On change
104 * flushes the vertices and notifies the driver via
105 * the dd_function_table::ClearStencil callback.
106 */
107 void GLAPIENTRY
108 _mesa_ClearStencil( GLint s )
109 {
110 GET_CURRENT_CONTEXT(ctx);
111
112 ctx->Stencil.Clear = (GLuint) s;
113 }
114
115
116 /**
117 * Set the function and reference value for stencil testing.
118 *
119 * \param frontfunc front test function.
120 * \param backfunc back test function.
121 * \param ref front and back reference value.
122 * \param mask front and back bitmask.
123 *
124 * \sa glStencilFunc().
125 *
126 * Verifies the parameters and updates the respective values in
127 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies
128 * the driver via the dd_function_table::StencilFunc callback.
129 */
130 void GLAPIENTRY
131 _mesa_StencilFuncSeparateATI( GLenum frontfunc, GLenum backfunc, GLint ref, GLuint mask )
132 {
133 GET_CURRENT_CONTEXT(ctx);
134
135 if (MESA_VERBOSE & VERBOSE_API)
136 _mesa_debug(ctx, "glStencilFuncSeparateATI()\n");
137
138 if (!validate_stencil_func(ctx, frontfunc)) {
139 _mesa_error(ctx, GL_INVALID_ENUM,
140 "glStencilFuncSeparateATI(frontfunc)");
141 return;
142 }
143 if (!validate_stencil_func(ctx, backfunc)) {
144 _mesa_error(ctx, GL_INVALID_ENUM,
145 "glStencilFuncSeparateATI(backfunc)");
146 return;
147 }
148
149 /* set both front and back state */
150 if (ctx->Stencil.Function[0] == frontfunc &&
151 ctx->Stencil.Function[1] == backfunc &&
152 ctx->Stencil.ValueMask[0] == mask &&
153 ctx->Stencil.ValueMask[1] == mask &&
154 ctx->Stencil.Ref[0] == ref &&
155 ctx->Stencil.Ref[1] == ref)
156 return;
157 FLUSH_VERTICES(ctx, _NEW_STENCIL);
158 ctx->Stencil.Function[0] = frontfunc;
159 ctx->Stencil.Function[1] = backfunc;
160 ctx->Stencil.Ref[0] = ctx->Stencil.Ref[1] = ref;
161 ctx->Stencil.ValueMask[0] = ctx->Stencil.ValueMask[1] = mask;
162 if (ctx->Driver.StencilFuncSeparate) {
163 ctx->Driver.StencilFuncSeparate(ctx, GL_FRONT,
164 frontfunc, ref, mask);
165 ctx->Driver.StencilFuncSeparate(ctx, GL_BACK,
166 backfunc, ref, mask);
167 }
168 }
169
170
171 /**
172 * Set the function and reference value for stencil testing.
173 *
174 * \param func test function.
175 * \param ref reference value.
176 * \param mask bitmask.
177 *
178 * \sa glStencilFunc().
179 *
180 * Verifies the parameters and updates the respective values in
181 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies
182 * the driver via the dd_function_table::StencilFunc callback.
183 */
184 void GLAPIENTRY
185 _mesa_StencilFunc( GLenum func, GLint ref, GLuint mask )
186 {
187 GET_CURRENT_CONTEXT(ctx);
188 const GLint face = ctx->Stencil.ActiveFace;
189
190 if (MESA_VERBOSE & VERBOSE_API)
191 _mesa_debug(ctx, "glStencilFunc()\n");
192
193 if (!validate_stencil_func(ctx, func)) {
194 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilFunc(func)");
195 return;
196 }
197
198 if (face != 0) {
199 if (ctx->Stencil.Function[face] == func &&
200 ctx->Stencil.ValueMask[face] == mask &&
201 ctx->Stencil.Ref[face] == ref)
202 return;
203 FLUSH_VERTICES(ctx, _NEW_STENCIL);
204 ctx->Stencil.Function[face] = func;
205 ctx->Stencil.Ref[face] = ref;
206 ctx->Stencil.ValueMask[face] = mask;
207
208 /* Only propagate the change to the driver if EXT_stencil_two_side
209 * is enabled.
210 */
211 if (ctx->Driver.StencilFuncSeparate && ctx->Stencil.TestTwoSide) {
212 ctx->Driver.StencilFuncSeparate(ctx, GL_BACK, func, ref, mask);
213 }
214 }
215 else {
216 /* set both front and back state */
217 if (ctx->Stencil.Function[0] == func &&
218 ctx->Stencil.Function[1] == func &&
219 ctx->Stencil.ValueMask[0] == mask &&
220 ctx->Stencil.ValueMask[1] == mask &&
221 ctx->Stencil.Ref[0] == ref &&
222 ctx->Stencil.Ref[1] == ref)
223 return;
224 FLUSH_VERTICES(ctx, _NEW_STENCIL);
225 ctx->Stencil.Function[0] = ctx->Stencil.Function[1] = func;
226 ctx->Stencil.Ref[0] = ctx->Stencil.Ref[1] = ref;
227 ctx->Stencil.ValueMask[0] = ctx->Stencil.ValueMask[1] = mask;
228 if (ctx->Driver.StencilFuncSeparate) {
229 ctx->Driver.StencilFuncSeparate(ctx,
230 ((ctx->Stencil.TestTwoSide)
231 ? GL_FRONT : GL_FRONT_AND_BACK),
232 func, ref, mask);
233 }
234 }
235 }
236
237
238 /**
239 * Set the stencil writing mask.
240 *
241 * \param mask bit-mask to enable/disable writing of individual bits in the
242 * stencil planes.
243 *
244 * \sa glStencilMask().
245 *
246 * Updates gl_stencil_attrib::WriteMask. On change flushes the vertices and
247 * notifies the driver via the dd_function_table::StencilMask callback.
248 */
249 void GLAPIENTRY
250 _mesa_StencilMask( GLuint mask )
251 {
252 GET_CURRENT_CONTEXT(ctx);
253 const GLint face = ctx->Stencil.ActiveFace;
254
255 if (MESA_VERBOSE & VERBOSE_API)
256 _mesa_debug(ctx, "glStencilMask()\n");
257
258 if (face != 0) {
259 /* Only modify the EXT_stencil_two_side back-face state.
260 */
261 if (ctx->Stencil.WriteMask[face] == mask)
262 return;
263 FLUSH_VERTICES(ctx, _NEW_STENCIL);
264 ctx->Stencil.WriteMask[face] = mask;
265
266 /* Only propagate the change to the driver if EXT_stencil_two_side
267 * is enabled.
268 */
269 if (ctx->Driver.StencilMaskSeparate && ctx->Stencil.TestTwoSide) {
270 ctx->Driver.StencilMaskSeparate(ctx, GL_BACK, mask);
271 }
272 }
273 else {
274 /* set both front and back state */
275 if (ctx->Stencil.WriteMask[0] == mask &&
276 ctx->Stencil.WriteMask[1] == mask)
277 return;
278 FLUSH_VERTICES(ctx, _NEW_STENCIL);
279 ctx->Stencil.WriteMask[0] = ctx->Stencil.WriteMask[1] = mask;
280 if (ctx->Driver.StencilMaskSeparate) {
281 ctx->Driver.StencilMaskSeparate(ctx,
282 ((ctx->Stencil.TestTwoSide)
283 ? GL_FRONT : GL_FRONT_AND_BACK),
284 mask);
285 }
286 }
287 }
288
289
290 /**
291 * Set the stencil test actions.
292 *
293 * \param fail action to take when stencil test fails.
294 * \param zfail action to take when stencil test passes, but depth test fails.
295 * \param zpass action to take when stencil test passes and the depth test
296 * passes (or depth testing is not enabled).
297 *
298 * \sa glStencilOp().
299 *
300 * Verifies the parameters and updates the respective fields in
301 * __struct gl_contextRec::Stencil. On change flushes the vertices and notifies
302 * the driver via the dd_function_table::StencilOp callback.
303 */
304 void GLAPIENTRY
305 _mesa_StencilOp(GLenum fail, GLenum zfail, GLenum zpass)
306 {
307 GET_CURRENT_CONTEXT(ctx);
308 const GLint face = ctx->Stencil.ActiveFace;
309
310 if (MESA_VERBOSE & VERBOSE_API)
311 _mesa_debug(ctx, "glStencilOp()\n");
312
313 if (!validate_stencil_op(ctx, fail)) {
314 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOp(sfail)");
315 return;
316 }
317 if (!validate_stencil_op(ctx, zfail)) {
318 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOp(zfail)");
319 return;
320 }
321 if (!validate_stencil_op(ctx, zpass)) {
322 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOp(zpass)");
323 return;
324 }
325
326 if (face != 0) {
327 /* only set active face state */
328 if (ctx->Stencil.ZFailFunc[face] == zfail &&
329 ctx->Stencil.ZPassFunc[face] == zpass &&
330 ctx->Stencil.FailFunc[face] == fail)
331 return;
332 FLUSH_VERTICES(ctx, _NEW_STENCIL);
333 ctx->Stencil.ZFailFunc[face] = zfail;
334 ctx->Stencil.ZPassFunc[face] = zpass;
335 ctx->Stencil.FailFunc[face] = fail;
336
337 /* Only propagate the change to the driver if EXT_stencil_two_side
338 * is enabled.
339 */
340 if (ctx->Driver.StencilOpSeparate && ctx->Stencil.TestTwoSide) {
341 ctx->Driver.StencilOpSeparate(ctx, GL_BACK, fail, zfail, zpass);
342 }
343 }
344 else {
345 /* set both front and back state */
346 if (ctx->Stencil.ZFailFunc[0] == zfail &&
347 ctx->Stencil.ZFailFunc[1] == zfail &&
348 ctx->Stencil.ZPassFunc[0] == zpass &&
349 ctx->Stencil.ZPassFunc[1] == zpass &&
350 ctx->Stencil.FailFunc[0] == fail &&
351 ctx->Stencil.FailFunc[1] == fail)
352 return;
353 FLUSH_VERTICES(ctx, _NEW_STENCIL);
354 ctx->Stencil.ZFailFunc[0] = ctx->Stencil.ZFailFunc[1] = zfail;
355 ctx->Stencil.ZPassFunc[0] = ctx->Stencil.ZPassFunc[1] = zpass;
356 ctx->Stencil.FailFunc[0] = ctx->Stencil.FailFunc[1] = fail;
357 if (ctx->Driver.StencilOpSeparate) {
358 ctx->Driver.StencilOpSeparate(ctx,
359 ((ctx->Stencil.TestTwoSide)
360 ? GL_FRONT : GL_FRONT_AND_BACK),
361 fail, zfail, zpass);
362 }
363 }
364 }
365
366
367
368 /* GL_EXT_stencil_two_side */
369 void GLAPIENTRY
370 _mesa_ActiveStencilFaceEXT(GLenum face)
371 {
372 GET_CURRENT_CONTEXT(ctx);
373
374 if (MESA_VERBOSE & VERBOSE_API)
375 _mesa_debug(ctx, "glActiveStencilFaceEXT()\n");
376
377 if (!ctx->Extensions.EXT_stencil_two_side) {
378 _mesa_error(ctx, GL_INVALID_OPERATION, "glActiveStencilFaceEXT");
379 return;
380 }
381
382 if (face == GL_FRONT || face == GL_BACK) {
383 ctx->Stencil.ActiveFace = (face == GL_FRONT) ? 0 : 2;
384 }
385 else {
386 _mesa_error(ctx, GL_INVALID_ENUM, "glActiveStencilFaceEXT(face)");
387 }
388 }
389
390
391
392 void GLAPIENTRY
393 _mesa_StencilOpSeparate(GLenum face, GLenum sfail, GLenum zfail, GLenum zpass)
394 {
395 GLboolean set = GL_FALSE;
396 GET_CURRENT_CONTEXT(ctx);
397
398 if (MESA_VERBOSE & VERBOSE_API)
399 _mesa_debug(ctx, "glStencilOpSeparate()\n");
400
401 if (!validate_stencil_op(ctx, sfail)) {
402 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(sfail)");
403 return;
404 }
405 if (!validate_stencil_op(ctx, zfail)) {
406 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(zfail)");
407 return;
408 }
409 if (!validate_stencil_op(ctx, zpass)) {
410 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(zpass)");
411 return;
412 }
413 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
414 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilOpSeparate(face)");
415 return;
416 }
417
418 if (face != GL_BACK) {
419 /* set front */
420 if (ctx->Stencil.ZFailFunc[0] != zfail ||
421 ctx->Stencil.ZPassFunc[0] != zpass ||
422 ctx->Stencil.FailFunc[0] != sfail){
423 FLUSH_VERTICES(ctx, _NEW_STENCIL);
424 ctx->Stencil.ZFailFunc[0] = zfail;
425 ctx->Stencil.ZPassFunc[0] = zpass;
426 ctx->Stencil.FailFunc[0] = sfail;
427 set = GL_TRUE;
428 }
429 }
430 if (face != GL_FRONT) {
431 /* set back */
432 if (ctx->Stencil.ZFailFunc[1] != zfail ||
433 ctx->Stencil.ZPassFunc[1] != zpass ||
434 ctx->Stencil.FailFunc[1] != sfail) {
435 FLUSH_VERTICES(ctx, _NEW_STENCIL);
436 ctx->Stencil.ZFailFunc[1] = zfail;
437 ctx->Stencil.ZPassFunc[1] = zpass;
438 ctx->Stencil.FailFunc[1] = sfail;
439 set = GL_TRUE;
440 }
441 }
442 if (set && ctx->Driver.StencilOpSeparate) {
443 ctx->Driver.StencilOpSeparate(ctx, face, sfail, zfail, zpass);
444 }
445 }
446
447
448 /* OpenGL 2.0 */
449 void GLAPIENTRY
450 _mesa_StencilFuncSeparate(GLenum face, GLenum func, GLint ref, GLuint mask)
451 {
452 GET_CURRENT_CONTEXT(ctx);
453
454 if (MESA_VERBOSE & VERBOSE_API)
455 _mesa_debug(ctx, "glStencilFuncSeparate()\n");
456
457 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
458 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilFuncSeparate(face)");
459 return;
460 }
461 if (!validate_stencil_func(ctx, func)) {
462 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilFuncSeparate(func)");
463 return;
464 }
465
466 FLUSH_VERTICES(ctx, _NEW_STENCIL);
467
468 if (face != GL_BACK) {
469 /* set front */
470 ctx->Stencil.Function[0] = func;
471 ctx->Stencil.Ref[0] = ref;
472 ctx->Stencil.ValueMask[0] = mask;
473 }
474 if (face != GL_FRONT) {
475 /* set back */
476 ctx->Stencil.Function[1] = func;
477 ctx->Stencil.Ref[1] = ref;
478 ctx->Stencil.ValueMask[1] = mask;
479 }
480 if (ctx->Driver.StencilFuncSeparate) {
481 ctx->Driver.StencilFuncSeparate(ctx, face, func, ref, mask);
482 }
483 }
484
485
486 /* OpenGL 2.0 */
487 void GLAPIENTRY
488 _mesa_StencilMaskSeparate(GLenum face, GLuint mask)
489 {
490 GET_CURRENT_CONTEXT(ctx);
491
492 if (MESA_VERBOSE & VERBOSE_API)
493 _mesa_debug(ctx, "glStencilMaskSeparate()\n");
494
495 if (face != GL_FRONT && face != GL_BACK && face != GL_FRONT_AND_BACK) {
496 _mesa_error(ctx, GL_INVALID_ENUM, "glStencilaMaskSeparate(face)");
497 return;
498 }
499
500 FLUSH_VERTICES(ctx, _NEW_STENCIL);
501
502 if (face != GL_BACK) {
503 ctx->Stencil.WriteMask[0] = mask;
504 }
505 if (face != GL_FRONT) {
506 ctx->Stencil.WriteMask[1] = mask;
507 }
508 if (ctx->Driver.StencilMaskSeparate) {
509 ctx->Driver.StencilMaskSeparate(ctx, face, mask);
510 }
511 }
512
513
514 /**
515 * Update derived stencil state.
516 */
517 void
518 _mesa_update_stencil(struct gl_context *ctx)
519 {
520 const GLint face = ctx->Stencil._BackFace;
521
522 ctx->Stencil._Enabled = (ctx->Stencil.Enabled &&
523 ctx->DrawBuffer->Visual.stencilBits > 0);
524
525 ctx->Stencil._TestTwoSide =
526 ctx->Stencil._Enabled &&
527 (ctx->Stencil.Function[0] != ctx->Stencil.Function[face] ||
528 ctx->Stencil.FailFunc[0] != ctx->Stencil.FailFunc[face] ||
529 ctx->Stencil.ZPassFunc[0] != ctx->Stencil.ZPassFunc[face] ||
530 ctx->Stencil.ZFailFunc[0] != ctx->Stencil.ZFailFunc[face] ||
531 ctx->Stencil.Ref[0] != ctx->Stencil.Ref[face] ||
532 ctx->Stencil.ValueMask[0] != ctx->Stencil.ValueMask[face] ||
533 ctx->Stencil.WriteMask[0] != ctx->Stencil.WriteMask[face]);
534
535 ctx->Stencil._WriteEnabled =
536 ctx->Stencil._Enabled &&
537 (ctx->Stencil.WriteMask[0] != 0 ||
538 (ctx->Stencil._TestTwoSide && ctx->Stencil.WriteMask[face] != 0));
539 }
540
541
542 /**
543 * Initialize the context stipple state.
544 *
545 * \param ctx GL context.
546 *
547 * Initializes __struct gl_contextRec::Stencil attribute group.
548 */
549 void
550 _mesa_init_stencil(struct gl_context *ctx)
551 {
552 ctx->Stencil.Enabled = GL_FALSE;
553 ctx->Stencil.TestTwoSide = GL_FALSE;
554 ctx->Stencil.ActiveFace = 0; /* 0 = GL_FRONT, 2 = GL_BACK */
555 ctx->Stencil.Function[0] = GL_ALWAYS;
556 ctx->Stencil.Function[1] = GL_ALWAYS;
557 ctx->Stencil.Function[2] = GL_ALWAYS;
558 ctx->Stencil.FailFunc[0] = GL_KEEP;
559 ctx->Stencil.FailFunc[1] = GL_KEEP;
560 ctx->Stencil.FailFunc[2] = GL_KEEP;
561 ctx->Stencil.ZPassFunc[0] = GL_KEEP;
562 ctx->Stencil.ZPassFunc[1] = GL_KEEP;
563 ctx->Stencil.ZPassFunc[2] = GL_KEEP;
564 ctx->Stencil.ZFailFunc[0] = GL_KEEP;
565 ctx->Stencil.ZFailFunc[1] = GL_KEEP;
566 ctx->Stencil.ZFailFunc[2] = GL_KEEP;
567 ctx->Stencil.Ref[0] = 0;
568 ctx->Stencil.Ref[1] = 0;
569 ctx->Stencil.Ref[2] = 0;
570
571 /* 4.1.4 Stencil Test section of the GL-ES 3.0 specification says:
572 *
573 * "In the initial state, [...] the front and back stencil mask are both
574 * set to the value 2^s − 1, where s is greater than or equal to the
575 * number of bits in the deepest stencil buffer* supported by the GL
576 * implementation."
577 *
578 * Since the maximum supported precision for stencil buffers is 8 bits,
579 * mask values should be initialized to 2^8 - 1 = 0xFF.
580 */
581 ctx->Stencil.ValueMask[0] = 0xFF;
582 ctx->Stencil.ValueMask[1] = 0xFF;
583 ctx->Stencil.ValueMask[2] = 0xFF;
584 ctx->Stencil.WriteMask[0] = 0xFF;
585 ctx->Stencil.WriteMask[1] = 0xFF;
586 ctx->Stencil.WriteMask[2] = 0xFF;
587
588 ctx->Stencil.Clear = 0;
589 ctx->Stencil._BackFace = 1;
590 }