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 **************************************************************************/
29 #include "util/u_format.h"
31 #include "lp_bld_type.h"
32 #include "lp_bld_const.h"
33 #include "lp_bld_conv.h"
34 #include "lp_bld_format.h"
38 lp_build_format_swizzle_chan_soa(struct lp_type type
,
39 const LLVMValueRef
*unswizzled
,
40 enum util_format_swizzle swizzle
)
43 case UTIL_FORMAT_SWIZZLE_X
:
44 case UTIL_FORMAT_SWIZZLE_Y
:
45 case UTIL_FORMAT_SWIZZLE_Z
:
46 case UTIL_FORMAT_SWIZZLE_W
:
47 return unswizzled
[swizzle
];
48 case UTIL_FORMAT_SWIZZLE_0
:
49 return lp_build_zero(type
);
50 case UTIL_FORMAT_SWIZZLE_1
:
51 return lp_build_one(type
);
52 case UTIL_FORMAT_SWIZZLE_NONE
:
53 return lp_build_undef(type
);
56 return lp_build_undef(type
);
62 lp_build_format_swizzle_soa(const struct util_format_description
*format_desc
,
64 const LLVMValueRef
*unswizzled
,
65 LLVMValueRef
*swizzled
)
67 if(format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
68 enum util_format_swizzle swizzle
= format_desc
->swizzle
[0];
69 LLVMValueRef depth
= lp_build_format_swizzle_chan_soa(type
, unswizzled
, swizzle
);
70 swizzled
[2] = swizzled
[1] = swizzled
[0] = depth
;
71 swizzled
[3] = lp_build_one(type
);
75 for (chan
= 0; chan
< 4; ++chan
) {
76 enum util_format_swizzle swizzle
= format_desc
->swizzle
[chan
];
77 swizzled
[chan
] = lp_build_format_swizzle_chan_soa(type
, unswizzled
, swizzle
);
84 * Unpack several pixels in SoA.
86 * It takes a vector of packed pixels:
88 * packed = {P0, P1, P2, P3, ..., Pn}
90 * And will produce four vectors:
92 * red = {R0, R1, R2, R3, ..., Rn}
93 * green = {G0, G1, G2, G3, ..., Gn}
94 * blue = {B0, B1, B2, B3, ..., Bn}
95 * alpha = {A0, A1, A2, A3, ..., An}
97 * It requires that a packed pixel fits into an element of the output
98 * channels. The common case is when converting pixel with a depth of 32 bit or
102 lp_build_unpack_rgba_soa(LLVMBuilderRef builder
,
103 const struct util_format_description
*format_desc
,
108 LLVMValueRef inputs
[4];
112 /* FIXME: Support more pixel formats */
113 assert(format_desc
->block
.width
== 1);
114 assert(format_desc
->block
.height
== 1);
115 assert(format_desc
->block
.bits
<= type
.width
);
116 /* FIXME: Support more output types */
117 assert(type
.floating
);
118 assert(type
.width
== 32);
120 /* Decode the input vector components */
122 for (chan
= 0; chan
< format_desc
->nr_channels
; ++chan
) {
123 unsigned width
= format_desc
->channel
[chan
].size
;
124 unsigned stop
= start
+ width
;
129 switch(format_desc
->channel
[chan
].type
) {
130 case UTIL_FORMAT_TYPE_VOID
:
131 input
= lp_build_undef(type
);
134 case UTIL_FORMAT_TYPE_UNSIGNED
:
140 input
= LLVMBuildLShr(builder
, input
, lp_build_const_int_vec(type
, start
), "");
147 if (stop
< format_desc
->block
.bits
) {
148 unsigned mask
= ((unsigned long long)1 << width
) - 1;
149 input
= LLVMBuildAnd(builder
, input
, lp_build_const_int_vec(type
, mask
), "");
157 if(format_desc
->channel
[chan
].normalized
)
158 input
= lp_build_unsigned_norm_to_float(builder
, width
, type
, input
);
160 input
= LLVMBuildSIToFP(builder
, input
, lp_build_vec_type(type
), "");
165 input
= lp_build_undef(type
);
170 case UTIL_FORMAT_TYPE_SIGNED
:
172 * Align the sign bit first.
175 if (stop
< type
.width
) {
176 unsigned bits
= type
.width
- stop
;
177 LLVMValueRef bits_val
= lp_build_const_int_vec(type
, bits
);
178 input
= LLVMBuildShl(builder
, input
, bits_val
, "");
182 * Align the LSB (with an arithmetic shift to preserve the sign)
185 if (format_desc
->channel
[chan
].size
< type
.width
) {
186 unsigned bits
= type
.width
- format_desc
->channel
[chan
].size
;
187 LLVMValueRef bits_val
= lp_build_const_int_vec(type
, bits
);
188 input
= LLVMBuildAShr(builder
, input
, bits_val
, "");
196 input
= LLVMBuildSIToFP(builder
, input
, lp_build_vec_type(type
), "");
197 if (format_desc
->channel
[chan
].normalized
) {
198 double scale
= 1.0 / ((1 << (format_desc
->channel
[chan
].size
- 1)) - 1);
199 LLVMValueRef scale_val
= lp_build_const_vec(type
, scale
);
200 input
= LLVMBuildMul(builder
, input
, scale_val
, "");
206 input
= lp_build_undef(type
);
211 case UTIL_FORMAT_TYPE_FLOAT
:
215 assert(type
.width
== 32);
216 input
= LLVMBuildBitCast(builder
, input
, lp_build_vec_type(type
), "");
221 input
= lp_build_undef(type
);
225 case UTIL_FORMAT_TYPE_FIXED
:
227 input
= lp_build_undef(type
);
232 input
= lp_build_undef(type
);
236 inputs
[chan
] = input
;
241 lp_build_format_swizzle_soa(format_desc
, type
, inputs
, rgba
);