gallivm: more integer texture format fetch fixes
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_format_aos_array.c
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/u_format.h"
40 #include "pipe/p_state.h"
41
42
43 #ifdef PIPE_ARCH_BIG_ENDIAN
44 static LLVMValueRef
45 lp_build_read_int_bswap(struct gallivm_state *gallivm,
46 LLVMValueRef base_ptr,
47 unsigned src_width,
48 LLVMTypeRef src_type,
49 unsigned i,
50 LLVMTypeRef dst_type)
51 {
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, "");
58 }
59
60 static LLVMValueRef
61 lp_build_fetch_read_big_endian(struct gallivm_state *gallivm,
62 struct lp_type src_type,
63 LLVMValueRef base_ptr)
64 {
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);
71 LLVMValueRef res;
72
73 base_ptr = LLVMBuildPointerCast(builder, base_ptr, src_ptr_type, "");
74 if (length == 1) {
75 /* Scalar */
76 res = lp_build_read_int_bswap(gallivm, base_ptr, src_width, src_elem_type,
77 0, dst_elem_type);
78 } else {
79 /* Vector */
80 LLVMTypeRef dst_vec_type = LLVMVectorType(dst_elem_type, length);
81 unsigned i;
82
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, "");
89 }
90 }
91
92 return res;
93 }
94 #endif
95
96 /**
97 * @brief lp_build_fetch_rgba_aos_array
98 *
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
103 */
104 LLVMValueRef
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,
109 LLVMValueRef offset)
110 {
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;
118
119 lp_type_from_format_desc(&src_type, format_desc);
120
121 assert(src_type.length <= dst_type.length);
122
123 src_vec_type = lp_build_vec_type(gallivm, src_type);
124
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);
129 #else
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);
133 #endif
134
135 /* Truncate doubles to float */
136 if (src_type.floating && src_type.width == 64) {
137 src_type.width = 32;
138 src_vec_type = lp_build_vec_type(gallivm, src_type);
139
140 res = LLVMBuildFPTrunc(builder, res, src_vec_type, "");
141 }
142
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;
147 }
148
149 tmp_type = dst_type;
150 if (pure_integer) {
151 /* some callers expect (fake) floats other real ints. */
152 tmp_type.floating = 0;
153 tmp_type.sign = src_type.sign;
154 }
155
156 /* Convert to correct format */
157 lp_build_conv(gallivm, src_type, tmp_type, &res, 1, &res, 1);
158
159 /* Swizzle it */
160 lp_build_context_init(&bld, gallivm, tmp_type);
161 res = lp_build_format_swizzle_aos(format_desc, &bld, res);
162
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), "");
166 }
167
168 return res;
169 }