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_init.h"
42 #include "lp_bld_type.h"
43 #include "lp_bld_format.h"
47 * Unpack a single pixel into its RGBA components.
49 * @param packed integer.
51 * @return RGBA in a 4 floats vector.
54 lp_build_unpack_rgba_aos(LLVMBuilderRef builder
,
55 const struct util_format_description
*desc
,
58 LLVMValueRef shifted
, casted
, scaled
, masked
;
59 LLVMValueRef shifts
[4];
60 LLVMValueRef masks
[4];
61 LLVMValueRef scales
[4];
62 LLVMValueRef swizzles
[4];
70 /* TODO: Support more formats */
71 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
);
72 assert(desc
->block
.width
== 1);
73 assert(desc
->block
.height
== 1);
74 assert(desc
->block
.bits
<= 32);
76 /* Do the intermediate integer computations with 32bit integers since it
77 * matches floating point size */
78 if (desc
->block
.bits
< 32)
79 packed
= LLVMBuildZExt(builder
, packed
, LLVMInt32Type(), "");
81 /* Broadcast the packed value to all four channels */
82 packed
= LLVMBuildInsertElement(builder
,
83 LLVMGetUndef(LLVMVectorType(LLVMInt32Type(), 4)),
85 LLVMConstNull(LLVMInt32Type()),
87 packed
= LLVMBuildShuffleVector(builder
,
89 LLVMGetUndef(LLVMVectorType(LLVMInt32Type(), 4)),
90 LLVMConstNull(LLVMVectorType(LLVMInt32Type(), 4)),
93 /* Initialize vector constants */
98 for (i
= 0; i
< 4; ++i
) {
99 unsigned bits
= desc
->channel
[i
].size
;
101 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
) {
102 shifts
[i
] = LLVMGetUndef(LLVMInt32Type());
103 masks
[i
] = LLVMConstNull(LLVMInt32Type());
104 scales
[i
] = LLVMConstNull(LLVMFloatType());
108 unsigned long long mask
= (1ULL << bits
) - 1;
110 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
116 shifts
[i
] = LLVMConstInt(LLVMInt32Type(), shift
, 0);
117 masks
[i
] = LLVMConstInt(LLVMInt32Type(), mask
, 0);
119 if (desc
->channel
[i
].normalized
) {
120 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0/mask
);
124 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0);
130 shifted
= LLVMBuildLShr(builder
, packed
, LLVMConstVector(shifts
, 4), "");
131 masked
= LLVMBuildAnd(builder
, shifted
, LLVMConstVector(masks
, 4), "");
133 /* UIToFP can't be expressed in SSE2 */
134 casted
= LLVMBuildSIToFP(builder
, masked
, LLVMVectorType(LLVMFloatType(), 4), "");
136 casted
= LLVMBuildUIToFP(builder
, masked
, LLVMVectorType(LLVMFloatType(), 4), "");
140 scaled
= LLVMBuildMul(builder
, casted
, LLVMConstVector(scales
, 4), "");
144 for (i
= 0; i
< 4; ++i
)
145 aux
[i
] = LLVMGetUndef(LLVMFloatType());
147 for (i
= 0; i
< 4; ++i
) {
148 enum util_format_swizzle swizzle
;
150 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
152 * For ZS formats do RGBA = ZZZ1
155 swizzle
= UTIL_FORMAT_SWIZZLE_1
;
156 } else if (desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_NONE
) {
157 swizzle
= UTIL_FORMAT_SWIZZLE_0
;
159 swizzle
= desc
->swizzle
[0];
162 swizzle
= desc
->swizzle
[i
];
166 case UTIL_FORMAT_SWIZZLE_X
:
167 case UTIL_FORMAT_SWIZZLE_Y
:
168 case UTIL_FORMAT_SWIZZLE_Z
:
169 case UTIL_FORMAT_SWIZZLE_W
:
170 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), swizzle
, 0);
172 case UTIL_FORMAT_SWIZZLE_0
:
173 assert(empty_channel
>= 0);
174 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), empty_channel
, 0);
176 case UTIL_FORMAT_SWIZZLE_1
:
177 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), 4, 0);
178 aux
[0] = LLVMConstReal(LLVMFloatType(), 1.0);
180 case UTIL_FORMAT_SWIZZLE_NONE
:
181 swizzles
[i
] = LLVMGetUndef(LLVMFloatType());
187 return LLVMBuildShuffleVector(builder
, scaled
, LLVMConstVector(aux
, 4), LLVMConstVector(swizzles
, 4), "");
192 * Pack a single pixel.
194 * @param rgba 4 float vector with the unpacked components.
196 * XXX: This is mostly for reference and testing -- operating a single pixel at
197 * a time is rarely if ever needed.
200 lp_build_pack_rgba_aos(LLVMBuilderRef builder
,
201 const struct util_format_description
*desc
,
205 LLVMValueRef packed
= NULL
;
206 LLVMValueRef swizzles
[4];
207 LLVMValueRef shifted
, casted
, scaled
, unswizzled
;
208 LLVMValueRef shifts
[4];
209 LLVMValueRef scales
[4];
214 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
);
215 assert(desc
->block
.width
== 1);
216 assert(desc
->block
.height
== 1);
218 type
= LLVMIntType(desc
->block
.bits
);
220 /* Unswizzle the color components into the source vector. */
221 for (i
= 0; i
< 4; ++i
) {
222 for (j
= 0; j
< 4; ++j
) {
223 if (desc
->swizzle
[j
] == i
)
227 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), j
, 0);
229 swizzles
[i
] = LLVMGetUndef(LLVMInt32Type());
232 unswizzled
= LLVMBuildShuffleVector(builder
, rgba
,
233 LLVMGetUndef(LLVMVectorType(LLVMFloatType(), 4)),
234 LLVMConstVector(swizzles
, 4), "");
238 for (i
= 0; i
< 4; ++i
) {
239 unsigned bits
= desc
->channel
[i
].size
;
241 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
) {
242 shifts
[i
] = LLVMGetUndef(LLVMInt32Type());
243 scales
[i
] = LLVMGetUndef(LLVMFloatType());
246 unsigned mask
= (1 << bits
) - 1;
248 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
251 shifts
[i
] = LLVMConstInt(LLVMInt32Type(), shift
, 0);
253 if (desc
->channel
[i
].normalized
) {
254 scales
[i
] = LLVMConstReal(LLVMFloatType(), mask
);
258 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0);
265 scaled
= LLVMBuildMul(builder
, unswizzled
, LLVMConstVector(scales
, 4), "");
269 casted
= LLVMBuildFPToSI(builder
, scaled
, LLVMVectorType(LLVMInt32Type(), 4), "");
271 shifted
= LLVMBuildShl(builder
, casted
, LLVMConstVector(shifts
, 4), "");
273 /* Bitwise or all components */
274 for (i
= 0; i
< 4; ++i
) {
275 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
276 LLVMValueRef component
= LLVMBuildExtractElement(builder
, shifted
, LLVMConstInt(LLVMInt32Type(), i
, 0), "");
278 packed
= LLVMBuildOr(builder
, packed
, component
, "");
285 packed
= LLVMGetUndef(LLVMInt32Type());
287 if (desc
->block
.bits
< 32)
288 packed
= LLVMBuildTrunc(builder
, packed
, type
, "");
295 * Fetch a pixel into a 4 float AoS.
297 * i and j are the sub-block pixel coordinates.
300 lp_build_fetch_rgba_aos(LLVMBuilderRef builder
,
301 const struct util_format_description
*format_desc
,
307 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
&&
308 (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
309 format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) &&
310 format_desc
->block
.width
== 1 &&
311 format_desc
->block
.height
== 1 &&
312 util_is_pot(format_desc
->block
.bits
) &&
313 format_desc
->block
.bits
<= 32 &&
314 format_desc
->is_bitmask
&&
315 !format_desc
->is_mixed
&&
316 (format_desc
->channel
[0].type
== UTIL_FORMAT_TYPE_UNSIGNED
||
317 format_desc
->channel
[1].type
== UTIL_FORMAT_TYPE_UNSIGNED
))
321 ptr
= LLVMBuildBitCast(builder
, ptr
,
322 LLVMPointerType(LLVMIntType(format_desc
->block
.bits
), 0) ,
325 packed
= LLVMBuildLoad(builder
, ptr
, "packed");
327 return lp_build_unpack_rgba_aos(builder
, format_desc
, packed
);
329 else if (format_desc
->fetch_rgba_float
) {
331 * Fallback to calling util_format_description::fetch_rgba_float.
333 * This is definitely not the most efficient way of fetching pixels, as
334 * we miss the opportunity to do vectorization, but this it is a
335 * convenient for formats or scenarios for which there was no opportunity
336 * or incentive to optimize.
339 LLVMModuleRef module
= LLVMGetGlobalParent(LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder
)));
341 LLVMValueRef function
;
343 LLVMValueRef args
[4];
345 util_snprintf(name
, sizeof name
, "util_format_%s_fetch_rgba_float",
346 format_desc
->short_name
);
349 * Declare and bind format_desc->fetch_rgba_float().
352 function
= LLVMGetNamedFunction(module
, name
);
354 LLVMTypeRef ret_type
;
355 LLVMTypeRef arg_types
[4];
356 LLVMTypeRef function_type
;
358 ret_type
= LLVMVoidType();
359 arg_types
[0] = LLVMPointerType(LLVMFloatType(), 0);
360 arg_types
[1] = LLVMPointerType(LLVMInt8Type(), 0);
361 arg_types
[3] = arg_types
[2] = LLVMIntType(sizeof(unsigned) * 8);
362 function_type
= LLVMFunctionType(ret_type
, arg_types
, Elements(arg_types
), 0);
363 function
= LLVMAddFunction(module
, name
, function_type
);
365 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
366 LLVMSetLinkage(function
, LLVMExternalLinkage
);
368 assert(LLVMIsDeclaration(function
));
370 LLVMAddGlobalMapping(lp_build_engine
, function
, format_desc
->fetch_rgba_float
);
374 * XXX: this should better go to the first block in the function
377 tmp
= LLVMBuildAlloca(builder
, LLVMVectorType(LLVMFloatType(), 4), "");
380 * Invoke format_desc->fetch_rgba_float() for each pixel and insert the result
381 * in the SoA vectors.
384 args
[0] = LLVMBuildBitCast(builder
, tmp
,
385 LLVMPointerType(LLVMFloatType(), 0), "");
390 LLVMBuildCall(builder
, function
, args
, 4, "");
392 return LLVMBuildLoad(builder
, tmp
, "");
396 return LLVMGetUndef(LLVMVectorType(LLVMFloatType(), 4));