1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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
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
depth_test_span16(struct gl_context * ctx,GLuint n,GLushort zbuffer[],const GLuint zfrag[],GLubyte mask[])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
depth_test_span32(struct gl_context * ctx,GLuint n,GLuint zbuffer[],const GLuint zfrag[],GLubyte mask[])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
_swrast_depth_clamp_span(struct gl_context * ctx,SWspan * span)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->ViewportArray[0].Near < ctx->ViewportArray[0].Far) {
175 min_f = ctx->ViewportArray[0].Near;
176 max_f = ctx->ViewportArray[0].Far;
177 } else {
178 min_f = ctx->ViewportArray[0].Far;
179 max_f = ctx->ViewportArray[0].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 * Note: the returned values are always in the range [0, 2^32-1].
209 */
210 static void
get_z32_values(struct gl_context * ctx,struct gl_renderbuffer * rb,GLuint count,const GLint x[],const GLint y[],GLuint zbuffer[])211 get_z32_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
212 GLuint count, const GLint x[], const GLint y[],
213 GLuint zbuffer[])
214 {
215 struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
216 const GLint w = rb->Width, h = rb->Height;
217 const GLubyte *map = _swrast_pixel_address(rb, 0, 0);
218 GLuint i;
219
220 if (rb->Format == MESA_FORMAT_Z_UNORM32) {
221 const GLint rowStride = srb->RowStride;
222 for (i = 0; i < count; i++) {
223 if (x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
224 zbuffer[i] = *((GLuint *) (map + y[i] * rowStride + x[i] * 4));
225 }
226 }
227 }
228 else {
229 const GLint bpp = _mesa_get_format_bytes(rb->Format);
230 const GLint rowStride = srb->RowStride;
231 for (i = 0; i < count; i++) {
232 if (x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
233 const GLubyte *src = map + y[i] * rowStride+ x[i] * bpp;
234 _mesa_unpack_uint_z_row(rb->Format, 1, src, &zbuffer[i]);
235 }
236 }
237 }
238 }
239
240
241 /** Helper struct for MESA_FORMAT_Z32_FLOAT_S8X24_UINT */
242 struct z32f_x24s8
243 {
244 float z;
245 uint32_t x24s8;
246 };
247
248
249 /**
250 ** Pack uint Z pixels. The incoming src value is always in
251 ** the range [0, 2^32-1].
252 **/
253
254 static void
pack_uint_S8_UINT_Z24_UNORM(const uint32_t * src,void * dst)255 pack_uint_S8_UINT_Z24_UNORM(const uint32_t *src, void *dst)
256 {
257 /* don't disturb the stencil values */
258 uint32_t *d = ((uint32_t *) dst);
259 uint32_t s = *d & 0xff;
260 uint32_t z = *src & 0xffffff00;
261 *d = z | s;
262 }
263
264 static void
pack_uint_Z24_UNORM_S8_UINT(const uint32_t * src,void * dst)265 pack_uint_Z24_UNORM_S8_UINT(const uint32_t *src, void *dst)
266 {
267 /* don't disturb the stencil values */
268 uint32_t *d = ((uint32_t *) dst);
269 uint32_t s = *d & 0xff000000;
270 uint32_t z = *src >> 8;
271 *d = s | z;
272 }
273
274 static void
pack_uint_Z_UNORM16(const uint32_t * src,void * dst)275 pack_uint_Z_UNORM16(const uint32_t *src, void *dst)
276 {
277 uint16_t *d = ((uint16_t *) dst);
278 *d = *src >> 16;
279 }
280
281 static void
pack_uint_Z_UNORM32(const uint32_t * src,void * dst)282 pack_uint_Z_UNORM32(const uint32_t *src, void *dst)
283 {
284 uint32_t *d = ((uint32_t *) dst);
285 *d = *src;
286 }
287
288 /**
289 ** Pack uint to Z_FLOAT32 or Z_FLOAT32_X24S8.
290 **/
291
292 static void
pack_uint_Z_FLOAT32(const uint32_t * src,void * dst)293 pack_uint_Z_FLOAT32(const uint32_t *src, void *dst)
294 {
295 float *d = ((float *) dst);
296 const double scale = 1.0 / (double) 0xffffffff;
297 *d = (float) (*src * scale);
298 assert(*d >= 0.0f);
299 assert(*d <= 1.0f);
300 }
301
302 /** Pack a uint32_t Z value to dest address */
303 typedef void (*mesa_pack_uint_z_func)(const uint32_t *src, void *dst);
304
305 static mesa_pack_uint_z_func
get_pack_uint_z_func(mesa_format format)306 get_pack_uint_z_func(mesa_format format)
307 {
308 switch (format) {
309 case MESA_FORMAT_S8_UINT_Z24_UNORM:
310 case MESA_FORMAT_X8_UINT_Z24_UNORM:
311 return pack_uint_S8_UINT_Z24_UNORM;
312 case MESA_FORMAT_Z24_UNORM_S8_UINT:
313 case MESA_FORMAT_Z24_UNORM_X8_UINT:
314 return pack_uint_Z24_UNORM_S8_UINT;
315 case MESA_FORMAT_Z_UNORM16:
316 return pack_uint_Z_UNORM16;
317 case MESA_FORMAT_Z_UNORM32:
318 return pack_uint_Z_UNORM32;
319 case MESA_FORMAT_Z_FLOAT32:
320 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
321 return pack_uint_Z_FLOAT32;
322 default:
323 unreachable("unexpected format in get_pack_uint_z_func()");
324 }
325 }
326
327 /**
328 * Put an array of 32-bit z values into the depth buffer.
329 * Note: the z values are always in the range [0, 2^32-1].
330 */
331 static void
put_z32_values(struct gl_context * ctx,struct gl_renderbuffer * rb,GLuint count,const GLint x[],const GLint y[],const GLuint zvalues[],const GLubyte mask[])332 put_z32_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
333 GLuint count, const GLint x[], const GLint y[],
334 const GLuint zvalues[], const GLubyte mask[])
335 {
336 struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
337 const GLint w = rb->Width, h = rb->Height;
338 GLubyte *map = _swrast_pixel_address(rb, 0, 0);
339 GLuint i;
340
341 if (rb->Format == MESA_FORMAT_Z_UNORM32) {
342 const GLint rowStride = srb->RowStride;
343 for (i = 0; i < count; i++) {
344 if (mask[i] && x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
345 GLuint *dst = (GLuint *) (map + y[i] * rowStride + x[i] * 4);
346 *dst = zvalues[i];
347 }
348 }
349 }
350 else {
351 mesa_pack_uint_z_func packZ = get_pack_uint_z_func(rb->Format);
352 const GLint bpp = _mesa_get_format_bytes(rb->Format);
353 const GLint rowStride = srb->RowStride;
354 for (i = 0; i < count; i++) {
355 if (mask[i] && x[i] >= 0 && y[i] >= 0 && x[i] < w && y[i] < h) {
356 void *dst = map + y[i] * rowStride + x[i] * bpp;
357 packZ(zvalues + i, dst);
358 }
359 }
360 }
361 }
362
363
364 /**
365 * Apply depth (Z) buffer testing to the span.
366 * \return approx number of pixels that passed (only zero is reliable)
367 */
368 GLuint
_swrast_depth_test_span(struct gl_context * ctx,SWspan * span)369 _swrast_depth_test_span(struct gl_context *ctx, SWspan *span)
370 {
371 struct gl_framebuffer *fb = ctx->DrawBuffer;
372 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
373 const GLint bpp = _mesa_get_format_bytes(rb->Format);
374 void *zStart;
375 const GLuint count = span->end;
376 const GLuint *fragZ = span->array->z;
377 GLubyte *mask = span->array->mask;
378 void *zBufferVals;
379 GLuint *zBufferTemp = NULL;
380 GLuint passed;
381 GLuint zBits = _mesa_get_format_bits(rb->Format, GL_DEPTH_BITS);
382 GLboolean ztest16 = GL_FALSE;
383
384 if (span->arrayMask & SPAN_XY)
385 zStart = NULL;
386 else
387 zStart = _swrast_pixel_address(rb, span->x, span->y);
388
389 if (rb->Format == MESA_FORMAT_Z_UNORM16 && !(span->arrayMask & SPAN_XY)) {
390 /* directly read/write row of 16-bit Z values */
391 zBufferVals = zStart;
392 ztest16 = GL_TRUE;
393 }
394 else if (rb->Format == MESA_FORMAT_Z_UNORM32 && !(span->arrayMask & SPAN_XY)) {
395 /* directly read/write row of 32-bit Z values */
396 zBufferVals = zStart;
397 }
398 else {
399 /* copy Z buffer values into temp buffer (32-bit Z values) */
400 zBufferTemp = malloc(count * sizeof(GLuint));
401 if (!zBufferTemp)
402 return 0;
403
404 if (span->arrayMask & SPAN_XY) {
405 get_z32_values(ctx, rb, count,
406 span->array->x, span->array->y, zBufferTemp);
407 }
408 else {
409 _mesa_unpack_uint_z_row(rb->Format, count, zStart, zBufferTemp);
410 }
411
412 if (zBits == 24) {
413 GLuint i;
414 /* Convert depth buffer values from 32 to 24 bits to match the
415 * fragment Z values generated by rasterization.
416 */
417 for (i = 0; i < count; i++) {
418 zBufferTemp[i] >>= 8;
419 }
420 }
421 else if (zBits == 16) {
422 GLuint i;
423 /* Convert depth buffer values from 32 to 16 bits */
424 for (i = 0; i < count; i++) {
425 zBufferTemp[i] >>= 16;
426 }
427 }
428 else {
429 assert(zBits == 32);
430 }
431
432 zBufferVals = zBufferTemp;
433 }
434
435 /* do the depth test either with 16 or 32-bit values */
436 if (ztest16)
437 passed = depth_test_span16(ctx, count, zBufferVals, fragZ, mask);
438 else
439 passed = depth_test_span32(ctx, count, zBufferVals, fragZ, mask);
440
441 if (zBufferTemp) {
442 /* need to write temp Z values back into the buffer */
443
444 /* Convert depth buffer values back to 32-bit values. The least
445 * significant bits don't matter since they'll get dropped when
446 * they're packed back into the depth buffer.
447 */
448 if (zBits == 24) {
449 GLuint i;
450 for (i = 0; i < count; i++) {
451 zBufferTemp[i] = (zBufferTemp[i] << 8);
452 }
453 }
454 else if (zBits == 16) {
455 GLuint i;
456 for (i = 0; i < count; i++) {
457 zBufferTemp[i] = zBufferTemp[i] << 16;
458 }
459 }
460
461 if (span->arrayMask & SPAN_XY) {
462 /* random locations */
463 put_z32_values(ctx, rb, count, span->array->x, span->array->y,
464 zBufferTemp, mask);
465 }
466 else {
467 /* horizontal row */
468 mesa_pack_uint_z_func packZ = get_pack_uint_z_func(rb->Format);
469 GLubyte *dst = zStart;
470 GLuint i;
471 for (i = 0; i < count; i++) {
472 if (mask[i]) {
473 packZ(&zBufferTemp[i], dst);
474 }
475 dst += bpp;
476 }
477 }
478
479 free(zBufferTemp);
480 }
481
482 if (passed < count) {
483 span->writeAll = GL_FALSE;
484 }
485 return passed;
486 }
487
488
489 /**
490 * GL_EXT_depth_bounds_test extension.
491 * Discard fragments depending on whether the corresponding Z-buffer
492 * values are outside the depth bounds test range.
493 * Note: we test the Z buffer values, not the fragment Z values!
494 * \return GL_TRUE if any fragments pass, GL_FALSE if no fragments pass
495 */
496 GLboolean
_swrast_depth_bounds_test(struct gl_context * ctx,SWspan * span)497 _swrast_depth_bounds_test( struct gl_context *ctx, SWspan *span )
498 {
499 struct gl_framebuffer *fb = ctx->DrawBuffer;
500 struct gl_renderbuffer *rb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
501 GLubyte *zStart;
502 GLuint zMin = (GLuint)((double)ctx->Depth.BoundsMin * 0xffffffff);
503 GLuint zMax = (GLuint)((double)ctx->Depth.BoundsMax * 0xffffffff);
504 GLubyte *mask = span->array->mask;
505 const GLuint count = span->end;
506 GLuint i;
507 GLboolean anyPass = GL_FALSE;
508 GLuint *zBufferTemp;
509 const GLuint *zBufferVals;
510
511 zBufferTemp = malloc(count * sizeof(GLuint));
512 if (!zBufferTemp) {
513 /* don't generate a stream of OUT_OF_MEMORY errors here */
514 return GL_FALSE;
515 }
516
517 if (span->arrayMask & SPAN_XY)
518 zStart = NULL;
519 else
520 zStart = _swrast_pixel_address(rb, span->x, span->y);
521
522 if (rb->Format == MESA_FORMAT_Z_UNORM32 && !(span->arrayMask & SPAN_XY)) {
523 /* directly access 32-bit values in the depth buffer */
524 zBufferVals = (const GLuint *) zStart;
525 }
526 else {
527 /* Round the bounds to the precision of the zbuffer. */
528 if (rb->Format == MESA_FORMAT_Z_UNORM16) {
529 zMin = (zMin & 0xffff0000) | (zMin >> 16);
530 zMax = (zMax & 0xffff0000) | (zMax >> 16);
531 } else {
532 /* 24 bits */
533 zMin = (zMin & 0xffffff00) | (zMin >> 24);
534 zMax = (zMax & 0xffffff00) | (zMax >> 24);
535 }
536
537 /* unpack Z values into a temporary array */
538 if (span->arrayMask & SPAN_XY) {
539 get_z32_values(ctx, rb, count, span->array->x, span->array->y,
540 zBufferTemp);
541 }
542 else {
543 _mesa_unpack_uint_z_row(rb->Format, count, zStart, zBufferTemp);
544 }
545 zBufferVals = zBufferTemp;
546 }
547
548 /* Now do the tests */
549 for (i = 0; i < count; i++) {
550 if (mask[i]) {
551 if (zBufferVals[i] < zMin || zBufferVals[i] > zMax)
552 mask[i] = GL_FALSE;
553 else
554 anyPass = GL_TRUE;
555 }
556 }
557
558 free(zBufferTemp);
559
560 return anyPass;
561 }
562
563
564
565 /**********************************************************************/
566 /***** Read Depth Buffer *****/
567 /**********************************************************************/
568
569
570 /**
571 * Read a span of depth values from the given depth renderbuffer, returning
572 * the values as GLfloats.
573 * This function does clipping to prevent reading outside the depth buffer's
574 * bounds.
575 */
576 void
_swrast_read_depth_span_float(struct gl_context * ctx,struct gl_renderbuffer * rb,GLint n,GLint x,GLint y,GLfloat depth[])577 _swrast_read_depth_span_float(struct gl_context *ctx,
578 struct gl_renderbuffer *rb,
579 GLint n, GLint x, GLint y, GLfloat depth[])
580 {
581 if (!rb) {
582 /* really only doing this to prevent FP exceptions later */
583 memset(depth, 0, n * sizeof(GLfloat));
584 return;
585 }
586
587 if (y < 0 || y >= (GLint) rb->Height ||
588 x + n <= 0 || x >= (GLint) rb->Width) {
589 /* span is completely outside framebuffer */
590 memset(depth, 0, n * sizeof(GLfloat));
591 return;
592 }
593
594 if (x < 0) {
595 GLint dx = -x;
596 GLint i;
597 for (i = 0; i < dx; i++)
598 depth[i] = 0.0;
599 x = 0;
600 n -= dx;
601 depth += dx;
602 }
603 if (x + n > (GLint) rb->Width) {
604 GLint dx = x + n - (GLint) rb->Width;
605 GLint i;
606 for (i = 0; i < dx; i++)
607 depth[n - i - 1] = 0.0;
608 n -= dx;
609 }
610 if (n <= 0) {
611 return;
612 }
613
614 _mesa_unpack_float_z_row(rb->Format, n, _swrast_pixel_address(rb, x, y),
615 depth);
616 }
617
618
619 /**
620 * Clear the given z/depth renderbuffer. If the buffer is a combined
621 * depth+stencil buffer, only the Z bits will be touched.
622 */
623 void
_swrast_clear_depth_buffer(struct gl_context * ctx)624 _swrast_clear_depth_buffer(struct gl_context *ctx)
625 {
626 struct gl_renderbuffer *rb =
627 ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
628 GLint x, y, width, height;
629 GLubyte *map;
630 GLint rowStride, i, j;
631 GLbitfield mapMode;
632
633 if (!rb || !ctx->Depth.Mask) {
634 /* no depth buffer, or writing to it is disabled */
635 return;
636 }
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 (rb->Format == MESA_FORMAT_Z24_UNORM_S8_UINT ||
646 rb->Format == MESA_FORMAT_Z24_UNORM_X8_UINT ||
647 rb->Format == MESA_FORMAT_S8_UINT_Z24_UNORM ||
648 rb->Format == MESA_FORMAT_X8_UINT_Z24_UNORM) {
649 mapMode |= GL_MAP_READ_BIT;
650 }
651
652 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height,
653 mapMode, &map, &rowStride,
654 ctx->DrawBuffer->FlipY);
655 if (!map) {
656 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClear(depth)");
657 return;
658 }
659
660 switch (rb->Format) {
661 case MESA_FORMAT_Z_UNORM16:
662 {
663 GLfloat clear = (GLfloat) ctx->Depth.Clear;
664 GLushort clearVal = 0;
665 _mesa_pack_float_z_row(rb->Format, 1, &clear, &clearVal);
666 if (clearVal == 0xffff && width * 2 == rowStride) {
667 /* common case */
668 memset(map, 0xff, width * height * 2);
669 }
670 else {
671 for (i = 0; i < height; i++) {
672 GLushort *row = (GLushort *) map;
673 for (j = 0; j < width; j++) {
674 row[j] = clearVal;
675 }
676 map += rowStride;
677 }
678 }
679 }
680 break;
681 case MESA_FORMAT_Z_UNORM32:
682 case MESA_FORMAT_Z_FLOAT32:
683 {
684 GLfloat clear = (GLfloat) ctx->Depth.Clear;
685 GLuint clearVal = 0;
686 _mesa_pack_float_z_row(rb->Format, 1, &clear, &clearVal);
687 for (i = 0; i < height; i++) {
688 GLuint *row = (GLuint *) map;
689 for (j = 0; j < width; j++) {
690 row[j] = clearVal;
691 }
692 map += rowStride;
693 }
694 }
695 break;
696 case MESA_FORMAT_Z24_UNORM_S8_UINT:
697 case MESA_FORMAT_Z24_UNORM_X8_UINT:
698 case MESA_FORMAT_S8_UINT_Z24_UNORM:
699 case MESA_FORMAT_X8_UINT_Z24_UNORM:
700 {
701 GLfloat clear = (GLfloat) ctx->Depth.Clear;
702 GLuint clearVal = 0;
703 GLuint mask;
704
705 if (rb->Format == MESA_FORMAT_Z24_UNORM_S8_UINT ||
706 rb->Format == MESA_FORMAT_Z24_UNORM_X8_UINT)
707 mask = 0xff000000;
708 else
709 mask = 0xff;
710
711 _mesa_pack_float_z_row(rb->Format, 1, &clear, &clearVal);
712 for (i = 0; i < height; i++) {
713 GLuint *row = (GLuint *) map;
714 for (j = 0; j < width; j++) {
715 row[j] = (row[j] & mask) | clearVal;
716 }
717 map += rowStride;
718 }
719
720 }
721 break;
722 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
723 /* XXX untested */
724 {
725 GLfloat clearVal = (GLfloat) ctx->Depth.Clear;
726 for (i = 0; i < height; i++) {
727 GLfloat *row = (GLfloat *) map;
728 for (j = 0; j < width; j++) {
729 row[j * 2] = clearVal;
730 }
731 map += rowStride;
732 }
733 }
734 break;
735 default:
736 _mesa_problem(ctx, "Unexpected depth buffer format %s"
737 " in _swrast_clear_depth_buffer()",
738 _mesa_get_format_name(rb->Format));
739 }
740
741 ctx->Driver.UnmapRenderbuffer(ctx, rb);
742 }
743
744
745
746
747 /**
748 * Clear both depth and stencil values in a combined depth+stencil buffer.
749 */
750 void
_swrast_clear_depth_stencil_buffer(struct gl_context * ctx)751 _swrast_clear_depth_stencil_buffer(struct gl_context *ctx)
752 {
753 const GLubyte stencilBits = ctx->DrawBuffer->Visual.stencilBits;
754 const GLuint writeMask = ctx->Stencil.WriteMask[0];
755 const GLuint stencilMax = (1 << stencilBits) - 1;
756 struct gl_renderbuffer *rb =
757 ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
758 GLint x, y, width, height;
759 GLbitfield mapMode;
760 GLubyte *map;
761 GLint rowStride, i, j;
762
763 /* check that we really have a combined depth+stencil buffer */
764 assert(rb == ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer);
765
766 /* compute region to clear */
767 x = ctx->DrawBuffer->_Xmin;
768 y = ctx->DrawBuffer->_Ymin;
769 width = ctx->DrawBuffer->_Xmax - ctx->DrawBuffer->_Xmin;
770 height = ctx->DrawBuffer->_Ymax - ctx->DrawBuffer->_Ymin;
771
772 mapMode = GL_MAP_WRITE_BIT;
773 if ((writeMask & stencilMax) != stencilMax) {
774 /* need to mask stencil values */
775 mapMode |= GL_MAP_READ_BIT;
776 }
777
778 ctx->Driver.MapRenderbuffer(ctx, rb, x, y, width, height,
779 mapMode, &map, &rowStride,
780 ctx->DrawBuffer->FlipY);
781 if (!map) {
782 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClear(depth+stencil)");
783 return;
784 }
785
786 switch (rb->Format) {
787 case MESA_FORMAT_Z24_UNORM_S8_UINT:
788 case MESA_FORMAT_S8_UINT_Z24_UNORM:
789 {
790 GLfloat zClear = (GLfloat) ctx->Depth.Clear;
791 GLuint clear = 0, mask;
792
793 _mesa_pack_float_z_row(rb->Format, 1, &zClear, &clear);
794
795 if (rb->Format == MESA_FORMAT_Z24_UNORM_S8_UINT) {
796 mask = ((~writeMask) & 0xff) << 24;
797 clear |= (ctx->Stencil.Clear & writeMask & 0xff) << 24;
798 }
799 else {
800 mask = ((~writeMask) & 0xff);
801 clear |= (ctx->Stencil.Clear & writeMask & 0xff);
802 }
803
804 for (i = 0; i < height; i++) {
805 GLuint *row = (GLuint *) map;
806 if (mask != 0x0) {
807 for (j = 0; j < width; j++) {
808 row[j] = (row[j] & mask) | clear;
809 }
810 }
811 else {
812 for (j = 0; j < width; j++) {
813 row[j] = clear;
814 }
815 }
816 map += rowStride;
817 }
818 }
819 break;
820 case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:
821 /* XXX untested */
822 {
823 const GLfloat zClear = (GLfloat) ctx->Depth.Clear;
824 const GLuint sClear = ctx->Stencil.Clear & writeMask;
825 const GLuint sMask = (~writeMask) & 0xff;
826 for (i = 0; i < height; i++) {
827 GLfloat *zRow = (GLfloat *) map;
828 GLuint *sRow = (GLuint *) map;
829 for (j = 0; j < width; j++) {
830 zRow[j * 2 + 0] = zClear;
831 }
832 if (sMask != 0) {
833 for (j = 0; j < width; j++) {
834 sRow[j * 2 + 1] = (sRow[j * 2 + 1] & sMask) | sClear;
835 }
836 }
837 else {
838 for (j = 0; j < width; j++) {
839 sRow[j * 2 + 1] = sClear;
840 }
841 }
842 map += rowStride;
843 }
844 }
845 break;
846 default:
847 _mesa_problem(ctx, "Unexpected depth buffer format %s"
848 " in _swrast_clear_depth_buffer()",
849 _mesa_get_format_name(rb->Format));
850 }
851
852 ctx->Driver.UnmapRenderbuffer(ctx, rb);
853
854 }
855