llvmpipe: move getting mask value out of depth code. (v2)
[mesa.git] / src / gallium / drivers / llvmpipe / lp_bld_depth.c
1 /**************************************************************************
2 *
3 * Copyright 2009-2010 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 * 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
45 * linear format).
46 *
47 *
48 * @author Jose Fonseca <jfonseca@vmware.com>
49 * @author Brian Paul <jfonseca@vmware.com>
50 */
51
52 #include "pipe/p_state.h"
53 #include "util/format/u_format.h"
54 #include "util/u_cpu_detect.h"
55
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"
67
68 #include "lp_bld_depth.h"
69
70
71 /** Used to select fields from pipe_stencil_state */
72 enum stencil_op {
73 S_FAIL_OP,
74 Z_FAIL_OP,
75 Z_PASS_OP
76 };
77
78
79
80 /**
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)
87 */
88 static LLVMValueRef
89 lp_build_stencil_test_single(struct lp_build_context *bld,
90 const struct pipe_stencil_state *stencil,
91 LLVMValueRef stencilRef,
92 LLVMValueRef stencilVals)
93 {
94 LLVMBuilderRef builder = bld->gallivm->builder;
95 const unsigned stencilMax = 255; /* XXX fix */
96 struct lp_type type = bld->type;
97 LLVMValueRef res;
98
99 /*
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
102 * wider elements.
103 */
104 if (type.width <= 8) {
105 assert(!type.sign);
106 } else {
107 assert(type.sign);
108 }
109
110 assert(stencil->enabled);
111
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, "");
118 }
119
120 res = lp_build_cmp(bld, stencil->func, stencilRef, stencilVals);
121
122 return res;
123 }
124
125
126 /**
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.
131 */
132 static LLVMValueRef
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)
138 {
139 LLVMValueRef res;
140
141 assert(stencil[0].enabled);
142
143 /* do front face test */
144 res = lp_build_stencil_test_single(bld, &stencil[0],
145 stencilRefs[0], stencilVals);
146
147 if (stencil[1].enabled && front_facing != NULL) {
148 /* do back face test */
149 LLVMValueRef back_res;
150
151 back_res = lp_build_stencil_test_single(bld, &stencil[1],
152 stencilRefs[1], stencilVals);
153
154 res = lp_build_select(bld, front_facing, res, back_res);
155 }
156
157 return res;
158 }
159
160
161 /**
162 * Apply the stencil operator (add/sub/keep/etc) to the given vector
163 * of stencil values.
164 * \return new stencil values vector
165 */
166 static LLVMValueRef
167 lp_build_stencil_op_single(struct lp_build_context *bld,
168 const struct pipe_stencil_state *stencil,
169 enum stencil_op op,
170 LLVMValueRef stencilRef,
171 LLVMValueRef stencilVals)
172
173 {
174 LLVMBuilderRef builder = bld->gallivm->builder;
175 struct lp_type type = bld->type;
176 LLVMValueRef res;
177 LLVMValueRef max = lp_build_const_int_vec(bld->gallivm, type, 0xff);
178 unsigned stencil_op;
179
180 assert(type.sign);
181
182 switch (op) {
183 case S_FAIL_OP:
184 stencil_op = stencil->fail_op;
185 break;
186 case Z_FAIL_OP:
187 stencil_op = stencil->zfail_op;
188 break;
189 case Z_PASS_OP:
190 stencil_op = stencil->zpass_op;
191 break;
192 default:
193 assert(0 && "Invalid stencil_op mode");
194 stencil_op = PIPE_STENCIL_OP_KEEP;
195 }
196
197 switch (stencil_op) {
198 case PIPE_STENCIL_OP_KEEP:
199 res = stencilVals;
200 /* we can return early for this case */
201 return res;
202 case PIPE_STENCIL_OP_ZERO:
203 res = bld->zero;
204 break;
205 case PIPE_STENCIL_OP_REPLACE:
206 res = stencilRef;
207 break;
208 case PIPE_STENCIL_OP_INCR:
209 res = lp_build_add(bld, stencilVals, bld->one);
210 res = lp_build_min(bld, res, max);
211 break;
212 case PIPE_STENCIL_OP_DECR:
213 res = lp_build_sub(bld, stencilVals, bld->one);
214 res = lp_build_max(bld, res, bld->zero);
215 break;
216 case PIPE_STENCIL_OP_INCR_WRAP:
217 res = lp_build_add(bld, stencilVals, bld->one);
218 res = LLVMBuildAnd(builder, res, max, "");
219 break;
220 case PIPE_STENCIL_OP_DECR_WRAP:
221 res = lp_build_sub(bld, stencilVals, bld->one);
222 res = LLVMBuildAnd(builder, res, max, "");
223 break;
224 case PIPE_STENCIL_OP_INVERT:
225 res = LLVMBuildNot(builder, stencilVals, "");
226 res = LLVMBuildAnd(builder, res, max, "");
227 break;
228 default:
229 assert(0 && "bad stencil op mode");
230 res = bld->undef;
231 }
232
233 return res;
234 }
235
236
237 /**
238 * Do the one or two-sided stencil test op/update.
239 */
240 static LLVMValueRef
241 lp_build_stencil_op(struct lp_build_context *bld,
242 const struct pipe_stencil_state stencil[2],
243 enum stencil_op op,
244 LLVMValueRef stencilRefs[2],
245 LLVMValueRef stencilVals,
246 LLVMValueRef mask,
247 LLVMValueRef front_facing)
248
249 {
250 LLVMBuilderRef builder = bld->gallivm->builder;
251 LLVMValueRef res;
252
253 assert(stencil[0].enabled);
254
255 /* do front face op */
256 res = lp_build_stencil_op_single(bld, &stencil[0], op,
257 stencilRefs[0], stencilVals);
258
259 if (stencil[1].enabled && front_facing != NULL) {
260 /* do back face op */
261 LLVMValueRef back_res;
262
263 back_res = lp_build_stencil_op_single(bld, &stencil[1], op,
264 stencilRefs[1], stencilVals);
265
266 res = lp_build_select(bld, front_facing, res, back_res);
267 }
268
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);
278 }
279
280 mask = LLVMBuildAnd(builder, mask, writemask, "");
281 /* res = (res & mask) | (stencilVals & ~mask) */
282 res = lp_build_select_bitwise(bld, mask, res, stencilVals);
283 }
284 else {
285 /* res = mask ? res : stencilVals */
286 res = lp_build_select(bld, mask, res, stencilVals);
287 }
288
289 return res;
290 }
291
292
293
294 /**
295 * Return a type that matches the depth/stencil format.
296 */
297 struct lp_type
298 lp_depth_type(const struct util_format_description *format_desc,
299 unsigned length)
300 {
301 struct lp_type type;
302 unsigned z_swizzle;
303
304 assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
305 assert(format_desc->block.width == 1);
306 assert(format_desc->block.height == 1);
307
308 memset(&type, 0, sizeof type);
309 type.width = format_desc->block.bits;
310
311 z_swizzle = format_desc->swizzle[0];
312 if (z_swizzle < 4) {
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);
317 }
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;
324 */
325 type.sign = TRUE;
326 }
327 }
328 else
329 assert(0);
330 }
331
332 type.length = length;
333
334 return type;
335 }
336
337
338 /**
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.
341 *
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.
345 */
346 static boolean
347 get_z_shift_and_mask(const struct util_format_description *format_desc,
348 unsigned *shift, unsigned *width, unsigned *mask)
349 {
350 unsigned total_bits;
351 unsigned z_swizzle;
352
353 assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
354 assert(format_desc->block.width == 1);
355 assert(format_desc->block.height == 1);
356
357 /* 64bit d/s format is special already extracted 32 bits */
358 total_bits = format_desc->block.bits > 32 ? 32 : format_desc->block.bits;
359
360 z_swizzle = format_desc->swizzle[0];
361
362 if (z_swizzle == PIPE_SWIZZLE_NONE)
363 return FALSE;
364
365 *width = format_desc->channel[z_swizzle].size;
366 /* & 31 is for the same reason as the 32-bit limit above */
367 *shift = format_desc->channel[z_swizzle].shift & 31;
368
369 if (*width == total_bits) {
370 *mask = 0xffffffff;
371 } else {
372 *mask = ((1 << *width) - 1) << *shift;
373 }
374
375 return TRUE;
376 }
377
378
379 /**
380 * Compute bitmask and bit shift to apply to the framebuffer pixel values
381 * to put the stencil bits in the least significant position.
382 * (i.e. 0x000000ff)
383 */
384 static boolean
385 get_s_shift_and_mask(const struct util_format_description *format_desc,
386 unsigned *shift, unsigned *mask)
387 {
388 unsigned s_swizzle;
389 unsigned sz;
390
391 s_swizzle = format_desc->swizzle[1];
392
393 if (s_swizzle == PIPE_SWIZZLE_NONE)
394 return FALSE;
395
396 /* just special case 64bit d/s format */
397 if (format_desc->block.bits > 32) {
398 /* XXX big-endian? */
399 assert(format_desc->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT);
400 *shift = 0;
401 *mask = 0xff;
402 return TRUE;
403 }
404
405 *shift = format_desc->channel[s_swizzle].shift;
406 sz = format_desc->channel[s_swizzle].size;
407 *mask = (1U << sz) - 1U;
408
409 return TRUE;
410 }
411
412
413 /**
414 * Perform the occlusion test and increase the counter.
415 * Test the depth mask. Add the number of channel which has none zero mask
416 * into the occlusion counter. e.g. maskvalue is {-1, -1, -1, -1}.
417 * The counter will add 4.
418 * TODO: could get that out of the fs loop.
419 *
420 * \param type holds element type of the mask vector.
421 * \param maskvalue is the depth test mask.
422 * \param counter is a pointer of the uint32 counter.
423 */
424 void
425 lp_build_occlusion_count(struct gallivm_state *gallivm,
426 struct lp_type type,
427 LLVMValueRef maskvalue,
428 LLVMValueRef counter)
429 {
430 LLVMBuilderRef builder = gallivm->builder;
431 LLVMContextRef context = gallivm->context;
432 LLVMValueRef countmask = lp_build_const_int_vec(gallivm, type, 1);
433 LLVMValueRef count, newcount;
434
435 assert(type.length <= 16);
436 assert(type.floating);
437
438 if(util_cpu_caps.has_sse && type.length == 4) {
439 const char *movmskintr = "llvm.x86.sse.movmsk.ps";
440 const char *popcntintr = "llvm.ctpop.i32";
441 LLVMValueRef bits = LLVMBuildBitCast(builder, maskvalue,
442 lp_build_vec_type(gallivm, type), "");
443 bits = lp_build_intrinsic_unary(builder, movmskintr,
444 LLVMInt32TypeInContext(context), bits);
445 count = lp_build_intrinsic_unary(builder, popcntintr,
446 LLVMInt32TypeInContext(context), bits);
447 count = LLVMBuildZExt(builder, count, LLVMIntTypeInContext(context, 64), "");
448 }
449 else if(util_cpu_caps.has_avx && type.length == 8) {
450 const char *movmskintr = "llvm.x86.avx.movmsk.ps.256";
451 const char *popcntintr = "llvm.ctpop.i32";
452 LLVMValueRef bits = LLVMBuildBitCast(builder, maskvalue,
453 lp_build_vec_type(gallivm, type), "");
454 bits = lp_build_intrinsic_unary(builder, movmskintr,
455 LLVMInt32TypeInContext(context), bits);
456 count = lp_build_intrinsic_unary(builder, popcntintr,
457 LLVMInt32TypeInContext(context), bits);
458 count = LLVMBuildZExt(builder, count, LLVMIntTypeInContext(context, 64), "");
459 }
460 else {
461 unsigned i;
462 LLVMValueRef countv = LLVMBuildAnd(builder, maskvalue, countmask, "countv");
463 LLVMTypeRef counttype = LLVMIntTypeInContext(context, type.length * 8);
464 LLVMTypeRef i8vntype = LLVMVectorType(LLVMInt8TypeInContext(context), type.length * 4);
465 LLVMValueRef shufflev, countd;
466 LLVMValueRef shuffles[16];
467 const char *popcntintr = NULL;
468
469 countv = LLVMBuildBitCast(builder, countv, i8vntype, "");
470
471 for (i = 0; i < type.length; i++) {
472 shuffles[i] = lp_build_const_int32(gallivm, 4*i);
473 }
474
475 shufflev = LLVMConstVector(shuffles, type.length);
476 countd = LLVMBuildShuffleVector(builder, countv, LLVMGetUndef(i8vntype), shufflev, "");
477 countd = LLVMBuildBitCast(builder, countd, counttype, "countd");
478
479 /*
480 * XXX FIXME
481 * this is bad on cpus without popcount (on x86 supported by intel
482 * nehalem, amd barcelona, and up - not tied to sse42).
483 * Would be much faster to just sum the 4 elements of the vector with
484 * some horizontal add (shuffle/add/shuffle/add after the initial and).
485 */
486 switch (type.length) {
487 case 4:
488 popcntintr = "llvm.ctpop.i32";
489 break;
490 case 8:
491 popcntintr = "llvm.ctpop.i64";
492 break;
493 case 16:
494 popcntintr = "llvm.ctpop.i128";
495 break;
496 default:
497 assert(0);
498 }
499 count = lp_build_intrinsic_unary(builder, popcntintr, counttype, countd);
500
501 if (type.length > 8) {
502 count = LLVMBuildTrunc(builder, count, LLVMIntTypeInContext(context, 64), "");
503 }
504 else if (type.length < 8) {
505 count = LLVMBuildZExt(builder, count, LLVMIntTypeInContext(context, 64), "");
506 }
507 }
508 newcount = LLVMBuildLoad(builder, counter, "origcount");
509 newcount = LLVMBuildAdd(builder, newcount, count, "newcount");
510 LLVMBuildStore(builder, newcount, counter);
511 }
512
513
514 /**
515 * Load depth/stencil values.
516 * The stored values are linear, swizzle them.
517 *
518 * \param type the data type of the fragment depth/stencil values
519 * \param format_desc description of the depth/stencil surface
520 * \param is_1d whether this resource has only one dimension
521 * \param loop_counter the current loop iteration
522 * \param depth_ptr pointer to the depth/stencil values of this 4x4 block
523 * \param depth_stride stride of the depth/stencil buffer
524 * \param z_fb contains z values loaded from fb (may include padding)
525 * \param s_fb contains s values loaded from fb (may include padding)
526 */
527 void
528 lp_build_depth_stencil_load_swizzled(struct gallivm_state *gallivm,
529 struct lp_type z_src_type,
530 const struct util_format_description *format_desc,
531 boolean is_1d,
532 LLVMValueRef depth_ptr,
533 LLVMValueRef depth_stride,
534 LLVMValueRef *z_fb,
535 LLVMValueRef *s_fb,
536 LLVMValueRef loop_counter)
537 {
538 LLVMBuilderRef builder = gallivm->builder;
539 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH / 4];
540 LLVMValueRef zs_dst1, zs_dst2;
541 LLVMValueRef zs_dst_ptr;
542 LLVMValueRef depth_offset1, depth_offset2;
543 LLVMTypeRef load_ptr_type;
544 unsigned depth_bytes = format_desc->block.bits / 8;
545 struct lp_type zs_type = lp_depth_type(format_desc, z_src_type.length);
546 struct lp_type zs_load_type = zs_type;
547
548 zs_load_type.length = zs_load_type.length / 2;
549 load_ptr_type = LLVMPointerType(lp_build_vec_type(gallivm, zs_load_type), 0);
550
551 if (z_src_type.length == 4) {
552 unsigned i;
553 LLVMValueRef looplsb = LLVMBuildAnd(builder, loop_counter,
554 lp_build_const_int32(gallivm, 1), "");
555 LLVMValueRef loopmsb = LLVMBuildAnd(builder, loop_counter,
556 lp_build_const_int32(gallivm, 2), "");
557 LLVMValueRef offset2 = LLVMBuildMul(builder, loopmsb,
558 depth_stride, "");
559 depth_offset1 = LLVMBuildMul(builder, looplsb,
560 lp_build_const_int32(gallivm, depth_bytes * 2), "");
561 depth_offset1 = LLVMBuildAdd(builder, depth_offset1, offset2, "");
562
563 /* just concatenate the loaded 2x2 values into 4-wide vector */
564 for (i = 0; i < 4; i++) {
565 shuffles[i] = lp_build_const_int32(gallivm, i);
566 }
567 }
568 else {
569 unsigned i;
570 LLVMValueRef loopx2 = LLVMBuildShl(builder, loop_counter,
571 lp_build_const_int32(gallivm, 1), "");
572 assert(z_src_type.length == 8);
573 depth_offset1 = LLVMBuildMul(builder, loopx2, depth_stride, "");
574 /*
575 * We load 2x4 values, and need to swizzle them (order
576 * 0,1,4,5,2,3,6,7) - not so hot with avx unfortunately.
577 */
578 for (i = 0; i < 8; i++) {
579 shuffles[i] = lp_build_const_int32(gallivm, (i&1) + (i&2) * 2 + (i&4) / 2);
580 }
581 }
582
583 depth_offset2 = LLVMBuildAdd(builder, depth_offset1, depth_stride, "");
584
585 /* Load current z/stencil values from z/stencil buffer */
586 zs_dst_ptr = LLVMBuildGEP(builder, depth_ptr, &depth_offset1, 1, "");
587 zs_dst_ptr = LLVMBuildBitCast(builder, zs_dst_ptr, load_ptr_type, "");
588 zs_dst1 = LLVMBuildLoad(builder, zs_dst_ptr, "");
589 if (is_1d) {
590 zs_dst2 = lp_build_undef(gallivm, zs_load_type);
591 }
592 else {
593 zs_dst_ptr = LLVMBuildGEP(builder, depth_ptr, &depth_offset2, 1, "");
594 zs_dst_ptr = LLVMBuildBitCast(builder, zs_dst_ptr, load_ptr_type, "");
595 zs_dst2 = LLVMBuildLoad(builder, zs_dst_ptr, "");
596 }
597
598 *z_fb = LLVMBuildShuffleVector(builder, zs_dst1, zs_dst2,
599 LLVMConstVector(shuffles, zs_type.length), "");
600 *s_fb = *z_fb;
601
602 if (format_desc->block.bits == 8) {
603 /* Extend stencil-only 8 bit values (S8_UINT) */
604 *s_fb = LLVMBuildZExt(builder, *s_fb,
605 lp_build_int_vec_type(gallivm, z_src_type), "");
606 }
607
608 if (format_desc->block.bits < z_src_type.width) {
609 /* Extend destination ZS values (e.g., when reading from Z16_UNORM) */
610 *z_fb = LLVMBuildZExt(builder, *z_fb,
611 lp_build_int_vec_type(gallivm, z_src_type), "");
612 }
613
614 else if (format_desc->block.bits > 32) {
615 /* rely on llvm to handle too wide vector we have here nicely */
616 unsigned i;
617 struct lp_type typex2 = zs_type;
618 struct lp_type s_type = zs_type;
619 LLVMValueRef shuffles1[LP_MAX_VECTOR_LENGTH / 4];
620 LLVMValueRef shuffles2[LP_MAX_VECTOR_LENGTH / 4];
621 LLVMValueRef tmp;
622
623 typex2.width = typex2.width / 2;
624 typex2.length = typex2.length * 2;
625 s_type.width = s_type.width / 2;
626 s_type.floating = 0;
627
628 tmp = LLVMBuildBitCast(builder, *z_fb,
629 lp_build_vec_type(gallivm, typex2), "");
630
631 for (i = 0; i < zs_type.length; i++) {
632 shuffles1[i] = lp_build_const_int32(gallivm, i * 2);
633 shuffles2[i] = lp_build_const_int32(gallivm, i * 2 + 1);
634 }
635 *z_fb = LLVMBuildShuffleVector(builder, tmp, tmp,
636 LLVMConstVector(shuffles1, zs_type.length), "");
637 *s_fb = LLVMBuildShuffleVector(builder, tmp, tmp,
638 LLVMConstVector(shuffles2, zs_type.length), "");
639 *s_fb = LLVMBuildBitCast(builder, *s_fb,
640 lp_build_vec_type(gallivm, s_type), "");
641 lp_build_name(*s_fb, "s_dst");
642 }
643
644 lp_build_name(*z_fb, "z_dst");
645 lp_build_name(*s_fb, "s_dst");
646 lp_build_name(*z_fb, "z_dst");
647 }
648
649 /**
650 * Store depth/stencil values.
651 * Incoming values are swizzled (typically n 2x2 quads), stored linear.
652 * If there's a mask it will do select/store otherwise just store.
653 *
654 * \param type the data type of the fragment depth/stencil values
655 * \param format_desc description of the depth/stencil surface
656 * \param is_1d whether this resource has only one dimension
657 * \param mask_value the alive/dead pixel mask for the quad (vector)
658 * \param z_fb z values read from fb (with padding)
659 * \param s_fb s values read from fb (with padding)
660 * \param loop_counter the current loop iteration
661 * \param depth_ptr pointer to the depth/stencil values of this 4x4 block
662 * \param depth_stride stride of the depth/stencil buffer
663 * \param z_value the depth values to store (with padding)
664 * \param s_value the stencil values to store (with padding)
665 */
666 void
667 lp_build_depth_stencil_write_swizzled(struct gallivm_state *gallivm,
668 struct lp_type z_src_type,
669 const struct util_format_description *format_desc,
670 boolean is_1d,
671 LLVMValueRef mask_value,
672 LLVMValueRef z_fb,
673 LLVMValueRef s_fb,
674 LLVMValueRef loop_counter,
675 LLVMValueRef depth_ptr,
676 LLVMValueRef depth_stride,
677 LLVMValueRef z_value,
678 LLVMValueRef s_value)
679 {
680 struct lp_build_context z_bld;
681 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH / 4];
682 LLVMBuilderRef builder = gallivm->builder;
683 LLVMValueRef zs_dst1, zs_dst2;
684 LLVMValueRef zs_dst_ptr1, zs_dst_ptr2;
685 LLVMValueRef depth_offset1, depth_offset2;
686 LLVMTypeRef load_ptr_type;
687 unsigned depth_bytes = format_desc->block.bits / 8;
688 struct lp_type zs_type = lp_depth_type(format_desc, z_src_type.length);
689 struct lp_type z_type = zs_type;
690 struct lp_type zs_load_type = zs_type;
691
692 zs_load_type.length = zs_load_type.length / 2;
693 load_ptr_type = LLVMPointerType(lp_build_vec_type(gallivm, zs_load_type), 0);
694
695 z_type.width = z_src_type.width;
696
697 lp_build_context_init(&z_bld, gallivm, z_type);
698
699 /*
700 * This is far from ideal, at least for late depth write we should do this
701 * outside the fs loop to avoid all the swizzle stuff.
702 */
703 if (z_src_type.length == 4) {
704 LLVMValueRef looplsb = LLVMBuildAnd(builder, loop_counter,
705 lp_build_const_int32(gallivm, 1), "");
706 LLVMValueRef loopmsb = LLVMBuildAnd(builder, loop_counter,
707 lp_build_const_int32(gallivm, 2), "");
708 LLVMValueRef offset2 = LLVMBuildMul(builder, loopmsb,
709 depth_stride, "");
710 depth_offset1 = LLVMBuildMul(builder, looplsb,
711 lp_build_const_int32(gallivm, depth_bytes * 2), "");
712 depth_offset1 = LLVMBuildAdd(builder, depth_offset1, offset2, "");
713 }
714 else {
715 unsigned i;
716 LLVMValueRef loopx2 = LLVMBuildShl(builder, loop_counter,
717 lp_build_const_int32(gallivm, 1), "");
718 assert(z_src_type.length == 8);
719 depth_offset1 = LLVMBuildMul(builder, loopx2, depth_stride, "");
720 /*
721 * We load 2x4 values, and need to swizzle them (order
722 * 0,1,4,5,2,3,6,7) - not so hot with avx unfortunately.
723 */
724 for (i = 0; i < 8; i++) {
725 shuffles[i] = lp_build_const_int32(gallivm, (i&1) + (i&2) * 2 + (i&4) / 2);
726 }
727 }
728
729 depth_offset2 = LLVMBuildAdd(builder, depth_offset1, depth_stride, "");
730
731 zs_dst_ptr1 = LLVMBuildGEP(builder, depth_ptr, &depth_offset1, 1, "");
732 zs_dst_ptr1 = LLVMBuildBitCast(builder, zs_dst_ptr1, load_ptr_type, "");
733 zs_dst_ptr2 = LLVMBuildGEP(builder, depth_ptr, &depth_offset2, 1, "");
734 zs_dst_ptr2 = LLVMBuildBitCast(builder, zs_dst_ptr2, load_ptr_type, "");
735
736 if (format_desc->block.bits > 32) {
737 s_value = LLVMBuildBitCast(builder, s_value, z_bld.vec_type, "");
738 }
739
740 if (mask_value) {
741 z_value = lp_build_select(&z_bld, mask_value, z_value, z_fb);
742 if (format_desc->block.bits > 32) {
743 s_fb = LLVMBuildBitCast(builder, s_fb, z_bld.vec_type, "");
744 s_value = lp_build_select(&z_bld, mask_value, s_value, s_fb);
745 }
746 }
747
748 if (zs_type.width < z_src_type.width) {
749 /* Truncate ZS values (e.g., when writing to Z16_UNORM) */
750 z_value = LLVMBuildTrunc(builder, z_value,
751 lp_build_int_vec_type(gallivm, zs_type), "");
752 }
753
754 if (format_desc->block.bits <= 32) {
755 if (z_src_type.length == 4) {
756 zs_dst1 = lp_build_extract_range(gallivm, z_value, 0, 2);
757 zs_dst2 = lp_build_extract_range(gallivm, z_value, 2, 2);
758 }
759 else {
760 assert(z_src_type.length == 8);
761 zs_dst1 = LLVMBuildShuffleVector(builder, z_value, z_value,
762 LLVMConstVector(&shuffles[0],
763 zs_load_type.length), "");
764 zs_dst2 = LLVMBuildShuffleVector(builder, z_value, z_value,
765 LLVMConstVector(&shuffles[4],
766 zs_load_type.length), "");
767 }
768 }
769 else {
770 if (z_src_type.length == 4) {
771 zs_dst1 = lp_build_interleave2(gallivm, z_type,
772 z_value, s_value, 0);
773 zs_dst2 = lp_build_interleave2(gallivm, z_type,
774 z_value, s_value, 1);
775 }
776 else {
777 unsigned i;
778 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH / 2];
779 assert(z_src_type.length == 8);
780 for (i = 0; i < 8; i++) {
781 shuffles[i*2] = lp_build_const_int32(gallivm, (i&1) + (i&2) * 2 + (i&4) / 2);
782 shuffles[i*2+1] = lp_build_const_int32(gallivm, (i&1) + (i&2) * 2 + (i&4) / 2 +
783 z_src_type.length);
784 }
785 zs_dst1 = LLVMBuildShuffleVector(builder, z_value, s_value,
786 LLVMConstVector(&shuffles[0],
787 z_src_type.length), "");
788 zs_dst2 = LLVMBuildShuffleVector(builder, z_value, s_value,
789 LLVMConstVector(&shuffles[8],
790 z_src_type.length), "");
791 }
792 zs_dst1 = LLVMBuildBitCast(builder, zs_dst1,
793 lp_build_vec_type(gallivm, zs_load_type), "");
794 zs_dst2 = LLVMBuildBitCast(builder, zs_dst2,
795 lp_build_vec_type(gallivm, zs_load_type), "");
796 }
797
798 LLVMBuildStore(builder, zs_dst1, zs_dst_ptr1);
799 if (!is_1d) {
800 LLVMBuildStore(builder, zs_dst2, zs_dst_ptr2);
801 }
802 }
803
804 /**
805 * Generate code for performing depth and/or stencil tests.
806 * We operate on a vector of values (typically n 2x2 quads).
807 *
808 * \param depth the depth test state
809 * \param stencil the front/back stencil state
810 * \param type the data type of the fragment depth/stencil values
811 * \param format_desc description of the depth/stencil surface
812 * \param mask the alive/dead pixel mask for the quad (vector)
813 * \param stencil_refs the front/back stencil ref values (scalar)
814 * \param z_src the incoming depth/stencil values (n 2x2 quad values, float32)
815 * \param zs_dst the depth/stencil values in framebuffer
816 * \param face contains boolean value indicating front/back facing polygon
817 */
818 void
819 lp_build_depth_stencil_test(struct gallivm_state *gallivm,
820 const struct pipe_depth_state *depth,
821 const struct pipe_stencil_state stencil[2],
822 struct lp_type z_src_type,
823 const struct util_format_description *format_desc,
824 struct lp_build_mask_context *mask,
825 LLVMValueRef stencil_refs[2],
826 LLVMValueRef z_src,
827 LLVMValueRef z_fb,
828 LLVMValueRef s_fb,
829 LLVMValueRef face,
830 LLVMValueRef *z_value,
831 LLVMValueRef *s_value,
832 boolean do_branch)
833 {
834 LLVMBuilderRef builder = gallivm->builder;
835 struct lp_type z_type;
836 struct lp_build_context z_bld;
837 struct lp_build_context s_bld;
838 struct lp_type s_type;
839 unsigned z_shift = 0, z_width = 0, z_mask = 0;
840 LLVMValueRef z_dst = NULL;
841 LLVMValueRef stencil_vals = NULL;
842 LLVMValueRef z_bitmask = NULL, stencil_shift = NULL;
843 LLVMValueRef z_pass = NULL, s_pass_mask = NULL;
844 LLVMValueRef current_mask = lp_build_mask_value(mask);
845 LLVMValueRef front_facing = NULL;
846 boolean have_z, have_s;
847
848 /*
849 * Depths are expected to be between 0 and 1, even if they are stored in
850 * floats. Setting these bits here will ensure that the lp_build_conv() call
851 * below won't try to unnecessarily clamp the incoming values.
852 */
853 if(z_src_type.floating) {
854 z_src_type.sign = FALSE;
855 z_src_type.norm = TRUE;
856 }
857 else {
858 assert(!z_src_type.sign);
859 assert(z_src_type.norm);
860 }
861
862 /* Pick the type matching the depth-stencil format. */
863 z_type = lp_depth_type(format_desc, z_src_type.length);
864
865 /* Pick the intermediate type for depth operations. */
866 z_type.width = z_src_type.width;
867 assert(z_type.length == z_src_type.length);
868
869 /* FIXME: for non-float depth/stencil might generate better code
870 * if we'd always split it up to use 128bit operations.
871 * For stencil we'd almost certainly want to pack to 8xi16 values,
872 * for z just run twice.
873 */
874
875 /* Sanity checking */
876 {
877 const unsigned z_swizzle = format_desc->swizzle[0];
878 const unsigned s_swizzle = format_desc->swizzle[1];
879
880 assert(z_swizzle != PIPE_SWIZZLE_NONE ||
881 s_swizzle != PIPE_SWIZZLE_NONE);
882
883 assert(depth->enabled || stencil[0].enabled);
884
885 assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
886 assert(format_desc->block.width == 1);
887 assert(format_desc->block.height == 1);
888
889 if (stencil[0].enabled) {
890 assert(s_swizzle < 4);
891 assert(format_desc->channel[s_swizzle].type == UTIL_FORMAT_TYPE_UNSIGNED);
892 assert(format_desc->channel[s_swizzle].pure_integer);
893 assert(!format_desc->channel[s_swizzle].normalized);
894 assert(format_desc->channel[s_swizzle].size == 8);
895 }
896
897 if (depth->enabled) {
898 assert(z_swizzle < 4);
899 if (z_type.floating) {
900 assert(z_swizzle == 0);
901 assert(format_desc->channel[z_swizzle].type ==
902 UTIL_FORMAT_TYPE_FLOAT);
903 assert(format_desc->channel[z_swizzle].size == 32);
904 }
905 else {
906 assert(format_desc->channel[z_swizzle].type ==
907 UTIL_FORMAT_TYPE_UNSIGNED);
908 assert(format_desc->channel[z_swizzle].normalized);
909 assert(!z_type.fixed);
910 }
911 }
912 }
913
914
915 /* Setup build context for Z vals */
916 lp_build_context_init(&z_bld, gallivm, z_type);
917
918 /* Setup build context for stencil vals */
919 s_type = lp_int_type(z_type);
920 lp_build_context_init(&s_bld, gallivm, s_type);
921
922 /* Compute and apply the Z/stencil bitmasks and shifts.
923 */
924 {
925 unsigned s_shift, s_mask;
926
927 z_dst = z_fb;
928 stencil_vals = s_fb;
929
930 have_z = get_z_shift_and_mask(format_desc, &z_shift, &z_width, &z_mask);
931 have_s = get_s_shift_and_mask(format_desc, &s_shift, &s_mask);
932
933 if (have_z) {
934 if (z_mask != 0xffffffff) {
935 z_bitmask = lp_build_const_int_vec(gallivm, z_type, z_mask);
936 }
937
938 /*
939 * Align the framebuffer Z 's LSB to the right.
940 */
941 if (z_shift) {
942 LLVMValueRef shift = lp_build_const_int_vec(gallivm, z_type, z_shift);
943 z_dst = LLVMBuildLShr(builder, z_dst, shift, "z_dst");
944 } else if (z_bitmask) {
945 z_dst = LLVMBuildAnd(builder, z_dst, z_bitmask, "z_dst");
946 } else {
947 lp_build_name(z_dst, "z_dst");
948 }
949 }
950
951 if (have_s) {
952 if (s_shift) {
953 LLVMValueRef shift = lp_build_const_int_vec(gallivm, s_type, s_shift);
954 stencil_vals = LLVMBuildLShr(builder, stencil_vals, shift, "");
955 stencil_shift = shift; /* used below */
956 }
957
958 if (s_mask != 0xffffffff) {
959 LLVMValueRef mask = lp_build_const_int_vec(gallivm, s_type, s_mask);
960 stencil_vals = LLVMBuildAnd(builder, stencil_vals, mask, "");
961 }
962
963 lp_build_name(stencil_vals, "s_dst");
964 }
965 }
966
967 if (stencil[0].enabled) {
968
969 if (face) {
970 if (0) {
971 /*
972 * XXX: the scalar expansion below produces atrocious code
973 * (basically producing a 64bit scalar value, then moving the 2
974 * 32bit pieces separately to simd, plus 4 shuffles, which is
975 * seriously lame). But the scalar-simd transitions are always
976 * tricky, so no big surprise there.
977 * This here would be way better, however llvm has some serious
978 * trouble later using it in the select, probably because it will
979 * recognize the expression as constant and move the simd value
980 * away (out of the loop) - and then it will suddenly try
981 * constructing i1 high-bit masks out of it later...
982 * (Try piglit stencil-twoside.)
983 * Note this is NOT due to using SExt/Trunc, it fails exactly the
984 * same even when using native compare/select.
985 * I cannot reproduce this problem when using stand-alone compiler
986 * though, suggesting some problem with optimization passes...
987 * (With stand-alone compilation, the construction of this mask
988 * value, no matter if the easy 3 instruction here or the complex
989 * 16+ one below, never gets separated from where it's used.)
990 * The scalar code still has the same problem, but the generated
991 * code looks a bit better at least for some reason, even if
992 * mostly by luck (the fundamental issue clearly is the same).
993 */
994 front_facing = lp_build_broadcast(gallivm, s_bld.vec_type, face);
995 /* front_facing = face != 0 ? ~0 : 0 */
996 front_facing = lp_build_compare(gallivm, s_bld.type,
997 PIPE_FUNC_NOTEQUAL,
998 front_facing, s_bld.zero);
999 } else {
1000 LLVMValueRef zero = lp_build_const_int32(gallivm, 0);
1001
1002 /* front_facing = face != 0 ? ~0 : 0 */
1003 front_facing = LLVMBuildICmp(builder, LLVMIntNE, face, zero, "");
1004 front_facing = LLVMBuildSExt(builder, front_facing,
1005 LLVMIntTypeInContext(gallivm->context,
1006 s_bld.type.length*s_bld.type.width),
1007 "");
1008 front_facing = LLVMBuildBitCast(builder, front_facing,
1009 s_bld.int_vec_type, "");
1010
1011 }
1012 }
1013
1014 s_pass_mask = lp_build_stencil_test(&s_bld, stencil,
1015 stencil_refs, stencil_vals,
1016 front_facing);
1017
1018 /* apply stencil-fail operator */
1019 {
1020 LLVMValueRef s_fail_mask = lp_build_andnot(&s_bld, current_mask, s_pass_mask);
1021 stencil_vals = lp_build_stencil_op(&s_bld, stencil, S_FAIL_OP,
1022 stencil_refs, stencil_vals,
1023 s_fail_mask, front_facing);
1024 }
1025 }
1026
1027 if (depth->enabled) {
1028 /*
1029 * Convert fragment Z to the desired type, aligning the LSB to the right.
1030 */
1031
1032 assert(z_type.width == z_src_type.width);
1033 assert(z_type.length == z_src_type.length);
1034 assert(lp_check_value(z_src_type, z_src));
1035 if (z_src_type.floating) {
1036 /*
1037 * Convert from floating point values
1038 */
1039
1040 if (!z_type.floating) {
1041 z_src = lp_build_clamped_float_to_unsigned_norm(gallivm,
1042 z_src_type,
1043 z_width,
1044 z_src);
1045 }
1046 } else {
1047 /*
1048 * Convert from unsigned normalized values.
1049 */
1050
1051 assert(!z_src_type.sign);
1052 assert(!z_src_type.fixed);
1053 assert(z_src_type.norm);
1054 assert(!z_type.floating);
1055 if (z_src_type.width > z_width) {
1056 LLVMValueRef shift = lp_build_const_int_vec(gallivm, z_src_type,
1057 z_src_type.width - z_width);
1058 z_src = LLVMBuildLShr(builder, z_src, shift, "");
1059 }
1060 }
1061 assert(lp_check_value(z_type, z_src));
1062
1063 lp_build_name(z_src, "z_src");
1064
1065 /* compare src Z to dst Z, returning 'pass' mask */
1066 z_pass = lp_build_cmp(&z_bld, depth->func, z_src, z_dst);
1067
1068 /* mask off bits that failed stencil test */
1069 if (s_pass_mask) {
1070 current_mask = LLVMBuildAnd(builder, current_mask, s_pass_mask, "");
1071 }
1072
1073 if (!stencil[0].enabled) {
1074 /* We can potentially skip all remaining operations here, but only
1075 * if stencil is disabled because we still need to update the stencil
1076 * buffer values. Don't need to update Z buffer values.
1077 */
1078 lp_build_mask_update(mask, z_pass);
1079
1080 if (do_branch) {
1081 lp_build_mask_check(mask);
1082 }
1083 }
1084
1085 if (depth->writemask) {
1086 LLVMValueRef z_pass_mask;
1087
1088 /* mask off bits that failed Z test */
1089 z_pass_mask = LLVMBuildAnd(builder, current_mask, z_pass, "");
1090
1091 /* Mix the old and new Z buffer values.
1092 * z_dst[i] = zselectmask[i] ? z_src[i] : z_dst[i]
1093 */
1094 z_dst = lp_build_select(&z_bld, z_pass_mask, z_src, z_dst);
1095 }
1096
1097 if (stencil[0].enabled) {
1098 /* update stencil buffer values according to z pass/fail result */
1099 LLVMValueRef z_fail_mask, z_pass_mask;
1100
1101 /* apply Z-fail operator */
1102 z_fail_mask = lp_build_andnot(&s_bld, current_mask, z_pass);
1103 stencil_vals = lp_build_stencil_op(&s_bld, stencil, Z_FAIL_OP,
1104 stencil_refs, stencil_vals,
1105 z_fail_mask, front_facing);
1106
1107 /* apply Z-pass operator */
1108 z_pass_mask = LLVMBuildAnd(builder, current_mask, z_pass, "");
1109 stencil_vals = lp_build_stencil_op(&s_bld, stencil, Z_PASS_OP,
1110 stencil_refs, stencil_vals,
1111 z_pass_mask, front_facing);
1112 }
1113 }
1114 else {
1115 /* No depth test: apply Z-pass operator to stencil buffer values which
1116 * passed the stencil test.
1117 */
1118 s_pass_mask = LLVMBuildAnd(builder, current_mask, s_pass_mask, "");
1119 stencil_vals = lp_build_stencil_op(&s_bld, stencil, Z_PASS_OP,
1120 stencil_refs, stencil_vals,
1121 s_pass_mask, front_facing);
1122 }
1123
1124 /* Put Z and stencil bits in the right place */
1125 if (have_z && z_shift) {
1126 LLVMValueRef shift = lp_build_const_int_vec(gallivm, z_type, z_shift);
1127 z_dst = LLVMBuildShl(builder, z_dst, shift, "");
1128 }
1129 if (stencil_vals && stencil_shift)
1130 stencil_vals = LLVMBuildShl(builder, stencil_vals,
1131 stencil_shift, "");
1132
1133 /* Finally, merge the z/stencil values */
1134 if (format_desc->block.bits <= 32) {
1135 if (have_z && have_s)
1136 *z_value = LLVMBuildOr(builder, z_dst, stencil_vals, "");
1137 else if (have_z)
1138 *z_value = z_dst;
1139 else
1140 *z_value = stencil_vals;
1141 *s_value = *z_value;
1142 }
1143 else {
1144 *z_value = z_dst;
1145 *s_value = stencil_vals;
1146 }
1147
1148 if (s_pass_mask)
1149 lp_build_mask_update(mask, s_pass_mask);
1150
1151 if (depth->enabled && stencil[0].enabled)
1152 lp_build_mask_update(mask, z_pass);
1153 }
1154