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.
52 * XXX: This is mostly for reference and testing -- operating a single pixel at
53 * a time is rarely if ever needed.
56 lp_build_unpack_rgba_aos(LLVMBuilderRef builder
,
57 const struct util_format_description
*desc
,
61 LLVMValueRef shifted
, casted
, scaled
, masked
;
62 LLVMValueRef shifts
[4];
63 LLVMValueRef masks
[4];
64 LLVMValueRef scales
[4];
65 LLVMValueRef swizzles
[4];
72 /* FIXME: Support more formats */
73 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
);
74 assert(desc
->block
.width
== 1);
75 assert(desc
->block
.height
== 1);
76 assert(desc
->block
.bits
<= 32);
78 type
= LLVMIntType(desc
->block
.bits
);
80 /* Do the intermediate integer computations with 32bit integers since it
81 * matches floating point size */
82 if (desc
->block
.bits
< 32)
83 packed
= LLVMBuildZExt(builder
, packed
, LLVMInt32Type(), "");
85 /* Broadcast the packed value to all four channels */
86 packed
= LLVMBuildInsertElement(builder
,
87 LLVMGetUndef(LLVMVectorType(LLVMInt32Type(), 4)),
89 LLVMConstNull(LLVMInt32Type()),
91 packed
= LLVMBuildShuffleVector(builder
,
93 LLVMGetUndef(LLVMVectorType(LLVMInt32Type(), 4)),
94 LLVMConstNull(LLVMVectorType(LLVMInt32Type(), 4)),
97 /* Initialize vector constants */
101 for (i
= 0; i
< 4; ++i
) {
102 unsigned bits
= desc
->channel
[i
].size
;
104 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
) {
105 shifts
[i
] = LLVMGetUndef(LLVMInt32Type());
106 masks
[i
] = LLVMConstNull(LLVMInt32Type());
107 scales
[i
] = LLVMConstNull(LLVMFloatType());
111 unsigned mask
= (1 << bits
) - 1;
113 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), "");
132 /* UIToFP can't be expressed in SSE2 */
133 casted
= LLVMBuildSIToFP(builder
, masked
, LLVMVectorType(LLVMFloatType(), 4), "");
136 scaled
= LLVMBuildMul(builder
, casted
, LLVMConstVector(scales
, 4), "");
140 for (i
= 0; i
< 4; ++i
)
141 aux
[i
] = LLVMGetUndef(LLVMFloatType());
143 for (i
= 0; i
< 4; ++i
) {
144 enum util_format_swizzle swizzle
= desc
->swizzle
[i
];
147 case UTIL_FORMAT_SWIZZLE_X
:
148 case UTIL_FORMAT_SWIZZLE_Y
:
149 case UTIL_FORMAT_SWIZZLE_Z
:
150 case UTIL_FORMAT_SWIZZLE_W
:
151 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), swizzle
, 0);
153 case UTIL_FORMAT_SWIZZLE_0
:
154 assert(empty_channel
>= 0);
155 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), empty_channel
, 0);
157 case UTIL_FORMAT_SWIZZLE_1
:
158 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), 4, 0);
159 aux
[0] = LLVMConstReal(LLVMFloatType(), 1.0);
161 case UTIL_FORMAT_SWIZZLE_NONE
:
162 swizzles
[i
] = LLVMGetUndef(LLVMFloatType());
168 return LLVMBuildShuffleVector(builder
, scaled
, LLVMConstVector(aux
, 4), LLVMConstVector(swizzles
, 4), "");
173 * Take a vector with packed pixels and unpack into a rgba8 vector.
175 * Formats with bit depth smaller than 32bits are accepted, but they must be
179 lp_build_unpack_rgba8_aos(LLVMBuilderRef builder
,
180 const struct util_format_description
*desc
,
184 struct lp_build_context bld
;
189 lp_build_context_init(&bld
, builder
, type
);
191 /* FIXME: Support more formats */
192 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
);
193 assert(desc
->block
.width
== 1);
194 assert(desc
->block
.height
== 1);
195 assert(desc
->block
.bits
<= 32);
197 assert(!type
.floating
);
200 assert(type
.width
== 8);
201 assert(type
.length
% 4 == 0);
204 for(i
= 0; i
< 4; ++i
) {
205 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
||
206 desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
);
207 if(desc
->channel
[0].size
!= 8)
213 * The pixel is already in a rgba8 format variant. All it is necessary
214 * is to swizzle the channels.
217 unsigned char swizzles
[4];
218 boolean zeros
[4]; /* bitwise AND mask */
219 boolean ones
[4]; /* bitwise OR mask */
220 boolean swizzles_needed
= FALSE
;
221 boolean zeros_needed
= FALSE
;
222 boolean ones_needed
= FALSE
;
224 for(i
= 0; i
< 4; ++i
) {
225 enum util_format_swizzle swizzle
= desc
->swizzle
[i
];
227 /* Initialize with the no-op case */
228 swizzles
[i
] = util_cpu_caps
.little_endian
? 3 - i
: i
;
233 case UTIL_FORMAT_SWIZZLE_X
:
234 case UTIL_FORMAT_SWIZZLE_Y
:
235 case UTIL_FORMAT_SWIZZLE_Z
:
236 case UTIL_FORMAT_SWIZZLE_W
:
237 if(swizzle
!= swizzles
[i
]) {
238 swizzles
[i
] = swizzle
;
239 swizzles_needed
= TRUE
;
242 case UTIL_FORMAT_SWIZZLE_0
:
246 case UTIL_FORMAT_SWIZZLE_1
:
250 case UTIL_FORMAT_SWIZZLE_NONE
:
259 res
= lp_build_swizzle1_aos(&bld
, res
, swizzles
);
262 /* Mask out zero channels */
263 LLVMValueRef mask
= lp_build_const_mask_aos(type
, zeros
);
264 res
= LLVMBuildAnd(builder
, res
, mask
, "");
268 /* Or one channels */
269 LLVMValueRef mask
= lp_build_const_mask_aos(type
, ones
);
270 res
= LLVMBuildOr(builder
, res
, mask
, "");
276 res
= lp_build_undef(type
);
284 * Pack a single pixel.
286 * @param rgba 4 float vector with the unpacked components.
288 * XXX: This is mostly for reference and testing -- operating a single pixel at
289 * a time is rarely if ever needed.
292 lp_build_pack_rgba_aos(LLVMBuilderRef builder
,
293 const struct util_format_description
*desc
,
297 LLVMValueRef packed
= NULL
;
298 LLVMValueRef swizzles
[4];
299 LLVMValueRef shifted
, casted
, scaled
, unswizzled
;
300 LLVMValueRef shifts
[4];
301 LLVMValueRef scales
[4];
306 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
);
307 assert(desc
->block
.width
== 1);
308 assert(desc
->block
.height
== 1);
310 type
= LLVMIntType(desc
->block
.bits
);
312 /* Unswizzle the color components into the source vector. */
313 for (i
= 0; i
< 4; ++i
) {
314 for (j
= 0; j
< 4; ++j
) {
315 if (desc
->swizzle
[j
] == i
)
319 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), j
, 0);
321 swizzles
[i
] = LLVMGetUndef(LLVMInt32Type());
324 unswizzled
= LLVMBuildShuffleVector(builder
, rgba
,
325 LLVMGetUndef(LLVMVectorType(LLVMFloatType(), 4)),
326 LLVMConstVector(swizzles
, 4), "");
330 for (i
= 0; i
< 4; ++i
) {
331 unsigned bits
= desc
->channel
[i
].size
;
333 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
) {
334 shifts
[i
] = LLVMGetUndef(LLVMInt32Type());
335 scales
[i
] = LLVMGetUndef(LLVMFloatType());
338 unsigned mask
= (1 << bits
) - 1;
340 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
343 shifts
[i
] = LLVMConstInt(LLVMInt32Type(), shift
, 0);
345 if (desc
->channel
[i
].normalized
) {
346 scales
[i
] = LLVMConstReal(LLVMFloatType(), mask
);
350 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0);
357 scaled
= LLVMBuildMul(builder
, unswizzled
, LLVMConstVector(scales
, 4), "");
361 casted
= LLVMBuildFPToSI(builder
, scaled
, LLVMVectorType(LLVMInt32Type(), 4), "");
363 shifted
= LLVMBuildShl(builder
, casted
, LLVMConstVector(shifts
, 4), "");
365 /* Bitwise or all components */
366 for (i
= 0; i
< 4; ++i
) {
367 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
368 LLVMValueRef component
= LLVMBuildExtractElement(builder
, shifted
, LLVMConstInt(LLVMInt32Type(), i
, 0), "");
370 packed
= LLVMBuildOr(builder
, packed
, component
, "");
377 packed
= LLVMGetUndef(LLVMInt32Type());
379 if (desc
->block
.bits
< 32)
380 packed
= LLVMBuildTrunc(builder
, packed
, type
, "");