llvmpipe: hook up some state, add stub line and point functions
[mesa.git] / src / gallium / drivers / llvmpipe / lp_bld_format_soa.c
1 /**************************************************************************
2 *
3 * Copyright 2009 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
29 #include "util/u_format.h"
30
31 #include "lp_bld_type.h"
32 #include "lp_bld_const.h"
33 #include "lp_bld_conv.h"
34 #include "lp_bld_format.h"
35
36
37 /**
38 * Gather elements from scatter positions in memory into a single vector.
39 *
40 * @param src_width src element width
41 * @param dst_width result element width (source will be expanded to fit)
42 * @param length length of the offsets,
43 * @param base_ptr base pointer, should be a i8 pointer type.
44 * @param offsets vector with offsets
45 */
46 LLVMValueRef
47 lp_build_gather(LLVMBuilderRef builder,
48 unsigned length,
49 unsigned src_width,
50 unsigned dst_width,
51 LLVMValueRef base_ptr,
52 LLVMValueRef offsets)
53 {
54 LLVMTypeRef src_type = LLVMIntType(src_width);
55 LLVMTypeRef src_ptr_type = LLVMPointerType(src_type, 0);
56 LLVMTypeRef dst_elem_type = LLVMIntType(dst_width);
57 LLVMTypeRef dst_vec_type = LLVMVectorType(dst_elem_type, length);
58 LLVMValueRef res;
59 unsigned i;
60
61 res = LLVMGetUndef(dst_vec_type);
62 for(i = 0; i < length; ++i) {
63 LLVMValueRef index = LLVMConstInt(LLVMInt32Type(), i, 0);
64 LLVMValueRef elem_offset;
65 LLVMValueRef elem_ptr;
66 LLVMValueRef elem;
67
68 elem_offset = LLVMBuildExtractElement(builder, offsets, index, "");
69 elem_ptr = LLVMBuildGEP(builder, base_ptr, &elem_offset, 1, "");
70 elem_ptr = LLVMBuildBitCast(builder, elem_ptr, src_ptr_type, "");
71 elem = LLVMBuildLoad(builder, elem_ptr, "");
72
73 assert(src_width <= dst_width);
74 if(src_width > dst_width)
75 elem = LLVMBuildTrunc(builder, elem, dst_elem_type, "");
76 if(src_width < dst_width)
77 elem = LLVMBuildZExt(builder, elem, dst_elem_type, "");
78
79 res = LLVMBuildInsertElement(builder, res, elem, index, "");
80 }
81
82 return res;
83 }
84
85
86 static LLVMValueRef
87 lp_build_format_swizzle(struct lp_type type,
88 const LLVMValueRef *inputs,
89 enum util_format_swizzle swizzle)
90 {
91 switch (swizzle) {
92 case UTIL_FORMAT_SWIZZLE_X:
93 case UTIL_FORMAT_SWIZZLE_Y:
94 case UTIL_FORMAT_SWIZZLE_Z:
95 case UTIL_FORMAT_SWIZZLE_W:
96 return inputs[swizzle];
97 case UTIL_FORMAT_SWIZZLE_0:
98 return lp_build_zero(type);
99 case UTIL_FORMAT_SWIZZLE_1:
100 return lp_build_one(type);
101 case UTIL_FORMAT_SWIZZLE_NONE:
102 return lp_build_undef(type);
103 default:
104 assert(0);
105 return lp_build_undef(type);
106 }
107 }
108
109
110 void
111 lp_build_unpack_rgba_soa(LLVMBuilderRef builder,
112 const struct util_format_description *format_desc,
113 struct lp_type type,
114 LLVMValueRef packed,
115 LLVMValueRef *rgba)
116 {
117 LLVMValueRef inputs[4];
118 unsigned start;
119 unsigned chan;
120
121 /* FIXME: Support more formats */
122 assert(format_desc->layout == UTIL_FORMAT_LAYOUT_ARITH ||
123 (format_desc->layout == UTIL_FORMAT_LAYOUT_ARRAY &&
124 format_desc->block.bits == format_desc->channel[0].size));
125 assert(format_desc->block.width == 1);
126 assert(format_desc->block.height == 1);
127 assert(format_desc->block.bits <= 32);
128
129 /* Decode the input vector components */
130 start = 0;
131 for (chan = 0; chan < 4; ++chan) {
132 unsigned width = format_desc->channel[chan].size;
133 unsigned stop = start + width;
134 LLVMValueRef input;
135
136 input = packed;
137
138 switch(format_desc->channel[chan].type) {
139 case UTIL_FORMAT_TYPE_VOID:
140 input = NULL;
141 break;
142
143 case UTIL_FORMAT_TYPE_UNSIGNED:
144 if(type.floating) {
145 if(start)
146 input = LLVMBuildLShr(builder, input, lp_build_int_const_scalar(type, start), "");
147 if(stop < format_desc->block.bits) {
148 unsigned mask = ((unsigned long long)1 << width) - 1;
149 input = LLVMBuildAnd(builder, input, lp_build_int_const_scalar(type, mask), "");
150 }
151
152 if(format_desc->channel[chan].normalized)
153 input = lp_build_unsigned_norm_to_float(builder, width, type, input);
154 else
155 input = LLVMBuildFPToSI(builder, input, lp_build_vec_type(type), "");
156 }
157 else {
158 /* FIXME */
159 assert(0);
160 input = lp_build_undef(type);
161 }
162 break;
163
164 default:
165 /* fall through */
166 input = lp_build_undef(type);
167 break;
168 }
169
170 inputs[chan] = input;
171
172 start = stop;
173 }
174
175 if(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
176 enum util_format_swizzle swizzle = format_desc->swizzle[0];
177 LLVMValueRef depth = lp_build_format_swizzle(type, inputs, swizzle);
178 rgba[2] = rgba[1] = rgba[0] = depth;
179 rgba[3] = lp_build_one(type);
180 }
181 else {
182 for (chan = 0; chan < 4; ++chan) {
183 enum util_format_swizzle swizzle = format_desc->swizzle[chan];
184 rgba[chan] = lp_build_format_swizzle(type, inputs, swizzle);
185 }
186 }
187 }
188
189
190 void
191 lp_build_load_rgba_soa(LLVMBuilderRef builder,
192 const struct util_format_description *format_desc,
193 struct lp_type type,
194 LLVMValueRef base_ptr,
195 LLVMValueRef offsets,
196 LLVMValueRef *rgba)
197 {
198 LLVMValueRef packed;
199
200 assert(format_desc->block.width == 1);
201 assert(format_desc->block.height == 1);
202 assert(format_desc->block.bits <= type.width);
203
204 packed = lp_build_gather(builder,
205 type.length, format_desc->block.bits, type.width,
206 base_ptr, offsets);
207
208 lp_build_unpack_rgba_soa(builder, format_desc, type, packed, rgba);
209 }