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_logic.h"
42 #include "lp_bld_swizzle.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.
53 * XXX: This is mostly for reference and testing -- operating a single pixel at
54 * a time is rarely if ever needed.
57 lp_build_unpack_rgba_aos(LLVMBuilderRef builder
,
58 const struct util_format_description
*desc
,
62 LLVMValueRef shifted
, casted
, scaled
, masked
;
63 LLVMValueRef shifts
[4];
64 LLVMValueRef masks
[4];
65 LLVMValueRef scales
[4];
66 LLVMValueRef swizzles
[4];
73 /* FIXME: Support more formats */
74 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_ARITH
);
75 assert(desc
->block
.width
== 1);
76 assert(desc
->block
.height
== 1);
77 assert(desc
->block
.bits
<= 32);
79 type
= LLVMIntType(desc
->block
.bits
);
81 /* Do the intermediate integer computations with 32bit integers since it
82 * matches floating point size */
83 if (desc
->block
.bits
< 32)
84 packed
= LLVMBuildZExt(builder
, packed
, LLVMInt32Type(), "");
86 /* Broadcast the packed value to all four channels */
87 packed
= LLVMBuildInsertElement(builder
,
88 LLVMGetUndef(LLVMVectorType(LLVMInt32Type(), 4)),
90 LLVMConstNull(LLVMInt32Type()),
92 packed
= LLVMBuildShuffleVector(builder
,
94 LLVMGetUndef(LLVMVectorType(LLVMInt32Type(), 4)),
95 LLVMConstNull(LLVMVectorType(LLVMInt32Type(), 4)),
98 /* Initialize vector constants */
102 for (i
= 0; i
< 4; ++i
) {
103 unsigned bits
= desc
->channel
[i
].size
;
105 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
) {
106 shifts
[i
] = LLVMGetUndef(LLVMInt32Type());
107 masks
[i
] = LLVMConstNull(LLVMInt32Type());
108 scales
[i
] = LLVMConstNull(LLVMFloatType());
112 unsigned mask
= (1 << bits
) - 1;
114 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
117 shifts
[i
] = LLVMConstInt(LLVMInt32Type(), shift
, 0);
118 masks
[i
] = LLVMConstInt(LLVMInt32Type(), mask
, 0);
120 if (desc
->channel
[i
].normalized
) {
121 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0/mask
);
125 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0);
131 shifted
= LLVMBuildLShr(builder
, packed
, LLVMConstVector(shifts
, 4), "");
132 masked
= LLVMBuildAnd(builder
, shifted
, LLVMConstVector(masks
, 4), "");
133 // UIToFP can't be expressed in SSE2
134 casted
= LLVMBuildSIToFP(builder
, masked
, LLVMVectorType(LLVMFloatType(), 4), "");
137 scaled
= LLVMBuildMul(builder
, casted
, LLVMConstVector(scales
, 4), "");
141 for (i
= 0; i
< 4; ++i
)
142 aux
[i
] = LLVMGetUndef(LLVMFloatType());
144 for (i
= 0; i
< 4; ++i
) {
145 enum util_format_swizzle swizzle
= desc
->swizzle
[i
];
148 case UTIL_FORMAT_SWIZZLE_X
:
149 case UTIL_FORMAT_SWIZZLE_Y
:
150 case UTIL_FORMAT_SWIZZLE_Z
:
151 case UTIL_FORMAT_SWIZZLE_W
:
152 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), swizzle
, 0);
154 case UTIL_FORMAT_SWIZZLE_0
:
155 assert(empty_channel
>= 0);
156 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), empty_channel
, 0);
158 case UTIL_FORMAT_SWIZZLE_1
:
159 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), 4, 0);
160 aux
[0] = LLVMConstReal(LLVMFloatType(), 1.0);
162 case UTIL_FORMAT_SWIZZLE_NONE
:
163 swizzles
[i
] = LLVMGetUndef(LLVMFloatType());
169 return LLVMBuildShuffleVector(builder
, scaled
, LLVMConstVector(aux
, 4), LLVMConstVector(swizzles
, 4), "");
174 * Take a vector with packed pixels and unpack into a rgba8 vector.
176 * Formats with bit depth smaller than 32bits are accepted, but they must be
180 lp_build_unpack_rgba8_aos(LLVMBuilderRef builder
,
181 const struct util_format_description
*desc
,
185 struct lp_build_context bld
;
190 lp_build_context_init(&bld
, builder
, type
);
192 /* FIXME: Support more formats */
193 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_ARITH
);
194 assert(desc
->block
.width
== 1);
195 assert(desc
->block
.height
== 1);
196 assert(desc
->block
.bits
<= 32);
198 assert(!type
.floating
);
201 assert(type
.width
== 8);
202 assert(type
.length
% 4 == 0);
205 for(i
= 0; i
< 4; ++i
) {
206 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
||
207 desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
);
208 if(desc
->channel
[0].size
!= 8)
214 * The pixel is already in a rgba8 format variant. All it is necessary
215 * is to swizzle the channels.
218 unsigned char swizzles
[4];
219 boolean zeros
[4]; /* bitwise AND mask */
220 boolean ones
[4]; /* bitwise OR mask */
221 boolean swizzles_needed
= FALSE
;
222 boolean zeros_needed
= FALSE
;
223 boolean ones_needed
= FALSE
;
225 for(i
= 0; i
< 4; ++i
) {
226 enum util_format_swizzle swizzle
= desc
->swizzle
[i
];
228 /* Initialize with the no-op case */
229 swizzles
[i
] = util_cpu_caps
.little_endian
? 3 - i
: i
;
234 case UTIL_FORMAT_SWIZZLE_X
:
235 case UTIL_FORMAT_SWIZZLE_Y
:
236 case UTIL_FORMAT_SWIZZLE_Z
:
237 case UTIL_FORMAT_SWIZZLE_W
:
238 if(swizzle
!= swizzles
[i
]) {
239 swizzles
[i
] = swizzle
;
240 swizzles_needed
= TRUE
;
243 case UTIL_FORMAT_SWIZZLE_0
:
247 case UTIL_FORMAT_SWIZZLE_1
:
251 case UTIL_FORMAT_SWIZZLE_NONE
:
260 res
= lp_build_swizzle1_aos(&bld
, res
, swizzles
);
263 /* Mask out zero channels */
264 LLVMValueRef mask
= lp_build_const_mask_aos(type
, zeros
);
265 res
= LLVMBuildAnd(builder
, res
, mask
, "");
269 /* Or one channels */
270 LLVMValueRef mask
= lp_build_const_mask_aos(type
, ones
);
271 res
= LLVMBuildOr(builder
, res
, mask
, "");
277 res
= lp_build_undef(type
);
285 * Pack a single pixel.
287 * @param rgba 4 float vector with the unpacked components.
289 * XXX: This is mostly for reference and testing -- operating a single pixel at
290 * a time is rarely if ever needed.
293 lp_build_pack_rgba_aos(LLVMBuilderRef builder
,
294 const struct util_format_description
*desc
,
298 LLVMValueRef packed
= NULL
;
299 LLVMValueRef swizzles
[4];
300 LLVMValueRef shifted
, casted
, scaled
, unswizzled
;
301 LLVMValueRef shifts
[4];
302 LLVMValueRef scales
[4];
307 assert(desc
->layout
== UTIL_FORMAT_LAYOUT_ARITH
);
308 assert(desc
->block
.width
== 1);
309 assert(desc
->block
.height
== 1);
311 type
= LLVMIntType(desc
->block
.bits
);
313 /* Unswizzle the color components into the source vector. */
314 for (i
= 0; i
< 4; ++i
) {
315 for (j
= 0; j
< 4; ++j
) {
316 if (desc
->swizzle
[j
] == i
)
320 swizzles
[i
] = LLVMConstInt(LLVMInt32Type(), j
, 0);
322 swizzles
[i
] = LLVMGetUndef(LLVMInt32Type());
325 unswizzled
= LLVMBuildShuffleVector(builder
, rgba
,
326 LLVMGetUndef(LLVMVectorType(LLVMFloatType(), 4)),
327 LLVMConstVector(swizzles
, 4), "");
331 for (i
= 0; i
< 4; ++i
) {
332 unsigned bits
= desc
->channel
[i
].size
;
334 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_VOID
) {
335 shifts
[i
] = LLVMGetUndef(LLVMInt32Type());
336 scales
[i
] = LLVMGetUndef(LLVMFloatType());
339 unsigned mask
= (1 << bits
) - 1;
341 assert(desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
);
344 shifts
[i
] = LLVMConstInt(LLVMInt32Type(), shift
, 0);
346 if (desc
->channel
[i
].normalized
) {
347 scales
[i
] = LLVMConstReal(LLVMFloatType(), mask
);
351 scales
[i
] = LLVMConstReal(LLVMFloatType(), 1.0);
358 scaled
= LLVMBuildMul(builder
, unswizzled
, LLVMConstVector(scales
, 4), "");
362 casted
= LLVMBuildFPToSI(builder
, scaled
, LLVMVectorType(LLVMInt32Type(), 4), "");
364 shifted
= LLVMBuildShl(builder
, casted
, LLVMConstVector(shifts
, 4), "");
366 /* Bitwise or all components */
367 for (i
= 0; i
< 4; ++i
) {
368 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
) {
369 LLVMValueRef component
= LLVMBuildExtractElement(builder
, shifted
, LLVMConstInt(LLVMInt32Type(), i
, 0), "");
371 packed
= LLVMBuildOr(builder
, packed
, component
, "");
378 packed
= LLVMGetUndef(LLVMInt32Type());
380 if (desc
->block
.bits
< 32)
381 packed
= LLVMBuildTrunc(builder
, packed
, type
, "");