gallivm: checkpoint: stencil test code
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_depth.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Depth/stencil testing to LLVM IR translation.
31 *
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
37 * artifacts.
38 *
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
42 *
43 * Z11 Z12 Z13 Z14 ...
44 * Z21 Z22 Z23 Z24 ...
45 * Z31 Z32 Z33 Z34 ...
46 * Z41 Z42 Z43 Z44 ...
47 * ... ... ... ... ...
48 *
49 * will actually be stored in memory as
50 *
51 * Z11 Z12 Z21 Z22 Z13 Z14 Z23 Z24 ...
52 * Z31 Z32 Z41 Z42 Z33 Z34 Z43 Z44 ...
53 * ... ... ... ... ... ... ... ... ...
54 *
55 * FIXME: Code generate stencil test
56 *
57 * @author Jose Fonseca <jfonseca@vmware.com>
58 */
59
60 #include "pipe/p_state.h"
61 #include "util/u_format.h"
62
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"
71
72
73
74 /**
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
79 */
80 static LLVMValueRef
81 lp_build_stencil_test(struct lp_build_context *bld,
82 const struct pipe_stencil_state *stencil,
83 LLVMValueRef stencilVals,
84 LLVMValueRef stencilRef)
85 {
86 const unsigned stencilMax = 255; /* XXX fix */
87 struct lp_type type = bld->type;
88 LLVMValueRef res;
89
90 assert(stencil->enabled);
91
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, "");
98 }
99
100 res = lp_build_compare(bld->builder, bld->type, stencil->func,
101 stencilVals, stencilRef);
102
103 return res;
104 }
105
106
107 /**
108 * Apply the stencil operator (add/sub/keep/etc) to the given vector
109 * of stencil values.
110 * \return new stencil values vector
111 */
112 static LLVMValueRef
113 lp_build_stencil_op(struct lp_build_context *bld,
114 unsigned stencil_op,
115 LLVMValueRef stencilRef,
116 const struct pipe_stencil_state *stencil,
117 LLVMValueRef stencilVals)
118
119 {
120 const unsigned stencilMax = 255; /* XXX fix */
121 struct lp_type type = bld->type;
122 LLVMValueRef res;
123 LLVMValueRef max = lp_build_const_int_vec(type, stencilMax);
124
125 switch (stencil_op) {
126 case PIPE_STENCIL_OP_KEEP:
127 res = stencilVals;
128 case PIPE_STENCIL_OP_ZERO:
129 res = bld->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, "");
146 default:
147 assert(0 && "bad stencil op mode");
148 res = NULL;
149 }
150
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");
158 }
159
160 return res;
161 }
162
163
164 /**
165 * Return a type appropriate for depth/stencil testing.
166 */
167 struct lp_type
168 lp_depth_type(const struct util_format_description *format_desc,
169 unsigned length)
170 {
171 struct lp_type type;
172 unsigned swizzle;
173
174 assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
175 assert(format_desc->block.width == 1);
176 assert(format_desc->block.height == 1);
177
178 swizzle = format_desc->swizzle[0];
179 assert(swizzle < 4);
180
181 memset(&type, 0, sizeof type);
182 type.width = format_desc->block.bits;
183
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);
188 }
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)
192 type.norm = TRUE;
193 }
194 else
195 assert(0);
196
197 assert(type.width <= length);
198 type.length = length / type.width;
199
200 return type;
201 }
202
203
204 /**
205 * Generate code for performing depth and/or stencil tests.
206 * We operate on a vector of values (typically a 2x2 quad).
207 *
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
213 */
214 void
215 lp_build_depth_test(LLVMBuilderRef builder,
216 const struct pipe_depth_state *state,
217 struct lp_type type,
218 const struct util_format_description *format_desc,
219 struct lp_build_mask_context *mask,
220 LLVMValueRef src,
221 LLVMValueRef dst_ptr)
222 {
223 struct lp_build_context bld;
224 unsigned z_swizzle;
225 LLVMValueRef dst;
226 LLVMValueRef z_bitmask = NULL;
227 LLVMValueRef test;
228
229 (void) lp_build_stencil_test;
230 (void) lp_build_stencil_op;
231
232 if(!state->enabled)
233 return;
234
235 assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
236 assert(format_desc->block.width == 1);
237 assert(format_desc->block.height == 1);
238
239 z_swizzle = format_desc->swizzle[0];
240 if(z_swizzle == UTIL_FORMAT_SWIZZLE_NONE)
241 return;
242
243 /* Sanity checking */
244 assert(z_swizzle < 4);
245 assert(format_desc->block.bits == type.width);
246 if(type.floating) {
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);
250 }
251 else {
252 assert(format_desc->channel[z_swizzle].type == UTIL_FORMAT_TYPE_UNSIGNED);
253 assert(format_desc->channel[z_swizzle].normalized);
254 assert(!type.fixed);
255 assert(!type.sign);
256 assert(type.norm);
257 }
258
259 /* Setup build context */
260 lp_build_context_init(&bld, builder, type);
261
262 dst = LLVMBuildLoad(builder, dst_ptr, "");
263
264 lp_build_name(dst, "zsbuf");
265
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);
270 /* nothing to do */
271 }
272 else {
273 unsigned padding_left;
274 unsigned padding_right;
275 unsigned chan;
276
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);
281
282 padding_right = 0;
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);
287
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);
292 }
293
294 if(padding_left)
295 src = LLVMBuildLShr(builder, src, lp_build_const_int_vec(type, padding_left), "");
296 if(padding_right)
297 src = LLVMBuildAnd(builder, src, z_bitmask, "");
298 if(padding_left || padding_right)
299 dst = LLVMBuildAnd(builder, dst, z_bitmask, "");
300 }
301
302 lp_build_name(dst, "zsbuf.z");
303
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);
307
308 if(state->writemask) {
309 if(z_bitmask)
310 z_bitmask = LLVMBuildAnd(builder, mask->value, z_bitmask, "");
311 else
312 z_bitmask = mask->value;
313
314 dst = lp_build_select(&bld, z_bitmask, src, dst);
315 LLVMBuildStore(builder, dst, dst_ptr);
316 }
317 }