2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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.
24 #include "r600_opcodes.h"
25 #include "r600_formats.h"
26 #include "r600_shader.h"
30 #include "util/u_dump.h"
31 #include "util/u_memory.h"
32 #include "util/u_math.h"
33 #include "pipe/p_shader_tokens.h"
35 #include "sb/sb_public.h"
37 #define NUM_OF_CYCLES 3
38 #define NUM_OF_COMPONENTS 4
40 static inline unsigned int r600_bytecode_get_num_operands(
41 struct r600_bytecode
*bc
, struct r600_bytecode_alu
*alu
)
43 return r600_isa_alu(alu
->op
)->src_count
;
46 int r700_bytecode_alu_build(struct r600_bytecode
*bc
,
47 struct r600_bytecode_alu
*alu
, unsigned id
);
49 static struct r600_bytecode_cf
*r600_bytecode_cf(void)
51 struct r600_bytecode_cf
*cf
= CALLOC_STRUCT(r600_bytecode_cf
);
55 LIST_INITHEAD(&cf
->list
);
56 LIST_INITHEAD(&cf
->alu
);
57 LIST_INITHEAD(&cf
->vtx
);
58 LIST_INITHEAD(&cf
->tex
);
62 static struct r600_bytecode_alu
*r600_bytecode_alu(void)
64 struct r600_bytecode_alu
*alu
= CALLOC_STRUCT(r600_bytecode_alu
);
68 LIST_INITHEAD(&alu
->list
);
72 static struct r600_bytecode_vtx
*r600_bytecode_vtx(void)
74 struct r600_bytecode_vtx
*vtx
= CALLOC_STRUCT(r600_bytecode_vtx
);
78 LIST_INITHEAD(&vtx
->list
);
82 static struct r600_bytecode_tex
*r600_bytecode_tex(void)
84 struct r600_bytecode_tex
*tex
= CALLOC_STRUCT(r600_bytecode_tex
);
88 LIST_INITHEAD(&tex
->list
);
92 static unsigned stack_entry_size(enum radeon_family chip
) {
94 * 64: R600/RV670/RV770/Cypress/R740/Barts/Turks/Caicos/
95 * Aruba/Sumo/Sumo2/redwood/juniper
96 * 32: R630/R730/R710/Palm/Cedar
100 * Wavefront Size 16 32 48 64
101 * Columns per Row (R6xx/R7xx/R8xx only) 8 8 4 4
102 * Columns per Row (R9xx+) 8 4 4 4 */
105 /* FIXME: are some chips missing here? */
106 /* wavefront size 16 */
111 /* wavefront size 32 */
120 /* wavefront size 64 */
126 void r600_bytecode_init(struct r600_bytecode
*bc
,
127 enum chip_class chip_class
,
128 enum radeon_family family
,
129 bool has_compressed_msaa_texturing
)
131 static unsigned next_shader_id
= 0;
133 bc
->debug_id
= ++next_shader_id
;
135 if ((chip_class
== R600
) &&
136 (family
!= CHIP_RV670
&& family
!= CHIP_RS780
&& family
!= CHIP_RS880
)) {
137 bc
->ar_handling
= AR_HANDLE_RV6XX
;
138 bc
->r6xx_nop_after_rel_dst
= 1;
140 bc
->ar_handling
= AR_HANDLE_NORMAL
;
141 bc
->r6xx_nop_after_rel_dst
= 0;
144 LIST_INITHEAD(&bc
->cf
);
145 bc
->chip_class
= chip_class
;
146 bc
->has_compressed_msaa_texturing
= has_compressed_msaa_texturing
;
147 bc
->stack
.entry_size
= stack_entry_size(family
);
150 int r600_bytecode_add_cf(struct r600_bytecode
*bc
)
152 struct r600_bytecode_cf
*cf
= r600_bytecode_cf();
156 LIST_ADDTAIL(&cf
->list
, &bc
->cf
);
158 cf
->id
= bc
->cf_last
->id
+ 2;
159 if (bc
->cf_last
->eg_alu_extended
) {
160 /* take into account extended alu size */
168 bc
->force_add_cf
= 0;
173 int r600_bytecode_add_output(struct r600_bytecode
*bc
,
174 const struct r600_bytecode_output
*output
)
178 if (output
->gpr
>= bc
->ngpr
)
179 bc
->ngpr
= output
->gpr
+ 1;
181 if (bc
->cf_last
&& (bc
->cf_last
->op
== output
->op
||
182 (bc
->cf_last
->op
== CF_OP_EXPORT
&&
183 output
->op
== CF_OP_EXPORT_DONE
)) &&
184 output
->type
== bc
->cf_last
->output
.type
&&
185 output
->elem_size
== bc
->cf_last
->output
.elem_size
&&
186 output
->swizzle_x
== bc
->cf_last
->output
.swizzle_x
&&
187 output
->swizzle_y
== bc
->cf_last
->output
.swizzle_y
&&
188 output
->swizzle_z
== bc
->cf_last
->output
.swizzle_z
&&
189 output
->swizzle_w
== bc
->cf_last
->output
.swizzle_w
&&
190 output
->comp_mask
== bc
->cf_last
->output
.comp_mask
&&
191 (output
->burst_count
+ bc
->cf_last
->output
.burst_count
) <= 16) {
193 if ((output
->gpr
+ output
->burst_count
) == bc
->cf_last
->output
.gpr
&&
194 (output
->array_base
+ output
->burst_count
) == bc
->cf_last
->output
.array_base
) {
196 bc
->cf_last
->op
= bc
->cf_last
->output
.op
= output
->op
;
197 bc
->cf_last
->output
.gpr
= output
->gpr
;
198 bc
->cf_last
->output
.array_base
= output
->array_base
;
199 bc
->cf_last
->output
.burst_count
+= output
->burst_count
;
202 } else if (output
->gpr
== (bc
->cf_last
->output
.gpr
+ bc
->cf_last
->output
.burst_count
) &&
203 output
->array_base
== (bc
->cf_last
->output
.array_base
+ bc
->cf_last
->output
.burst_count
)) {
205 bc
->cf_last
->op
= bc
->cf_last
->output
.op
= output
->op
;
206 bc
->cf_last
->output
.burst_count
+= output
->burst_count
;
211 r
= r600_bytecode_add_cf(bc
);
214 bc
->cf_last
->op
= output
->op
;
215 memcpy(&bc
->cf_last
->output
, output
, sizeof(struct r600_bytecode_output
));
216 bc
->cf_last
->barrier
= 1;
220 /* alu instructions that can ony exits once per group */
221 static int is_alu_once_inst(struct r600_bytecode
*bc
, struct r600_bytecode_alu
*alu
)
223 return r600_isa_alu(alu
->op
)->flags
& (AF_KILL
| AF_PRED
);
226 static int is_alu_reduction_inst(struct r600_bytecode
*bc
, struct r600_bytecode_alu
*alu
)
228 return (r600_isa_alu(alu
->op
)->flags
& AF_REPL
) &&
229 (r600_isa_alu_slots(bc
->isa
->hw_class
, alu
->op
) == AF_4V
);
232 static int is_alu_mova_inst(struct r600_bytecode
*bc
, struct r600_bytecode_alu
*alu
)
234 return r600_isa_alu(alu
->op
)->flags
& AF_MOVA
;
237 static int alu_uses_rel(struct r600_bytecode
*bc
, struct r600_bytecode_alu
*alu
)
239 unsigned num_src
= r600_bytecode_get_num_operands(bc
, alu
);
246 for (src
= 0; src
< num_src
; ++src
) {
247 if (alu
->src
[src
].rel
) {
254 static int is_alu_vec_unit_inst(struct r600_bytecode
*bc
, struct r600_bytecode_alu
*alu
)
256 unsigned slots
= r600_isa_alu_slots(bc
->isa
->hw_class
, alu
->op
);
257 return !(slots
& AF_S
);
260 static int is_alu_trans_unit_inst(struct r600_bytecode
*bc
, struct r600_bytecode_alu
*alu
)
262 unsigned slots
= r600_isa_alu_slots(bc
->isa
->hw_class
, alu
->op
);
263 return !(slots
& AF_V
);
266 /* alu instructions that can execute on any unit */
267 static int is_alu_any_unit_inst(struct r600_bytecode
*bc
, struct r600_bytecode_alu
*alu
)
269 unsigned slots
= r600_isa_alu_slots(bc
->isa
->hw_class
, alu
->op
);
270 return slots
== AF_VS
;
273 static int is_nop_inst(struct r600_bytecode
*bc
, struct r600_bytecode_alu
*alu
)
275 return alu
->op
== ALU_OP0_NOP
;
278 static int assign_alu_units(struct r600_bytecode
*bc
, struct r600_bytecode_alu
*alu_first
,
279 struct r600_bytecode_alu
*assignment
[5])
281 struct r600_bytecode_alu
*alu
;
282 unsigned i
, chan
, trans
;
283 int max_slots
= bc
->chip_class
== CAYMAN
? 4 : 5;
285 for (i
= 0; i
< max_slots
; i
++)
286 assignment
[i
] = NULL
;
288 for (alu
= alu_first
; alu
; alu
= LIST_ENTRY(struct r600_bytecode_alu
, alu
->list
.next
, list
)) {
289 chan
= alu
->dst
.chan
;
292 else if (is_alu_trans_unit_inst(bc
, alu
))
294 else if (is_alu_vec_unit_inst(bc
, alu
))
296 else if (assignment
[chan
])
297 trans
= 1; /* Assume ALU_INST_PREFER_VECTOR. */
303 assert(0); /* ALU.Trans has already been allocated. */
308 if (assignment
[chan
]) {
309 assert(0); /* ALU.chan has already been allocated. */
312 assignment
[chan
] = alu
;
321 struct alu_bank_swizzle
{
322 int hw_gpr
[NUM_OF_CYCLES
][NUM_OF_COMPONENTS
];
323 int hw_cfile_addr
[4];
324 int hw_cfile_elem
[4];
327 static const unsigned cycle_for_bank_swizzle_vec
[][3] = {
328 [SQ_ALU_VEC_012
] = { 0, 1, 2 },
329 [SQ_ALU_VEC_021
] = { 0, 2, 1 },
330 [SQ_ALU_VEC_120
] = { 1, 2, 0 },
331 [SQ_ALU_VEC_102
] = { 1, 0, 2 },
332 [SQ_ALU_VEC_201
] = { 2, 0, 1 },
333 [SQ_ALU_VEC_210
] = { 2, 1, 0 }
336 static const unsigned cycle_for_bank_swizzle_scl
[][3] = {
337 [SQ_ALU_SCL_210
] = { 2, 1, 0 },
338 [SQ_ALU_SCL_122
] = { 1, 2, 2 },
339 [SQ_ALU_SCL_212
] = { 2, 1, 2 },
340 [SQ_ALU_SCL_221
] = { 2, 2, 1 }
343 static void init_bank_swizzle(struct alu_bank_swizzle
*bs
)
345 int i
, cycle
, component
;
347 for (cycle
= 0; cycle
< NUM_OF_CYCLES
; cycle
++)
348 for (component
= 0; component
< NUM_OF_COMPONENTS
; component
++)
349 bs
->hw_gpr
[cycle
][component
] = -1;
350 for (i
= 0; i
< 4; i
++)
351 bs
->hw_cfile_addr
[i
] = -1;
352 for (i
= 0; i
< 4; i
++)
353 bs
->hw_cfile_elem
[i
] = -1;
356 static int reserve_gpr(struct alu_bank_swizzle
*bs
, unsigned sel
, unsigned chan
, unsigned cycle
)
358 if (bs
->hw_gpr
[cycle
][chan
] == -1)
359 bs
->hw_gpr
[cycle
][chan
] = sel
;
360 else if (bs
->hw_gpr
[cycle
][chan
] != (int)sel
) {
361 /* Another scalar operation has already used the GPR read port for the channel. */
367 static int reserve_cfile(struct r600_bytecode
*bc
, struct alu_bank_swizzle
*bs
, unsigned sel
, unsigned chan
)
369 int res
, num_res
= 4;
370 if (bc
->chip_class
>= R700
) {
374 for (res
= 0; res
< num_res
; ++res
) {
375 if (bs
->hw_cfile_addr
[res
] == -1) {
376 bs
->hw_cfile_addr
[res
] = sel
;
377 bs
->hw_cfile_elem
[res
] = chan
;
379 } else if (bs
->hw_cfile_addr
[res
] == sel
&&
380 bs
->hw_cfile_elem
[res
] == chan
)
381 return 0; /* Read for this scalar element already reserved, nothing to do here. */
383 /* All cfile read ports are used, cannot reference vector element. */
387 static int is_gpr(unsigned sel
)
392 /* CB constants start at 512, and get translated to a kcache index when ALU
393 * clauses are constructed. Note that we handle kcache constants the same way
394 * as (the now gone) cfile constants, is that really required? */
395 static int is_cfile(unsigned sel
)
397 return (sel
> 255 && sel
< 512) ||
398 (sel
> 511 && sel
< 4607) || /* Kcache before translation. */
399 (sel
> 127 && sel
< 192); /* Kcache after translation. */
402 static int is_const(int sel
)
404 return is_cfile(sel
) ||
405 (sel
>= V_SQ_ALU_SRC_0
&&
406 sel
<= V_SQ_ALU_SRC_LITERAL
);
409 static int check_vector(struct r600_bytecode
*bc
, struct r600_bytecode_alu
*alu
,
410 struct alu_bank_swizzle
*bs
, int bank_swizzle
)
412 int r
, src
, num_src
, sel
, elem
, cycle
;
414 num_src
= r600_bytecode_get_num_operands(bc
, alu
);
415 for (src
= 0; src
< num_src
; src
++) {
416 sel
= alu
->src
[src
].sel
;
417 elem
= alu
->src
[src
].chan
;
419 cycle
= cycle_for_bank_swizzle_vec
[bank_swizzle
][src
];
420 if (src
== 1 && sel
== alu
->src
[0].sel
&& elem
== alu
->src
[0].chan
)
421 /* Nothing to do; special-case optimization,
422 * second source uses first source’s reservation. */
425 r
= reserve_gpr(bs
, sel
, elem
, cycle
);
429 } else if (is_cfile(sel
)) {
430 r
= reserve_cfile(bc
, bs
, (alu
->src
[src
].kc_bank
<<16) + sel
, elem
);
434 /* No restrictions on PV, PS, literal or special constants. */
439 static int check_scalar(struct r600_bytecode
*bc
, struct r600_bytecode_alu
*alu
,
440 struct alu_bank_swizzle
*bs
, int bank_swizzle
)
442 int r
, src
, num_src
, const_count
, sel
, elem
, cycle
;
444 num_src
= r600_bytecode_get_num_operands(bc
, alu
);
445 for (const_count
= 0, src
= 0; src
< num_src
; ++src
) {
446 sel
= alu
->src
[src
].sel
;
447 elem
= alu
->src
[src
].chan
;
448 if (is_const(sel
)) { /* Any constant, including literal and inline constants. */
449 if (const_count
>= 2)
450 /* More than two references to a constant in
451 * transcendental operation. */
457 r
= reserve_cfile(bc
, bs
, (alu
->src
[src
].kc_bank
<<16) + sel
, elem
);
462 for (src
= 0; src
< num_src
; ++src
) {
463 sel
= alu
->src
[src
].sel
;
464 elem
= alu
->src
[src
].chan
;
466 cycle
= cycle_for_bank_swizzle_scl
[bank_swizzle
][src
];
467 if (cycle
< const_count
)
468 /* Cycle for GPR load conflicts with
469 * constant load in transcendental operation. */
471 r
= reserve_gpr(bs
, sel
, elem
, cycle
);
475 /* PV PS restrictions */
476 if (const_count
&& (sel
== 254 || sel
== 255)) {
477 cycle
= cycle_for_bank_swizzle_scl
[bank_swizzle
][src
];
478 if (cycle
< const_count
)
485 static int check_and_set_bank_swizzle(struct r600_bytecode
*bc
,
486 struct r600_bytecode_alu
*slots
[5])
488 struct alu_bank_swizzle bs
;
490 int i
, r
= 0, forced
= 1;
491 boolean scalar_only
= bc
->chip_class
== CAYMAN
? false : true;
492 int max_slots
= bc
->chip_class
== CAYMAN
? 4 : 5;
494 for (i
= 0; i
< max_slots
; i
++) {
496 if (slots
[i
]->bank_swizzle_force
) {
497 slots
[i
]->bank_swizzle
= slots
[i
]->bank_swizzle_force
;
503 if (i
< 4 && slots
[i
])
509 /* Just check every possible combination of bank swizzle.
510 * Not very efficent, but works on the first try in most of the cases. */
511 for (i
= 0; i
< 4; i
++)
512 if (!slots
[i
] || !slots
[i
]->bank_swizzle_force
)
513 bank_swizzle
[i
] = SQ_ALU_VEC_012
;
515 bank_swizzle
[i
] = slots
[i
]->bank_swizzle
;
517 bank_swizzle
[4] = SQ_ALU_SCL_210
;
518 while(bank_swizzle
[4] <= SQ_ALU_SCL_221
) {
520 init_bank_swizzle(&bs
);
521 if (scalar_only
== false) {
522 for (i
= 0; i
< 4; i
++) {
524 r
= check_vector(bc
, slots
[i
], &bs
, bank_swizzle
[i
]);
532 if (!r
&& max_slots
== 5 && slots
[4]) {
533 r
= check_scalar(bc
, slots
[4], &bs
, bank_swizzle
[4]);
536 for (i
= 0; i
< max_slots
; i
++) {
538 slots
[i
]->bank_swizzle
= bank_swizzle
[i
];
546 for (i
= 0; i
< max_slots
; i
++) {
547 if (!slots
[i
] || !slots
[i
]->bank_swizzle_force
) {
549 if (bank_swizzle
[i
] <= SQ_ALU_VEC_210
)
551 else if (i
< max_slots
- 1)
552 bank_swizzle
[i
] = SQ_ALU_VEC_012
;
560 /* Couldn't find a working swizzle. */
564 static int replace_gpr_with_pv_ps(struct r600_bytecode
*bc
,
565 struct r600_bytecode_alu
*slots
[5], struct r600_bytecode_alu
*alu_prev
)
567 struct r600_bytecode_alu
*prev
[5];
569 int i
, j
, r
, src
, num_src
;
570 int max_slots
= bc
->chip_class
== CAYMAN
? 4 : 5;
572 r
= assign_alu_units(bc
, alu_prev
, prev
);
576 for (i
= 0; i
< max_slots
; ++i
) {
577 if (prev
[i
] && (prev
[i
]->dst
.write
|| prev
[i
]->is_op3
) && !prev
[i
]->dst
.rel
) {
578 gpr
[i
] = prev
[i
]->dst
.sel
;
579 /* cube writes more than PV.X */
580 if (is_alu_reduction_inst(bc
, prev
[i
]))
583 chan
[i
] = prev
[i
]->dst
.chan
;
588 for (i
= 0; i
< max_slots
; ++i
) {
589 struct r600_bytecode_alu
*alu
= slots
[i
];
593 num_src
= r600_bytecode_get_num_operands(bc
, alu
);
594 for (src
= 0; src
< num_src
; ++src
) {
595 if (!is_gpr(alu
->src
[src
].sel
) || alu
->src
[src
].rel
)
598 if (bc
->chip_class
< CAYMAN
) {
599 if (alu
->src
[src
].sel
== gpr
[4] &&
600 alu
->src
[src
].chan
== chan
[4] &&
601 alu_prev
->pred_sel
== alu
->pred_sel
) {
602 alu
->src
[src
].sel
= V_SQ_ALU_SRC_PS
;
603 alu
->src
[src
].chan
= 0;
608 for (j
= 0; j
< 4; ++j
) {
609 if (alu
->src
[src
].sel
== gpr
[j
] &&
610 alu
->src
[src
].chan
== j
&&
611 alu_prev
->pred_sel
== alu
->pred_sel
) {
612 alu
->src
[src
].sel
= V_SQ_ALU_SRC_PV
;
613 alu
->src
[src
].chan
= chan
[j
];
623 void r600_bytecode_special_constants(uint32_t value
, unsigned *sel
, unsigned *neg
)
627 *sel
= V_SQ_ALU_SRC_0
;
630 *sel
= V_SQ_ALU_SRC_1_INT
;
633 *sel
= V_SQ_ALU_SRC_M_1_INT
;
635 case 0x3F800000: /* 1.0f */
636 *sel
= V_SQ_ALU_SRC_1
;
638 case 0x3F000000: /* 0.5f */
639 *sel
= V_SQ_ALU_SRC_0_5
;
641 case 0xBF800000: /* -1.0f */
642 *sel
= V_SQ_ALU_SRC_1
;
645 case 0xBF000000: /* -0.5f */
646 *sel
= V_SQ_ALU_SRC_0_5
;
650 *sel
= V_SQ_ALU_SRC_LITERAL
;
655 /* compute how many literal are needed */
656 static int r600_bytecode_alu_nliterals(struct r600_bytecode
*bc
, struct r600_bytecode_alu
*alu
,
657 uint32_t literal
[4], unsigned *nliteral
)
659 unsigned num_src
= r600_bytecode_get_num_operands(bc
, alu
);
662 for (i
= 0; i
< num_src
; ++i
) {
663 if (alu
->src
[i
].sel
== V_SQ_ALU_SRC_LITERAL
) {
664 uint32_t value
= alu
->src
[i
].value
;
666 for (j
= 0; j
< *nliteral
; ++j
) {
667 if (literal
[j
] == value
) {
675 literal
[(*nliteral
)++] = value
;
682 static void r600_bytecode_alu_adjust_literals(struct r600_bytecode
*bc
,
683 struct r600_bytecode_alu
*alu
,
684 uint32_t literal
[4], unsigned nliteral
)
686 unsigned num_src
= r600_bytecode_get_num_operands(bc
, alu
);
689 for (i
= 0; i
< num_src
; ++i
) {
690 if (alu
->src
[i
].sel
== V_SQ_ALU_SRC_LITERAL
) {
691 uint32_t value
= alu
->src
[i
].value
;
692 for (j
= 0; j
< nliteral
; ++j
) {
693 if (literal
[j
] == value
) {
694 alu
->src
[i
].chan
= j
;
702 static int merge_inst_groups(struct r600_bytecode
*bc
, struct r600_bytecode_alu
*slots
[5],
703 struct r600_bytecode_alu
*alu_prev
)
705 struct r600_bytecode_alu
*prev
[5];
706 struct r600_bytecode_alu
*result
[5] = { NULL
};
708 uint32_t literal
[4], prev_literal
[4];
709 unsigned nliteral
= 0, prev_nliteral
= 0;
711 int i
, j
, r
, src
, num_src
;
712 int num_once_inst
= 0;
713 int have_mova
= 0, have_rel
= 0;
714 int max_slots
= bc
->chip_class
== CAYMAN
? 4 : 5;
716 r
= assign_alu_units(bc
, alu_prev
, prev
);
720 for (i
= 0; i
< max_slots
; ++i
) {
722 if (prev
[i
]->pred_sel
)
724 if (is_alu_once_inst(bc
, prev
[i
]))
728 if (slots
[i
]->pred_sel
)
730 if (is_alu_once_inst(bc
, slots
[i
]))
735 for (i
= 0; i
< max_slots
; ++i
) {
736 struct r600_bytecode_alu
*alu
;
738 if (num_once_inst
> 0)
741 /* check number of literals */
743 if (r600_bytecode_alu_nliterals(bc
, prev
[i
], literal
, &nliteral
))
745 if (r600_bytecode_alu_nliterals(bc
, prev
[i
], prev_literal
, &prev_nliteral
))
747 if (is_alu_mova_inst(bc
, prev
[i
])) {
753 if (alu_uses_rel(bc
, prev
[i
])) {
760 num_once_inst
+= is_alu_once_inst(bc
, prev
[i
]);
762 if (slots
[i
] && r600_bytecode_alu_nliterals(bc
, slots
[i
], literal
, &nliteral
))
765 /* Let's check used slots. */
766 if (prev
[i
] && !slots
[i
]) {
769 } else if (prev
[i
] && slots
[i
]) {
770 if (max_slots
== 5 && result
[4] == NULL
&& prev
[4] == NULL
&& slots
[4] == NULL
) {
771 /* Trans unit is still free try to use it. */
772 if (is_alu_any_unit_inst(bc
, slots
[i
])) {
774 result
[4] = slots
[i
];
775 } else if (is_alu_any_unit_inst(bc
, prev
[i
])) {
776 if (slots
[i
]->dst
.sel
== prev
[i
]->dst
.sel
&&
777 (slots
[i
]->dst
.write
== 1 || slots
[i
]->is_op3
) &&
778 (prev
[i
]->dst
.write
== 1 || prev
[i
]->is_op3
))
781 result
[i
] = slots
[i
];
787 } else if(!slots
[i
]) {
790 if (max_slots
== 5 && slots
[i
] && prev
[4] &&
791 slots
[i
]->dst
.sel
== prev
[4]->dst
.sel
&&
792 slots
[i
]->dst
.chan
== prev
[4]->dst
.chan
&&
793 (slots
[i
]->dst
.write
== 1 || slots
[i
]->is_op3
) &&
794 (prev
[4]->dst
.write
== 1 || prev
[4]->is_op3
))
797 result
[i
] = slots
[i
];
801 num_once_inst
+= is_alu_once_inst(bc
, alu
);
803 /* don't reschedule NOPs */
804 if (is_nop_inst(bc
, alu
))
807 if (is_alu_mova_inst(bc
, alu
)) {
814 if (alu_uses_rel(bc
, alu
)) {
821 /* Let's check source gprs */
822 num_src
= r600_bytecode_get_num_operands(bc
, alu
);
823 for (src
= 0; src
< num_src
; ++src
) {
825 /* Constants don't matter. */
826 if (!is_gpr(alu
->src
[src
].sel
))
829 for (j
= 0; j
< max_slots
; ++j
) {
830 if (!prev
[j
] || !(prev
[j
]->dst
.write
|| prev
[j
]->is_op3
))
833 /* If it's relative then we can't determin which gpr is really used. */
834 if (prev
[j
]->dst
.chan
== alu
->src
[src
].chan
&&
835 (prev
[j
]->dst
.sel
== alu
->src
[src
].sel
||
836 prev
[j
]->dst
.rel
|| alu
->src
[src
].rel
))
842 /* more than one PRED_ or KILL_ ? */
843 if (num_once_inst
> 1)
846 /* check if the result can still be swizzlet */
847 r
= check_and_set_bank_swizzle(bc
, result
);
851 /* looks like everything worked out right, apply the changes */
853 /* undo adding previus literals */
854 bc
->cf_last
->ndw
-= align(prev_nliteral
, 2);
856 /* sort instructions */
857 for (i
= 0; i
< max_slots
; ++i
) {
858 slots
[i
] = result
[i
];
860 LIST_DEL(&result
[i
]->list
);
862 LIST_ADDTAIL(&result
[i
]->list
, &bc
->cf_last
->alu
);
866 /* determine new last instruction */
867 LIST_ENTRY(struct r600_bytecode_alu
, bc
->cf_last
->alu
.prev
, list
)->last
= 1;
869 /* determine new first instruction */
870 for (i
= 0; i
< max_slots
; ++i
) {
872 bc
->cf_last
->curr_bs_head
= result
[i
];
877 bc
->cf_last
->prev_bs_head
= bc
->cf_last
->prev2_bs_head
;
878 bc
->cf_last
->prev2_bs_head
= NULL
;
883 /* we'll keep kcache sets sorted by bank & addr */
884 static int r600_bytecode_alloc_kcache_line(struct r600_bytecode
*bc
,
885 struct r600_bytecode_kcache
*kcache
,
886 unsigned bank
, unsigned line
)
888 int i
, kcache_banks
= bc
->chip_class
>= EVERGREEN
? 4 : 2;
890 for (i
= 0; i
< kcache_banks
; i
++) {
891 if (kcache
[i
].mode
) {
894 if (kcache
[i
].bank
< bank
)
897 if ((kcache
[i
].bank
== bank
&& kcache
[i
].addr
> line
+1) ||
898 kcache
[i
].bank
> bank
) {
899 /* try to insert new line */
900 if (kcache
[kcache_banks
-1].mode
) {
901 /* all sets are in use */
905 memmove(&kcache
[i
+1],&kcache
[i
], (kcache_banks
-i
-1)*sizeof(struct r600_bytecode_kcache
));
906 kcache
[i
].mode
= V_SQ_CF_KCACHE_LOCK_1
;
907 kcache
[i
].bank
= bank
;
908 kcache
[i
].addr
= line
;
912 d
= line
- kcache
[i
].addr
;
916 if (kcache
[i
].mode
== V_SQ_CF_KCACHE_LOCK_2
) {
917 /* we are prepending the line to the current set,
918 * discarding the existing second line,
919 * so we'll have to insert line+2 after it */
922 } else if (kcache
[i
].mode
== V_SQ_CF_KCACHE_LOCK_1
) {
923 kcache
[i
].mode
= V_SQ_CF_KCACHE_LOCK_2
;
926 /* V_SQ_CF_KCACHE_LOCK_LOOP_INDEX is not supported */
930 kcache
[i
].mode
= V_SQ_CF_KCACHE_LOCK_2
;
934 } else { /* free kcache set - use it */
935 kcache
[i
].mode
= V_SQ_CF_KCACHE_LOCK_1
;
936 kcache
[i
].bank
= bank
;
937 kcache
[i
].addr
= line
;
944 static int r600_bytecode_alloc_inst_kcache_lines(struct r600_bytecode
*bc
,
945 struct r600_bytecode_kcache
*kcache
,
946 struct r600_bytecode_alu
*alu
)
950 for (i
= 0; i
< 3; i
++) {
951 unsigned bank
, line
, sel
= alu
->src
[i
].sel
;
956 bank
= alu
->src
[i
].kc_bank
;
959 if ((r
= r600_bytecode_alloc_kcache_line(bc
, kcache
, bank
, line
)))
965 static int r600_bytecode_assign_kcache_banks(struct r600_bytecode
*bc
,
966 struct r600_bytecode_alu
*alu
,
967 struct r600_bytecode_kcache
* kcache
)
971 /* Alter the src operands to refer to the kcache. */
972 for (i
= 0; i
< 3; ++i
) {
973 static const unsigned int base
[] = {128, 160, 256, 288};
974 unsigned int line
, sel
= alu
->src
[i
].sel
, found
= 0;
982 for (j
= 0; j
< 4 && !found
; ++j
) {
983 switch (kcache
[j
].mode
) {
984 case V_SQ_CF_KCACHE_NOP
:
985 case V_SQ_CF_KCACHE_LOCK_LOOP_INDEX
:
986 R600_ERR("unexpected kcache line mode\n");
989 if (kcache
[j
].bank
== alu
->src
[i
].kc_bank
&&
990 kcache
[j
].addr
<= line
&&
991 line
< kcache
[j
].addr
+ kcache
[j
].mode
) {
992 alu
->src
[i
].sel
= sel
- (kcache
[j
].addr
<<4);
993 alu
->src
[i
].sel
+= base
[j
];
1002 static int r600_bytecode_alloc_kcache_lines(struct r600_bytecode
*bc
,
1003 struct r600_bytecode_alu
*alu
,
1006 struct r600_bytecode_kcache kcache_sets
[4];
1007 struct r600_bytecode_kcache
*kcache
= kcache_sets
;
1010 memcpy(kcache
, bc
->cf_last
->kcache
, 4 * sizeof(struct r600_bytecode_kcache
));
1012 if ((r
= r600_bytecode_alloc_inst_kcache_lines(bc
, kcache
, alu
))) {
1013 /* can't alloc, need to start new clause */
1014 if ((r
= r600_bytecode_add_cf(bc
))) {
1017 bc
->cf_last
->op
= type
;
1019 /* retry with the new clause */
1020 kcache
= bc
->cf_last
->kcache
;
1021 if ((r
= r600_bytecode_alloc_inst_kcache_lines(bc
, kcache
, alu
))) {
1022 /* can't alloc again- should never happen */
1026 /* update kcache sets */
1027 memcpy(bc
->cf_last
->kcache
, kcache
, 4 * sizeof(struct r600_bytecode_kcache
));
1030 /* if we actually used more than 2 kcache sets - use ALU_EXTENDED on eg+ */
1031 if (kcache
[2].mode
!= V_SQ_CF_KCACHE_NOP
) {
1032 if (bc
->chip_class
< EVERGREEN
)
1034 bc
->cf_last
->eg_alu_extended
= 1;
1040 static int insert_nop_r6xx(struct r600_bytecode
*bc
)
1042 struct r600_bytecode_alu alu
;
1045 for (i
= 0; i
< 4; i
++) {
1046 memset(&alu
, 0, sizeof(alu
));
1047 alu
.op
= ALU_OP0_NOP
;
1048 alu
.src
[0].chan
= i
;
1050 alu
.last
= (i
== 3);
1051 r
= r600_bytecode_add_alu(bc
, &alu
);
1058 /* load AR register from gpr (bc->ar_reg) with MOVA_INT */
1059 static int load_ar_r6xx(struct r600_bytecode
*bc
)
1061 struct r600_bytecode_alu alu
;
1067 /* hack to avoid making MOVA the last instruction in the clause */
1068 if ((bc
->cf_last
->ndw
>>1) >= 110)
1069 bc
->force_add_cf
= 1;
1071 memset(&alu
, 0, sizeof(alu
));
1072 alu
.op
= ALU_OP1_MOVA_GPR_INT
;
1073 alu
.src
[0].sel
= bc
->ar_reg
;
1074 alu
.src
[0].chan
= bc
->ar_chan
;
1076 alu
.index_mode
= INDEX_MODE_LOOP
;
1077 r
= r600_bytecode_add_alu(bc
, &alu
);
1081 /* no requirement to set uses waterfall on MOVA_GPR_INT */
1086 /* load AR register from gpr (bc->ar_reg) with MOVA_INT */
1087 static int load_ar(struct r600_bytecode
*bc
)
1089 struct r600_bytecode_alu alu
;
1092 if (bc
->ar_handling
)
1093 return load_ar_r6xx(bc
);
1098 /* hack to avoid making MOVA the last instruction in the clause */
1099 if ((bc
->cf_last
->ndw
>>1) >= 110)
1100 bc
->force_add_cf
= 1;
1102 memset(&alu
, 0, sizeof(alu
));
1103 alu
.op
= ALU_OP1_MOVA_INT
;
1104 alu
.src
[0].sel
= bc
->ar_reg
;
1105 alu
.src
[0].chan
= bc
->ar_chan
;
1107 r
= r600_bytecode_add_alu(bc
, &alu
);
1111 bc
->cf_last
->r6xx_uses_waterfall
= 1;
1116 int r600_bytecode_add_alu_type(struct r600_bytecode
*bc
,
1117 const struct r600_bytecode_alu
*alu
, unsigned type
)
1119 struct r600_bytecode_alu
*nalu
= r600_bytecode_alu();
1120 struct r600_bytecode_alu
*lalu
;
1125 memcpy(nalu
, alu
, sizeof(struct r600_bytecode_alu
));
1127 if (bc
->cf_last
!= NULL
&& bc
->cf_last
->op
!= type
) {
1128 /* check if we could add it anyway */
1129 if (bc
->cf_last
->op
== CF_OP_ALU
&&
1130 type
== CF_OP_ALU_PUSH_BEFORE
) {
1131 LIST_FOR_EACH_ENTRY(lalu
, &bc
->cf_last
->alu
, list
) {
1132 if (lalu
->execute_mask
) {
1133 bc
->force_add_cf
= 1;
1138 bc
->force_add_cf
= 1;
1141 /* cf can contains only alu or only vtx or only tex */
1142 if (bc
->cf_last
== NULL
|| bc
->force_add_cf
) {
1143 r
= r600_bytecode_add_cf(bc
);
1149 bc
->cf_last
->op
= type
;
1151 /* Check AR usage and load it if required */
1152 for (i
= 0; i
< 3; i
++)
1153 if (nalu
->src
[i
].rel
&& !bc
->ar_loaded
)
1156 if (nalu
->dst
.rel
&& !bc
->ar_loaded
)
1159 /* Setup the kcache for this ALU instruction. This will start a new
1160 * ALU clause if needed. */
1161 if ((r
= r600_bytecode_alloc_kcache_lines(bc
, nalu
, type
))) {
1166 if (!bc
->cf_last
->curr_bs_head
) {
1167 bc
->cf_last
->curr_bs_head
= nalu
;
1169 /* number of gpr == the last gpr used in any alu */
1170 for (i
= 0; i
< 3; i
++) {
1171 if (nalu
->src
[i
].sel
>= bc
->ngpr
&& nalu
->src
[i
].sel
< 128) {
1172 bc
->ngpr
= nalu
->src
[i
].sel
+ 1;
1174 if (nalu
->src
[i
].sel
== V_SQ_ALU_SRC_LITERAL
)
1175 r600_bytecode_special_constants(nalu
->src
[i
].value
,
1176 &nalu
->src
[i
].sel
, &nalu
->src
[i
].neg
);
1178 if (nalu
->dst
.sel
>= bc
->ngpr
) {
1179 bc
->ngpr
= nalu
->dst
.sel
+ 1;
1181 LIST_ADDTAIL(&nalu
->list
, &bc
->cf_last
->alu
);
1182 /* each alu use 2 dwords */
1183 bc
->cf_last
->ndw
+= 2;
1186 /* process cur ALU instructions for bank swizzle */
1188 uint32_t literal
[4];
1190 struct r600_bytecode_alu
*slots
[5];
1191 int max_slots
= bc
->chip_class
== CAYMAN
? 4 : 5;
1192 r
= assign_alu_units(bc
, bc
->cf_last
->curr_bs_head
, slots
);
1196 if (bc
->cf_last
->prev_bs_head
) {
1197 r
= merge_inst_groups(bc
, slots
, bc
->cf_last
->prev_bs_head
);
1202 if (bc
->cf_last
->prev_bs_head
) {
1203 r
= replace_gpr_with_pv_ps(bc
, slots
, bc
->cf_last
->prev_bs_head
);
1208 r
= check_and_set_bank_swizzle(bc
, slots
);
1212 for (i
= 0, nliteral
= 0; i
< max_slots
; i
++) {
1214 r
= r600_bytecode_alu_nliterals(bc
, slots
[i
], literal
, &nliteral
);
1219 bc
->cf_last
->ndw
+= align(nliteral
, 2);
1221 /* at most 128 slots, one add alu can add 5 slots + 4 constants(2 slots)
1223 if ((bc
->cf_last
->ndw
>> 1) >= 120) {
1224 bc
->force_add_cf
= 1;
1227 bc
->cf_last
->prev2_bs_head
= bc
->cf_last
->prev_bs_head
;
1228 bc
->cf_last
->prev_bs_head
= bc
->cf_last
->curr_bs_head
;
1229 bc
->cf_last
->curr_bs_head
= NULL
;
1232 if (nalu
->dst
.rel
&& bc
->r6xx_nop_after_rel_dst
)
1233 insert_nop_r6xx(bc
);
1238 int r600_bytecode_add_alu(struct r600_bytecode
*bc
, const struct r600_bytecode_alu
*alu
)
1240 return r600_bytecode_add_alu_type(bc
, alu
, CF_OP_ALU
);
1243 static unsigned r600_bytecode_num_tex_and_vtx_instructions(const struct r600_bytecode
*bc
)
1245 switch (bc
->chip_class
) {
1255 R600_ERR("Unknown chip class %d.\n", bc
->chip_class
);
1260 static inline boolean
last_inst_was_not_vtx_fetch(struct r600_bytecode
*bc
)
1262 return !((r600_isa_cf(bc
->cf_last
->op
)->flags
& CF_FETCH
) &&
1263 (bc
->chip_class
== CAYMAN
||
1264 bc
->cf_last
->op
!= CF_OP_TEX
));
1267 int r600_bytecode_add_vtx(struct r600_bytecode
*bc
, const struct r600_bytecode_vtx
*vtx
)
1269 struct r600_bytecode_vtx
*nvtx
= r600_bytecode_vtx();
1274 memcpy(nvtx
, vtx
, sizeof(struct r600_bytecode_vtx
));
1276 /* cf can contains only alu or only vtx or only tex */
1277 if (bc
->cf_last
== NULL
||
1278 last_inst_was_not_vtx_fetch(bc
) ||
1280 r
= r600_bytecode_add_cf(bc
);
1285 switch (bc
->chip_class
) {
1289 bc
->cf_last
->op
= CF_OP_VTX
;
1292 bc
->cf_last
->op
= CF_OP_TEX
;
1295 R600_ERR("Unknown chip class %d.\n", bc
->chip_class
);
1300 LIST_ADDTAIL(&nvtx
->list
, &bc
->cf_last
->vtx
);
1301 /* each fetch use 4 dwords */
1302 bc
->cf_last
->ndw
+= 4;
1304 if ((bc
->cf_last
->ndw
/ 4) >= r600_bytecode_num_tex_and_vtx_instructions(bc
))
1305 bc
->force_add_cf
= 1;
1307 bc
->ngpr
= MAX2(bc
->ngpr
, vtx
->src_gpr
+ 1);
1308 bc
->ngpr
= MAX2(bc
->ngpr
, vtx
->dst_gpr
+ 1);
1313 int r600_bytecode_add_tex(struct r600_bytecode
*bc
, const struct r600_bytecode_tex
*tex
)
1315 struct r600_bytecode_tex
*ntex
= r600_bytecode_tex();
1320 memcpy(ntex
, tex
, sizeof(struct r600_bytecode_tex
));
1322 /* we can't fetch data und use it as texture lookup address in the same TEX clause */
1323 if (bc
->cf_last
!= NULL
&&
1324 bc
->cf_last
->op
== CF_OP_TEX
) {
1325 struct r600_bytecode_tex
*ttex
;
1326 LIST_FOR_EACH_ENTRY(ttex
, &bc
->cf_last
->tex
, list
) {
1327 if (ttex
->dst_gpr
== ntex
->src_gpr
) {
1328 bc
->force_add_cf
= 1;
1332 /* slight hack to make gradients always go into same cf */
1333 if (ntex
->op
== FETCH_OP_SET_GRADIENTS_H
)
1334 bc
->force_add_cf
= 1;
1337 /* cf can contains only alu or only vtx or only tex */
1338 if (bc
->cf_last
== NULL
||
1339 bc
->cf_last
->op
!= CF_OP_TEX
||
1341 r
= r600_bytecode_add_cf(bc
);
1346 bc
->cf_last
->op
= CF_OP_TEX
;
1348 if (ntex
->src_gpr
>= bc
->ngpr
) {
1349 bc
->ngpr
= ntex
->src_gpr
+ 1;
1351 if (ntex
->dst_gpr
>= bc
->ngpr
) {
1352 bc
->ngpr
= ntex
->dst_gpr
+ 1;
1354 LIST_ADDTAIL(&ntex
->list
, &bc
->cf_last
->tex
);
1355 /* each texture fetch use 4 dwords */
1356 bc
->cf_last
->ndw
+= 4;
1358 if ((bc
->cf_last
->ndw
/ 4) >= r600_bytecode_num_tex_and_vtx_instructions(bc
))
1359 bc
->force_add_cf
= 1;
1363 int r600_bytecode_add_cfinst(struct r600_bytecode
*bc
, unsigned op
)
1366 r
= r600_bytecode_add_cf(bc
);
1370 bc
->cf_last
->cond
= V_SQ_CF_COND_ACTIVE
;
1371 bc
->cf_last
->op
= op
;
1375 int cm_bytecode_add_cf_end(struct r600_bytecode
*bc
)
1377 return r600_bytecode_add_cfinst(bc
, CF_OP_CF_END
);
1380 /* common to all 3 families */
1381 static int r600_bytecode_vtx_build(struct r600_bytecode
*bc
, struct r600_bytecode_vtx
*vtx
, unsigned id
)
1383 bc
->bytecode
[id
] = S_SQ_VTX_WORD0_BUFFER_ID(vtx
->buffer_id
) |
1384 S_SQ_VTX_WORD0_FETCH_TYPE(vtx
->fetch_type
) |
1385 S_SQ_VTX_WORD0_SRC_GPR(vtx
->src_gpr
) |
1386 S_SQ_VTX_WORD0_SRC_SEL_X(vtx
->src_sel_x
);
1387 if (bc
->chip_class
< CAYMAN
)
1388 bc
->bytecode
[id
] |= S_SQ_VTX_WORD0_MEGA_FETCH_COUNT(vtx
->mega_fetch_count
);
1390 bc
->bytecode
[id
++] = S_SQ_VTX_WORD1_DST_SEL_X(vtx
->dst_sel_x
) |
1391 S_SQ_VTX_WORD1_DST_SEL_Y(vtx
->dst_sel_y
) |
1392 S_SQ_VTX_WORD1_DST_SEL_Z(vtx
->dst_sel_z
) |
1393 S_SQ_VTX_WORD1_DST_SEL_W(vtx
->dst_sel_w
) |
1394 S_SQ_VTX_WORD1_USE_CONST_FIELDS(vtx
->use_const_fields
) |
1395 S_SQ_VTX_WORD1_DATA_FORMAT(vtx
->data_format
) |
1396 S_SQ_VTX_WORD1_NUM_FORMAT_ALL(vtx
->num_format_all
) |
1397 S_SQ_VTX_WORD1_FORMAT_COMP_ALL(vtx
->format_comp_all
) |
1398 S_SQ_VTX_WORD1_SRF_MODE_ALL(vtx
->srf_mode_all
) |
1399 S_SQ_VTX_WORD1_GPR_DST_GPR(vtx
->dst_gpr
);
1400 bc
->bytecode
[id
] = S_SQ_VTX_WORD2_OFFSET(vtx
->offset
)|
1401 S_SQ_VTX_WORD2_ENDIAN_SWAP(vtx
->endian
);
1402 if (bc
->chip_class
< CAYMAN
)
1403 bc
->bytecode
[id
] |= S_SQ_VTX_WORD2_MEGA_FETCH(1);
1405 bc
->bytecode
[id
++] = 0;
1409 /* common to all 3 families */
1410 static int r600_bytecode_tex_build(struct r600_bytecode
*bc
, struct r600_bytecode_tex
*tex
, unsigned id
)
1412 bc
->bytecode
[id
++] = S_SQ_TEX_WORD0_TEX_INST(
1413 r600_isa_fetch_opcode(bc
->isa
->hw_class
, tex
->op
)) |
1414 EG_S_SQ_TEX_WORD0_INST_MOD(tex
->inst_mod
) |
1415 S_SQ_TEX_WORD0_RESOURCE_ID(tex
->resource_id
) |
1416 S_SQ_TEX_WORD0_SRC_GPR(tex
->src_gpr
) |
1417 S_SQ_TEX_WORD0_SRC_REL(tex
->src_rel
);
1418 bc
->bytecode
[id
++] = S_SQ_TEX_WORD1_DST_GPR(tex
->dst_gpr
) |
1419 S_SQ_TEX_WORD1_DST_REL(tex
->dst_rel
) |
1420 S_SQ_TEX_WORD1_DST_SEL_X(tex
->dst_sel_x
) |
1421 S_SQ_TEX_WORD1_DST_SEL_Y(tex
->dst_sel_y
) |
1422 S_SQ_TEX_WORD1_DST_SEL_Z(tex
->dst_sel_z
) |
1423 S_SQ_TEX_WORD1_DST_SEL_W(tex
->dst_sel_w
) |
1424 S_SQ_TEX_WORD1_LOD_BIAS(tex
->lod_bias
) |
1425 S_SQ_TEX_WORD1_COORD_TYPE_X(tex
->coord_type_x
) |
1426 S_SQ_TEX_WORD1_COORD_TYPE_Y(tex
->coord_type_y
) |
1427 S_SQ_TEX_WORD1_COORD_TYPE_Z(tex
->coord_type_z
) |
1428 S_SQ_TEX_WORD1_COORD_TYPE_W(tex
->coord_type_w
);
1429 bc
->bytecode
[id
++] = S_SQ_TEX_WORD2_OFFSET_X(tex
->offset_x
) |
1430 S_SQ_TEX_WORD2_OFFSET_Y(tex
->offset_y
) |
1431 S_SQ_TEX_WORD2_OFFSET_Z(tex
->offset_z
) |
1432 S_SQ_TEX_WORD2_SAMPLER_ID(tex
->sampler_id
) |
1433 S_SQ_TEX_WORD2_SRC_SEL_X(tex
->src_sel_x
) |
1434 S_SQ_TEX_WORD2_SRC_SEL_Y(tex
->src_sel_y
) |
1435 S_SQ_TEX_WORD2_SRC_SEL_Z(tex
->src_sel_z
) |
1436 S_SQ_TEX_WORD2_SRC_SEL_W(tex
->src_sel_w
);
1437 bc
->bytecode
[id
++] = 0;
1441 /* r600 only, r700/eg bits in r700_asm.c */
1442 static int r600_bytecode_alu_build(struct r600_bytecode
*bc
, struct r600_bytecode_alu
*alu
, unsigned id
)
1444 unsigned opcode
= r600_isa_alu_opcode(bc
->isa
->hw_class
, alu
->op
);
1446 /* don't replace gpr by pv or ps for destination register */
1447 bc
->bytecode
[id
++] = S_SQ_ALU_WORD0_SRC0_SEL(alu
->src
[0].sel
) |
1448 S_SQ_ALU_WORD0_SRC0_REL(alu
->src
[0].rel
) |
1449 S_SQ_ALU_WORD0_SRC0_CHAN(alu
->src
[0].chan
) |
1450 S_SQ_ALU_WORD0_SRC0_NEG(alu
->src
[0].neg
) |
1451 S_SQ_ALU_WORD0_SRC1_SEL(alu
->src
[1].sel
) |
1452 S_SQ_ALU_WORD0_SRC1_REL(alu
->src
[1].rel
) |
1453 S_SQ_ALU_WORD0_SRC1_CHAN(alu
->src
[1].chan
) |
1454 S_SQ_ALU_WORD0_SRC1_NEG(alu
->src
[1].neg
) |
1455 S_SQ_ALU_WORD0_INDEX_MODE(alu
->index_mode
) |
1456 S_SQ_ALU_WORD0_PRED_SEL(alu
->pred_sel
) |
1457 S_SQ_ALU_WORD0_LAST(alu
->last
);
1460 bc
->bytecode
[id
++] = S_SQ_ALU_WORD1_DST_GPR(alu
->dst
.sel
) |
1461 S_SQ_ALU_WORD1_DST_CHAN(alu
->dst
.chan
) |
1462 S_SQ_ALU_WORD1_DST_REL(alu
->dst
.rel
) |
1463 S_SQ_ALU_WORD1_CLAMP(alu
->dst
.clamp
) |
1464 S_SQ_ALU_WORD1_OP3_SRC2_SEL(alu
->src
[2].sel
) |
1465 S_SQ_ALU_WORD1_OP3_SRC2_REL(alu
->src
[2].rel
) |
1466 S_SQ_ALU_WORD1_OP3_SRC2_CHAN(alu
->src
[2].chan
) |
1467 S_SQ_ALU_WORD1_OP3_SRC2_NEG(alu
->src
[2].neg
) |
1468 S_SQ_ALU_WORD1_OP3_ALU_INST(opcode
) |
1469 S_SQ_ALU_WORD1_BANK_SWIZZLE(alu
->bank_swizzle
);
1471 bc
->bytecode
[id
++] = S_SQ_ALU_WORD1_DST_GPR(alu
->dst
.sel
) |
1472 S_SQ_ALU_WORD1_DST_CHAN(alu
->dst
.chan
) |
1473 S_SQ_ALU_WORD1_DST_REL(alu
->dst
.rel
) |
1474 S_SQ_ALU_WORD1_CLAMP(alu
->dst
.clamp
) |
1475 S_SQ_ALU_WORD1_OP2_SRC0_ABS(alu
->src
[0].abs
) |
1476 S_SQ_ALU_WORD1_OP2_SRC1_ABS(alu
->src
[1].abs
) |
1477 S_SQ_ALU_WORD1_OP2_WRITE_MASK(alu
->dst
.write
) |
1478 S_SQ_ALU_WORD1_OP2_OMOD(alu
->omod
) |
1479 S_SQ_ALU_WORD1_OP2_ALU_INST(opcode
) |
1480 S_SQ_ALU_WORD1_BANK_SWIZZLE(alu
->bank_swizzle
) |
1481 S_SQ_ALU_WORD1_OP2_UPDATE_EXECUTE_MASK(alu
->execute_mask
) |
1482 S_SQ_ALU_WORD1_OP2_UPDATE_PRED(alu
->update_pred
);
1487 static void r600_bytecode_cf_vtx_build(uint32_t *bytecode
, const struct r600_bytecode_cf
*cf
)
1489 *bytecode
++ = S_SQ_CF_WORD0_ADDR(cf
->addr
>> 1);
1490 *bytecode
++ = S_SQ_CF_WORD1_CF_INST(r600_isa_cf_opcode(ISA_CC_R600
, cf
->op
)) |
1491 S_SQ_CF_WORD1_BARRIER(1) |
1492 S_SQ_CF_WORD1_COUNT((cf
->ndw
/ 4) - 1);
1495 /* common for r600/r700 - eg in eg_asm.c */
1496 static int r600_bytecode_cf_build(struct r600_bytecode
*bc
, struct r600_bytecode_cf
*cf
)
1498 unsigned id
= cf
->id
;
1499 const struct cf_op_info
*cfop
= r600_isa_cf(cf
->op
);
1500 unsigned opcode
= r600_isa_cf_opcode(bc
->isa
->hw_class
, cf
->op
);
1503 if (cf
->op
== CF_NATIVE
) {
1504 bc
->bytecode
[id
++] = cf
->isa
[0];
1505 bc
->bytecode
[id
++] = cf
->isa
[1];
1506 } else if (cfop
->flags
& CF_ALU
) {
1507 bc
->bytecode
[id
++] = S_SQ_CF_ALU_WORD0_ADDR(cf
->addr
>> 1) |
1508 S_SQ_CF_ALU_WORD0_KCACHE_MODE0(cf
->kcache
[0].mode
) |
1509 S_SQ_CF_ALU_WORD0_KCACHE_BANK0(cf
->kcache
[0].bank
) |
1510 S_SQ_CF_ALU_WORD0_KCACHE_BANK1(cf
->kcache
[1].bank
);
1512 bc
->bytecode
[id
++] = S_SQ_CF_ALU_WORD1_CF_INST(opcode
) |
1513 S_SQ_CF_ALU_WORD1_KCACHE_MODE1(cf
->kcache
[1].mode
) |
1514 S_SQ_CF_ALU_WORD1_KCACHE_ADDR0(cf
->kcache
[0].addr
) |
1515 S_SQ_CF_ALU_WORD1_KCACHE_ADDR1(cf
->kcache
[1].addr
) |
1516 S_SQ_CF_ALU_WORD1_BARRIER(1) |
1517 S_SQ_CF_ALU_WORD1_USES_WATERFALL(bc
->chip_class
== R600
? cf
->r6xx_uses_waterfall
: 0) |
1518 S_SQ_CF_ALU_WORD1_COUNT((cf
->ndw
/ 2) - 1);
1519 } else if (cfop
->flags
& CF_FETCH
) {
1520 if (bc
->chip_class
== R700
)
1521 r700_bytecode_cf_vtx_build(&bc
->bytecode
[id
], cf
);
1523 r600_bytecode_cf_vtx_build(&bc
->bytecode
[id
], cf
);
1524 } else if (cfop
->flags
& CF_EXP
) {
1525 bc
->bytecode
[id
++] = S_SQ_CF_ALLOC_EXPORT_WORD0_RW_GPR(cf
->output
.gpr
) |
1526 S_SQ_CF_ALLOC_EXPORT_WORD0_ELEM_SIZE(cf
->output
.elem_size
) |
1527 S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(cf
->output
.array_base
) |
1528 S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(cf
->output
.type
) |
1529 S_SQ_CF_ALLOC_EXPORT_WORD0_INDEX_GPR(cf
->output
.index_gpr
);
1530 bc
->bytecode
[id
++] = S_SQ_CF_ALLOC_EXPORT_WORD1_BURST_COUNT(cf
->output
.burst_count
- 1) |
1531 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_X(cf
->output
.swizzle_x
) |
1532 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Y(cf
->output
.swizzle_y
) |
1533 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Z(cf
->output
.swizzle_z
) |
1534 S_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_W(cf
->output
.swizzle_w
) |
1535 S_SQ_CF_ALLOC_EXPORT_WORD1_BARRIER(cf
->barrier
) |
1536 S_SQ_CF_ALLOC_EXPORT_WORD1_CF_INST(opcode
) |
1537 S_SQ_CF_ALLOC_EXPORT_WORD1_END_OF_PROGRAM(cf
->end_of_program
);
1538 } else if (cfop
->flags
& CF_MEM
) {
1539 bc
->bytecode
[id
++] = S_SQ_CF_ALLOC_EXPORT_WORD0_RW_GPR(cf
->output
.gpr
) |
1540 S_SQ_CF_ALLOC_EXPORT_WORD0_ELEM_SIZE(cf
->output
.elem_size
) |
1541 S_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(cf
->output
.array_base
) |
1542 S_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(cf
->output
.type
) |
1543 S_SQ_CF_ALLOC_EXPORT_WORD0_INDEX_GPR(cf
->output
.index_gpr
);
1544 bc
->bytecode
[id
++] = S_SQ_CF_ALLOC_EXPORT_WORD1_BURST_COUNT(cf
->output
.burst_count
- 1) |
1545 S_SQ_CF_ALLOC_EXPORT_WORD1_BARRIER(cf
->barrier
) |
1546 S_SQ_CF_ALLOC_EXPORT_WORD1_CF_INST(opcode
) |
1547 S_SQ_CF_ALLOC_EXPORT_WORD1_END_OF_PROGRAM(cf
->end_of_program
) |
1548 S_SQ_CF_ALLOC_EXPORT_WORD1_BUF_ARRAY_SIZE(cf
->output
.array_size
) |
1549 S_SQ_CF_ALLOC_EXPORT_WORD1_BUF_COMP_MASK(cf
->output
.comp_mask
);
1551 bc
->bytecode
[id
++] = S_SQ_CF_WORD0_ADDR(cf
->cf_addr
>> 1);
1552 bc
->bytecode
[id
++] = S_SQ_CF_WORD1_CF_INST(opcode
) |
1553 S_SQ_CF_WORD1_BARRIER(1) |
1554 S_SQ_CF_WORD1_COND(cf
->cond
) |
1555 S_SQ_CF_WORD1_POP_COUNT(cf
->pop_count
) |
1556 S_SQ_CF_WORD1_END_OF_PROGRAM(cf
->end_of_program
);
1561 int r600_bytecode_build(struct r600_bytecode
*bc
)
1563 struct r600_bytecode_cf
*cf
;
1564 struct r600_bytecode_alu
*alu
;
1565 struct r600_bytecode_vtx
*vtx
;
1566 struct r600_bytecode_tex
*tex
;
1567 uint32_t literal
[4];
1572 if (!bc
->nstack
) // If not 0, Stack_size already provided by llvm
1573 bc
->nstack
= bc
->stack
.max_entries
;
1575 if (bc
->type
== TGSI_PROCESSOR_VERTEX
&& !bc
->nstack
) {
1579 /* first path compute addr of each CF block */
1580 /* addr start after all the CF instructions */
1581 addr
= bc
->cf_last
->id
+ 2;
1582 LIST_FOR_EACH_ENTRY(cf
, &bc
->cf
, list
) {
1583 if (r600_isa_cf(cf
->op
)->flags
& CF_FETCH
) {
1585 addr
&= 0xFFFFFFFCUL
;
1589 bc
->ndw
= cf
->addr
+ cf
->ndw
;
1592 bc
->bytecode
= calloc(1, bc
->ndw
* 4);
1593 if (bc
->bytecode
== NULL
)
1595 LIST_FOR_EACH_ENTRY(cf
, &bc
->cf
, list
) {
1596 const struct cf_op_info
*cfop
= r600_isa_cf(cf
->op
);
1598 if (bc
->chip_class
>= EVERGREEN
)
1599 r
= eg_bytecode_cf_build(bc
, cf
);
1601 r
= r600_bytecode_cf_build(bc
, cf
);
1604 if (cfop
->flags
& CF_ALU
) {
1606 memset(literal
, 0, sizeof(literal
));
1607 LIST_FOR_EACH_ENTRY(alu
, &cf
->alu
, list
) {
1608 r
= r600_bytecode_alu_nliterals(bc
, alu
, literal
, &nliteral
);
1611 r600_bytecode_alu_adjust_literals(bc
, alu
, literal
, nliteral
);
1612 r600_bytecode_assign_kcache_banks(bc
, alu
, cf
->kcache
);
1614 switch(bc
->chip_class
) {
1616 r
= r600_bytecode_alu_build(bc
, alu
, addr
);
1619 case EVERGREEN
: /* eg alu is same encoding as r700 */
1621 r
= r700_bytecode_alu_build(bc
, alu
, addr
);
1624 R600_ERR("unknown chip class %d.\n", bc
->chip_class
);
1631 for (i
= 0; i
< align(nliteral
, 2); ++i
) {
1632 bc
->bytecode
[addr
++] = literal
[i
];
1635 memset(literal
, 0, sizeof(literal
));
1638 } else if (cf
->op
== CF_OP_VTX
) {
1639 LIST_FOR_EACH_ENTRY(vtx
, &cf
->vtx
, list
) {
1640 r
= r600_bytecode_vtx_build(bc
, vtx
, addr
);
1645 } else if (cf
->op
== CF_OP_TEX
) {
1646 LIST_FOR_EACH_ENTRY(vtx
, &cf
->vtx
, list
) {
1647 assert(bc
->chip_class
>= EVERGREEN
);
1648 r
= r600_bytecode_vtx_build(bc
, vtx
, addr
);
1653 LIST_FOR_EACH_ENTRY(tex
, &cf
->tex
, list
) {
1654 r
= r600_bytecode_tex_build(bc
, tex
, addr
);
1664 void r600_bytecode_clear(struct r600_bytecode
*bc
)
1666 struct r600_bytecode_cf
*cf
= NULL
, *next_cf
;
1669 bc
->bytecode
= NULL
;
1671 LIST_FOR_EACH_ENTRY_SAFE(cf
, next_cf
, &bc
->cf
, list
) {
1672 struct r600_bytecode_alu
*alu
= NULL
, *next_alu
;
1673 struct r600_bytecode_tex
*tex
= NULL
, *next_tex
;
1674 struct r600_bytecode_tex
*vtx
= NULL
, *next_vtx
;
1676 LIST_FOR_EACH_ENTRY_SAFE(alu
, next_alu
, &cf
->alu
, list
) {
1680 LIST_INITHEAD(&cf
->alu
);
1682 LIST_FOR_EACH_ENTRY_SAFE(tex
, next_tex
, &cf
->tex
, list
) {
1686 LIST_INITHEAD(&cf
->tex
);
1688 LIST_FOR_EACH_ENTRY_SAFE(vtx
, next_vtx
, &cf
->vtx
, list
) {
1692 LIST_INITHEAD(&cf
->vtx
);
1697 LIST_INITHEAD(&cf
->list
);
1700 static int print_swizzle(unsigned swz
)
1702 const char * swzchars
= "xyzw01?_";
1703 assert(swz
<8 && swz
!= 6);
1704 return fprintf(stderr
, "%c", swzchars
[swz
]);
1707 static int print_sel(unsigned sel
, unsigned rel
, unsigned index_mode
,
1708 unsigned need_brackets
)
1711 if (rel
&& index_mode
>= 5 && sel
< 128)
1712 o
+= fprintf(stderr
, "G");
1713 if (rel
|| need_brackets
) {
1714 o
+= fprintf(stderr
, "[");
1716 o
+= fprintf(stderr
, "%d", sel
);
1718 if (index_mode
== 0 || index_mode
== 6)
1719 o
+= fprintf(stderr
, "+AR");
1720 else if (index_mode
== 4)
1721 o
+= fprintf(stderr
, "+AL");
1723 if (rel
|| need_brackets
) {
1724 o
+= fprintf(stderr
, "]");
1729 static int print_dst(struct r600_bytecode_alu
*alu
)
1732 unsigned sel
= alu
->dst
.sel
;
1733 char reg_char
= 'R';
1734 if (sel
> 128 - 4) { /* clause temporary gpr */
1739 if (alu
->dst
.write
|| alu
->is_op3
) {
1740 o
+= fprintf(stderr
, "%c", reg_char
);
1741 o
+= print_sel(alu
->dst
.sel
, alu
->dst
.rel
, alu
->index_mode
, 0);
1743 o
+= fprintf(stderr
, "__");
1745 o
+= fprintf(stderr
, ".");
1746 o
+= print_swizzle(alu
->dst
.chan
);
1750 static int print_src(struct r600_bytecode_alu
*alu
, unsigned idx
)
1753 struct r600_bytecode_alu_src
*src
= &alu
->src
[idx
];
1754 unsigned sel
= src
->sel
, need_sel
= 1, need_chan
= 1, need_brackets
= 0;
1757 o
+= fprintf(stderr
,"-");
1759 o
+= fprintf(stderr
,"|");
1761 if (sel
< 128 - 4) {
1762 o
+= fprintf(stderr
, "R");
1763 } else if (sel
< 128) {
1764 o
+= fprintf(stderr
, "T");
1766 } else if (sel
< 160) {
1767 o
+= fprintf(stderr
, "KC0");
1770 } else if (sel
< 192) {
1771 o
+= fprintf(stderr
, "KC1");
1774 } else if (sel
>= 512) {
1775 o
+= fprintf(stderr
, "C%d", src
->kc_bank
);
1778 } else if (sel
>= 448) {
1779 o
+= fprintf(stderr
, "Param");
1782 } else if (sel
>= 288) {
1783 o
+= fprintf(stderr
, "KC3");
1786 } else if (sel
>= 256) {
1787 o
+= fprintf(stderr
, "KC2");
1794 case V_SQ_ALU_SRC_PS
:
1795 o
+= fprintf(stderr
, "PS");
1797 case V_SQ_ALU_SRC_PV
:
1798 o
+= fprintf(stderr
, "PV");
1801 case V_SQ_ALU_SRC_LITERAL
:
1802 o
+= fprintf(stderr
, "[0x%08X %f]", src
->value
, *(float*)&src
->value
);
1804 case V_SQ_ALU_SRC_0_5
:
1805 o
+= fprintf(stderr
, "0.5");
1807 case V_SQ_ALU_SRC_M_1_INT
:
1808 o
+= fprintf(stderr
, "-1");
1810 case V_SQ_ALU_SRC_1_INT
:
1811 o
+= fprintf(stderr
, "1");
1813 case V_SQ_ALU_SRC_1
:
1814 o
+= fprintf(stderr
, "1.0");
1816 case V_SQ_ALU_SRC_0
:
1817 o
+= fprintf(stderr
, "0");
1820 o
+= fprintf(stderr
, "??IMM_%d", sel
);
1826 o
+= print_sel(sel
, src
->rel
, alu
->index_mode
, need_brackets
);
1829 o
+= fprintf(stderr
, ".");
1830 o
+= print_swizzle(src
->chan
);
1834 o
+= fprintf(stderr
,"|");
1839 static int print_indent(int p
, int c
)
1843 o
+= fprintf(stderr
, " ");
1847 void r600_bytecode_disasm(struct r600_bytecode
*bc
)
1849 static int index
= 0;
1850 struct r600_bytecode_cf
*cf
= NULL
;
1851 struct r600_bytecode_alu
*alu
= NULL
;
1852 struct r600_bytecode_vtx
*vtx
= NULL
;
1853 struct r600_bytecode_tex
*tex
= NULL
;
1855 unsigned i
, id
, ngr
= 0, last
;
1856 uint32_t literal
[4];
1860 switch (bc
->chip_class
) {
1875 fprintf(stderr
, "bytecode %d dw -- %d gprs -- %d nstack -------------\n",
1876 bc
->ndw
, bc
->ngpr
, bc
->nstack
);
1877 fprintf(stderr
, "shader %d -- %c\n", index
++, chip
);
1879 LIST_FOR_EACH_ENTRY(cf
, &bc
->cf
, list
) {
1881 if (cf
->op
== CF_NATIVE
) {
1882 fprintf(stderr
, "%04d %08X %08X CF_NATIVE\n", id
, bc
->bytecode
[id
],
1883 bc
->bytecode
[id
+ 1]);
1885 const struct cf_op_info
*cfop
= r600_isa_cf(cf
->op
);
1886 if (cfop
->flags
& CF_ALU
) {
1887 if (cf
->eg_alu_extended
) {
1888 fprintf(stderr
, "%04d %08X %08X %s\n", id
, bc
->bytecode
[id
],
1889 bc
->bytecode
[id
+ 1], "ALU_EXT");
1892 fprintf(stderr
, "%04d %08X %08X %s ", id
, bc
->bytecode
[id
],
1893 bc
->bytecode
[id
+ 1], cfop
->name
);
1894 fprintf(stderr
, "%d @%d ", cf
->ndw
/ 2, cf
->addr
);
1895 for (i
= 0; i
< 4; ++i
) {
1896 if (cf
->kcache
[i
].mode
) {
1897 int c_start
= (cf
->kcache
[i
].addr
<< 4);
1898 int c_end
= c_start
+ (cf
->kcache
[i
].mode
<< 4);
1899 fprintf(stderr
, "KC%d[CB%d:%d-%d] ",
1900 i
, cf
->kcache
[i
].bank
, c_start
, c_end
);
1903 fprintf(stderr
, "\n");
1904 } else if (cfop
->flags
& CF_FETCH
) {
1905 fprintf(stderr
, "%04d %08X %08X %s ", id
, bc
->bytecode
[id
],
1906 bc
->bytecode
[id
+ 1], cfop
->name
);
1907 fprintf(stderr
, "%d @%d ", cf
->ndw
/ 4, cf
->addr
);
1908 fprintf(stderr
, "\n");
1909 } else if (cfop
->flags
& CF_EXP
) {
1911 const char *exp_type
[] = {"PIXEL", "POS ", "PARAM"};
1912 o
+= fprintf(stderr
, "%04d %08X %08X %s ", id
, bc
->bytecode
[id
],
1913 bc
->bytecode
[id
+ 1], cfop
->name
);
1914 o
+= print_indent(o
, 43);
1915 o
+= fprintf(stderr
, "%s ", exp_type
[cf
->output
.type
]);
1916 if (cf
->output
.burst_count
> 1) {
1917 o
+= fprintf(stderr
, "%d-%d ", cf
->output
.array_base
,
1918 cf
->output
.array_base
+ cf
->output
.burst_count
- 1);
1920 o
+= print_indent(o
, 55);
1921 o
+= fprintf(stderr
, "R%d-%d.", cf
->output
.gpr
,
1922 cf
->output
.gpr
+ cf
->output
.burst_count
- 1);
1924 o
+= fprintf(stderr
, "%d ", cf
->output
.array_base
);
1925 o
+= print_indent(o
, 55);
1926 o
+= fprintf(stderr
, "R%d.", cf
->output
.gpr
);
1929 o
+= print_swizzle(cf
->output
.swizzle_x
);
1930 o
+= print_swizzle(cf
->output
.swizzle_y
);
1931 o
+= print_swizzle(cf
->output
.swizzle_z
);
1932 o
+= print_swizzle(cf
->output
.swizzle_w
);
1934 print_indent(o
, 67);
1936 fprintf(stderr
, " ES:%X ", cf
->output
.elem_size
);
1938 fprintf(stderr
, "NO_BARRIER ");
1939 if (cf
->end_of_program
)
1940 fprintf(stderr
, "EOP ");
1941 fprintf(stderr
, "\n");
1942 } else if (r600_isa_cf(cf
->op
)->flags
& CF_MEM
) {
1944 const char *exp_type
[] = {"WRITE", "WRITE_IND", "WRITE_ACK",
1946 o
+= fprintf(stderr
, "%04d %08X %08X %s ", id
,
1947 bc
->bytecode
[id
], bc
->bytecode
[id
+ 1], cfop
->name
);
1948 o
+= print_indent(o
, 43);
1949 o
+= fprintf(stderr
, "%s ", exp_type
[cf
->output
.type
]);
1950 if (cf
->output
.burst_count
> 1) {
1951 o
+= fprintf(stderr
, "%d-%d ", cf
->output
.array_base
,
1952 cf
->output
.array_base
+ cf
->output
.burst_count
- 1);
1953 o
+= print_indent(o
, 55);
1954 o
+= fprintf(stderr
, "R%d-%d.", cf
->output
.gpr
,
1955 cf
->output
.gpr
+ cf
->output
.burst_count
- 1);
1957 o
+= fprintf(stderr
, "%d ", cf
->output
.array_base
);
1958 o
+= print_indent(o
, 55);
1959 o
+= fprintf(stderr
, "R%d.", cf
->output
.gpr
);
1961 for (i
= 0; i
< 4; ++i
) {
1962 if (cf
->output
.comp_mask
& (1 << i
))
1963 o
+= print_swizzle(i
);
1965 o
+= print_swizzle(7);
1968 if (cf
->output
.type
== V_SQ_CF_ALLOC_EXPORT_WORD0_SQ_EXPORT_WRITE_IND
)
1969 o
+= fprintf(stderr
, " R%d", cf
->output
.index_gpr
);
1971 o
+= print_indent(o
, 67);
1973 fprintf(stderr
, " ES:%i ", cf
->output
.elem_size
);
1974 if (cf
->output
.array_size
!= 0xFFF)
1975 fprintf(stderr
, "AS:%i ", cf
->output
.array_size
);
1977 fprintf(stderr
, "NO_BARRIER ");
1978 if (cf
->end_of_program
)
1979 fprintf(stderr
, "EOP ");
1980 fprintf(stderr
, "\n");
1982 fprintf(stderr
, "%04d %08X %08X %s ", id
, bc
->bytecode
[id
],
1983 bc
->bytecode
[id
+ 1], cfop
->name
);
1984 fprintf(stderr
, "@%d ", cf
->cf_addr
);
1986 fprintf(stderr
, "CND:%X ", cf
->cond
);
1988 fprintf(stderr
, "POP:%X ", cf
->pop_count
);
1989 fprintf(stderr
, "\n");
1996 LIST_FOR_EACH_ENTRY(alu
, &cf
->alu
, list
) {
1997 const char *omod_str
[] = {"","*2","*4","/2"};
1998 const struct alu_op_info
*aop
= r600_isa_alu(alu
->op
);
2001 r600_bytecode_alu_nliterals(bc
, alu
, literal
, &nliteral
);
2002 o
+= fprintf(stderr
, " %04d %08X %08X ", id
, bc
->bytecode
[id
], bc
->bytecode
[id
+1]);
2004 o
+= fprintf(stderr
, "%4d ", ++ngr
);
2006 o
+= fprintf(stderr
, " ");
2007 o
+= fprintf(stderr
, "%c%c %c ", alu
->execute_mask
? 'M':' ',
2008 alu
->update_pred
? 'P':' ',
2009 alu
->pred_sel
? alu
->pred_sel
==2 ? '0':'1':' ');
2011 o
+= fprintf(stderr
, "%s%s%s ", aop
->name
,
2012 omod_str
[alu
->omod
], alu
->dst
.clamp
? "_sat":"");
2014 o
+= print_indent(o
,60);
2015 o
+= print_dst(alu
);
2016 for (i
= 0; i
< aop
->src_count
; ++i
) {
2017 o
+= fprintf(stderr
, i
== 0 ? ", ": ", ");
2018 o
+= print_src(alu
, i
);
2021 if (alu
->bank_swizzle
) {
2022 o
+= print_indent(o
,75);
2023 o
+= fprintf(stderr
, " BS:%d", alu
->bank_swizzle
);
2026 fprintf(stderr
, "\n");
2030 for (i
= 0; i
< nliteral
; i
++, id
++) {
2031 float *f
= (float*)(bc
->bytecode
+ id
);
2032 o
= fprintf(stderr
, " %04d %08X", id
, bc
->bytecode
[id
]);
2033 print_indent(o
, 60);
2034 fprintf(stderr
, " %f (%d)\n", *f
, *(bc
->bytecode
+ id
));
2042 LIST_FOR_EACH_ENTRY(tex
, &cf
->tex
, list
) {
2044 o
+= fprintf(stderr
, " %04d %08X %08X %08X ", id
, bc
->bytecode
[id
],
2045 bc
->bytecode
[id
+ 1], bc
->bytecode
[id
+ 2]);
2047 o
+= fprintf(stderr
, "%s ", r600_isa_fetch(tex
->op
)->name
);
2049 o
+= print_indent(o
, 50);
2051 o
+= fprintf(stderr
, "R%d.", tex
->dst_gpr
);
2052 o
+= print_swizzle(tex
->dst_sel_x
);
2053 o
+= print_swizzle(tex
->dst_sel_y
);
2054 o
+= print_swizzle(tex
->dst_sel_z
);
2055 o
+= print_swizzle(tex
->dst_sel_w
);
2057 o
+= fprintf(stderr
, ", R%d.", tex
->src_gpr
);
2058 o
+= print_swizzle(tex
->src_sel_x
);
2059 o
+= print_swizzle(tex
->src_sel_y
);
2060 o
+= print_swizzle(tex
->src_sel_z
);
2061 o
+= print_swizzle(tex
->src_sel_w
);
2063 o
+= fprintf(stderr
, ", RID:%d", tex
->resource_id
);
2064 o
+= fprintf(stderr
, ", SID:%d ", tex
->sampler_id
);
2067 fprintf(stderr
, "LB:%d ", tex
->lod_bias
);
2069 fprintf(stderr
, "CT:%c%c%c%c ",
2070 tex
->coord_type_x
? 'N' : 'U',
2071 tex
->coord_type_y
? 'N' : 'U',
2072 tex
->coord_type_z
? 'N' : 'U',
2073 tex
->coord_type_w
? 'N' : 'U');
2076 fprintf(stderr
, "OX:%d ", tex
->offset_x
);
2078 fprintf(stderr
, "OY:%d ", tex
->offset_y
);
2080 fprintf(stderr
, "OZ:%d ", tex
->offset_z
);
2083 fprintf(stderr
, "\n");
2086 LIST_FOR_EACH_ENTRY(vtx
, &cf
->vtx
, list
) {
2088 const char * fetch_type
[] = {"VERTEX", "INSTANCE", ""};
2089 o
+= fprintf(stderr
, " %04d %08X %08X %08X ", id
, bc
->bytecode
[id
],
2090 bc
->bytecode
[id
+ 1], bc
->bytecode
[id
+ 2]);
2092 o
+= fprintf(stderr
, "%s ", r600_isa_fetch(vtx
->op
)->name
);
2094 o
+= print_indent(o
, 50);
2096 o
+= fprintf(stderr
, "R%d.", vtx
->dst_gpr
);
2097 o
+= print_swizzle(vtx
->dst_sel_x
);
2098 o
+= print_swizzle(vtx
->dst_sel_y
);
2099 o
+= print_swizzle(vtx
->dst_sel_z
);
2100 o
+= print_swizzle(vtx
->dst_sel_w
);
2102 o
+= fprintf(stderr
, ", R%d.", vtx
->src_gpr
);
2103 o
+= print_swizzle(vtx
->src_sel_x
);
2106 fprintf(stderr
, " +%db", vtx
->offset
);
2108 o
+= print_indent(o
, 55);
2110 fprintf(stderr
, ", RID:%d ", vtx
->buffer_id
);
2112 fprintf(stderr
, "%s ", fetch_type
[vtx
->fetch_type
]);
2114 if (bc
->chip_class
< CAYMAN
&& vtx
->mega_fetch_count
)
2115 fprintf(stderr
, "MFC:%d ", vtx
->mega_fetch_count
);
2117 fprintf(stderr
, "UCF:%d ", vtx
->use_const_fields
);
2118 fprintf(stderr
, "FMT(DTA:%d ", vtx
->data_format
);
2119 fprintf(stderr
, "NUM:%d ", vtx
->num_format_all
);
2120 fprintf(stderr
, "COMP:%d ", vtx
->format_comp_all
);
2121 fprintf(stderr
, "MODE:%d)\n", vtx
->srf_mode_all
);
2127 fprintf(stderr
, "--------------------------------------\n");
2130 void r600_vertex_data_type(enum pipe_format pformat
,
2132 unsigned *num_format
, unsigned *format_comp
, unsigned *endian
)
2134 const struct util_format_description
*desc
;
2140 *endian
= ENDIAN_NONE
;
2142 if (pformat
== PIPE_FORMAT_R11G11B10_FLOAT
) {
2143 *format
= FMT_10_11_11_FLOAT
;
2144 *endian
= r600_endian_swap(32);
2148 desc
= util_format_description(pformat
);
2149 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
) {
2153 /* Find the first non-VOID channel. */
2154 for (i
= 0; i
< 4; i
++) {
2155 if (desc
->channel
[i
].type
!= UTIL_FORMAT_TYPE_VOID
) {
2160 *endian
= r600_endian_swap(desc
->channel
[i
].size
);
2162 switch (desc
->channel
[i
].type
) {
2163 /* Half-floats, floats, ints */
2164 case UTIL_FORMAT_TYPE_FLOAT
:
2165 switch (desc
->channel
[i
].size
) {
2167 switch (desc
->nr_channels
) {
2169 *format
= FMT_16_FLOAT
;
2172 *format
= FMT_16_16_FLOAT
;
2176 *format
= FMT_16_16_16_16_FLOAT
;
2181 switch (desc
->nr_channels
) {
2183 *format
= FMT_32_FLOAT
;
2186 *format
= FMT_32_32_FLOAT
;
2189 *format
= FMT_32_32_32_FLOAT
;
2192 *format
= FMT_32_32_32_32_FLOAT
;
2201 case UTIL_FORMAT_TYPE_UNSIGNED
:
2203 case UTIL_FORMAT_TYPE_SIGNED
:
2204 switch (desc
->channel
[i
].size
) {
2206 switch (desc
->nr_channels
) {
2215 *format
= FMT_8_8_8_8
;
2220 if (desc
->nr_channels
!= 4)
2223 *format
= FMT_2_10_10_10
;
2226 switch (desc
->nr_channels
) {
2231 *format
= FMT_16_16
;
2235 *format
= FMT_16_16_16_16
;
2240 switch (desc
->nr_channels
) {
2245 *format
= FMT_32_32
;
2248 *format
= FMT_32_32_32
;
2251 *format
= FMT_32_32_32_32
;
2263 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
2268 if (desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_UNSIGNED
||
2269 desc
->channel
[i
].type
== UTIL_FORMAT_TYPE_SIGNED
) {
2270 if (!desc
->channel
[i
].normalized
) {
2271 if (desc
->channel
[i
].pure_integer
)
2279 R600_ERR("unsupported vertex format %s\n", util_format_name(pformat
));
2282 void *r600_create_vertex_fetch_shader(struct pipe_context
*ctx
,
2284 const struct pipe_vertex_element
*elements
)
2286 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
2287 struct r600_bytecode bc
;
2288 struct r600_bytecode_vtx vtx
;
2289 const struct util_format_description
*desc
;
2290 unsigned fetch_resource_start
= rctx
->b
.chip_class
>= EVERGREEN
? 0 : 160;
2291 unsigned format
, num_format
, format_comp
, endian
;
2293 int i
, j
, r
, fs_size
;
2294 struct r600_fetch_shader
*shader
;
2295 unsigned no_sb
= rctx
->screen
->b
.debug_flags
& DBG_NO_SB
;
2296 unsigned sb_disasm
= !no_sb
|| (rctx
->screen
->b
.debug_flags
& DBG_SB_DISASM
);
2300 memset(&bc
, 0, sizeof(bc
));
2301 r600_bytecode_init(&bc
, rctx
->b
.chip_class
, rctx
->b
.family
,
2302 rctx
->screen
->has_compressed_msaa_texturing
);
2306 for (i
= 0; i
< count
; i
++) {
2307 if (elements
[i
].instance_divisor
> 1) {
2308 if (rctx
->b
.chip_class
== CAYMAN
) {
2309 for (j
= 0; j
< 4; j
++) {
2310 struct r600_bytecode_alu alu
;
2311 memset(&alu
, 0, sizeof(alu
));
2312 alu
.op
= ALU_OP2_MULHI_UINT
;
2314 alu
.src
[0].chan
= 3;
2315 alu
.src
[1].sel
= V_SQ_ALU_SRC_LITERAL
;
2316 alu
.src
[1].value
= (1ll << 32) / elements
[i
].instance_divisor
+ 1;
2317 alu
.dst
.sel
= i
+ 1;
2319 alu
.dst
.write
= j
== 3;
2321 if ((r
= r600_bytecode_add_alu(&bc
, &alu
))) {
2322 r600_bytecode_clear(&bc
);
2327 struct r600_bytecode_alu alu
;
2328 memset(&alu
, 0, sizeof(alu
));
2329 alu
.op
= ALU_OP2_MULHI_UINT
;
2331 alu
.src
[0].chan
= 3;
2332 alu
.src
[1].sel
= V_SQ_ALU_SRC_LITERAL
;
2333 alu
.src
[1].value
= (1ll << 32) / elements
[i
].instance_divisor
+ 1;
2334 alu
.dst
.sel
= i
+ 1;
2338 if ((r
= r600_bytecode_add_alu(&bc
, &alu
))) {
2339 r600_bytecode_clear(&bc
);
2346 for (i
= 0; i
< count
; i
++) {
2347 r600_vertex_data_type(elements
[i
].src_format
,
2348 &format
, &num_format
, &format_comp
, &endian
);
2350 desc
= util_format_description(elements
[i
].src_format
);
2352 r600_bytecode_clear(&bc
);
2353 R600_ERR("unknown format %d\n", elements
[i
].src_format
);
2357 if (elements
[i
].src_offset
> 65535) {
2358 r600_bytecode_clear(&bc
);
2359 R600_ERR("too big src_offset: %u\n", elements
[i
].src_offset
);
2363 memset(&vtx
, 0, sizeof(vtx
));
2364 vtx
.buffer_id
= elements
[i
].vertex_buffer_index
+ fetch_resource_start
;
2365 vtx
.fetch_type
= elements
[i
].instance_divisor
? 1 : 0;
2366 vtx
.src_gpr
= elements
[i
].instance_divisor
> 1 ? i
+ 1 : 0;
2367 vtx
.src_sel_x
= elements
[i
].instance_divisor
? 3 : 0;
2368 vtx
.mega_fetch_count
= 0x1F;
2369 vtx
.dst_gpr
= i
+ 1;
2370 vtx
.dst_sel_x
= desc
->swizzle
[0];
2371 vtx
.dst_sel_y
= desc
->swizzle
[1];
2372 vtx
.dst_sel_z
= desc
->swizzle
[2];
2373 vtx
.dst_sel_w
= desc
->swizzle
[3];
2374 vtx
.data_format
= format
;
2375 vtx
.num_format_all
= num_format
;
2376 vtx
.format_comp_all
= format_comp
;
2377 vtx
.srf_mode_all
= 1;
2378 vtx
.offset
= elements
[i
].src_offset
;
2379 vtx
.endian
= endian
;
2381 if ((r
= r600_bytecode_add_vtx(&bc
, &vtx
))) {
2382 r600_bytecode_clear(&bc
);
2387 r600_bytecode_add_cfinst(&bc
, CF_OP_RET
);
2389 if ((r
= r600_bytecode_build(&bc
))) {
2390 r600_bytecode_clear(&bc
);
2394 if (rctx
->screen
->b
.debug_flags
& DBG_FS
) {
2395 fprintf(stderr
, "--------------------------------------------------------------\n");
2396 fprintf(stderr
, "Vertex elements state:\n");
2397 for (i
= 0; i
< count
; i
++) {
2398 fprintf(stderr
, " ");
2399 util_dump_vertex_element(stderr
, elements
+i
);
2400 fprintf(stderr
, "\n");
2404 r600_bytecode_disasm(&bc
);
2406 fprintf(stderr
, "______________________________________________________________\n");
2408 r600_sb_bytecode_process(rctx
, &bc
, NULL
, 1 /*dump*/, 0 /*optimize*/);
2414 /* Allocate the CSO. */
2415 shader
= CALLOC_STRUCT(r600_fetch_shader
);
2417 r600_bytecode_clear(&bc
);
2421 u_suballocator_alloc(rctx
->allocator_fetch_shader
, fs_size
, &shader
->offset
,
2422 (struct pipe_resource
**)&shader
->buffer
);
2423 if (!shader
->buffer
) {
2424 r600_bytecode_clear(&bc
);
2429 bytecode
= r600_buffer_map_sync_with_rings(&rctx
->b
, shader
->buffer
, PIPE_TRANSFER_WRITE
| PIPE_TRANSFER_UNSYNCHRONIZED
);
2430 bytecode
+= shader
->offset
/ 4;
2432 if (R600_BIG_ENDIAN
) {
2433 for (i
= 0; i
< fs_size
/ 4; ++i
) {
2434 bytecode
[i
] = util_cpu_to_le32(bc
.bytecode
[i
]);
2437 memcpy(bytecode
, bc
.bytecode
, fs_size
);
2439 rctx
->b
.ws
->buffer_unmap(shader
->buffer
->cs_buf
);
2441 r600_bytecode_clear(&bc
);
2445 void r600_bytecode_alu_read(struct r600_bytecode
*bc
,
2446 struct r600_bytecode_alu
*alu
, uint32_t word0
, uint32_t word1
)
2449 alu
->src
[0].sel
= G_SQ_ALU_WORD0_SRC0_SEL(word0
);
2450 alu
->src
[0].rel
= G_SQ_ALU_WORD0_SRC0_REL(word0
);
2451 alu
->src
[0].chan
= G_SQ_ALU_WORD0_SRC0_CHAN(word0
);
2452 alu
->src
[0].neg
= G_SQ_ALU_WORD0_SRC0_NEG(word0
);
2453 alu
->src
[1].sel
= G_SQ_ALU_WORD0_SRC1_SEL(word0
);
2454 alu
->src
[1].rel
= G_SQ_ALU_WORD0_SRC1_REL(word0
);
2455 alu
->src
[1].chan
= G_SQ_ALU_WORD0_SRC1_CHAN(word0
);
2456 alu
->src
[1].neg
= G_SQ_ALU_WORD0_SRC1_NEG(word0
);
2457 alu
->index_mode
= G_SQ_ALU_WORD0_INDEX_MODE(word0
);
2458 alu
->pred_sel
= G_SQ_ALU_WORD0_PRED_SEL(word0
);
2459 alu
->last
= G_SQ_ALU_WORD0_LAST(word0
);
2462 alu
->bank_swizzle
= G_SQ_ALU_WORD1_BANK_SWIZZLE(word1
);
2463 if (alu
->bank_swizzle
)
2464 alu
->bank_swizzle_force
= alu
->bank_swizzle
;
2465 alu
->dst
.sel
= G_SQ_ALU_WORD1_DST_GPR(word1
);
2466 alu
->dst
.rel
= G_SQ_ALU_WORD1_DST_REL(word1
);
2467 alu
->dst
.chan
= G_SQ_ALU_WORD1_DST_CHAN(word1
);
2468 alu
->dst
.clamp
= G_SQ_ALU_WORD1_CLAMP(word1
);
2469 if (G_SQ_ALU_WORD1_ENCODING(word1
)) /*ALU_DWORD1_OP3*/
2472 alu
->src
[2].sel
= G_SQ_ALU_WORD1_OP3_SRC2_SEL(word1
);
2473 alu
->src
[2].rel
= G_SQ_ALU_WORD1_OP3_SRC2_REL(word1
);
2474 alu
->src
[2].chan
= G_SQ_ALU_WORD1_OP3_SRC2_CHAN(word1
);
2475 alu
->src
[2].neg
= G_SQ_ALU_WORD1_OP3_SRC2_NEG(word1
);
2476 alu
->op
= r600_isa_alu_by_opcode(bc
->isa
,
2477 G_SQ_ALU_WORD1_OP3_ALU_INST(word1
), /* is_op3 = */ 1);
2480 else /*ALU_DWORD1_OP2*/
2482 alu
->src
[0].abs
= G_SQ_ALU_WORD1_OP2_SRC0_ABS(word1
);
2483 alu
->src
[1].abs
= G_SQ_ALU_WORD1_OP2_SRC1_ABS(word1
);
2484 alu
->op
= r600_isa_alu_by_opcode(bc
->isa
,
2485 G_SQ_ALU_WORD1_OP2_ALU_INST(word1
), /* is_op3 = */ 0);
2486 alu
->omod
= G_SQ_ALU_WORD1_OP2_OMOD(word1
);
2487 alu
->dst
.write
= G_SQ_ALU_WORD1_OP2_WRITE_MASK(word1
);
2488 alu
->update_pred
= G_SQ_ALU_WORD1_OP2_UPDATE_PRED(word1
);
2490 G_SQ_ALU_WORD1_OP2_UPDATE_EXECUTE_MASK(word1
);
2495 void r600_bytecode_export_read(struct r600_bytecode
*bc
,
2496 struct r600_bytecode_output
*output
, uint32_t word0
, uint32_t word1
)
2498 output
->array_base
= G_SQ_CF_ALLOC_EXPORT_WORD0_ARRAY_BASE(word0
);
2499 output
->type
= G_SQ_CF_ALLOC_EXPORT_WORD0_TYPE(word0
);
2500 output
->gpr
= G_SQ_CF_ALLOC_EXPORT_WORD0_RW_GPR(word0
);
2501 output
->elem_size
= G_SQ_CF_ALLOC_EXPORT_WORD0_ELEM_SIZE(word0
);
2503 output
->swizzle_x
= G_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_X(word1
);
2504 output
->swizzle_y
= G_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Y(word1
);
2505 output
->swizzle_z
= G_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_Z(word1
);
2506 output
->swizzle_w
= G_SQ_CF_ALLOC_EXPORT_WORD1_SWIZ_SEL_W(word1
);
2507 output
->burst_count
= G_SQ_CF_ALLOC_EXPORT_WORD1_BURST_COUNT(word1
);
2508 output
->end_of_program
= G_SQ_CF_ALLOC_EXPORT_WORD1_END_OF_PROGRAM(word1
);
2509 output
->op
= r600_isa_cf_by_opcode(bc
->isa
,
2510 G_SQ_CF_ALLOC_EXPORT_WORD1_CF_INST(word1
), 0);
2511 output
->barrier
= G_SQ_CF_ALLOC_EXPORT_WORD1_BARRIER(word1
);
2512 output
->array_size
= G_SQ_CF_ALLOC_EXPORT_WORD1_BUF_ARRAY_SIZE(word1
);
2513 output
->comp_mask
= G_SQ_CF_ALLOC_EXPORT_WORD1_BUF_COMP_MASK(word1
);