1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 * \brief Quad depth testing
32 #include "pipe/p_defines.h"
33 #include "util/u_memory.h"
34 #include "tgsi/tgsi_scan.h"
35 #include "sp_context.h"
37 #include "sp_surface.h"
38 #include "sp_quad_pipe.h"
39 #include "sp_tile_cache.h"
40 #include "sp_state.h" /* for sp_fragment_shader */
44 struct pipe_surface
*ps
;
45 enum pipe_format format
;
46 unsigned bzzzz
[QUAD_SIZE
]; /**< Z values fetched from depth buffer */
47 unsigned qzzzz
[QUAD_SIZE
]; /**< Z values from the quad */
48 ubyte stencilVals
[QUAD_SIZE
];
49 struct softpipe_cached_tile
*tile
;
55 get_depth_stencil_values( struct depth_data
*data
,
56 const struct quad_header
*quad
)
59 const struct softpipe_cached_tile
*tile
= data
->tile
;
61 switch (data
->format
) {
62 case PIPE_FORMAT_Z16_UNORM
:
63 for (j
= 0; j
< QUAD_SIZE
; j
++) {
64 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
65 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
66 data
->bzzzz
[j
] = tile
->data
.depth16
[y
][x
];
69 case PIPE_FORMAT_Z32_UNORM
:
70 for (j
= 0; j
< QUAD_SIZE
; j
++) {
71 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
72 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
73 data
->bzzzz
[j
] = tile
->data
.depth32
[y
][x
];
76 case PIPE_FORMAT_X8Z24_UNORM
:
77 case PIPE_FORMAT_S8Z24_UNORM
:
78 for (j
= 0; j
< QUAD_SIZE
; j
++) {
79 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
80 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
81 data
->bzzzz
[j
] = tile
->data
.depth32
[y
][x
] & 0xffffff;
82 data
->stencilVals
[j
] = tile
->data
.depth32
[y
][x
] >> 24;
85 case PIPE_FORMAT_Z24X8_UNORM
:
86 case PIPE_FORMAT_Z24S8_UNORM
:
87 for (j
= 0; j
< QUAD_SIZE
; j
++) {
88 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
89 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
90 data
->bzzzz
[j
] = tile
->data
.depth32
[y
][x
] >> 8;
91 data
->stencilVals
[j
] = tile
->data
.depth32
[y
][x
] & 0xff;
99 /* If the shader has not been run, interpolate the depth values
103 interpolate_quad_depth( struct quad_header
*quad
)
105 const float fx
= (float) quad
->input
.x0
;
106 const float fy
= (float) quad
->input
.y0
;
107 const float dzdx
= quad
->posCoef
->dadx
[2];
108 const float dzdy
= quad
->posCoef
->dady
[2];
109 const float z0
= quad
->posCoef
->a0
[2] + dzdx
* fx
+ dzdy
* fy
;
111 quad
->output
.depth
[0] = z0
;
112 quad
->output
.depth
[1] = z0
+ dzdx
;
113 quad
->output
.depth
[2] = z0
+ dzdy
;
114 quad
->output
.depth
[3] = z0
+ dzdx
+ dzdy
;
119 convert_quad_depth( struct depth_data
*data
,
120 const struct quad_header
*quad
)
124 /* Convert quad's float depth values to int depth values (qzzzz).
125 * If the Z buffer stores integer values, we _have_ to do the depth
126 * compares with integers (not floats). Otherwise, the float->int->float
127 * conversion of Z values (which isn't an identity function) will cause
130 switch (data
->format
) {
131 case PIPE_FORMAT_Z16_UNORM
:
133 float scale
= 65535.0;
135 for (j
= 0; j
< QUAD_SIZE
; j
++) {
136 data
->qzzzz
[j
] = (unsigned) (quad
->output
.depth
[j
] * scale
);
140 case PIPE_FORMAT_Z32_UNORM
:
142 double scale
= (double) (uint
) ~0UL;
144 for (j
= 0; j
< QUAD_SIZE
; j
++) {
145 data
->qzzzz
[j
] = (unsigned) (quad
->output
.depth
[j
] * scale
);
149 case PIPE_FORMAT_X8Z24_UNORM
:
150 case PIPE_FORMAT_S8Z24_UNORM
:
152 float scale
= (float) ((1 << 24) - 1);
154 for (j
= 0; j
< QUAD_SIZE
; j
++) {
155 data
->qzzzz
[j
] = (unsigned) (quad
->output
.depth
[j
] * scale
);
159 case PIPE_FORMAT_Z24X8_UNORM
:
160 case PIPE_FORMAT_Z24S8_UNORM
:
162 float scale
= (float) ((1 << 24) - 1);
164 for (j
= 0; j
< QUAD_SIZE
; j
++) {
165 data
->qzzzz
[j
] = (unsigned) (quad
->output
.depth
[j
] * scale
);
177 write_depth_stencil_values( struct depth_data
*data
,
178 struct quad_header
*quad
)
180 struct softpipe_cached_tile
*tile
= data
->tile
;
183 /* put updated Z values back into cached tile */
184 switch (data
->format
) {
185 case PIPE_FORMAT_Z16_UNORM
:
186 for (j
= 0; j
< QUAD_SIZE
; j
++) {
187 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
188 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
189 tile
->data
.depth16
[y
][x
] = (ushort
) data
->bzzzz
[j
];
192 case PIPE_FORMAT_X8Z24_UNORM
:
193 case PIPE_FORMAT_Z32_UNORM
:
194 for (j
= 0; j
< QUAD_SIZE
; j
++) {
195 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
196 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
197 tile
->data
.depth32
[y
][x
] = data
->bzzzz
[j
];
200 case PIPE_FORMAT_S8Z24_UNORM
:
201 for (j
= 0; j
< QUAD_SIZE
; j
++) {
202 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
203 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
204 tile
->data
.depth32
[y
][x
] = (data
->stencilVals
[j
] << 24) | data
->bzzzz
[j
];
207 case PIPE_FORMAT_Z24S8_UNORM
:
208 for (j
= 0; j
< QUAD_SIZE
; j
++) {
209 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
210 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
211 tile
->data
.depth32
[y
][x
] = (data
->bzzzz
[j
] << 8) | data
->stencilVals
[j
];
214 case PIPE_FORMAT_Z24X8_UNORM
:
215 for (j
= 0; j
< QUAD_SIZE
; j
++) {
216 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
217 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
218 tile
->data
.depth32
[y
][x
] = data
->bzzzz
[j
] << 8;
229 /** Only 8-bit stencil supported */
230 #define STENCIL_MAX 0xff
234 * Do the basic stencil test (compare stencil buffer values against the
237 * \param data->stencilVals the stencil values from the stencil buffer
238 * \param func the stencil func (PIPE_FUNC_x)
239 * \param ref the stencil reference value
240 * \param valMask the stencil value mask indicating which bits of the stencil
241 * values and ref value are to be used.
242 * \return mask indicating which pixels passed the stencil test
245 do_stencil_test(struct depth_data
*data
,
247 unsigned ref
, unsigned valMask
)
249 unsigned passMask
= 0x0;
255 case PIPE_FUNC_NEVER
:
259 for (j
= 0; j
< QUAD_SIZE
; j
++) {
260 if (ref
< (data
->stencilVals
[j
] & valMask
)) {
261 passMask
|= (1 << j
);
265 case PIPE_FUNC_EQUAL
:
266 for (j
= 0; j
< QUAD_SIZE
; j
++) {
267 if (ref
== (data
->stencilVals
[j
] & valMask
)) {
268 passMask
|= (1 << j
);
272 case PIPE_FUNC_LEQUAL
:
273 for (j
= 0; j
< QUAD_SIZE
; j
++) {
274 if (ref
<= (data
->stencilVals
[j
] & valMask
)) {
275 passMask
|= (1 << j
);
279 case PIPE_FUNC_GREATER
:
280 for (j
= 0; j
< QUAD_SIZE
; j
++) {
281 if (ref
> (data
->stencilVals
[j
] & valMask
)) {
282 passMask
|= (1 << j
);
286 case PIPE_FUNC_NOTEQUAL
:
287 for (j
= 0; j
< QUAD_SIZE
; j
++) {
288 if (ref
!= (data
->stencilVals
[j
] & valMask
)) {
289 passMask
|= (1 << j
);
293 case PIPE_FUNC_GEQUAL
:
294 for (j
= 0; j
< QUAD_SIZE
; j
++) {
295 if (ref
>= (data
->stencilVals
[j
] & valMask
)) {
296 passMask
|= (1 << j
);
300 case PIPE_FUNC_ALWAYS
:
312 * Apply the stencil operator to stencil values.
314 * \param data->stencilVals the stencil buffer values (read and written)
315 * \param mask indicates which pixels to update
316 * \param op the stencil operator (PIPE_STENCIL_OP_x)
317 * \param ref the stencil reference value
318 * \param wrtMask writemask controlling which bits are changed in the
322 apply_stencil_op(struct depth_data
*data
,
323 unsigned mask
, unsigned op
, ubyte ref
, ubyte wrtMask
)
326 ubyte newstencil
[QUAD_SIZE
];
328 for (j
= 0; j
< QUAD_SIZE
; j
++) {
329 newstencil
[j
] = data
->stencilVals
[j
];
333 case PIPE_STENCIL_OP_KEEP
:
336 case PIPE_STENCIL_OP_ZERO
:
337 for (j
= 0; j
< QUAD_SIZE
; j
++) {
338 if (mask
& (1 << j
)) {
343 case PIPE_STENCIL_OP_REPLACE
:
344 for (j
= 0; j
< QUAD_SIZE
; j
++) {
345 if (mask
& (1 << j
)) {
350 case PIPE_STENCIL_OP_INCR
:
351 for (j
= 0; j
< QUAD_SIZE
; j
++) {
352 if (mask
& (1 << j
)) {
353 if (data
->stencilVals
[j
] < STENCIL_MAX
) {
354 newstencil
[j
] = data
->stencilVals
[j
] + 1;
359 case PIPE_STENCIL_OP_DECR
:
360 for (j
= 0; j
< QUAD_SIZE
; j
++) {
361 if (mask
& (1 << j
)) {
362 if (data
->stencilVals
[j
] > 0) {
363 newstencil
[j
] = data
->stencilVals
[j
] - 1;
368 case PIPE_STENCIL_OP_INCR_WRAP
:
369 for (j
= 0; j
< QUAD_SIZE
; j
++) {
370 if (mask
& (1 << j
)) {
371 newstencil
[j
] = data
->stencilVals
[j
] + 1;
375 case PIPE_STENCIL_OP_DECR_WRAP
:
376 for (j
= 0; j
< QUAD_SIZE
; j
++) {
377 if (mask
& (1 << j
)) {
378 newstencil
[j
] = data
->stencilVals
[j
] - 1;
382 case PIPE_STENCIL_OP_INVERT
:
383 for (j
= 0; j
< QUAD_SIZE
; j
++) {
384 if (mask
& (1 << j
)) {
385 newstencil
[j
] = ~data
->stencilVals
[j
];
394 * update the stencil values
396 if (wrtMask
!= STENCIL_MAX
) {
397 /* apply bit-wise stencil buffer writemask */
398 for (j
= 0; j
< QUAD_SIZE
; j
++) {
399 data
->stencilVals
[j
] = (wrtMask
& newstencil
[j
]) | (~wrtMask
& data
->stencilVals
[j
]);
403 for (j
= 0; j
< QUAD_SIZE
; j
++) {
404 data
->stencilVals
[j
] = newstencil
[j
];
412 * To increase efficiency, we should probably have multiple versions
413 * of this function that are specifically for Z16, Z32 and FP Z buffers.
414 * Try to effectively do that with codegen...
418 depth_test_quad(struct quad_stage
*qs
,
419 struct depth_data
*data
,
420 struct quad_header
*quad
)
422 struct softpipe_context
*softpipe
= qs
->softpipe
;
426 switch (softpipe
->depth_stencil
->depth
.func
) {
427 case PIPE_FUNC_NEVER
:
431 /* Note this is pretty much a single sse or cell instruction.
432 * Like this: quad->mask &= (quad->outputs.depth < zzzz);
434 for (j
= 0; j
< QUAD_SIZE
; j
++) {
435 if (data
->qzzzz
[j
] < data
->bzzzz
[j
])
439 case PIPE_FUNC_EQUAL
:
440 for (j
= 0; j
< QUAD_SIZE
; j
++) {
441 if (data
->qzzzz
[j
] == data
->bzzzz
[j
])
445 case PIPE_FUNC_LEQUAL
:
446 for (j
= 0; j
< QUAD_SIZE
; j
++) {
447 if (data
->qzzzz
[j
] <= data
->bzzzz
[j
])
451 case PIPE_FUNC_GREATER
:
452 for (j
= 0; j
< QUAD_SIZE
; j
++) {
453 if (data
->qzzzz
[j
] > data
->bzzzz
[j
])
457 case PIPE_FUNC_NOTEQUAL
:
458 for (j
= 0; j
< QUAD_SIZE
; j
++) {
459 if (data
->qzzzz
[j
] != data
->bzzzz
[j
])
463 case PIPE_FUNC_GEQUAL
:
464 for (j
= 0; j
< QUAD_SIZE
; j
++) {
465 if (data
->qzzzz
[j
] >= data
->bzzzz
[j
])
469 case PIPE_FUNC_ALWAYS
:
476 quad
->inout
.mask
&= zmask
;
477 if (quad
->inout
.mask
== 0)
480 /* Update our internal copy only if writemask set. Even if
481 * depth.writemask is FALSE, may still need to write out buffer
482 * data due to stencil changes.
484 if (softpipe
->depth_stencil
->depth
.writemask
) {
485 for (j
= 0; j
< QUAD_SIZE
; j
++) {
486 if (quad
->inout
.mask
& (1 << j
)) {
487 data
->bzzzz
[j
] = data
->qzzzz
[j
];
498 * Do stencil (and depth) testing. Stenciling depends on the outcome of
502 depth_stencil_test_quad(struct quad_stage
*qs
,
503 struct depth_data
*data
,
504 struct quad_header
*quad
)
506 struct softpipe_context
*softpipe
= qs
->softpipe
;
507 unsigned func
, zFailOp
, zPassOp
, failOp
;
508 ubyte ref
, wrtMask
, valMask
;
509 uint face
= quad
->input
.facing
;
511 if (!softpipe
->depth_stencil
->stencil
[1].enabled
) {
512 /* single-sided stencil test, use front (face=0) state */
516 /* choose front or back face function, operator, etc */
517 /* XXX we could do these initializations once per primitive */
518 func
= softpipe
->depth_stencil
->stencil
[face
].func
;
519 failOp
= softpipe
->depth_stencil
->stencil
[face
].fail_op
;
520 zFailOp
= softpipe
->depth_stencil
->stencil
[face
].zfail_op
;
521 zPassOp
= softpipe
->depth_stencil
->stencil
[face
].zpass_op
;
522 ref
= softpipe
->depth_stencil
->stencil
[face
].ref_value
;
523 wrtMask
= softpipe
->depth_stencil
->stencil
[face
].writemask
;
524 valMask
= softpipe
->depth_stencil
->stencil
[face
].valuemask
;
527 /* do the stencil test first */
529 unsigned passMask
, failMask
;
530 passMask
= do_stencil_test(data
, func
, ref
, valMask
);
531 failMask
= quad
->inout
.mask
& ~passMask
;
532 quad
->inout
.mask
&= passMask
;
534 if (failOp
!= PIPE_STENCIL_OP_KEEP
) {
535 apply_stencil_op(data
, failMask
, failOp
, ref
, wrtMask
);
539 if (quad
->inout
.mask
) {
540 /* now the pixels that passed the stencil test are depth tested */
541 if (softpipe
->depth_stencil
->depth
.enabled
) {
542 const unsigned origMask
= quad
->inout
.mask
;
544 depth_test_quad(qs
, data
, quad
); /* quad->mask is updated */
546 /* update stencil buffer values according to z pass/fail result */
547 if (zFailOp
!= PIPE_STENCIL_OP_KEEP
) {
548 const unsigned failMask
= origMask
& ~quad
->inout
.mask
;
549 apply_stencil_op(data
, failMask
, zFailOp
, ref
, wrtMask
);
552 if (zPassOp
!= PIPE_STENCIL_OP_KEEP
) {
553 const unsigned passMask
= origMask
& quad
->inout
.mask
;
554 apply_stencil_op(data
, passMask
, zPassOp
, ref
, wrtMask
);
558 /* no depth test, apply Zpass operator to stencil buffer values */
559 apply_stencil_op(data
, quad
->inout
.mask
, zPassOp
, ref
, wrtMask
);
563 return quad
->inout
.mask
!= 0;
567 #define ALPHATEST( FUNC, COMP ) \
569 alpha_test_quads_##FUNC( struct quad_stage *qs, \
570 struct quad_header *quads[], \
573 const float ref = qs->softpipe->depth_stencil->alpha.ref_value; \
574 const uint cbuf = 0; /* only output[0].alpha is tested */ \
575 unsigned pass_nr = 0; \
578 for (i = 0; i < nr; i++) { \
579 const float *aaaa = quads[i]->output.color[cbuf][3]; \
580 unsigned passMask = 0; \
582 if (aaaa[0] COMP ref) passMask |= (1 << 0); \
583 if (aaaa[1] COMP ref) passMask |= (1 << 1); \
584 if (aaaa[2] COMP ref) passMask |= (1 << 2); \
585 if (aaaa[3] COMP ref) passMask |= (1 << 3); \
587 quads[i]->inout.mask &= passMask; \
589 if (quads[i]->inout.mask) \
590 quads[pass_nr++] = quads[i]; \
598 ALPHATEST( EQUAL
, == )
599 ALPHATEST( LEQUAL
, <= )
600 ALPHATEST( GREATER
, > )
601 ALPHATEST( NOTEQUAL
, != )
602 ALPHATEST( GEQUAL
, >= )
605 /* XXX: Incorporate into shader using KILP.
608 alpha_test_quads(struct quad_stage
*qs
,
609 struct quad_header
*quads
[],
612 switch (qs
->softpipe
->depth_stencil
->alpha
.func
) {
614 return alpha_test_quads_LESS( qs
, quads
, nr
);
615 case PIPE_FUNC_EQUAL
:
616 return alpha_test_quads_EQUAL( qs
, quads
, nr
);
618 case PIPE_FUNC_LEQUAL
:
619 return alpha_test_quads_LEQUAL( qs
, quads
, nr
);
620 case PIPE_FUNC_GREATER
:
621 return alpha_test_quads_GREATER( qs
, quads
, nr
);
622 case PIPE_FUNC_NOTEQUAL
:
623 return alpha_test_quads_NOTEQUAL( qs
, quads
, nr
);
624 case PIPE_FUNC_GEQUAL
:
625 return alpha_test_quads_GEQUAL( qs
, quads
, nr
);
626 case PIPE_FUNC_ALWAYS
:
628 case PIPE_FUNC_NEVER
:
634 static unsigned mask_count
[16] =
657 depth_test_quads_fallback(struct quad_stage
*qs
,
658 struct quad_header
*quads
[],
661 unsigned i
, pass
= 0;
662 const struct sp_fragment_shader
*fs
= qs
->softpipe
->fs
;
663 boolean interp_depth
= !fs
->info
.writes_z
;
664 struct depth_data data
;
667 if (qs
->softpipe
->depth_stencil
->alpha
.enabled
) {
668 nr
= alpha_test_quads(qs
, quads
, nr
);
671 if (qs
->softpipe
->framebuffer
.zsbuf
&&
672 (qs
->softpipe
->depth_stencil
->depth
.enabled
||
673 qs
->softpipe
->depth_stencil
->stencil
[0].enabled
)) {
675 data
.ps
= qs
->softpipe
->framebuffer
.zsbuf
;
676 data
.format
= data
.ps
->format
;
677 data
.tile
= sp_get_cached_tile(qs
->softpipe
->zsbuf_cache
,
681 for (i
= 0; i
< nr
; i
++) {
682 get_depth_stencil_values(&data
, quads
[i
]);
684 if (qs
->softpipe
->depth_stencil
->depth
.enabled
) {
686 interpolate_quad_depth(quads
[i
]);
688 convert_quad_depth(&data
, quads
[i
]);
691 if (qs
->softpipe
->depth_stencil
->stencil
[0].enabled
) {
692 if (!depth_stencil_test_quad(qs
, &data
, quads
[i
]))
696 if (!depth_test_quad(qs
, &data
, quads
[i
]))
700 if (qs
->softpipe
->depth_stencil
->stencil
[0].enabled
||
701 qs
->softpipe
->depth_stencil
->depth
.writemask
)
702 write_depth_stencil_values(&data
, quads
[i
]);
704 quads
[pass
++] = quads
[i
];
710 if (qs
->softpipe
->active_query_count
) {
711 for (i
= 0; i
< nr
; i
++)
712 qs
->softpipe
->occlusion_count
+= mask_count
[quads
[i
]->inout
.mask
];
716 qs
->next
->run(qs
->next
, quads
, nr
);
719 /* XXX: this function assumes setup function actually emits linear
720 * spans of quads. It seems a lot more natural to do (early)
721 * depth-testing on spans rather than quads.
724 depth_interp_z16_less_write(struct quad_stage
*qs
,
725 struct quad_header
*quads
[],
728 unsigned i
, pass
= 0;
729 const unsigned ix
= quads
[0]->input
.x0
;
730 const unsigned iy
= quads
[0]->input
.y0
;
731 const float fx
= (float) ix
;
732 const float fy
= (float) iy
;
733 const float dzdx
= quads
[0]->posCoef
->dadx
[2];
734 const float dzdy
= quads
[0]->posCoef
->dady
[2];
735 const float z0
= quads
[0]->posCoef
->a0
[2] + dzdx
* fx
+ dzdy
* fy
;
736 struct softpipe_cached_tile
*tile
;
737 ushort (*depth16
)[TILE_SIZE
];
738 ushort idepth
[4], depth_step
;
739 const float scale
= 65535.0;
741 idepth
[0] = (ushort
)((z0
) * scale
);
742 idepth
[1] = (ushort
)((z0
+ dzdx
) * scale
);
743 idepth
[2] = (ushort
)((z0
+ dzdy
) * scale
);
744 idepth
[3] = (ushort
)((z0
+ dzdx
+ dzdy
) * scale
);
746 depth_step
= (ushort
)(dzdx
* 2 * scale
);
748 tile
= sp_get_cached_tile(qs
->softpipe
->zsbuf_cache
, ix
, iy
);
750 depth16
= (ushort (*)[TILE_SIZE
])
751 &tile
->data
.depth16
[iy
% TILE_SIZE
][ix
% TILE_SIZE
];
753 for (i
= 0; i
< nr
; i
++) {
754 unsigned outmask
= quads
[i
]->inout
.mask
;
757 if ((outmask
& 1) && idepth
[0] < depth16
[0][0]) {
758 depth16
[0][0] = idepth
[0];
762 if ((outmask
& 2) && idepth
[1] < depth16
[0][1]) {
763 depth16
[0][1] = idepth
[1];
767 if ((outmask
& 4) && idepth
[2] < depth16
[1][0]) {
768 depth16
[1][0] = idepth
[2];
772 if ((outmask
& 8) && idepth
[3] < depth16
[1][1]) {
773 depth16
[1][1] = idepth
[3];
777 idepth
[0] += depth_step
;
778 idepth
[1] += depth_step
;
779 idepth
[2] += depth_step
;
780 idepth
[3] += depth_step
;
782 depth16
= (ushort (*)[TILE_SIZE
]) &depth16
[0][2];
784 quads
[i
]->inout
.mask
= mask
;
785 if (quads
[i
]->inout
.mask
)
786 quads
[pass
++] = quads
[i
];
790 qs
->next
->run(qs
->next
, quads
, pass
);
796 depth_interp_z16_lequal_write(struct quad_stage
*qs
,
797 struct quad_header
*quads
[],
800 unsigned i
, pass
= 0;
801 const unsigned ix
= quads
[0]->input
.x0
;
802 const unsigned iy
= quads
[0]->input
.y0
;
803 const float fx
= (float) ix
;
804 const float fy
= (float) iy
;
805 const float dzdx
= quads
[0]->posCoef
->dadx
[2];
806 const float dzdy
= quads
[0]->posCoef
->dady
[2];
807 const float z0
= quads
[0]->posCoef
->a0
[2] + dzdx
* fx
+ dzdy
* fy
;
808 struct softpipe_cached_tile
*tile
;
809 ushort (*depth16
)[TILE_SIZE
];
810 ushort idepth
[4], depth_step
;
811 const float scale
= 65535.0;
813 idepth
[0] = (ushort
)((z0
) * scale
);
814 idepth
[1] = (ushort
)((z0
+ dzdx
) * scale
);
815 idepth
[2] = (ushort
)((z0
+ dzdy
) * scale
);
816 idepth
[3] = (ushort
)((z0
+ dzdx
+ dzdy
) * scale
);
818 depth_step
= (ushort
)(dzdx
* 2 * scale
);
820 tile
= sp_get_cached_tile(qs
->softpipe
->zsbuf_cache
, ix
, iy
);
822 depth16
= (ushort (*)[TILE_SIZE
])
823 &tile
->data
.depth16
[iy
% TILE_SIZE
][ix
% TILE_SIZE
];
825 for (i
= 0; i
< nr
; i
++) {
826 unsigned outmask
= quads
[i
]->inout
.mask
;
829 if ((outmask
& 1) && idepth
[0] <= depth16
[0][0]) {
830 depth16
[0][0] = idepth
[0];
834 if ((outmask
& 2) && idepth
[1] <= depth16
[0][1]) {
835 depth16
[0][1] = idepth
[1];
839 if ((outmask
& 4) && idepth
[2] <= depth16
[1][0]) {
840 depth16
[1][0] = idepth
[2];
844 if ((outmask
& 8) && idepth
[3] <= depth16
[1][1]) {
845 depth16
[1][1] = idepth
[3];
849 idepth
[0] += depth_step
;
850 idepth
[1] += depth_step
;
851 idepth
[2] += depth_step
;
852 idepth
[3] += depth_step
;
854 depth16
= (ushort (*)[TILE_SIZE
]) &depth16
[0][2];
856 quads
[i
]->inout
.mask
= mask
;
857 if (quads
[i
]->inout
.mask
)
858 quads
[pass
++] = quads
[i
];
862 qs
->next
->run(qs
->next
, quads
, pass
);
871 depth_noop(struct quad_stage
*qs
,
872 struct quad_header
*quads
[],
875 qs
->next
->run(qs
->next
, quads
, nr
);
881 choose_depth_test(struct quad_stage
*qs
,
882 struct quad_header
*quads
[],
885 boolean interp_depth
= !qs
->softpipe
->fs
->info
.writes_z
;
887 boolean alpha
= qs
->softpipe
->depth_stencil
->alpha
.enabled
;
889 boolean depth
= (qs
->softpipe
->framebuffer
.zsbuf
&&
890 qs
->softpipe
->depth_stencil
->depth
.enabled
);
892 unsigned depthfunc
= qs
->softpipe
->depth_stencil
->depth
.func
;
894 boolean stencil
= qs
->softpipe
->depth_stencil
->stencil
[0].enabled
;
896 boolean depthwrite
= qs
->softpipe
->depth_stencil
->depth
.writemask
;
898 boolean occlusion
= qs
->softpipe
->active_query_count
;
904 qs
->run
= depth_noop
;
915 switch (qs
->softpipe
->framebuffer
.zsbuf
->format
) {
916 case PIPE_FORMAT_Z16_UNORM
:
917 qs
->run
= depth_interp_z16_less_write
;
920 qs
->run
= depth_test_quads_fallback
;
924 case PIPE_FUNC_LEQUAL
:
925 switch (qs
->softpipe
->framebuffer
.zsbuf
->format
) {
926 case PIPE_FORMAT_Z16_UNORM
:
927 qs
->run
= depth_interp_z16_lequal_write
;
930 qs
->run
= depth_test_quads_fallback
;
935 qs
->run
= depth_test_quads_fallback
;
939 qs
->run
= depth_test_quads_fallback
;
943 qs
->run( qs
, quads
, nr
);
950 static void depth_test_begin(struct quad_stage
*qs
)
952 qs
->run
= choose_depth_test
;
953 qs
->next
->begin(qs
->next
);
957 static void depth_test_destroy(struct quad_stage
*qs
)
963 struct quad_stage
*sp_quad_depth_test_stage( struct softpipe_context
*softpipe
)
965 struct quad_stage
*stage
= CALLOC_STRUCT(quad_stage
);
967 stage
->softpipe
= softpipe
;
968 stage
->begin
= depth_test_begin
;
969 stage
->run
= choose_depth_test
;
970 stage
->destroy
= depth_test_destroy
;