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 */
52LLVMValueRef
53lp_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   ptr = LLVMBuildGEP(builder, base_ptr, &offset, 1, "");
82   ptr = LLVMBuildPointerCast(builder, ptr, LLVMPointerType(src_vec_type, 0), "");
83   res = LLVMBuildLoad(builder, ptr, "");
84   LLVMSetAlignment(res, src_type.width / 8);
85
86   /* Truncate doubles to float */
87   if (src_type.floating && src_type.width == 64) {
88      src_type.width = 32;
89      src_vec_type  = lp_build_vec_type(gallivm,  src_type);
90
91      res = LLVMBuildFPTrunc(builder, res, src_vec_type, "");
92   }
93
94   /* Expand to correct length */
95   if (src_type.length < dst_type.length) {
96      res = lp_build_pad_vector(gallivm, res, dst_type.length);
97      src_type.length = dst_type.length;
98   }
99
100   tmp_type = dst_type;
101   if (pure_integer) {
102       /* some callers expect (fake) floats other real ints. */
103      tmp_type.floating = 0;
104      tmp_type.sign = src_type.sign;
105   }
106
107   /* Convert to correct format */
108   lp_build_conv(gallivm, src_type, tmp_type, &res, 1, &res, 1);
109
110   /* Swizzle it */
111   lp_build_context_init(&bld, gallivm, tmp_type);
112   res = lp_build_format_swizzle_aos(format_desc, &bld, res);
113
114   /* Bitcast to floats (for pure integers) when requested */
115   if (pure_integer && dst_type.floating) {
116      res = LLVMBuildBitCast(builder, res, lp_build_vec_type(gallivm, dst_type), "");
117   }
118
119   return res;
120}
121