r300compiler: Add MRT number to debugging output.
[mesa.git] / src / mesa / drivers / dri / r300 / compiler / radeon_program_print.c
1 /*
2 * Copyright 2009 Nicolai Hähnle <nhaehnle@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "radeon_program.h"
24
25 #include <stdio.h>
26
27 static const char * textarget_to_string(rc_texture_target target)
28 {
29 switch(target) {
30 case RC_TEXTURE_2D_ARRAY: return "2D_ARRAY";
31 case RC_TEXTURE_1D_ARRAY: return "1D_ARRAY";
32 case RC_TEXTURE_CUBE: return "CUBE";
33 case RC_TEXTURE_3D: return "3D";
34 case RC_TEXTURE_RECT: return "RECT";
35 case RC_TEXTURE_2D: return "2D";
36 case RC_TEXTURE_1D: return "1D";
37 default: return "BAD_TEXTURE_TARGET";
38 }
39 }
40
41 static void rc_print_comparefunc(FILE * f, const char * lhs, rc_compare_func func, const char * rhs)
42 {
43 if (func == RC_COMPARE_FUNC_NEVER) {
44 fprintf(f, "false");
45 } else if (func == RC_COMPARE_FUNC_ALWAYS) {
46 fprintf(f, "true");
47 } else {
48 const char * op;
49 switch(func) {
50 case RC_COMPARE_FUNC_LESS: op = "<"; break;
51 case RC_COMPARE_FUNC_EQUAL: op = "=="; break;
52 case RC_COMPARE_FUNC_LEQUAL: op = "<="; break;
53 case RC_COMPARE_FUNC_GREATER: op = ">"; break;
54 case RC_COMPARE_FUNC_NOTEQUAL: op = "!="; break;
55 case RC_COMPARE_FUNC_GEQUAL: op = ">="; break;
56 default: op = "???"; break;
57 }
58 fprintf(f, "%s %s %s", lhs, op, rhs);
59 }
60 }
61
62 static void rc_print_register(FILE * f, rc_register_file file, int index, unsigned int reladdr)
63 {
64 if (file == RC_FILE_NONE) {
65 fprintf(f, "none");
66 } else if (file == RC_FILE_SPECIAL) {
67 switch(index) {
68 case RC_SPECIAL_ALU_RESULT: fprintf(f, "aluresult"); break;
69 default: fprintf(f, "special[%i]", index); break;
70 }
71 } else {
72 const char * filename;
73 switch(file) {
74 case RC_FILE_TEMPORARY: filename = "temp"; break;
75 case RC_FILE_INPUT: filename = "input"; break;
76 case RC_FILE_OUTPUT: filename = "output"; break;
77 case RC_FILE_ADDRESS: filename = "addr"; break;
78 case RC_FILE_CONSTANT: filename = "const"; break;
79 default: filename = "BAD FILE"; break;
80 }
81 fprintf(f, "%s[%i%s]", filename, index, reladdr ? " + addr[0]" : "");
82 }
83 }
84
85 static void rc_print_mask(FILE * f, unsigned int mask)
86 {
87 if (mask & RC_MASK_X) fprintf(f, "x");
88 if (mask & RC_MASK_Y) fprintf(f, "y");
89 if (mask & RC_MASK_Z) fprintf(f, "z");
90 if (mask & RC_MASK_W) fprintf(f, "w");
91 }
92
93 static void rc_print_dst_register(FILE * f, struct rc_dst_register dst)
94 {
95 rc_print_register(f, dst.File, dst.Index, dst.RelAddr);
96 if (dst.WriteMask != RC_MASK_XYZW) {
97 fprintf(f, ".");
98 rc_print_mask(f, dst.WriteMask);
99 }
100 }
101
102 static char rc_swizzle_char(unsigned int swz)
103 {
104 switch(swz) {
105 case RC_SWIZZLE_X: return 'x';
106 case RC_SWIZZLE_Y: return 'y';
107 case RC_SWIZZLE_Z: return 'z';
108 case RC_SWIZZLE_W: return 'w';
109 case RC_SWIZZLE_ZERO: return '0';
110 case RC_SWIZZLE_ONE: return '1';
111 case RC_SWIZZLE_HALF: return 'H';
112 case RC_SWIZZLE_UNUSED: return '_';
113 }
114 return '?';
115 }
116
117 static void rc_print_swizzle(FILE * f, unsigned int swizzle, unsigned int negate)
118 {
119 unsigned int comp;
120 for(comp = 0; comp < 4; ++comp) {
121 rc_swizzle swz = GET_SWZ(swizzle, comp);
122 if (GET_BIT(negate, comp))
123 fprintf(f, "-");
124 fprintf(f, "%c", rc_swizzle_char(swz));
125 }
126 }
127
128 static void rc_print_src_register(FILE * f, struct rc_src_register src)
129 {
130 int trivial_negate = (src.Negate == RC_MASK_NONE || src.Negate == RC_MASK_XYZW);
131
132 if (src.Negate == RC_MASK_XYZW)
133 fprintf(f, "-");
134 if (src.Abs)
135 fprintf(f, "|");
136
137 rc_print_register(f, src.File, src.Index, src.RelAddr);
138
139 if (src.Abs && !trivial_negate)
140 fprintf(f, "|");
141
142 if (src.Swizzle != RC_SWIZZLE_XYZW || !trivial_negate) {
143 fprintf(f, ".");
144 rc_print_swizzle(f, src.Swizzle, trivial_negate ? 0 : src.Negate);
145 }
146
147 if (src.Abs && trivial_negate)
148 fprintf(f, "|");
149 }
150
151 static void rc_print_normal_instruction(FILE * f, struct rc_instruction * inst)
152 {
153 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->U.I.Opcode);
154 unsigned int reg;
155
156 fprintf(f, "%s", opcode->Name);
157
158 switch(inst->U.I.SaturateMode) {
159 case RC_SATURATE_NONE: break;
160 case RC_SATURATE_ZERO_ONE: fprintf(f, "_SAT"); break;
161 case RC_SATURATE_MINUS_PLUS_ONE: fprintf(f, "_SAT2"); break;
162 default: fprintf(f, "_BAD_SAT"); break;
163 }
164
165 if (opcode->HasDstReg) {
166 fprintf(f, " ");
167 rc_print_dst_register(f, inst->U.I.DstReg);
168 if (opcode->NumSrcRegs)
169 fprintf(f, ",");
170 }
171
172 for(reg = 0; reg < opcode->NumSrcRegs; ++reg) {
173 if (reg > 0)
174 fprintf(f, ",");
175 fprintf(f, " ");
176 rc_print_src_register(f, inst->U.I.SrcReg[reg]);
177 }
178
179 if (opcode->HasTexture) {
180 fprintf(f, ", %s%s[%u]",
181 textarget_to_string(inst->U.I.TexSrcTarget),
182 inst->U.I.TexShadow ? "SHADOW" : "",
183 inst->U.I.TexSrcUnit);
184 }
185
186 fprintf(f, ";");
187
188 if (inst->U.I.WriteALUResult) {
189 fprintf(f, " [aluresult = (");
190 rc_print_comparefunc(f,
191 (inst->U.I.WriteALUResult == RC_ALURESULT_X) ? "x" : "w",
192 inst->U.I.ALUResultCompare, "0");
193 fprintf(f, ")]");
194 }
195
196 fprintf(f, "\n");
197 }
198
199 static void rc_print_pair_instruction(FILE * f, struct rc_instruction * fullinst)
200 {
201 struct rc_pair_instruction * inst = &fullinst->U.P;
202 int printedsrc = 0;
203
204 for(unsigned int src = 0; src < 3; ++src) {
205 if (inst->RGB.Src[src].Used) {
206 if (printedsrc)
207 fprintf(f, ", ");
208 fprintf(f, "src%i.xyz = ", src);
209 rc_print_register(f, inst->RGB.Src[src].File, inst->RGB.Src[src].Index, 0);
210 printedsrc = 1;
211 }
212 if (inst->Alpha.Src[src].Used) {
213 if (printedsrc)
214 fprintf(f, ", ");
215 fprintf(f, "src%i.w = ", src);
216 rc_print_register(f, inst->Alpha.Src[src].File, inst->Alpha.Src[src].Index, 0);
217 printedsrc = 1;
218 }
219 }
220 fprintf(f, "\n");
221
222 if (inst->RGB.Opcode != RC_OPCODE_NOP) {
223 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->RGB.Opcode);
224
225 fprintf(f, " %s%s", opcode->Name, inst->RGB.Saturate ? "_SAT" : "");
226 if (inst->RGB.WriteMask)
227 fprintf(f, " temp[%i].%s%s%s", inst->RGB.DestIndex,
228 (inst->RGB.WriteMask & 1) ? "x" : "",
229 (inst->RGB.WriteMask & 2) ? "y" : "",
230 (inst->RGB.WriteMask & 4) ? "z" : "");
231 if (inst->RGB.OutputWriteMask)
232 fprintf(f, " color[%i].%s%s%s", inst->RGB.Target,
233 (inst->RGB.OutputWriteMask & 1) ? "x" : "",
234 (inst->RGB.OutputWriteMask & 2) ? "y" : "",
235 (inst->RGB.OutputWriteMask & 4) ? "z" : "");
236 if (inst->WriteALUResult == RC_ALURESULT_X)
237 fprintf(f, " aluresult");
238
239 for(unsigned int arg = 0; arg < opcode->NumSrcRegs; ++arg) {
240 const char* abs = inst->RGB.Arg[arg].Abs ? "|" : "";
241 const char* neg = inst->RGB.Arg[arg].Negate ? "-" : "";
242 fprintf(f, ", %s%ssrc%i.%c%c%c%s", neg, abs, inst->RGB.Arg[arg].Source,
243 rc_swizzle_char(GET_SWZ(inst->RGB.Arg[arg].Swizzle, 0)),
244 rc_swizzle_char(GET_SWZ(inst->RGB.Arg[arg].Swizzle, 1)),
245 rc_swizzle_char(GET_SWZ(inst->RGB.Arg[arg].Swizzle, 2)),
246 abs);
247 }
248 fprintf(f, "\n");
249 }
250
251 if (inst->Alpha.Opcode != RC_OPCODE_NOP) {
252 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->Alpha.Opcode);
253
254 fprintf(f, " %s%s", opcode->Name, inst->Alpha.Saturate ? "_SAT" : "");
255 if (inst->Alpha.WriteMask)
256 fprintf(f, " temp[%i].w", inst->Alpha.DestIndex);
257 if (inst->Alpha.OutputWriteMask)
258 fprintf(f, " color[%i].w", inst->Alpha.Target);
259 if (inst->Alpha.DepthWriteMask)
260 fprintf(f, " depth.w");
261 if (inst->WriteALUResult == RC_ALURESULT_W)
262 fprintf(f, " aluresult");
263
264 for(unsigned int arg = 0; arg < opcode->NumSrcRegs; ++arg) {
265 const char* abs = inst->Alpha.Arg[arg].Abs ? "|" : "";
266 const char* neg = inst->Alpha.Arg[arg].Negate ? "-" : "";
267 fprintf(f, ", %s%ssrc%i.%c%s", neg, abs, inst->Alpha.Arg[arg].Source,
268 rc_swizzle_char(inst->Alpha.Arg[arg].Swizzle), abs);
269 }
270 fprintf(f, "\n");
271 }
272
273 if (inst->WriteALUResult) {
274 fprintf(f, " [aluresult = (");
275 rc_print_comparefunc(f, "result", inst->ALUResultCompare, "0");
276 fprintf(f, ")]\n");
277 }
278 }
279
280 /**
281 * Print program to stderr, default options.
282 */
283 void rc_print_program(const struct rc_program *prog)
284 {
285 unsigned int linenum = 0;
286 struct rc_instruction *inst;
287
288 fprintf(stderr, "# Radeon Compiler Program\n");
289
290 for(inst = prog->Instructions.Next; inst != &prog->Instructions; inst = inst->Next) {
291 fprintf(stderr, "%3d: ", linenum);
292
293 if (inst->Type == RC_INSTRUCTION_PAIR)
294 rc_print_pair_instruction(stderr, inst);
295 else
296 rc_print_normal_instruction(stderr, inst);
297
298 linenum++;
299 }
300 }