Merge remote branch 'origin/master' into nv50-compiler
[mesa.git] / src / mesa / drivers / dri / r300 / compiler / r3xx_vertprog.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_compiler.h"
24
25 #include <stdio.h>
26
27 #include "../r300_reg.h"
28
29 #include "radeon_dataflow.h"
30 #include "radeon_program_alu.h"
31 #include "radeon_swizzle.h"
32 #include "radeon_emulate_branches.h"
33 #include "radeon_emulate_loops.h"
34 #include "radeon_remove_constants.h"
35
36 struct loop {
37 int BgnLoop;
38
39 };
40
41 /*
42 * Take an already-setup and valid source then swizzle it appropriately to
43 * obtain a constant ZERO or ONE source.
44 */
45 #define __CONST(x, y) \
46 (PVS_SRC_OPERAND(t_src_index(vp, &vpi->SrcReg[x]), \
47 t_swizzle(y), \
48 t_swizzle(y), \
49 t_swizzle(y), \
50 t_swizzle(y), \
51 t_src_class(vpi->SrcReg[x].File), \
52 RC_MASK_NONE) | (vpi->SrcReg[x].RelAddr << 4))
53
54
55 static unsigned long t_dst_mask(unsigned int mask)
56 {
57 /* RC_MASK_* is equivalent to VSF_FLAG_* */
58 return mask & RC_MASK_XYZW;
59 }
60
61 static unsigned long t_dst_class(rc_register_file file)
62 {
63 switch (file) {
64 default:
65 fprintf(stderr, "%s: Bad register file %i\n", __FUNCTION__, file);
66 /* fall-through */
67 case RC_FILE_TEMPORARY:
68 return PVS_DST_REG_TEMPORARY;
69 case RC_FILE_OUTPUT:
70 return PVS_DST_REG_OUT;
71 case RC_FILE_ADDRESS:
72 return PVS_DST_REG_A0;
73 }
74 }
75
76 static unsigned long t_dst_index(struct r300_vertex_program_code *vp,
77 struct rc_dst_register *dst)
78 {
79 if (dst->File == RC_FILE_OUTPUT)
80 return vp->outputs[dst->Index];
81
82 return dst->Index;
83 }
84
85 static unsigned long t_src_class(rc_register_file file)
86 {
87 switch (file) {
88 default:
89 fprintf(stderr, "%s: Bad register file %i\n", __FUNCTION__, file);
90 /* fall-through */
91 case RC_FILE_NONE:
92 case RC_FILE_TEMPORARY:
93 return PVS_SRC_REG_TEMPORARY;
94 case RC_FILE_INPUT:
95 return PVS_SRC_REG_INPUT;
96 case RC_FILE_CONSTANT:
97 return PVS_SRC_REG_CONSTANT;
98 }
99 }
100
101 static int t_src_conflict(struct rc_src_register a, struct rc_src_register b)
102 {
103 unsigned long aclass = t_src_class(a.File);
104 unsigned long bclass = t_src_class(b.File);
105
106 if (aclass != bclass)
107 return 0;
108 if (aclass == PVS_SRC_REG_TEMPORARY)
109 return 0;
110
111 if (a.RelAddr || b.RelAddr)
112 return 1;
113 if (a.Index != b.Index)
114 return 1;
115
116 return 0;
117 }
118
119 static inline unsigned long t_swizzle(unsigned int swizzle)
120 {
121 /* this is in fact a NOP as the Mesa RC_SWIZZLE_* are all identical to VSF_IN_COMPONENT_* */
122 return swizzle;
123 }
124
125 static unsigned long t_src_index(struct r300_vertex_program_code *vp,
126 struct rc_src_register *src)
127 {
128 if (src->File == RC_FILE_INPUT) {
129 assert(vp->inputs[src->Index] != -1);
130 return vp->inputs[src->Index];
131 } else {
132 if (src->Index < 0) {
133 fprintf(stderr,
134 "negative offsets for indirect addressing do not work.\n");
135 return 0;
136 }
137 return src->Index;
138 }
139 }
140
141 /* these two functions should probably be merged... */
142
143 static unsigned long t_src(struct r300_vertex_program_code *vp,
144 struct rc_src_register *src)
145 {
146 /* src->Negate uses the RC_MASK_ flags from program_instruction.h,
147 * which equal our VSF_FLAGS_ values, so it's safe to just pass it here.
148 */
149 return PVS_SRC_OPERAND(t_src_index(vp, src),
150 t_swizzle(GET_SWZ(src->Swizzle, 0)),
151 t_swizzle(GET_SWZ(src->Swizzle, 1)),
152 t_swizzle(GET_SWZ(src->Swizzle, 2)),
153 t_swizzle(GET_SWZ(src->Swizzle, 3)),
154 t_src_class(src->File),
155 src->Negate) |
156 (src->RelAddr << 4) | (src->Abs << 3);
157 }
158
159 static unsigned long t_src_scalar(struct r300_vertex_program_code *vp,
160 struct rc_src_register *src)
161 {
162 /* src->Negate uses the RC_MASK_ flags from program_instruction.h,
163 * which equal our VSF_FLAGS_ values, so it's safe to just pass it here.
164 */
165 return PVS_SRC_OPERAND(t_src_index(vp, src),
166 t_swizzle(GET_SWZ(src->Swizzle, 0)),
167 t_swizzle(GET_SWZ(src->Swizzle, 0)),
168 t_swizzle(GET_SWZ(src->Swizzle, 0)),
169 t_swizzle(GET_SWZ(src->Swizzle, 0)),
170 t_src_class(src->File),
171 src->Negate ? RC_MASK_XYZW : RC_MASK_NONE) |
172 (src->RelAddr << 4) | (src->Abs << 3);
173 }
174
175 static int valid_dst(struct r300_vertex_program_code *vp,
176 struct rc_dst_register *dst)
177 {
178 if (dst->File == RC_FILE_OUTPUT && vp->outputs[dst->Index] == -1) {
179 return 0;
180 } else if (dst->File == RC_FILE_ADDRESS) {
181 assert(dst->Index == 0);
182 }
183
184 return 1;
185 }
186
187 static void ei_vector1(struct r300_vertex_program_code *vp,
188 unsigned int hw_opcode,
189 struct rc_sub_instruction *vpi,
190 unsigned int * inst)
191 {
192 inst[0] = PVS_OP_DST_OPERAND(hw_opcode,
193 0,
194 0,
195 t_dst_index(vp, &vpi->DstReg),
196 t_dst_mask(vpi->DstReg.WriteMask),
197 t_dst_class(vpi->DstReg.File));
198 inst[1] = t_src(vp, &vpi->SrcReg[0]);
199 inst[2] = __CONST(0, RC_SWIZZLE_ZERO);
200 inst[3] = __CONST(0, RC_SWIZZLE_ZERO);
201 }
202
203 static void ei_vector2(struct r300_vertex_program_code *vp,
204 unsigned int hw_opcode,
205 struct rc_sub_instruction *vpi,
206 unsigned int * inst)
207 {
208 inst[0] = PVS_OP_DST_OPERAND(hw_opcode,
209 0,
210 0,
211 t_dst_index(vp, &vpi->DstReg),
212 t_dst_mask(vpi->DstReg.WriteMask),
213 t_dst_class(vpi->DstReg.File));
214 inst[1] = t_src(vp, &vpi->SrcReg[0]);
215 inst[2] = t_src(vp, &vpi->SrcReg[1]);
216 inst[3] = __CONST(1, RC_SWIZZLE_ZERO);
217 }
218
219 static void ei_math1(struct r300_vertex_program_code *vp,
220 unsigned int hw_opcode,
221 struct rc_sub_instruction *vpi,
222 unsigned int * inst)
223 {
224 inst[0] = PVS_OP_DST_OPERAND(hw_opcode,
225 1,
226 0,
227 t_dst_index(vp, &vpi->DstReg),
228 t_dst_mask(vpi->DstReg.WriteMask),
229 t_dst_class(vpi->DstReg.File));
230 inst[1] = t_src_scalar(vp, &vpi->SrcReg[0]);
231 inst[2] = __CONST(0, RC_SWIZZLE_ZERO);
232 inst[3] = __CONST(0, RC_SWIZZLE_ZERO);
233 }
234
235 static void ei_lit(struct r300_vertex_program_code *vp,
236 struct rc_sub_instruction *vpi,
237 unsigned int * inst)
238 {
239 //LIT TMP 1.Y Z TMP 1{} {X W Z Y} TMP 1{} {Y W Z X} TMP 1{} {Y X Z W}
240
241 inst[0] = PVS_OP_DST_OPERAND(ME_LIGHT_COEFF_DX,
242 1,
243 0,
244 t_dst_index(vp, &vpi->DstReg),
245 t_dst_mask(vpi->DstReg.WriteMask),
246 t_dst_class(vpi->DstReg.File));
247 /* NOTE: Users swizzling might not work. */
248 inst[1] = PVS_SRC_OPERAND(t_src_index(vp, &vpi->SrcReg[0]), t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 0)), // X
249 t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 3)), // W
250 PVS_SRC_SELECT_FORCE_0, // Z
251 t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 1)), // Y
252 t_src_class(vpi->SrcReg[0].File),
253 vpi->SrcReg[0].Negate ? RC_MASK_XYZW : RC_MASK_NONE) |
254 (vpi->SrcReg[0].RelAddr << 4);
255 inst[2] = PVS_SRC_OPERAND(t_src_index(vp, &vpi->SrcReg[0]), t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 1)), // Y
256 t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 3)), // W
257 PVS_SRC_SELECT_FORCE_0, // Z
258 t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 0)), // X
259 t_src_class(vpi->SrcReg[0].File),
260 vpi->SrcReg[0].Negate ? RC_MASK_XYZW : RC_MASK_NONE) |
261 (vpi->SrcReg[0].RelAddr << 4);
262 inst[3] = PVS_SRC_OPERAND(t_src_index(vp, &vpi->SrcReg[0]), t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 1)), // Y
263 t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 0)), // X
264 PVS_SRC_SELECT_FORCE_0, // Z
265 t_swizzle(GET_SWZ(vpi->SrcReg[0].Swizzle, 3)), // W
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 }
270
271 static void ei_mad(struct r300_vertex_program_code *vp,
272 struct rc_sub_instruction *vpi,
273 unsigned int * inst)
274 {
275 /* Remarks about hardware limitations of MAD
276 * (please preserve this comment, as this information is _NOT_
277 * in the documentation provided by AMD).
278 *
279 * As described in the documentation, MAD with three unique temporary
280 * source registers requires the use of the macro version.
281 *
282 * However (and this is not mentioned in the documentation), apparently
283 * the macro version is _NOT_ a full superset of the normal version.
284 * In particular, the macro version does not always work when relative
285 * addressing is used in the source operands.
286 *
287 * This limitation caused incorrect rendering in Sauerbraten's OpenGL
288 * assembly shader path when using medium quality animations
289 * (i.e. animations with matrix blending instead of quaternion blending).
290 *
291 * Unfortunately, I (nha) have been unable to extract a Piglit regression
292 * test for this issue - for some reason, it is possible to have vertex
293 * programs whose prefix is *exactly* the same as the prefix of the
294 * offending program in Sauerbraten up to the offending instruction
295 * without causing any trouble.
296 *
297 * Bottom line: Only use the macro version only when really necessary;
298 * according to AMD docs, this should improve performance by one clock
299 * as a nice side bonus.
300 */
301 if (vpi->SrcReg[0].File == RC_FILE_TEMPORARY &&
302 vpi->SrcReg[1].File == RC_FILE_TEMPORARY &&
303 vpi->SrcReg[2].File == RC_FILE_TEMPORARY &&
304 vpi->SrcReg[0].Index != vpi->SrcReg[1].Index &&
305 vpi->SrcReg[0].Index != vpi->SrcReg[2].Index &&
306 vpi->SrcReg[1].Index != vpi->SrcReg[2].Index) {
307 inst[0] = PVS_OP_DST_OPERAND(PVS_MACRO_OP_2CLK_MADD,
308 0,
309 1,
310 t_dst_index(vp, &vpi->DstReg),
311 t_dst_mask(vpi->DstReg.WriteMask),
312 t_dst_class(vpi->DstReg.File));
313 } else {
314 inst[0] = PVS_OP_DST_OPERAND(VE_MULTIPLY_ADD,
315 0,
316 0,
317 t_dst_index(vp, &vpi->DstReg),
318 t_dst_mask(vpi->DstReg.WriteMask),
319 t_dst_class(vpi->DstReg.File));
320 }
321 inst[1] = t_src(vp, &vpi->SrcReg[0]);
322 inst[2] = t_src(vp, &vpi->SrcReg[1]);
323 inst[3] = t_src(vp, &vpi->SrcReg[2]);
324 }
325
326 static void ei_pow(struct r300_vertex_program_code *vp,
327 struct rc_sub_instruction *vpi,
328 unsigned int * inst)
329 {
330 inst[0] = PVS_OP_DST_OPERAND(ME_POWER_FUNC_FF,
331 1,
332 0,
333 t_dst_index(vp, &vpi->DstReg),
334 t_dst_mask(vpi->DstReg.WriteMask),
335 t_dst_class(vpi->DstReg.File));
336 inst[1] = t_src_scalar(vp, &vpi->SrcReg[0]);
337 inst[2] = __CONST(0, RC_SWIZZLE_ZERO);
338 inst[3] = t_src_scalar(vp, &vpi->SrcReg[1]);
339 }
340
341 static void mark_write(void * userdata, struct rc_instruction * inst,
342 rc_register_file file, unsigned int index, unsigned int mask)
343 {
344 unsigned int * writemasks = userdata;
345
346 if (file != RC_FILE_TEMPORARY)
347 return;
348
349 if (index >= R300_VS_MAX_TEMPS)
350 return;
351
352 writemasks[index] |= mask;
353 }
354
355 static unsigned long t_pred_src(struct r300_vertex_program_compiler * compiler)
356 {
357 return PVS_SRC_OPERAND(compiler->PredicateIndex,
358 t_swizzle(RC_SWIZZLE_ZERO),
359 t_swizzle(RC_SWIZZLE_ZERO),
360 t_swizzle(RC_SWIZZLE_ZERO),
361 t_swizzle(RC_SWIZZLE_W),
362 t_src_class(RC_FILE_TEMPORARY),
363 0);
364 }
365
366 static unsigned long t_pred_dst(struct r300_vertex_program_compiler * compiler,
367 unsigned int hw_opcode, int is_math)
368 {
369 return PVS_OP_DST_OPERAND(hw_opcode,
370 is_math,
371 0,
372 compiler->PredicateIndex,
373 RC_MASK_W,
374 t_dst_class(RC_FILE_TEMPORARY));
375
376 }
377
378 static void ei_if(struct r300_vertex_program_compiler * compiler,
379 struct rc_instruction *rci,
380 unsigned int * inst,
381 unsigned int branch_depth)
382 {
383 unsigned int predicate_opcode;
384 int is_math = 0;
385
386 if (!compiler->Base.is_r500) {
387 rc_error(&compiler->Base,"Opcode IF not supported\n");
388 return;
389 }
390
391 /* Reserve a temporary to use as our predicate stack counter, if we
392 * don't already have one. */
393 if (!compiler->PredicateMask) {
394 unsigned int writemasks[R300_VS_MAX_TEMPS];
395 memset(writemasks, 0, sizeof(writemasks));
396 struct rc_instruction * inst;
397 unsigned int i;
398 for(inst = compiler->Base.Program.Instructions.Next;
399 inst != &compiler->Base.Program.Instructions;
400 inst = inst->Next) {
401 rc_for_all_writes_mask(inst, mark_write, writemasks);
402 }
403 for(i = 0; i < R300_VS_MAX_TEMPS; i++) {
404 unsigned int mask = ~writemasks[i] & RC_MASK_XYZW;
405 /* Only the W component can be used fo the predicate
406 * stack counter. */
407 if (mask & RC_MASK_W) {
408 compiler->PredicateMask = RC_MASK_W;
409 compiler->PredicateIndex = i;
410 break;
411 }
412 }
413 if (i == R300_VS_MAX_TEMPS) {
414 rc_error(&compiler->Base, "No free temporary to use for"
415 " predicate stack counter.\n");
416 return;
417 }
418 }
419 predicate_opcode =
420 branch_depth ? VE_PRED_SET_NEQ_PUSH : ME_PRED_SET_NEQ;
421
422 rci->U.I.SrcReg[0].Swizzle = RC_MAKE_SWIZZLE_SMEAR(GET_SWZ(rci->U.I.SrcReg[0].Swizzle,0));
423 if (branch_depth == 0) {
424 is_math = 1;
425 predicate_opcode = ME_PRED_SET_NEQ;
426 inst[1] = t_src(compiler->code, &rci->U.I.SrcReg[0]);
427 inst[2] = 0;
428 } else {
429 predicate_opcode = VE_PRED_SET_NEQ_PUSH;
430 inst[1] = t_pred_src(compiler);
431 inst[2] = t_src(compiler->code, &rci->U.I.SrcReg[0]);
432 }
433
434 inst[0] = t_pred_dst(compiler, predicate_opcode, is_math);
435 inst[3] = 0;
436
437 }
438
439 static void ei_else(struct r300_vertex_program_compiler * compiler,
440 unsigned int * inst)
441 {
442 if (!compiler->Base.is_r500) {
443 rc_error(&compiler->Base,"Opcode ELSE not supported\n");
444 return;
445 }
446 inst[0] = t_pred_dst(compiler, ME_PRED_SET_INV, 1);
447 inst[1] = t_pred_src(compiler);
448 inst[2] = 0;
449 inst[3] = 0;
450 }
451
452 static void ei_endif(struct r300_vertex_program_compiler *compiler,
453 unsigned int * inst)
454 {
455 if (!compiler->Base.is_r500) {
456 rc_error(&compiler->Base,"Opcode ENDIF not supported\n");
457 return;
458 }
459 inst[0] = t_pred_dst(compiler, ME_PRED_SET_POP, 1);
460 inst[1] = t_pred_src(compiler);
461 inst[2] = 0;
462 inst[3] = 0;
463 }
464
465 static void translate_vertex_program(struct r300_vertex_program_compiler * compiler)
466 {
467 struct rc_instruction *rci;
468
469 struct loop * loops = NULL;
470 int current_loop_depth = 0;
471 int loops_reserved = 0;
472
473 unsigned int branch_depth = 0;
474
475 compiler->code->pos_end = 0; /* Not supported yet */
476 compiler->code->length = 0;
477
478 compiler->SetHwInputOutput(compiler);
479
480 for(rci = compiler->Base.Program.Instructions.Next; rci != &compiler->Base.Program.Instructions; rci = rci->Next) {
481 struct rc_sub_instruction *vpi = &rci->U.I;
482 unsigned int *inst = compiler->code->body.d + compiler->code->length;
483
484 /* Skip instructions writing to non-existing destination */
485 if (!valid_dst(compiler->code, &vpi->DstReg))
486 continue;
487
488 if (rc_get_opcode_info(vpi->Opcode)->HasDstReg) {
489 /* Relative addressing of destination operands is not supported yet. */
490 if (vpi->DstReg.RelAddr) {
491 rc_error(&compiler->Base, "Vertex program does not support relative "
492 "addressing of destination operands (yet).\n");
493 return;
494 }
495
496 /* Neither is Saturate. */
497 if (vpi->SaturateMode != RC_SATURATE_NONE) {
498 rc_error(&compiler->Base, "Vertex program does not support the Saturate "
499 "modifier (yet).\n");
500 }
501 }
502
503 if (compiler->code->length >= R500_VS_MAX_ALU_DWORDS ||
504 (compiler->code->length >= R300_VS_MAX_ALU_DWORDS && !compiler->Base.is_r500)) {
505 rc_error(&compiler->Base, "Vertex program has too many instructions\n");
506 return;
507 }
508
509 assert(compiler->Base.is_r500 ||
510 (vpi->Opcode != RC_OPCODE_SEQ &&
511 vpi->Opcode != RC_OPCODE_SNE));
512
513 switch (vpi->Opcode) {
514 case RC_OPCODE_ADD: ei_vector2(compiler->code, VE_ADD, vpi, inst); break;
515 case RC_OPCODE_ARL: ei_vector1(compiler->code, VE_FLT2FIX_DX, vpi, inst); break;
516 case RC_OPCODE_COS: ei_math1(compiler->code, ME_COS, vpi, inst); break;
517 case RC_OPCODE_DP4: ei_vector2(compiler->code, VE_DOT_PRODUCT, vpi, inst); break;
518 case RC_OPCODE_DST: ei_vector2(compiler->code, VE_DISTANCE_VECTOR, vpi, inst); break;
519 case RC_OPCODE_ELSE: ei_else(compiler, inst); break;
520 case RC_OPCODE_ENDIF: ei_endif(compiler, inst); branch_depth--; break;
521 case RC_OPCODE_EX2: ei_math1(compiler->code, ME_EXP_BASE2_FULL_DX, vpi, inst); break;
522 case RC_OPCODE_EXP: ei_math1(compiler->code, ME_EXP_BASE2_DX, vpi, inst); break;
523 case RC_OPCODE_FRC: ei_vector1(compiler->code, VE_FRACTION, vpi, inst); break;
524 case RC_OPCODE_IF: ei_if(compiler, rci, inst, branch_depth); branch_depth++; break;
525 case RC_OPCODE_LG2: ei_math1(compiler->code, ME_LOG_BASE2_FULL_DX, vpi, inst); break;
526 case RC_OPCODE_LIT: ei_lit(compiler->code, vpi, inst); break;
527 case RC_OPCODE_LOG: ei_math1(compiler->code, ME_LOG_BASE2_DX, vpi, inst); break;
528 case RC_OPCODE_MAD: ei_mad(compiler->code, vpi, inst); break;
529 case RC_OPCODE_MAX: ei_vector2(compiler->code, VE_MAXIMUM, vpi, inst); break;
530 case RC_OPCODE_MIN: ei_vector2(compiler->code, VE_MINIMUM, vpi, inst); break;
531 case RC_OPCODE_MOV: ei_vector1(compiler->code, VE_ADD, vpi, inst); break;
532 case RC_OPCODE_MUL: ei_vector2(compiler->code, VE_MULTIPLY, vpi, inst); break;
533 case RC_OPCODE_POW: ei_pow(compiler->code, vpi, inst); break;
534 case RC_OPCODE_RCP: ei_math1(compiler->code, ME_RECIP_DX, vpi, inst); break;
535 case RC_OPCODE_RSQ: ei_math1(compiler->code, ME_RECIP_SQRT_DX, vpi, inst); break;
536 case RC_OPCODE_SEQ: ei_vector2(compiler->code, VE_SET_EQUAL, vpi, inst); break;
537 case RC_OPCODE_SGE: ei_vector2(compiler->code, VE_SET_GREATER_THAN_EQUAL, vpi, inst); break;
538 case RC_OPCODE_SIN: ei_math1(compiler->code, ME_SIN, vpi, inst); break;
539 case RC_OPCODE_SLT: ei_vector2(compiler->code, VE_SET_LESS_THAN, vpi, inst); break;
540 case RC_OPCODE_SNE: ei_vector2(compiler->code, VE_SET_NOT_EQUAL, vpi, inst); break;
541 case RC_OPCODE_BGNLOOP:
542 {
543 struct loop * l;
544
545 if ((!compiler->Base.is_r500
546 && loops_reserved >= R300_VS_MAX_LOOP_DEPTH)
547 || loops_reserved >= R500_VS_MAX_FC_DEPTH) {
548 rc_error(&compiler->Base,
549 "Loops are nested too deep.");
550 return;
551 }
552 memory_pool_array_reserve(&compiler->Base.Pool,
553 struct loop, loops, current_loop_depth,
554 loops_reserved, 1);
555 l = &loops[current_loop_depth++];
556 memset(l , 0, sizeof(struct loop));
557 l->BgnLoop = (compiler->code->length / 4);
558 continue;
559 }
560 case RC_OPCODE_ENDLOOP:
561 {
562 struct loop * l;
563 unsigned int act_addr;
564 unsigned int last_addr;
565 unsigned int ret_addr;
566
567 assert(loops);
568 l = &loops[current_loop_depth - 1];
569 act_addr = l->BgnLoop - 1;
570 last_addr = (compiler->code->length / 4) - 1;
571 ret_addr = l->BgnLoop;
572
573 if (loops_reserved >= R300_VS_MAX_FC_OPS) {
574 rc_error(&compiler->Base,
575 "Too many flow control instructions.");
576 return;
577 }
578 if (compiler->Base.is_r500) {
579 compiler->code->fc_op_addrs.r500
580 [compiler->code->num_fc_ops].lw =
581 R500_PVS_FC_ACT_ADRS(act_addr)
582 | R500_PVS_FC_LOOP_CNT_JMP_INST(0xffff)
583 ;
584 compiler->code->fc_op_addrs.r500
585 [compiler->code->num_fc_ops].uw =
586 R500_PVS_FC_LAST_INST(last_addr)
587 | R500_PVS_FC_RTN_INST(ret_addr)
588 ;
589 } else {
590 compiler->code->fc_op_addrs.r300
591 [compiler->code->num_fc_ops] =
592 R300_PVS_FC_ACT_ADRS(act_addr)
593 | R300_PVS_FC_LOOP_CNT_JMP_INST(0xff)
594 | R300_PVS_FC_LAST_INST(last_addr)
595 | R300_PVS_FC_RTN_INST(ret_addr)
596 ;
597 }
598 compiler->code->fc_loop_index[compiler->code->num_fc_ops] =
599 R300_PVS_FC_LOOP_INIT_VAL(0x0)
600 | R300_PVS_FC_LOOP_STEP_VAL(0x1)
601 ;
602 compiler->code->fc_ops |= R300_VAP_PVS_FC_OPC_LOOP(
603 compiler->code->num_fc_ops);
604 compiler->code->num_fc_ops++;
605 current_loop_depth--;
606 continue;
607 }
608
609 default:
610 rc_error(&compiler->Base, "Unknown opcode %s\n", rc_get_opcode_info(vpi->Opcode)->Name);
611 return;
612 }
613
614 /* Non-flow control instructions that are inside an if statement
615 * need to pay attention to the predicate bit. */
616 if (branch_depth
617 && vpi->Opcode != RC_OPCODE_IF
618 && vpi->Opcode != RC_OPCODE_ELSE
619 && vpi->Opcode != RC_OPCODE_ENDIF) {
620
621 inst[0] |= (PVS_DST_PRED_ENABLE_MASK
622 << PVS_DST_PRED_ENABLE_SHIFT);
623 inst[0] |= (PVS_DST_PRED_SENSE_MASK
624 << PVS_DST_PRED_SENSE_SHIFT);
625 }
626
627 compiler->code->length += 4;
628
629 if (compiler->Base.Error)
630 return;
631 }
632 }
633
634 struct temporary_allocation {
635 unsigned int Allocated:1;
636 unsigned int HwTemp:15;
637 struct rc_instruction * LastRead;
638 };
639
640 static void allocate_temporary_registers(struct r300_vertex_program_compiler * compiler)
641 {
642 struct rc_instruction *inst;
643 struct rc_instruction *end_loop = NULL;
644 unsigned int num_orig_temps = 0;
645 char hwtemps[R300_VS_MAX_TEMPS];
646 struct temporary_allocation * ta;
647 unsigned int i, j;
648
649 memset(hwtemps, 0, sizeof(hwtemps));
650
651 /* Pass 1: Count original temporaries. */
652 for(inst = compiler->Base.Program.Instructions.Next; inst != &compiler->Base.Program.Instructions; inst = inst->Next) {
653 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->U.I.Opcode);
654
655 for (i = 0; i < opcode->NumSrcRegs; ++i) {
656 if (inst->U.I.SrcReg[i].File == RC_FILE_TEMPORARY) {
657 if (inst->U.I.SrcReg[i].Index >= num_orig_temps)
658 num_orig_temps = inst->U.I.SrcReg[i].Index + 1;
659 }
660 }
661
662 if (opcode->HasDstReg) {
663 if (inst->U.I.DstReg.File == RC_FILE_TEMPORARY) {
664 if (inst->U.I.DstReg.Index >= num_orig_temps)
665 num_orig_temps = inst->U.I.DstReg.Index + 1;
666 }
667 }
668 }
669 compiler->code->num_temporaries = num_orig_temps;
670
671 /* Pass 2: If there is relative addressing of temporaries, we cannot change register indices. Give up. */
672 for (inst = compiler->Base.Program.Instructions.Next; inst != &compiler->Base.Program.Instructions; inst = inst->Next) {
673 const struct rc_opcode_info *opcode = rc_get_opcode_info(inst->U.I.Opcode);
674
675 if (opcode->HasDstReg)
676 if (inst->U.I.DstReg.RelAddr)
677 return;
678
679 for (i = 0; i < opcode->NumSrcRegs; ++i) {
680 if (inst->U.I.SrcReg[i].File == RC_FILE_TEMPORARY &&
681 inst->U.I.SrcReg[i].RelAddr) {
682 return;
683 }
684 }
685 }
686
687 compiler->code->num_temporaries = 0;
688 ta = (struct temporary_allocation*)memory_pool_malloc(&compiler->Base.Pool,
689 sizeof(struct temporary_allocation) * num_orig_temps);
690 memset(ta, 0, sizeof(struct temporary_allocation) * num_orig_temps);
691
692 /* Pass 3: Determine original temporary lifetimes */
693 for(inst = compiler->Base.Program.Instructions.Next; inst != &compiler->Base.Program.Instructions; inst = inst->Next) {
694 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->U.I.Opcode);
695 /* Instructions inside of loops need to use the ENDLOOP
696 * instruction as their LastRead. */
697 if (!end_loop && inst->U.I.Opcode == RC_OPCODE_BGNLOOP) {
698 int endloops = 1;
699 struct rc_instruction * ptr;
700 for(ptr = inst->Next;
701 ptr != &compiler->Base.Program.Instructions;
702 ptr = ptr->Next){
703 if (ptr->U.I.Opcode == RC_OPCODE_BGNLOOP) {
704 endloops++;
705 } else if (ptr->U.I.Opcode == RC_OPCODE_ENDLOOP) {
706 endloops--;
707 if (endloops <= 0) {
708 end_loop = ptr;
709 break;
710 }
711 }
712 }
713 }
714
715 if (inst == end_loop) {
716 end_loop = NULL;
717 continue;
718 }
719
720 for (i = 0; i < opcode->NumSrcRegs; ++i) {
721 if (inst->U.I.SrcReg[i].File == RC_FILE_TEMPORARY)
722 ta[inst->U.I.SrcReg[i].Index].LastRead =
723 end_loop ? end_loop : inst;
724 }
725 }
726
727 /* Pass 4: Register allocation */
728 for(inst = compiler->Base.Program.Instructions.Next; inst != &compiler->Base.Program.Instructions; inst = inst->Next) {
729 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->U.I.Opcode);
730
731 for (i = 0; i < opcode->NumSrcRegs; ++i) {
732 if (inst->U.I.SrcReg[i].File == RC_FILE_TEMPORARY) {
733 unsigned int orig = inst->U.I.SrcReg[i].Index;
734 inst->U.I.SrcReg[i].Index = ta[orig].HwTemp;
735
736 if (ta[orig].Allocated && inst == ta[orig].LastRead)
737 hwtemps[ta[orig].HwTemp] = 0;
738 }
739 }
740
741 if (opcode->HasDstReg) {
742 if (inst->U.I.DstReg.File == RC_FILE_TEMPORARY) {
743 unsigned int orig = inst->U.I.DstReg.Index;
744
745 if (!ta[orig].Allocated) {
746 for(j = 0; j < R300_VS_MAX_TEMPS; ++j) {
747 if (!hwtemps[j])
748 break;
749 }
750 if (j >= R300_VS_MAX_TEMPS) {
751 fprintf(stderr, "Out of hw temporaries\n");
752 } else {
753 ta[orig].Allocated = 1;
754 ta[orig].HwTemp = j;
755 hwtemps[j] = 1;
756
757 if (j >= compiler->code->num_temporaries)
758 compiler->code->num_temporaries = j + 1;
759 }
760 }
761
762 inst->U.I.DstReg.Index = ta[orig].HwTemp;
763 }
764 }
765 }
766 }
767
768 /**
769 * R3xx-R4xx vertex engine does not support the Absolute source operand modifier
770 * and the Saturate opcode modifier. Only Absolute is currently transformed.
771 */
772 static int transform_nonnative_modifiers(
773 struct radeon_compiler *c,
774 struct rc_instruction *inst,
775 void* unused)
776 {
777 const struct rc_opcode_info *opcode = rc_get_opcode_info(inst->U.I.Opcode);
778 unsigned i;
779
780 /* Transform ABS(a) to MAX(a, -a). */
781 for (i = 0; i < opcode->NumSrcRegs; i++) {
782 if (inst->U.I.SrcReg[i].Abs) {
783 struct rc_instruction *new_inst;
784 unsigned temp;
785
786 inst->U.I.SrcReg[i].Abs = 0;
787
788 temp = rc_find_free_temporary(c);
789
790 new_inst = rc_insert_new_instruction(c, inst->Prev);
791 new_inst->U.I.Opcode = RC_OPCODE_MAX;
792 new_inst->U.I.DstReg.File = RC_FILE_TEMPORARY;
793 new_inst->U.I.DstReg.Index = temp;
794 new_inst->U.I.SrcReg[0] = inst->U.I.SrcReg[i];
795 new_inst->U.I.SrcReg[1] = inst->U.I.SrcReg[i];
796 new_inst->U.I.SrcReg[1].Negate ^= RC_MASK_XYZW;
797
798 memset(&inst->U.I.SrcReg[i], 0, sizeof(inst->U.I.SrcReg[i]));
799 inst->U.I.SrcReg[i].File = RC_FILE_TEMPORARY;
800 inst->U.I.SrcReg[i].Index = temp;
801 inst->U.I.SrcReg[i].Swizzle = RC_SWIZZLE_XYZW;
802 }
803 }
804 return 1;
805 }
806
807 /**
808 * Vertex engine cannot read two inputs or two constants at the same time.
809 * Introduce intermediate MOVs to temporary registers to account for this.
810 */
811 static int transform_source_conflicts(
812 struct radeon_compiler *c,
813 struct rc_instruction* inst,
814 void* unused)
815 {
816 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->U.I.Opcode);
817
818 if (opcode->NumSrcRegs == 3) {
819 if (t_src_conflict(inst->U.I.SrcReg[1], inst->U.I.SrcReg[2])
820 || t_src_conflict(inst->U.I.SrcReg[0], inst->U.I.SrcReg[2])) {
821 int tmpreg = rc_find_free_temporary(c);
822 struct rc_instruction * inst_mov = rc_insert_new_instruction(c, inst->Prev);
823 inst_mov->U.I.Opcode = RC_OPCODE_MOV;
824 inst_mov->U.I.DstReg.File = RC_FILE_TEMPORARY;
825 inst_mov->U.I.DstReg.Index = tmpreg;
826 inst_mov->U.I.SrcReg[0] = inst->U.I.SrcReg[2];
827
828 reset_srcreg(&inst->U.I.SrcReg[2]);
829 inst->U.I.SrcReg[2].File = RC_FILE_TEMPORARY;
830 inst->U.I.SrcReg[2].Index = tmpreg;
831 }
832 }
833
834 if (opcode->NumSrcRegs >= 2) {
835 if (t_src_conflict(inst->U.I.SrcReg[1], inst->U.I.SrcReg[0])) {
836 int tmpreg = rc_find_free_temporary(c);
837 struct rc_instruction * inst_mov = rc_insert_new_instruction(c, inst->Prev);
838 inst_mov->U.I.Opcode = RC_OPCODE_MOV;
839 inst_mov->U.I.DstReg.File = RC_FILE_TEMPORARY;
840 inst_mov->U.I.DstReg.Index = tmpreg;
841 inst_mov->U.I.SrcReg[0] = inst->U.I.SrcReg[1];
842
843 reset_srcreg(&inst->U.I.SrcReg[1]);
844 inst->U.I.SrcReg[1].File = RC_FILE_TEMPORARY;
845 inst->U.I.SrcReg[1].Index = tmpreg;
846 }
847 }
848
849 return 1;
850 }
851
852 static void addArtificialOutputs(struct r300_vertex_program_compiler * compiler)
853 {
854 int i;
855
856 for(i = 0; i < 32; ++i) {
857 if ((compiler->RequiredOutputs & (1 << i)) &&
858 !(compiler->Base.Program.OutputsWritten & (1 << i))) {
859 struct rc_instruction * inst = rc_insert_new_instruction(&compiler->Base, compiler->Base.Program.Instructions.Prev);
860 inst->U.I.Opcode = RC_OPCODE_MOV;
861
862 inst->U.I.DstReg.File = RC_FILE_OUTPUT;
863 inst->U.I.DstReg.Index = i;
864 inst->U.I.DstReg.WriteMask = RC_MASK_XYZW;
865
866 inst->U.I.SrcReg[0].File = RC_FILE_CONSTANT;
867 inst->U.I.SrcReg[0].Index = 0;
868 inst->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_XYZW;
869
870 compiler->Base.Program.OutputsWritten |= 1 << i;
871 }
872 }
873 }
874
875 static void dataflow_outputs_mark_used(void * userdata, void * data,
876 void (*callback)(void *, unsigned int, unsigned int))
877 {
878 struct r300_vertex_program_compiler * c = userdata;
879 int i;
880
881 for(i = 0; i < 32; ++i) {
882 if (c->RequiredOutputs & (1 << i))
883 callback(data, i, RC_MASK_XYZW);
884 }
885 }
886
887 static int swizzle_is_native(rc_opcode opcode, struct rc_src_register reg)
888 {
889 (void) opcode;
890 (void) reg;
891
892 return 1;
893 }
894
895 static void transform_negative_addressing(struct r300_vertex_program_compiler *c,
896 struct rc_instruction *arl,
897 struct rc_instruction *end,
898 int min_offset)
899 {
900 struct rc_instruction *inst, *add;
901 unsigned const_swizzle;
902
903 /* Transform ARL */
904 add = rc_insert_new_instruction(&c->Base, arl->Prev);
905 add->U.I.Opcode = RC_OPCODE_ADD;
906 add->U.I.DstReg.File = RC_FILE_TEMPORARY;
907 add->U.I.DstReg.Index = rc_find_free_temporary(&c->Base);
908 add->U.I.DstReg.WriteMask = RC_MASK_X;
909 add->U.I.SrcReg[0] = arl->U.I.SrcReg[0];
910 add->U.I.SrcReg[1].File = RC_FILE_CONSTANT;
911 add->U.I.SrcReg[1].Index = rc_constants_add_immediate_scalar(&c->Base.Program.Constants,
912 min_offset, &const_swizzle);
913 add->U.I.SrcReg[1].Swizzle = const_swizzle;
914
915 arl->U.I.SrcReg[0].File = RC_FILE_TEMPORARY;
916 arl->U.I.SrcReg[0].Index = add->U.I.DstReg.Index;
917 arl->U.I.SrcReg[0].Swizzle = RC_SWIZZLE_XXXX;
918
919 /* Rewrite offsets up to and excluding inst. */
920 for (inst = arl->Next; inst != end; inst = inst->Next) {
921 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->U.I.Opcode);
922
923 for (unsigned i = 0; i < opcode->NumSrcRegs; i++)
924 if (inst->U.I.SrcReg[i].RelAddr)
925 inst->U.I.SrcReg[i].Index -= min_offset;
926 }
927 }
928
929 static void rc_emulate_negative_addressing(struct r300_vertex_program_compiler *c)
930 {
931 struct rc_instruction *inst, *lastARL = NULL;
932 int min_offset = 0;
933
934 for (inst = c->Base.Program.Instructions.Next; inst != &c->Base.Program.Instructions; inst = inst->Next) {
935 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->U.I.Opcode);
936
937 if (inst->U.I.Opcode == RC_OPCODE_ARL) {
938 if (lastARL != NULL && min_offset < 0)
939 transform_negative_addressing(c, lastARL, inst, min_offset);
940
941 lastARL = inst;
942 min_offset = 0;
943 continue;
944 }
945
946 for (unsigned i = 0; i < opcode->NumSrcRegs; i++) {
947 if (inst->U.I.SrcReg[i].RelAddr &&
948 inst->U.I.SrcReg[i].Index < 0) {
949 /* ARL must precede any indirect addressing. */
950 if (lastARL == NULL) {
951 rc_error(&c->Base, "Vertex shader: Found relative addressing without ARL.");
952 return;
953 }
954
955 if (inst->U.I.SrcReg[i].Index < min_offset)
956 min_offset = inst->U.I.SrcReg[i].Index;
957 }
958 }
959 }
960
961 if (lastARL != NULL && min_offset < 0)
962 transform_negative_addressing(c, lastARL, inst, min_offset);
963 }
964
965 static void debug_program_log(struct r300_vertex_program_compiler* c, const char * where)
966 {
967 if (c->Base.Debug) {
968 fprintf(stderr, "Vertex Program: %s\n", where);
969 rc_print_program(&c->Base.Program);
970 }
971 }
972
973
974 static struct rc_swizzle_caps r300_vertprog_swizzle_caps = {
975 .IsNative = &swizzle_is_native,
976 .Split = 0 /* should never be called */
977 };
978
979
980 void r3xx_compile_vertex_program(struct r300_vertex_program_compiler *c)
981 {
982 struct emulate_loop_state loop_state;
983
984 c->Base.SwizzleCaps = &r300_vertprog_swizzle_caps;
985
986 addArtificialOutputs(c);
987
988 debug_program_log(c, "before compilation");
989
990 if (c->Base.is_r500)
991 rc_transform_loops(&c->Base, &loop_state, R500_VS_MAX_ALU);
992 else
993 rc_transform_loops(&c->Base, &loop_state, R300_VS_MAX_ALU);
994 if (c->Base.Error)
995 return;
996
997 debug_program_log(c, "after emulate loops");
998
999 if (!c->Base.is_r500) {
1000 rc_emulate_branches(&c->Base);
1001 if (c->Base.Error)
1002 return;
1003 debug_program_log(c, "after emulate branches");
1004 }
1005
1006 rc_emulate_negative_addressing(c);
1007
1008 debug_program_log(c, "after negative addressing emulation");
1009
1010 if (c->Base.is_r500) {
1011 struct radeon_program_transformation transformations[] = {
1012 { &r300_transform_vertex_alu, 0 },
1013 { &r300_transform_trig_scale_vertex, 0 }
1014 };
1015 radeonLocalTransform(&c->Base, 2, transformations);
1016 if (c->Base.Error)
1017 return;
1018
1019 debug_program_log(c, "after native rewrite");
1020 } else {
1021 struct radeon_program_transformation transformations[] = {
1022 { &r300_transform_vertex_alu, 0 },
1023 { &radeonTransformTrigSimple, 0 }
1024 };
1025 radeonLocalTransform(&c->Base, 2, transformations);
1026 if (c->Base.Error)
1027 return;
1028
1029 debug_program_log(c, "after native rewrite");
1030
1031 /* Note: This pass has to be done seperately from ALU rewrite,
1032 * because it needs to check every instruction.
1033 */
1034 struct radeon_program_transformation transformations2[] = {
1035 { &transform_nonnative_modifiers, 0 },
1036 };
1037 radeonLocalTransform(&c->Base, 1, transformations2);
1038 if (c->Base.Error)
1039 return;
1040
1041 debug_program_log(c, "after emulate modifiers");
1042 }
1043
1044 {
1045 /* Note: This pass has to be done seperately from ALU rewrite,
1046 * otherwise non-native ALU instructions with source conflits
1047 * will not be treated properly.
1048 */
1049 struct radeon_program_transformation transformations[] = {
1050 { &transform_source_conflicts, 0 },
1051 };
1052 radeonLocalTransform(&c->Base, 1, transformations);
1053 if (c->Base.Error)
1054 return;
1055 }
1056
1057 debug_program_log(c, "after source conflict resolve");
1058
1059 rc_dataflow_deadcode(&c->Base, &dataflow_outputs_mark_used, c);
1060 if (c->Base.Error)
1061 return;
1062
1063 debug_program_log(c, "after deadcode");
1064
1065 rc_dataflow_swizzles(&c->Base);
1066 if (c->Base.Error)
1067 return;
1068
1069 debug_program_log(c, "after dataflow");
1070
1071 allocate_temporary_registers(c);
1072 if (c->Base.Error)
1073 return;
1074
1075 debug_program_log(c, "after register allocation");
1076
1077 if (c->Base.remove_unused_constants) {
1078 rc_remove_unused_constants(&c->Base,
1079 &c->code->constants_remap_table);
1080 if (c->Base.Error)
1081 return;
1082
1083 debug_program_log(c, "after constants cleanup");
1084 }
1085
1086 translate_vertex_program(c);
1087 if (c->Base.Error)
1088 return;
1089
1090 rc_constants_copy(&c->code->constants, &c->Base.Program.Constants);
1091
1092 c->code->InputsRead = c->Base.Program.InputsRead;
1093 c->code->OutputsWritten = c->Base.Program.OutputsWritten;
1094
1095 if (c->Base.Debug) {
1096 fprintf(stderr, "Final vertex program code:\n");
1097 r300_vertex_program_dump(c);
1098 }
1099
1100 /* Check the number of constants. */
1101 if (!c->Base.Error &&
1102 c->Base.Program.Constants.Count > 256) {
1103 rc_error(&c->Base, "Too many constants. Max: 256, Got: %i\n",
1104 c->Base.Program.Constants.Count);
1105 }
1106 }