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