2 * Copyright 2010 Tom Stellard <tstellar@gmail.com>
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:
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.
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.
32 #include "radeon_compiler_util.h"
34 #include "radeon_compiler.h"
35 #include "radeon_dataflow.h"
38 unsigned int rc_swizzle_to_writemask(unsigned int swz
)
40 unsigned int mask
= 0;
43 for(i
= 0; i
< 4; i
++) {
44 mask
|= 1 << GET_SWZ(swz
, i
);
51 rc_swizzle
get_swz(unsigned int swz
, rc_swizzle idx
)
55 return GET_SWZ(swz
, idx
);
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
)
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;
71 unsigned int combine_swizzles(unsigned int src
, unsigned int swz
)
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;
84 * @param mask Must be either RC_MASK_X, RC_MASK_Y, RC_MASK_Z, or RC_MASK_W
86 rc_swizzle
rc_mask_to_swizzle(unsigned int 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
;
94 return RC_SWIZZLE_UNUSED
;
97 /* Reorder mask bits according to swizzle. */
98 unsigned swizzle_mask(unsigned swizzle
, unsigned mask
)
101 for (unsigned chan
= 0; chan
< 4; ++chan
) {
102 unsigned swz
= GET_SWZ(swizzle
, chan
);
104 ret
|= GET_BIT(mask
, swz
) << chan
;
110 * Left multiplication of a register with a swizzle
112 struct rc_src_register
lmul_swizzle(unsigned int swizzle
, struct rc_src_register srcreg
)
114 struct rc_src_register tmp
= srcreg
;
118 for(i
= 0; i
< 4; ++i
) {
119 rc_swizzle swz
= GET_SWZ(swizzle
, i
);
121 tmp
.Swizzle
|= GET_SWZ(srcreg
.Swizzle
, swz
) << (i
*3);
122 tmp
.Negate
|= GET_BIT(srcreg
.Negate
, swz
) << i
;
124 tmp
.Swizzle
|= swz
<< (i
*3);
130 void reset_srcreg(struct rc_src_register
* reg
)
132 memset(reg
, 0, sizeof(struct rc_src_register
));
133 reg
->Swizzle
= RC_SWIZZLE_XYZW
;
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
)
144 if (src_file
!= dst_file
|| src_idx
!= dst_idx
) {
147 return dst_mask
& rc_swizzle_to_writemask(src_swz
);
150 unsigned int rc_source_type_swz(unsigned int swizzle
, unsigned int channels
)
153 unsigned int swz
= RC_SWIZZLE_UNUSED
;
154 unsigned int ret
= RC_SOURCE_NONE
;
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
;
168 unsigned int rc_source_type_mask(unsigned int mask
)
170 unsigned int ret
= RC_SOURCE_NONE
;
172 if (mask
& RC_MASK_XYZ
)
173 ret
|= RC_SOURCE_RGB
;
175 if (mask
& RC_MASK_W
)
176 ret
|= RC_SOURCE_ALPHA
;
181 struct can_use_presub_data
{
182 struct rc_src_register RemoveSrcs
[3];
183 unsigned int RGBCount
;
184 unsigned int AlphaCount
;
187 static void can_use_presub_read_cb(
189 struct rc_instruction
* inst
,
190 rc_register_file file
,
194 struct can_use_presub_data
* d
= userdata
;
195 unsigned int src_type
= rc_source_type_mask(mask
);
198 if (file
== RC_FILE_NONE
)
201 for(i
= 0; i
< 3; i
++) {
202 if (d
->RemoveSrcs
[i
].File
== file
203 && d
->RemoveSrcs
[i
].Index
== index
) {
205 ~rc_source_type_swz(d
->RemoveSrcs
[i
].Swizzle
, 4);
209 if (src_type
& RC_SOURCE_RGB
)
212 if (src_type
& RC_SOURCE_ALPHA
)
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
)
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
);
229 if (presub_op
== RC_PRESUB_NONE
) {
233 if (info
->HasTexture
) {
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
242 if (inst
->U
.I
.PreSub
.Opcode
!= RC_PRESUB_NONE
) {
246 memset(&d
, 0, sizeof(d
));
247 d
.RemoveSrcs
[0] = replace_reg
;
248 d
.RemoveSrcs
[1] = presub_src0
;
249 d
.RemoveSrcs
[2] = presub_src1
;
251 rc_for_all_reads_mask(inst
, can_use_presub_read_cb
, &d
);
253 num_presub_srcs
= rc_presubtract_src_reg_count(presub_op
);
255 if (d
.RGBCount
+ num_presub_srcs
> 3 || d
.AlphaCount
+ num_presub_srcs
> 3) {