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