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_debug.h"
50 #include "lp_bld_format.h"
54 * Basic swizzling. Rearrange the order of the unswizzled array elements
55 * according to the format description. PIPE_SWIZZLE_ZERO/ONE are supported
57 * Ex: if unswizzled[4] = {B, G, R, x}, then swizzled_out[4] = {R, G, B, 1}.
60 lp_build_format_swizzle_aos(const struct util_format_description
*desc
,
61 struct lp_build_context
*bld
,
62 LLVMValueRef unswizzled
)
64 unsigned char swizzles
[4];
67 assert(bld
->type
.length
% 4 == 0);
69 for (chan
= 0; chan
< 4; ++chan
) {
70 enum util_format_swizzle swizzle
;
72 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
74 * For ZS formats do RGBA = ZZZ1
77 swizzle
= UTIL_FORMAT_SWIZZLE_1
;
78 } else if (desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_NONE
) {
79 swizzle
= UTIL_FORMAT_SWIZZLE_0
;
81 swizzle
= desc
->swizzle
[0];
84 swizzle
= desc
->swizzle
[chan
];
86 swizzles
[chan
] = swizzle
;
89 return lp_build_swizzle_aos(bld
, unswizzled
, swizzles
);
94 * Whether the format matches the vector type, apart of swizzles.
97 format_matches_type(const struct util_format_description
*desc
,
100 enum util_format_type chan_type
;
103 assert(type
.length
% 4 == 0);
105 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
||
106 desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_RGB
||
107 desc
->block
.width
!= 1 ||
108 desc
->block
.height
!= 1) {
113 chan_type
= UTIL_FORMAT_TYPE_FLOAT
;
114 } else if (type
.fixed
) {
115 chan_type
= UTIL_FORMAT_TYPE_FIXED
;
116 } else if (type
.sign
) {
117 chan_type
= UTIL_FORMAT_TYPE_SIGNED
;
119 chan_type
= UTIL_FORMAT_TYPE_UNSIGNED
;
122 for (chan
= 0; chan
< desc
->nr_channels
; ++chan
) {
123 if (desc
->channel
[chan
].size
!= type
.width
) {
127 if (desc
->channel
[chan
].type
!= UTIL_FORMAT_TYPE_VOID
) {
128 if (desc
->channel
[chan
].type
!= chan_type
||
129 desc
->channel
[chan
].normalized
!= type
.norm
) {
140 * Unpack a single pixel into its RGBA components.
142 * @param desc the pixel format for the packed pixel value
143 * @param packed integer pixel in a format such as PIPE_FORMAT_B8G8R8A8_UNORM
145 * @return RGBA in a float[4] or ubyte[4] or ushort[4] vector.
147 static INLINE LLVMValueRef
148 lp_build_unpack_arith_rgba_aos(LLVMBuilderRef builder
,
149 const struct util_format_description
*desc
,
152 LLVMValueRef shifted
, casted
, scaled
, masked
;
153 LLVMValueRef shifts
[4];
154 LLVMValueRef masks
[4];
155 LLVMValueRef scales
[4];
158 boolean needs_uitofp
;
162 /* TODO: Support more formats */
163 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
);
164 assert(desc
->block
.width
== 1);
165 assert(desc
->block
.height
== 1);
166 assert(desc
->block
.bits
<= 32);
168 /* Do the intermediate integer computations with 32bit integers since it
169 * matches floating point size */
170 assert (LLVMTypeOf(packed
) == LLVMInt32Type());
172 /* Broadcast the packed value to all four channels
173 * before: packed = BGRA
174 * after: packed = {BGRA, BGRA, BGRA, BGRA}
176 packed
= LLVMBuildInsertElement(builder
,
177 LLVMGetUndef(LLVMVectorType(LLVMInt32Type(), 4)),
179 LLVMConstNull(LLVMInt32Type()),
181 packed
= LLVMBuildShuffleVector(builder
,
183 LLVMGetUndef(LLVMVectorType(LLVMInt32Type(), 4)),
184 LLVMConstNull(LLVMVectorType(LLVMInt32Type(), 4)),
187 /* Initialize vector constants */
189 needs_uitofp
= FALSE
;
192 /* Loop over 4 color components */
193 for (i
= 0; i
< 4; ++i
) {
194 unsigned bits
= desc
->channel
[i
].size
;
196 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
) {
197 shifts
[i
] = LLVMGetUndef(LLVMInt32Type());
198 masks
[i
] = LLVMConstNull(LLVMInt32Type());
199 scales
[i
] = LLVMConstNull(LLVMFloatType());
202 unsigned long long mask
= (1ULL << bits
) - 1;
204 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
210 shifts
[i
] = LLVMConstInt(LLVMInt32Type(), shift
, 0);
211 masks
[i
] = LLVMConstInt(LLVMInt32Type(), mask
, 0);
213 if (desc
->channel
[i
].normalized
) {
214 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0/mask
);
218 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0);
224 /* Ex: convert packed = {BGRA, BGRA, BGRA, BGRA}
225 * into masked = {B, G, R, A}
227 shifted
= LLVMBuildLShr(builder
, packed
, LLVMConstVector(shifts
, 4), "");
228 masked
= LLVMBuildAnd(builder
, shifted
, LLVMConstVector(masks
, 4), "");
232 /* UIToFP can't be expressed in SSE2 */
233 casted
= LLVMBuildSIToFP(builder
, masked
, LLVMVectorType(LLVMFloatType(), 4), "");
235 casted
= LLVMBuildUIToFP(builder
, masked
, LLVMVectorType(LLVMFloatType(), 4), "");
238 /* At this point 'casted' may be a vector of floats such as
239 * {255.0, 255.0, 255.0, 255.0}. Next, if the pixel values are normalized
240 * we'll scale this to {1.0, 1.0, 1.0, 1.0}.
244 scaled
= LLVMBuildFMul(builder
, casted
, LLVMConstVector(scales
, 4), "");
253 * Pack a single pixel.
255 * @param rgba 4 float vector with the unpacked components.
257 * XXX: This is mostly for reference and testing -- operating a single pixel at
258 * a time is rarely if ever needed.
261 lp_build_pack_rgba_aos(LLVMBuilderRef builder
,
262 const struct util_format_description
*desc
,
266 LLVMValueRef packed
= NULL
;
267 LLVMValueRef swizzles
[4];
268 LLVMValueRef shifted
, casted
, scaled
, unswizzled
;
269 LLVMValueRef shifts
[4];
270 LLVMValueRef scales
[4];
275 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
);
276 assert(desc
->block
.width
== 1);
277 assert(desc
->block
.height
== 1);
279 type
= LLVMIntType(desc
->block
.bits
);
281 /* Unswizzle the color components into the source vector. */
282 for (i
= 0; i
< 4; ++i
) {
283 for (j
= 0; j
< 4; ++j
) {
284 if (desc
->swizzle
[j
] == i
)
288 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), j
, 0);
290 swizzles
[i
] = LLVMGetUndef(LLVMInt32Type());
293 unswizzled
= LLVMBuildShuffleVector(builder
, rgba
,
294 LLVMGetUndef(LLVMVectorType(LLVMFloatType(), 4)),
295 LLVMConstVector(swizzles
, 4), "");
299 for (i
= 0; i
< 4; ++i
) {
300 unsigned bits
= desc
->channel
[i
].size
;
302 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
) {
303 shifts
[i
] = LLVMGetUndef(LLVMInt32Type());
304 scales
[i
] = LLVMGetUndef(LLVMFloatType());
307 unsigned mask
= (1 << bits
) - 1;
309 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
312 shifts
[i
] = LLVMConstInt(LLVMInt32Type(), shift
, 0);
314 if (desc
->channel
[i
].normalized
) {
315 scales
[i
] = LLVMConstReal(LLVMFloatType(), mask
);
319 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0);
326 scaled
= LLVMBuildFMul(builder
, unswizzled
, LLVMConstVector(scales
, 4), "");
330 casted
= LLVMBuildFPToSI(builder
, scaled
, LLVMVectorType(LLVMInt32Type(), 4), "");
332 shifted
= LLVMBuildShl(builder
, casted
, LLVMConstVector(shifts
, 4), "");
334 /* Bitwise or all components */
335 for (i
= 0; i
< 4; ++i
) {
336 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
337 LLVMValueRef component
= LLVMBuildExtractElement(builder
, shifted
, LLVMConstInt(LLVMInt32Type(), i
, 0), "");
339 packed
= LLVMBuildOr(builder
, packed
, component
, "");
346 packed
= LLVMGetUndef(LLVMInt32Type());
348 if (desc
->block
.bits
< 32)
349 packed
= LLVMBuildTrunc(builder
, packed
, type
, "");
358 * Fetch a pixel into a 4 float AoS.
360 * \param format_desc describes format of the image we're fetching from
361 * \param ptr address of the pixel block (or the texel if uncompressed)
362 * \param i, j the sub-block pixel coordinates. For non-compressed formats
363 * these will always be (0, 0).
364 * \return a 4 element vector with the pixel's RGBA values.
367 lp_build_fetch_rgba_aos(LLVMBuilderRef builder
,
368 const struct util_format_description
*format_desc
,
370 LLVMValueRef base_ptr
,
375 unsigned num_pixels
= type
.length
/ 4;
376 struct lp_build_context bld
;
378 assert(type
.length
<= LP_MAX_VECTOR_LENGTH
);
379 assert(type
.length
% 4 == 0);
381 lp_build_context_init(&bld
, builder
, type
);
386 * The format matches the type (apart of a swizzle) so no need for
387 * scaling or converting.
390 if (format_matches_type(format_desc
, type
) &&
391 format_desc
->block
.bits
<= type
.width
* 4 &&
392 util_is_power_of_two(format_desc
->block
.bits
)) {
396 * The format matches the type (apart of a swizzle) so no need for
397 * scaling or converting.
400 packed
= lp_build_gather(builder
, type
.length
/4,
401 format_desc
->block
.bits
, type
.width
*4,
404 assert(format_desc
->block
.bits
<= type
.width
* type
.length
);
406 packed
= LLVMBuildBitCast(builder
, packed
, lp_build_vec_type(type
), "");
408 return lp_build_format_swizzle_aos(format_desc
, &bld
, packed
);
415 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
&&
416 (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
417 format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) &&
418 format_desc
->block
.width
== 1 &&
419 format_desc
->block
.height
== 1 &&
420 util_is_power_of_two(format_desc
->block
.bits
) &&
421 format_desc
->block
.bits
<= 32 &&
422 format_desc
->is_bitmask
&&
423 !format_desc
->is_mixed
&&
424 (format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_UNSIGNED
||
425 format_desc
->channel
[1].type
== UTIL_FORMAT_TYPE_UNSIGNED
)) {
427 LLVMValueRef tmps
[LP_MAX_VECTOR_LENGTH
/4];
432 * Unpack a pixel at a time into a <4 x float> RGBA vector
435 for (k
= 0; k
< num_pixels
; ++k
) {
438 packed
= lp_build_gather_elem(builder
, num_pixels
,
439 format_desc
->block
.bits
, 32,
440 base_ptr
, offset
, k
);
442 tmps
[k
] = lp_build_unpack_arith_rgba_aos(builder
, format_desc
,
449 * TODO: We could avoid floating conversion for integer to
450 * integer conversions.
453 if (gallivm_debug
& GALLIVM_DEBUG_PERF
&& !type
.floating
) {
454 debug_printf("%s: unpacking %s with floating point\n",
455 __FUNCTION__
, format_desc
->short_name
);
458 lp_build_conv(builder
,
459 lp_float32_vec4_type(),
461 tmps
, num_pixels
, &res
, 1);
463 return lp_build_format_swizzle_aos(format_desc
, &bld
, res
);
467 * YUV / subsampled formats
470 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_SUBSAMPLED
) {
471 struct lp_type tmp_type
;
474 memset(&tmp_type
, 0, sizeof tmp_type
);
476 tmp_type
.length
= num_pixels
* 4;
477 tmp_type
.norm
= TRUE
;
479 tmp
= lp_build_fetch_subsampled_rgba_aos(builder
,
486 lp_build_conv(builder
,
494 * Fallback to util_format_description::fetch_rgba_8unorm().
497 if (format_desc
->fetch_rgba_8unorm
&&
498 !type
.floating
&& type
.width
== 8 && !type
.sign
&& type
.norm
) {
500 * Fallback to calling util_format_description::fetch_rgba_8unorm.
502 * This is definitely not the most efficient way of fetching pixels, as
503 * we miss the opportunity to do vectorization, but this it is a
504 * convenient for formats or scenarios for which there was no opportunity
505 * or incentive to optimize.
508 LLVMModuleRef module
= LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder
)));
510 LLVMTypeRef i8t
= LLVMInt8Type();
511 LLVMTypeRef pi8t
= LLVMPointerType(i8t
, 0);
512 LLVMTypeRef i32t
= LLVMInt32Type();
513 LLVMValueRef function
;
514 LLVMValueRef tmp_ptr
;
519 util_snprintf(name
, sizeof name
, "util_format_%s_fetch_rgba_8unorm",
520 format_desc
->short_name
);
522 if (gallivm_debug
& GALLIVM_DEBUG_PERF
) {
523 debug_printf("%s: falling back to %s\n", __FUNCTION__
, name
);
527 * Declare and bind format_desc->fetch_rgba_8unorm().
530 function
= LLVMGetNamedFunction(module
, name
);
532 LLVMTypeRef ret_type
;
533 LLVMTypeRef arg_types
[4];
534 LLVMTypeRef function_type
;
536 ret_type
= LLVMVoidType();
539 arg_types
[3] = arg_types
[2] = LLVMIntType(sizeof(unsigned) * 8);
540 function_type
= LLVMFunctionType(ret_type
, arg_types
, Elements(arg_types
), 0);
541 function
= LLVMAddFunction(module
, name
, function_type
);
543 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
544 LLVMSetLinkage(function
, LLVMExternalLinkage
);
546 assert(LLVMIsDeclaration(function
));
548 LLVMAddGlobalMapping(lp_build_engine
, function
,
549 func_to_pointer((func_pointer
)format_desc
->fetch_rgba_8unorm
));
552 tmp_ptr
= lp_build_alloca(builder
, i32t
, "");
554 res
= LLVMGetUndef(LLVMVectorType(i32t
, num_pixels
));
557 * Invoke format_desc->fetch_rgba_8unorm() for each pixel and insert the result
558 * in the SoA vectors.
561 for (k
= 0; k
< num_pixels
; ++k
) {
562 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), k
, 0);
563 LLVMValueRef args
[4];
565 args
[0] = LLVMBuildBitCast(builder
, tmp_ptr
, pi8t
, "");
566 args
[1] = lp_build_gather_elem_ptr(builder
, num_pixels
,
567 base_ptr
, offset
, k
);
569 if (num_pixels
== 1) {
574 args
[2] = LLVMBuildExtractElement(builder
, i
, index
, "");
575 args
[3] = LLVMBuildExtractElement(builder
, j
, index
, "");
578 LLVMBuildCall(builder
, function
, args
, Elements(args
), "");
580 tmp
= LLVMBuildLoad(builder
, tmp_ptr
, "");
582 if (num_pixels
== 1) {
586 res
= LLVMBuildInsertElement(builder
, res
, tmp
, index
, "");
590 /* Bitcast from <n x i32> to <4n x i8> */
591 res
= LLVMBuildBitCast(builder
, res
, bld
.vec_type
, "");
598 * Fallback to util_format_description::fetch_rgba_float().
601 if (format_desc
->fetch_rgba_float
) {
603 * Fallback to calling util_format_description::fetch_rgba_float.
605 * This is definitely not the most efficient way of fetching pixels, as
606 * we miss the opportunity to do vectorization, but this it is a
607 * convenient for formats or scenarios for which there was no opportunity
608 * or incentive to optimize.
611 LLVMModuleRef module
= LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder
)));
613 LLVMTypeRef f32t
= LLVMFloatType();
614 LLVMTypeRef f32x4t
= LLVMVectorType(f32t
, 4);
615 LLVMTypeRef pf32t
= LLVMPointerType(f32t
, 0);
616 LLVMValueRef function
;
617 LLVMValueRef tmp_ptr
;
618 LLVMValueRef tmps
[LP_MAX_VECTOR_LENGTH
/4];
622 util_snprintf(name
, sizeof name
, "util_format_%s_fetch_rgba_float",
623 format_desc
->short_name
);
625 if (gallivm_debug
& GALLIVM_DEBUG_PERF
) {
626 debug_printf("%s: falling back to %s\n", __FUNCTION__
, name
);
630 * Declare and bind format_desc->fetch_rgba_float().
633 function
= LLVMGetNamedFunction(module
, name
);
635 LLVMTypeRef ret_type
;
636 LLVMTypeRef arg_types
[4];
637 LLVMTypeRef function_type
;
639 ret_type
= LLVMVoidType();
640 arg_types
[0] = pf32t
;
641 arg_types
[1] = LLVMPointerType(LLVMInt8Type(), 0);
642 arg_types
[3] = arg_types
[2] = LLVMIntType(sizeof(unsigned) * 8);
643 function_type
= LLVMFunctionType(ret_type
, arg_types
, Elements(arg_types
), 0);
644 function
= LLVMAddFunction(module
, name
, function_type
);
646 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
647 LLVMSetLinkage(function
, LLVMExternalLinkage
);
649 assert(LLVMIsDeclaration(function
));
651 LLVMAddGlobalMapping(lp_build_engine
, function
,
652 func_to_pointer((func_pointer
)format_desc
->fetch_rgba_float
));
655 tmp_ptr
= lp_build_alloca(builder
, f32x4t
, "");
658 * Invoke format_desc->fetch_rgba_float() for each pixel and insert the result
659 * in the SoA vectors.
662 for (k
= 0; k
< num_pixels
; ++k
) {
663 LLVMValueRef args
[4];
665 args
[0] = LLVMBuildBitCast(builder
, tmp_ptr
, pf32t
, "");
666 args
[1] = lp_build_gather_elem_ptr(builder
, num_pixels
,
667 base_ptr
, offset
, k
);
669 if (num_pixels
== 1) {
674 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), k
, 0);
675 args
[2] = LLVMBuildExtractElement(builder
, i
, index
, "");
676 args
[3] = LLVMBuildExtractElement(builder
, j
, index
, "");
679 LLVMBuildCall(builder
, function
, args
, Elements(args
), "");
681 tmps
[k
] = LLVMBuildLoad(builder
, tmp_ptr
, "");
684 lp_build_conv(builder
,
685 lp_float32_vec4_type(),
687 tmps
, num_pixels
, &res
, 1);
693 return lp_build_undef(type
);