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 * Gather elements from scatter positions in memory into a single vector.
40 * @param src_width src element width
41 * @param dst_width result element width (source will be expanded to fit)
42 * @param length length of the offsets,
43 * @param base_ptr base pointer, should be a i8 pointer type.
44 * @param offsets vector with offsets
47 lp_build_gather(LLVMBuilderRef builder
,
51 LLVMValueRef base_ptr
,
54 LLVMTypeRef src_type
= LLVMIntType(src_width
);
55 LLVMTypeRef src_ptr_type
= LLVMPointerType(src_type
, 0);
56 LLVMTypeRef dst_elem_type
= LLVMIntType(dst_width
);
57 LLVMTypeRef dst_vec_type
= LLVMVectorType(dst_elem_type
, length
);
61 res
= LLVMGetUndef(dst_vec_type
);
62 for(i
= 0; i
< length
; ++i
) {
63 LLVMValueRef index
= LLVMConstInt(LLVMInt32Type(), i
, 0);
64 LLVMValueRef elem_offset
;
65 LLVMValueRef elem_ptr
;
68 elem_offset
= LLVMBuildExtractElement(builder
, offsets
, index
, "");
69 elem_ptr
= LLVMBuildGEP(builder
, base_ptr
, &elem_offset
, 1, "");
70 elem_ptr
= LLVMBuildBitCast(builder
, elem_ptr
, src_ptr_type
, "");
71 elem
= LLVMBuildLoad(builder
, elem_ptr
, "");
73 assert(src_width
<= dst_width
);
74 if(src_width
> dst_width
)
75 elem
= LLVMBuildTrunc(builder
, elem
, dst_elem_type
, "");
76 if(src_width
< dst_width
)
77 elem
= LLVMBuildZExt(builder
, elem
, dst_elem_type
, "");
79 res
= LLVMBuildInsertElement(builder
, res
, elem
, index
, "");
87 lp_build_format_swizzle(struct lp_type type
,
88 const LLVMValueRef
*inputs
,
89 enum util_format_swizzle swizzle
)
92 case UTIL_FORMAT_SWIZZLE_X
:
93 case UTIL_FORMAT_SWIZZLE_Y
:
94 case UTIL_FORMAT_SWIZZLE_Z
:
95 case UTIL_FORMAT_SWIZZLE_W
:
96 return inputs
[swizzle
];
97 case UTIL_FORMAT_SWIZZLE_0
:
98 return lp_build_zero(type
);
99 case UTIL_FORMAT_SWIZZLE_1
:
100 return lp_build_one(type
);
101 case UTIL_FORMAT_SWIZZLE_NONE
:
102 return lp_build_undef(type
);
105 return lp_build_undef(type
);
111 lp_build_unpack_rgba_soa(LLVMBuilderRef builder
,
112 const struct util_format_description
*format_desc
,
117 LLVMValueRef inputs
[4];
121 /* FIXME: Support more formats */
122 assert(format_desc
->layout
== UTIL_FORMAT_LAYOUT_ARITH
);
123 assert(format_desc
->block
.width
== 1);
124 assert(format_desc
->block
.height
== 1);
125 assert(format_desc
->block
.bits
<= 32);
127 /* Decode the input vector components */
129 for (chan
= 0; chan
< 4; ++chan
) {
130 unsigned width
= format_desc
->channel
[chan
].size
;
131 unsigned stop
= start
+ width
;
136 switch(format_desc
->channel
[chan
].type
) {
137 case UTIL_FORMAT_TYPE_VOID
:
141 case UTIL_FORMAT_TYPE_UNSIGNED
:
144 input
= LLVMBuildLShr(builder
, input
, lp_build_int_const_scalar(type
, start
), "");
145 if(stop
< format_desc
->block
.bits
) {
146 unsigned mask
= ((unsigned long long)1 << width
) - 1;
147 input
= LLVMBuildAnd(builder
, input
, lp_build_int_const_scalar(type
, mask
), "");
150 if(format_desc
->channel
[chan
].normalized
)
151 input
= lp_build_unsigned_norm_to_float(builder
, width
, type
, input
);
153 input
= LLVMBuildFPToSI(builder
, input
, lp_build_vec_type(type
), "");
158 input
= lp_build_undef(type
);
164 input
= lp_build_undef(type
);
168 inputs
[chan
] = input
;
173 if(format_desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
) {
174 enum util_format_swizzle swizzle
= format_desc
->swizzle
[0];
175 LLVMValueRef depth
= lp_build_format_swizzle(type
, inputs
, swizzle
);
176 rgba
[2] = rgba
[1] = rgba
[0] = depth
;
177 rgba
[3] = lp_build_one(type
);
180 for (chan
= 0; chan
< 4; ++chan
) {
181 enum util_format_swizzle swizzle
= format_desc
->swizzle
[chan
];
182 rgba
[chan
] = lp_build_format_swizzle(type
, inputs
, swizzle
);
189 lp_build_load_rgba_soa(LLVMBuilderRef builder
,
190 const struct util_format_description
*format_desc
,
192 LLVMValueRef base_ptr
,
193 LLVMValueRef offsets
,
198 assert(format_desc
->layout
== UTIL_FORMAT_LAYOUT_ARITH
);
199 assert(format_desc
->block
.width
== 1);
200 assert(format_desc
->block
.height
== 1);
201 assert(format_desc
->block
.bits
<= 32);
203 packed
= lp_build_gather(builder
,
204 type
.length
, format_desc
->block
.bits
, type
.width
,
207 lp_build_unpack_rgba_soa(builder
, format_desc
, type
, packed
, rgba
);