lp_bld_depth.c revision caa05ef41996f7d0ac6b77f2fe86b1771cb10e43
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_const.h"
73#include "gallivm/lp_bld_logic.h"
74#include "gallivm/lp_bld_flow.h"
75#include "gallivm/lp_bld_debug.h"
76#include "gallivm/lp_bld_swizzle.h"
77
78#include "lp_bld_depth.h"
79
80
81/** Used to select fields from pipe_stencil_state */
82enum stencil_op {
83   S_FAIL_OP,
84   Z_FAIL_OP,
85   Z_PASS_OP
86};
87
88
89
90/**
91 * Do the stencil test comparison (compare FB stencil values against ref value).
92 * This will be used twice when generating two-sided stencil code.
93 * \param stencil  the front/back stencil state
94 * \param stencilRef  the stencil reference value, replicated as a vector
95 * \param stencilVals  vector of stencil values from framebuffer
96 * \return vector mask of pass/fail values (~0 or 0)
97 */
98static LLVMValueRef
99lp_build_stencil_test_single(struct lp_build_context *bld,
100                             const struct pipe_stencil_state *stencil,
101                             LLVMValueRef stencilRef,
102                             LLVMValueRef stencilVals)
103{
104   const unsigned stencilMax = 255; /* XXX fix */
105   struct lp_type type = bld->type;
106   LLVMValueRef res;
107
108   assert(type.sign);
109
110   assert(stencil->enabled);
111
112   if (stencil->valuemask != stencilMax) {
113      /* compute stencilRef = stencilRef & valuemask */
114      LLVMValueRef valuemask = lp_build_const_int_vec(type, stencil->valuemask);
115      stencilRef = LLVMBuildAnd(bld->builder, stencilRef, valuemask, "");
116      /* compute stencilVals = stencilVals & valuemask */
117      stencilVals = LLVMBuildAnd(bld->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 face  an integer indicating front (+) or back (-) facing polygon.
130 *              If NULL, assume front-facing.
131 */
132static LLVMValueRef
133lp_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 face)
138{
139   LLVMValueRef res;
140
141   assert(stencil[0].enabled);
142
143   if (stencil[1].enabled && face) {
144      /* do two-sided test */
145      struct lp_build_flow_context *flow_ctx;
146      struct lp_build_if_state if_ctx;
147      LLVMValueRef front_facing;
148      LLVMValueRef zero = LLVMConstReal(LLVMFloatType(), 0.0);
149      LLVMValueRef result = bld->undef;
150
151      flow_ctx = lp_build_flow_create(bld->builder);
152      lp_build_flow_scope_begin(flow_ctx);
153
154      lp_build_flow_scope_declare(flow_ctx, &result);
155
156      /* front_facing = face > 0.0 */
157      front_facing = LLVMBuildFCmp(bld->builder, LLVMRealUGT, face, zero, "");
158
159      lp_build_if(&if_ctx, flow_ctx, bld->builder, front_facing);
160      {
161         result = lp_build_stencil_test_single(bld, &stencil[0],
162                                               stencilRefs[0], stencilVals);
163      }
164      lp_build_else(&if_ctx);
165      {
166         result = lp_build_stencil_test_single(bld, &stencil[1],
167                                               stencilRefs[1], stencilVals);
168      }
169      lp_build_endif(&if_ctx);
170
171      lp_build_flow_scope_end(flow_ctx);
172      lp_build_flow_destroy(flow_ctx);
173
174      res = result;
175   }
176   else {
177      /* do single-side test */
178      res = lp_build_stencil_test_single(bld, &stencil[0],
179                                         stencilRefs[0], stencilVals);
180   }
181
182   return res;
183}
184
185
186/**
187 * Apply the stencil operator (add/sub/keep/etc) to the given vector
188 * of stencil values.
189 * \return  new stencil values vector
190 */
191static LLVMValueRef
192lp_build_stencil_op_single(struct lp_build_context *bld,
193                           const struct pipe_stencil_state *stencil,
194                           enum stencil_op op,
195                           LLVMValueRef stencilRef,
196                           LLVMValueRef stencilVals,
197                           LLVMValueRef mask)
198
199{
200   const unsigned stencilMax = 255; /* XXX fix */
201   struct lp_type type = bld->type;
202   LLVMValueRef res;
203   LLVMValueRef max = lp_build_const_int_vec(type, stencilMax);
204   unsigned stencil_op;
205
206   assert(type.sign);
207
208   switch (op) {
209   case S_FAIL_OP:
210      stencil_op = stencil->fail_op;
211      break;
212   case Z_FAIL_OP:
213      stencil_op = stencil->zfail_op;
214      break;
215   case Z_PASS_OP:
216      stencil_op = stencil->zpass_op;
217      break;
218   default:
219      assert(0 && "Invalid stencil_op mode");
220      stencil_op = PIPE_STENCIL_OP_KEEP;
221   }
222
223   switch (stencil_op) {
224   case PIPE_STENCIL_OP_KEEP:
225      res = stencilVals;
226      /* we can return early for this case */
227      return res;
228   case PIPE_STENCIL_OP_ZERO:
229      res = bld->zero;
230      break;
231   case PIPE_STENCIL_OP_REPLACE:
232      res = stencilRef;
233      break;
234   case PIPE_STENCIL_OP_INCR:
235      res = lp_build_add(bld, stencilVals, bld->one);
236      res = lp_build_min(bld, res, max);
237      break;
238   case PIPE_STENCIL_OP_DECR:
239      res = lp_build_sub(bld, stencilVals, bld->one);
240      res = lp_build_max(bld, res, bld->zero);
241      break;
242   case PIPE_STENCIL_OP_INCR_WRAP:
243      res = lp_build_add(bld, stencilVals, bld->one);
244      res = LLVMBuildAnd(bld->builder, res, max, "");
245      break;
246   case PIPE_STENCIL_OP_DECR_WRAP:
247      res = lp_build_sub(bld, stencilVals, bld->one);
248      res = LLVMBuildAnd(bld->builder, res, max, "");
249      break;
250   case PIPE_STENCIL_OP_INVERT:
251      res = LLVMBuildNot(bld->builder, stencilVals, "");
252      res = LLVMBuildAnd(bld->builder, res, max, "");
253      break;
254   default:
255      assert(0 && "bad stencil op mode");
256      res = NULL;
257   }
258
259   if (stencil->writemask != stencilMax) {
260      /* compute res = (res & mask) | (stencilVals & ~mask) */
261      LLVMValueRef mask = lp_build_const_int_vec(type, stencil->writemask);
262      LLVMValueRef cmask = LLVMBuildNot(bld->builder, mask, "notWritemask");
263      LLVMValueRef t1 = LLVMBuildAnd(bld->builder, res, mask, "t1");
264      LLVMValueRef t2 = LLVMBuildAnd(bld->builder, stencilVals, cmask, "t2");
265      res = LLVMBuildOr(bld->builder, t1, t2, "t1_or_t2");
266   }
267
268   /* only the update the vector elements enabled by 'mask' */
269   res = lp_build_select(bld, mask, res, stencilVals);
270
271   return res;
272}
273
274
275/**
276 * Do the one or two-sided stencil test op/update.
277 */
278static LLVMValueRef
279lp_build_stencil_op(struct lp_build_context *bld,
280                    const struct pipe_stencil_state stencil[2],
281                    enum stencil_op op,
282                    LLVMValueRef stencilRefs[2],
283                    LLVMValueRef stencilVals,
284                    LLVMValueRef mask,
285                    LLVMValueRef face)
286
287{
288   assert(stencil[0].enabled);
289
290   if (stencil[1].enabled && face) {
291      /* do two-sided op */
292      struct lp_build_flow_context *flow_ctx;
293      struct lp_build_if_state if_ctx;
294      LLVMValueRef front_facing;
295      LLVMValueRef zero = LLVMConstReal(LLVMFloatType(), 0.0);
296      LLVMValueRef result = bld->undef;
297
298      flow_ctx = lp_build_flow_create(bld->builder);
299      lp_build_flow_scope_begin(flow_ctx);
300
301      lp_build_flow_scope_declare(flow_ctx, &result);
302
303      /* front_facing = face > 0.0 */
304      front_facing = LLVMBuildFCmp(bld->builder, LLVMRealUGT, face, zero, "");
305
306      lp_build_if(&if_ctx, flow_ctx, bld->builder, front_facing);
307      {
308         result = lp_build_stencil_op_single(bld, &stencil[0], op,
309                                             stencilRefs[0], stencilVals, mask);
310      }
311      lp_build_else(&if_ctx);
312      {
313         result = lp_build_stencil_op_single(bld, &stencil[1], op,
314                                             stencilRefs[1], stencilVals, mask);
315      }
316      lp_build_endif(&if_ctx);
317
318      lp_build_flow_scope_end(flow_ctx);
319      lp_build_flow_destroy(flow_ctx);
320
321      return result;
322   }
323   else {
324      /* do single-sided op */
325      return lp_build_stencil_op_single(bld, &stencil[0], op,
326                                        stencilRefs[0], stencilVals, mask);
327   }
328}
329
330
331
332/**
333 * Return a type appropriate for depth/stencil testing.
334 */
335struct lp_type
336lp_depth_type(const struct util_format_description *format_desc,
337              unsigned length)
338{
339   struct lp_type type;
340   unsigned swizzle;
341
342   assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
343   assert(format_desc->block.width == 1);
344   assert(format_desc->block.height == 1);
345
346   swizzle = format_desc->swizzle[0];
347   assert(swizzle < 4);
348
349   memset(&type, 0, sizeof type);
350   type.width = format_desc->block.bits;
351
352   if(format_desc->channel[swizzle].type == UTIL_FORMAT_TYPE_FLOAT) {
353      type.floating = TRUE;
354      assert(swizzle == 0);
355      assert(format_desc->channel[swizzle].size == format_desc->block.bits);
356   }
357   else if(format_desc->channel[swizzle].type == UTIL_FORMAT_TYPE_UNSIGNED) {
358      assert(format_desc->block.bits <= 32);
359      if(format_desc->channel[swizzle].normalized)
360         type.norm = TRUE;
361   }
362   else
363      assert(0);
364
365   assert(type.width <= length);
366   type.length = length / type.width;
367
368   return type;
369}
370
371
372/**
373 * Compute bitmask and bit shift to apply to the incoming fragment Z values
374 * and the Z buffer values needed before doing the Z comparison.
375 *
376 * Note that we leave the Z bits in the position that we find them
377 * in the Z buffer (typically 0xffffff00 or 0x00ffffff).  That lets us
378 * get by with fewer bit twiddling steps.
379 */
380static boolean
381get_z_shift_and_mask(const struct util_format_description *format_desc,
382                     unsigned *shift, unsigned *mask)
383{
384   const unsigned total_bits = format_desc->block.bits;
385   unsigned z_swizzle;
386   unsigned chan;
387   unsigned padding_left, padding_right;
388
389   assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
390   assert(format_desc->block.width == 1);
391   assert(format_desc->block.height == 1);
392
393   z_swizzle = format_desc->swizzle[0];
394
395   if (z_swizzle == UTIL_FORMAT_SWIZZLE_NONE)
396      return FALSE;
397
398   padding_right = 0;
399   for (chan = 0; chan < z_swizzle; ++chan)
400      padding_right += format_desc->channel[chan].size;
401
402   padding_left =
403      total_bits - (padding_right + format_desc->channel[z_swizzle].size);
404
405   if (padding_left || padding_right) {
406      unsigned long long mask_left = (1ULL << (total_bits - padding_left)) - 1;
407      unsigned long long mask_right = (1ULL << (padding_right)) - 1;
408      *mask = mask_left ^ mask_right;
409   }
410   else {
411      *mask = 0xffffffff;
412   }
413
414   *shift = padding_left;
415
416   return TRUE;
417}
418
419
420/**
421 * Compute bitmask and bit shift to apply to the framebuffer pixel values
422 * to put the stencil bits in the least significant position.
423 * (i.e. 0x000000ff)
424 */
425static boolean
426get_s_shift_and_mask(const struct util_format_description *format_desc,
427                     unsigned *shift, unsigned *mask)
428{
429   unsigned s_swizzle;
430   unsigned chan, sz;
431
432   s_swizzle = format_desc->swizzle[1];
433
434   if (s_swizzle == UTIL_FORMAT_SWIZZLE_NONE)
435      return FALSE;
436
437   *shift = 0;
438   for (chan = 0; chan < s_swizzle; chan++)
439      *shift += format_desc->channel[chan].size;
440
441   sz = format_desc->channel[s_swizzle].size;
442   *mask = (1U << sz) - 1U;
443
444   return TRUE;
445}
446
447
448
449/**
450 * Generate code for performing depth and/or stencil tests.
451 * We operate on a vector of values (typically a 2x2 quad).
452 *
453 * \param depth  the depth test state
454 * \param stencil  the front/back stencil state
455 * \param type  the data type of the fragment depth/stencil values
456 * \param format_desc  description of the depth/stencil surface
457 * \param mask  the alive/dead pixel mask for the quad (vector)
458 * \param stencil_refs  the front/back stencil ref values (scalar)
459 * \param z_src  the incoming depth/stencil values (a 2x2 quad)
460 * \param zs_dst_ptr  pointer to depth/stencil values in framebuffer
461 * \param facing  contains float value indicating front/back facing polygon
462 */
463void
464lp_build_depth_stencil_test(LLVMBuilderRef builder,
465                            const struct pipe_depth_state *depth,
466                            const struct pipe_stencil_state stencil[2],
467                            struct lp_type type,
468                            const struct util_format_description *format_desc,
469                            struct lp_build_mask_context *mask,
470                            LLVMValueRef stencil_refs[2],
471                            LLVMValueRef z_src,
472                            LLVMValueRef zs_dst_ptr,
473                            LLVMValueRef face)
474{
475   struct lp_build_context bld;
476   struct lp_build_context sbld;
477   struct lp_type s_type;
478   LLVMValueRef zs_dst, z_dst = NULL;
479   LLVMValueRef stencil_vals = NULL;
480   LLVMValueRef z_bitmask = NULL, stencil_shift = NULL;
481   LLVMValueRef z_pass = NULL, s_pass_mask = NULL;
482   LLVMValueRef orig_mask = mask->value;
483
484   /* Sanity checking */
485   {
486      const unsigned z_swizzle = format_desc->swizzle[0];
487      const unsigned s_swizzle = format_desc->swizzle[1];
488
489      assert(z_swizzle != UTIL_FORMAT_SWIZZLE_NONE ||
490             s_swizzle != UTIL_FORMAT_SWIZZLE_NONE);
491
492      assert(depth->enabled || stencil[0].enabled);
493
494      assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
495      assert(format_desc->block.width == 1);
496      assert(format_desc->block.height == 1);
497
498      if (stencil[0].enabled) {
499         assert(format_desc->format == PIPE_FORMAT_Z24_UNORM_S8_USCALED ||
500                format_desc->format == PIPE_FORMAT_S8_USCALED_Z24_UNORM);
501      }
502
503      assert(z_swizzle < 4);
504      assert(format_desc->block.bits == type.width);
505      if (type.floating) {
506         assert(z_swizzle == 0);
507         assert(format_desc->channel[z_swizzle].type ==
508                UTIL_FORMAT_TYPE_FLOAT);
509         assert(format_desc->channel[z_swizzle].size ==
510                format_desc->block.bits);
511      }
512      else {
513         assert(format_desc->channel[z_swizzle].type ==
514                UTIL_FORMAT_TYPE_UNSIGNED);
515         assert(format_desc->channel[z_swizzle].normalized);
516         assert(!type.fixed);
517         assert(!type.sign);
518         assert(type.norm);
519      }
520   }
521
522
523   /* Setup build context for Z vals */
524   lp_build_context_init(&bld, builder, type);
525
526   /* Setup build context for stencil vals */
527   s_type = lp_type_int_vec(type.width);
528   lp_build_context_init(&sbld, builder, s_type);
529
530   /* Load current z/stencil value from z/stencil buffer */
531   zs_dst = LLVMBuildLoad(builder, zs_dst_ptr, "");
532
533   lp_build_name(zs_dst, "zsbufval");
534
535
536   /* Compute and apply the Z/stencil bitmasks and shifts.
537    */
538   {
539      unsigned z_shift, z_mask;
540      unsigned s_shift, s_mask;
541
542      if (get_z_shift_and_mask(format_desc, &z_shift, &z_mask)) {
543         if (z_shift) {
544            LLVMValueRef shift = lp_build_const_int_vec(type, z_shift);
545            z_src = LLVMBuildLShr(builder, z_src, shift, "");
546         }
547
548         if (z_mask != 0xffffffff) {
549            LLVMValueRef mask = lp_build_const_int_vec(type, z_mask);
550            z_src = LLVMBuildAnd(builder, z_src, mask, "");
551            z_dst = LLVMBuildAnd(builder, zs_dst, mask, "");
552            z_bitmask = mask;  /* used below */
553         }
554         else {
555            z_dst = zs_dst;
556         }
557
558         lp_build_name(z_dst, "zsbuf.z");
559      }
560
561      if (get_s_shift_and_mask(format_desc, &s_shift, &s_mask)) {
562         if (s_shift) {
563            LLVMValueRef shift = lp_build_const_int_vec(type, s_shift);
564            stencil_vals = LLVMBuildLShr(builder, zs_dst, shift, "");
565            stencil_shift = shift;  /* used below */
566         }
567         else {
568            stencil_vals = zs_dst;
569         }
570
571         if (s_mask != 0xffffffff) {
572            LLVMValueRef mask = lp_build_const_int_vec(type, s_mask);
573            stencil_vals = LLVMBuildAnd(builder, stencil_vals, mask, "");
574         }
575
576         lp_build_name(stencil_vals, "stencil");
577      }
578   }
579
580
581   if (stencil[0].enabled) {
582      /* convert scalar stencil refs into vectors */
583      stencil_refs[0] = lp_build_broadcast_scalar(&bld, stencil_refs[0]);
584      stencil_refs[1] = lp_build_broadcast_scalar(&bld, stencil_refs[1]);
585
586      s_pass_mask = lp_build_stencil_test(&sbld, stencil,
587                                          stencil_refs, stencil_vals, face);
588
589      /* apply stencil-fail operator */
590      {
591         LLVMValueRef s_fail_mask = lp_build_andc(&bld, orig_mask, s_pass_mask);
592         stencil_vals = lp_build_stencil_op(&sbld, stencil, S_FAIL_OP,
593                                            stencil_refs, stencil_vals,
594                                            s_fail_mask, face);
595      }
596   }
597
598   if (depth->enabled) {
599      /* compare src Z to dst Z, returning 'pass' mask */
600      z_pass = lp_build_cmp(&bld, depth->func, z_src, z_dst);
601
602      if (!stencil[0].enabled) {
603         /* We can potentially skip all remaining operations here, but only
604          * if stencil is disabled because we still need to update the stencil
605          * buffer values.  Don't need to update Z buffer values.
606          */
607         lp_build_mask_update(mask, z_pass);
608      }
609
610      if (depth->writemask) {
611         LLVMValueRef zselectmask = mask->value;
612
613         /* mask off bits that failed Z test */
614         zselectmask = LLVMBuildAnd(builder, zselectmask, z_pass, "");
615
616         /* mask off bits that failed stencil test */
617         if (s_pass_mask) {
618            zselectmask = LLVMBuildAnd(builder, zselectmask, s_pass_mask, "");
619         }
620
621         /* if combined Z/stencil format, mask off the stencil bits */
622         if (z_bitmask) {
623            zselectmask = LLVMBuildAnd(builder, zselectmask, z_bitmask, "");
624         }
625
626         /* Mix the old and new Z buffer values.
627          * z_dst[i] = zselectmask[i] ? z_src[i] : z_dst[i]
628          */
629         z_dst = lp_build_select(&bld, zselectmask, z_src, z_dst);
630      }
631
632      if (stencil[0].enabled) {
633         /* update stencil buffer values according to z pass/fail result */
634         LLVMValueRef z_fail_mask, z_pass_mask;
635
636         /* apply Z-fail operator */
637         z_fail_mask = lp_build_andc(&bld, orig_mask, z_pass);
638         stencil_vals = lp_build_stencil_op(&sbld, stencil, Z_FAIL_OP,
639                                            stencil_refs, stencil_vals,
640                                            z_fail_mask, face);
641
642         /* apply Z-pass operator */
643         z_pass_mask = LLVMBuildAnd(bld.builder, orig_mask, z_pass, "");
644         stencil_vals = lp_build_stencil_op(&sbld, stencil, Z_PASS_OP,
645                                            stencil_refs, stencil_vals,
646                                            z_pass_mask, face);
647      }
648   }
649   else {
650      /* No depth test: apply Z-pass operator to stencil buffer values which
651       * passed the stencil test.
652       */
653      s_pass_mask = LLVMBuildAnd(bld.builder, orig_mask, s_pass_mask, "");
654      stencil_vals = lp_build_stencil_op(&sbld, stencil, Z_PASS_OP,
655                                         stencil_refs, stencil_vals,
656                                         s_pass_mask, face);
657   }
658
659   /* The Z bits are already in the right place but we may need to shift the
660    * stencil bits before ORing Z with Stencil to make the final pixel value.
661    */
662   if (stencil_vals && stencil_shift)
663      stencil_vals = LLVMBuildShl(bld.builder, stencil_vals,
664                                  stencil_shift, "");
665
666   /* Finally, merge/store the z/stencil values */
667   if ((depth->enabled && depth->writemask) ||
668       (stencil[0].enabled && stencil[0].writemask)) {
669
670      if (z_dst && stencil_vals)
671         zs_dst = LLVMBuildOr(bld.builder, z_dst, stencil_vals, "");
672      else if (z_dst)
673         zs_dst = z_dst;
674      else
675         zs_dst = stencil_vals;
676
677      LLVMBuildStore(builder, zs_dst, zs_dst_ptr);
678   }
679
680   if (s_pass_mask)
681      lp_build_mask_update(mask, s_pass_mask);
682
683   if (depth->enabled && stencil[0].enabled)
684      lp_build_mask_update(mask, z_pass);
685}
686