Merge branch '7.8'
[mesa.git] / src / gallium / auxiliary / gallivm / 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 static LLVMValueRef
38 lp_build_format_swizzle_chan_soa(struct lp_type type,
39 const LLVMValueRef *unswizzled,
40 enum util_format_swizzle swizzle)
41 {
42 switch (swizzle) {
43 case UTIL_FORMAT_SWIZZLE_X:
44 case UTIL_FORMAT_SWIZZLE_Y:
45 case UTIL_FORMAT_SWIZZLE_Z:
46 case UTIL_FORMAT_SWIZZLE_W:
47 return unswizzled[swizzle];
48 case UTIL_FORMAT_SWIZZLE_0:
49 return lp_build_zero(type);
50 case UTIL_FORMAT_SWIZZLE_1:
51 return lp_build_one(type);
52 case UTIL_FORMAT_SWIZZLE_NONE:
53 return lp_build_undef(type);
54 default:
55 assert(0);
56 return lp_build_undef(type);
57 }
58 }
59
60
61 void
62 lp_build_format_swizzle_soa(const struct util_format_description *format_desc,
63 struct lp_type type,
64 const LLVMValueRef *unswizzled,
65 LLVMValueRef *swizzled)
66 {
67 if(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
68 enum util_format_swizzle swizzle = format_desc->swizzle[0];
69 LLVMValueRef depth = lp_build_format_swizzle_chan_soa(type, unswizzled, swizzle);
70 swizzled[2] = swizzled[1] = swizzled[0] = depth;
71 swizzled[3] = lp_build_one(type);
72 }
73 else {
74 unsigned chan;
75 for (chan = 0; chan < 4; ++chan) {
76 enum util_format_swizzle swizzle = format_desc->swizzle[chan];
77 swizzled[chan] = lp_build_format_swizzle_chan_soa(type, unswizzled, swizzle);
78 }
79 }
80 }
81
82
83 void
84 lp_build_unpack_rgba_soa(LLVMBuilderRef builder,
85 const struct util_format_description *format_desc,
86 struct lp_type type,
87 LLVMValueRef packed,
88 LLVMValueRef *rgba)
89 {
90 LLVMValueRef inputs[4];
91 unsigned start;
92 unsigned chan;
93
94 /* FIXME: Support more formats */
95 assert(format_desc->layout == UTIL_FORMAT_LAYOUT_PLAIN);
96 assert(format_desc->block.width == 1);
97 assert(format_desc->block.height == 1);
98 assert(format_desc->block.bits <= 32);
99
100 /* Decode the input vector components */
101 start = 0;
102 for (chan = 0; chan < 4; ++chan) {
103 unsigned width = format_desc->channel[chan].size;
104 unsigned stop = start + width;
105 LLVMValueRef input;
106
107 input = packed;
108
109 switch(format_desc->channel[chan].type) {
110 case UTIL_FORMAT_TYPE_VOID:
111 input = NULL;
112 break;
113
114 case UTIL_FORMAT_TYPE_UNSIGNED:
115 if(type.floating) {
116 if(start)
117 input = LLVMBuildLShr(builder, input, lp_build_const_int_vec(type, start), "");
118 if(stop < format_desc->block.bits) {
119 unsigned mask = ((unsigned long long)1 << width) - 1;
120 input = LLVMBuildAnd(builder, input, lp_build_const_int_vec(type, mask), "");
121 }
122
123 if(format_desc->channel[chan].normalized)
124 input = lp_build_unsigned_norm_to_float(builder, width, type, input);
125 else
126 input = LLVMBuildFPToSI(builder, input, lp_build_vec_type(type), "");
127 }
128 else {
129 /* FIXME */
130 assert(0);
131 input = lp_build_undef(type);
132 }
133 break;
134
135 default:
136 /* fall through */
137 input = lp_build_undef(type);
138 break;
139 }
140
141 inputs[chan] = input;
142
143 start = stop;
144 }
145
146 lp_build_format_swizzle_soa(format_desc, type, inputs, rgba);
147 }