2 * Copyright 2009 Nicolai Hähnle <nhaehnle@gmail.com>
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:
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
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. */
23 #include "radeon_program.h"
27 static const char * textarget_to_string(rc_texture_target 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";
41 static const char * presubtract_op_to_string(rc_presubtract_op op)
47 return "(1 - 2 * src0)";
49 return "(src1 - src0)";
51 return "(src1 + src0)";
55 return "BAD_PRESUBTRACT_OP";
59 static void print_omod_op(FILE * f, rc_omod_op op)
61 const char * omod_str;
88 fprintf(f, " %s", omod_str);
91 static void rc_print_comparefunc(FILE * f, const char * lhs, rc_compare_func func, const char * rhs)
93 if (func == RC_COMPARE_FUNC_NEVER) {
95 } else if (func == RC_COMPARE_FUNC_ALWAYS) {
100 case RC_COMPARE_FUNC_LESS: op = "<"; break;
101 case RC_COMPARE_FUNC_EQUAL: op = "=="; break;
102 case RC_COMPARE_FUNC_LEQUAL: op = "<="; break;
103 case RC_COMPARE_FUNC_GREATER: op = ">"; break;
104 case RC_COMPARE_FUNC_NOTEQUAL: op = "!="; break;
105 case RC_COMPARE_FUNC_GEQUAL: op = ">="; break;
106 default: op = "???"; break;
108 fprintf(f, "%s %s %s", lhs, op, rhs);
112 static void rc_print_register(FILE * f, rc_register_file file, int index, unsigned int reladdr)
114 if (file == RC_FILE_NONE) {
116 } else if (file == RC_FILE_SPECIAL) {
118 case RC_SPECIAL_ALU_RESULT: fprintf(f, "aluresult"); break;
119 default: fprintf(f, "special[%i]", index); break;
122 const char * filename;
124 case RC_FILE_TEMPORARY: filename = "temp"; break;
125 case RC_FILE_INPUT: filename = "input"; break;
126 case RC_FILE_OUTPUT: filename = "output"; break;
127 case RC_FILE_ADDRESS: filename = "addr"; break;
128 case RC_FILE_CONSTANT: filename = "const"; break;
129 default: filename = "BAD FILE"; break;
131 fprintf(f, "%s[%i%s]", filename, index, reladdr ? " + addr[0]" : "");
135 static void rc_print_mask(FILE * f, unsigned int mask)
137 if (mask & RC_MASK_X) fprintf(f, "x");
138 if (mask & RC_MASK_Y) fprintf(f, "y");
139 if (mask & RC_MASK_Z) fprintf(f, "z");
140 if (mask & RC_MASK_W) fprintf(f, "w");
143 static void rc_print_dst_register(FILE * f, struct rc_dst_register dst)
145 rc_print_register(f, dst.File, dst.Index, 0);
146 if (dst.WriteMask != RC_MASK_XYZW) {
148 rc_print_mask(f, dst.WriteMask);
152 static char rc_swizzle_char(unsigned int swz)
155 case RC_SWIZZLE_X: return 'x';
156 case RC_SWIZZLE_Y: return 'y';
157 case RC_SWIZZLE_Z: return 'z';
158 case RC_SWIZZLE_W: return 'w';
159 case RC_SWIZZLE_ZERO: return '0';
160 case RC_SWIZZLE_ONE: return '1';
161 case RC_SWIZZLE_HALF: return 'H';
162 case RC_SWIZZLE_UNUSED: return '_';
164 fprintf(stderr, "bad swz: %u\n", swz);
168 static void rc_print_swizzle(FILE * f, unsigned int swizzle, unsigned int negate)
171 for(comp = 0; comp < 4; ++comp) {
172 rc_swizzle swz = GET_SWZ(swizzle, comp);
173 if (GET_BIT(negate, comp))
175 fprintf(f, "%c", rc_swizzle_char(swz));
179 static void rc_print_presub_instruction(FILE * f,
180 struct rc_presub_instruction inst)
185 fprintf(f, "1 - 2 * ");
186 rc_print_register(f, inst.SrcReg[0].File,
187 inst.SrcReg[0].Index,inst.SrcReg[0].RelAddr);
190 rc_print_register(f, inst.SrcReg[1].File,
191 inst.SrcReg[1].Index,inst.SrcReg[1].RelAddr);
193 rc_print_register(f, inst.SrcReg[0].File,
194 inst.SrcReg[0].Index,inst.SrcReg[0].RelAddr);
197 rc_print_register(f, inst.SrcReg[1].File,
198 inst.SrcReg[1].Index,inst.SrcReg[1].RelAddr);
200 rc_print_register(f, inst.SrcReg[0].File,
201 inst.SrcReg[0].Index,inst.SrcReg[0].RelAddr);
205 rc_print_register(f, inst.SrcReg[0].File,
206 inst.SrcReg[0].Index,inst.SrcReg[0].RelAddr);
214 static void rc_print_src_register(FILE * f, struct rc_instruction * inst,
215 struct rc_src_register src)
217 int trivial_negate = (src.Negate == RC_MASK_NONE || src.Negate == RC_MASK_XYZW);
219 if (src.Negate == RC_MASK_XYZW)
224 if(src.File == RC_FILE_PRESUB)
225 rc_print_presub_instruction(f, inst->U.I.PreSub);
227 rc_print_register(f, src.File, src.Index, src.RelAddr);
229 if (src.Abs && !trivial_negate)
232 if (src.Swizzle != RC_SWIZZLE_XYZW || !trivial_negate) {
234 rc_print_swizzle(f, src.Swizzle, trivial_negate ? 0 : src.Negate);
237 if (src.Abs && trivial_negate)
241 static unsigned update_branch_depth(rc_opcode opcode, unsigned *branch_depth)
245 case RC_OPCODE_BGNLOOP:
246 return (*branch_depth)++ * 2;
248 case RC_OPCODE_ENDIF:
249 case RC_OPCODE_ENDLOOP:
250 assert(*branch_depth > 0);
251 return --(*branch_depth) * 2;
254 assert(*branch_depth > 0);
255 return (*branch_depth - 1) * 2;
258 return *branch_depth * 2;
262 static void rc_print_normal_instruction(FILE * f, struct rc_instruction * inst, unsigned *branch_depth)
264 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->U.I.Opcode);
266 unsigned spaces = update_branch_depth(inst->U.I.Opcode, branch_depth);
268 for (unsigned i = 0; i < spaces; i++)
271 fprintf(f, "%s", opcode->Name);
273 switch(inst->U.I.SaturateMode) {
274 case RC_SATURATE_NONE: break;
275 case RC_SATURATE_ZERO_ONE: fprintf(f, "_SAT"); break;
276 case RC_SATURATE_MINUS_PLUS_ONE: fprintf(f, "_SAT2"); break;
277 default: fprintf(f, "_BAD_SAT"); break;
280 if (opcode->HasDstReg) {
282 rc_print_dst_register(f, inst->U.I.DstReg);
283 print_omod_op(f, inst->U.I.Omod);
284 if (opcode->NumSrcRegs)
288 for(reg = 0; reg < opcode->NumSrcRegs; ++reg) {
292 rc_print_src_register(f, inst, inst->U.I.SrcReg[reg]);
295 if (opcode->HasTexture) {
296 fprintf(f, ", %s%s[%u]%s%s",
297 textarget_to_string(inst->U.I.TexSrcTarget),
298 inst->U.I.TexShadow ? "SHADOW" : "",
299 inst->U.I.TexSrcUnit,
300 inst->U.I.TexSemWait ? " SEM_WAIT" : "",
301 inst->U.I.TexSemAcquire ? " SEM_ACQUIRE" : "");
306 if (inst->U.I.WriteALUResult) {
307 fprintf(f, " [aluresult = (");
308 rc_print_comparefunc(f,
309 (inst->U.I.WriteALUResult == RC_ALURESULT_X) ? "x" : "w",
310 inst->U.I.ALUResultCompare, "0");
317 static void rc_print_pair_instruction(FILE * f, struct rc_instruction * fullinst, unsigned *branch_depth)
319 struct rc_pair_instruction * inst = &fullinst->U.P;
321 unsigned spaces = update_branch_depth(inst->RGB.Opcode != RC_OPCODE_NOP ?
322 inst->RGB.Opcode : inst->Alpha.Opcode, branch_depth);
324 for (unsigned i = 0; i < spaces; i++)
327 for(unsigned int src = 0; src < 3; ++src) {
328 if (inst->RGB.Src[src].Used) {
331 fprintf(f, "src%i.xyz = ", src);
332 rc_print_register(f, inst->RGB.Src[src].File, inst->RGB.Src[src].Index, 0);
335 if (inst->Alpha.Src[src].Used) {
338 fprintf(f, "src%i.w = ", src);
339 rc_print_register(f, inst->Alpha.Src[src].File, inst->Alpha.Src[src].Index, 0);
343 if(inst->RGB.Src[RC_PAIR_PRESUB_SRC].Used) {
344 fprintf(f, ", srcp.xyz = %s",
345 presubtract_op_to_string(
346 inst->RGB.Src[RC_PAIR_PRESUB_SRC].Index));
348 if(inst->Alpha.Src[RC_PAIR_PRESUB_SRC].Used) {
349 fprintf(f, ", srcp.w = %s",
350 presubtract_op_to_string(
351 inst->Alpha.Src[RC_PAIR_PRESUB_SRC].Index));
354 fprintf(f, " SEM_WAIT");
358 if (inst->RGB.Opcode != RC_OPCODE_NOP) {
359 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->RGB.Opcode);
361 for (unsigned i = 0; i < spaces; i++)
364 fprintf(f, " %s%s", opcode->Name, inst->RGB.Saturate ? "_SAT" : "");
365 if (inst->RGB.WriteMask)
366 fprintf(f, " temp[%i].%s%s%s", inst->RGB.DestIndex,
367 (inst->RGB.WriteMask & 1) ? "x" : "",
368 (inst->RGB.WriteMask & 2) ? "y" : "",
369 (inst->RGB.WriteMask & 4) ? "z" : "");
370 if (inst->RGB.OutputWriteMask)
371 fprintf(f, " color[%i].%s%s%s", inst->RGB.Target,
372 (inst->RGB.OutputWriteMask & 1) ? "x" : "",
373 (inst->RGB.OutputWriteMask & 2) ? "y" : "",
374 (inst->RGB.OutputWriteMask & 4) ? "z" : "");
375 if (inst->WriteALUResult == RC_ALURESULT_X)
376 fprintf(f, " aluresult");
378 print_omod_op(f, inst->RGB.Omod);
380 for(unsigned int arg = 0; arg < opcode->NumSrcRegs; ++arg) {
381 const char* abs = inst->RGB.Arg[arg].Abs ? "|" : "";
382 const char* neg = inst->RGB.Arg[arg].Negate ? "-" : "";
383 fprintf(f, ", %s%ssrc", neg, abs);
384 if(inst->RGB.Arg[arg].Source == RC_PAIR_PRESUB_SRC)
387 fprintf(f,"%d", inst->RGB.Arg[arg].Source);
388 fprintf(f,".%c%c%c%s",
389 rc_swizzle_char(GET_SWZ(inst->RGB.Arg[arg].Swizzle, 0)),
390 rc_swizzle_char(GET_SWZ(inst->RGB.Arg[arg].Swizzle, 1)),
391 rc_swizzle_char(GET_SWZ(inst->RGB.Arg[arg].Swizzle, 2)),
397 if (inst->Alpha.Opcode != RC_OPCODE_NOP) {
398 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->Alpha.Opcode);
400 for (unsigned i = 0; i < spaces; i++)
403 fprintf(f, " %s%s", opcode->Name, inst->Alpha.Saturate ? "_SAT" : "");
404 if (inst->Alpha.WriteMask)
405 fprintf(f, " temp[%i].w", inst->Alpha.DestIndex);
406 if (inst->Alpha.OutputWriteMask)
407 fprintf(f, " color[%i].w", inst->Alpha.Target);
408 if (inst->Alpha.DepthWriteMask)
409 fprintf(f, " depth.w");
410 if (inst->WriteALUResult == RC_ALURESULT_W)
411 fprintf(f, " aluresult");
413 print_omod_op(f, inst->Alpha.Omod);
415 for(unsigned int arg = 0; arg < opcode->NumSrcRegs; ++arg) {
416 const char* abs = inst->Alpha.Arg[arg].Abs ? "|" : "";
417 const char* neg = inst->Alpha.Arg[arg].Negate ? "-" : "";
418 fprintf(f, ", %s%ssrc", neg, abs);
419 if(inst->Alpha.Arg[arg].Source == RC_PAIR_PRESUB_SRC)
422 fprintf(f,"%d", inst->Alpha.Arg[arg].Source);
424 rc_swizzle_char(GET_SWZ(inst->Alpha.Arg[arg].Swizzle, 0)), abs);
429 if (inst->WriteALUResult) {
430 for (unsigned i = 0; i < spaces; i++)
433 fprintf(f, " [aluresult = (");
434 rc_print_comparefunc(f, "result", inst->ALUResultCompare, "0");
440 * Print program to stderr, default options.
442 void rc_print_program(const struct rc_program *prog)
444 unsigned int linenum = 0;
445 unsigned branch_depth = 0;
446 struct rc_instruction *inst;
448 fprintf(stderr, "# Radeon Compiler Program\n");
450 for(inst = prog->Instructions.Next; inst != &prog->Instructions; inst = inst->Next) {
451 fprintf(stderr, "%3d: ", linenum);
453 if (inst->Type == RC_INSTRUCTION_PAIR)
454 rc_print_pair_instruction(stderr, inst, &branch_depth);
456 rc_print_normal_instruction(stderr, inst, &branch_depth);