draw: corrections to allow for different cliptest cases
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_swizzle.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 * @file
30 * Helper functions for swizzling/shuffling.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35
36 #include "util/u_debug.h"
37
38 #include "lp_bld_type.h"
39 #include "lp_bld_const.h"
40 #include "lp_bld_logic.h"
41 #include "lp_bld_swizzle.h"
42
43
44 LLVMValueRef
45 lp_build_broadcast(LLVMBuilderRef builder,
46 LLVMTypeRef vec_type,
47 LLVMValueRef scalar)
48 {
49 const unsigned n = LLVMGetVectorSize(vec_type);
50 LLVMValueRef res;
51 unsigned i;
52
53 res = LLVMGetUndef(vec_type);
54 for(i = 0; i < n; ++i) {
55 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
56 res = LLVMBuildInsertElement(builder, res, scalar, index, "");
57 }
58
59 return res;
60 }
61
62
63 /**
64 * Broadcast
65 */
66 LLVMValueRef
67 lp_build_broadcast_scalar(struct lp_build_context *bld,
68 LLVMValueRef scalar)
69 {
70 const struct lp_type type = bld->type;
71
72 assert(lp_check_elem_type(type, LLVMTypeOf(scalar)));
73
74 if (type.length == 1) {
75 return scalar;
76 }
77 else {
78 LLVMValueRef res;
79
80 #if HAVE_LLVM >= 0x207
81 /* The shuffle vector is always made of int32 elements */
82 struct lp_type i32_vec_type = lp_type_int_vec(32);
83 i32_vec_type.length = type.length;
84
85 res = LLVMBuildInsertElement(bld->builder, bld->undef, scalar,
86 LLVMConstInt(LLVMInt32Type(), 0, 0), "");
87 res = LLVMBuildShuffleVector(bld->builder, res, bld->undef,
88 lp_build_const_int_vec(i32_vec_type, 0), "");
89 #else
90 /* XXX: The above path provokes a bug in LLVM 2.6 */
91 unsigned i;
92 res = bld->undef;
93 for(i = 0; i < type.length; ++i) {
94 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
95 res = LLVMBuildInsertElement(bld->builder, res, scalar, index, "");
96 }
97 #endif
98 return res;
99 }
100 }
101
102
103 /**
104 * Swizzle one channel into all other three channels.
105 */
106 LLVMValueRef
107 lp_build_swizzle_scalar_aos(struct lp_build_context *bld,
108 LLVMValueRef a,
109 unsigned channel)
110 {
111 const struct lp_type type = bld->type;
112 const unsigned n = type.length;
113 unsigned i, j;
114
115 if(a == bld->undef || a == bld->zero || a == bld->one)
116 return a;
117
118 /* XXX: SSE3 has PSHUFB which should be better than bitmasks, but forcing
119 * using shuffles here actually causes worst results. More investigation is
120 * needed. */
121 if (type.width >= 16) {
122 /*
123 * Shuffle.
124 */
125 LLVMTypeRef elem_type = LLVMInt32Type();
126 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
127
128 for(j = 0; j < n; j += 4)
129 for(i = 0; i < 4; ++i)
130 shuffles[j + i] = LLVMConstInt(elem_type, j + channel, 0);
131
132 return LLVMBuildShuffleVector(bld->builder, a, bld->undef, LLVMConstVector(shuffles, n), "");
133 }
134 else {
135 /*
136 * Bit mask and recursive shifts
137 *
138 * XYZW XYZW .... XYZW <= input
139 * 0Y00 0Y00 .... 0Y00
140 * YY00 YY00 .... YY00
141 * YYYY YYYY .... YYYY <= output
142 */
143 struct lp_type type4;
144 const char shifts[4][2] = {
145 { 1, 2},
146 {-1, 2},
147 { 1, -2},
148 {-1, -2}
149 };
150 unsigned i;
151
152 a = LLVMBuildAnd(bld->builder, a,
153 lp_build_const_mask_aos(type, 1 << channel), "");
154
155 /*
156 * Build a type where each element is an integer that cover the four
157 * channels.
158 */
159
160 type4 = type;
161 type4.floating = FALSE;
162 type4.width *= 4;
163 type4.length /= 4;
164
165 a = LLVMBuildBitCast(bld->builder, a, lp_build_vec_type(type4), "");
166
167 for(i = 0; i < 2; ++i) {
168 LLVMValueRef tmp = NULL;
169 int shift = shifts[channel][i];
170
171 #ifdef PIPE_ARCH_LITTLE_ENDIAN
172 shift = -shift;
173 #endif
174
175 if(shift > 0)
176 tmp = LLVMBuildLShr(bld->builder, a, lp_build_const_int_vec(type4, shift*type.width), "");
177 if(shift < 0)
178 tmp = LLVMBuildShl(bld->builder, a, lp_build_const_int_vec(type4, -shift*type.width), "");
179
180 assert(tmp);
181 if(tmp)
182 a = LLVMBuildOr(bld->builder, a, tmp, "");
183 }
184
185 return LLVMBuildBitCast(bld->builder, a, lp_build_vec_type(type), "");
186 }
187 }
188
189
190 LLVMValueRef
191 lp_build_swizzle_aos(struct lp_build_context *bld,
192 LLVMValueRef a,
193 const unsigned char swizzles[4])
194 {
195 const struct lp_type type = bld->type;
196 const unsigned n = type.length;
197 unsigned i, j;
198
199 if (swizzles[0] == PIPE_SWIZZLE_RED &&
200 swizzles[1] == PIPE_SWIZZLE_GREEN &&
201 swizzles[2] == PIPE_SWIZZLE_BLUE &&
202 swizzles[3] == PIPE_SWIZZLE_ALPHA) {
203 return a;
204 }
205
206 if (swizzles[0] == swizzles[1] &&
207 swizzles[1] == swizzles[2] &&
208 swizzles[2] == swizzles[3]) {
209 switch (swizzles[0]) {
210 case PIPE_SWIZZLE_RED:
211 case PIPE_SWIZZLE_GREEN:
212 case PIPE_SWIZZLE_BLUE:
213 case PIPE_SWIZZLE_ALPHA:
214 return lp_build_swizzle_scalar_aos(bld, a, swizzles[0]);
215 case PIPE_SWIZZLE_ZERO:
216 return bld->zero;
217 case PIPE_SWIZZLE_ONE:
218 return bld->one;
219 default:
220 assert(0);
221 return bld->undef;
222 }
223 }
224
225 if (type.width >= 16) {
226 /*
227 * Shuffle.
228 */
229 LLVMValueRef undef = LLVMGetUndef(lp_build_elem_type(type));
230 LLVMTypeRef i32t = LLVMInt32Type();
231 LLVMValueRef shuffles[LP_MAX_VECTOR_LENGTH];
232 LLVMValueRef aux[LP_MAX_VECTOR_LENGTH];
233
234 memset(aux, 0, sizeof aux);
235
236 for(j = 0; j < n; j += 4) {
237 for(i = 0; i < 4; ++i) {
238 unsigned shuffle;
239 switch (swizzles[i]) {
240 default:
241 assert(0);
242 /* fall through */
243 case PIPE_SWIZZLE_RED:
244 case PIPE_SWIZZLE_GREEN:
245 case PIPE_SWIZZLE_BLUE:
246 case PIPE_SWIZZLE_ALPHA:
247 shuffle = j + swizzles[i];
248 break;
249 case PIPE_SWIZZLE_ZERO:
250 shuffle = type.length + 0;
251 if (!aux[0]) {
252 aux[0] = lp_build_const_elem(type, 0.0);
253 }
254 break;
255 case PIPE_SWIZZLE_ONE:
256 shuffle = type.length + 1;
257 if (!aux[1]) {
258 aux[1] = lp_build_const_elem(type, 1.0);
259 }
260 break;
261 }
262 shuffles[j + i] = LLVMConstInt(i32t, shuffle, 0);
263 }
264 }
265
266 for (i = 0; i < n; ++i) {
267 if (!aux[i]) {
268 aux[i] = undef;
269 }
270 }
271
272 return LLVMBuildShuffleVector(bld->builder, a,
273 LLVMConstVector(aux, n),
274 LLVMConstVector(shuffles, n), "");
275 } else {
276 /*
277 * Bit mask and shifts.
278 *
279 * For example, this will convert BGRA to RGBA by doing
280 *
281 * rgba = (bgra & 0x00ff0000) >> 16
282 * | (bgra & 0xff00ff00)
283 * | (bgra & 0x000000ff) << 16
284 *
285 * This is necessary not only for faster cause, but because X86 backend
286 * will refuse shuffles of <4 x i8> vectors
287 */
288 LLVMValueRef res;
289 struct lp_type type4;
290 unsigned cond = 0;
291 unsigned chan;
292 int shift;
293
294 /*
295 * Start with a mixture of 1 and 0.
296 */
297 for (chan = 0; chan < 4; ++chan) {
298 if (swizzles[chan] == PIPE_SWIZZLE_ONE) {
299 cond |= 1 << chan;
300 }
301 }
302 res = lp_build_select_aos(bld, cond, bld->one, bld->zero);
303
304 /*
305 * Build a type where each element is an integer that cover the four
306 * channels.
307 */
308 type4 = type;
309 type4.floating = FALSE;
310 type4.width *= 4;
311 type4.length /= 4;
312
313 a = LLVMBuildBitCast(bld->builder, a, lp_build_vec_type(type4), "");
314 res = LLVMBuildBitCast(bld->builder, res, lp_build_vec_type(type4), "");
315
316 /*
317 * Mask and shift the channels, trying to group as many channels in the
318 * same shift as possible
319 */
320 for (shift = -3; shift <= 3; ++shift) {
321 unsigned long long mask = 0;
322
323 assert(type4.width <= sizeof(mask)*8);
324
325 for (chan = 0; chan < 4; ++chan) {
326 /* FIXME: big endian */
327 if (swizzles[chan] < 4 &&
328 chan - swizzles[chan] == shift) {
329 mask |= ((1ULL << type.width) - 1) << (swizzles[chan] * type.width);
330 }
331 }
332
333 if (mask) {
334 LLVMValueRef masked;
335 LLVMValueRef shifted;
336
337 if (0)
338 debug_printf("shift = %i, mask = 0x%08llx\n", shift, mask);
339
340 masked = LLVMBuildAnd(bld->builder, a,
341 lp_build_const_int_vec(type4, mask), "");
342 if (shift > 0) {
343 shifted = LLVMBuildShl(bld->builder, masked,
344 lp_build_const_int_vec(type4, shift*type.width), "");
345 } else if (shift < 0) {
346 shifted = LLVMBuildLShr(bld->builder, masked,
347 lp_build_const_int_vec(type4, -shift*type.width), "");
348 } else {
349 shifted = masked;
350 }
351
352 res = LLVMBuildOr(bld->builder, res, shifted, "");
353 }
354 }
355
356 return LLVMBuildBitCast(bld->builder, res, lp_build_vec_type(type), "");
357 }
358 }
359
360
361 /**
362 * Extended swizzle of a single channel of a SoA vector.
363 *
364 * @param bld building context
365 * @param unswizzled array with the 4 unswizzled values
366 * @param swizzle one of the PIPE_SWIZZLE_*
367 *
368 * @return the swizzled value.
369 */
370 LLVMValueRef
371 lp_build_swizzle_soa_channel(struct lp_build_context *bld,
372 const LLVMValueRef *unswizzled,
373 unsigned swizzle)
374 {
375 switch (swizzle) {
376 case PIPE_SWIZZLE_RED:
377 case PIPE_SWIZZLE_GREEN:
378 case PIPE_SWIZZLE_BLUE:
379 case PIPE_SWIZZLE_ALPHA:
380 return unswizzled[swizzle];
381 case PIPE_SWIZZLE_ZERO:
382 return bld->zero;
383 case PIPE_SWIZZLE_ONE:
384 return bld->one;
385 default:
386 assert(0);
387 return bld->undef;
388 }
389 }
390
391
392 /**
393 * Extended swizzle of a SoA vector.
394 *
395 * @param bld building context
396 * @param unswizzled array with the 4 unswizzled values
397 * @param swizzles array of PIPE_SWIZZLE_*
398 * @param swizzled output swizzled values
399 */
400 void
401 lp_build_swizzle_soa(struct lp_build_context *bld,
402 const LLVMValueRef *unswizzled,
403 const unsigned char swizzles[4],
404 LLVMValueRef *swizzled)
405 {
406 unsigned chan;
407
408 for (chan = 0; chan < 4; ++chan) {
409 swizzled[chan] = lp_build_swizzle_soa_channel(bld, unswizzled,
410 swizzles[chan]);
411 }
412 }
413
414
415 /**
416 * Do an extended swizzle of a SoA vector inplace.
417 *
418 * @param bld building context
419 * @param values intput/output array with the 4 values
420 * @param swizzles array of PIPE_SWIZZLE_*
421 */
422 void
423 lp_build_swizzle_soa_inplace(struct lp_build_context *bld,
424 LLVMValueRef *values,
425 const unsigned char swizzles[4])
426 {
427 LLVMValueRef unswizzled[4];
428 unsigned chan;
429
430 for (chan = 0; chan < 4; ++chan) {
431 unswizzled[chan] = values[chan];
432 }
433
434 lp_build_swizzle_soa(bld, unswizzled, swizzles, values);
435 }