ee3ca86c000adbb9a19f3dc2d635636feb469e19
[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
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
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 /* Read whole vector from memory, unaligned */
74 ptr = LLVMBuildGEP(builder, base_ptr, &offset, 1, "");
75 ptr = LLVMBuildPointerCast(builder, ptr, LLVMPointerType(src_vec_type, 0), "");
76 res = LLVMBuildLoad(builder, ptr, "");
77 lp_set_load_alignment(res, src_type.width / 8);
78
79 /* Truncate doubles to float */
80 if (src_type.floating && src_type.width == 64) {
81 src_type.width = 32;
82 src_vec_type = lp_build_vec_type(gallivm, src_type);
83
84 res = LLVMBuildFPTrunc(builder, res, src_vec_type, "");
85 }
86
87 /* Expand to correct length */
88 if (src_type.length < dst_type.length) {
89 res = lp_build_pad_vector(gallivm, res, dst_type.length);
90 src_type.length = dst_type.length;
91 }
92
93 tmp_type = dst_type;
94 if (pure_integer) {
95 /* some callers expect (fake) floats other real ints. */
96 tmp_type.floating = 0;
97 tmp_type.sign = src_type.sign;
98 }
99
100 /* Convert to correct format */
101 lp_build_conv(gallivm, src_type, tmp_type, &res, 1, &res, 1);
102
103 /* Swizzle it */
104 lp_build_context_init(&bld, gallivm, tmp_type);
105 res = lp_build_format_swizzle_aos(format_desc, &bld, res);
106
107 /* Bitcast to floats (for pure integers) when requested */
108 if (pure_integer && dst_type.floating) {
109 res = LLVMBuildBitCast(builder, res, lp_build_vec_type(gallivm, dst_type), "");
110 }
111
112 return res;
113 }