1 /* $Id: depth.c,v 1.20 2000/10/29 18:23:16 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.
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
);
62 _mesa_DepthFunc( GLenum func
)
64 GET_CURRENT_CONTEXT(ctx
);
65 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDepthFunc");
67 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
68 fprintf(stderr
, "glDepthFunc %s\n", gl_lookup_enum_by_nr(func
));
71 case GL_LESS
: /* (default) pass if incoming z < stored z */
78 if (ctx
->Depth
.Func
!= func
) {
79 ctx
->Depth
.Func
= func
;
80 ctx
->NewState
|= NEW_RASTER_OPS
;
81 ctx
->TriangleCaps
&= ~DD_Z_NEVER
;
82 if (ctx
->Driver
.DepthFunc
) {
83 (*ctx
->Driver
.DepthFunc
)( ctx
, func
);
88 if (ctx
->Depth
.Func
!= func
) {
89 ctx
->Depth
.Func
= func
;
90 ctx
->NewState
|= NEW_RASTER_OPS
;
91 ctx
->TriangleCaps
|= DD_Z_NEVER
;
92 if (ctx
->Driver
.DepthFunc
) {
93 (*ctx
->Driver
.DepthFunc
)( ctx
, func
);
98 gl_error( ctx
, GL_INVALID_ENUM
, "glDepth.Func" );
105 _mesa_DepthMask( GLboolean flag
)
107 GET_CURRENT_CONTEXT(ctx
);
108 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx
, "glDepthMask");
110 if (MESA_VERBOSE
& (VERBOSE_API
|VERBOSE_TEXTURE
))
111 fprintf(stderr
, "glDepthMask %d\n", flag
);
114 * GL_TRUE indicates depth buffer writing is enabled (default)
115 * GL_FALSE indicates depth buffer writing is disabled
117 if (ctx
->Depth
.Mask
!= flag
) {
118 ctx
->Depth
.Mask
= flag
;
119 ctx
->NewState
|= NEW_RASTER_OPS
;
120 if (ctx
->Driver
.DepthMask
) {
121 (*ctx
->Driver
.DepthMask
)( ctx
, flag
);
128 /**********************************************************************/
130 /**********************************************************************/
133 * Return address of depth buffer value for given window coord.
136 _mesa_zbuffer_address(GLcontext
*ctx
, GLint x
, GLint y
)
138 if (ctx
->Visual
.DepthBits
<= 16)
139 return (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
+ ctx
->DrawBuffer
->Width
* y
+ x
;
141 return (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
+ ctx
->DrawBuffer
->Width
* y
+ x
;
145 #define Z_ADDRESS16( CTX, X, Y ) \
146 ( ((GLushort *) (CTX)->DrawBuffer->DepthBuffer) \
147 + (CTX)->DrawBuffer->Width * (Y) + (X) )
149 #define Z_ADDRESS32( CTX, X, Y ) \
150 ( ((GLuint *) (CTX)->DrawBuffer->DepthBuffer) \
151 + (CTX)->DrawBuffer->Width * (Y) + (X) )
155 /**********************************************************************/
156 /***** Depth Testing Functions *****/
157 /**********************************************************************/
161 * Do depth test for an array of fragments. This is used both for
162 * software and hardware Z buffers.
163 * Input: zbuffer - array of z values in the zbuffer
164 * z - array of fragment z values
165 * Return: number of fragments which pass the test.
168 depth_test_span16( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
169 GLushort zbuffer
[], const GLdepth z
[], GLubyte mask
[] )
173 /* switch cases ordered from most frequent to less frequent */
174 switch (ctx
->Depth
.Func
) {
176 if (ctx
->Depth
.Mask
) {
177 /* Update Z buffer */
179 for (i
=0; i
<n
; i
++) {
181 if (z
[i
] < zbuffer
[i
]) {
194 /* Don't update Z buffer */
196 for (i
=0; i
<n
; i
++) {
198 if (z
[i
] < zbuffer
[i
]) {
210 if (ctx
->Depth
.Mask
) {
211 /* Update Z buffer */
215 if (z
[i
] <= zbuffer
[i
]) {
226 /* Don't update Z buffer */
230 if (z
[i
] <= zbuffer
[i
]) {
242 if (ctx
->Depth
.Mask
) {
243 /* Update Z buffer */
247 if (z
[i
] >= zbuffer
[i
]) {
258 /* Don't update Z buffer */
262 if (z
[i
] >= zbuffer
[i
]) {
274 if (ctx
->Depth
.Mask
) {
275 /* Update Z buffer */
279 if (z
[i
] > zbuffer
[i
]) {
290 /* Don't update Z buffer */
294 if (z
[i
] > zbuffer
[i
]) {
306 if (ctx
->Depth
.Mask
) {
307 /* Update Z buffer */
311 if (z
[i
] != zbuffer
[i
]) {
322 /* Don't update Z buffer */
326 if (z
[i
] != zbuffer
[i
]) {
338 if (ctx
->Depth
.Mask
) {
339 /* Update Z buffer */
343 if (z
[i
] == zbuffer
[i
]) {
354 /* Don't update Z buffer */
358 if (z
[i
] == zbuffer
[i
]) {
370 if (ctx
->Depth
.Mask
) {
371 /* Update Z buffer */
381 /* Don't update Z buffer or mask */
386 BZERO(mask
, n
* sizeof(GLubyte
));
389 gl_problem(ctx
, "Bad depth func in depth_test_span16");
397 depth_test_span32( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
398 GLuint zbuffer
[], const GLdepth z
[], GLubyte mask
[] )
402 /* switch cases ordered from most frequent to less frequent */
403 switch (ctx
->Depth
.Func
) {
405 if (ctx
->Depth
.Mask
) {
406 /* Update Z buffer */
408 for (i
=0; i
<n
; i
++) {
410 if (z
[i
] < zbuffer
[i
]) {
423 /* Don't update Z buffer */
425 for (i
=0; i
<n
; i
++) {
427 if (z
[i
] < zbuffer
[i
]) {
439 if (ctx
->Depth
.Mask
) {
440 /* Update Z buffer */
444 if (z
[i
] <= zbuffer
[i
]) {
455 /* Don't update Z buffer */
459 if (z
[i
] <= zbuffer
[i
]) {
471 if (ctx
->Depth
.Mask
) {
472 /* Update Z buffer */
476 if (z
[i
] >= zbuffer
[i
]) {
487 /* Don't update Z buffer */
491 if (z
[i
] >= zbuffer
[i
]) {
503 if (ctx
->Depth
.Mask
) {
504 /* Update Z buffer */
508 if (z
[i
] > zbuffer
[i
]) {
519 /* Don't update Z buffer */
523 if (z
[i
] > zbuffer
[i
]) {
535 if (ctx
->Depth
.Mask
) {
536 /* Update Z buffer */
540 if (z
[i
] != zbuffer
[i
]) {
551 /* Don't update Z buffer */
555 if (z
[i
] != zbuffer
[i
]) {
567 if (ctx
->Depth
.Mask
) {
568 /* Update Z buffer */
572 if (z
[i
] == zbuffer
[i
]) {
583 /* Don't update Z buffer */
587 if (z
[i
] == zbuffer
[i
]) {
599 if (ctx
->Depth
.Mask
) {
600 /* Update Z buffer */
610 /* Don't update Z buffer or mask */
615 BZERO(mask
, n
* sizeof(GLubyte
));
618 gl_problem(ctx
, "Bad depth func in depth_test_span32");
627 * Apply depth test to span of fragments. Hardware or software z buffer.
630 _mesa_depth_test_span( GLcontext
*ctx
, GLuint n
, GLint x
, GLint y
,
631 const GLdepth z
[], GLubyte mask
[] )
633 if (ctx
->Driver
.ReadDepthSpan
) {
634 /* hardware-based depth buffer */
635 GLdepth zbuffer
[MAX_WIDTH
];
637 (*ctx
->Driver
.ReadDepthSpan
)(ctx
, n
, x
, y
, zbuffer
);
638 passed
= depth_test_span32(ctx
, n
, x
, y
, zbuffer
, z
, mask
);
639 assert(ctx
->Driver
.WriteDepthSpan
);
640 (*ctx
->Driver
.WriteDepthSpan
)(ctx
, n
, x
, y
, zbuffer
, mask
);
644 /* software depth buffer */
645 if (ctx
->Visual
.DepthBits
<= 16) {
646 GLushort
*zptr
= (GLushort
*) Z_ADDRESS16(ctx
, x
, y
);
647 GLuint passed
= depth_test_span16(ctx
, n
, x
, y
, zptr
, z
, mask
);
651 GLuint
*zptr
= (GLuint
*) Z_ADDRESS32(ctx
, x
, y
);
652 GLuint passed
= depth_test_span32(ctx
, n
, x
, y
, zptr
, z
, mask
);
662 * Do depth testing for an array of fragments using software Z buffer.
665 software_depth_test_pixels16( GLcontext
*ctx
, GLuint n
,
666 const GLint x
[], const GLint y
[],
667 const GLdepth z
[], GLubyte mask
[] )
669 /* switch cases ordered from most frequent to less frequent */
670 switch (ctx
->Depth
.Func
) {
672 if (ctx
->Depth
.Mask
) {
673 /* Update Z buffer */
675 for (i
=0; i
<n
; i
++) {
677 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
690 /* Don't update Z buffer */
692 for (i
=0; i
<n
; i
++) {
694 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
707 if (ctx
->Depth
.Mask
) {
708 /* Update Z buffer */
710 for (i
=0; i
<n
; i
++) {
712 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
725 /* Don't update Z buffer */
727 for (i
=0; i
<n
; i
++) {
729 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
742 if (ctx
->Depth
.Mask
) {
743 /* Update Z buffer */
745 for (i
=0; i
<n
; i
++) {
747 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
760 /* Don't update Z buffer */
762 for (i
=0; i
<n
; i
++) {
764 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
777 if (ctx
->Depth
.Mask
) {
778 /* Update Z buffer */
780 for (i
=0; i
<n
; i
++) {
782 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
795 /* Don't update Z buffer */
797 for (i
=0; i
<n
; i
++) {
799 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
812 if (ctx
->Depth
.Mask
) {
813 /* Update Z buffer */
815 for (i
=0; i
<n
; i
++) {
817 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
830 /* Don't update Z buffer */
832 for (i
=0; i
<n
; i
++) {
834 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
847 if (ctx
->Depth
.Mask
) {
848 /* Update Z buffer */
850 for (i
=0; i
<n
; i
++) {
852 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
865 /* Don't update Z buffer */
867 for (i
=0; i
<n
; i
++) {
869 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
882 if (ctx
->Depth
.Mask
) {
883 /* Update Z buffer */
885 for (i
=0; i
<n
; i
++) {
887 GLushort
*zptr
= Z_ADDRESS16(ctx
,x
[i
],y
[i
]);
893 /* Don't update Z buffer or mask */
897 /* depth test never passes */
898 BZERO(mask
, n
* sizeof(GLubyte
));
901 gl_problem(ctx
, "Bad depth func in software_depth_test_pixels");
908 * Do depth testing for an array of fragments using software Z buffer.
911 software_depth_test_pixels32( GLcontext
*ctx
, GLuint n
,
912 const GLint x
[], const GLint y
[],
913 const GLdepth z
[], GLubyte mask
[] )
915 /* switch cases ordered from most frequent to less frequent */
916 switch (ctx
->Depth
.Func
) {
918 if (ctx
->Depth
.Mask
) {
919 /* Update Z buffer */
921 for (i
=0; i
<n
; i
++) {
923 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
936 /* Don't update Z buffer */
938 for (i
=0; i
<n
; i
++) {
940 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
953 if (ctx
->Depth
.Mask
) {
954 /* Update Z buffer */
956 for (i
=0; i
<n
; i
++) {
958 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
971 /* Don't update Z buffer */
973 for (i
=0; i
<n
; i
++) {
975 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
988 if (ctx
->Depth
.Mask
) {
989 /* Update Z buffer */
991 for (i
=0; i
<n
; i
++) {
993 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1006 /* Don't update Z buffer */
1008 for (i
=0; i
<n
; i
++) {
1010 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1011 if (z
[i
] >= *zptr
) {
1023 if (ctx
->Depth
.Mask
) {
1024 /* Update Z buffer */
1026 for (i
=0; i
<n
; i
++) {
1028 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1041 /* Don't update Z buffer */
1043 for (i
=0; i
<n
; i
++) {
1045 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1058 if (ctx
->Depth
.Mask
) {
1059 /* Update Z buffer */
1061 for (i
=0; i
<n
; i
++) {
1063 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1064 if (z
[i
] != *zptr
) {
1076 /* Don't update Z buffer */
1078 for (i
=0; i
<n
; i
++) {
1080 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1081 if (z
[i
] != *zptr
) {
1093 if (ctx
->Depth
.Mask
) {
1094 /* Update Z buffer */
1096 for (i
=0; i
<n
; i
++) {
1098 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1099 if (z
[i
] == *zptr
) {
1111 /* Don't update Z buffer */
1113 for (i
=0; i
<n
; i
++) {
1115 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1116 if (z
[i
] == *zptr
) {
1128 if (ctx
->Depth
.Mask
) {
1129 /* Update Z buffer */
1131 for (i
=0; i
<n
; i
++) {
1133 GLuint
*zptr
= Z_ADDRESS32(ctx
,x
[i
],y
[i
]);
1139 /* Don't update Z buffer or mask */
1143 /* depth test never passes */
1144 BZERO(mask
, n
* sizeof(GLubyte
));
1147 gl_problem(ctx
, "Bad depth func in software_depth_test_pixels");
1154 * Do depth testing for an array of pixels using hardware Z buffer.
1155 * Input/output: zbuffer - array of depth values from Z buffer
1156 * Input: z - array of fragment z values.
1159 hardware_depth_test_pixels( GLcontext
*ctx
, GLuint n
, GLdepth zbuffer
[],
1160 const GLdepth z
[], GLubyte mask
[] )
1162 /* switch cases ordered from most frequent to less frequent */
1163 switch (ctx
->Depth
.Func
) {
1165 if (ctx
->Depth
.Mask
) {
1166 /* Update Z buffer */
1168 for (i
=0; i
<n
; i
++) {
1170 if (z
[i
] < zbuffer
[i
]) {
1182 /* Don't update Z buffer */
1184 for (i
=0; i
<n
; i
++) {
1186 if (z
[i
] < zbuffer
[i
]) {
1198 if (ctx
->Depth
.Mask
) {
1199 /* Update Z buffer */
1201 for (i
=0; i
<n
; i
++) {
1203 if (z
[i
] <= zbuffer
[i
]) {
1215 /* Don't update Z buffer */
1217 for (i
=0; i
<n
; i
++) {
1219 if (z
[i
] <= zbuffer
[i
]) {
1231 if (ctx
->Depth
.Mask
) {
1232 /* Update Z buffer */
1234 for (i
=0; i
<n
; i
++) {
1236 if (z
[i
] >= zbuffer
[i
]) {
1248 /* Don't update Z buffer */
1250 for (i
=0; i
<n
; i
++) {
1252 if (z
[i
] >= zbuffer
[i
]) {
1264 if (ctx
->Depth
.Mask
) {
1265 /* Update Z buffer */
1267 for (i
=0; i
<n
; i
++) {
1269 if (z
[i
] > zbuffer
[i
]) {
1281 /* Don't update Z buffer */
1283 for (i
=0; i
<n
; i
++) {
1285 if (z
[i
] > zbuffer
[i
]) {
1297 if (ctx
->Depth
.Mask
) {
1298 /* Update Z buffer */
1300 for (i
=0; i
<n
; i
++) {
1302 if (z
[i
] != zbuffer
[i
]) {
1314 /* Don't update Z buffer */
1316 for (i
=0; i
<n
; i
++) {
1318 if (z
[i
] != zbuffer
[i
]) {
1330 if (ctx
->Depth
.Mask
) {
1331 /* Update Z buffer */
1333 for (i
=0; i
<n
; i
++) {
1335 if (z
[i
] == zbuffer
[i
]) {
1347 /* Don't update Z buffer */
1349 for (i
=0; i
<n
; i
++) {
1351 if (z
[i
] == zbuffer
[i
]) {
1363 if (ctx
->Depth
.Mask
) {
1364 /* Update Z buffer */
1366 for (i
=0; i
<n
; i
++) {
1373 /* Don't update Z buffer or mask */
1377 /* depth test never passes */
1378 BZERO(mask
, n
* sizeof(GLubyte
));
1381 gl_problem(ctx
, "Bad depth func in hardware_depth_test_pixels");
1388 _mesa_depth_test_pixels( GLcontext
*ctx
,
1389 GLuint n
, const GLint x
[], const GLint y
[],
1390 const GLdepth z
[], GLubyte mask
[] )
1392 if (ctx
->Driver
.ReadDepthPixels
) {
1393 /* read depth values from hardware Z buffer */
1394 GLdepth zbuffer
[PB_SIZE
];
1395 (*ctx
->Driver
.ReadDepthPixels
)(ctx
, n
, x
, y
, zbuffer
);
1397 hardware_depth_test_pixels( ctx
, n
, zbuffer
, z
, mask
);
1399 /* update hardware Z buffer with new values */
1400 assert(ctx
->Driver
.WriteDepthPixels
);
1401 (*ctx
->Driver
.WriteDepthPixels
)(ctx
, n
, x
, y
, zbuffer
, mask
);
1404 /* software depth testing */
1405 if (ctx
->Visual
.DepthBits
<= 16)
1406 software_depth_test_pixels16(ctx
, n
, x
, y
, z
, mask
);
1408 software_depth_test_pixels32(ctx
, n
, x
, y
, z
, mask
);
1416 /**********************************************************************/
1417 /***** Read Depth Buffer *****/
1418 /**********************************************************************/
1422 * Read a span of depth values from the depth buffer.
1423 * This function does clipping before calling the device driver function.
1426 _mesa_read_depth_span( GLcontext
*ctx
,
1427 GLint n
, GLint x
, GLint y
, GLdepth depth
[] )
1429 if (y
< 0 || y
>= ctx
->DrawBuffer
->Height
||
1430 x
+ (GLint
) n
<= 0 || x
>= ctx
->DrawBuffer
->Width
) {
1431 /* span is completely outside framebuffer */
1433 for (i
= 0; i
< n
; i
++)
1441 for (i
= 0; i
< dx
; i
++)
1447 if (x
+ n
> ctx
->DrawBuffer
->Width
) {
1448 GLint dx
= x
+ n
- ctx
->DrawBuffer
->Width
;
1450 for (i
= 0; i
< dx
; i
++)
1451 depth
[n
- i
- 1] = 0;
1458 if (ctx
->DrawBuffer
->DepthBuffer
) {
1459 /* read from software depth buffer */
1460 if (ctx
->Visual
.DepthBits
<= 16) {
1461 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1463 for (i
= 0; i
< n
; i
++) {
1468 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1470 for (i
= 0; i
< n
; i
++) {
1475 else if (ctx
->Driver
.ReadDepthSpan
) {
1476 /* read from hardware depth buffer */
1477 (*ctx
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, depth
);
1480 /* no depth buffer */
1481 BZERO(depth
, n
* sizeof(GLfloat
));
1490 * Return a span of depth values from the depth buffer as floats in [0,1].
1491 * This is used for both hardware and software depth buffers.
1492 * Input: n - how many pixels
1493 * x,y - location of first pixel
1494 * Output: depth - the array of depth values
1497 _mesa_read_depth_span_float( GLcontext
*ctx
,
1498 GLint n
, GLint x
, GLint y
, GLfloat depth
[] )
1500 const GLfloat scale
= 1.0F
/ ctx
->Visual
.DepthMaxF
;
1502 if (y
< 0 || y
>= ctx
->DrawBuffer
->Height
||
1503 x
+ (GLint
) n
<= 0 || x
>= ctx
->DrawBuffer
->Width
) {
1504 /* span is completely outside framebuffer */
1506 for (i
= 0; i
< n
; i
++)
1514 for (i
= 0; i
< dx
; i
++)
1519 if (x
+ n
> ctx
->DrawBuffer
->Width
) {
1520 GLint dx
= x
+ n
- ctx
->DrawBuffer
->Width
;
1522 for (i
= 0; i
< dx
; i
++)
1523 depth
[n
- i
- 1] = 0.0F
;
1530 if (ctx
->DrawBuffer
->DepthBuffer
) {
1531 /* read from software depth buffer */
1532 if (ctx
->Visual
.DepthBits
<= 16) {
1533 const GLushort
*zptr
= Z_ADDRESS16( ctx
, x
, y
);
1535 for (i
= 0; i
< n
; i
++) {
1536 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1540 const GLuint
*zptr
= Z_ADDRESS32( ctx
, x
, y
);
1542 for (i
= 0; i
< n
; i
++) {
1543 depth
[i
] = (GLfloat
) zptr
[i
] * scale
;
1547 else if (ctx
->Driver
.ReadDepthSpan
) {
1548 /* read from hardware depth buffer */
1549 GLdepth d
[MAX_WIDTH
];
1551 assert(n
<= MAX_WIDTH
);
1552 (*ctx
->Driver
.ReadDepthSpan
)( ctx
, n
, x
, y
, d
);
1553 for (i
= 0; i
< n
; i
++) {
1554 depth
[i
] = d
[i
] * scale
;
1558 /* no depth buffer */
1559 BZERO(depth
, n
* sizeof(GLfloat
));
1565 /**********************************************************************/
1566 /***** Allocate and Clear Depth Buffer *****/
1567 /**********************************************************************/
1572 * Allocate a new depth buffer. If there's already a depth buffer allocated
1573 * it will be free()'d. The new depth buffer will be uniniitalized.
1574 * This function is only called through Driver.alloc_depth_buffer.
1577 _mesa_alloc_depth_buffer( GLcontext
*ctx
)
1579 /* deallocate current depth buffer if present */
1580 if (ctx
->DrawBuffer
->UseSoftwareDepthBuffer
) {
1581 GLint bytesPerValue
;
1583 if (ctx
->DrawBuffer
->DepthBuffer
) {
1584 FREE(ctx
->DrawBuffer
->DepthBuffer
);
1585 ctx
->DrawBuffer
->DepthBuffer
= NULL
;
1588 /* allocate new depth buffer, but don't initialize it */
1589 if (ctx
->Visual
.DepthBits
<= 16)
1590 bytesPerValue
= sizeof(GLushort
);
1592 bytesPerValue
= sizeof(GLuint
);
1594 ctx
->DrawBuffer
->DepthBuffer
= MALLOC( ctx
->DrawBuffer
->Width
1595 * ctx
->DrawBuffer
->Height
1598 if (!ctx
->DrawBuffer
->DepthBuffer
) {
1600 ctx
->Depth
.Test
= GL_FALSE
;
1601 ctx
->NewState
|= NEW_RASTER_OPS
;
1602 gl_error( ctx
, GL_OUT_OF_MEMORY
, "Couldn't allocate depth buffer" );
1611 * Clear the depth buffer. If the depth buffer doesn't exist yet we'll
1613 * This function is only called through Driver.clear_depth_buffer.
1616 _mesa_clear_depth_buffer( GLcontext
*ctx
)
1618 if (ctx
->Visual
.DepthBits
== 0
1619 || !ctx
->DrawBuffer
->DepthBuffer
1620 || !ctx
->Depth
.Mask
) {
1621 /* no depth buffer, or writing to it is disabled */
1625 /* The loops in this function have been written so the IRIX 5.3
1626 * C compiler can unroll them. Hopefully other compilers can too!
1629 if (ctx
->Scissor
.Enabled
) {
1630 /* only clear scissor region */
1631 if (ctx
->Visual
.DepthBits
<= 16) {
1632 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->Visual
.DepthMax
);
1633 const GLint rows
= ctx
->DrawBuffer
->Ymax
- ctx
->DrawBuffer
->Ymin
;
1634 const GLint width
= ctx
->DrawBuffer
->Width
;
1635 GLushort
*dRow
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
1636 + ctx
->DrawBuffer
->Ymin
* width
+ ctx
->DrawBuffer
->Xmin
;
1638 for (i
= 0; i
< rows
; i
++) {
1639 for (j
= 0; j
< width
; j
++) {
1640 dRow
[j
] = clearValue
;
1646 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->Visual
.DepthMax
);
1647 const GLint rows
= ctx
->DrawBuffer
->Ymax
- ctx
->DrawBuffer
->Ymin
;
1648 const GLint width
= ctx
->DrawBuffer
->Width
;
1649 GLuint
*dRow
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
1650 + ctx
->DrawBuffer
->Ymin
* width
+ ctx
->DrawBuffer
->Xmin
;
1652 for (i
= 0; i
< rows
; i
++) {
1653 for (j
= 0; j
< width
; j
++) {
1654 dRow
[j
] = clearValue
;
1661 /* clear whole buffer */
1662 if (ctx
->Visual
.DepthBits
<= 16) {
1663 const GLushort clearValue
= (GLushort
) (ctx
->Depth
.Clear
* ctx
->Visual
.DepthMax
);
1664 if ((clearValue
& 0xff) == (clearValue
>> 8)) {
1665 if (clearValue
== 0) {
1666 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1667 2*ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
);
1670 /* lower and upper bytes of clear_value are same, use MEMSET */
1671 MEMSET( ctx
->DrawBuffer
->DepthBuffer
, clearValue
& 0xff,
1672 2 * ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
);
1676 GLushort
*d
= (GLushort
*) ctx
->DrawBuffer
->DepthBuffer
;
1677 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1679 d
[0] = clearValue
; d
[1] = clearValue
;
1680 d
[2] = clearValue
; d
[3] = clearValue
;
1681 d
[4] = clearValue
; d
[5] = clearValue
;
1682 d
[6] = clearValue
; d
[7] = clearValue
;
1683 d
[8] = clearValue
; d
[9] = clearValue
;
1684 d
[10] = clearValue
; d
[11] = clearValue
;
1685 d
[12] = clearValue
; d
[13] = clearValue
;
1686 d
[14] = clearValue
; d
[15] = clearValue
;
1697 /* >16 bit depth buffer */
1698 const GLuint clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->Visual
.DepthMax
);
1699 if (clearValue
== 0) {
1700 BZERO(ctx
->DrawBuffer
->DepthBuffer
,
1701 ctx
->DrawBuffer
->Width
*ctx
->DrawBuffer
->Height
*sizeof(GLuint
));
1704 GLint n
= ctx
->DrawBuffer
->Width
* ctx
->DrawBuffer
->Height
;
1705 GLuint
*d
= (GLuint
*) ctx
->DrawBuffer
->DepthBuffer
;
1707 d
[0] = clearValue
; d
[1] = clearValue
;
1708 d
[2] = clearValue
; d
[3] = clearValue
;
1709 d
[4] = clearValue
; d
[5] = clearValue
;
1710 d
[6] = clearValue
; d
[7] = clearValue
;
1711 d
[8] = clearValue
; d
[9] = clearValue
;
1712 d
[10] = clearValue
; d
[11] = clearValue
;
1713 d
[12] = clearValue
; d
[13] = clearValue
;
1714 d
[14] = clearValue
; d
[15] = clearValue
;