1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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 VMWARE 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 **************************************************************************/
30 * Depth/stencil testing to LLVM IR translation.
32 * To be done accurately/efficiently the depth/stencil test must be done with
33 * the same type/format of the depth/stencil buffer, which implies massaging
34 * the incoming depths to fit into place. Using a more straightforward
35 * type/format for depth/stencil values internally and only convert when
36 * flushing would avoid this, but it would most likely result in depth fighting
39 * We are free to use a different pixel layout though. Since our basic
40 * processing unit is a quad (2x2 pixel block) we store the depth/stencil
41 * values tiled, a quad at time. That is, a depth buffer containing
49 * will actually be stored in memory as
51 * Z11 Z12 Z21 Z22 Z13 Z14 Z23 Z24 ...
52 * Z31 Z32 Z41 Z42 Z33 Z34 Z43 Z44 ...
53 * ... ... ... ... ... ... ... ... ...
57 * Two-sided stencil test is supported but probably not as efficient as
58 * it could be. Currently, we use if/then/else constructs to do the
59 * operations for front vs. back-facing polygons. We could probably do
60 * both the front and back arithmetic then use a Select() instruction to
61 * choose the result depending on polyon orientation. We'd have to
62 * measure performance both ways and see which is better.
64 * @author Jose Fonseca <jfonseca@vmware.com>
67 #include "pipe/p_state.h"
68 #include "util/u_format.h"
70 #include "gallivm/lp_bld_type.h"
71 #include "gallivm/lp_bld_arit.h"
72 #include "gallivm/lp_bld_bitarit.h"
73 #include "gallivm/lp_bld_const.h"
74 #include "gallivm/lp_bld_logic.h"
75 #include "gallivm/lp_bld_flow.h"
76 #include "gallivm/lp_bld_intr.h"
77 #include "gallivm/lp_bld_debug.h"
78 #include "gallivm/lp_bld_swizzle.h"
80 #include "lp_bld_depth.h"
83 /** Used to select fields from pipe_stencil_state */
93 * Do the stencil test comparison (compare FB stencil values against ref value).
94 * This will be used twice when generating two-sided stencil code.
95 * \param stencil the front/back stencil state
96 * \param stencilRef the stencil reference value, replicated as a vector
97 * \param stencilVals vector of stencil values from framebuffer
98 * \return vector mask of pass/fail values (~0 or 0)
101 lp_build_stencil_test_single(struct lp_build_context
*bld
,
102 const struct pipe_stencil_state
*stencil
,
103 LLVMValueRef stencilRef
,
104 LLVMValueRef stencilVals
)
106 const unsigned stencilMax
= 255; /* XXX fix */
107 struct lp_type type
= bld
->type
;
112 assert(stencil
->enabled
);
114 if (stencil
->valuemask
!= stencilMax
) {
115 /* compute stencilRef = stencilRef & valuemask */
116 LLVMValueRef valuemask
= lp_build_const_int_vec(type
, stencil
->valuemask
);
117 stencilRef
= LLVMBuildAnd(bld
->builder
, stencilRef
, valuemask
, "");
118 /* compute stencilVals = stencilVals & valuemask */
119 stencilVals
= LLVMBuildAnd(bld
->builder
, stencilVals
, valuemask
, "");
122 res
= lp_build_cmp(bld
, stencil
->func
, stencilRef
, stencilVals
);
129 * Do the one or two-sided stencil test comparison.
130 * \sa lp_build_stencil_test_single
131 * \param front_facing an integer vector mask, indicating front (~0) or back
132 * (0) facing polygon. If NULL, assume front-facing.
135 lp_build_stencil_test(struct lp_build_context
*bld
,
136 const struct pipe_stencil_state stencil
[2],
137 LLVMValueRef stencilRefs
[2],
138 LLVMValueRef stencilVals
,
139 LLVMValueRef front_facing
)
143 assert(stencil
[0].enabled
);
145 /* do front face test */
146 res
= lp_build_stencil_test_single(bld
, &stencil
[0],
147 stencilRefs
[0], stencilVals
);
149 if (stencil
[1].enabled
&& front_facing
) {
150 /* do back face test */
151 LLVMValueRef back_res
;
153 back_res
= lp_build_stencil_test_single(bld
, &stencil
[1],
154 stencilRefs
[1], stencilVals
);
156 res
= lp_build_select(bld
, front_facing
, res
, back_res
);
164 * Apply the stencil operator (add/sub/keep/etc) to the given vector
166 * \return new stencil values vector
169 lp_build_stencil_op_single(struct lp_build_context
*bld
,
170 const struct pipe_stencil_state
*stencil
,
172 LLVMValueRef stencilRef
,
173 LLVMValueRef stencilVals
)
176 struct lp_type type
= bld
->type
;
178 LLVMValueRef max
= lp_build_const_int_vec(type
, 0xff);
185 stencil_op
= stencil
->fail_op
;
188 stencil_op
= stencil
->zfail_op
;
191 stencil_op
= stencil
->zpass_op
;
194 assert(0 && "Invalid stencil_op mode");
195 stencil_op
= PIPE_STENCIL_OP_KEEP
;
198 switch (stencil_op
) {
199 case PIPE_STENCIL_OP_KEEP
:
201 /* we can return early for this case */
203 case PIPE_STENCIL_OP_ZERO
:
206 case PIPE_STENCIL_OP_REPLACE
:
209 case PIPE_STENCIL_OP_INCR
:
210 res
= lp_build_add(bld
, stencilVals
, bld
->one
);
211 res
= lp_build_min(bld
, res
, max
);
213 case PIPE_STENCIL_OP_DECR
:
214 res
= lp_build_sub(bld
, stencilVals
, bld
->one
);
215 res
= lp_build_max(bld
, res
, bld
->zero
);
217 case PIPE_STENCIL_OP_INCR_WRAP
:
218 res
= lp_build_add(bld
, stencilVals
, bld
->one
);
219 res
= LLVMBuildAnd(bld
->builder
, res
, max
, "");
221 case PIPE_STENCIL_OP_DECR_WRAP
:
222 res
= lp_build_sub(bld
, stencilVals
, bld
->one
);
223 res
= LLVMBuildAnd(bld
->builder
, res
, max
, "");
225 case PIPE_STENCIL_OP_INVERT
:
226 res
= LLVMBuildNot(bld
->builder
, stencilVals
, "");
227 res
= LLVMBuildAnd(bld
->builder
, res
, max
, "");
230 assert(0 && "bad stencil op mode");
239 * Do the one or two-sided stencil test op/update.
242 lp_build_stencil_op(struct lp_build_context
*bld
,
243 const struct pipe_stencil_state stencil
[2],
245 LLVMValueRef stencilRefs
[2],
246 LLVMValueRef stencilVals
,
248 LLVMValueRef front_facing
)
253 assert(stencil
[0].enabled
);
255 /* do front face op */
256 res
= lp_build_stencil_op_single(bld
, &stencil
[0], op
,
257 stencilRefs
[0], stencilVals
);
259 if (stencil
[1].enabled
&& front_facing
) {
260 /* do back face op */
261 LLVMValueRef back_res
;
263 back_res
= lp_build_stencil_op_single(bld
, &stencil
[1], op
,
264 stencilRefs
[1], stencilVals
);
266 res
= lp_build_select(bld
, front_facing
, res
, back_res
);
269 if (stencil
->writemask
!= 0xff) {
270 /* mask &= stencil->writemask */
271 LLVMValueRef writemask
= lp_build_const_int_vec(bld
->type
, stencil
->writemask
);
272 mask
= LLVMBuildAnd(bld
->builder
, mask
, writemask
, "");
273 /* res = (res & mask) | (stencilVals & ~mask) */
274 res
= lp_build_select_bitwise(bld
, writemask
, res
, stencilVals
);
277 /* res = mask ? res : stencilVals */
278 res
= lp_build_select(bld
, mask
, res
, stencilVals
);
287 * Return a type appropriate for depth/stencil testing.
290 lp_depth_type(const struct util_format_description
*format_desc
,
296 assert(format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
);
297 assert(format_desc
->block
.width
== 1);
298 assert(format_desc
->block
.height
== 1);
300 swizzle
= format_desc
->swizzle
[0];
303 memset(&type
, 0, sizeof type
);
304 type
.width
= format_desc
->block
.bits
;
306 if(format_desc
->channel
[swizzle
].type
== UTIL_FORMAT_TYPE_FLOAT
) {
307 type
.floating
= TRUE
;
308 assert(swizzle
== 0);
309 assert(format_desc
->channel
[swizzle
].size
== format_desc
->block
.bits
);
311 else if(format_desc
->channel
[swizzle
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
312 assert(format_desc
->block
.bits
<= 32);
313 if(format_desc
->channel
[swizzle
].normalized
)
319 assert(type
.width
<= length
);
320 type
.length
= length
/ type
.width
;
327 * Compute bitmask and bit shift to apply to the incoming fragment Z values
328 * and the Z buffer values needed before doing the Z comparison.
330 * Note that we leave the Z bits in the position that we find them
331 * in the Z buffer (typically 0xffffff00 or 0x00ffffff). That lets us
332 * get by with fewer bit twiddling steps.
335 get_z_shift_and_mask(const struct util_format_description
*format_desc
,
336 unsigned *shift
, unsigned *mask
)
338 const unsigned total_bits
= format_desc
->block
.bits
;
341 unsigned padding_left
, padding_right
;
343 assert(format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
);
344 assert(format_desc
->block
.width
== 1);
345 assert(format_desc
->block
.height
== 1);
347 z_swizzle
= format_desc
->swizzle
[0];
349 if (z_swizzle
== UTIL_FORMAT_SWIZZLE_NONE
)
353 for (chan
= 0; chan
< z_swizzle
; ++chan
)
354 padding_right
+= format_desc
->channel
[chan
].size
;
357 total_bits
- (padding_right
+ format_desc
->channel
[z_swizzle
].size
);
359 if (padding_left
|| padding_right
) {
360 unsigned long long mask_left
= (1ULL << (total_bits
- padding_left
)) - 1;
361 unsigned long long mask_right
= (1ULL << (padding_right
)) - 1;
362 *mask
= mask_left
^ mask_right
;
368 *shift
= padding_left
;
375 * Compute bitmask and bit shift to apply to the framebuffer pixel values
376 * to put the stencil bits in the least significant position.
380 get_s_shift_and_mask(const struct util_format_description
*format_desc
,
381 unsigned *shift
, unsigned *mask
)
386 s_swizzle
= format_desc
->swizzle
[1];
388 if (s_swizzle
== UTIL_FORMAT_SWIZZLE_NONE
)
392 for (chan
= 0; chan
< s_swizzle
; chan
++)
393 *shift
+= format_desc
->channel
[chan
].size
;
395 sz
= format_desc
->channel
[s_swizzle
].size
;
396 *mask
= (1U << sz
) - 1U;
403 * Perform the occlusion test and increase the counter.
404 * Test the depth mask. Add the number of channel which has none zero mask
405 * into the occlusion counter. e.g. maskvalue is {-1, -1, -1, -1}.
406 * The counter will add 4.
408 * \param type holds element type of the mask vector.
409 * \param maskvalue is the depth test mask.
410 * \param counter is a pointer of the uint32 counter.
413 lp_build_occlusion_count(LLVMBuilderRef builder
,
415 LLVMValueRef maskvalue
,
416 LLVMValueRef counter
)
418 LLVMValueRef countmask
= lp_build_const_int_vec(type
, 1);
419 LLVMValueRef countv
= LLVMBuildAnd(builder
, maskvalue
, countmask
, "countv");
420 LLVMTypeRef i8v16
= LLVMVectorType(LLVMInt8Type(), 16);
421 LLVMValueRef counti
= LLVMBuildBitCast(builder
, countv
, i8v16
, "counti");
422 LLVMValueRef maskarray
[4] = {
423 LLVMConstInt(LLVMInt32Type(), 0, 0),
424 LLVMConstInt(LLVMInt32Type(), 4, 0),
425 LLVMConstInt(LLVMInt32Type(), 8, 0),
426 LLVMConstInt(LLVMInt32Type(), 12, 0),
428 LLVMValueRef shufflemask
= LLVMConstVector(maskarray
, 4);
429 LLVMValueRef shufflev
= LLVMBuildShuffleVector(builder
, counti
, LLVMGetUndef(i8v16
), shufflemask
, "shufflev");
430 LLVMValueRef shuffle
= LLVMBuildBitCast(builder
, shufflev
, LLVMInt32Type(), "shuffle");
431 LLVMValueRef count
= lp_build_intrinsic_unary(builder
, "llvm.ctpop.i32", LLVMInt32Type(), shuffle
);
432 LLVMValueRef orig
= LLVMBuildLoad(builder
, counter
, "orig");
433 LLVMValueRef incr
= LLVMBuildAdd(builder
, orig
, count
, "incr");
434 LLVMBuildStore(builder
, incr
, counter
);
440 * Generate code for performing depth and/or stencil tests.
441 * We operate on a vector of values (typically a 2x2 quad).
443 * \param depth the depth test state
444 * \param stencil the front/back stencil state
445 * \param type the data type of the fragment depth/stencil values
446 * \param format_desc description of the depth/stencil surface
447 * \param mask the alive/dead pixel mask for the quad (vector)
448 * \param stencil_refs the front/back stencil ref values (scalar)
449 * \param z_src the incoming depth/stencil values (a 2x2 quad)
450 * \param zs_dst_ptr pointer to depth/stencil values in framebuffer
451 * \param facing contains float value indicating front/back facing polygon
454 lp_build_depth_stencil_test(LLVMBuilderRef builder
,
455 const struct pipe_depth_state
*depth
,
456 const struct pipe_stencil_state stencil
[2],
458 const struct util_format_description
*format_desc
,
459 struct lp_build_mask_context
*mask
,
460 LLVMValueRef stencil_refs
[2],
462 LLVMValueRef zs_dst_ptr
,
464 LLVMValueRef counter
)
466 struct lp_build_context bld
;
467 struct lp_build_context sbld
;
468 struct lp_type s_type
;
469 LLVMValueRef zs_dst
, z_dst
= NULL
;
470 LLVMValueRef stencil_vals
= NULL
;
471 LLVMValueRef z_bitmask
= NULL
, stencil_shift
= NULL
;
472 LLVMValueRef z_pass
= NULL
, s_pass_mask
= NULL
;
473 LLVMValueRef orig_mask
= mask
->value
;
474 LLVMValueRef front_facing
= NULL
;
476 /* Sanity checking */
478 const unsigned z_swizzle
= format_desc
->swizzle
[0];
479 const unsigned s_swizzle
= format_desc
->swizzle
[1];
481 assert(z_swizzle
!= UTIL_FORMAT_SWIZZLE_NONE
||
482 s_swizzle
!= UTIL_FORMAT_SWIZZLE_NONE
);
484 assert(depth
->enabled
|| stencil
[0].enabled
);
486 assert(format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
);
487 assert(format_desc
->block
.width
== 1);
488 assert(format_desc
->block
.height
== 1);
490 if (stencil
[0].enabled
) {
491 assert(format_desc
->format
== PIPE_FORMAT_Z24_UNORM_S8_USCALED
||
492 format_desc
->format
== PIPE_FORMAT_S8_USCALED_Z24_UNORM
);
495 assert(z_swizzle
< 4);
496 assert(format_desc
->block
.bits
== type
.width
);
498 assert(z_swizzle
== 0);
499 assert(format_desc
->channel
[z_swizzle
].type
==
500 UTIL_FORMAT_TYPE_FLOAT
);
501 assert(format_desc
->channel
[z_swizzle
].size
==
502 format_desc
->block
.bits
);
505 assert(format_desc
->channel
[z_swizzle
].type
==
506 UTIL_FORMAT_TYPE_UNSIGNED
);
507 assert(format_desc
->channel
[z_swizzle
].normalized
);
515 /* Setup build context for Z vals */
516 lp_build_context_init(&bld
, builder
, type
);
518 /* Setup build context for stencil vals */
519 s_type
= lp_type_int_vec(type
.width
);
520 lp_build_context_init(&sbld
, builder
, s_type
);
522 /* Load current z/stencil value from z/stencil buffer */
523 zs_dst
= LLVMBuildLoad(builder
, zs_dst_ptr
, "");
525 lp_build_name(zs_dst
, "zsbufval");
528 /* Compute and apply the Z/stencil bitmasks and shifts.
531 unsigned z_shift
, z_mask
;
532 unsigned s_shift
, s_mask
;
534 if (get_z_shift_and_mask(format_desc
, &z_shift
, &z_mask
)) {
536 LLVMValueRef shift
= lp_build_const_int_vec(type
, z_shift
);
537 z_src
= LLVMBuildLShr(builder
, z_src
, shift
, "");
540 if (z_mask
!= 0xffffffff) {
541 LLVMValueRef mask
= lp_build_const_int_vec(type
, z_mask
);
542 z_src
= LLVMBuildAnd(builder
, z_src
, mask
, "");
543 z_dst
= LLVMBuildAnd(builder
, zs_dst
, mask
, "");
544 z_bitmask
= mask
; /* used below */
550 lp_build_name(z_dst
, "zsbuf.z");
553 if (get_s_shift_and_mask(format_desc
, &s_shift
, &s_mask
)) {
555 LLVMValueRef shift
= lp_build_const_int_vec(type
, s_shift
);
556 stencil_vals
= LLVMBuildLShr(builder
, zs_dst
, shift
, "");
557 stencil_shift
= shift
; /* used below */
560 stencil_vals
= zs_dst
;
563 if (s_mask
!= 0xffffffff) {
564 LLVMValueRef mask
= lp_build_const_int_vec(type
, s_mask
);
565 stencil_vals
= LLVMBuildAnd(builder
, stencil_vals
, mask
, "");
568 lp_build_name(stencil_vals
, "stencil");
572 if (stencil
[0].enabled
) {
575 LLVMValueRef zero
= LLVMConstReal(LLVMFloatType(), 0.0);
577 /* front_facing = face > 0.0 ? ~0 : 0 */
578 front_facing
= LLVMBuildFCmp(builder
, LLVMRealUGT
, face
, zero
, "");
579 front_facing
= LLVMBuildSExt(builder
, front_facing
,
580 LLVMIntType(bld
.type
.length
*bld
.type
.width
),
582 front_facing
= LLVMBuildBitCast(builder
, front_facing
,
583 bld
.int_vec_type
, "");
586 /* convert scalar stencil refs into vectors */
587 stencil_refs
[0] = lp_build_broadcast_scalar(&bld
, stencil_refs
[0]);
588 stencil_refs
[1] = lp_build_broadcast_scalar(&bld
, stencil_refs
[1]);
590 s_pass_mask
= lp_build_stencil_test(&sbld
, stencil
,
591 stencil_refs
, stencil_vals
,
594 /* apply stencil-fail operator */
596 LLVMValueRef s_fail_mask
= lp_build_andnot(&bld
, orig_mask
, s_pass_mask
);
597 stencil_vals
= lp_build_stencil_op(&sbld
, stencil
, S_FAIL_OP
,
598 stencil_refs
, stencil_vals
,
599 s_fail_mask
, front_facing
);
603 if (depth
->enabled
) {
604 /* compare src Z to dst Z, returning 'pass' mask */
605 z_pass
= lp_build_cmp(&bld
, depth
->func
, z_src
, z_dst
);
607 if (!stencil
[0].enabled
) {
608 /* We can potentially skip all remaining operations here, but only
609 * if stencil is disabled because we still need to update the stencil
610 * buffer values. Don't need to update Z buffer values.
612 lp_build_mask_update(mask
, z_pass
);
615 if (depth
->writemask
) {
616 LLVMValueRef zselectmask
= mask
->value
;
618 /* mask off bits that failed Z test */
619 zselectmask
= LLVMBuildAnd(builder
, zselectmask
, z_pass
, "");
621 /* mask off bits that failed stencil test */
623 zselectmask
= LLVMBuildAnd(builder
, zselectmask
, s_pass_mask
, "");
626 /* if combined Z/stencil format, mask off the stencil bits */
628 zselectmask
= LLVMBuildAnd(builder
, zselectmask
, z_bitmask
, "");
631 /* Mix the old and new Z buffer values.
632 * z_dst[i] = (zselectmask[i] & z_src[i]) | (~zselectmask[i] & z_dst[i])
634 z_dst
= lp_build_select_bitwise(&bld
, zselectmask
, z_src
, z_dst
);
637 if (stencil
[0].enabled
) {
638 /* update stencil buffer values according to z pass/fail result */
639 LLVMValueRef z_fail_mask
, z_pass_mask
;
641 /* apply Z-fail operator */
642 z_fail_mask
= lp_build_andnot(&bld
, orig_mask
, z_pass
);
643 stencil_vals
= lp_build_stencil_op(&sbld
, stencil
, Z_FAIL_OP
,
644 stencil_refs
, stencil_vals
,
645 z_fail_mask
, front_facing
);
647 /* apply Z-pass operator */
648 z_pass_mask
= LLVMBuildAnd(bld
.builder
, orig_mask
, z_pass
, "");
649 stencil_vals
= lp_build_stencil_op(&sbld
, stencil
, Z_PASS_OP
,
650 stencil_refs
, stencil_vals
,
651 z_pass_mask
, front_facing
);
655 /* No depth test: apply Z-pass operator to stencil buffer values which
656 * passed the stencil test.
658 s_pass_mask
= LLVMBuildAnd(bld
.builder
, orig_mask
, s_pass_mask
, "");
659 stencil_vals
= lp_build_stencil_op(&sbld
, stencil
, Z_PASS_OP
,
660 stencil_refs
, stencil_vals
,
661 s_pass_mask
, front_facing
);
664 /* The Z bits are already in the right place but we may need to shift the
665 * stencil bits before ORing Z with Stencil to make the final pixel value.
667 if (stencil_vals
&& stencil_shift
)
668 stencil_vals
= LLVMBuildShl(bld
.builder
, stencil_vals
,
671 /* Finally, merge/store the z/stencil values */
672 if ((depth
->enabled
&& depth
->writemask
) ||
673 (stencil
[0].enabled
&& stencil
[0].writemask
)) {
675 if (z_dst
&& stencil_vals
)
676 zs_dst
= LLVMBuildOr(bld
.builder
, z_dst
, stencil_vals
, "");
680 zs_dst
= stencil_vals
;
682 LLVMBuildStore(builder
, zs_dst
, zs_dst_ptr
);
686 lp_build_mask_update(mask
, s_pass_mask
);
688 if (depth
->enabled
&& stencil
[0].enabled
)
689 lp_build_mask_update(mask
, z_pass
);
692 lp_build_occlusion_count(builder
, type
, mask
->value
, counter
);