Merge branch 'lp-offset-twoside'
[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 unsigned int rc_src_reads_dst_mask(
52 rc_register_file src_file,
53 unsigned int src_idx,
54 unsigned int src_swz,
55 rc_register_file dst_file,
56 unsigned int dst_idx,
57 unsigned int dst_mask)
58 {
59 if (src_file != dst_file || src_idx != dst_idx) {
60 return RC_MASK_NONE;
61 }
62 return dst_mask & rc_swizzle_to_writemask(src_swz);
63 }
64
65 unsigned int rc_source_type_swz(unsigned int swizzle, unsigned int channels)
66 {
67 unsigned int chan;
68 unsigned int swz = RC_SWIZZLE_UNUSED;
69 unsigned int ret = RC_SOURCE_NONE;
70
71 for(chan = 0; chan < channels; chan++) {
72 swz = GET_SWZ(swizzle, chan);
73 if (swz == RC_SWIZZLE_W) {
74 ret |= RC_SOURCE_ALPHA;
75 } else if (swz == RC_SWIZZLE_X || swz == RC_SWIZZLE_Y
76 || swz == RC_SWIZZLE_Z) {
77 ret |= RC_SOURCE_RGB;
78 }
79 }
80 return ret;
81 }
82
83 unsigned int rc_source_type_mask(unsigned int mask)
84 {
85 unsigned int ret = RC_SOURCE_NONE;
86
87 if (mask & RC_MASK_XYZ)
88 ret |= RC_SOURCE_RGB;
89
90 if (mask & RC_MASK_W)
91 ret |= RC_SOURCE_ALPHA;
92
93 return ret;
94 }
95
96 struct can_use_presub_data {
97 struct rc_src_register RemoveSrcs[3];
98 unsigned int RGBCount;
99 unsigned int AlphaCount;
100 };
101
102 static void can_use_presub_read_cb(
103 void * userdata,
104 struct rc_instruction * inst,
105 rc_register_file file,
106 unsigned int index,
107 unsigned int mask)
108 {
109 struct can_use_presub_data * d = userdata;
110 unsigned int src_type = rc_source_type_mask(mask);
111 unsigned int i;
112
113 if (file == RC_FILE_NONE)
114 return;
115
116 for(i = 0; i < 3; i++) {
117 if (d->RemoveSrcs[i].File == file
118 && d->RemoveSrcs[i].Index == index) {
119 src_type &=
120 ~rc_source_type_swz(d->RemoveSrcs[i].Swizzle, 4);
121 }
122 }
123
124 if (src_type & RC_SOURCE_RGB)
125 d->RGBCount++;
126
127 if (src_type & RC_SOURCE_ALPHA)
128 d->AlphaCount++;
129 }
130
131 unsigned int rc_inst_can_use_presub(
132 struct rc_instruction * inst,
133 rc_presubtract_op presub_op,
134 unsigned int presub_writemask,
135 struct rc_src_register replace_reg,
136 struct rc_src_register presub_src0,
137 struct rc_src_register presub_src1)
138 {
139 struct can_use_presub_data d;
140 unsigned int num_presub_srcs;
141 unsigned int presub_src_type = rc_source_type_mask(presub_writemask);
142 const struct rc_opcode_info * info =
143 rc_get_opcode_info(inst->U.I.Opcode);
144
145 if (presub_op == RC_PRESUB_NONE) {
146 return 1;
147 }
148
149 if (info->HasTexture) {
150 return 0;
151 }
152
153 /* We can't use more than one presubtract value in an
154 * instruction, unless the two prsubtract operations
155 * are the same and read from the same registers.
156 * XXX For now we will limit instructions to only one presubtract
157 * value.*/
158 if (inst->U.I.PreSub.Opcode != RC_PRESUB_NONE) {
159 return 0;
160 }
161
162 memset(&d, 0, sizeof(d));
163 d.RemoveSrcs[0] = replace_reg;
164 d.RemoveSrcs[1] = presub_src0;
165 d.RemoveSrcs[2] = presub_src1;
166
167 rc_for_all_reads_mask(inst, can_use_presub_read_cb, &d);
168
169 num_presub_srcs = rc_presubtract_src_reg_count(presub_op);
170
171 if ((presub_src_type & RC_SOURCE_RGB)
172 && d.RGBCount + num_presub_srcs > 3) {
173 return 0;
174 }
175
176 if ((presub_src_type & RC_SOURCE_ALPHA)
177 && d.AlphaCount + num_presub_srcs > 3) {
178 return 0;
179 }
180
181 return 1;
182 }
183