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"
36 #include "r300_fragprog_swizzle.h"
39 unsigned int rc_swizzle_to_writemask(unsigned int swz)
41 unsigned int mask = 0;
44 for(i = 0; i < 4; i++) {
45 mask |= 1 << GET_SWZ(swz, i);
52 rc_swizzle get_swz(unsigned int swz, rc_swizzle idx)
56 return GET_SWZ(swz, idx);
60 * The purpose of this function is to standardize the number channels used by
61 * swizzles. All swizzles regardless of what instruction they are a part of
62 * should have 4 channels initialized with values.
63 * @param channels The number of channels in initial_value that have a
65 * @return An initialized swizzle that has all of the unused channels set to
68 unsigned int rc_init_swizzle(unsigned int initial_value, unsigned int channels)
71 for (i = channels; i < 4; i++) {
72 SET_SWZ(initial_value, i, RC_SWIZZLE_UNUSED);
77 unsigned int combine_swizzles4(unsigned int src,
78 rc_swizzle swz_x, rc_swizzle swz_y, rc_swizzle swz_z, rc_swizzle swz_w)
82 ret |= get_swz(src, swz_x);
83 ret |= get_swz(src, swz_y) << 3;
84 ret |= get_swz(src, swz_z) << 6;
85 ret |= get_swz(src, swz_w) << 9;
90 unsigned int combine_swizzles(unsigned int src, unsigned int swz)
94 ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_X));
95 ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_Y)) << 3;
96 ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_Z)) << 6;
97 ret |= get_swz(src, GET_SWZ(swz, RC_SWIZZLE_W)) << 9;
103 * @param mask Must be either RC_MASK_X, RC_MASK_Y, RC_MASK_Z, or RC_MASK_W
105 rc_swizzle rc_mask_to_swizzle(unsigned int mask)
108 case RC_MASK_X: return RC_SWIZZLE_X;
109 case RC_MASK_Y: return RC_SWIZZLE_Y;
110 case RC_MASK_Z: return RC_SWIZZLE_Z;
111 case RC_MASK_W: return RC_SWIZZLE_W;
113 return RC_SWIZZLE_UNUSED;
116 /* Reorder mask bits according to swizzle. */
117 unsigned swizzle_mask(unsigned swizzle, unsigned mask)
120 for (unsigned chan = 0; chan < 4; ++chan) {
121 unsigned swz = GET_SWZ(swizzle, chan);
123 ret |= GET_BIT(mask, swz) << chan;
128 static unsigned int srcs_need_rewrite(const struct rc_opcode_info * info)
130 if (info->HasTexture) {
133 switch (info->Opcode) {
146 * This function moves the old swizzles to new channels using the values
147 * in the conversion swizzle. For example if the instruction writemask is
148 * changed from x to y, then conversion_swizzle should be y___ and this
149 * function will adjust the old argument swizzles (of the same instruction)
150 * to the new channels, so x___ will become _x__, etc...
152 * @param old_swizzle The swizzle to change
153 * @param conversion_swizzle Describes the conversion to perform on the swizzle
154 * @return A new swizzle
156 unsigned int rc_adjust_channels(
157 unsigned int old_swizzle,
158 unsigned int conversion_swizzle)
161 unsigned int new_swizzle = rc_init_swizzle(RC_SWIZZLE_UNUSED, 0);
162 for (i = 0; i < 4; i++) {
163 unsigned int new_chan = get_swz(conversion_swizzle, i);
164 if (new_chan == RC_SWIZZLE_UNUSED) {
167 SET_SWZ(new_swizzle, new_chan, GET_SWZ(old_swizzle, i));
172 static unsigned int rewrite_writemask(
173 unsigned int old_mask,
174 unsigned int conversion_swizzle)
176 unsigned int new_mask = 0;
179 for (i = 0; i < 4; i++) {
180 if (!GET_BIT(old_mask, i)
181 || GET_SWZ(conversion_swizzle, i) == RC_SWIZZLE_UNUSED) {
184 new_mask |= (1 << GET_SWZ(conversion_swizzle, i));
191 * This function rewrites the writemask of sub and adjusts the swizzles
192 * of all its source registers based on the conversion_swizzle.
193 * conversion_swizzle represents a mapping of the old writemask to the
194 * new writemask. For a detailed description of how conversion swizzles
195 * work see rc_rewrite_swizzle().
197 void rc_pair_rewrite_writemask(
198 struct rc_pair_sub_instruction * sub,
199 unsigned int conversion_swizzle)
201 const struct rc_opcode_info * info = rc_get_opcode_info(sub->Opcode);
204 sub->WriteMask = rewrite_writemask(sub->WriteMask, conversion_swizzle);
206 if (!srcs_need_rewrite(info)) {
210 for (i = 0; i < info->NumSrcRegs; i++) {
211 sub->Arg[i].Swizzle =
212 rc_adjust_channels(sub->Arg[i].Swizzle,
217 static void normal_rewrite_writemask_cb(
219 struct rc_instruction * inst,
220 struct rc_src_register * src)
222 unsigned int * conversion_swizzle = (unsigned int *)userdata;
223 src->Swizzle = rc_adjust_channels(src->Swizzle, *conversion_swizzle);
225 /* Per-channel negates are possible in vertex shaders,
226 * so we need to rewrite it properly as well. */
227 unsigned int new_negate = 0;
228 for (unsigned int i = 0; i < 4; i++) {
229 unsigned int new_chan = get_swz(*conversion_swizzle, i);
231 if (new_chan == RC_SWIZZLE_UNUSED)
234 if ((1 << i) & src->Negate)
235 new_negate |= 1 << new_chan;
237 src->Negate = new_negate;
241 * This function is the same as rc_pair_rewrite_writemask() except it
242 * operates on normal instructions.
244 void rc_normal_rewrite_writemask(
245 struct rc_instruction * inst,
246 unsigned int conversion_swizzle)
248 struct rc_sub_instruction * sub = &inst->U.I;
249 const struct rc_opcode_info * info = rc_get_opcode_info(sub->Opcode);
250 sub->DstReg.WriteMask =
251 rewrite_writemask(sub->DstReg.WriteMask, conversion_swizzle);
253 if (info->HasTexture) {
255 assert(sub->TexSwizzle == RC_SWIZZLE_XYZW);
256 for (i = 0; i < 4; i++) {
257 unsigned int swz = GET_SWZ(conversion_swizzle, i);
260 SET_SWZ(sub->TexSwizzle, swz, i);
264 if (!srcs_need_rewrite(info)) {
268 rc_for_all_reads_src(inst, normal_rewrite_writemask_cb,
269 &conversion_swizzle);
273 * This function replaces each value 'swz' in swizzle with the value of
274 * GET_SWZ(conversion_swizzle, swz). So, if you want to change all the X's
275 * in swizzle to Y, then conversion_swizzle should be Y___ (0xff9). If you want
276 * to change all the Y's in swizzle to X, then conversion_swizzle should be
277 * _X__ (0xfc7). If you want to change the Y's to X and the X's to Y, then
278 * conversion swizzle should be YX__ (0xfc1).
279 * @param swizzle The swizzle to change
280 * @param conversion_swizzle Describes the conversion to perform on the swizzle
281 * @return A converted swizzle
283 unsigned int rc_rewrite_swizzle(
284 unsigned int swizzle,
285 unsigned int conversion_swizzle)
288 unsigned int out_swizzle = swizzle;
290 for (chan = 0; chan < 4; chan++) {
291 unsigned int swz = GET_SWZ(swizzle, chan);
292 unsigned int new_swz;
294 SET_SWZ(out_swizzle, chan, swz);
296 new_swz = GET_SWZ(conversion_swizzle, swz);
297 if (new_swz != RC_SWIZZLE_UNUSED) {
298 SET_SWZ(out_swizzle, chan, new_swz);
300 SET_SWZ(out_swizzle, chan, swz);
308 * Left multiplication of a register with a swizzle
310 struct rc_src_register lmul_swizzle(unsigned int swizzle, struct rc_src_register srcreg)
312 struct rc_src_register tmp = srcreg;
316 for(i = 0; i < 4; ++i) {
317 rc_swizzle swz = GET_SWZ(swizzle, i);
319 tmp.Swizzle |= GET_SWZ(srcreg.Swizzle, swz) << (i*3);
320 tmp.Negate |= GET_BIT(srcreg.Negate, swz) << i;
322 tmp.Swizzle |= swz << (i*3);
328 void reset_srcreg(struct rc_src_register* reg)
330 memset(reg, 0, sizeof(struct rc_src_register));
331 reg->Swizzle = RC_SWIZZLE_XYZW;
334 unsigned int rc_src_reads_dst_mask(
335 rc_register_file src_file,
336 unsigned int src_idx,
337 unsigned int src_swz,
338 rc_register_file dst_file,
339 unsigned int dst_idx,
340 unsigned int dst_mask)
342 if (src_file != dst_file || src_idx != dst_idx) {
345 return dst_mask & rc_swizzle_to_writemask(src_swz);
349 * @return A bit mask specifying whether this swizzle will select from an RGB
350 * source, an Alpha source, or both.
352 unsigned int rc_source_type_swz(unsigned int swizzle)
355 unsigned int swz = RC_SWIZZLE_UNUSED;
356 unsigned int ret = RC_SOURCE_NONE;
358 for(chan = 0; chan < 4; chan++) {
359 swz = GET_SWZ(swizzle, chan);
360 if (swz == RC_SWIZZLE_W) {
361 ret |= RC_SOURCE_ALPHA;
362 } else if (swz == RC_SWIZZLE_X || swz == RC_SWIZZLE_Y
363 || swz == RC_SWIZZLE_Z) {
364 ret |= RC_SOURCE_RGB;
370 unsigned int rc_source_type_mask(unsigned int mask)
372 unsigned int ret = RC_SOURCE_NONE;
374 if (mask & RC_MASK_XYZ)
375 ret |= RC_SOURCE_RGB;
377 if (mask & RC_MASK_W)
378 ret |= RC_SOURCE_ALPHA;
384 rc_register_file File;
386 unsigned int SrcType;
387 unsigned int Swizzle;
390 struct can_use_presub_data {
391 struct src_select Selects[5];
392 unsigned int SelectCount;
393 const struct rc_src_register * ReplaceReg;
394 unsigned int ReplaceRemoved;
397 static void can_use_presub_data_add_select(
398 struct can_use_presub_data * data,
399 rc_register_file file,
401 unsigned int swizzle)
403 struct src_select * select;
405 select = &data->Selects[data->SelectCount++];
407 select->Index = index;
408 select->SrcType = rc_source_type_swz(swizzle);
409 select->Swizzle = swizzle;
413 * This callback function counts the number of sources in inst that are
414 * different from the sources in can_use_presub_data->RemoveSrcs.
416 static void can_use_presub_read_cb(
418 struct rc_instruction * inst,
419 struct rc_src_register * src)
421 struct can_use_presub_data * d = userdata;
423 if (!d->ReplaceRemoved && src == d->ReplaceReg) {
424 d->ReplaceRemoved = 1;
428 if (src->File == RC_FILE_NONE)
431 can_use_presub_data_add_select(d, src->File, src->Index,
435 unsigned int rc_inst_can_use_presub(
436 struct radeon_compiler * c,
437 struct rc_instruction * inst,
438 rc_presubtract_op presub_op,
439 unsigned int presub_writemask,
440 const struct rc_src_register * replace_reg,
441 const struct rc_src_register * presub_src0,
442 const struct rc_src_register * presub_src1)
444 struct can_use_presub_data d;
445 unsigned int num_presub_srcs;
447 const struct rc_opcode_info * info =
448 rc_get_opcode_info(inst->U.I.Opcode);
449 int rgb_count = 0, alpha_count = 0;
450 unsigned int src_type0, src_type1;
452 if (presub_op == RC_PRESUB_NONE) {
456 if (info->HasTexture) {
460 /* We can't allow constant swizzles from presubtract, because it is not possible
461 * to rewrite it to a native swizzle later. */
463 for (i = 0; i < 4; i++) {
464 rc_swizzle swz = GET_SWZ(replace_reg->Swizzle, i);
465 if (swz > RC_SWIZZLE_W && swz < RC_SWIZZLE_UNUSED)
470 /* We can't use more than one presubtract value in an
471 * instruction, unless the two prsubtract operations
472 * are the same and read from the same registers.
473 * XXX For now we will limit instructions to only one presubtract
475 if (inst->U.I.PreSub.Opcode != RC_PRESUB_NONE) {
479 memset(&d, 0, sizeof(d));
480 d.ReplaceReg = replace_reg;
482 rc_for_all_reads_src(inst, can_use_presub_read_cb, &d);
484 num_presub_srcs = rc_presubtract_src_reg_count(presub_op);
486 src_type0 = rc_source_type_swz(presub_src0->Swizzle);
487 can_use_presub_data_add_select(&d,
490 presub_src0->Swizzle);
492 if (num_presub_srcs > 1) {
493 src_type1 = rc_source_type_swz(presub_src1->Swizzle);
494 can_use_presub_data_add_select(&d,
497 presub_src1->Swizzle);
499 /* Even if both of the presub sources read from the same
500 * register, we still need to use 2 different source selects
501 * for them, so we need to increment the count to compensate.
503 if (presub_src0->File == presub_src1->File
504 && presub_src0->Index == presub_src1->Index) {
505 if (src_type0 & src_type1 & RC_SOURCE_RGB) {
508 if (src_type0 & src_type1 & RC_SOURCE_ALPHA) {
514 /* Count the number of source selects for Alpha and RGB. If we
515 * encounter two of the same source selects then we can ignore the
517 for (i = 0; i < d.SelectCount; i++) {
519 unsigned int src_type = d.Selects[i].SrcType;
520 for (j = i + 1; j < d.SelectCount; j++) {
521 /* Even if the sources are the same now, they will not be the
522 * same later, if we have to rewrite some non-native swizzle. */
524 !r300_swizzle_is_native_basic(d.Selects[i].Swizzle) ||
525 !r300_swizzle_is_native_basic(d.Selects[j].Swizzle)))
527 if (d.Selects[i].File == d.Selects[j].File
528 && d.Selects[i].Index == d.Selects[j].Index) {
529 src_type &= ~d.Selects[j].SrcType;
532 if (src_type & RC_SOURCE_RGB) {
536 if (src_type & RC_SOURCE_ALPHA) {
541 if (rgb_count > 3 || alpha_count > 3) {
550 unsigned int HasFileType;
551 rc_register_file File;
554 static void max_callback(
556 struct rc_instruction * inst,
557 rc_register_file file,
561 struct max_data * d = (struct max_data*)userdata;
562 if (file == d->File && (!d->HasFileType || index > d->Max)) {
569 * @return The maximum index of the specified register file used by the
572 int rc_get_max_index(
573 struct radeon_compiler * c,
574 rc_register_file file)
576 struct max_data data;
577 struct rc_instruction * inst;
579 data.HasFileType = 0;
581 for (inst = c->Program.Instructions.Next;
582 inst != &c->Program.Instructions;
584 rc_for_all_reads_mask(inst, max_callback, &data);
585 rc_for_all_writes_mask(inst, max_callback, &data);
587 if (!data.HasFileType) {
595 * This function removes a source from a pair instructions.
597 * @param src_type RC_SOURCE_RGB, RC_SOURCE_ALPHA, or both bitwise or'd
598 * @param source The index of the source to remove
601 void rc_pair_remove_src(
602 struct rc_instruction * inst,
603 unsigned int src_type,
606 if (src_type & RC_SOURCE_RGB) {
607 memset(&inst->U.P.RGB.Src[source], 0,
608 sizeof(struct rc_pair_instruction_source));
611 if (src_type & RC_SOURCE_ALPHA) {
612 memset(&inst->U.P.Alpha.Src[source], 0,
613 sizeof(struct rc_pair_instruction_source));
618 * @return RC_OPCODE_NOOP if inst is not a flow control instruction.
619 * @return The opcode of inst if it is a flow control instruction.
621 rc_opcode rc_get_flow_control_inst(struct rc_instruction * inst)
623 const struct rc_opcode_info * info;
624 if (inst->Type == RC_INSTRUCTION_NORMAL) {
625 info = rc_get_opcode_info(inst->U.I.Opcode);
627 info = rc_get_opcode_info(inst->U.P.RGB.Opcode);
628 /*A flow control instruction shouldn't have an alpha
630 assert(!info->IsFlowControl ||
631 inst->U.P.Alpha.Opcode == RC_OPCODE_NOP);
634 if (info->IsFlowControl)
637 return RC_OPCODE_NOP;
642 * @return The BGNLOOP instruction that starts the loop ended by endloop.
644 struct rc_instruction * rc_match_endloop(struct rc_instruction * endloop)
646 unsigned int endloop_count = 0;
647 struct rc_instruction * inst;
648 for (inst = endloop->Prev; inst != endloop; inst = inst->Prev) {
649 rc_opcode op = rc_get_flow_control_inst(inst);
650 if (op == RC_OPCODE_ENDLOOP) {
652 } else if (op == RC_OPCODE_BGNLOOP) {
653 if (endloop_count == 0) {
664 * @return The ENDLOOP instruction that ends the loop started by bgnloop.
666 struct rc_instruction * rc_match_bgnloop(struct rc_instruction * bgnloop)
668 unsigned int bgnloop_count = 0;
669 struct rc_instruction * inst;
670 for (inst = bgnloop->Next; inst!=bgnloop; inst = inst->Next) {
671 rc_opcode op = rc_get_flow_control_inst(inst);
672 if (op == RC_OPCODE_BGNLOOP) {
674 } else if (op == RC_OPCODE_ENDLOOP) {
675 if (bgnloop_count == 0) {
686 * @return A conversion swizzle for converting from old_mask->new_mask
688 unsigned int rc_make_conversion_swizzle(
689 unsigned int old_mask,
690 unsigned int new_mask)
692 unsigned int conversion_swizzle = rc_init_swizzle(RC_SWIZZLE_UNUSED, 0);
693 unsigned int old_idx;
694 unsigned int new_idx = 0;
695 for (old_idx = 0; old_idx < 4; old_idx++) {
696 if (!GET_BIT(old_mask, old_idx))
698 for ( ; new_idx < 4; new_idx++) {
699 if (GET_BIT(new_mask, new_idx)) {
700 SET_SWZ(conversion_swizzle, old_idx, new_idx);
706 return conversion_swizzle;
710 * @return 1 if the register contains an immediate value, 0 otherwise.
712 unsigned int rc_src_reg_is_immediate(
713 struct radeon_compiler * c,
717 return file == RC_FILE_CONSTANT &&
718 c->Program.Constants.Constants[index].Type == RC_CONSTANT_IMMEDIATE;
722 * @return The immediate value in the specified register.
724 float rc_get_constant_value(
725 struct radeon_compiler * c,
727 unsigned int swizzle,
732 int swz = GET_SWZ(swizzle, chan);
733 if(swz >= 4 || index >= c->Program.Constants.Count ){
734 rc_error(c, "get_constant_value: Can't find a value.\n");
737 if(GET_BIT(negate, chan)){
741 c->Program.Constants.Constants[index].u.Immediate[swz];
745 * This function returns the component value (RC_SWIZZLE_*) of the first used
746 * channel in the swizzle. This is only useful for scalar instructions that are
747 * known to use only one channel of the swizzle.
749 unsigned int rc_get_scalar_src_swz(unsigned int swizzle)
751 unsigned int swz, chan;
752 for (chan = 0; chan < 4; chan++) {
753 swz = GET_SWZ(swizzle, chan);
754 if (swz != RC_SWIZZLE_UNUSED) {
758 assert(swz != RC_SWIZZLE_UNUSED);
762 bool rc_inst_has_three_diff_temp_srcs(struct rc_instruction *inst)
764 return (inst->U.I.SrcReg[0].File == RC_FILE_TEMPORARY &&
765 inst->U.I.SrcReg[1].File == RC_FILE_TEMPORARY &&
766 inst->U.I.SrcReg[2].File == RC_FILE_TEMPORARY &&
767 inst->U.I.SrcReg[0].Index != inst->U.I.SrcReg[1].Index &&
768 inst->U.I.SrcReg[1].Index != inst->U.I.SrcReg[2].Index &&
769 inst->U.I.SrcReg[0].Index != inst->U.I.SrcReg[2].Index);