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