Debian Codegraph @

src/gallium/drivers/r300/compiler/r3xx_vertprog.c

raw symbols SCIP
1 /*
2 * Copyright 2009 Nicolai Hähnle <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "radeon_compiler.h"
7
8 #include <stdbool.h>
9 #include <stdio.h>
10
11 #include "r300_reg.h"
12
13 #include "radeon_compiler_util.h"
14 #include "radeon_dataflow.h"
15 #include "radeon_list.h"
16 #include "radeon_program.h"
17 #include "radeon_program_alu.h"
18 #include "radeon_regalloc.h"
19 #include "radeon_remove_constants.h"
20 #include "radeon_swizzle.h"
21
22 #include "util/compiler.h"
23
24 /*
25 * Take an already-setup and valid source then swizzle it appropriately to
26 * obtain a constant ZERO or ONE source.
27 */
28 #define __CONST(x, y) \
29 (PVS_SRC_OPERAND(t_src_index(vp, &vpi->SrcReg[x]), t_swizzle(y), t_swizzle(y), t_swizzle(y), \
30 t_swizzle(y), t_src_class(vpi->SrcReg[x].File), RC_MASK_NONE) | \
31 (vpi->SrcReg[x].RelAddr << 4))
32
33 static unsigned long
34 t_dst_mask(unsigned int mask)
35 {
36 /* RC_MASK_* is equivalent to VSF_FLAG_* */
37 return mask & RC_MASK_XYZW;
38 }
39
40 static unsigned long
41 t_dst_class(rc_register_file file)
42 {
43 switch (file) {
44 default:
45 fprintf(stderr, "%s: Bad register file %i\n", __func__, file);
46 FALLTHROUGH;
47 case RC_FILE_TEMPORARY:
48 return PVS_DST_REG_TEMPORARY;
49 case RC_FILE_OUTPUT:
50 return PVS_DST_REG_OUT;
51 case RC_FILE_ADDRESS:
52 return PVS_DST_REG_A0;
53 }
54 }
55
56 static unsigned long
57 t_dst_index(struct r300_vertex_program_code *vp, struct rc_dst_register *dst)
58 {
59 if (dst->File == RC_FILE_OUTPUT)
60 return vp->outputs[dst->Index];
61
62 return dst->Index;
63 }
64
65 static unsigned long
66 t_src_class(rc_register_file file)
67 {
68 switch (file) {
69 default:
70 fprintf(stderr, "%s: Bad register file %i\n", __func__, file);
71 FALLTHROUGH;
72 case RC_FILE_NONE:
73 case RC_FILE_TEMPORARY:
74 return PVS_SRC_REG_TEMPORARY;
75 case RC_FILE_INPUT:
76 return PVS_SRC_REG_INPUT;
77 case RC_FILE_CONSTANT:
78 return PVS_SRC_REG_CONSTANT;
79 }
80 }
81
82 static int
83 t_src_conflict(struct rc_src_register a, struct rc_src_register b)
84 {
85 unsigned long aclass = t_src_class(a.File);
86 unsigned long bclass = t_src_class(b.File);
87
88 if (aclass != bclass)
89 return 0;
90 if (aclass == PVS_SRC_REG_TEMPORARY)
91 return 0;
92
93 if (a.RelAddr || b.RelAddr)
94 return 1;
95 if (a.Index != b.Index)
96 return 1;
97
98 return 0;
99 }
100
101 static inline unsigned long
102 t_swizzle(unsigned int swizzle)
103 {
104 /* this is in fact a NOP as the Mesa RC_SWIZZLE_* are all identical to VSF_IN_COMPONENT_* */
105 return swizzle;
106 }
107
108 static unsigned long
109 t_src_index(struct r300_vertex_program_code *vp, struct rc_src_register *src)
110 {
111 if (src->File == RC_FILE_INPUT) {
112 assert(vp->inputs[src->Index] != -1);
113 return vp->inputs[src->Index];
114 } else {
115 if (src->Index < 0) {
116 fprintf(stderr, "negative offsets for indirect addressing do not work.\n");
117 return 0;
118 }
119 return src->Index;
120 }
121 }
122
123 /* these two functions should probably be merged... */
124
125 static unsigned long
126 t_src(struct r300_vertex_program_code *vp, struct rc_src_register *src)
127 {
128 /* src->Negate uses the RC_MASK_ flags from program_instruction.h,
129 * which equal our VSF_FLAGS_ values, so it's safe to just pass it here.
130 */
131 return PVS_SRC_OPERAND(t_src_index(vp, src), t_swizzle(GET_SWZ(src->Swizzle, 0)),
132 t_swizzle(GET_SWZ(src->Swizzle, 1)), t_swizzle(GET_SWZ(src->Swizzle, 2)),
133 t_swizzle(GET_SWZ(src->Swizzle, 3)), t_src_class(src->File),
134 src->Negate) |
135 (src->RelAddr << 4) | (src->Abs << 3);
136 }
137
138 static unsigned long
139 t_src_scalar(struct r300_vertex_program_code *vp, struct rc_src_register *src)
140 {
141 /* src->Negate uses the RC_MASK_ flags from program_instruction.h,
142 * which equal our VSF_FLAGS_ values, so it's safe to just pass it here.
143 */
144 unsigned int swz = rc_get_scalar_src_swz(src->Swizzle);
145
146 return PVS_SRC_OPERAND(t_src_index(vp, src), t_swizzle(swz), t_swizzle(swz), t_swizzle(swz),
147 t_swizzle(swz), t_src_class(src->File),
148 src->Negate ? RC_MASK_XYZW : RC_MASK_NONE) |
149 (src->RelAddr << 4) | (src->Abs << 3);
150 }
151
152 static int
153 valid_dst(struct r300_vertex_program_code *vp, struct rc_dst_register *dst)
154 {
155 if (dst->File == RC_FILE_OUTPUT && vp->outputs[dst->Index] == -1) {
156 return 0;
157 } else if (dst->File == RC_FILE_ADDRESS) {
158 assert(dst->Index == 0);
159 }
160
161 return 1;
162 }
163
164 static void
165 ei_vector1(struct r300_vertex_program_code *vp, unsigned int hw_opcode,
166 struct rc_sub_instruction *vpi, unsigned int *inst)
167 {
168 inst[0] = PVS_OP_DST_OPERAND(hw_opcode, 0, 0, t_dst_index(vp, &vpi->DstReg),
169 t_dst_mask(vpi->DstReg.WriteMask), t_dst_class(vpi->DstReg.File),
170 vpi->SaturateMode == RC_SATURATE_ZERO_ONE);
171 inst[1] = t_src(vp, &vpi->SrcReg[0]);
172 inst[2] = __CONST(0, RC_SWIZZLE_ZERO);
173 inst[3] = __CONST(0, RC_SWIZZLE_ZERO);
174 }
175
176 static void
177 ei_vector2(struct r300_vertex_program_code *vp, unsigned int hw_opcode,
178 struct rc_sub_instruction *vpi, unsigned int *inst)
179 {
180 inst[0] = PVS_OP_DST_OPERAND(hw_opcode, 0, 0, t_dst_index(vp, &vpi->DstReg),
181 t_dst_mask(vpi->DstReg.WriteMask), t_dst_class(vpi->DstReg.File),
182 vpi->SaturateMode == RC_SATURATE_ZERO_ONE);
183 inst[1] = t_src(vp, &vpi->SrcReg[0]);
184 inst[2] = t_src(vp, &vpi->SrcReg[1]);
185 inst[3] = __CONST(1, RC_SWIZZLE_ZERO);
186 }
187
188 static void
189 ei_math1(struct r300_vertex_program_code *vp, unsigned int hw_opcode,
190 struct rc_sub_instruction *vpi, unsigned int *inst)
191 {
192 inst[0] = PVS_OP_DST_OPERAND(hw_opcode, 1, 0, t_dst_index(vp, &vpi->DstReg),
193 t_dst_mask(vpi->DstReg.WriteMask), t_dst_class(vpi->DstReg.File),
194 vpi->SaturateMode == RC_SATURATE_ZERO_ONE);
195 inst[1] = t_src_scalar(vp, &vpi->SrcReg[0]);
196 inst[2] = __CONST(0, RC_SWIZZLE_ZERO);
197 inst[3] = __CONST(0, RC_SWIZZLE_ZERO);
198 }
199
200 static void
201 ei_math1_select(struct r300_vertex_program_code *vp,
202 unsigned math_mode,
203 unsigned hw_opcode_ieee,
204 unsigned hw_opcode_dx,
205 unsigned hw_opcode_ff,
206 struct rc_sub_instruction *vpi,
207 unsigned int *inst)
208 {
209 unsigned hw_opcode;
210 switch (math_mode) {
211 case RC_MATH_IEEE: hw_opcode = hw_opcode_ieee; break;
212 case RC_MATH_DX: hw_opcode = hw_opcode_dx; break;
213 case RC_MATH_FF: hw_opcode = hw_opcode_ff; break;
214 default:
215 UNREACHABLE("");
216 }
217 ei_math1(vp, hw_opcode, vpi, inst);
218 }
219
220 static void
221 ei_cmp(struct r300_vertex_program_code *vp, struct rc_sub_instruction *vpi, unsigned int *inst)
222 {
223 inst[0] = PVS_OP_DST_OPERAND(VE_COND_MUX_GTE, 0, 0, t_dst_index(vp, &vpi->DstReg),
224 t_dst_mask(vpi->DstReg.WriteMask), t_dst_class(vpi->DstReg.File),
225 vpi->SaturateMode == RC_SATURATE_ZERO_ONE);
226
227 /* Arguments with constant swizzles still count as a unique
228 * temporary, so we should make sure these arguments share a
229 * register index with one of the other arguments. */
230 for (unsigned i = 0; i < 3; i++) {
231 unsigned j = (i + 1) % 3;
232 if (vpi->SrcReg[i].File == RC_FILE_NONE &&
233 (vpi->SrcReg[j].File == RC_FILE_NONE || vpi->SrcReg[j].File == RC_FILE_TEMPORARY)) {
234 vpi->SrcReg[i].Index = vpi->SrcReg[j].Index;
235 break;
236 }
237 }
238
239 inst[1] = t_src(vp, &vpi->SrcReg[0]);
240 inst[2] = t_src(vp, &vpi->SrcReg[2]);
241 inst[3] = t_src(vp, &vpi->SrcReg[1]);
242 }
243
244 static void
245 ei_lit(struct r300_vertex_program_code *vp, struct rc_sub_instruction *vpi, unsigned int *inst)
246 {
247 // LIT TMP 1.Y Z TMP 1{} {X W Z Y} TMP 1{} {Y W Z X} TMP 1{} {Y X Z W}
248
249 inst[0] = PVS_OP_DST_OPERAND(ME_LIGHT_COEFF_DX, 1, 0, t_dst_index(vp, &vpi->DstReg),
250 t_dst_mask(vpi->DstReg.WriteMask), t_dst_class(vpi->DstReg.File),
251 vpi->SaturateMode == RC_SATURATE_ZERO_ONE);
252 /* NOTE: Users swizzling might not work. */
253 inst[1] = PVS_SRC_OPERAND(t_src_index(vp, &vpi->SrcReg[0]),
254 t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 0)), // X
255 t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 3)), // W
256 PVS_SRC_SELECT_FORCE_0, // Z
257 t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 1)), // Y
258 t_src_class(vpi->SrcReg[0].File),
259 vpi->SrcReg[0].Negate ? RC_MASK_XYZW : RC_MASK_NONE) |
260 (vpi->SrcReg[0].RelAddr << 4);
261 inst[2] = PVS_SRC_OPERAND(t_src_index(vp, &vpi->SrcReg[0]),
262 t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 1)), // Y
263 t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 3)), // W
264 PVS_SRC_SELECT_FORCE_0, // Z
265 t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 0)), // X
266 t_src_class(vpi->SrcReg[0].File),
267 vpi->SrcReg[0].Negate ? RC_MASK_XYZW : RC_MASK_NONE) |
268 (vpi->SrcReg[0].RelAddr << 4);
269 inst[3] = PVS_SRC_OPERAND(t_src_index(vp, &vpi->SrcReg[0]),
270 t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 1)), // Y
271 t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 0)), // X
272 PVS_SRC_SELECT_FORCE_0, // Z
273 t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 3)), // W
274 t_src_class(vpi->SrcReg[0].File),
275 vpi->SrcReg[0].Negate ? RC_MASK_XYZW : RC_MASK_NONE) |
276 (vpi->SrcReg[0].RelAddr << 4);
277 }
278
279 static void
280 ei_mad(struct r300_vertex_program_code *vp, struct rc_sub_instruction *vpi, unsigned int *inst)
281 {
282 unsigned int i;
283 /* Remarks about hardware limitations of MAD
284 * (please preserve this comment, as this information is _NOT_
285 * in the documentation provided by AMD).
286 *
287 * As described in the documentation, MAD with three unique temporary
288 * source registers requires the use of the macro version.
289 *
290 * However (and this is not mentioned in the documentation), apparently
291 * the macro version is _NOT_ a full superset of the normal version.
292 * In particular, the macro version does not always work when relative
293 * addressing is used in the source operands.
294 *
295 * This limitation caused incorrect rendering in Sauerbraten's OpenGL
296 * assembly shader path when using medium quality animations
297 * (i.e. animations with matrix blending instead of quaternion blending).
298 *
299 * Unfortunately, I (nha) have been unable to extract a Piglit regression
300 * test for this issue - for some reason, it is possible to have vertex
301 * programs whose prefix is *exactly* the same as the prefix of the
302 * offending program in Sauerbraten up to the offending instruction
303 * without causing any trouble.
304 *
305 * Bottom line: Only use the macro version only when really necessary;
306 * according to AMD docs, this should improve performance by one clock
307 * as a nice side bonus.
308 */
309 if (vpi->SrcReg[0].File == RC_FILE_TEMPORARY && vpi->SrcReg[1].File == RC_FILE_TEMPORARY &&
310 vpi->SrcReg[2].File == RC_FILE_TEMPORARY && vpi->SrcReg[0].Index != vpi->SrcReg[1].Index &&
311 vpi->SrcReg[0].Index != vpi->SrcReg[2].Index &&
312 vpi->SrcReg[1].Index != vpi->SrcReg[2].Index) {
313 inst[0] = PVS_OP_DST_OPERAND(PVS_MACRO_OP_2CLK_MADD, 0, 1, t_dst_index(vp, &vpi->DstReg),
314 t_dst_mask(vpi->DstReg.WriteMask), t_dst_class(vpi->DstReg.File),
315 vpi->SaturateMode == RC_SATURATE_ZERO_ONE);
316 } else {
317 inst[0] = PVS_OP_DST_OPERAND(VE_MULTIPLY_ADD, 0, 0, t_dst_index(vp, &vpi->DstReg),
318 t_dst_mask(vpi->DstReg.WriteMask), t_dst_class(vpi->DstReg.File),
319 vpi->SaturateMode == RC_SATURATE_ZERO_ONE);
320
321 /* Arguments with constant swizzles still count as a unique
322 * temporary, so we should make sure these arguments share a
323 * register index with one of the other arguments. */
324 for (i = 0; i < 3; i++) {
325 unsigned int j;
326 if (vpi->SrcReg[i].File != RC_FILE_NONE)
327 continue;
328
329 for (j = 0; j < 3; j++) {
330 if (i != j) {
331 vpi->SrcReg[i].Index = vpi->SrcReg[j].Index;
332 break;
333 }
334 }
335 }
336 }
337 inst[1] = t_src(vp, &vpi->SrcReg[0]);
338 inst[2] = t_src(vp, &vpi->SrcReg[1]);
339 inst[3] = t_src(vp, &vpi->SrcReg[2]);
340 }
341
342 static void
343 ei_pow(struct r300_vertex_program_code *vp, struct rc_sub_instruction *vpi, unsigned int *inst)
344 {
345 inst[0] = PVS_OP_DST_OPERAND(ME_POWER_FUNC_FF, 1, 0, t_dst_index(vp, &vpi->DstReg),
346 t_dst_mask(vpi->DstReg.WriteMask), t_dst_class(vpi->DstReg.File),
347 vpi->SaturateMode == RC_SATURATE_ZERO_ONE);
348 inst[1] = t_src_scalar(vp, &vpi->SrcReg[0]);
349 inst[2] = __CONST(0, RC_SWIZZLE_ZERO);
350 inst[3] = t_src_scalar(vp, &vpi->SrcReg[1]);
351 }
352
353 static void
354 ei_vector0(struct r300_vertex_program_code *vp,
355 struct rc_sub_instruction *vpi,
356 unsigned int hw_opcode,
357 unsigned int *inst)
358 {
359 inst[0] = PVS_OP_DST_OPERAND(hw_opcode, 0, 0, 0, 0, PVS_DST_REG_TEMPORARY, 0);
360 inst[1] = __CONST(0, RC_SWIZZLE_ZERO);
361 inst[2] = inst[1];
362 inst[3] = inst[1];
363 }
364
365 static void
366 pad_vertex_program_instructions(struct radeon_compiler *c)
367 {
368 const unsigned min_inst_count = 4;
369 unsigned inst_count = 0;
370
371 for (struct rc_instruction *inst = c->Program.Instructions.Next;
372 inst != &c->Program.Instructions; inst = inst->Next)
373 inst_count++;
374
375 unsigned orig_inst_count = inst_count;
376
377 while (inst_count < min_inst_count) {
378 struct rc_instruction *inst = rc_insert_new_instruction(c, c->Program.Instructions.Prev);
379 inst->U.I.Opcode = RC_OPCODE_NOP;
380 inst->U.I.SrcReg[0].File = RC_FILE_NONE;
381 inst_count++;
382 }
383
384 if (orig_inst_count < min_inst_count)
385 rc_debug(c, "r300: padded tiny VS from %u to %u instructions\n",
386 orig_inst_count, inst_count);
387 }
388
389 static void
390 translate_vertex_program(struct radeon_compiler *c, void *user)
391 {
392 struct r300_vertex_program_compiler *compiler = (struct r300_vertex_program_compiler *)c;
393 struct rc_instruction *rci;
394
395 unsigned loops[R500_PVS_MAX_LOOP_DEPTH] = {};
396 unsigned loop_depth = 0;
397 bool last_input_read_at_loop_end = false;
398 bool last_pos_write_at_loop_end = false;
399
400 compiler->code->pos_end = 0; /* Not supported yet */
401 compiler->code->length = 0;
402 compiler->code->num_temporaries = 0;
403 compiler->code->last_input_read = 0;
404 compiler->code->last_pos_write = 0;
405
406 compiler->SetHwInputOutput(compiler);
407
408 /* Vertex shaders shorter than 4 instructions can hard-lock r3xx GPUs. */
409 if (!c->is_r400 && !c->is_r500)
410 pad_vertex_program_instructions(c);
411
412 for (rci = compiler->Base.Program.Instructions.Next; rci != &compiler->Base.Program.Instructions;
413 rci = rci->Next) {
414 struct rc_sub_instruction *vpi = &rci->U.I;
415 unsigned int *inst = compiler->code->body.d + compiler->code->length;
416 const struct rc_opcode_info *info = rc_get_opcode_info(vpi->Opcode);
417
418 /* Skip instructions writing to non-existing destination */
419 if (!valid_dst(compiler->code, &vpi->DstReg))
420 continue;
421
422 if (info->HasDstReg) {
423 /* Neither is Saturate. */
424 if (vpi->SaturateMode != RC_SATURATE_NONE && !c->is_r500) {
425 rc_error(&compiler->Base, "Vertex program does not support the Saturate "
426 "modifier (yet).\n");
427 }
428 }
429
430 if (compiler->code->length >= c->max_alu_insts * 4) {
431 rc_error(&compiler->Base, "Vertex program has too many instructions\n");
432 return;
433 }
434
435 assert(compiler->Base.is_r500 ||
436 (vpi->Opcode != RC_OPCODE_SEQ && vpi->Opcode != RC_OPCODE_SNE));
437
438 switch (vpi->Opcode) {
439 case RC_OPCODE_NOP:
440 ei_vector0(compiler->code, vpi, VECTOR_NO_OP, inst);
441 break;
442 case RC_OPCODE_ADD:
443 ei_vector2(compiler->code, VE_ADD, vpi, inst);
444 break;
445 case RC_OPCODE_ARL:
446 ei_vector1(compiler->code, VE_FLT2FIX_DX, vpi, inst);
447 break;
448 case RC_OPCODE_ARR:
449 ei_vector1(compiler->code, VE_FLT2FIX_DX_RND, vpi, inst);
450 break;
451 case RC_OPCODE_COS:
452 ei_math1(compiler->code, ME_COS, vpi, inst);
453 break;
454 case RC_OPCODE_CMP:
455 ei_cmp(compiler->code, vpi, inst);
456 break;
457 case RC_OPCODE_DP4:
458 ei_vector2(compiler->code, VE_DOT_PRODUCT, vpi, inst);
459 break;
460 case RC_OPCODE_DST:
461 ei_vector2(compiler->code, VE_DISTANCE_VECTOR, vpi, inst);
462 break;
463 case RC_OPCODE_EX2:
464 ei_math1(compiler->code, ME_EXP_BASE2_FULL_DX, vpi, inst);
465 break;
466 case RC_OPCODE_EXP:
467 ei_math1(compiler->code, ME_EXP_BASE2_DX, vpi, inst);
468 break;
469 case RC_OPCODE_FRC:
470 ei_vector1(compiler->code, VE_FRACTION, vpi, inst);
471 break;
472 case RC_OPCODE_LG2:
473 ei_math1_select(compiler->code, compiler->Base.math_rules, ME_LOG_BASE2_IEEE,
474 ME_LOG_BASE2_FULL_DX, ME_LOG_BASE2_FULL_DX, vpi, inst);
475 break;
476 case RC_OPCODE_LIT:
477 ei_lit(compiler->code, vpi, inst);
478 break;
479 case RC_OPCODE_LOG:
480 ei_math1(compiler->code, ME_LOG_BASE2_DX, vpi, inst);
481 break;
482 case RC_OPCODE_MAD:
483 ei_mad(compiler->code, vpi, inst);
484 break;
485 case RC_OPCODE_MAX:
486 ei_vector2(compiler->code, VE_MAXIMUM, vpi, inst);
487 break;
488 case RC_OPCODE_MIN:
489 ei_vector2(compiler->code, VE_MINIMUM, vpi, inst);
490 break;
491 case RC_OPCODE_MOV:
492 ei_vector1(compiler->code, VE_ADD, vpi, inst);
493 break;
494 case RC_OPCODE_MUL:
495 ei_vector2(compiler->code, VE_MULTIPLY, vpi, inst);
496 break;
497 case RC_OPCODE_POW:
498 ei_pow(compiler->code, vpi, inst);
499 break;
500 case RC_OPCODE_RCP:
501 ei_math1_select(compiler->code, compiler->Base.math_rules, ME_RECIP_IEEE,
502 ME_RECIP_DX, ME_RECIP_FF, vpi, inst);
503 break;
504 case RC_OPCODE_RSQ:
505 ei_math1_select(compiler->code, compiler->Base.math_rules, ME_RECIP_SQRT_IEEE,
506 ME_RECIP_SQRT_DX, ME_RECIP_SQRT_FF, vpi, inst);
507 break;
508 case RC_OPCODE_SEQ:
509 ei_vector2(compiler->code, VE_SET_EQUAL, vpi, inst);
510 break;
511 case RC_OPCODE_SGE:
512 ei_vector2(compiler->code, VE_SET_GREATER_THAN_EQUAL, vpi, inst);
513 break;
514 case RC_OPCODE_SIN:
515 ei_math1(compiler->code, ME_SIN, vpi, inst);
516 break;
517 case RC_OPCODE_SLT:
518 ei_vector2(compiler->code, VE_SET_LESS_THAN, vpi, inst);
519 break;
520 case RC_OPCODE_SNE:
521 ei_vector2(compiler->code, VE_SET_NOT_EQUAL, vpi, inst);
522 break;
523 case RC_OPCODE_BGNLOOP: {
524 if ((!compiler->Base.is_r500 && loop_depth >= R300_VS_MAX_LOOP_DEPTH) ||
525 loop_depth >= R500_PVS_MAX_LOOP_DEPTH) {
526 rc_error(&compiler->Base, "Loops are nested too deep.");
527 return;
528 }
529 loops[loop_depth++] = ((compiler->code->length) / 4) + 1;
530 break;
531 }
532 case RC_OPCODE_ENDLOOP: {
533 unsigned int act_addr;
534 unsigned int last_addr;
535 unsigned int ret_addr;
536
537 if (loop_depth == 1 && last_input_read_at_loop_end) {
538 compiler->code->last_input_read = compiler->code->length / 4;
539 last_input_read_at_loop_end = false;
540 }
541 if (loop_depth == 1 && last_pos_write_at_loop_end) {
542 compiler->code->last_pos_write = compiler->code->length / 4;
543 last_pos_write_at_loop_end = false;
544 }
545
546 ret_addr = loops[--loop_depth];
547 act_addr = ret_addr - 1;
548 last_addr = (compiler->code->length / 4) - 1;
549
550 if (loop_depth >= R300_VS_MAX_FC_OPS) {
551 rc_error(&compiler->Base, "Too many flow control instructions.");
552 return;
553 }
554 /* Maximum of R500_PVS_FC_LOOP_CNT_JMP_INST is 0xff, here
555 * we reduce it to half to avoid occasional hangs on RV516
556 * and downclocked RV530.
557 */
558 if (compiler->Base.is_r500) {
559 compiler->code->fc_op_addrs.r500[compiler->code->num_fc_ops].lw =
560 R500_PVS_FC_ACT_ADRS(act_addr) | R500_PVS_FC_LOOP_CNT_JMP_INST(0x0080);
561 compiler->code->fc_op_addrs.r500[compiler->code->num_fc_ops].uw =
562 R500_PVS_FC_LAST_INST(last_addr) | R500_PVS_FC_RTN_INST(ret_addr);
563 } else {
564 compiler->code->fc_op_addrs.r300[compiler->code->num_fc_ops] =
565 R300_PVS_FC_ACT_ADRS(act_addr) | R300_PVS_FC_LOOP_CNT_JMP_INST(0xff) |
566 R300_PVS_FC_LAST_INST(last_addr) | R300_PVS_FC_RTN_INST(ret_addr);
567 }
568 compiler->code->fc_loop_index[compiler->code->num_fc_ops] =
569 R300_PVS_FC_LOOP_INIT_VAL(0x0) | R300_PVS_FC_LOOP_STEP_VAL(0x1);
570 compiler->code->fc_ops |= R300_VAP_PVS_FC_OPC_LOOP(compiler->code->num_fc_ops);
571 compiler->code->num_fc_ops++;
572
573 break;
574 }
575
576 case RC_ME_PRED_SET_CLR:
577 ei_math1(compiler->code, ME_PRED_SET_CLR, vpi, inst);
578 break;
579
580 case RC_ME_PRED_SET_INV:
581 ei_math1(compiler->code, ME_PRED_SET_INV, vpi, inst);
582 break;
583
584 case RC_ME_PRED_SET_POP:
585 ei_math1(compiler->code, ME_PRED_SET_POP, vpi, inst);
586 break;
587
588 case RC_ME_PRED_SET_RESTORE:
589 ei_math1(compiler->code, ME_PRED_SET_RESTORE, vpi, inst);
590 break;
591
592 case RC_ME_PRED_SEQ:
593 ei_math1(compiler->code, ME_PRED_SET_EQ, vpi, inst);
594 break;
595
596 case RC_ME_PRED_SNEQ:
597 ei_math1(compiler->code, ME_PRED_SET_NEQ, vpi, inst);
598 break;
599
600 case RC_VE_PRED_SNEQ_PUSH:
601 ei_vector2(compiler->code, VE_PRED_SET_NEQ_PUSH, vpi, inst);
602 break;
603
604 default:
605 rc_error(&compiler->Base, "Unknown opcode %s\n", info->Name);
606 return;
607 }
608
609 if (vpi->DstReg.Pred != RC_PRED_DISABLED) {
610 inst[0] |= (PVS_DST_PRED_ENABLE_MASK << PVS_DST_PRED_ENABLE_SHIFT);
611 if (vpi->DstReg.Pred == RC_PRED_SET) {
612 inst[0] |= (PVS_DST_PRED_SENSE_MASK << PVS_DST_PRED_SENSE_SHIFT);
613 }
614 }
615
616 /* Update the number of temporaries. */
617 if (info->HasDstReg && vpi->DstReg.File == RC_FILE_TEMPORARY &&
618 vpi->DstReg.Index >= compiler->code->num_temporaries)
619 compiler->code->num_temporaries = vpi->DstReg.Index + 1;
620
621 /* last instruction that writes position */
622 if (info->HasDstReg && vpi->DstReg.File == RC_FILE_OUTPUT &&
623 t_dst_index(compiler->code, &vpi->DstReg) == 0) {
624 if (loop_depth == 0)
625 compiler->code->last_pos_write = compiler->code->length / 4;
626 else
627 last_pos_write_at_loop_end = true;
628 }
629
630 for (unsigned i = 0; i < info->NumSrcRegs; i++) {
631 if (vpi->SrcReg[i].File == RC_FILE_TEMPORARY &&
632 vpi->SrcReg[i].Index >= compiler->code->num_temporaries)
633 compiler->code->num_temporaries = vpi->SrcReg[i].Index + 1;
634 if (vpi->SrcReg[i].File == RC_FILE_INPUT) {
635 if (loop_depth == 0)
636 compiler->code->last_input_read = compiler->code->length / 4;
637 else
638 last_input_read_at_loop_end = true;
639 }
640 }
641
642 if (compiler->code->num_temporaries > compiler->Base.max_temp_regs) {
643 rc_error(&compiler->Base, "Too many temporaries.\n");
644 return;
645 }
646
647 compiler->code->length += 4;
648
649 if (compiler->Base.Error)
650 return;
651 }
652 }
653
654 struct temporary_allocation {
655 unsigned int Allocated : 1;
656 unsigned int HwTemp : 15;
657 struct rc_instruction *LastRead;
658 };
659
660 static int
661 get_reg(struct radeon_compiler *c, struct temporary_allocation *ta, bool *hwtemps,
662 unsigned int orig)
663 {
664 if (!ta[orig].Allocated) {
665 int j;
666 for (j = 0; j < c->max_temp_regs; ++j) {
667 if (!hwtemps[j])
668 break;
669 }
670 ta[orig].Allocated = 1;
671 ta[orig].HwTemp = j;
672 hwtemps[ta[orig].HwTemp] = true;
673 }
674
675 return ta[orig].HwTemp;
676 }
677
678 static void
679 allocate_temporary_registers(struct radeon_compiler *c, void *user)
680 {
681 unsigned int node_count, node_index;
682 struct ra_class **node_classes;
683 struct rc_list *var_ptr;
684 struct rc_list *variables;
685 struct ra_graph *graph;
686 const struct rc_regalloc_state *ra_state = c->regalloc_state;
687
688 rc_recompute_ips(c);
689
690 /* Get list of program variables */
691 variables = rc_get_variables(c);
692 node_count = rc_list_count(variables);
693 node_classes = memory_pool_malloc(&c->Pool, node_count * sizeof(struct ra_class *));
694
695 for (var_ptr = variables, node_index = 0; var_ptr; var_ptr = var_ptr->Next, node_index++) {
696 unsigned int class_index = 0;
697 int index;
698 /* Compute the live intervals */
699 rc_variable_compute_live_intervals(var_ptr->Item);
700 unsigned int writemask = rc_variable_writemask_sum(var_ptr->Item);
701 index = rc_find_class(c->regalloc_state->class_list, writemask, 6);
702 if (index > -1) {
703 class_index = c->regalloc_state->class_list[index].ID;
704 } else {
705 rc_error(c, "Could not find class for index=%u mask=%u\n",
706 ((struct rc_variable *)var_ptr->Item)->Dst.Index, writemask);
707 }
708 node_classes[node_index] = ra_state->classes[class_index];
709 }
710
711 graph = ra_alloc_interference_graph(ra_state->regs, node_count);
712
713 for (node_index = 0; node_index < node_count; node_index++) {
714 ra_set_node_class(graph, node_index, node_classes[node_index]);
715 }
716
717 rc_build_interference_graph(graph, variables);
718
719 if (!ra_allocate(graph)) {
720 rc_error(c, "Ran out of hardware temporaries\n");
721 ralloc_free(graph);
722 return;
723 }
724
725 /* Rewrite the registers */
726 for (var_ptr = variables, node_index = 0; var_ptr; var_ptr = var_ptr->Next, node_index++) {
727 int reg = ra_get_node_reg(graph, node_index);
728 unsigned int writemask = reg_get_writemask(reg);
729 unsigned int index = reg_get_index(reg);
730 struct rc_variable *var = var_ptr->Item;
731
732 rc_variable_change_dst(var, index, writemask);
733 }
734
735 ralloc_free(graph);
736 }
737
738 /**
739 * Vertex engine cannot read two inputs or two constants at the same time.
740 * Introduce intermediate MOVs to temporary registers to account for this.
741 */
742 static int
743 transform_source_conflicts(struct radeon_compiler *c, struct rc_instruction *inst, void *unused)
744 {
745 const struct rc_opcode_info *opcode = rc_get_opcode_info(inst->U.I.Opcode);
746
747 if (opcode->NumSrcRegs == 3) {
748 if (t_src_conflict(inst->U.I.SrcReg[1], inst->U.I.SrcReg[2]) ||
749 t_src_conflict(inst->U.I.SrcReg[0], inst->U.I.SrcReg[2])) {
750 int tmpreg = rc_find_free_temporary(c);
751 struct rc_instruction *inst_mov = rc_insert_new_instruction(c, inst->Prev);
752 inst_mov->U.I.Opcode = RC_OPCODE_MOV;
753 inst_mov->U.I.DstReg.File = RC_FILE_TEMPORARY;
754 inst_mov->U.I.DstReg.Index = tmpreg;
755 inst_mov->U.I.SrcReg[0] = inst->U.I.SrcReg[2];
756 inst_mov->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_XYZW;
757 inst_mov->U.I.SrcReg[0].Negate = 0;
758 inst_mov->U.I.SrcReg[0].Abs = 0;
759
760 inst->U.I.SrcReg[2].File = RC_FILE_TEMPORARY;
761 inst->U.I.SrcReg[2].Index = tmpreg;
762 inst->U.I.SrcReg[2].RelAddr = false;
763 }
764 }
765
766 if (opcode->NumSrcRegs >= 2) {
767 if (t_src_conflict(inst->U.I.SrcReg[1], inst->U.I.SrcReg[0])) {
768 int tmpreg = rc_find_free_temporary(c);
769 struct rc_instruction *inst_mov = rc_insert_new_instruction(c, inst->Prev);
770 inst_mov->U.I.Opcode = RC_OPCODE_MOV;
771 inst_mov->U.I.DstReg.File = RC_FILE_TEMPORARY;
772 inst_mov->U.I.DstReg.Index = tmpreg;
773 inst_mov->U.I.SrcReg[0] = inst->U.I.SrcReg[1];
774 inst_mov->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_XYZW;
775 inst_mov->U.I.SrcReg[0].Negate = 0;
776 inst_mov->U.I.SrcReg[0].Abs = 0;
777
778 inst->U.I.SrcReg[1].File = RC_FILE_TEMPORARY;
779 inst->U.I.SrcReg[1].Index = tmpreg;
780 inst->U.I.SrcReg[1].RelAddr = false;
781 }
782 }
783
784 return 1;
785 }
786
787 static void
788 rc_vs_add_artificial_outputs(struct radeon_compiler *c, void *user)
789 {
790 struct r300_vertex_program_compiler *compiler = (struct r300_vertex_program_compiler *)c;
791 int i;
792
793 for (i = 0; i < 32; ++i) {
794 if ((compiler->RequiredOutputs & (1U << i)) &&
795 !(compiler->Base.Program.OutputsWritten & (1U << i))) {
796 struct rc_instruction *inst =
797 rc_insert_new_instruction(&compiler->Base, compiler->Base.Program.Instructions.Prev);
798 inst->U.I.Opcode = RC_OPCODE_MOV;
799
800 inst->U.I.DstReg.File = RC_FILE_OUTPUT;
801 inst->U.I.DstReg.Index = i;
802 inst->U.I.DstReg.WriteMask = RC_MASK_XYZW;
803
804 inst->U.I.SrcReg[0].File = RC_FILE_CONSTANT;
805 inst->U.I.SrcReg[0].Index = 0;
806 inst->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_XYZW;
807
808 compiler->Base.Program.OutputsWritten |= 1U << i;
809 }
810 }
811 }
812
813 static int
814 swizzle_is_native(rc_opcode opcode, struct rc_src_register reg)
815 {
816 (void)opcode;
817 (void)reg;
818
819 return 1;
820 }
821
822 const struct rc_swizzle_caps r300_vertprog_swizzle_caps = {
823 .IsNative = &swizzle_is_native, .Split = NULL /* should never be called */
824 };
825
826 void
827 r3xx_compile_vertex_program(struct r300_vertex_program_compiler *c)
828 {
829 int is_r500 = c->Base.is_r500;
830 int opt = !c->Base.disable_optimizations;
831 bool debug = c->Base.Debug & RC_DBG_LOG;
832
833 /* Lists of instruction transformations. */
834 struct radeon_program_transformation alu_rewrite[] = {{&r300_transform_vertex_alu, NULL},
835 {NULL, NULL}};
836
837 struct radeon_program_transformation resolve_src_conflicts[] = {
838 {&transform_source_conflicts, NULL},
839 {NULL, NULL}};
840
841 /* List of compiler passes. */
842 struct radeon_compiler_pass vs_list[] = {
843 /* clang-format off */
844 /* NAME DUMP PREDICATE FUNCTION PARAM */
845 {"add artificial outputs", 0, 1, rc_vs_add_artificial_outputs, NULL},
846 {"native rewrite", 1, 1, rc_local_transform, alu_rewrite},
847 {"unused channels", 1, opt, rc_mark_unused_channels, NULL},
848 {"dataflow optimize", 1, opt, rc_optimize, NULL},
849 {"dead constants", 1, 1, rc_remove_unused_constants, &c->code->constants_remap_table},
850 /* This pass must be done after optimizations. */
851 {"source conflict resolve", 1, 1, rc_local_transform, resolve_src_conflicts},
852 {"register allocation", 1, opt, allocate_temporary_registers, NULL},
853 {"lower control flow opcodes", 1, is_r500, rc_vert_fc, NULL},
854 {"final code validation", 0, 1, rc_validate_final_shader, NULL},
855 {"machine code generation", 0, 1, translate_vertex_program, NULL},
856 {"dump machine code", 0, debug, r300_vertex_program_dump, NULL},
857 {NULL, 0, 0, NULL, NULL}};
858 /* clang-format on */
859
860 c->Base.type = RC_VERTEX_PROGRAM;
861 c->Base.SwizzleCaps = &r300_vertprog_swizzle_caps;
862
863 rc_run_compiler(&c->Base, vs_list);
864
865 c->code->InputsRead = c->Base.Program.InputsRead;
866 c->code->OutputsWritten = c->Base.Program.OutputsWritten;
867 rc_constants_copy(&c->code->constants, &c->Base.Program.Constants);
868 }