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_arit.h"
65 #include "lp_bld_const.h"
66 #include "lp_bld_logic.h"
67 #include "lp_bld_flow.h"
68 #include "lp_bld_debug.h"
69 #include "lp_bld_depth.h"
70 #include "lp_bld_swizzle.h"
75 * Do the stencil test comparison (compare fb Z values against ref value.
76 * \param stencilVals vector of stencil values from framebuffer
77 * \param stencilRef the stencil reference value, replicated as a vector
78 * \return mask of pass/fail values
81 lp_build_stencil_test(struct lp_build_context
*bld
,
82 const struct pipe_stencil_state
*stencil
,
83 LLVMValueRef stencilVals
,
84 LLVMValueRef stencilRef
)
86 const unsigned stencilMax
= 255; /* XXX fix */
87 struct lp_type type
= bld
->type
;
90 assert(stencil
->enabled
);
92 if (stencil
->valuemask
!= stencilMax
) {
93 /* compute stencilRef = stencilRef & valuemask */
94 LLVMValueRef valuemask
= lp_build_const_int_vec(type
, stencil
->valuemask
);
95 stencilRef
= LLVMBuildAnd(bld
->builder
, stencilRef
, valuemask
, "");
96 /* compute stencilVals = stencilVals & valuemask */
97 stencilVals
= LLVMBuildAnd(bld
->builder
, stencilVals
, valuemask
, "");
100 res
= lp_build_compare(bld
->builder
, bld
->type
, stencil
->func
,
101 stencilVals
, stencilRef
);
108 * Apply the stencil operator (add/sub/keep/etc) to the given vector
110 * \return new stencil values vector
113 lp_build_stencil_op(struct lp_build_context
*bld
,
115 LLVMValueRef stencilRef
,
116 const struct pipe_stencil_state
*stencil
,
117 LLVMValueRef stencilVals
)
120 const unsigned stencilMax
= 255; /* XXX fix */
121 struct lp_type type
= bld
->type
;
123 LLVMValueRef max
= lp_build_const_int_vec(type
, stencilMax
);
125 switch (stencil_op
) {
126 case PIPE_STENCIL_OP_KEEP
:
128 case PIPE_STENCIL_OP_ZERO
:
130 case PIPE_STENCIL_OP_REPLACE
:
131 res
= lp_build_broadcast_scalar(bld
, stencilRef
);
132 case PIPE_STENCIL_OP_INCR
:
133 res
= lp_build_add(bld
, stencilVals
, bld
->one
);
134 res
= lp_build_min(bld
, res
, max
);
135 case PIPE_STENCIL_OP_DECR
:
136 res
= lp_build_sub(bld
, stencilVals
, bld
->one
);
137 res
= lp_build_max(bld
, res
, bld
->zero
);
138 case PIPE_STENCIL_OP_INCR_WRAP
:
139 res
= lp_build_add(bld
, stencilVals
, bld
->one
);
140 res
= LLVMBuildAnd(bld
->builder
, res
, max
, "");
141 case PIPE_STENCIL_OP_DECR_WRAP
:
142 res
= lp_build_sub(bld
, stencilVals
, bld
->one
);
143 res
= LLVMBuildAnd(bld
->builder
, res
, max
, "");
144 case PIPE_STENCIL_OP_INVERT
:
145 res
= LLVMBuildNot(bld
->builder
, stencilVals
, "");
147 assert(0 && "bad stencil op mode");
151 if (stencil
->writemask
!= stencilMax
) {
152 /* compute res = (res & mask) | (stencilVals & ~mask) */
153 LLVMValueRef mask
= lp_build_const_int_vec(type
, stencil
->writemask
);
154 LLVMValueRef cmask
= LLVMBuildNot(bld
->builder
, mask
, "notWritemask");
155 LLVMValueRef t1
= LLVMBuildAnd(bld
->builder
, res
, mask
, "t1");
156 LLVMValueRef t2
= LLVMBuildAnd(bld
->builder
, stencilVals
, cmask
, "t2");
157 res
= LLVMBuildOr(bld
->builder
, t1
, t2
, "t1_or_t2");
165 * Return a type appropriate for depth/stencil testing.
168 lp_depth_type(const struct util_format_description
*format_desc
,
174 assert(format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
);
175 assert(format_desc
->block
.width
== 1);
176 assert(format_desc
->block
.height
== 1);
178 swizzle
= format_desc
->swizzle
[0];
181 memset(&type
, 0, sizeof type
);
182 type
.width
= format_desc
->block
.bits
;
184 if(format_desc
->channel
[swizzle
].type
== UTIL_FORMAT_TYPE_FLOAT
) {
185 type
.floating
= TRUE
;
186 assert(swizzle
== 0);
187 assert(format_desc
->channel
[swizzle
].size
== format_desc
->block
.bits
);
189 else if(format_desc
->channel
[swizzle
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
190 assert(format_desc
->block
.bits
<= 32);
191 if(format_desc
->channel
[swizzle
].normalized
)
197 assert(type
.width
<= length
);
198 type
.length
= length
/ type
.width
;
205 * Generate code for performing depth and/or stencil tests.
206 * We operate on a vector of values (typically a 2x2 quad).
208 * \param type the data type of the fragment depth/stencil values
209 * \param format_desc description of the depth/stencil surface
210 * \param mask the alive/dead pixel mask for the quad
211 * \param src the incoming depth/stencil values (a 2x2 quad)
212 * \param dst_ptr the outgoing/updated depth/stencil values
215 lp_build_depth_test(LLVMBuilderRef builder
,
216 const struct pipe_depth_state
*state
,
218 const struct util_format_description
*format_desc
,
219 struct lp_build_mask_context
*mask
,
221 LLVMValueRef dst_ptr
)
223 struct lp_build_context bld
;
226 LLVMValueRef z_bitmask
= NULL
;
229 (void) lp_build_stencil_test
;
230 (void) lp_build_stencil_op
;
235 assert(format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
);
236 assert(format_desc
->block
.width
== 1);
237 assert(format_desc
->block
.height
== 1);
239 z_swizzle
= format_desc
->swizzle
[0];
240 if(z_swizzle
== UTIL_FORMAT_SWIZZLE_NONE
)
243 /* Sanity checking */
244 assert(z_swizzle
< 4);
245 assert(format_desc
->block
.bits
== type
.width
);
247 assert(z_swizzle
== 0);
248 assert(format_desc
->channel
[z_swizzle
].type
== UTIL_FORMAT_TYPE_FLOAT
);
249 assert(format_desc
->channel
[z_swizzle
].size
== format_desc
->block
.bits
);
252 assert(format_desc
->channel
[z_swizzle
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
253 assert(format_desc
->channel
[z_swizzle
].normalized
);
259 /* Setup build context */
260 lp_build_context_init(&bld
, builder
, type
);
262 dst
= LLVMBuildLoad(builder
, dst_ptr
, "");
264 lp_build_name(dst
, "zsbuf");
266 /* Align the source depth bits with the destination's, and mask out any
267 * stencil or padding bits from both */
268 if(format_desc
->channel
[z_swizzle
].size
== format_desc
->block
.bits
) {
269 assert(z_swizzle
== 0);
273 unsigned padding_left
;
274 unsigned padding_right
;
277 assert(format_desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
);
278 assert(format_desc
->channel
[z_swizzle
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
279 assert(format_desc
->channel
[z_swizzle
].size
<= format_desc
->block
.bits
);
280 assert(format_desc
->channel
[z_swizzle
].normalized
);
283 for(chan
= 0; chan
< z_swizzle
; ++chan
)
284 padding_right
+= format_desc
->channel
[chan
].size
;
285 padding_left
= format_desc
->block
.bits
-
286 (padding_right
+ format_desc
->channel
[z_swizzle
].size
);
288 if(padding_left
|| padding_right
) {
289 const unsigned long long mask_left
= ((unsigned long long)1 << (format_desc
->block
.bits
- padding_left
)) - 1;
290 const unsigned long long mask_right
= ((unsigned long long)1 << (padding_right
)) - 1;
291 z_bitmask
= lp_build_const_int_vec(type
, mask_left
^ mask_right
);
295 src
= LLVMBuildLShr(builder
, src
, lp_build_const_int_vec(type
, padding_left
), "");
297 src
= LLVMBuildAnd(builder
, src
, z_bitmask
, "");
298 if(padding_left
|| padding_right
)
299 dst
= LLVMBuildAnd(builder
, dst
, z_bitmask
, "");
302 lp_build_name(dst
, "zsbuf.z");
304 /* compare src Z to dst Z, returning 'pass' mask */
305 test
= lp_build_cmp(&bld
, state
->func
, src
, dst
);
306 lp_build_mask_update(mask
, test
);
308 if(state
->writemask
) {
310 z_bitmask
= LLVMBuildAnd(builder
, mask
->value
, z_bitmask
, "");
312 z_bitmask
= mask
->value
;
314 dst
= lp_build_select(&bld
, z_bitmask
, src
, dst
);
315 LLVMBuildStore(builder
, dst
, dst_ptr
);