79891cf5d832f47823eaf9ce659ac8e7ade42fb2
[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/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 unsigned chan;
353 unsigned padding_left, padding_right;
354
355 assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
356 assert(format_desc->block.width == 1);
357 assert(format_desc->block.height == 1);
358
359 /* 64bit d/s format is special already extracted 32 bits */
360 total_bits = format_desc->block.bits > 32 ? 32 : format_desc->block.bits;
361
362 z_swizzle = format_desc->swizzle[0];
363
364 if (z_swizzle == UTIL_FORMAT_SWIZZLE_NONE)
365 return FALSE;
366
367 *width = format_desc->channel[z_swizzle].size;
368
369 padding_right = 0;
370 for (chan = 0; chan < z_swizzle; ++chan)
371 padding_right += format_desc->channel[chan].size;
372
373 padding_left =
374 total_bits - (padding_right + *width);
375
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;
380 }
381 else {
382 *mask = 0xffffffff;
383 }
384
385 *shift = padding_right;
386
387 return TRUE;
388 }
389
390
391 /**
392 * Compute bitmask and bit shift to apply to the framebuffer pixel values
393 * to put the stencil bits in the least significant position.
394 * (i.e. 0x000000ff)
395 */
396 static boolean
397 get_s_shift_and_mask(const struct util_format_description *format_desc,
398 unsigned *shift, unsigned *mask)
399 {
400 unsigned s_swizzle;
401 unsigned chan, sz;
402
403 s_swizzle = format_desc->swizzle[1];
404
405 if (s_swizzle == UTIL_FORMAT_SWIZZLE_NONE)
406 return FALSE;
407
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);
411 *shift = 0;
412 *mask = 0xff;
413 return TRUE;
414 }
415
416 *shift = 0;
417 for (chan = 0; chan < s_swizzle; chan++)
418 *shift += format_desc->channel[chan].size;
419
420 sz = format_desc->channel[s_swizzle].size;
421 *mask = (1U << sz) - 1U;
422
423 return TRUE;
424 }
425
426
427 /**
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.
432 * TODO: could get that out of the fs loop.
433 *
434 * \param type holds element type of the mask vector.
435 * \param maskvalue is the depth test mask.
436 * \param counter is a pointer of the uint32 counter.
437 */
438 void
439 lp_build_occlusion_count(struct gallivm_state *gallivm,
440 struct lp_type type,
441 LLVMValueRef maskvalue,
442 LLVMValueRef counter)
443 {
444 LLVMBuilderRef builder = gallivm->builder;
445 LLVMContextRef context = gallivm->context;
446 LLVMValueRef countmask = lp_build_const_int_vec(gallivm, type, 1);
447 LLVMValueRef count, newcount;
448
449 assert(type.length <= 16);
450 assert(type.floating);
451
452 if(util_cpu_caps.has_sse && type.length == 4) {
453 const char *movmskintr = "llvm.x86.sse.movmsk.ps";
454 const char *popcntintr = "llvm.ctpop.i32";
455 LLVMValueRef bits = LLVMBuildBitCast(builder, maskvalue,
456 lp_build_vec_type(gallivm, type), "");
457 bits = lp_build_intrinsic_unary(builder, movmskintr,
458 LLVMInt32TypeInContext(context), bits);
459 count = lp_build_intrinsic_unary(builder, popcntintr,
460 LLVMInt32TypeInContext(context), bits);
461 count = LLVMBuildZExt(builder, count, LLVMIntTypeInContext(context, 64), "");
462 }
463 else if(util_cpu_caps.has_avx && type.length == 8) {
464 const char *movmskintr = "llvm.x86.avx.movmsk.ps.256";
465 const char *popcntintr = "llvm.ctpop.i32";
466 LLVMValueRef bits = LLVMBuildBitCast(builder, maskvalue,
467 lp_build_vec_type(gallivm, type), "");
468 bits = lp_build_intrinsic_unary(builder, movmskintr,
469 LLVMInt32TypeInContext(context), bits);
470 count = lp_build_intrinsic_unary(builder, popcntintr,
471 LLVMInt32TypeInContext(context), bits);
472 count = LLVMBuildZExt(builder, count, LLVMIntTypeInContext(context, 64), "");
473 }
474 else {
475 unsigned i;
476 LLVMValueRef countv = LLVMBuildAnd(builder, maskvalue, countmask, "countv");
477 LLVMTypeRef counttype = LLVMIntTypeInContext(context, type.length * 8);
478 LLVMTypeRef i8vntype = LLVMVectorType(LLVMInt8TypeInContext(context), type.length * 4);
479 LLVMValueRef shufflev, countd;
480 LLVMValueRef shuffles[16];
481 const char *popcntintr = NULL;
482
483 countv = LLVMBuildBitCast(builder, countv, i8vntype, "");
484
485 for (i = 0; i < type.length; i++) {
486 shuffles[i] = lp_build_const_int32(gallivm, 4*i);
487 }
488
489 shufflev = LLVMConstVector(shuffles, type.length);
490 countd = LLVMBuildShuffleVector(builder, countv, LLVMGetUndef(i8vntype), shufflev, "");
491 countd = LLVMBuildBitCast(builder, countd, counttype, "countd");
492
493 /*
494 * XXX FIXME
495 * this is bad on cpus without popcount (on x86 supported by intel
496 * nehalem, amd barcelona, and up - not tied to sse42).
497 * Would be much faster to just sum the 4 elements of the vector with
498 * some horizontal add (shuffle/add/shuffle/add after the initial and).
499 */
500 switch (type.length) {
501 case 4:
502 popcntintr = "llvm.ctpop.i32";
503 break;
504 case 8:
505 popcntintr = "llvm.ctpop.i64";
506 break;
507 case 16:
508 popcntintr = "llvm.ctpop.i128";
509 break;
510 default:
511 assert(0);
512 }
513 count = lp_build_intrinsic_unary(builder, popcntintr, counttype, countd);
514
515 if (type.length > 8) {
516 count = LLVMBuildTrunc(builder, count, LLVMIntTypeInContext(context, 64), "");
517 }
518 else if (type.length < 8) {
519 count = LLVMBuildZExt(builder, count, LLVMIntTypeInContext(context, 64), "");
520 }
521 }
522 newcount = LLVMBuildLoad(builder, counter, "origcount");
523 newcount = LLVMBuildAdd(builder, newcount, count, "newcount");
524 LLVMBuildStore(builder, newcount, counter);
525 }
526
527
528 /**
529 * Load depth/stencil values.
530 * The stored values are linear, swizzle them.
531 *
532 * \param type the data type of the fragment depth/stencil values
533 * \param format_desc description of the depth/stencil surface
534 * \param is_1d whether this resource has only one dimension
535 * \param loop_counter the current loop iteration
536 * \param depth_ptr pointer to the depth/stencil values of this 4x4 block
537 * \param depth_stride stride of the depth/stencil buffer
538 * \param z_fb contains z values loaded from fb (may include padding)
539 * \param s_fb contains s values loaded from fb (may include padding)
540 */
541 void
542 lp_build_depth_stencil_load_swizzled(struct gallivm_state *gallivm,
543 struct lp_type z_src_type,
544 const struct util_format_description *format_desc,
545 boolean is_1d,
546 LLVMValueRef depth_ptr,
547 LLVMValueRef depth_stride,
548 LLVMValueRef *z_fb,
549 LLVMValueRef *s_fb,
550 LLVMValueRef loop_counter)
551 {
552 LLVMBuilderRef builder = gallivm->builder;
553 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH / 4];
554 LLVMValueRef zs_dst1, zs_dst2;
555 LLVMValueRef zs_dst_ptr;
556 LLVMValueRef depth_offset1, depth_offset2;
557 LLVMTypeRef load_ptr_type;
558 unsigned depth_bytes = format_desc->block.bits / 8;
559 struct lp_type zs_type = lp_depth_type(format_desc, z_src_type.length);
560 struct lp_type zs_load_type = zs_type;
561
562 zs_load_type.length = zs_load_type.length / 2;
563 load_ptr_type = LLVMPointerType(lp_build_vec_type(gallivm, zs_load_type), 0);
564
565 if (z_src_type.length == 4) {
566 unsigned i;
567 LLVMValueRef looplsb = LLVMBuildAnd(builder, loop_counter,
568 lp_build_const_int32(gallivm, 1), "");
569 LLVMValueRef loopmsb = LLVMBuildAnd(builder, loop_counter,
570 lp_build_const_int32(gallivm, 2), "");
571 LLVMValueRef offset2 = LLVMBuildMul(builder, loopmsb,
572 depth_stride, "");
573 depth_offset1 = LLVMBuildMul(builder, looplsb,
574 lp_build_const_int32(gallivm, depth_bytes * 2), "");
575 depth_offset1 = LLVMBuildAdd(builder, depth_offset1, offset2, "");
576
577 /* just concatenate the loaded 2x2 values into 4-wide vector */
578 for (i = 0; i < 4; i++) {
579 shuffles[i] = lp_build_const_int32(gallivm, i);
580 }
581 }
582 else {
583 unsigned i;
584 LLVMValueRef loopx2 = LLVMBuildShl(builder, loop_counter,
585 lp_build_const_int32(gallivm, 1), "");
586 assert(z_src_type.length == 8);
587 depth_offset1 = LLVMBuildMul(builder, loopx2, depth_stride, "");
588 /*
589 * We load 2x4 values, and need to swizzle them (order
590 * 0,1,4,5,2,3,6,7) - not so hot with avx unfortunately.
591 */
592 for (i = 0; i < 8; i++) {
593 shuffles[i] = lp_build_const_int32(gallivm, (i&1) + (i&2) * 2 + (i&4) / 2);
594 }
595 }
596
597 depth_offset2 = LLVMBuildAdd(builder, depth_offset1, depth_stride, "");
598
599 /* Load current z/stencil values from z/stencil buffer */
600 zs_dst_ptr = LLVMBuildGEP(builder, depth_ptr, &depth_offset1, 1, "");
601 zs_dst_ptr = LLVMBuildBitCast(builder, zs_dst_ptr, load_ptr_type, "");
602 zs_dst1 = LLVMBuildLoad(builder, zs_dst_ptr, "");
603 if (is_1d) {
604 zs_dst2 = lp_build_undef(gallivm, zs_load_type);
605 }
606 else {
607 zs_dst_ptr = LLVMBuildGEP(builder, depth_ptr, &depth_offset2, 1, "");
608 zs_dst_ptr = LLVMBuildBitCast(builder, zs_dst_ptr, load_ptr_type, "");
609 zs_dst2 = LLVMBuildLoad(builder, zs_dst_ptr, "");
610 }
611
612 *z_fb = LLVMBuildShuffleVector(builder, zs_dst1, zs_dst2,
613 LLVMConstVector(shuffles, zs_type.length), "");
614 *s_fb = *z_fb;
615
616 if (format_desc->block.bits < z_src_type.width) {
617 /* Extend destination ZS values (e.g., when reading from Z16_UNORM) */
618 *z_fb = LLVMBuildZExt(builder, *z_fb,
619 lp_build_int_vec_type(gallivm, z_src_type), "");
620 }
621
622 else if (format_desc->block.bits > 32) {
623 /* rely on llvm to handle too wide vector we have here nicely */
624 unsigned i;
625 struct lp_type typex2 = zs_type;
626 struct lp_type s_type = zs_type;
627 LLVMValueRef shuffles1[LP_MAX_VECTOR_LENGTH / 4];
628 LLVMValueRef shuffles2[LP_MAX_VECTOR_LENGTH / 4];
629 LLVMValueRef tmp;
630
631 typex2.width = typex2.width / 2;
632 typex2.length = typex2.length * 2;
633 s_type.width = s_type.width / 2;
634 s_type.floating = 0;
635
636 tmp = LLVMBuildBitCast(builder, *z_fb,
637 lp_build_vec_type(gallivm, typex2), "");
638
639 for (i = 0; i < zs_type.length; i++) {
640 shuffles1[i] = lp_build_const_int32(gallivm, i * 2);
641 shuffles2[i] = lp_build_const_int32(gallivm, i * 2 + 1);
642 }
643 *z_fb = LLVMBuildShuffleVector(builder, tmp, tmp,
644 LLVMConstVector(shuffles1, zs_type.length), "");
645 *s_fb = LLVMBuildShuffleVector(builder, tmp, tmp,
646 LLVMConstVector(shuffles2, zs_type.length), "");
647 *s_fb = LLVMBuildBitCast(builder, *s_fb,
648 lp_build_vec_type(gallivm, s_type), "");
649 lp_build_name(*s_fb, "s_dst");
650 }
651
652 lp_build_name(*z_fb, "z_dst");
653 lp_build_name(*s_fb, "s_dst");
654 lp_build_name(*z_fb, "z_dst");
655 }
656
657 /**
658 * Store depth/stencil values.
659 * Incoming values are swizzled (typically n 2x2 quads), stored linear.
660 * If there's a mask it will do select/store otherwise just store.
661 *
662 * \param type the data type of the fragment depth/stencil values
663 * \param format_desc description of the depth/stencil surface
664 * \param is_1d whether this resource has only one dimension
665 * \param mask the alive/dead pixel mask for the quad (vector)
666 * \param z_fb z values read from fb (with padding)
667 * \param s_fb s values read from fb (with padding)
668 * \param loop_counter the current loop iteration
669 * \param depth_ptr pointer to the depth/stencil values of this 4x4 block
670 * \param depth_stride stride of the depth/stencil buffer
671 * \param z_value the depth values to store (with padding)
672 * \param s_value the stencil values to store (with padding)
673 */
674 void
675 lp_build_depth_stencil_write_swizzled(struct gallivm_state *gallivm,
676 struct lp_type z_src_type,
677 const struct util_format_description *format_desc,
678 boolean is_1d,
679 struct lp_build_mask_context *mask,
680 LLVMValueRef z_fb,
681 LLVMValueRef s_fb,
682 LLVMValueRef loop_counter,
683 LLVMValueRef depth_ptr,
684 LLVMValueRef depth_stride,
685 LLVMValueRef z_value,
686 LLVMValueRef s_value)
687 {
688 struct lp_build_context z_bld;
689 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH / 4];
690 LLVMBuilderRef builder = gallivm->builder;
691 LLVMValueRef mask_value = NULL;
692 LLVMValueRef zs_dst1, zs_dst2;
693 LLVMValueRef zs_dst_ptr1, zs_dst_ptr2;
694 LLVMValueRef depth_offset1, depth_offset2;
695 LLVMTypeRef load_ptr_type;
696 unsigned depth_bytes = format_desc->block.bits / 8;
697 struct lp_type zs_type = lp_depth_type(format_desc, z_src_type.length);
698 struct lp_type z_type = zs_type;
699 struct lp_type zs_load_type = zs_type;
700
701 zs_load_type.length = zs_load_type.length / 2;
702 load_ptr_type = LLVMPointerType(lp_build_vec_type(gallivm, zs_load_type), 0);
703
704 z_type.width = z_src_type.width;
705
706 lp_build_context_init(&z_bld, gallivm, z_type);
707
708 /*
709 * This is far from ideal, at least for late depth write we should do this
710 * outside the fs loop to avoid all the swizzle stuff.
711 */
712 if (z_src_type.length == 4) {
713 LLVMValueRef looplsb = LLVMBuildAnd(builder, loop_counter,
714 lp_build_const_int32(gallivm, 1), "");
715 LLVMValueRef loopmsb = LLVMBuildAnd(builder, loop_counter,
716 lp_build_const_int32(gallivm, 2), "");
717 LLVMValueRef offset2 = LLVMBuildMul(builder, loopmsb,
718 depth_stride, "");
719 depth_offset1 = LLVMBuildMul(builder, looplsb,
720 lp_build_const_int32(gallivm, depth_bytes * 2), "");
721 depth_offset1 = LLVMBuildAdd(builder, depth_offset1, offset2, "");
722 }
723 else {
724 unsigned i;
725 LLVMValueRef loopx2 = LLVMBuildShl(builder, loop_counter,
726 lp_build_const_int32(gallivm, 1), "");
727 assert(z_src_type.length == 8);
728 depth_offset1 = LLVMBuildMul(builder, loopx2, depth_stride, "");
729 /*
730 * We load 2x4 values, and need to swizzle them (order
731 * 0,1,4,5,2,3,6,7) - not so hot with avx unfortunately.
732 */
733 for (i = 0; i < 8; i++) {
734 shuffles[i] = lp_build_const_int32(gallivm, (i&1) + (i&2) * 2 + (i&4) / 2);
735 }
736 }
737
738 depth_offset2 = LLVMBuildAdd(builder, depth_offset1, depth_stride, "");
739
740 zs_dst_ptr1 = LLVMBuildGEP(builder, depth_ptr, &depth_offset1, 1, "");
741 zs_dst_ptr1 = LLVMBuildBitCast(builder, zs_dst_ptr1, load_ptr_type, "");
742 zs_dst_ptr2 = LLVMBuildGEP(builder, depth_ptr, &depth_offset2, 1, "");
743 zs_dst_ptr2 = LLVMBuildBitCast(builder, zs_dst_ptr2, load_ptr_type, "");
744
745 if (format_desc->block.bits > 32) {
746 s_value = LLVMBuildBitCast(builder, s_value, z_bld.vec_type, "");
747 }
748
749 if (mask) {
750 mask_value = lp_build_mask_value(mask);
751 z_value = lp_build_select(&z_bld, mask_value, z_value, z_fb);
752 if (format_desc->block.bits > 32) {
753 s_fb = LLVMBuildBitCast(builder, s_fb, z_bld.vec_type, "");
754 s_value = lp_build_select(&z_bld, mask_value, s_value, s_fb);
755 }
756 }
757
758 if (zs_type.width < z_src_type.width) {
759 /* Truncate ZS values (e.g., when writing to Z16_UNORM) */
760 z_value = LLVMBuildTrunc(builder, z_value,
761 lp_build_int_vec_type(gallivm, zs_type), "");
762 }
763
764 if (format_desc->block.bits <= 32) {
765 if (z_src_type.length == 4) {
766 zs_dst1 = lp_build_extract_range(gallivm, z_value, 0, 2);
767 zs_dst2 = lp_build_extract_range(gallivm, z_value, 2, 2);
768 }
769 else {
770 assert(z_src_type.length == 8);
771 zs_dst1 = LLVMBuildShuffleVector(builder, z_value, z_value,
772 LLVMConstVector(&shuffles[0],
773 zs_load_type.length), "");
774 zs_dst2 = LLVMBuildShuffleVector(builder, z_value, z_value,
775 LLVMConstVector(&shuffles[4],
776 zs_load_type.length), "");
777 }
778 }
779 else {
780 if (z_src_type.length == 4) {
781 zs_dst1 = lp_build_interleave2(gallivm, z_type,
782 z_value, s_value, 0);
783 zs_dst2 = lp_build_interleave2(gallivm, z_type,
784 z_value, s_value, 1);
785 }
786 else {
787 unsigned i;
788 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH / 2];
789 assert(z_src_type.length == 8);
790 for (i = 0; i < 8; i++) {
791 shuffles[i*2] = lp_build_const_int32(gallivm, (i&1) + (i&2) * 2 + (i&4) / 2);
792 shuffles[i*2+1] = lp_build_const_int32(gallivm, (i&1) + (i&2) * 2 + (i&4) / 2 +
793 z_src_type.length);
794 }
795 zs_dst1 = LLVMBuildShuffleVector(builder, z_value, s_value,
796 LLVMConstVector(&shuffles[0],
797 z_src_type.length), "");
798 zs_dst2 = LLVMBuildShuffleVector(builder, z_value, s_value,
799 LLVMConstVector(&shuffles[8],
800 z_src_type.length), "");
801 }
802 zs_dst1 = LLVMBuildBitCast(builder, zs_dst1,
803 lp_build_vec_type(gallivm, zs_load_type), "");
804 zs_dst2 = LLVMBuildBitCast(builder, zs_dst2,
805 lp_build_vec_type(gallivm, zs_load_type), "");
806 }
807
808 LLVMBuildStore(builder, zs_dst1, zs_dst_ptr1);
809 if (!is_1d) {
810 LLVMBuildStore(builder, zs_dst2, zs_dst_ptr2);
811 }
812 }
813
814 /**
815 * Generate code for performing depth and/or stencil tests.
816 * We operate on a vector of values (typically n 2x2 quads).
817 *
818 * \param depth the depth test state
819 * \param stencil the front/back stencil state
820 * \param type the data type of the fragment depth/stencil values
821 * \param format_desc description of the depth/stencil surface
822 * \param mask the alive/dead pixel mask for the quad (vector)
823 * \param stencil_refs the front/back stencil ref values (scalar)
824 * \param z_src the incoming depth/stencil values (n 2x2 quad values, float32)
825 * \param zs_dst the depth/stencil values in framebuffer
826 * \param face contains boolean value indicating front/back facing polygon
827 */
828 void
829 lp_build_depth_stencil_test(struct gallivm_state *gallivm,
830 const struct pipe_depth_state *depth,
831 const struct pipe_stencil_state stencil[2],
832 struct lp_type z_src_type,
833 const struct util_format_description *format_desc,
834 struct lp_build_mask_context *mask,
835 LLVMValueRef stencil_refs[2],
836 LLVMValueRef z_src,
837 LLVMValueRef z_fb,
838 LLVMValueRef s_fb,
839 LLVMValueRef face,
840 LLVMValueRef *z_value,
841 LLVMValueRef *s_value,
842 boolean do_branch)
843 {
844 LLVMBuilderRef builder = gallivm->builder;
845 struct lp_type z_type;
846 struct lp_build_context z_bld;
847 struct lp_build_context s_bld;
848 struct lp_type s_type;
849 unsigned z_shift = 0, z_width = 0, z_mask = 0;
850 LLVMValueRef z_dst = NULL;
851 LLVMValueRef stencil_vals = NULL;
852 LLVMValueRef z_bitmask = NULL, stencil_shift = NULL;
853 LLVMValueRef z_pass = NULL, s_pass_mask = NULL;
854 LLVMValueRef orig_mask = lp_build_mask_value(mask);
855 LLVMValueRef front_facing = NULL;
856 boolean have_z, have_s;
857
858 /*
859 * Depths are expected to be between 0 and 1, even if they are stored in
860 * floats. Setting these bits here will ensure that the lp_build_conv() call
861 * below won't try to unnecessarily clamp the incoming values.
862 */
863 if(z_src_type.floating) {
864 z_src_type.sign = FALSE;
865 z_src_type.norm = TRUE;
866 }
867 else {
868 assert(!z_src_type.sign);
869 assert(z_src_type.norm);
870 }
871
872 /* Pick the type matching the depth-stencil format. */
873 z_type = lp_depth_type(format_desc, z_src_type.length);
874
875 /* Pick the intermediate type for depth operations. */
876 z_type.width = z_src_type.width;
877 assert(z_type.length == z_src_type.length);
878
879 /* FIXME: for non-float depth/stencil might generate better code
880 * if we'd always split it up to use 128bit operations.
881 * For stencil we'd almost certainly want to pack to 8xi16 values,
882 * for z just run twice.
883 */
884
885 /* Sanity checking */
886 {
887 const unsigned z_swizzle = format_desc->swizzle[0];
888 const unsigned s_swizzle = format_desc->swizzle[1];
889
890 assert(z_swizzle != UTIL_FORMAT_SWIZZLE_NONE ||
891 s_swizzle != UTIL_FORMAT_SWIZZLE_NONE);
892
893 assert(depth->enabled || stencil[0].enabled);
894
895 assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
896 assert(format_desc->block.width == 1);
897 assert(format_desc->block.height == 1);
898
899 if (stencil[0].enabled) {
900 assert(s_swizzle < 4);
901 assert(format_desc->channel[s_swizzle].type == UTIL_FORMAT_TYPE_UNSIGNED);
902 assert(format_desc->channel[s_swizzle].pure_integer);
903 assert(!format_desc->channel[s_swizzle].normalized);
904 assert(format_desc->channel[s_swizzle].size == 8);
905 }
906
907 if (depth->enabled) {
908 assert(z_swizzle < 4);
909 if (z_type.floating) {
910 assert(z_swizzle == 0);
911 assert(format_desc->channel[z_swizzle].type ==
912 UTIL_FORMAT_TYPE_FLOAT);
913 assert(format_desc->channel[z_swizzle].size == 32);
914 }
915 else {
916 assert(format_desc->channel[z_swizzle].type ==
917 UTIL_FORMAT_TYPE_UNSIGNED);
918 assert(format_desc->channel[z_swizzle].normalized);
919 assert(!z_type.fixed);
920 }
921 }
922 }
923
924
925 /* Setup build context for Z vals */
926 lp_build_context_init(&z_bld, gallivm, z_type);
927
928 /* Setup build context for stencil vals */
929 s_type = lp_int_type(z_type);
930 lp_build_context_init(&s_bld, gallivm, s_type);
931
932 /* Compute and apply the Z/stencil bitmasks and shifts.
933 */
934 {
935 unsigned s_shift, s_mask;
936
937 z_dst = z_fb;
938 stencil_vals = s_fb;
939
940 have_z = get_z_shift_and_mask(format_desc, &z_shift, &z_width, &z_mask);
941 have_s = get_s_shift_and_mask(format_desc, &s_shift, &s_mask);
942
943 if (have_z) {
944 if (z_mask != 0xffffffff) {
945 z_bitmask = lp_build_const_int_vec(gallivm, z_type, z_mask);
946 }
947
948 /*
949 * Align the framebuffer Z 's LSB to the right.
950 */
951 if (z_shift) {
952 LLVMValueRef shift = lp_build_const_int_vec(gallivm, z_type, z_shift);
953 z_dst = LLVMBuildLShr(builder, z_dst, shift, "z_dst");
954 } else if (z_bitmask) {
955 z_dst = LLVMBuildAnd(builder, z_dst, z_bitmask, "z_dst");
956 } else {
957 lp_build_name(z_dst, "z_dst");
958 }
959 }
960
961 if (have_s) {
962 if (s_shift) {
963 LLVMValueRef shift = lp_build_const_int_vec(gallivm, s_type, s_shift);
964 stencil_vals = LLVMBuildLShr(builder, stencil_vals, shift, "");
965 stencil_shift = shift; /* used below */
966 }
967
968 if (s_mask != 0xffffffff) {
969 LLVMValueRef mask = lp_build_const_int_vec(gallivm, s_type, s_mask);
970 stencil_vals = LLVMBuildAnd(builder, stencil_vals, mask, "");
971 }
972
973 lp_build_name(stencil_vals, "s_dst");
974 }
975 }
976
977 if (stencil[0].enabled) {
978
979 if (face) {
980 LLVMValueRef zero = lp_build_const_int32(gallivm, 0);
981
982 /* front_facing = face != 0 ? ~0 : 0 */
983 front_facing = LLVMBuildICmp(builder, LLVMIntNE, face, zero, "");
984 front_facing = LLVMBuildSExt(builder, front_facing,
985 LLVMIntTypeInContext(gallivm->context,
986 s_bld.type.length*s_bld.type.width),
987 "");
988 front_facing = LLVMBuildBitCast(builder, front_facing,
989 s_bld.int_vec_type, "");
990 }
991
992 /* convert scalar stencil refs into vectors */
993 stencil_refs[0] = lp_build_broadcast_scalar(&s_bld, stencil_refs[0]);
994 stencil_refs[1] = lp_build_broadcast_scalar(&s_bld, stencil_refs[1]);
995
996 s_pass_mask = lp_build_stencil_test(&s_bld, stencil,
997 stencil_refs, stencil_vals,
998 front_facing);
999
1000 /* apply stencil-fail operator */
1001 {
1002 LLVMValueRef s_fail_mask = lp_build_andnot(&s_bld, orig_mask, s_pass_mask);
1003 stencil_vals = lp_build_stencil_op(&s_bld, stencil, S_FAIL_OP,
1004 stencil_refs, stencil_vals,
1005 s_fail_mask, front_facing);
1006 }
1007 }
1008
1009 if (depth->enabled) {
1010 /*
1011 * Convert fragment Z to the desired type, aligning the LSB to the right.
1012 */
1013
1014 assert(z_type.width == z_src_type.width);
1015 assert(z_type.length == z_src_type.length);
1016 assert(lp_check_value(z_src_type, z_src));
1017 if (z_src_type.floating) {
1018 /*
1019 * Convert from floating point values
1020 */
1021
1022 if (!z_type.floating) {
1023 z_src = lp_build_clamped_float_to_unsigned_norm(gallivm,
1024 z_src_type,
1025 z_width,
1026 z_src);
1027 }
1028 } else {
1029 /*
1030 * Convert from unsigned normalized values.
1031 */
1032
1033 assert(!z_src_type.sign);
1034 assert(!z_src_type.fixed);
1035 assert(z_src_type.norm);
1036 assert(!z_type.floating);
1037 if (z_src_type.width > z_width) {
1038 LLVMValueRef shift = lp_build_const_int_vec(gallivm, z_src_type,
1039 z_src_type.width - z_width);
1040 z_src = LLVMBuildLShr(builder, z_src, shift, "");
1041 }
1042 }
1043 assert(lp_check_value(z_type, z_src));
1044
1045 lp_build_name(z_src, "z_src");
1046
1047 /* compare src Z to dst Z, returning 'pass' mask */
1048 z_pass = lp_build_cmp(&z_bld, depth->func, z_src, z_dst);
1049
1050 if (!stencil[0].enabled) {
1051 /* We can potentially skip all remaining operations here, but only
1052 * if stencil is disabled because we still need to update the stencil
1053 * buffer values. Don't need to update Z buffer values.
1054 */
1055 lp_build_mask_update(mask, z_pass);
1056
1057 if (do_branch) {
1058 lp_build_mask_check(mask);
1059 do_branch = FALSE;
1060 }
1061 }
1062
1063 if (depth->writemask) {
1064 LLVMValueRef zselectmask;
1065
1066 /* mask off bits that failed Z test */
1067 zselectmask = LLVMBuildAnd(builder, orig_mask, z_pass, "");
1068
1069 /* mask off bits that failed stencil test */
1070 if (s_pass_mask) {
1071 zselectmask = LLVMBuildAnd(builder, zselectmask, s_pass_mask, "");
1072 }
1073
1074 /* Mix the old and new Z buffer values.
1075 * z_dst[i] = zselectmask[i] ? z_src[i] : z_dst[i]
1076 */
1077 z_dst = lp_build_select(&z_bld, zselectmask, z_src, z_dst);
1078 }
1079
1080 if (stencil[0].enabled) {
1081 /* update stencil buffer values according to z pass/fail result */
1082 LLVMValueRef z_fail_mask, z_pass_mask;
1083
1084 /* apply Z-fail operator */
1085 z_fail_mask = lp_build_andnot(&s_bld, orig_mask, z_pass);
1086 stencil_vals = lp_build_stencil_op(&s_bld, stencil, Z_FAIL_OP,
1087 stencil_refs, stencil_vals,
1088 z_fail_mask, front_facing);
1089
1090 /* apply Z-pass operator */
1091 z_pass_mask = LLVMBuildAnd(builder, orig_mask, z_pass, "");
1092 stencil_vals = lp_build_stencil_op(&s_bld, stencil, Z_PASS_OP,
1093 stencil_refs, stencil_vals,
1094 z_pass_mask, front_facing);
1095 }
1096 }
1097 else {
1098 /* No depth test: apply Z-pass operator to stencil buffer values which
1099 * passed the stencil test.
1100 */
1101 s_pass_mask = LLVMBuildAnd(builder, orig_mask, s_pass_mask, "");
1102 stencil_vals = lp_build_stencil_op(&s_bld, stencil, Z_PASS_OP,
1103 stencil_refs, stencil_vals,
1104 s_pass_mask, front_facing);
1105 }
1106
1107 /* Put Z and stencil bits in the right place */
1108 if (have_z && z_shift) {
1109 LLVMValueRef shift = lp_build_const_int_vec(gallivm, z_type, z_shift);
1110 z_dst = LLVMBuildShl(builder, z_dst, shift, "");
1111 }
1112 if (stencil_vals && stencil_shift)
1113 stencil_vals = LLVMBuildShl(builder, stencil_vals,
1114 stencil_shift, "");
1115
1116 /* Finally, merge the z/stencil values */
1117 if (format_desc->block.bits <= 32) {
1118 if (have_z && have_s)
1119 *z_value = LLVMBuildOr(builder, z_dst, stencil_vals, "");
1120 else if (have_z)
1121 *z_value = z_dst;
1122 else
1123 *z_value = stencil_vals;
1124 *s_value = *z_value;
1125 }
1126 else {
1127 *z_value = z_dst;
1128 *s_value = stencil_vals;
1129 }
1130
1131 if (s_pass_mask)
1132 lp_build_mask_update(mask, s_pass_mask);
1133
1134 if (depth->enabled && stencil[0].enabled)
1135 lp_build_mask_update(mask, z_pass);
1136 }
1137