blob: d438c0e63d7d43695775d89ed9ca137c8cf4d9db [file] [log] [blame]
José Fonseca343ccc82009-08-21 07:43:49 +01001/**************************************************************************
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/**
José Fonseca5811ed82009-08-22 22:26:55 +010029 * @file
José Fonseca343ccc82009-08-21 07:43:49 +010030 * Depth/stencil testing to LLVM IR translation.
31 *
José Fonseca5811ed82009-08-22 22:26:55 +010032 * 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 * FIXME: Code generate stencil test
56 *
José Fonseca343ccc82009-08-21 07:43:49 +010057 * @author Jose Fonseca <jfonseca@vmware.com>
58 */
59
60#include "pipe/p_state.h"
61#include "util/u_format.h"
62
63#include "lp_bld_type.h"
64#include "lp_bld_const.h"
65#include "lp_bld_logic.h"
José Fonseca3d7a8862009-08-21 13:49:10 +010066#include "lp_bld_flow.h"
José Fonseca343ccc82009-08-21 07:43:49 +010067#include "lp_bld_debug.h"
68#include "lp_bld_depth.h"
69
70
José Fonseca5811ed82009-08-22 22:26:55 +010071/**
72 * Return a type appropriate for depth/stencil testing.
73 */
José Fonsecab4835ea2009-09-14 11:05:06 +010074struct lp_type
José Fonseca343ccc82009-08-21 07:43:49 +010075lp_depth_type(const struct util_format_description *format_desc,
76 unsigned length)
77{
José Fonsecab4835ea2009-09-14 11:05:06 +010078 struct lp_type type;
José Fonseca343ccc82009-08-21 07:43:49 +010079 unsigned swizzle;
80
81 assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
82 assert(format_desc->block.width == 1);
83 assert(format_desc->block.height == 1);
84
85 swizzle = format_desc->swizzle[0];
86 assert(swizzle < 4);
87
José Fonsecab4835ea2009-09-14 11:05:06 +010088 memset(&type, 0, sizeof type);
José Fonseca343ccc82009-08-21 07:43:49 +010089 type.width = format_desc->block.bits;
90
91 if(format_desc->channel[swizzle].type == UTIL_FORMAT_TYPE_FLOAT) {
92 type.floating = TRUE;
José Fonseca52df5322009-11-10 16:55:44 -080093 assert(swizzle == 0);
José Fonseca343ccc82009-08-21 07:43:49 +010094 assert(format_desc->channel[swizzle].size == format_desc->block.bits);
95 }
96 else if(format_desc->channel[swizzle].type == UTIL_FORMAT_TYPE_UNSIGNED) {
97 assert(format_desc->block.bits <= 32);
98 if(format_desc->channel[swizzle].normalized)
99 type.norm = TRUE;
100 }
101 else
102 assert(0);
103
104 assert(type.width <= length);
105 type.length = length / type.width;
106
107 return type;
108}
109
110
José Fonseca5811ed82009-08-22 22:26:55 +0100111/**
112 * Depth test.
113 */
José Fonseca343ccc82009-08-21 07:43:49 +0100114void
115lp_build_depth_test(LLVMBuilderRef builder,
116 const struct pipe_depth_state *state,
José Fonsecab4835ea2009-09-14 11:05:06 +0100117 struct lp_type type,
José Fonseca343ccc82009-08-21 07:43:49 +0100118 const struct util_format_description *format_desc,
José Fonseca3d7a8862009-08-21 13:49:10 +0100119 struct lp_build_mask_context *mask,
José Fonseca343ccc82009-08-21 07:43:49 +0100120 LLVMValueRef src,
121 LLVMValueRef dst_ptr)
122{
123 struct lp_build_context bld;
124 unsigned z_swizzle;
125 LLVMValueRef dst;
126 LLVMValueRef z_bitmask = NULL;
127 LLVMValueRef test;
128
129 if(!state->enabled)
130 return;
131
132 assert(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS);
133 assert(format_desc->block.width == 1);
134 assert(format_desc->block.height == 1);
135
136 z_swizzle = format_desc->swizzle[0];
137 if(z_swizzle == UTIL_FORMAT_SWIZZLE_NONE)
138 return;
139
140 /* Sanity checking */
141 assert(z_swizzle < 4);
142 assert(format_desc->block.bits == type.width);
143 if(type.floating) {
144 assert(z_swizzle == 0);
145 assert(format_desc->channel[z_swizzle].type == UTIL_FORMAT_TYPE_FLOAT);
146 assert(format_desc->channel[z_swizzle].size == format_desc->block.bits);
147 }
148 else {
149 assert(format_desc->channel[z_swizzle].type == UTIL_FORMAT_TYPE_UNSIGNED);
150 assert(format_desc->channel[z_swizzle].normalized);
151 assert(!type.fixed);
152 assert(!type.sign);
153 assert(type.norm);
154 }
155
156 /* Setup build context */
157 lp_build_context_init(&bld, builder, type);
158
159 dst = LLVMBuildLoad(builder, dst_ptr, "");
160
161 lp_build_name(dst, "zsbuf");
162
163 /* Align the source depth bits with the destination's, and mask out any
164 * stencil or padding bits from both */
165 if(format_desc->channel[z_swizzle].size == format_desc->block.bits) {
166 assert(z_swizzle == 0);
167 /* nothing to do */
168 }
169 else {
170 unsigned padding_left;
171 unsigned padding_right;
172 unsigned chan;
173
174 assert(format_desc->layout == UTIL_FORMAT_LAYOUT_ARITH);
175 assert(format_desc->channel[z_swizzle].type == UTIL_FORMAT_TYPE_UNSIGNED);
176 assert(format_desc->channel[z_swizzle].size <= format_desc->block.bits);
177 assert(format_desc->channel[z_swizzle].normalized);
178
179 padding_right = 0;
180 for(chan = 0; chan < z_swizzle; ++chan)
181 padding_right += format_desc->channel[chan].size;
José Fonsecaabc160b2009-09-09 21:17:20 +0100182 padding_left = format_desc->block.bits -
183 (padding_right + format_desc->channel[z_swizzle].size);
José Fonseca343ccc82009-08-21 07:43:49 +0100184
185 if(padding_left || padding_right) {
José Fonsecaabc160b2009-09-09 21:17:20 +0100186 const unsigned long long mask_left = ((unsigned long long)1 << (format_desc->block.bits - padding_left)) - 1;
187 const unsigned long long mask_right = ((unsigned long long)1 << (padding_right)) - 1;
188 z_bitmask = lp_build_int_const_scalar(type, mask_left ^ mask_right);
José Fonseca343ccc82009-08-21 07:43:49 +0100189 }
190
191 if(padding_left)
José Fonseca77b35dc2009-08-22 22:30:03 +0100192 src = LLVMBuildLShr(builder, src, lp_build_int_const_scalar(type, padding_left), "");
José Fonseca343ccc82009-08-21 07:43:49 +0100193 if(padding_right)
194 src = LLVMBuildAnd(builder, src, z_bitmask, "");
195 if(padding_left || padding_right)
196 dst = LLVMBuildAnd(builder, dst, z_bitmask, "");
197 }
198
199 lp_build_name(dst, "zsbuf.z");
200
201 test = lp_build_cmp(&bld, state->func, src, dst);
José Fonseca3d7a8862009-08-21 13:49:10 +0100202 lp_build_mask_update(mask, test);
José Fonseca343ccc82009-08-21 07:43:49 +0100203
204 if(state->writemask) {
205 if(z_bitmask)
José Fonseca3d7a8862009-08-21 13:49:10 +0100206 z_bitmask = LLVMBuildAnd(builder, mask->value, z_bitmask, "");
José Fonseca343ccc82009-08-21 07:43:49 +0100207 else
José Fonseca3d7a8862009-08-21 13:49:10 +0100208 z_bitmask = mask->value;
José Fonseca343ccc82009-08-21 07:43:49 +0100209
210 dst = lp_build_select(&bld, z_bitmask, src, dst);
211 LLVMBuildStore(builder, dst, dst_ptr);
212 }
José Fonseca343ccc82009-08-21 07:43:49 +0100213}