Merge branch 'mesa_7_6_branch'
[mesa.git] / src / gallium / drivers / llvmpipe / lp_bld_format_soa.c
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 #include "util/u_format.h"
30
31 #include "lp_bld_type.h"
32 #include "lp_bld_const.h"
33 #include "lp_bld_conv.h"
34 #include "lp_bld_format.h"
35
36
37 /**
38 * Gather elements from scatter positions in memory into a single vector.
39 *
40 * @param src_width src element width
41 * @param dst_width result element width (source will be expanded to fit)
42 * @param length length of the offsets,
43 * @param base_ptr base pointer, should be a i8 pointer type.
44 * @param offsets vector with offsets
45 */
46 LLVMValueRef
47 lp_build_gather(LLVMBuilderRef builder,
48 unsigned length,
49 unsigned src_width,
50 unsigned dst_width,
51 LLVMValueRef base_ptr,
52 LLVMValueRef offsets)
53 {
54 LLVMTypeRef src_type = LLVMIntType(src_width);
55 LLVMTypeRef src_ptr_type = LLVMPointerType(src_type, 0);
56 LLVMTypeRef dst_elem_type = LLVMIntType(dst_width);
57 LLVMTypeRef dst_vec_type = LLVMVectorType(dst_elem_type, length);
58 LLVMValueRef res;
59 unsigned i;
60
61 res = LLVMGetUndef(dst_vec_type);
62 for(i = 0; i < length; ++i) {
63 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
64 LLVMValueRef elem_offset;
65 LLVMValueRef elem_ptr;
66 LLVMValueRef elem;
67
68 elem_offset = LLVMBuildExtractElement(builder, offsets, index, "");
69 elem_ptr = LLVMBuildGEP(builder, base_ptr, &elem_offset, 1, "");
70 elem_ptr = LLVMBuildBitCast(builder, elem_ptr, src_ptr_type, "");
71 elem = LLVMBuildLoad(builder, elem_ptr, "");
72
73 assert(src_width <= dst_width);
74 if(src_width > dst_width)
75 elem = LLVMBuildTrunc(builder, elem, dst_elem_type, "");
76 if(src_width < dst_width)
77 elem = LLVMBuildZExt(builder, elem, dst_elem_type, "");
78
79 res = LLVMBuildInsertElement(builder, res, elem, index, "");
80 }
81
82 return res;
83 }
84
85
86 static LLVMValueRef
87 lp_build_format_swizzle(struct lp_type type,
88 const LLVMValueRef *inputs,
89 enum util_format_swizzle swizzle)
90 {
91 switch (swizzle) {
92 case UTIL_FORMAT_SWIZZLE_X:
93 case UTIL_FORMAT_SWIZZLE_Y:
94 case UTIL_FORMAT_SWIZZLE_Z:
95 case UTIL_FORMAT_SWIZZLE_W:
96 return inputs[swizzle];
97 case UTIL_FORMAT_SWIZZLE_0:
98 return lp_build_zero(type);
99 case UTIL_FORMAT_SWIZZLE_1:
100 return lp_build_one(type);
101 case UTIL_FORMAT_SWIZZLE_NONE:
102 return lp_build_undef(type);
103 default:
104 assert(0);
105 return lp_build_undef(type);
106 }
107 }
108
109
110 void
111 lp_build_unpack_rgba_soa(LLVMBuilderRef builder,
112 const struct util_format_description *format_desc,
113 struct lp_type type,
114 LLVMValueRef packed,
115 LLVMValueRef *rgba)
116 {
117 LLVMValueRef inputs[4];
118 unsigned start;
119 unsigned chan;
120
121 /* FIXME: Support more formats */
122 assert(format_desc->layout == UTIL_FORMAT_LAYOUT_ARITH);
123 assert(format_desc->block.width == 1);
124 assert(format_desc->block.height == 1);
125 assert(format_desc->block.bits <= 32);
126
127 /* Decode the input vector components */
128 start = 0;
129 for (chan = 0; chan < 4; ++chan) {
130 unsigned width = format_desc->channel[chan].size;
131 unsigned stop = start + width;
132 LLVMValueRef input;
133
134 input = packed;
135
136 switch(format_desc->channel[chan].type) {
137 case UTIL_FORMAT_TYPE_VOID:
138 input = NULL;
139 break;
140
141 case UTIL_FORMAT_TYPE_UNSIGNED:
142 if(type.floating) {
143 if(start)
144 input = LLVMBuildLShr(builder, input, lp_build_int_const_scalar(type, start), "");
145 if(stop < format_desc->block.bits) {
146 unsigned mask = ((unsigned long long)1 << width) - 1;
147 input = LLVMBuildAnd(builder, input, lp_build_int_const_scalar(type, mask), "");
148 }
149
150 if(format_desc->channel[chan].normalized)
151 input = lp_build_unsigned_norm_to_float(builder, width, type, input);
152 else
153 input = LLVMBuildFPToSI(builder, input, lp_build_vec_type(type), "");
154 }
155 else {
156 /* FIXME */
157 assert(0);
158 input = lp_build_undef(type);
159 }
160 break;
161
162 default:
163 /* fall through */
164 input = lp_build_undef(type);
165 break;
166 }
167
168 inputs[chan] = input;
169
170 start = stop;
171 }
172
173 if(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
174 enum util_format_swizzle swizzle = format_desc->swizzle[0];
175 LLVMValueRef depth = lp_build_format_swizzle(type, inputs, swizzle);
176 rgba[2] = rgba[1] = rgba[0] = depth;
177 rgba[3] = lp_build_one(type);
178 }
179 else {
180 for (chan = 0; chan < 4; ++chan) {
181 enum util_format_swizzle swizzle = format_desc->swizzle[chan];
182 rgba[chan] = lp_build_format_swizzle(type, inputs, swizzle);
183 }
184 }
185 }
186
187
188 void
189 lp_build_load_rgba_soa(LLVMBuilderRef builder,
190 const struct util_format_description *format_desc,
191 struct lp_type type,
192 LLVMValueRef base_ptr,
193 LLVMValueRef offsets,
194 LLVMValueRef *rgba)
195 {
196 LLVMValueRef packed;
197
198 assert(format_desc->layout == UTIL_FORMAT_LAYOUT_ARITH);
199 assert(format_desc->block.width == 1);
200 assert(format_desc->block.height == 1);
201 assert(format_desc->block.bits <= 32);
202
203 packed = lp_build_gather(builder,
204 type.length, format_desc->block.bits, type.width,
205 base_ptr, offsets);
206
207 lp_build_unpack_rgba_soa(builder, format_desc, type, packed, rgba);
208 }