1 /* $Id: s_depth.c,v 1.11 2002/01/21 18:12:34 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"
41 * Return address of depth buffer value for given window coord.
44 _mesa_zbuffer_address(GLcontext
*ctx
, GLint x
, GLint y
)
46 if (ctx
->Visual
.depthBits
<= 16)
47 return (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
+ ctx
->DrawBuffer
->Width
* y
+ x
;
49 return (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
+ ctx
->DrawBuffer
->Width
* y
+ x
;
53 #define Z_ADDRESS16( CTX, X, Y ) \
54 ( ((GLushort *) (CTX)->DrawBuffer->DepthBuffer) \
55 + (CTX)->DrawBuffer->Width * (Y) + (X) )
57 #define Z_ADDRESS32( CTX, X, Y ) \
58 ( ((GLuint *) (CTX)->DrawBuffer->DepthBuffer) \
59 + (CTX)->DrawBuffer->Width * (Y) + (X) )
63 /**********************************************************************/
64 /***** Depth Testing Functions *****/
65 /**********************************************************************/
69 * Do depth test for an array of fragments. This is used both for
70 * software and hardware Z buffers.
71 * Input: zbuffer - array of z values in the zbuffer
72 * z - array of fragment z values
73 * Return: number of fragments which pass the test.
76 depth_test_span16( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
77 GLushort zbuffer
[], const GLdepth z
[], GLubyte mask
[] )
81 /* switch cases ordered from most frequent to less frequent */
82 switch (ctx
->Depth
.Func
) {
84 if (ctx
->Depth
.Mask
) {
89 if (z
[i
] < zbuffer
[i
]) {
102 /* Don't update Z buffer */
104 for (i
=0; i
<n
; i
++) {
106 if (z
[i
] < zbuffer
[i
]) {
118 if (ctx
->Depth
.Mask
) {
119 /* Update Z buffer */
123 if (z
[i
] <= zbuffer
[i
]) {
134 /* Don't update Z buffer */
138 if (z
[i
] <= zbuffer
[i
]) {
150 if (ctx
->Depth
.Mask
) {
151 /* Update Z buffer */
155 if (z
[i
] >= zbuffer
[i
]) {
166 /* Don't update Z buffer */
170 if (z
[i
] >= zbuffer
[i
]) {
182 if (ctx
->Depth
.Mask
) {
183 /* Update Z buffer */
187 if (z
[i
] > zbuffer
[i
]) {
198 /* Don't update Z buffer */
202 if (z
[i
] > zbuffer
[i
]) {
214 if (ctx
->Depth
.Mask
) {
215 /* Update Z buffer */
219 if (z
[i
] != zbuffer
[i
]) {
230 /* Don't update Z buffer */
234 if (z
[i
] != zbuffer
[i
]) {
246 if (ctx
->Depth
.Mask
) {
247 /* Update Z buffer */
251 if (z
[i
] == zbuffer
[i
]) {
262 /* Don't update Z buffer */
266 if (z
[i
] == zbuffer
[i
]) {
278 if (ctx
->Depth
.Mask
) {
279 /* Update Z buffer */
289 /* Don't update Z buffer or mask */
294 BZERO(mask
, n
* sizeof(GLubyte
));
297 _mesa_problem(ctx
, "Bad depth func in depth_test_span16");
305 depth_test_span32( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
306 GLuint zbuffer
[], const GLdepth z
[], GLubyte mask
[] )
310 /* switch cases ordered from most frequent to less frequent */
311 switch (ctx
->Depth
.Func
) {
313 if (ctx
->Depth
.Mask
) {
314 /* Update Z buffer */
316 for (i
=0; i
<n
; i
++) {
318 if (z
[i
] < zbuffer
[i
]) {
331 /* Don't update Z buffer */
333 for (i
=0; i
<n
; i
++) {
335 if (z
[i
] < zbuffer
[i
]) {
347 if (ctx
->Depth
.Mask
) {
348 /* Update Z buffer */
352 if (z
[i
] <= zbuffer
[i
]) {
363 /* Don't update Z buffer */
367 if (z
[i
] <= zbuffer
[i
]) {
379 if (ctx
->Depth
.Mask
) {
380 /* Update Z buffer */
384 if (z
[i
] >= zbuffer
[i
]) {
395 /* Don't update Z buffer */
399 if (z
[i
] >= zbuffer
[i
]) {
411 if (ctx
->Depth
.Mask
) {
412 /* Update Z buffer */
416 if (z
[i
] > zbuffer
[i
]) {
427 /* Don't update Z buffer */
431 if (z
[i
] > zbuffer
[i
]) {
443 if (ctx
->Depth
.Mask
) {
444 /* Update Z buffer */
448 if (z
[i
] != zbuffer
[i
]) {
459 /* Don't update Z buffer */
463 if (z
[i
] != zbuffer
[i
]) {
475 if (ctx
->Depth
.Mask
) {
476 /* Update Z buffer */
480 if (z
[i
] == zbuffer
[i
]) {
491 /* Don't update Z buffer */
495 if (z
[i
] == zbuffer
[i
]) {
507 if (ctx
->Depth
.Mask
) {
508 /* Update Z buffer */
518 /* Don't update Z buffer or mask */
523 BZERO(mask
, n
* sizeof(GLubyte
));
526 _mesa_problem(ctx
, "Bad depth func in depth_test_span32");
535 * Apply depth test to span of fragments. Hardware or software z buffer.
538 _old_depth_test_span( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
539 const GLdepth z
[], GLubyte mask
[] )
541 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
542 if (swrast
->Driver
.ReadDepthSpan
) {
543 /* hardware-based depth buffer */
544 GLdepth zbuffer
[MAX_WIDTH
];
546 (*swrast
->Driver
.ReadDepthSpan
)(ctx
, n
, x
, y
, zbuffer
);
547 passed
= depth_test_span32(ctx
, n
, x
, y
, zbuffer
, z
, mask
);
548 assert(swrast
->Driver
.WriteDepthSpan
);
549 (*swrast
->Driver
.WriteDepthSpan
)(ctx
, n
, x
, y
, zbuffer
, mask
);
553 /* software depth buffer */
554 if (ctx
->Visual
.depthBits
<= 16) {
555 GLushort
*zptr
= (GLushort
*) Z_ADDRESS16(ctx
, x
, y
);
556 GLuint passed
= depth_test_span16(ctx
, n
, x
, y
, zptr
, z
, mask
);
560 GLuint
*zptr
= (GLuint
*) Z_ADDRESS32(ctx
, x
, y
);
561 GLuint passed
= depth_test_span32(ctx
, n
, x
, y
, zptr
, z
, mask
);
568 * Apply depth test to span of fragments. Hardware or software z buffer.
571 _mesa_depth_test_span( GLcontext
*ctx
, struct sw_span
*span
)
573 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
575 ASSERT(span
->activeMask
& SPAN_Z
);
576 ASSERT(span
->filledMask
== GL_TRUE
);
577 ASSERT(span
->filledDepth
== GL_TRUE
);
578 SW_SPAN_SET_FLAG(span
->testedDepth
);
580 if (swrast
->Driver
.ReadDepthSpan
) {
581 /* hardware-based depth buffer */
582 GLdepth zbuffer
[MAX_WIDTH
];
584 (*swrast
->Driver
.ReadDepthSpan
)(ctx
, span
->end
, span
->x
, span
->y
, zbuffer
);
585 passed
= depth_test_span32(ctx
, span
->end
, span
->x
, span
->y
,
586 zbuffer
, span
->depth
, span
->mask
);
587 ASSERT(swrast
->Driver
.WriteDepthSpan
);
588 (*swrast
->Driver
.WriteDepthSpan
)(ctx
, span
->end
, span
->x
, span
->y
, zbuffer
, span
->mask
);
589 if (passed
< span
->end
)
590 span
->writeAll
= GL_FALSE
;
595 /* software depth buffer */
596 if (ctx
->Visual
.depthBits
<= 16) {
597 GLushort
*zptr
= (GLushort
*) Z_ADDRESS16(ctx
, span
->x
, span
->y
);
598 passed
= depth_test_span16(ctx
, span
->end
, span
->x
, span
->y
, zptr
, span
->depth
, span
->mask
);
601 GLuint
*zptr
= (GLuint
*) Z_ADDRESS32(ctx
, span
->x
, span
->y
);
602 passed
= depth_test_span32(ctx
, span
->end
, span
->x
, span
->y
, zptr
, span
->depth
, span
->mask
);
604 if (passed
< span
->end
)
605 span
->writeAll
= GL_FALSE
;
614 * Do depth testing for an array of fragments using software Z buffer.
617 software_depth_test_pixels16( GLcontext
*ctx
, GLuint n
,
618 const GLint x
[], const GLint y
[],
619 const GLdepth z
[], GLubyte mask
[] )
621 /* switch cases ordered from most frequent to less frequent */
622 switch (ctx
->Depth
.Func
) {
624 if (ctx
->Depth
.Mask
) {
625 /* Update Z buffer */
627 for (i
=0; i
<n
; i
++) {
629 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
642 /* Don't update Z buffer */
644 for (i
=0; i
<n
; i
++) {
646 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
659 if (ctx
->Depth
.Mask
) {
660 /* Update Z buffer */
662 for (i
=0; i
<n
; i
++) {
664 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
677 /* Don't update Z buffer */
679 for (i
=0; i
<n
; i
++) {
681 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
694 if (ctx
->Depth
.Mask
) {
695 /* Update Z buffer */
697 for (i
=0; i
<n
; i
++) {
699 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
712 /* Don't update Z buffer */
714 for (i
=0; i
<n
; i
++) {
716 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
729 if (ctx
->Depth
.Mask
) {
730 /* Update Z buffer */
732 for (i
=0; i
<n
; i
++) {
734 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
747 /* Don't update Z buffer */
749 for (i
=0; i
<n
; i
++) {
751 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
764 if (ctx
->Depth
.Mask
) {
765 /* Update Z buffer */
767 for (i
=0; i
<n
; i
++) {
769 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
782 /* Don't update Z buffer */
784 for (i
=0; i
<n
; i
++) {
786 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
799 if (ctx
->Depth
.Mask
) {
800 /* Update Z buffer */
802 for (i
=0; i
<n
; i
++) {
804 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
817 /* Don't update Z buffer */
819 for (i
=0; i
<n
; i
++) {
821 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
834 if (ctx
->Depth
.Mask
) {
835 /* Update Z buffer */
837 for (i
=0; i
<n
; i
++) {
839 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
845 /* Don't update Z buffer or mask */
849 /* depth test never passes */
850 BZERO(mask
, n
* sizeof(GLubyte
));
853 _mesa_problem(ctx
, "Bad depth func in software_depth_test_pixels");
860 * Do depth testing for an array of fragments using software Z buffer.
863 software_depth_test_pixels32( GLcontext
*ctx
, GLuint n
,
864 const GLint x
[], const GLint y
[],
865 const GLdepth z
[], GLubyte mask
[] )
867 /* switch cases ordered from most frequent to less frequent */
868 switch (ctx
->Depth
.Func
) {
870 if (ctx
->Depth
.Mask
) {
871 /* Update Z buffer */
873 for (i
=0; i
<n
; i
++) {
875 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
888 /* Don't update Z buffer */
890 for (i
=0; i
<n
; i
++) {
892 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
905 if (ctx
->Depth
.Mask
) {
906 /* Update Z buffer */
908 for (i
=0; i
<n
; i
++) {
910 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
923 /* Don't update Z buffer */
925 for (i
=0; i
<n
; i
++) {
927 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
940 if (ctx
->Depth
.Mask
) {
941 /* Update Z buffer */
943 for (i
=0; i
<n
; i
++) {
945 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
958 /* Don't update Z buffer */
960 for (i
=0; i
<n
; i
++) {
962 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
975 if (ctx
->Depth
.Mask
) {
976 /* Update Z buffer */
978 for (i
=0; i
<n
; i
++) {
980 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
993 /* Don't update Z buffer */
995 for (i
=0; i
<n
; i
++) {
997 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1010 if (ctx
->Depth
.Mask
) {
1011 /* Update Z buffer */
1013 for (i
=0; i
<n
; i
++) {
1015 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1016 if (z
[i
] != *zptr
) {
1028 /* Don't update Z buffer */
1030 for (i
=0; i
<n
; i
++) {
1032 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1033 if (z
[i
] != *zptr
) {
1045 if (ctx
->Depth
.Mask
) {
1046 /* Update Z buffer */
1048 for (i
=0; i
<n
; i
++) {
1050 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1051 if (z
[i
] == *zptr
) {
1063 /* Don't update Z buffer */
1065 for (i
=0; i
<n
; i
++) {
1067 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1068 if (z
[i
] == *zptr
) {
1080 if (ctx
->Depth
.Mask
) {
1081 /* Update Z buffer */
1083 for (i
=0; i
<n
; i
++) {
1085 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1091 /* Don't update Z buffer or mask */
1095 /* depth test never passes */
1096 BZERO(mask
, n
* sizeof(GLubyte
));
1099 _mesa_problem(ctx
, "Bad depth func in software_depth_test_pixels");
1106 * Do depth testing for an array of pixels using hardware Z buffer.
1107 * Input/output: zbuffer - array of depth values from Z buffer
1108 * Input: z - array of fragment z values.
1111 hardware_depth_test_pixels( GLcontext
*ctx
, GLuint n
, GLdepth zbuffer
[],
1112 const GLdepth z
[], GLubyte mask
[] )
1114 /* switch cases ordered from most frequent to less frequent */
1115 switch (ctx
->Depth
.Func
) {
1117 if (ctx
->Depth
.Mask
) {
1118 /* Update Z buffer */
1120 for (i
=0; i
<n
; i
++) {
1122 if (z
[i
] < zbuffer
[i
]) {
1134 /* Don't update Z buffer */
1136 for (i
=0; i
<n
; i
++) {
1138 if (z
[i
] < zbuffer
[i
]) {
1150 if (ctx
->Depth
.Mask
) {
1151 /* Update Z buffer */
1153 for (i
=0; i
<n
; i
++) {
1155 if (z
[i
] <= zbuffer
[i
]) {
1167 /* Don't update Z buffer */
1169 for (i
=0; i
<n
; i
++) {
1171 if (z
[i
] <= zbuffer
[i
]) {
1183 if (ctx
->Depth
.Mask
) {
1184 /* Update Z buffer */
1186 for (i
=0; i
<n
; i
++) {
1188 if (z
[i
] >= zbuffer
[i
]) {
1200 /* Don't update Z buffer */
1202 for (i
=0; i
<n
; i
++) {
1204 if (z
[i
] >= zbuffer
[i
]) {
1216 if (ctx
->Depth
.Mask
) {
1217 /* Update Z buffer */
1219 for (i
=0; i
<n
; i
++) {
1221 if (z
[i
] > zbuffer
[i
]) {
1233 /* Don't update Z buffer */
1235 for (i
=0; i
<n
; i
++) {
1237 if (z
[i
] > zbuffer
[i
]) {
1249 if (ctx
->Depth
.Mask
) {
1250 /* Update Z buffer */
1252 for (i
=0; i
<n
; i
++) {
1254 if (z
[i
] != zbuffer
[i
]) {
1266 /* Don't update Z buffer */
1268 for (i
=0; i
<n
; i
++) {
1270 if (z
[i
] != zbuffer
[i
]) {
1282 if (ctx
->Depth
.Mask
) {
1283 /* Update Z buffer */
1285 for (i
=0; i
<n
; i
++) {
1287 if (z
[i
] == zbuffer
[i
]) {
1299 /* Don't update Z buffer */
1301 for (i
=0; i
<n
; i
++) {
1303 if (z
[i
] == zbuffer
[i
]) {
1315 if (ctx
->Depth
.Mask
) {
1316 /* Update Z buffer */
1318 for (i
=0; i
<n
; i
++) {
1325 /* Don't update Z buffer or mask */
1329 /* depth test never passes */
1330 BZERO(mask
, n
* sizeof(GLubyte
));
1333 _mesa_problem(ctx
, "Bad depth func in hardware_depth_test_pixels");
1340 _mesa_depth_test_pixels( GLcontext
*ctx
,
1341 GLuint n
, const GLint x
[], const GLint y
[],
1342 const GLdepth z
[], GLubyte mask
[] )
1344 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1345 if (swrast
->Driver
.ReadDepthPixels
) {
1346 /* read depth values from hardware Z buffer */
1347 GLdepth zbuffer
[PB_SIZE
];
1348 (*swrast
->Driver
.ReadDepthPixels
)(ctx
, n
, x
, y
, zbuffer
);
1350 hardware_depth_test_pixels( ctx
, n
, zbuffer
, z
, mask
);
1352 /* update hardware Z buffer with new values */
1353 assert(swrast
->Driver
.WriteDepthPixels
);
1354 (*swrast
->Driver
.WriteDepthPixels
)(ctx
, n
, x
, y
, zbuffer
, mask
);
1357 /* software depth testing */
1358 if (ctx
->Visual
.depthBits
<= 16)
1359 software_depth_test_pixels16(ctx
, n
, x
, y
, z
, mask
);
1361 software_depth_test_pixels32(ctx
, n
, x
, y
, z
, mask
);
1369 /**********************************************************************/
1370 /***** Read Depth Buffer *****/
1371 /**********************************************************************/
1375 * Read a span of depth values from the depth buffer.
1376 * This function does clipping before calling the device driver function.
1379 _mesa_read_depth_span( GLcontext
*ctx
,
1380 GLint n
, GLint x
, GLint y
, GLdepth depth
[] )
1382 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1384 if (y
< 0 || y
>= ctx
->DrawBuffer
->Height
||
1385 x
+ (GLint
) n
<= 0 || x
>= ctx
->DrawBuffer
->Width
) {
1386 /* span is completely outside framebuffer */
1388 for (i
= 0; i
< n
; i
++)
1396 for (i
= 0; i
< dx
; i
++)
1402 if (x
+ n
> ctx
->DrawBuffer
->Width
) {
1403 GLint dx
= x
+ n
- ctx
->DrawBuffer
->Width
;
1405 for (i
= 0; i
< dx
; i
++)
1406 depth
[n
- i
- 1] = 0;
1413 if (ctx
->DrawBuffer
->DepthBuffer
) {
1414 /* read from software depth buffer */
1415 if (ctx
->Visual
.depthBits
<= 16) {
1416 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1418 for (i
= 0; i
< n
; i
++) {
1423 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1425 for (i
= 0; i
< n
; i
++) {
1430 else if (swrast
->Driver
.ReadDepthSpan
) {
1431 /* read from hardware depth buffer */
1432 (*swrast
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, depth
);
1435 /* no depth buffer */
1436 BZERO(depth
, n
* sizeof(GLfloat
));
1445 * Return a span of depth values from the depth buffer as floats in [0,1].
1446 * This is used for both hardware and software depth buffers.
1447 * Input: n - how many pixels
1448 * x,y - location of first pixel
1449 * Output: depth - the array of depth values
1452 _mesa_read_depth_span_float( GLcontext
*ctx
,
1453 GLint n
, GLint x
, GLint y
, GLfloat depth
[] )
1455 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1456 const GLfloat scale
= 1.0F
/ ctx
->DepthMaxF
;
1458 if (y
< 0 || y
>= ctx
->DrawBuffer
->Height
||
1459 x
+ (GLint
) n
<= 0 || x
>= ctx
->DrawBuffer
->Width
) {
1460 /* span is completely outside framebuffer */
1462 for (i
= 0; i
< n
; i
++)
1470 for (i
= 0; i
< dx
; i
++)
1475 if (x
+ n
> ctx
->DrawBuffer
->Width
) {
1476 GLint dx
= x
+ n
- ctx
->DrawBuffer
->Width
;
1478 for (i
= 0; i
< dx
; i
++)
1479 depth
[n
- i
- 1] = 0.0F
;
1486 if (ctx
->DrawBuffer
->DepthBuffer
) {
1487 /* read from software depth buffer */
1488 if (ctx
->Visual
.depthBits
<= 16) {
1489 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1491 for (i
= 0; i
< n
; i
++) {
1492 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1496 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1498 for (i
= 0; i
< n
; i
++) {
1499 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1503 else if (swrast
->Driver
.ReadDepthSpan
) {
1504 /* read from hardware depth buffer */
1505 GLdepth d
[MAX_WIDTH
];
1507 assert(n
<= MAX_WIDTH
);
1508 (*swrast
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, d
);
1509 for (i
= 0; i
< n
; i
++) {
1510 depth
[i
] = d
[i
] * scale
;
1514 /* no depth buffer */
1515 BZERO(depth
, n
* sizeof(GLfloat
));
1521 /**********************************************************************/
1522 /***** Allocate and Clear Depth Buffer *****/
1523 /**********************************************************************/
1528 * Allocate a new depth buffer. If there's already a depth buffer allocated
1529 * it will be free()'d. The new depth buffer will be uniniitalized.
1530 * This function is only called through Driver.alloc_depth_buffer.
1533 _mesa_alloc_depth_buffer( GLcontext
*ctx
)
1535 /* deallocate current depth buffer if present */
1536 if (ctx
->DrawBuffer
->UseSoftwareDepthBuffer
) {
1537 GLint bytesPerValue
;
1539 if (ctx
->DrawBuffer
->DepthBuffer
) {
1540 FREE(ctx
->DrawBuffer
->DepthBuffer
);
1541 ctx
->DrawBuffer
->DepthBuffer
= NULL
;
1544 /* allocate new depth buffer, but don't initialize it */
1545 if (ctx
->Visual
.depthBits
<= 16)
1546 bytesPerValue
= sizeof(GLushort
);
1548 bytesPerValue
= sizeof(GLuint
);
1550 ctx
->DrawBuffer
->DepthBuffer
= MALLOC( ctx
->DrawBuffer
->Width
1551 * ctx
->DrawBuffer
->Height
1554 if (!ctx
->DrawBuffer
->DepthBuffer
) {
1556 ctx
->Depth
.Test
= GL_FALSE
;
1557 ctx
->NewState
|= _NEW_DEPTH
;
1558 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "Couldn't allocate depth buffer" );
1567 * Clear the depth buffer. If the depth buffer doesn't exist yet we'll
1569 * This function is only called through Driver.clear_depth_buffer.
1572 _mesa_clear_depth_buffer( GLcontext
*ctx
)
1574 if (ctx
->Visual
.depthBits
== 0
1575 || !ctx
->DrawBuffer
->DepthBuffer
1576 || !ctx
->Depth
.Mask
) {
1577 /* no depth buffer, or writing to it is disabled */
1581 /* The loops in this function have been written so the IRIX 5.3
1582 * C compiler can unroll them. Hopefully other compilers can too!
1585 if (ctx
->Scissor
.Enabled
) {
1586 /* only clear scissor region */
1587 if (ctx
->Visual
.depthBits
<= 16) {
1588 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1589 const GLint rows
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
1590 const GLint cols
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
1591 const GLint rowStride
= ctx
->DrawBuffer
->Width
;
1592 GLushort
*dRow
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
1593 + ctx
->DrawBuffer
->_Ymin
* rowStride
+ ctx
->DrawBuffer
->_Xmin
;
1595 for (i
= 0; i
< rows
; i
++) {
1596 for (j
= 0; j
< cols
; j
++) {
1597 dRow
[j
] = clearValue
;
1603 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1604 const GLint rows
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
1605 const GLint cols
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
1606 const GLint rowStride
= ctx
->DrawBuffer
->Width
;
1607 GLuint
*dRow
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
1608 + ctx
->DrawBuffer
->_Ymin
* rowStride
+ ctx
->DrawBuffer
->_Xmin
;
1610 for (i
= 0; i
< rows
; i
++) {
1611 for (j
= 0; j
< cols
; j
++) {
1612 dRow
[j
] = clearValue
;
1619 /* clear whole buffer */
1620 if (ctx
->Visual
.depthBits
<= 16) {
1621 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1622 if ((clearValue
& 0xff) == (clearValue
>> 8)) {
1623 if (clearValue
== 0) {
1624 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1625 2*ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
);
1628 /* lower and upper bytes of clear_value are same, use MEMSET */
1629 MEMSET( ctx
->DrawBuffer
->DepthBuffer
, clearValue
& 0xff,
1630 2 * ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
);
1634 GLushort
*d
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
;
1635 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1637 d
[0] = clearValue
; d
[1] = clearValue
;
1638 d
[2] = clearValue
; d
[3] = clearValue
;
1639 d
[4] = clearValue
; d
[5] = clearValue
;
1640 d
[6] = clearValue
; d
[7] = clearValue
;
1641 d
[8] = clearValue
; d
[9] = clearValue
;
1642 d
[10] = clearValue
; d
[11] = clearValue
;
1643 d
[12] = clearValue
; d
[13] = clearValue
;
1644 d
[14] = clearValue
; d
[15] = clearValue
;
1655 /* >16 bit depth buffer */
1656 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1657 if (clearValue
== 0) {
1658 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1659 ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
*sizeof(GLuint
));
1662 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1663 GLuint
*d
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
;
1665 d
[0] = clearValue
; d
[1] = clearValue
;
1666 d
[2] = clearValue
; d
[3] = clearValue
;
1667 d
[4] = clearValue
; d
[5] = clearValue
;
1668 d
[6] = clearValue
; d
[7] = clearValue
;
1669 d
[8] = clearValue
; d
[9] = clearValue
;
1670 d
[10] = clearValue
; d
[11] = clearValue
;
1671 d
[12] = clearValue
; d
[13] = clearValue
;
1672 d
[14] = clearValue
; d
[15] = clearValue
;