freedreno/ir3: drop unused create_input() arg
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_compiler_nir.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2015 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include <stdarg.h>
30
31 #include "pipe/p_state.h"
32 #include "util/u_string.h"
33 #include "util/u_memory.h"
34 #include "util/u_inlines.h"
35 #include "tgsi/tgsi_lowering.h"
36 #include "tgsi/tgsi_strings.h"
37
38 #include "nir/tgsi_to_nir.h"
39 #include "glsl/shader_enums.h"
40
41 #include "freedreno_util.h"
42
43 #include "ir3_compiler.h"
44 #include "ir3_shader.h"
45 #include "ir3_nir.h"
46
47 #include "instr-a3xx.h"
48 #include "ir3.h"
49
50
51 struct ir3_compile {
52 struct ir3_compiler *compiler;
53
54 const struct tgsi_token *tokens;
55 struct nir_shader *s;
56
57 struct ir3 *ir;
58 struct ir3_shader_variant *so;
59
60 struct ir3_block *block; /* the current block */
61 struct ir3_block *in_block; /* block created for shader inputs */
62
63 nir_function_impl *impl;
64
65 /* For fragment shaders, from the hw perspective the only
66 * actual input is r0.xy position register passed to bary.f.
67 * But TGSI doesn't know that, it still declares things as
68 * IN[] registers. So we do all the input tracking normally
69 * and fix things up after compile_instructions()
70 *
71 * NOTE that frag_pos is the hardware position (possibly it
72 * is actually an index or tag or some such.. it is *not*
73 * values that can be directly used for gl_FragCoord..)
74 */
75 struct ir3_instruction *frag_pos, *frag_face, *frag_coord[4];
76
77 /* For vertex shaders, keep track of the system values sources */
78 struct ir3_instruction *vertex_id, *basevertex, *instance_id;
79
80 /* mapping from nir_register to defining instruction: */
81 struct hash_table *def_ht;
82
83 /* mapping from nir_variable to ir3_array: */
84 struct hash_table *var_ht;
85 unsigned num_arrays;
86
87 /* a common pattern for indirect addressing is to request the
88 * same address register multiple times. To avoid generating
89 * duplicate instruction sequences (which our backend does not
90 * try to clean up, since that should be done as the NIR stage)
91 * we cache the address value generated for a given src value:
92 */
93 struct hash_table *addr_ht;
94
95 /* maps nir_block to ir3_block, mostly for the purposes of
96 * figuring out the blocks successors
97 */
98 struct hash_table *block_ht;
99
100 /* for calculating input/output positions/linkages: */
101 unsigned next_inloc;
102
103 /* a4xx (at least patchlevel 0) cannot seem to flat-interpolate
104 * so we need to use ldlv.u32 to load the varying directly:
105 */
106 bool flat_bypass;
107
108 /* on a3xx, we need to add one to # of array levels:
109 */
110 bool levels_add_one;
111
112 /* on a3xx, we need to scale up integer coords for isaml based
113 * on LoD:
114 */
115 bool unminify_coords;
116
117 /* for looking up which system value is which */
118 unsigned sysval_semantics[8];
119
120 /* list of kill instructions: */
121 struct ir3_instruction *kill[16];
122 unsigned int kill_count;
123
124 /* set if we encounter something we can't handle yet, so we
125 * can bail cleanly and fallback to TGSI compiler f/e
126 */
127 bool error;
128 };
129
130
131 static struct ir3_instruction * create_immed(struct ir3_block *block, uint32_t val);
132 static struct ir3_block * get_block(struct ir3_compile *ctx, nir_block *nblock);
133
134 static struct nir_shader *to_nir(const struct tgsi_token *tokens)
135 {
136 struct nir_shader_compiler_options options = {
137 .lower_fpow = true,
138 .lower_fsat = true,
139 .lower_scmp = true,
140 .lower_flrp = true,
141 .native_integers = true,
142 };
143 bool progress;
144
145 struct nir_shader *s = tgsi_to_nir(tokens, &options);
146
147 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
148 debug_printf("----------------------\n");
149 nir_print_shader(s, stdout);
150 debug_printf("----------------------\n");
151 }
152
153 nir_opt_global_to_local(s);
154 nir_convert_to_ssa(s);
155 nir_lower_idiv(s);
156
157 do {
158 progress = false;
159
160 nir_lower_vars_to_ssa(s);
161 nir_lower_alu_to_scalar(s);
162 nir_lower_phis_to_scalar(s);
163
164 progress |= nir_copy_prop(s);
165 progress |= nir_opt_dce(s);
166 progress |= nir_opt_cse(s);
167 progress |= ir3_nir_lower_if_else(s);
168 progress |= nir_opt_algebraic(s);
169 progress |= nir_opt_constant_folding(s);
170
171 } while (progress);
172
173 nir_remove_dead_variables(s);
174 nir_validate_shader(s);
175
176 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
177 debug_printf("----------------------\n");
178 nir_print_shader(s, stdout);
179 debug_printf("----------------------\n");
180 }
181
182 return s;
183 }
184
185 /* TODO nir doesn't lower everything for us yet, but ideally it would: */
186 static const struct tgsi_token *
187 lower_tgsi(struct ir3_compile *ctx, const struct tgsi_token *tokens,
188 struct ir3_shader_variant *so)
189 {
190 struct tgsi_shader_info info;
191 struct tgsi_lowering_config lconfig = {
192 .color_two_side = so->key.color_two_side,
193 .lower_FRC = true,
194 };
195
196 switch (so->type) {
197 case SHADER_FRAGMENT:
198 case SHADER_COMPUTE:
199 lconfig.saturate_s = so->key.fsaturate_s;
200 lconfig.saturate_t = so->key.fsaturate_t;
201 lconfig.saturate_r = so->key.fsaturate_r;
202 break;
203 case SHADER_VERTEX:
204 lconfig.saturate_s = so->key.vsaturate_s;
205 lconfig.saturate_t = so->key.vsaturate_t;
206 lconfig.saturate_r = so->key.vsaturate_r;
207 break;
208 }
209
210 if (ctx->compiler->gpu_id >= 400) {
211 /* a4xx seems to have *no* sam.p */
212 lconfig.lower_TXP = ~0; /* lower all txp */
213 } else {
214 /* a3xx just needs to avoid sam.p for 3d tex */
215 lconfig.lower_TXP = (1 << TGSI_TEXTURE_3D);
216 }
217
218 return tgsi_transform_lowering(&lconfig, tokens, &info);
219 }
220
221 static struct ir3_compile *
222 compile_init(struct ir3_compiler *compiler,
223 struct ir3_shader_variant *so,
224 const struct tgsi_token *tokens)
225 {
226 struct ir3_compile *ctx = rzalloc(NULL, struct ir3_compile);
227 const struct tgsi_token *lowered_tokens;
228
229 if (compiler->gpu_id >= 400) {
230 /* need special handling for "flat" */
231 ctx->flat_bypass = true;
232 ctx->levels_add_one = false;
233 ctx->unminify_coords = false;
234 } else {
235 /* no special handling for "flat" */
236 ctx->flat_bypass = false;
237 ctx->levels_add_one = true;
238 ctx->unminify_coords = true;
239 }
240
241 ctx->compiler = compiler;
242 ctx->ir = so->ir;
243 ctx->so = so;
244 ctx->next_inloc = 8;
245 ctx->def_ht = _mesa_hash_table_create(ctx,
246 _mesa_hash_pointer, _mesa_key_pointer_equal);
247 ctx->var_ht = _mesa_hash_table_create(ctx,
248 _mesa_hash_pointer, _mesa_key_pointer_equal);
249 ctx->addr_ht = _mesa_hash_table_create(ctx,
250 _mesa_hash_pointer, _mesa_key_pointer_equal);
251 ctx->block_ht = _mesa_hash_table_create(ctx,
252 _mesa_hash_pointer, _mesa_key_pointer_equal);
253
254 lowered_tokens = lower_tgsi(ctx, tokens, so);
255 if (!lowered_tokens)
256 lowered_tokens = tokens;
257 ctx->s = to_nir(lowered_tokens);
258
259 if (lowered_tokens != tokens)
260 free((void *)lowered_tokens);
261
262 so->first_driver_param = so->first_immediate = ctx->s->num_uniforms;
263
264 /* one (vec4) slot for vertex id base: */
265 if (so->type == SHADER_VERTEX)
266 so->first_immediate++;
267
268 /* reserve 4 (vec4) slots for ubo base addresses: */
269 so->first_immediate += 4;
270
271 return ctx;
272 }
273
274 static void
275 compile_error(struct ir3_compile *ctx, const char *format, ...)
276 {
277 va_list ap;
278 va_start(ap, format);
279 _debug_vprintf(format, ap);
280 va_end(ap);
281 nir_print_shader(ctx->s, stdout);
282 ctx->error = true;
283 debug_assert(0);
284 }
285
286 #define compile_assert(ctx, cond) do { \
287 if (!(cond)) compile_error((ctx), "failed assert: "#cond"\n"); \
288 } while (0)
289
290 static void
291 compile_free(struct ir3_compile *ctx)
292 {
293 ralloc_free(ctx);
294 }
295
296 /* global per-array information: */
297 struct ir3_array {
298 unsigned length, aid;
299 };
300
301 /* per-block array state: */
302 struct ir3_array_value {
303 /* TODO drop length/aid, and just have ptr back to ir3_array */
304 unsigned length, aid;
305 /* initial array element values are phi's, other than for the
306 * entry block. The phi src's get added later in a resolve step
307 * after we have visited all the blocks, to account for back
308 * edges in the cfg.
309 */
310 struct ir3_instruction **phis;
311 /* current array element values (as block is processed). When
312 * the array phi's are resolved, it will contain the array state
313 * at exit of block, so successor blocks can use it to add their
314 * phi srcs.
315 */
316 struct ir3_instruction *arr[];
317 };
318
319 /* track array assignments per basic block. When an array is read
320 * outside of the same basic block, we can use NIR's dominance-frontier
321 * information to figure out where phi nodes are needed.
322 */
323 struct ir3_nir_block_data {
324 unsigned foo;
325 /* indexed by array-id (aid): */
326 struct ir3_array_value *arrs[];
327 };
328
329 static struct ir3_nir_block_data *
330 get_block_data(struct ir3_compile *ctx, struct ir3_block *block)
331 {
332 if (!block->bd) {
333 struct ir3_nir_block_data *bd = ralloc_size(ctx, sizeof(*bd) +
334 ((ctx->num_arrays + 1) * sizeof(bd->arrs[0])));
335 block->bd = bd;
336 }
337 return block->bd;
338 }
339
340 static void
341 declare_var(struct ir3_compile *ctx, nir_variable *var)
342 {
343 unsigned length = glsl_get_length(var->type) * 4; /* always vec4, at least with ttn */
344 struct ir3_array *arr = ralloc(ctx, struct ir3_array);
345 arr->length = length;
346 arr->aid = ++ctx->num_arrays;
347 _mesa_hash_table_insert(ctx->var_ht, var, arr);
348 }
349
350 static nir_block *
351 nir_block_pred(nir_block *block)
352 {
353 assert(block->predecessors->entries < 2);
354 if (block->predecessors->entries == 0)
355 return NULL;
356 return (nir_block *)_mesa_set_next_entry(block->predecessors, NULL)->key;
357 }
358
359 static struct ir3_array_value *
360 get_var(struct ir3_compile *ctx, nir_variable *var)
361 {
362 struct hash_entry *entry = _mesa_hash_table_search(ctx->var_ht, var);
363 struct ir3_block *block = ctx->block;
364 struct ir3_nir_block_data *bd = get_block_data(ctx, block);
365 struct ir3_array *arr = entry->data;
366
367 if (!bd->arrs[arr->aid]) {
368 struct ir3_array_value *av = ralloc_size(bd, sizeof(*av) +
369 (arr->length * sizeof(av->arr[0])));
370 struct ir3_array_value *defn = NULL;
371 nir_block *pred_block;
372
373 av->length = arr->length;
374 av->aid = arr->aid;
375
376 /* For loops, we have to consider that we have not visited some
377 * of the blocks who should feed into the phi (ie. back-edges in
378 * the cfg).. for example:
379 *
380 * loop {
381 * block { load_var; ... }
382 * if then block {} else block {}
383 * block { store_var; ... }
384 * if then block {} else block {}
385 * block {...}
386 * }
387 *
388 * We can skip the phi if we can chase the block predecessors
389 * until finding the block previously defining the array without
390 * crossing a block that has more than one predecessor.
391 *
392 * Otherwise create phi's and resolve them as a post-pass after
393 * all the blocks have been visited (to handle back-edges).
394 */
395
396 for (pred_block = block->nblock;
397 pred_block && (pred_block->predecessors->entries < 2) && !defn;
398 pred_block = nir_block_pred(pred_block)) {
399 struct ir3_block *pblock = get_block(ctx, pred_block);
400 struct ir3_nir_block_data *pbd = pblock->bd;
401 if (!pbd)
402 continue;
403 defn = pbd->arrs[arr->aid];
404 }
405
406 if (defn) {
407 /* only one possible definer: */
408 for (unsigned i = 0; i < arr->length; i++)
409 av->arr[i] = defn->arr[i];
410 } else if (pred_block) {
411 /* not the first block, and multiple potential definers: */
412 av->phis = ralloc_size(av, arr->length * sizeof(av->phis[0]));
413
414 for (unsigned i = 0; i < arr->length; i++) {
415 struct ir3_instruction *phi;
416
417 phi = ir3_instr_create2(block, -1, OPC_META_PHI,
418 1 + ctx->impl->num_blocks);
419 ir3_reg_create(phi, 0, 0); /* dst */
420
421 /* phi's should go at head of block: */
422 list_delinit(&phi->node);
423 list_add(&phi->node, &block->instr_list);
424
425 av->phis[i] = av->arr[i] = phi;
426 }
427 } else {
428 /* Some shaders end up reading array elements without
429 * first writing.. so initialize things to prevent null
430 * instr ptrs later:
431 */
432 for (unsigned i = 0; i < arr->length; i++)
433 av->arr[i] = create_immed(block, 0);
434 }
435
436 bd->arrs[arr->aid] = av;
437 }
438
439 return bd->arrs[arr->aid];
440 }
441
442 static void
443 add_array_phi_srcs(struct ir3_compile *ctx, nir_block *nblock,
444 struct ir3_array_value *av, BITSET_WORD *visited)
445 {
446 struct ir3_block *block;
447 struct ir3_nir_block_data *bd;
448
449 if (BITSET_TEST(visited, nblock->index))
450 return;
451
452 BITSET_SET(visited, nblock->index);
453
454 block = get_block(ctx, nblock);
455 bd = block->bd;
456
457 if (bd && bd->arrs[av->aid]) {
458 struct ir3_array_value *dav = bd->arrs[av->aid];
459 for (unsigned i = 0; i < av->length; i++) {
460 ir3_reg_create(av->phis[i], 0, IR3_REG_SSA)->instr =
461 dav->arr[i];
462 }
463 } else {
464 /* didn't find defn, recurse predecessors: */
465 struct set_entry *entry;
466 set_foreach(nblock->predecessors, entry) {
467 add_array_phi_srcs(ctx, (nir_block *)entry->key, av, visited);
468 }
469 }
470 }
471
472 static void
473 resolve_array_phis(struct ir3_compile *ctx, struct ir3_block *block)
474 {
475 struct ir3_nir_block_data *bd = block->bd;
476 unsigned bitset_words = BITSET_WORDS(ctx->impl->num_blocks);
477
478 if (!bd)
479 return;
480
481 /* TODO use nir dom_frontier to help us with this? */
482
483 for (unsigned i = 1; i <= ctx->num_arrays; i++) {
484 struct ir3_array_value *av = bd->arrs[i];
485 BITSET_WORD visited[bitset_words];
486 struct set_entry *entry;
487
488 if (!(av && av->phis))
489 continue;
490
491 memset(visited, 0, sizeof(visited));
492 set_foreach(block->nblock->predecessors, entry) {
493 add_array_phi_srcs(ctx, (nir_block *)entry->key, av, visited);
494 }
495 }
496 }
497
498 /* allocate a n element value array (to be populated by caller) and
499 * insert in def_ht
500 */
501 static struct ir3_instruction **
502 __get_dst(struct ir3_compile *ctx, void *key, unsigned n)
503 {
504 struct ir3_instruction **value =
505 ralloc_array(ctx->def_ht, struct ir3_instruction *, n);
506 _mesa_hash_table_insert(ctx->def_ht, key, value);
507 return value;
508 }
509
510 static struct ir3_instruction **
511 get_dst(struct ir3_compile *ctx, nir_dest *dst, unsigned n)
512 {
513 if (dst->is_ssa) {
514 return __get_dst(ctx, &dst->ssa, n);
515 } else {
516 return __get_dst(ctx, dst->reg.reg, n);
517 }
518 }
519
520 static struct ir3_instruction **
521 get_dst_ssa(struct ir3_compile *ctx, nir_ssa_def *dst, unsigned n)
522 {
523 return __get_dst(ctx, dst, n);
524 }
525
526 static struct ir3_instruction **
527 get_src(struct ir3_compile *ctx, nir_src *src)
528 {
529 struct hash_entry *entry;
530 if (src->is_ssa) {
531 entry = _mesa_hash_table_search(ctx->def_ht, src->ssa);
532 } else {
533 entry = _mesa_hash_table_search(ctx->def_ht, src->reg.reg);
534 }
535 compile_assert(ctx, entry);
536 return entry->data;
537 }
538
539 static struct ir3_instruction *
540 create_immed(struct ir3_block *block, uint32_t val)
541 {
542 struct ir3_instruction *mov;
543
544 mov = ir3_instr_create(block, 1, 0);
545 mov->cat1.src_type = TYPE_U32;
546 mov->cat1.dst_type = TYPE_U32;
547 ir3_reg_create(mov, 0, 0);
548 ir3_reg_create(mov, 0, IR3_REG_IMMED)->uim_val = val;
549
550 return mov;
551 }
552
553 static struct ir3_instruction *
554 create_addr(struct ir3_block *block, struct ir3_instruction *src)
555 {
556 struct ir3_instruction *instr, *immed;
557
558 /* TODO in at least some cases, the backend could probably be
559 * made clever enough to propagate IR3_REG_HALF..
560 */
561 instr = ir3_COV(block, src, TYPE_U32, TYPE_S16);
562 instr->regs[0]->flags |= IR3_REG_HALF;
563
564 immed = create_immed(block, 2);
565 immed->regs[0]->flags |= IR3_REG_HALF;
566
567 instr = ir3_SHL_B(block, instr, 0, immed, 0);
568 instr->regs[0]->flags |= IR3_REG_HALF;
569 instr->regs[1]->flags |= IR3_REG_HALF;
570
571 instr = ir3_MOV(block, instr, TYPE_S16);
572 instr->regs[0]->num = regid(REG_A0, 0);
573 instr->regs[0]->flags |= IR3_REG_HALF;
574 instr->regs[1]->flags |= IR3_REG_HALF;
575
576 return instr;
577 }
578
579 /* caches addr values to avoid generating multiple cov/shl/mova
580 * sequences for each use of a given NIR level src as address
581 */
582 static struct ir3_instruction *
583 get_addr(struct ir3_compile *ctx, struct ir3_instruction *src)
584 {
585 struct ir3_instruction *addr;
586 struct hash_entry *entry;
587 entry = _mesa_hash_table_search(ctx->addr_ht, src);
588 if (entry)
589 return entry->data;
590
591 /* TODO do we need to cache per block? */
592 addr = create_addr(ctx->block, src);
593 _mesa_hash_table_insert(ctx->addr_ht, src, addr);
594
595 return addr;
596 }
597
598 static struct ir3_instruction *
599 get_predicate(struct ir3_compile *ctx, struct ir3_instruction *src)
600 {
601 struct ir3_block *b = ctx->block;
602 struct ir3_instruction *cond;
603
604 /* NOTE: only cmps.*.* can write p0.x: */
605 cond = ir3_CMPS_S(b, src, 0, create_immed(b, 0), 0);
606 cond->cat2.condition = IR3_COND_NE;
607
608 /* condition always goes in predicate register: */
609 cond->regs[0]->num = regid(REG_P0, 0);
610
611 return cond;
612 }
613
614 static struct ir3_instruction *
615 create_uniform(struct ir3_compile *ctx, unsigned n)
616 {
617 struct ir3_instruction *mov;
618
619 mov = ir3_instr_create(ctx->block, 1, 0);
620 /* TODO get types right? */
621 mov->cat1.src_type = TYPE_F32;
622 mov->cat1.dst_type = TYPE_F32;
623 ir3_reg_create(mov, 0, 0);
624 ir3_reg_create(mov, n, IR3_REG_CONST);
625
626 return mov;
627 }
628
629 static struct ir3_instruction *
630 create_uniform_indirect(struct ir3_compile *ctx, unsigned n,
631 struct ir3_instruction *address)
632 {
633 struct ir3_instruction *mov;
634
635 mov = ir3_instr_create(ctx->block, 1, 0);
636 mov->cat1.src_type = TYPE_U32;
637 mov->cat1.dst_type = TYPE_U32;
638 ir3_reg_create(mov, 0, 0);
639 ir3_reg_create(mov, n, IR3_REG_CONST | IR3_REG_RELATIV);
640
641 ir3_instr_set_address(mov, address);
642
643 return mov;
644 }
645
646 static struct ir3_instruction *
647 create_collect(struct ir3_block *block, struct ir3_instruction **arr,
648 unsigned arrsz)
649 {
650 struct ir3_instruction *collect;
651
652 if (arrsz == 0)
653 return NULL;
654
655 collect = ir3_instr_create2(block, -1, OPC_META_FI, 1 + arrsz);
656 ir3_reg_create(collect, 0, 0); /* dst */
657 for (unsigned i = 0; i < arrsz; i++)
658 ir3_reg_create(collect, 0, IR3_REG_SSA)->instr = arr[i];
659
660 return collect;
661 }
662
663 static struct ir3_instruction *
664 create_indirect_load(struct ir3_compile *ctx, unsigned arrsz, unsigned n,
665 struct ir3_instruction *address, struct ir3_instruction *collect)
666 {
667 struct ir3_block *block = ctx->block;
668 struct ir3_instruction *mov;
669 struct ir3_register *src;
670
671 mov = ir3_instr_create(block, 1, 0);
672 mov->cat1.src_type = TYPE_U32;
673 mov->cat1.dst_type = TYPE_U32;
674 ir3_reg_create(mov, 0, 0);
675 src = ir3_reg_create(mov, 0, IR3_REG_SSA | IR3_REG_RELATIV);
676 src->instr = collect;
677 src->size = arrsz;
678 src->offset = n;
679
680 ir3_instr_set_address(mov, address);
681
682 return mov;
683 }
684
685 static struct ir3_instruction *
686 create_indirect_store(struct ir3_compile *ctx, unsigned arrsz, unsigned n,
687 struct ir3_instruction *src, struct ir3_instruction *address,
688 struct ir3_instruction *collect)
689 {
690 struct ir3_block *block = ctx->block;
691 struct ir3_instruction *mov;
692 struct ir3_register *dst;
693
694 mov = ir3_instr_create(block, 1, 0);
695 mov->cat1.src_type = TYPE_U32;
696 mov->cat1.dst_type = TYPE_U32;
697 dst = ir3_reg_create(mov, 0, IR3_REG_RELATIV);
698 dst->size = arrsz;
699 dst->offset = n;
700 ir3_reg_create(mov, 0, IR3_REG_SSA)->instr = src;
701 mov->fanin = collect;
702
703 ir3_instr_set_address(mov, address);
704
705 return mov;
706 }
707
708 static struct ir3_instruction *
709 create_input(struct ir3_block *block, unsigned n)
710 {
711 struct ir3_instruction *in;
712
713 in = ir3_instr_create(block, -1, OPC_META_INPUT);
714 in->inout.block = block;
715 ir3_reg_create(in, n, 0);
716
717 return in;
718 }
719
720 static struct ir3_instruction *
721 create_frag_input(struct ir3_compile *ctx, unsigned n, bool use_ldlv)
722 {
723 struct ir3_block *block = ctx->block;
724 struct ir3_instruction *instr;
725 struct ir3_instruction *inloc = create_immed(block, n);
726
727 if (use_ldlv) {
728 instr = ir3_LDLV(block, inloc, 0, create_immed(block, 1), 0);
729 instr->cat6.type = TYPE_U32;
730 instr->cat6.iim_val = 1;
731 } else {
732 instr = ir3_BARY_F(block, inloc, 0, ctx->frag_pos, 0);
733 instr->regs[2]->wrmask = 0x3;
734 }
735
736 return instr;
737 }
738
739 static struct ir3_instruction *
740 create_frag_coord(struct ir3_compile *ctx, unsigned comp)
741 {
742 struct ir3_block *block = ctx->block;
743 struct ir3_instruction *instr;
744
745 compile_assert(ctx, !ctx->frag_coord[comp]);
746
747 ctx->frag_coord[comp] = create_input(ctx->block, 0);
748
749 switch (comp) {
750 case 0: /* .x */
751 case 1: /* .y */
752 /* for frag_coord, we get unsigned values.. we need
753 * to subtract (integer) 8 and divide by 16 (right-
754 * shift by 4) then convert to float:
755 *
756 * sub.s tmp, src, 8
757 * shr.b tmp, tmp, 4
758 * mov.u32f32 dst, tmp
759 *
760 */
761 instr = ir3_SUB_S(block, ctx->frag_coord[comp], 0,
762 create_immed(block, 8), 0);
763 instr = ir3_SHR_B(block, instr, 0,
764 create_immed(block, 4), 0);
765 instr = ir3_COV(block, instr, TYPE_U32, TYPE_F32);
766
767 return instr;
768 case 2: /* .z */
769 case 3: /* .w */
770 default:
771 /* seems that we can use these as-is: */
772 return ctx->frag_coord[comp];
773 }
774 }
775
776 static struct ir3_instruction *
777 create_frag_face(struct ir3_compile *ctx, unsigned comp)
778 {
779 struct ir3_block *block = ctx->block;
780 struct ir3_instruction *instr;
781
782 switch (comp) {
783 case 0: /* .x */
784 compile_assert(ctx, !ctx->frag_face);
785
786 ctx->frag_face = create_input(block, 0);
787 ctx->frag_face->regs[0]->flags |= IR3_REG_HALF;
788
789 /* for faceness, we always get -1 or 0 (int).. but TGSI expects
790 * positive vs negative float.. and piglit further seems to
791 * expect -1.0 or 1.0:
792 *
793 * mul.s tmp, hr0.x, 2
794 * add.s tmp, tmp, 1
795 * mov.s32f32, dst, tmp
796 *
797 */
798 instr = ir3_MUL_S(block, ctx->frag_face, 0,
799 create_immed(block, 2), 0);
800 instr = ir3_ADD_S(block, instr, 0,
801 create_immed(block, 1), 0);
802 instr = ir3_COV(block, instr, TYPE_S32, TYPE_F32);
803
804 return instr;
805 case 1: /* .y */
806 case 2: /* .z */
807 return create_immed(block, fui(0.0));
808 default:
809 case 3: /* .w */
810 return create_immed(block, fui(1.0));
811 }
812 }
813
814 /* helper for instructions that produce multiple consecutive scalar
815 * outputs which need to have a split/fanout meta instruction inserted
816 */
817 static void
818 split_dest(struct ir3_block *block, struct ir3_instruction **dst,
819 struct ir3_instruction *src, unsigned n)
820 {
821 struct ir3_instruction *prev = NULL;
822 for (int i = 0, j = 0; i < n; i++) {
823 struct ir3_instruction *split =
824 ir3_instr_create(block, -1, OPC_META_FO);
825 ir3_reg_create(split, 0, IR3_REG_SSA);
826 ir3_reg_create(split, 0, IR3_REG_SSA)->instr = src;
827 split->fo.off = i;
828
829 if (prev) {
830 split->cp.left = prev;
831 split->cp.left_cnt++;
832 prev->cp.right = split;
833 prev->cp.right_cnt++;
834 }
835 prev = split;
836
837 if (src->regs[0]->wrmask & (1 << i))
838 dst[j++] = split;
839 }
840 }
841
842 /*
843 * Adreno uses uint rather than having dedicated bool type,
844 * which (potentially) requires some conversion, in particular
845 * when using output of an bool instr to int input, or visa
846 * versa.
847 *
848 * | Adreno | NIR |
849 * -------+---------+-------+-
850 * true | 1 | ~0 |
851 * false | 0 | 0 |
852 *
853 * To convert from an adreno bool (uint) to nir, use:
854 *
855 * absneg.s dst, (neg)src
856 *
857 * To convert back in the other direction:
858 *
859 * absneg.s dst, (abs)arc
860 *
861 * The CP step can clean up the absneg.s that cancel each other
862 * out, and with a slight bit of extra cleverness (to recognize
863 * the instructions which produce either a 0 or 1) can eliminate
864 * the absneg.s's completely when an instruction that wants
865 * 0/1 consumes the result. For example, when a nir 'bcsel'
866 * consumes the result of 'feq'. So we should be able to get by
867 * without a boolean resolve step, and without incuring any
868 * extra penalty in instruction count.
869 */
870
871 /* NIR bool -> native (adreno): */
872 static struct ir3_instruction *
873 ir3_b2n(struct ir3_block *block, struct ir3_instruction *instr)
874 {
875 return ir3_ABSNEG_S(block, instr, IR3_REG_SABS);
876 }
877
878 /* native (adreno) -> NIR bool: */
879 static struct ir3_instruction *
880 ir3_n2b(struct ir3_block *block, struct ir3_instruction *instr)
881 {
882 return ir3_ABSNEG_S(block, instr, IR3_REG_SNEG);
883 }
884
885 /*
886 * alu/sfu instructions:
887 */
888
889 static void
890 emit_alu(struct ir3_compile *ctx, nir_alu_instr *alu)
891 {
892 const nir_op_info *info = &nir_op_infos[alu->op];
893 struct ir3_instruction **dst, *src[info->num_inputs];
894 struct ir3_block *b = ctx->block;
895
896 dst = get_dst(ctx, &alu->dest.dest, MAX2(info->output_size, 1));
897
898 /* Vectors are special in that they have non-scalarized writemasks,
899 * and just take the first swizzle channel for each argument in
900 * order into each writemask channel.
901 */
902 if ((alu->op == nir_op_vec2) ||
903 (alu->op == nir_op_vec3) ||
904 (alu->op == nir_op_vec4)) {
905
906 for (int i = 0; i < info->num_inputs; i++) {
907 nir_alu_src *asrc = &alu->src[i];
908
909 compile_assert(ctx, !asrc->abs);
910 compile_assert(ctx, !asrc->negate);
911
912 src[i] = get_src(ctx, &asrc->src)[asrc->swizzle[0]];
913 if (!src[i])
914 src[i] = create_immed(ctx->block, 0);
915 dst[i] = ir3_MOV(b, src[i], TYPE_U32);
916 }
917
918 return;
919 }
920
921 /* General case: We can just grab the one used channel per src. */
922 for (int i = 0; i < info->num_inputs; i++) {
923 unsigned chan = ffs(alu->dest.write_mask) - 1;
924 nir_alu_src *asrc = &alu->src[i];
925
926 compile_assert(ctx, !asrc->abs);
927 compile_assert(ctx, !asrc->negate);
928
929 src[i] = get_src(ctx, &asrc->src)[asrc->swizzle[chan]];
930
931 compile_assert(ctx, src[i]);
932 }
933
934 switch (alu->op) {
935 case nir_op_f2i:
936 dst[0] = ir3_COV(b, src[0], TYPE_F32, TYPE_S32);
937 break;
938 case nir_op_f2u:
939 dst[0] = ir3_COV(b, src[0], TYPE_F32, TYPE_U32);
940 break;
941 case nir_op_i2f:
942 dst[0] = ir3_COV(b, src[0], TYPE_S32, TYPE_F32);
943 break;
944 case nir_op_u2f:
945 dst[0] = ir3_COV(b, src[0], TYPE_U32, TYPE_F32);
946 break;
947 case nir_op_imov:
948 dst[0] = ir3_MOV(b, src[0], TYPE_S32);
949 break;
950 case nir_op_fmov:
951 dst[0] = ir3_MOV(b, src[0], TYPE_F32);
952 break;
953 case nir_op_f2b:
954 dst[0] = ir3_CMPS_F(b, src[0], 0, create_immed(b, fui(0.0)), 0);
955 dst[0]->cat2.condition = IR3_COND_NE;
956 dst[0] = ir3_n2b(b, dst[0]);
957 break;
958 case nir_op_b2f:
959 dst[0] = ir3_COV(b, ir3_b2n(b, src[0]), TYPE_U32, TYPE_F32);
960 break;
961 case nir_op_b2i:
962 dst[0] = ir3_b2n(b, src[0]);
963 break;
964 case nir_op_i2b:
965 dst[0] = ir3_CMPS_S(b, src[0], 0, create_immed(b, 0), 0);
966 dst[0]->cat2.condition = IR3_COND_NE;
967 dst[0] = ir3_n2b(b, dst[0]);
968 break;
969
970 case nir_op_fneg:
971 dst[0] = ir3_ABSNEG_F(b, src[0], IR3_REG_FNEG);
972 break;
973 case nir_op_fabs:
974 dst[0] = ir3_ABSNEG_F(b, src[0], IR3_REG_FABS);
975 break;
976 case nir_op_fmax:
977 dst[0] = ir3_MAX_F(b, src[0], 0, src[1], 0);
978 break;
979 case nir_op_fmin:
980 dst[0] = ir3_MIN_F(b, src[0], 0, src[1], 0);
981 break;
982 case nir_op_fmul:
983 dst[0] = ir3_MUL_F(b, src[0], 0, src[1], 0);
984 break;
985 case nir_op_fadd:
986 dst[0] = ir3_ADD_F(b, src[0], 0, src[1], 0);
987 break;
988 case nir_op_fsub:
989 dst[0] = ir3_ADD_F(b, src[0], 0, src[1], IR3_REG_FNEG);
990 break;
991 case nir_op_ffma:
992 dst[0] = ir3_MAD_F32(b, src[0], 0, src[1], 0, src[2], 0);
993 break;
994 case nir_op_fddx:
995 dst[0] = ir3_DSX(b, src[0], 0);
996 dst[0]->cat5.type = TYPE_F32;
997 break;
998 case nir_op_fddy:
999 dst[0] = ir3_DSY(b, src[0], 0);
1000 dst[0]->cat5.type = TYPE_F32;
1001 break;
1002 break;
1003 case nir_op_flt:
1004 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
1005 dst[0]->cat2.condition = IR3_COND_LT;
1006 dst[0] = ir3_n2b(b, dst[0]);
1007 break;
1008 case nir_op_fge:
1009 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
1010 dst[0]->cat2.condition = IR3_COND_GE;
1011 dst[0] = ir3_n2b(b, dst[0]);
1012 break;
1013 case nir_op_feq:
1014 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
1015 dst[0]->cat2.condition = IR3_COND_EQ;
1016 dst[0] = ir3_n2b(b, dst[0]);
1017 break;
1018 case nir_op_fne:
1019 dst[0] = ir3_CMPS_F(b, src[0], 0, src[1], 0);
1020 dst[0]->cat2.condition = IR3_COND_NE;
1021 dst[0] = ir3_n2b(b, dst[0]);
1022 break;
1023 case nir_op_fceil:
1024 dst[0] = ir3_CEIL_F(b, src[0], 0);
1025 break;
1026 case nir_op_ffloor:
1027 dst[0] = ir3_FLOOR_F(b, src[0], 0);
1028 break;
1029 case nir_op_ftrunc:
1030 dst[0] = ir3_TRUNC_F(b, src[0], 0);
1031 break;
1032 case nir_op_fround_even:
1033 dst[0] = ir3_RNDNE_F(b, src[0], 0);
1034 break;
1035 case nir_op_fsign:
1036 dst[0] = ir3_SIGN_F(b, src[0], 0);
1037 break;
1038
1039 case nir_op_fsin:
1040 dst[0] = ir3_SIN(b, src[0], 0);
1041 break;
1042 case nir_op_fcos:
1043 dst[0] = ir3_COS(b, src[0], 0);
1044 break;
1045 case nir_op_frsq:
1046 dst[0] = ir3_RSQ(b, src[0], 0);
1047 break;
1048 case nir_op_frcp:
1049 dst[0] = ir3_RCP(b, src[0], 0);
1050 break;
1051 case nir_op_flog2:
1052 dst[0] = ir3_LOG2(b, src[0], 0);
1053 break;
1054 case nir_op_fexp2:
1055 dst[0] = ir3_EXP2(b, src[0], 0);
1056 break;
1057 case nir_op_fsqrt:
1058 dst[0] = ir3_SQRT(b, src[0], 0);
1059 break;
1060
1061 case nir_op_iabs:
1062 dst[0] = ir3_ABSNEG_S(b, src[0], IR3_REG_SABS);
1063 break;
1064 case nir_op_iadd:
1065 dst[0] = ir3_ADD_U(b, src[0], 0, src[1], 0);
1066 break;
1067 case nir_op_iand:
1068 dst[0] = ir3_AND_B(b, src[0], 0, src[1], 0);
1069 break;
1070 case nir_op_imax:
1071 dst[0] = ir3_MAX_S(b, src[0], 0, src[1], 0);
1072 break;
1073 case nir_op_umax:
1074 dst[0] = ir3_MAX_U(b, src[0], 0, src[1], 0);
1075 break;
1076 case nir_op_imin:
1077 dst[0] = ir3_MIN_S(b, src[0], 0, src[1], 0);
1078 break;
1079 case nir_op_umin:
1080 dst[0] = ir3_MIN_U(b, src[0], 0, src[1], 0);
1081 break;
1082 case nir_op_imul:
1083 /*
1084 * dst = (al * bl) + (ah * bl << 16) + (al * bh << 16)
1085 * mull.u tmp0, a, b ; mul low, i.e. al * bl
1086 * madsh.m16 tmp1, a, b, tmp0 ; mul-add shift high mix, i.e. ah * bl << 16
1087 * madsh.m16 dst, b, a, tmp1 ; i.e. al * bh << 16
1088 */
1089 dst[0] = ir3_MADSH_M16(b, src[1], 0, src[0], 0,
1090 ir3_MADSH_M16(b, src[0], 0, src[1], 0,
1091 ir3_MULL_U(b, src[0], 0, src[1], 0), 0), 0);
1092 break;
1093 case nir_op_ineg:
1094 dst[0] = ir3_ABSNEG_S(b, src[0], IR3_REG_SNEG);
1095 break;
1096 case nir_op_inot:
1097 dst[0] = ir3_NOT_B(b, src[0], 0);
1098 break;
1099 case nir_op_ior:
1100 dst[0] = ir3_OR_B(b, src[0], 0, src[1], 0);
1101 break;
1102 case nir_op_ishl:
1103 dst[0] = ir3_SHL_B(b, src[0], 0, src[1], 0);
1104 break;
1105 case nir_op_ishr:
1106 dst[0] = ir3_ASHR_B(b, src[0], 0, src[1], 0);
1107 break;
1108 case nir_op_isign: {
1109 /* maybe this would be sane to lower in nir.. */
1110 struct ir3_instruction *neg, *pos;
1111
1112 neg = ir3_CMPS_S(b, src[0], 0, create_immed(b, 0), 0);
1113 neg->cat2.condition = IR3_COND_LT;
1114
1115 pos = ir3_CMPS_S(b, src[0], 0, create_immed(b, 0), 0);
1116 pos->cat2.condition = IR3_COND_GT;
1117
1118 dst[0] = ir3_SUB_U(b, pos, 0, neg, 0);
1119
1120 break;
1121 }
1122 case nir_op_isub:
1123 dst[0] = ir3_SUB_U(b, src[0], 0, src[1], 0);
1124 break;
1125 case nir_op_ixor:
1126 dst[0] = ir3_XOR_B(b, src[0], 0, src[1], 0);
1127 break;
1128 case nir_op_ushr:
1129 dst[0] = ir3_SHR_B(b, src[0], 0, src[1], 0);
1130 break;
1131 case nir_op_ilt:
1132 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
1133 dst[0]->cat2.condition = IR3_COND_LT;
1134 dst[0] = ir3_n2b(b, dst[0]);
1135 break;
1136 case nir_op_ige:
1137 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
1138 dst[0]->cat2.condition = IR3_COND_GE;
1139 dst[0] = ir3_n2b(b, dst[0]);
1140 break;
1141 case nir_op_ieq:
1142 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
1143 dst[0]->cat2.condition = IR3_COND_EQ;
1144 dst[0] = ir3_n2b(b, dst[0]);
1145 break;
1146 case nir_op_ine:
1147 dst[0] = ir3_CMPS_S(b, src[0], 0, src[1], 0);
1148 dst[0]->cat2.condition = IR3_COND_NE;
1149 dst[0] = ir3_n2b(b, dst[0]);
1150 break;
1151 case nir_op_ult:
1152 dst[0] = ir3_CMPS_U(b, src[0], 0, src[1], 0);
1153 dst[0]->cat2.condition = IR3_COND_LT;
1154 dst[0] = ir3_n2b(b, dst[0]);
1155 break;
1156 case nir_op_uge:
1157 dst[0] = ir3_CMPS_U(b, src[0], 0, src[1], 0);
1158 dst[0]->cat2.condition = IR3_COND_GE;
1159 dst[0] = ir3_n2b(b, dst[0]);
1160 break;
1161
1162 case nir_op_bcsel:
1163 dst[0] = ir3_SEL_B32(b, src[1], 0, ir3_b2n(b, src[0]), 0, src[2], 0);
1164 break;
1165
1166 default:
1167 compile_error(ctx, "Unhandled ALU op: %s\n",
1168 nir_op_infos[alu->op].name);
1169 break;
1170 }
1171 }
1172
1173 /* handles direct/indirect UBO reads: */
1174 static void
1175 emit_intrinsic_load_ubo(struct ir3_compile *ctx, nir_intrinsic_instr *intr,
1176 struct ir3_instruction **dst)
1177 {
1178 struct ir3_block *b = ctx->block;
1179 struct ir3_instruction *addr, *src0, *src1;
1180 /* UBO addresses are the first driver params: */
1181 unsigned ubo = regid(ctx->so->first_driver_param, 0);
1182 unsigned off = intr->const_index[0];
1183
1184 /* First src is ubo index, which could either be an immed or not: */
1185 src0 = get_src(ctx, &intr->src[0])[0];
1186 if (is_same_type_mov(src0) &&
1187 (src0->regs[1]->flags & IR3_REG_IMMED)) {
1188 addr = create_uniform(ctx, ubo + src0->regs[1]->iim_val);
1189 } else {
1190 addr = create_uniform_indirect(ctx, ubo, get_addr(ctx, src0));
1191 }
1192
1193 if (intr->intrinsic == nir_intrinsic_load_ubo_indirect) {
1194 /* For load_ubo_indirect, second src is indirect offset: */
1195 src1 = get_src(ctx, &intr->src[1])[0];
1196
1197 /* and add offset to addr: */
1198 addr = ir3_ADD_S(b, addr, 0, src1, 0);
1199 }
1200
1201 /* if offset is to large to encode in the ldg, split it out: */
1202 if ((off + (intr->num_components * 4)) > 1024) {
1203 /* split out the minimal amount to improve the odds that
1204 * cp can fit the immediate in the add.s instruction:
1205 */
1206 unsigned off2 = off + (intr->num_components * 4) - 1024;
1207 addr = ir3_ADD_S(b, addr, 0, create_immed(b, off2), 0);
1208 off -= off2;
1209 }
1210
1211 for (int i = 0; i < intr->num_components; i++) {
1212 struct ir3_instruction *load =
1213 ir3_LDG(b, addr, 0, create_immed(b, 1), 0);
1214 load->cat6.type = TYPE_U32;
1215 load->cat6.src_offset = off + i * 4; /* byte offset */
1216 dst[i] = load;
1217 }
1218 }
1219
1220 /* handles array reads: */
1221 static void
1222 emit_intrinisic_load_var(struct ir3_compile *ctx, nir_intrinsic_instr *intr,
1223 struct ir3_instruction **dst)
1224 {
1225 nir_deref_var *dvar = intr->variables[0];
1226 nir_deref_array *darr = nir_deref_as_array(dvar->deref.child);
1227 struct ir3_array_value *arr = get_var(ctx, dvar->var);
1228
1229 compile_assert(ctx, dvar->deref.child &&
1230 (dvar->deref.child->deref_type == nir_deref_type_array));
1231
1232 switch (darr->deref_array_type) {
1233 case nir_deref_array_type_direct:
1234 /* direct access does not require anything special: */
1235 for (int i = 0; i < intr->num_components; i++) {
1236 unsigned n = darr->base_offset * 4 + i;
1237 compile_assert(ctx, n < arr->length);
1238 dst[i] = arr->arr[n];
1239 }
1240 break;
1241 case nir_deref_array_type_indirect: {
1242 /* for indirect, we need to collect all the array elements: */
1243 struct ir3_instruction *collect =
1244 create_collect(ctx->block, arr->arr, arr->length);
1245 struct ir3_instruction *addr =
1246 get_addr(ctx, get_src(ctx, &darr->indirect)[0]);
1247 for (int i = 0; i < intr->num_components; i++) {
1248 unsigned n = darr->base_offset * 4 + i;
1249 compile_assert(ctx, n < arr->length);
1250 dst[i] = create_indirect_load(ctx, arr->length, n, addr, collect);
1251 }
1252 break;
1253 }
1254 default:
1255 compile_error(ctx, "Unhandled load deref type: %u\n",
1256 darr->deref_array_type);
1257 break;
1258 }
1259 }
1260
1261 /* handles array writes: */
1262 static void
1263 emit_intrinisic_store_var(struct ir3_compile *ctx, nir_intrinsic_instr *intr)
1264 {
1265 nir_deref_var *dvar = intr->variables[0];
1266 nir_deref_array *darr = nir_deref_as_array(dvar->deref.child);
1267 struct ir3_array_value *arr = get_var(ctx, dvar->var);
1268 struct ir3_instruction **src;
1269
1270 compile_assert(ctx, dvar->deref.child &&
1271 (dvar->deref.child->deref_type == nir_deref_type_array));
1272
1273 src = get_src(ctx, &intr->src[0]);
1274
1275 switch (darr->deref_array_type) {
1276 case nir_deref_array_type_direct:
1277 /* direct access does not require anything special: */
1278 for (int i = 0; i < intr->num_components; i++) {
1279 unsigned n = darr->base_offset * 4 + i;
1280 compile_assert(ctx, n < arr->length);
1281 arr->arr[n] = src[i];
1282 }
1283 break;
1284 case nir_deref_array_type_indirect: {
1285 /* for indirect, create indirect-store and fan that out: */
1286 struct ir3_instruction *collect =
1287 create_collect(ctx->block, arr->arr, arr->length);
1288 struct ir3_instruction *addr =
1289 get_addr(ctx, get_src(ctx, &darr->indirect)[0]);
1290 for (int i = 0; i < intr->num_components; i++) {
1291 struct ir3_instruction *store;
1292 unsigned n = darr->base_offset * 4 + i;
1293 compile_assert(ctx, n < arr->length);
1294
1295 store = create_indirect_store(ctx, arr->length,
1296 n, src[i], addr, collect);
1297
1298 store->fanin->fi.aid = arr->aid;
1299
1300 /* TODO: probably split this out to be used for
1301 * store_output_indirect? or move this into
1302 * create_indirect_store()?
1303 */
1304 for (int j = i; j < arr->length; j += intr->num_components) {
1305 struct ir3_instruction *split;
1306
1307 split = ir3_instr_create(ctx->block, -1, OPC_META_FO);
1308 split->fo.off = j;
1309 ir3_reg_create(split, 0, 0);
1310 ir3_reg_create(split, 0, IR3_REG_SSA)->instr = store;
1311
1312 arr->arr[j] = split;
1313 }
1314 }
1315 /* fixup fanout/split neighbors: */
1316 for (int i = 0; i < arr->length; i++) {
1317 arr->arr[i]->cp.right = (i < (arr->length - 1)) ?
1318 arr->arr[i+1] : NULL;
1319 arr->arr[i]->cp.left = (i > 0) ?
1320 arr->arr[i-1] : NULL;
1321 }
1322 break;
1323 }
1324 default:
1325 compile_error(ctx, "Unhandled store deref type: %u\n",
1326 darr->deref_array_type);
1327 break;
1328 }
1329 }
1330
1331 static void add_sysval_input(struct ir3_compile *ctx, unsigned name,
1332 struct ir3_instruction *instr)
1333 {
1334 struct ir3_shader_variant *so = ctx->so;
1335 unsigned r = regid(so->inputs_count, 0);
1336 unsigned n = so->inputs_count++;
1337
1338 so->inputs[n].semantic = ir3_semantic_name(name, 0);
1339 so->inputs[n].compmask = 1;
1340 so->inputs[n].regid = r;
1341 so->inputs[n].interpolate = TGSI_INTERPOLATE_CONSTANT;
1342 so->total_in++;
1343
1344 ctx->ir->ninputs = MAX2(ctx->ir->ninputs, r + 1);
1345 ctx->ir->inputs[r] = instr;
1346 }
1347
1348 static void
1349 emit_intrinisic(struct ir3_compile *ctx, nir_intrinsic_instr *intr)
1350 {
1351 const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
1352 struct ir3_instruction **dst, **src;
1353 struct ir3_block *b = ctx->block;
1354 unsigned idx = intr->const_index[0];
1355
1356 if (info->has_dest) {
1357 dst = get_dst(ctx, &intr->dest, intr->num_components);
1358 } else {
1359 dst = NULL;
1360 }
1361
1362 switch (intr->intrinsic) {
1363 case nir_intrinsic_load_uniform:
1364 for (int i = 0; i < intr->num_components; i++) {
1365 unsigned n = idx * 4 + i;
1366 dst[i] = create_uniform(ctx, n);
1367 }
1368 break;
1369 case nir_intrinsic_load_uniform_indirect:
1370 src = get_src(ctx, &intr->src[0]);
1371 for (int i = 0; i < intr->num_components; i++) {
1372 unsigned n = idx * 4 + i;
1373 dst[i] = create_uniform_indirect(ctx, n,
1374 get_addr(ctx, src[0]));
1375 }
1376 /* NOTE: if relative addressing is used, we set constlen in
1377 * the compiler (to worst-case value) since we don't know in
1378 * the assembler what the max addr reg value can be:
1379 */
1380 ctx->so->constlen = ctx->s->num_uniforms;
1381 break;
1382 case nir_intrinsic_load_ubo:
1383 case nir_intrinsic_load_ubo_indirect:
1384 emit_intrinsic_load_ubo(ctx, intr, dst);
1385 break;
1386 case nir_intrinsic_load_input:
1387 for (int i = 0; i < intr->num_components; i++) {
1388 unsigned n = idx * 4 + i;
1389 dst[i] = ctx->ir->inputs[n];
1390 }
1391 break;
1392 case nir_intrinsic_load_input_indirect:
1393 src = get_src(ctx, &intr->src[0]);
1394 struct ir3_instruction *collect =
1395 create_collect(b, ctx->ir->inputs, ctx->ir->ninputs);
1396 struct ir3_instruction *addr = get_addr(ctx, src[0]);
1397 for (int i = 0; i < intr->num_components; i++) {
1398 unsigned n = idx * 4 + i;
1399 dst[i] = create_indirect_load(ctx, ctx->ir->ninputs,
1400 n, addr, collect);
1401 }
1402 break;
1403 case nir_intrinsic_load_var:
1404 emit_intrinisic_load_var(ctx, intr, dst);
1405 break;
1406 case nir_intrinsic_store_var:
1407 emit_intrinisic_store_var(ctx, intr);
1408 break;
1409 case nir_intrinsic_store_output:
1410 src = get_src(ctx, &intr->src[0]);
1411 for (int i = 0; i < intr->num_components; i++) {
1412 unsigned n = idx * 4 + i;
1413 ctx->ir->outputs[n] = src[i];
1414 }
1415 break;
1416 case nir_intrinsic_load_base_vertex:
1417 if (!ctx->basevertex) {
1418 /* first four vec4 sysval's reserved for UBOs: */
1419 unsigned r = regid(ctx->so->first_driver_param + 4, 0);
1420 ctx->basevertex = create_uniform(ctx, r);
1421 add_sysval_input(ctx, TGSI_SEMANTIC_BASEVERTEX,
1422 ctx->basevertex);
1423 }
1424 dst[0] = ctx->basevertex;
1425 break;
1426 case nir_intrinsic_load_vertex_id_zero_base:
1427 if (!ctx->vertex_id) {
1428 ctx->vertex_id = create_input(ctx->block, 0);
1429 add_sysval_input(ctx, TGSI_SEMANTIC_VERTEXID_NOBASE,
1430 ctx->vertex_id);
1431 }
1432 dst[0] = ctx->vertex_id;
1433 break;
1434 case nir_intrinsic_load_instance_id:
1435 if (!ctx->instance_id) {
1436 ctx->instance_id = create_input(ctx->block, 0);
1437 add_sysval_input(ctx, TGSI_SEMANTIC_INSTANCEID,
1438 ctx->instance_id);
1439 }
1440 dst[0] = ctx->instance_id;
1441 break;
1442 case nir_intrinsic_discard_if:
1443 case nir_intrinsic_discard: {
1444 struct ir3_instruction *cond, *kill;
1445
1446 if (intr->intrinsic == nir_intrinsic_discard_if) {
1447 /* conditional discard: */
1448 src = get_src(ctx, &intr->src[0]);
1449 cond = ir3_b2n(b, src[0]);
1450 } else {
1451 /* unconditional discard: */
1452 cond = create_immed(b, 1);
1453 }
1454
1455 /* NOTE: only cmps.*.* can write p0.x: */
1456 cond = ir3_CMPS_S(b, cond, 0, create_immed(b, 0), 0);
1457 cond->cat2.condition = IR3_COND_NE;
1458
1459 /* condition always goes in predicate register: */
1460 cond->regs[0]->num = regid(REG_P0, 0);
1461
1462 kill = ir3_KILL(b, cond, 0);
1463 array_insert(ctx->ir->predicates, kill);
1464
1465 ctx->kill[ctx->kill_count++] = kill;
1466 ctx->so->has_kill = true;
1467
1468 break;
1469 }
1470 default:
1471 compile_error(ctx, "Unhandled intrinsic type: %s\n",
1472 nir_intrinsic_infos[intr->intrinsic].name);
1473 break;
1474 }
1475 }
1476
1477 static void
1478 emit_load_const(struct ir3_compile *ctx, nir_load_const_instr *instr)
1479 {
1480 struct ir3_instruction **dst = get_dst_ssa(ctx, &instr->def,
1481 instr->def.num_components);
1482 for (int i = 0; i < instr->def.num_components; i++)
1483 dst[i] = create_immed(ctx->block, instr->value.u[i]);
1484 }
1485
1486 static void
1487 emit_undef(struct ir3_compile *ctx, nir_ssa_undef_instr *undef)
1488 {
1489 struct ir3_instruction **dst = get_dst_ssa(ctx, &undef->def,
1490 undef->def.num_components);
1491 /* backend doesn't want undefined instructions, so just plug
1492 * in 0.0..
1493 */
1494 for (int i = 0; i < undef->def.num_components; i++)
1495 dst[i] = create_immed(ctx->block, fui(0.0));
1496 }
1497
1498 /*
1499 * texture fetch/sample instructions:
1500 */
1501
1502 static void
1503 tex_info(nir_tex_instr *tex, unsigned *flagsp, unsigned *coordsp)
1504 {
1505 unsigned coords, flags = 0;
1506
1507 /* note: would use tex->coord_components.. except txs.. also,
1508 * since array index goes after shadow ref, we don't want to
1509 * count it:
1510 */
1511 switch (tex->sampler_dim) {
1512 case GLSL_SAMPLER_DIM_1D:
1513 case GLSL_SAMPLER_DIM_BUF:
1514 coords = 1;
1515 break;
1516 case GLSL_SAMPLER_DIM_2D:
1517 case GLSL_SAMPLER_DIM_RECT:
1518 case GLSL_SAMPLER_DIM_EXTERNAL:
1519 case GLSL_SAMPLER_DIM_MS:
1520 coords = 2;
1521 break;
1522 case GLSL_SAMPLER_DIM_3D:
1523 case GLSL_SAMPLER_DIM_CUBE:
1524 coords = 3;
1525 flags |= IR3_INSTR_3D;
1526 break;
1527 default:
1528 unreachable("bad sampler_dim");
1529 }
1530
1531 if (tex->is_shadow)
1532 flags |= IR3_INSTR_S;
1533
1534 if (tex->is_array)
1535 flags |= IR3_INSTR_A;
1536
1537 *flagsp = flags;
1538 *coordsp = coords;
1539 }
1540
1541 static void
1542 emit_tex(struct ir3_compile *ctx, nir_tex_instr *tex)
1543 {
1544 struct ir3_block *b = ctx->block;
1545 struct ir3_instruction **dst, *sam, *src0[12], *src1[4];
1546 struct ir3_instruction **coord, *lod, *compare, *proj, **off, **ddx, **ddy;
1547 bool has_bias = false, has_lod = false, has_proj = false, has_off = false;
1548 unsigned i, coords, flags;
1549 unsigned nsrc0 = 0, nsrc1 = 0;
1550 type_t type;
1551 opc_t opc = 0;
1552
1553 coord = off = ddx = ddy = NULL;
1554 lod = proj = compare = NULL;
1555
1556 /* TODO: might just be one component for gathers? */
1557 dst = get_dst(ctx, &tex->dest, 4);
1558
1559 for (unsigned i = 0; i < tex->num_srcs; i++) {
1560 switch (tex->src[i].src_type) {
1561 case nir_tex_src_coord:
1562 coord = get_src(ctx, &tex->src[i].src);
1563 break;
1564 case nir_tex_src_bias:
1565 lod = get_src(ctx, &tex->src[i].src)[0];
1566 has_bias = true;
1567 break;
1568 case nir_tex_src_lod:
1569 lod = get_src(ctx, &tex->src[i].src)[0];
1570 has_lod = true;
1571 break;
1572 case nir_tex_src_comparitor: /* shadow comparator */
1573 compare = get_src(ctx, &tex->src[i].src)[0];
1574 break;
1575 case nir_tex_src_projector:
1576 proj = get_src(ctx, &tex->src[i].src)[0];
1577 has_proj = true;
1578 break;
1579 case nir_tex_src_offset:
1580 off = get_src(ctx, &tex->src[i].src);
1581 has_off = true;
1582 break;
1583 case nir_tex_src_ddx:
1584 ddx = get_src(ctx, &tex->src[i].src);
1585 break;
1586 case nir_tex_src_ddy:
1587 ddy = get_src(ctx, &tex->src[i].src);
1588 break;
1589 default:
1590 compile_error(ctx, "Unhandled NIR tex serc type: %d\n",
1591 tex->src[i].src_type);
1592 return;
1593 }
1594 }
1595
1596 switch (tex->op) {
1597 case nir_texop_tex: opc = OPC_SAM; break;
1598 case nir_texop_txb: opc = OPC_SAMB; break;
1599 case nir_texop_txl: opc = OPC_SAML; break;
1600 case nir_texop_txd: opc = OPC_SAMGQ; break;
1601 case nir_texop_txf: opc = OPC_ISAML; break;
1602 case nir_texop_txf_ms:
1603 case nir_texop_txs:
1604 case nir_texop_lod:
1605 case nir_texop_tg4:
1606 case nir_texop_query_levels:
1607 compile_error(ctx, "Unhandled NIR tex type: %d\n", tex->op);
1608 return;
1609 }
1610
1611 tex_info(tex, &flags, &coords);
1612
1613 /* scale up integer coords for TXF based on the LOD */
1614 if (ctx->unminify_coords && (opc == OPC_ISAML)) {
1615 assert(has_lod);
1616 for (i = 0; i < coords; i++)
1617 coord[i] = ir3_SHL_B(b, coord[i], 0, lod, 0);
1618 }
1619
1620 /*
1621 * lay out the first argument in the proper order:
1622 * - actual coordinates first
1623 * - shadow reference
1624 * - array index
1625 * - projection w
1626 * - starting at offset 4, dpdx.xy, dpdy.xy
1627 *
1628 * bias/lod go into the second arg
1629 */
1630
1631 /* insert tex coords: */
1632 for (i = 0; i < coords; i++)
1633 src0[nsrc0++] = coord[i];
1634
1635 if (coords == 1) {
1636 /* hw doesn't do 1d, so we treat it as 2d with
1637 * height of 1, and patch up the y coord.
1638 * TODO: y coord should be (int)0 in some cases..
1639 */
1640 src0[nsrc0++] = create_immed(b, fui(0.5));
1641 }
1642
1643 if (tex->is_shadow)
1644 src0[nsrc0++] = compare;
1645
1646 if (tex->is_array)
1647 src0[nsrc0++] = coord[coords];
1648
1649 if (has_proj) {
1650 src0[nsrc0++] = proj;
1651 flags |= IR3_INSTR_P;
1652 }
1653
1654 /* pad to 4, then ddx/ddy: */
1655 if (tex->op == nir_texop_txd) {
1656 while (nsrc0 < 4)
1657 src0[nsrc0++] = create_immed(b, fui(0.0));
1658 for (i = 0; i < coords; i++)
1659 src0[nsrc0++] = ddx[i];
1660 if (coords < 2)
1661 src0[nsrc0++] = create_immed(b, fui(0.0));
1662 for (i = 0; i < coords; i++)
1663 src0[nsrc0++] = ddy[i];
1664 if (coords < 2)
1665 src0[nsrc0++] = create_immed(b, fui(0.0));
1666 }
1667
1668 /*
1669 * second argument (if applicable):
1670 * - offsets
1671 * - lod
1672 * - bias
1673 */
1674 if (has_off | has_lod | has_bias) {
1675 if (has_off) {
1676 for (i = 0; i < coords; i++)
1677 src1[nsrc1++] = off[i];
1678 if (coords < 2)
1679 src1[nsrc1++] = create_immed(b, fui(0.0));
1680 flags |= IR3_INSTR_O;
1681 }
1682
1683 if (has_lod | has_bias)
1684 src1[nsrc1++] = lod;
1685 }
1686
1687 switch (tex->dest_type) {
1688 case nir_type_invalid:
1689 case nir_type_float:
1690 type = TYPE_F32;
1691 break;
1692 case nir_type_int:
1693 type = TYPE_S32;
1694 break;
1695 case nir_type_unsigned:
1696 case nir_type_bool:
1697 type = TYPE_U32;
1698 break;
1699 default:
1700 unreachable("bad dest_type");
1701 }
1702
1703 sam = ir3_SAM(b, opc, type, TGSI_WRITEMASK_XYZW,
1704 flags, tex->sampler_index, tex->sampler_index,
1705 create_collect(b, src0, nsrc0),
1706 create_collect(b, src1, nsrc1));
1707
1708 split_dest(b, dst, sam, 4);
1709 }
1710
1711 static void
1712 emit_tex_query_levels(struct ir3_compile *ctx, nir_tex_instr *tex)
1713 {
1714 struct ir3_block *b = ctx->block;
1715 struct ir3_instruction **dst, *sam;
1716
1717 dst = get_dst(ctx, &tex->dest, 1);
1718
1719 sam = ir3_SAM(b, OPC_GETINFO, TYPE_U32, TGSI_WRITEMASK_Z, 0,
1720 tex->sampler_index, tex->sampler_index, NULL, NULL);
1721
1722 /* even though there is only one component, since it ends
1723 * up in .z rather than .x, we need a split_dest()
1724 */
1725 split_dest(b, dst, sam, 3);
1726
1727 /* The # of levels comes from getinfo.z. We need to add 1 to it, since
1728 * the value in TEX_CONST_0 is zero-based.
1729 */
1730 if (ctx->levels_add_one)
1731 dst[0] = ir3_ADD_U(b, dst[0], 0, create_immed(b, 1), 0);
1732 }
1733
1734 static void
1735 emit_tex_txs(struct ir3_compile *ctx, nir_tex_instr *tex)
1736 {
1737 struct ir3_block *b = ctx->block;
1738 struct ir3_instruction **dst, *sam, *lod;
1739 unsigned flags, coords;
1740
1741 tex_info(tex, &flags, &coords);
1742
1743 dst = get_dst(ctx, &tex->dest, 4);
1744
1745 compile_assert(ctx, tex->num_srcs == 1);
1746 compile_assert(ctx, tex->src[0].src_type == nir_tex_src_lod);
1747
1748 lod = get_src(ctx, &tex->src[0].src)[0];
1749
1750 sam = ir3_SAM(b, OPC_GETSIZE, TYPE_U32, TGSI_WRITEMASK_XYZW, flags,
1751 tex->sampler_index, tex->sampler_index, lod, NULL);
1752
1753 split_dest(b, dst, sam, 4);
1754
1755 /* Array size actually ends up in .w rather than .z. This doesn't
1756 * matter for miplevel 0, but for higher mips the value in z is
1757 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
1758 * returned, which means that we have to add 1 to it for arrays.
1759 */
1760 if (tex->is_array) {
1761 if (ctx->levels_add_one) {
1762 dst[coords] = ir3_ADD_U(b, dst[3], 0, create_immed(b, 1), 0);
1763 } else {
1764 dst[coords] = ir3_MOV(b, dst[3], TYPE_U32);
1765 }
1766 }
1767 }
1768
1769 static void
1770 emit_phi(struct ir3_compile *ctx, nir_phi_instr *nphi)
1771 {
1772 struct ir3_instruction *phi, **dst;
1773
1774 /* NOTE: phi's should be lowered to scalar at this point */
1775 compile_assert(ctx, nphi->dest.ssa.num_components == 1);
1776
1777 dst = get_dst(ctx, &nphi->dest, 1);
1778
1779 phi = ir3_instr_create2(ctx->block, -1, OPC_META_PHI,
1780 1 + exec_list_length(&nphi->srcs));
1781 ir3_reg_create(phi, 0, 0); /* dst */
1782 phi->phi.nphi = nphi;
1783
1784 dst[0] = phi;
1785 }
1786
1787 /* phi instructions are left partially constructed. We don't resolve
1788 * their srcs until the end of the block, since (eg. loops) one of
1789 * the phi's srcs might be defined after the phi due to back edges in
1790 * the CFG.
1791 */
1792 static void
1793 resolve_phis(struct ir3_compile *ctx, struct ir3_block *block)
1794 {
1795 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
1796 nir_phi_instr *nphi;
1797
1798 /* phi's only come at start of block: */
1799 if (!(is_meta(instr) && (instr->opc == OPC_META_PHI)))
1800 break;
1801
1802 if (!instr->phi.nphi)
1803 break;
1804
1805 nphi = instr->phi.nphi;
1806 instr->phi.nphi = NULL;
1807
1808 foreach_list_typed(nir_phi_src, nsrc, node, &nphi->srcs) {
1809 struct ir3_instruction *src = get_src(ctx, &nsrc->src)[0];
1810 ir3_reg_create(instr, 0, IR3_REG_SSA)->instr = src;
1811 }
1812 }
1813
1814 resolve_array_phis(ctx, block);
1815 }
1816
1817 static void
1818 emit_jump(struct ir3_compile *ctx, nir_jump_instr *jump)
1819 {
1820 switch (jump->type) {
1821 case nir_jump_break:
1822 case nir_jump_continue:
1823 /* I *think* we can simply just ignore this, and use the
1824 * successor block link to figure out where we need to
1825 * jump to for break/continue
1826 */
1827 break;
1828 default:
1829 compile_error(ctx, "Unhandled NIR jump type: %d\n", jump->type);
1830 break;
1831 }
1832 }
1833
1834 static void
1835 emit_instr(struct ir3_compile *ctx, nir_instr *instr)
1836 {
1837 switch (instr->type) {
1838 case nir_instr_type_alu:
1839 emit_alu(ctx, nir_instr_as_alu(instr));
1840 break;
1841 case nir_instr_type_intrinsic:
1842 emit_intrinisic(ctx, nir_instr_as_intrinsic(instr));
1843 break;
1844 case nir_instr_type_load_const:
1845 emit_load_const(ctx, nir_instr_as_load_const(instr));
1846 break;
1847 case nir_instr_type_ssa_undef:
1848 emit_undef(ctx, nir_instr_as_ssa_undef(instr));
1849 break;
1850 case nir_instr_type_tex: {
1851 nir_tex_instr *tex = nir_instr_as_tex(instr);
1852 /* couple tex instructions get special-cased:
1853 */
1854 switch (tex->op) {
1855 case nir_texop_txs:
1856 emit_tex_txs(ctx, tex);
1857 break;
1858 case nir_texop_query_levels:
1859 emit_tex_query_levels(ctx, tex);
1860 break;
1861 default:
1862 emit_tex(ctx, tex);
1863 break;
1864 }
1865 break;
1866 }
1867 case nir_instr_type_phi:
1868 emit_phi(ctx, nir_instr_as_phi(instr));
1869 break;
1870 case nir_instr_type_jump:
1871 emit_jump(ctx, nir_instr_as_jump(instr));
1872 break;
1873 case nir_instr_type_call:
1874 case nir_instr_type_parallel_copy:
1875 compile_error(ctx, "Unhandled NIR instruction type: %d\n", instr->type);
1876 break;
1877 }
1878 }
1879
1880 static struct ir3_block *
1881 get_block(struct ir3_compile *ctx, nir_block *nblock)
1882 {
1883 struct ir3_block *block;
1884 struct hash_entry *entry;
1885 entry = _mesa_hash_table_search(ctx->block_ht, nblock);
1886 if (entry)
1887 return entry->data;
1888
1889 block = ir3_block_create(ctx->ir);
1890 block->nblock = nblock;
1891 _mesa_hash_table_insert(ctx->block_ht, nblock, block);
1892
1893 return block;
1894 }
1895
1896 static void
1897 emit_block(struct ir3_compile *ctx, nir_block *nblock)
1898 {
1899 struct ir3_block *block = get_block(ctx, nblock);
1900
1901 for (int i = 0; i < ARRAY_SIZE(block->successors); i++) {
1902 if (nblock->successors[i]) {
1903 block->successors[i] =
1904 get_block(ctx, nblock->successors[i]);
1905 }
1906 }
1907
1908 ctx->block = block;
1909 list_addtail(&block->node, &ctx->ir->block_list);
1910
1911 nir_foreach_instr(nblock, instr) {
1912 emit_instr(ctx, instr);
1913 if (ctx->error)
1914 return;
1915 }
1916 }
1917
1918 static void emit_cf_list(struct ir3_compile *ctx, struct exec_list *list);
1919
1920 static void
1921 emit_if(struct ir3_compile *ctx, nir_if *nif)
1922 {
1923 struct ir3_instruction *condition = get_src(ctx, &nif->condition)[0];
1924
1925 ctx->block->condition =
1926 get_predicate(ctx, ir3_b2n(condition->block, condition));
1927
1928 emit_cf_list(ctx, &nif->then_list);
1929 emit_cf_list(ctx, &nif->else_list);
1930 }
1931
1932 static void
1933 emit_loop(struct ir3_compile *ctx, nir_loop *nloop)
1934 {
1935 emit_cf_list(ctx, &nloop->body);
1936 }
1937
1938 static void
1939 emit_cf_list(struct ir3_compile *ctx, struct exec_list *list)
1940 {
1941 foreach_list_typed(nir_cf_node, node, node, list) {
1942 switch (node->type) {
1943 case nir_cf_node_block:
1944 emit_block(ctx, nir_cf_node_as_block(node));
1945 break;
1946 case nir_cf_node_if:
1947 emit_if(ctx, nir_cf_node_as_if(node));
1948 break;
1949 case nir_cf_node_loop:
1950 emit_loop(ctx, nir_cf_node_as_loop(node));
1951 break;
1952 case nir_cf_node_function:
1953 compile_error(ctx, "TODO\n");
1954 break;
1955 }
1956 }
1957 }
1958
1959 static void
1960 emit_function(struct ir3_compile *ctx, nir_function_impl *impl)
1961 {
1962 emit_cf_list(ctx, &impl->body);
1963 emit_block(ctx, impl->end_block);
1964
1965 /* at this point, we should have a single empty block,
1966 * into which we emit the 'end' instruction.
1967 */
1968 compile_assert(ctx, list_empty(&ctx->block->instr_list));
1969 ir3_END(ctx->block);
1970 }
1971
1972 static void
1973 setup_input(struct ir3_compile *ctx, nir_variable *in)
1974 {
1975 struct ir3_shader_variant *so = ctx->so;
1976 unsigned array_len = MAX2(glsl_get_length(in->type), 1);
1977 unsigned ncomp = glsl_get_components(in->type);
1978 /* XXX: map loc slots to semantics */
1979 unsigned semantic_name = in->data.location;
1980 unsigned semantic_index = in->data.index;
1981 unsigned n = in->data.driver_location;
1982
1983 DBG("; in: %u:%u, len=%ux%u, loc=%u",
1984 semantic_name, semantic_index, array_len,
1985 ncomp, n);
1986
1987 so->inputs[n].semantic =
1988 ir3_semantic_name(semantic_name, semantic_index);
1989 so->inputs[n].compmask = (1 << ncomp) - 1;
1990 so->inputs[n].inloc = ctx->next_inloc;
1991 so->inputs[n].interpolate = 0;
1992 so->inputs_count = MAX2(so->inputs_count, n + 1);
1993
1994 /* the fdN_program_emit() code expects tgsi consts here, so map
1995 * things back to tgsi for now:
1996 */
1997 switch (in->data.interpolation) {
1998 case INTERP_QUALIFIER_FLAT:
1999 so->inputs[n].interpolate = TGSI_INTERPOLATE_CONSTANT;
2000 break;
2001 case INTERP_QUALIFIER_NOPERSPECTIVE:
2002 so->inputs[n].interpolate = TGSI_INTERPOLATE_LINEAR;
2003 break;
2004 case INTERP_QUALIFIER_SMOOTH:
2005 so->inputs[n].interpolate = TGSI_INTERPOLATE_PERSPECTIVE;
2006 break;
2007 }
2008
2009 for (int i = 0; i < ncomp; i++) {
2010 struct ir3_instruction *instr = NULL;
2011 unsigned idx = (n * 4) + i;
2012
2013 if (ctx->so->type == SHADER_FRAGMENT) {
2014 if (semantic_name == TGSI_SEMANTIC_POSITION) {
2015 so->inputs[n].bary = false;
2016 so->frag_coord = true;
2017 instr = create_frag_coord(ctx, i);
2018 } else if (semantic_name == TGSI_SEMANTIC_FACE) {
2019 so->inputs[n].bary = false;
2020 so->frag_face = true;
2021 instr = create_frag_face(ctx, i);
2022 } else {
2023 bool use_ldlv = false;
2024
2025 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
2026 * from the semantic name:
2027 */
2028 if ((in->data.interpolation == INTERP_QUALIFIER_NONE) &&
2029 ((semantic_name == TGSI_SEMANTIC_COLOR) ||
2030 (semantic_name == TGSI_SEMANTIC_BCOLOR)))
2031 so->inputs[n].interpolate = TGSI_INTERPOLATE_COLOR;
2032
2033 if (ctx->flat_bypass) {
2034 /* with NIR, we need to infer TGSI_INTERPOLATE_COLOR
2035 * from the semantic name:
2036 */
2037 switch (so->inputs[n].interpolate) {
2038 case TGSI_INTERPOLATE_COLOR:
2039 if (!ctx->so->key.rasterflat)
2040 break;
2041 /* fallthrough */
2042 case TGSI_INTERPOLATE_CONSTANT:
2043 use_ldlv = true;
2044 break;
2045 }
2046 }
2047
2048 so->inputs[n].bary = true;
2049
2050 instr = create_frag_input(ctx,
2051 so->inputs[n].inloc + i - 8, use_ldlv);
2052 }
2053 } else {
2054 instr = create_input(ctx->block, idx);
2055 }
2056
2057 ctx->ir->inputs[idx] = instr;
2058 }
2059
2060 if (so->inputs[n].bary || (ctx->so->type == SHADER_VERTEX)) {
2061 ctx->next_inloc += ncomp;
2062 so->total_in += ncomp;
2063 }
2064 }
2065
2066 static void
2067 setup_output(struct ir3_compile *ctx, nir_variable *out)
2068 {
2069 struct ir3_shader_variant *so = ctx->so;
2070 unsigned array_len = MAX2(glsl_get_length(out->type), 1);
2071 unsigned ncomp = glsl_get_components(out->type);
2072 /* XXX: map loc slots to semantics */
2073 unsigned semantic_name = out->data.location;
2074 unsigned semantic_index = out->data.index;
2075 unsigned n = out->data.driver_location;
2076 unsigned comp = 0;
2077
2078 DBG("; out: %u:%u, len=%ux%u, loc=%u",
2079 semantic_name, semantic_index, array_len,
2080 ncomp, n);
2081
2082 if (ctx->so->type == SHADER_VERTEX) {
2083 switch (semantic_name) {
2084 case TGSI_SEMANTIC_POSITION:
2085 so->writes_pos = true;
2086 break;
2087 case TGSI_SEMANTIC_PSIZE:
2088 so->writes_psize = true;
2089 break;
2090 case TGSI_SEMANTIC_COLOR:
2091 case TGSI_SEMANTIC_BCOLOR:
2092 case TGSI_SEMANTIC_GENERIC:
2093 case TGSI_SEMANTIC_FOG:
2094 case TGSI_SEMANTIC_TEXCOORD:
2095 break;
2096 default:
2097 compile_error(ctx, "unknown VS semantic name: %s\n",
2098 tgsi_semantic_names[semantic_name]);
2099 }
2100 } else {
2101 switch (semantic_name) {
2102 case TGSI_SEMANTIC_POSITION:
2103 comp = 2; /* tgsi will write to .z component */
2104 so->writes_pos = true;
2105 break;
2106 case TGSI_SEMANTIC_COLOR:
2107 if (semantic_index == -1) {
2108 semantic_index = 0;
2109 so->color0_mrt = 1;
2110 }
2111 break;
2112 default:
2113 compile_error(ctx, "unknown FS semantic name: %s\n",
2114 tgsi_semantic_names[semantic_name]);
2115 }
2116 }
2117
2118 compile_assert(ctx, n < ARRAY_SIZE(so->outputs));
2119
2120 so->outputs[n].semantic =
2121 ir3_semantic_name(semantic_name, semantic_index);
2122 so->outputs[n].regid = regid(n, comp);
2123 so->outputs_count = MAX2(so->outputs_count, n + 1);
2124
2125 for (int i = 0; i < ncomp; i++) {
2126 unsigned idx = (n * 4) + i;
2127
2128 ctx->ir->outputs[idx] = create_immed(ctx->block, fui(0.0));
2129 }
2130 }
2131
2132 static void
2133 emit_instructions(struct ir3_compile *ctx)
2134 {
2135 unsigned ninputs, noutputs;
2136 nir_function_impl *fxn = NULL;
2137
2138 /* Find the main function: */
2139 nir_foreach_overload(ctx->s, overload) {
2140 compile_assert(ctx, strcmp(overload->function->name, "main") == 0);
2141 compile_assert(ctx, overload->impl);
2142 fxn = overload->impl;
2143 break;
2144 }
2145
2146 ninputs = exec_list_length(&ctx->s->inputs) * 4;
2147 noutputs = exec_list_length(&ctx->s->outputs) * 4;
2148
2149 /* we need to allocate big enough outputs array so that
2150 * we can stuff the kill's at the end. Likewise for vtx
2151 * shaders, we need to leave room for sysvals:
2152 */
2153 if (ctx->so->type == SHADER_FRAGMENT) {
2154 noutputs += ARRAY_SIZE(ctx->kill);
2155 } else if (ctx->so->type == SHADER_VERTEX) {
2156 ninputs += 8;
2157 }
2158
2159 ctx->ir = ir3_create(ctx->compiler, ninputs, noutputs);
2160
2161 /* Create inputs in first block: */
2162 ctx->block = get_block(ctx, fxn->start_block);
2163 ctx->in_block = ctx->block;
2164 list_addtail(&ctx->block->node, &ctx->ir->block_list);
2165
2166 if (ctx->so->type == SHADER_FRAGMENT) {
2167 ctx->ir->noutputs -= ARRAY_SIZE(ctx->kill);
2168 } else if (ctx->so->type == SHADER_VERTEX) {
2169 ctx->ir->ninputs -= 8;
2170 }
2171
2172 /* for fragment shader, we have a single input register (usually
2173 * r0.xy) which is used as the base for bary.f varying fetch instrs:
2174 */
2175 if (ctx->so->type == SHADER_FRAGMENT) {
2176 // TODO maybe a helper for fi since we need it a few places..
2177 struct ir3_instruction *instr;
2178 instr = ir3_instr_create(ctx->block, -1, OPC_META_FI);
2179 ir3_reg_create(instr, 0, 0);
2180 ir3_reg_create(instr, 0, IR3_REG_SSA); /* r0.x */
2181 ir3_reg_create(instr, 0, IR3_REG_SSA); /* r0.y */
2182 ctx->frag_pos = instr;
2183 }
2184
2185 /* Setup inputs: */
2186 foreach_list_typed(nir_variable, var, node, &ctx->s->inputs) {
2187 setup_input(ctx, var);
2188 }
2189
2190 /* Setup outputs: */
2191 foreach_list_typed(nir_variable, var, node, &ctx->s->outputs) {
2192 setup_output(ctx, var);
2193 }
2194
2195 /* Setup variables (which should only be arrays): */
2196 foreach_list_typed(nir_variable, var, node, &ctx->s->globals) {
2197 declare_var(ctx, var);
2198 }
2199
2200 /* And emit the body: */
2201 ctx->impl = fxn;
2202 emit_function(ctx, fxn);
2203
2204 list_for_each_entry (struct ir3_block, block, &ctx->ir->block_list, node) {
2205 resolve_phis(ctx, block);
2206 }
2207 }
2208
2209 /* from NIR perspective, we actually have inputs. But most of the "inputs"
2210 * for a fragment shader are just bary.f instructions. The *actual* inputs
2211 * from the hw perspective are the frag_pos and optionally frag_coord and
2212 * frag_face.
2213 */
2214 static void
2215 fixup_frag_inputs(struct ir3_compile *ctx)
2216 {
2217 struct ir3_shader_variant *so = ctx->so;
2218 struct ir3 *ir = ctx->ir;
2219 struct ir3_instruction **inputs;
2220 struct ir3_instruction *instr;
2221 int n, regid = 0;
2222
2223 ir->ninputs = 0;
2224
2225 n = 4; /* always have frag_pos */
2226 n += COND(so->frag_face, 4);
2227 n += COND(so->frag_coord, 4);
2228
2229 inputs = ir3_alloc(ctx->ir, n * (sizeof(struct ir3_instruction *)));
2230
2231 if (so->frag_face) {
2232 /* this ultimately gets assigned to hr0.x so doesn't conflict
2233 * with frag_coord/frag_pos..
2234 */
2235 inputs[ir->ninputs++] = ctx->frag_face;
2236 ctx->frag_face->regs[0]->num = 0;
2237
2238 /* remaining channels not used, but let's avoid confusing
2239 * other parts that expect inputs to come in groups of vec4
2240 */
2241 inputs[ir->ninputs++] = NULL;
2242 inputs[ir->ninputs++] = NULL;
2243 inputs[ir->ninputs++] = NULL;
2244 }
2245
2246 /* since we don't know where to set the regid for frag_coord,
2247 * we have to use r0.x for it. But we don't want to *always*
2248 * use r1.x for frag_pos as that could increase the register
2249 * footprint on simple shaders:
2250 */
2251 if (so->frag_coord) {
2252 ctx->frag_coord[0]->regs[0]->num = regid++;
2253 ctx->frag_coord[1]->regs[0]->num = regid++;
2254 ctx->frag_coord[2]->regs[0]->num = regid++;
2255 ctx->frag_coord[3]->regs[0]->num = regid++;
2256
2257 inputs[ir->ninputs++] = ctx->frag_coord[0];
2258 inputs[ir->ninputs++] = ctx->frag_coord[1];
2259 inputs[ir->ninputs++] = ctx->frag_coord[2];
2260 inputs[ir->ninputs++] = ctx->frag_coord[3];
2261 }
2262
2263 /* we always have frag_pos: */
2264 so->pos_regid = regid;
2265
2266 /* r0.x */
2267 instr = create_input(ctx->in_block, ir->ninputs);
2268 instr->regs[0]->num = regid++;
2269 inputs[ir->ninputs++] = instr;
2270 ctx->frag_pos->regs[1]->instr = instr;
2271
2272 /* r0.y */
2273 instr = create_input(ctx->in_block, ir->ninputs);
2274 instr->regs[0]->num = regid++;
2275 inputs[ir->ninputs++] = instr;
2276 ctx->frag_pos->regs[2]->instr = instr;
2277
2278 ir->inputs = inputs;
2279 }
2280
2281 int
2282 ir3_compile_shader_nir(struct ir3_compiler *compiler,
2283 struct ir3_shader_variant *so)
2284 {
2285 struct ir3_compile *ctx;
2286 struct ir3 *ir;
2287 struct ir3_instruction **inputs;
2288 unsigned i, j, actual_in;
2289 int ret = 0, max_bary;
2290
2291 assert(!so->ir);
2292
2293 ctx = compile_init(compiler, so, so->shader->tokens);
2294 if (!ctx) {
2295 DBG("INIT failed!");
2296 ret = -1;
2297 goto out;
2298 }
2299
2300 emit_instructions(ctx);
2301
2302 if (ctx->error) {
2303 DBG("EMIT failed!");
2304 ret = -1;
2305 goto out;
2306 }
2307
2308 ir = so->ir = ctx->ir;
2309
2310 /* keep track of the inputs from TGSI perspective.. */
2311 inputs = ir->inputs;
2312
2313 /* but fixup actual inputs for frag shader: */
2314 if (so->type == SHADER_FRAGMENT)
2315 fixup_frag_inputs(ctx);
2316
2317 /* at this point, for binning pass, throw away unneeded outputs: */
2318 if (so->key.binning_pass) {
2319 for (i = 0, j = 0; i < so->outputs_count; i++) {
2320 unsigned name = sem2name(so->outputs[i].semantic);
2321 unsigned idx = sem2idx(so->outputs[i].semantic);
2322
2323 /* throw away everything but first position/psize */
2324 if ((idx == 0) && ((name == TGSI_SEMANTIC_POSITION) ||
2325 (name == TGSI_SEMANTIC_PSIZE))) {
2326 if (i != j) {
2327 so->outputs[j] = so->outputs[i];
2328 ir->outputs[(j*4)+0] = ir->outputs[(i*4)+0];
2329 ir->outputs[(j*4)+1] = ir->outputs[(i*4)+1];
2330 ir->outputs[(j*4)+2] = ir->outputs[(i*4)+2];
2331 ir->outputs[(j*4)+3] = ir->outputs[(i*4)+3];
2332 }
2333 j++;
2334 }
2335 }
2336 so->outputs_count = j;
2337 ir->noutputs = j * 4;
2338 }
2339
2340 /* if we want half-precision outputs, mark the output registers
2341 * as half:
2342 */
2343 if (so->key.half_precision) {
2344 for (i = 0; i < ir->noutputs; i++) {
2345 struct ir3_instruction *out = ir->outputs[i];
2346 if (!out)
2347 continue;
2348 out->regs[0]->flags |= IR3_REG_HALF;
2349 /* output could be a fanout (ie. texture fetch output)
2350 * in which case we need to propagate the half-reg flag
2351 * up to the definer so that RA sees it:
2352 */
2353 if (is_meta(out) && (out->opc == OPC_META_FO)) {
2354 out = out->regs[1]->instr;
2355 out->regs[0]->flags |= IR3_REG_HALF;
2356 }
2357
2358 if (out->category == 1) {
2359 out->cat1.dst_type = half_type(out->cat1.dst_type);
2360 }
2361 }
2362 }
2363
2364 /* at this point, we want the kill's in the outputs array too,
2365 * so that they get scheduled (since they have no dst).. we've
2366 * already ensured that the array is big enough in push_block():
2367 */
2368 if (so->type == SHADER_FRAGMENT) {
2369 for (i = 0; i < ctx->kill_count; i++)
2370 ir->outputs[ir->noutputs++] = ctx->kill[i];
2371 }
2372
2373 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2374 printf("BEFORE CP:\n");
2375 ir3_print(ir);
2376 }
2377
2378 ir3_cp(ir);
2379
2380 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2381 printf("BEFORE GROUPING:\n");
2382 ir3_print(ir);
2383 }
2384
2385 /* Group left/right neighbors, inserting mov's where needed to
2386 * solve conflicts:
2387 */
2388 ir3_group(ir);
2389
2390 ir3_depth(ir);
2391
2392 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2393 printf("AFTER DEPTH:\n");
2394 ir3_print(ir);
2395 }
2396
2397 ret = ir3_sched(ir);
2398 if (ret) {
2399 DBG("SCHED failed!");
2400 goto out;
2401 }
2402
2403 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2404 printf("AFTER SCHED:\n");
2405 ir3_print(ir);
2406 }
2407
2408 ret = ir3_ra(ir, so->type, so->frag_coord, so->frag_face);
2409 if (ret) {
2410 DBG("RA failed!");
2411 goto out;
2412 }
2413
2414 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2415 printf("AFTER RA:\n");
2416 ir3_print(ir);
2417 }
2418
2419 ir3_legalize(ir, &so->has_samp, &max_bary);
2420
2421 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2422 printf("AFTER LEGALIZE:\n");
2423 ir3_print(ir);
2424 }
2425
2426 /* fixup input/outputs: */
2427 for (i = 0; i < so->outputs_count; i++) {
2428 so->outputs[i].regid = ir->outputs[i*4]->regs[0]->num;
2429 /* preserve hack for depth output.. tgsi writes depth to .z,
2430 * but what we give the hw is the scalar register:
2431 */
2432 if ((so->type == SHADER_FRAGMENT) &&
2433 (sem2name(so->outputs[i].semantic) == TGSI_SEMANTIC_POSITION))
2434 so->outputs[i].regid += 2;
2435 }
2436
2437 /* Note that some or all channels of an input may be unused: */
2438 actual_in = 0;
2439 for (i = 0; i < so->inputs_count; i++) {
2440 unsigned j, regid = ~0, compmask = 0;
2441 so->inputs[i].ncomp = 0;
2442 for (j = 0; j < 4; j++) {
2443 struct ir3_instruction *in = inputs[(i*4) + j];
2444 if (in) {
2445 compmask |= (1 << j);
2446 regid = in->regs[0]->num - j;
2447 actual_in++;
2448 so->inputs[i].ncomp++;
2449 }
2450 }
2451 so->inputs[i].regid = regid;
2452 so->inputs[i].compmask = compmask;
2453 }
2454
2455 /* fragment shader always gets full vec4's even if it doesn't
2456 * fetch all components, but vertex shader we need to update
2457 * with the actual number of components fetch, otherwise thing
2458 * will hang due to mismaptch between VFD_DECODE's and
2459 * TOTALATTRTOVS
2460 */
2461 if (so->type == SHADER_VERTEX)
2462 so->total_in = actual_in;
2463 else
2464 so->total_in = align(max_bary + 1, 4);
2465
2466 out:
2467 if (ret) {
2468 if (so->ir)
2469 ir3_destroy(so->ir);
2470 so->ir = NULL;
2471 }
2472 compile_free(ctx);
2473
2474 return ret;
2475 }