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_string.h"
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_format.h"
53 * Basic swizzling. Rearrange the order of the unswizzled array elements
54 * according to the format description. PIPE_SWIZZLE_ZERO/ONE are supported
56 * Ex: if unswizzled[4] = {B, G, R, x}, then swizzled_out[4] = {R, G, B, 1}.
59 lp_build_format_swizzle_aos(const struct util_format_description
*desc
,
60 struct lp_build_context
*bld
,
61 LLVMValueRef unswizzled
)
63 unsigned char swizzles
[4];
66 assert(bld
->type
.length
% 4 == 0);
68 for (chan
= 0; chan
< 4; ++chan
) {
69 enum util_format_swizzle swizzle
;
71 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
73 * For ZS formats do RGBA = ZZZ1
76 swizzle
= UTIL_FORMAT_SWIZZLE_1
;
77 } else if (desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_NONE
) {
78 swizzle
= UTIL_FORMAT_SWIZZLE_0
;
80 swizzle
= desc
->swizzle
[0];
83 swizzle
= desc
->swizzle
[chan
];
85 swizzles
[chan
] = swizzle
;
88 return lp_build_swizzle_aos(bld
, unswizzled
, swizzles
);
93 * Whether the format matches the vector type, apart of swizzles.
96 format_matches_type(const struct util_format_description
*desc
,
99 enum util_format_type chan_type
;
102 assert(type
.length
% 4 == 0);
104 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
105 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
||
106 desc
->block
.width
!= 1 ||
107 desc
->block
.height
!= 1) {
112 chan_type
= UTIL_FORMAT_TYPE_FLOAT
;
113 } else if (type
.fixed
) {
114 chan_type
= UTIL_FORMAT_TYPE_FIXED
;
115 } else if (type
.sign
) {
116 chan_type
= UTIL_FORMAT_TYPE_SIGNED
;
118 chan_type
= UTIL_FORMAT_TYPE_UNSIGNED
;
121 for (chan
= 0; chan
< desc
->nr_channels
; ++chan
) {
122 if (desc
->channel
[chan
].size
!= type
.width
) {
126 if (desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
) {
127 if (desc
->channel
[chan
].type
!= chan_type
||
128 desc
->channel
[chan
].normalized
!= type
.norm
) {
139 * Unpack a single pixel into its RGBA components.
141 * @param desc the pixel format for the packed pixel value
142 * @param packed integer pixel in a format such as PIPE_FORMAT_B8G8R8A8_UNORM
144 * @return RGBA in a float[4] or ubyte[4] or ushort[4] vector.
146 static INLINE LLVMValueRef
147 lp_build_unpack_arith_rgba_aos(LLVMBuilderRef builder
,
148 const struct util_format_description
*desc
,
151 LLVMValueRef shifted
, casted
, scaled
, masked
;
152 LLVMValueRef shifts
[4];
153 LLVMValueRef masks
[4];
154 LLVMValueRef scales
[4];
157 boolean needs_uitofp
;
161 /* TODO: Support more formats */
162 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
);
163 assert(desc
->block
.width
== 1);
164 assert(desc
->block
.height
== 1);
165 assert(desc
->block
.bits
<= 32);
167 /* Do the intermediate integer computations with 32bit integers since it
168 * matches floating point size */
169 assert (LLVMTypeOf(packed
) == LLVMInt32Type());
171 /* Broadcast the packed value to all four channels
172 * before: packed = BGRA
173 * after: packed = {BGRA, BGRA, BGRA, BGRA}
175 packed
= LLVMBuildInsertElement(builder
,
176 LLVMGetUndef(LLVMVectorType(LLVMInt32Type(), 4)),
178 LLVMConstNull(LLVMInt32Type()),
180 packed
= LLVMBuildShuffleVector(builder
,
182 LLVMGetUndef(LLVMVectorType(LLVMInt32Type(), 4)),
183 LLVMConstNull(LLVMVectorType(LLVMInt32Type(), 4)),
186 /* Initialize vector constants */
188 needs_uitofp
= FALSE
;
191 /* Loop over 4 color components */
192 for (i
= 0; i
< 4; ++i
) {
193 unsigned bits
= desc
->channel
[i
].size
;
195 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
) {
196 shifts
[i
] = LLVMGetUndef(LLVMInt32Type());
197 masks
[i
] = LLVMConstNull(LLVMInt32Type());
198 scales
[i
] = LLVMConstNull(LLVMFloatType());
201 unsigned long long mask
= (1ULL << bits
) - 1;
203 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
209 shifts
[i
] = LLVMConstInt(LLVMInt32Type(), shift
, 0);
210 masks
[i
] = LLVMConstInt(LLVMInt32Type(), mask
, 0);
212 if (desc
->channel
[i
].normalized
) {
213 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0/mask
);
217 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0);
223 /* Ex: convert packed = {BGRA, BGRA, BGRA, BGRA}
224 * into masked = {B, G, R, A}
226 shifted
= LLVMBuildLShr(builder
, packed
, LLVMConstVector(shifts
, 4), "");
227 masked
= LLVMBuildAnd(builder
, shifted
, LLVMConstVector(masks
, 4), "");
231 /* UIToFP can't be expressed in SSE2 */
232 casted
= LLVMBuildSIToFP(builder
, masked
, LLVMVectorType(LLVMFloatType(), 4), "");
234 casted
= LLVMBuildUIToFP(builder
, masked
, LLVMVectorType(LLVMFloatType(), 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(LLVMBuilderRef builder
,
261 const struct util_format_description
*desc
,
265 LLVMValueRef packed
= NULL
;
266 LLVMValueRef swizzles
[4];
267 LLVMValueRef shifted
, casted
, scaled
, unswizzled
;
268 LLVMValueRef shifts
[4];
269 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
= LLVMIntType(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
] = LLVMConstInt(LLVMInt32Type(), j
, 0);
289 swizzles
[i
] = LLVMGetUndef(LLVMInt32Type());
292 unswizzled
= LLVMBuildShuffleVector(builder
, rgba
,
293 LLVMGetUndef(LLVMVectorType(LLVMFloatType(), 4)),
294 LLVMConstVector(swizzles
, 4), "");
298 for (i
= 0; i
< 4; ++i
) {
299 unsigned bits
= desc
->channel
[i
].size
;
301 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
) {
302 shifts
[i
] = LLVMGetUndef(LLVMInt32Type());
303 scales
[i
] = LLVMGetUndef(LLVMFloatType());
306 unsigned mask
= (1 << bits
) - 1;
308 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
311 shifts
[i
] = LLVMConstInt(LLVMInt32Type(), shift
, 0);
313 if (desc
->channel
[i
].normalized
) {
314 scales
[i
] = LLVMConstReal(LLVMFloatType(), mask
);
318 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0);
325 scaled
= LLVMBuildFMul(builder
, unswizzled
, LLVMConstVector(scales
, 4), "");
329 casted
= LLVMBuildFPToSI(builder
, scaled
, LLVMVectorType(LLVMInt32Type(), 4), "");
331 shifted
= LLVMBuildShl(builder
, casted
, LLVMConstVector(shifts
, 4), "");
333 /* Bitwise or all components */
334 for (i
= 0; i
< 4; ++i
) {
335 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
336 LLVMValueRef component
= LLVMBuildExtractElement(builder
, shifted
, LLVMConstInt(LLVMInt32Type(), i
, 0), "");
338 packed
= LLVMBuildOr(builder
, packed
, component
, "");
345 packed
= LLVMGetUndef(LLVMInt32Type());
347 if (desc
->block
.bits
< 32)
348 packed
= LLVMBuildTrunc(builder
, packed
, type
, "");
357 * Fetch a pixel into a 4 float AoS.
359 * \param format_desc describes format of the image we're fetching from
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(LLVMBuilderRef builder
,
367 const struct util_format_description
*format_desc
,
369 LLVMValueRef base_ptr
,
374 unsigned num_pixels
= type
.length
/ 4;
375 struct lp_build_context bld
;
377 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
378 assert(type
.length
% 4 == 0);
380 lp_build_context_init(&bld
, builder
, type
);
385 * The format matches the type (apart of a swizzle) so no need for
386 * scaling or converting.
389 if (format_matches_type(format_desc
, type
) &&
390 format_desc
->block
.bits
<= type
.width
* 4 &&
391 util_is_power_of_two(format_desc
->block
.bits
)) {
395 * The format matches the type (apart of a swizzle) so no need for
396 * scaling or converting.
399 packed
= lp_build_gather(builder
, type
.length
/4,
400 format_desc
->block
.bits
, type
.width
*4,
403 assert(format_desc
->block
.bits
<= type
.width
* type
.length
);
405 packed
= LLVMBuildBitCast(builder
, packed
, lp_build_vec_type(type
), "");
407 return lp_build_format_swizzle_aos(format_desc
, &bld
, packed
);
414 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
&&
415 (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
416 format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) &&
417 format_desc
->block
.width
== 1 &&
418 format_desc
->block
.height
== 1 &&
419 util_is_power_of_two(format_desc
->block
.bits
) &&
420 format_desc
->block
.bits
<= 32 &&
421 format_desc
->is_bitmask
&&
422 !format_desc
->is_mixed
&&
423 (format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_UNSIGNED
||
424 format_desc
->channel
[1].type
== UTIL_FORMAT_TYPE_UNSIGNED
)) {
426 LLVMValueRef tmps
[LP_MAX_VECTOR_LENGTH
/4];
431 * Unpack a pixel at a time into a <4 x float> RGBA vector
434 for (k
= 0; k
< num_pixels
; ++k
) {
437 packed
= lp_build_gather_elem(builder
, num_pixels
,
438 format_desc
->block
.bits
, 32,
439 base_ptr
, offset
, k
);
441 tmps
[k
] = lp_build_unpack_arith_rgba_aos(builder
, format_desc
,
448 * TODO: We could avoid floating conversion for integer to
449 * integer conversions.
452 lp_build_conv(builder
,
453 lp_float32_vec4_type(),
455 tmps
, num_pixels
, &res
, 1);
457 return lp_build_format_swizzle_aos(format_desc
, &bld
, res
);
461 * YUV / subsampled formats
464 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
) {
465 struct lp_type tmp_type
;
468 memset(&tmp_type
, 0, sizeof tmp_type
);
470 tmp_type
.length
= num_pixels
* 4;
471 tmp_type
.norm
= TRUE
;
473 tmp
= lp_build_fetch_subsampled_rgba_aos(builder
,
480 lp_build_conv(builder
,
488 * Fallback to util_format_description::fetch_rgba_8unorm().
491 if (format_desc
->fetch_rgba_8unorm
&&
492 !type
.floating
&& type
.width
== 8 && !type
.sign
&& type
.norm
) {
494 * Fallback to calling util_format_description::fetch_rgba_8unorm.
496 * This is definitely not the most efficient way of fetching pixels, as
497 * we miss the opportunity to do vectorization, but this it is a
498 * convenient for formats or scenarios for which there was no opportunity
499 * or incentive to optimize.
502 LLVMModuleRef module
= LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder
)));
504 LLVMTypeRef i8t
= LLVMInt8Type();
505 LLVMTypeRef pi8t
= LLVMPointerType(i8t
, 0);
506 LLVMTypeRef i32t
= LLVMInt32Type();
507 LLVMValueRef function
;
508 LLVMValueRef tmp_ptr
;
513 util_snprintf(name
, sizeof name
, "util_format_%s_fetch_rgba_8unorm",
514 format_desc
->short_name
);
517 * Declare and bind format_desc->fetch_rgba_8unorm().
520 function
= LLVMGetNamedFunction(module
, name
);
522 LLVMTypeRef ret_type
;
523 LLVMTypeRef arg_types
[4];
524 LLVMTypeRef function_type
;
526 ret_type
= LLVMVoidType();
529 arg_types
[3] = arg_types
[2] = LLVMIntType(sizeof(unsigned) * 8);
530 function_type
= LLVMFunctionType(ret_type
, arg_types
, Elements(arg_types
), 0);
531 function
= LLVMAddFunction(module
, name
, function_type
);
533 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
534 LLVMSetLinkage(function
, LLVMExternalLinkage
);
536 assert(LLVMIsDeclaration(function
));
538 LLVMAddGlobalMapping(lp_build_engine
, function
,
539 func_to_pointer((func_pointer
)format_desc
->fetch_rgba_8unorm
));
542 tmp_ptr
= lp_build_alloca(builder
, i32t
, "");
544 res
= LLVMGetUndef(LLVMVectorType(i32t
, num_pixels
));
547 * Invoke format_desc->fetch_rgba_8unorm() for each pixel and insert the result
548 * in the SoA vectors.
551 for (k
= 0; k
< num_pixels
; ++k
) {
552 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), k
, 0);
553 LLVMValueRef args
[4];
555 args
[0] = LLVMBuildBitCast(builder
, tmp_ptr
, pi8t
, "");
556 args
[1] = lp_build_gather_elem_ptr(builder
, num_pixels
,
557 base_ptr
, offset
, k
);
559 if (num_pixels
== 1) {
564 args
[2] = LLVMBuildExtractElement(builder
, i
, index
, "");
565 args
[3] = LLVMBuildExtractElement(builder
, j
, index
, "");
568 LLVMBuildCall(builder
, function
, args
, Elements(args
), "");
570 tmp
= LLVMBuildLoad(builder
, tmp_ptr
, "");
572 if (num_pixels
== 1) {
576 res
= LLVMBuildInsertElement(builder
, res
, tmp
, index
, "");
580 /* Bitcast from <n x i32> to <4n x i8> */
581 res
= LLVMBuildBitCast(builder
, res
, bld
.vec_type
, "");
588 * Fallback to util_format_description::fetch_rgba_float().
591 if (format_desc
->fetch_rgba_float
) {
593 * Fallback to calling util_format_description::fetch_rgba_float.
595 * This is definitely not the most efficient way of fetching pixels, as
596 * we miss the opportunity to do vectorization, but this it is a
597 * convenient for formats or scenarios for which there was no opportunity
598 * or incentive to optimize.
601 LLVMModuleRef module
= LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder
)));
603 LLVMTypeRef f32t
= LLVMFloatType();
604 LLVMTypeRef f32x4t
= LLVMVectorType(f32t
, 4);
605 LLVMTypeRef pf32t
= LLVMPointerType(f32t
, 0);
606 LLVMValueRef function
;
607 LLVMValueRef tmp_ptr
;
608 LLVMValueRef tmps
[LP_MAX_VECTOR_LENGTH
/4];
612 util_snprintf(name
, sizeof name
, "util_format_%s_fetch_rgba_float",
613 format_desc
->short_name
);
616 * Declare and bind format_desc->fetch_rgba_float().
619 function
= LLVMGetNamedFunction(module
, name
);
621 LLVMTypeRef ret_type
;
622 LLVMTypeRef arg_types
[4];
623 LLVMTypeRef function_type
;
625 ret_type
= LLVMVoidType();
626 arg_types
[0] = pf32t
;
627 arg_types
[1] = LLVMPointerType(LLVMInt8Type(), 0);
628 arg_types
[3] = arg_types
[2] = LLVMIntType(sizeof(unsigned) * 8);
629 function_type
= LLVMFunctionType(ret_type
, arg_types
, Elements(arg_types
), 0);
630 function
= LLVMAddFunction(module
, name
, function_type
);
632 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
633 LLVMSetLinkage(function
, LLVMExternalLinkage
);
635 assert(LLVMIsDeclaration(function
));
637 LLVMAddGlobalMapping(lp_build_engine
, function
,
638 func_to_pointer((func_pointer
)format_desc
->fetch_rgba_float
));
641 tmp_ptr
= lp_build_alloca(builder
, f32x4t
, "");
644 * Invoke format_desc->fetch_rgba_float() for each pixel and insert the result
645 * in the SoA vectors.
648 for (k
= 0; k
< num_pixels
; ++k
) {
649 LLVMValueRef args
[4];
651 args
[0] = LLVMBuildBitCast(builder
, tmp_ptr
, pf32t
, "");
652 args
[1] = lp_build_gather_elem_ptr(builder
, num_pixels
,
653 base_ptr
, offset
, k
);
655 if (num_pixels
== 1) {
660 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), k
, 0);
661 args
[2] = LLVMBuildExtractElement(builder
, i
, index
, "");
662 args
[3] = LLVMBuildExtractElement(builder
, j
, index
, "");
665 LLVMBuildCall(builder
, function
, args
, Elements(args
), "");
667 tmps
[k
] = LLVMBuildLoad(builder
, tmp_ptr
, "");
670 lp_build_conv(builder
,
671 lp_float32_vec4_type(),
673 tmps
, num_pixels
, &res
, 1);
679 return lp_build_undef(type
);