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 * ... ... ... ... ... ... ... ... ...
55 * FIXME: Code generate stencil test
57 * @author Jose Fonseca <jfonseca@vmware.com>
60 #include "pipe/p_state.h"
61 #include "util/u_format.h"
63 #include "lp_bld_type.h"
64 #include "lp_bld_const.h"
65 #include "lp_bld_logic.h"
66 #include "lp_bld_flow.h"
67 #include "lp_bld_debug.h"
68 #include "lp_bld_depth.h"
72 * Return a type appropriate for depth/stencil testing.
75 lp_depth_type(const struct util_format_description
*format_desc
,
81 assert(format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
);
82 assert(format_desc
->block
.width
== 1);
83 assert(format_desc
->block
.height
== 1);
85 swizzle
= format_desc
->swizzle
[0];
88 memset(&type
, 0, sizeof type
);
89 type
.width
= format_desc
->block
.bits
;
91 if(format_desc
->channel
[swizzle
].type
== UTIL_FORMAT_TYPE_FLOAT
) {
94 assert(format_desc
->channel
[swizzle
].size
== format_desc
->block
.bits
);
96 else if(format_desc
->channel
[swizzle
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
97 assert(format_desc
->block
.bits
<= 32);
98 if(format_desc
->channel
[swizzle
].normalized
)
104 assert(type
.width
<= length
);
105 type
.length
= length
/ type
.width
;
115 lp_build_depth_test(LLVMBuilderRef builder
,
116 const struct pipe_depth_state
*state
,
118 const struct util_format_description
*format_desc
,
119 struct lp_build_mask_context
*mask
,
121 LLVMValueRef dst_ptr
)
123 struct lp_build_context bld
;
126 LLVMValueRef z_bitmask
= NULL
;
132 assert(format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
);
133 assert(format_desc
->block
.width
== 1);
134 assert(format_desc
->block
.height
== 1);
136 z_swizzle
= format_desc
->swizzle
[0];
137 if(z_swizzle
== UTIL_FORMAT_SWIZZLE_NONE
)
140 /* Sanity checking */
141 assert(z_swizzle
< 4);
142 assert(format_desc
->block
.bits
== type
.width
);
144 assert(z_swizzle
== 0);
145 assert(format_desc
->channel
[z_swizzle
].type
== UTIL_FORMAT_TYPE_FLOAT
);
146 assert(format_desc
->channel
[z_swizzle
].size
== format_desc
->block
.bits
);
149 assert(format_desc
->channel
[z_swizzle
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
150 assert(format_desc
->channel
[z_swizzle
].normalized
);
156 /* Setup build context */
157 lp_build_context_init(&bld
, builder
, type
);
159 dst
= LLVMBuildLoad(builder
, dst_ptr
, "");
161 lp_build_name(dst
, "zsbuf");
163 /* Align the source depth bits with the destination's, and mask out any
164 * stencil or padding bits from both */
165 if(format_desc
->channel
[z_swizzle
].size
== format_desc
->block
.bits
) {
166 assert(z_swizzle
== 0);
170 unsigned padding_left
;
171 unsigned padding_right
;
174 assert(format_desc
->layout
== UTIL_FORMAT_LAYOUT_ARITH
);
175 assert(format_desc
->channel
[z_swizzle
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
176 assert(format_desc
->channel
[z_swizzle
].size
<= format_desc
->block
.bits
);
177 assert(format_desc
->channel
[z_swizzle
].normalized
);
180 for(chan
= 0; chan
< z_swizzle
; ++chan
)
181 padding_right
+= format_desc
->channel
[chan
].size
;
182 padding_left
= format_desc
->block
.bits
-
183 (padding_right
+ format_desc
->channel
[z_swizzle
].size
);
185 if(padding_left
|| padding_right
) {
186 const unsigned long long mask_left
= ((unsigned long long)1 << (format_desc
->block
.bits
- padding_left
)) - 1;
187 const unsigned long long mask_right
= ((unsigned long long)1 << (padding_right
)) - 1;
188 z_bitmask
= lp_build_int_const_scalar(type
, mask_left
^ mask_right
);
192 src
= LLVMBuildLShr(builder
, src
, lp_build_int_const_scalar(type
, padding_left
), "");
194 src
= LLVMBuildAnd(builder
, src
, z_bitmask
, "");
195 if(padding_left
|| padding_right
)
196 dst
= LLVMBuildAnd(builder
, dst
, z_bitmask
, "");
199 lp_build_name(dst
, "zsbuf.z");
201 test
= lp_build_cmp(&bld
, state
->func
, src
, dst
);
202 lp_build_mask_update(mask
, test
);
204 if(state
->writemask
) {
206 z_bitmask
= LLVMBuildAnd(builder
, mask
->value
, z_bitmask
, "");
208 z_bitmask
= mask
->value
;
210 dst
= lp_build_select(&bld
, z_bitmask
, src
, dst
);
211 LLVMBuildStore(builder
, dst
, dst_ptr
);
215 assert(!state
->occlusion_count
);