1 /* $Id: s_depth.c,v 1.16 2002/02/04 15:59:29 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"
38 * Return address of depth buffer value for given window coord.
41 _mesa_zbuffer_address(GLcontext
*ctx
, GLint x
, GLint y
)
43 if (ctx
->Visual
.depthBits
<= 16)
44 return (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
45 + ctx
->DrawBuffer
->Width
* y
+ x
;
47 return (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
48 + ctx
->DrawBuffer
->Width
* y
+ x
;
52 #define Z_ADDRESS16( CTX, X, Y ) \
53 ( ((GLushort *) (CTX)->DrawBuffer->DepthBuffer) \
54 + (CTX)->DrawBuffer->Width * (Y) + (X) )
56 #define Z_ADDRESS32( CTX, X, Y ) \
57 ( ((GLuint *) (CTX)->DrawBuffer->DepthBuffer) \
58 + (CTX)->DrawBuffer->Width * (Y) + (X) )
62 /**********************************************************************/
63 /***** Depth Testing Functions *****/
64 /**********************************************************************/
68 * Do depth test for an array of fragments. This is used both for
69 * software and hardware Z buffers.
70 * Input: zbuffer - array of z values in the zbuffer
71 * z - array of fragment z values
72 * Return: number of fragments which pass the test.
75 depth_test_span16( GLcontext
*ctx
, GLuint n
,
76 GLushort zbuffer
[], const GLdepth z
[], GLubyte mask
[] )
80 /* switch cases ordered from most frequent to less frequent */
81 switch (ctx
->Depth
.Func
) {
83 if (ctx
->Depth
.Mask
) {
88 if (z
[i
] < zbuffer
[i
]) {
101 /* Don't update Z buffer */
103 for (i
=0; i
<n
; i
++) {
105 if (z
[i
] < zbuffer
[i
]) {
117 if (ctx
->Depth
.Mask
) {
118 /* Update Z buffer */
122 if (z
[i
] <= zbuffer
[i
]) {
133 /* Don't update Z buffer */
137 if (z
[i
] <= zbuffer
[i
]) {
149 if (ctx
->Depth
.Mask
) {
150 /* Update Z buffer */
154 if (z
[i
] >= zbuffer
[i
]) {
165 /* Don't update Z buffer */
169 if (z
[i
] >= zbuffer
[i
]) {
181 if (ctx
->Depth
.Mask
) {
182 /* Update Z buffer */
186 if (z
[i
] > zbuffer
[i
]) {
197 /* Don't update Z buffer */
201 if (z
[i
] > zbuffer
[i
]) {
213 if (ctx
->Depth
.Mask
) {
214 /* Update Z buffer */
218 if (z
[i
] != zbuffer
[i
]) {
229 /* Don't update Z buffer */
233 if (z
[i
] != zbuffer
[i
]) {
245 if (ctx
->Depth
.Mask
) {
246 /* Update Z buffer */
250 if (z
[i
] == zbuffer
[i
]) {
261 /* Don't update Z buffer */
265 if (z
[i
] == zbuffer
[i
]) {
277 if (ctx
->Depth
.Mask
) {
278 /* Update Z buffer */
288 /* Don't update Z buffer or mask */
293 BZERO(mask
, n
* sizeof(GLubyte
));
296 _mesa_problem(ctx
, "Bad depth func in depth_test_span16");
304 depth_test_span32( GLcontext
*ctx
, GLuint n
,
305 GLuint zbuffer
[], const GLdepth z
[], GLubyte mask
[] )
309 /* switch cases ordered from most frequent to less frequent */
310 switch (ctx
->Depth
.Func
) {
312 if (ctx
->Depth
.Mask
) {
313 /* Update Z buffer */
315 for (i
=0; i
<n
; i
++) {
317 if (z
[i
] < zbuffer
[i
]) {
330 /* Don't update Z buffer */
332 for (i
=0; i
<n
; i
++) {
334 if (z
[i
] < zbuffer
[i
]) {
346 if (ctx
->Depth
.Mask
) {
347 /* Update Z buffer */
351 if (z
[i
] <= zbuffer
[i
]) {
362 /* Don't update Z buffer */
366 if (z
[i
] <= zbuffer
[i
]) {
378 if (ctx
->Depth
.Mask
) {
379 /* Update Z buffer */
383 if (z
[i
] >= zbuffer
[i
]) {
394 /* Don't update Z buffer */
398 if (z
[i
] >= zbuffer
[i
]) {
410 if (ctx
->Depth
.Mask
) {
411 /* Update Z buffer */
415 if (z
[i
] > zbuffer
[i
]) {
426 /* Don't update Z buffer */
430 if (z
[i
] > zbuffer
[i
]) {
442 if (ctx
->Depth
.Mask
) {
443 /* Update Z buffer */
447 if (z
[i
] != zbuffer
[i
]) {
458 /* Don't update Z buffer */
462 if (z
[i
] != zbuffer
[i
]) {
474 if (ctx
->Depth
.Mask
) {
475 /* Update Z buffer */
479 if (z
[i
] == zbuffer
[i
]) {
490 /* Don't update Z buffer */
494 if (z
[i
] == zbuffer
[i
]) {
506 if (ctx
->Depth
.Mask
) {
507 /* Update Z buffer */
517 /* Don't update Z buffer or mask */
522 BZERO(mask
, n
* sizeof(GLubyte
));
525 _mesa_problem(ctx
, "Bad depth func in depth_test_span32");
534 * Apply depth test to span of fragments. Hardware or software z buffer.
537 depth_test_span( GLcontext
*ctx
, struct sw_span
*span
)
539 const GLint x
= span
->x
;
540 const GLint y
= span
->y
;
541 const GLuint n
= span
->end
;
542 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
544 ASSERT((span
->arrayMask
& SPAN_XY
) == 0);
545 ASSERT(span
->arrayMask
& SPAN_Z
);
547 if (swrast
->Driver
.ReadDepthSpan
) {
548 /* hardware-based depth buffer */
549 GLdepth zbuffer
[MAX_WIDTH
];
551 (*swrast
->Driver
.ReadDepthSpan
)(ctx
, n
, x
, y
, zbuffer
);
552 passed
= depth_test_span32(ctx
, n
, zbuffer
, span
->zArray
, span
->mask
);
553 ASSERT(swrast
->Driver
.WriteDepthSpan
);
554 (*swrast
->Driver
.WriteDepthSpan
)(ctx
, n
, x
, y
, zbuffer
, span
->mask
);
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
->zArray
, span
->mask
);
567 GLuint
*zptr
= (GLuint
*) Z_ADDRESS32(ctx
, x
, y
);
568 passed
= depth_test_span32(ctx
, n
, zptr
, span
->zArray
, span
->mask
);
570 if (passed
< span
->end
)
571 span
->writeAll
= GL_FALSE
;
580 * Do depth testing for an array of fragments using software Z buffer.
583 software_depth_test_pixels16( GLcontext
*ctx
, GLuint n
,
584 const GLint x
[], const GLint y
[],
585 const GLdepth z
[], GLubyte mask
[] )
587 /* switch cases ordered from most frequent to less frequent */
588 switch (ctx
->Depth
.Func
) {
590 if (ctx
->Depth
.Mask
) {
591 /* Update Z buffer */
593 for (i
=0; i
<n
; i
++) {
595 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
608 /* Don't update Z buffer */
610 for (i
=0; i
<n
; i
++) {
612 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
625 if (ctx
->Depth
.Mask
) {
626 /* Update Z buffer */
628 for (i
=0; i
<n
; i
++) {
630 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
643 /* Don't update Z buffer */
645 for (i
=0; i
<n
; i
++) {
647 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
660 if (ctx
->Depth
.Mask
) {
661 /* Update Z buffer */
663 for (i
=0; i
<n
; i
++) {
665 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
678 /* Don't update Z buffer */
680 for (i
=0; i
<n
; i
++) {
682 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
695 if (ctx
->Depth
.Mask
) {
696 /* Update Z buffer */
698 for (i
=0; i
<n
; i
++) {
700 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
713 /* Don't update Z buffer */
715 for (i
=0; i
<n
; i
++) {
717 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
730 if (ctx
->Depth
.Mask
) {
731 /* Update Z buffer */
733 for (i
=0; i
<n
; i
++) {
735 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
748 /* Don't update Z buffer */
750 for (i
=0; i
<n
; i
++) {
752 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
765 if (ctx
->Depth
.Mask
) {
766 /* Update Z buffer */
768 for (i
=0; i
<n
; i
++) {
770 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
783 /* Don't update Z buffer */
785 for (i
=0; i
<n
; i
++) {
787 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
800 if (ctx
->Depth
.Mask
) {
801 /* Update Z buffer */
803 for (i
=0; i
<n
; i
++) {
805 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
811 /* Don't update Z buffer or mask */
815 /* depth test never passes */
816 BZERO(mask
, n
* sizeof(GLubyte
));
819 _mesa_problem(ctx
, "Bad depth func in software_depth_test_pixels");
826 * Do depth testing for an array of fragments using software Z buffer.
829 software_depth_test_pixels32( GLcontext
*ctx
, GLuint n
,
830 const GLint x
[], const GLint y
[],
831 const GLdepth z
[], GLubyte mask
[] )
833 /* switch cases ordered from most frequent to less frequent */
834 switch (ctx
->Depth
.Func
) {
836 if (ctx
->Depth
.Mask
) {
837 /* Update Z buffer */
839 for (i
=0; i
<n
; i
++) {
841 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
854 /* Don't update Z buffer */
856 for (i
=0; i
<n
; i
++) {
858 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
871 if (ctx
->Depth
.Mask
) {
872 /* Update Z buffer */
874 for (i
=0; i
<n
; i
++) {
876 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
889 /* Don't update Z buffer */
891 for (i
=0; i
<n
; i
++) {
893 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
906 if (ctx
->Depth
.Mask
) {
907 /* Update Z buffer */
909 for (i
=0; i
<n
; i
++) {
911 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
924 /* Don't update Z buffer */
926 for (i
=0; i
<n
; i
++) {
928 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
941 if (ctx
->Depth
.Mask
) {
942 /* Update Z buffer */
944 for (i
=0; i
<n
; i
++) {
946 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
959 /* Don't update Z buffer */
961 for (i
=0; i
<n
; i
++) {
963 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
976 if (ctx
->Depth
.Mask
) {
977 /* Update Z buffer */
979 for (i
=0; i
<n
; i
++) {
981 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
994 /* Don't update Z buffer */
996 for (i
=0; i
<n
; i
++) {
998 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1011 if (ctx
->Depth
.Mask
) {
1012 /* Update Z buffer */
1014 for (i
=0; i
<n
; i
++) {
1016 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1017 if (z
[i
] == *zptr
) {
1029 /* Don't update Z buffer */
1031 for (i
=0; i
<n
; i
++) {
1033 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1034 if (z
[i
] == *zptr
) {
1046 if (ctx
->Depth
.Mask
) {
1047 /* Update Z buffer */
1049 for (i
=0; i
<n
; i
++) {
1051 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1057 /* Don't update Z buffer or mask */
1061 /* depth test never passes */
1062 BZERO(mask
, n
* sizeof(GLubyte
));
1065 _mesa_problem(ctx
, "Bad depth func in software_depth_test_pixels");
1072 * Do depth testing for an array of pixels using hardware Z buffer.
1073 * Input/output: zbuffer - array of depth values from Z buffer
1074 * Input: z - array of fragment z values.
1077 hardware_depth_test_pixels( GLcontext
*ctx
, GLuint n
, GLdepth zbuffer
[],
1078 const GLdepth z
[], GLubyte mask
[] )
1080 /* switch cases ordered from most frequent to less frequent */
1081 switch (ctx
->Depth
.Func
) {
1083 if (ctx
->Depth
.Mask
) {
1084 /* Update Z buffer */
1086 for (i
=0; i
<n
; i
++) {
1088 if (z
[i
] < zbuffer
[i
]) {
1100 /* Don't update Z buffer */
1102 for (i
=0; i
<n
; i
++) {
1104 if (z
[i
] < zbuffer
[i
]) {
1116 if (ctx
->Depth
.Mask
) {
1117 /* Update Z buffer */
1119 for (i
=0; i
<n
; i
++) {
1121 if (z
[i
] <= zbuffer
[i
]) {
1133 /* Don't update Z buffer */
1135 for (i
=0; i
<n
; i
++) {
1137 if (z
[i
] <= zbuffer
[i
]) {
1149 if (ctx
->Depth
.Mask
) {
1150 /* Update Z buffer */
1152 for (i
=0; i
<n
; i
++) {
1154 if (z
[i
] >= zbuffer
[i
]) {
1166 /* Don't update Z buffer */
1168 for (i
=0; i
<n
; i
++) {
1170 if (z
[i
] >= zbuffer
[i
]) {
1182 if (ctx
->Depth
.Mask
) {
1183 /* Update Z buffer */
1185 for (i
=0; i
<n
; i
++) {
1187 if (z
[i
] > zbuffer
[i
]) {
1199 /* Don't update Z buffer */
1201 for (i
=0; i
<n
; i
++) {
1203 if (z
[i
] > zbuffer
[i
]) {
1215 if (ctx
->Depth
.Mask
) {
1216 /* Update Z buffer */
1218 for (i
=0; i
<n
; i
++) {
1220 if (z
[i
] != zbuffer
[i
]) {
1232 /* Don't update Z buffer */
1234 for (i
=0; i
<n
; i
++) {
1236 if (z
[i
] != zbuffer
[i
]) {
1248 if (ctx
->Depth
.Mask
) {
1249 /* Update Z buffer */
1251 for (i
=0; i
<n
; i
++) {
1253 if (z
[i
] == zbuffer
[i
]) {
1265 /* Don't update Z buffer */
1267 for (i
=0; i
<n
; i
++) {
1269 if (z
[i
] == zbuffer
[i
]) {
1281 if (ctx
->Depth
.Mask
) {
1282 /* Update Z buffer */
1284 for (i
=0; i
<n
; i
++) {
1291 /* Don't update Z buffer or mask */
1295 /* depth test never passes */
1296 BZERO(mask
, n
* sizeof(GLubyte
));
1299 _mesa_problem(ctx
, "Bad depth func in hardware_depth_test_pixels");
1306 depth_test_pixels( GLcontext
*ctx
, struct sw_span
*span
)
1308 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1309 const GLuint n
= span
->end
;
1310 const GLint
*x
= span
->xArray
;
1311 const GLint
*y
= span
->xArray
;
1312 const GLdepth
*z
= span
->zArray
;
1313 GLubyte
*mask
= span
->mask
;
1315 if (swrast
->Driver
.ReadDepthPixels
) {
1316 /* read depth values from hardware Z buffer */
1317 GLdepth zbuffer
[MAX_WIDTH
];
1318 (*swrast
->Driver
.ReadDepthPixels
)(ctx
, n
, x
, y
, zbuffer
);
1320 hardware_depth_test_pixels( ctx
, n
, zbuffer
, z
, mask
);
1322 /* update hardware Z buffer with new values */
1323 assert(swrast
->Driver
.WriteDepthPixels
);
1324 (*swrast
->Driver
.WriteDepthPixels
)(ctx
, n
, x
, y
, zbuffer
, mask
);
1327 /* software depth testing */
1328 if (ctx
->Visual
.depthBits
<= 16)
1329 software_depth_test_pixels16(ctx
, n
, x
, y
, z
, mask
);
1331 software_depth_test_pixels32(ctx
, n
, x
, y
, z
, mask
);
1333 return n
; /* not really correct, but OK */
1338 * Apply depth (Z) buffer testing to the span.
1339 * \return approx number of pixels that passed (only zero is reliable)
1342 _mesa_depth_test_span( GLcontext
*ctx
, struct sw_span
*span
)
1344 if (span
->arrayMask
& SPAN_XY
)
1345 return depth_test_pixels(ctx
, span
);
1347 return depth_test_span(ctx
, span
);
1352 /**********************************************************************/
1353 /***** Read Depth Buffer *****/
1354 /**********************************************************************/
1358 * Read a span of depth values from the depth buffer.
1359 * This function does clipping before calling the device driver function.
1362 _mesa_read_depth_span( GLcontext
*ctx
,
1363 GLint n
, GLint x
, GLint y
, GLdepth depth
[] )
1365 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1367 if (y
< 0 || y
>= ctx
->DrawBuffer
->Height
||
1368 x
+ (GLint
) n
<= 0 || x
>= ctx
->DrawBuffer
->Width
) {
1369 /* span is completely outside framebuffer */
1371 for (i
= 0; i
< n
; i
++)
1379 for (i
= 0; i
< dx
; i
++)
1385 if (x
+ n
> ctx
->DrawBuffer
->Width
) {
1386 GLint dx
= x
+ n
- ctx
->DrawBuffer
->Width
;
1388 for (i
= 0; i
< dx
; i
++)
1389 depth
[n
- i
- 1] = 0;
1396 if (ctx
->DrawBuffer
->DepthBuffer
) {
1397 /* read from software depth buffer */
1398 if (ctx
->Visual
.depthBits
<= 16) {
1399 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1401 for (i
= 0; i
< n
; i
++) {
1406 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1408 for (i
= 0; i
< n
; i
++) {
1413 else if (swrast
->Driver
.ReadDepthSpan
) {
1414 /* read from hardware depth buffer */
1415 (*swrast
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, depth
);
1418 /* no depth buffer */
1419 BZERO(depth
, n
* sizeof(GLfloat
));
1428 * Return a span of depth values from the depth buffer as floats in [0,1].
1429 * This is used for both hardware and software depth buffers.
1430 * Input: n - how many pixels
1431 * x,y - location of first pixel
1432 * Output: depth - the array of depth values
1435 _mesa_read_depth_span_float( GLcontext
*ctx
,
1436 GLint n
, GLint x
, GLint y
, GLfloat depth
[] )
1438 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1439 const GLfloat scale
= 1.0F
/ ctx
->DepthMaxF
;
1441 if (y
< 0 || y
>= ctx
->DrawBuffer
->Height
||
1442 x
+ (GLint
) n
<= 0 || x
>= ctx
->DrawBuffer
->Width
) {
1443 /* span is completely outside framebuffer */
1445 for (i
= 0; i
< n
; i
++)
1453 for (i
= 0; i
< dx
; i
++)
1458 if (x
+ n
> ctx
->DrawBuffer
->Width
) {
1459 GLint dx
= x
+ n
- ctx
->DrawBuffer
->Width
;
1461 for (i
= 0; i
< dx
; i
++)
1462 depth
[n
- i
- 1] = 0.0F
;
1469 if (ctx
->DrawBuffer
->DepthBuffer
) {
1470 /* read from software depth buffer */
1471 if (ctx
->Visual
.depthBits
<= 16) {
1472 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1474 for (i
= 0; i
< n
; i
++) {
1475 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1479 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1481 for (i
= 0; i
< n
; i
++) {
1482 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1486 else if (swrast
->Driver
.ReadDepthSpan
) {
1487 /* read from hardware depth buffer */
1488 GLdepth d
[MAX_WIDTH
];
1490 assert(n
<= MAX_WIDTH
);
1491 (*swrast
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, d
);
1492 for (i
= 0; i
< n
; i
++) {
1493 depth
[i
] = d
[i
] * scale
;
1497 /* no depth buffer */
1498 BZERO(depth
, n
* sizeof(GLfloat
));
1504 /**********************************************************************/
1505 /***** Allocate and Clear Depth Buffer *****/
1506 /**********************************************************************/
1511 * Allocate a new depth buffer. If there's already a depth buffer allocated
1512 * it will be free()'d. The new depth buffer will be uniniitalized.
1513 * This function is only called through Driver.alloc_depth_buffer.
1516 _mesa_alloc_depth_buffer( GLcontext
*ctx
)
1518 /* deallocate current depth buffer if present */
1519 if (ctx
->DrawBuffer
->UseSoftwareDepthBuffer
) {
1520 GLint bytesPerValue
;
1522 if (ctx
->DrawBuffer
->DepthBuffer
) {
1523 FREE(ctx
->DrawBuffer
->DepthBuffer
);
1524 ctx
->DrawBuffer
->DepthBuffer
= NULL
;
1527 /* allocate new depth buffer, but don't initialize it */
1528 if (ctx
->Visual
.depthBits
<= 16)
1529 bytesPerValue
= sizeof(GLushort
);
1531 bytesPerValue
= sizeof(GLuint
);
1533 ctx
->DrawBuffer
->DepthBuffer
= MALLOC( ctx
->DrawBuffer
->Width
1534 * ctx
->DrawBuffer
->Height
1537 if (!ctx
->DrawBuffer
->DepthBuffer
) {
1539 ctx
->Depth
.Test
= GL_FALSE
;
1540 ctx
->NewState
|= _NEW_DEPTH
;
1541 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "Couldn't allocate depth buffer" );
1548 * Clear the depth buffer. If the depth buffer doesn't exist yet we'll
1550 * This function is only called through Driver.clear_depth_buffer.
1553 _mesa_clear_depth_buffer( GLcontext
*ctx
)
1555 if (ctx
->Visual
.depthBits
== 0
1556 || !ctx
->DrawBuffer
->DepthBuffer
1557 || !ctx
->Depth
.Mask
) {
1558 /* no depth buffer, or writing to it is disabled */
1562 /* The loops in this function have been written so the IRIX 5.3
1563 * C compiler can unroll them. Hopefully other compilers can too!
1566 if (ctx
->Scissor
.Enabled
) {
1567 /* only clear scissor region */
1568 if (ctx
->Visual
.depthBits
<= 16) {
1569 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1570 const GLint rows
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
1571 const GLint cols
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
1572 const GLint rowStride
= ctx
->DrawBuffer
->Width
;
1573 GLushort
*dRow
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
1574 + ctx
->DrawBuffer
->_Ymin
* rowStride
+ ctx
->DrawBuffer
->_Xmin
;
1576 for (i
= 0; i
< rows
; i
++) {
1577 for (j
= 0; j
< cols
; j
++) {
1578 dRow
[j
] = clearValue
;
1584 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1585 const GLint rows
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
1586 const GLint cols
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
1587 const GLint rowStride
= ctx
->DrawBuffer
->Width
;
1588 GLuint
*dRow
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
1589 + ctx
->DrawBuffer
->_Ymin
* rowStride
+ ctx
->DrawBuffer
->_Xmin
;
1591 for (i
= 0; i
< rows
; i
++) {
1592 for (j
= 0; j
< cols
; j
++) {
1593 dRow
[j
] = clearValue
;
1600 /* clear whole buffer */
1601 if (ctx
->Visual
.depthBits
<= 16) {
1602 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1603 if ((clearValue
& 0xff) == (clearValue
>> 8)) {
1604 if (clearValue
== 0) {
1605 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1606 2*ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
);
1609 /* lower and upper bytes of clear_value are same, use MEMSET */
1610 MEMSET( ctx
->DrawBuffer
->DepthBuffer
, clearValue
& 0xff,
1611 2 * ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
);
1615 GLushort
*d
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
;
1616 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1618 d
[0] = clearValue
; d
[1] = clearValue
;
1619 d
[2] = clearValue
; d
[3] = clearValue
;
1620 d
[4] = clearValue
; d
[5] = clearValue
;
1621 d
[6] = clearValue
; d
[7] = clearValue
;
1622 d
[8] = clearValue
; d
[9] = clearValue
;
1623 d
[10] = clearValue
; d
[11] = clearValue
;
1624 d
[12] = clearValue
; d
[13] = clearValue
;
1625 d
[14] = clearValue
; d
[15] = clearValue
;
1636 /* >16 bit depth buffer */
1637 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1638 if (clearValue
== 0) {
1639 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1640 ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
*sizeof(GLuint
));
1643 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1644 GLuint
*d
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
;
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
;