OpenGL 2.0's two-sided stencil feature wasn't implemented correctly.
[mesa.git] / src / mesa / swrast / s_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 #include "glheader.h"
27 #include "context.h"
28 #include "macros.h"
29 #include "imports.h"
30 #include "fbobject.h"
31
32 #include "s_context.h"
33 #include "s_depth.h"
34 #include "s_stencil.h"
35
36
37
38 /* Stencil Logic:
39
40 IF stencil test fails THEN
41 Apply fail-op to stencil value
42 Don't write the pixel (RGBA,Z)
43 ELSE
44 IF doing depth test && depth test fails THEN
45 Apply zfail-op to stencil value
46 Write RGBA and Z to appropriate buffers
47 ELSE
48 Apply zpass-op to stencil value
49 ENDIF
50
51 */
52
53
54 /**
55 * Apply the given stencil operator to the array of stencil values.
56 * Don't touch stencil[i] if mask[i] is zero.
57 * Input: n - size of stencil array
58 * oper - the stencil buffer operator
59 * face - 0 or 1 for front or back face operation
60 * stencil - array of stencil values
61 * mask - array [n] of flag: 1=apply operator, 0=don't apply operator
62 * Output: stencil - modified values
63 */
64 static void
65 apply_stencil_op( const GLcontext *ctx, GLenum oper, GLuint face,
66 GLuint n, GLstencil stencil[], const GLubyte mask[] )
67 {
68 const GLstencil ref = ctx->Stencil.Ref[face];
69 const GLstencil wrtmask = ctx->Stencil.WriteMask[face];
70 const GLstencil invmask = (GLstencil) (~wrtmask);
71 GLuint i;
72
73 switch (oper) {
74 case GL_KEEP:
75 /* do nothing */
76 break;
77 case GL_ZERO:
78 if (invmask==0) {
79 for (i=0;i<n;i++) {
80 if (mask[i]) {
81 stencil[i] = 0;
82 }
83 }
84 }
85 else {
86 for (i=0;i<n;i++) {
87 if (mask[i]) {
88 stencil[i] = (GLstencil) (stencil[i] & invmask);
89 }
90 }
91 }
92 break;
93 case GL_REPLACE:
94 if (invmask==0) {
95 for (i=0;i<n;i++) {
96 if (mask[i]) {
97 stencil[i] = ref;
98 }
99 }
100 }
101 else {
102 for (i=0;i<n;i++) {
103 if (mask[i]) {
104 GLstencil s = stencil[i];
105 stencil[i] = (GLstencil) ((invmask & s ) | (wrtmask & ref));
106 }
107 }
108 }
109 break;
110 case GL_INCR:
111 if (invmask==0) {
112 for (i=0;i<n;i++) {
113 if (mask[i]) {
114 GLstencil s = stencil[i];
115 if (s < STENCIL_MAX) {
116 stencil[i] = (GLstencil) (s+1);
117 }
118 }
119 }
120 }
121 else {
122 for (i=0;i<n;i++) {
123 if (mask[i]) {
124 /* VERIFY logic of adding 1 to a write-masked value */
125 GLstencil s = stencil[i];
126 if (s < STENCIL_MAX) {
127 stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & (s+1)));
128 }
129 }
130 }
131 }
132 break;
133 case GL_DECR:
134 if (invmask==0) {
135 for (i=0;i<n;i++) {
136 if (mask[i]) {
137 GLstencil s = stencil[i];
138 if (s>0) {
139 stencil[i] = (GLstencil) (s-1);
140 }
141 }
142 }
143 }
144 else {
145 for (i=0;i<n;i++) {
146 if (mask[i]) {
147 /* VERIFY logic of subtracting 1 to a write-masked value */
148 GLstencil s = stencil[i];
149 if (s>0) {
150 stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & (s-1)));
151 }
152 }
153 }
154 }
155 break;
156 case GL_INCR_WRAP_EXT:
157 if (invmask==0) {
158 for (i=0;i<n;i++) {
159 if (mask[i]) {
160 stencil[i]++;
161 }
162 }
163 }
164 else {
165 for (i=0;i<n;i++) {
166 if (mask[i]) {
167 GLstencil s = stencil[i];
168 stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & (s+1)));
169 }
170 }
171 }
172 break;
173 case GL_DECR_WRAP_EXT:
174 if (invmask==0) {
175 for (i=0;i<n;i++) {
176 if (mask[i]) {
177 stencil[i]--;
178 }
179 }
180 }
181 else {
182 for (i=0;i<n;i++) {
183 if (mask[i]) {
184 GLstencil s = stencil[i];
185 stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & (s-1)));
186 }
187 }
188 }
189 break;
190 case GL_INVERT:
191 if (invmask==0) {
192 for (i=0;i<n;i++) {
193 if (mask[i]) {
194 GLstencil s = stencil[i];
195 stencil[i] = (GLstencil) ~s;
196 }
197 }
198 }
199 else {
200 for (i=0;i<n;i++) {
201 if (mask[i]) {
202 GLstencil s = stencil[i];
203 stencil[i] = (GLstencil) ((invmask & s) | (wrtmask & ~s));
204 }
205 }
206 }
207 break;
208 default:
209 _mesa_problem(ctx, "Bad stencil op in apply_stencil_op");
210 }
211 }
212
213
214
215
216 /**
217 * Apply stencil test to an array of stencil values (before depth buffering).
218 * Input: face - 0 or 1 for front or back-face polygons
219 * n - number of pixels in the array
220 * stencil - array of [n] stencil values
221 * mask - array [n] of flag: 0=skip the pixel, 1=stencil the pixel
222 * Output: mask - pixels which fail the stencil test will have their
223 * mask flag set to 0.
224 * stencil - updated stencil values (where the test passed)
225 * Return: GL_FALSE = all pixels failed, GL_TRUE = zero or more pixels passed.
226 */
227 static GLboolean
228 do_stencil_test( GLcontext *ctx, GLuint face, GLuint n, GLstencil stencil[],
229 GLubyte mask[] )
230 {
231 GLubyte fail[MAX_WIDTH];
232 GLboolean allfail = GL_FALSE;
233 GLuint i;
234 GLstencil r, s;
235 const GLuint valueMask = ctx->Stencil.ValueMask[face];
236
237 ASSERT(n <= MAX_WIDTH);
238
239 /*
240 * Perform stencil test. The results of this operation are stored
241 * in the fail[] array:
242 * IF fail[i] is non-zero THEN
243 * the stencil fail operator is to be applied
244 * ELSE
245 * the stencil fail operator is not to be applied
246 * ENDIF
247 */
248 switch (ctx->Stencil.Function[face]) {
249 case GL_NEVER:
250 /* never pass; always fail */
251 for (i=0;i<n;i++) {
252 if (mask[i]) {
253 mask[i] = 0;
254 fail[i] = 1;
255 }
256 else {
257 fail[i] = 0;
258 }
259 }
260 allfail = GL_TRUE;
261 break;
262 case GL_LESS:
263 r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
264 for (i=0;i<n;i++) {
265 if (mask[i]) {
266 s = (GLstencil) (stencil[i] & valueMask);
267 if (r < s) {
268 /* passed */
269 fail[i] = 0;
270 }
271 else {
272 fail[i] = 1;
273 mask[i] = 0;
274 }
275 }
276 else {
277 fail[i] = 0;
278 }
279 }
280 break;
281 case GL_LEQUAL:
282 r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
283 for (i=0;i<n;i++) {
284 if (mask[i]) {
285 s = (GLstencil) (stencil[i] & valueMask);
286 if (r <= s) {
287 /* pass */
288 fail[i] = 0;
289 }
290 else {
291 fail[i] = 1;
292 mask[i] = 0;
293 }
294 }
295 else {
296 fail[i] = 0;
297 }
298 }
299 break;
300 case GL_GREATER:
301 r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
302 for (i=0;i<n;i++) {
303 if (mask[i]) {
304 s = (GLstencil) (stencil[i] & valueMask);
305 if (r > s) {
306 /* passed */
307 fail[i] = 0;
308 }
309 else {
310 fail[i] = 1;
311 mask[i] = 0;
312 }
313 }
314 else {
315 fail[i] = 0;
316 }
317 }
318 break;
319 case GL_GEQUAL:
320 r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
321 for (i=0;i<n;i++) {
322 if (mask[i]) {
323 s = (GLstencil) (stencil[i] & valueMask);
324 if (r >= s) {
325 /* passed */
326 fail[i] = 0;
327 }
328 else {
329 fail[i] = 1;
330 mask[i] = 0;
331 }
332 }
333 else {
334 fail[i] = 0;
335 }
336 }
337 break;
338 case GL_EQUAL:
339 r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
340 for (i=0;i<n;i++) {
341 if (mask[i]) {
342 s = (GLstencil) (stencil[i] & valueMask);
343 if (r == s) {
344 /* passed */
345 fail[i] = 0;
346 }
347 else {
348 fail[i] = 1;
349 mask[i] = 0;
350 }
351 }
352 else {
353 fail[i] = 0;
354 }
355 }
356 break;
357 case GL_NOTEQUAL:
358 r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
359 for (i=0;i<n;i++) {
360 if (mask[i]) {
361 s = (GLstencil) (stencil[i] & valueMask);
362 if (r != s) {
363 /* passed */
364 fail[i] = 0;
365 }
366 else {
367 fail[i] = 1;
368 mask[i] = 0;
369 }
370 }
371 else {
372 fail[i] = 0;
373 }
374 }
375 break;
376 case GL_ALWAYS:
377 /* always pass */
378 for (i=0;i<n;i++) {
379 fail[i] = 0;
380 }
381 break;
382 default:
383 _mesa_problem(ctx, "Bad stencil func in gl_stencil_span");
384 return 0;
385 }
386
387 if (ctx->Stencil.FailFunc[face] != GL_KEEP) {
388 apply_stencil_op( ctx, ctx->Stencil.FailFunc[face], face, n, stencil, fail );
389 }
390
391 return !allfail;
392 }
393
394
395
396 /**
397 * Apply stencil and depth testing to the span of pixels.
398 * Both software and hardware stencil buffers are acceptable.
399 * Input: n - number of pixels in the span
400 * x, y - location of leftmost pixel in span
401 * z - array [n] of z values
402 * mask - array [n] of flags (1=test this pixel, 0=skip the pixel)
403 * Output: mask - array [n] of flags (1=stencil and depth test passed)
404 * Return: GL_FALSE - all fragments failed the testing
405 * GL_TRUE - one or more fragments passed the testing
406 *
407 */
408 static GLboolean
409 stencil_and_ztest_span(GLcontext *ctx, struct sw_span *span, GLuint face)
410 {
411 struct gl_framebuffer *fb = ctx->DrawBuffer;
412 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
413 GLstencil stencilRow[MAX_WIDTH];
414 GLstencil *stencil;
415 const GLuint n = span->end;
416 const GLint x = span->x;
417 const GLint y = span->y;
418 GLubyte *mask = span->array->mask;
419
420 ASSERT((span->arrayMask & SPAN_XY) == 0);
421 ASSERT(ctx->Stencil.Enabled);
422 ASSERT(n <= MAX_WIDTH);
423 #ifdef DEBUG
424 if (ctx->Depth.Test) {
425 ASSERT(span->arrayMask & SPAN_Z);
426 }
427 #endif
428
429 stencil = rb->GetPointer(ctx, rb, x, y);
430 if (!stencil) {
431 rb->GetRow(ctx, rb, n, x, y, stencilRow);
432 stencil = stencilRow;
433 }
434
435 /*
436 * Apply the stencil test to the fragments.
437 * failMask[i] is 1 if the stencil test failed.
438 */
439 if (do_stencil_test( ctx, face, n, stencil, mask ) == GL_FALSE) {
440 /* all fragments failed the stencil test, we're done. */
441 span->writeAll = GL_FALSE;
442 return GL_FALSE;
443 }
444
445 /*
446 * Some fragments passed the stencil test, apply depth test to them
447 * and apply Zpass and Zfail stencil ops.
448 */
449 if (ctx->Depth.Test == GL_FALSE) {
450 /*
451 * No depth buffer, just apply zpass stencil function to active pixels.
452 */
453 apply_stencil_op( ctx, ctx->Stencil.ZPassFunc[face], face, n, stencil, mask );
454 }
455 else {
456 /*
457 * Perform depth buffering, then apply zpass or zfail stencil function.
458 */
459 GLubyte passmask[MAX_WIDTH], failmask[MAX_WIDTH], oldmask[MAX_WIDTH];
460 GLuint i;
461
462 /* save the current mask bits */
463 MEMCPY(oldmask, mask, n * sizeof(GLubyte));
464
465 /* apply the depth test */
466 _swrast_depth_test_span(ctx, span);
467
468 /* Set the stencil pass/fail flags according to result of depth testing.
469 * if oldmask[i] == 0 then
470 * Don't touch the stencil value
471 * else if oldmask[i] and newmask[i] then
472 * Depth test passed
473 * else
474 * assert(oldmask[i] && !newmask[i])
475 * Depth test failed
476 * endif
477 */
478 for (i=0;i<n;i++) {
479 ASSERT(mask[i] == 0 || mask[i] == 1);
480 passmask[i] = oldmask[i] & mask[i];
481 failmask[i] = oldmask[i] & (mask[i] ^ 1);
482 }
483
484 /* apply the pass and fail operations */
485 if (ctx->Stencil.ZFailFunc[face] != GL_KEEP) {
486 apply_stencil_op( ctx, ctx->Stencil.ZFailFunc[face], face,
487 n, stencil, failmask );
488 }
489 if (ctx->Stencil.ZPassFunc[face] != GL_KEEP) {
490 apply_stencil_op( ctx, ctx->Stencil.ZPassFunc[face], face,
491 n, stencil, passmask );
492 }
493 }
494
495 /*
496 * Write updated stencil values back into hardware stencil buffer.
497 */
498 if (!rb->GetPointer(ctx, rb, 0, 0)) {
499 rb->PutRow(ctx, rb, n, x, y, stencil, mask);
500 }
501
502 span->writeAll = GL_FALSE;
503
504 return GL_TRUE; /* one or more fragments passed both tests */
505 }
506
507
508
509 /*
510 * Return the address of a stencil buffer value given the window coords:
511 */
512 #define STENCIL_ADDRESS(X, Y) (stencilStart + (Y) * stride + (X))
513
514
515
516 /**
517 * Apply the given stencil operator for each pixel in the array whose
518 * mask flag is set.
519 * \note This is for software stencil buffers only.
520 * Input: n - number of pixels in the span
521 * x, y - array of [n] pixels
522 * operator - the stencil buffer operator
523 * mask - array [n] of flag: 1=apply operator, 0=don't apply operator
524 */
525 static void
526 apply_stencil_op_to_pixels( GLcontext *ctx,
527 GLuint n, const GLint x[], const GLint y[],
528 GLenum oper, GLuint face, const GLubyte mask[] )
529 {
530 struct gl_framebuffer *fb = ctx->DrawBuffer;
531 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
532 const GLstencil ref = ctx->Stencil.Ref[face];
533 const GLstencil wrtmask = ctx->Stencil.WriteMask[face];
534 const GLstencil invmask = (GLstencil) (~wrtmask);
535 GLuint i;
536 GLstencil *stencilStart = (GLubyte *) rb->Data;
537 const GLuint stride = rb->Width;
538
539 ASSERT(rb->GetPointer(ctx, rb, 0, 0));
540 ASSERT(sizeof(GLstencil) == 1);
541
542 switch (oper) {
543 case GL_KEEP:
544 /* do nothing */
545 break;
546 case GL_ZERO:
547 if (invmask==0) {
548 for (i=0;i<n;i++) {
549 if (mask[i]) {
550 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
551 *sptr = 0;
552 }
553 }
554 }
555 else {
556 for (i=0;i<n;i++) {
557 if (mask[i]) {
558 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
559 *sptr = (GLstencil) (invmask & *sptr);
560 }
561 }
562 }
563 break;
564 case GL_REPLACE:
565 if (invmask==0) {
566 for (i=0;i<n;i++) {
567 if (mask[i]) {
568 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
569 *sptr = ref;
570 }
571 }
572 }
573 else {
574 for (i=0;i<n;i++) {
575 if (mask[i]) {
576 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
577 *sptr = (GLstencil) ((invmask & *sptr ) | (wrtmask & ref));
578 }
579 }
580 }
581 break;
582 case GL_INCR:
583 if (invmask==0) {
584 for (i=0;i<n;i++) {
585 if (mask[i]) {
586 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
587 if (*sptr < STENCIL_MAX) {
588 *sptr = (GLstencil) (*sptr + 1);
589 }
590 }
591 }
592 }
593 else {
594 for (i=0;i<n;i++) {
595 if (mask[i]) {
596 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
597 if (*sptr < STENCIL_MAX) {
598 *sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & (*sptr+1)));
599 }
600 }
601 }
602 }
603 break;
604 case GL_DECR:
605 if (invmask==0) {
606 for (i=0;i<n;i++) {
607 if (mask[i]) {
608 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
609 if (*sptr>0) {
610 *sptr = (GLstencil) (*sptr - 1);
611 }
612 }
613 }
614 }
615 else {
616 for (i=0;i<n;i++) {
617 if (mask[i]) {
618 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
619 if (*sptr>0) {
620 *sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & (*sptr-1)));
621 }
622 }
623 }
624 }
625 break;
626 case GL_INCR_WRAP_EXT:
627 if (invmask==0) {
628 for (i=0;i<n;i++) {
629 if (mask[i]) {
630 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
631 *sptr = (GLstencil) (*sptr + 1);
632 }
633 }
634 }
635 else {
636 for (i=0;i<n;i++) {
637 if (mask[i]) {
638 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
639 *sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & (*sptr+1)));
640 }
641 }
642 }
643 break;
644 case GL_DECR_WRAP_EXT:
645 if (invmask==0) {
646 for (i=0;i<n;i++) {
647 if (mask[i]) {
648 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
649 *sptr = (GLstencil) (*sptr - 1);
650 }
651 }
652 }
653 else {
654 for (i=0;i<n;i++) {
655 if (mask[i]) {
656 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
657 *sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & (*sptr-1)));
658 }
659 }
660 }
661 break;
662 case GL_INVERT:
663 if (invmask==0) {
664 for (i=0;i<n;i++) {
665 if (mask[i]) {
666 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
667 *sptr = (GLstencil) (~*sptr);
668 }
669 }
670 }
671 else {
672 for (i=0;i<n;i++) {
673 if (mask[i]) {
674 GLstencil *sptr = STENCIL_ADDRESS( x[i], y[i] );
675 *sptr = (GLstencil) ((invmask & *sptr) | (wrtmask & ~*sptr));
676 }
677 }
678 }
679 break;
680 default:
681 _mesa_problem(ctx, "Bad stencilop in apply_stencil_op_to_pixels");
682 }
683 }
684
685
686
687 /**
688 * Apply stencil test to an array of pixels before depth buffering.
689 *
690 * \note Used for software stencil buffer only.
691 * Input: n - number of pixels in the span
692 * x, y - array of [n] pixels to stencil
693 * mask - array [n] of flag: 0=skip the pixel, 1=stencil the pixel
694 * Output: mask - pixels which fail the stencil test will have their
695 * mask flag set to 0.
696 * \return GL_FALSE = all pixels failed, GL_TRUE = zero or more pixels passed.
697 */
698 static GLboolean
699 stencil_test_pixels( GLcontext *ctx, GLuint face, GLuint n,
700 const GLint x[], const GLint y[], GLubyte mask[] )
701 {
702 const struct gl_framebuffer *fb = ctx->DrawBuffer;
703 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
704 GLubyte fail[MAX_WIDTH];
705 GLstencil r, s;
706 GLuint i;
707 GLboolean allfail = GL_FALSE;
708 const GLuint valueMask = ctx->Stencil.ValueMask[face];
709 const GLstencil *stencilStart = (GLstencil *) rb->Data;
710 const GLuint stride = rb->Width;
711
712 ASSERT(rb->GetPointer(ctx, rb, 0, 0));
713 ASSERT(sizeof(GLstencil) == 1);
714
715 /*
716 * Perform stencil test. The results of this operation are stored
717 * in the fail[] array:
718 * IF fail[i] is non-zero THEN
719 * the stencil fail operator is to be applied
720 * ELSE
721 * the stencil fail operator is not to be applied
722 * ENDIF
723 */
724
725 switch (ctx->Stencil.Function[face]) {
726 case GL_NEVER:
727 /* always fail */
728 for (i=0;i<n;i++) {
729 if (mask[i]) {
730 mask[i] = 0;
731 fail[i] = 1;
732 }
733 else {
734 fail[i] = 0;
735 }
736 }
737 allfail = GL_TRUE;
738 break;
739 case GL_LESS:
740 r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
741 for (i=0;i<n;i++) {
742 if (mask[i]) {
743 const GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
744 s = (GLstencil) (*sptr & valueMask);
745 if (r < s) {
746 /* passed */
747 fail[i] = 0;
748 }
749 else {
750 fail[i] = 1;
751 mask[i] = 0;
752 }
753 }
754 else {
755 fail[i] = 0;
756 }
757 }
758 break;
759 case GL_LEQUAL:
760 r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
761 for (i=0;i<n;i++) {
762 if (mask[i]) {
763 const GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
764 s = (GLstencil) (*sptr & valueMask);
765 if (r <= s) {
766 /* pass */
767 fail[i] = 0;
768 }
769 else {
770 fail[i] = 1;
771 mask[i] = 0;
772 }
773 }
774 else {
775 fail[i] = 0;
776 }
777 }
778 break;
779 case GL_GREATER:
780 r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
781 for (i=0;i<n;i++) {
782 if (mask[i]) {
783 const GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
784 s = (GLstencil) (*sptr & valueMask);
785 if (r > s) {
786 /* passed */
787 fail[i] = 0;
788 }
789 else {
790 fail[i] = 1;
791 mask[i] = 0;
792 }
793 }
794 else {
795 fail[i] = 0;
796 }
797 }
798 break;
799 case GL_GEQUAL:
800 r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
801 for (i=0;i<n;i++) {
802 if (mask[i]) {
803 const GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
804 s = (GLstencil) (*sptr & valueMask);
805 if (r >= s) {
806 /* passed */
807 fail[i] = 0;
808 }
809 else {
810 fail[i] = 1;
811 mask[i] = 0;
812 }
813 }
814 else {
815 fail[i] = 0;
816 }
817 }
818 break;
819 case GL_EQUAL:
820 r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
821 for (i=0;i<n;i++) {
822 if (mask[i]) {
823 const GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
824 s = (GLstencil) (*sptr & valueMask);
825 if (r == s) {
826 /* passed */
827 fail[i] = 0;
828 }
829 else {
830 fail[i] = 1;
831 mask[i] = 0;
832 }
833 }
834 else {
835 fail[i] = 0;
836 }
837 }
838 break;
839 case GL_NOTEQUAL:
840 r = (GLstencil) (ctx->Stencil.Ref[face] & valueMask);
841 for (i=0;i<n;i++) {
842 if (mask[i]) {
843 const GLstencil *sptr = STENCIL_ADDRESS(x[i],y[i]);
844 s = (GLstencil) (*sptr & valueMask);
845 if (r != s) {
846 /* passed */
847 fail[i] = 0;
848 }
849 else {
850 fail[i] = 1;
851 mask[i] = 0;
852 }
853 }
854 else {
855 fail[i] = 0;
856 }
857 }
858 break;
859 case GL_ALWAYS:
860 /* always pass */
861 for (i=0;i<n;i++) {
862 fail[i] = 0;
863 }
864 break;
865 default:
866 _mesa_problem(ctx, "Bad stencil func in gl_stencil_pixels");
867 return 0;
868 }
869
870 if (ctx->Stencil.FailFunc[face] != GL_KEEP) {
871 apply_stencil_op_to_pixels( ctx, n, x, y, ctx->Stencil.FailFunc[face],
872 face, fail );
873 }
874
875 return !allfail;
876 }
877
878
879
880
881 /**
882 * Apply stencil and depth testing to an array of pixels.
883 * This is used both for software and hardware stencil buffers.
884 *
885 * The comments in this function are a bit sparse but the code is
886 * almost identical to stencil_and_ztest_span(), which is well
887 * commented.
888 *
889 * Input: n - number of pixels in the array
890 * x, y - array of [n] pixel positions
891 * z - array [n] of z values
892 * mask - array [n] of flags (1=test this pixel, 0=skip the pixel)
893 * Output: mask - array [n] of flags (1=stencil and depth test passed)
894 * Return: GL_FALSE - all fragments failed the testing
895 * GL_TRUE - one or more fragments passed the testing
896 */
897 static GLboolean
898 stencil_and_ztest_pixels( GLcontext *ctx, struct sw_span *span, GLuint face )
899 {
900 struct gl_framebuffer *fb = ctx->DrawBuffer;
901 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
902 const GLuint n = span->end;
903 const GLint *x = span->array->x;
904 const GLint *y = span->array->y;
905 GLubyte *mask = span->array->mask;
906
907 ASSERT(span->arrayMask & SPAN_XY);
908 ASSERT(ctx->Stencil.Enabled);
909 ASSERT(n <= MAX_WIDTH);
910
911 if (!rb->GetPointer(ctx, rb, 0, 0)) {
912 /* No direct access */
913 GLstencil stencil[MAX_WIDTH];
914 GLubyte origMask[MAX_WIDTH];
915
916 rb->GetValues(ctx, rb, n, x, y, stencil);
917
918 MEMCPY(origMask, mask, n * sizeof(GLubyte));
919
920 (void) do_stencil_test(ctx, face, n, stencil, mask);
921
922 if (ctx->Depth.Test == GL_FALSE) {
923 apply_stencil_op(ctx, ctx->Stencil.ZPassFunc[face], face,
924 n, stencil, mask);
925 }
926 else {
927 _swrast_depth_test_span(ctx, span);
928
929 if (ctx->Stencil.ZFailFunc[face] != GL_KEEP) {
930 GLubyte failmask[MAX_WIDTH];
931 GLuint i;
932 for (i = 0; i < n; i++) {
933 ASSERT(mask[i] == 0 || mask[i] == 1);
934 failmask[i] = origMask[i] & (mask[i] ^ 1);
935 }
936 apply_stencil_op(ctx, ctx->Stencil.ZFailFunc[face], face,
937 n, stencil, failmask);
938 }
939 if (ctx->Stencil.ZPassFunc[face] != GL_KEEP) {
940 GLubyte passmask[MAX_WIDTH];
941 GLuint i;
942 for (i = 0; i < n; i++) {
943 ASSERT(mask[i] == 0 || mask[i] == 1);
944 passmask[i] = origMask[i] & mask[i];
945 }
946 apply_stencil_op(ctx, ctx->Stencil.ZPassFunc[face], face,
947 n, stencil, passmask);
948 }
949 }
950
951 /* Write updated stencil values into hardware stencil buffer */
952 rb->PutValues(ctx, rb, n, x, y, stencil, origMask);
953
954 return GL_TRUE;
955 }
956 else {
957 /* Direct access to stencil buffer */
958
959 if (stencil_test_pixels(ctx, face, n, x, y, mask) == GL_FALSE) {
960 /* all fragments failed the stencil test, we're done. */
961 return GL_FALSE;
962 }
963
964 if (ctx->Depth.Test==GL_FALSE) {
965 apply_stencil_op_to_pixels(ctx, n, x, y,
966 ctx->Stencil.ZPassFunc[face], face, mask);
967 }
968 else {
969 GLubyte passmask[MAX_WIDTH], failmask[MAX_WIDTH], oldmask[MAX_WIDTH];
970 GLuint i;
971
972 MEMCPY(oldmask, mask, n * sizeof(GLubyte));
973
974 _swrast_depth_test_span(ctx, span);
975
976 for (i=0;i<n;i++) {
977 ASSERT(mask[i] == 0 || mask[i] == 1);
978 passmask[i] = oldmask[i] & mask[i];
979 failmask[i] = oldmask[i] & (mask[i] ^ 1);
980 }
981
982 if (ctx->Stencil.ZFailFunc[face] != GL_KEEP) {
983 apply_stencil_op_to_pixels(ctx, n, x, y,
984 ctx->Stencil.ZFailFunc[face],
985 face, failmask);
986 }
987 if (ctx->Stencil.ZPassFunc[face] != GL_KEEP) {
988 apply_stencil_op_to_pixels(ctx, n, x, y,
989 ctx->Stencil.ZPassFunc[face],
990 face, passmask);
991 }
992 }
993
994 return GL_TRUE; /* one or more fragments passed both tests */
995 }
996 }
997
998
999 /**
1000 * /return GL_TRUE = one or more fragments passed,
1001 * GL_FALSE = all fragments failed.
1002 */
1003 GLboolean
1004 _swrast_stencil_and_ztest_span(GLcontext *ctx, struct sw_span *span)
1005 {
1006 /* span->facing can only be non-zero if using two-sided stencil */
1007 ASSERT(ctx->Stencil._TestTwoSide || span->facing == 0);
1008 if (span->arrayMask & SPAN_XY)
1009 return stencil_and_ztest_pixels(ctx, span, span->facing);
1010 else
1011 return stencil_and_ztest_span(ctx, span, span->facing);
1012 }
1013
1014
1015 #if 0
1016 GLuint
1017 clip_span(GLuint bufferWidth, GLuint bufferHeight,
1018 GLint x, GLint y, GLuint *count)
1019 {
1020 GLuint n = *count;
1021 GLuint skipPixels = 0;
1022
1023 if (y < 0 || y >= bufferHeight || x + n <= 0 || x >= bufferWidth) {
1024 /* totally out of bounds */
1025 n = 0;
1026 }
1027 else {
1028 /* left clip */
1029 if (x < 0) {
1030 skipPixels = -x;
1031 x = 0;
1032 n -= skipPixels;
1033 }
1034 /* right clip */
1035 if (x + n > bufferWidth) {
1036 GLint dx = x + n - bufferWidth;
1037 n -= dx;
1038 }
1039 }
1040
1041 *count = n;
1042
1043 return skipPixels;
1044 }
1045 #endif
1046
1047
1048 /**
1049 * Return a span of stencil values from the stencil buffer.
1050 * Used for glRead/CopyPixels
1051 * Input: n - how many pixels
1052 * x,y - location of first pixel
1053 * Output: stencil - the array of stencil values
1054 */
1055 void
1056 _swrast_read_stencil_span(GLcontext *ctx, struct gl_renderbuffer *rb,
1057 GLint n, GLint x, GLint y, GLstencil stencil[])
1058 {
1059 if (y < 0 || y >= rb->Height || x + n <= 0 || x >= rb->Width) {
1060 /* span is completely outside framebuffer */
1061 return; /* undefined values OK */
1062 }
1063
1064 if (x < 0) {
1065 GLint dx = -x;
1066 x = 0;
1067 n -= dx;
1068 stencil += dx;
1069 }
1070 if (x + n > rb->Width) {
1071 GLint dx = x + n - rb->Width;
1072 n -= dx;
1073 }
1074 if (n <= 0) {
1075 return;
1076 }
1077
1078 rb->GetRow(ctx, rb, n, x, y, stencil);
1079 }
1080
1081
1082
1083 /**
1084 * Write a span of stencil values to the stencil buffer.
1085 * Used for glDraw/CopyPixels
1086 * Input: n - how many pixels
1087 * x, y - location of first pixel
1088 * stencil - the array of stencil values
1089 */
1090 void
1091 _swrast_write_stencil_span(GLcontext *ctx, GLint n, GLint x, GLint y,
1092 const GLstencil stencil[] )
1093 {
1094 struct gl_framebuffer *fb = ctx->DrawBuffer;
1095 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
1096
1097 if (y < 0 || y >= rb->Height || x + n <= 0 || x >= rb->Width) {
1098 /* span is completely outside framebuffer */
1099 return; /* undefined values OK */
1100 }
1101 if (x < 0) {
1102 GLint dx = -x;
1103 x = 0;
1104 n -= dx;
1105 stencil += dx;
1106 }
1107 if (x + n > rb->Width) {
1108 GLint dx = x + n - rb->Width;
1109 n -= dx;
1110 }
1111 if (n <= 0) {
1112 return;
1113 }
1114
1115 rb->PutRow(ctx, rb, n, x, y, stencil, NULL);
1116 }
1117
1118
1119
1120 /**
1121 * Clear the stencil buffer.
1122 */
1123 void
1124 _swrast_clear_stencil_buffer( GLcontext *ctx, struct gl_renderbuffer *rb )
1125 {
1126 const GLubyte stencilBits = rb->ComponentSizes[0];
1127 const GLuint mask = ctx->Stencil.WriteMask[0];
1128 const GLuint invMask = ~mask;
1129 const GLuint clearVal = (ctx->Stencil.Clear & mask);
1130 const GLuint stencilMax = (1 << stencilBits) - 1;
1131 GLint x, y, width, height;
1132
1133 if (!rb || mask == 0)
1134 return;
1135
1136 ASSERT(rb->DataType == GL_UNSIGNED_BYTE ||
1137 rb->DataType == GL_UNSIGNED_SHORT);
1138
1139 ASSERT(rb->_BaseFormat == GL_STENCIL_INDEX);
1140
1141 /* compute region to clear */
1142 x = ctx->DrawBuffer->_Xmin;
1143 y = ctx->DrawBuffer->_Ymin;
1144 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
1145 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
1146
1147 if (rb->GetPointer(ctx, rb, 0, 0)) {
1148 /* Direct buffer access */
1149 if ((mask & stencilMax) != stencilMax) {
1150 /* need to mask the clear */
1151 if (rb->DataType == GL_UNSIGNED_BYTE) {
1152 GLint i, j;
1153 for (i = 0; i < height; i++) {
1154 GLubyte *stencil = rb->GetPointer(ctx, rb, x, y + i);
1155 for (j = 0; j < width; j++) {
1156 stencil[j] = (stencil[j] & invMask) | clearVal;
1157 }
1158 }
1159 }
1160 else {
1161 GLint i, j;
1162 for (i = 0; i < height; i++) {
1163 GLushort *stencil = rb->GetPointer(ctx, rb, x, y + i);
1164 for (j = 0; j < width; j++) {
1165 stencil[j] = (stencil[j] & invMask) | clearVal;
1166 }
1167 }
1168 }
1169 }
1170 else {
1171 /* no bit masking */
1172 if (width == rb->Width &&
1173 rb->InternalFormat == GL_STENCIL_INDEX8_EXT) {
1174 /* optimized case */
1175 /* XXX bottom-to-op raster assumed! */
1176 GLubyte *stencil = rb->GetPointer(ctx, rb, x, y);
1177 GLuint len = width * height * sizeof(GLubyte);
1178 _mesa_memset(stencil, clearVal, len);
1179 }
1180 else {
1181 /* general case */
1182 GLint i;
1183 for (i = 0; i < height; i++) {
1184 GLvoid *stencil = rb->GetPointer(ctx, rb, x, y + i);
1185 if (rb->DataType == GL_UNSIGNED_BYTE) {
1186 _mesa_memset(stencil, clearVal, width);
1187 }
1188 else {
1189 _mesa_memset16(stencil, clearVal, width);
1190 }
1191 }
1192 }
1193 }
1194 }
1195 else {
1196 /* no direct access */
1197 if ((mask & stencilMax) != stencilMax) {
1198 /* need to mask the clear */
1199 if (rb->DataType == GL_UNSIGNED_BYTE) {
1200 GLint i, j;
1201 for (i = 0; i < height; i++) {
1202 GLubyte stencil[MAX_WIDTH];
1203 rb->GetRow(ctx, rb, width, x, y + i, stencil);
1204 for (j = 0; j < width; j++) {
1205 stencil[j] = (stencil[j] & invMask) | clearVal;
1206 }
1207 rb->PutRow(ctx, rb, width, x, y + i, stencil, NULL);
1208 }
1209 }
1210 else {
1211 GLint i, j;
1212 for (i = 0; i < height; i++) {
1213 GLushort stencil[MAX_WIDTH];
1214 rb->GetRow(ctx, rb, width, x, y + i, stencil);
1215 for (j = 0; j < width; j++) {
1216 stencil[j] = (stencil[j] & invMask) | clearVal;
1217 }
1218 rb->PutRow(ctx, rb, width, x, y + i, stencil, NULL);
1219 }
1220 }
1221 }
1222 else {
1223 /* no bit masking */
1224 const GLubyte clear8 = (GLubyte) clearVal;
1225 const GLushort clear16 = (GLushort) clearVal;
1226 const void *clear;
1227 GLint i;
1228 if (rb->DataType == GL_UNSIGNED_BYTE) {
1229 clear = &clear8;
1230 }
1231 else {
1232 clear = &clear16;
1233 }
1234 for (i = 0; i < height; i++) {
1235 rb->PutMonoRow(ctx, rb, width, x, y + i, clear, NULL);
1236 }
1237 }
1238 }
1239 }