Debian Codegraph @

debian/external_deps/fetk/sg/src/gl/stencil.c

raw
1 /* $Id: stencil.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.2
6 * Copyright (C) 1995-1997 Brian Paul
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include "context.h"
26 #include "dlist.h"
27 #include "macros.h"
28 #include "pb.h"
29 #include "stencil.h"
30 #include "types.h"
31
32
33
34 /*
35 * Return the address of a stencil buffer value given the window coords:
36 */
37 #define STENCIL_ADDRESS(X,Y) (ctx->Buffer->Stencil + ctx->Buffer->Width * (Y) + (X))
38
39
40 void gl_ClearStencil( GLcontext *ctx, GLint s )
41 {
42 if (INSIDE_BEGIN_END(ctx)) {
43 gl_error( ctx, GL_INVALID_OPERATION, "glClearStencil" );
44 return;
45 }
46 ctx->Stencil.Clear = (GLstencil) s;
47 }
48
49
50
51 void gl_StencilFunc( GLcontext *ctx, GLenum func, GLint ref, GLuint mask )
52 {
53 GLint maxref;
54
55 if (INSIDE_BEGIN_END(ctx)) {
56 gl_error( ctx, GL_INVALID_OPERATION, "glStencilFunc" );
57 return;
58 }
59
60 switch (func) {
61 case GL_NEVER:
62 case GL_LESS:
63 case GL_LEQUAL:
64 case GL_GREATER:
65 case GL_GEQUAL:
66 case GL_EQUAL:
67 case GL_NOTEQUAL:
68 case GL_ALWAYS:
69 ctx->Stencil.Function = func;
70 break;
71 default:
72 gl_error( ctx, GL_INVALID_ENUM, "glStencilFunc" );
73 return;
74 }
75
76 maxref = (1 << STENCIL_BITS) - 1;
77 ctx->Stencil.Ref = CLAMP( ref, 0, maxref );
78 ctx->Stencil.ValueMask = mask;
79 }
80
81
82
83 void gl_StencilMask( GLcontext *ctx, GLuint mask )
84 {
85 if (INSIDE_BEGIN_END(ctx)) {
86 gl_error( ctx, GL_INVALID_OPERATION, "glStencilMask" );
87 return;
88 }
89 ctx->Stencil.WriteMask = (GLstencil) mask;
90 }
91
92
93
94 void gl_StencilOp( GLcontext *ctx, GLenum fail, GLenum zfail, GLenum zpass )
95 {
96 if (INSIDE_BEGIN_END(ctx)) {
97 gl_error( ctx, GL_INVALID_OPERATION, "glStencilOp" );
98 return;
99 }
100 switch (fail) {
101 case GL_KEEP:
102 case GL_ZERO:
103 case GL_REPLACE:
104 case GL_INCR:
105 case GL_DECR:
106 case GL_INVERT:
107 ctx->Stencil.FailFunc = fail;
108 break;
109 default:
110 gl_error( ctx, GL_INVALID_ENUM, "glStencilOp" );
111 return;
112 }
113 switch (zfail) {
114 case GL_KEEP:
115 case GL_ZERO:
116 case GL_REPLACE:
117 case GL_INCR:
118 case GL_DECR:
119 case GL_INVERT:
120 ctx->Stencil.ZFailFunc = zfail;
121 break;
122 default:
123 gl_error( ctx, GL_INVALID_ENUM, "glStencilOp" );
124 return;
125 }
126 switch (zpass) {
127 case GL_KEEP:
128 case GL_ZERO:
129 case GL_REPLACE:
130 case GL_INCR:
131 case GL_DECR:
132 case GL_INVERT:
133 ctx->Stencil.ZPassFunc = zpass;
134 break;
135 default:
136 gl_error( ctx, GL_INVALID_ENUM, "glStencilOp" );
137 return;
138 }
139 }
140
141
142
143 /* Stencil Logic:
144
145 IF stencil test fails THEN
146 Don't write the pixel (RGBA,Z)
147 Execute FailOp
148 ELSE
149 Write the pixel
150 ENDIF
151
152 Perform Depth Test
153
154 IF depth test passes OR no depth buffer THEN
155 Execute ZPass
156 Write the pixel
157 ELSE
158 Execute ZFail
159 ENDIF
160
161 */
162
163
164
165
166 /*
167 * Apply the given stencil operator for each pixel in the span whose
168 * mask flag is set.
169 * Input: n - number of pixels in the span
170 * x, y - location of leftmost pixel in the span
171 * oper - the stencil buffer operator
172 * mask - array [n] of flag: 1=apply operator, 0=don't apply operator
173 */
174 static void apply_stencil_op_to_span( GLcontext *ctx,
175 GLuint n, GLint x, GLint y,
176 GLenum oper, GLubyte mask[] )
177 {
178 GLint i;
179 GLstencil s, ref;
180 GLstencil wrtmask, invmask;
181 GLstencil *stencil;
182
183 wrtmask = ctx->Stencil.WriteMask;
184 invmask = ~ctx->Stencil.WriteMask;
185 ref = ctx->Stencil.Ref;
186 stencil = STENCIL_ADDRESS( x, y );
187
188 switch (oper) {
189 case GL_KEEP:
190 /* do nothing */
191 break;
192 case GL_ZERO:
193 if (invmask==0) {
194 for (i=0;i<n;i++) {
195 if (mask[i]) {
196 stencil[i] = 0;
197 }
198 }
199 }
200 else {
201 for (i=0;i<n;i++) {
202 if (mask[i]) {
203 stencil[i] = stencil[i] & invmask;
204 }
205 }
206 }
207 break;
208 case GL_REPLACE:
209 if (invmask==0) {
210 for (i=0;i<n;i++) {
211 if (mask[i]) {
212 stencil[i] = ref;
213 }
214 }
215 }
216 else {
217 for (i=0;i<n;i++) {
218 if (mask[i]) {
219 s = stencil[i];
220 stencil[i] = (invmask & s ) | (wrtmask & ref);
221 }
222 }
223 }
224 break;
225 case GL_INCR:
226 if (invmask==0) {
227 for (i=0;i<n;i++) {
228 if (mask[i]) {
229 s = stencil[i];
230 if (s<0xff) {
231 stencil[i] = s+1;
232 }
233 }
234 }
235 }
236 else {
237 for (i=0;i<n;i++) {
238 if (mask[i]) {
239 /* VERIFY logic of adding 1 to a write-masked value */
240 s = stencil[i];
241 if (s<0xff) {
242 stencil[i] = (invmask & s) | (wrtmask & (s+1));
243 }
244 }
245 }
246 }
247 break;
248 case GL_DECR:
249 if (invmask==0) {
250 for (i=0;i<n;i++) {
251 if (mask[i]) {
252 s = stencil[i];
253 if (s>0) {
254 stencil[i] = s-1;
255 }
256 }
257 }
258 }
259 else {
260 for (i=0;i<n;i++) {
261 if (mask[i]) {
262 /* VERIFY logic of subtracting 1 to a write-masked value */
263 s = stencil[i];
264 if (s>0) {
265 stencil[i] = (invmask & s) | (wrtmask & (s-1));
266 }
267 }
268 }
269 }
270 break;
271 case GL_INVERT:
272 if (invmask==0) {
273 for (i=0;i<n;i++) {
274 if (mask[i]) {
275 s = stencil[i];
276 stencil[i] = ~s;
277 }
278 }
279 }
280 else {
281 for (i=0;i<n;i++) {
282 if (mask[i]) {
283 s = stencil[i];
284 stencil[i] = (invmask & s) | (wrtmask & ~s);
285 }
286 }
287 }
288 break;
289 default:
290 abort();
291 }
292 }
293
294
295
296
297 /*
298 * Apply stencil test to a span of pixels before depth buffering.
299 * Input: n - number of pixels in the span
300 * x, y - coordinate of left-most pixel in the span
301 * mask - array [n] of flag: 0=skip the pixel, 1=stencil the pixel
302 * Output: mask - pixels which fail the stencil test will have their
303 * mask flag set to 0.
304 * Return: 0 = all pixels failed, 1 = zero or more pixels passed.
305 */
306 GLint gl_stencil_span( GLcontext *ctx,
307 GLuint n, GLint x, GLint y, GLubyte mask[] )
308 {
309 GLubyte fail[MAX_WIDTH];
310 GLint allfail = 0;
311 GLuint i;
312 GLstencil r, s;
313 GLstencil *stencil;
314
315 stencil = STENCIL_ADDRESS( x, y );
316
317 /*
318 * Perform stencil test. The results of this operation are stored
319 * in the fail[] array:
320 * IF fail[i] is non-zero THEN
321 * the stencil fail operator is to be applied
322 * ELSE
323 * the stencil fail operator is not to be applied
324 * ENDIF
325 */
326 switch (ctx->Stencil.Function) {
327 case GL_NEVER:
328 /* always fail */
329 for (i=0;i<n;i++) {
330 if (mask[i]) {
331 mask[i] = 0;
332 fail[i] = 1;
333 }
334 else {
335 fail[i] = 0;
336 }
337 }
338 allfail = 1;
339 break;
340 case GL_LESS:
341 r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
342 for (i=0;i<n;i++) {
343 if (mask[i]) {
344 s = stencil[i] & ctx->Stencil.ValueMask;
345 if (r < s) {
346 /* passed */
347 fail[i] = 0;
348 }
349 else {
350 fail[i] = 1;
351 mask[i] = 0;
352 }
353 }
354 else {
355 fail[i] = 0;
356 }
357 }
358 break;
359 case GL_LEQUAL:
360 r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
361 for (i=0;i<n;i++) {
362 if (mask[i]) {
363 s = stencil[i] & ctx->Stencil.ValueMask;
364 if (r <= s) {
365 /* pass */
366 fail[i] = 0;
367 }
368 else {
369 fail[i] = 1;
370 mask[i] = 0;
371 }
372 }
373 else {
374 fail[i] = 0;
375 }
376 }
377 break;
378 case GL_GREATER:
379 r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
380 for (i=0;i<n;i++) {
381 if (mask[i]) {
382 s = stencil[i] & ctx->Stencil.ValueMask;
383 if (r > s) {
384 /* passed */
385 fail[i] = 0;
386 }
387 else {
388 fail[i] = 1;
389 mask[i] = 0;
390 }
391 }
392 else {
393 fail[i] = 0;
394 }
395 }
396 break;
397 case GL_GEQUAL:
398 r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
399 for (i=0;i<n;i++) {
400 if (mask[i]) {
401 s = stencil[i] & ctx->Stencil.ValueMask;
402 if (r >= s) {
403 /* passed */
404 fail[i] = 0;
405 }
406 else {
407 fail[i] = 1;
408 mask[i] = 0;
409 }
410 }
411 else {
412 fail[i] = 0;
413 }
414 }
415 break;
416 case GL_EQUAL:
417 r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
418 for (i=0;i<n;i++) {
419 if (mask[i]) {
420 s = stencil[i] & ctx->Stencil.ValueMask;
421 if (r == s) {
422 /* passed */
423 fail[i] = 0;
424 }
425 else {
426 fail[i] = 1;
427 mask[i] = 0;
428 }
429 }
430 else {
431 fail[i] = 0;
432 }
433 }
434 break;
435 case GL_NOTEQUAL:
436 r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
437 for (i=0;i<n;i++) {
438 if (mask[i]) {
439 s = stencil[i] & ctx->Stencil.ValueMask;
440 if (r != s) {
441 /* passed */
442 fail[i] = 0;
443 }
444 else {
445 fail[i] = 1;
446 mask[i] = 0;
447 }
448 }
449 else {
450 fail[i] = 0;
451 }
452 }
453 break;
454 case GL_ALWAYS:
455 /* always pass */
456 for (i=0;i<n;i++) {
457 fail[i] = 0;
458 }
459 break;
460 default:
461 abort();
462 }
463
464 apply_stencil_op_to_span( ctx, n, x, y, ctx->Stencil.FailFunc, fail );
465
466 return (allfail) ? 0 : 1;
467 }
468
469
470
471
472 /*
473 * Apply the combination depth-buffer/stencil operator to a span of pixels.
474 * Input: n - number of pixels in the span
475 * x, y - location of leftmost pixel in span
476 * z - array [n] of z values
477 * Input: mask - array [n] of flags (1=test this pixel, 0=skip the pixel)
478 * Output: mask - array [n] of flags (1=depth test passed, 0=failed)
479 */
480 void gl_depth_stencil_span( GLcontext *ctx,
481 GLuint n, GLint x, GLint y, const GLdepth z[],
482 GLubyte mask[] )
483 {
484 if (ctx->Depth.Test==GL_FALSE) {
485 /*
486 * No depth buffer, just apply zpass stencil function to active pixels.
487 */
488 apply_stencil_op_to_span( ctx, n, x, y, ctx->Stencil.ZPassFunc, mask );
489 }
490 else {
491 /*
492 * Perform depth buffering, then apply zpass or zfail stencil function.
493 */
494 GLubyte passmask[MAX_WIDTH], failmask[MAX_WIDTH], oldmask[MAX_WIDTH];
495 GLuint i;
496
497 /* init pass and fail masks to zero, copy mask[] to oldmask[] */
498 for (i=0;i<n;i++) {
499 passmask[i] = failmask[i] = 0;
500 oldmask[i] = mask[i];
501 }
502
503 /* apply the depth test */
504 (*ctx->Driver.DepthTestSpan)( ctx, n, x, y, z, mask );
505
506 /* set the stencil pass/fail flags according to result of depth test */
507 for (i=0;i<n;i++) {
508 if (oldmask[i]) {
509 if (mask[i]) {
510 passmask[i] = 1;
511 }
512 else {
513 failmask[i] = 1;
514 }
515 }
516 }
517
518 /* apply the pass and fail operations */
519 apply_stencil_op_to_span( ctx, n, x, y, ctx->Stencil.ZFailFunc, failmask );
520 apply_stencil_op_to_span( ctx, n, x, y, ctx->Stencil.ZPassFunc, passmask );
521 }
522 }
523
524
525
526
527 /*
528 * Apply the given stencil operator for each pixel in the array whose
529 * mask flag is set.
530 * Input: n - number of pixels in the span
531 * x, y - array of [n] pixels
532 * operator - the stencil buffer operator
533 * mask - array [n] of flag: 1=apply operator, 0=don't apply operator
534 */
535 static void apply_stencil_op_to_pixels( GLcontext *ctx,
536 GLuint n, const GLint x[],
537 const GLint y[],
538 GLenum oper, GLubyte mask[] )
539 {
540 GLint i;
541 GLstencil ref;
542 GLstencil wrtmask, invmask;
543
544 wrtmask = ctx->Stencil.WriteMask;
545 invmask = ~ctx->Stencil.WriteMask;
546
547 ref = ctx->Stencil.Ref;
548
549 switch (oper) {
550 case GL_KEEP:
551 /* do nothing */
552 break;
553 case GL_ZERO:
554 if (invmask==0) {
555 for (i=0;i<n;i++) {
556 if (mask[i]) {
557 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
558 *sptr = 0;
559 }
560 }
561 }
562 else {
563 for (i=0;i<n;i++) {
564 if (mask[i]) {
565 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
566 *sptr = invmask & *sptr;
567 }
568 }
569 }
570 break;
571 case GL_REPLACE:
572 if (invmask==0) {
573 for (i=0;i<n;i++) {
574 if (mask[i]) {
575 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
576 *sptr = ref;
577 }
578 }
579 }
580 else {
581 for (i=0;i<n;i++) {
582 if (mask[i]) {
583 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
584 *sptr = (invmask & *sptr ) | (wrtmask & ref);
585 }
586 }
587 }
588 break;
589 case GL_INCR:
590 if (invmask==0) {
591 for (i=0;i<n;i++) {
592 if (mask[i]) {
593 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
594 if (*sptr < 0xff) {
595 *sptr = *sptr + 1;
596 }
597 }
598 }
599 }
600 else {
601 for (i=0;i<n;i++) {
602 if (mask[i]) {
603 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
604 if (*sptr<0xff) {
605 *sptr = (invmask & *sptr) | (wrtmask & (*sptr+1));
606 }
607 }
608 }
609 }
610 break;
611 case GL_DECR:
612 if (invmask==0) {
613 for (i=0;i<n;i++) {
614 if (mask[i]) {
615 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
616 if (*sptr>0) {
617 *sptr = *sptr - 1;
618 }
619 }
620 }
621 }
622 else {
623 for (i=0;i<n;i++) {
624 if (mask[i]) {
625 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
626 if (*sptr>0) {
627 *sptr = (invmask & *sptr) | (wrtmask & (*sptr-1));
628 }
629 }
630 }
631 }
632 break;
633 case GL_INVERT:
634 if (invmask==0) {
635 for (i=0;i<n;i++) {
636 if (mask[i]) {
637 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
638 *sptr = ~*sptr;
639 }
640 }
641 }
642 else {
643 for (i=0;i<n;i++) {
644 if (mask[i]) {
645 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
646 *sptr = (invmask & *sptr) | (wrtmask & ~*sptr);
647 }
648 }
649 }
650 break;
651 default:
652 abort();
653 return;
654 }
655 }
656
657
658
659 /*
660 * Apply stencil test to an array of pixels before depth buffering.
661 * Input: n - number of pixels in the span
662 * x, y - array of [n] pixels to stencil
663 * mask - array [n] of flag: 0=skip the pixel, 1=stencil the pixel
664 * Output: mask - pixels which fail the stencil test will have their
665 * mask flag set to 0.
666 * Return: 0 = all pixels failed, 1 = zero or more pixels passed.
667 */
668 GLint gl_stencil_pixels( GLcontext *ctx,
669 GLuint n, const GLint x[], const GLint y[],
670 GLubyte mask[] )
671 {
672 GLubyte fail[PB_SIZE];
673 GLstencil r, s;
674 GLuint i;
675 GLint allfail = 0;
676
677 /*
678 * Perform stencil test. The results of this operation are stored
679 * in the fail[] array:
680 * IF fail[i] is non-zero THEN
681 * the stencil fail operator is to be applied
682 * ELSE
683 * the stencil fail operator is not to be applied
684 * ENDIF
685 */
686
687 switch (ctx->Stencil.Function) {
688 case GL_NEVER:
689 /* always fail */
690 for (i=0;i<n;i++) {
691 if (mask[i]) {
692 mask[i] = 0;
693 fail[i] = 1;
694 }
695 else {
696 fail[i] = 0;
697 }
698 }
699 allfail = 1;
700 break;
701 case GL_LESS:
702 r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
703 for (i=0;i<n;i++) {
704 if (mask[i]) {
705 GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
706 s = *sptr & ctx->Stencil.ValueMask;
707 if (r < s) {
708 /* passed */
709 fail[i] = 0;
710 }
711 else {
712 fail[i] = 1;
713 mask[i] = 0;
714 }
715 }
716 else {
717 fail[i] = 0;
718 }
719 }
720 break;
721 case GL_LEQUAL:
722 r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
723 for (i=0;i<n;i++) {
724 if (mask[i]) {
725 GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
726 s = *sptr & ctx->Stencil.ValueMask;
727 if (r <= s) {
728 /* pass */
729 fail[i] = 0;
730 }
731 else {
732 fail[i] = 1;
733 mask[i] = 0;
734 }
735 }
736 else {
737 fail[i] = 0;
738 }
739 }
740 break;
741 case GL_GREATER:
742 r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
743 for (i=0;i<n;i++) {
744 if (mask[i]) {
745 GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
746 s = *sptr & ctx->Stencil.ValueMask;
747 if (r > s) {
748 /* passed */
749 fail[i] = 0;
750 }
751 else {
752 fail[i] = 1;
753 mask[i] = 0;
754 }
755 }
756 else {
757 fail[i] = 0;
758 }
759 }
760 break;
761 case GL_GEQUAL:
762 r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
763 for (i=0;i<n;i++) {
764 if (mask[i]) {
765 GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
766 s = *sptr & ctx->Stencil.ValueMask;
767 if (r >= s) {
768 /* passed */
769 fail[i] = 0;
770 }
771 else {
772 fail[i] = 1;
773 mask[i] = 0;
774 }
775 }
776 else {
777 fail[i] = 0;
778 }
779 }
780 break;
781 case GL_EQUAL:
782 r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
783 for (i=0;i<n;i++) {
784 if (mask[i]) {
785 GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
786 s = *sptr & ctx->Stencil.ValueMask;
787 if (r == s) {
788 /* passed */
789 fail[i] = 0;
790 }
791 else {
792 fail[i] = 1;
793 mask[i] = 0;
794 }
795 }
796 else {
797 fail[i] = 0;
798 }
799 }
800 break;
801 case GL_NOTEQUAL:
802 r = ctx->Stencil.Ref & ctx->Stencil.ValueMask;
803 for (i=0;i<n;i++) {
804 if (mask[i]) {
805 GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
806 s = *sptr & ctx->Stencil.ValueMask;
807 if (r != s) {
808 /* passed */
809 fail[i] = 0;
810 }
811 else {
812 fail[i] = 1;
813 mask[i] = 0;
814 }
815 }
816 else {
817 fail[i] = 0;
818 }
819 }
820 break;
821 case GL_ALWAYS:
822 /* always pass */
823 for (i=0;i<n;i++) {
824 fail[i] = 0;
825 }
826 break;
827 default:
828 abort();
829 }
830
831 apply_stencil_op_to_pixels( ctx, n, x, y, ctx->Stencil.FailFunc, fail );
832
833 return (allfail) ? 0 : 1;
834 }
835
836
837
838
839 /*
840 * Apply the combination depth-buffer/stencil operator to a span of pixels.
841 * Input: n - number of pixels in the span
842 * x, y - array of [n] pixels to stencil
843 * z - array [n] of z values
844 * Input: mask - array [n] of flags (1=test this pixel, 0=skip the pixel)
845 * Output: mask - array [n] of flags (1=depth test passed, 0=failed)
846 */
847 void gl_depth_stencil_pixels( GLcontext *ctx,
848 GLuint n, const GLint x[], const GLint y[],
849 const GLdepth z[], GLubyte mask[] )
850 {
851 if (ctx->Depth.Test==GL_FALSE) {
852 /*
853 * No depth buffer, just apply zpass stencil function to active pixels.
854 */
855 apply_stencil_op_to_pixels( ctx, n, x, y, ctx->Stencil.ZPassFunc, mask );
856 }
857 else {
858 /*
859 * Perform depth buffering, then apply zpass or zfail stencil function.
860 */
861 GLubyte passmask[PB_SIZE], failmask[PB_SIZE], oldmask[PB_SIZE];
862 GLuint i;
863
864 /* init pass and fail masks to zero */
865 for (i=0;i<n;i++) {
866 passmask[i] = failmask[i] = 0;
867 oldmask[i] = mask[i];
868 }
869
870 /* apply the depth test */
871 (*ctx->Driver.DepthTestPixels)( ctx, n, x, y, z, mask );
872
873 /* set the stencil pass/fail flags according to result of depth test */
874 for (i=0;i<n;i++) {
875 if (oldmask[i]) {
876 if (mask[i]) {
877 passmask[i] = 1;
878 }
879 else {
880 failmask[i] = 1;
881 }
882 }
883 }
884
885 /* apply the pass and fail operations */
886 apply_stencil_op_to_pixels( ctx, n, x, y,
887 ctx->Stencil.ZFailFunc, failmask );
888 apply_stencil_op_to_pixels( ctx, n, x, y,
889 ctx->Stencil.ZPassFunc, passmask );
890 }
891
892 }
893
894
895
896 /*
897 * Return a span of stencil values from the stencil buffer.
898 * Input: n - how many pixels
899 * x,y - location of first pixel
900 * Output: stencil - the array of stencil values
901 */
902 void gl_read_stencil_span( GLcontext *ctx,
903 GLuint n, GLint x, GLint y, GLubyte stencil[] )
904 {
905 GLstencil *s;
906
907 if (ctx->Buffer->Stencil) {
908 s = STENCIL_ADDRESS( x, y );
909 MEMCPY( stencil, s, n * sizeof(GLubyte) );
910 }
911 }
912
913
914
915 /*
916 * Write a span of stencil values to the stencil buffer.
917 * Input: n - how many pixels
918 * x,y - location of first pixel
919 * stencil - the array of stencil values
920 */
921 void gl_write_stencil_span( GLcontext *ctx,
922 GLuint n, GLint x, GLint y,
923 const GLubyte stencil[] )
924 {
925 GLstencil *s;
926
927 if (ctx->Buffer->Stencil) {
928 s = STENCIL_ADDRESS( x, y );
929 MEMCPY( s, stencil, n * sizeof(GLubyte) );
930 }
931 }
932
933
934
935 /*
936 * Allocate a new stencil buffer. If there's an old one it will be
937 * deallocated first. The new stencil buffer will be uninitialized.
938 */
939 void gl_alloc_stencil_buffer( GLcontext *ctx )
940 {
941 GLuint buffersize = ctx->Buffer->Width * ctx->Buffer->Height;
942
943 /* deallocate current stencil buffer if present */
944 if (ctx->Buffer->Stencil) {
945 free(ctx->Buffer->Stencil);
946 ctx->Buffer->Stencil = NULL;
947 }
948
949 /* allocate new stencil buffer */
950 ctx->Buffer->Stencil = (GLstencil *) malloc(buffersize * sizeof(GLstencil));
951 if (!ctx->Buffer->Stencil) {
952 /* out of memory */
953 ctx->Stencil.Enabled = GL_FALSE;
954 gl_error( ctx, GL_OUT_OF_MEMORY, "gl_alloc_stencil_buffer" );
955 }
956 }
957
958
959
960
961 /*
962 * Clear the stencil buffer. If the stencil buffer doesn't exist yet we'll
963 * allocate it now.
964 */
965 void gl_clear_stencil_buffer( GLcontext *ctx )
966 {
967 if (ctx->Visual->StencilBits==0 || !ctx->Buffer->Stencil) {
968 /* no stencil buffer */
969 return;
970 }
971
972 if (ctx->Scissor.Enabled) {
973 /* clear scissor region only */
974 GLint y;
975 GLint width = ctx->Buffer->Xmax - ctx->Buffer->Xmin + 1;
976 for (y=ctx->Buffer->Ymin; y<=ctx->Buffer->Ymax; y++) {
977 GLstencil *ptr = STENCIL_ADDRESS( ctx->Buffer->Xmin, y );
978 MEMSET( ptr, ctx->Stencil.Clear, width * sizeof(GLstencil) );
979 }
980 }
981 else {
982 /* clear whole stencil buffer */
983 MEMSET( ctx->Buffer->Stencil, ctx->Stencil.Clear,
984 ctx->Buffer->Width * ctx->Buffer->Height * sizeof(GLstencil) );
985 }
986 }