2482fc68bebf9bd92c4d0e289d6535c864d3af10
[mesa.git] / src / mesa / drivers / dri / r300 / compiler / radeon_compiler_util.c
1 /*
2 * Copyright 2010 Tom Stellard <tstellar@gmail.com>
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a 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, sublicense, 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
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28 /**
29 * \file
30 */
31
32 #include "radeon_compiler_util.h"
33
34 #include "radeon_compiler.h"
35 #include "radeon_dataflow.h"
36 /**
37 */
38 unsigned int rc_swizzle_to_writemask(unsigned int swz)
39 {
40 unsigned int mask = 0;
41 unsigned int i;
42
43 for(i = 0; i < 4; i++) {
44 mask |= 1 << GET_SWZ(swz, i);
45 }
46 mask &= RC_MASK_XYZW;
47
48 return mask;
49 }
50
51 rc_swizzle get_swz(unsigned int swz, rc_swizzle idx)
52 {
53 if (idx & 0x4)
54 return idx;
55 return GET_SWZ(swz, idx);
56 }
57
58 unsigned int combine_swizzles4(unsigned int src,
59 rc_swizzle swz_x, rc_swizzle swz_y, rc_swizzle swz_z, rc_swizzle swz_w)
60 {
61 unsigned int ret = 0;
62
63 ret |= get_swz(src, swz_x);
64 ret |= get_swz(src, swz_y) << 3;
65 ret |= get_swz(src, swz_z) << 6;
66 ret |= get_swz(src, swz_w) << 9;
67
68 return ret;
69 }
70
71 unsigned int combine_swizzles(unsigned int src, unsigned int swz)
72 {
73 unsigned int ret = 0;
74
75 ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_X));
76 ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_Y)) << 3;
77 ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_Z)) << 6;
78 ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_W)) << 9;
79
80 return ret;
81 }
82
83 /**
84 * @param mask Must be either RC_MASK_X, RC_MASK_Y, RC_MASK_Z, or RC_MASK_W
85 */
86 rc_swizzle rc_mask_to_swizzle(unsigned int mask)
87 {
88 switch (mask) {
89 case RC_MASK_X: return RC_SWIZZLE_X;
90 case RC_MASK_Y: return RC_SWIZZLE_Y;
91 case RC_MASK_Z: return RC_SWIZZLE_Z;
92 case RC_MASK_W: return RC_SWIZZLE_W;
93 }
94 return RC_SWIZZLE_UNUSED;
95 }
96
97 /* Reorder mask bits according to swizzle. */
98 unsigned swizzle_mask(unsigned swizzle, unsigned mask)
99 {
100 unsigned ret = 0;
101 for (unsigned chan = 0; chan < 4; ++chan) {
102 unsigned swz = GET_SWZ(swizzle, chan);
103 if (swz < 4)
104 ret |= GET_BIT(mask, swz) << chan;
105 }
106 return ret;
107 }
108
109 /**
110 * Left multiplication of a register with a swizzle
111 */
112 struct rc_src_register lmul_swizzle(unsigned int swizzle, struct rc_src_register srcreg)
113 {
114 struct rc_src_register tmp = srcreg;
115 int i;
116 tmp.Swizzle = 0;
117 tmp.Negate = 0;
118 for(i = 0; i < 4; ++i) {
119 rc_swizzle swz = GET_SWZ(swizzle, i);
120 if (swz < 4) {
121 tmp.Swizzle |= GET_SWZ(srcreg.Swizzle, swz) << (i*3);
122 tmp.Negate |= GET_BIT(srcreg.Negate, swz) << i;
123 } else {
124 tmp.Swizzle |= swz << (i*3);
125 }
126 }
127 return tmp;
128 }
129
130 void reset_srcreg(struct rc_src_register* reg)
131 {
132 memset(reg, 0, sizeof(struct rc_src_register));
133 reg->Swizzle = RC_SWIZZLE_XYZW;
134 }
135
136 unsigned int rc_src_reads_dst_mask(
137 rc_register_file src_file,
138 unsigned int src_idx,
139 unsigned int src_swz,
140 rc_register_file dst_file,
141 unsigned int dst_idx,
142 unsigned int dst_mask)
143 {
144 if (src_file != dst_file || src_idx != dst_idx) {
145 return RC_MASK_NONE;
146 }
147 return dst_mask & rc_swizzle_to_writemask(src_swz);
148 }
149
150 unsigned int rc_source_type_swz(unsigned int swizzle, unsigned int channels)
151 {
152 unsigned int chan;
153 unsigned int swz = RC_SWIZZLE_UNUSED;
154 unsigned int ret = RC_SOURCE_NONE;
155
156 for(chan = 0; chan < channels; chan++) {
157 swz = GET_SWZ(swizzle, chan);
158 if (swz == RC_SWIZZLE_W) {
159 ret |= RC_SOURCE_ALPHA;
160 } else if (swz == RC_SWIZZLE_X || swz == RC_SWIZZLE_Y
161 || swz == RC_SWIZZLE_Z) {
162 ret |= RC_SOURCE_RGB;
163 }
164 }
165 return ret;
166 }
167
168 unsigned int rc_source_type_mask(unsigned int mask)
169 {
170 unsigned int ret = RC_SOURCE_NONE;
171
172 if (mask & RC_MASK_XYZ)
173 ret |= RC_SOURCE_RGB;
174
175 if (mask & RC_MASK_W)
176 ret |= RC_SOURCE_ALPHA;
177
178 return ret;
179 }
180
181 struct can_use_presub_data {
182 struct rc_src_register RemoveSrcs[3];
183 unsigned int RGBCount;
184 unsigned int AlphaCount;
185 };
186
187 static void can_use_presub_read_cb(
188 void * userdata,
189 struct rc_instruction * inst,
190 rc_register_file file,
191 unsigned int index,
192 unsigned int mask)
193 {
194 struct can_use_presub_data * d = userdata;
195 unsigned int src_type = rc_source_type_mask(mask);
196 unsigned int i;
197
198 if (file == RC_FILE_NONE)
199 return;
200
201 for(i = 0; i < 3; i++) {
202 if (d->RemoveSrcs[i].File == file
203 && d->RemoveSrcs[i].Index == index) {
204 src_type &=
205 ~rc_source_type_swz(d->RemoveSrcs[i].Swizzle, 4);
206 }
207 }
208
209 if (src_type & RC_SOURCE_RGB)
210 d->RGBCount++;
211
212 if (src_type & RC_SOURCE_ALPHA)
213 d->AlphaCount++;
214 }
215
216 unsigned int rc_inst_can_use_presub(
217 struct rc_instruction * inst,
218 rc_presubtract_op presub_op,
219 unsigned int presub_writemask,
220 struct rc_src_register replace_reg,
221 struct rc_src_register presub_src0,
222 struct rc_src_register presub_src1)
223 {
224 struct can_use_presub_data d;
225 unsigned int num_presub_srcs;
226 const struct rc_opcode_info * info =
227 rc_get_opcode_info(inst->U.I.Opcode);
228
229 if (presub_op == RC_PRESUB_NONE) {
230 return 1;
231 }
232
233 if (info->HasTexture) {
234 return 0;
235 }
236
237 /* We can't use more than one presubtract value in an
238 * instruction, unless the two prsubtract operations
239 * are the same and read from the same registers.
240 * XXX For now we will limit instructions to only one presubtract
241 * value.*/
242 if (inst->U.I.PreSub.Opcode != RC_PRESUB_NONE) {
243 return 0;
244 }
245
246 memset(&d, 0, sizeof(d));
247 d.RemoveSrcs[0] = replace_reg;
248 d.RemoveSrcs[1] = presub_src0;
249 d.RemoveSrcs[2] = presub_src1;
250
251 rc_for_all_reads_mask(inst, can_use_presub_read_cb, &d);
252
253 num_presub_srcs = rc_presubtract_src_reg_count(presub_op);
254
255 if (d.RGBCount + num_presub_srcs > 3 || d.AlphaCount + num_presub_srcs > 3) {
256 return 0;
257 }
258
259 return 1;
260 }
261