intel: Fix performance regression in Lightsmark since HiZ changes.
[mesa.git] / src / mesa / swrast / s_depth.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.2.1
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25
26 #include "main/glheader.h"
27 #include "main/context.h"
28 #include "main/formats.h"
29 #include "main/format_unpack.h"
30 #include "main/format_pack.h"
31 #include "main/macros.h"
32 #include "main/imports.h"
33
34 #include "s_context.h"
35 #include "s_depth.h"
36 #include "s_span.h"
37
38
39
40 #define Z_TEST(COMPARE) \
41 do { \
42 GLuint i; \
43 for (i = 0; i < n; i++) { \
44 if (mask[i]) { \
45 if (COMPARE) { \
46 /* pass */ \
47 if (write) { \
48 zbuffer[i] = zfrag[i]; \
49 } \
50 passed++; \
51 } \
52 else { \
53 /* fail */ \
54 mask[i] = 0; \
55 } \
56 } \
57 } \
58 } while (0)
59
60
61 /**
62 * Do depth test for an array of 16-bit Z values.
63 * @param zbuffer array of Z buffer values (16-bit)
64 * @param zfrag array of fragment Z values (use 16-bit in 32-bit uint)
65 * @param mask which fragments are alive, killed afterward
66 * @return number of fragments which pass the test.
67 */
68 static GLuint
69 depth_test_span16( struct gl_context *ctx, GLuint n,
70 GLushort zbuffer[], const GLuint zfrag[], GLubyte mask[] )
71 {
72 const GLboolean write = ctx->Depth.Mask;
73 GLuint passed = 0;
74
75 /* switch cases ordered from most frequent to less frequent */
76 switch (ctx->Depth.Func) {
77 case GL_LESS:
78 Z_TEST(zfrag[i] < zbuffer[i]);
79 break;
80 case GL_LEQUAL:
81 Z_TEST(zfrag[i] <= zbuffer[i]);
82 break;
83 case GL_GEQUAL:
84 Z_TEST(zfrag[i] >= zbuffer[i]);
85 break;
86 case GL_GREATER:
87 Z_TEST(zfrag[i] > zbuffer[i]);
88 break;
89 case GL_NOTEQUAL:
90 Z_TEST(zfrag[i] != zbuffer[i]);
91 break;
92 case GL_EQUAL:
93 Z_TEST(zfrag[i] == zbuffer[i]);
94 break;
95 case GL_ALWAYS:
96 Z_TEST(1);
97 break;
98 case GL_NEVER:
99 memset(mask, 0, n * sizeof(GLubyte));
100 break;
101 default:
102 _mesa_problem(ctx, "Bad depth func in depth_test_span16");
103 }
104
105 return passed;
106 }
107
108
109 /**
110 * Do depth test for an array of 32-bit Z values.
111 * @param zbuffer array of Z buffer values (32-bit)
112 * @param zfrag array of fragment Z values (use 32-bits in 32-bit uint)
113 * @param mask which fragments are alive, killed afterward
114 * @return number of fragments which pass the test.
115 */
116 static GLuint
117 depth_test_span32( struct gl_context *ctx, GLuint n,
118 GLuint zbuffer[], const GLuint zfrag[], GLubyte mask[])
119 {
120 const GLboolean write = ctx->Depth.Mask;
121 GLuint passed = 0;
122
123 /* switch cases ordered from most frequent to less frequent */
124 switch (ctx->Depth.Func) {
125 case GL_LESS:
126 Z_TEST(zfrag[i] < zbuffer[i]);
127 break;
128 case GL_LEQUAL:
129 Z_TEST(zfrag[i] <= zbuffer[i]);
130 break;
131 case GL_GEQUAL:
132 Z_TEST(zfrag[i] >= zbuffer[i]);
133 break;
134 case GL_GREATER:
135 Z_TEST(zfrag[i] > zbuffer[i]);
136 break;
137 case GL_NOTEQUAL:
138 Z_TEST(zfrag[i] != zbuffer[i]);
139 break;
140 case GL_EQUAL:
141 Z_TEST(zfrag[i] == zbuffer[i]);
142 break;
143 case GL_ALWAYS:
144 Z_TEST(1);
145 break;
146 case GL_NEVER:
147 memset(mask, 0, n * sizeof(GLubyte));
148 break;
149 default:
150 _mesa_problem(ctx, "Bad depth func in depth_test_span32");
151 }
152
153 return passed;
154 }
155
156
157 /**
158 * Clamp fragment Z values to the depth near/far range (glDepthRange()).
159 * This is used when GL_ARB_depth_clamp/GL_DEPTH_CLAMP is turned on.
160 * In that case, vertexes are not clipped against the near/far planes
161 * so rasterization will produce fragment Z values outside the usual
162 * [0,1] range.
163 */
164 void
165 _swrast_depth_clamp_span( struct gl_context *ctx, SWspan *span )
166 {
167 struct gl_framebuffer *fb = ctx->DrawBuffer;
168 const GLuint count = span->end;
169 GLint *zValues = (GLint *) span->array->z; /* sign change */
170 GLint min, max;
171 GLfloat min_f, max_f;
172 GLuint i;
173
174 if (ctx->Viewport.Near < ctx->Viewport.Far) {
175 min_f = ctx->Viewport.Near;
176 max_f = ctx->Viewport.Far;
177 } else {
178 min_f = ctx->Viewport.Far;
179 max_f = ctx->Viewport.Near;
180 }
181
182 /* Convert floating point values in [0,1] to device Z coordinates in
183 * [0, DepthMax].
184 * ex: If the Z buffer has 24 bits, DepthMax = 0xffffff.
185 *
186 * XXX this all falls apart if we have 31 or more bits of Z because
187 * the triangle rasterization code produces unsigned Z values. Negative
188 * vertex Z values come out as large fragment Z uints.
189 */
190 min = (GLint) (min_f * fb->_DepthMaxF);
191 max = (GLint) (max_f * fb->_DepthMaxF);
192 if (max < 0)
193 max = 0x7fffffff; /* catch over flow for 30-bit z */
194
195 /* Note that we do the comparisons here using signed integers.
196 */
197 for (i = 0; i < count; i++) {
198 if (zValues[i] < min)
199 zValues[i] = min;
200 if (zValues[i] > max)
201 zValues[i] = max;
202 }
203 }
204
205
206 /**
207 * Get array of 32-bit z values from the depth buffer. With clipping.
208 */
209 static void
210 get_z32_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
211 GLuint count, const GLint x[], const GLint y[],
212 GLuint zbuffer[])
213 {
214 const GLint w = rb->Width, h = rb->Height;
215 const GLubyte *map = (const GLubyte *) rb->Data;
216 GLuint i;
217
218 if (rb->Format == MESA_FORMAT_Z32) {
219 const GLint rowStride = rb->RowStride * 4;
220 for (i = 0; i < count; i++) {
221 if (x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
222 zbuffer[i] = *((GLuint *) (map + y[i] * rowStride + x[i] * 4));
223 }
224 }
225 }
226 else {
227 const GLint bpp = _mesa_get_format_bytes(rb->Format);
228 const GLint rowStride = rb->RowStride * bpp;
229 for (i = 0; i < count; i++) {
230 if (x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
231 const GLubyte *src = map + y[i] * rowStride+ x[i] * bpp;
232 _mesa_unpack_uint_z_row(rb->Format, 1, src, &zbuffer[i]);
233 }
234 }
235 }
236 }
237
238 static void
239 put_z32_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
240 GLuint count, const GLint x[], const GLint y[],
241 const GLuint zvalues[], const GLubyte mask[])
242 {
243 const GLint w = rb->Width, h = rb->Height;
244 GLubyte *map = (GLubyte *) rb->Data;
245 GLuint i;
246
247 if (rb->Format == MESA_FORMAT_Z32) {
248 const GLuint rowStride = rb->RowStride * 4;
249 for (i = 0; i < count; i++) {
250 if (mask[i] && x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
251 GLuint *dst = (GLuint *) (map + y[i] * rowStride + x[i] * 4);
252 *dst = zvalues[i];
253 }
254 }
255 }
256 else {
257 gl_pack_uint_z_func packZ = _mesa_get_pack_uint_z_func(rb->Format);
258 const GLint bpp = _mesa_get_format_bytes(rb->Format);
259 const GLint rowStride = rb->RowStride * bpp;
260 for (i = 0; i < count; i++) {
261 if (mask[i] && x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
262 void *dst = map + y[i] * rowStride + x[i] * bpp;
263 packZ(zvalues + i, dst);
264 }
265 }
266 }
267 }
268
269
270 /**
271 * Apply depth (Z) buffer testing to the span.
272 * \return approx number of pixels that passed (only zero is reliable)
273 */
274 GLuint
275 _swrast_depth_test_span(struct gl_context *ctx, SWspan *span)
276 {
277 struct gl_framebuffer *fb = ctx->DrawBuffer;
278 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
279 const GLint bpp = _mesa_get_format_bytes(rb->Format);
280 void *zStart = _swrast_pixel_address(rb, span->x, span->y);
281 const GLuint count = span->end;
282 const GLuint *fragZ = span->array->z;
283 GLubyte *mask = span->array->mask;
284 void *zBufferVals;
285 GLuint *zBufferTemp = NULL;
286 GLuint passed;
287 GLboolean ztest16 = GL_FALSE;
288 GLboolean ztest24 = _mesa_get_format_bits(rb->Format, GL_DEPTH_BITS) == 24;
289
290 if (rb->Format == MESA_FORMAT_Z16 && !(span->arrayMask & SPAN_XY)) {
291 /* directly read/write row of 16-bit Z values */
292 zBufferVals = zStart;
293 ztest16 = GL_TRUE;
294 }
295 else if (rb->Format == MESA_FORMAT_Z32 && !(span->arrayMask & SPAN_XY)) {
296 /* directly read/write row of 32-bit Z values */
297 zBufferVals = zStart;
298 }
299 else {
300 /* copy Z buffer values into temp buffer (32-bit Z values) */
301 zBufferTemp = (GLuint *) malloc(count * sizeof(GLuint));
302 if (!zBufferTemp)
303 return 0;
304
305 if (span->arrayMask & SPAN_XY) {
306 get_z32_values(ctx, rb, count,
307 span->array->x, span->array->y, zBufferTemp);
308 }
309 else {
310 _mesa_unpack_uint_z_row(rb->Format, count, zStart, zBufferTemp);
311 }
312
313 if (ztest24) {
314 GLuint i;
315 /* Convert depth buffer values from 32 to 24 bits to match the
316 * fragment Z values generated by rasterization.
317 */
318 for (i = 0; i < count; i++) {
319 zBufferTemp[i] >>= 8;
320 }
321 }
322
323 zBufferVals = zBufferTemp;
324 }
325
326 /* do the depth test either with 16 or 32-bit values */
327 if (ztest16)
328 passed = depth_test_span16(ctx, count, zBufferVals, fragZ, mask);
329 else
330 passed = depth_test_span32(ctx, count, zBufferVals, fragZ, mask);
331
332 if (zBufferTemp) {
333 /* need to write temp Z values back into the buffer */
334
335 if (ztest24) {
336 GLuint i;
337 /* Convert depth buffer values back to 32-bit values. The least
338 * significant bits don't matter since they'll get dropped when
339 * they're packed back into the depth buffer.
340 */
341 for (i = 0; i < count; i++) {
342 zBufferTemp[i] = (zBufferTemp[i] << 8);
343 }
344 }
345
346 if (span->arrayMask & SPAN_XY) {
347 /* random locations */
348 put_z32_values(ctx, rb, count, span->array->x, span->array->y,
349 zBufferTemp, mask);
350 }
351 else {
352 /* horizontal row */
353 gl_pack_uint_z_func packZ = _mesa_get_pack_uint_z_func(rb->Format);
354 GLubyte *dst = zStart;
355 GLuint i;
356 for (i = 0; i < count; i++) {
357 if (mask[i]) {
358 packZ(&zBufferTemp[i], dst);
359 }
360 dst += bpp;
361 }
362 }
363
364 free(zBufferTemp);
365 }
366
367 if (passed < count) {
368 span->writeAll = GL_FALSE;
369 }
370 return passed;
371 }
372
373
374 /**
375 * GL_EXT_depth_bounds_test extension.
376 * Discard fragments depending on whether the corresponding Z-buffer
377 * values are outside the depth bounds test range.
378 * Note: we test the Z buffer values, not the fragment Z values!
379 * \return GL_TRUE if any fragments pass, GL_FALSE if no fragments pass
380 */
381 GLboolean
382 _swrast_depth_bounds_test( struct gl_context *ctx, SWspan *span )
383 {
384 struct gl_framebuffer *fb = ctx->DrawBuffer;
385 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
386 const GLint bpp = _mesa_get_format_bytes(rb->Format);
387 const GLint rowStride = rb->RowStride * bpp;
388 GLubyte *zStart = (GLubyte*) rb->Data + span->y * rowStride + span->x * bpp;
389 GLuint zMin = (GLuint) (ctx->Depth.BoundsMin * fb->_DepthMaxF + 0.5F);
390 GLuint zMax = (GLuint) (ctx->Depth.BoundsMax * fb->_DepthMaxF + 0.5F);
391 GLubyte *mask = span->array->mask;
392 const GLuint count = span->end;
393 GLuint i;
394 GLboolean anyPass = GL_FALSE;
395 GLuint zBufferTemp[MAX_WIDTH];
396 const GLuint *zBufferVals;
397
398 if (rb->Format == MESA_FORMAT_Z32 && !(span->arrayMask & SPAN_XY)) {
399 /* directly access 32-bit values in the depth buffer */
400 zBufferVals = (const GLuint *) zStart;
401 }
402 else {
403 /* unpack Z values into a temporary array */
404 if (span->arrayMask & SPAN_XY) {
405 get_z32_values(ctx, rb, count, span->array->x, span->array->y,
406 zBufferTemp);
407 }
408 else {
409 _mesa_unpack_uint_z_row(rb->Format, count, zStart, zBufferTemp);
410 }
411 zBufferVals = zBufferTemp;
412 }
413
414 /* Now do the tests */
415 for (i = 0; i < count; i++) {
416 if (mask[i]) {
417 if (zBufferVals[i] < zMin || zBufferVals[i] > zMax)
418 mask[i] = GL_FALSE;
419 else
420 anyPass = GL_TRUE;
421 }
422 }
423
424 return anyPass;
425 }
426
427
428
429 /**********************************************************************/
430 /***** Read Depth Buffer *****/
431 /**********************************************************************/
432
433
434 /**
435 * Read a span of depth values from the given depth renderbuffer, returning
436 * the values as GLfloats.
437 * This function does clipping to prevent reading outside the depth buffer's
438 * bounds.
439 */
440 void
441 _swrast_read_depth_span_float(struct gl_context *ctx,
442 struct gl_renderbuffer *rb,
443 GLint n, GLint x, GLint y, GLfloat depth[])
444 {
445 if (!rb) {
446 /* really only doing this to prevent FP exceptions later */
447 memset(depth, 0, n * sizeof(GLfloat));
448 return;
449 }
450
451 if (y < 0 || y >= (GLint) rb->Height ||
452 x + n <= 0 || x >= (GLint) rb->Width) {
453 /* span is completely outside framebuffer */
454 memset(depth, 0, n * sizeof(GLfloat));
455 return;
456 }
457
458 if (x < 0) {
459 GLint dx = -x;
460 GLint i;
461 for (i = 0; i < dx; i++)
462 depth[i] = 0.0;
463 x = 0;
464 n -= dx;
465 depth += dx;
466 }
467 if (x + n > (GLint) rb->Width) {
468 GLint dx = x + n - (GLint) rb->Width;
469 GLint i;
470 for (i = 0; i < dx; i++)
471 depth[n - i - 1] = 0.0;
472 n -= dx;
473 }
474 if (n <= 0) {
475 return;
476 }
477
478 _mesa_unpack_float_z_row(rb->Format, n, _swrast_pixel_address(rb, x, y),
479 depth);
480 }
481
482
483 /**
484 * Clear the given z/depth renderbuffer. If the buffer is a combined
485 * depth+stencil buffer, only the Z bits will be touched.
486 */
487 void
488 _swrast_clear_depth_buffer(struct gl_context *ctx)
489 {
490 struct gl_renderbuffer *rb =
491 ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
492 GLuint clearValue;
493 GLint x, y, width, height;
494 GLubyte *map;
495 GLint rowStride, i, j;
496 GLbitfield mapMode;
497
498 if (!rb || !ctx->Depth.Mask) {
499 /* no depth buffer, or writing to it is disabled */
500 return;
501 }
502
503 /* compute integer clearing value */
504 if (ctx->Depth.Clear == 1.0) {
505 clearValue = ctx->DrawBuffer->_DepthMax;
506 }
507 else {
508 clearValue = (GLuint) (ctx->Depth.Clear * ctx->DrawBuffer->_DepthMaxF);
509 }
510
511 /* compute region to clear */
512 x = ctx->DrawBuffer->_Xmin;
513 y = ctx->DrawBuffer->_Ymin;
514 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
515 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
516
517 mapMode = GL_MAP_WRITE_BIT;
518 if (rb->Format == MESA_FORMAT_S8_Z24 ||
519 rb->Format == MESA_FORMAT_X8_Z24 ||
520 rb->Format == MESA_FORMAT_Z24_S8 ||
521 rb->Format == MESA_FORMAT_Z24_X8) {
522 mapMode |= GL_MAP_READ_BIT;
523 }
524
525 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height,
526 mapMode, &map, &rowStride);
527 if (!map) {
528 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClear(depth)");
529 return;
530 }
531
532 switch (rb->Format) {
533 case MESA_FORMAT_Z16:
534 {
535 GLfloat clear = (GLfloat) ctx->Depth.Clear;
536 GLushort clearVal = 0;
537 _mesa_pack_float_z_row(rb->Format, 1, &clear, &clearVal);
538 if (clearVal == 0xffff && width * 2 == rowStride) {
539 /* common case */
540 memset(map, 0xff, width * height * 2);
541 }
542 else {
543 for (i = 0; i < height; i++) {
544 GLushort *row = (GLushort *) map;
545 for (j = 0; j < width; j++) {
546 row[j] = clearVal;
547 }
548 map += rowStride;
549 }
550 }
551 }
552 break;
553 case MESA_FORMAT_Z32:
554 case MESA_FORMAT_Z32_FLOAT:
555 {
556 GLfloat clear = (GLfloat) ctx->Depth.Clear;
557 GLuint clearVal = 0;
558 _mesa_pack_float_z_row(rb->Format, 1, &clear, &clearVal);
559 for (i = 0; i < height; i++) {
560 GLuint *row = (GLuint *) map;
561 for (j = 0; j < width; j++) {
562 row[j] = clearVal;
563 }
564 map += rowStride;
565 }
566 }
567 break;
568 case MESA_FORMAT_S8_Z24:
569 case MESA_FORMAT_X8_Z24:
570 case MESA_FORMAT_Z24_S8:
571 case MESA_FORMAT_Z24_X8:
572 {
573 GLfloat clear = (GLfloat) ctx->Depth.Clear;
574 GLuint clearVal = 0;
575 GLuint mask;
576
577 if (rb->Format == MESA_FORMAT_S8_Z24 ||
578 rb->Format == MESA_FORMAT_X8_Z24)
579 mask = 0xff000000;
580 else
581 mask = 0xff;
582
583 _mesa_pack_float_z_row(rb->Format, 1, &clear, &clearVal);
584 for (i = 0; i < height; i++) {
585 GLuint *row = (GLuint *) map;
586 for (j = 0; j < width; j++) {
587 row[j] = (row[j] & mask) | clearVal;
588 }
589 map += rowStride;
590 }
591
592 }
593 break;
594 case MESA_FORMAT_Z32_FLOAT_X24S8:
595 /* XXX untested */
596 {
597 GLfloat clearVal = (GLfloat) ctx->Depth.Clear;
598 for (i = 0; i < height; i++) {
599 GLfloat *row = (GLfloat *) map;
600 for (j = 0; j < width; j++) {
601 row[j * 2] = clearVal;
602 }
603 map += rowStride;
604 }
605 }
606 break;
607 default:
608 _mesa_problem(ctx, "Unexpected depth buffer format %s"
609 " in _swrast_clear_depth_buffer()",
610 _mesa_get_format_name(rb->Format));
611 }
612
613 ctx->Driver.UnmapRenderbuffer(ctx, rb);
614 }
615
616
617
618
619 /**
620 * Clear both depth and stencil values in a combined depth+stencil buffer.
621 */
622 void
623 _swrast_clear_depth_stencil_buffer(struct gl_context *ctx)
624 {
625 const GLubyte stencilBits = ctx->DrawBuffer->Visual.stencilBits;
626 const GLuint writeMask = ctx->Stencil.WriteMask[0];
627 const GLuint stencilMax = (1 << stencilBits) - 1;
628 struct gl_renderbuffer *rb =
629 ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
630 GLint x, y, width, height;
631 GLbitfield mapMode;
632 GLubyte *map;
633 GLint rowStride, i, j;
634
635 /* check that we really have a combined depth+stencil buffer */
636 assert(rb == ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer);
637
638 /* compute region to clear */
639 x = ctx->DrawBuffer->_Xmin;
640 y = ctx->DrawBuffer->_Ymin;
641 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
642 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
643
644 mapMode = GL_MAP_WRITE_BIT;
645 if ((writeMask & stencilMax) != stencilMax) {
646 /* need to mask stencil values */
647 mapMode |= GL_MAP_READ_BIT;
648 }
649
650 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height,
651 mapMode, &map, &rowStride);
652 if (!map) {
653 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClear(depth+stencil)");
654 return;
655 }
656
657 switch (rb->Format) {
658 case MESA_FORMAT_S8_Z24:
659 case MESA_FORMAT_Z24_S8:
660 {
661 GLfloat zClear = (GLfloat) ctx->Depth.Clear;
662 GLuint clear = 0, mask;
663
664 _mesa_pack_float_z_row(rb->Format, 1, &zClear, &clear);
665
666 if (rb->Format == MESA_FORMAT_S8_Z24) {
667 mask = ((~writeMask) & 0xff) << 24;
668 clear |= (ctx->Stencil.Clear & writeMask & 0xff) << 24;
669 }
670 else {
671 mask = ((~writeMask) & 0xff);
672 clear |= (ctx->Stencil.Clear & writeMask & 0xff);
673 }
674
675 for (i = 0; i < height; i++) {
676 GLuint *row = (GLuint *) map;
677 if (mask != 0x0) {
678 for (j = 0; j < width; j++) {
679 row[j] = (row[j] & mask) | clear;
680 }
681 }
682 else {
683 for (j = 0; j < width; j++) {
684 row[j] = clear;
685 }
686 }
687 map += rowStride;
688 }
689 }
690 break;
691 case MESA_FORMAT_Z32_FLOAT_X24S8:
692 /* XXX untested */
693 {
694 const GLfloat zClear = (GLfloat) ctx->Depth.Clear;
695 const GLuint sClear = ctx->Stencil.Clear & writeMask;
696 const GLuint sMask = (~writeMask) & 0xff;
697 for (i = 0; i < height; i++) {
698 GLfloat *zRow = (GLfloat *) map;
699 GLuint *sRow = (GLuint *) map;
700 for (j = 0; j < width; j++) {
701 zRow[j * 2 + 0] = zClear;
702 }
703 if (sMask != 0) {
704 for (j = 0; j < width; j++) {
705 sRow[j * 2 + 1] = (sRow[j * 2 + 1] & sMask) | sClear;
706 }
707 }
708 else {
709 for (j = 0; j < width; j++) {
710 sRow[j * 2 + 1] = sClear;
711 }
712 }
713 map += rowStride;
714 }
715 }
716 break;
717 default:
718 _mesa_problem(ctx, "Unexpected depth buffer format %s"
719 " in _swrast_clear_depth_buffer()",
720 _mesa_get_format_name(rb->Format));
721 }
722
723 ctx->Driver.UnmapRenderbuffer(ctx, rb);
724
725 }