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