1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * AoS pixel format manipulation.
32 * @author Jose Fonseca <jfonseca@vmware.com>
36 #include "util/u_format.h"
37 #include "util/u_memory.h"
38 #include "util/u_math.h"
39 #include "util/u_pointer.h"
40 #include "util/u_string.h"
42 #include "lp_bld_arit.h"
43 #include "lp_bld_init.h"
44 #include "lp_bld_type.h"
45 #include "lp_bld_flow.h"
46 #include "lp_bld_const.h"
47 #include "lp_bld_conv.h"
48 #include "lp_bld_swizzle.h"
49 #include "lp_bld_gather.h"
50 #include "lp_bld_debug.h"
51 #include "lp_bld_format.h"
52 #include "lp_bld_intr.h"
56 * Basic swizzling. Rearrange the order of the unswizzled array elements
57 * according to the format description. PIPE_SWIZZLE_ZERO/ONE are supported
59 * Ex: if unswizzled[4] = {B, G, R, x}, then swizzled_out[4] = {R, G, B, 1}.
62 lp_build_format_swizzle_aos(const struct util_format_description
*desc
,
63 struct lp_build_context
*bld
,
64 LLVMValueRef unswizzled
)
66 unsigned char swizzles
[4];
69 assert(bld
->type
.length
% 4 == 0);
71 for (chan
= 0; chan
< 4; ++chan
) {
72 enum util_format_swizzle swizzle
;
74 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
76 * For ZS formats do RGBA = ZZZ1
79 swizzle
= UTIL_FORMAT_SWIZZLE_1
;
80 } else if (desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_NONE
) {
81 swizzle
= UTIL_FORMAT_SWIZZLE_0
;
83 swizzle
= desc
->swizzle
[0];
86 swizzle
= desc
->swizzle
[chan
];
88 swizzles
[chan
] = swizzle
;
91 return lp_build_swizzle_aos(bld
, unswizzled
, swizzles
);
96 * Whether the format matches the vector type, apart of swizzles.
99 format_matches_type(const struct util_format_description
*desc
,
102 enum util_format_type chan_type
;
105 assert(type
.length
% 4 == 0);
107 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
108 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
||
109 desc
->block
.width
!= 1 ||
110 desc
->block
.height
!= 1) {
115 chan_type
= UTIL_FORMAT_TYPE_FLOAT
;
116 } else if (type
.fixed
) {
117 chan_type
= UTIL_FORMAT_TYPE_FIXED
;
118 } else if (type
.sign
) {
119 chan_type
= UTIL_FORMAT_TYPE_SIGNED
;
121 chan_type
= UTIL_FORMAT_TYPE_UNSIGNED
;
124 for (chan
= 0; chan
< desc
->nr_channels
; ++chan
) {
125 if (desc
->channel
[chan
].size
!= type
.width
) {
129 if (desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
) {
130 if (desc
->channel
[chan
].type
!= chan_type
||
131 desc
->channel
[chan
].normalized
!= type
.norm
) {
142 * Unpack a single pixel into its XYZW components.
144 * @param desc the pixel format for the packed pixel value
145 * @param packed integer pixel in a format such as PIPE_FORMAT_B8G8R8A8_UNORM
147 * @return XYZW in a float[4] or ubyte[4] or ushort[4] vector.
149 static inline LLVMValueRef
150 lp_build_unpack_arith_rgba_aos(struct gallivm_state
*gallivm
,
151 const struct util_format_description
*desc
,
154 LLVMBuilderRef builder
= gallivm
->builder
;
155 LLVMValueRef shifted
, casted
, scaled
, masked
;
156 LLVMValueRef shifts
[4];
157 LLVMValueRef masks
[4];
158 LLVMValueRef scales
[4];
161 boolean needs_uitofp
;
164 /* TODO: Support more formats */
165 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
);
166 assert(desc
->block
.width
== 1);
167 assert(desc
->block
.height
== 1);
168 assert(desc
->block
.bits
<= 32);
170 /* Do the intermediate integer computations with 32bit integers since it
171 * matches floating point size */
172 assert (LLVMTypeOf(packed
) == LLVMInt32TypeInContext(gallivm
->context
));
174 /* Broadcast the packed value to all four channels
175 * before: packed = BGRA
176 * after: packed = {BGRA, BGRA, BGRA, BGRA}
178 packed
= LLVMBuildInsertElement(builder
,
179 LLVMGetUndef(LLVMVectorType(LLVMInt32TypeInContext(gallivm
->context
), 4)),
181 LLVMConstNull(LLVMInt32TypeInContext(gallivm
->context
)),
183 packed
= LLVMBuildShuffleVector(builder
,
185 LLVMGetUndef(LLVMVectorType(LLVMInt32TypeInContext(gallivm
->context
), 4)),
186 LLVMConstNull(LLVMVectorType(LLVMInt32TypeInContext(gallivm
->context
), 4)),
189 /* Initialize vector constants */
191 needs_uitofp
= FALSE
;
193 /* Loop over 4 color components */
194 for (i
= 0; i
< 4; ++i
) {
195 unsigned bits
= desc
->channel
[i
].size
;
196 unsigned shift
= desc
->channel
[i
].shift
;
198 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
) {
199 shifts
[i
] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
200 masks
[i
] = LLVMConstNull(LLVMInt32TypeInContext(gallivm
->context
));
201 scales
[i
] = LLVMConstNull(LLVMFloatTypeInContext(gallivm
->context
));
204 unsigned long long mask
= (1ULL << bits
) - 1;
206 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
212 shifts
[i
] = lp_build_const_int32(gallivm
, shift
);
213 masks
[i
] = lp_build_const_int32(gallivm
, mask
);
215 if (desc
->channel
[i
].normalized
) {
216 scales
[i
] = lp_build_const_float(gallivm
, 1.0 / mask
);
220 scales
[i
] = lp_build_const_float(gallivm
, 1.0);
224 /* Ex: convert packed = {XYZW, XYZW, XYZW, XYZW}
225 * into masked = {X, Y, Z, W}
227 shifted
= LLVMBuildLShr(builder
, packed
, LLVMConstVector(shifts
, 4), "");
228 masked
= LLVMBuildAnd(builder
, shifted
, LLVMConstVector(masks
, 4), "");
231 /* UIToFP can't be expressed in SSE2 */
232 casted
= LLVMBuildSIToFP(builder
, masked
, LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4), "");
234 casted
= LLVMBuildUIToFP(builder
, masked
, LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4), "");
237 /* At this point 'casted' may be a vector of floats such as
238 * {255.0, 255.0, 255.0, 255.0}. Next, if the pixel values are normalized
239 * we'll scale this to {1.0, 1.0, 1.0, 1.0}.
243 scaled
= LLVMBuildFMul(builder
, casted
, LLVMConstVector(scales
, 4), "");
252 * Pack a single pixel.
254 * @param rgba 4 float vector with the unpacked components.
256 * XXX: This is mostly for reference and testing -- operating a single pixel at
257 * a time is rarely if ever needed.
260 lp_build_pack_rgba_aos(struct gallivm_state
*gallivm
,
261 const struct util_format_description
*desc
,
264 LLVMBuilderRef builder
= gallivm
->builder
;
266 LLVMValueRef packed
= NULL
;
267 LLVMValueRef swizzles
[4];
268 LLVMValueRef shifted
, casted
, scaled
, unswizzled
;
269 LLVMValueRef shifts
[4];
270 LLVMValueRef scales
[4];
274 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
);
275 assert(desc
->block
.width
== 1);
276 assert(desc
->block
.height
== 1);
278 type
= LLVMIntTypeInContext(gallivm
->context
, desc
->block
.bits
);
280 /* Unswizzle the color components into the source vector. */
281 for (i
= 0; i
< 4; ++i
) {
282 for (j
= 0; j
< 4; ++j
) {
283 if (desc
->swizzle
[j
] == i
)
287 swizzles
[i
] = lp_build_const_int32(gallivm
, j
);
289 swizzles
[i
] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
292 unswizzled
= LLVMBuildShuffleVector(builder
, rgba
,
293 LLVMGetUndef(LLVMVectorType(LLVMFloatTypeInContext(gallivm
->context
), 4)),
294 LLVMConstVector(swizzles
, 4), "");
297 for (i
= 0; i
< 4; ++i
) {
298 unsigned bits
= desc
->channel
[i
].size
;
299 unsigned shift
= desc
->channel
[i
].shift
;
301 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
) {
302 shifts
[i
] = LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
303 scales
[i
] = LLVMGetUndef(LLVMFloatTypeInContext(gallivm
->context
));
306 unsigned mask
= (1 << bits
) - 1;
308 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
311 shifts
[i
] = lp_build_const_int32(gallivm
, shift
);
313 if (desc
->channel
[i
].normalized
) {
314 scales
[i
] = lp_build_const_float(gallivm
, mask
);
318 scales
[i
] = lp_build_const_float(gallivm
, 1.0);
323 scaled
= LLVMBuildFMul(builder
, unswizzled
, LLVMConstVector(scales
, 4), "");
327 casted
= LLVMBuildFPToSI(builder
, scaled
, LLVMVectorType(LLVMInt32TypeInContext(gallivm
->context
), 4), "");
329 shifted
= LLVMBuildShl(builder
, casted
, LLVMConstVector(shifts
, 4), "");
331 /* Bitwise or all components */
332 for (i
= 0; i
< 4; ++i
) {
333 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
334 LLVMValueRef component
= LLVMBuildExtractElement(builder
, shifted
,
335 lp_build_const_int32(gallivm
, i
), "");
337 packed
= LLVMBuildOr(builder
, packed
, component
, "");
344 packed
= LLVMGetUndef(LLVMInt32TypeInContext(gallivm
->context
));
346 if (desc
->block
.bits
< 32)
347 packed
= LLVMBuildTrunc(builder
, packed
, type
, "");
356 * Fetch a pixel into a 4 float AoS.
358 * \param format_desc describes format of the image we're fetching from
359 * \param aligned whether the data is guaranteed to be aligned
360 * \param ptr address of the pixel block (or the texel if uncompressed)
361 * \param i, j the sub-block pixel coordinates. For non-compressed formats
362 * these will always be (0, 0).
363 * \return a 4 element vector with the pixel's RGBA values.
366 lp_build_fetch_rgba_aos(struct gallivm_state
*gallivm
,
367 const struct util_format_description
*format_desc
,
370 LLVMValueRef base_ptr
,
376 LLVMBuilderRef builder
= gallivm
->builder
;
377 unsigned num_pixels
= type
.length
/ 4;
378 struct lp_build_context bld
;
380 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
381 assert(type
.length
% 4 == 0);
383 lp_build_context_init(&bld
, gallivm
, type
);
388 * The format matches the type (apart of a swizzle) so no need for
389 * scaling or converting.
392 if (format_matches_type(format_desc
, type
) &&
393 format_desc
->block
.bits
<= type
.width
* 4 &&
394 util_is_power_of_two(format_desc
->block
.bits
)) {
396 LLVMTypeRef dst_vec_type
= lp_build_vec_type(gallivm
, type
);
397 unsigned vec_len
= type
.width
* type
.length
;
400 * The format matches the type (apart of a swizzle) so no need for
401 * scaling or converting.
404 packed
= lp_build_gather(gallivm
, type
.length
/4,
405 format_desc
->block
.bits
, type
.width
*4,
406 aligned
, base_ptr
, offset
, TRUE
);
408 assert(format_desc
->block
.bits
<= vec_len
);
409 (void) vec_len
; /* silence unused var warning for non-debug build */
411 packed
= LLVMBuildBitCast(gallivm
->builder
, packed
, dst_vec_type
, "");
412 return lp_build_format_swizzle_aos(format_desc
, &bld
, packed
);
419 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
&&
420 (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
421 format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) &&
422 format_desc
->block
.width
== 1 &&
423 format_desc
->block
.height
== 1 &&
424 util_is_power_of_two(format_desc
->block
.bits
) &&
425 format_desc
->block
.bits
<= 32 &&
426 format_desc
->is_bitmask
&&
427 !format_desc
->is_mixed
&&
428 (format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_UNSIGNED
||
429 format_desc
->channel
[1].type
== UTIL_FORMAT_TYPE_UNSIGNED
) &&
430 !format_desc
->channel
[0].pure_integer
) {
432 LLVMValueRef tmps
[LP_MAX_VECTOR_LENGTH
/4];
437 * Unpack a pixel at a time into a <4 x float> RGBA vector
440 for (k
= 0; k
< num_pixels
; ++k
) {
443 packed
= lp_build_gather_elem(gallivm
, num_pixels
,
444 format_desc
->block
.bits
, 32, aligned
,
445 base_ptr
, offset
, k
, FALSE
);
447 tmps
[k
] = lp_build_unpack_arith_rgba_aos(gallivm
,
455 * TODO: We could avoid floating conversion for integer to
456 * integer conversions.
459 if (gallivm_debug
& GALLIVM_DEBUG_PERF
&& !type
.floating
) {
460 debug_printf("%s: unpacking %s with floating point\n",
461 __FUNCTION__
, format_desc
->short_name
);
464 lp_build_conv(gallivm
,
465 lp_float32_vec4_type(),
467 tmps
, num_pixels
, &res
, 1);
469 return lp_build_format_swizzle_aos(format_desc
, &bld
, res
);
472 /* If all channels are of same type and we are not using half-floats */
473 if (format_desc
->is_array
&&
474 format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
) {
475 assert(!format_desc
->is_mixed
);
476 return lp_build_fetch_rgba_aos_array(gallivm
, format_desc
, type
, base_ptr
, offset
);
480 * YUV / subsampled formats
483 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
) {
484 struct lp_type tmp_type
;
487 memset(&tmp_type
, 0, sizeof tmp_type
);
489 tmp_type
.length
= num_pixels
* 4;
490 tmp_type
.norm
= TRUE
;
492 tmp
= lp_build_fetch_subsampled_rgba_aos(gallivm
,
499 lp_build_conv(gallivm
,
510 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
&& cache
) {
511 struct lp_type tmp_type
;
514 memset(&tmp_type
, 0, sizeof tmp_type
);
516 tmp_type
.length
= num_pixels
* 4;
517 tmp_type
.norm
= TRUE
;
519 tmp
= lp_build_fetch_cached_texels(gallivm
,
527 lp_build_conv(gallivm
,
535 * Fallback to util_format_description::fetch_rgba_8unorm().
538 if (format_desc
->fetch_rgba_8unorm
&&
539 !type
.floating
&& type
.width
== 8 && !type
.sign
&& type
.norm
) {
541 * Fallback to calling util_format_description::fetch_rgba_8unorm.
543 * This is definitely not the most efficient way of fetching pixels, as
544 * we miss the opportunity to do vectorization, but this it is a
545 * convenient for formats or scenarios for which there was no opportunity
546 * or incentive to optimize.
549 LLVMTypeRef i8t
= LLVMInt8TypeInContext(gallivm
->context
);
550 LLVMTypeRef pi8t
= LLVMPointerType(i8t
, 0);
551 LLVMTypeRef i32t
= LLVMInt32TypeInContext(gallivm
->context
);
552 LLVMValueRef function
;
553 LLVMValueRef tmp_ptr
;
558 if (gallivm_debug
& GALLIVM_DEBUG_PERF
) {
559 debug_printf("%s: falling back to util_format_%s_fetch_rgba_8unorm\n",
560 __FUNCTION__
, format_desc
->short_name
);
564 * Declare and bind format_desc->fetch_rgba_8unorm().
569 * Function to call looks like:
570 * fetch(uint8_t *dst, const uint8_t *src, unsigned i, unsigned j)
572 LLVMTypeRef ret_type
;
573 LLVMTypeRef arg_types
[4];
574 LLVMTypeRef function_type
;
576 ret_type
= LLVMVoidTypeInContext(gallivm
->context
);
581 function_type
= LLVMFunctionType(ret_type
, arg_types
,
582 Elements(arg_types
), 0);
584 /* make const pointer for the C fetch_rgba_8unorm function */
585 function
= lp_build_const_int_pointer(gallivm
,
586 func_to_pointer((func_pointer
) format_desc
->fetch_rgba_8unorm
));
588 /* cast the callee pointer to the function's type */
589 function
= LLVMBuildBitCast(builder
, function
,
590 LLVMPointerType(function_type
, 0),
594 tmp_ptr
= lp_build_alloca(gallivm
, i32t
, "");
596 res
= LLVMGetUndef(LLVMVectorType(i32t
, num_pixels
));
599 * Invoke format_desc->fetch_rgba_8unorm() for each pixel and insert the result
600 * in the SoA vectors.
603 for (k
= 0; k
< num_pixels
; ++k
) {
604 LLVMValueRef index
= lp_build_const_int32(gallivm
, k
);
605 LLVMValueRef args
[4];
607 args
[0] = LLVMBuildBitCast(builder
, tmp_ptr
, pi8t
, "");
608 args
[1] = lp_build_gather_elem_ptr(gallivm
, num_pixels
,
609 base_ptr
, offset
, k
);
611 if (num_pixels
== 1) {
616 args
[2] = LLVMBuildExtractElement(builder
, i
, index
, "");
617 args
[3] = LLVMBuildExtractElement(builder
, j
, index
, "");
620 LLVMBuildCall(builder
, function
, args
, Elements(args
), "");
622 tmp
= LLVMBuildLoad(builder
, tmp_ptr
, "");
624 if (num_pixels
== 1) {
628 res
= LLVMBuildInsertElement(builder
, res
, tmp
, index
, "");
632 /* Bitcast from <n x i32> to <4n x i8> */
633 res
= LLVMBuildBitCast(builder
, res
, bld
.vec_type
, "");
639 * Fallback to util_format_description::fetch_rgba_float().
642 if (format_desc
->fetch_rgba_float
) {
644 * Fallback to calling util_format_description::fetch_rgba_float.
646 * This is definitely not the most efficient way of fetching pixels, as
647 * we miss the opportunity to do vectorization, but this it is a
648 * convenient for formats or scenarios for which there was no opportunity
649 * or incentive to optimize.
652 LLVMTypeRef f32t
= LLVMFloatTypeInContext(gallivm
->context
);
653 LLVMTypeRef f32x4t
= LLVMVectorType(f32t
, 4);
654 LLVMTypeRef pf32t
= LLVMPointerType(f32t
, 0);
655 LLVMTypeRef pi8t
= LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
656 LLVMTypeRef i32t
= LLVMInt32TypeInContext(gallivm
->context
);
657 LLVMValueRef function
;
658 LLVMValueRef tmp_ptr
;
659 LLVMValueRef tmps
[LP_MAX_VECTOR_LENGTH
/4];
663 if (gallivm_debug
& GALLIVM_DEBUG_PERF
) {
664 debug_printf("%s: falling back to util_format_%s_fetch_rgba_float\n",
665 __FUNCTION__
, format_desc
->short_name
);
669 * Declare and bind format_desc->fetch_rgba_float().
674 * Function to call looks like:
675 * fetch(float *dst, const uint8_t *src, unsigned i, unsigned j)
677 LLVMTypeRef ret_type
;
678 LLVMTypeRef arg_types
[4];
680 ret_type
= LLVMVoidTypeInContext(gallivm
->context
);
681 arg_types
[0] = pf32t
;
686 function
= lp_build_const_func_pointer(gallivm
,
687 func_to_pointer((func_pointer
) format_desc
->fetch_rgba_float
),
689 arg_types
, Elements(arg_types
),
690 format_desc
->short_name
);
693 tmp_ptr
= lp_build_alloca(gallivm
, f32x4t
, "");
696 * Invoke format_desc->fetch_rgba_float() for each pixel and insert the result
697 * in the SoA vectors.
700 for (k
= 0; k
< num_pixels
; ++k
) {
701 LLVMValueRef args
[4];
703 args
[0] = LLVMBuildBitCast(builder
, tmp_ptr
, pf32t
, "");
704 args
[1] = lp_build_gather_elem_ptr(gallivm
, num_pixels
,
705 base_ptr
, offset
, k
);
707 if (num_pixels
== 1) {
712 LLVMValueRef index
= lp_build_const_int32(gallivm
, k
);
713 args
[2] = LLVMBuildExtractElement(builder
, i
, index
, "");
714 args
[3] = LLVMBuildExtractElement(builder
, j
, index
, "");
717 LLVMBuildCall(builder
, function
, args
, Elements(args
), "");
719 tmps
[k
] = LLVMBuildLoad(builder
, tmp_ptr
, "");
722 lp_build_conv(gallivm
,
723 lp_float32_vec4_type(),
725 tmps
, num_pixels
, &res
, 1);
730 assert(!util_format_is_pure_integer(format_desc
->format
));
733 return lp_build_undef(gallivm
, type
);