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 const GLint w
= rb
->Width
, h
= rb
->Height
;
216 const GLubyte
*map
= (const GLubyte
*) rb
->Data
;
219 if (rb
->Format
== MESA_FORMAT_Z32
) {
220 const GLint rowStride
= rb
->RowStride
* 4;
221 for (i
= 0; i
< count
; i
++) {
222 if (x
[i
] >= 0 && y
[i
] >= 0 && x
[i
] < w
&& y
[i
] < h
) {
223 zbuffer
[i
] = *((GLuint
*) (map
+ y
[i
] * rowStride
+ x
[i
] * 4));
228 const GLint bpp
= _mesa_get_format_bytes(rb
->Format
);
229 const GLint rowStride
= rb
->RowStride
* bpp
;
230 for (i
= 0; i
< count
; i
++) {
231 if (x
[i
] >= 0 && y
[i
] >= 0 && x
[i
] < w
&& y
[i
] < h
) {
232 const GLubyte
*src
= map
+ y
[i
] * rowStride
+ x
[i
] * bpp
;
233 _mesa_unpack_uint_z_row(rb
->Format
, 1, src
, &zbuffer
[i
]);
241 * Put an array of 32-bit z values into the depth buffer.
242 * Note: the z values are always in the range [0, 2^32-1].
245 put_z32_values(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
246 GLuint count
, const GLint x
[], const GLint y
[],
247 const GLuint zvalues
[], const GLubyte mask
[])
249 const GLint w
= rb
->Width
, h
= rb
->Height
;
250 GLubyte
*map
= (GLubyte
*) rb
->Data
;
253 if (rb
->Format
== MESA_FORMAT_Z32
) {
254 const GLuint rowStride
= rb
->RowStride
* 4;
255 for (i
= 0; i
< count
; i
++) {
256 if (mask
[i
] && x
[i
] >= 0 && y
[i
] >= 0 && x
[i
] < w
&& y
[i
] < h
) {
257 GLuint
*dst
= (GLuint
*) (map
+ y
[i
] * rowStride
+ x
[i
] * 4);
263 gl_pack_uint_z_func packZ
= _mesa_get_pack_uint_z_func(rb
->Format
);
264 const GLint bpp
= _mesa_get_format_bytes(rb
->Format
);
265 const GLint rowStride
= rb
->RowStride
* bpp
;
266 for (i
= 0; i
< count
; i
++) {
267 if (mask
[i
] && x
[i
] >= 0 && y
[i
] >= 0 && x
[i
] < w
&& y
[i
] < h
) {
268 void *dst
= map
+ y
[i
] * rowStride
+ x
[i
] * bpp
;
269 packZ(zvalues
+ i
, dst
);
277 * Apply depth (Z) buffer testing to the span.
278 * \return approx number of pixels that passed (only zero is reliable)
281 _swrast_depth_test_span(struct gl_context
*ctx
, SWspan
*span
)
283 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
284 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
285 const GLint bpp
= _mesa_get_format_bytes(rb
->Format
);
286 void *zStart
= _swrast_pixel_address(rb
, span
->x
, span
->y
);
287 const GLuint count
= span
->end
;
288 const GLuint
*fragZ
= span
->array
->z
;
289 GLubyte
*mask
= span
->array
->mask
;
291 GLuint
*zBufferTemp
= NULL
;
293 GLuint zBits
= _mesa_get_format_bits(rb
->Format
, GL_DEPTH_BITS
);
294 GLboolean ztest16
= GL_FALSE
;
296 if (rb
->Format
== MESA_FORMAT_Z16
&& !(span
->arrayMask
& SPAN_XY
)) {
297 /* directly read/write row of 16-bit Z values */
298 zBufferVals
= zStart
;
301 else if (rb
->Format
== MESA_FORMAT_Z32
&& !(span
->arrayMask
& SPAN_XY
)) {
302 /* directly read/write row of 32-bit Z values */
303 zBufferVals
= zStart
;
306 /* copy Z buffer values into temp buffer (32-bit Z values) */
307 zBufferTemp
= (GLuint
*) malloc(count
* sizeof(GLuint
));
311 if (span
->arrayMask
& SPAN_XY
) {
312 get_z32_values(ctx
, rb
, count
,
313 span
->array
->x
, span
->array
->y
, zBufferTemp
);
316 _mesa_unpack_uint_z_row(rb
->Format
, count
, zStart
, zBufferTemp
);
321 /* Convert depth buffer values from 32 to 24 bits to match the
322 * fragment Z values generated by rasterization.
324 for (i
= 0; i
< count
; i
++) {
325 zBufferTemp
[i
] >>= 8;
328 else if (zBits
== 16) {
330 /* Convert depth buffer values from 32 to 16 bits */
331 for (i
= 0; i
< count
; i
++) {
332 zBufferTemp
[i
] >>= 16;
339 zBufferVals
= zBufferTemp
;
342 /* do the depth test either with 16 or 32-bit values */
344 passed
= depth_test_span16(ctx
, count
, zBufferVals
, fragZ
, mask
);
346 passed
= depth_test_span32(ctx
, count
, zBufferVals
, fragZ
, mask
);
349 /* need to write temp Z values back into the buffer */
351 /* Convert depth buffer values back to 32-bit values. The least
352 * significant bits don't matter since they'll get dropped when
353 * they're packed back into the depth buffer.
357 for (i
= 0; i
< count
; i
++) {
358 zBufferTemp
[i
] = (zBufferTemp
[i
] << 8);
361 else if (zBits
== 16) {
363 for (i
= 0; i
< count
; i
++) {
364 zBufferTemp
[i
] = zBufferTemp
[i
] << 16;
368 if (span
->arrayMask
& SPAN_XY
) {
369 /* random locations */
370 put_z32_values(ctx
, rb
, count
, span
->array
->x
, span
->array
->y
,
375 gl_pack_uint_z_func packZ
= _mesa_get_pack_uint_z_func(rb
->Format
);
376 GLubyte
*dst
= zStart
;
378 for (i
= 0; i
< count
; i
++) {
380 packZ(&zBufferTemp
[i
], dst
);
389 if (passed
< count
) {
390 span
->writeAll
= GL_FALSE
;
397 * GL_EXT_depth_bounds_test extension.
398 * Discard fragments depending on whether the corresponding Z-buffer
399 * values are outside the depth bounds test range.
400 * Note: we test the Z buffer values, not the fragment Z values!
401 * \return GL_TRUE if any fragments pass, GL_FALSE if no fragments pass
404 _swrast_depth_bounds_test( struct gl_context
*ctx
, SWspan
*span
)
406 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
407 struct gl_renderbuffer
*rb
= fb
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
408 const GLint bpp
= _mesa_get_format_bytes(rb
->Format
);
409 const GLint rowStride
= rb
->RowStride
* bpp
;
410 GLubyte
*zStart
= (GLubyte
*) rb
->Data
+ span
->y
* rowStride
+ span
->x
* bpp
;
411 GLuint zMin
= (GLuint
) (ctx
->Depth
.BoundsMin
* fb
->_DepthMaxF
+ 0.5F
);
412 GLuint zMax
= (GLuint
) (ctx
->Depth
.BoundsMax
* fb
->_DepthMaxF
+ 0.5F
);
413 GLubyte
*mask
= span
->array
->mask
;
414 const GLuint count
= span
->end
;
416 GLboolean anyPass
= GL_FALSE
;
417 GLuint zBufferTemp
[MAX_WIDTH
];
418 const GLuint
*zBufferVals
;
420 if (rb
->Format
== MESA_FORMAT_Z32
&& !(span
->arrayMask
& SPAN_XY
)) {
421 /* directly access 32-bit values in the depth buffer */
422 zBufferVals
= (const GLuint
*) zStart
;
425 /* unpack Z values into a temporary array */
426 if (span
->arrayMask
& SPAN_XY
) {
427 get_z32_values(ctx
, rb
, count
, span
->array
->x
, span
->array
->y
,
431 _mesa_unpack_uint_z_row(rb
->Format
, count
, zStart
, zBufferTemp
);
433 zBufferVals
= zBufferTemp
;
436 /* Now do the tests */
437 for (i
= 0; i
< count
; i
++) {
439 if (zBufferVals
[i
] < zMin
|| zBufferVals
[i
] > zMax
)
451 /**********************************************************************/
452 /***** Read Depth Buffer *****/
453 /**********************************************************************/
457 * Read a span of depth values from the given depth renderbuffer, returning
458 * the values as GLfloats.
459 * This function does clipping to prevent reading outside the depth buffer's
463 _swrast_read_depth_span_float(struct gl_context
*ctx
,
464 struct gl_renderbuffer
*rb
,
465 GLint n
, GLint x
, GLint y
, GLfloat depth
[])
468 /* really only doing this to prevent FP exceptions later */
469 memset(depth
, 0, n
* sizeof(GLfloat
));
473 if (y
< 0 || y
>= (GLint
) rb
->Height
||
474 x
+ n
<= 0 || x
>= (GLint
) rb
->Width
) {
475 /* span is completely outside framebuffer */
476 memset(depth
, 0, n
* sizeof(GLfloat
));
483 for (i
= 0; i
< dx
; i
++)
489 if (x
+ n
> (GLint
) rb
->Width
) {
490 GLint dx
= x
+ n
- (GLint
) rb
->Width
;
492 for (i
= 0; i
< dx
; i
++)
493 depth
[n
- i
- 1] = 0.0;
500 _mesa_unpack_float_z_row(rb
->Format
, n
, _swrast_pixel_address(rb
, x
, y
),
506 * Clear the given z/depth renderbuffer. If the buffer is a combined
507 * depth+stencil buffer, only the Z bits will be touched.
510 _swrast_clear_depth_buffer(struct gl_context
*ctx
)
512 struct gl_renderbuffer
*rb
=
513 ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
514 GLint x
, y
, width
, height
;
516 GLint rowStride
, i
, j
;
519 if (!rb
|| !ctx
->Depth
.Mask
) {
520 /* no depth buffer, or writing to it is disabled */
524 /* compute region to clear */
525 x
= ctx
->DrawBuffer
->_Xmin
;
526 y
= ctx
->DrawBuffer
->_Ymin
;
527 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
528 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
530 mapMode
= GL_MAP_WRITE_BIT
;
531 if (rb
->Format
== MESA_FORMAT_S8_Z24
||
532 rb
->Format
== MESA_FORMAT_X8_Z24
||
533 rb
->Format
== MESA_FORMAT_Z24_S8
||
534 rb
->Format
== MESA_FORMAT_Z24_X8
) {
535 mapMode
|= GL_MAP_READ_BIT
;
538 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
,
539 mapMode
, &map
, &rowStride
);
541 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClear(depth)");
545 switch (rb
->Format
) {
546 case MESA_FORMAT_Z16
:
548 GLfloat clear
= (GLfloat
) ctx
->Depth
.Clear
;
549 GLushort clearVal
= 0;
550 _mesa_pack_float_z_row(rb
->Format
, 1, &clear
, &clearVal
);
551 if (clearVal
== 0xffff && width
* 2 == rowStride
) {
553 memset(map
, 0xff, width
* height
* 2);
556 for (i
= 0; i
< height
; i
++) {
557 GLushort
*row
= (GLushort
*) map
;
558 for (j
= 0; j
< width
; j
++) {
566 case MESA_FORMAT_Z32
:
567 case MESA_FORMAT_Z32_FLOAT
:
569 GLfloat clear
= (GLfloat
) ctx
->Depth
.Clear
;
571 _mesa_pack_float_z_row(rb
->Format
, 1, &clear
, &clearVal
);
572 for (i
= 0; i
< height
; i
++) {
573 GLuint
*row
= (GLuint
*) map
;
574 for (j
= 0; j
< width
; j
++) {
581 case MESA_FORMAT_S8_Z24
:
582 case MESA_FORMAT_X8_Z24
:
583 case MESA_FORMAT_Z24_S8
:
584 case MESA_FORMAT_Z24_X8
:
586 GLfloat clear
= (GLfloat
) ctx
->Depth
.Clear
;
590 if (rb
->Format
== MESA_FORMAT_S8_Z24
||
591 rb
->Format
== MESA_FORMAT_X8_Z24
)
596 _mesa_pack_float_z_row(rb
->Format
, 1, &clear
, &clearVal
);
597 for (i
= 0; i
< height
; i
++) {
598 GLuint
*row
= (GLuint
*) map
;
599 for (j
= 0; j
< width
; j
++) {
600 row
[j
] = (row
[j
] & mask
) | clearVal
;
607 case MESA_FORMAT_Z32_FLOAT_X24S8
:
610 GLfloat clearVal
= (GLfloat
) ctx
->Depth
.Clear
;
611 for (i
= 0; i
< height
; i
++) {
612 GLfloat
*row
= (GLfloat
*) map
;
613 for (j
= 0; j
< width
; j
++) {
614 row
[j
* 2] = clearVal
;
621 _mesa_problem(ctx
, "Unexpected depth buffer format %s"
622 " in _swrast_clear_depth_buffer()",
623 _mesa_get_format_name(rb
->Format
));
626 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);
633 * Clear both depth and stencil values in a combined depth+stencil buffer.
636 _swrast_clear_depth_stencil_buffer(struct gl_context
*ctx
)
638 const GLubyte stencilBits
= ctx
->DrawBuffer
->Visual
.stencilBits
;
639 const GLuint writeMask
= ctx
->Stencil
.WriteMask
[0];
640 const GLuint stencilMax
= (1 << stencilBits
) - 1;
641 struct gl_renderbuffer
*rb
=
642 ctx
->DrawBuffer
->Attachment
[BUFFER_DEPTH
].Renderbuffer
;
643 GLint x
, y
, width
, height
;
646 GLint rowStride
, i
, j
;
648 /* check that we really have a combined depth+stencil buffer */
649 assert(rb
== ctx
->DrawBuffer
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
651 /* compute region to clear */
652 x
= ctx
->DrawBuffer
->_Xmin
;
653 y
= ctx
->DrawBuffer
->_Ymin
;
654 width
= ctx
->DrawBuffer
->_Xmax
- ctx
->DrawBuffer
->_Xmin
;
655 height
= ctx
->DrawBuffer
->_Ymax
- ctx
->DrawBuffer
->_Ymin
;
657 mapMode
= GL_MAP_WRITE_BIT
;
658 if ((writeMask
& stencilMax
) != stencilMax
) {
659 /* need to mask stencil values */
660 mapMode
|= GL_MAP_READ_BIT
;
663 ctx
->Driver
.MapRenderbuffer(ctx
, rb
, x
, y
, width
, height
,
664 mapMode
, &map
, &rowStride
);
666 _mesa_error(ctx
, GL_OUT_OF_MEMORY
, "glClear(depth+stencil)");
670 switch (rb
->Format
) {
671 case MESA_FORMAT_S8_Z24
:
672 case MESA_FORMAT_Z24_S8
:
674 GLfloat zClear
= (GLfloat
) ctx
->Depth
.Clear
;
675 GLuint clear
= 0, mask
;
677 _mesa_pack_float_z_row(rb
->Format
, 1, &zClear
, &clear
);
679 if (rb
->Format
== MESA_FORMAT_S8_Z24
) {
680 mask
= ((~writeMask
) & 0xff) << 24;
681 clear
|= (ctx
->Stencil
.Clear
& writeMask
& 0xff) << 24;
684 mask
= ((~writeMask
) & 0xff);
685 clear
|= (ctx
->Stencil
.Clear
& writeMask
& 0xff);
688 for (i
= 0; i
< height
; i
++) {
689 GLuint
*row
= (GLuint
*) map
;
691 for (j
= 0; j
< width
; j
++) {
692 row
[j
] = (row
[j
] & mask
) | clear
;
696 for (j
= 0; j
< width
; j
++) {
704 case MESA_FORMAT_Z32_FLOAT_X24S8
:
707 const GLfloat zClear
= (GLfloat
) ctx
->Depth
.Clear
;
708 const GLuint sClear
= ctx
->Stencil
.Clear
& writeMask
;
709 const GLuint sMask
= (~writeMask
) & 0xff;
710 for (i
= 0; i
< height
; i
++) {
711 GLfloat
*zRow
= (GLfloat
*) map
;
712 GLuint
*sRow
= (GLuint
*) map
;
713 for (j
= 0; j
< width
; j
++) {
714 zRow
[j
* 2 + 0] = zClear
;
717 for (j
= 0; j
< width
; j
++) {
718 sRow
[j
* 2 + 1] = (sRow
[j
* 2 + 1] & sMask
) | sClear
;
722 for (j
= 0; j
< width
; j
++) {
723 sRow
[j
* 2 + 1] = sClear
;
731 _mesa_problem(ctx
, "Unexpected depth buffer format %s"
732 " in _swrast_clear_depth_buffer()",
733 _mesa_get_format_name(rb
->Format
));
736 ctx
->Driver
.UnmapRenderbuffer(ctx
, rb
);