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
);
59 * The purpose of this function is to standardize the number channels used by
60 * swizzles. All swizzles regardless of what instruction they are a part of
61 * should have 4 channels initialized with values.
62 * @param channels The number of channels in initial_value that have a
64 * @return An initialized swizzle that has all of the unused channels set to
67 unsigned int rc_init_swizzle(unsigned int initial_value
, unsigned int channels
)
70 for (i
= channels
; i
< 4; i
++) {
71 SET_SWZ(initial_value
, i
, RC_SWIZZLE_UNUSED
);
76 unsigned int combine_swizzles4(unsigned int src
,
77 rc_swizzle swz_x
, rc_swizzle swz_y
, rc_swizzle swz_z
, rc_swizzle swz_w
)
81 ret
|= get_swz(src
, swz_x
);
82 ret
|= get_swz(src
, swz_y
) << 3;
83 ret
|= get_swz(src
, swz_z
) << 6;
84 ret
|= get_swz(src
, swz_w
) << 9;
89 unsigned int combine_swizzles(unsigned int src
, unsigned int swz
)
93 ret
|= get_swz(src
, GET_SWZ(swz
, RC_SWIZZLE_X
));
94 ret
|= get_swz(src
, GET_SWZ(swz
, RC_SWIZZLE_Y
)) << 3;
95 ret
|= get_swz(src
, GET_SWZ(swz
, RC_SWIZZLE_Z
)) << 6;
96 ret
|= get_swz(src
, GET_SWZ(swz
, RC_SWIZZLE_W
)) << 9;
102 * @param mask Must be either RC_MASK_X, RC_MASK_Y, RC_MASK_Z, or RC_MASK_W
104 rc_swizzle
rc_mask_to_swizzle(unsigned int mask
)
107 case RC_MASK_X
: return RC_SWIZZLE_X
;
108 case RC_MASK_Y
: return RC_SWIZZLE_Y
;
109 case RC_MASK_Z
: return RC_SWIZZLE_Z
;
110 case RC_MASK_W
: return RC_SWIZZLE_W
;
112 return RC_SWIZZLE_UNUSED
;
115 /* Reorder mask bits according to swizzle. */
116 unsigned swizzle_mask(unsigned swizzle
, unsigned mask
)
119 for (unsigned chan
= 0; chan
< 4; ++chan
) {
120 unsigned swz
= GET_SWZ(swizzle
, chan
);
122 ret
|= GET_BIT(mask
, swz
) << chan
;
128 * Left multiplication of a register with a swizzle
130 struct rc_src_register
lmul_swizzle(unsigned int swizzle
, struct rc_src_register srcreg
)
132 struct rc_src_register tmp
= srcreg
;
136 for(i
= 0; i
< 4; ++i
) {
137 rc_swizzle swz
= GET_SWZ(swizzle
, i
);
139 tmp
.Swizzle
|= GET_SWZ(srcreg
.Swizzle
, swz
) << (i
*3);
140 tmp
.Negate
|= GET_BIT(srcreg
.Negate
, swz
) << i
;
142 tmp
.Swizzle
|= swz
<< (i
*3);
148 void reset_srcreg(struct rc_src_register
* reg
)
150 memset(reg
, 0, sizeof(struct rc_src_register
));
151 reg
->Swizzle
= RC_SWIZZLE_XYZW
;
154 unsigned int rc_src_reads_dst_mask(
155 rc_register_file src_file
,
156 unsigned int src_idx
,
157 unsigned int src_swz
,
158 rc_register_file dst_file
,
159 unsigned int dst_idx
,
160 unsigned int dst_mask
)
162 if (src_file
!= dst_file
|| src_idx
!= dst_idx
) {
165 return dst_mask
& rc_swizzle_to_writemask(src_swz
);
169 * @return A bit mask specifying whether this swizzle will select from an RGB
170 * source, an Alpha source, or both.
172 unsigned int rc_source_type_swz(unsigned int swizzle
)
175 unsigned int swz
= RC_SWIZZLE_UNUSED
;
176 unsigned int ret
= RC_SOURCE_NONE
;
178 for(chan
= 0; chan
< 4; chan
++) {
179 swz
= GET_SWZ(swizzle
, chan
);
180 if (swz
== RC_SWIZZLE_W
) {
181 ret
|= RC_SOURCE_ALPHA
;
182 } else if (swz
== RC_SWIZZLE_X
|| swz
== RC_SWIZZLE_Y
183 || swz
== RC_SWIZZLE_Z
) {
184 ret
|= RC_SOURCE_RGB
;
190 unsigned int rc_source_type_mask(unsigned int mask
)
192 unsigned int ret
= RC_SOURCE_NONE
;
194 if (mask
& RC_MASK_XYZ
)
195 ret
|= RC_SOURCE_RGB
;
197 if (mask
& RC_MASK_W
)
198 ret
|= RC_SOURCE_ALPHA
;
203 struct can_use_presub_data
{
204 struct rc_src_register RemoveSrcs
[3];
205 unsigned int RGBCount
;
206 unsigned int AlphaCount
;
209 static void can_use_presub_read_cb(
211 struct rc_instruction
* inst
,
212 rc_register_file file
,
216 struct can_use_presub_data
* d
= userdata
;
217 unsigned int src_type
= rc_source_type_mask(mask
);
220 if (file
== RC_FILE_NONE
)
223 for(i
= 0; i
< 3; i
++) {
224 if (d
->RemoveSrcs
[i
].File
== file
225 && d
->RemoveSrcs
[i
].Index
== index
) {
227 ~rc_source_type_swz(d
->RemoveSrcs
[i
].Swizzle
);
231 if (src_type
& RC_SOURCE_RGB
)
234 if (src_type
& RC_SOURCE_ALPHA
)
238 unsigned int rc_inst_can_use_presub(
239 struct rc_instruction
* inst
,
240 rc_presubtract_op presub_op
,
241 unsigned int presub_writemask
,
242 struct rc_src_register replace_reg
,
243 struct rc_src_register presub_src0
,
244 struct rc_src_register presub_src1
)
246 struct can_use_presub_data d
;
247 unsigned int num_presub_srcs
;
248 const struct rc_opcode_info
* info
=
249 rc_get_opcode_info(inst
->U
.I
.Opcode
);
251 if (presub_op
== RC_PRESUB_NONE
) {
255 if (info
->HasTexture
) {
259 /* We can't use more than one presubtract value in an
260 * instruction, unless the two prsubtract operations
261 * are the same and read from the same registers.
262 * XXX For now we will limit instructions to only one presubtract
264 if (inst
->U
.I
.PreSub
.Opcode
!= RC_PRESUB_NONE
) {
268 memset(&d
, 0, sizeof(d
));
269 d
.RemoveSrcs
[0] = replace_reg
;
270 d
.RemoveSrcs
[1] = presub_src0
;
271 d
.RemoveSrcs
[2] = presub_src1
;
273 rc_for_all_reads_mask(inst
, can_use_presub_read_cb
, &d
);
275 num_presub_srcs
= rc_presubtract_src_reg_count(presub_op
);
277 if (d
.RGBCount
+ num_presub_srcs
> 3 || d
.AlphaCount
+ num_presub_srcs
> 3) {