2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
32 #include "s_context.h"
36 * Return address of depth buffer value for given window coord.
39 _swrast_zbuffer_address(GLcontext
*ctx
, GLint x
, GLint y
)
41 if (ctx
->Visual
.depthBits
<= 16)
42 return (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
43 + ctx
->DrawBuffer
->Width
* y
+ x
;
45 return (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
46 + ctx
->DrawBuffer
->Width
* y
+ x
;
50 #define Z_ADDRESS16( CTX, X, Y ) \
51 ( ((GLushort *) (CTX)->DrawBuffer->DepthBuffer) \
52 + (CTX)->DrawBuffer->Width * (Y) + (X) )
54 #define Z_ADDRESS32( CTX, X, Y ) \
55 ( ((GLuint *) (CTX)->DrawBuffer->DepthBuffer) \
56 + (CTX)->DrawBuffer->Width * (Y) + (X) )
60 /**********************************************************************/
61 /***** Depth Testing Functions *****/
62 /**********************************************************************/
66 * Do depth test for an array of fragments. This is used both for
67 * software and hardware Z buffers.
68 * Input: zbuffer - array of z values in the zbuffer
69 * z - array of fragment z values
70 * Return: number of fragments which pass the test.
73 depth_test_span16( GLcontext
*ctx
, GLuint n
,
74 GLushort zbuffer
[], const GLdepth z
[], GLubyte mask
[] )
78 /* switch cases ordered from most frequent to less frequent */
79 switch (ctx
->Depth
.Func
) {
81 if (ctx
->Depth
.Mask
) {
86 if (z
[i
] < zbuffer
[i
]) {
99 /* Don't update Z buffer */
101 for (i
=0; i
<n
; i
++) {
103 if (z
[i
] < zbuffer
[i
]) {
115 if (ctx
->Depth
.Mask
) {
116 /* Update Z buffer */
120 if (z
[i
] <= zbuffer
[i
]) {
131 /* Don't update Z buffer */
135 if (z
[i
] <= zbuffer
[i
]) {
147 if (ctx
->Depth
.Mask
) {
148 /* Update Z buffer */
152 if (z
[i
] >= zbuffer
[i
]) {
163 /* Don't update Z buffer */
167 if (z
[i
] >= zbuffer
[i
]) {
179 if (ctx
->Depth
.Mask
) {
180 /* Update Z buffer */
184 if (z
[i
] > zbuffer
[i
]) {
195 /* Don't update Z buffer */
199 if (z
[i
] > zbuffer
[i
]) {
211 if (ctx
->Depth
.Mask
) {
212 /* Update Z buffer */
216 if (z
[i
] != zbuffer
[i
]) {
227 /* Don't update Z buffer */
231 if (z
[i
] != zbuffer
[i
]) {
243 if (ctx
->Depth
.Mask
) {
244 /* Update Z buffer */
248 if (z
[i
] == zbuffer
[i
]) {
259 /* Don't update Z buffer */
263 if (z
[i
] == zbuffer
[i
]) {
275 if (ctx
->Depth
.Mask
) {
276 /* Update Z buffer */
286 /* Don't update Z buffer or mask */
291 _mesa_bzero(mask
, n
* sizeof(GLubyte
));
294 _mesa_problem(ctx
, "Bad depth func in depth_test_span16");
302 depth_test_span32( GLcontext
*ctx
, GLuint n
,
303 GLuint zbuffer
[], const GLdepth z
[], GLubyte mask
[] )
307 /* switch cases ordered from most frequent to less frequent */
308 switch (ctx
->Depth
.Func
) {
310 if (ctx
->Depth
.Mask
) {
311 /* Update Z buffer */
313 for (i
=0; i
<n
; i
++) {
315 if (z
[i
] < zbuffer
[i
]) {
328 /* Don't update Z buffer */
330 for (i
=0; i
<n
; i
++) {
332 if (z
[i
] < zbuffer
[i
]) {
344 if (ctx
->Depth
.Mask
) {
345 /* Update Z buffer */
349 if (z
[i
] <= zbuffer
[i
]) {
360 /* Don't update Z buffer */
364 if (z
[i
] <= zbuffer
[i
]) {
376 if (ctx
->Depth
.Mask
) {
377 /* Update Z buffer */
381 if (z
[i
] >= zbuffer
[i
]) {
392 /* Don't update Z buffer */
396 if (z
[i
] >= zbuffer
[i
]) {
408 if (ctx
->Depth
.Mask
) {
409 /* Update Z buffer */
413 if (z
[i
] > zbuffer
[i
]) {
424 /* Don't update Z buffer */
428 if (z
[i
] > zbuffer
[i
]) {
440 if (ctx
->Depth
.Mask
) {
441 /* Update Z buffer */
445 if (z
[i
] != zbuffer
[i
]) {
456 /* Don't update Z buffer */
460 if (z
[i
] != zbuffer
[i
]) {
472 if (ctx
->Depth
.Mask
) {
473 /* Update Z buffer */
477 if (z
[i
] == zbuffer
[i
]) {
488 /* Don't update Z buffer */
492 if (z
[i
] == zbuffer
[i
]) {
504 if (ctx
->Depth
.Mask
) {
505 /* Update Z buffer */
515 /* Don't update Z buffer or mask */
520 _mesa_bzero(mask
, n
* sizeof(GLubyte
));
523 _mesa_problem(ctx
, "Bad depth func in depth_test_span32");
532 * Apply depth test to span of fragments. Hardware or software z buffer.
535 depth_test_span( GLcontext
*ctx
, struct sw_span
*span
)
537 const GLint x
= span
->x
;
538 const GLint y
= span
->y
;
539 const GLuint n
= span
->end
;
540 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
542 ASSERT((span
->arrayMask
& SPAN_XY
) == 0);
543 ASSERT(span
->arrayMask
& SPAN_Z
);
545 if (swrast
->Driver
.ReadDepthSpan
) {
546 /* hardware-based depth buffer */
547 GLdepth zbuffer
[MAX_WIDTH
];
549 (*swrast
->Driver
.ReadDepthSpan
)(ctx
, n
, x
, y
, zbuffer
);
550 passed
= depth_test_span32(ctx
, n
, zbuffer
, span
->array
->z
,
552 ASSERT(swrast
->Driver
.WriteDepthSpan
);
553 (*swrast
->Driver
.WriteDepthSpan
)(ctx
, n
, x
, y
, zbuffer
,
556 span
->writeAll
= GL_FALSE
;
561 /* software depth buffer */
562 if (ctx
->Visual
.depthBits
<= 16) {
563 GLushort
*zptr
= (GLushort
*) Z_ADDRESS16(ctx
, x
, y
);
564 passed
= depth_test_span16(ctx
, n
, zptr
, span
->array
->z
, span
->array
->mask
);
567 GLuint
*zptr
= (GLuint
*) Z_ADDRESS32(ctx
, x
, y
);
568 passed
= depth_test_span32(ctx
, n
, zptr
, span
->array
->z
, span
->array
->mask
);
571 if (passed
< span
->end
) {
572 span
->writeAll
= GL_FALSE
;
575 /* this causes a glDrawPixels(GL_DEPTH_COMPONENT) conformance failure */
576 if (passed
< span
->end
) {
577 span
->writeAll
= GL_FALSE
;
582 while (span
->end
> 0 && span
->mask
[span
->end
- 1] == 0)
594 * Do depth testing for an array of fragments using software Z buffer.
597 software_depth_test_pixels16( GLcontext
*ctx
, GLuint n
,
598 const GLint x
[], const GLint y
[],
599 const GLdepth z
[], GLubyte mask
[] )
601 /* switch cases ordered from most frequent to less frequent */
602 switch (ctx
->Depth
.Func
) {
604 if (ctx
->Depth
.Mask
) {
605 /* Update Z buffer */
607 for (i
=0; i
<n
; i
++) {
609 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
622 /* Don't update Z buffer */
624 for (i
=0; i
<n
; i
++) {
626 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
639 if (ctx
->Depth
.Mask
) {
640 /* Update Z buffer */
642 for (i
=0; i
<n
; i
++) {
644 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
657 /* Don't update Z buffer */
659 for (i
=0; i
<n
; i
++) {
661 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
674 if (ctx
->Depth
.Mask
) {
675 /* Update Z buffer */
677 for (i
=0; i
<n
; i
++) {
679 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
692 /* Don't update Z buffer */
694 for (i
=0; i
<n
; i
++) {
696 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
709 if (ctx
->Depth
.Mask
) {
710 /* Update Z buffer */
712 for (i
=0; i
<n
; i
++) {
714 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
727 /* Don't update Z buffer */
729 for (i
=0; i
<n
; i
++) {
731 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
744 if (ctx
->Depth
.Mask
) {
745 /* Update Z buffer */
747 for (i
=0; i
<n
; i
++) {
749 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
762 /* Don't update Z buffer */
764 for (i
=0; i
<n
; i
++) {
766 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
779 if (ctx
->Depth
.Mask
) {
780 /* Update Z buffer */
782 for (i
=0; i
<n
; i
++) {
784 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
797 /* Don't update Z buffer */
799 for (i
=0; i
<n
; i
++) {
801 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
814 if (ctx
->Depth
.Mask
) {
815 /* Update Z buffer */
817 for (i
=0; i
<n
; i
++) {
819 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
825 /* Don't update Z buffer or mask */
829 /* depth test never passes */
830 _mesa_bzero(mask
, n
* sizeof(GLubyte
));
833 _mesa_problem(ctx
, "Bad depth func in software_depth_test_pixels");
840 * Do depth testing for an array of fragments using software Z buffer.
843 software_depth_test_pixels32( GLcontext
*ctx
, GLuint n
,
844 const GLint x
[], const GLint y
[],
845 const GLdepth z
[], GLubyte mask
[] )
847 /* switch cases ordered from most frequent to less frequent */
848 switch (ctx
->Depth
.Func
) {
850 if (ctx
->Depth
.Mask
) {
851 /* Update Z buffer */
853 for (i
=0; i
<n
; i
++) {
855 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
868 /* Don't update Z buffer */
870 for (i
=0; i
<n
; i
++) {
872 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
885 if (ctx
->Depth
.Mask
) {
886 /* Update Z buffer */
888 for (i
=0; i
<n
; i
++) {
890 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
903 /* Don't update Z buffer */
905 for (i
=0; i
<n
; i
++) {
907 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
920 if (ctx
->Depth
.Mask
) {
921 /* Update Z buffer */
923 for (i
=0; i
<n
; i
++) {
925 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
938 /* Don't update Z buffer */
940 for (i
=0; i
<n
; i
++) {
942 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
955 if (ctx
->Depth
.Mask
) {
956 /* Update Z buffer */
958 for (i
=0; i
<n
; i
++) {
960 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
973 /* Don't update Z buffer */
975 for (i
=0; i
<n
; i
++) {
977 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
990 if (ctx
->Depth
.Mask
) {
991 /* Update Z buffer */
993 for (i
=0; i
<n
; i
++) {
995 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1008 /* Don't 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 if (ctx
->Depth
.Mask
) {
1026 /* Update Z buffer */
1028 for (i
=0; i
<n
; i
++) {
1030 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1031 if (z
[i
] == *zptr
) {
1043 /* Don't 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 if (ctx
->Depth
.Mask
) {
1061 /* Update Z buffer */
1063 for (i
=0; i
<n
; i
++) {
1065 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1071 /* Don't update Z buffer or mask */
1075 /* depth test never passes */
1076 _mesa_bzero(mask
, n
* sizeof(GLubyte
));
1079 _mesa_problem(ctx
, "Bad depth func in software_depth_test_pixels");
1086 * Do depth testing for an array of pixels using hardware Z buffer.
1087 * Input/output: zbuffer - array of depth values from Z buffer
1088 * Input: z - array of fragment z values.
1091 hardware_depth_test_pixels( GLcontext
*ctx
, GLuint n
, GLdepth zbuffer
[],
1092 const GLdepth z
[], GLubyte mask
[] )
1094 /* switch cases ordered from most frequent to less frequent */
1095 switch (ctx
->Depth
.Func
) {
1097 if (ctx
->Depth
.Mask
) {
1098 /* Update Z buffer */
1100 for (i
=0; i
<n
; i
++) {
1102 if (z
[i
] < zbuffer
[i
]) {
1114 /* Don't update Z buffer */
1116 for (i
=0; i
<n
; i
++) {
1118 if (z
[i
] < zbuffer
[i
]) {
1130 if (ctx
->Depth
.Mask
) {
1131 /* Update Z buffer */
1133 for (i
=0; i
<n
; i
++) {
1135 if (z
[i
] <= zbuffer
[i
]) {
1147 /* Don't update Z buffer */
1149 for (i
=0; i
<n
; i
++) {
1151 if (z
[i
] <= zbuffer
[i
]) {
1163 if (ctx
->Depth
.Mask
) {
1164 /* Update Z buffer */
1166 for (i
=0; i
<n
; i
++) {
1168 if (z
[i
] >= zbuffer
[i
]) {
1180 /* Don't update Z buffer */
1182 for (i
=0; i
<n
; i
++) {
1184 if (z
[i
] >= zbuffer
[i
]) {
1196 if (ctx
->Depth
.Mask
) {
1197 /* Update Z buffer */
1199 for (i
=0; i
<n
; i
++) {
1201 if (z
[i
] > zbuffer
[i
]) {
1213 /* Don't update Z buffer */
1215 for (i
=0; i
<n
; i
++) {
1217 if (z
[i
] > zbuffer
[i
]) {
1229 if (ctx
->Depth
.Mask
) {
1230 /* Update Z buffer */
1232 for (i
=0; i
<n
; i
++) {
1234 if (z
[i
] != zbuffer
[i
]) {
1246 /* Don't update Z buffer */
1248 for (i
=0; i
<n
; i
++) {
1250 if (z
[i
] != zbuffer
[i
]) {
1262 if (ctx
->Depth
.Mask
) {
1263 /* Update Z buffer */
1265 for (i
=0; i
<n
; i
++) {
1267 if (z
[i
] == zbuffer
[i
]) {
1279 /* Don't update Z buffer */
1281 for (i
=0; i
<n
; i
++) {
1283 if (z
[i
] == zbuffer
[i
]) {
1295 if (ctx
->Depth
.Mask
) {
1296 /* Update Z buffer */
1298 for (i
=0; i
<n
; i
++) {
1305 /* Don't update Z buffer or mask */
1309 /* depth test never passes */
1310 _mesa_bzero(mask
, n
* sizeof(GLubyte
));
1313 _mesa_problem(ctx
, "Bad depth func in hardware_depth_test_pixels");
1320 depth_test_pixels( GLcontext
*ctx
, struct sw_span
*span
)
1322 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1323 const GLuint n
= span
->end
;
1324 const GLint
*x
= span
->array
->x
;
1325 const GLint
*y
= span
->array
->y
;
1326 const GLdepth
*z
= span
->array
->z
;
1327 GLubyte
*mask
= span
->array
->mask
;
1329 if (swrast
->Driver
.ReadDepthPixels
) {
1330 /* read depth values from hardware Z buffer */
1331 GLdepth zbuffer
[MAX_WIDTH
];
1332 (*swrast
->Driver
.ReadDepthPixels
)(ctx
, n
, x
, y
, zbuffer
);
1334 hardware_depth_test_pixels( ctx
, n
, zbuffer
, z
, mask
);
1336 /* update hardware Z buffer with new values */
1337 assert(swrast
->Driver
.WriteDepthPixels
);
1338 (*swrast
->Driver
.WriteDepthPixels
)(ctx
, n
, x
, y
, zbuffer
, mask
);
1341 /* software depth testing */
1342 if (ctx
->Visual
.depthBits
<= 16)
1343 software_depth_test_pixels16(ctx
, n
, x
, y
, z
, mask
);
1345 software_depth_test_pixels32(ctx
, n
, x
, y
, z
, mask
);
1347 return n
; /* not really correct, but OK */
1352 * Apply depth (Z) buffer testing to the span.
1353 * \return approx number of pixels that passed (only zero is reliable)
1356 _swrast_depth_test_span( GLcontext
*ctx
, struct sw_span
*span
)
1358 if (span
->arrayMask
& SPAN_XY
)
1359 return depth_test_pixels(ctx
, span
);
1361 return depth_test_span(ctx
, span
);
1366 * GL_EXT_depth_bounds_test extension.
1367 * Discard fragments depending on whether the corresponding Z-buffer
1368 * values are outside the depth bounds test range.
1369 * Note: we test the Z buffer values, not the fragment Z values!
1370 * \return GL_TRUE if any fragments pass, GL_FALSE if no fragments pass
1373 _swrast_depth_bounds_test( GLcontext
*ctx
, struct sw_span
*span
)
1375 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1376 GLdepth zMin
= (GLdepth
) (ctx
->Depth
.BoundsMin
* ctx
->DepthMaxF
+ 0.5F
);
1377 GLdepth zMax
= (GLdepth
) (ctx
->Depth
.BoundsMax
* ctx
->DepthMaxF
+ 0.5F
);
1378 GLubyte
*mask
= span
->array
->mask
;
1380 GLboolean anyPass
= GL_FALSE
;
1382 if (swrast
->Driver
.ReadDepthPixels
) {
1383 /* read depth values from hardware Z buffer */
1384 GLdepth zbuffer
[MAX_WIDTH
];
1385 ASSERT(span
->end
<= MAX_WIDTH
);
1386 if (span
->arrayMask
& SPAN_XY
)
1387 (*swrast
->Driver
.ReadDepthPixels
)(ctx
, span
->end
, span
->array
->x
,
1388 span
->array
->y
, zbuffer
);
1390 (*swrast
->Driver
.ReadDepthSpan
)(ctx
, span
->end
, span
->x
, span
->y
,
1392 for (i
= 0; i
< span
->end
; i
++) {
1394 if (zbuffer
[i
] < zMin
|| zbuffer
[i
] > zMax
)
1402 /* software Z buffer */
1403 if (span
->arrayMask
& SPAN_XY
) {
1404 if (ctx
->Visual
.depthBits
<= 16) {
1406 for (i
= 0; i
< span
->end
; i
++) {
1408 const GLushort
*zPtr
= Z_ADDRESS16(ctx
, span
->array
->x
[i
],
1410 if (*zPtr
< zMin
|| *zPtr
> zMax
)
1419 for (i
= 0; i
< span
->end
; i
++) {
1421 const GLuint
*zPtr
= Z_ADDRESS32(ctx
, span
->array
->x
[i
],
1423 if (*zPtr
< zMin
|| *zPtr
> zMax
)
1432 if (ctx
->Visual
.depthBits
<= 16) {
1434 const GLushort
*zPtr
= Z_ADDRESS16(ctx
, span
->x
, span
->y
);
1435 for (i
= 0; i
< span
->end
; i
++) {
1437 if (zPtr
[i
] < zMin
|| zPtr
[i
] > zMax
)
1446 const GLuint
*zPtr
= Z_ADDRESS32(ctx
, span
->x
, span
->y
);
1447 for (i
= 0; i
< span
->end
; i
++) {
1449 if (zPtr
[i
] < zMin
|| zPtr
[i
] > zMax
)
1463 /**********************************************************************/
1464 /***** Read Depth Buffer *****/
1465 /**********************************************************************/
1469 * Read a span of depth values from the depth buffer.
1470 * This function does clipping before calling the device driver function.
1473 _swrast_read_depth_span( GLcontext
*ctx
,
1474 GLint n
, GLint x
, GLint y
, GLdepth depth
[] )
1476 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1478 if (y
< 0 || y
>= (GLint
) ctx
->DrawBuffer
->Height
||
1479 x
+ (GLint
) n
<= 0 || x
>= (GLint
) ctx
->DrawBuffer
->Width
) {
1480 /* span is completely outside framebuffer */
1482 for (i
= 0; i
< n
; i
++)
1490 for (i
= 0; i
< dx
; i
++)
1496 if (x
+ n
> (GLint
) ctx
->DrawBuffer
->Width
) {
1497 GLint dx
= x
+ n
- (GLint
) ctx
->DrawBuffer
->Width
;
1499 for (i
= 0; i
< dx
; i
++)
1500 depth
[n
- i
- 1] = 0;
1507 if (ctx
->DrawBuffer
->DepthBuffer
) {
1508 /* read from software depth buffer */
1509 if (ctx
->Visual
.depthBits
<= 16) {
1510 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1512 for (i
= 0; i
< n
; i
++) {
1517 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1519 for (i
= 0; i
< n
; i
++) {
1524 else if (swrast
->Driver
.ReadDepthSpan
) {
1525 /* read from hardware depth buffer */
1526 (*swrast
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, depth
);
1529 /* no depth buffer */
1530 _mesa_bzero(depth
, n
* sizeof(GLfloat
));
1539 * Return a span of depth values from the depth buffer as floats in [0,1].
1540 * This is used for both hardware and software depth buffers.
1541 * Input: n - how many pixels
1542 * x,y - location of first pixel
1543 * Output: depth - the array of depth values
1546 _swrast_read_depth_span_float( GLcontext
*ctx
,
1547 GLint n
, GLint x
, GLint y
, GLfloat depth
[] )
1549 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1550 const GLfloat scale
= 1.0F
/ ctx
->DepthMaxF
;
1552 if (y
< 0 || y
>= (GLint
) ctx
->DrawBuffer
->Height
||
1553 x
+ (GLint
) n
<= 0 || x
>= (GLint
) ctx
->DrawBuffer
->Width
) {
1554 /* span is completely outside framebuffer */
1556 for (i
= 0; i
< n
; i
++)
1564 for (i
= 0; i
< dx
; i
++)
1569 if (x
+ n
> (GLint
) ctx
->DrawBuffer
->Width
) {
1570 GLint dx
= x
+ n
- (GLint
) ctx
->DrawBuffer
->Width
;
1572 for (i
= 0; i
< dx
; i
++)
1573 depth
[n
- i
- 1] = 0.0F
;
1580 if (ctx
->DrawBuffer
->DepthBuffer
) {
1581 /* read from software depth buffer */
1582 if (ctx
->Visual
.depthBits
<= 16) {
1583 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1585 for (i
= 0; i
< n
; i
++) {
1586 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1590 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1592 for (i
= 0; i
< n
; i
++) {
1593 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1597 else if (swrast
->Driver
.ReadDepthSpan
) {
1598 /* read from hardware depth buffer */
1599 GLdepth d
[MAX_WIDTH
];
1601 assert(n
<= MAX_WIDTH
);
1602 (*swrast
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, d
);
1603 for (i
= 0; i
< n
; i
++) {
1604 depth
[i
] = d
[i
] * scale
;
1608 /* no depth buffer */
1609 _mesa_bzero(depth
, n
* sizeof(GLfloat
));
1615 /**********************************************************************/
1616 /***** Allocate and Clear Depth Buffer *****/
1617 /**********************************************************************/
1622 * Allocate a new depth buffer. If there's already a depth buffer allocated
1623 * it will be free()'d. The new depth buffer will be uniniitalized.
1624 * This function is only called through Driver.alloc_depth_buffer.
1627 _swrast_alloc_depth_buffer( GLframebuffer
*buffer
)
1629 GLint bytesPerValue
;
1631 ASSERT(buffer
->UseSoftwareDepthBuffer
);
1633 /* deallocate current depth buffer if present */
1634 if (buffer
->DepthBuffer
) {
1635 MESA_PBUFFER_FREE(buffer
->DepthBuffer
);
1636 buffer
->DepthBuffer
= NULL
;
1639 /* allocate new depth buffer, but don't initialize it */
1640 if (buffer
->Visual
.depthBits
<= 16)
1641 bytesPerValue
= sizeof(GLushort
);
1643 bytesPerValue
= sizeof(GLuint
);
1645 buffer
->DepthBuffer
= MESA_PBUFFER_ALLOC(buffer
->Width
* buffer
->Height
1648 if (!buffer
->DepthBuffer
) {
1650 GET_CURRENT_CONTEXT(ctx
);
1652 ctx
->Depth
.Test
= GL_FALSE
;
1653 ctx
->NewState
|= _NEW_DEPTH
;
1654 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "Couldn't allocate depth buffer");
1661 * Clear the depth buffer. If the depth buffer doesn't exist yet we'll
1663 * This function is only called through Driver.clear_depth_buffer.
1666 _swrast_clear_depth_buffer( GLcontext
*ctx
)
1668 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1670 if (ctx
->Visual
.depthBits
== 0
1671 || !ctx
->Depth
.Mask
) {
1672 /* no depth buffer, or writing to it is disabled */
1676 if (swrast
->Driver
.WriteMonoDepthSpan
) {
1677 const GLdepth clearValue
= (GLdepth
)(ctx
->Depth
.Clear
* ctx
->DepthMax
);
1678 const GLint x
= ctx
->DrawBuffer
->_Xmin
;
1679 const GLint y
= ctx
->DrawBuffer
->_Ymin
;
1680 const GLint height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
1681 const GLint width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
1684 for (i
= 0; i
< height
; i
++) {
1685 (*swrast
->Driver
.WriteMonoDepthSpan
)( ctx
, width
, x
, y
+ i
,
1692 if (!ctx
->DrawBuffer
->DepthBuffer
)
1695 /* The loops in this function have been written so the IRIX 5.3
1696 * C compiler can unroll them. Hopefully other compilers can too!
1699 if (ctx
->Scissor
.Enabled
) {
1700 /* only clear scissor region */
1701 if (ctx
->Visual
.depthBits
<= 16) {
1702 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1703 const GLint rows
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
1704 const GLint cols
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
1705 const GLint rowStride
= ctx
->DrawBuffer
->Width
;
1706 GLushort
*dRow
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
1707 + ctx
->DrawBuffer
->_Ymin
* rowStride
+ ctx
->DrawBuffer
->_Xmin
;
1709 for (i
= 0; i
< rows
; i
++) {
1710 for (j
= 0; j
< cols
; j
++) {
1711 dRow
[j
] = clearValue
;
1717 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1718 const GLint rows
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
1719 const GLint cols
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
1720 const GLint rowStride
= ctx
->DrawBuffer
->Width
;
1721 GLuint
*dRow
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
1722 + ctx
->DrawBuffer
->_Ymin
* rowStride
+ ctx
->DrawBuffer
->_Xmin
;
1724 for (i
= 0; i
< rows
; i
++) {
1725 for (j
= 0; j
< cols
; j
++) {
1726 dRow
[j
] = clearValue
;
1733 /* clear whole buffer */
1734 if (ctx
->Visual
.depthBits
<= 16) {
1735 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1736 if ((clearValue
& 0xff) == (clearValue
>> 8)) {
1737 if (clearValue
== 0) {
1738 _mesa_bzero(ctx
->DrawBuffer
->DepthBuffer
,
1739 2*ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
);
1742 /* lower and upper bytes of clear_value are same, use MEMSET */
1743 MEMSET( ctx
->DrawBuffer
->DepthBuffer
, clearValue
& 0xff,
1744 2 * ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
);
1748 GLushort
*d
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
;
1749 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1751 d
[0] = clearValue
; d
[1] = clearValue
;
1752 d
[2] = clearValue
; d
[3] = clearValue
;
1753 d
[4] = clearValue
; d
[5] = clearValue
;
1754 d
[6] = clearValue
; d
[7] = clearValue
;
1755 d
[8] = clearValue
; d
[9] = clearValue
;
1756 d
[10] = clearValue
; d
[11] = clearValue
;
1757 d
[12] = clearValue
; d
[13] = clearValue
;
1758 d
[14] = clearValue
; d
[15] = clearValue
;
1769 /* >16 bit depth buffer */
1770 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1771 if (clearValue
== 0) {
1772 _mesa_bzero(ctx
->DrawBuffer
->DepthBuffer
,
1773 ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
*sizeof(GLuint
));
1776 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1777 GLuint
*d
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
;
1779 d
[0] = clearValue
; d
[1] = clearValue
;
1780 d
[2] = clearValue
; d
[3] = clearValue
;
1781 d
[4] = clearValue
; d
[5] = clearValue
;
1782 d
[6] = clearValue
; d
[7] = clearValue
;
1783 d
[8] = clearValue
; d
[9] = clearValue
;
1784 d
[10] = clearValue
; d
[11] = clearValue
;
1785 d
[12] = clearValue
; d
[13] = clearValue
;
1786 d
[14] = clearValue
; d
[15] = clearValue
;