1 /**************************************************************************
2 *
3 * Copyright 2012 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
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"
37
38 #include "util/u_memory.h"
39 #include "util/format/u_format.h"
40 #include "pipe/p_state.h"
41
42
43
44 /**
45 * @brief lp_build_fetch_rgba_aos_array
46 *
47 * \param format_desc describes format of the image we're fetching from
48 * \param dst_type output type
49 * \param base_ptr address of the pixel block (or the texel if uncompressed)
50 * \param offset ptr offset
51 */
52 LLVMValueRef
lp_build_fetch_rgba_aos_array(struct gallivm_state * gallivm,const struct util_format_description * format_desc,struct lp_type dst_type,LLVMValueRef base_ptr,LLVMValueRef offset)53 lp_build_fetch_rgba_aos_array(struct gallivm_state *gallivm,
54 const struct util_format_description *format_desc,
55 struct lp_type dst_type,
56 LLVMValueRef base_ptr,
57 LLVMValueRef offset)
58 {
59 struct lp_build_context bld;
60 LLVMBuilderRef builder = gallivm->builder;
61 LLVMTypeRef src_vec_type;
62 LLVMValueRef ptr, res = NULL;
63 struct lp_type src_type;
64 boolean pure_integer = format_desc->channel[0].pure_integer;
65 struct lp_type tmp_type;
66
67 lp_type_from_format_desc(&src_type, format_desc);
68
69 assert(src_type.length <= dst_type.length);
70
71 src_vec_type = lp_build_vec_type(gallivm, src_type);
72
73 /*
74 * Read whole vector from memory, unaligned.
75 * XXX: Note it's actually aligned to element type. Not sure if all
76 * callers are able to guarantee that (whereas for others, we should
77 * be able to use full alignment when there's 2 or 4 channels).
78 * (If all callers can guarantee element type alignment, we should
79 * relax alignment restrictions elsewhere.)
80 */
81 LLVMTypeRef byte_type = LLVMInt8TypeInContext(gallivm->context);
82 ptr = LLVMBuildGEP2(builder, byte_type, base_ptr, &offset, 1, "");
83 ptr = LLVMBuildPointerCast(builder, ptr, LLVMPointerType(src_vec_type, 0), "");
84 res = LLVMBuildLoad2(builder, src_vec_type, ptr, "");
85 LLVMSetAlignment(res, src_type.width / 8);
86
87 /* Truncate doubles to float */
88 if (src_type.floating && src_type.width == 64) {
89 src_type.width = 32;
90 src_vec_type = lp_build_vec_type(gallivm, src_type);
91
92 res = LLVMBuildFPTrunc(builder, res, src_vec_type, "");
93 }
94
95 /* Expand to correct length */
96 if (src_type.length < dst_type.length) {
97 res = lp_build_pad_vector(gallivm, res, dst_type.length);
98 src_type.length = dst_type.length;
99 }
100
101 tmp_type = dst_type;
102 if (pure_integer) {
103 /* some callers expect (fake) floats other real ints. */
104 tmp_type.floating = 0;
105 tmp_type.sign = src_type.sign;
106 }
107
108 /* Convert to correct format */
109 lp_build_conv(gallivm, src_type, tmp_type, &res, 1, &res, 1);
110
111 /* Swizzle it */
112 lp_build_context_init(&bld, gallivm, tmp_type);
113 res = lp_build_format_swizzle_aos(format_desc, &bld, res);
114
115 /* Bitcast to floats (for pure integers) when requested */
116 if (pure_integer && dst_type.floating) {
117 res = LLVMBuildBitCast(builder, res, lp_build_vec_type(gallivm, dst_type), "");
118 }
119
120 return res;
121 }
122