Debian Codegraph @

src/gallium/auxiliary/gallivm/lp_bld_format_aos.c

raw symbols SCIP
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 * AoS pixel format manipulation.
31 *
32 * @author Jose Fonseca <[email protected]>
33 */
34
35
36 #include "util/format/u_format.h"
37 #include "util/bitscan.h"
38 #include "util/u_pointer.h"
39
40 #include "lp_bld_arit.h"
41 #include "lp_bld_init.h"
42 #include "lp_bld_type.h"
43 #include "lp_bld_flow.h"
44 #include "lp_bld_const.h"
45 #include "lp_bld_conv.h"
46 #include "lp_bld_swizzle.h"
47 #include "lp_bld_gather.h"
48 #include "lp_bld_debug.h"
49 #include "lp_bld_format.h"
50 #include "lp_bld_pack.h"
51 #include "lp_bld_intr.h"
52 #include "lp_bld_logic.h"
53 #include "lp_bld_bitarit.h"
54 #include "lp_bld_misc.h"
55
56 /**
57 * Basic swizzling. Rearrange the order of the unswizzled array elements
58 * according to the format description. PIPE_SWIZZLE_0/ONE are supported
59 * too.
60 * Ex: if unswizzled[4] = {B, G, R, x}, then swizzled_out[4] = {R, G, B, 1}.
61 */
62 LLVMValueRef
63 lp_build_format_swizzle_aos(const struct util_format_description *desc,
64 struct lp_build_context *bld,
65 LLVMValueRef unswizzled)
66 {
67 unsigned char swizzles[4];
68 unsigned chan;
69
70 assert(bld->type.length % 4 == 0);
71
72 for (chan = 0; chan < 4; ++chan) {
73 enum pipe_swizzle swizzle;
74
75 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
76 /*
77 * For ZS formats do RGBA = ZZZ1
78 */
79 if (chan == 3) {
80 swizzle = PIPE_SWIZZLE_1;
81 } else if (desc->swizzle[0] == PIPE_SWIZZLE_NONE) {
82 swizzle = PIPE_SWIZZLE_0;
83 } else {
84 swizzle = desc->swizzle[0];
85 }
86 } else {
87 swizzle = desc->swizzle[chan];
88 }
89 swizzles[chan] = swizzle;
90 }
91
92 return lp_build_swizzle_aos(bld, unswizzled, swizzles);
93 }
94
95
96 /**
97 * Whether the format matches the vector type, apart of swizzles.
98 */
99 static inline bool
100 format_matches_type(const struct util_format_description *desc,
101 struct lp_type type)
102 {
103 enum util_format_type chan_type;
104 unsigned chan;
105
106 assert(type.length % 4 == 0);
107
108 if (desc->layout != UTIL_FORMAT_LAYOUT_PLAIN ||
109 desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB ||
110 desc->block.width != 1 ||
111 desc->block.height != 1) {
112 return false;
113 }
114
115 if (type.floating) {
116 chan_type = UTIL_FORMAT_TYPE_FLOAT;
117 } else if (type.fixed) {
118 chan_type = UTIL_FORMAT_TYPE_FIXED;
119 } else if (type.sign) {
120 chan_type = UTIL_FORMAT_TYPE_SIGNED;
121 } else {
122 chan_type = UTIL_FORMAT_TYPE_UNSIGNED;
123 }
124
125 for (chan = 0; chan < desc->nr_channels; ++chan) {
126 if (desc->channel[chan].size != type.width) {
127 return false;
128 }
129
130 if (desc->channel[chan].type != UTIL_FORMAT_TYPE_VOID) {
131 if (desc->channel[chan].type != chan_type ||
132 desc->channel[chan].normalized != type.norm) {
133 return false;
134 }
135 }
136 }
137
138 return true;
139 }
140
141 /*
142 * Do rounding when converting small unorm values to larger ones.
143 * Not quite 100% accurate, as it's done by appending MSBs, but
144 * should be good enough.
145 */
146
147 static inline LLVMValueRef
148 scale_bits_up(struct gallivm_state *gallivm,
149 int src_bits,
150 int dst_bits,
151 LLVMValueRef src,
152 struct lp_type src_type)
153 {
154 LLVMBuilderRef builder = gallivm->builder;
155 LLVMValueRef result = src;
156
157 if (src_bits == 1 && dst_bits > 1) {
158 /*
159 * Useful for a1 - we'd need quite some repeated copies otherwise.
160 */
161 struct lp_build_context bld;
162 LLVMValueRef dst_mask;
163 lp_build_context_init(&bld, gallivm, src_type);
164 dst_mask = lp_build_const_int_vec(gallivm, src_type,
165 (1 << dst_bits) - 1),
166 result = lp_build_cmp(&bld, PIPE_FUNC_EQUAL, src,
167 lp_build_const_int_vec(gallivm, src_type, 0));
168 result = lp_build_andnot(&bld, dst_mask, result);
169 }
170 else if (dst_bits > src_bits) {
171 /* Scale up bits */
172 int db = dst_bits - src_bits;
173
174 /* Shift left by difference in bits */
175 result = LLVMBuildShl(builder,
176 src,
177 lp_build_const_int_vec(gallivm, src_type, db),
178 "");
179
180 if (db <= src_bits) {
181 /* Enough bits in src to fill the remainder */
182 LLVMValueRef lower = LLVMBuildLShr(builder,
183 src,
184 lp_build_const_int_vec(gallivm, src_type,
185 src_bits - db),
186 "");
187
188 result = LLVMBuildOr(builder, result, lower, "");
189 } else if (db > src_bits) {
190 /* Need to repeatedly copy src bits to fill remainder in dst */
191 unsigned n;
192
193 for (n = src_bits; n < dst_bits; n *= 2) {
194 LLVMValueRef shuv = lp_build_const_int_vec(gallivm, src_type, n);
195
196 result = LLVMBuildOr(builder,
197 result,
198 LLVMBuildLShr(builder, result, shuv, ""),
199 "");
200 }
201 }
202 } else {
203 assert (dst_bits == src_bits);
204 }
205
206 return result;
207 }
208
209 /**
210 * Unpack a single pixel into its XYZW components.
211 *
212 * @param desc the pixel format for the packed pixel value
213 * @param packed integer pixel in a format such as PIPE_FORMAT_B8G8R8A8_UNORM
214 *
215 * @return XYZW in a float[4] or ubyte[4] or ushort[4] vector.
216 */
217 static inline LLVMValueRef
218 lp_build_unpack_arith_rgba_aos(struct gallivm_state *gallivm,
219 const struct util_format_description *desc,
220 LLVMValueRef packed)
221 {
222 LLVMBuilderRef builder = gallivm->builder;
223 LLVMValueRef shifted, casted, scaled, masked;
224 LLVMValueRef shifts[4];
225 LLVMValueRef masks[4];
226 LLVMValueRef scales[4];
227 LLVMTypeRef vec32_type;
228
229 bool normalized;
230 bool needs_uitofp;
231 unsigned i;
232
233 /* TODO: Support more formats */
234 assert(desc->layout == UTIL_FORMAT_LAYOUT_PLAIN);
235 assert(desc->block.width == 1);
236 assert(desc->block.height == 1);
237 assert(desc->block.bits <= 32);
238
239 /* Do the intermediate integer computations with 32bit integers since it
240 * matches floating point size */
241 assert (LLVMTypeOf(packed) == LLVMInt32TypeInContext(gallivm->context));
242
243 vec32_type = LLVMVectorType(LLVMInt32TypeInContext(gallivm->context), 4);
244
245 /* Broadcast the packed value to all four channels
246 * before: packed = BGRA
247 * after: packed = {BGRA, BGRA, BGRA, BGRA}
248 */
249 packed = LLVMBuildInsertElement(builder, LLVMGetUndef(vec32_type), packed,
250 LLVMConstNull(LLVMInt32TypeInContext(gallivm->context)),
251 "");
252 packed = LLVMBuildShuffleVector(builder, packed, LLVMGetUndef(vec32_type),
253 LLVMConstNull(vec32_type),
254 "");
255
256 /* Initialize vector constants */
257 normalized = false;
258 needs_uitofp = false;
259
260 /* Loop over 4 color components */
261 for (i = 0; i < 4; ++i) {
262 unsigned bits = desc->channel[i].size;
263 unsigned shift = desc->channel[i].shift;
264
265 if (desc->channel[i].type == UTIL_FORMAT_TYPE_VOID) {
266 shifts[i] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
267 masks[i] = LLVMConstNull(LLVMInt32TypeInContext(gallivm->context));
268 scales[i] = LLVMConstNull(LLVMFloatTypeInContext(gallivm->context));
269 }
270 else {
271 unsigned long long mask = (1ULL << bits) - 1;
272
273 assert(desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED);
274
275 if (bits == 32) {
276 needs_uitofp = true;
277 }
278
279 shifts[i] = lp_build_const_int32(gallivm, shift);
280 masks[i] = lp_build_const_int32(gallivm, mask);
281
282 if (desc->channel[i].normalized) {
283 scales[i] = lp_build_const_float(gallivm, 1.0 / mask);
284 normalized = true;
285 }
286 else
287 scales[i] = lp_build_const_float(gallivm, 1.0);
288 }
289 }
290
291 /* Ex: convert packed = {XYZW, XYZW, XYZW, XYZW}
292 * into masked = {X, Y, Z, W}
293 */
294 if (desc->block.bits < 32 && normalized) {
295 /*
296 * Note: we cannot do the shift below on x86 natively until AVX2.
297 *
298 * Old llvm versions will resort to scalar extract/shift insert,
299 * which is definitely terrible, new versions will just do
300 * several vector shifts and shuffle/blend results together.
301 * We could turn this into a variable left shift plus a constant
302 * right shift, and llvm would then turn the variable left shift
303 * into a mul for us (albeit without sse41 the mul needs emulation
304 * too...). However, since we're going to do a float mul
305 * anyway, we just adjust that mul instead (plus the mask), skipping
306 * the shift completely.
307 * We could also use a extra mul when the format isn't normalized and
308 * we don't have AVX2 support, but don't bother for now. Unfortunately,
309 * this strategy doesn't work for 32bit formats (such as rgb10a2 or even
310 * rgba8 if it ends up here), as that would require UIToFP, albeit that
311 * would be fixable with easy 16bit shuffle (unless there's channels
312 * crossing 16bit boundaries).
313 */
314 for (i = 0; i < 4; ++i) {
315 if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID) {
316 unsigned bits = desc->channel[i].size;
317 unsigned shift = desc->channel[i].shift;
318 unsigned long long mask = ((1ULL << bits) - 1) << shift;
319 scales[i] = lp_build_const_float(gallivm, 1.0 / mask);
320 masks[i] = lp_build_const_int32(gallivm, mask);
321 }
322 }
323 masked = LLVMBuildAnd(builder, packed, LLVMConstVector(masks, 4), "");
324 } else {
325 shifted = LLVMBuildLShr(builder, packed, LLVMConstVector(shifts, 4), "");
326 masked = LLVMBuildAnd(builder, shifted, LLVMConstVector(masks, 4), "");
327 }
328
329 if (!needs_uitofp) {
330 /* UIToFP can't be expressed in SSE2 */
331 casted = LLVMBuildSIToFP(builder, masked, LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 4), "");
332 } else {
333 casted = LLVMBuildUIToFP(builder, masked, LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 4), "");
334 }
335
336 /*
337 * At this point 'casted' may be a vector of floats such as
338 * {255.0, 255.0, 255.0, 255.0}. (Normalized values may be multiplied
339 * by powers of two). Next, if the pixel values are normalized
340 * we'll scale this to {1.0, 1.0, 1.0, 1.0}.
341 */
342
343 if (normalized)
344 scaled = LLVMBuildFMul(builder, casted, LLVMConstVector(scales, 4), "");
345 else
346 scaled = casted;
347
348 return scaled;
349 }
350
351
352 /**
353 * Pack a single pixel.
354 *
355 * @param rgba 4 float vector with the unpacked components.
356 *
357 * XXX: This is mostly for reference and testing -- operating a single pixel at
358 * a time is rarely if ever needed.
359 */
360 LLVMValueRef
361 lp_build_pack_rgba_aos(struct gallivm_state *gallivm,
362 const struct util_format_description *desc,
363 LLVMValueRef rgba)
364 {
365 LLVMBuilderRef builder = gallivm->builder;
366 LLVMTypeRef type;
367 LLVMValueRef packed = NULL;
368 LLVMValueRef swizzles[4];
369 LLVMValueRef shifted, casted, scaled, unswizzled;
370 LLVMValueRef shifts[4];
371 LLVMValueRef scales[4];
372 bool normalized;
373 unsigned i, j;
374
375 assert(desc->layout == UTIL_FORMAT_LAYOUT_PLAIN);
376 assert(desc->block.width == 1);
377 assert(desc->block.height == 1);
378
379 type = LLVMIntTypeInContext(gallivm->context, desc->block.bits);
380
381 /* Unswizzle the color components into the source vector. */
382 for (i = 0; i < 4; ++i) {
383 for (j = 0; j < 4; ++j) {
384 if (desc->swizzle[j] == i)
385 break;
386 }
387 if (j < 4)
388 swizzles[i] = lp_build_const_int32(gallivm, j);
389 else
390 swizzles[i] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
391 }
392
393 unswizzled = LLVMBuildShuffleVector(builder, rgba,
394 LLVMGetUndef(LLVMVectorType(LLVMFloatTypeInContext(gallivm->context), 4)),
395 LLVMConstVector(swizzles, 4), "");
396
397 normalized = false;
398 for (i = 0; i < 4; ++i) {
399 unsigned bits = desc->channel[i].size;
400 unsigned shift = desc->channel[i].shift;
401
402 if (desc->channel[i].type == UTIL_FORMAT_TYPE_VOID) {
403 shifts[i] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
404 scales[i] = LLVMGetUndef(LLVMFloatTypeInContext(gallivm->context));
405 }
406 else {
407 unsigned mask = (1 << bits) - 1;
408
409 assert(desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED);
410 assert(bits < 32);
411
412 shifts[i] = lp_build_const_int32(gallivm, shift);
413
414 if (desc->channel[i].normalized) {
415 scales[i] = lp_build_const_float(gallivm, mask);
416 normalized = true;
417 }
418 else
419 scales[i] = lp_build_const_float(gallivm, 1.0);
420 }
421 }
422
423 if (normalized)
424 scaled = LLVMBuildFMul(builder, unswizzled, LLVMConstVector(scales, 4), "");
425 else
426 scaled = unswizzled;
427
428 casted = LLVMBuildFPToSI(builder, scaled, LLVMVectorType(LLVMInt32TypeInContext(gallivm->context), 4), "");
429
430 shifted = LLVMBuildShl(builder, casted, LLVMConstVector(shifts, 4), "");
431
432 /* Bitwise or all components */
433 for (i = 0; i < 4; ++i) {
434 if (desc->channel[i].type == UTIL_FORMAT_TYPE_UNSIGNED) {
435 LLVMValueRef component = LLVMBuildExtractElement(builder, shifted,
436 lp_build_const_int32(gallivm, i), "");
437 if (packed)
438 packed = LLVMBuildOr(builder, packed, component, "");
439 else
440 packed = component;
441 }
442 }
443
444 if (!packed)
445 packed = LLVMGetUndef(LLVMInt32TypeInContext(gallivm->context));
446
447 if (desc->block.bits < 32)
448 packed = LLVMBuildTrunc(builder, packed, type, "");
449
450 return packed;
451 }
452
453
454
455
456 /**
457 * Fetch a pixel into a 4 float AoS.
458 *
459 * \param format_desc describes format of the image we're fetching from
460 * \param aligned whether the data is guaranteed to be aligned
461 * \param ptr address of the pixel block (or the texel if uncompressed)
462 * \param i, j the sub-block pixel coordinates. For non-compressed formats
463 * these will always be (0, 0).
464 * \param cache optional value pointing to a lp_build_format_cache structure
465 * \return a 4 element vector with the pixel's RGBA values.
466 */
467 LLVMValueRef
468 lp_build_fetch_rgba_aos(struct gallivm_state *gallivm,
469 const struct util_format_description *format_desc,
470 struct lp_type type,
471 bool aligned,
472 LLVMValueRef base_ptr,
473 LLVMValueRef offset,
474 LLVMValueRef i,
475 LLVMValueRef j,
476 LLVMValueRef cache)
477 {
478 const struct util_format_unpack_description *unpack =
479 util_format_unpack_description(format_desc->format);
480 LLVMBuilderRef builder = gallivm->builder;
481 unsigned num_pixels = type.length / 4;
482 struct lp_build_context bld;
483
484 assert(type.length <= LP_MAX_VECTOR_LENGTH);
485 assert(type.length % 4 == 0);
486
487 lp_build_context_init(&bld, gallivm, type);
488
489 /*
490 * Trivial case
491 *
492 * The format matches the type (apart of a swizzle) so no need for
493 * scaling or converting.
494 */
495
496 if (format_matches_type(format_desc, type) &&
497 format_desc->block.bits <= type.width * 4 &&
498 /* XXX this shouldn't be needed */
499 util_is_power_of_two_or_zero(format_desc->block.bits)) {
500 LLVMValueRef packed;
501 LLVMTypeRef dst_vec_type = lp_build_vec_type(gallivm, type);
502 struct lp_type fetch_type;
503 unsigned vec_len = type.width * type.length;
504
505 /*
506 * The format matches the type (apart of a swizzle) so no need for
507 * scaling or converting.
508 */
509
510 fetch_type = lp_type_uint(type.width*4);
511 packed = lp_build_gather(gallivm, type.length/4,
512 format_desc->block.bits, fetch_type,
513 aligned, base_ptr, offset, true);
514
515 assert(format_desc->block.bits <= vec_len);
516 (void) vec_len; /* silence unused var warning for non-debug build */
517
518 packed = LLVMBuildBitCast(gallivm->builder, packed, dst_vec_type, "");
519 return lp_build_format_swizzle_aos(format_desc, &bld, packed);
520 }
521
522 /*
523 * Bit arithmetic for converting small_unorm to unorm8.
524 *
525 * This misses some opportunities for optimizations (like skipping mask
526 * for the highest channel for instance, or doing bit scaling in parallel
527 * for channels with the same bit width) but it should be passable for
528 * all arithmetic formats.
529 */
530 if (format_desc->layout == UTIL_FORMAT_LAYOUT_PLAIN &&
531 format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB &&
532 util_format_fits_8unorm(format_desc) &&
533 type.width == 8 && type.norm == 1 && type.sign == 0 &&
534 type.fixed == 0 && type.floating == 0) {
535 LLVMValueRef packed, res = NULL, chans[4], rgba[4];
536 LLVMTypeRef dst_vec_type, conv_vec_type;
537 struct lp_type fetch_type, conv_type;
538 struct lp_build_context bld_conv;
539 unsigned j;
540
541 fetch_type = lp_type_uint(type.width*4);
542 conv_type = lp_type_int_vec(type.width*4, type.width * type.length);
543 dst_vec_type = lp_build_vec_type(gallivm, type);
544 conv_vec_type = lp_build_vec_type(gallivm, conv_type);
545 lp_build_context_init(&bld_conv, gallivm, conv_type);
546
547 packed = lp_build_gather(gallivm, type.length/4,
548 format_desc->block.bits, fetch_type,
549 aligned, base_ptr, offset, true);
550
551 assert(format_desc->block.bits * type.length / 4 <=
552 type.width * type.length);
553
554 packed = LLVMBuildBitCast(gallivm->builder, packed, conv_vec_type, "");
555
556 for (j = 0; j < format_desc->nr_channels; ++j) {
557 unsigned mask = 0;
558 unsigned sa = format_desc->channel[j].shift;
559
560 mask = (1 << format_desc->channel[j].size) - 1;
561
562 /* Extract bits from source */
563 chans[j] = LLVMBuildLShr(builder, packed,
564 lp_build_const_int_vec(gallivm, conv_type, sa),
565 "");
566
567 chans[j] = LLVMBuildAnd(builder, chans[j],
568 lp_build_const_int_vec(gallivm, conv_type, mask),
569 "");
570
571 /* Scale bits */
572 if (type.norm) {
573 chans[j] = scale_bits_up(gallivm, format_desc->channel[j].size,
574 type.width, chans[j], conv_type);
575 }
576 }
577 /*
578 * This is a hacked lp_build_format_swizzle_soa() since we need a
579 * normalized 1 but only 8 bits in a 32bit vector...
580 */
581 for (j = 0; j < 4; ++j) {
582 enum pipe_swizzle swizzle = format_desc->swizzle[j];
583 if (swizzle == PIPE_SWIZZLE_1) {
584 rgba[j] = lp_build_const_int_vec(gallivm, conv_type, (1 << type.width) - 1);
585 } else {
586 rgba[j] = lp_build_swizzle_soa_channel(&bld_conv, chans, swizzle);
587 }
588 if (j == 0) {
589 res = rgba[j];
590 } else {
591 rgba[j] = LLVMBuildShl(builder, rgba[j],
592 lp_build_const_int_vec(gallivm, conv_type,
593 j * type.width), "");
594 res = LLVMBuildOr(builder, res, rgba[j], "");
595 }
596 }
597 res = LLVMBuildBitCast(gallivm->builder, res, dst_vec_type, "");
598
599 return res;
600 }
601
602 /*
603 * Bit arithmetic
604 */
605
606 if (format_desc->layout == UTIL_FORMAT_LAYOUT_PLAIN &&
607 (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB ||
608 format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) &&
609 format_desc->block.width == 1 &&
610 format_desc->block.height == 1 &&
611 /* XXX this shouldn't be needed */
612 util_is_power_of_two_or_zero(format_desc->block.bits) &&
613 format_desc->block.bits <= 32 &&
614 format_desc->is_bitmask &&
615 !format_desc->is_mixed &&
616 (format_desc->channel[0].type == UTIL_FORMAT_TYPE_UNSIGNED ||
617 format_desc->channel[1].type == UTIL_FORMAT_TYPE_UNSIGNED) &&
618 !format_desc->channel[0].pure_integer) {
619
620 LLVMValueRef tmps[LP_MAX_VECTOR_LENGTH/4];
621 LLVMValueRef res[LP_MAX_VECTOR_WIDTH / 128];
622 struct lp_type conv_type;
623 unsigned k, num_conv_src, num_conv_dst;
624
625 /*
626 * Note this path is generally terrible for fetching multiple pixels.
627 * We should make sure we cannot hit this code path for anything but
628 * single pixels.
629 */
630
631 /*
632 * Unpack a pixel at a time into a <4 x float> RGBA vector
633 */
634
635 for (k = 0; k < num_pixels; ++k) {
636 LLVMValueRef packed;
637
638 packed = lp_build_gather_elem(gallivm, num_pixels,
639 format_desc->block.bits, 32, aligned,
640 base_ptr, offset, k, false);
641
642 tmps[k] = lp_build_unpack_arith_rgba_aos(gallivm,
643 format_desc,
644 packed);
645 }
646
647 /*
648 * Type conversion.
649 *
650 * TODO: We could avoid floating conversion for integer to
651 * integer conversions.
652 */
653
654 if (gallivm_debug & GALLIVM_DEBUG_PERF && !type.floating) {
655 debug_printf("%s: unpacking %s with floating point\n",
656 __func__, format_desc->short_name);
657 }
658
659 conv_type = lp_float32_vec4_type();
660 num_conv_src = num_pixels;
661 num_conv_dst = 1;
662
663 if (num_pixels % 8 == 0) {
664 lp_build_concat_n(gallivm, lp_float32_vec4_type(),
665 tmps, num_pixels, tmps, num_pixels / 2);
666 conv_type.length *= num_pixels / 4;
667 num_conv_src = 4 * num_pixels / 8;
668 if (type.width == 8 && type.floating == 0 && type.fixed == 0) {
669 /*
670 * FIXME: The fast float->unorm path (which is basically
671 * skipping the MIN/MAX which are extremely pointless in any
672 * case) requires that there's 2 destinations...
673 * In any case, we really should make sure we don't hit this
674 * code with multiple pixels for unorm8 dst types, it's
675 * completely hopeless even if we do hit the right conversion.
676 */
677 type.length /= num_pixels / 4;
678 num_conv_dst = num_pixels / 4;
679 }
680 }
681
682 lp_build_conv(gallivm, conv_type, type,
683 tmps, num_conv_src, res, num_conv_dst);
684
685 if (num_pixels % 8 == 0 &&
686 (type.width == 8 && type.floating == 0 && type.fixed == 0)) {
687 lp_build_concat_n(gallivm, type, res, num_conv_dst, res, 1);
688 }
689
690 return lp_build_format_swizzle_aos(format_desc, &bld, res[0]);
691 }
692
693 /* If all channels are of same type and we are not using half-floats */
694 if (format_desc->is_array &&
695 format_desc->colorspace == UTIL_FORMAT_COLORSPACE_RGB) {
696 assert(!format_desc->is_mixed);
697 return lp_build_fetch_rgba_aos_array(gallivm, format_desc, type, base_ptr, offset);
698 }
699
700 /*
701 * YUV / subsampled formats
702 */
703
704 if (format_desc->layout == UTIL_FORMAT_LAYOUT_SUBSAMPLED) {
705 struct lp_type tmp_type;
706 LLVMValueRef tmp;
707
708 memset(&tmp_type, 0, sizeof tmp_type);
709 tmp_type.width = 8;
710 tmp_type.length = num_pixels * 4;
711 tmp_type.norm = true;
712
713 tmp = lp_build_fetch_subsampled_rgba_aos(gallivm,
714 format_desc,
715 num_pixels,
716 base_ptr,
717 offset,
718 i, j);
719
720 lp_build_conv(gallivm,
721 tmp_type, type,
722 &tmp, 1, &tmp, 1);
723
724 return tmp;
725 }
726
727 /*
728 * s3tc rgb formats
729 */
730
731 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
732 struct lp_type tmp_type;
733 LLVMValueRef tmp;
734
735 memset(&tmp_type, 0, sizeof tmp_type);
736 tmp_type.width = 8;
737 tmp_type.length = num_pixels * 4;
738 tmp_type.norm = true;
739
740 tmp = lp_build_fetch_s3tc_rgba_aos(gallivm,
741 format_desc,
742 num_pixels,
743 base_ptr,
744 offset,
745 i, j,
746 cache);
747
748 lp_build_conv(gallivm,
749 tmp_type, type,
750 &tmp, 1, &tmp, 1);
751
752 return tmp;
753 }
754
755 /*
756 * rgtc rgb formats
757 */
758
759 if (format_desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
760 struct lp_type tmp_type;
761 LLVMValueRef tmp;
762
763 memset(&tmp_type, 0, sizeof tmp_type);
764 tmp_type.width = 8;
765 tmp_type.length = num_pixels * 4;
766 tmp_type.norm = true;
767 tmp_type.sign = (format_desc->format == PIPE_FORMAT_RGTC1_SNORM ||
768 format_desc->format == PIPE_FORMAT_RGTC2_SNORM ||
769 format_desc->format == PIPE_FORMAT_LATC1_SNORM ||
770 format_desc->format == PIPE_FORMAT_LATC2_SNORM);
771
772 tmp = lp_build_fetch_rgtc_rgba_aos(gallivm,
773 format_desc,
774 num_pixels,
775 base_ptr,
776 offset,
777 i, j,
778 cache);
779
780 lp_build_conv(gallivm,
781 tmp_type, type,
782 &tmp, 1, &tmp, 1);
783
784 return tmp;
785 }
786
787 /*
788 * Fallback to util_format_description::fetch_rgba_8unorm().
789 */
790
791 if (unpack->fetch_rgba_8unorm &&
792 !type.floating && type.width == 8 && !type.sign && type.norm) {
793 /*
794 * Fallback to calling util_format_description::fetch_rgba_8unorm.
795 *
796 * This is definitely not the most efficient way of fetching pixels, as
797 * we miss the opportunity to do vectorization, but this it is a
798 * convenient for formats or scenarios for which there was no opportunity
799 * or incentive to optimize.
800 */
801
802 LLVMTypeRef i8t = LLVMInt8TypeInContext(gallivm->context);
803 LLVMTypeRef pi8t = LLVMPointerType(i8t, 0);
804 LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context);
805 LLVMValueRef function;
806 LLVMValueRef tmp_ptr;
807 LLVMValueRef tmp;
808 LLVMValueRef res;
809 unsigned k;
810
811 if (gallivm_debug & GALLIVM_DEBUG_PERF) {
812 debug_printf("%s: falling back to util_format_%s_fetch_rgba_8unorm\n",
813 __func__, format_desc->short_name);
814 }
815
816 /*
817 * Declare and bind format_desc->fetch_rgba_8unorm().
818 */
819
820 LLVMTypeRef function_type;
821 {
822 /*
823 * Function to call looks like:
824 * fetch(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
825 */
826 LLVMTypeRef ret_type;
827 LLVMTypeRef arg_types[4];
828
829 ret_type = LLVMVoidTypeInContext(gallivm->context);
830 arg_types[0] = pi8t;
831 arg_types[1] = pi8t;
832 arg_types[2] = i32t;
833 arg_types[3] = i32t;
834 function_type = LLVMFunctionType(ret_type, arg_types,
835 ARRAY_SIZE(arg_types), 0);
836 }
837
838 if (gallivm->cache)
839 gallivm->cache->dont_cache = true;
840 /* make const pointer for the C fetch_rgba_8unorm function */
841 function = lp_build_const_int_pointer(gallivm,
842 func_to_pointer((func_pointer) unpack->fetch_rgba_8unorm));
843 /* cast the callee pointer to the function's type */
844 function = LLVMBuildBitCast(builder, function, LLVMPointerType(function_type, 0), "cast callee");
845
846 tmp_ptr = lp_build_alloca(gallivm, i32t, "");
847
848 res = LLVMGetUndef(LLVMVectorType(i32t, num_pixels));
849
850 /*
851 * Invoke format_desc->fetch_rgba_8unorm() for each pixel and insert the result
852 * in the SoA vectors.
853 */
854
855 for (k = 0; k < num_pixels; ++k) {
856 LLVMValueRef index = lp_build_const_int32(gallivm, k);
857 LLVMValueRef args[4];
858
859 args[0] = LLVMBuildBitCast(builder, tmp_ptr, pi8t, "");
860 args[1] = lp_build_gather_elem_ptr(gallivm, num_pixels,
861 base_ptr, offset, k);
862
863 if (num_pixels == 1) {
864 args[2] = i;
865 args[3] = j;
866 }
867 else {
868 args[2] = LLVMBuildExtractElement(builder, i, index, "");
869 args[3] = LLVMBuildExtractElement(builder, j, index, "");
870 }
871
872 LLVMBuildCall2(builder, function_type, function, args, ARRAY_SIZE(args), "");
873
874 tmp = LLVMBuildLoad2(builder, i32t, tmp_ptr, "");
875
876 if (num_pixels == 1) {
877 res = tmp;
878 }
879 else {
880 res = LLVMBuildInsertElement(builder, res, tmp, index, "");
881 }
882 }
883
884 /* Bitcast from <n x i32> to <4n x i8> */
885 res = LLVMBuildBitCast(builder, res, bld.vec_type, "");
886
887 return res;
888 }
889
890 /*
891 * Fallback to fetch_rgba().
892 */
893
894 util_format_fetch_rgba_func_ptr fetch_rgba =
895 util_format_fetch_rgba_func(format_desc->format);
896 if (fetch_rgba) {
897 /*
898 * Fallback to calling util_format_description::fetch_rgba_float.
899 *
900 * This is definitely not the most efficient way of fetching pixels, as
901 * we miss the opportunity to do vectorization, but this it is a
902 * convenient for formats or scenarios for which there was no opportunity
903 * or incentive to optimize.
904 */
905
906 LLVMTypeRef f32t = LLVMFloatTypeInContext(gallivm->context);
907 LLVMTypeRef f32x4t = LLVMVectorType(f32t, 4);
908 LLVMTypeRef pf32t = LLVMPointerType(f32t, 0);
909 LLVMTypeRef pi8t = LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0);
910 LLVMTypeRef i32t = LLVMInt32TypeInContext(gallivm->context);
911 LLVMValueRef function;
912 LLVMValueRef tmp_ptr;
913 LLVMValueRef tmps[LP_MAX_VECTOR_LENGTH/4];
914 LLVMValueRef res;
915 unsigned k;
916
917 if (gallivm_debug & GALLIVM_DEBUG_PERF) {
918 debug_printf("%s: falling back to util_format_%s_fetch_rgba_float\n",
919 __func__, format_desc->short_name);
920 }
921
922 /*
923 * Declare and bind unpack->fetch_rgba_float().
924 */
925
926 LLVMTypeRef function_type = NULL;
927 {
928 /*
929 * Function to call looks like:
930 * fetch(float *dst, const uint8_t *src, unsigned i, unsigned j)
931 */
932 LLVMTypeRef ret_type;
933 LLVMTypeRef arg_types[4];
934
935 ret_type = LLVMVoidTypeInContext(gallivm->context);
936 arg_types[0] = pf32t;
937 arg_types[1] = pi8t;
938 arg_types[2] = i32t;
939 arg_types[3] = i32t;
940 function_type = LLVMFunctionType(ret_type, arg_types, ARRAY_SIZE(arg_types), 0);
941 }
942 if (gallivm->cache)
943 gallivm->cache->dont_cache = true;
944 function = lp_build_const_func_pointer_from_type(gallivm,
945 func_to_pointer((func_pointer) fetch_rgba),
946 function_type,
947 format_desc->short_name);
948
949 tmp_ptr = lp_build_alloca(gallivm, f32x4t, "");
950
951 /*
952 * Invoke format_desc->fetch_rgba_float() for each pixel and insert the result
953 * in the SoA vectors.
954 */
955
956 for (k = 0; k < num_pixels; ++k) {
957 LLVMValueRef args[4];
958
959 args[0] = LLVMBuildBitCast(builder, tmp_ptr, pf32t, "");
960 args[1] = lp_build_gather_elem_ptr(gallivm, num_pixels,
961 base_ptr, offset, k);
962
963 if (num_pixels == 1) {
964 args[2] = i;
965 args[3] = j;
966 }
967 else {
968 LLVMValueRef index = lp_build_const_int32(gallivm, k);
969 args[2] = LLVMBuildExtractElement(builder, i, index, "");
970 args[3] = LLVMBuildExtractElement(builder, j, index, "");
971 }
972
973 LLVMBuildCall2(builder, function_type, function, args, ARRAY_SIZE(args), "");
974
975 tmps[k] = LLVMBuildLoad2(builder, f32x4t, tmp_ptr, "");
976 }
977
978 lp_build_conv(gallivm,
979 lp_float32_vec4_type(),
980 type,
981 tmps, num_pixels, &res, 1);
982
983 return res;
984 }
985
986 assert(!util_format_is_pure_integer(format_desc->format));
987
988 assert(0);
989 return lp_build_undef(gallivm, type);
990 }