2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "main/glheader.h"
27 #include "main/context.h"
28 #include "main/formats.h"
29 #include "main/macros.h"
30 #include "main/imports.h"
31 #include "main/fbobject.h"
34 #include "s_context.h"
39 * Do depth test for a horizontal span of fragments.
40 * Input: zbuffer - array of z values in the zbuffer
41 * z - array of fragment z values
42 * Return: number of fragments which pass the test.
45 depth_test_span16( GLcontext
*ctx
, GLuint n
,
46 GLushort zbuffer
[], const GLuint z
[], GLubyte mask
[] )
50 /* switch cases ordered from most frequent to less frequent */
51 switch (ctx
->Depth
.Func
) {
53 if (ctx
->Depth
.Mask
) {
58 if (z
[i
] < zbuffer
[i
]) {
71 /* Don't update Z buffer */
75 if (z
[i
] < zbuffer
[i
]) {
87 if (ctx
->Depth
.Mask
) {
92 if (z
[i
] <= zbuffer
[i
]) {
103 /* Don't update Z buffer */
107 if (z
[i
] <= zbuffer
[i
]) {
119 if (ctx
->Depth
.Mask
) {
120 /* Update Z buffer */
124 if (z
[i
] >= zbuffer
[i
]) {
135 /* Don't update Z buffer */
139 if (z
[i
] >= zbuffer
[i
]) {
151 if (ctx
->Depth
.Mask
) {
152 /* Update Z buffer */
156 if (z
[i
] > zbuffer
[i
]) {
167 /* Don't update Z buffer */
171 if (z
[i
] > zbuffer
[i
]) {
183 if (ctx
->Depth
.Mask
) {
184 /* Update Z buffer */
188 if (z
[i
] != zbuffer
[i
]) {
199 /* Don't update Z buffer */
203 if (z
[i
] != zbuffer
[i
]) {
215 if (ctx
->Depth
.Mask
) {
216 /* Update Z buffer */
220 if (z
[i
] == zbuffer
[i
]) {
231 /* Don't update Z buffer */
235 if (z
[i
] == zbuffer
[i
]) {
247 if (ctx
->Depth
.Mask
) {
248 /* Update Z buffer */
258 /* Don't update Z buffer or mask */
263 _mesa_bzero(mask
, n
* sizeof(GLubyte
));
266 _mesa_problem(ctx
, "Bad depth func in depth_test_span16");
274 depth_test_span32( GLcontext
*ctx
, GLuint n
,
275 GLuint zbuffer
[], const GLuint z
[], GLubyte mask
[] )
279 /* switch cases ordered from most frequent to less frequent */
280 switch (ctx
->Depth
.Func
) {
282 if (ctx
->Depth
.Mask
) {
283 /* Update Z buffer */
285 for (i
=0; i
<n
; i
++) {
287 if (z
[i
] < zbuffer
[i
]) {
300 /* Don't update Z buffer */
302 for (i
=0; i
<n
; i
++) {
304 if (z
[i
] < zbuffer
[i
]) {
316 if (ctx
->Depth
.Mask
) {
317 /* Update Z buffer */
321 if (z
[i
] <= zbuffer
[i
]) {
332 /* Don't update Z buffer */
336 if (z
[i
] <= zbuffer
[i
]) {
348 if (ctx
->Depth
.Mask
) {
349 /* Update Z buffer */
353 if (z
[i
] >= zbuffer
[i
]) {
364 /* Don't update Z buffer */
368 if (z
[i
] >= zbuffer
[i
]) {
380 if (ctx
->Depth
.Mask
) {
381 /* Update Z buffer */
385 if (z
[i
] > zbuffer
[i
]) {
396 /* Don't update Z buffer */
400 if (z
[i
] > zbuffer
[i
]) {
412 if (ctx
->Depth
.Mask
) {
413 /* Update Z buffer */
417 if (z
[i
] != zbuffer
[i
]) {
428 /* Don't update Z buffer */
432 if (z
[i
] != zbuffer
[i
]) {
444 if (ctx
->Depth
.Mask
) {
445 /* Update Z buffer */
449 if (z
[i
] == zbuffer
[i
]) {
460 /* Don't update Z buffer */
464 if (z
[i
] == zbuffer
[i
]) {
476 if (ctx
->Depth
.Mask
) {
477 /* Update Z buffer */
487 /* Don't update Z buffer or mask */
492 _mesa_bzero(mask
, n
* sizeof(GLubyte
));
495 _mesa_problem(ctx
, "Bad depth func in depth_test_span32");
501 /* Apply ARB_depth_clamp to span of fragments. */
503 _swrast_depth_clamp_span( GLcontext
*ctx
, SWspan
*span
)
505 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
506 struct gl_renderbuffer
*rb
= fb
->_DepthBuffer
;
507 const GLuint count
= span
->end
;
508 GLuint
*zValues
= span
->array
->z
;
512 if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
513 near
= FLOAT_TO_UINT(ctx
->Viewport
.Near
);
514 far
= FLOAT_TO_UINT(ctx
->Viewport
.Far
);
516 assert(rb
->DataType
== GL_UNSIGNED_INT
);
517 CLAMPED_FLOAT_TO_USHORT(near
, ctx
->Viewport
.Near
);
518 CLAMPED_FLOAT_TO_USHORT(far
, ctx
->Viewport
.Far
);
520 for (i
= 0; i
< count
; i
++) {
521 if (zValues
[i
] < near
)
523 if (zValues
[i
] > far
)
531 * Apply depth test to span of fragments.
534 depth_test_span( GLcontext
*ctx
, SWspan
*span
)
536 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
537 struct gl_renderbuffer
*rb
= fb
->_DepthBuffer
;
538 const GLint x
= span
->x
;
539 const GLint y
= span
->y
;
540 const GLuint count
= span
->end
;
541 const GLuint
*zValues
= span
->array
->z
;
542 GLubyte
*mask
= span
->array
->mask
;
545 ASSERT((span
->arrayMask
& SPAN_XY
) == 0);
546 ASSERT(span
->arrayMask
& SPAN_Z
);
548 if (rb
->GetPointer(ctx
, rb
, 0, 0)) {
549 /* Directly access buffer */
550 if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
551 GLushort
*zbuffer
= (GLushort
*) rb
->GetPointer(ctx
, rb
, x
, y
);
552 passed
= depth_test_span16(ctx
, count
, zbuffer
, zValues
, mask
);
555 GLuint
*zbuffer
= (GLuint
*) rb
->GetPointer(ctx
, rb
, x
, y
);
556 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
557 passed
= depth_test_span32(ctx
, count
, zbuffer
, zValues
, mask
);
561 /* read depth values from buffer, test, write back */
562 if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
563 GLushort zbuffer
[MAX_WIDTH
];
564 rb
->GetRow(ctx
, rb
, count
, x
, y
, zbuffer
);
565 passed
= depth_test_span16(ctx
, count
, zbuffer
, zValues
, mask
);
566 rb
->PutRow(ctx
, rb
, count
, x
, y
, zbuffer
, mask
);
569 GLuint zbuffer
[MAX_WIDTH
];
570 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
571 rb
->GetRow(ctx
, rb
, count
, x
, y
, zbuffer
);
572 passed
= depth_test_span32(ctx
, count
, zbuffer
, zValues
, mask
);
573 rb
->PutRow(ctx
, rb
, count
, x
, y
, zbuffer
, mask
);
577 if (passed
< count
) {
578 span
->writeAll
= GL_FALSE
;
585 #define Z_ADDRESS(X, Y) (zStart + (Y) * stride + (X))
589 * Do depth testing for an array of fragments at assorted locations.
592 direct_depth_test_pixels16(GLcontext
*ctx
, GLushort
*zStart
, GLuint stride
,
593 GLuint n
, const GLint x
[], const GLint y
[],
594 const GLuint z
[], GLubyte mask
[] )
596 /* switch cases ordered from most frequent to less frequent */
597 switch (ctx
->Depth
.Func
) {
599 if (ctx
->Depth
.Mask
) {
600 /* Update Z buffer */
602 for (i
=0; i
<n
; i
++) {
604 GLushort
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
617 /* Don't update Z buffer */
619 for (i
=0; i
<n
; i
++) {
621 GLushort
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
634 if (ctx
->Depth
.Mask
) {
635 /* Update Z buffer */
637 for (i
=0; i
<n
; i
++) {
639 GLushort
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
652 /* Don't update Z buffer */
654 for (i
=0; i
<n
; i
++) {
656 GLushort
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
669 if (ctx
->Depth
.Mask
) {
670 /* Update Z buffer */
672 for (i
=0; i
<n
; i
++) {
674 GLushort
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
687 /* Don't update Z buffer */
689 for (i
=0; i
<n
; i
++) {
691 GLushort
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
704 if (ctx
->Depth
.Mask
) {
705 /* Update Z buffer */
707 for (i
=0; i
<n
; i
++) {
709 GLushort
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
722 /* Don't update Z buffer */
724 for (i
=0; i
<n
; i
++) {
726 GLushort
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
739 if (ctx
->Depth
.Mask
) {
740 /* Update Z buffer */
742 for (i
=0; i
<n
; i
++) {
744 GLushort
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
757 /* Don't update Z buffer */
759 for (i
=0; i
<n
; i
++) {
761 GLushort
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
774 if (ctx
->Depth
.Mask
) {
775 /* Update Z buffer */
777 for (i
=0; i
<n
; i
++) {
779 GLushort
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
792 /* Don't update Z buffer */
794 for (i
=0; i
<n
; i
++) {
796 GLushort
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
809 if (ctx
->Depth
.Mask
) {
810 /* Update Z buffer */
812 for (i
=0; i
<n
; i
++) {
814 GLushort
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
820 /* Don't update Z buffer or mask */
824 /* depth test never passes */
825 _mesa_bzero(mask
, n
* sizeof(GLubyte
));
828 _mesa_problem(ctx
, "Bad depth func in direct_depth_test_pixels");
835 * Do depth testing for an array of fragments with direct access to zbuffer.
838 direct_depth_test_pixels32(GLcontext
*ctx
, GLuint
*zStart
, GLuint stride
,
839 GLuint n
, const GLint x
[], const GLint y
[],
840 const GLuint z
[], GLubyte mask
[] )
842 /* switch cases ordered from most frequent to less frequent */
843 switch (ctx
->Depth
.Func
) {
845 if (ctx
->Depth
.Mask
) {
846 /* Update Z buffer */
848 for (i
=0; i
<n
; i
++) {
850 GLuint
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
863 /* Don't update Z buffer */
865 for (i
=0; i
<n
; i
++) {
867 GLuint
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
880 if (ctx
->Depth
.Mask
) {
881 /* Update Z buffer */
883 for (i
=0; i
<n
; i
++) {
885 GLuint
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
898 /* Don't update Z buffer */
900 for (i
=0; i
<n
; i
++) {
902 GLuint
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
915 if (ctx
->Depth
.Mask
) {
916 /* Update Z buffer */
918 for (i
=0; i
<n
; i
++) {
920 GLuint
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
933 /* Don't update Z buffer */
935 for (i
=0; i
<n
; i
++) {
937 GLuint
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
950 if (ctx
->Depth
.Mask
) {
951 /* Update Z buffer */
953 for (i
=0; i
<n
; i
++) {
955 GLuint
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
968 /* Don't update Z buffer */
970 for (i
=0; i
<n
; i
++) {
972 GLuint
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
985 if (ctx
->Depth
.Mask
) {
986 /* Update Z buffer */
988 for (i
=0; i
<n
; i
++) {
990 GLuint
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
1003 /* Don't update Z buffer */
1005 for (i
=0; i
<n
; i
++) {
1007 GLuint
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
1008 if (z
[i
] != *zptr
) {
1020 if (ctx
->Depth
.Mask
) {
1021 /* Update Z buffer */
1023 for (i
=0; i
<n
; i
++) {
1025 GLuint
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
1026 if (z
[i
] == *zptr
) {
1038 /* Don't update Z buffer */
1040 for (i
=0; i
<n
; i
++) {
1042 GLuint
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
1043 if (z
[i
] == *zptr
) {
1055 if (ctx
->Depth
.Mask
) {
1056 /* Update Z buffer */
1058 for (i
=0; i
<n
; i
++) {
1060 GLuint
*zptr
= Z_ADDRESS(x
[i
], y
[i
]);
1066 /* Don't update Z buffer or mask */
1070 /* depth test never passes */
1071 _mesa_bzero(mask
, n
* sizeof(GLubyte
));
1074 _mesa_problem(ctx
, "Bad depth func in direct_depth_test_pixels");
1082 depth_test_pixels( GLcontext
*ctx
, SWspan
*span
)
1084 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1085 struct gl_renderbuffer
*rb
= fb
->_DepthBuffer
;
1086 const GLuint count
= span
->end
;
1087 const GLint
*x
= span
->array
->x
;
1088 const GLint
*y
= span
->array
->y
;
1089 const GLuint
*z
= span
->array
->z
;
1090 GLubyte
*mask
= span
->array
->mask
;
1092 if (rb
->GetPointer(ctx
, rb
, 0, 0)) {
1093 /* Directly access values */
1094 if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1095 GLushort
*zStart
= (GLushort
*) rb
->Data
;
1096 GLuint stride
= rb
->Width
;
1097 direct_depth_test_pixels16(ctx
, zStart
, stride
, count
, x
, y
, z
, mask
);
1100 GLuint
*zStart
= (GLuint
*) rb
->Data
;
1101 GLuint stride
= rb
->Width
;
1102 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
1103 direct_depth_test_pixels32(ctx
, zStart
, stride
, count
, x
, y
, z
, mask
);
1107 /* read depth values from buffer, test, write back */
1108 if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1109 GLushort zbuffer
[MAX_WIDTH
];
1110 _swrast_get_values(ctx
, rb
, count
, x
, y
, zbuffer
, sizeof(GLushort
));
1111 depth_test_span16(ctx
, count
, zbuffer
, z
, mask
);
1112 rb
->PutValues(ctx
, rb
, count
, x
, y
, zbuffer
, mask
);
1115 GLuint zbuffer
[MAX_WIDTH
];
1116 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
1117 _swrast_get_values(ctx
, rb
, count
, x
, y
, zbuffer
, sizeof(GLuint
));
1118 depth_test_span32(ctx
, count
, zbuffer
, z
, mask
);
1119 rb
->PutValues(ctx
, rb
, count
, x
, y
, zbuffer
, mask
);
1123 return count
; /* not really correct, but OK */
1128 * Apply depth (Z) buffer testing to the span.
1129 * \return approx number of pixels that passed (only zero is reliable)
1132 _swrast_depth_test_span( GLcontext
*ctx
, SWspan
*span
)
1134 if (span
->arrayMask
& SPAN_XY
)
1135 return depth_test_pixels(ctx
, span
);
1137 return depth_test_span(ctx
, span
);
1142 * GL_EXT_depth_bounds_test extension.
1143 * Discard fragments depending on whether the corresponding Z-buffer
1144 * values are outside the depth bounds test range.
1145 * Note: we test the Z buffer values, not the fragment Z values!
1146 * \return GL_TRUE if any fragments pass, GL_FALSE if no fragments pass
1149 _swrast_depth_bounds_test( GLcontext
*ctx
, SWspan
*span
)
1151 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
1152 struct gl_renderbuffer
*rb
= fb
->_DepthBuffer
;
1153 GLuint zMin
= (GLuint
) (ctx
->Depth
.BoundsMin
* fb
->_DepthMaxF
+ 0.5F
);
1154 GLuint zMax
= (GLuint
) (ctx
->Depth
.BoundsMax
* fb
->_DepthMaxF
+ 0.5F
);
1155 GLubyte
*mask
= span
->array
->mask
;
1156 const GLuint count
= span
->end
;
1158 GLboolean anyPass
= GL_FALSE
;
1160 if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1161 /* get 16-bit values */
1162 GLushort zbuffer16
[MAX_WIDTH
], *zbuffer
;
1163 if (span
->arrayMask
& SPAN_XY
) {
1164 _swrast_get_values(ctx
, rb
, count
, span
->array
->x
, span
->array
->y
,
1165 zbuffer16
, sizeof(GLushort
));
1166 zbuffer
= zbuffer16
;
1169 zbuffer
= (GLushort
*) rb
->GetPointer(ctx
, rb
, span
->x
, span
->y
);
1171 rb
->GetRow(ctx
, rb
, count
, span
->x
, span
->y
, zbuffer16
);
1172 zbuffer
= zbuffer16
;
1177 /* Now do the tests */
1178 for (i
= 0; i
< count
; i
++) {
1180 if (zbuffer
[i
] < zMin
|| zbuffer
[i
] > zMax
)
1188 /* get 32-bit values */
1189 GLuint zbuffer32
[MAX_WIDTH
], *zbuffer
;
1190 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
1191 if (span
->arrayMask
& SPAN_XY
) {
1192 _swrast_get_values(ctx
, rb
, count
, span
->array
->x
, span
->array
->y
,
1193 zbuffer32
, sizeof(GLuint
));
1194 zbuffer
= zbuffer32
;
1197 zbuffer
= (GLuint
*) rb
->GetPointer(ctx
, rb
, span
->x
, span
->y
);
1199 rb
->GetRow(ctx
, rb
, count
, span
->x
, span
->y
, zbuffer32
);
1200 zbuffer
= zbuffer32
;
1205 /* Now do the tests */
1206 for (i
= 0; i
< count
; i
++) {
1208 if (zbuffer
[i
] < zMin
|| zbuffer
[i
] > zMax
)
1221 /**********************************************************************/
1222 /***** Read Depth Buffer *****/
1223 /**********************************************************************/
1227 * Read a span of depth values from the given depth renderbuffer, returning
1228 * the values as GLfloats.
1229 * This function does clipping to prevent reading outside the depth buffer's
1230 * bounds. Though the clipping is redundant when we're called from
1231 * _swrast_ReadPixels.
1234 _swrast_read_depth_span_float( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1235 GLint n
, GLint x
, GLint y
, GLfloat depth
[] )
1237 const GLfloat scale
= 1.0F
/ ctx
->DrawBuffer
->_DepthMaxF
;
1240 /* really only doing this to prevent FP exceptions later */
1241 _mesa_bzero(depth
, n
* sizeof(GLfloat
));
1244 ASSERT(rb
->_BaseFormat
== GL_DEPTH_COMPONENT
);
1246 if (y
< 0 || y
>= (GLint
) rb
->Height
||
1247 x
+ n
<= 0 || x
>= (GLint
) rb
->Width
) {
1248 /* span is completely outside framebuffer */
1249 _mesa_bzero(depth
, n
* sizeof(GLfloat
));
1256 for (i
= 0; i
< dx
; i
++)
1262 if (x
+ n
> (GLint
) rb
->Width
) {
1263 GLint dx
= x
+ n
- (GLint
) rb
->Width
;
1265 for (i
= 0; i
< dx
; i
++)
1266 depth
[n
- i
- 1] = 0.0;
1273 if (rb
->DataType
== GL_UNSIGNED_INT
) {
1274 GLuint temp
[MAX_WIDTH
];
1276 rb
->GetRow(ctx
, rb
, n
, x
, y
, temp
);
1277 for (i
= 0; i
< n
; i
++) {
1278 depth
[i
] = temp
[i
] * scale
;
1281 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1282 GLushort temp
[MAX_WIDTH
];
1284 rb
->GetRow(ctx
, rb
, n
, x
, y
, temp
);
1285 for (i
= 0; i
< n
; i
++) {
1286 depth
[i
] = temp
[i
] * scale
;
1290 _mesa_problem(ctx
, "Invalid depth renderbuffer data type");
1296 * As above, but return 32-bit GLuint values.
1299 _swrast_read_depth_span_uint( GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
1300 GLint n
, GLint x
, GLint y
, GLuint depth
[] )
1305 /* really only doing this to prevent FP exceptions later */
1306 _mesa_bzero(depth
, n
* sizeof(GLfloat
));
1309 depthBits
= _mesa_get_format_bits(rb
->Format
, GL_DEPTH_BITS
);
1311 ASSERT(rb
->_BaseFormat
== GL_DEPTH_COMPONENT
);
1313 if (y
< 0 || y
>= (GLint
) rb
->Height
||
1314 x
+ n
<= 0 || x
>= (GLint
) rb
->Width
) {
1315 /* span is completely outside framebuffer */
1316 _mesa_bzero(depth
, n
* sizeof(GLfloat
));
1323 for (i
= 0; i
< dx
; i
++)
1329 if (x
+ n
> (GLint
) rb
->Width
) {
1330 GLint dx
= x
+ n
- (GLint
) rb
->Width
;
1332 for (i
= 0; i
< dx
; i
++)
1333 depth
[n
- i
- 1] = 0;
1340 if (rb
->DataType
== GL_UNSIGNED_INT
) {
1341 rb
->GetRow(ctx
, rb
, n
, x
, y
, depth
);
1342 if (depthBits
< 32) {
1343 GLuint shift
= 32 - depthBits
;
1345 for (i
= 0; i
< n
; i
++) {
1346 GLuint z
= depth
[i
];
1347 depth
[i
] = z
<< shift
; /* XXX lsb bits? */
1351 else if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1352 GLushort temp
[MAX_WIDTH
];
1354 rb
->GetRow(ctx
, rb
, n
, x
, y
, temp
);
1355 if (depthBits
== 16) {
1356 for (i
= 0; i
< n
; i
++) {
1358 depth
[i
] = (z
<< 16) | z
;
1362 GLuint shift
= 16 - depthBits
;
1363 for (i
= 0; i
< n
; i
++) {
1365 depth
[i
] = (z
<< (shift
+ 16)) | (z
<< shift
); /* XXX lsb bits? */
1370 _mesa_problem(ctx
, "Invalid depth renderbuffer data type");
1377 * Clear the given z/depth renderbuffer.
1380 _swrast_clear_depth_buffer( GLcontext
*ctx
, struct gl_renderbuffer
*rb
)
1383 GLint x
, y
, width
, height
;
1385 if (!rb
|| !ctx
->Depth
.Mask
) {
1386 /* no depth buffer, or writing to it is disabled */
1390 /* compute integer clearing value */
1391 if (ctx
->Depth
.Clear
== 1.0) {
1392 clearValue
= ctx
->DrawBuffer
->_DepthMax
;
1395 clearValue
= (GLuint
) (ctx
->Depth
.Clear
* ctx
->DrawBuffer
->_DepthMaxF
);
1398 assert(rb
->_BaseFormat
== GL_DEPTH_COMPONENT
);
1400 /* compute region to clear */
1401 x
= ctx
->DrawBuffer
->_Xmin
;
1402 y
= ctx
->DrawBuffer
->_Ymin
;
1403 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
1404 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
1406 if (rb
->GetPointer(ctx
, rb
, 0, 0)) {
1407 /* Direct buffer access is possible. Either this is just malloc'd
1408 * memory, or perhaps the driver mmap'd the zbuffer memory.
1410 if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1411 if ((clearValue
& 0xff) == ((clearValue
>> 8) & 0xff) &&
1412 ((GLushort
*) rb
->GetPointer(ctx
, rb
, 0, 0) + width
==
1413 (GLushort
*) rb
->GetPointer(ctx
, rb
, 0, 1))) {
1414 /* optimized case */
1415 GLushort
*dst
= (GLushort
*) rb
->GetPointer(ctx
, rb
, x
, y
);
1416 GLuint len
= width
* height
* sizeof(GLushort
);
1417 _mesa_memset(dst
, (clearValue
& 0xff), len
);
1422 for (i
= 0; i
< height
; i
++) {
1423 GLushort
*dst
= (GLushort
*) rb
->GetPointer(ctx
, rb
, x
, y
+ i
);
1424 for (j
= 0; j
< width
; j
++) {
1425 dst
[j
] = clearValue
;
1432 ASSERT(rb
->DataType
== GL_UNSIGNED_INT
);
1433 for (i
= 0; i
< height
; i
++) {
1434 GLuint
*dst
= (GLuint
*) rb
->GetPointer(ctx
, rb
, x
, y
+ i
);
1435 for (j
= 0; j
< width
; j
++) {
1436 dst
[j
] = clearValue
;
1442 /* Direct access not possible. Use PutRow to write new values. */
1443 if (rb
->DataType
== GL_UNSIGNED_SHORT
) {
1444 GLushort clearVal16
= (GLushort
) (clearValue
& 0xffff);
1446 for (i
= 0; i
< height
; i
++) {
1447 rb
->PutMonoRow(ctx
, rb
, width
, x
, y
+ i
, &clearVal16
, NULL
);
1450 else if (rb
->DataType
== GL_UNSIGNED_INT
) {
1452 ASSERT(sizeof(clearValue
) == sizeof(GLuint
));
1453 for (i
= 0; i
< height
; i
++) {
1454 rb
->PutMonoRow(ctx
, rb
, width
, x
, y
+ i
, &clearValue
, NULL
);
1458 _mesa_problem(ctx
, "bad depth renderbuffer DataType");