03a5ec9735153c5d3486eecf6d1a374b655274da
1 /* $Id: s_depth.c,v 1.15 2002/02/02 21:40:33 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 #include "s_context.h"
40 * Return address of depth buffer value for given window coord.
43 _mesa_zbuffer_address(GLcontext
*ctx
, GLint x
, GLint y
)
45 if (ctx
->Visual
.depthBits
<= 16)
46 return (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
47 + ctx
->DrawBuffer
->Width
* y
+ x
;
49 return (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
50 + ctx
->DrawBuffer
->Width
* y
+ x
;
54 #define Z_ADDRESS16( CTX, X, Y ) \
55 ( ((GLushort *) (CTX)->DrawBuffer->DepthBuffer) \
56 + (CTX)->DrawBuffer->Width * (Y) + (X) )
58 #define Z_ADDRESS32( CTX, X, Y ) \
59 ( ((GLuint *) (CTX)->DrawBuffer->DepthBuffer) \
60 + (CTX)->DrawBuffer->Width * (Y) + (X) )
64 /**********************************************************************/
65 /***** Depth Testing Functions *****/
66 /**********************************************************************/
70 * Do depth test for an array of fragments. This is used both for
71 * software and hardware Z buffers.
72 * Input: zbuffer - array of z values in the zbuffer
73 * z - array of fragment z values
74 * Return: number of fragments which pass the test.
77 depth_test_span16( GLcontext
*ctx
, GLuint n
,
78 GLushort zbuffer
[], const GLdepth z
[], GLubyte mask
[] )
82 /* switch cases ordered from most frequent to less frequent */
83 switch (ctx
->Depth
.Func
) {
85 if (ctx
->Depth
.Mask
) {
90 if (z
[i
] < zbuffer
[i
]) {
103 /* Don't update Z buffer */
105 for (i
=0; i
<n
; i
++) {
107 if (z
[i
] < zbuffer
[i
]) {
119 if (ctx
->Depth
.Mask
) {
120 /* Update Z buffer */
124 if (z
[i
] <= zbuffer
[i
]) {
135 /* Don't update Z buffer */
139 if (z
[i
] <= zbuffer
[i
]) {
151 if (ctx
->Depth
.Mask
) {
152 /* Update Z buffer */
156 if (z
[i
] >= zbuffer
[i
]) {
167 /* Don't update Z buffer */
171 if (z
[i
] >= zbuffer
[i
]) {
183 if (ctx
->Depth
.Mask
) {
184 /* Update Z buffer */
188 if (z
[i
] > zbuffer
[i
]) {
199 /* Don't update Z buffer */
203 if (z
[i
] > zbuffer
[i
]) {
215 if (ctx
->Depth
.Mask
) {
216 /* Update Z buffer */
220 if (z
[i
] != zbuffer
[i
]) {
231 /* Don't update Z buffer */
235 if (z
[i
] != zbuffer
[i
]) {
247 if (ctx
->Depth
.Mask
) {
248 /* Update Z buffer */
252 if (z
[i
] == zbuffer
[i
]) {
263 /* Don't update Z buffer */
267 if (z
[i
] == zbuffer
[i
]) {
279 if (ctx
->Depth
.Mask
) {
280 /* Update Z buffer */
290 /* Don't update Z buffer or mask */
295 BZERO(mask
, n
* sizeof(GLubyte
));
298 _mesa_problem(ctx
, "Bad depth func in depth_test_span16");
306 depth_test_span32( GLcontext
*ctx
, GLuint n
,
307 GLuint zbuffer
[], const GLdepth z
[], GLubyte mask
[] )
311 /* switch cases ordered from most frequent to less frequent */
312 switch (ctx
->Depth
.Func
) {
314 if (ctx
->Depth
.Mask
) {
315 /* Update Z buffer */
317 for (i
=0; i
<n
; i
++) {
319 if (z
[i
] < zbuffer
[i
]) {
332 /* Don't update Z buffer */
334 for (i
=0; i
<n
; i
++) {
336 if (z
[i
] < zbuffer
[i
]) {
348 if (ctx
->Depth
.Mask
) {
349 /* Update Z buffer */
353 if (z
[i
] <= zbuffer
[i
]) {
364 /* Don't update Z buffer */
368 if (z
[i
] <= zbuffer
[i
]) {
380 if (ctx
->Depth
.Mask
) {
381 /* Update Z buffer */
385 if (z
[i
] >= zbuffer
[i
]) {
396 /* Don't update Z buffer */
400 if (z
[i
] >= zbuffer
[i
]) {
412 if (ctx
->Depth
.Mask
) {
413 /* Update Z buffer */
417 if (z
[i
] > zbuffer
[i
]) {
428 /* Don't update Z buffer */
432 if (z
[i
] > zbuffer
[i
]) {
444 if (ctx
->Depth
.Mask
) {
445 /* Update Z buffer */
449 if (z
[i
] != zbuffer
[i
]) {
460 /* Don't update Z buffer */
464 if (z
[i
] != zbuffer
[i
]) {
476 if (ctx
->Depth
.Mask
) {
477 /* Update Z buffer */
481 if (z
[i
] == zbuffer
[i
]) {
492 /* Don't update Z buffer */
496 if (z
[i
] == zbuffer
[i
]) {
508 if (ctx
->Depth
.Mask
) {
509 /* Update Z buffer */
519 /* Don't update Z buffer or mask */
524 BZERO(mask
, n
* sizeof(GLubyte
));
527 _mesa_problem(ctx
, "Bad depth func in depth_test_span32");
537 _old_depth_test_span( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
538 const GLdepth z
[], GLubyte mask
[] )
540 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
541 if (swrast
->Driver
.ReadDepthSpan
) {
542 /* hardware-based depth buffer */
543 GLdepth zbuffer
[MAX_WIDTH
];
545 (*swrast
->Driver
.ReadDepthSpan
)(ctx
, n
, x
, y
, zbuffer
);
546 passed
= depth_test_span32(ctx
, n
, zbuffer
, z
, mask
);
547 assert(swrast
->Driver
.WriteDepthSpan
);
548 (*swrast
->Driver
.WriteDepthSpan
)(ctx
, n
, x
, y
, zbuffer
, mask
);
552 /* software depth buffer */
553 if (ctx
->Visual
.depthBits
<= 16) {
554 GLushort
*zptr
= (GLushort
*) Z_ADDRESS16(ctx
, x
, y
);
555 GLuint passed
= depth_test_span16(ctx
, n
, zptr
, z
, mask
);
559 GLuint
*zptr
= (GLuint
*) Z_ADDRESS32(ctx
, x
, y
);
560 GLuint passed
= depth_test_span32(ctx
, n
, zptr
, z
, mask
);
568 * Apply depth test to span of fragments. Hardware or software z buffer.
571 depth_test_span( GLcontext
*ctx
, struct sw_span
*span
)
573 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
575 ASSERT(span
->arrayMask
& SPAN_Z
);
577 if (swrast
->Driver
.ReadDepthSpan
) {
578 /* hardware-based depth buffer */
579 GLdepth zbuffer
[MAX_WIDTH
];
581 (*swrast
->Driver
.ReadDepthSpan
)(ctx
, span
->end
, span
->x
, span
->y
, zbuffer
);
582 passed
= depth_test_span32(ctx
, span
->end
,
583 zbuffer
, span
->zArray
, span
->mask
);
584 ASSERT(swrast
->Driver
.WriteDepthSpan
);
585 (*swrast
->Driver
.WriteDepthSpan
)(ctx
, span
->end
, span
->x
, span
->y
, zbuffer
, span
->mask
);
586 if (passed
< span
->end
)
587 span
->writeAll
= GL_FALSE
;
592 /* software depth buffer */
593 if (ctx
->Visual
.depthBits
<= 16) {
594 GLushort
*zptr
= (GLushort
*) Z_ADDRESS16(ctx
, span
->x
, span
->y
);
595 passed
= depth_test_span16(ctx
, span
->end
, zptr
, span
->zArray
, span
->mask
);
598 GLuint
*zptr
= (GLuint
*) Z_ADDRESS32(ctx
, span
->x
, span
->y
);
599 passed
= depth_test_span32(ctx
, span
->end
, zptr
, span
->zArray
, span
->mask
);
601 if (passed
< span
->end
)
602 span
->writeAll
= GL_FALSE
;
611 * Do depth testing for an array of fragments using software Z buffer.
614 software_depth_test_pixels16( GLcontext
*ctx
, GLuint n
,
615 const GLint x
[], const GLint y
[],
616 const GLdepth z
[], GLubyte mask
[] )
618 /* switch cases ordered from most frequent to less frequent */
619 switch (ctx
->Depth
.Func
) {
621 if (ctx
->Depth
.Mask
) {
622 /* Update Z buffer */
624 for (i
=0; i
<n
; i
++) {
626 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
639 /* Don't update Z buffer */
641 for (i
=0; i
<n
; i
++) {
643 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
656 if (ctx
->Depth
.Mask
) {
657 /* Update Z buffer */
659 for (i
=0; i
<n
; i
++) {
661 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
674 /* Don't update Z buffer */
676 for (i
=0; i
<n
; i
++) {
678 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
691 if (ctx
->Depth
.Mask
) {
692 /* Update Z buffer */
694 for (i
=0; i
<n
; i
++) {
696 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
709 /* Don't update Z buffer */
711 for (i
=0; i
<n
; i
++) {
713 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
726 if (ctx
->Depth
.Mask
) {
727 /* Update Z buffer */
729 for (i
=0; i
<n
; i
++) {
731 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
744 /* Don't update Z buffer */
746 for (i
=0; i
<n
; i
++) {
748 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
761 if (ctx
->Depth
.Mask
) {
762 /* Update Z buffer */
764 for (i
=0; i
<n
; i
++) {
766 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
779 /* Don't update Z buffer */
781 for (i
=0; i
<n
; i
++) {
783 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
796 if (ctx
->Depth
.Mask
) {
797 /* Update Z buffer */
799 for (i
=0; i
<n
; i
++) {
801 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
814 /* Don't update Z buffer */
816 for (i
=0; i
<n
; i
++) {
818 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
831 if (ctx
->Depth
.Mask
) {
832 /* Update Z buffer */
834 for (i
=0; i
<n
; i
++) {
836 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
842 /* Don't update Z buffer or mask */
846 /* depth test never passes */
847 BZERO(mask
, n
* sizeof(GLubyte
));
850 _mesa_problem(ctx
, "Bad depth func in software_depth_test_pixels");
857 * Do depth testing for an array of fragments using software Z buffer.
860 software_depth_test_pixels32( GLcontext
*ctx
, GLuint n
,
861 const GLint x
[], const GLint y
[],
862 const GLdepth z
[], GLubyte mask
[] )
864 /* switch cases ordered from most frequent to less frequent */
865 switch (ctx
->Depth
.Func
) {
867 if (ctx
->Depth
.Mask
) {
868 /* Update Z buffer */
870 for (i
=0; i
<n
; i
++) {
872 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
885 /* Don't update Z buffer */
887 for (i
=0; i
<n
; i
++) {
889 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
902 if (ctx
->Depth
.Mask
) {
903 /* Update Z buffer */
905 for (i
=0; i
<n
; i
++) {
907 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
920 /* Don't update Z buffer */
922 for (i
=0; i
<n
; i
++) {
924 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
937 if (ctx
->Depth
.Mask
) {
938 /* Update Z buffer */
940 for (i
=0; i
<n
; i
++) {
942 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
955 /* Don't update Z buffer */
957 for (i
=0; i
<n
; i
++) {
959 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
972 if (ctx
->Depth
.Mask
) {
973 /* Update Z buffer */
975 for (i
=0; i
<n
; i
++) {
977 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
990 /* Don't update Z buffer */
992 for (i
=0; i
<n
; i
++) {
994 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1007 if (ctx
->Depth
.Mask
) {
1008 /* Update Z buffer */
1010 for (i
=0; i
<n
; i
++) {
1012 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1013 if (z
[i
] != *zptr
) {
1025 /* Don't update Z buffer */
1027 for (i
=0; i
<n
; i
++) {
1029 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1030 if (z
[i
] != *zptr
) {
1042 if (ctx
->Depth
.Mask
) {
1043 /* Update Z buffer */
1045 for (i
=0; i
<n
; i
++) {
1047 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1048 if (z
[i
] == *zptr
) {
1060 /* Don't update Z buffer */
1062 for (i
=0; i
<n
; i
++) {
1064 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1065 if (z
[i
] == *zptr
) {
1077 if (ctx
->Depth
.Mask
) {
1078 /* Update Z buffer */
1080 for (i
=0; i
<n
; i
++) {
1082 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1088 /* Don't update Z buffer or mask */
1092 /* depth test never passes */
1093 BZERO(mask
, n
* sizeof(GLubyte
));
1096 _mesa_problem(ctx
, "Bad depth func in software_depth_test_pixels");
1103 * Do depth testing for an array of pixels using hardware Z buffer.
1104 * Input/output: zbuffer - array of depth values from Z buffer
1105 * Input: z - array of fragment z values.
1108 hardware_depth_test_pixels( GLcontext
*ctx
, GLuint n
, GLdepth zbuffer
[],
1109 const GLdepth z
[], GLubyte mask
[] )
1111 /* switch cases ordered from most frequent to less frequent */
1112 switch (ctx
->Depth
.Func
) {
1114 if (ctx
->Depth
.Mask
) {
1115 /* Update Z buffer */
1117 for (i
=0; i
<n
; i
++) {
1119 if (z
[i
] < zbuffer
[i
]) {
1131 /* Don't update Z buffer */
1133 for (i
=0; i
<n
; i
++) {
1135 if (z
[i
] < zbuffer
[i
]) {
1147 if (ctx
->Depth
.Mask
) {
1148 /* Update Z buffer */
1150 for (i
=0; i
<n
; i
++) {
1152 if (z
[i
] <= zbuffer
[i
]) {
1164 /* Don't update Z buffer */
1166 for (i
=0; i
<n
; i
++) {
1168 if (z
[i
] <= zbuffer
[i
]) {
1180 if (ctx
->Depth
.Mask
) {
1181 /* Update Z buffer */
1183 for (i
=0; i
<n
; i
++) {
1185 if (z
[i
] >= zbuffer
[i
]) {
1197 /* Don't update Z buffer */
1199 for (i
=0; i
<n
; i
++) {
1201 if (z
[i
] >= zbuffer
[i
]) {
1213 if (ctx
->Depth
.Mask
) {
1214 /* Update Z buffer */
1216 for (i
=0; i
<n
; i
++) {
1218 if (z
[i
] > zbuffer
[i
]) {
1230 /* Don't update Z buffer */
1232 for (i
=0; i
<n
; i
++) {
1234 if (z
[i
] > zbuffer
[i
]) {
1246 if (ctx
->Depth
.Mask
) {
1247 /* Update Z buffer */
1249 for (i
=0; i
<n
; i
++) {
1251 if (z
[i
] != zbuffer
[i
]) {
1263 /* Don't update Z buffer */
1265 for (i
=0; i
<n
; i
++) {
1267 if (z
[i
] != zbuffer
[i
]) {
1279 if (ctx
->Depth
.Mask
) {
1280 /* Update Z buffer */
1282 for (i
=0; i
<n
; i
++) {
1284 if (z
[i
] == zbuffer
[i
]) {
1296 /* Don't update Z buffer */
1298 for (i
=0; i
<n
; i
++) {
1300 if (z
[i
] == zbuffer
[i
]) {
1312 if (ctx
->Depth
.Mask
) {
1313 /* Update Z buffer */
1315 for (i
=0; i
<n
; i
++) {
1322 /* Don't update Z buffer or mask */
1326 /* depth test never passes */
1327 BZERO(mask
, n
* sizeof(GLubyte
));
1330 _mesa_problem(ctx
, "Bad depth func in hardware_depth_test_pixels");
1335 _mesa_depth_test_pixels( GLcontext
*ctx
,
1336 GLuint n
, const GLint x
[], const GLint y
[],
1337 const GLdepth z
[], GLubyte mask
[] )
1339 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1340 if (swrast
->Driver
.ReadDepthPixels
) {
1341 /* read depth values from hardware Z buffer */
1342 GLdepth zbuffer
[MAX_WIDTH
];
1343 (*swrast
->Driver
.ReadDepthPixels
)(ctx
, n
, x
, y
, zbuffer
);
1345 hardware_depth_test_pixels( ctx
, n
, zbuffer
, z
, mask
);
1347 /* update hardware Z buffer with new values */
1348 assert(swrast
->Driver
.WriteDepthPixels
);
1349 (*swrast
->Driver
.WriteDepthPixels
)(ctx
, n
, x
, y
, zbuffer
, mask
);
1352 /* software depth testing */
1353 if (ctx
->Visual
.depthBits
<= 16)
1354 software_depth_test_pixels16(ctx
, n
, x
, y
, z
, mask
);
1356 software_depth_test_pixels32(ctx
, n
, x
, y
, z
, mask
);
1363 _mesa_depth_test_span( GLcontext
*ctx
, struct sw_span
*span
)
1365 if (span
->arrayMask
& SPAN_XY
) {
1366 _mesa_depth_test_pixels(ctx
, span
->end
,
1367 span
->xArray
, span
->yArray
,
1368 span
->zArray
, span
->mask
);
1372 return depth_test_span(ctx
, span
);
1378 /**********************************************************************/
1379 /***** Read Depth Buffer *****/
1380 /**********************************************************************/
1384 * Read a span of depth values from the depth buffer.
1385 * This function does clipping before calling the device driver function.
1388 _mesa_read_depth_span( GLcontext
*ctx
,
1389 GLint n
, GLint x
, GLint y
, GLdepth depth
[] )
1391 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1393 if (y
< 0 || y
>= ctx
->DrawBuffer
->Height
||
1394 x
+ (GLint
) n
<= 0 || x
>= ctx
->DrawBuffer
->Width
) {
1395 /* span is completely outside framebuffer */
1397 for (i
= 0; i
< n
; i
++)
1405 for (i
= 0; i
< dx
; i
++)
1411 if (x
+ n
> ctx
->DrawBuffer
->Width
) {
1412 GLint dx
= x
+ n
- ctx
->DrawBuffer
->Width
;
1414 for (i
= 0; i
< dx
; i
++)
1415 depth
[n
- i
- 1] = 0;
1422 if (ctx
->DrawBuffer
->DepthBuffer
) {
1423 /* read from software depth buffer */
1424 if (ctx
->Visual
.depthBits
<= 16) {
1425 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1427 for (i
= 0; i
< n
; i
++) {
1432 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1434 for (i
= 0; i
< n
; i
++) {
1439 else if (swrast
->Driver
.ReadDepthSpan
) {
1440 /* read from hardware depth buffer */
1441 (*swrast
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, depth
);
1444 /* no depth buffer */
1445 BZERO(depth
, n
* sizeof(GLfloat
));
1454 * Return a span of depth values from the depth buffer as floats in [0,1].
1455 * This is used for both hardware and software depth buffers.
1456 * Input: n - how many pixels
1457 * x,y - location of first pixel
1458 * Output: depth - the array of depth values
1461 _mesa_read_depth_span_float( GLcontext
*ctx
,
1462 GLint n
, GLint x
, GLint y
, GLfloat depth
[] )
1464 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1465 const GLfloat scale
= 1.0F
/ ctx
->DepthMaxF
;
1467 if (y
< 0 || y
>= ctx
->DrawBuffer
->Height
||
1468 x
+ (GLint
) n
<= 0 || x
>= ctx
->DrawBuffer
->Width
) {
1469 /* span is completely outside framebuffer */
1471 for (i
= 0; i
< n
; i
++)
1479 for (i
= 0; i
< dx
; i
++)
1484 if (x
+ n
> ctx
->DrawBuffer
->Width
) {
1485 GLint dx
= x
+ n
- ctx
->DrawBuffer
->Width
;
1487 for (i
= 0; i
< dx
; i
++)
1488 depth
[n
- i
- 1] = 0.0F
;
1495 if (ctx
->DrawBuffer
->DepthBuffer
) {
1496 /* read from software depth buffer */
1497 if (ctx
->Visual
.depthBits
<= 16) {
1498 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1500 for (i
= 0; i
< n
; i
++) {
1501 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1505 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1507 for (i
= 0; i
< n
; i
++) {
1508 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1512 else if (swrast
->Driver
.ReadDepthSpan
) {
1513 /* read from hardware depth buffer */
1514 GLdepth d
[MAX_WIDTH
];
1516 assert(n
<= MAX_WIDTH
);
1517 (*swrast
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, d
);
1518 for (i
= 0; i
< n
; i
++) {
1519 depth
[i
] = d
[i
] * scale
;
1523 /* no depth buffer */
1524 BZERO(depth
, n
* sizeof(GLfloat
));
1530 /**********************************************************************/
1531 /***** Allocate and Clear Depth Buffer *****/
1532 /**********************************************************************/
1537 * Allocate a new depth buffer. If there's already a depth buffer allocated
1538 * it will be free()'d. The new depth buffer will be uniniitalized.
1539 * This function is only called through Driver.alloc_depth_buffer.
1542 _mesa_alloc_depth_buffer( GLcontext
*ctx
)
1544 /* deallocate current depth buffer if present */
1545 if (ctx
->DrawBuffer
->UseSoftwareDepthBuffer
) {
1546 GLint bytesPerValue
;
1548 if (ctx
->DrawBuffer
->DepthBuffer
) {
1549 FREE(ctx
->DrawBuffer
->DepthBuffer
);
1550 ctx
->DrawBuffer
->DepthBuffer
= NULL
;
1553 /* allocate new depth buffer, but don't initialize it */
1554 if (ctx
->Visual
.depthBits
<= 16)
1555 bytesPerValue
= sizeof(GLushort
);
1557 bytesPerValue
= sizeof(GLuint
);
1559 ctx
->DrawBuffer
->DepthBuffer
= MALLOC( ctx
->DrawBuffer
->Width
1560 * ctx
->DrawBuffer
->Height
1563 if (!ctx
->DrawBuffer
->DepthBuffer
) {
1565 ctx
->Depth
.Test
= GL_FALSE
;
1566 ctx
->NewState
|= _NEW_DEPTH
;
1567 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "Couldn't allocate depth buffer" );
1576 * Clear the depth buffer. If the depth buffer doesn't exist yet we'll
1578 * This function is only called through Driver.clear_depth_buffer.
1581 _mesa_clear_depth_buffer( GLcontext
*ctx
)
1583 if (ctx
->Visual
.depthBits
== 0
1584 || !ctx
->DrawBuffer
->DepthBuffer
1585 || !ctx
->Depth
.Mask
) {
1586 /* no depth buffer, or writing to it is disabled */
1590 /* The loops in this function have been written so the IRIX 5.3
1591 * C compiler can unroll them. Hopefully other compilers can too!
1594 if (ctx
->Scissor
.Enabled
) {
1595 /* only clear scissor region */
1596 if (ctx
->Visual
.depthBits
<= 16) {
1597 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1598 const GLint rows
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
1599 const GLint cols
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
1600 const GLint rowStride
= ctx
->DrawBuffer
->Width
;
1601 GLushort
*dRow
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
1602 + ctx
->DrawBuffer
->_Ymin
* rowStride
+ ctx
->DrawBuffer
->_Xmin
;
1604 for (i
= 0; i
< rows
; i
++) {
1605 for (j
= 0; j
< cols
; j
++) {
1606 dRow
[j
] = clearValue
;
1612 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1613 const GLint rows
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
1614 const GLint cols
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
1615 const GLint rowStride
= ctx
->DrawBuffer
->Width
;
1616 GLuint
*dRow
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
1617 + ctx
->DrawBuffer
->_Ymin
* rowStride
+ ctx
->DrawBuffer
->_Xmin
;
1619 for (i
= 0; i
< rows
; i
++) {
1620 for (j
= 0; j
< cols
; j
++) {
1621 dRow
[j
] = clearValue
;
1628 /* clear whole buffer */
1629 if (ctx
->Visual
.depthBits
<= 16) {
1630 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1631 if ((clearValue
& 0xff) == (clearValue
>> 8)) {
1632 if (clearValue
== 0) {
1633 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1634 2*ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
);
1637 /* lower and upper bytes of clear_value are same, use MEMSET */
1638 MEMSET( ctx
->DrawBuffer
->DepthBuffer
, clearValue
& 0xff,
1639 2 * ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
);
1643 GLushort
*d
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
;
1644 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1646 d
[0] = clearValue
; d
[1] = clearValue
;
1647 d
[2] = clearValue
; d
[3] = clearValue
;
1648 d
[4] = clearValue
; d
[5] = clearValue
;
1649 d
[6] = clearValue
; d
[7] = clearValue
;
1650 d
[8] = clearValue
; d
[9] = clearValue
;
1651 d
[10] = clearValue
; d
[11] = clearValue
;
1652 d
[12] = clearValue
; d
[13] = clearValue
;
1653 d
[14] = clearValue
; d
[15] = clearValue
;
1664 /* >16 bit depth buffer */
1665 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1666 if (clearValue
== 0) {
1667 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1668 ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
*sizeof(GLuint
));
1671 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1672 GLuint
*d
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
;
1674 d
[0] = clearValue
; d
[1] = clearValue
;
1675 d
[2] = clearValue
; d
[3] = clearValue
;
1676 d
[4] = clearValue
; d
[5] = clearValue
;
1677 d
[6] = clearValue
; d
[7] = clearValue
;
1678 d
[8] = clearValue
; d
[9] = clearValue
;
1679 d
[10] = clearValue
; d
[11] = clearValue
;
1680 d
[12] = clearValue
; d
[13] = clearValue
;
1681 d
[14] = clearValue
; d
[15] = clearValue
;