Debian Codegraph @

nx-X11/extras/Mesa/src/mesa/swrast/s_depth.c

raw
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_depth.h"
33 #include "s_context.h"
34 #include "s_span.h"
35
36
37 /**
38 * Do depth test for a horizontal span of fragments.
39 * Input: zbuffer - array of z values in the zbuffer
40 * z - array of fragment z values
41 * Return: number of fragments which pass the test.
42 */
43 static GLuint
44 depth_test_span16( GLcontext *ctx, GLuint n,
45 GLushort zbuffer[], const GLuint z[], GLubyte mask[] )
46 {
47 GLuint passed = 0;
48
49 /* switch cases ordered from most frequent to less frequent */
50 switch (ctx->Depth.Func) {
51 case GL_LESS:
52 if (ctx->Depth.Mask) {
53 /* Update Z buffer */
54 GLuint i;
55 for (i=0; i<n; i++) {
56 if (mask[i]) {
57 if (z[i] < zbuffer[i]) {
58 /* pass */
59 zbuffer[i] = z[i];
60 passed++;
61 }
62 else {
63 /* fail */
64 mask[i] = 0;
65 }
66 }
67 }
68 }
69 else {
70 /* Don't update Z buffer */
71 GLuint i;
72 for (i=0; i<n; i++) {
73 if (mask[i]) {
74 if (z[i] < zbuffer[i]) {
75 /* pass */
76 passed++;
77 }
78 else {
79 mask[i] = 0;
80 }
81 }
82 }
83 }
84 break;
85 case GL_LEQUAL:
86 if (ctx->Depth.Mask) {
87 /* Update Z buffer */
88 GLuint i;
89 for (i=0;i<n;i++) {
90 if (mask[i]) {
91 if (z[i] <= zbuffer[i]) {
92 zbuffer[i] = z[i];
93 passed++;
94 }
95 else {
96 mask[i] = 0;
97 }
98 }
99 }
100 }
101 else {
102 /* Don't update Z buffer */
103 GLuint i;
104 for (i=0;i<n;i++) {
105 if (mask[i]) {
106 if (z[i] <= zbuffer[i]) {
107 /* pass */
108 passed++;
109 }
110 else {
111 mask[i] = 0;
112 }
113 }
114 }
115 }
116 break;
117 case GL_GEQUAL:
118 if (ctx->Depth.Mask) {
119 /* Update Z buffer */
120 GLuint i;
121 for (i=0;i<n;i++) {
122 if (mask[i]) {
123 if (z[i] >= zbuffer[i]) {
124 zbuffer[i] = z[i];
125 passed++;
126 }
127 else {
128 mask[i] = 0;
129 }
130 }
131 }
132 }
133 else {
134 /* Don't update Z buffer */
135 GLuint i;
136 for (i=0;i<n;i++) {
137 if (mask[i]) {
138 if (z[i] >= zbuffer[i]) {
139 /* pass */
140 passed++;
141 }
142 else {
143 mask[i] = 0;
144 }
145 }
146 }
147 }
148 break;
149 case GL_GREATER:
150 if (ctx->Depth.Mask) {
151 /* Update Z buffer */
152 GLuint i;
153 for (i=0;i<n;i++) {
154 if (mask[i]) {
155 if (z[i] > zbuffer[i]) {
156 zbuffer[i] = z[i];
157 passed++;
158 }
159 else {
160 mask[i] = 0;
161 }
162 }
163 }
164 }
165 else {
166 /* Don't update Z buffer */
167 GLuint i;
168 for (i=0;i<n;i++) {
169 if (mask[i]) {
170 if (z[i] > zbuffer[i]) {
171 /* pass */
172 passed++;
173 }
174 else {
175 mask[i] = 0;
176 }
177 }
178 }
179 }
180 break;
181 case GL_NOTEQUAL:
182 if (ctx->Depth.Mask) {
183 /* Update Z buffer */
184 GLuint i;
185 for (i=0;i<n;i++) {
186 if (mask[i]) {
187 if (z[i] != zbuffer[i]) {
188 zbuffer[i] = z[i];
189 passed++;
190 }
191 else {
192 mask[i] = 0;
193 }
194 }
195 }
196 }
197 else {
198 /* Don't update Z buffer */
199 GLuint i;
200 for (i=0;i<n;i++) {
201 if (mask[i]) {
202 if (z[i] != zbuffer[i]) {
203 /* pass */
204 passed++;
205 }
206 else {
207 mask[i] = 0;
208 }
209 }
210 }
211 }
212 break;
213 case GL_EQUAL:
214 if (ctx->Depth.Mask) {
215 /* Update Z buffer */
216 GLuint i;
217 for (i=0;i<n;i++) {
218 if (mask[i]) {
219 if (z[i] == zbuffer[i]) {
220 zbuffer[i] = z[i];
221 passed++;
222 }
223 else {
224 mask[i] = 0;
225 }
226 }
227 }
228 }
229 else {
230 /* Don't update Z buffer */
231 GLuint i;
232 for (i=0;i<n;i++) {
233 if (mask[i]) {
234 if (z[i] == zbuffer[i]) {
235 /* pass */
236 passed++;
237 }
238 else {
239 mask[i] = 0;
240 }
241 }
242 }
243 }
244 break;
245 case GL_ALWAYS:
246 if (ctx->Depth.Mask) {
247 /* Update Z buffer */
248 GLuint i;
249 for (i=0;i<n;i++) {
250 if (mask[i]) {
251 zbuffer[i] = z[i];
252 passed++;
253 }
254 }
255 }
256 else {
257 /* Don't update Z buffer or mask */
258 passed = n;
259 }
260 break;
261 case GL_NEVER:
262 _mesa_bzero(mask, n * sizeof(GLubyte));
263 break;
264 default:
265 _mesa_problem(ctx, "Bad depth func in depth_test_span16");
266 }
267
268 return passed;
269 }
270
271
272 static GLuint
273 depth_test_span32( GLcontext *ctx, GLuint n,
274 GLuint zbuffer[], const GLuint z[], GLubyte mask[] )
275 {
276 GLuint passed = 0;
277
278 /* switch cases ordered from most frequent to less frequent */
279 switch (ctx->Depth.Func) {
280 case GL_LESS:
281 if (ctx->Depth.Mask) {
282 /* Update Z buffer */
283 GLuint i;
284 for (i=0; i<n; i++) {
285 if (mask[i]) {
286 if (z[i] < zbuffer[i]) {
287 /* pass */
288 zbuffer[i] = z[i];
289 passed++;
290 }
291 else {
292 /* fail */
293 mask[i] = 0;
294 }
295 }
296 }
297 }
298 else {
299 /* Don't update Z buffer */
300 GLuint i;
301 for (i=0; i<n; i++) {
302 if (mask[i]) {
303 if (z[i] < zbuffer[i]) {
304 /* pass */
305 passed++;
306 }
307 else {
308 mask[i] = 0;
309 }
310 }
311 }
312 }
313 break;
314 case GL_LEQUAL:
315 if (ctx->Depth.Mask) {
316 /* Update Z buffer */
317 GLuint i;
318 for (i=0;i<n;i++) {
319 if (mask[i]) {
320 if (z[i] <= zbuffer[i]) {
321 zbuffer[i] = z[i];
322 passed++;
323 }
324 else {
325 mask[i] = 0;
326 }
327 }
328 }
329 }
330 else {
331 /* Don't update Z buffer */
332 GLuint i;
333 for (i=0;i<n;i++) {
334 if (mask[i]) {
335 if (z[i] <= zbuffer[i]) {
336 /* pass */
337 passed++;
338 }
339 else {
340 mask[i] = 0;
341 }
342 }
343 }
344 }
345 break;
346 case GL_GEQUAL:
347 if (ctx->Depth.Mask) {
348 /* Update Z buffer */
349 GLuint i;
350 for (i=0;i<n;i++) {
351 if (mask[i]) {
352 if (z[i] >= zbuffer[i]) {
353 zbuffer[i] = z[i];
354 passed++;
355 }
356 else {
357 mask[i] = 0;
358 }
359 }
360 }
361 }
362 else {
363 /* Don't update Z buffer */
364 GLuint i;
365 for (i=0;i<n;i++) {
366 if (mask[i]) {
367 if (z[i] >= zbuffer[i]) {
368 /* pass */
369 passed++;
370 }
371 else {
372 mask[i] = 0;
373 }
374 }
375 }
376 }
377 break;
378 case GL_GREATER:
379 if (ctx->Depth.Mask) {
380 /* Update Z buffer */
381 GLuint i;
382 for (i=0;i<n;i++) {
383 if (mask[i]) {
384 if (z[i] > zbuffer[i]) {
385 zbuffer[i] = z[i];
386 passed++;
387 }
388 else {
389 mask[i] = 0;
390 }
391 }
392 }
393 }
394 else {
395 /* Don't update Z buffer */
396 GLuint i;
397 for (i=0;i<n;i++) {
398 if (mask[i]) {
399 if (z[i] > zbuffer[i]) {
400 /* pass */
401 passed++;
402 }
403 else {
404 mask[i] = 0;
405 }
406 }
407 }
408 }
409 break;
410 case GL_NOTEQUAL:
411 if (ctx->Depth.Mask) {
412 /* Update Z buffer */
413 GLuint i;
414 for (i=0;i<n;i++) {
415 if (mask[i]) {
416 if (z[i] != zbuffer[i]) {
417 zbuffer[i] = z[i];
418 passed++;
419 }
420 else {
421 mask[i] = 0;
422 }
423 }
424 }
425 }
426 else {
427 /* Don't update Z buffer */
428 GLuint i;
429 for (i=0;i<n;i++) {
430 if (mask[i]) {
431 if (z[i] != zbuffer[i]) {
432 /* pass */
433 passed++;
434 }
435 else {
436 mask[i] = 0;
437 }
438 }
439 }
440 }
441 break;
442 case GL_EQUAL:
443 if (ctx->Depth.Mask) {
444 /* Update Z buffer */
445 GLuint i;
446 for (i=0;i<n;i++) {
447 if (mask[i]) {
448 if (z[i] == zbuffer[i]) {
449 zbuffer[i] = z[i];
450 passed++;
451 }
452 else {
453 mask[i] = 0;
454 }
455 }
456 }
457 }
458 else {
459 /* Don't update Z buffer */
460 GLuint i;
461 for (i=0;i<n;i++) {
462 if (mask[i]) {
463 if (z[i] == zbuffer[i]) {
464 /* pass */
465 passed++;
466 }
467 else {
468 mask[i] = 0;
469 }
470 }
471 }
472 }
473 break;
474 case GL_ALWAYS:
475 if (ctx->Depth.Mask) {
476 /* Update Z buffer */
477 GLuint i;
478 for (i=0;i<n;i++) {
479 if (mask[i]) {
480 zbuffer[i] = z[i];
481 passed++;
482 }
483 }
484 }
485 else {
486 /* Don't update Z buffer or mask */
487 passed = n;
488 }
489 break;
490 case GL_NEVER:
491 _mesa_bzero(mask, n * sizeof(GLubyte));
492 break;
493 default:
494 _mesa_problem(ctx, "Bad depth func in depth_test_span32");
495 }
496
497 return passed;
498 }
499
500
501
502 /*
503 * Apply depth test to span of fragments.
504 */
505 static GLuint
506 depth_test_span( GLcontext *ctx, struct sw_span *span)
507 {
508 struct gl_framebuffer *fb = ctx->DrawBuffer;
509 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
510 const GLint x = span->x;
511 const GLint y = span->y;
512 const GLuint count = span->end;
513 const GLuint *zValues = span->array->z;
514 GLubyte *mask = span->array->mask;
515 GLuint passed;
516
517 ASSERT((span->arrayMask & SPAN_XY) == 0);
518 ASSERT(span->arrayMask & SPAN_Z);
519
520 if (rb->GetPointer(ctx, rb, 0, 0)) {
521 /* Directly access buffer */
522 if (ctx->DrawBuffer->Visual.depthBits <= 16) {
523 GLushort *zbuffer = (GLushort *) rb->GetPointer(ctx, rb, x, y);
524 passed = depth_test_span16(ctx, count, zbuffer, zValues, mask);
525 }
526 else {
527 GLuint *zbuffer = (GLuint *) rb->GetPointer(ctx, rb, x, y);
528 passed = depth_test_span32(ctx, count, zbuffer, zValues, mask);
529 }
530 }
531 else {
532 /* read depth values from buffer, test, write back */
533 if (rb->DataType == GL_UNSIGNED_SHORT) {
534 GLushort zbuffer[MAX_WIDTH];
535 rb->GetRow(ctx, rb, count, x, y, zbuffer);
536 passed = depth_test_span16(ctx, count, zbuffer, zValues, mask );
537 rb->PutRow(ctx, rb, count, x, y, zbuffer, NULL);
538 }
539 else {
540 GLuint zbuffer[MAX_WIDTH];
541 ASSERT(rb->DataType == GL_UNSIGNED_INT);
542 rb->GetRow(ctx, rb, count, x, y, zbuffer);
543 passed = depth_test_span32(ctx, count, zbuffer, zValues, mask );
544 rb->PutRow(ctx, rb, count, x, y, zbuffer, NULL);
545 }
546 }
547
548 if (passed < count) {
549 span->writeAll = GL_FALSE;
550 }
551 return passed;
552 }
553
554
555
556 #define Z_ADDRESS(X, Y) (zStart + (Y) * stride + (X))
557
558
559 /*
560 * Do depth testing for an array of fragments at assorted locations.
561 */
562 static void
563 direct_depth_test_pixels16(GLcontext *ctx, GLushort *zStart, GLuint stride,
564 GLuint n, const GLint x[], const GLint y[],
565 const GLuint z[], GLubyte mask[] )
566 {
567 /* switch cases ordered from most frequent to less frequent */
568 switch (ctx->Depth.Func) {
569 case GL_LESS:
570 if (ctx->Depth.Mask) {
571 /* Update Z buffer */
572 GLuint i;
573 for (i=0; i<n; i++) {
574 if (mask[i]) {
575 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
576 if (z[i] < *zptr) {
577 /* pass */
578 *zptr = z[i];
579 }
580 else {
581 /* fail */
582 mask[i] = 0;
583 }
584 }
585 }
586 }
587 else {
588 /* Don't update Z buffer */
589 GLuint i;
590 for (i=0; i<n; i++) {
591 if (mask[i]) {
592 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
593 if (z[i] < *zptr) {
594 /* pass */
595 }
596 else {
597 /* fail */
598 mask[i] = 0;
599 }
600 }
601 }
602 }
603 break;
604 case GL_LEQUAL:
605 if (ctx->Depth.Mask) {
606 /* Update Z buffer */
607 GLuint i;
608 for (i=0; i<n; i++) {
609 if (mask[i]) {
610 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
611 if (z[i] <= *zptr) {
612 /* pass */
613 *zptr = z[i];
614 }
615 else {
616 /* fail */
617 mask[i] = 0;
618 }
619 }
620 }
621 }
622 else {
623 /* Don't update Z buffer */
624 GLuint i;
625 for (i=0; i<n; i++) {
626 if (mask[i]) {
627 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
628 if (z[i] <= *zptr) {
629 /* pass */
630 }
631 else {
632 /* fail */
633 mask[i] = 0;
634 }
635 }
636 }
637 }
638 break;
639 case GL_GEQUAL:
640 if (ctx->Depth.Mask) {
641 /* Update Z buffer */
642 GLuint i;
643 for (i=0; i<n; i++) {
644 if (mask[i]) {
645 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
646 if (z[i] >= *zptr) {
647 /* pass */
648 *zptr = z[i];
649 }
650 else {
651 /* fail */
652 mask[i] = 0;
653 }
654 }
655 }
656 }
657 else {
658 /* Don't update Z buffer */
659 GLuint i;
660 for (i=0; i<n; i++) {
661 if (mask[i]) {
662 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
663 if (z[i] >= *zptr) {
664 /* pass */
665 }
666 else {
667 /* fail */
668 mask[i] = 0;
669 }
670 }
671 }
672 }
673 break;
674 case GL_GREATER:
675 if (ctx->Depth.Mask) {
676 /* Update Z buffer */
677 GLuint i;
678 for (i=0; i<n; i++) {
679 if (mask[i]) {
680 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
681 if (z[i] > *zptr) {
682 /* pass */
683 *zptr = z[i];
684 }
685 else {
686 /* fail */
687 mask[i] = 0;
688 }
689 }
690 }
691 }
692 else {
693 /* Don't update Z buffer */
694 GLuint i;
695 for (i=0; i<n; i++) {
696 if (mask[i]) {
697 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
698 if (z[i] > *zptr) {
699 /* pass */
700 }
701 else {
702 /* fail */
703 mask[i] = 0;
704 }
705 }
706 }
707 }
708 break;
709 case GL_NOTEQUAL:
710 if (ctx->Depth.Mask) {
711 /* Update Z buffer */
712 GLuint i;
713 for (i=0; i<n; i++) {
714 if (mask[i]) {
715 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
716 if (z[i] != *zptr) {
717 /* pass */
718 *zptr = z[i];
719 }
720 else {
721 /* fail */
722 mask[i] = 0;
723 }
724 }
725 }
726 }
727 else {
728 /* Don't update Z buffer */
729 GLuint i;
730 for (i=0; i<n; i++) {
731 if (mask[i]) {
732 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
733 if (z[i] != *zptr) {
734 /* pass */
735 }
736 else {
737 /* fail */
738 mask[i] = 0;
739 }
740 }
741 }
742 }
743 break;
744 case GL_EQUAL:
745 if (ctx->Depth.Mask) {
746 /* Update Z buffer */
747 GLuint i;
748 for (i=0; i<n; i++) {
749 if (mask[i]) {
750 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
751 if (z[i] == *zptr) {
752 /* pass */
753 *zptr = z[i];
754 }
755 else {
756 /* fail */
757 mask[i] = 0;
758 }
759 }
760 }
761 }
762 else {
763 /* Don't update Z buffer */
764 GLuint i;
765 for (i=0; i<n; i++) {
766 if (mask[i]) {
767 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
768 if (z[i] == *zptr) {
769 /* pass */
770 }
771 else {
772 /* fail */
773 mask[i] = 0;
774 }
775 }
776 }
777 }
778 break;
779 case GL_ALWAYS:
780 if (ctx->Depth.Mask) {
781 /* Update Z buffer */
782 GLuint i;
783 for (i=0; i<n; i++) {
784 if (mask[i]) {
785 GLushort *zptr = Z_ADDRESS(x[i], y[i]);
786 *zptr = z[i];
787 }
788 }
789 }
790 else {
791 /* Don't update Z buffer or mask */
792 }
793 break;
794 case GL_NEVER:
795 /* depth test never passes */
796 _mesa_bzero(mask, n * sizeof(GLubyte));
797 break;
798 default:
799 _mesa_problem(ctx, "Bad depth func in direct_depth_test_pixels");
800 }
801 }
802
803
804
805 /*
806 * Do depth testing for an array of fragments with direct access to zbuffer.
807 */
808 static void
809 direct_depth_test_pixels32(GLcontext *ctx, GLuint *zStart, GLuint stride,
810 GLuint n, const GLint x[], const GLint y[],
811 const GLuint z[], GLubyte mask[] )
812 {
813 /* switch cases ordered from most frequent to less frequent */
814 switch (ctx->Depth.Func) {
815 case GL_LESS:
816 if (ctx->Depth.Mask) {
817 /* Update Z buffer */
818 GLuint i;
819 for (i=0; i<n; i++) {
820 if (mask[i]) {
821 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
822 if (z[i] < *zptr) {
823 /* pass */
824 *zptr = z[i];
825 }
826 else {
827 /* fail */
828 mask[i] = 0;
829 }
830 }
831 }
832 }
833 else {
834 /* Don't update Z buffer */
835 GLuint i;
836 for (i=0; i<n; i++) {
837 if (mask[i]) {
838 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
839 if (z[i] < *zptr) {
840 /* pass */
841 }
842 else {
843 /* fail */
844 mask[i] = 0;
845 }
846 }
847 }
848 }
849 break;
850 case GL_LEQUAL:
851 if (ctx->Depth.Mask) {
852 /* Update Z buffer */
853 GLuint i;
854 for (i=0; i<n; i++) {
855 if (mask[i]) {
856 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
857 if (z[i] <= *zptr) {
858 /* pass */
859 *zptr = z[i];
860 }
861 else {
862 /* fail */
863 mask[i] = 0;
864 }
865 }
866 }
867 }
868 else {
869 /* Don't update Z buffer */
870 GLuint i;
871 for (i=0; i<n; i++) {
872 if (mask[i]) {
873 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
874 if (z[i] <= *zptr) {
875 /* pass */
876 }
877 else {
878 /* fail */
879 mask[i] = 0;
880 }
881 }
882 }
883 }
884 break;
885 case GL_GEQUAL:
886 if (ctx->Depth.Mask) {
887 /* Update Z buffer */
888 GLuint i;
889 for (i=0; i<n; i++) {
890 if (mask[i]) {
891 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
892 if (z[i] >= *zptr) {
893 /* pass */
894 *zptr = z[i];
895 }
896 else {
897 /* fail */
898 mask[i] = 0;
899 }
900 }
901 }
902 }
903 else {
904 /* Don't update Z buffer */
905 GLuint i;
906 for (i=0; i<n; i++) {
907 if (mask[i]) {
908 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
909 if (z[i] >= *zptr) {
910 /* pass */
911 }
912 else {
913 /* fail */
914 mask[i] = 0;
915 }
916 }
917 }
918 }
919 break;
920 case GL_GREATER:
921 if (ctx->Depth.Mask) {
922 /* Update Z buffer */
923 GLuint i;
924 for (i=0; i<n; i++) {
925 if (mask[i]) {
926 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
927 if (z[i] > *zptr) {
928 /* pass */
929 *zptr = z[i];
930 }
931 else {
932 /* fail */
933 mask[i] = 0;
934 }
935 }
936 }
937 }
938 else {
939 /* Don't update Z buffer */
940 GLuint i;
941 for (i=0; i<n; i++) {
942 if (mask[i]) {
943 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
944 if (z[i] > *zptr) {
945 /* pass */
946 }
947 else {
948 /* fail */
949 mask[i] = 0;
950 }
951 }
952 }
953 }
954 break;
955 case GL_NOTEQUAL:
956 if (ctx->Depth.Mask) {
957 /* Update Z buffer */
958 GLuint i;
959 for (i=0; i<n; i++) {
960 if (mask[i]) {
961 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
962 if (z[i] != *zptr) {
963 /* pass */
964 *zptr = z[i];
965 }
966 else {
967 /* fail */
968 mask[i] = 0;
969 }
970 }
971 }
972 }
973 else {
974 /* Don't update Z buffer */
975 GLuint i;
976 for (i=0; i<n; i++) {
977 if (mask[i]) {
978 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
979 if (z[i] != *zptr) {
980 /* pass */
981 }
982 else {
983 /* fail */
984 mask[i] = 0;
985 }
986 }
987 }
988 }
989 break;
990 case GL_EQUAL:
991 if (ctx->Depth.Mask) {
992 /* Update Z buffer */
993 GLuint i;
994 for (i=0; i<n; i++) {
995 if (mask[i]) {
996 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
997 if (z[i] == *zptr) {
998 /* pass */
999 *zptr = z[i];
1000 }
1001 else {
1002 /* fail */
1003 mask[i] = 0;
1004 }
1005 }
1006 }
1007 }
1008 else {
1009 /* Don't update Z buffer */
1010 GLuint i;
1011 for (i=0; i<n; i++) {
1012 if (mask[i]) {
1013 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
1014 if (z[i] == *zptr) {
1015 /* pass */
1016 }
1017 else {
1018 /* fail */
1019 mask[i] = 0;
1020 }
1021 }
1022 }
1023 }
1024 break;
1025 case GL_ALWAYS:
1026 if (ctx->Depth.Mask) {
1027 /* Update Z buffer */
1028 GLuint i;
1029 for (i=0; i<n; i++) {
1030 if (mask[i]) {
1031 GLuint *zptr = Z_ADDRESS(x[i], y[i]);
1032 *zptr = z[i];
1033 }
1034 }
1035 }
1036 else {
1037 /* Don't update Z buffer or mask */
1038 }
1039 break;
1040 case GL_NEVER:
1041 /* depth test never passes */
1042 _mesa_bzero(mask, n * sizeof(GLubyte));
1043 break;
1044 default:
1045 _mesa_problem(ctx, "Bad depth func in direct_depth_test_pixels");
1046 }
1047 }
1048
1049
1050
1051
1052 static GLuint
1053 depth_test_pixels( GLcontext *ctx, struct sw_span *span )
1054 {
1055 struct gl_framebuffer *fb = ctx->DrawBuffer;
1056 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
1057 const GLuint count = span->end;
1058 const GLint *x = span->array->x;
1059 const GLint *y = span->array->y;
1060 const GLuint *z = span->array->z;
1061 GLubyte *mask = span->array->mask;
1062
1063 if (rb->GetPointer(ctx, rb, 0, 0)) {
1064 /* Directly access values */
1065 if (rb->DataType == GL_UNSIGNED_SHORT) {
1066 GLushort *zStart = (GLushort *) rb->Data;
1067 GLuint stride = rb->Width;
1068 direct_depth_test_pixels16(ctx, zStart, stride, count, x, y, z, mask);
1069 }
1070 else {
1071 GLuint *zStart = (GLuint *) rb->Data;
1072 GLuint stride = rb->Width;
1073 direct_depth_test_pixels32(ctx, zStart, stride, count, x, y, z, mask);
1074 }
1075 }
1076 else {
1077 /* read depth values from buffer, test, write back */
1078 if (rb->DataType == GL_UNSIGNED_SHORT) {
1079 GLushort zbuffer[MAX_WIDTH];
1080 _swrast_get_values(ctx, rb, count, x, y, zbuffer, sizeof(GLushort));
1081 depth_test_span16(ctx, count, zbuffer, z, mask );
1082 rb->PutValues(ctx, rb, count, x, y, zbuffer, NULL);
1083 }
1084 else {
1085 GLuint zbuffer[MAX_WIDTH];
1086 ASSERT(rb->DataType == GL_UNSIGNED_INT);
1087 _swrast_get_values(ctx, rb, count, x, y, zbuffer, sizeof(GLuint));
1088 depth_test_span32(ctx, count, zbuffer, z, mask );
1089 rb->PutValues(ctx, rb, count, x, y, zbuffer, NULL);
1090 }
1091 }
1092
1093 return count; /* not really correct, but OK */
1094 }
1095
1096
1097 /**
1098 * Apply depth (Z) buffer testing to the span.
1099 * \return approx number of pixels that passed (only zero is reliable)
1100 */
1101 GLuint
1102 _swrast_depth_test_span( GLcontext *ctx, struct sw_span *span)
1103 {
1104 if (span->arrayMask & SPAN_XY)
1105 return depth_test_pixels(ctx, span);
1106 else
1107 return depth_test_span(ctx, span);
1108 }
1109
1110
1111 /**
1112 * GL_EXT_depth_bounds_test extension.
1113 * Discard fragments depending on whether the corresponding Z-buffer
1114 * values are outside the depth bounds test range.
1115 * Note: we test the Z buffer values, not the fragment Z values!
1116 * \return GL_TRUE if any fragments pass, GL_FALSE if no fragments pass
1117 */
1118 GLboolean
1119 _swrast_depth_bounds_test( GLcontext *ctx, struct sw_span *span )
1120 {
1121 struct gl_framebuffer *fb = ctx->DrawBuffer;
1122 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
1123 GLuint zMin = (GLuint) (ctx->Depth.BoundsMin * fb->_DepthMaxF + 0.5F);
1124 GLuint zMax = (GLuint) (ctx->Depth.BoundsMax * fb->_DepthMaxF + 0.5F);
1125 GLubyte *mask = span->array->mask;
1126 const GLuint count = span->end;
1127 GLuint i;
1128 GLboolean anyPass = GL_FALSE;
1129
1130 if (rb->DataType == GL_UNSIGNED_SHORT) {
1131 /* get 16-bit values */
1132 GLushort zbuffer16[MAX_WIDTH], *zbuffer;
1133 if (span->arrayMask & SPAN_XY) {
1134 _swrast_get_values(ctx, rb, count, span->array->x, span->array->y,
1135 zbuffer16, sizeof(GLushort));
1136 zbuffer = zbuffer16;
1137 }
1138 else {
1139 zbuffer = rb->GetPointer(ctx, rb, span->x, span->y);
1140 if (!zbuffer) {
1141 rb->GetRow(ctx, rb, count, span->x, span->y, zbuffer16);
1142 zbuffer = zbuffer16;
1143 }
1144 }
1145 assert(zbuffer);
1146
1147 /* Now do the tests */
1148 for (i = 0; i < count; i++) {
1149 if (mask[i]) {
1150 if (zbuffer[i] < zMin || zbuffer[i] > zMax)
1151 mask[i] = GL_FALSE;
1152 else
1153 anyPass = GL_TRUE;
1154 }
1155 }
1156 }
1157 else {
1158 /* get 32-bit values */
1159 GLuint zbuffer32[MAX_WIDTH], *zbuffer;
1160 if (span->arrayMask & SPAN_XY) {
1161 _swrast_get_values(ctx, rb, count, span->array->x, span->array->y,
1162 zbuffer32, sizeof(GLuint));
1163 zbuffer = zbuffer32;
1164 }
1165 else {
1166 zbuffer = rb->GetPointer(ctx, rb, span->x, span->y);
1167 if (!zbuffer) {
1168 rb->GetRow(ctx, rb, count, span->x, span->y, zbuffer32);
1169 zbuffer = zbuffer32;
1170 }
1171 }
1172 assert(zbuffer);
1173
1174 /* Now do the tests */
1175 for (i = 0; i < count; i++) {
1176 if (mask[i]) {
1177 if (zbuffer[i] < zMin || zbuffer[i] > zMax)
1178 mask[i] = GL_FALSE;
1179 else
1180 anyPass = GL_TRUE;
1181 }
1182 }
1183 }
1184
1185 return anyPass;
1186 }
1187
1188
1189
1190 /**********************************************************************/
1191 /***** Read Depth Buffer *****/
1192 /**********************************************************************/
1193
1194
1195 /**
1196 * Read a span of depth values from the depth buffer.
1197 * This function does clipping before calling the device driver function.
1198 *
1199 * XXXX this is no longer a swrast function!!!
1200 *
1201 */
1202 void
1203 _swrast_read_depth_span( GLcontext *ctx, struct gl_renderbuffer *rb,
1204 GLint n, GLint x, GLint y, GLuint depth[] )
1205 {
1206 if (y < 0 || y >= (GLint) rb->Height ||
1207 x + (GLint) n <= 0 || x >= (GLint) rb->Width) {
1208 /* span is completely outside framebuffer */
1209 GLint i;
1210 for (i = 0; i < n; i++)
1211 depth[i] = 0;
1212 return;
1213 }
1214
1215 if (x < 0) {
1216 GLint dx = -x;
1217 GLint i;
1218 for (i = 0; i < dx; i++)
1219 depth[i] = 0;
1220 x = 0;
1221 n -= dx;
1222 depth += dx;
1223 }
1224 if (x + n > (GLint) rb->Width) {
1225 GLint dx = x + n - (GLint) rb->Width;
1226 GLint i;
1227 for (i = 0; i < dx; i++)
1228 depth[n - i - 1] = 0;
1229 n -= dx;
1230 }
1231 if (n <= 0) {
1232 return;
1233 }
1234
1235 /* we'll always return 32-bit values to our caller */
1236 if (!rb) {
1237 _mesa_bzero(depth, n * sizeof(GLuint));
1238 }
1239 else if (rb->DataType == GL_UNSIGNED_INT) {
1240 rb->GetRow(ctx, rb, n, x, y, depth);
1241 }
1242 else {
1243 GLushort temp[MAX_WIDTH];
1244 GLuint i;
1245 ASSERT(rb->DataType == GL_UNSIGNED_SHORT);
1246 rb->GetRow(ctx, rb, n, x, y, temp);
1247 for (i = 0; i < n; i++) {
1248 depth[i] = temp[i];
1249 }
1250 }
1251 }
1252
1253
1254 /**
1255 * Return a span of depth values from the depth buffer as floats in [0,1].
1256 * Input: n - how many pixels
1257 * x,y - location of first pixel
1258 * Output: depth - the array of depth values
1259 */
1260 void
1261 _swrast_read_depth_span_float( GLcontext *ctx, struct gl_renderbuffer *rb,
1262 GLint n, GLint x, GLint y, GLfloat depth[] )
1263 {
1264 const GLfloat scale = 1.0F / ctx->DrawBuffer->_DepthMaxF;
1265 GLuint temp[MAX_WIDTH];
1266 GLint i;
1267
1268 assert(n <= MAX_WIDTH);
1269
1270 _swrast_read_depth_span(ctx, rb, n, x, y, temp);
1271 for (i = 0; i < n; i++) {
1272 depth[i] = temp[i] * scale;
1273 }
1274 }
1275
1276
1277 /**
1278 * Clear the depth buffer.
1279 */
1280 void
1281 _swrast_clear_depth_buffer( GLcontext *ctx, struct gl_renderbuffer *rb )
1282 {
1283 GLuint clearValue;
1284 GLint x, y, width, height;
1285
1286 if (!rb || !ctx->Depth.Mask) {
1287 /* no depth buffer, or writing to it is disabled */
1288 return;
1289 }
1290
1291 /* compute integer clearing value */
1292 if (ctx->Depth.Clear == 1.0) {
1293 clearValue = ctx->DrawBuffer->_DepthMax;
1294 }
1295 else {
1296 clearValue = (GLuint) (ctx->Depth.Clear * ctx->DrawBuffer->_DepthMaxF);
1297 }
1298
1299 assert(rb->_BaseFormat == GL_DEPTH_COMPONENT);
1300
1301 /* compute region to clear */
1302 x = ctx->DrawBuffer->_Xmin;
1303 y = ctx->DrawBuffer->_Ymin;
1304 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
1305 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
1306
1307 if (rb->GetPointer(ctx, rb, 0, 0)) {
1308 /* Direct buffer access is possible. Either this is just malloc'd
1309 * memory, or perhaps the driver mmap'd the zbuffer memory.
1310 */
1311 if (rb->DataType == GL_UNSIGNED_SHORT) {
1312 if (width == rb->Width &&
1313 (clearValue & 0xff) == ((clearValue >> 8) & 0xff)) {
1314 /* optimized case */
1315 GLushort *dst = (GLushort *) rb->GetPointer(ctx, rb, x, y);
1316 GLuint len = width * height * sizeof(GLushort);
1317 _mesa_memset(dst, (clearValue & 0xff), len);
1318 }
1319 else {
1320 /* general case */
1321 GLint i, j;
1322 for (i = 0; i < height; i++) {
1323 GLushort *dst = (GLushort *) rb->GetPointer(ctx, rb, x, y + i);
1324 for (j = 0; j < width; j++) {
1325 dst[j] = clearValue;
1326 }
1327 }
1328 }
1329 }
1330 else {
1331 GLint i, j;
1332 ASSERT(rb->DataType == GL_UNSIGNED_INT);
1333 for (i = 0; i < height; i++) {
1334 GLuint *dst = (GLuint *) rb->GetPointer(ctx, rb, x, y + i);
1335 for (j = 0; j < width; j++) {
1336 dst[j] = clearValue;
1337 }
1338 }
1339 }
1340 }
1341 else {
1342 /* Direct access not possible. Use PutRow to write new values. */
1343 if (rb->DataType == GL_UNSIGNED_SHORT) {
1344 GLushort clearRow[MAX_WIDTH];
1345 GLint i, j;
1346 for (j = 0; j < width; j++) {
1347 clearRow[j] = clearValue;
1348 }
1349 for (i = 0; i < height; i++) {
1350 rb->PutRow(ctx, rb, width, x, y + i, clearRow, NULL);
1351 }
1352 }
1353 else {
1354 GLuint clearRow[MAX_WIDTH];
1355 GLint i, j;
1356 assert(rb->DataType == GL_UNSIGNED_INT);
1357 for (j = 0; j < width; j++) {
1358 clearRow[j] = clearValue;
1359 }
1360 for (i = 0; i < height; i++) {
1361 rb->PutRow(ctx, rb, width, x, y + i, clearRow, NULL);
1362 }
1363 }
1364 }
1365 }