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"
30 #include "util/u_memory.h"
31 #include "util/u_string.h"
33 #include "lp_bld_type.h"
34 #include "lp_bld_const.h"
35 #include "lp_bld_conv.h"
36 #include "lp_bld_sample.h" /* for lp_build_gather */
37 #include "lp_bld_format.h"
41 lp_build_format_swizzle_chan_soa(struct lp_type type
,
42 const LLVMValueRef
*unswizzled
,
43 enum util_format_swizzle swizzle
)
46 case UTIL_FORMAT_SWIZZLE_X
:
47 case UTIL_FORMAT_SWIZZLE_Y
:
48 case UTIL_FORMAT_SWIZZLE_Z
:
49 case UTIL_FORMAT_SWIZZLE_W
:
50 return unswizzled
[swizzle
];
51 case UTIL_FORMAT_SWIZZLE_0
:
52 return lp_build_zero(type
);
53 case UTIL_FORMAT_SWIZZLE_1
:
54 return lp_build_one(type
);
55 case UTIL_FORMAT_SWIZZLE_NONE
:
56 return lp_build_undef(type
);
59 return lp_build_undef(type
);
65 lp_build_format_swizzle_soa(const struct util_format_description
*format_desc
,
67 const LLVMValueRef
*unswizzled
,
68 LLVMValueRef
*swizzled
)
70 if(format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
71 enum util_format_swizzle swizzle
= format_desc
->swizzle
[0];
72 LLVMValueRef depth
= lp_build_format_swizzle_chan_soa(type
, unswizzled
, swizzle
);
73 swizzled
[2] = swizzled
[1] = swizzled
[0] = depth
;
74 swizzled
[3] = lp_build_one(type
);
78 for (chan
= 0; chan
< 4; ++chan
) {
79 enum util_format_swizzle swizzle
= format_desc
->swizzle
[chan
];
80 swizzled
[chan
] = lp_build_format_swizzle_chan_soa(type
, unswizzled
, swizzle
);
87 * Unpack several pixels in SoA.
89 * It takes a vector of packed pixels:
91 * packed = {P0, P1, P2, P3, ..., Pn}
93 * And will produce four vectors:
95 * red = {R0, R1, R2, R3, ..., Rn}
96 * green = {G0, G1, G2, G3, ..., Gn}
97 * blue = {B0, B1, B2, B3, ..., Bn}
98 * alpha = {A0, A1, A2, A3, ..., An}
100 * It requires that a packed pixel fits into an element of the output
101 * channels. The common case is when converting pixel with a depth of 32 bit or
105 lp_build_unpack_rgba_soa(LLVMBuilderRef builder
,
106 const struct util_format_description
*format_desc
,
111 LLVMValueRef inputs
[4];
115 assert(format_desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
);
116 assert(format_desc
->block
.width
== 1);
117 assert(format_desc
->block
.height
== 1);
118 assert(format_desc
->block
.bits
<= type
.width
);
119 /* FIXME: Support more output types */
120 assert(type
.floating
);
121 assert(type
.width
== 32);
123 /* Decode the input vector components */
125 for (chan
= 0; chan
< format_desc
->nr_channels
; ++chan
) {
126 unsigned width
= format_desc
->channel
[chan
].size
;
127 unsigned stop
= start
+ width
;
132 switch(format_desc
->channel
[chan
].type
) {
133 case UTIL_FORMAT_TYPE_VOID
:
134 input
= lp_build_undef(type
);
137 case UTIL_FORMAT_TYPE_UNSIGNED
:
143 input
= LLVMBuildLShr(builder
, input
, lp_build_const_int_vec(type
, start
), "");
150 if (stop
< format_desc
->block
.bits
) {
151 unsigned mask
= ((unsigned long long)1 << width
) - 1;
152 input
= LLVMBuildAnd(builder
, input
, lp_build_const_int_vec(type
, mask
), "");
160 if(format_desc
->channel
[chan
].normalized
)
161 input
= lp_build_unsigned_norm_to_float(builder
, width
, type
, input
);
163 input
= LLVMBuildSIToFP(builder
, input
, lp_build_vec_type(type
), "");
168 input
= lp_build_undef(type
);
173 case UTIL_FORMAT_TYPE_SIGNED
:
175 * Align the sign bit first.
178 if (stop
< type
.width
) {
179 unsigned bits
= type
.width
- stop
;
180 LLVMValueRef bits_val
= lp_build_const_int_vec(type
, bits
);
181 input
= LLVMBuildShl(builder
, input
, bits_val
, "");
185 * Align the LSB (with an arithmetic shift to preserve the sign)
188 if (format_desc
->channel
[chan
].size
< type
.width
) {
189 unsigned bits
= type
.width
- format_desc
->channel
[chan
].size
;
190 LLVMValueRef bits_val
= lp_build_const_int_vec(type
, bits
);
191 input
= LLVMBuildAShr(builder
, input
, bits_val
, "");
199 input
= LLVMBuildSIToFP(builder
, input
, lp_build_vec_type(type
), "");
200 if (format_desc
->channel
[chan
].normalized
) {
201 double scale
= 1.0 / ((1 << (format_desc
->channel
[chan
].size
- 1)) - 1);
202 LLVMValueRef scale_val
= lp_build_const_vec(type
, scale
);
203 input
= LLVMBuildMul(builder
, input
, scale_val
, "");
209 input
= lp_build_undef(type
);
214 case UTIL_FORMAT_TYPE_FLOAT
:
218 assert(type
.width
== 32);
219 input
= LLVMBuildBitCast(builder
, input
, lp_build_vec_type(type
), "");
224 input
= lp_build_undef(type
);
228 case UTIL_FORMAT_TYPE_FIXED
:
230 double scale
= 1.0 / ((1 << (format_desc
->channel
[chan
].size
/2)) - 1);
231 LLVMValueRef scale_val
= lp_build_const_vec(type
, scale
);
232 input
= LLVMBuildSIToFP(builder
, input
, lp_build_vec_type(type
), "");
233 input
= LLVMBuildMul(builder
, input
, scale_val
, "");
238 input
= lp_build_undef(type
);
244 input
= lp_build_undef(type
);
248 inputs
[chan
] = input
;
253 lp_build_format_swizzle_soa(format_desc
, type
, inputs
, rgba
);
258 * Fetch a pixel into a SoA.
260 * i and j are the sub-block pixel coordinates.
263 lp_build_fetch_rgba_soa(LLVMBuilderRef builder
,
264 const struct util_format_description
*format_desc
,
266 LLVMValueRef base_ptr
,
273 if (format_desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
&&
274 (format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_RGB
||
275 format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) &&
276 format_desc
->block
.width
== 1 &&
277 format_desc
->block
.height
== 1 &&
278 format_desc
->block
.bits
<= type
.width
&&
279 (format_desc
->channel
[0].type
!= UTIL_FORMAT_TYPE_FLOAT
||
280 format_desc
->channel
[0].size
== 32))
283 * The packed pixel fits into an element of the destination format. Put
284 * the packed pixels into a vector and estract each component for all
285 * vector elements in parallel.
291 * gather the texels from the texture
293 packed
= lp_build_gather(builder
,
295 format_desc
->block
.bits
,
300 * convert texels to float rgba
302 lp_build_unpack_rgba_soa(builder
,
309 * Fallback to calling lp_build_fetch_rgba_aos for each pixel.
311 * This is not the most efficient way of fetching pixels, as
312 * we miss some opportunities to do vectorization, but this it is a
313 * convenient for formats or scenarios for which there was no opportunity
314 * or incentive to optimize.
319 assert(type
.floating
);
321 for (chan
= 0; chan
< 4; ++chan
) {
322 rgba
[chan
] = lp_build_undef(type
);
325 for(k
= 0; k
< type
.length
; ++k
) {
326 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), k
, 0);
327 LLVMValueRef offset_elem
;
329 LLVMValueRef i_elem
, j_elem
;
332 offset_elem
= LLVMBuildExtractElement(builder
, offset
, index
, "");
333 ptr
= LLVMBuildGEP(builder
, base_ptr
, &offset_elem
, 1, "");
335 i_elem
= LLVMBuildExtractElement(builder
, i
, index
, "");
336 j_elem
= LLVMBuildExtractElement(builder
, j
, index
, "");
338 tmp
= lp_build_fetch_rgba_aos(builder
, format_desc
, ptr
, i_elem
, j_elem
);
344 for (chan
= 0; chan
< 4; ++chan
) {
345 LLVMValueRef chan_val
= LLVMConstInt(LLVMInt32Type(), chan
, 0),
346 tmp_chan
= LLVMBuildExtractElement(builder
, tmp
, chan_val
, "");
347 rgba
[chan
] = LLVMBuildInsertElement(builder
, rgba
[chan
], tmp_chan
, index
, "");