lp_bld_depth.c revision d5ef59d8b0ce2ea8f0ad983951e696d1679e3eb7
1/**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28/**
29 * @file
30 * Depth/stencil testing to LLVM IR translation.
31 *
32 * To be done accurately/efficiently the depth/stencil test must be done with
33 * the same type/format of the depth/stencil buffer, which implies massaging
34 * the incoming depths to fit into place. Using a more straightforward
35 * type/format for depth/stencil values internally and only convert when
36 * flushing would avoid this, but it would most likely result in depth fighting
37 * artifacts.
38 *
39 * We are free to use a different pixel layout though. Since our basic
40 * processing unit is a quad (2x2 pixel block) we store the depth/stencil
41 * values tiled, a quad at time. That is, a depth buffer containing
42 *
43 *  Z11 Z12 Z13 Z14 ...
44 *  Z21 Z22 Z23 Z24 ...
45 *  Z31 Z32 Z33 Z34 ...
46 *  Z41 Z42 Z43 Z44 ...
47 *  ... ... ... ... ...
48 *
49 * will actually be stored in memory as
50 *
51 *  Z11 Z12 Z21 Z22 Z13 Z14 Z23 Z24 ...
52 *  Z31 Z32 Z41 Z42 Z33 Z34 Z43 Z44 ...
53 *  ... ... ... ... ... ... ... ... ...
54 *
55 *
56 * Stencil test:
57 * Two-sided stencil test is supported but probably not as efficient as
58 * it could be.  Currently, we use if/then/else constructs to do the
59 * operations for front vs. back-facing polygons.  We could probably do
60 * both the front and back arithmetic then use a Select() instruction to
61 * choose the result depending on polyon orientation.  We'd have to
62 * measure performance both ways and see which is better.
63 *
64 * @author Jose Fonseca <jfonseca@vmware.com>
65 */
66
67#include "pipe/p_state.h"
68#include "util/u_format.h"
69
70#include "gallivm/lp_bld_type.h"
71#include "gallivm/lp_bld_arit.h"
72#include "gallivm/lp_bld_bitarit.h"
73#include "gallivm/lp_bld_const.h"
74#include "gallivm/lp_bld_logic.h"
75#include "gallivm/lp_bld_flow.h"
76#include "gallivm/lp_bld_intr.h"
77#include "gallivm/lp_bld_debug.h"
78#include "gallivm/lp_bld_swizzle.h"
79
80#include "lp_bld_depth.h"
81
82
83/** Used to select fields from pipe_stencil_state */
84enum stencil_op {
85   S_FAIL_OP,
86   Z_FAIL_OP,
87   Z_PASS_OP
88};
89
90
91
92/**
93 * Do the stencil test comparison (compare FB stencil values against ref value).
94 * This will be used twice when generating two-sided stencil code.
95 * \param stencil  the front/back stencil state
96 * \param stencilRef  the stencil reference value, replicated as a vector
97 * \param stencilVals  vector of stencil values from framebuffer
98 * \return vector mask of pass/fail values (~0 or 0)
99 */
100static LLVMValueRef
101lp_build_stencil_test_single(struct lp_build_context *bld,
102                             const struct pipe_stencil_state *stencil,
103                             LLVMValueRef stencilRef,
104                             LLVMValueRef stencilVals)
105{
106   const unsigned stencilMax = 255; /* XXX fix */
107   struct lp_type type = bld->type;
108   LLVMValueRef res;
109
110   assert(type.sign);
111
112   assert(stencil->enabled);
113
114   if (stencil->valuemask != stencilMax) {
115      /* compute stencilRef = stencilRef & valuemask */
116      LLVMValueRef valuemask = lp_build_const_int_vec(type, stencil->valuemask);
117      stencilRef = LLVMBuildAnd(bld->builder, stencilRef, valuemask, "");
118      /* compute stencilVals = stencilVals & valuemask */
119      stencilVals = LLVMBuildAnd(bld->builder, stencilVals, valuemask, "");
120   }
121
122   res = lp_build_cmp(bld, stencil->func, stencilRef, stencilVals);
123
124   return res;
125}
126
127
128/**
129 * Do the one or two-sided stencil test comparison.
130 * \sa lp_build_stencil_test_single
131 * \param front_facing  an integer vector mask, indicating front (~0) or back
132 *                      (0) facing polygon. If NULL, assume front-facing.
133 */
134static LLVMValueRef
135lp_build_stencil_test(struct lp_build_context *bld,
136                      const struct pipe_stencil_state stencil[2],
137                      LLVMValueRef stencilRefs[2],
138                      LLVMValueRef stencilVals,
139                      LLVMValueRef front_facing)
140{
141   LLVMValueRef res;
142
143   assert(stencil[0].enabled);
144
145   /* do front face test */
146   res = lp_build_stencil_test_single(bld, &stencil[0],
147                                      stencilRefs[0], stencilVals);
148
149   if (stencil[1].enabled && front_facing) {
150      /* do back face test */
151      LLVMValueRef back_res;
152
153      back_res = lp_build_stencil_test_single(bld, &stencil[1],
154                                              stencilRefs[1], stencilVals);
155
156      res = lp_build_select(bld, front_facing, res, back_res);
157   }
158
159   return res;
160}
161
162
163/**
164 * Apply the stencil operator (add/sub/keep/etc) to the given vector
165 * of stencil values.
166 * \return  new stencil values vector
167 */
168static LLVMValueRef
169lp_build_stencil_op_single(struct lp_build_context *bld,
170                           const struct pipe_stencil_state *stencil,
171                           enum stencil_op op,
172                           LLVMValueRef stencilRef,
173                           LLVMValueRef stencilVals)
174
175{
176   struct lp_type type = bld->type;
177   LLVMValueRef res;
178   LLVMValueRef max = lp_build_const_int_vec(type, 0xff);
179   unsigned stencil_op;
180
181   assert(type.sign);
182
183   switch (op) {
184   case S_FAIL_OP:
185      stencil_op = stencil->fail_op;
186      break;
187   case Z_FAIL_OP:
188      stencil_op = stencil->zfail_op;
189      break;
190   case Z_PASS_OP:
191      stencil_op = stencil->zpass_op;
192      break;
193   default:
194      assert(0 && "Invalid stencil_op mode");
195      stencil_op = PIPE_STENCIL_OP_KEEP;
196   }
197
198   switch (stencil_op) {
199   case PIPE_STENCIL_OP_KEEP:
200      res = stencilVals;
201      /* we can return early for this case */
202      return res;
203   case PIPE_STENCIL_OP_ZERO:
204      res = bld->zero;
205      break;
206   case PIPE_STENCIL_OP_REPLACE:
207      res = stencilRef;
208      break;
209   case PIPE_STENCIL_OP_INCR:
210      res = lp_build_add(bld, stencilVals, bld->one);
211      res = lp_build_min(bld, res, max);
212      break;
213   case PIPE_STENCIL_OP_DECR:
214      res = lp_build_sub(bld, stencilVals, bld->one);
215      res = lp_build_max(bld, res, bld->zero);
216      break;
217   case PIPE_STENCIL_OP_INCR_WRAP:
218      res = lp_build_add(bld, stencilVals, bld->one);
219      res = LLVMBuildAnd(bld->builder, res, max, "");
220      break;
221   case PIPE_STENCIL_OP_DECR_WRAP:
222      res = lp_build_sub(bld, stencilVals, bld->one);
223      res = LLVMBuildAnd(bld->builder, res, max, "");
224      break;
225   case PIPE_STENCIL_OP_INVERT:
226      res = LLVMBuildNot(bld->builder, stencilVals, "");
227      res = LLVMBuildAnd(bld->builder, res, max, "");
228      break;
229   default:
230      assert(0 && "bad stencil op mode");
231      res = bld->undef;
232   }
233
234   return res;
235}
236
237
238/**
239 * Do the one or two-sided stencil test op/update.
240 */
241static LLVMValueRef
242lp_build_stencil_op(struct lp_build_context *bld,
243                    const struct pipe_stencil_state stencil[2],
244                    enum stencil_op op,
245                    LLVMValueRef stencilRefs[2],
246                    LLVMValueRef stencilVals,
247                    LLVMValueRef mask,
248                    LLVMValueRef front_facing)
249
250{
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) {
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->writemask != 0xff) {
270      /* mask &= stencil->writemask */
271      LLVMValueRef writemask = lp_build_const_int_vec(bld->type, stencil->writemask);
272      mask = LLVMBuildAnd(bld->builder, mask, writemask, "");
273      /* res = (res & mask) | (stencilVals & ~mask) */
274      res = lp_build_select_bitwise(bld, writemask, res, stencilVals);
275   }
276   else {
277      /* res = mask ? res : stencilVals */
278      res = lp_build_select(bld, mask, res, stencilVals);
279   }
280
281   return res;
282}
283
284
285
286/**
287 * Return a type appropriate for depth/stencil testing.
288 */
289struct lp_type
290lp_depth_type(const struct util_format_description *format_desc,
291              unsigned length)
292{
293   struct lp_type type;
294   unsigned swizzle;
295
296   assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
297   assert(format_desc->block.width == 1);
298   assert(format_desc->block.height == 1);
299
300   swizzle = format_desc->swizzle[0];
301   assert(swizzle < 4);
302
303   memset(&type, 0, sizeof type);
304   type.width = format_desc->block.bits;
305
306   if(format_desc->channel[swizzle].type == UTIL_FORMAT_TYPE_FLOAT) {
307      type.floating = TRUE;
308      assert(swizzle == 0);
309      assert(format_desc->channel[swizzle].size == format_desc->block.bits);
310   }
311   else if(format_desc->channel[swizzle].type == UTIL_FORMAT_TYPE_UNSIGNED) {
312      assert(format_desc->block.bits <= 32);
313      if(format_desc->channel[swizzle].normalized)
314         type.norm = TRUE;
315   }
316   else
317      assert(0);
318
319   assert(type.width <= length);
320   type.length = length / type.width;
321
322   return type;
323}
324
325
326/**
327 * Compute bitmask and bit shift to apply to the incoming fragment Z values
328 * and the Z buffer values needed before doing the Z comparison.
329 *
330 * Note that we leave the Z bits in the position that we find them
331 * in the Z buffer (typically 0xffffff00 or 0x00ffffff).  That lets us
332 * get by with fewer bit twiddling steps.
333 */
334static boolean
335get_z_shift_and_mask(const struct util_format_description *format_desc,
336                     unsigned *shift, unsigned *mask)
337{
338   const unsigned total_bits = format_desc->block.bits;
339   unsigned z_swizzle;
340   unsigned chan;
341   unsigned padding_left, padding_right;
342
343   assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
344   assert(format_desc->block.width == 1);
345   assert(format_desc->block.height == 1);
346
347   z_swizzle = format_desc->swizzle[0];
348
349   if (z_swizzle == UTIL_FORMAT_SWIZZLE_NONE)
350      return FALSE;
351
352   padding_right = 0;
353   for (chan = 0; chan < z_swizzle; ++chan)
354      padding_right += format_desc->channel[chan].size;
355
356   padding_left =
357      total_bits - (padding_right + format_desc->channel[z_swizzle].size);
358
359   if (padding_left || padding_right) {
360      unsigned long long mask_left = (1ULL << (total_bits - padding_left)) - 1;
361      unsigned long long mask_right = (1ULL << (padding_right)) - 1;
362      *mask = mask_left ^ mask_right;
363   }
364   else {
365      *mask = 0xffffffff;
366   }
367
368   *shift = padding_left;
369
370   return TRUE;
371}
372
373
374/**
375 * Compute bitmask and bit shift to apply to the framebuffer pixel values
376 * to put the stencil bits in the least significant position.
377 * (i.e. 0x000000ff)
378 */
379static boolean
380get_s_shift_and_mask(const struct util_format_description *format_desc,
381                     unsigned *shift, unsigned *mask)
382{
383   unsigned s_swizzle;
384   unsigned chan, sz;
385
386   s_swizzle = format_desc->swizzle[1];
387
388   if (s_swizzle == UTIL_FORMAT_SWIZZLE_NONE)
389      return FALSE;
390
391   *shift = 0;
392   for (chan = 0; chan < s_swizzle; chan++)
393      *shift += format_desc->channel[chan].size;
394
395   sz = format_desc->channel[s_swizzle].size;
396   *mask = (1U << sz) - 1U;
397
398   return TRUE;
399}
400
401
402/**
403 * Perform the occlusion test and increase the counter.
404 * Test the depth mask. Add the number of channel which has none zero mask
405 * into the occlusion counter. e.g. maskvalue is {-1, -1, -1, -1}.
406 * The counter will add 4.
407 *
408 * \param type holds element type of the mask vector.
409 * \param maskvalue is the depth test mask.
410 * \param counter is a pointer of the uint32 counter.
411 */
412static void
413lp_build_occlusion_count(LLVMBuilderRef builder,
414                         struct lp_type type,
415                         LLVMValueRef maskvalue,
416                         LLVMValueRef counter)
417{
418   LLVMValueRef countmask = lp_build_const_int_vec(type, 1);
419   LLVMValueRef countv = LLVMBuildAnd(builder, maskvalue, countmask, "countv");
420   LLVMTypeRef i8v16 = LLVMVectorType(LLVMInt8Type(), 16);
421   LLVMValueRef counti = LLVMBuildBitCast(builder, countv, i8v16, "counti");
422   LLVMValueRef maskarray[4] = {
423      LLVMConstInt(LLVMInt32Type(), 0, 0),
424      LLVMConstInt(LLVMInt32Type(), 4, 0),
425      LLVMConstInt(LLVMInt32Type(), 8, 0),
426      LLVMConstInt(LLVMInt32Type(), 12, 0),
427   };
428   LLVMValueRef shufflemask = LLVMConstVector(maskarray, 4);
429   LLVMValueRef shufflev =  LLVMBuildShuffleVector(builder, counti, LLVMGetUndef(i8v16), shufflemask, "shufflev");
430   LLVMValueRef shuffle = LLVMBuildBitCast(builder, shufflev, LLVMInt32Type(), "shuffle");
431   LLVMValueRef count = lp_build_intrinsic_unary(builder, "llvm.ctpop.i32", LLVMInt32Type(), shuffle);
432   LLVMValueRef orig = LLVMBuildLoad(builder, counter, "orig");
433   LLVMValueRef incr = LLVMBuildAdd(builder, orig, count, "incr");
434   LLVMBuildStore(builder, incr, counter);
435}
436
437
438
439/**
440 * Generate code for performing depth and/or stencil tests.
441 * We operate on a vector of values (typically a 2x2 quad).
442 *
443 * \param depth  the depth test state
444 * \param stencil  the front/back stencil state
445 * \param type  the data type of the fragment depth/stencil values
446 * \param format_desc  description of the depth/stencil surface
447 * \param mask  the alive/dead pixel mask for the quad (vector)
448 * \param stencil_refs  the front/back stencil ref values (scalar)
449 * \param z_src  the incoming depth/stencil values (a 2x2 quad)
450 * \param zs_dst_ptr  pointer to depth/stencil values in framebuffer
451 * \param facing  contains float value indicating front/back facing polygon
452 */
453void
454lp_build_depth_stencil_test(LLVMBuilderRef builder,
455                            const struct pipe_depth_state *depth,
456                            const struct pipe_stencil_state stencil[2],
457                            struct lp_type type,
458                            const struct util_format_description *format_desc,
459                            struct lp_build_mask_context *mask,
460                            LLVMValueRef stencil_refs[2],
461                            LLVMValueRef z_src,
462                            LLVMValueRef zs_dst_ptr,
463                            LLVMValueRef face,
464                            LLVMValueRef counter)
465{
466   struct lp_build_context bld;
467   struct lp_build_context sbld;
468   struct lp_type s_type;
469   LLVMValueRef zs_dst, z_dst = NULL;
470   LLVMValueRef stencil_vals = NULL;
471   LLVMValueRef z_bitmask = NULL, stencil_shift = NULL;
472   LLVMValueRef z_pass = NULL, s_pass_mask = NULL;
473   LLVMValueRef orig_mask = mask->value;
474   LLVMValueRef front_facing = NULL;
475
476   /* Sanity checking */
477   {
478      const unsigned z_swizzle = format_desc->swizzle[0];
479      const unsigned s_swizzle = format_desc->swizzle[1];
480
481      assert(z_swizzle != UTIL_FORMAT_SWIZZLE_NONE ||
482             s_swizzle != UTIL_FORMAT_SWIZZLE_NONE);
483
484      assert(depth->enabled || stencil[0].enabled);
485
486      assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
487      assert(format_desc->block.width == 1);
488      assert(format_desc->block.height == 1);
489
490      if (stencil[0].enabled) {
491         assert(format_desc->format == PIPE_FORMAT_Z24_UNORM_S8_USCALED ||
492                format_desc->format == PIPE_FORMAT_S8_USCALED_Z24_UNORM);
493      }
494
495      assert(z_swizzle < 4);
496      assert(format_desc->block.bits == type.width);
497      if (type.floating) {
498         assert(z_swizzle == 0);
499         assert(format_desc->channel[z_swizzle].type ==
500                UTIL_FORMAT_TYPE_FLOAT);
501         assert(format_desc->channel[z_swizzle].size ==
502                format_desc->block.bits);
503      }
504      else {
505         assert(format_desc->channel[z_swizzle].type ==
506                UTIL_FORMAT_TYPE_UNSIGNED);
507         assert(format_desc->channel[z_swizzle].normalized);
508         assert(!type.fixed);
509         assert(!type.sign);
510         assert(type.norm);
511      }
512   }
513
514
515   /* Setup build context for Z vals */
516   lp_build_context_init(&bld, builder, type);
517
518   /* Setup build context for stencil vals */
519   s_type = lp_type_int_vec(type.width);
520   lp_build_context_init(&sbld, builder, s_type);
521
522   /* Load current z/stencil value from z/stencil buffer */
523   zs_dst = LLVMBuildLoad(builder, zs_dst_ptr, "");
524
525   lp_build_name(zs_dst, "zsbufval");
526
527
528   /* Compute and apply the Z/stencil bitmasks and shifts.
529    */
530   {
531      unsigned z_shift, z_mask;
532      unsigned s_shift, s_mask;
533
534      if (get_z_shift_and_mask(format_desc, &z_shift, &z_mask)) {
535         if (z_shift) {
536            LLVMValueRef shift = lp_build_const_int_vec(type, z_shift);
537            z_src = LLVMBuildLShr(builder, z_src, shift, "");
538         }
539
540         if (z_mask != 0xffffffff) {
541            LLVMValueRef mask = lp_build_const_int_vec(type, z_mask);
542            z_src = LLVMBuildAnd(builder, z_src, mask, "");
543            z_dst = LLVMBuildAnd(builder, zs_dst, mask, "");
544            z_bitmask = mask;  /* used below */
545         }
546         else {
547            z_dst = zs_dst;
548         }
549
550         lp_build_name(z_dst, "zsbuf.z");
551      }
552
553      if (get_s_shift_and_mask(format_desc, &s_shift, &s_mask)) {
554         if (s_shift) {
555            LLVMValueRef shift = lp_build_const_int_vec(type, s_shift);
556            stencil_vals = LLVMBuildLShr(builder, zs_dst, shift, "");
557            stencil_shift = shift;  /* used below */
558         }
559         else {
560            stencil_vals = zs_dst;
561         }
562
563         if (s_mask != 0xffffffff) {
564            LLVMValueRef mask = lp_build_const_int_vec(type, s_mask);
565            stencil_vals = LLVMBuildAnd(builder, stencil_vals, mask, "");
566         }
567
568         lp_build_name(stencil_vals, "stencil");
569      }
570   }
571
572   if (stencil[0].enabled) {
573
574      if (face) {
575         LLVMValueRef zero = LLVMConstReal(LLVMFloatType(), 0.0);
576
577         /* front_facing = face > 0.0 ? ~0 : 0 */
578         front_facing = LLVMBuildFCmp(builder, LLVMRealUGT, face, zero, "");
579         front_facing = LLVMBuildSExt(builder, front_facing,
580                                      LLVMIntType(bld.type.length*bld.type.width),
581                                      "");
582         front_facing = LLVMBuildBitCast(builder, front_facing,
583                                         bld.int_vec_type, "");
584      }
585
586      /* convert scalar stencil refs into vectors */
587      stencil_refs[0] = lp_build_broadcast_scalar(&bld, stencil_refs[0]);
588      stencil_refs[1] = lp_build_broadcast_scalar(&bld, stencil_refs[1]);
589
590      s_pass_mask = lp_build_stencil_test(&sbld, stencil,
591                                          stencil_refs, stencil_vals,
592                                          front_facing);
593
594      /* apply stencil-fail operator */
595      {
596         LLVMValueRef s_fail_mask = lp_build_andnot(&bld, orig_mask, s_pass_mask);
597         stencil_vals = lp_build_stencil_op(&sbld, stencil, S_FAIL_OP,
598                                            stencil_refs, stencil_vals,
599                                            s_fail_mask, front_facing);
600      }
601   }
602
603   if (depth->enabled) {
604      /* compare src Z to dst Z, returning 'pass' mask */
605      z_pass = lp_build_cmp(&bld, depth->func, z_src, z_dst);
606
607      if (!stencil[0].enabled) {
608         /* We can potentially skip all remaining operations here, but only
609          * if stencil is disabled because we still need to update the stencil
610          * buffer values.  Don't need to update Z buffer values.
611          */
612         lp_build_mask_update(mask, z_pass);
613      }
614
615      if (depth->writemask) {
616         LLVMValueRef zselectmask = mask->value;
617
618         /* mask off bits that failed Z test */
619         zselectmask = LLVMBuildAnd(builder, zselectmask, z_pass, "");
620
621         /* mask off bits that failed stencil test */
622         if (s_pass_mask) {
623            zselectmask = LLVMBuildAnd(builder, zselectmask, s_pass_mask, "");
624         }
625
626         /* if combined Z/stencil format, mask off the stencil bits */
627         if (z_bitmask) {
628            zselectmask = LLVMBuildAnd(builder, zselectmask, z_bitmask, "");
629         }
630
631         /* Mix the old and new Z buffer values.
632          * z_dst[i] = (zselectmask[i] & z_src[i]) | (~zselectmask[i] & z_dst[i])
633          */
634         z_dst = lp_build_select_bitwise(&bld, zselectmask, z_src, z_dst);
635      }
636
637      if (stencil[0].enabled) {
638         /* update stencil buffer values according to z pass/fail result */
639         LLVMValueRef z_fail_mask, z_pass_mask;
640
641         /* apply Z-fail operator */
642         z_fail_mask = lp_build_andnot(&bld, orig_mask, z_pass);
643         stencil_vals = lp_build_stencil_op(&sbld, stencil, Z_FAIL_OP,
644                                            stencil_refs, stencil_vals,
645                                            z_fail_mask, front_facing);
646
647         /* apply Z-pass operator */
648         z_pass_mask = LLVMBuildAnd(bld.builder, orig_mask, z_pass, "");
649         stencil_vals = lp_build_stencil_op(&sbld, stencil, Z_PASS_OP,
650                                            stencil_refs, stencil_vals,
651                                            z_pass_mask, front_facing);
652      }
653   }
654   else {
655      /* No depth test: apply Z-pass operator to stencil buffer values which
656       * passed the stencil test.
657       */
658      s_pass_mask = LLVMBuildAnd(bld.builder, orig_mask, s_pass_mask, "");
659      stencil_vals = lp_build_stencil_op(&sbld, stencil, Z_PASS_OP,
660                                         stencil_refs, stencil_vals,
661                                         s_pass_mask, front_facing);
662   }
663
664   /* The Z bits are already in the right place but we may need to shift the
665    * stencil bits before ORing Z with Stencil to make the final pixel value.
666    */
667   if (stencil_vals && stencil_shift)
668      stencil_vals = LLVMBuildShl(bld.builder, stencil_vals,
669                                  stencil_shift, "");
670
671   /* Finally, merge/store the z/stencil values */
672   if ((depth->enabled && depth->writemask) ||
673       (stencil[0].enabled && stencil[0].writemask)) {
674
675      if (z_dst && stencil_vals)
676         zs_dst = LLVMBuildOr(bld.builder, z_dst, stencil_vals, "");
677      else if (z_dst)
678         zs_dst = z_dst;
679      else
680         zs_dst = stencil_vals;
681
682      LLVMBuildStore(builder, zs_dst, zs_dst_ptr);
683   }
684
685   if (s_pass_mask)
686      lp_build_mask_update(mask, s_pass_mask);
687
688   if (depth->enabled && stencil[0].enabled)
689      lp_build_mask_update(mask, z_pass);
690
691   if (counter)
692      lp_build_occlusion_count(builder, type, mask->value, counter);
693}
694