1 /* $Id: s_depth.c,v 1.1 2000/10/31 18:00:04 keithw Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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.
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
+ ctx
->DrawBuffer
->Width
* y
+ x
;
48 return (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
+ 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
, GLint x
, GLint y
,
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 gl_problem(ctx
, "Bad depth func in depth_test_span16");
304 depth_test_span32( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
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 gl_problem(ctx
, "Bad depth func in depth_test_span32");
534 * Apply depth test to span of fragments. Hardware or software z buffer.
537 _mesa_depth_test_span( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
538 const GLdepth z
[], GLubyte mask
[] )
540 if (ctx
->Driver
.ReadDepthSpan
) {
541 /* hardware-based depth buffer */
542 GLdepth zbuffer
[MAX_WIDTH
];
544 (*ctx
->Driver
.ReadDepthSpan
)(ctx
, n
, x
, y
, zbuffer
);
545 passed
= depth_test_span32(ctx
, n
, x
, y
, zbuffer
, z
, mask
);
546 assert(ctx
->Driver
.WriteDepthSpan
);
547 (*ctx
->Driver
.WriteDepthSpan
)(ctx
, n
, x
, y
, zbuffer
, mask
);
551 /* software depth buffer */
552 if (ctx
->Visual
.DepthBits
<= 16) {
553 GLushort
*zptr
= (GLushort
*) Z_ADDRESS16(ctx
, x
, y
);
554 GLuint passed
= depth_test_span16(ctx
, n
, x
, y
, zptr
, z
, mask
);
558 GLuint
*zptr
= (GLuint
*) Z_ADDRESS32(ctx
, x
, y
);
559 GLuint passed
= depth_test_span32(ctx
, n
, x
, y
, zptr
, z
, mask
);
569 * Do depth testing for an array of fragments using software Z buffer.
572 software_depth_test_pixels16( GLcontext
*ctx
, GLuint n
,
573 const GLint x
[], const GLint y
[],
574 const GLdepth z
[], GLubyte mask
[] )
576 /* switch cases ordered from most frequent to less frequent */
577 switch (ctx
->Depth
.Func
) {
579 if (ctx
->Depth
.Mask
) {
580 /* Update Z buffer */
582 for (i
=0; i
<n
; i
++) {
584 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
597 /* Don't update Z buffer */
599 for (i
=0; i
<n
; i
++) {
601 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
614 if (ctx
->Depth
.Mask
) {
615 /* Update Z buffer */
617 for (i
=0; i
<n
; i
++) {
619 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
632 /* Don't update Z buffer */
634 for (i
=0; i
<n
; i
++) {
636 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
649 if (ctx
->Depth
.Mask
) {
650 /* Update Z buffer */
652 for (i
=0; i
<n
; i
++) {
654 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
667 /* Don't update Z buffer */
669 for (i
=0; i
<n
; i
++) {
671 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
684 if (ctx
->Depth
.Mask
) {
685 /* Update Z buffer */
687 for (i
=0; i
<n
; i
++) {
689 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
702 /* Don't update Z buffer */
704 for (i
=0; i
<n
; i
++) {
706 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
719 if (ctx
->Depth
.Mask
) {
720 /* Update Z buffer */
722 for (i
=0; i
<n
; i
++) {
724 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
737 /* Don't update Z buffer */
739 for (i
=0; i
<n
; i
++) {
741 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
754 if (ctx
->Depth
.Mask
) {
755 /* Update Z buffer */
757 for (i
=0; i
<n
; i
++) {
759 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
772 /* Don't update Z buffer */
774 for (i
=0; i
<n
; i
++) {
776 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
789 if (ctx
->Depth
.Mask
) {
790 /* Update Z buffer */
792 for (i
=0; i
<n
; i
++) {
794 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
800 /* Don't update Z buffer or mask */
804 /* depth test never passes */
805 BZERO(mask
, n
* sizeof(GLubyte
));
808 gl_problem(ctx
, "Bad depth func in software_depth_test_pixels");
815 * Do depth testing for an array of fragments using software Z buffer.
818 software_depth_test_pixels32( GLcontext
*ctx
, GLuint n
,
819 const GLint x
[], const GLint y
[],
820 const GLdepth z
[], GLubyte mask
[] )
822 /* switch cases ordered from most frequent to less frequent */
823 switch (ctx
->Depth
.Func
) {
825 if (ctx
->Depth
.Mask
) {
826 /* Update Z buffer */
828 for (i
=0; i
<n
; i
++) {
830 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
843 /* Don't update Z buffer */
845 for (i
=0; i
<n
; i
++) {
847 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
860 if (ctx
->Depth
.Mask
) {
861 /* Update Z buffer */
863 for (i
=0; i
<n
; i
++) {
865 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
878 /* Don't update Z buffer */
880 for (i
=0; i
<n
; i
++) {
882 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
895 if (ctx
->Depth
.Mask
) {
896 /* Update Z buffer */
898 for (i
=0; i
<n
; i
++) {
900 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
913 /* Don't update Z buffer */
915 for (i
=0; i
<n
; i
++) {
917 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
930 if (ctx
->Depth
.Mask
) {
931 /* Update Z buffer */
933 for (i
=0; i
<n
; i
++) {
935 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
948 /* Don't update Z buffer */
950 for (i
=0; i
<n
; i
++) {
952 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
965 if (ctx
->Depth
.Mask
) {
966 /* Update Z buffer */
968 for (i
=0; i
<n
; i
++) {
970 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
983 /* Don't update Z buffer */
985 for (i
=0; i
<n
; i
++) {
987 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1000 if (ctx
->Depth
.Mask
) {
1001 /* Update Z buffer */
1003 for (i
=0; i
<n
; i
++) {
1005 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1006 if (z
[i
] == *zptr
) {
1018 /* Don't update Z buffer */
1020 for (i
=0; i
<n
; i
++) {
1022 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1023 if (z
[i
] == *zptr
) {
1035 if (ctx
->Depth
.Mask
) {
1036 /* Update Z buffer */
1038 for (i
=0; i
<n
; i
++) {
1040 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1046 /* Don't update Z buffer or mask */
1050 /* depth test never passes */
1051 BZERO(mask
, n
* sizeof(GLubyte
));
1054 gl_problem(ctx
, "Bad depth func in software_depth_test_pixels");
1061 * Do depth testing for an array of pixels using hardware Z buffer.
1062 * Input/output: zbuffer - array of depth values from Z buffer
1063 * Input: z - array of fragment z values.
1066 hardware_depth_test_pixels( GLcontext
*ctx
, GLuint n
, GLdepth zbuffer
[],
1067 const GLdepth z
[], GLubyte mask
[] )
1069 /* switch cases ordered from most frequent to less frequent */
1070 switch (ctx
->Depth
.Func
) {
1072 if (ctx
->Depth
.Mask
) {
1073 /* Update Z buffer */
1075 for (i
=0; i
<n
; i
++) {
1077 if (z
[i
] < zbuffer
[i
]) {
1089 /* Don't update Z buffer */
1091 for (i
=0; i
<n
; i
++) {
1093 if (z
[i
] < zbuffer
[i
]) {
1105 if (ctx
->Depth
.Mask
) {
1106 /* Update Z buffer */
1108 for (i
=0; i
<n
; i
++) {
1110 if (z
[i
] <= zbuffer
[i
]) {
1122 /* Don't update Z buffer */
1124 for (i
=0; i
<n
; i
++) {
1126 if (z
[i
] <= zbuffer
[i
]) {
1138 if (ctx
->Depth
.Mask
) {
1139 /* Update Z buffer */
1141 for (i
=0; i
<n
; i
++) {
1143 if (z
[i
] >= zbuffer
[i
]) {
1155 /* Don't update Z buffer */
1157 for (i
=0; i
<n
; i
++) {
1159 if (z
[i
] >= zbuffer
[i
]) {
1171 if (ctx
->Depth
.Mask
) {
1172 /* Update Z buffer */
1174 for (i
=0; i
<n
; i
++) {
1176 if (z
[i
] > zbuffer
[i
]) {
1188 /* Don't update Z buffer */
1190 for (i
=0; i
<n
; i
++) {
1192 if (z
[i
] > zbuffer
[i
]) {
1204 if (ctx
->Depth
.Mask
) {
1205 /* Update Z buffer */
1207 for (i
=0; i
<n
; i
++) {
1209 if (z
[i
] != zbuffer
[i
]) {
1221 /* Don't update Z buffer */
1223 for (i
=0; i
<n
; i
++) {
1225 if (z
[i
] != zbuffer
[i
]) {
1237 if (ctx
->Depth
.Mask
) {
1238 /* Update Z buffer */
1240 for (i
=0; i
<n
; i
++) {
1242 if (z
[i
] == zbuffer
[i
]) {
1254 /* Don't update Z buffer */
1256 for (i
=0; i
<n
; i
++) {
1258 if (z
[i
] == zbuffer
[i
]) {
1270 if (ctx
->Depth
.Mask
) {
1271 /* Update Z buffer */
1273 for (i
=0; i
<n
; i
++) {
1280 /* Don't update Z buffer or mask */
1284 /* depth test never passes */
1285 BZERO(mask
, n
* sizeof(GLubyte
));
1288 gl_problem(ctx
, "Bad depth func in hardware_depth_test_pixels");
1295 _mesa_depth_test_pixels( GLcontext
*ctx
,
1296 GLuint n
, const GLint x
[], const GLint y
[],
1297 const GLdepth z
[], GLubyte mask
[] )
1299 if (ctx
->Driver
.ReadDepthPixels
) {
1300 /* read depth values from hardware Z buffer */
1301 GLdepth zbuffer
[PB_SIZE
];
1302 (*ctx
->Driver
.ReadDepthPixels
)(ctx
, n
, x
, y
, zbuffer
);
1304 hardware_depth_test_pixels( ctx
, n
, zbuffer
, z
, mask
);
1306 /* update hardware Z buffer with new values */
1307 assert(ctx
->Driver
.WriteDepthPixels
);
1308 (*ctx
->Driver
.WriteDepthPixels
)(ctx
, n
, x
, y
, zbuffer
, mask
);
1311 /* software depth testing */
1312 if (ctx
->Visual
.DepthBits
<= 16)
1313 software_depth_test_pixels16(ctx
, n
, x
, y
, z
, mask
);
1315 software_depth_test_pixels32(ctx
, n
, x
, y
, z
, mask
);
1323 /**********************************************************************/
1324 /***** Read Depth Buffer *****/
1325 /**********************************************************************/
1329 * Read a span of depth values from the depth buffer.
1330 * This function does clipping before calling the device driver function.
1333 _mesa_read_depth_span( GLcontext
*ctx
,
1334 GLint n
, GLint x
, GLint y
, GLdepth depth
[] )
1336 if (y
< 0 || y
>= ctx
->DrawBuffer
->Height
||
1337 x
+ (GLint
) n
<= 0 || x
>= ctx
->DrawBuffer
->Width
) {
1338 /* span is completely outside framebuffer */
1340 for (i
= 0; i
< n
; i
++)
1348 for (i
= 0; i
< dx
; i
++)
1354 if (x
+ n
> ctx
->DrawBuffer
->Width
) {
1355 GLint dx
= x
+ n
- ctx
->DrawBuffer
->Width
;
1357 for (i
= 0; i
< dx
; i
++)
1358 depth
[n
- i
- 1] = 0;
1365 if (ctx
->DrawBuffer
->DepthBuffer
) {
1366 /* read from software depth buffer */
1367 if (ctx
->Visual
.DepthBits
<= 16) {
1368 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1370 for (i
= 0; i
< n
; i
++) {
1375 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1377 for (i
= 0; i
< n
; i
++) {
1382 else if (ctx
->Driver
.ReadDepthSpan
) {
1383 /* read from hardware depth buffer */
1384 (*ctx
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, depth
);
1387 /* no depth buffer */
1388 BZERO(depth
, n
* sizeof(GLfloat
));
1397 * Return a span of depth values from the depth buffer as floats in [0,1].
1398 * This is used for both hardware and software depth buffers.
1399 * Input: n - how many pixels
1400 * x,y - location of first pixel
1401 * Output: depth - the array of depth values
1404 _mesa_read_depth_span_float( GLcontext
*ctx
,
1405 GLint n
, GLint x
, GLint y
, GLfloat depth
[] )
1407 const GLfloat scale
= 1.0F
/ ctx
->Visual
.DepthMaxF
;
1409 if (y
< 0 || y
>= ctx
->DrawBuffer
->Height
||
1410 x
+ (GLint
) n
<= 0 || x
>= ctx
->DrawBuffer
->Width
) {
1411 /* span is completely outside framebuffer */
1413 for (i
= 0; i
< n
; i
++)
1421 for (i
= 0; i
< dx
; i
++)
1426 if (x
+ n
> ctx
->DrawBuffer
->Width
) {
1427 GLint dx
= x
+ n
- ctx
->DrawBuffer
->Width
;
1429 for (i
= 0; i
< dx
; i
++)
1430 depth
[n
- i
- 1] = 0.0F
;
1437 if (ctx
->DrawBuffer
->DepthBuffer
) {
1438 /* read from software depth buffer */
1439 if (ctx
->Visual
.DepthBits
<= 16) {
1440 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1442 for (i
= 0; i
< n
; i
++) {
1443 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1447 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1449 for (i
= 0; i
< n
; i
++) {
1450 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1454 else if (ctx
->Driver
.ReadDepthSpan
) {
1455 /* read from hardware depth buffer */
1456 GLdepth d
[MAX_WIDTH
];
1458 assert(n
<= MAX_WIDTH
);
1459 (*ctx
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, d
);
1460 for (i
= 0; i
< n
; i
++) {
1461 depth
[i
] = d
[i
] * scale
;
1465 /* no depth buffer */
1466 BZERO(depth
, n
* sizeof(GLfloat
));
1472 /**********************************************************************/
1473 /***** Allocate and Clear Depth Buffer *****/
1474 /**********************************************************************/
1479 * Allocate a new depth buffer. If there's already a depth buffer allocated
1480 * it will be free()'d. The new depth buffer will be uniniitalized.
1481 * This function is only called through Driver.alloc_depth_buffer.
1484 _mesa_alloc_depth_buffer( GLcontext
*ctx
)
1486 /* deallocate current depth buffer if present */
1487 if (ctx
->DrawBuffer
->UseSoftwareDepthBuffer
) {
1488 GLint bytesPerValue
;
1490 if (ctx
->DrawBuffer
->DepthBuffer
) {
1491 FREE(ctx
->DrawBuffer
->DepthBuffer
);
1492 ctx
->DrawBuffer
->DepthBuffer
= NULL
;
1495 /* allocate new depth buffer, but don't initialize it */
1496 if (ctx
->Visual
.DepthBits
<= 16)
1497 bytesPerValue
= sizeof(GLushort
);
1499 bytesPerValue
= sizeof(GLuint
);
1501 ctx
->DrawBuffer
->DepthBuffer
= MALLOC( ctx
->DrawBuffer
->Width
1502 * ctx
->DrawBuffer
->Height
1505 if (!ctx
->DrawBuffer
->DepthBuffer
) {
1507 ctx
->Depth
.Test
= GL_FALSE
;
1508 ctx
->NewState
|= _NEW_DEPTH
;
1509 gl_error( ctx
, GL_OUT_OF_MEMORY
, "Couldn't allocate depth buffer" );
1518 * Clear the depth buffer. If the depth buffer doesn't exist yet we'll
1520 * This function is only called through Driver.clear_depth_buffer.
1523 _mesa_clear_depth_buffer( GLcontext
*ctx
)
1525 if (ctx
->Visual
.DepthBits
== 0
1526 || !ctx
->DrawBuffer
->DepthBuffer
1527 || !ctx
->Depth
.Mask
) {
1528 /* no depth buffer, or writing to it is disabled */
1532 /* The loops in this function have been written so the IRIX 5.3
1533 * C compiler can unroll them. Hopefully other compilers can too!
1536 if (ctx
->Scissor
.Enabled
) {
1537 /* only clear scissor region */
1538 if (ctx
->Visual
.DepthBits
<= 16) {
1539 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->Visual
.DepthMax
);
1540 const GLint rows
= ctx
->DrawBuffer
->Ymax
- ctx
->DrawBuffer
->Ymin
;
1541 const GLint width
= ctx
->DrawBuffer
->Width
;
1542 GLushort
*dRow
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
1543 + ctx
->DrawBuffer
->Ymin
* width
+ ctx
->DrawBuffer
->Xmin
;
1545 for (i
= 0; i
< rows
; i
++) {
1546 for (j
= 0; j
< width
; j
++) {
1547 dRow
[j
] = clearValue
;
1553 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->Visual
.DepthMax
);
1554 const GLint rows
= ctx
->DrawBuffer
->Ymax
- ctx
->DrawBuffer
->Ymin
;
1555 const GLint width
= ctx
->DrawBuffer
->Width
;
1556 GLuint
*dRow
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
1557 + ctx
->DrawBuffer
->Ymin
* width
+ ctx
->DrawBuffer
->Xmin
;
1559 for (i
= 0; i
< rows
; i
++) {
1560 for (j
= 0; j
< width
; j
++) {
1561 dRow
[j
] = clearValue
;
1568 /* clear whole buffer */
1569 if (ctx
->Visual
.DepthBits
<= 16) {
1570 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->Visual
.DepthMax
);
1571 if ((clearValue
& 0xff) == (clearValue
>> 8)) {
1572 if (clearValue
== 0) {
1573 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1574 2*ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
);
1577 /* lower and upper bytes of clear_value are same, use MEMSET */
1578 MEMSET( ctx
->DrawBuffer
->DepthBuffer
, clearValue
& 0xff,
1579 2 * ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
);
1583 GLushort
*d
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
;
1584 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1586 d
[0] = clearValue
; d
[1] = clearValue
;
1587 d
[2] = clearValue
; d
[3] = clearValue
;
1588 d
[4] = clearValue
; d
[5] = clearValue
;
1589 d
[6] = clearValue
; d
[7] = clearValue
;
1590 d
[8] = clearValue
; d
[9] = clearValue
;
1591 d
[10] = clearValue
; d
[11] = clearValue
;
1592 d
[12] = clearValue
; d
[13] = clearValue
;
1593 d
[14] = clearValue
; d
[15] = clearValue
;
1604 /* >16 bit depth buffer */
1605 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->Visual
.DepthMax
);
1606 if (clearValue
== 0) {
1607 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1608 ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
*sizeof(GLuint
));
1611 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1612 GLuint
*d
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
;
1614 d
[0] = clearValue
; d
[1] = clearValue
;
1615 d
[2] = clearValue
; d
[3] = clearValue
;
1616 d
[4] = clearValue
; d
[5] = clearValue
;
1617 d
[6] = clearValue
; d
[7] = clearValue
;
1618 d
[8] = clearValue
; d
[9] = clearValue
;
1619 d
[10] = clearValue
; d
[11] = clearValue
;
1620 d
[12] = clearValue
; d
[13] = clearValue
;
1621 d
[14] = clearValue
; d
[15] = clearValue
;