1 /* $Id: depth.c,v 1.16 2000/04/04 00:54:23 brianp 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.
42 /**********************************************************************/
43 /***** API Functions *****/
44 /**********************************************************************/
49 _mesa_ClearDepth( GLclampd depth
)
51 GET_CURRENT_CONTEXT(ctx
);
52 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glClearDepth");
53 ctx
->Depth
.Clear
= (GLfloat
) CLAMP( depth
, 0.0, 1.0 );
54 if (ctx
->Driver
.ClearDepth
)
55 (*ctx
->Driver
.ClearDepth
)( ctx
, ctx
->Depth
.Clear
);
61 _mesa_DepthFunc( GLenum func
)
63 GET_CURRENT_CONTEXT(ctx
);
64 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDepthFunc");
66 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
67 fprintf(stderr
, "glDepthFunc %s\n", gl_lookup_enum_by_nr(func
));
70 case GL_LESS
: /* (default) pass if incoming z < stored z */
77 if (ctx
->Depth
.Func
!= func
) {
78 ctx
->Depth
.Func
= func
;
79 ctx
->NewState
|= NEW_RASTER_OPS
;
80 ctx
->TriangleCaps
&= ~DD_Z_NEVER
;
81 if (ctx
->Driver
.DepthFunc
) {
82 (*ctx
->Driver
.DepthFunc
)( ctx
, func
);
87 if (ctx
->Depth
.Func
!= func
) {
88 ctx
->Depth
.Func
= func
;
89 ctx
->NewState
|= NEW_RASTER_OPS
;
90 ctx
->TriangleCaps
|= DD_Z_NEVER
;
91 if (ctx
->Driver
.DepthFunc
) {
92 (*ctx
->Driver
.DepthFunc
)( ctx
, func
);
97 gl_error( ctx
, GL_INVALID_ENUM
, "glDepth.Func" );
104 _mesa_DepthMask( GLboolean flag
)
106 GET_CURRENT_CONTEXT(ctx
);
107 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDepthMask");
109 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
110 fprintf(stderr
, "glDepthMask %d\n", flag
);
113 * GL_TRUE indicates depth buffer writing is enabled (default)
114 * GL_FALSE indicates depth buffer writing is disabled
116 if (ctx
->Depth
.Mask
!= flag
) {
117 ctx
->Depth
.Mask
= flag
;
118 ctx
->NewState
|= NEW_RASTER_OPS
;
119 if (ctx
->Driver
.DepthMask
) {
120 (*ctx
->Driver
.DepthMask
)( ctx
, flag
);
127 /**********************************************************************/
129 /**********************************************************************/
132 * Return address of depth buffer value for given window coord.
135 _mesa_zbuffer_address(GLcontext
*ctx
, GLint x
, GLint y
)
137 if (ctx
->Visual
->DepthBits
<= 16)
138 return (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
+ ctx
->DrawBuffer
->Width
* y
+ x
;
140 return (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
+ ctx
->DrawBuffer
->Width
* y
+ x
;
144 #define Z_ADDRESS16( CTX, X, Y ) \
145 ( ((GLushort *) (CTX)->DrawBuffer->DepthBuffer) \
146 + (CTX)->DrawBuffer->Width * (Y) + (X) )
148 #define Z_ADDRESS32( CTX, X, Y ) \
149 ( ((GLuint *) (CTX)->DrawBuffer->DepthBuffer) \
150 + (CTX)->DrawBuffer->Width * (Y) + (X) )
154 /**********************************************************************/
155 /***** Depth Testing Functions *****/
156 /**********************************************************************/
160 * Do depth test for an array of fragments. This is used both for
161 * software and hardware Z buffers.
162 * Input: zbuffer - array of z values in the zbuffer
163 * z - array of fragment z values
164 * Return: number of fragments which pass the test.
167 depth_test_span16( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
168 GLushort zbuffer
[], const GLdepth z
[], GLubyte mask
[] )
172 /* switch cases ordered from most frequent to less frequent */
173 switch (ctx
->Depth
.Func
) {
175 if (ctx
->Depth
.Mask
) {
176 /* Update Z buffer */
178 for (i
=0; i
<n
; i
++) {
180 if (z
[i
] < zbuffer
[i
]) {
193 /* Don't update Z buffer */
195 for (i
=0; i
<n
; i
++) {
197 if (z
[i
] < zbuffer
[i
]) {
209 if (ctx
->Depth
.Mask
) {
210 /* Update Z buffer */
214 if (z
[i
] <= zbuffer
[i
]) {
225 /* Don't update Z buffer */
229 if (z
[i
] <= zbuffer
[i
]) {
241 if (ctx
->Depth
.Mask
) {
242 /* Update Z buffer */
246 if (z
[i
] >= zbuffer
[i
]) {
257 /* Don't update Z buffer */
261 if (z
[i
] >= zbuffer
[i
]) {
273 if (ctx
->Depth
.Mask
) {
274 /* Update Z buffer */
278 if (z
[i
] > zbuffer
[i
]) {
289 /* Don't update Z buffer */
293 if (z
[i
] > zbuffer
[i
]) {
305 if (ctx
->Depth
.Mask
) {
306 /* Update Z buffer */
310 if (z
[i
] != zbuffer
[i
]) {
321 /* Don't update Z buffer */
325 if (z
[i
] != zbuffer
[i
]) {
337 if (ctx
->Depth
.Mask
) {
338 /* Update Z buffer */
342 if (z
[i
] == zbuffer
[i
]) {
353 /* Don't update Z buffer */
357 if (z
[i
] == zbuffer
[i
]) {
369 if (ctx
->Depth
.Mask
) {
370 /* Update Z buffer */
380 /* Don't update Z buffer or mask */
385 BZERO(mask
, n
* sizeof(GLubyte
));
388 gl_problem(ctx
, "Bad depth func in depth_test_span16");
396 depth_test_span32( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
397 GLuint zbuffer
[], const GLdepth z
[], GLubyte mask
[] )
401 /* switch cases ordered from most frequent to less frequent */
402 switch (ctx
->Depth
.Func
) {
404 if (ctx
->Depth
.Mask
) {
405 /* Update Z buffer */
407 for (i
=0; i
<n
; i
++) {
409 if (z
[i
] < zbuffer
[i
]) {
422 /* Don't update Z buffer */
424 for (i
=0; i
<n
; i
++) {
426 if (z
[i
] < zbuffer
[i
]) {
438 if (ctx
->Depth
.Mask
) {
439 /* Update Z buffer */
443 if (z
[i
] <= zbuffer
[i
]) {
454 /* Don't update Z buffer */
458 if (z
[i
] <= zbuffer
[i
]) {
470 if (ctx
->Depth
.Mask
) {
471 /* Update Z buffer */
475 if (z
[i
] >= zbuffer
[i
]) {
486 /* Don't update Z buffer */
490 if (z
[i
] >= zbuffer
[i
]) {
502 if (ctx
->Depth
.Mask
) {
503 /* Update Z buffer */
507 if (z
[i
] > zbuffer
[i
]) {
518 /* Don't update Z buffer */
522 if (z
[i
] > zbuffer
[i
]) {
534 if (ctx
->Depth
.Mask
) {
535 /* Update Z buffer */
539 if (z
[i
] != zbuffer
[i
]) {
550 /* Don't update Z buffer */
554 if (z
[i
] != zbuffer
[i
]) {
566 if (ctx
->Depth
.Mask
) {
567 /* Update Z buffer */
571 if (z
[i
] == zbuffer
[i
]) {
582 /* Don't update Z buffer */
586 if (z
[i
] == zbuffer
[i
]) {
598 if (ctx
->Depth
.Mask
) {
599 /* Update Z buffer */
609 /* Don't update Z buffer or mask */
614 BZERO(mask
, n
* sizeof(GLubyte
));
617 gl_problem(ctx
, "Bad depth func in depth_test_span32");
626 * Apply depth test to span of fragments. Hardware or software z buffer.
629 _mesa_depth_test_span( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
630 const GLdepth z
[], GLubyte mask
[] )
632 if (ctx
->Driver
.ReadDepthSpan
) {
633 /* hardware-based depth buffer */
634 GLdepth zbuffer
[MAX_WIDTH
];
636 (*ctx
->Driver
.ReadDepthSpan
)(ctx
, n
, x
, y
, zbuffer
);
637 passed
= depth_test_span32(ctx
, n
, x
, y
, zbuffer
, z
, mask
);
638 assert(ctx
->Driver
.WriteDepthSpan
);
639 (*ctx
->Driver
.WriteDepthSpan
)(ctx
, n
, x
, y
, zbuffer
, mask
);
643 /* software depth buffer */
644 if (ctx
->Visual
->DepthBits
<= 16) {
645 GLushort
*zptr
= (GLushort
*) Z_ADDRESS16(ctx
, x
, y
);
646 GLuint passed
= depth_test_span16(ctx
, n
, x
, y
, zptr
, z
, mask
);
650 GLuint
*zptr
= (GLuint
*) Z_ADDRESS32(ctx
, x
, y
);
651 GLuint passed
= depth_test_span32(ctx
, n
, x
, y
, zptr
, z
, mask
);
661 * Do depth testing for an array of fragments using software Z buffer.
664 software_depth_test_pixels16( GLcontext
*ctx
, GLuint n
,
665 const GLint x
[], const GLint y
[],
666 const GLdepth z
[], GLubyte mask
[] )
668 /* switch cases ordered from most frequent to less frequent */
669 switch (ctx
->Depth
.Func
) {
671 if (ctx
->Depth
.Mask
) {
672 /* Update Z buffer */
674 for (i
=0; i
<n
; i
++) {
676 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
689 /* Don't update Z buffer */
691 for (i
=0; i
<n
; i
++) {
693 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
706 if (ctx
->Depth
.Mask
) {
707 /* Update Z buffer */
709 for (i
=0; i
<n
; i
++) {
711 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
724 /* Don't update Z buffer */
726 for (i
=0; i
<n
; i
++) {
728 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
741 if (ctx
->Depth
.Mask
) {
742 /* Update Z buffer */
744 for (i
=0; i
<n
; i
++) {
746 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
759 /* Don't update Z buffer */
761 for (i
=0; i
<n
; i
++) {
763 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
776 if (ctx
->Depth
.Mask
) {
777 /* Update Z buffer */
779 for (i
=0; i
<n
; i
++) {
781 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
794 /* Don't update Z buffer */
796 for (i
=0; i
<n
; i
++) {
798 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
811 if (ctx
->Depth
.Mask
) {
812 /* Update Z buffer */
814 for (i
=0; i
<n
; i
++) {
816 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
829 /* Don't update Z buffer */
831 for (i
=0; i
<n
; i
++) {
833 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
846 if (ctx
->Depth
.Mask
) {
847 /* Update Z buffer */
849 for (i
=0; i
<n
; i
++) {
851 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
864 /* Don't update Z buffer */
866 for (i
=0; i
<n
; i
++) {
868 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
881 if (ctx
->Depth
.Mask
) {
882 /* Update Z buffer */
884 for (i
=0; i
<n
; i
++) {
886 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
892 /* Don't update Z buffer or mask */
896 /* depth test never passes */
897 BZERO(mask
, n
* sizeof(GLubyte
));
900 gl_problem(ctx
, "Bad depth func in software_depth_test_pixels");
907 * Do depth testing for an array of fragments using software Z buffer.
910 software_depth_test_pixels32( GLcontext
*ctx
, GLuint n
,
911 const GLint x
[], const GLint y
[],
912 const GLdepth z
[], GLubyte mask
[] )
914 /* switch cases ordered from most frequent to less frequent */
915 switch (ctx
->Depth
.Func
) {
917 if (ctx
->Depth
.Mask
) {
918 /* Update Z buffer */
920 for (i
=0; i
<n
; i
++) {
922 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
935 /* Don't update Z buffer */
937 for (i
=0; i
<n
; i
++) {
939 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
952 if (ctx
->Depth
.Mask
) {
953 /* Update Z buffer */
955 for (i
=0; i
<n
; i
++) {
957 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
970 /* Don't update Z buffer */
972 for (i
=0; i
<n
; i
++) {
974 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
987 if (ctx
->Depth
.Mask
) {
988 /* Update Z buffer */
990 for (i
=0; i
<n
; i
++) {
992 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1005 /* Don't update Z buffer */
1007 for (i
=0; i
<n
; i
++) {
1009 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1010 if (z
[i
] >= *zptr
) {
1022 if (ctx
->Depth
.Mask
) {
1023 /* Update Z buffer */
1025 for (i
=0; i
<n
; i
++) {
1027 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1040 /* Don't update Z buffer */
1042 for (i
=0; i
<n
; i
++) {
1044 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1057 if (ctx
->Depth
.Mask
) {
1058 /* Update Z buffer */
1060 for (i
=0; i
<n
; i
++) {
1062 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1063 if (z
[i
] != *zptr
) {
1075 /* Don't update Z buffer */
1077 for (i
=0; i
<n
; i
++) {
1079 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1080 if (z
[i
] != *zptr
) {
1092 if (ctx
->Depth
.Mask
) {
1093 /* Update Z buffer */
1095 for (i
=0; i
<n
; i
++) {
1097 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1098 if (z
[i
] == *zptr
) {
1110 /* Don't update Z buffer */
1112 for (i
=0; i
<n
; i
++) {
1114 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1115 if (z
[i
] == *zptr
) {
1127 if (ctx
->Depth
.Mask
) {
1128 /* Update Z buffer */
1130 for (i
=0; i
<n
; i
++) {
1132 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1138 /* Don't update Z buffer or mask */
1142 /* depth test never passes */
1143 BZERO(mask
, n
* sizeof(GLubyte
));
1146 gl_problem(ctx
, "Bad depth func in software_depth_test_pixels");
1153 * Do depth testing for an array of pixels using hardware Z buffer.
1154 * Input/output: zbuffer - array of depth values from Z buffer
1155 * Input: z - array of fragment z values.
1158 hardware_depth_test_pixels( GLcontext
*ctx
, GLuint n
, GLdepth zbuffer
[],
1159 const GLdepth z
[], GLubyte mask
[] )
1161 /* switch cases ordered from most frequent to less frequent */
1162 switch (ctx
->Depth
.Func
) {
1164 if (ctx
->Depth
.Mask
) {
1165 /* Update Z buffer */
1167 for (i
=0; i
<n
; i
++) {
1169 if (z
[i
] < zbuffer
[i
]) {
1181 /* Don't update Z buffer */
1183 for (i
=0; i
<n
; i
++) {
1185 if (z
[i
] < zbuffer
[i
]) {
1197 if (ctx
->Depth
.Mask
) {
1198 /* Update Z buffer */
1200 for (i
=0; i
<n
; i
++) {
1202 if (z
[i
] <= zbuffer
[i
]) {
1214 /* Don't update Z buffer */
1216 for (i
=0; i
<n
; i
++) {
1218 if (z
[i
] <= zbuffer
[i
]) {
1230 if (ctx
->Depth
.Mask
) {
1231 /* Update Z buffer */
1233 for (i
=0; i
<n
; i
++) {
1235 if (z
[i
] >= zbuffer
[i
]) {
1247 /* Don't update Z buffer */
1249 for (i
=0; i
<n
; i
++) {
1251 if (z
[i
] >= zbuffer
[i
]) {
1263 if (ctx
->Depth
.Mask
) {
1264 /* Update Z buffer */
1266 for (i
=0; i
<n
; i
++) {
1268 if (z
[i
] > zbuffer
[i
]) {
1280 /* Don't update Z buffer */
1282 for (i
=0; i
<n
; i
++) {
1284 if (z
[i
] > zbuffer
[i
]) {
1296 if (ctx
->Depth
.Mask
) {
1297 /* Update Z buffer */
1299 for (i
=0; i
<n
; i
++) {
1301 if (z
[i
] != zbuffer
[i
]) {
1313 /* Don't update Z buffer */
1315 for (i
=0; i
<n
; i
++) {
1317 if (z
[i
] != zbuffer
[i
]) {
1329 if (ctx
->Depth
.Mask
) {
1330 /* Update Z buffer */
1332 for (i
=0; i
<n
; i
++) {
1334 if (z
[i
] == zbuffer
[i
]) {
1346 /* Don't update Z buffer */
1348 for (i
=0; i
<n
; i
++) {
1350 if (z
[i
] == zbuffer
[i
]) {
1362 if (ctx
->Depth
.Mask
) {
1363 /* Update Z buffer */
1365 for (i
=0; i
<n
; i
++) {
1372 /* Don't update Z buffer or mask */
1376 /* depth test never passes */
1377 BZERO(mask
, n
* sizeof(GLubyte
));
1380 gl_problem(ctx
, "Bad depth func in hardware_depth_test_pixels");
1387 _mesa_depth_test_pixels( GLcontext
*ctx
,
1388 GLuint n
, const GLint x
[], const GLint y
[],
1389 const GLdepth z
[], GLubyte mask
[] )
1391 if (ctx
->Driver
.ReadDepthPixels
) {
1392 /* read depth values from hardware Z buffer */
1393 GLdepth zbuffer
[PB_SIZE
];
1394 (*ctx
->Driver
.ReadDepthPixels
)(ctx
, n
, x
, y
, zbuffer
);
1396 hardware_depth_test_pixels( ctx
, n
, zbuffer
, z
, mask
);
1398 /* update hardware Z buffer with new values */
1399 assert(ctx
->Driver
.WriteDepthPixels
);
1400 (*ctx
->Driver
.WriteDepthPixels
)(ctx
, n
, x
, y
, zbuffer
, mask
);
1403 /* software depth testing */
1404 if (ctx
->Visual
->DepthBits
<= 16)
1405 software_depth_test_pixels16(ctx
, n
, x
, y
, z
, mask
);
1407 software_depth_test_pixels32(ctx
, n
, x
, y
, z
, mask
);
1415 /**********************************************************************/
1416 /***** Read Depth Buffer *****/
1417 /**********************************************************************/
1421 * Return a span of depth values from the depth buffer as floats in [0,1].
1422 * This is used for both hardware and software depth buffers.
1423 * Input: n - how many pixels
1424 * x,y - location of first pixel
1425 * Output: depth - the array of depth values
1428 _mesa_read_depth_span_float( GLcontext
* ctx
,
1429 GLuint n
, GLint x
, GLint y
, GLfloat depth
[] )
1431 const GLfloat scale
= 1.0F
/ ctx
->Visual
->DepthMaxF
;
1433 if (ctx
->DrawBuffer
->DepthBuffer
) {
1434 /* read from software depth buffer */
1435 if (ctx
->Visual
->DepthBits
<= 16) {
1436 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1438 for (i
= 0; i
< n
; i
++) {
1439 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1443 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1445 for (i
= 0; i
< n
; i
++) {
1446 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1450 else if (ctx
->Driver
.ReadDepthSpan
) {
1451 /* read from hardware depth buffer */
1452 GLdepth d
[MAX_WIDTH
];
1454 assert(n
<= MAX_WIDTH
);
1455 (*ctx
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, d
);
1456 for (i
= 0; i
< n
; i
++) {
1457 depth
[i
] = d
[i
] * scale
;
1461 /* no depth buffer */
1462 BZERO(depth
, n
* sizeof(GLfloat
));
1468 /**********************************************************************/
1469 /***** Allocate and Clear Depth Buffer *****/
1470 /**********************************************************************/
1475 * Allocate a new depth buffer. If there's already a depth buffer allocated
1476 * it will be free()'d. The new depth buffer will be uniniitalized.
1477 * This function is only called through Driver.alloc_depth_buffer.
1480 _mesa_alloc_depth_buffer( GLcontext
*ctx
)
1482 /* deallocate current depth buffer if present */
1483 if (ctx
->DrawBuffer
->UseSoftwareDepthBuffer
) {
1484 GLint bytesPerValue
;
1486 if (ctx
->DrawBuffer
->DepthBuffer
) {
1487 FREE(ctx
->DrawBuffer
->DepthBuffer
);
1488 ctx
->DrawBuffer
->DepthBuffer
= NULL
;
1491 /* allocate new depth buffer, but don't initialize it */
1492 if (ctx
->Visual
->DepthBits
<= 16)
1493 bytesPerValue
= sizeof(GLushort
);
1495 bytesPerValue
= sizeof(GLuint
);
1497 ctx
->DrawBuffer
->DepthBuffer
= MALLOC( ctx
->DrawBuffer
->Width
1498 * ctx
->DrawBuffer
->Height
1501 if (!ctx
->DrawBuffer
->DepthBuffer
) {
1503 ctx
->Depth
.Test
= GL_FALSE
;
1504 ctx
->NewState
|= NEW_RASTER_OPS
;
1505 gl_error( ctx
, GL_OUT_OF_MEMORY
, "Couldn't allocate depth buffer" );
1514 * Clear the depth buffer. If the depth buffer doesn't exist yet we'll
1516 * This function is only called through Driver.clear_depth_buffer.
1519 _mesa_clear_depth_buffer( GLcontext
*ctx
)
1521 if (ctx
->Visual
->DepthBits
== 0
1522 || !ctx
->DrawBuffer
->DepthBuffer
1523 || !ctx
->Depth
.Mask
) {
1524 /* no depth buffer, or writing to it is disabled */
1528 /* The loops in this function have been written so the IRIX 5.3
1529 * C compiler can unroll them. Hopefully other compilers can too!
1532 if (ctx
->Scissor
.Enabled
) {
1533 /* only clear scissor region */
1534 if (ctx
->Visual
->DepthBits
<= 16) {
1535 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->Visual
->DepthMax
);
1536 const GLint rows
= ctx
->DrawBuffer
->Ymax
- ctx
->DrawBuffer
->Ymin
+ 1;
1537 const GLint width
= ctx
->DrawBuffer
->Width
;
1538 GLushort
*dRow
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
1539 + ctx
->DrawBuffer
->Ymin
* width
+ ctx
->DrawBuffer
->Xmin
;
1541 for (i
= 0; i
< rows
; i
++) {
1542 for (j
= 0; j
< width
; j
++) {
1543 dRow
[j
] = clearValue
;
1549 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->Visual
->DepthMax
);
1550 const GLint rows
= ctx
->DrawBuffer
->Ymax
- ctx
->DrawBuffer
->Ymin
+ 1;
1551 const GLint width
= ctx
->DrawBuffer
->Width
;
1552 GLuint
*dRow
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
1553 + ctx
->DrawBuffer
->Ymin
* width
+ ctx
->DrawBuffer
->Xmin
;
1555 for (i
= 0; i
< rows
; i
++) {
1556 for (j
= 0; j
< width
; j
++) {
1557 dRow
[j
] = clearValue
;
1564 /* clear whole buffer */
1565 if (ctx
->Visual
->DepthBits
<= 16) {
1566 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->Visual
->DepthMax
);
1567 if ((clearValue
& 0xff) == (clearValue
>> 8)) {
1568 if (clearValue
== 0) {
1569 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1570 2*ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
);
1573 /* lower and upper bytes of clear_value are same, use MEMSET */
1574 MEMSET( ctx
->DrawBuffer
->DepthBuffer
, clearValue
& 0xff,
1575 2 * ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
);
1579 GLushort
*d
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
;
1580 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1582 d
[0] = clearValue
; d
[1] = clearValue
;
1583 d
[2] = clearValue
; d
[3] = clearValue
;
1584 d
[4] = clearValue
; d
[5] = clearValue
;
1585 d
[6] = clearValue
; d
[7] = clearValue
;
1586 d
[8] = clearValue
; d
[9] = clearValue
;
1587 d
[10] = clearValue
; d
[11] = clearValue
;
1588 d
[12] = clearValue
; d
[13] = clearValue
;
1589 d
[14] = clearValue
; d
[15] = clearValue
;
1600 /* >16 bit depth buffer */
1601 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->Visual
->DepthMax
);
1602 if (clearValue
== 0) {
1603 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1604 ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
*sizeof(GLuint
));
1607 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1608 GLuint
*d
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
;
1610 d
[0] = clearValue
; d
[1] = clearValue
;
1611 d
[2] = clearValue
; d
[3] = clearValue
;
1612 d
[4] = clearValue
; d
[5] = clearValue
;
1613 d
[6] = clearValue
; d
[7] = clearValue
;
1614 d
[8] = clearValue
; d
[9] = clearValue
;
1615 d
[10] = clearValue
; d
[11] = clearValue
;
1616 d
[12] = clearValue
; d
[13] = clearValue
;
1617 d
[14] = clearValue
; d
[15] = clearValue
;