lp_bld_depth.c revision 3d7a88674f9eb3320eeff511968f041426e25023
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 * Depth/stencil testing to LLVM IR translation.
30 *
31 * @author Jose Fonseca <jfonseca@vmware.com>
32 */
33
34#include "pipe/p_state.h"
35#include "util/u_format.h"
36
37#include "lp_bld_type.h"
38#include "lp_bld_const.h"
39#include "lp_bld_logic.h"
40#include "lp_bld_flow.h"
41#include "lp_bld_debug.h"
42#include "lp_bld_depth.h"
43
44
45union lp_type
46lp_depth_type(const struct util_format_description *format_desc,
47              unsigned length)
48{
49   union lp_type type;
50   unsigned swizzle;
51
52   assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
53   assert(format_desc->block.width == 1);
54   assert(format_desc->block.height == 1);
55
56   swizzle = format_desc->swizzle[0];
57   assert(swizzle < 4);
58
59   type.value = 0;
60   type.width = format_desc->block.bits;
61
62   if(format_desc->channel[swizzle].type == UTIL_FORMAT_TYPE_FLOAT) {
63      type.floating = TRUE;
64      assert(swizzle = 0);
65      assert(format_desc->channel[swizzle].size == format_desc->block.bits);
66   }
67   else if(format_desc->channel[swizzle].type == UTIL_FORMAT_TYPE_UNSIGNED) {
68      assert(format_desc->block.bits <= 32);
69      if(format_desc->channel[swizzle].normalized)
70         type.norm = TRUE;
71   }
72   else
73      assert(0);
74
75   assert(type.width <= length);
76   type.length = length / type.width;
77
78   return type;
79}
80
81
82void
83lp_build_depth_test(LLVMBuilderRef builder,
84                    const struct pipe_depth_state *state,
85                    union lp_type type,
86                    const struct util_format_description *format_desc,
87                    struct lp_build_mask_context *mask,
88                    LLVMValueRef src,
89                    LLVMValueRef dst_ptr)
90{
91   struct lp_build_context bld;
92   unsigned z_swizzle;
93   LLVMValueRef dst;
94   LLVMValueRef z_bitmask = NULL;
95   LLVMValueRef test;
96
97   if(!state->enabled)
98      return;
99
100   assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
101   assert(format_desc->block.width == 1);
102   assert(format_desc->block.height == 1);
103
104   z_swizzle = format_desc->swizzle[0];
105   if(z_swizzle == UTIL_FORMAT_SWIZZLE_NONE)
106      return;
107
108   /* Sanity checking */
109   assert(z_swizzle < 4);
110   assert(format_desc->block.bits == type.width);
111   if(type.floating) {
112      assert(z_swizzle == 0);
113      assert(format_desc->channel[z_swizzle].type == UTIL_FORMAT_TYPE_FLOAT);
114      assert(format_desc->channel[z_swizzle].size == format_desc->block.bits);
115   }
116   else {
117      assert(format_desc->channel[z_swizzle].type == UTIL_FORMAT_TYPE_UNSIGNED);
118      assert(format_desc->channel[z_swizzle].normalized);
119      assert(!type.fixed);
120      assert(!type.sign);
121      assert(type.norm);
122   }
123
124   /* Setup build context */
125   lp_build_context_init(&bld, builder, type);
126
127   dst = LLVMBuildLoad(builder, dst_ptr, "");
128
129   lp_build_name(dst, "zsbuf");
130
131   /* Align the source depth bits with the destination's, and mask out any
132    * stencil or padding bits from both */
133   if(format_desc->channel[z_swizzle].size == format_desc->block.bits) {
134      assert(z_swizzle == 0);
135      /* nothing to do */
136   }
137   else {
138      unsigned padding_left;
139      unsigned padding_right;
140      unsigned chan;
141
142      assert(format_desc->layout == UTIL_FORMAT_LAYOUT_ARITH);
143      assert(format_desc->channel[z_swizzle].type == UTIL_FORMAT_TYPE_UNSIGNED);
144      assert(format_desc->channel[z_swizzle].size <= format_desc->block.bits);
145      assert(format_desc->channel[z_swizzle].normalized);
146
147      padding_right = 0;
148      for(chan = 0; chan < z_swizzle; ++chan)
149         padding_right += format_desc->channel[chan].size;
150      padding_left = format_desc->block.bits - format_desc->channel[z_swizzle].size;
151
152      if(padding_left || padding_right) {
153         const long long mask_left = ((long long)1 << (format_desc->block.bits - padding_left)) - 1;
154         const long long mask_right = ((long long)1 << (padding_right)) - 1;
155         z_bitmask = lp_build_int_const_uni(type, mask_left & mask_right);
156      }
157
158      if(padding_left)
159         src = LLVMBuildLShr(builder, src, lp_build_int_const_uni(type, padding_left), "");
160      if(padding_right)
161         src = LLVMBuildAnd(builder, src, z_bitmask, "");
162      if(padding_left || padding_right)
163         dst = LLVMBuildAnd(builder, dst, z_bitmask, "");
164   }
165
166   lp_build_name(dst, "zsbuf.z");
167
168   test = lp_build_cmp(&bld, state->func, src, dst);
169   lp_build_mask_update(mask, test);
170
171   if(state->writemask) {
172      if(z_bitmask)
173         z_bitmask = LLVMBuildAnd(builder, mask->value, z_bitmask, "");
174      else
175         z_bitmask = mask->value;
176
177      dst = lp_build_select(&bld, z_bitmask, src, dst);
178      LLVMBuildStore(builder, dst, dst_ptr);
179   }
180
181   assert(!state->occlusion_count);
182}
183