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