Debian Codegraph @

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

raw
1 /* $Id: depth.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 /*
24 * Depth buffer functions
25 */
26
27
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include "context.h"
32 #include "depth.h"
33 #include "dlist.h"
34 #include "macros.h"
35 #include "types.h"
36
37
38
39 /**********************************************************************/
40 /***** API Functions *****/
41 /**********************************************************************/
42
43
44
45 void gl_ClearDepth( GLcontext* ctx, GLclampd depth )
46 {
47 if (INSIDE_BEGIN_END(ctx)) {
48 gl_error( ctx, GL_INVALID_OPERATION, "glClearDepth" );
49 return;
50 }
51 ctx->Depth.Clear = (GLfloat) CLAMP( depth, 0.0, 1.0 );
52 }
53
54
55
56 void gl_DepthFunc( GLcontext* ctx, GLenum func )
57 {
58 if (INSIDE_BEGIN_END(ctx)) {
59 gl_error( ctx, GL_INVALID_OPERATION, "glDepthFunc" );
60 return;
61 }
62
63 switch (func) {
64 case GL_NEVER:
65 case GL_LESS: /* (default) pass if incoming z < stored z */
66 case GL_GEQUAL:
67 case GL_LEQUAL:
68 case GL_GREATER:
69 case GL_NOTEQUAL:
70 case GL_EQUAL:
71 case GL_ALWAYS:
72 ctx->Depth.Func = func;
73 ctx->NewState |= NEW_RASTER_OPS;
74 break;
75 default:
76 gl_error( ctx, GL_INVALID_ENUM, "glDepth.Func" );
77 }
78 }
79
80
81
82 void gl_DepthMask( GLcontext* ctx, GLboolean flag )
83 {
84 if (INSIDE_BEGIN_END(ctx)) {
85 gl_error( ctx, GL_INVALID_OPERATION, "glDepthMask" );
86 return;
87 }
88
89 /*
90 * GL_TRUE indicates depth buffer writing is enabled (default)
91 * GL_FALSE indicates depth buffer writing is disabled
92 */
93 ctx->Depth.Mask = flag;
94 ctx->NewState |= NEW_RASTER_OPS;
95 }
96
97
98
99 void gl_DepthRange( GLcontext* ctx, GLclampd nearval, GLclampd farval )
100 {
101 /*
102 * nearval - specifies mapping of the near clipping plane to window
103 * coordinates, default is 0
104 * farval - specifies mapping of the far clipping plane to window
105 * coordinates, default is 1
106 *
107 * After clipping and div by w, z coords are in -1.0 to 1.0,
108 * corresponding to near and far clipping planes. glDepthRange
109 * specifies a linear mapping of the normalized z coords in
110 * this range to window z coords.
111 */
112
113 GLfloat n, f;
114
115 if (INSIDE_BEGIN_END(ctx)) {
116 gl_error( ctx, GL_INVALID_OPERATION, "glDepthRange" );
117 return;
118 }
119
120 n = (GLfloat) CLAMP( nearval, 0.0, 1.0 );
121 f = (GLfloat) CLAMP( farval, 0.0, 1.0 );
122
123 ctx->Viewport.Near = n;
124 ctx->Viewport.Far = f;
125 ctx->Viewport.Sz = DEPTH_SCALE * ((f - n) / 2.0);
126 ctx->Viewport.Tz = DEPTH_SCALE * ((f - n) / 2.0 + n);
127 }
128
129
130
131 /**********************************************************************/
132 /***** Depth Testing Functions *****/
133 /**********************************************************************/
134
135
136 /*
137 * Depth test horizontal spans of fragments. These functions are called
138 * via ctx->Driver.depth_test_span only.
139 *
140 * Input: n - number of pixels in the span
141 * x, y - location of leftmost pixel in span in window coords
142 * z - array [n] of integer depth values
143 * In/Out: mask - array [n] of flags (1=draw pixel, 0=don't draw)
144 * Return: number of pixels which passed depth test
145 */
146
147
148 /*
149 * glDepthFunc( any ) and glDepthMask( GL_TRUE or GL_FALSE ).
150 */
151 GLuint gl_depth_test_span_generic( GLcontext* ctx,
152 GLuint n, GLint x, GLint y,
153 const GLdepth z[],
154 GLubyte mask[] )
155 {
156 GLdepth *zptr = Z_ADDRESS( ctx, x, y );
157 GLubyte *m = mask;
158 GLuint i;
159 GLuint passed = 0;
160
161 /* switch cases ordered from most frequent to less frequent */
162 switch (ctx->Depth.Func) {
163 case GL_LESS:
164 if (ctx->Depth.Mask) {
165 /* Update Z buffer */
166 for (i=0; i<n; i++,zptr++,m++) {
167 if (*m) {
168 if (z[i] < *zptr) {
169 /* pass */
170 *zptr = z[i];
171 passed++;
172 }
173 else {
174 /* fail */
175 *m = 0;
176 }
177 }
178 }
179 }
180 else {
181 /* Don't update Z buffer */
182 for (i=0; i<n; i++,zptr++,m++) {
183 if (*m) {
184 if (z[i] < *zptr) {
185 /* pass */
186 passed++;
187 }
188 else {
189 *m = 0;
190 }
191 }
192 }
193 }
194 break;
195 case GL_LEQUAL:
196 if (ctx->Depth.Mask) {
197 /* Update Z buffer */
198 for (i=0;i<n;i++,zptr++,m++) {
199 if (*m) {
200 if (z[i] <= *zptr) {
201 *zptr = z[i];
202 passed++;
203 }
204 else {
205 *m = 0;
206 }
207 }
208 }
209 }
210 else {
211 /* Don't update Z buffer */
212 for (i=0;i<n;i++,zptr++,m++) {
213 if (*m) {
214 if (z[i] <= *zptr) {
215 /* pass */
216 passed++;
217 }
218 else {
219 *m = 0;
220 }
221 }
222 }
223 }
224 break;
225 case GL_GEQUAL:
226 if (ctx->Depth.Mask) {
227 /* Update Z buffer */
228 for (i=0;i<n;i++,zptr++,m++) {
229 if (*m) {
230 if (z[i] >= *zptr) {
231 *zptr = z[i];
232 passed++;
233 }
234 else {
235 *m = 0;
236 }
237 }
238 }
239 }
240 else {
241 /* Don't update Z buffer */
242 for (i=0;i<n;i++,zptr++,m++) {
243 if (*m) {
244 if (z[i] >= *zptr) {
245 /* pass */
246 passed++;
247 }
248 else {
249 *m = 0;
250 }
251 }
252 }
253 }
254 break;
255 case GL_GREATER:
256 if (ctx->Depth.Mask) {
257 /* Update Z buffer */
258 for (i=0;i<n;i++,zptr++,m++) {
259 if (*m) {
260 if (z[i] > *zptr) {
261 *zptr = z[i];
262 passed++;
263 }
264 else {
265 *m = 0;
266 }
267 }
268 }
269 }
270 else {
271 /* Don't update Z buffer */
272 for (i=0;i<n;i++,zptr++,m++) {
273 if (*m) {
274 if (z[i] > *zptr) {
275 /* pass */
276 passed++;
277 }
278 else {
279 *m = 0;
280 }
281 }
282 }
283 }
284 break;
285 case GL_NOTEQUAL:
286 if (ctx->Depth.Mask) {
287 /* Update Z buffer */
288 for (i=0;i<n;i++,zptr++,m++) {
289 if (*m) {
290 if (z[i] != *zptr) {
291 *zptr = z[i];
292 passed++;
293 }
294 else {
295 *m = 0;
296 }
297 }
298 }
299 }
300 else {
301 /* Don't update Z buffer */
302 for (i=0;i<n;i++,zptr++,m++) {
303 if (*m) {
304 if (z[i] != *zptr) {
305 /* pass */
306 passed++;
307 }
308 else {
309 *m = 0;
310 }
311 }
312 }
313 }
314 break;
315 case GL_EQUAL:
316 if (ctx->Depth.Mask) {
317 /* Update Z buffer */
318 for (i=0;i<n;i++,zptr++,m++) {
319 if (*m) {
320 if (z[i] == *zptr) {
321 *zptr = z[i];
322 passed++;
323 }
324 else {
325 *m =0;
326 }
327 }
328 }
329 }
330 else {
331 /* Don't update Z buffer */
332 for (i=0;i<n;i++,zptr++,m++) {
333 if (*m) {
334 if (z[i] == *zptr) {
335 /* pass */
336 passed++;
337 }
338 else {
339 *m =0;
340 }
341 }
342 }
343 }
344 break;
345 case GL_ALWAYS:
346 if (ctx->Depth.Mask) {
347 /* Update Z buffer */
348 for (i=0;i<n;i++,zptr++,m++) {
349 if (*m) {
350 *zptr = z[i];
351 passed++;
352 }
353 }
354 }
355 else {
356 /* Don't update Z buffer or mask */
357 passed = n;
358 }
359 break;
360 case GL_NEVER:
361 for (i=0;i<n;i++) {
362 mask[i] = 0;
363 }
364 break;
365 default:
366 abort();
367 } /*switch*/
368
369 return passed;
370 }
371
372
373
374 /*
375 * glDepthFunc(GL_LESS) and glDepthMask(GL_TRUE).
376 */
377 GLuint gl_depth_test_span_less( GLcontext* ctx,
378 GLuint n, GLint x, GLint y, const GLdepth z[],
379 GLubyte mask[] )
380 {
381 GLdepth *zptr = Z_ADDRESS( ctx, x, y );
382 GLuint i;
383 GLuint passed = 0;
384
385 for (i=0; i<n; i++) {
386 if (mask[i]) {
387 if (z[i] < zptr[i]) {
388 /* pass */
389 zptr[i] = z[i];
390 passed++;
391 }
392 else {
393 /* fail */
394 mask[i] = 0;
395 }
396 }
397 }
398 return passed;
399 }
400
401
402 /*
403 * glDepthFunc(GL_GREATER) and glDepthMask(GL_TRUE).
404 */
405 GLuint gl_depth_test_span_greater( GLcontext* ctx,
406 GLuint n, GLint x, GLint y,
407 const GLdepth z[],
408 GLubyte mask[] )
409 {
410 GLdepth *zptr = Z_ADDRESS( ctx, x, y );
411 GLuint i;
412 GLuint passed = 0;
413
414 for (i=0; i<n; i++) {
415 if (mask[i]) {
416 if (z[i] > zptr[i]) {
417 /* pass */
418 zptr[i] = z[i];
419 passed++;
420 }
421 else {
422 /* fail */
423 mask[i] = 0;
424 }
425 }
426 }
427 return passed;
428 }
429
430
431
432 /*
433 * Depth test an array of randomly positioned fragments.
434 */
435
436
437 #define ZADDR_SETUP GLdepth *depthbuffer = ctx->Buffer->Depth; \
438 GLint width = ctx->Buffer->Width;
439
440 #define ZADDR( X, Y ) (depthbuffer + (Y) * width + (X) )
441
442
443
444 /*
445 * glDepthFunc( any ) and glDepthMask( GL_TRUE or GL_FALSE ).
446 */
447 void gl_depth_test_pixels_generic( GLcontext* ctx,
448 GLuint n, const GLint x[], const GLint y[],
449 const GLdepth z[], GLubyte mask[] )
450 {
451 register GLdepth *zptr;
452 register GLuint i;
453
454 /* switch cases ordered from most frequent to less frequent */
455 switch (ctx->Depth.Func) {
456 case GL_LESS:
457 if (ctx->Depth.Mask) {
458 /* Update Z buffer */
459 for (i=0; i<n; i++) {
460 if (mask[i]) {
461 zptr = Z_ADDRESS(ctx,x[i],y[i]);
462 if (z[i] < *zptr) {
463 /* pass */
464 *zptr = z[i];
465 }
466 else {
467 /* fail */
468 mask[i] = 0;
469 }
470 }
471 }
472 }
473 else {
474 /* Don't update Z buffer */
475 for (i=0; i<n; i++) {
476 if (mask[i]) {
477 zptr = Z_ADDRESS(ctx,x[i],y[i]);
478 if (z[i] < *zptr) {
479 /* pass */
480 }
481 else {
482 /* fail */
483 mask[i] = 0;
484 }
485 }
486 }
487 }
488 break;
489 case GL_LEQUAL:
490 if (ctx->Depth.Mask) {
491 /* Update Z buffer */
492 for (i=0; i<n; i++) {
493 if (mask[i]) {
494 zptr = Z_ADDRESS(ctx,x[i],y[i]);
495 if (z[i] <= *zptr) {
496 /* pass */
497 *zptr = z[i];
498 }
499 else {
500 /* fail */
501 mask[i] = 0;
502 }
503 }
504 }
505 }
506 else {
507 /* Don't update Z buffer */
508 for (i=0; i<n; i++) {
509 if (mask[i]) {
510 zptr = Z_ADDRESS(ctx,x[i],y[i]);
511 if (z[i] <= *zptr) {
512 /* pass */
513 }
514 else {
515 /* fail */
516 mask[i] = 0;
517 }
518 }
519 }
520 }
521 break;
522 case GL_GEQUAL:
523 if (ctx->Depth.Mask) {
524 /* Update Z buffer */
525 for (i=0; i<n; i++) {
526 if (mask[i]) {
527 zptr = Z_ADDRESS(ctx,x[i],y[i]);
528 if (z[i] >= *zptr) {
529 /* pass */
530 *zptr = z[i];
531 }
532 else {
533 /* fail */
534 mask[i] = 0;
535 }
536 }
537 }
538 }
539 else {
540 /* Don't update Z buffer */
541 for (i=0; i<n; i++) {
542 if (mask[i]) {
543 zptr = Z_ADDRESS(ctx,x[i],y[i]);
544 if (z[i] >= *zptr) {
545 /* pass */
546 }
547 else {
548 /* fail */
549 mask[i] = 0;
550 }
551 }
552 }
553 }
554 break;
555 case GL_GREATER:
556 if (ctx->Depth.Mask) {
557 /* Update Z buffer */
558 for (i=0; i<n; i++) {
559 if (mask[i]) {
560 zptr = Z_ADDRESS(ctx,x[i],y[i]);
561 if (z[i] > *zptr) {
562 /* pass */
563 *zptr = z[i];
564 }
565 else {
566 /* fail */
567 mask[i] = 0;
568 }
569 }
570 }
571 }
572 else {
573 /* Don't update Z buffer */
574 for (i=0; i<n; i++) {
575 if (mask[i]) {
576 zptr = Z_ADDRESS(ctx,x[i],y[i]);
577 if (z[i] > *zptr) {
578 /* pass */
579 }
580 else {
581 /* fail */
582 mask[i] = 0;
583 }
584 }
585 }
586 }
587 break;
588 case GL_NOTEQUAL:
589 if (ctx->Depth.Mask) {
590 /* Update Z buffer */
591 for (i=0; i<n; i++) {
592 if (mask[i]) {
593 zptr = Z_ADDRESS(ctx,x[i],y[i]);
594 if (z[i] != *zptr) {
595 /* pass */
596 *zptr = z[i];
597 }
598 else {
599 /* fail */
600 mask[i] = 0;
601 }
602 }
603 }
604 }
605 else {
606 /* Don't update Z buffer */
607 for (i=0; i<n; i++) {
608 if (mask[i]) {
609 zptr = Z_ADDRESS(ctx,x[i],y[i]);
610 if (z[i] != *zptr) {
611 /* pass */
612 }
613 else {
614 /* fail */
615 mask[i] = 0;
616 }
617 }
618 }
619 }
620 break;
621 case GL_EQUAL:
622 if (ctx->Depth.Mask) {
623 /* Update Z buffer */
624 for (i=0; i<n; i++) {
625 if (mask[i]) {
626 zptr = Z_ADDRESS(ctx,x[i],y[i]);
627 if (z[i] == *zptr) {
628 /* pass */
629 *zptr = z[i];
630 }
631 else {
632 /* fail */
633 mask[i] = 0;
634 }
635 }
636 }
637 }
638 else {
639 /* Don't update Z buffer */
640 for (i=0; i<n; i++) {
641 if (mask[i]) {
642 zptr = Z_ADDRESS(ctx,x[i],y[i]);
643 if (z[i] == *zptr) {
644 /* pass */
645 }
646 else {
647 /* fail */
648 mask[i] = 0;
649 }
650 }
651 }
652 }
653 break;
654 case GL_ALWAYS:
655 if (ctx->Depth.Mask) {
656 /* Update Z buffer */
657 for (i=0; i<n; i++) {
658 if (mask[i]) {
659 zptr = Z_ADDRESS(ctx,x[i],y[i]);
660 *zptr = z[i];
661 }
662 }
663 }
664 else {
665 /* Don't update Z buffer or mask */
666 }
667 break;
668 case GL_NEVER:
669 /* depth test never passes */
670 for (i=0;i<n;i++) {
671 mask[i] = 0;
672 }
673 break;
674 default:
675 abort();
676 } /*switch*/
677 }
678
679
680
681 /*
682 * glDepthFunc( GL_LESS ) and glDepthMask( GL_TRUE ).
683 */
684 void gl_depth_test_pixels_less( GLcontext* ctx,
685 GLuint n, const GLint x[], const GLint y[],
686 const GLdepth z[], GLubyte mask[] )
687 {
688 GLdepth *zptr;
689 GLuint i;
690
691 for (i=0; i<n; i++) {
692 if (mask[i]) {
693 zptr = Z_ADDRESS(ctx,x[i],y[i]);
694 if (z[i] < *zptr) {
695 /* pass */
696 *zptr = z[i];
697 }
698 else {
699 /* fail */
700 mask[i] = 0;
701 }
702 }
703 }
704 }
705
706
707 /*
708 * glDepthFunc( GL_GREATER ) and glDepthMask( GL_TRUE ).
709 */
710 void gl_depth_test_pixels_greater( GLcontext* ctx,
711 GLuint n, const GLint x[], const GLint y[],
712 const GLdepth z[], GLubyte mask[] )
713 {
714 GLdepth *zptr;
715 GLuint i;
716
717 for (i=0; i<n; i++) {
718 if (mask[i]) {
719 zptr = Z_ADDRESS(ctx,x[i],y[i]);
720 if (z[i] > *zptr) {
721 /* pass */
722 *zptr = z[i];
723 }
724 else {
725 /* fail */
726 mask[i] = 0;
727 }
728 }
729 }
730 }
731
732
733
734
735 /**********************************************************************/
736 /***** Read Depth Buffer *****/
737 /**********************************************************************/
738
739
740 /*
741 * Return a span of depth values from the depth buffer as floats in [0,1].
742 * This function is only called through Driver.read_depth_span_float()
743 * Input: n - how many pixels
744 * x,y - location of first pixel
745 * Output: depth - the array of depth values
746 */
747 void gl_read_depth_span_float( GLcontext* ctx,
748 GLuint n, GLint x, GLint y, GLfloat depth[] )
749 {
750 GLdepth *zptr;
751 GLfloat scale;
752 GLuint i;
753
754 scale = 1.0F / DEPTH_SCALE;
755
756 if (ctx->Buffer->Depth) {
757 zptr = Z_ADDRESS( ctx, x, y );
758 for (i=0;i<n;i++) {
759 depth[i] = (GLfloat) zptr[i] * scale;
760 }
761 }
762 else {
763 for (i=0;i<n;i++) {
764 depth[i] = 0.0F;
765 }
766 }
767 }
768
769
770 /*
771 * Return a span of depth values from the depth buffer as integers in
772 * [0,MAX_DEPTH].
773 * This function is only called through Driver.read_depth_span_int()
774 * Input: n - how many pixels
775 * x,y - location of first pixel
776 * Output: depth - the array of depth values
777 */
778 void gl_read_depth_span_int( GLcontext* ctx,
779 GLuint n, GLint x, GLint y, GLdepth depth[] )
780 {
781 if (ctx->Buffer->Depth) {
782 GLdepth *zptr = Z_ADDRESS( ctx, x, y );
783 MEMCPY( depth, zptr, n * sizeof(GLdepth) );
784 }
785 else {
786 GLuint i;
787 for (i=0;i<n;i++) {
788 depth[i] = 0.0;
789 }
790 }
791 }
792
793
794
795 /**********************************************************************/
796 /***** Allocate and Clear Depth Buffer *****/
797 /**********************************************************************/
798
799
800
801 /*
802 * Allocate a new depth buffer. If there's already a depth buffer allocated
803 * it will be free()'d. The new depth buffer will be uniniitalized.
804 * This function is only called through Driver.alloc_depth_buffer.
805 */
806 void gl_alloc_depth_buffer( GLcontext* ctx )
807 {
808 /* deallocate current depth buffer if present */
809 if (ctx->Buffer->Depth) {
810 free(ctx->Buffer->Depth);
811 ctx->Buffer->Depth = NULL;
812 }
813
814 /* allocate new depth buffer, but don't initialize it */
815 ctx->Buffer->Depth = (GLdepth *) malloc( ctx->Buffer->Width
816 * ctx->Buffer->Height
817 * sizeof(GLdepth) );
818 if (!ctx->Buffer->Depth) {
819 /* out of memory */
820 ctx->Depth.Test = GL_FALSE;
821 gl_error( ctx, GL_OUT_OF_MEMORY, "Couldn't allocate depth buffer" );
822 }
823 }
824
825
826
827
828 /*
829 * Clear the depth buffer. If the depth buffer doesn't exist yet we'll
830 * allocate it now.
831 * This function is only called through Driver.clear_depth_buffer.
832 */
833 void gl_clear_depth_buffer( GLcontext* ctx )
834 {
835 GLdepth clear_value = (GLdepth) (ctx->Depth.Clear * DEPTH_SCALE);
836
837 if (ctx->Visual->DepthBits==0 || !ctx->Buffer->Depth) {
838 /* no depth buffer */
839 return;
840 }
841
842 /* The loops in this function have been written so the IRIX 5.3
843 * C compiler can unroll them. Hopefully other compilers can too!
844 */
845
846 if (ctx->Scissor.Enabled) {
847 /* only clear scissor region */
848 GLint y;
849 for (y=ctx->Buffer->Ymin; y<=ctx->Buffer->Ymax; y++) {
850 GLdepth *d = Z_ADDRESS( ctx, ctx->Buffer->Xmin, y );
851 GLint n = ctx->Buffer->Xmax - ctx->Buffer->Xmin + 1;
852 do {
853 *d++ = clear_value;
854 n--;
855 } while (n);
856 }
857 }
858 else {
859 /* clear whole buffer */
860 if (sizeof(GLdepth)==2 && (clear_value&0xff)==(clear_value>>8)) {
861 /* lower and upper bytes of clear_value are same, use MEMSET */
862 MEMSET( ctx->Buffer->Depth, clear_value&0xff,
863 2*ctx->Buffer->Width*ctx->Buffer->Height);
864 }
865 else {
866 GLdepth *d = ctx->Buffer->Depth;
867 GLint n = ctx->Buffer->Width * ctx->Buffer->Height;
868 while (n>=16) {
869 d[0] = clear_value; d[1] = clear_value;
870 d[2] = clear_value; d[3] = clear_value;
871 d[4] = clear_value; d[5] = clear_value;
872 d[6] = clear_value; d[7] = clear_value;
873 d[8] = clear_value; d[9] = clear_value;
874 d[10] = clear_value; d[11] = clear_value;
875 d[12] = clear_value; d[13] = clear_value;
876 d[14] = clear_value; d[15] = clear_value;
877 d += 16;
878 n -= 16;
879 }
880 while (n>0) {
881 *d++ = clear_value;
882 n--;
883 }
884 }
885 }
886 }
887
888
889