1 /**************************************************************************
3 * Copyright 2009-2010 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 * Since we're using linear layout for everything, but we need to deal with
40 * 2x2 quads, we need to load/store multiple values and swizzle them into
41 * place (we could avoid this by doing depth/stencil testing in linear format,
42 * which would be easy for late depth/stencil test as we could do that after
43 * the fragment shader loop just as we do for color buffers, but more tricky
44 * for early depth test as we'd need both masks and interpolated depth in
48 * @author Jose Fonseca <jfonseca@vmware.com>
49 * @author Brian Paul <jfonseca@vmware.com>
52 #include "pipe/p_state.h"
53 #include "util/u_format.h"
54 #include "util/u_cpu_detect.h"
56 #include "gallivm/lp_bld_type.h"
57 #include "gallivm/lp_bld_arit.h"
58 #include "gallivm/lp_bld_bitarit.h"
59 #include "gallivm/lp_bld_const.h"
60 #include "gallivm/lp_bld_conv.h"
61 #include "gallivm/lp_bld_logic.h"
62 #include "gallivm/lp_bld_flow.h"
63 #include "gallivm/lp_bld_intr.h"
64 #include "gallivm/lp_bld_debug.h"
65 #include "gallivm/lp_bld_swizzle.h"
66 #include "gallivm/lp_bld_pack.h"
68 #include "lp_bld_depth.h"
71 /** Used to select fields from pipe_stencil_state */
81 * Do the stencil test comparison (compare FB stencil values against ref value).
82 * This will be used twice when generating two-sided stencil code.
83 * \param stencil the front/back stencil state
84 * \param stencilRef the stencil reference value, replicated as a vector
85 * \param stencilVals vector of stencil values from framebuffer
86 * \return vector mask of pass/fail values (~0 or 0)
89 lp_build_stencil_test_single(struct lp_build_context
*bld
,
90 const struct pipe_stencil_state
*stencil
,
91 LLVMValueRef stencilRef
,
92 LLVMValueRef stencilVals
)
94 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
95 const unsigned stencilMax
= 255; /* XXX fix */
96 struct lp_type type
= bld
->type
;
100 * SSE2 has intrinsics for signed comparisons, but not unsigned ones. Values
101 * are between 0..255 so ensure we generate the fastest comparisons for
104 if (type
.width
<= 8) {
110 assert(stencil
->enabled
);
112 if (stencil
->valuemask
!= stencilMax
) {
113 /* compute stencilRef = stencilRef & valuemask */
114 LLVMValueRef valuemask
= lp_build_const_int_vec(bld
->gallivm
, type
, stencil
->valuemask
);
115 stencilRef
= LLVMBuildAnd(builder
, stencilRef
, valuemask
, "");
116 /* compute stencilVals = stencilVals & valuemask */
117 stencilVals
= LLVMBuildAnd(builder
, stencilVals
, valuemask
, "");
120 res
= lp_build_cmp(bld
, stencil
->func
, stencilRef
, stencilVals
);
127 * Do the one or two-sided stencil test comparison.
128 * \sa lp_build_stencil_test_single
129 * \param front_facing an integer vector mask, indicating front (~0) or back
130 * (0) facing polygon. If NULL, assume front-facing.
133 lp_build_stencil_test(struct lp_build_context
*bld
,
134 const struct pipe_stencil_state stencil
[2],
135 LLVMValueRef stencilRefs
[2],
136 LLVMValueRef stencilVals
,
137 LLVMValueRef front_facing
)
141 assert(stencil
[0].enabled
);
143 /* do front face test */
144 res
= lp_build_stencil_test_single(bld
, &stencil
[0],
145 stencilRefs
[0], stencilVals
);
147 if (stencil
[1].enabled
&& front_facing
!= NULL
) {
148 /* do back face test */
149 LLVMValueRef back_res
;
151 back_res
= lp_build_stencil_test_single(bld
, &stencil
[1],
152 stencilRefs
[1], stencilVals
);
154 res
= lp_build_select(bld
, front_facing
, res
, back_res
);
162 * Apply the stencil operator (add/sub/keep/etc) to the given vector
164 * \return new stencil values vector
167 lp_build_stencil_op_single(struct lp_build_context
*bld
,
168 const struct pipe_stencil_state
*stencil
,
170 LLVMValueRef stencilRef
,
171 LLVMValueRef stencilVals
)
174 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
175 struct lp_type type
= bld
->type
;
177 LLVMValueRef max
= lp_build_const_int_vec(bld
->gallivm
, type
, 0xff);
184 stencil_op
= stencil
->fail_op
;
187 stencil_op
= stencil
->zfail_op
;
190 stencil_op
= stencil
->zpass_op
;
193 assert(0 && "Invalid stencil_op mode");
194 stencil_op
= PIPE_STENCIL_OP_KEEP
;
197 switch (stencil_op
) {
198 case PIPE_STENCIL_OP_KEEP
:
200 /* we can return early for this case */
202 case PIPE_STENCIL_OP_ZERO
:
205 case PIPE_STENCIL_OP_REPLACE
:
208 case PIPE_STENCIL_OP_INCR
:
209 res
= lp_build_add(bld
, stencilVals
, bld
->one
);
210 res
= lp_build_min(bld
, res
, max
);
212 case PIPE_STENCIL_OP_DECR
:
213 res
= lp_build_sub(bld
, stencilVals
, bld
->one
);
214 res
= lp_build_max(bld
, res
, bld
->zero
);
216 case PIPE_STENCIL_OP_INCR_WRAP
:
217 res
= lp_build_add(bld
, stencilVals
, bld
->one
);
218 res
= LLVMBuildAnd(builder
, res
, max
, "");
220 case PIPE_STENCIL_OP_DECR_WRAP
:
221 res
= lp_build_sub(bld
, stencilVals
, bld
->one
);
222 res
= LLVMBuildAnd(builder
, res
, max
, "");
224 case PIPE_STENCIL_OP_INVERT
:
225 res
= LLVMBuildNot(builder
, stencilVals
, "");
226 res
= LLVMBuildAnd(builder
, res
, max
, "");
229 assert(0 && "bad stencil op mode");
238 * Do the one or two-sided stencil test op/update.
241 lp_build_stencil_op(struct lp_build_context
*bld
,
242 const struct pipe_stencil_state stencil
[2],
244 LLVMValueRef stencilRefs
[2],
245 LLVMValueRef stencilVals
,
247 LLVMValueRef front_facing
)
250 LLVMBuilderRef builder
= bld
->gallivm
->builder
;
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
!= NULL
) {
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
[0].writemask
!= 0xff ||
270 (stencil
[1].enabled
&& front_facing
!= NULL
&& stencil
[1].writemask
!= 0xff)) {
271 /* mask &= stencil[0].writemask */
272 LLVMValueRef writemask
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
,
273 stencil
[0].writemask
);
274 if (stencil
[1].enabled
&& stencil
[1].writemask
!= stencil
[0].writemask
&& front_facing
!= NULL
) {
275 LLVMValueRef back_writemask
= lp_build_const_int_vec(bld
->gallivm
, bld
->type
,
276 stencil
[1].writemask
);
277 writemask
= lp_build_select(bld
, front_facing
, writemask
, back_writemask
);
280 mask
= LLVMBuildAnd(builder
, mask
, writemask
, "");
281 /* res = (res & mask) | (stencilVals & ~mask) */
282 res
= lp_build_select_bitwise(bld
, mask
, res
, stencilVals
);
285 /* res = mask ? res : stencilVals */
286 res
= lp_build_select(bld
, mask
, res
, stencilVals
);
295 * Return a type that matches the depth/stencil format.
298 lp_depth_type(const struct util_format_description
*format_desc
,
304 assert(format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
);
305 assert(format_desc
->block
.width
== 1);
306 assert(format_desc
->block
.height
== 1);
308 memset(&type
, 0, sizeof type
);
309 type
.width
= format_desc
->block
.bits
;
311 z_swizzle
= format_desc
->swizzle
[0];
313 if (format_desc
->channel
[z_swizzle
].type
== UTIL_FORMAT_TYPE_FLOAT
) {
314 type
.floating
= TRUE
;
315 assert(z_swizzle
== 0);
316 assert(format_desc
->channel
[z_swizzle
].size
== 32);
318 else if(format_desc
->channel
[z_swizzle
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
319 assert(format_desc
->block
.bits
<= 32);
320 assert(format_desc
->channel
[z_swizzle
].normalized
);
321 if (format_desc
->channel
[z_swizzle
].size
< format_desc
->block
.bits
) {
322 /* Prefer signed integers when possible, as SSE has less support
323 * for unsigned comparison;
332 type
.length
= length
;
339 * Compute bitmask and bit shift to apply to the incoming fragment Z values
340 * and the Z buffer values needed before doing the Z comparison.
342 * Note that we leave the Z bits in the position that we find them
343 * in the Z buffer (typically 0xffffff00 or 0x00ffffff). That lets us
344 * get by with fewer bit twiddling steps.
347 get_z_shift_and_mask(const struct util_format_description
*format_desc
,
348 unsigned *shift
, unsigned *width
, unsigned *mask
)
353 unsigned padding_left
, padding_right
;
355 assert(format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
);
356 assert(format_desc
->block
.width
== 1);
357 assert(format_desc
->block
.height
== 1);
359 /* 64bit d/s format is special already extracted 32 bits */
360 total_bits
= format_desc
->block
.bits
> 32 ? 32 : format_desc
->block
.bits
;
362 z_swizzle
= format_desc
->swizzle
[0];
364 if (z_swizzle
== UTIL_FORMAT_SWIZZLE_NONE
)
367 *width
= format_desc
->channel
[z_swizzle
].size
;
370 for (chan
= 0; chan
< z_swizzle
; ++chan
)
371 padding_right
+= format_desc
->channel
[chan
].size
;
374 total_bits
- (padding_right
+ *width
);
376 if (padding_left
|| padding_right
) {
377 unsigned long long mask_left
= (1ULL << (total_bits
- padding_left
)) - 1;
378 unsigned long long mask_right
= (1ULL << (padding_right
)) - 1;
379 *mask
= mask_left
^ mask_right
;
385 *shift
= padding_right
;
392 * Compute bitmask and bit shift to apply to the framebuffer pixel values
393 * to put the stencil bits in the least significant position.
397 get_s_shift_and_mask(const struct util_format_description
*format_desc
,
398 unsigned *shift
, unsigned *mask
)
403 s_swizzle
= format_desc
->swizzle
[1];
405 if (s_swizzle
== UTIL_FORMAT_SWIZZLE_NONE
)
408 /* just special case 64bit d/s format */
409 if (format_desc
->block
.bits
> 32) {
410 assert(format_desc
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
);
417 for (chan
= 0; chan
< s_swizzle
; chan
++)
418 *shift
+= format_desc
->channel
[chan
].size
;
420 sz
= format_desc
->channel
[s_swizzle
].size
;
421 *mask
= (1U << sz
) - 1U;
428 * Perform the occlusion test and increase the counter.
429 * Test the depth mask. Add the number of channel which has none zero mask
430 * into the occlusion counter. e.g. maskvalue is {-1, -1, -1, -1}.
431 * The counter will add 4.
433 * \param type holds element type of the mask vector.
434 * \param maskvalue is the depth test mask.
435 * \param counter is a pointer of the uint32 counter.
438 lp_build_occlusion_count(struct gallivm_state
*gallivm
,
440 LLVMValueRef maskvalue
,
441 LLVMValueRef counter
)
443 LLVMBuilderRef builder
= gallivm
->builder
;
444 LLVMContextRef context
= gallivm
->context
;
445 LLVMValueRef countmask
= lp_build_const_int_vec(gallivm
, type
, 1);
446 LLVMValueRef count
, newcount
;
448 assert(type
.length
<= 16);
449 assert(type
.floating
);
451 if(util_cpu_caps
.has_sse
&& type
.length
== 4) {
452 const char *movmskintr
= "llvm.x86.sse.movmsk.ps";
453 const char *popcntintr
= "llvm.ctpop.i32";
454 LLVMValueRef bits
= LLVMBuildBitCast(builder
, maskvalue
,
455 lp_build_vec_type(gallivm
, type
), "");
456 bits
= lp_build_intrinsic_unary(builder
, movmskintr
,
457 LLVMInt32TypeInContext(context
), bits
);
458 count
= lp_build_intrinsic_unary(builder
, popcntintr
,
459 LLVMInt32TypeInContext(context
), bits
);
461 else if(util_cpu_caps
.has_avx
&& type
.length
== 8) {
462 const char *movmskintr
= "llvm.x86.avx.movmsk.ps.256";
463 const char *popcntintr
= "llvm.ctpop.i32";
464 LLVMValueRef bits
= LLVMBuildBitCast(builder
, maskvalue
,
465 lp_build_vec_type(gallivm
, type
), "");
466 bits
= lp_build_intrinsic_unary(builder
, movmskintr
,
467 LLVMInt32TypeInContext(context
), bits
);
468 count
= lp_build_intrinsic_unary(builder
, popcntintr
,
469 LLVMInt32TypeInContext(context
), bits
);
473 LLVMValueRef countv
= LLVMBuildAnd(builder
, maskvalue
, countmask
, "countv");
474 LLVMTypeRef counttype
= LLVMIntTypeInContext(context
, type
.length
* 8);
475 LLVMTypeRef i8vntype
= LLVMVectorType(LLVMInt8TypeInContext(context
), type
.length
* 4);
476 LLVMValueRef shufflev
, countd
;
477 LLVMValueRef shuffles
[16];
478 const char *popcntintr
= NULL
;
480 countv
= LLVMBuildBitCast(builder
, countv
, i8vntype
, "");
482 for (i
= 0; i
< type
.length
; i
++) {
483 shuffles
[i
] = lp_build_const_int32(gallivm
, 4*i
);
486 shufflev
= LLVMConstVector(shuffles
, type
.length
);
487 countd
= LLVMBuildShuffleVector(builder
, countv
, LLVMGetUndef(i8vntype
), shufflev
, "");
488 countd
= LLVMBuildBitCast(builder
, countd
, counttype
, "countd");
492 * this is bad on cpus without popcount (on x86 supported by intel
493 * nehalem, amd barcelona, and up - not tied to sse42).
494 * Would be much faster to just sum the 4 elements of the vector with
495 * some horizontal add (shuffle/add/shuffle/add after the initial and).
497 switch (type
.length
) {
499 popcntintr
= "llvm.ctpop.i32";
502 popcntintr
= "llvm.ctpop.i64";
505 popcntintr
= "llvm.ctpop.i128";
510 count
= lp_build_intrinsic_unary(builder
, popcntintr
, counttype
, countd
);
512 if (type
.length
> 4) {
513 count
= LLVMBuildTrunc(builder
, count
, LLVMIntTypeInContext(context
, 32), "");
516 newcount
= LLVMBuildLoad(builder
, counter
, "origcount");
517 newcount
= LLVMBuildAdd(builder
, newcount
, count
, "newcount");
518 LLVMBuildStore(builder
, newcount
, counter
);
523 * Load depth/stencil values.
524 * The stored values are linear, swizzle them.
526 * \param type the data type of the fragment depth/stencil values
527 * \param format_desc description of the depth/stencil surface
528 * \param loop_counter the current loop iteration
529 * \param depth_ptr pointer to the depth/stencil values of this 4x4 block
530 * \param depth_stride stride of the depth/stencil buffer
531 * \param z_fb contains z values loaded from fb (may include padding)
532 * \param s_fb contains s values loaded from fb (may include padding)
535 lp_build_depth_stencil_load_swizzled(struct gallivm_state
*gallivm
,
536 struct lp_type z_src_type
,
537 const struct util_format_description
*format_desc
,
538 LLVMValueRef depth_ptr
,
539 LLVMValueRef depth_stride
,
542 LLVMValueRef loop_counter
)
544 LLVMBuilderRef builder
= gallivm
->builder
;
545 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
/ 4];
546 LLVMValueRef zs_dst1
, zs_dst2
;
547 LLVMValueRef zs_dst_ptr
;
548 LLVMValueRef depth_offset1
, depth_offset2
;
549 LLVMTypeRef load_ptr_type
;
550 unsigned depth_bytes
= format_desc
->block
.bits
/ 8;
551 struct lp_type zs_type
= lp_depth_type(format_desc
, z_src_type
.length
);
552 struct lp_type zs_load_type
= zs_type
;
554 zs_load_type
.length
= zs_load_type
.length
/ 2;
555 load_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, zs_load_type
), 0);
557 if (z_src_type
.length
== 4) {
559 LLVMValueRef looplsb
= LLVMBuildAnd(builder
, loop_counter
,
560 lp_build_const_int32(gallivm
, 1), "");
561 LLVMValueRef loopmsb
= LLVMBuildAnd(builder
, loop_counter
,
562 lp_build_const_int32(gallivm
, 2), "");
563 LLVMValueRef offset2
= LLVMBuildMul(builder
, loopmsb
,
565 depth_offset1
= LLVMBuildMul(builder
, looplsb
,
566 lp_build_const_int32(gallivm
, depth_bytes
* 2), "");
567 depth_offset1
= LLVMBuildAdd(builder
, depth_offset1
, offset2
, "");
569 /* just concatenate the loaded 2x2 values into 4-wide vector */
570 for (i
= 0; i
< 4; i
++) {
571 shuffles
[i
] = lp_build_const_int32(gallivm
, i
);
576 LLVMValueRef loopx2
= LLVMBuildShl(builder
, loop_counter
,
577 lp_build_const_int32(gallivm
, 1), "");
578 assert(z_src_type
.length
== 8);
579 depth_offset1
= LLVMBuildMul(builder
, loopx2
, depth_stride
, "");
581 * We load 2x4 values, and need to swizzle them (order
582 * 0,1,4,5,2,3,6,7) - not so hot with avx unfortunately.
584 for (i
= 0; i
< 8; i
++) {
585 shuffles
[i
] = lp_build_const_int32(gallivm
, (i
&1) + (i
&2) * 2 + (i
&4) / 2);
589 depth_offset2
= LLVMBuildAdd(builder
, depth_offset1
, depth_stride
, "");
591 /* Load current z/stencil values from z/stencil buffer */
592 zs_dst_ptr
= LLVMBuildGEP(builder
, depth_ptr
, &depth_offset1
, 1, "");
593 zs_dst_ptr
= LLVMBuildBitCast(builder
, zs_dst_ptr
, load_ptr_type
, "");
594 zs_dst1
= LLVMBuildLoad(builder
, zs_dst_ptr
, "");
595 zs_dst_ptr
= LLVMBuildGEP(builder
, depth_ptr
, &depth_offset2
, 1, "");
596 zs_dst_ptr
= LLVMBuildBitCast(builder
, zs_dst_ptr
, load_ptr_type
, "");
597 zs_dst2
= LLVMBuildLoad(builder
, zs_dst_ptr
, "");
599 *z_fb
= LLVMBuildShuffleVector(builder
, zs_dst1
, zs_dst2
,
600 LLVMConstVector(shuffles
, zs_type
.length
), "");
603 if (format_desc
->block
.bits
< z_src_type
.width
) {
604 /* Extend destination ZS values (e.g., when reading from Z16_UNORM) */
605 *z_fb
= LLVMBuildZExt(builder
, *z_fb
,
606 lp_build_int_vec_type(gallivm
, z_src_type
), "");
609 else if (format_desc
->block
.bits
> 32) {
610 /* rely on llvm to handle too wide vector we have here nicely */
612 struct lp_type typex2
= zs_type
;
613 struct lp_type s_type
= zs_type
;
614 LLVMValueRef shuffles1
[LP_MAX_VECTOR_LENGTH
/ 4];
615 LLVMValueRef shuffles2
[LP_MAX_VECTOR_LENGTH
/ 4];
618 typex2
.width
= typex2
.width
/ 2;
619 typex2
.length
= typex2
.length
* 2;
620 s_type
.width
= s_type
.width
/ 2;
623 tmp
= LLVMBuildBitCast(builder
, *z_fb
,
624 lp_build_vec_type(gallivm
, typex2
), "");
626 for (i
= 0; i
< zs_type
.length
; i
++) {
627 shuffles1
[i
] = lp_build_const_int32(gallivm
, i
* 2);
628 shuffles2
[i
] = lp_build_const_int32(gallivm
, i
* 2 + 1);
630 *z_fb
= LLVMBuildShuffleVector(builder
, tmp
, tmp
,
631 LLVMConstVector(shuffles1
, zs_type
.length
), "");
632 *s_fb
= LLVMBuildShuffleVector(builder
, tmp
, tmp
,
633 LLVMConstVector(shuffles2
, zs_type
.length
), "");
634 *s_fb
= LLVMBuildBitCast(builder
, *s_fb
,
635 lp_build_vec_type(gallivm
, s_type
), "");
636 lp_build_name(*s_fb
, "s_dst");
639 lp_build_name(*z_fb
, "z_dst");
640 lp_build_name(*s_fb
, "s_dst");
641 lp_build_name(*z_fb
, "z_dst");
645 * Store depth/stencil values.
646 * Incoming values are swizzled (typically n 2x2 quads), stored linear.
647 * If there's a mask it will do select/store otherwise just store.
649 * \param type the data type of the fragment depth/stencil values
650 * \param format_desc description of the depth/stencil surface
651 * \param mask the alive/dead pixel mask for the quad (vector)
652 * \param z_fb z values read from fb (with padding)
653 * \param s_fb s values read from fb (with padding)
654 * \param loop_counter the current loop iteration
655 * \param depth_ptr pointer to the depth/stencil values of this 4x4 block
656 * \param depth_stride stride of the depth/stencil buffer
657 * \param z_value the depth values to store (with padding)
658 * \param s_value the stencil values to store (with padding)
661 lp_build_depth_stencil_write_swizzled(struct gallivm_state
*gallivm
,
662 struct lp_type z_src_type
,
663 const struct util_format_description
*format_desc
,
664 struct lp_build_mask_context
*mask
,
667 LLVMValueRef loop_counter
,
668 LLVMValueRef depth_ptr
,
669 LLVMValueRef depth_stride
,
670 LLVMValueRef z_value
,
671 LLVMValueRef s_value
)
673 struct lp_build_context z_bld
;
674 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
/ 4];
675 LLVMBuilderRef builder
= gallivm
->builder
;
676 LLVMValueRef mask_value
= NULL
;
677 LLVMValueRef zs_dst1
, zs_dst2
;
678 LLVMValueRef zs_dst_ptr1
, zs_dst_ptr2
;
679 LLVMValueRef depth_offset1
, depth_offset2
;
680 LLVMTypeRef load_ptr_type
;
681 unsigned depth_bytes
= format_desc
->block
.bits
/ 8;
682 struct lp_type zs_type
= lp_depth_type(format_desc
, z_src_type
.length
);
683 struct lp_type zs_load_type
= zs_type
;
685 zs_load_type
.length
= zs_load_type
.length
/ 2;
686 load_ptr_type
= LLVMPointerType(lp_build_vec_type(gallivm
, zs_load_type
), 0);
688 if (zs_type
.width
> 32)
691 lp_build_context_init(&z_bld
, gallivm
, zs_type
);
694 * This is far from ideal, at least for late depth write we should do this
695 * outside the fs loop to avoid all the swizzle stuff.
697 if (z_src_type
.length
== 4) {
698 LLVMValueRef looplsb
= LLVMBuildAnd(builder
, loop_counter
,
699 lp_build_const_int32(gallivm
, 1), "");
700 LLVMValueRef loopmsb
= LLVMBuildAnd(builder
, loop_counter
,
701 lp_build_const_int32(gallivm
, 2), "");
702 LLVMValueRef offset2
= LLVMBuildMul(builder
, loopmsb
,
704 depth_offset1
= LLVMBuildMul(builder
, looplsb
,
705 lp_build_const_int32(gallivm
, depth_bytes
* 2), "");
706 depth_offset1
= LLVMBuildAdd(builder
, depth_offset1
, offset2
, "");
710 LLVMValueRef loopx2
= LLVMBuildShl(builder
, loop_counter
,
711 lp_build_const_int32(gallivm
, 1), "");
712 assert(z_src_type
.length
== 8);
713 depth_offset1
= LLVMBuildMul(builder
, loopx2
, depth_stride
, "");
715 * We load 2x4 values, and need to swizzle them (order
716 * 0,1,4,5,2,3,6,7) - not so hot with avx unfortunately.
718 for (i
= 0; i
< 8; i
++) {
719 shuffles
[i
] = lp_build_const_int32(gallivm
, (i
&1) + (i
&2) * 2 + (i
&4) / 2);
723 depth_offset2
= LLVMBuildAdd(builder
, depth_offset1
, depth_stride
, "");
725 zs_dst_ptr1
= LLVMBuildGEP(builder
, depth_ptr
, &depth_offset1
, 1, "");
726 zs_dst_ptr1
= LLVMBuildBitCast(builder
, zs_dst_ptr1
, load_ptr_type
, "");
727 zs_dst_ptr2
= LLVMBuildGEP(builder
, depth_ptr
, &depth_offset2
, 1, "");
728 zs_dst_ptr2
= LLVMBuildBitCast(builder
, zs_dst_ptr2
, load_ptr_type
, "");
730 if (format_desc
->block
.bits
> 32) {
731 s_value
= LLVMBuildBitCast(builder
, s_value
, z_bld
.vec_type
, "");
735 mask_value
= lp_build_mask_value(mask
);
736 z_value
= lp_build_select(&z_bld
, mask_value
, z_value
, z_fb
);
737 if (format_desc
->block
.bits
> 32) {
738 s_fb
= LLVMBuildBitCast(builder
, s_fb
, z_bld
.vec_type
, "");
739 s_value
= lp_build_select(&z_bld
, mask_value
, s_value
, s_fb
);
743 if (zs_type
.width
< z_src_type
.width
) {
744 /* Truncate ZS values (e.g., when writing to Z16_UNORM) */
745 z_value
= LLVMBuildTrunc(builder
, z_value
, z_bld
.vec_type
, "");
748 if (format_desc
->block
.bits
<= 32) {
749 if (z_src_type
.length
== 4) {
750 zs_dst1
= lp_build_extract_range(gallivm
, z_value
, 0, 2);
751 zs_dst2
= lp_build_extract_range(gallivm
, z_value
, 2, 2);
754 assert(z_src_type
.length
== 8);
755 zs_dst1
= LLVMBuildShuffleVector(builder
, z_value
, z_value
,
756 LLVMConstVector(&shuffles
[0],
757 zs_load_type
.length
), "");
758 zs_dst2
= LLVMBuildShuffleVector(builder
, z_value
, z_value
,
759 LLVMConstVector(&shuffles
[4],
760 zs_load_type
.length
), "");
764 if (z_src_type
.length
== 4) {
765 zs_dst1
= lp_build_interleave2(gallivm
, zs_type
,
766 z_value
, s_value
, 0);
767 zs_dst2
= lp_build_interleave2(gallivm
, zs_type
,
768 z_value
, s_value
, 1);
772 LLVMValueRef shuffles
[LP_MAX_VECTOR_LENGTH
/ 2];
773 assert(z_src_type
.length
== 8);
774 for (i
= 0; i
< 8; i
++) {
775 shuffles
[i
*2] = lp_build_const_int32(gallivm
, (i
&1) + (i
&2) * 2 + (i
&4) / 2);
776 shuffles
[i
*2+1] = lp_build_const_int32(gallivm
, (i
&1) + (i
&2) * 2 + (i
&4) / 2 +
779 zs_dst1
= LLVMBuildShuffleVector(builder
, z_value
, s_value
,
780 LLVMConstVector(&shuffles
[0],
781 z_src_type
.length
), "");
782 zs_dst2
= LLVMBuildShuffleVector(builder
, z_value
, s_value
,
783 LLVMConstVector(&shuffles
[8],
784 z_src_type
.length
), "");
786 zs_dst1
= LLVMBuildBitCast(builder
, zs_dst1
,
787 lp_build_vec_type(gallivm
, zs_load_type
), "");
788 zs_dst2
= LLVMBuildBitCast(builder
, zs_dst2
,
789 lp_build_vec_type(gallivm
, zs_load_type
), "");
792 LLVMBuildStore(builder
, zs_dst1
, zs_dst_ptr1
);
793 LLVMBuildStore(builder
, zs_dst2
, zs_dst_ptr2
);
797 * Generate code for performing depth and/or stencil tests.
798 * We operate on a vector of values (typically n 2x2 quads).
800 * \param depth the depth test state
801 * \param stencil the front/back stencil state
802 * \param type the data type of the fragment depth/stencil values
803 * \param format_desc description of the depth/stencil surface
804 * \param mask the alive/dead pixel mask for the quad (vector)
805 * \param stencil_refs the front/back stencil ref values (scalar)
806 * \param z_src the incoming depth/stencil values (n 2x2 quad values, float32)
807 * \param zs_dst the depth/stencil values in framebuffer
808 * \param face contains boolean value indicating front/back facing polygon
811 lp_build_depth_stencil_test(struct gallivm_state
*gallivm
,
812 const struct pipe_depth_state
*depth
,
813 const struct pipe_stencil_state stencil
[2],
814 struct lp_type z_src_type
,
815 const struct util_format_description
*format_desc
,
816 struct lp_build_mask_context
*mask
,
817 LLVMValueRef stencil_refs
[2],
822 LLVMValueRef
*z_value
,
823 LLVMValueRef
*s_value
,
826 LLVMBuilderRef builder
= gallivm
->builder
;
827 struct lp_type z_type
;
828 struct lp_build_context z_bld
;
829 struct lp_build_context s_bld
;
830 struct lp_type s_type
;
831 unsigned z_shift
= 0, z_width
= 0, z_mask
= 0;
832 LLVMValueRef z_dst
= NULL
;
833 LLVMValueRef stencil_vals
= NULL
;
834 LLVMValueRef z_bitmask
= NULL
, stencil_shift
= NULL
;
835 LLVMValueRef z_pass
= NULL
, s_pass_mask
= NULL
;
836 LLVMValueRef orig_mask
= lp_build_mask_value(mask
);
837 LLVMValueRef front_facing
= NULL
;
838 boolean have_z
, have_s
;
841 * Depths are expected to be between 0 and 1, even if they are stored in
842 * floats. Setting these bits here will ensure that the lp_build_conv() call
843 * below won't try to unnecessarily clamp the incoming values.
845 if(z_src_type
.floating
) {
846 z_src_type
.sign
= FALSE
;
847 z_src_type
.norm
= TRUE
;
850 assert(!z_src_type
.sign
);
851 assert(z_src_type
.norm
);
854 /* Pick the type matching the depth-stencil format. */
855 z_type
= lp_depth_type(format_desc
, z_src_type
.length
);
857 /* Pick the intermediate type for depth operations. */
858 z_type
.width
= z_src_type
.width
;
859 assert(z_type
.length
== z_src_type
.length
);
861 /* FIXME: for non-float depth/stencil might generate better code
862 * if we'd always split it up to use 128bit operations.
863 * For stencil we'd almost certainly want to pack to 8xi16 values,
864 * for z just run twice.
867 /* Sanity checking */
869 const unsigned z_swizzle
= format_desc
->swizzle
[0];
870 const unsigned s_swizzle
= format_desc
->swizzle
[1];
872 assert(z_swizzle
!= UTIL_FORMAT_SWIZZLE_NONE
||
873 s_swizzle
!= UTIL_FORMAT_SWIZZLE_NONE
);
875 assert(depth
->enabled
|| stencil
[0].enabled
);
877 assert(format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
);
878 assert(format_desc
->block
.width
== 1);
879 assert(format_desc
->block
.height
== 1);
881 if (stencil
[0].enabled
) {
882 assert(s_swizzle
< 4);
883 assert(format_desc
->channel
[s_swizzle
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
884 assert(format_desc
->channel
[s_swizzle
].pure_integer
);
885 assert(!format_desc
->channel
[s_swizzle
].normalized
);
886 assert(format_desc
->channel
[s_swizzle
].size
== 8);
889 if (depth
->enabled
) {
890 assert(z_swizzle
< 4);
891 if (z_type
.floating
) {
892 assert(z_swizzle
== 0);
893 assert(format_desc
->channel
[z_swizzle
].type
==
894 UTIL_FORMAT_TYPE_FLOAT
);
895 assert(format_desc
->channel
[z_swizzle
].size
== 32);
898 assert(format_desc
->channel
[z_swizzle
].type
==
899 UTIL_FORMAT_TYPE_UNSIGNED
);
900 assert(format_desc
->channel
[z_swizzle
].normalized
);
901 assert(!z_type
.fixed
);
907 /* Setup build context for Z vals */
908 lp_build_context_init(&z_bld
, gallivm
, z_type
);
910 /* Setup build context for stencil vals */
911 s_type
= lp_int_type(z_type
);
912 lp_build_context_init(&s_bld
, gallivm
, s_type
);
914 /* Compute and apply the Z/stencil bitmasks and shifts.
917 unsigned s_shift
, s_mask
;
922 have_z
= get_z_shift_and_mask(format_desc
, &z_shift
, &z_width
, &z_mask
);
923 have_s
= get_s_shift_and_mask(format_desc
, &s_shift
, &s_mask
);
926 if (z_mask
!= 0xffffffff) {
927 z_bitmask
= lp_build_const_int_vec(gallivm
, z_type
, z_mask
);
931 * Align the framebuffer Z 's LSB to the right.
934 LLVMValueRef shift
= lp_build_const_int_vec(gallivm
, z_type
, z_shift
);
935 z_dst
= LLVMBuildLShr(builder
, z_dst
, shift
, "z_dst");
936 } else if (z_bitmask
) {
937 z_dst
= LLVMBuildAnd(builder
, z_dst
, z_bitmask
, "z_dst");
939 lp_build_name(z_dst
, "z_dst");
945 LLVMValueRef shift
= lp_build_const_int_vec(gallivm
, s_type
, s_shift
);
946 stencil_vals
= LLVMBuildLShr(builder
, stencil_vals
, shift
, "");
947 stencil_shift
= shift
; /* used below */
950 if (s_mask
!= 0xffffffff) {
951 LLVMValueRef mask
= lp_build_const_int_vec(gallivm
, s_type
, s_mask
);
952 stencil_vals
= LLVMBuildAnd(builder
, stencil_vals
, mask
, "");
955 lp_build_name(stencil_vals
, "s_dst");
959 if (stencil
[0].enabled
) {
962 LLVMValueRef zero
= lp_build_const_int32(gallivm
, 0);
964 /* front_facing = face != 0 ? ~0 : 0 */
965 front_facing
= LLVMBuildICmp(builder
, LLVMIntNE
, face
, zero
, "");
966 front_facing
= LLVMBuildSExt(builder
, front_facing
,
967 LLVMIntTypeInContext(gallivm
->context
,
968 s_bld
.type
.length
*s_bld
.type
.width
),
970 front_facing
= LLVMBuildBitCast(builder
, front_facing
,
971 s_bld
.int_vec_type
, "");
974 /* convert scalar stencil refs into vectors */
975 stencil_refs
[0] = lp_build_broadcast_scalar(&s_bld
, stencil_refs
[0]);
976 stencil_refs
[1] = lp_build_broadcast_scalar(&s_bld
, stencil_refs
[1]);
978 s_pass_mask
= lp_build_stencil_test(&s_bld
, stencil
,
979 stencil_refs
, stencil_vals
,
982 /* apply stencil-fail operator */
984 LLVMValueRef s_fail_mask
= lp_build_andnot(&s_bld
, orig_mask
, s_pass_mask
);
985 stencil_vals
= lp_build_stencil_op(&s_bld
, stencil
, S_FAIL_OP
,
986 stencil_refs
, stencil_vals
,
987 s_fail_mask
, front_facing
);
991 if (depth
->enabled
) {
993 * Convert fragment Z to the desired type, aligning the LSB to the right.
996 assert(z_type
.width
== z_src_type
.width
);
997 assert(z_type
.length
== z_src_type
.length
);
998 assert(lp_check_value(z_src_type
, z_src
));
999 if (z_src_type
.floating
) {
1001 * Convert from floating point values
1004 if (!z_type
.floating
) {
1005 z_src
= lp_build_clamped_float_to_unsigned_norm(gallivm
,
1012 * Convert from unsigned normalized values.
1015 assert(!z_src_type
.sign
);
1016 assert(!z_src_type
.fixed
);
1017 assert(z_src_type
.norm
);
1018 assert(!z_type
.floating
);
1019 if (z_src_type
.width
> z_width
) {
1020 LLVMValueRef shift
= lp_build_const_int_vec(gallivm
, z_src_type
,
1021 z_src_type
.width
- z_width
);
1022 z_src
= LLVMBuildLShr(builder
, z_src
, shift
, "");
1025 assert(lp_check_value(z_type
, z_src
));
1027 lp_build_name(z_src
, "z_src");
1029 /* compare src Z to dst Z, returning 'pass' mask */
1030 z_pass
= lp_build_cmp(&z_bld
, depth
->func
, z_src
, z_dst
);
1032 if (!stencil
[0].enabled
) {
1033 /* We can potentially skip all remaining operations here, but only
1034 * if stencil is disabled because we still need to update the stencil
1035 * buffer values. Don't need to update Z buffer values.
1037 lp_build_mask_update(mask
, z_pass
);
1040 lp_build_mask_check(mask
);
1045 if (depth
->writemask
) {
1046 LLVMValueRef zselectmask
;
1048 /* mask off bits that failed Z test */
1049 zselectmask
= LLVMBuildAnd(builder
, orig_mask
, z_pass
, "");
1051 /* mask off bits that failed stencil test */
1053 zselectmask
= LLVMBuildAnd(builder
, zselectmask
, s_pass_mask
, "");
1056 /* Mix the old and new Z buffer values.
1057 * z_dst[i] = zselectmask[i] ? z_src[i] : z_dst[i]
1059 z_dst
= lp_build_select(&z_bld
, zselectmask
, z_src
, z_dst
);
1062 if (stencil
[0].enabled
) {
1063 /* update stencil buffer values according to z pass/fail result */
1064 LLVMValueRef z_fail_mask
, z_pass_mask
;
1066 /* apply Z-fail operator */
1067 z_fail_mask
= lp_build_andnot(&s_bld
, orig_mask
, z_pass
);
1068 stencil_vals
= lp_build_stencil_op(&s_bld
, stencil
, Z_FAIL_OP
,
1069 stencil_refs
, stencil_vals
,
1070 z_fail_mask
, front_facing
);
1072 /* apply Z-pass operator */
1073 z_pass_mask
= LLVMBuildAnd(builder
, orig_mask
, z_pass
, "");
1074 stencil_vals
= lp_build_stencil_op(&s_bld
, stencil
, Z_PASS_OP
,
1075 stencil_refs
, stencil_vals
,
1076 z_pass_mask
, front_facing
);
1080 /* No depth test: apply Z-pass operator to stencil buffer values which
1081 * passed the stencil test.
1083 s_pass_mask
= LLVMBuildAnd(builder
, orig_mask
, s_pass_mask
, "");
1084 stencil_vals
= lp_build_stencil_op(&s_bld
, stencil
, Z_PASS_OP
,
1085 stencil_refs
, stencil_vals
,
1086 s_pass_mask
, front_facing
);
1089 /* Put Z and stencil bits in the right place */
1090 if (have_z
&& z_shift
) {
1091 LLVMValueRef shift
= lp_build_const_int_vec(gallivm
, z_type
, z_shift
);
1092 z_dst
= LLVMBuildShl(builder
, z_dst
, shift
, "");
1094 if (stencil_vals
&& stencil_shift
)
1095 stencil_vals
= LLVMBuildShl(builder
, stencil_vals
,
1098 /* Finally, merge the z/stencil values */
1099 if ((depth
->enabled
&& depth
->writemask
) ||
1100 (stencil
[0].enabled
&& (stencil
[0].writemask
||
1101 (stencil
[1].enabled
&& stencil
[1].writemask
)))) {
1103 if (format_desc
->block
.bits
<= 32) {
1104 if (have_z
&& have_s
)
1105 *z_value
= LLVMBuildOr(builder
, z_dst
, stencil_vals
, "");
1109 *z_value
= stencil_vals
;
1110 *s_value
= *z_value
;
1114 *s_value
= stencil_vals
;
1119 lp_build_mask_update(mask
, s_pass_mask
);
1121 if (depth
->enabled
&& stencil
[0].enabled
)
1122 lp_build_mask_update(mask
, z_pass
);
1125 lp_build_mask_check(mask
);