1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * Copyright 2010 VMware, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
30 * \brief Quad depth / stencil testing
33 #include "pipe/p_defines.h"
34 #include "util/u_format.h"
35 #include "util/u_math.h"
36 #include "util/u_memory.h"
37 #include "tgsi/tgsi_scan.h"
38 #include "sp_context.h"
40 #include "sp_quad_pipe.h"
41 #include "sp_tile_cache.h"
42 #include "sp_state.h" /* for sp_fragment_shader */
46 struct pipe_surface
*ps
;
47 enum pipe_format format
;
48 unsigned bzzzz
[QUAD_SIZE
]; /**< Z values fetched from depth buffer */
49 unsigned qzzzz
[QUAD_SIZE
]; /**< Z values from the quad */
50 ubyte stencilVals
[QUAD_SIZE
];
51 boolean use_shader_stencil_refs
;
52 ubyte shader_stencil_refs
[QUAD_SIZE
];
53 struct softpipe_cached_tile
*tile
;
59 get_depth_stencil_values( struct depth_data
*data
,
60 const struct quad_header
*quad
)
63 const struct softpipe_cached_tile
*tile
= data
->tile
;
65 switch (data
->format
) {
66 case PIPE_FORMAT_Z16_UNORM
:
67 for (j
= 0; j
< QUAD_SIZE
; j
++) {
68 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
69 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
70 data
->bzzzz
[j
] = tile
->data
.depth16
[y
][x
];
73 case PIPE_FORMAT_Z32_UNORM
:
74 for (j
= 0; j
< QUAD_SIZE
; j
++) {
75 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
76 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
77 data
->bzzzz
[j
] = tile
->data
.depth32
[y
][x
];
80 case PIPE_FORMAT_Z24X8_UNORM
:
81 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
82 for (j
= 0; j
< QUAD_SIZE
; j
++) {
83 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
84 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
85 data
->bzzzz
[j
] = tile
->data
.depth32
[y
][x
] & 0xffffff;
86 data
->stencilVals
[j
] = tile
->data
.depth32
[y
][x
] >> 24;
89 case PIPE_FORMAT_X8Z24_UNORM
:
90 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
91 for (j
= 0; j
< QUAD_SIZE
; j
++) {
92 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
93 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
94 data
->bzzzz
[j
] = tile
->data
.depth32
[y
][x
] >> 8;
95 data
->stencilVals
[j
] = tile
->data
.depth32
[y
][x
] & 0xff;
98 case PIPE_FORMAT_S8_UINT
:
99 for (j
= 0; j
< QUAD_SIZE
; j
++) {
100 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
101 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
103 data
->stencilVals
[j
] = tile
->data
.stencil8
[y
][x
];
106 case PIPE_FORMAT_Z32_FLOAT
:
107 for (j
= 0; j
< QUAD_SIZE
; j
++) {
108 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
109 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
110 data
->bzzzz
[j
] = tile
->data
.depth32
[y
][x
];
113 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
114 for (j
= 0; j
< QUAD_SIZE
; j
++) {
115 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
116 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
117 data
->bzzzz
[j
] = tile
->data
.depth64
[y
][x
] & 0xffffffff;
118 data
->stencilVals
[j
] = (tile
->data
.depth64
[y
][x
] >> 32) & 0xff;
128 * If the shader has not been run, interpolate the depth values
132 interpolate_quad_depth( struct quad_header
*quad
)
134 const float fx
= (float) quad
->input
.x0
;
135 const float fy
= (float) quad
->input
.y0
;
136 const float dzdx
= quad
->posCoef
->dadx
[2];
137 const float dzdy
= quad
->posCoef
->dady
[2];
138 const float z0
= quad
->posCoef
->a0
[2] + dzdx
* fx
+ dzdy
* fy
;
140 quad
->output
.depth
[0] = z0
;
141 quad
->output
.depth
[1] = z0
+ dzdx
;
142 quad
->output
.depth
[2] = z0
+ dzdy
;
143 quad
->output
.depth
[3] = z0
+ dzdx
+ dzdy
;
148 * Compute the depth_data::qzzzz[] values from the float fragment Z values.
151 convert_quad_depth( struct depth_data
*data
,
152 const struct quad_header
*quad
)
156 /* Convert quad's float depth values to int depth values (qzzzz).
157 * If the Z buffer stores integer values, we _have_ to do the depth
158 * compares with integers (not floats). Otherwise, the float->int->float
159 * conversion of Z values (which isn't an identity function) will cause
162 switch (data
->format
) {
163 case PIPE_FORMAT_Z16_UNORM
:
165 float scale
= 65535.0;
167 for (j
= 0; j
< QUAD_SIZE
; j
++) {
168 data
->qzzzz
[j
] = (unsigned) (quad
->output
.depth
[j
] * scale
);
172 case PIPE_FORMAT_Z32_UNORM
:
174 double scale
= (double) (uint
) ~0UL;
176 for (j
= 0; j
< QUAD_SIZE
; j
++) {
177 data
->qzzzz
[j
] = (unsigned) (quad
->output
.depth
[j
] * scale
);
181 case PIPE_FORMAT_Z24X8_UNORM
:
182 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
184 float scale
= (float) ((1 << 24) - 1);
186 for (j
= 0; j
< QUAD_SIZE
; j
++) {
187 data
->qzzzz
[j
] = (unsigned) (quad
->output
.depth
[j
] * scale
);
191 case PIPE_FORMAT_X8Z24_UNORM
:
192 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
194 float scale
= (float) ((1 << 24) - 1);
196 for (j
= 0; j
< QUAD_SIZE
; j
++) {
197 data
->qzzzz
[j
] = (unsigned) (quad
->output
.depth
[j
] * scale
);
201 case PIPE_FORMAT_Z32_FLOAT
:
202 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
206 for (j
= 0; j
< QUAD_SIZE
; j
++) {
207 fui
.f
= quad
->output
.depth
[j
];
208 data
->qzzzz
[j
] = fui
.ui
;
219 * Compute the depth_data::shader_stencil_refs[] values from the float
220 * fragment stencil values.
223 convert_quad_stencil( struct depth_data
*data
,
224 const struct quad_header
*quad
)
228 data
->use_shader_stencil_refs
= TRUE
;
229 /* Copy quads stencil values
231 switch (data
->format
) {
232 case PIPE_FORMAT_Z24X8_UNORM
:
233 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
234 case PIPE_FORMAT_X8Z24_UNORM
:
235 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
236 case PIPE_FORMAT_S8_UINT
:
237 case PIPE_FORMAT_Z32_FLOAT
:
238 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
239 for (j
= 0; j
< QUAD_SIZE
; j
++) {
240 data
->shader_stencil_refs
[j
] = ((unsigned)(quad
->output
.stencil
[j
]));
250 * Write data->bzzzz[] values and data->stencilVals into the Z/stencil buffer.
253 write_depth_stencil_values( struct depth_data
*data
,
254 struct quad_header
*quad
)
256 struct softpipe_cached_tile
*tile
= data
->tile
;
259 /* put updated Z values back into cached tile */
260 switch (data
->format
) {
261 case PIPE_FORMAT_Z16_UNORM
:
262 for (j
= 0; j
< QUAD_SIZE
; j
++) {
263 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
264 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
265 tile
->data
.depth16
[y
][x
] = (ushort
) data
->bzzzz
[j
];
268 case PIPE_FORMAT_Z24X8_UNORM
:
269 case PIPE_FORMAT_Z32_UNORM
:
270 for (j
= 0; j
< QUAD_SIZE
; j
++) {
271 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
272 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
273 tile
->data
.depth32
[y
][x
] = data
->bzzzz
[j
];
276 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
277 for (j
= 0; j
< QUAD_SIZE
; j
++) {
278 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
279 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
280 tile
->data
.depth32
[y
][x
] = (data
->stencilVals
[j
] << 24) | data
->bzzzz
[j
];
283 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
284 for (j
= 0; j
< QUAD_SIZE
; j
++) {
285 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
286 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
287 tile
->data
.depth32
[y
][x
] = (data
->bzzzz
[j
] << 8) | data
->stencilVals
[j
];
290 case PIPE_FORMAT_X8Z24_UNORM
:
291 for (j
= 0; j
< QUAD_SIZE
; j
++) {
292 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
293 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
294 tile
->data
.depth32
[y
][x
] = data
->bzzzz
[j
] << 8;
297 case PIPE_FORMAT_S8_UINT
:
298 for (j
= 0; j
< QUAD_SIZE
; j
++) {
299 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
300 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
301 tile
->data
.stencil8
[y
][x
] = data
->stencilVals
[j
];
304 case PIPE_FORMAT_Z32_FLOAT
:
305 for (j
= 0; j
< QUAD_SIZE
; j
++) {
306 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
307 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
308 tile
->data
.depth32
[y
][x
] = data
->bzzzz
[j
];
311 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
312 for (j
= 0; j
< QUAD_SIZE
; j
++) {
313 int x
= quad
->input
.x0
% TILE_SIZE
+ (j
& 1);
314 int y
= quad
->input
.y0
% TILE_SIZE
+ (j
>> 1);
315 tile
->data
.depth64
[y
][x
] = (uint64_t)data
->bzzzz
[j
] | ((uint64_t)data
->stencilVals
[j
] << 32);
325 /** Only 8-bit stencil supported */
326 #define STENCIL_MAX 0xff
330 * Do the basic stencil test (compare stencil buffer values against the
333 * \param data->stencilVals the stencil values from the stencil buffer
334 * \param func the stencil func (PIPE_FUNC_x)
335 * \param ref the stencil reference value
336 * \param valMask the stencil value mask indicating which bits of the stencil
337 * values and ref value are to be used.
338 * \return mask indicating which pixels passed the stencil test
341 do_stencil_test(struct depth_data
*data
,
343 unsigned ref
, unsigned valMask
)
345 unsigned passMask
= 0x0;
347 ubyte refs
[QUAD_SIZE
];
349 for (j
= 0; j
< QUAD_SIZE
; j
++) {
350 if (data
->use_shader_stencil_refs
)
351 refs
[j
] = data
->shader_stencil_refs
[j
] & valMask
;
353 refs
[j
] = ref
& valMask
;
357 case PIPE_FUNC_NEVER
:
361 for (j
= 0; j
< QUAD_SIZE
; j
++) {
362 if (refs
[j
] < (data
->stencilVals
[j
] & valMask
)) {
363 passMask
|= (1 << j
);
367 case PIPE_FUNC_EQUAL
:
368 for (j
= 0; j
< QUAD_SIZE
; j
++) {
369 if (refs
[j
] == (data
->stencilVals
[j
] & valMask
)) {
370 passMask
|= (1 << j
);
374 case PIPE_FUNC_LEQUAL
:
375 for (j
= 0; j
< QUAD_SIZE
; j
++) {
376 if (refs
[j
] <= (data
->stencilVals
[j
] & valMask
)) {
377 passMask
|= (1 << j
);
381 case PIPE_FUNC_GREATER
:
382 for (j
= 0; j
< QUAD_SIZE
; j
++) {
383 if (refs
[j
] > (data
->stencilVals
[j
] & valMask
)) {
384 passMask
|= (1 << j
);
388 case PIPE_FUNC_NOTEQUAL
:
389 for (j
= 0; j
< QUAD_SIZE
; j
++) {
390 if (refs
[j
] != (data
->stencilVals
[j
] & valMask
)) {
391 passMask
|= (1 << j
);
395 case PIPE_FUNC_GEQUAL
:
396 for (j
= 0; j
< QUAD_SIZE
; j
++) {
397 if (refs
[j
] >= (data
->stencilVals
[j
] & valMask
)) {
398 passMask
|= (1 << j
);
402 case PIPE_FUNC_ALWAYS
:
414 * Apply the stencil operator to stencil values.
416 * \param data->stencilVals the stencil buffer values (read and written)
417 * \param mask indicates which pixels to update
418 * \param op the stencil operator (PIPE_STENCIL_OP_x)
419 * \param ref the stencil reference value
420 * \param wrtMask writemask controlling which bits are changed in the
424 apply_stencil_op(struct depth_data
*data
,
425 unsigned mask
, unsigned op
, ubyte ref
, ubyte wrtMask
)
428 ubyte newstencil
[QUAD_SIZE
];
429 ubyte refs
[QUAD_SIZE
];
431 for (j
= 0; j
< QUAD_SIZE
; j
++) {
432 newstencil
[j
] = data
->stencilVals
[j
];
433 if (data
->use_shader_stencil_refs
)
434 refs
[j
] = data
->shader_stencil_refs
[j
];
440 case PIPE_STENCIL_OP_KEEP
:
443 case PIPE_STENCIL_OP_ZERO
:
444 for (j
= 0; j
< QUAD_SIZE
; j
++) {
445 if (mask
& (1 << j
)) {
450 case PIPE_STENCIL_OP_REPLACE
:
451 for (j
= 0; j
< QUAD_SIZE
; j
++) {
452 if (mask
& (1 << j
)) {
453 newstencil
[j
] = refs
[j
];
457 case PIPE_STENCIL_OP_INCR
:
458 for (j
= 0; j
< QUAD_SIZE
; j
++) {
459 if (mask
& (1 << j
)) {
460 if (data
->stencilVals
[j
] < STENCIL_MAX
) {
461 newstencil
[j
] = data
->stencilVals
[j
] + 1;
466 case PIPE_STENCIL_OP_DECR
:
467 for (j
= 0; j
< QUAD_SIZE
; j
++) {
468 if (mask
& (1 << j
)) {
469 if (data
->stencilVals
[j
] > 0) {
470 newstencil
[j
] = data
->stencilVals
[j
] - 1;
475 case PIPE_STENCIL_OP_INCR_WRAP
:
476 for (j
= 0; j
< QUAD_SIZE
; j
++) {
477 if (mask
& (1 << j
)) {
478 newstencil
[j
] = data
->stencilVals
[j
] + 1;
482 case PIPE_STENCIL_OP_DECR_WRAP
:
483 for (j
= 0; j
< QUAD_SIZE
; j
++) {
484 if (mask
& (1 << j
)) {
485 newstencil
[j
] = data
->stencilVals
[j
] - 1;
489 case PIPE_STENCIL_OP_INVERT
:
490 for (j
= 0; j
< QUAD_SIZE
; j
++) {
491 if (mask
& (1 << j
)) {
492 newstencil
[j
] = ~data
->stencilVals
[j
];
501 * update the stencil values
503 if (wrtMask
!= STENCIL_MAX
) {
504 /* apply bit-wise stencil buffer writemask */
505 for (j
= 0; j
< QUAD_SIZE
; j
++) {
506 data
->stencilVals
[j
] = (wrtMask
& newstencil
[j
]) | (~wrtMask
& data
->stencilVals
[j
]);
510 for (j
= 0; j
< QUAD_SIZE
; j
++) {
511 data
->stencilVals
[j
] = newstencil
[j
];
519 * To increase efficiency, we should probably have multiple versions
520 * of this function that are specifically for Z16, Z32 and FP Z buffers.
521 * Try to effectively do that with codegen...
524 depth_test_quad(struct quad_stage
*qs
,
525 struct depth_data
*data
,
526 struct quad_header
*quad
)
528 struct softpipe_context
*softpipe
= qs
->softpipe
;
532 switch (softpipe
->depth_stencil
->depth
.func
) {
533 case PIPE_FUNC_NEVER
:
537 /* Note this is pretty much a single sse or cell instruction.
538 * Like this: quad->mask &= (quad->outputs.depth < zzzz);
540 for (j
= 0; j
< QUAD_SIZE
; j
++) {
541 if (data
->qzzzz
[j
] < data
->bzzzz
[j
])
545 case PIPE_FUNC_EQUAL
:
546 for (j
= 0; j
< QUAD_SIZE
; j
++) {
547 if (data
->qzzzz
[j
] == data
->bzzzz
[j
])
551 case PIPE_FUNC_LEQUAL
:
552 for (j
= 0; j
< QUAD_SIZE
; j
++) {
553 if (data
->qzzzz
[j
] <= data
->bzzzz
[j
])
557 case PIPE_FUNC_GREATER
:
558 for (j
= 0; j
< QUAD_SIZE
; j
++) {
559 if (data
->qzzzz
[j
] > data
->bzzzz
[j
])
563 case PIPE_FUNC_NOTEQUAL
:
564 for (j
= 0; j
< QUAD_SIZE
; j
++) {
565 if (data
->qzzzz
[j
] != data
->bzzzz
[j
])
569 case PIPE_FUNC_GEQUAL
:
570 for (j
= 0; j
< QUAD_SIZE
; j
++) {
571 if (data
->qzzzz
[j
] >= data
->bzzzz
[j
])
575 case PIPE_FUNC_ALWAYS
:
582 quad
->inout
.mask
&= zmask
;
583 if (quad
->inout
.mask
== 0)
586 /* Update our internal copy only if writemask set. Even if
587 * depth.writemask is FALSE, may still need to write out buffer
588 * data due to stencil changes.
590 if (softpipe
->depth_stencil
->depth
.writemask
) {
591 for (j
= 0; j
< QUAD_SIZE
; j
++) {
592 if (quad
->inout
.mask
& (1 << j
)) {
593 data
->bzzzz
[j
] = data
->qzzzz
[j
];
604 * Do stencil (and depth) testing. Stenciling depends on the outcome of
608 depth_stencil_test_quad(struct quad_stage
*qs
,
609 struct depth_data
*data
,
610 struct quad_header
*quad
)
612 struct softpipe_context
*softpipe
= qs
->softpipe
;
613 unsigned func
, zFailOp
, zPassOp
, failOp
;
614 ubyte ref
, wrtMask
, valMask
;
615 uint face
= quad
->input
.facing
;
617 if (!softpipe
->depth_stencil
->stencil
[1].enabled
) {
618 /* single-sided stencil test, use front (face=0) state */
622 /* 0 = front-face, 1 = back-face */
623 assert(face
== 0 || face
== 1);
625 /* choose front or back face function, operator, etc */
626 /* XXX we could do these initializations once per primitive */
627 func
= softpipe
->depth_stencil
->stencil
[face
].func
;
628 failOp
= softpipe
->depth_stencil
->stencil
[face
].fail_op
;
629 zFailOp
= softpipe
->depth_stencil
->stencil
[face
].zfail_op
;
630 zPassOp
= softpipe
->depth_stencil
->stencil
[face
].zpass_op
;
631 ref
= softpipe
->stencil_ref
.ref_value
[face
];
632 wrtMask
= softpipe
->depth_stencil
->stencil
[face
].writemask
;
633 valMask
= softpipe
->depth_stencil
->stencil
[face
].valuemask
;
635 /* do the stencil test first */
637 unsigned passMask
, failMask
;
638 passMask
= do_stencil_test(data
, func
, ref
, valMask
);
639 failMask
= quad
->inout
.mask
& ~passMask
;
640 quad
->inout
.mask
&= passMask
;
642 if (failOp
!= PIPE_STENCIL_OP_KEEP
) {
643 apply_stencil_op(data
, failMask
, failOp
, ref
, wrtMask
);
647 if (quad
->inout
.mask
) {
648 /* now the pixels that passed the stencil test are depth tested */
649 if (softpipe
->depth_stencil
->depth
.enabled
) {
650 const unsigned origMask
= quad
->inout
.mask
;
652 depth_test_quad(qs
, data
, quad
); /* quad->mask is updated */
654 /* update stencil buffer values according to z pass/fail result */
655 if (zFailOp
!= PIPE_STENCIL_OP_KEEP
) {
656 const unsigned zFailMask
= origMask
& ~quad
->inout
.mask
;
657 apply_stencil_op(data
, zFailMask
, zFailOp
, ref
, wrtMask
);
660 if (zPassOp
!= PIPE_STENCIL_OP_KEEP
) {
661 const unsigned zPassMask
= origMask
& quad
->inout
.mask
;
662 apply_stencil_op(data
, zPassMask
, zPassOp
, ref
, wrtMask
);
666 /* no depth test, apply Zpass operator to stencil buffer values */
667 apply_stencil_op(data
, quad
->inout
.mask
, zPassOp
, ref
, wrtMask
);
673 #define ALPHATEST( FUNC, COMP ) \
675 alpha_test_quads_##FUNC( struct quad_stage *qs, \
676 struct quad_header *quads[], \
679 const float ref = qs->softpipe->depth_stencil->alpha.ref_value; \
680 const uint cbuf = 0; /* only output[0].alpha is tested */ \
681 unsigned pass_nr = 0; \
684 for (i = 0; i < nr; i++) { \
685 const float *aaaa = quads[i]->output.color[cbuf][3]; \
686 unsigned passMask = 0; \
688 if (aaaa[0] COMP ref) passMask |= (1 << 0); \
689 if (aaaa[1] COMP ref) passMask |= (1 << 1); \
690 if (aaaa[2] COMP ref) passMask |= (1 << 2); \
691 if (aaaa[3] COMP ref) passMask |= (1 << 3); \
693 quads[i]->inout.mask &= passMask; \
695 if (quads[i]->inout.mask) \
696 quads[pass_nr++] = quads[i]; \
704 ALPHATEST( EQUAL
, == )
705 ALPHATEST( LEQUAL
, <= )
706 ALPHATEST( GREATER
, > )
707 ALPHATEST( NOTEQUAL
, != )
708 ALPHATEST( GEQUAL
, >= )
711 /* XXX: Incorporate into shader using KILP.
714 alpha_test_quads(struct quad_stage
*qs
,
715 struct quad_header
*quads
[],
718 switch (qs
->softpipe
->depth_stencil
->alpha
.func
) {
720 return alpha_test_quads_LESS( qs
, quads
, nr
);
721 case PIPE_FUNC_EQUAL
:
722 return alpha_test_quads_EQUAL( qs
, quads
, nr
);
723 case PIPE_FUNC_LEQUAL
:
724 return alpha_test_quads_LEQUAL( qs
, quads
, nr
);
725 case PIPE_FUNC_GREATER
:
726 return alpha_test_quads_GREATER( qs
, quads
, nr
);
727 case PIPE_FUNC_NOTEQUAL
:
728 return alpha_test_quads_NOTEQUAL( qs
, quads
, nr
);
729 case PIPE_FUNC_GEQUAL
:
730 return alpha_test_quads_GEQUAL( qs
, quads
, nr
);
731 case PIPE_FUNC_ALWAYS
:
733 case PIPE_FUNC_NEVER
:
740 static unsigned mask_count
[16] =
763 * General depth/stencil test function. Used when there's no fast-path.
766 depth_test_quads_fallback(struct quad_stage
*qs
,
767 struct quad_header
*quads
[],
770 unsigned i
, pass
= 0;
771 const struct tgsi_shader_info
*fsInfo
= &qs
->softpipe
->fs_variant
->info
;
772 boolean interp_depth
= !fsInfo
->writes_z
;
773 boolean shader_stencil_ref
= fsInfo
->writes_stencil
;
774 struct depth_data data
;
776 data
.use_shader_stencil_refs
= FALSE
;
778 if (qs
->softpipe
->depth_stencil
->alpha
.enabled
) {
779 nr
= alpha_test_quads(qs
, quads
, nr
);
782 if (qs
->softpipe
->framebuffer
.zsbuf
&&
783 (qs
->softpipe
->depth_stencil
->depth
.enabled
||
784 qs
->softpipe
->depth_stencil
->stencil
[0].enabled
)) {
786 data
.ps
= qs
->softpipe
->framebuffer
.zsbuf
;
787 data
.format
= data
.ps
->format
;
788 data
.tile
= sp_get_cached_tile(qs
->softpipe
->zsbuf_cache
,
792 for (i
= 0; i
< nr
; i
++) {
793 get_depth_stencil_values(&data
, quads
[i
]);
795 if (qs
->softpipe
->depth_stencil
->depth
.enabled
) {
797 interpolate_quad_depth(quads
[i
]);
799 convert_quad_depth(&data
, quads
[i
]);
802 if (qs
->softpipe
->depth_stencil
->stencil
[0].enabled
) {
803 if (shader_stencil_ref
)
804 convert_quad_stencil(&data
, quads
[i
]);
806 depth_stencil_test_quad(qs
, &data
, quads
[i
]);
807 write_depth_stencil_values(&data
, quads
[i
]);
810 if (!depth_test_quad(qs
, &data
, quads
[i
]))
813 if (qs
->softpipe
->depth_stencil
->depth
.writemask
)
814 write_depth_stencil_values(&data
, quads
[i
]);
817 quads
[pass
++] = quads
[i
];
823 if (qs
->softpipe
->active_query_count
) {
824 for (i
= 0; i
< nr
; i
++)
825 qs
->softpipe
->occlusion_count
+= mask_count
[quads
[i
]->inout
.mask
];
829 qs
->next
->run(qs
->next
, quads
, nr
);
834 * Special-case Z testing for 16-bit Zbuffer and Z buffer writes enabled.
837 #define NAME depth_interp_z16_less_write
839 #include "sp_quad_depth_test_tmp.h"
841 #define NAME depth_interp_z16_equal_write
843 #include "sp_quad_depth_test_tmp.h"
845 #define NAME depth_interp_z16_lequal_write
847 #include "sp_quad_depth_test_tmp.h"
849 #define NAME depth_interp_z16_greater_write
851 #include "sp_quad_depth_test_tmp.h"
853 #define NAME depth_interp_z16_notequal_write
855 #include "sp_quad_depth_test_tmp.h"
857 #define NAME depth_interp_z16_gequal_write
859 #include "sp_quad_depth_test_tmp.h"
861 #define NAME depth_interp_z16_always_write
863 #include "sp_quad_depth_test_tmp.h"
868 depth_noop(struct quad_stage
*qs
,
869 struct quad_header
*quads
[],
872 qs
->next
->run(qs
->next
, quads
, nr
);
878 choose_depth_test(struct quad_stage
*qs
,
879 struct quad_header
*quads
[],
882 const struct tgsi_shader_info
*fsInfo
= &qs
->softpipe
->fs_variant
->info
;
884 boolean interp_depth
= !fsInfo
->writes_z
;
886 boolean alpha
= qs
->softpipe
->depth_stencil
->alpha
.enabled
;
888 boolean depth
= qs
->softpipe
->depth_stencil
->depth
.enabled
;
890 unsigned depthfunc
= qs
->softpipe
->depth_stencil
->depth
.func
;
892 boolean stencil
= qs
->softpipe
->depth_stencil
->stencil
[0].enabled
;
894 boolean depthwrite
= qs
->softpipe
->depth_stencil
->depth
.writemask
;
896 boolean occlusion
= qs
->softpipe
->active_query_count
;
898 if(!qs
->softpipe
->framebuffer
.zsbuf
)
899 depth
= depthwrite
= stencil
= FALSE
;
902 qs
->run
= depth_test_quads_fallback
;
904 /* look for special cases */
909 qs
->run
= depth_noop
;
918 if (qs
->softpipe
->framebuffer
.zsbuf
->format
== PIPE_FORMAT_Z16_UNORM
) {
920 case PIPE_FUNC_NEVER
:
921 qs
->run
= depth_test_quads_fallback
;
924 qs
->run
= depth_interp_z16_less_write
;
926 case PIPE_FUNC_EQUAL
:
927 qs
->run
= depth_interp_z16_equal_write
;
929 case PIPE_FUNC_LEQUAL
:
930 qs
->run
= depth_interp_z16_lequal_write
;
932 case PIPE_FUNC_GREATER
:
933 qs
->run
= depth_interp_z16_greater_write
;
935 case PIPE_FUNC_NOTEQUAL
:
936 qs
->run
= depth_interp_z16_notequal_write
;
938 case PIPE_FUNC_GEQUAL
:
939 qs
->run
= depth_interp_z16_gequal_write
;
941 case PIPE_FUNC_ALWAYS
:
942 qs
->run
= depth_interp_z16_always_write
;
945 qs
->run
= depth_test_quads_fallback
;
951 /* next quad/fragment stage */
952 qs
->run( qs
, quads
, nr
);
958 depth_test_begin(struct quad_stage
*qs
)
960 qs
->run
= choose_depth_test
;
961 qs
->next
->begin(qs
->next
);
966 depth_test_destroy(struct quad_stage
*qs
)
973 sp_quad_depth_test_stage(struct softpipe_context
*softpipe
)
975 struct quad_stage
*stage
= CALLOC_STRUCT(quad_stage
);
977 stage
->softpipe
= softpipe
;
978 stage
->begin
= depth_test_begin
;
979 stage
->run
= choose_depth_test
;
980 stage
->destroy
= depth_test_destroy
;