1 /* $Id: s_depth.c,v 1.9 2001/03/19 02:25:36 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 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 _mesa_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
);
571 * Do depth testing for an array of fragments using software Z buffer.
574 software_depth_test_pixels16( GLcontext
*ctx
, GLuint n
,
575 const GLint x
[], const GLint y
[],
576 const GLdepth z
[], GLubyte mask
[] )
578 /* switch cases ordered from most frequent to less frequent */
579 switch (ctx
->Depth
.Func
) {
581 if (ctx
->Depth
.Mask
) {
582 /* Update Z buffer */
584 for (i
=0; i
<n
; i
++) {
586 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
599 /* Don't update Z buffer */
601 for (i
=0; i
<n
; i
++) {
603 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
616 if (ctx
->Depth
.Mask
) {
617 /* Update Z buffer */
619 for (i
=0; i
<n
; i
++) {
621 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
634 /* Don't update Z buffer */
636 for (i
=0; i
<n
; i
++) {
638 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
651 if (ctx
->Depth
.Mask
) {
652 /* Update Z buffer */
654 for (i
=0; i
<n
; i
++) {
656 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
669 /* Don't update Z buffer */
671 for (i
=0; i
<n
; i
++) {
673 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
686 if (ctx
->Depth
.Mask
) {
687 /* Update Z buffer */
689 for (i
=0; i
<n
; i
++) {
691 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
704 /* Don't update Z buffer */
706 for (i
=0; i
<n
; i
++) {
708 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
721 if (ctx
->Depth
.Mask
) {
722 /* Update Z buffer */
724 for (i
=0; i
<n
; i
++) {
726 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
739 /* Don't update Z buffer */
741 for (i
=0; i
<n
; i
++) {
743 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
756 if (ctx
->Depth
.Mask
) {
757 /* Update Z buffer */
759 for (i
=0; i
<n
; i
++) {
761 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
774 /* Don't update Z buffer */
776 for (i
=0; i
<n
; i
++) {
778 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
791 if (ctx
->Depth
.Mask
) {
792 /* Update Z buffer */
794 for (i
=0; i
<n
; i
++) {
796 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
802 /* Don't update Z buffer or mask */
806 /* depth test never passes */
807 BZERO(mask
, n
* sizeof(GLubyte
));
810 _mesa_problem(ctx
, "Bad depth func in software_depth_test_pixels");
817 * Do depth testing for an array of fragments using software Z buffer.
820 software_depth_test_pixels32( GLcontext
*ctx
, GLuint n
,
821 const GLint x
[], const GLint y
[],
822 const GLdepth z
[], GLubyte mask
[] )
824 /* switch cases ordered from most frequent to less frequent */
825 switch (ctx
->Depth
.Func
) {
827 if (ctx
->Depth
.Mask
) {
828 /* Update Z buffer */
830 for (i
=0; i
<n
; i
++) {
832 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
845 /* Don't update Z buffer */
847 for (i
=0; i
<n
; i
++) {
849 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
862 if (ctx
->Depth
.Mask
) {
863 /* Update Z buffer */
865 for (i
=0; i
<n
; i
++) {
867 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
880 /* Don't update Z buffer */
882 for (i
=0; i
<n
; i
++) {
884 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
897 if (ctx
->Depth
.Mask
) {
898 /* Update Z buffer */
900 for (i
=0; i
<n
; i
++) {
902 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
915 /* Don't update Z buffer */
917 for (i
=0; i
<n
; i
++) {
919 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
932 if (ctx
->Depth
.Mask
) {
933 /* Update Z buffer */
935 for (i
=0; i
<n
; i
++) {
937 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
950 /* Don't update Z buffer */
952 for (i
=0; i
<n
; i
++) {
954 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
967 if (ctx
->Depth
.Mask
) {
968 /* Update Z buffer */
970 for (i
=0; i
<n
; i
++) {
972 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
985 /* Don't update Z buffer */
987 for (i
=0; i
<n
; i
++) {
989 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1002 if (ctx
->Depth
.Mask
) {
1003 /* Update Z buffer */
1005 for (i
=0; i
<n
; i
++) {
1007 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1008 if (z
[i
] == *zptr
) {
1020 /* Don't update Z buffer */
1022 for (i
=0; i
<n
; i
++) {
1024 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1025 if (z
[i
] == *zptr
) {
1037 if (ctx
->Depth
.Mask
) {
1038 /* Update Z buffer */
1040 for (i
=0; i
<n
; i
++) {
1042 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1048 /* Don't update Z buffer or mask */
1052 /* depth test never passes */
1053 BZERO(mask
, n
* sizeof(GLubyte
));
1056 _mesa_problem(ctx
, "Bad depth func in software_depth_test_pixels");
1063 * Do depth testing for an array of pixels using hardware Z buffer.
1064 * Input/output: zbuffer - array of depth values from Z buffer
1065 * Input: z - array of fragment z values.
1068 hardware_depth_test_pixels( GLcontext
*ctx
, GLuint n
, GLdepth zbuffer
[],
1069 const GLdepth z
[], GLubyte mask
[] )
1071 /* switch cases ordered from most frequent to less frequent */
1072 switch (ctx
->Depth
.Func
) {
1074 if (ctx
->Depth
.Mask
) {
1075 /* Update Z buffer */
1077 for (i
=0; i
<n
; i
++) {
1079 if (z
[i
] < zbuffer
[i
]) {
1091 /* Don't update Z buffer */
1093 for (i
=0; i
<n
; i
++) {
1095 if (z
[i
] < zbuffer
[i
]) {
1107 if (ctx
->Depth
.Mask
) {
1108 /* Update Z buffer */
1110 for (i
=0; i
<n
; i
++) {
1112 if (z
[i
] <= zbuffer
[i
]) {
1124 /* Don't update Z buffer */
1126 for (i
=0; i
<n
; i
++) {
1128 if (z
[i
] <= zbuffer
[i
]) {
1140 if (ctx
->Depth
.Mask
) {
1141 /* Update Z buffer */
1143 for (i
=0; i
<n
; i
++) {
1145 if (z
[i
] >= zbuffer
[i
]) {
1157 /* Don't update Z buffer */
1159 for (i
=0; i
<n
; i
++) {
1161 if (z
[i
] >= zbuffer
[i
]) {
1173 if (ctx
->Depth
.Mask
) {
1174 /* Update Z buffer */
1176 for (i
=0; i
<n
; i
++) {
1178 if (z
[i
] > zbuffer
[i
]) {
1190 /* Don't update Z buffer */
1192 for (i
=0; i
<n
; i
++) {
1194 if (z
[i
] > zbuffer
[i
]) {
1206 if (ctx
->Depth
.Mask
) {
1207 /* Update Z buffer */
1209 for (i
=0; i
<n
; i
++) {
1211 if (z
[i
] != zbuffer
[i
]) {
1223 /* Don't update Z buffer */
1225 for (i
=0; i
<n
; i
++) {
1227 if (z
[i
] != zbuffer
[i
]) {
1239 if (ctx
->Depth
.Mask
) {
1240 /* Update Z buffer */
1242 for (i
=0; i
<n
; i
++) {
1244 if (z
[i
] == zbuffer
[i
]) {
1256 /* Don't update Z buffer */
1258 for (i
=0; i
<n
; i
++) {
1260 if (z
[i
] == zbuffer
[i
]) {
1272 if (ctx
->Depth
.Mask
) {
1273 /* Update Z buffer */
1275 for (i
=0; i
<n
; i
++) {
1282 /* Don't update Z buffer or mask */
1286 /* depth test never passes */
1287 BZERO(mask
, n
* sizeof(GLubyte
));
1290 _mesa_problem(ctx
, "Bad depth func in hardware_depth_test_pixels");
1297 _mesa_depth_test_pixels( GLcontext
*ctx
,
1298 GLuint n
, const GLint x
[], const GLint y
[],
1299 const GLdepth z
[], GLubyte mask
[] )
1301 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1302 if (swrast
->Driver
.ReadDepthPixels
) {
1303 /* read depth values from hardware Z buffer */
1304 GLdepth zbuffer
[PB_SIZE
];
1305 (*swrast
->Driver
.ReadDepthPixels
)(ctx
, n
, x
, y
, zbuffer
);
1307 hardware_depth_test_pixels( ctx
, n
, zbuffer
, z
, mask
);
1309 /* update hardware Z buffer with new values */
1310 assert(swrast
->Driver
.WriteDepthPixels
);
1311 (*swrast
->Driver
.WriteDepthPixels
)(ctx
, n
, x
, y
, zbuffer
, mask
);
1314 /* software depth testing */
1315 if (ctx
->Visual
.depthBits
<= 16)
1316 software_depth_test_pixels16(ctx
, n
, x
, y
, z
, mask
);
1318 software_depth_test_pixels32(ctx
, n
, x
, y
, z
, mask
);
1326 /**********************************************************************/
1327 /***** Read Depth Buffer *****/
1328 /**********************************************************************/
1332 * Read a span of depth values from the depth buffer.
1333 * This function does clipping before calling the device driver function.
1336 _mesa_read_depth_span( GLcontext
*ctx
,
1337 GLint n
, GLint x
, GLint y
, GLdepth depth
[] )
1339 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1341 if (y
< 0 || y
>= ctx
->DrawBuffer
->Height
||
1342 x
+ (GLint
) n
<= 0 || x
>= ctx
->DrawBuffer
->Width
) {
1343 /* span is completely outside framebuffer */
1345 for (i
= 0; i
< n
; i
++)
1353 for (i
= 0; i
< dx
; i
++)
1359 if (x
+ n
> ctx
->DrawBuffer
->Width
) {
1360 GLint dx
= x
+ n
- ctx
->DrawBuffer
->Width
;
1362 for (i
= 0; i
< dx
; i
++)
1363 depth
[n
- i
- 1] = 0;
1370 if (ctx
->DrawBuffer
->DepthBuffer
) {
1371 /* read from software depth buffer */
1372 if (ctx
->Visual
.depthBits
<= 16) {
1373 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1375 for (i
= 0; i
< n
; i
++) {
1380 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1382 for (i
= 0; i
< n
; i
++) {
1387 else if (swrast
->Driver
.ReadDepthSpan
) {
1388 /* read from hardware depth buffer */
1389 (*swrast
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, depth
);
1392 /* no depth buffer */
1393 BZERO(depth
, n
* sizeof(GLfloat
));
1402 * Return a span of depth values from the depth buffer as floats in [0,1].
1403 * This is used for both hardware and software depth buffers.
1404 * Input: n - how many pixels
1405 * x,y - location of first pixel
1406 * Output: depth - the array of depth values
1409 _mesa_read_depth_span_float( GLcontext
*ctx
,
1410 GLint n
, GLint x
, GLint y
, GLfloat depth
[] )
1412 SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
1413 const GLfloat scale
= 1.0F
/ ctx
->DepthMaxF
;
1415 if (y
< 0 || y
>= ctx
->DrawBuffer
->Height
||
1416 x
+ (GLint
) n
<= 0 || x
>= ctx
->DrawBuffer
->Width
) {
1417 /* span is completely outside framebuffer */
1419 for (i
= 0; i
< n
; i
++)
1427 for (i
= 0; i
< dx
; i
++)
1432 if (x
+ n
> ctx
->DrawBuffer
->Width
) {
1433 GLint dx
= x
+ n
- ctx
->DrawBuffer
->Width
;
1435 for (i
= 0; i
< dx
; i
++)
1436 depth
[n
- i
- 1] = 0.0F
;
1443 if (ctx
->DrawBuffer
->DepthBuffer
) {
1444 /* read from software depth buffer */
1445 if (ctx
->Visual
.depthBits
<= 16) {
1446 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1448 for (i
= 0; i
< n
; i
++) {
1449 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1453 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1455 for (i
= 0; i
< n
; i
++) {
1456 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1460 else if (swrast
->Driver
.ReadDepthSpan
) {
1461 /* read from hardware depth buffer */
1462 GLdepth d
[MAX_WIDTH
];
1464 assert(n
<= MAX_WIDTH
);
1465 (*swrast
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, d
);
1466 for (i
= 0; i
< n
; i
++) {
1467 depth
[i
] = d
[i
] * scale
;
1471 /* no depth buffer */
1472 BZERO(depth
, n
* sizeof(GLfloat
));
1478 /**********************************************************************/
1479 /***** Allocate and Clear Depth Buffer *****/
1480 /**********************************************************************/
1485 * Allocate a new depth buffer. If there's already a depth buffer allocated
1486 * it will be free()'d. The new depth buffer will be uniniitalized.
1487 * This function is only called through Driver.alloc_depth_buffer.
1490 _mesa_alloc_depth_buffer( GLcontext
*ctx
)
1492 /* deallocate current depth buffer if present */
1493 if (ctx
->DrawBuffer
->UseSoftwareDepthBuffer
) {
1494 GLint bytesPerValue
;
1496 if (ctx
->DrawBuffer
->DepthBuffer
) {
1497 FREE(ctx
->DrawBuffer
->DepthBuffer
);
1498 ctx
->DrawBuffer
->DepthBuffer
= NULL
;
1501 /* allocate new depth buffer, but don't initialize it */
1502 if (ctx
->Visual
.depthBits
<= 16)
1503 bytesPerValue
= sizeof(GLushort
);
1505 bytesPerValue
= sizeof(GLuint
);
1507 ctx
->DrawBuffer
->DepthBuffer
= MALLOC( ctx
->DrawBuffer
->Width
1508 * ctx
->DrawBuffer
->Height
1511 if (!ctx
->DrawBuffer
->DepthBuffer
) {
1513 ctx
->Depth
.Test
= GL_FALSE
;
1514 ctx
->NewState
|= _NEW_DEPTH
;
1515 _mesa_error( ctx
, GL_OUT_OF_MEMORY
, "Couldn't allocate depth buffer" );
1524 * Clear the depth buffer. If the depth buffer doesn't exist yet we'll
1526 * This function is only called through Driver.clear_depth_buffer.
1529 _mesa_clear_depth_buffer( GLcontext
*ctx
)
1531 if (ctx
->Visual
.depthBits
== 0
1532 || !ctx
->DrawBuffer
->DepthBuffer
1533 || !ctx
->Depth
.Mask
) {
1534 /* no depth buffer, or writing to it is disabled */
1538 /* The loops in this function have been written so the IRIX 5.3
1539 * C compiler can unroll them. Hopefully other compilers can too!
1542 if (ctx
->Scissor
.Enabled
) {
1543 /* only clear scissor region */
1544 if (ctx
->Visual
.depthBits
<= 16) {
1545 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1546 const GLint rows
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
1547 const GLint cols
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
1548 const GLint rowStride
= ctx
->DrawBuffer
->Width
;
1549 GLushort
*dRow
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
1550 + ctx
->DrawBuffer
->_Ymin
* rowStride
+ ctx
->DrawBuffer
->_Xmin
;
1552 for (i
= 0; i
< rows
; i
++) {
1553 for (j
= 0; j
< cols
; j
++) {
1554 dRow
[j
] = clearValue
;
1560 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1561 const GLint rows
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
1562 const GLint cols
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
1563 const GLint rowStride
= ctx
->DrawBuffer
->Width
;
1564 GLuint
*dRow
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
1565 + ctx
->DrawBuffer
->_Ymin
* rowStride
+ ctx
->DrawBuffer
->_Xmin
;
1567 for (i
= 0; i
< rows
; i
++) {
1568 for (j
= 0; j
< cols
; j
++) {
1569 dRow
[j
] = clearValue
;
1576 /* clear whole buffer */
1577 if (ctx
->Visual
.depthBits
<= 16) {
1578 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1579 if ((clearValue
& 0xff) == (clearValue
>> 8)) {
1580 if (clearValue
== 0) {
1581 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1582 2*ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
);
1585 /* lower and upper bytes of clear_value are same, use MEMSET */
1586 MEMSET( ctx
->DrawBuffer
->DepthBuffer
, clearValue
& 0xff,
1587 2 * ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
);
1591 GLushort
*d
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
;
1592 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1594 d
[0] = clearValue
; d
[1] = clearValue
;
1595 d
[2] = clearValue
; d
[3] = clearValue
;
1596 d
[4] = clearValue
; d
[5] = clearValue
;
1597 d
[6] = clearValue
; d
[7] = clearValue
;
1598 d
[8] = clearValue
; d
[9] = clearValue
;
1599 d
[10] = clearValue
; d
[11] = clearValue
;
1600 d
[12] = clearValue
; d
[13] = clearValue
;
1601 d
[14] = clearValue
; d
[15] = clearValue
;
1612 /* >16 bit depth buffer */
1613 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->DepthMax
);
1614 if (clearValue
== 0) {
1615 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1616 ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
*sizeof(GLuint
));
1619 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1620 GLuint
*d
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
;
1622 d
[0] = clearValue
; d
[1] = clearValue
;
1623 d
[2] = clearValue
; d
[3] = clearValue
;
1624 d
[4] = clearValue
; d
[5] = clearValue
;
1625 d
[6] = clearValue
; d
[7] = clearValue
;
1626 d
[8] = clearValue
; d
[9] = clearValue
;
1627 d
[10] = clearValue
; d
[11] = clearValue
;
1628 d
[12] = clearValue
; d
[13] = clearValue
;
1629 d
[14] = clearValue
; d
[15] = clearValue
;