1 /* $Id: depth.c,v 1.21 2000/10/30 13:32:00 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.
43 /**********************************************************************/
44 /***** API Functions *****/
45 /**********************************************************************/
50 _mesa_ClearDepth( GLclampd depth
)
52 GET_CURRENT_CONTEXT(ctx
);
53 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glClearDepth");
54 ctx
->Depth
.Clear
= (GLfloat
) CLAMP( depth
, 0.0, 1.0 );
55 if (ctx
->Driver
.ClearDepth
)
56 (*ctx
->Driver
.ClearDepth
)( ctx
, ctx
->Depth
.Clear
);
57 ctx
->NewState
|= _NEW_DEPTH
;
63 _mesa_DepthFunc( GLenum func
)
65 GET_CURRENT_CONTEXT(ctx
);
66 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDepthFunc");
68 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
69 fprintf(stderr
, "glDepthFunc %s\n", gl_lookup_enum_by_nr(func
));
72 case GL_LESS
: /* (default) pass if incoming z < stored z */
79 if (ctx
->Depth
.Func
!= func
) {
80 ctx
->Depth
.Func
= func
;
81 ctx
->NewState
|= _NEW_DEPTH
;
82 ctx
->TriangleCaps
&= ~DD_Z_NEVER
;
83 if (ctx
->Driver
.DepthFunc
) {
84 (*ctx
->Driver
.DepthFunc
)( ctx
, func
);
89 if (ctx
->Depth
.Func
!= func
) {
90 ctx
->Depth
.Func
= func
;
91 ctx
->NewState
|= _NEW_DEPTH
;
92 ctx
->TriangleCaps
|= DD_Z_NEVER
;
93 if (ctx
->Driver
.DepthFunc
) {
94 (*ctx
->Driver
.DepthFunc
)( ctx
, func
);
99 gl_error( ctx
, GL_INVALID_ENUM
, "glDepth.Func" );
106 _mesa_DepthMask( GLboolean flag
)
108 GET_CURRENT_CONTEXT(ctx
);
109 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDepthMask");
111 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
112 fprintf(stderr
, "glDepthMask %d\n", flag
);
115 * GL_TRUE indicates depth buffer writing is enabled (default)
116 * GL_FALSE indicates depth buffer writing is disabled
118 if (ctx
->Depth
.Mask
!= flag
) {
119 ctx
->Depth
.Mask
= flag
;
120 ctx
->NewState
|= _NEW_DEPTH
;
121 if (ctx
->Driver
.DepthMask
) {
122 (*ctx
->Driver
.DepthMask
)( ctx
, flag
);
129 /**********************************************************************/
131 /**********************************************************************/
134 * Return address of depth buffer value for given window coord.
137 _mesa_zbuffer_address(GLcontext
*ctx
, GLint x
, GLint y
)
139 if (ctx
->Visual
.DepthBits
<= 16)
140 return (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
+ ctx
->DrawBuffer
->Width
* y
+ x
;
142 return (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
+ ctx
->DrawBuffer
->Width
* y
+ x
;
146 #define Z_ADDRESS16( CTX, X, Y ) \
147 ( ((GLushort *) (CTX)->DrawBuffer->DepthBuffer) \
148 + (CTX)->DrawBuffer->Width * (Y) + (X) )
150 #define Z_ADDRESS32( CTX, X, Y ) \
151 ( ((GLuint *) (CTX)->DrawBuffer->DepthBuffer) \
152 + (CTX)->DrawBuffer->Width * (Y) + (X) )
156 /**********************************************************************/
157 /***** Depth Testing Functions *****/
158 /**********************************************************************/
162 * Do depth test for an array of fragments. This is used both for
163 * software and hardware Z buffers.
164 * Input: zbuffer - array of z values in the zbuffer
165 * z - array of fragment z values
166 * Return: number of fragments which pass the test.
169 depth_test_span16( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
170 GLushort zbuffer
[], const GLdepth z
[], GLubyte mask
[] )
174 /* switch cases ordered from most frequent to less frequent */
175 switch (ctx
->Depth
.Func
) {
177 if (ctx
->Depth
.Mask
) {
178 /* Update Z buffer */
180 for (i
=0; i
<n
; i
++) {
182 if (z
[i
] < zbuffer
[i
]) {
195 /* Don't update Z buffer */
197 for (i
=0; i
<n
; i
++) {
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 */
280 if (z
[i
] > zbuffer
[i
]) {
291 /* Don't update Z buffer */
295 if (z
[i
] > zbuffer
[i
]) {
307 if (ctx
->Depth
.Mask
) {
308 /* Update Z buffer */
312 if (z
[i
] != zbuffer
[i
]) {
323 /* Don't update Z buffer */
327 if (z
[i
] != zbuffer
[i
]) {
339 if (ctx
->Depth
.Mask
) {
340 /* Update Z buffer */
344 if (z
[i
] == zbuffer
[i
]) {
355 /* Don't update Z buffer */
359 if (z
[i
] == zbuffer
[i
]) {
371 if (ctx
->Depth
.Mask
) {
372 /* Update Z buffer */
382 /* Don't update Z buffer or mask */
387 BZERO(mask
, n
* sizeof(GLubyte
));
390 gl_problem(ctx
, "Bad depth func in depth_test_span16");
398 depth_test_span32( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
399 GLuint zbuffer
[], const GLdepth z
[], GLubyte mask
[] )
403 /* switch cases ordered from most frequent to less frequent */
404 switch (ctx
->Depth
.Func
) {
406 if (ctx
->Depth
.Mask
) {
407 /* Update Z buffer */
409 for (i
=0; i
<n
; i
++) {
411 if (z
[i
] < zbuffer
[i
]) {
424 /* Don't update Z buffer */
426 for (i
=0; i
<n
; i
++) {
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 */
509 if (z
[i
] > zbuffer
[i
]) {
520 /* Don't update Z buffer */
524 if (z
[i
] > zbuffer
[i
]) {
536 if (ctx
->Depth
.Mask
) {
537 /* Update Z buffer */
541 if (z
[i
] != zbuffer
[i
]) {
552 /* Don't update Z buffer */
556 if (z
[i
] != zbuffer
[i
]) {
568 if (ctx
->Depth
.Mask
) {
569 /* Update Z buffer */
573 if (z
[i
] == zbuffer
[i
]) {
584 /* Don't update Z buffer */
588 if (z
[i
] == zbuffer
[i
]) {
600 if (ctx
->Depth
.Mask
) {
601 /* Update Z buffer */
611 /* Don't update Z buffer or mask */
616 BZERO(mask
, n
* sizeof(GLubyte
));
619 gl_problem(ctx
, "Bad depth func in depth_test_span32");
628 * Apply depth test to span of fragments. Hardware or software z buffer.
631 _mesa_depth_test_span( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
632 const GLdepth z
[], GLubyte mask
[] )
634 if (ctx
->Driver
.ReadDepthSpan
) {
635 /* hardware-based depth buffer */
636 GLdepth zbuffer
[MAX_WIDTH
];
638 (*ctx
->Driver
.ReadDepthSpan
)(ctx
, n
, x
, y
, zbuffer
);
639 passed
= depth_test_span32(ctx
, n
, x
, y
, zbuffer
, z
, mask
);
640 assert(ctx
->Driver
.WriteDepthSpan
);
641 (*ctx
->Driver
.WriteDepthSpan
)(ctx
, n
, x
, y
, zbuffer
, mask
);
645 /* software depth buffer */
646 if (ctx
->Visual
.DepthBits
<= 16) {
647 GLushort
*zptr
= (GLushort
*) Z_ADDRESS16(ctx
, x
, y
);
648 GLuint passed
= depth_test_span16(ctx
, n
, x
, y
, zptr
, z
, mask
);
652 GLuint
*zptr
= (GLuint
*) Z_ADDRESS32(ctx
, x
, y
);
653 GLuint passed
= depth_test_span32(ctx
, n
, x
, y
, zptr
, z
, mask
);
663 * Do depth testing for an array of fragments using software Z buffer.
666 software_depth_test_pixels16( GLcontext
*ctx
, GLuint n
,
667 const GLint x
[], const GLint y
[],
668 const GLdepth z
[], GLubyte mask
[] )
670 /* switch cases ordered from most frequent to less frequent */
671 switch (ctx
->Depth
.Func
) {
673 if (ctx
->Depth
.Mask
) {
674 /* Update Z buffer */
676 for (i
=0; i
<n
; i
++) {
678 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
691 /* Don't update Z buffer */
693 for (i
=0; i
<n
; i
++) {
695 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
708 if (ctx
->Depth
.Mask
) {
709 /* Update Z buffer */
711 for (i
=0; i
<n
; i
++) {
713 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
726 /* Don't update Z buffer */
728 for (i
=0; i
<n
; i
++) {
730 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
743 if (ctx
->Depth
.Mask
) {
744 /* Update Z buffer */
746 for (i
=0; i
<n
; i
++) {
748 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
761 /* Don't update Z buffer */
763 for (i
=0; i
<n
; i
++) {
765 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
778 if (ctx
->Depth
.Mask
) {
779 /* Update Z buffer */
781 for (i
=0; i
<n
; i
++) {
783 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
796 /* Don't update Z buffer */
798 for (i
=0; i
<n
; i
++) {
800 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
813 if (ctx
->Depth
.Mask
) {
814 /* Update Z buffer */
816 for (i
=0; i
<n
; i
++) {
818 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
831 /* Don't update Z buffer */
833 for (i
=0; i
<n
; i
++) {
835 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
848 if (ctx
->Depth
.Mask
) {
849 /* Update Z buffer */
851 for (i
=0; i
<n
; i
++) {
853 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
866 /* Don't update Z buffer */
868 for (i
=0; i
<n
; i
++) {
870 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
883 if (ctx
->Depth
.Mask
) {
884 /* Update Z buffer */
886 for (i
=0; i
<n
; i
++) {
888 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
894 /* Don't update Z buffer or mask */
898 /* depth test never passes */
899 BZERO(mask
, n
* sizeof(GLubyte
));
902 gl_problem(ctx
, "Bad depth func in software_depth_test_pixels");
909 * Do depth testing for an array of fragments using software Z buffer.
912 software_depth_test_pixels32( GLcontext
*ctx
, GLuint n
,
913 const GLint x
[], const GLint y
[],
914 const GLdepth z
[], GLubyte mask
[] )
916 /* switch cases ordered from most frequent to less frequent */
917 switch (ctx
->Depth
.Func
) {
919 if (ctx
->Depth
.Mask
) {
920 /* Update Z buffer */
922 for (i
=0; i
<n
; i
++) {
924 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
937 /* Don't update Z buffer */
939 for (i
=0; i
<n
; i
++) {
941 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
954 if (ctx
->Depth
.Mask
) {
955 /* Update Z buffer */
957 for (i
=0; i
<n
; i
++) {
959 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
972 /* Don't update Z buffer */
974 for (i
=0; i
<n
; i
++) {
976 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
989 if (ctx
->Depth
.Mask
) {
990 /* Update Z buffer */
992 for (i
=0; i
<n
; i
++) {
994 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1007 /* Don't update Z buffer */
1009 for (i
=0; i
<n
; i
++) {
1011 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1012 if (z
[i
] >= *zptr
) {
1024 if (ctx
->Depth
.Mask
) {
1025 /* Update Z buffer */
1027 for (i
=0; i
<n
; i
++) {
1029 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1042 /* Don't update Z buffer */
1044 for (i
=0; i
<n
; i
++) {
1046 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1059 if (ctx
->Depth
.Mask
) {
1060 /* Update Z buffer */
1062 for (i
=0; i
<n
; i
++) {
1064 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1065 if (z
[i
] != *zptr
) {
1077 /* Don't update Z buffer */
1079 for (i
=0; i
<n
; i
++) {
1081 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1082 if (z
[i
] != *zptr
) {
1094 if (ctx
->Depth
.Mask
) {
1095 /* Update Z buffer */
1097 for (i
=0; i
<n
; i
++) {
1099 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1100 if (z
[i
] == *zptr
) {
1112 /* Don't update Z buffer */
1114 for (i
=0; i
<n
; i
++) {
1116 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1117 if (z
[i
] == *zptr
) {
1129 if (ctx
->Depth
.Mask
) {
1130 /* Update Z buffer */
1132 for (i
=0; i
<n
; i
++) {
1134 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1140 /* Don't update Z buffer or mask */
1144 /* depth test never passes */
1145 BZERO(mask
, n
* sizeof(GLubyte
));
1148 gl_problem(ctx
, "Bad depth func in software_depth_test_pixels");
1155 * Do depth testing for an array of pixels using hardware Z buffer.
1156 * Input/output: zbuffer - array of depth values from Z buffer
1157 * Input: z - array of fragment z values.
1160 hardware_depth_test_pixels( GLcontext
*ctx
, GLuint n
, GLdepth zbuffer
[],
1161 const GLdepth z
[], GLubyte mask
[] )
1163 /* switch cases ordered from most frequent to less frequent */
1164 switch (ctx
->Depth
.Func
) {
1166 if (ctx
->Depth
.Mask
) {
1167 /* Update Z buffer */
1169 for (i
=0; i
<n
; i
++) {
1171 if (z
[i
] < zbuffer
[i
]) {
1183 /* Don't update Z buffer */
1185 for (i
=0; i
<n
; i
++) {
1187 if (z
[i
] < zbuffer
[i
]) {
1199 if (ctx
->Depth
.Mask
) {
1200 /* Update Z buffer */
1202 for (i
=0; i
<n
; i
++) {
1204 if (z
[i
] <= zbuffer
[i
]) {
1216 /* Don't update Z buffer */
1218 for (i
=0; i
<n
; i
++) {
1220 if (z
[i
] <= zbuffer
[i
]) {
1232 if (ctx
->Depth
.Mask
) {
1233 /* Update Z buffer */
1235 for (i
=0; i
<n
; i
++) {
1237 if (z
[i
] >= zbuffer
[i
]) {
1249 /* Don't update Z buffer */
1251 for (i
=0; i
<n
; i
++) {
1253 if (z
[i
] >= zbuffer
[i
]) {
1265 if (ctx
->Depth
.Mask
) {
1266 /* Update Z buffer */
1268 for (i
=0; i
<n
; i
++) {
1270 if (z
[i
] > zbuffer
[i
]) {
1282 /* Don't update Z buffer */
1284 for (i
=0; i
<n
; i
++) {
1286 if (z
[i
] > zbuffer
[i
]) {
1298 if (ctx
->Depth
.Mask
) {
1299 /* Update Z buffer */
1301 for (i
=0; i
<n
; i
++) {
1303 if (z
[i
] != zbuffer
[i
]) {
1315 /* Don't update Z buffer */
1317 for (i
=0; i
<n
; i
++) {
1319 if (z
[i
] != zbuffer
[i
]) {
1331 if (ctx
->Depth
.Mask
) {
1332 /* Update Z buffer */
1334 for (i
=0; i
<n
; i
++) {
1336 if (z
[i
] == zbuffer
[i
]) {
1348 /* Don't update Z buffer */
1350 for (i
=0; i
<n
; i
++) {
1352 if (z
[i
] == zbuffer
[i
]) {
1364 if (ctx
->Depth
.Mask
) {
1365 /* Update Z buffer */
1367 for (i
=0; i
<n
; i
++) {
1374 /* Don't update Z buffer or mask */
1378 /* depth test never passes */
1379 BZERO(mask
, n
* sizeof(GLubyte
));
1382 gl_problem(ctx
, "Bad depth func in hardware_depth_test_pixels");
1389 _mesa_depth_test_pixels( GLcontext
*ctx
,
1390 GLuint n
, const GLint x
[], const GLint y
[],
1391 const GLdepth z
[], GLubyte mask
[] )
1393 if (ctx
->Driver
.ReadDepthPixels
) {
1394 /* read depth values from hardware Z buffer */
1395 GLdepth zbuffer
[PB_SIZE
];
1396 (*ctx
->Driver
.ReadDepthPixels
)(ctx
, n
, x
, y
, zbuffer
);
1398 hardware_depth_test_pixels( ctx
, n
, zbuffer
, z
, mask
);
1400 /* update hardware Z buffer with new values */
1401 assert(ctx
->Driver
.WriteDepthPixels
);
1402 (*ctx
->Driver
.WriteDepthPixels
)(ctx
, n
, x
, y
, zbuffer
, mask
);
1405 /* software depth testing */
1406 if (ctx
->Visual
.DepthBits
<= 16)
1407 software_depth_test_pixels16(ctx
, n
, x
, y
, z
, mask
);
1409 software_depth_test_pixels32(ctx
, n
, x
, y
, z
, mask
);
1417 /**********************************************************************/
1418 /***** Read Depth Buffer *****/
1419 /**********************************************************************/
1423 * Read a span of depth values from the depth buffer.
1424 * This function does clipping before calling the device driver function.
1427 _mesa_read_depth_span( GLcontext
*ctx
,
1428 GLint n
, GLint x
, GLint y
, GLdepth depth
[] )
1430 if (y
< 0 || y
>= ctx
->DrawBuffer
->Height
||
1431 x
+ (GLint
) n
<= 0 || x
>= ctx
->DrawBuffer
->Width
) {
1432 /* span is completely outside framebuffer */
1434 for (i
= 0; i
< n
; i
++)
1442 for (i
= 0; i
< dx
; i
++)
1448 if (x
+ n
> ctx
->DrawBuffer
->Width
) {
1449 GLint dx
= x
+ n
- ctx
->DrawBuffer
->Width
;
1451 for (i
= 0; i
< dx
; i
++)
1452 depth
[n
- i
- 1] = 0;
1459 if (ctx
->DrawBuffer
->DepthBuffer
) {
1460 /* read from software depth buffer */
1461 if (ctx
->Visual
.DepthBits
<= 16) {
1462 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1464 for (i
= 0; i
< n
; i
++) {
1469 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1471 for (i
= 0; i
< n
; i
++) {
1476 else if (ctx
->Driver
.ReadDepthSpan
) {
1477 /* read from hardware depth buffer */
1478 (*ctx
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, depth
);
1481 /* no depth buffer */
1482 BZERO(depth
, n
* sizeof(GLfloat
));
1491 * Return a span of depth values from the depth buffer as floats in [0,1].
1492 * This is used for both hardware and software depth buffers.
1493 * Input: n - how many pixels
1494 * x,y - location of first pixel
1495 * Output: depth - the array of depth values
1498 _mesa_read_depth_span_float( GLcontext
*ctx
,
1499 GLint n
, GLint x
, GLint y
, GLfloat depth
[] )
1501 const GLfloat scale
= 1.0F
/ ctx
->Visual
.DepthMaxF
;
1503 if (y
< 0 || y
>= ctx
->DrawBuffer
->Height
||
1504 x
+ (GLint
) n
<= 0 || x
>= ctx
->DrawBuffer
->Width
) {
1505 /* span is completely outside framebuffer */
1507 for (i
= 0; i
< n
; i
++)
1515 for (i
= 0; i
< dx
; i
++)
1520 if (x
+ n
> ctx
->DrawBuffer
->Width
) {
1521 GLint dx
= x
+ n
- ctx
->DrawBuffer
->Width
;
1523 for (i
= 0; i
< dx
; i
++)
1524 depth
[n
- i
- 1] = 0.0F
;
1531 if (ctx
->DrawBuffer
->DepthBuffer
) {
1532 /* read from software depth buffer */
1533 if (ctx
->Visual
.DepthBits
<= 16) {
1534 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1536 for (i
= 0; i
< n
; i
++) {
1537 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1541 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1543 for (i
= 0; i
< n
; i
++) {
1544 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1548 else if (ctx
->Driver
.ReadDepthSpan
) {
1549 /* read from hardware depth buffer */
1550 GLdepth d
[MAX_WIDTH
];
1552 assert(n
<= MAX_WIDTH
);
1553 (*ctx
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, d
);
1554 for (i
= 0; i
< n
; i
++) {
1555 depth
[i
] = d
[i
] * scale
;
1559 /* no depth buffer */
1560 BZERO(depth
, n
* sizeof(GLfloat
));
1566 /**********************************************************************/
1567 /***** Allocate and Clear Depth Buffer *****/
1568 /**********************************************************************/
1573 * Allocate a new depth buffer. If there's already a depth buffer allocated
1574 * it will be free()'d. The new depth buffer will be uniniitalized.
1575 * This function is only called through Driver.alloc_depth_buffer.
1578 _mesa_alloc_depth_buffer( GLcontext
*ctx
)
1580 /* deallocate current depth buffer if present */
1581 if (ctx
->DrawBuffer
->UseSoftwareDepthBuffer
) {
1582 GLint bytesPerValue
;
1584 if (ctx
->DrawBuffer
->DepthBuffer
) {
1585 FREE(ctx
->DrawBuffer
->DepthBuffer
);
1586 ctx
->DrawBuffer
->DepthBuffer
= NULL
;
1589 /* allocate new depth buffer, but don't initialize it */
1590 if (ctx
->Visual
.DepthBits
<= 16)
1591 bytesPerValue
= sizeof(GLushort
);
1593 bytesPerValue
= sizeof(GLuint
);
1595 ctx
->DrawBuffer
->DepthBuffer
= MALLOC( ctx
->DrawBuffer
->Width
1596 * ctx
->DrawBuffer
->Height
1599 if (!ctx
->DrawBuffer
->DepthBuffer
) {
1601 ctx
->Depth
.Test
= GL_FALSE
;
1602 ctx
->NewState
|= _NEW_DEPTH
;
1603 gl_error( ctx
, GL_OUT_OF_MEMORY
, "Couldn't allocate depth buffer" );
1612 * Clear the depth buffer. If the depth buffer doesn't exist yet we'll
1614 * This function is only called through Driver.clear_depth_buffer.
1617 _mesa_clear_depth_buffer( GLcontext
*ctx
)
1619 if (ctx
->Visual
.DepthBits
== 0
1620 || !ctx
->DrawBuffer
->DepthBuffer
1621 || !ctx
->Depth
.Mask
) {
1622 /* no depth buffer, or writing to it is disabled */
1626 /* The loops in this function have been written so the IRIX 5.3
1627 * C compiler can unroll them. Hopefully other compilers can too!
1630 if (ctx
->Scissor
.Enabled
) {
1631 /* only clear scissor region */
1632 if (ctx
->Visual
.DepthBits
<= 16) {
1633 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->Visual
.DepthMax
);
1634 const GLint rows
= ctx
->DrawBuffer
->Ymax
- ctx
->DrawBuffer
->Ymin
;
1635 const GLint width
= ctx
->DrawBuffer
->Width
;
1636 GLushort
*dRow
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
1637 + ctx
->DrawBuffer
->Ymin
* width
+ ctx
->DrawBuffer
->Xmin
;
1639 for (i
= 0; i
< rows
; i
++) {
1640 for (j
= 0; j
< width
; j
++) {
1641 dRow
[j
] = clearValue
;
1647 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->Visual
.DepthMax
);
1648 const GLint rows
= ctx
->DrawBuffer
->Ymax
- ctx
->DrawBuffer
->Ymin
;
1649 const GLint width
= ctx
->DrawBuffer
->Width
;
1650 GLuint
*dRow
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
1651 + ctx
->DrawBuffer
->Ymin
* width
+ ctx
->DrawBuffer
->Xmin
;
1653 for (i
= 0; i
< rows
; i
++) {
1654 for (j
= 0; j
< width
; j
++) {
1655 dRow
[j
] = clearValue
;
1662 /* clear whole buffer */
1663 if (ctx
->Visual
.DepthBits
<= 16) {
1664 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->Visual
.DepthMax
);
1665 if ((clearValue
& 0xff) == (clearValue
>> 8)) {
1666 if (clearValue
== 0) {
1667 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1668 2*ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
);
1671 /* lower and upper bytes of clear_value are same, use MEMSET */
1672 MEMSET( ctx
->DrawBuffer
->DepthBuffer
, clearValue
& 0xff,
1673 2 * ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
);
1677 GLushort
*d
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
;
1678 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1680 d
[0] = clearValue
; d
[1] = clearValue
;
1681 d
[2] = clearValue
; d
[3] = clearValue
;
1682 d
[4] = clearValue
; d
[5] = clearValue
;
1683 d
[6] = clearValue
; d
[7] = clearValue
;
1684 d
[8] = clearValue
; d
[9] = clearValue
;
1685 d
[10] = clearValue
; d
[11] = clearValue
;
1686 d
[12] = clearValue
; d
[13] = clearValue
;
1687 d
[14] = clearValue
; d
[15] = clearValue
;
1698 /* >16 bit depth buffer */
1699 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->Visual
.DepthMax
);
1700 if (clearValue
== 0) {
1701 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1702 ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
*sizeof(GLuint
));
1705 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1706 GLuint
*d
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
;
1708 d
[0] = clearValue
; d
[1] = clearValue
;
1709 d
[2] = clearValue
; d
[3] = clearValue
;
1710 d
[4] = clearValue
; d
[5] = clearValue
;
1711 d
[6] = clearValue
; d
[7] = clearValue
;
1712 d
[8] = clearValue
; d
[9] = clearValue
;
1713 d
[10] = clearValue
; d
[11] = clearValue
;
1714 d
[12] = clearValue
; d
[13] = clearValue
;
1715 d
[14] = clearValue
; d
[15] = clearValue
;