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/format_unpack.h"
30 #include "main/format_pack.h"
31 #include "main/macros.h"
32 #include "main/imports.h"
34 #include "s_context.h"
40 #define Z_TEST(COMPARE) \
43 for (i = 0; i < n; i++) { \
48 zbuffer[i] = zfrag[i]; \
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.
69 depth_test_span16( struct gl_context
*ctx
, GLuint n
,
70 GLushort zbuffer
[], const GLuint zfrag
[], GLubyte mask
[] )
72 const GLboolean write
= ctx
->Depth
.Mask
;
75 /* switch cases ordered from most frequent to less frequent */
76 switch (ctx
->Depth
.Func
) {
78 Z_TEST(zfrag
[i
] < zbuffer
[i
]);
81 Z_TEST(zfrag
[i
] <= zbuffer
[i
]);
84 Z_TEST(zfrag
[i
] >= zbuffer
[i
]);
87 Z_TEST(zfrag
[i
] > zbuffer
[i
]);
90 Z_TEST(zfrag
[i
] != zbuffer
[i
]);
93 Z_TEST(zfrag
[i
] == zbuffer
[i
]);
99 memset(mask
, 0, n
* sizeof(GLubyte
));
102 _mesa_problem(ctx
, "Bad depth func in depth_test_span16");
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.
117 depth_test_span32( struct gl_context
*ctx
, GLuint n
,
118 GLuint zbuffer
[], const GLuint zfrag
[], GLubyte mask
[])
120 const GLboolean write
= ctx
->Depth
.Mask
;
123 /* switch cases ordered from most frequent to less frequent */
124 switch (ctx
->Depth
.Func
) {
126 Z_TEST(zfrag
[i
] < zbuffer
[i
]);
129 Z_TEST(zfrag
[i
] <= zbuffer
[i
]);
132 Z_TEST(zfrag
[i
] >= zbuffer
[i
]);
135 Z_TEST(zfrag
[i
] > zbuffer
[i
]);
138 Z_TEST(zfrag
[i
] != zbuffer
[i
]);
141 Z_TEST(zfrag
[i
] == zbuffer
[i
]);
147 memset(mask
, 0, n
* sizeof(GLubyte
));
150 _mesa_problem(ctx
, "Bad depth func in depth_test_span32");
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
165 _swrast_depth_clamp_span( struct gl_context
*ctx
, SWspan
*span
)
167 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
168 const GLuint count
= span
->end
;
169 GLint
*zValues
= (GLint
*) span
->array
->z
; /* sign change */
171 GLfloat min_f
, max_f
;
174 if (ctx
->Viewport
.Near
< ctx
->Viewport
.Far
) {
175 min_f
= ctx
->Viewport
.Near
;
176 max_f
= ctx
->Viewport
.Far
;
178 min_f
= ctx
->Viewport
.Far
;
179 max_f
= ctx
->Viewport
.Near
;
182 /* Convert floating point values in [0,1] to device Z coordinates in
184 * ex: If the Z buffer has 24 bits, DepthMax = 0xffffff.
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.
190 min
= (GLint
) (min_f
* fb
->_DepthMaxF
);
191 max
= (GLint
) (max_f
* fb
->_DepthMaxF
);
193 max
= 0x7fffffff; /* catch over flow for 30-bit z */
195 /* Note that we do the comparisons here using signed integers.
197 for (i
= 0; i
< count
; i
++) {
198 if (zValues
[i
] < min
)
200 if (zValues
[i
] > max
)
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].
211 get_z32_values(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
212 GLuint count
, const GLint x
[], const GLint y
[],
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);
220 if (rb
->Format
== MESA_FORMAT_Z32
) {
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));
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
]);
242 * Put an array of 32-bit z values into the depth buffer.
243 * Note: the z values are always in the range [0, 2^32-1].
246 put_z32_values(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
247 GLuint count
, const GLint x
[], const GLint y
[],
248 const GLuint zvalues
[], const GLubyte mask
[])
250 struct swrast_renderbuffer
*srb
= swrast_renderbuffer(rb
);
251 const GLint w
= rb
->Width
, h
= rb
->Height
;
252 GLubyte
*map
= _swrast_pixel_address(rb
, 0, 0);
255 if (rb
->Format
== MESA_FORMAT_Z32
) {
256 const GLint rowStride
= srb
->RowStride
;
257 for (i
= 0; i
< count
; i
++) {
258 if (mask
[i
] && x
[i
] >= 0 && y
[i
] >= 0 && x
[i
] < w
&& y
[i
] < h
) {
259 GLuint
*dst
= (GLuint
*) (map
+ y
[i
] * rowStride
+ x
[i
] * 4);
265 gl_pack_uint_z_func packZ
= _mesa_get_pack_uint_z_func(rb
->Format
);
266 const GLint bpp
= _mesa_get_format_bytes(rb
->Format
);
267 const GLint rowStride
= srb
->RowStride
;
268 for (i
= 0; i
< count
; i
++) {
269 if (mask
[i
] && x
[i
] >= 0 && y
[i
] >= 0 && x
[i
] < w
&& y
[i
] < h
) {
270 void *dst
= map
+ y
[i
] * rowStride
+ x
[i
] * bpp
;
271 packZ(zvalues
+ i
, dst
);
279 * Apply depth (Z) buffer testing to the span.
280 * \return approx number of pixels that passed (only zero is reliable)
283 _swrast_depth_test_span(struct gl_context
*ctx
, SWspan
*span
)
285 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
286 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
287 const GLint bpp
= _mesa_get_format_bytes(rb
->Format
);
289 const GLuint count
= span
->end
;
290 const GLuint
*fragZ
= span
->array
->z
;
291 GLubyte
*mask
= span
->array
->mask
;
293 GLuint
*zBufferTemp
= NULL
;
295 GLuint zBits
= _mesa_get_format_bits(rb
->Format
, GL_DEPTH_BITS
);
296 GLboolean ztest16
= GL_FALSE
;
298 if (span
->arrayMask
& SPAN_XY
)
301 zStart
= _swrast_pixel_address(rb
, span
->x
, span
->y
);
303 if (rb
->Format
== MESA_FORMAT_Z16
&& !(span
->arrayMask
& SPAN_XY
)) {
304 /* directly read/write row of 16-bit Z values */
305 zBufferVals
= zStart
;
308 else if (rb
->Format
== MESA_FORMAT_Z32
&& !(span
->arrayMask
& SPAN_XY
)) {
309 /* directly read/write row of 32-bit Z values */
310 zBufferVals
= zStart
;
313 /* copy Z buffer values into temp buffer (32-bit Z values) */
314 zBufferTemp
= malloc(count
* sizeof(GLuint
));
318 if (span
->arrayMask
& SPAN_XY
) {
319 get_z32_values(ctx
, rb
, count
,
320 span
->array
->x
, span
->array
->y
, zBufferTemp
);
323 _mesa_unpack_uint_z_row(rb
->Format
, count
, zStart
, zBufferTemp
);
328 /* Convert depth buffer values from 32 to 24 bits to match the
329 * fragment Z values generated by rasterization.
331 for (i
= 0; i
< count
; i
++) {
332 zBufferTemp
[i
] >>= 8;
335 else if (zBits
== 16) {
337 /* Convert depth buffer values from 32 to 16 bits */
338 for (i
= 0; i
< count
; i
++) {
339 zBufferTemp
[i
] >>= 16;
346 zBufferVals
= zBufferTemp
;
349 /* do the depth test either with 16 or 32-bit values */
351 passed
= depth_test_span16(ctx
, count
, zBufferVals
, fragZ
, mask
);
353 passed
= depth_test_span32(ctx
, count
, zBufferVals
, fragZ
, mask
);
356 /* need to write temp Z values back into the buffer */
358 /* Convert depth buffer values back to 32-bit values. The least
359 * significant bits don't matter since they'll get dropped when
360 * they're packed back into the depth buffer.
364 for (i
= 0; i
< count
; i
++) {
365 zBufferTemp
[i
] = (zBufferTemp
[i
] << 8);
368 else if (zBits
== 16) {
370 for (i
= 0; i
< count
; i
++) {
371 zBufferTemp
[i
] = zBufferTemp
[i
] << 16;
375 if (span
->arrayMask
& SPAN_XY
) {
376 /* random locations */
377 put_z32_values(ctx
, rb
, count
, span
->array
->x
, span
->array
->y
,
382 gl_pack_uint_z_func packZ
= _mesa_get_pack_uint_z_func(rb
->Format
);
383 GLubyte
*dst
= zStart
;
385 for (i
= 0; i
< count
; i
++) {
387 packZ(&zBufferTemp
[i
], dst
);
396 if (passed
< count
) {
397 span
->writeAll
= GL_FALSE
;
404 * GL_EXT_depth_bounds_test extension.
405 * Discard fragments depending on whether the corresponding Z-buffer
406 * values are outside the depth bounds test range.
407 * Note: we test the Z buffer values, not the fragment Z values!
408 * \return GL_TRUE if any fragments pass, GL_FALSE if no fragments pass
411 _swrast_depth_bounds_test( struct gl_context
*ctx
, SWspan
*span
)
413 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
414 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
416 GLuint zMin
= (GLuint
) (ctx
->Depth
.BoundsMin
* fb
->_DepthMaxF
+ 0.5F
);
417 GLuint zMax
= (GLuint
) (ctx
->Depth
.BoundsMax
* fb
->_DepthMaxF
+ 0.5F
);
418 GLubyte
*mask
= span
->array
->mask
;
419 const GLuint count
= span
->end
;
421 GLboolean anyPass
= GL_FALSE
;
423 const GLuint
*zBufferVals
;
425 zBufferTemp
= malloc(count
* sizeof(GLuint
));
427 /* don't generate a stream of OUT_OF_MEMORY errors here */
431 if (span
->arrayMask
& SPAN_XY
)
434 zStart
= _swrast_pixel_address(rb
, span
->x
, span
->y
);
436 if (rb
->Format
== MESA_FORMAT_Z32
&& !(span
->arrayMask
& SPAN_XY
)) {
437 /* directly access 32-bit values in the depth buffer */
438 zBufferVals
= (const GLuint
*) zStart
;
441 /* unpack Z values into a temporary array */
442 if (span
->arrayMask
& SPAN_XY
) {
443 get_z32_values(ctx
, rb
, count
, span
->array
->x
, span
->array
->y
,
447 _mesa_unpack_uint_z_row(rb
->Format
, count
, zStart
, zBufferTemp
);
449 zBufferVals
= zBufferTemp
;
452 /* Now do the tests */
453 for (i
= 0; i
< count
; i
++) {
455 if (zBufferVals
[i
] < zMin
|| zBufferVals
[i
] > zMax
)
469 /**********************************************************************/
470 /***** Read Depth Buffer *****/
471 /**********************************************************************/
475 * Read a span of depth values from the given depth renderbuffer, returning
476 * the values as GLfloats.
477 * This function does clipping to prevent reading outside the depth buffer's
481 _swrast_read_depth_span_float(struct gl_context
*ctx
,
482 struct gl_renderbuffer
*rb
,
483 GLint n
, GLint x
, GLint y
, GLfloat depth
[])
486 /* really only doing this to prevent FP exceptions later */
487 memset(depth
, 0, n
* sizeof(GLfloat
));
491 if (y
< 0 || y
>= (GLint
) rb
->Height
||
492 x
+ n
<= 0 || x
>= (GLint
) rb
->Width
) {
493 /* span is completely outside framebuffer */
494 memset(depth
, 0, n
* sizeof(GLfloat
));
501 for (i
= 0; i
< dx
; i
++)
507 if (x
+ n
> (GLint
) rb
->Width
) {
508 GLint dx
= x
+ n
- (GLint
) rb
->Width
;
510 for (i
= 0; i
< dx
; i
++)
511 depth
[n
- i
- 1] = 0.0;
518 _mesa_unpack_float_z_row(rb
->Format
, n
, _swrast_pixel_address(rb
, x
, y
),
524 * Clear the given z/depth renderbuffer. If the buffer is a combined
525 * depth+stencil buffer, only the Z bits will be touched.
528 _swrast_clear_depth_buffer(struct gl_context
*ctx
)
530 struct gl_renderbuffer
*rb
=
531 ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
532 GLint x
, y
, width
, height
;
534 GLint rowStride
, i
, j
;
537 if (!rb
|| !ctx
->Depth
.Mask
) {
538 /* no depth buffer, or writing to it is disabled */
542 /* compute region to clear */
543 x
= ctx
->DrawBuffer
->_Xmin
;
544 y
= ctx
->DrawBuffer
->_Ymin
;
545 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
546 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
548 mapMode
= GL_MAP_WRITE_BIT
;
549 if (rb
->Format
== MESA_FORMAT_S8_Z24
||
550 rb
->Format
== MESA_FORMAT_X8_Z24
||
551 rb
->Format
== MESA_FORMAT_Z24_S8
||
552 rb
->Format
== MESA_FORMAT_Z24_X8
) {
553 mapMode
|= GL_MAP_READ_BIT
;
556 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
,
557 mapMode
, &map
, &rowStride
);
559 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClear(depth)");
563 switch (rb
->Format
) {
564 case MESA_FORMAT_Z16
:
566 GLfloat clear
= (GLfloat
) ctx
->Depth
.Clear
;
567 GLushort clearVal
= 0;
568 _mesa_pack_float_z_row(rb
->Format
, 1, &clear
, &clearVal
);
569 if (clearVal
== 0xffff && width
* 2 == rowStride
) {
571 memset(map
, 0xff, width
* height
* 2);
574 for (i
= 0; i
< height
; i
++) {
575 GLushort
*row
= (GLushort
*) map
;
576 for (j
= 0; j
< width
; j
++) {
584 case MESA_FORMAT_Z32
:
585 case MESA_FORMAT_Z32_FLOAT
:
587 GLfloat clear
= (GLfloat
) ctx
->Depth
.Clear
;
589 _mesa_pack_float_z_row(rb
->Format
, 1, &clear
, &clearVal
);
590 for (i
= 0; i
< height
; i
++) {
591 GLuint
*row
= (GLuint
*) map
;
592 for (j
= 0; j
< width
; j
++) {
599 case MESA_FORMAT_S8_Z24
:
600 case MESA_FORMAT_X8_Z24
:
601 case MESA_FORMAT_Z24_S8
:
602 case MESA_FORMAT_Z24_X8
:
604 GLfloat clear
= (GLfloat
) ctx
->Depth
.Clear
;
608 if (rb
->Format
== MESA_FORMAT_S8_Z24
||
609 rb
->Format
== MESA_FORMAT_X8_Z24
)
614 _mesa_pack_float_z_row(rb
->Format
, 1, &clear
, &clearVal
);
615 for (i
= 0; i
< height
; i
++) {
616 GLuint
*row
= (GLuint
*) map
;
617 for (j
= 0; j
< width
; j
++) {
618 row
[j
] = (row
[j
] & mask
) | clearVal
;
625 case MESA_FORMAT_Z32_FLOAT_X24S8
:
628 GLfloat clearVal
= (GLfloat
) ctx
->Depth
.Clear
;
629 for (i
= 0; i
< height
; i
++) {
630 GLfloat
*row
= (GLfloat
*) map
;
631 for (j
= 0; j
< width
; j
++) {
632 row
[j
* 2] = clearVal
;
639 _mesa_problem(ctx
, "Unexpected depth buffer format %s"
640 " in _swrast_clear_depth_buffer()",
641 _mesa_get_format_name(rb
->Format
));
644 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
651 * Clear both depth and stencil values in a combined depth+stencil buffer.
654 _swrast_clear_depth_stencil_buffer(struct gl_context
*ctx
)
656 const GLubyte stencilBits
= ctx
->DrawBuffer
->Visual
.stencilBits
;
657 const GLuint writeMask
= ctx
->Stencil
.WriteMask
[0];
658 const GLuint stencilMax
= (1 << stencilBits
) - 1;
659 struct gl_renderbuffer
*rb
=
660 ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
661 GLint x
, y
, width
, height
;
664 GLint rowStride
, i
, j
;
666 /* check that we really have a combined depth+stencil buffer */
667 assert(rb
== ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
669 /* compute region to clear */
670 x
= ctx
->DrawBuffer
->_Xmin
;
671 y
= ctx
->DrawBuffer
->_Ymin
;
672 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
673 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
675 mapMode
= GL_MAP_WRITE_BIT
;
676 if ((writeMask
& stencilMax
) != stencilMax
) {
677 /* need to mask stencil values */
678 mapMode
|= GL_MAP_READ_BIT
;
681 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
,
682 mapMode
, &map
, &rowStride
);
684 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClear(depth+stencil)");
688 switch (rb
->Format
) {
689 case MESA_FORMAT_S8_Z24
:
690 case MESA_FORMAT_Z24_S8
:
692 GLfloat zClear
= (GLfloat
) ctx
->Depth
.Clear
;
693 GLuint clear
= 0, mask
;
695 _mesa_pack_float_z_row(rb
->Format
, 1, &zClear
, &clear
);
697 if (rb
->Format
== MESA_FORMAT_S8_Z24
) {
698 mask
= ((~writeMask
) & 0xff) << 24;
699 clear
|= (ctx
->Stencil
.Clear
& writeMask
& 0xff) << 24;
702 mask
= ((~writeMask
) & 0xff);
703 clear
|= (ctx
->Stencil
.Clear
& writeMask
& 0xff);
706 for (i
= 0; i
< height
; i
++) {
707 GLuint
*row
= (GLuint
*) map
;
709 for (j
= 0; j
< width
; j
++) {
710 row
[j
] = (row
[j
] & mask
) | clear
;
714 for (j
= 0; j
< width
; j
++) {
722 case MESA_FORMAT_Z32_FLOAT_X24S8
:
725 const GLfloat zClear
= (GLfloat
) ctx
->Depth
.Clear
;
726 const GLuint sClear
= ctx
->Stencil
.Clear
& writeMask
;
727 const GLuint sMask
= (~writeMask
) & 0xff;
728 for (i
= 0; i
< height
; i
++) {
729 GLfloat
*zRow
= (GLfloat
*) map
;
730 GLuint
*sRow
= (GLuint
*) map
;
731 for (j
= 0; j
< width
; j
++) {
732 zRow
[j
* 2 + 0] = zClear
;
735 for (j
= 0; j
< width
; j
++) {
736 sRow
[j
* 2 + 1] = (sRow
[j
* 2 + 1] & sMask
) | sClear
;
740 for (j
= 0; j
< width
; j
++) {
741 sRow
[j
* 2 + 1] = sClear
;
749 _mesa_problem(ctx
, "Unexpected depth buffer format %s"
750 " in _swrast_clear_depth_buffer()",
751 _mesa_get_format_name(rb
->Format
));
754 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);