Merge remote branch 'origin/7.8'
[mesa.git] / src / mesa / drivers / dri / r300 / compiler / radeon_pair_translate.c
1 /*
2 * Copyright (C) 2009 Nicolai Haehnle.
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 #include "radeon_program_pair.h"
29
30 #include "radeon_compiler.h"
31
32
33 /**
34 * Finally rewrite ADD, MOV, MUL as the appropriate native instruction
35 * and reverse the order of arguments for CMP.
36 */
37 static void final_rewrite(struct rc_sub_instruction *inst)
38 {
39 struct rc_src_register tmp;
40
41 switch(inst->Opcode) {
42 case RC_OPCODE_ADD:
43 inst->SrcReg[2] = inst->SrcReg[1];
44 inst->SrcReg[1].File = RC_FILE_NONE;
45 inst->SrcReg[1].Swizzle = RC_SWIZZLE_1111;
46 inst->SrcReg[1].Negate = RC_MASK_NONE;
47 inst->Opcode = RC_OPCODE_MAD;
48 break;
49 case RC_OPCODE_CMP:
50 tmp = inst->SrcReg[2];
51 inst->SrcReg[2] = inst->SrcReg[0];
52 inst->SrcReg[0] = tmp;
53 break;
54 case RC_OPCODE_MOV:
55 /* AMD say we should use CMP.
56 * However, when we transform
57 * KIL -r0;
58 * into
59 * CMP tmp, -r0, -r0, 0;
60 * KIL tmp;
61 * we get incorrect behaviour on R500 when r0 == 0.0.
62 * It appears that the R500 KIL hardware treats -0.0 as less
63 * than zero.
64 */
65 inst->SrcReg[1].File = RC_FILE_NONE;
66 inst->SrcReg[1].Swizzle = RC_SWIZZLE_1111;
67 inst->SrcReg[2].File = RC_FILE_NONE;
68 inst->SrcReg[2].Swizzle = RC_SWIZZLE_0000;
69 inst->Opcode = RC_OPCODE_MAD;
70 break;
71 case RC_OPCODE_MUL:
72 inst->SrcReg[2].File = RC_FILE_NONE;
73 inst->SrcReg[2].Swizzle = RC_SWIZZLE_0000;
74 inst->Opcode = RC_OPCODE_MAD;
75 break;
76 default:
77 /* nothing to do */
78 break;
79 }
80 }
81
82
83 /**
84 * Classify an instruction according to which ALUs etc. it needs
85 */
86 static void classify_instruction(struct rc_sub_instruction * inst,
87 int * needrgb, int * needalpha, int * istranscendent)
88 {
89 *needrgb = (inst->DstReg.WriteMask & RC_MASK_XYZ) ? 1 : 0;
90 *needalpha = (inst->DstReg.WriteMask & RC_MASK_W) ? 1 : 0;
91 *istranscendent = 0;
92
93 if (inst->WriteALUResult == RC_ALURESULT_X)
94 *needrgb = 1;
95 else if (inst->WriteALUResult == RC_ALURESULT_W)
96 *needalpha = 1;
97
98 switch(inst->Opcode) {
99 case RC_OPCODE_ADD:
100 case RC_OPCODE_CMP:
101 case RC_OPCODE_DDX:
102 case RC_OPCODE_DDY:
103 case RC_OPCODE_FRC:
104 case RC_OPCODE_MAD:
105 case RC_OPCODE_MAX:
106 case RC_OPCODE_MIN:
107 case RC_OPCODE_MOV:
108 case RC_OPCODE_MUL:
109 break;
110 case RC_OPCODE_COS:
111 case RC_OPCODE_EX2:
112 case RC_OPCODE_LG2:
113 case RC_OPCODE_RCP:
114 case RC_OPCODE_RSQ:
115 case RC_OPCODE_SIN:
116 *istranscendent = 1;
117 *needalpha = 1;
118 break;
119 case RC_OPCODE_DP4:
120 *needalpha = 1;
121 /* fall through */
122 case RC_OPCODE_DP3:
123 *needrgb = 1;
124 break;
125 default:
126 break;
127 }
128 }
129
130
131 /**
132 * Fill the given ALU instruction's opcodes and source operands into the given pair,
133 * if possible.
134 */
135 static void set_pair_instruction(struct r300_fragment_program_compiler *c,
136 struct rc_pair_instruction * pair,
137 struct rc_sub_instruction * inst)
138 {
139 memset(pair, 0, sizeof(struct rc_pair_instruction));
140
141 int needrgb, needalpha, istranscendent;
142 classify_instruction(inst, &needrgb, &needalpha, &istranscendent);
143
144 if (needrgb) {
145 if (istranscendent)
146 pair->RGB.Opcode = RC_OPCODE_REPL_ALPHA;
147 else
148 pair->RGB.Opcode = inst->Opcode;
149 if (inst->SaturateMode == RC_SATURATE_ZERO_ONE)
150 pair->RGB.Saturate = 1;
151 }
152 if (needalpha) {
153 pair->Alpha.Opcode = inst->Opcode;
154 if (inst->SaturateMode == RC_SATURATE_ZERO_ONE)
155 pair->Alpha.Saturate = 1;
156 }
157
158 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->Opcode);
159 int nargs = opcode->NumSrcRegs;
160 int i;
161
162 for(i = 0; i < opcode->NumSrcRegs; ++i) {
163 int source;
164 if (needrgb && !istranscendent) {
165 unsigned int srcrgb = 0;
166 unsigned int srcalpha = 0;
167 int j;
168 for(j = 0; j < 3; ++j) {
169 unsigned int swz = GET_SWZ(inst->SrcReg[i].Swizzle, j);
170 if (swz < 3)
171 srcrgb = 1;
172 else if (swz < 4)
173 srcalpha = 1;
174 }
175 source = rc_pair_alloc_source(pair, srcrgb, srcalpha,
176 inst->SrcReg[i].File, inst->SrcReg[i].Index);
177 pair->RGB.Arg[i].Source = source;
178 pair->RGB.Arg[i].Swizzle = inst->SrcReg[i].Swizzle & 0x1ff;
179 pair->RGB.Arg[i].Abs = inst->SrcReg[i].Abs;
180 pair->RGB.Arg[i].Negate = !!(inst->SrcReg[i].Negate & (RC_MASK_X | RC_MASK_Y | RC_MASK_Z));
181 }
182 if (needalpha) {
183 unsigned int srcrgb = 0;
184 unsigned int srcalpha = 0;
185 unsigned int swz = GET_SWZ(inst->SrcReg[i].Swizzle, istranscendent ? 0 : 3);
186 if (swz < 3)
187 srcrgb = 1;
188 else if (swz < 4)
189 srcalpha = 1;
190 source = rc_pair_alloc_source(pair, srcrgb, srcalpha,
191 inst->SrcReg[i].File, inst->SrcReg[i].Index);
192 pair->Alpha.Arg[i].Source = source;
193 pair->Alpha.Arg[i].Swizzle = swz;
194 pair->Alpha.Arg[i].Abs = inst->SrcReg[i].Abs;
195 pair->Alpha.Arg[i].Negate = !!(inst->SrcReg[i].Negate & RC_MASK_W);
196 }
197 }
198
199 /* Destination handling */
200 if (inst->DstReg.File == RC_FILE_OUTPUT) {
201 if (inst->DstReg.Index == c->OutputDepth) {
202 pair->Alpha.DepthWriteMask |= GET_BIT(inst->DstReg.WriteMask, 3);
203 } else {
204 for (i = 0; i < 4; i++) {
205 if (inst->DstReg.Index == c->OutputColor[i]) {
206 pair->RGB.Target = i;
207 pair->Alpha.Target = i;
208 pair->RGB.OutputWriteMask |=
209 inst->DstReg.WriteMask & RC_MASK_XYZ;
210 pair->Alpha.OutputWriteMask |=
211 GET_BIT(inst->DstReg.WriteMask, 3);
212 break;
213 }
214 }
215 }
216 } else {
217 if (needrgb) {
218 pair->RGB.DestIndex = inst->DstReg.Index;
219 pair->RGB.WriteMask |= inst->DstReg.WriteMask & RC_MASK_XYZ;
220 }
221 if (needalpha) {
222 pair->Alpha.DestIndex = inst->DstReg.Index;
223 pair->Alpha.WriteMask |= GET_BIT(inst->DstReg.WriteMask, 3);
224 }
225 }
226
227 if (inst->WriteALUResult) {
228 pair->WriteALUResult = inst->WriteALUResult;
229 pair->ALUResultCompare = inst->ALUResultCompare;
230 }
231 }
232
233
234 /**
235 * Translate all ALU instructions into corresponding pair instructions,
236 * performing no other changes.
237 */
238 void rc_pair_translate(struct r300_fragment_program_compiler *c)
239 {
240 for(struct rc_instruction * inst = c->Base.Program.Instructions.Next;
241 inst != &c->Base.Program.Instructions;
242 inst = inst->Next) {
243 if (inst->Type != RC_INSTRUCTION_NORMAL)
244 continue;
245
246 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->U.I.Opcode);
247
248 if (opcode->HasTexture || opcode->IsFlowControl || opcode->Opcode == RC_OPCODE_KIL)
249 continue;
250
251 struct rc_sub_instruction copy = inst->U.I;
252
253 final_rewrite(&copy);
254 inst->Type = RC_INSTRUCTION_PAIR;
255 set_pair_instruction(c, &inst->U.P, &copy);
256 }
257 }