1 /**************************************************************************
3 * Copyright 2012 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 **************************************************************************/
28 #include "lp_bld_const.h"
29 #include "lp_bld_struct.h"
30 #include "lp_bld_format.h"
31 #include "lp_bld_debug.h"
32 #include "lp_bld_type.h"
33 #include "lp_bld_conv.h"
34 #include "lp_bld_pack.h"
35 #include "lp_bld_intr.h"
36 #include "lp_bld_gather.h"
38 #include "util/u_memory.h"
39 #include "util/u_format.h"
40 #include "pipe/p_state.h"
43 #ifdef PIPE_ARCH_BIG_ENDIAN
45 lp_build_read_int_bswap(struct gallivm_state
*gallivm
,
46 LLVMValueRef base_ptr
,
52 LLVMBuilderRef builder
= gallivm
->builder
;
53 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
54 LLVMValueRef ptr
= LLVMBuildGEP(builder
, base_ptr
, &index
, 1, "");
55 LLVMValueRef res
= LLVMBuildLoad(builder
, ptr
, "");
56 res
= lp_build_bswap(gallivm
, res
, lp_type_uint(src_width
));
57 return LLVMBuildBitCast(builder
, res
, dst_type
, "");
61 lp_build_fetch_read_big_endian(struct gallivm_state
*gallivm
,
62 struct lp_type src_type
,
63 LLVMValueRef base_ptr
)
65 LLVMBuilderRef builder
= gallivm
->builder
;
66 unsigned src_width
= src_type
.width
;
67 unsigned length
= src_type
.length
;
68 LLVMTypeRef src_elem_type
= LLVMIntTypeInContext(gallivm
->context
, src_width
);
69 LLVMTypeRef dst_elem_type
= lp_build_elem_type (gallivm
, src_type
);
70 LLVMTypeRef src_ptr_type
= LLVMPointerType(src_elem_type
, 0);
73 base_ptr
= LLVMBuildPointerCast(builder
, base_ptr
, src_ptr_type
, "");
76 res
= lp_build_read_int_bswap(gallivm
, base_ptr
, src_width
, src_elem_type
,
80 LLVMTypeRef dst_vec_type
= LLVMVectorType(dst_elem_type
, length
);
83 res
= LLVMGetUndef(dst_vec_type
);
84 for (i
= 0; i
< length
; ++i
) {
85 LLVMValueRef index
= lp_build_const_int32(gallivm
, i
);
86 LLVMValueRef elem
= lp_build_read_int_bswap(gallivm
, base_ptr
, src_width
,
87 src_elem_type
, i
, dst_elem_type
);
88 res
= LLVMBuildInsertElement(builder
, res
, elem
, index
, "");
97 * @brief lp_build_fetch_rgba_aos_array
99 * \param format_desc describes format of the image we're fetching from
100 * \param dst_type output type
101 * \param base_ptr address of the pixel block (or the texel if uncompressed)
102 * \param offset ptr offset
105 lp_build_fetch_rgba_aos_array(struct gallivm_state
*gallivm
,
106 const struct util_format_description
*format_desc
,
107 struct lp_type dst_type
,
108 LLVMValueRef base_ptr
,
111 struct lp_build_context bld
;
112 LLVMBuilderRef builder
= gallivm
->builder
;
113 LLVMTypeRef src_vec_type
;
114 LLVMValueRef ptr
, res
= NULL
;
115 struct lp_type src_type
;
116 boolean pure_integer
= format_desc
->channel
[0].pure_integer
;
117 struct lp_type tmp_type
;
119 lp_type_from_format_desc(&src_type
, format_desc
);
121 assert(src_type
.length
<= dst_type
.length
);
123 src_vec_type
= lp_build_vec_type(gallivm
, src_type
);
125 /* Read whole vector from memory, unaligned */
126 ptr
= LLVMBuildGEP(builder
, base_ptr
, &offset
, 1, "");
127 #ifdef PIPE_ARCH_BIG_ENDIAN
128 res
= lp_build_fetch_read_big_endian(gallivm
, src_type
, ptr
);
130 ptr
= LLVMBuildPointerCast(builder
, ptr
, LLVMPointerType(src_vec_type
, 0), "");
131 res
= LLVMBuildLoad(builder
, ptr
, "");
132 lp_set_load_alignment(res
, src_type
.width
/ 8);
135 /* Truncate doubles to float */
136 if (src_type
.floating
&& src_type
.width
== 64) {
138 src_vec_type
= lp_build_vec_type(gallivm
, src_type
);
140 res
= LLVMBuildFPTrunc(builder
, res
, src_vec_type
, "");
143 /* Expand to correct length */
144 if (src_type
.length
< dst_type
.length
) {
145 res
= lp_build_pad_vector(gallivm
, res
, dst_type
.length
);
146 src_type
.length
= dst_type
.length
;
151 /* some callers expect (fake) floats other real ints. */
152 tmp_type
.floating
= 0;
153 tmp_type
.sign
= src_type
.sign
;
156 /* Convert to correct format */
157 lp_build_conv(gallivm
, src_type
, tmp_type
, &res
, 1, &res
, 1);
160 lp_build_context_init(&bld
, gallivm
, tmp_type
);
161 res
= lp_build_format_swizzle_aos(format_desc
, &bld
, res
);
163 /* Bitcast to floats (for pure integers) when requested */
164 if (pure_integer
&& dst_type
.floating
) {
165 res
= LLVMBuildBitCast(builder
, res
, lp_build_vec_type(gallivm
, dst_type
), "");