• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3  * SPDX-License-Identifier: MIT
4  */
5 
6 #include "r500_fragprog.h"
7 
8 #include <stdio.h>
9 
10 #include "r300_reg.h"
11 #include "radeon_compiler_util.h"
12 #include "radeon_list.h"
13 #include "radeon_variable.h"
14 
15 #include "util/compiler.h"
16 
17 /**
18  * Rewrite IF instructions to use the ALU result special register.
19  */
20 static void
r500_transform_IF_instr(struct radeon_compiler * c,struct rc_instruction * inst_if,struct rc_list * var_list)21 r500_transform_IF_instr(struct radeon_compiler *c, struct rc_instruction *inst_if,
22                         struct rc_list *var_list)
23 {
24 
25    struct rc_variable *writer;
26    struct rc_list *writer_list, *list_ptr;
27    unsigned int generic_if = 0;
28    unsigned int alu_chan;
29 
30    writer_list = rc_variable_list_get_writers(var_list, inst_if->Type, &inst_if->U.I.SrcReg[0]);
31    if (!writer_list) {
32       generic_if = 1;
33    } else {
34 
35       /* Make sure it is safe for the writers to write to
36        * ALU Result */
37       for (list_ptr = writer_list; list_ptr; list_ptr = list_ptr->Next) {
38          struct rc_instruction *inst;
39          writer = list_ptr->Item;
40          /* We are going to modify the destination register
41           * of writer, so if it has a reader other than
42           * inst_if (aka ReaderCount > 1) we must fall back to
43           * our generic IF.
44           * If the writer has a lower IP than inst_if, this
45           * means that inst_if is above the writer in a loop.
46           * I'm not sure why this would ever happen, but
47           * if it does we want to make sure we fall back
48           * to our generic IF. */
49          if (writer->ReaderCount > 1 || writer->Inst->IP < inst_if->IP) {
50             generic_if = 1;
51             break;
52          }
53 
54          /* The ALU Result is not preserved across IF
55           * instructions, so if there is another IF
56           * instruction between writer and inst_if, then
57           * we need to fall back to generic IF. */
58          for (inst = writer->Inst; inst != inst_if; inst = inst->Next) {
59             const struct rc_opcode_info *info = rc_get_opcode_info(inst->U.I.Opcode);
60             if (info->IsFlowControl) {
61                generic_if = 1;
62                break;
63             }
64          }
65          if (generic_if) {
66             break;
67          }
68       }
69    }
70 
71    if (GET_SWZ(inst_if->U.I.SrcReg[0].Swizzle, 0) == RC_SWIZZLE_X) {
72       alu_chan = RC_ALURESULT_X;
73    } else {
74       alu_chan = RC_ALURESULT_W;
75    }
76    if (generic_if) {
77       struct rc_instruction *inst_mov = rc_insert_new_instruction(c, inst_if->Prev);
78 
79       inst_mov->U.I.Opcode = RC_OPCODE_MOV;
80       inst_mov->U.I.DstReg.WriteMask = 0;
81       inst_mov->U.I.DstReg.File = RC_FILE_NONE;
82       inst_mov->U.I.ALUResultCompare = RC_COMPARE_FUNC_NOTEQUAL;
83       inst_mov->U.I.WriteALUResult = alu_chan;
84       inst_mov->U.I.SrcReg[0] = inst_if->U.I.SrcReg[0];
85       if (alu_chan == RC_ALURESULT_X) {
86          inst_mov->U.I.SrcReg[0].Swizzle =
87             combine_swizzles4(inst_mov->U.I.SrcReg[0].Swizzle, RC_SWIZZLE_X, RC_SWIZZLE_UNUSED,
88                               RC_SWIZZLE_UNUSED, RC_SWIZZLE_UNUSED);
89       } else {
90          inst_mov->U.I.SrcReg[0].Swizzle =
91             combine_swizzles4(inst_mov->U.I.SrcReg[0].Swizzle, RC_SWIZZLE_UNUSED, RC_SWIZZLE_UNUSED,
92                               RC_SWIZZLE_UNUSED, RC_SWIZZLE_Z);
93       }
94    } else {
95       rc_compare_func compare_func = RC_COMPARE_FUNC_NEVER;
96       unsigned int preserve_opcode = 0;
97       for (list_ptr = writer_list; list_ptr; list_ptr = list_ptr->Next) {
98          writer = list_ptr->Item;
99          switch (writer->Inst->U.I.Opcode) {
100          case RC_OPCODE_SEQ:
101             compare_func = RC_COMPARE_FUNC_EQUAL;
102             break;
103          case RC_OPCODE_SNE:
104             compare_func = RC_COMPARE_FUNC_NOTEQUAL;
105             break;
106          case RC_OPCODE_SGE:
107             compare_func = RC_COMPARE_FUNC_GEQUAL;
108             break;
109          case RC_OPCODE_SLT:
110             compare_func = RC_COMPARE_FUNC_LESS;
111             break;
112          default:
113             compare_func = RC_COMPARE_FUNC_NOTEQUAL;
114             preserve_opcode = 1;
115             break;
116          }
117          if (!preserve_opcode) {
118             writer->Inst->U.I.Opcode = RC_OPCODE_ADD;
119             writer->Inst->U.I.SrcReg[1].Negate = ~writer->Inst->U.I.SrcReg[1].Negate;
120          }
121          writer->Inst->U.I.DstReg.WriteMask = 0;
122          writer->Inst->U.I.DstReg.File = RC_FILE_NONE;
123          writer->Inst->U.I.WriteALUResult = alu_chan;
124          writer->Inst->U.I.ALUResultCompare = compare_func;
125       }
126    }
127 
128    inst_if->U.I.SrcReg[0].File = RC_FILE_SPECIAL;
129    inst_if->U.I.SrcReg[0].Index = RC_SPECIAL_ALU_RESULT;
130    inst_if->U.I.SrcReg[0].Swizzle =
131       RC_MAKE_SWIZZLE(RC_SWIZZLE_X, RC_SWIZZLE_UNUSED, RC_SWIZZLE_UNUSED, RC_SWIZZLE_UNUSED);
132    inst_if->U.I.SrcReg[0].Negate = 0;
133 }
134 
135 void
r500_transform_IF(struct radeon_compiler * c,void * user)136 r500_transform_IF(struct radeon_compiler *c, void *user)
137 {
138    struct rc_list *var_list = rc_get_variables(c);
139 
140    struct rc_instruction *inst = c->Program.Instructions.Next;
141    while (inst != &c->Program.Instructions) {
142       struct rc_instruction *current = inst;
143       inst = inst->Next;
144 
145       if (current->U.I.Opcode == RC_OPCODE_IF)
146          r500_transform_IF_instr(c, current, var_list);
147    }
148 }
149 
150 static int
r500_swizzle_is_native(rc_opcode opcode,struct rc_src_register reg)151 r500_swizzle_is_native(rc_opcode opcode, struct rc_src_register reg)
152 {
153    unsigned int relevant;
154    int i;
155 
156    if (opcode == RC_OPCODE_TEX || opcode == RC_OPCODE_TXB || opcode == RC_OPCODE_TXP ||
157        opcode == RC_OPCODE_TXD || opcode == RC_OPCODE_TXL || opcode == RC_OPCODE_KIL) {
158       if (reg.Abs)
159          return 0;
160 
161       /* Texture coordinates can be only read from temporary file,
162        * input is just a temporary with varying in it.
163        */
164       if (reg.File != RC_FILE_TEMPORARY && reg.File != RC_FILE_INPUT)
165          return 0;
166 
167       if (opcode == RC_OPCODE_KIL && (reg.Swizzle != RC_SWIZZLE_XYZW || reg.Negate != RC_MASK_NONE))
168          return 0;
169 
170       for (i = 0; i < 4; ++i) {
171          unsigned int swz = GET_SWZ(reg.Swizzle, i);
172          if (swz == RC_SWIZZLE_UNUSED) {
173             reg.Negate &= ~(1 << i);
174             continue;
175          }
176          if (swz >= 4)
177             return 0;
178       }
179 
180       if (reg.Negate)
181          return 0;
182 
183       return 1;
184    } else if (opcode == RC_OPCODE_DDX || opcode == RC_OPCODE_DDY) {
185       /* DDX/MDH and DDY/MDV explicitly ignore incoming swizzles;
186        * if it doesn't fit perfectly into a .xyzw case... */
187       if (reg.Swizzle == RC_SWIZZLE_XYZW && !reg.Abs && !reg.Negate)
188          return 1;
189 
190       return 0;
191    } else {
192       /* ALU instructions support almost everything */
193       relevant = 0;
194       for (i = 0; i < 3; ++i) {
195          unsigned int swz = GET_SWZ(reg.Swizzle, i);
196          if (swz != RC_SWIZZLE_UNUSED && swz != RC_SWIZZLE_ZERO)
197             relevant |= 1 << i;
198       }
199       if ((reg.Negate & relevant) && ((reg.Negate & relevant) != relevant))
200          return 0;
201 
202       return 1;
203    }
204 }
205 
206 /**
207  * Split source register access.
208  *
209  * The only thing we *cannot* do in an ALU instruction is per-component
210  * negation.
211  */
212 static void
r500_swizzle_split(struct rc_src_register src,unsigned int usemask,struct rc_swizzle_split * split)213 r500_swizzle_split(struct rc_src_register src, unsigned int usemask, struct rc_swizzle_split *split)
214 {
215    unsigned int negatebase[2] = {0, 0};
216    int i;
217 
218    for (i = 0; i < 4; ++i) {
219       unsigned int swz = GET_SWZ(src.Swizzle, i);
220       if (swz == RC_SWIZZLE_UNUSED || !GET_BIT(usemask, i))
221          continue;
222       negatebase[GET_BIT(src.Negate, i)] |= 1 << i;
223    }
224 
225    split->NumPhases = 0;
226 
227    for (i = 0; i <= 1; ++i) {
228       if (!negatebase[i])
229          continue;
230 
231       split->Phase[split->NumPhases++] = negatebase[i];
232    }
233 }
234 
235 const struct rc_swizzle_caps r500_swizzle_caps = {.IsNative = r500_swizzle_is_native,
236                                                   .Split = r500_swizzle_split};
237 
238 static char *
toswiz(int swiz_val)239 toswiz(int swiz_val)
240 {
241    switch (swiz_val) {
242    case 0: return "R";
243    case 1: return "G";
244    case 2: return "B";
245    case 3: return "A";
246    case 4: return "0";
247    case 5: return "H";
248    case 6: return "1";
249    case 7: return "U";
250    }
251    return NULL;
252 }
253 
254 static char *
toop(int op_val)255 toop(int op_val)
256 {
257    char *str = NULL;
258    switch (op_val) {
259    case 0: str = "MAD"; break;
260    case 1: str = "DP3"; break;
261    case 2: str = "DP4"; break;
262    case 3: str = "D2A"; break;
263    case 4: str = "MIN"; break;
264    case 5: str = "MAX"; break;
265    case 6: str = "Reserved"; break;
266    case 7: str = "CND"; break;
267    case 8: str = "CMP"; break;
268    case 9: str = "FRC"; break;
269    case 10: str = "SOP"; break;
270    case 11: str = "MDH"; break;
271    case 12: str = "MDV"; break;
272    }
273    return str;
274 }
275 
276 static char *
to_alpha_op(int op_val)277 to_alpha_op(int op_val)
278 {
279    char *str = NULL;
280    switch (op_val) {
281    case 0: str = "MAD"; break;
282    case 1: str = "DP"; break;
283    case 2: str = "MIN"; break;
284    case 3: str = "MAX"; break;
285    case 4: str = "Reserved"; break;
286    case 5: str = "CND"; break;
287    case 6: str = "CMP"; break;
288    case 7: str = "FRC"; break;
289    case 8: str = "EX2"; break;
290    case 9: str = "LN2"; break;
291    case 10: str = "RCP"; break;
292    case 11: str = "RSQ"; break;
293    case 12: str = "SIN"; break;
294    case 13: str = "COS"; break;
295    case 14: str = "MDH"; break;
296    case 15: str = "MDV"; break;
297    }
298    return str;
299 }
300 
301 static char *
to_mask(int val)302 to_mask(int val)
303 {
304    char *str = NULL;
305    switch (val) {
306    case 0: str = "NONE"; break;
307    case 1: str = "R"; break;
308    case 2: str = "G"; break;
309    case 3: str = "RG"; break;
310    case 4: str = "B"; break;
311    case 5: str = "RB"; break;
312    case 6: str = "GB"; break;
313    case 7: str = "RGB"; break;
314    case 8: str = "A"; break;
315    case 9: str = "AR"; break;
316    case 10: str = "AG"; break;
317    case 11: str = "ARG"; break;
318    case 12: str = "AB"; break;
319    case 13: str = "ARB"; break;
320    case 14: str = "AGB"; break;
321    case 15: str = "ARGB"; break;
322    }
323    return str;
324 }
325 
326 static char *
to_texop(int val)327 to_texop(int val)
328 {
329    switch (val) {
330    case 0: return "NOP";
331    case 1: return "LD";
332    case 2: return "TEXKILL";
333    case 3: return "PROJ";
334    case 4: return "LODBIAS";
335    case 5: return "LOD";
336    case 6: return "DXDY";
337    }
338    return NULL;
339 }
340 
341 void
r500FragmentProgramDump(struct radeon_compiler * c,void * user)342 r500FragmentProgramDump(struct radeon_compiler *c, void *user)
343 {
344    struct r300_fragment_program_compiler *compiler = (struct r300_fragment_program_compiler *)c;
345    struct r500_fragment_program_code *code = &compiler->code->code.r500;
346    int n, i;
347    uint32_t inst;
348    uint32_t inst0;
349    char *str = NULL;
350    fprintf(stderr, "R500 Fragment Program:\n--------\n");
351 
352    for (n = 0; n < code->inst_end + 1; n++) {
353       inst0 = inst = code->inst[n].inst0;
354       fprintf(stderr, "%d\t0:CMN_INST   0x%08x:", n, inst);
355       switch (inst & 0x3) {
356       case R500_INST_TYPE_ALU: str = "ALU"; break;
357       case R500_INST_TYPE_OUT: str = "OUT"; break;
358       case R500_INST_TYPE_FC: str = "FC"; break;
359       case R500_INST_TYPE_TEX: str = "TEX"; break;
360       }
361       fprintf(stderr, "%s %s %s %s %s ", str, inst & R500_INST_TEX_SEM_WAIT ? "TEX_WAIT" : "",
362               inst & R500_INST_LAST ? "LAST" : "", inst & R500_INST_NOP ? "NOP" : "",
363               inst & R500_INST_ALU_WAIT ? "ALU WAIT" : "");
364       fprintf(stderr, "wmask: %s omask: %s\n", to_mask((inst >> 11) & 0xf),
365               to_mask((inst >> 15) & 0xf));
366 
367       switch (inst0 & 0x3) {
368       case R500_INST_TYPE_ALU:
369       case R500_INST_TYPE_OUT:
370          fprintf(stderr, "\t1:RGB_ADDR   0x%08x:", code->inst[n].inst1);
371          inst = code->inst[n].inst1;
372 
373          fprintf(stderr, "Addr0: %d%c, Addr1: %d%c, Addr2: %d%c, srcp:%d\n", inst & 0xff,
374                  (inst & (1 << 8)) ? 'c' : 't', (inst >> 10) & 0xff, (inst & (1 << 18)) ? 'c' : 't',
375                  (inst >> 20) & 0xff, (inst & (1 << 28)) ? 'c' : 't', (inst >> 30));
376 
377          fprintf(stderr, "\t2:ALPHA_ADDR 0x%08x:", code->inst[n].inst2);
378          inst = code->inst[n].inst2;
379          fprintf(stderr, "Addr0: %d%c, Addr1: %d%c, Addr2: %d%c, srcp:%d\n", inst & 0xff,
380                  (inst & (1 << 8)) ? 'c' : 't', (inst >> 10) & 0xff, (inst & (1 << 18)) ? 'c' : 't',
381                  (inst >> 20) & 0xff, (inst & (1 << 28)) ? 'c' : 't', (inst >> 30));
382          fprintf(stderr, "\t3 RGB_INST:  0x%08x:", code->inst[n].inst3);
383          inst = code->inst[n].inst3;
384          fprintf(stderr, "rgb_A_src:%d %s/%s/%s %d rgb_B_src:%d %s/%s/%s %d targ: %d\n",
385                  (inst) & 0x3, toswiz((inst >> 2) & 0x7), toswiz((inst >> 5) & 0x7),
386                  toswiz((inst >> 8) & 0x7), (inst >> 11) & 0x3, (inst >> 13) & 0x3,
387                  toswiz((inst >> 15) & 0x7), toswiz((inst >> 18) & 0x7), toswiz((inst >> 21) & 0x7),
388                  (inst >> 24) & 0x3, (inst >> 29) & 0x3);
389 
390          fprintf(stderr, "\t4 ALPHA_INST:0x%08x:", code->inst[n].inst4);
391          inst = code->inst[n].inst4;
392          fprintf(stderr, "%s dest:%d%s alp_A_src:%d %s %d alp_B_src:%d %s %d targ %d w:%d\n",
393                  to_alpha_op(inst & 0xf), (inst >> 4) & 0x7f, inst & (1 << 11) ? "(rel)" : "",
394                  (inst >> 12) & 0x3, toswiz((inst >> 14) & 0x7), (inst >> 17) & 0x3,
395                  (inst >> 19) & 0x3, toswiz((inst >> 21) & 0x7), (inst >> 24) & 0x3,
396                  (inst >> 29) & 0x3, (inst >> 31) & 0x1);
397 
398          fprintf(stderr, "\t5 RGBA_INST: 0x%08x:", code->inst[n].inst5);
399          inst = code->inst[n].inst5;
400          fprintf(stderr, "%s dest:%d%s rgb_C_src:%d %s/%s/%s %d alp_C_src:%d %s %d\n",
401                  toop(inst & 0xf), (inst >> 4) & 0x7f, inst & (1 << 11) ? "(rel)" : "",
402                  (inst >> 12) & 0x3, toswiz((inst >> 14) & 0x7), toswiz((inst >> 17) & 0x7),
403                  toswiz((inst >> 20) & 0x7), (inst >> 23) & 0x3, (inst >> 25) & 0x3,
404                  toswiz((inst >> 27) & 0x7), (inst >> 30) & 0x3);
405          break;
406       case R500_INST_TYPE_FC:
407          fprintf(stderr, "\t2:FC_INST    0x%08x:", code->inst[n].inst2);
408          inst = code->inst[n].inst2;
409          /* JUMP_FUNC JUMP_ANY*/
410          fprintf(stderr, "0x%02x %1x ", inst >> 8 & 0xff, (inst & R500_FC_JUMP_ANY) >> 5);
411 
412          /* OP */
413          switch (inst & 0x7) {
414          case R500_FC_OP_JUMP: fprintf(stderr, "JUMP"); break;
415          case R500_FC_OP_LOOP: fprintf(stderr, "LOOP"); break;
416          case R500_FC_OP_ENDLOOP: fprintf(stderr, "ENDLOOP"); break;
417          case R500_FC_OP_REP: fprintf(stderr, "REP"); break;
418          case R500_FC_OP_ENDREP: fprintf(stderr, "ENDREP"); break;
419          case R500_FC_OP_BREAKLOOP: fprintf(stderr, "BREAKLOOP"); break;
420          case R500_FC_OP_BREAKREP: fprintf(stderr, "BREAKREP"); break;
421          case R500_FC_OP_CONTINUE: fprintf(stderr, "CONTINUE"); break;
422          }
423          fprintf(stderr, " ");
424          /* A_OP */
425          switch (inst & (0x3 << 6)) {
426          case R500_FC_A_OP_NONE: fprintf(stderr, "NONE"); break;
427          case R500_FC_A_OP_POP: fprintf(stderr, "POP"); break;
428          case R500_FC_A_OP_PUSH: fprintf(stderr, "PUSH"); break;
429          }
430          /* B_OP0 B_OP1 */
431          for (i = 0; i < 2; i++) {
432             fprintf(stderr, " ");
433             switch (inst & (0x3 << (24 + (i * 2)))) {
434             /* R500_FC_B_OP0_NONE
435              * R500_FC_B_OP1_NONE */
436             case 0:
437                fprintf(stderr, "NONE");
438                break;
439             case R500_FC_B_OP0_DECR:
440             case R500_FC_B_OP1_DECR:
441                fprintf(stderr, "DECR");
442                break;
443             case R500_FC_B_OP0_INCR:
444             case R500_FC_B_OP1_INCR:
445                fprintf(stderr, "INCR");
446                break;
447             }
448          }
449          /*POP_CNT B_ELSE */
450          fprintf(stderr, " %d %1x", (inst >> 16) & 0x1f, (inst & R500_FC_B_ELSE) >> 4);
451          inst = code->inst[n].inst3;
452          /* JUMP_ADDR */
453          fprintf(stderr, " %d", inst >> 16);
454 
455          if (code->inst[n].inst2 & R500_FC_IGNORE_UNCOVERED) {
456             fprintf(stderr, " IGN_UNC");
457          }
458          inst = code->inst[n].inst3;
459          fprintf(stderr, "\n\t3:FC_ADDR    0x%08x:", inst);
460          fprintf(stderr, "BOOL: 0x%02x, INT: 0x%02x, JUMP_ADDR: %d, JMP_GLBL: %1x\n", inst & 0x1f,
461                  (inst >> 8) & 0x1f, (inst >> 16) & 0x1ff, inst >> 31);
462          break;
463       case R500_INST_TYPE_TEX:
464          inst = code->inst[n].inst1;
465          fprintf(stderr, "\t1:TEX_INST:  0x%08x: id: %d op:%s, %s, %s %s\n", inst,
466                  (inst >> 16) & 0xf, to_texop((inst >> 22) & 0x7), (inst & (1 << 25)) ? "ACQ" : "",
467                  (inst & (1 << 26)) ? "IGNUNC" : "", (inst & (1 << 27)) ? "UNSCALED" : "SCALED");
468          inst = code->inst[n].inst2;
469          fprintf(stderr, "\t2:TEX_ADDR:  0x%08x: src: %d%s %s/%s/%s/%s dst: %d%s %s/%s/%s/%s\n",
470                  inst, inst & 127, inst & (1 << 7) ? "(rel)" : "", toswiz((inst >> 8) & 0x3),
471                  toswiz((inst >> 10) & 0x3), toswiz((inst >> 12) & 0x3), toswiz((inst >> 14) & 0x3),
472                  (inst >> 16) & 127, inst & (1 << 23) ? "(rel)" : "", toswiz((inst >> 24) & 0x3),
473                  toswiz((inst >> 26) & 0x3), toswiz((inst >> 28) & 0x3),
474                  toswiz((inst >> 30) & 0x3));
475 
476          fprintf(stderr, "\t3:TEX_DXDY:  0x%08x\n", code->inst[n].inst3);
477          break;
478       }
479       fprintf(stderr, "\n");
480    }
481 }
482