Merge branch '7.8'
[mesa.git] / src / gallium / auxiliary / gallivm / 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 static LLVMValueRef
38 lp_build_format_swizzle_chan_soa(struct lp_type type,
39 const LLVMValueRef *unswizzled,
40 enum util_format_swizzle swizzle)
41 {
42 switch (swizzle) {
43 case UTIL_FORMAT_SWIZZLE_X:
44 case UTIL_FORMAT_SWIZZLE_Y:
45 case UTIL_FORMAT_SWIZZLE_Z:
46 case UTIL_FORMAT_SWIZZLE_W:
47 return unswizzled[swizzle];
48 case UTIL_FORMAT_SWIZZLE_0:
49 return lp_build_zero(type);
50 case UTIL_FORMAT_SWIZZLE_1:
51 return lp_build_one(type);
52 case UTIL_FORMAT_SWIZZLE_NONE:
53 return lp_build_undef(type);
54 default:
55 assert(0);
56 return lp_build_undef(type);
57 }
58 }
59
60
61 void
62 lp_build_format_swizzle_soa(const struct util_format_description *format_desc,
63 struct lp_type type,
64 const LLVMValueRef *unswizzled,
65 LLVMValueRef *swizzled)
66 {
67 if(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
68 enum util_format_swizzle swizzle = format_desc->swizzle[0];
69 LLVMValueRef depth = lp_build_format_swizzle_chan_soa(type, unswizzled, swizzle);
70 swizzled[2] = swizzled[1] = swizzled[0] = depth;
71 swizzled[3] = lp_build_one(type);
72 }
73 else {
74 unsigned chan;
75 for (chan = 0; chan < 4; ++chan) {
76 enum util_format_swizzle swizzle = format_desc->swizzle[chan];
77 swizzled[chan] = lp_build_format_swizzle_chan_soa(type, unswizzled, swizzle);
78 }
79 }
80 }
81
82
83 /**
84 * Unpack several pixels in SoA.
85 *
86 * It takes a vector of packed pixels:
87 *
88 * packed = {P0, P1, P2, P3, ..., Pn}
89 *
90 * And will produce four vectors:
91 *
92 * red = {R0, R1, R2, R3, ..., Rn}
93 * green = {G0, G1, G2, G3, ..., Gn}
94 * blue = {B0, B1, B2, B3, ..., Bn}
95 * alpha = {A0, A1, A2, A3, ..., An}
96 *
97 * It requires that a packed pixel fits into an element of the output
98 * channels. The common case is when converting pixel with a depth of 32 bit or
99 * less into floats.
100 */
101 void
102 lp_build_unpack_rgba_soa(LLVMBuilderRef builder,
103 const struct util_format_description *format_desc,
104 struct lp_type type,
105 LLVMValueRef packed,
106 LLVMValueRef *rgba)
107 {
108 LLVMValueRef inputs[4];
109 unsigned start;
110 unsigned chan;
111
112 /* FIXME: Support more pixel formats */
113 assert(format_desc->block.width == 1);
114 assert(format_desc->block.height == 1);
115 assert(format_desc->block.bits <= type.width);
116 /* FIXME: Support more output types */
117 assert(type.floating);
118 assert(type.width == 32);
119
120 /* Decode the input vector components */
121 start = 0;
122 for (chan = 0; chan < format_desc->nr_channels; ++chan) {
123 unsigned width = format_desc->channel[chan].size;
124 unsigned stop = start + width;
125 LLVMValueRef input;
126
127 input = packed;
128
129 switch(format_desc->channel[chan].type) {
130 case UTIL_FORMAT_TYPE_VOID:
131 input = lp_build_undef(type);
132 break;
133
134 case UTIL_FORMAT_TYPE_UNSIGNED:
135 /*
136 * Align the LSB
137 */
138
139 if (start) {
140 input = LLVMBuildLShr(builder, input, lp_build_const_int_vec(type, start), "");
141 }
142
143 /*
144 * Zero the MSBs
145 */
146
147 if (stop < format_desc->block.bits) {
148 unsigned mask = ((unsigned long long)1 << width) - 1;
149 input = LLVMBuildAnd(builder, input, lp_build_const_int_vec(type, mask), "");
150 }
151
152 /*
153 * Type conversion
154 */
155
156 if (type.floating) {
157 if(format_desc->channel[chan].normalized)
158 input = lp_build_unsigned_norm_to_float(builder, width, type, input);
159 else
160 input = LLVMBuildSIToFP(builder, input, lp_build_vec_type(type), "");
161 }
162 else {
163 /* FIXME */
164 assert(0);
165 input = lp_build_undef(type);
166 }
167
168 break;
169
170 case UTIL_FORMAT_TYPE_SIGNED:
171 /*
172 * Align the sign bit first.
173 */
174
175 if (stop < type.width) {
176 unsigned bits = type.width - stop;
177 LLVMValueRef bits_val = lp_build_const_int_vec(type, bits);
178 input = LLVMBuildShl(builder, input, bits_val, "");
179 }
180
181 /*
182 * Align the LSB (with an arithmetic shift to preserve the sign)
183 */
184
185 if (format_desc->channel[chan].size < type.width) {
186 unsigned bits = type.width - format_desc->channel[chan].size;
187 LLVMValueRef bits_val = lp_build_const_int_vec(type, bits);
188 input = LLVMBuildAShr(builder, input, bits_val, "");
189 }
190
191 /*
192 * Type conversion
193 */
194
195 if (type.floating) {
196 input = LLVMBuildSIToFP(builder, input, lp_build_vec_type(type), "");
197 if (format_desc->channel[chan].normalized) {
198 double scale = 1.0 / ((1 << (format_desc->channel[chan].size - 1)) - 1);
199 LLVMValueRef scale_val = lp_build_const_vec(type, scale);
200 input = LLVMBuildMul(builder, input, scale_val, "");
201 }
202 }
203 else {
204 /* FIXME */
205 assert(0);
206 input = lp_build_undef(type);
207 }
208
209 break;
210
211 case UTIL_FORMAT_TYPE_FLOAT:
212 if (type.floating) {
213 assert(start == 0);
214 assert(stop == 32);
215 assert(type.width == 32);
216 input = LLVMBuildBitCast(builder, input, lp_build_vec_type(type), "");
217 }
218 else {
219 /* FIXME */
220 assert(0);
221 input = lp_build_undef(type);
222 }
223 break;
224
225 case UTIL_FORMAT_TYPE_FIXED:
226 assert(0);
227 input = lp_build_undef(type);
228 break;
229
230 default:
231 assert(0);
232 input = lp_build_undef(type);
233 break;
234 }
235
236 inputs[chan] = input;
237
238 start = stop;
239 }
240
241 lp_build_format_swizzle_soa(format_desc, type, inputs, rgba);
242 }