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_cpu_detect.h"
37 #include "util/u_format.h"
39 #include "lp_bld_type.h"
40 #include "lp_bld_const.h"
41 #include "lp_bld_swizzle.h"
42 #include "lp_bld_format.h"
46 * Unpack a single pixel into its RGBA components.
48 * @param packed integer.
50 * @return RGBA in a 4 floats vector.
53 lp_build_unpack_rgba_aos(LLVMBuilderRef builder
,
54 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];
69 /* FIXME: Support more formats */
70 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
);
71 assert(desc
->block
.width
== 1);
72 assert(desc
->block
.height
== 1);
73 assert(desc
->block
.bits
<= 32);
75 type
= LLVMIntType(desc
->block
.bits
);
77 /* Do the intermediate integer computations with 32bit integers since it
78 * matches floating point size */
79 if (desc
->block
.bits
< 32)
80 packed
= LLVMBuildZExt(builder
, packed
, LLVMInt32Type(), "");
82 /* Broadcast the packed value to all four channels */
83 packed
= LLVMBuildInsertElement(builder
,
84 LLVMGetUndef(LLVMVectorType(LLVMInt32Type(), 4)),
86 LLVMConstNull(LLVMInt32Type()),
88 packed
= LLVMBuildShuffleVector(builder
,
90 LLVMGetUndef(LLVMVectorType(LLVMInt32Type(), 4)),
91 LLVMConstNull(LLVMVectorType(LLVMInt32Type(), 4)),
94 /* 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 mask
= (1 << bits
) - 1;
110 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
113 shifts
[i
] = LLVMConstInt(LLVMInt32Type(), shift
, 0);
114 masks
[i
] = LLVMConstInt(LLVMInt32Type(), mask
, 0);
116 if (desc
->channel
[i
].normalized
) {
117 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0/mask
);
121 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0);
127 shifted
= LLVMBuildLShr(builder
, packed
, LLVMConstVector(shifts
, 4), "");
128 masked
= LLVMBuildAnd(builder
, shifted
, LLVMConstVector(masks
, 4), "");
129 /* UIToFP can't be expressed in SSE2 */
130 casted
= LLVMBuildSIToFP(builder
, masked
, LLVMVectorType(LLVMFloatType(), 4), "");
133 scaled
= LLVMBuildMul(builder
, casted
, LLVMConstVector(scales
, 4), "");
137 for (i
= 0; i
< 4; ++i
)
138 aux
[i
] = LLVMGetUndef(LLVMFloatType());
140 for (i
= 0; i
< 4; ++i
) {
141 enum util_format_swizzle swizzle
= desc
->swizzle
[i
];
144 case UTIL_FORMAT_SWIZZLE_X
:
145 case UTIL_FORMAT_SWIZZLE_Y
:
146 case UTIL_FORMAT_SWIZZLE_Z
:
147 case UTIL_FORMAT_SWIZZLE_W
:
148 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), swizzle
, 0);
150 case UTIL_FORMAT_SWIZZLE_0
:
151 assert(empty_channel
>= 0);
152 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), empty_channel
, 0);
154 case UTIL_FORMAT_SWIZZLE_1
:
155 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), 4, 0);
156 aux
[0] = LLVMConstReal(LLVMFloatType(), 1.0);
158 case UTIL_FORMAT_SWIZZLE_NONE
:
159 swizzles
[i
] = LLVMGetUndef(LLVMFloatType());
165 return LLVMBuildShuffleVector(builder
, scaled
, LLVMConstVector(aux
, 4), LLVMConstVector(swizzles
, 4), "");
170 * Take a vector with packed pixels and unpack into a rgba8 vector.
172 * Formats with bit depth smaller than 32bits are accepted, but they must be
176 lp_build_unpack_rgba8_aos(LLVMBuilderRef builder
,
177 const struct util_format_description
*desc
,
181 struct lp_build_context bld
;
186 lp_build_context_init(&bld
, builder
, type
);
188 /* FIXME: Support more formats */
189 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
);
190 assert(desc
->block
.width
== 1);
191 assert(desc
->block
.height
== 1);
192 assert(desc
->block
.bits
<= 32);
194 assert(!type
.floating
);
197 assert(type
.width
== 8);
198 assert(type
.length
% 4 == 0);
201 for(i
= 0; i
< 4; ++i
) {
202 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
||
203 desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
);
204 if(desc
->channel
[0].size
!= 8)
210 * The pixel is already in a rgba8 format variant. All it is necessary
211 * is to swizzle the channels.
214 unsigned char swizzles
[4];
215 boolean zeros
[4]; /* bitwise AND mask */
216 boolean ones
[4]; /* bitwise OR mask */
217 boolean swizzles_needed
= FALSE
;
218 boolean zeros_needed
= FALSE
;
219 boolean ones_needed
= FALSE
;
221 for(i
= 0; i
< 4; ++i
) {
222 enum util_format_swizzle swizzle
= desc
->swizzle
[i
];
224 /* Initialize with the no-op case */
225 swizzles
[i
] = util_cpu_caps
.little_endian
? 3 - i
: i
;
230 case UTIL_FORMAT_SWIZZLE_X
:
231 case UTIL_FORMAT_SWIZZLE_Y
:
232 case UTIL_FORMAT_SWIZZLE_Z
:
233 case UTIL_FORMAT_SWIZZLE_W
:
234 if(swizzle
!= swizzles
[i
]) {
235 swizzles
[i
] = swizzle
;
236 swizzles_needed
= TRUE
;
239 case UTIL_FORMAT_SWIZZLE_0
:
243 case UTIL_FORMAT_SWIZZLE_1
:
247 case UTIL_FORMAT_SWIZZLE_NONE
:
256 res
= lp_build_swizzle1_aos(&bld
, res
, swizzles
);
259 /* Mask out zero channels */
260 LLVMValueRef mask
= lp_build_const_mask_aos(type
, zeros
);
261 res
= LLVMBuildAnd(builder
, res
, mask
, "");
265 /* Or one channels */
266 LLVMValueRef mask
= lp_build_const_mask_aos(type
, ones
);
267 res
= LLVMBuildOr(builder
, res
, mask
, "");
273 res
= lp_build_undef(type
);
281 * Pack a single pixel.
283 * @param rgba 4 float vector with the unpacked components.
285 * XXX: This is mostly for reference and testing -- operating a single pixel at
286 * a time is rarely if ever needed.
289 lp_build_pack_rgba_aos(LLVMBuilderRef builder
,
290 const struct util_format_description
*desc
,
294 LLVMValueRef packed
= NULL
;
295 LLVMValueRef swizzles
[4];
296 LLVMValueRef shifted
, casted
, scaled
, unswizzled
;
297 LLVMValueRef shifts
[4];
298 LLVMValueRef scales
[4];
303 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
);
304 assert(desc
->block
.width
== 1);
305 assert(desc
->block
.height
== 1);
307 type
= LLVMIntType(desc
->block
.bits
);
309 /* Unswizzle the color components into the source vector. */
310 for (i
= 0; i
< 4; ++i
) {
311 for (j
= 0; j
< 4; ++j
) {
312 if (desc
->swizzle
[j
] == i
)
316 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), j
, 0);
318 swizzles
[i
] = LLVMGetUndef(LLVMInt32Type());
321 unswizzled
= LLVMBuildShuffleVector(builder
, rgba
,
322 LLVMGetUndef(LLVMVectorType(LLVMFloatType(), 4)),
323 LLVMConstVector(swizzles
, 4), "");
327 for (i
= 0; i
< 4; ++i
) {
328 unsigned bits
= desc
->channel
[i
].size
;
330 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
) {
331 shifts
[i
] = LLVMGetUndef(LLVMInt32Type());
332 scales
[i
] = LLVMGetUndef(LLVMFloatType());
335 unsigned mask
= (1 << bits
) - 1;
337 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
340 shifts
[i
] = LLVMConstInt(LLVMInt32Type(), shift
, 0);
342 if (desc
->channel
[i
].normalized
) {
343 scales
[i
] = LLVMConstReal(LLVMFloatType(), mask
);
347 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0);
354 scaled
= LLVMBuildMul(builder
, unswizzled
, LLVMConstVector(scales
, 4), "");
358 casted
= LLVMBuildFPToSI(builder
, scaled
, LLVMVectorType(LLVMInt32Type(), 4), "");
360 shifted
= LLVMBuildShl(builder
, casted
, LLVMConstVector(shifts
, 4), "");
362 /* Bitwise or all components */
363 for (i
= 0; i
< 4; ++i
) {
364 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
365 LLVMValueRef component
= LLVMBuildExtractElement(builder
, shifted
, LLVMConstInt(LLVMInt32Type(), i
, 0), "");
367 packed
= LLVMBuildOr(builder
, packed
, component
, "");
374 packed
= LLVMGetUndef(LLVMInt32Type());
376 if (desc
->block
.bits
< 32)
377 packed
= LLVMBuildTrunc(builder
, packed
, type
, "");
384 * Fetch a pixel into a 4 float AoS.
387 lp_build_fetch_rgba_aos(LLVMBuilderRef builder
,
388 const struct util_format_description
*format_desc
,
391 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
&&
392 (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
393 format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) &&
394 format_desc
->block
.width
== 1 &&
395 format_desc
->block
.height
== 1 &&
396 format_desc
->block
.bits
<= 32 &&
397 format_desc
->channel
[0].type
!= UTIL_FORMAT_TYPE_FLOAT
)
401 ptr
= LLVMBuildBitCast(builder
, ptr
,
402 LLVMPointerType(LLVMIntType(format_desc
->block
.bits
), 0) , "");
404 packed
= LLVMBuildLoad(builder
, ptr
, "packed");
406 return lp_build_unpack_rgba_aos(builder
, format_desc
, packed
);
410 return LLVMGetUndef(LLVMVectorType(LLVMFloatType(), 4));