glsl: move shader_enums into nir
[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 compile_error(ctx, "Unhandled NIR tex type: %d\n", tex->op);
1628 return;
1629 }
1630
1631 tex_info(tex, &flags, &coords);
1632
1633 /* scale up integer coords for TXF based on the LOD */
1634 if (ctx->unminify_coords && (opc == OPC_ISAML)) {
1635 assert(has_lod);
1636 for (i = 0; i < coords; i++)
1637 coord[i] = ir3_SHL_B(b, coord[i], 0, lod, 0);
1638 }
1639
1640 /* the array coord for cube arrays needs 0.5 added to it */
1641 if (tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE && tex->is_array &&
1642 opc != OPC_ISAML)
1643 coord[3] = ir3_ADD_F(b, coord[3], 0, create_immed(b, fui(0.5)), 0);
1644
1645 /*
1646 * lay out the first argument in the proper order:
1647 * - actual coordinates first
1648 * - shadow reference
1649 * - array index
1650 * - projection w
1651 * - starting at offset 4, dpdx.xy, dpdy.xy
1652 *
1653 * bias/lod go into the second arg
1654 */
1655
1656 /* insert tex coords: */
1657 for (i = 0; i < coords; i++)
1658 src0[nsrc0++] = coord[i];
1659
1660 if (coords == 1) {
1661 /* hw doesn't do 1d, so we treat it as 2d with
1662 * height of 1, and patch up the y coord.
1663 * TODO: y coord should be (int)0 in some cases..
1664 */
1665 src0[nsrc0++] = create_immed(b, fui(0.5));
1666 }
1667
1668 if (tex->is_shadow)
1669 src0[nsrc0++] = compare;
1670
1671 if (tex->is_array)
1672 src0[nsrc0++] = coord[coords];
1673
1674 if (has_proj) {
1675 src0[nsrc0++] = proj;
1676 flags |= IR3_INSTR_P;
1677 }
1678
1679 /* pad to 4, then ddx/ddy: */
1680 if (tex->op == nir_texop_txd) {
1681 while (nsrc0 < 4)
1682 src0[nsrc0++] = create_immed(b, fui(0.0));
1683 for (i = 0; i < coords; i++)
1684 src0[nsrc0++] = ddx[i];
1685 if (coords < 2)
1686 src0[nsrc0++] = create_immed(b, fui(0.0));
1687 for (i = 0; i < coords; i++)
1688 src0[nsrc0++] = ddy[i];
1689 if (coords < 2)
1690 src0[nsrc0++] = create_immed(b, fui(0.0));
1691 }
1692
1693 /*
1694 * second argument (if applicable):
1695 * - offsets
1696 * - lod
1697 * - bias
1698 */
1699 if (has_off | has_lod | has_bias) {
1700 if (has_off) {
1701 for (i = 0; i < coords; i++)
1702 src1[nsrc1++] = off[i];
1703 if (coords < 2)
1704 src1[nsrc1++] = create_immed(b, fui(0.0));
1705 flags |= IR3_INSTR_O;
1706 }
1707
1708 if (has_lod | has_bias)
1709 src1[nsrc1++] = lod;
1710 }
1711
1712 switch (tex->dest_type) {
1713 case nir_type_invalid:
1714 case nir_type_float:
1715 type = TYPE_F32;
1716 break;
1717 case nir_type_int:
1718 type = TYPE_S32;
1719 break;
1720 case nir_type_unsigned:
1721 case nir_type_bool:
1722 type = TYPE_U32;
1723 break;
1724 default:
1725 unreachable("bad dest_type");
1726 }
1727
1728 sam = ir3_SAM(b, opc, type, TGSI_WRITEMASK_XYZW,
1729 flags, tex->sampler_index, tex->sampler_index,
1730 create_collect(b, src0, nsrc0),
1731 create_collect(b, src1, nsrc1));
1732
1733 split_dest(b, dst, sam, 4);
1734 }
1735
1736 static void
1737 emit_tex_query_levels(struct ir3_compile *ctx, nir_tex_instr *tex)
1738 {
1739 struct ir3_block *b = ctx->block;
1740 struct ir3_instruction **dst, *sam;
1741
1742 dst = get_dst(ctx, &tex->dest, 1);
1743
1744 sam = ir3_SAM(b, OPC_GETINFO, TYPE_U32, TGSI_WRITEMASK_Z, 0,
1745 tex->sampler_index, tex->sampler_index, NULL, NULL);
1746
1747 /* even though there is only one component, since it ends
1748 * up in .z rather than .x, we need a split_dest()
1749 */
1750 split_dest(b, dst, sam, 3);
1751
1752 /* The # of levels comes from getinfo.z. We need to add 1 to it, since
1753 * the value in TEX_CONST_0 is zero-based.
1754 */
1755 if (ctx->levels_add_one)
1756 dst[0] = ir3_ADD_U(b, dst[0], 0, create_immed(b, 1), 0);
1757 }
1758
1759 static void
1760 emit_tex_txs(struct ir3_compile *ctx, nir_tex_instr *tex)
1761 {
1762 struct ir3_block *b = ctx->block;
1763 struct ir3_instruction **dst, *sam, *lod;
1764 unsigned flags, coords;
1765
1766 tex_info(tex, &flags, &coords);
1767
1768 /* Actually we want the number of dimensions, not coordinates. This
1769 * distinction only matters for cubes.
1770 */
1771 if (tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE)
1772 coords = 2;
1773
1774 dst = get_dst(ctx, &tex->dest, 4);
1775
1776 compile_assert(ctx, tex->num_srcs == 1);
1777 compile_assert(ctx, tex->src[0].src_type == nir_tex_src_lod);
1778
1779 lod = get_src(ctx, &tex->src[0].src)[0];
1780
1781 sam = ir3_SAM(b, OPC_GETSIZE, TYPE_U32, TGSI_WRITEMASK_XYZW, flags,
1782 tex->sampler_index, tex->sampler_index, lod, NULL);
1783
1784 split_dest(b, dst, sam, 4);
1785
1786 /* Array size actually ends up in .w rather than .z. This doesn't
1787 * matter for miplevel 0, but for higher mips the value in z is
1788 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
1789 * returned, which means that we have to add 1 to it for arrays.
1790 */
1791 if (tex->is_array) {
1792 if (ctx->levels_add_one) {
1793 dst[coords] = ir3_ADD_U(b, dst[3], 0, create_immed(b, 1), 0);
1794 } else {
1795 dst[coords] = ir3_MOV(b, dst[3], TYPE_U32);
1796 }
1797 }
1798 }
1799
1800 static void
1801 emit_phi(struct ir3_compile *ctx, nir_phi_instr *nphi)
1802 {
1803 struct ir3_instruction *phi, **dst;
1804
1805 /* NOTE: phi's should be lowered to scalar at this point */
1806 compile_assert(ctx, nphi->dest.ssa.num_components == 1);
1807
1808 dst = get_dst(ctx, &nphi->dest, 1);
1809
1810 phi = ir3_instr_create2(ctx->block, -1, OPC_META_PHI,
1811 1 + exec_list_length(&nphi->srcs));
1812 ir3_reg_create(phi, 0, 0); /* dst */
1813 phi->phi.nphi = nphi;
1814
1815 dst[0] = phi;
1816 }
1817
1818 /* phi instructions are left partially constructed. We don't resolve
1819 * their srcs until the end of the block, since (eg. loops) one of
1820 * the phi's srcs might be defined after the phi due to back edges in
1821 * the CFG.
1822 */
1823 static void
1824 resolve_phis(struct ir3_compile *ctx, struct ir3_block *block)
1825 {
1826 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
1827 nir_phi_instr *nphi;
1828
1829 /* phi's only come at start of block: */
1830 if (!(is_meta(instr) && (instr->opc == OPC_META_PHI)))
1831 break;
1832
1833 if (!instr->phi.nphi)
1834 break;
1835
1836 nphi = instr->phi.nphi;
1837 instr->phi.nphi = NULL;
1838
1839 foreach_list_typed(nir_phi_src, nsrc, node, &nphi->srcs) {
1840 struct ir3_instruction *src = get_src(ctx, &nsrc->src)[0];
1841 ir3_reg_create(instr, 0, IR3_REG_SSA)->instr = src;
1842 }
1843 }
1844
1845 resolve_array_phis(ctx, block);
1846 }
1847
1848 static void
1849 emit_jump(struct ir3_compile *ctx, nir_jump_instr *jump)
1850 {
1851 switch (jump->type) {
1852 case nir_jump_break:
1853 case nir_jump_continue:
1854 /* I *think* we can simply just ignore this, and use the
1855 * successor block link to figure out where we need to
1856 * jump to for break/continue
1857 */
1858 break;
1859 default:
1860 compile_error(ctx, "Unhandled NIR jump type: %d\n", jump->type);
1861 break;
1862 }
1863 }
1864
1865 static void
1866 emit_instr(struct ir3_compile *ctx, nir_instr *instr)
1867 {
1868 switch (instr->type) {
1869 case nir_instr_type_alu:
1870 emit_alu(ctx, nir_instr_as_alu(instr));
1871 break;
1872 case nir_instr_type_intrinsic:
1873 emit_intrinisic(ctx, nir_instr_as_intrinsic(instr));
1874 break;
1875 case nir_instr_type_load_const:
1876 emit_load_const(ctx, nir_instr_as_load_const(instr));
1877 break;
1878 case nir_instr_type_ssa_undef:
1879 emit_undef(ctx, nir_instr_as_ssa_undef(instr));
1880 break;
1881 case nir_instr_type_tex: {
1882 nir_tex_instr *tex = nir_instr_as_tex(instr);
1883 /* couple tex instructions get special-cased:
1884 */
1885 switch (tex->op) {
1886 case nir_texop_txs:
1887 emit_tex_txs(ctx, tex);
1888 break;
1889 case nir_texop_query_levels:
1890 emit_tex_query_levels(ctx, tex);
1891 break;
1892 default:
1893 emit_tex(ctx, tex);
1894 break;
1895 }
1896 break;
1897 }
1898 case nir_instr_type_phi:
1899 emit_phi(ctx, nir_instr_as_phi(instr));
1900 break;
1901 case nir_instr_type_jump:
1902 emit_jump(ctx, nir_instr_as_jump(instr));
1903 break;
1904 case nir_instr_type_call:
1905 case nir_instr_type_parallel_copy:
1906 compile_error(ctx, "Unhandled NIR instruction type: %d\n", instr->type);
1907 break;
1908 }
1909 }
1910
1911 static struct ir3_block *
1912 get_block(struct ir3_compile *ctx, nir_block *nblock)
1913 {
1914 struct ir3_block *block;
1915 struct hash_entry *entry;
1916 entry = _mesa_hash_table_search(ctx->block_ht, nblock);
1917 if (entry)
1918 return entry->data;
1919
1920 block = ir3_block_create(ctx->ir);
1921 block->nblock = nblock;
1922 _mesa_hash_table_insert(ctx->block_ht, nblock, block);
1923
1924 return block;
1925 }
1926
1927 static void
1928 emit_block(struct ir3_compile *ctx, nir_block *nblock)
1929 {
1930 struct ir3_block *block = get_block(ctx, nblock);
1931
1932 for (int i = 0; i < ARRAY_SIZE(block->successors); i++) {
1933 if (nblock->successors[i]) {
1934 block->successors[i] =
1935 get_block(ctx, nblock->successors[i]);
1936 }
1937 }
1938
1939 ctx->block = block;
1940 list_addtail(&block->node, &ctx->ir->block_list);
1941
1942 nir_foreach_instr(nblock, instr) {
1943 emit_instr(ctx, instr);
1944 if (ctx->error)
1945 return;
1946 }
1947 }
1948
1949 static void emit_cf_list(struct ir3_compile *ctx, struct exec_list *list);
1950
1951 static void
1952 emit_if(struct ir3_compile *ctx, nir_if *nif)
1953 {
1954 struct ir3_instruction *condition = get_src(ctx, &nif->condition)[0];
1955
1956 ctx->block->condition =
1957 get_predicate(ctx, ir3_b2n(condition->block, condition));
1958
1959 emit_cf_list(ctx, &nif->then_list);
1960 emit_cf_list(ctx, &nif->else_list);
1961 }
1962
1963 static void
1964 emit_loop(struct ir3_compile *ctx, nir_loop *nloop)
1965 {
1966 emit_cf_list(ctx, &nloop->body);
1967 }
1968
1969 static void
1970 emit_cf_list(struct ir3_compile *ctx, struct exec_list *list)
1971 {
1972 foreach_list_typed(nir_cf_node, node, node, list) {
1973 switch (node->type) {
1974 case nir_cf_node_block:
1975 emit_block(ctx, nir_cf_node_as_block(node));
1976 break;
1977 case nir_cf_node_if:
1978 emit_if(ctx, nir_cf_node_as_if(node));
1979 break;
1980 case nir_cf_node_loop:
1981 emit_loop(ctx, nir_cf_node_as_loop(node));
1982 break;
1983 case nir_cf_node_function:
1984 compile_error(ctx, "TODO\n");
1985 break;
1986 }
1987 }
1988 }
1989
1990 /* emit stream-out code. At this point, the current block is the original
1991 * (nir) end block, and nir ensures that all flow control paths terminate
1992 * into the end block. We re-purpose the original end block to generate
1993 * the 'if (vtxcnt < maxvtxcnt)' condition, then append the conditional
1994 * block holding stream-out write instructions, followed by the new end
1995 * block:
1996 *
1997 * blockOrigEnd {
1998 * p0.x = (vtxcnt < maxvtxcnt)
1999 * // succs: blockStreamOut, blockNewEnd
2000 * }
2001 * blockStreamOut {
2002 * ... stream-out instructions ...
2003 * // succs: blockNewEnd
2004 * }
2005 * blockNewEnd {
2006 * }
2007 */
2008 static void
2009 emit_stream_out(struct ir3_compile *ctx)
2010 {
2011 struct ir3_shader_variant *v = ctx->so;
2012 struct ir3 *ir = ctx->ir;
2013 struct pipe_stream_output_info *strmout =
2014 &ctx->so->shader->stream_output;
2015 struct ir3_block *orig_end_block, *stream_out_block, *new_end_block;
2016 struct ir3_instruction *vtxcnt, *maxvtxcnt, *cond;
2017 struct ir3_instruction *bases[PIPE_MAX_SO_BUFFERS];
2018
2019 /* create vtxcnt input in input block at top of shader,
2020 * so that it is seen as live over the entire duration
2021 * of the shader:
2022 */
2023 vtxcnt = create_input(ctx->in_block, 0);
2024 add_sysval_input(ctx, SYSTEM_VALUE_VERTEX_CNT, vtxcnt);
2025
2026 maxvtxcnt = create_driver_param(ctx, IR3_DP_VTXCNT_MAX);
2027
2028 /* at this point, we are at the original 'end' block,
2029 * re-purpose this block to stream-out condition, then
2030 * append stream-out block and new-end block
2031 */
2032 orig_end_block = ctx->block;
2033
2034 stream_out_block = ir3_block_create(ir);
2035 list_addtail(&stream_out_block->node, &ir->block_list);
2036
2037 new_end_block = ir3_block_create(ir);
2038 list_addtail(&new_end_block->node, &ir->block_list);
2039
2040 orig_end_block->successors[0] = stream_out_block;
2041 orig_end_block->successors[1] = new_end_block;
2042 stream_out_block->successors[0] = new_end_block;
2043
2044 /* setup 'if (vtxcnt < maxvtxcnt)' condition: */
2045 cond = ir3_CMPS_S(ctx->block, vtxcnt, 0, maxvtxcnt, 0);
2046 cond->regs[0]->num = regid(REG_P0, 0);
2047 cond->cat2.condition = IR3_COND_LT;
2048
2049 /* condition goes on previous block to the conditional,
2050 * since it is used to pick which of the two successor
2051 * paths to take:
2052 */
2053 orig_end_block->condition = cond;
2054
2055 /* switch to stream_out_block to generate the stream-out
2056 * instructions:
2057 */
2058 ctx->block = stream_out_block;
2059
2060 /* Calculate base addresses based on vtxcnt. Instructions
2061 * generated for bases not used in following loop will be
2062 * stripped out in the backend.
2063 */
2064 for (unsigned i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
2065 unsigned stride = strmout->stride[i];
2066 struct ir3_instruction *base, *off;
2067
2068 base = create_uniform(ctx, regid(v->first_driver_param + IR3_TFBOS_OFF, i));
2069
2070 /* 24-bit should be enough: */
2071 off = ir3_MUL_U(ctx->block, vtxcnt, 0,
2072 create_immed(ctx->block, stride * 4), 0);
2073
2074 bases[i] = ir3_ADD_S(ctx->block, off, 0, base, 0);
2075 }
2076
2077 /* Generate the per-output store instructions: */
2078 for (unsigned i = 0; i < strmout->num_outputs; i++) {
2079 for (unsigned j = 0; j < strmout->output[i].num_components; j++) {
2080 unsigned c = j + strmout->output[i].start_component;
2081 struct ir3_instruction *base, *out, *stg;
2082
2083 base = bases[strmout->output[i].output_buffer];
2084 out = ctx->ir->outputs[regid(strmout->output[i].register_index, c)];
2085
2086 stg = ir3_STG(ctx->block, base, 0, out, 0,
2087 create_immed(ctx->block, 1), 0);
2088 stg->cat6.type = TYPE_U32;
2089 stg->cat6.dst_offset = (strmout->output[i].dst_offset + j) * 4;
2090
2091 array_insert(ctx->ir->keeps, stg);
2092 }
2093 }
2094
2095 /* and finally switch to the new_end_block: */
2096 ctx->block = new_end_block;
2097 }
2098
2099 static void
2100 emit_function(struct ir3_compile *ctx, nir_function_impl *impl)
2101 {
2102 emit_cf_list(ctx, &impl->body);
2103 emit_block(ctx, impl->end_block);
2104
2105 /* at this point, we should have a single empty block,
2106 * into which we emit the 'end' instruction.
2107 */
2108 compile_assert(ctx, list_empty(&ctx->block->instr_list));
2109
2110 /* If stream-out (aka transform-feedback) enabled, emit the
2111 * stream-out instructions, followed by a new empty block (into
2112 * which the 'end' instruction lands).
2113 *
2114 * NOTE: it is done in this order, rather than inserting before
2115 * we emit end_block, because NIR guarantees that all blocks
2116 * flow into end_block, and that end_block has no successors.
2117 * So by re-purposing end_block as the first block of stream-
2118 * out, we guarantee that all exit paths flow into the stream-
2119 * out instructions.
2120 */
2121 if ((ctx->so->shader->stream_output.num_outputs > 0) &&
2122 !ctx->so->key.binning_pass) {
2123 debug_assert(ctx->so->type == SHADER_VERTEX);
2124 emit_stream_out(ctx);
2125 }
2126
2127 ir3_END(ctx->block);
2128 }
2129
2130 static void
2131 setup_input(struct ir3_compile *ctx, nir_variable *in)
2132 {
2133 struct ir3_shader_variant *so = ctx->so;
2134 unsigned array_len = MAX2(glsl_get_length(in->type), 1);
2135 unsigned ncomp = glsl_get_components(in->type);
2136 unsigned n = in->data.driver_location;
2137 unsigned slot = in->data.location;
2138
2139 DBG("; in: slot=%u, len=%ux%u, drvloc=%u",
2140 slot, array_len, ncomp, n);
2141
2142 so->inputs[n].slot = slot;
2143 so->inputs[n].compmask = (1 << ncomp) - 1;
2144 so->inputs[n].inloc = ctx->next_inloc;
2145 so->inputs[n].interpolate = INTERP_QUALIFIER_NONE;
2146 so->inputs_count = MAX2(so->inputs_count, n + 1);
2147 so->inputs[n].interpolate = in->data.interpolation;
2148
2149 if (ctx->so->type == SHADER_FRAGMENT) {
2150 for (int i = 0; i < ncomp; i++) {
2151 struct ir3_instruction *instr = NULL;
2152 unsigned idx = (n * 4) + i;
2153
2154 if (slot == VARYING_SLOT_POS) {
2155 so->inputs[n].bary = false;
2156 so->frag_coord = true;
2157 instr = create_frag_coord(ctx, i);
2158 } else if (slot == VARYING_SLOT_FACE) {
2159 so->inputs[n].bary = false;
2160 so->frag_face = true;
2161 instr = create_frag_face(ctx, i);
2162 } else {
2163 bool use_ldlv = false;
2164
2165 /* detect the special case for front/back colors where
2166 * we need to do flat vs smooth shading depending on
2167 * rast state:
2168 */
2169 if (in->data.interpolation == INTERP_QUALIFIER_NONE) {
2170 switch (slot) {
2171 case VARYING_SLOT_COL0:
2172 case VARYING_SLOT_COL1:
2173 case VARYING_SLOT_BFC0:
2174 case VARYING_SLOT_BFC1:
2175 so->inputs[n].rasterflat = true;
2176 break;
2177 default:
2178 break;
2179 }
2180 }
2181
2182 if (ctx->flat_bypass) {
2183 if ((so->inputs[n].interpolate == INTERP_QUALIFIER_FLAT) ||
2184 (so->inputs[n].rasterflat && ctx->so->key.rasterflat))
2185 use_ldlv = true;
2186 }
2187
2188 so->inputs[n].bary = true;
2189
2190 instr = create_frag_input(ctx,
2191 so->inputs[n].inloc + i - 8, use_ldlv);
2192 }
2193
2194 ctx->ir->inputs[idx] = instr;
2195 }
2196 } else if (ctx->so->type == SHADER_VERTEX) {
2197 for (int i = 0; i < ncomp; i++) {
2198 unsigned idx = (n * 4) + i;
2199 ctx->ir->inputs[idx] = create_input(ctx->block, idx);
2200 }
2201 } else {
2202 compile_error(ctx, "unknown shader type: %d\n", ctx->so->type);
2203 }
2204
2205 if (so->inputs[n].bary || (ctx->so->type == SHADER_VERTEX)) {
2206 ctx->next_inloc += ncomp;
2207 so->total_in += ncomp;
2208 }
2209 }
2210
2211 static void
2212 setup_output(struct ir3_compile *ctx, nir_variable *out)
2213 {
2214 struct ir3_shader_variant *so = ctx->so;
2215 unsigned array_len = MAX2(glsl_get_length(out->type), 1);
2216 unsigned ncomp = glsl_get_components(out->type);
2217 unsigned n = out->data.driver_location;
2218 unsigned slot = out->data.location;
2219 unsigned comp = 0;
2220
2221 DBG("; out: slot=%u, len=%ux%u, drvloc=%u",
2222 slot, array_len, ncomp, n);
2223
2224 if (ctx->so->type == SHADER_FRAGMENT) {
2225 switch (slot) {
2226 case FRAG_RESULT_DEPTH:
2227 comp = 2; /* tgsi will write to .z component */
2228 so->writes_pos = true;
2229 break;
2230 case FRAG_RESULT_COLOR:
2231 so->color0_mrt = 1;
2232 break;
2233 default:
2234 if (slot >= FRAG_RESULT_DATA0)
2235 break;
2236 compile_error(ctx, "unknown FS output name: %s\n",
2237 gl_frag_result_name(slot));
2238 }
2239 } else if (ctx->so->type == SHADER_VERTEX) {
2240 switch (slot) {
2241 case VARYING_SLOT_POS:
2242 so->writes_pos = true;
2243 break;
2244 case VARYING_SLOT_PSIZ:
2245 so->writes_psize = true;
2246 break;
2247 case VARYING_SLOT_COL0:
2248 case VARYING_SLOT_COL1:
2249 case VARYING_SLOT_BFC0:
2250 case VARYING_SLOT_BFC1:
2251 case VARYING_SLOT_FOGC:
2252 case VARYING_SLOT_CLIP_DIST0:
2253 case VARYING_SLOT_CLIP_DIST1:
2254 break;
2255 default:
2256 if (slot >= VARYING_SLOT_VAR0)
2257 break;
2258 if ((VARYING_SLOT_TEX0 <= slot) && (slot <= VARYING_SLOT_TEX7))
2259 break;
2260 compile_error(ctx, "unknown VS output name: %s\n",
2261 gl_varying_slot_name(slot));
2262 }
2263 } else {
2264 compile_error(ctx, "unknown shader type: %d\n", ctx->so->type);
2265 }
2266
2267 compile_assert(ctx, n < ARRAY_SIZE(so->outputs));
2268
2269 so->outputs[n].slot = slot;
2270 so->outputs[n].regid = regid(n, comp);
2271 so->outputs_count = MAX2(so->outputs_count, n + 1);
2272
2273 for (int i = 0; i < ncomp; i++) {
2274 unsigned idx = (n * 4) + i;
2275
2276 ctx->ir->outputs[idx] = create_immed(ctx->block, fui(0.0));
2277 }
2278 }
2279
2280 static void
2281 emit_instructions(struct ir3_compile *ctx)
2282 {
2283 unsigned ninputs, noutputs;
2284 nir_function_impl *fxn = NULL;
2285
2286 /* Find the main function: */
2287 nir_foreach_overload(ctx->s, overload) {
2288 compile_assert(ctx, strcmp(overload->function->name, "main") == 0);
2289 compile_assert(ctx, overload->impl);
2290 fxn = overload->impl;
2291 break;
2292 }
2293
2294 ninputs = exec_list_length(&ctx->s->inputs) * 4;
2295 noutputs = exec_list_length(&ctx->s->outputs) * 4;
2296
2297 /* or vtx shaders, we need to leave room for sysvals:
2298 */
2299 if (ctx->so->type == SHADER_VERTEX) {
2300 ninputs += 8;
2301 }
2302
2303 ctx->ir = ir3_create(ctx->compiler, ninputs, noutputs);
2304
2305 /* Create inputs in first block: */
2306 ctx->block = get_block(ctx, nir_start_block(fxn));
2307 ctx->in_block = ctx->block;
2308 list_addtail(&ctx->block->node, &ctx->ir->block_list);
2309
2310 if (ctx->so->type == SHADER_VERTEX) {
2311 ctx->ir->ninputs -= 8;
2312 }
2313
2314 /* for fragment shader, we have a single input register (usually
2315 * r0.xy) which is used as the base for bary.f varying fetch instrs:
2316 */
2317 if (ctx->so->type == SHADER_FRAGMENT) {
2318 // TODO maybe a helper for fi since we need it a few places..
2319 struct ir3_instruction *instr;
2320 instr = ir3_instr_create(ctx->block, -1, OPC_META_FI);
2321 ir3_reg_create(instr, 0, 0);
2322 ir3_reg_create(instr, 0, IR3_REG_SSA); /* r0.x */
2323 ir3_reg_create(instr, 0, IR3_REG_SSA); /* r0.y */
2324 ctx->frag_pos = instr;
2325 }
2326
2327 /* Setup inputs: */
2328 foreach_list_typed(nir_variable, var, node, &ctx->s->inputs) {
2329 setup_input(ctx, var);
2330 }
2331
2332 /* Setup outputs: */
2333 foreach_list_typed(nir_variable, var, node, &ctx->s->outputs) {
2334 setup_output(ctx, var);
2335 }
2336
2337 /* Setup variables (which should only be arrays): */
2338 foreach_list_typed(nir_variable, var, node, &ctx->s->globals) {
2339 declare_var(ctx, var);
2340 }
2341
2342 /* And emit the body: */
2343 ctx->impl = fxn;
2344 emit_function(ctx, fxn);
2345
2346 list_for_each_entry (struct ir3_block, block, &ctx->ir->block_list, node) {
2347 resolve_phis(ctx, block);
2348 }
2349 }
2350
2351 /* from NIR perspective, we actually have inputs. But most of the "inputs"
2352 * for a fragment shader are just bary.f instructions. The *actual* inputs
2353 * from the hw perspective are the frag_pos and optionally frag_coord and
2354 * frag_face.
2355 */
2356 static void
2357 fixup_frag_inputs(struct ir3_compile *ctx)
2358 {
2359 struct ir3_shader_variant *so = ctx->so;
2360 struct ir3 *ir = ctx->ir;
2361 struct ir3_instruction **inputs;
2362 struct ir3_instruction *instr;
2363 int n, regid = 0;
2364
2365 ir->ninputs = 0;
2366
2367 n = 4; /* always have frag_pos */
2368 n += COND(so->frag_face, 4);
2369 n += COND(so->frag_coord, 4);
2370
2371 inputs = ir3_alloc(ctx->ir, n * (sizeof(struct ir3_instruction *)));
2372
2373 if (so->frag_face) {
2374 /* this ultimately gets assigned to hr0.x so doesn't conflict
2375 * with frag_coord/frag_pos..
2376 */
2377 inputs[ir->ninputs++] = ctx->frag_face;
2378 ctx->frag_face->regs[0]->num = 0;
2379
2380 /* remaining channels not used, but let's avoid confusing
2381 * other parts that expect inputs to come in groups of vec4
2382 */
2383 inputs[ir->ninputs++] = NULL;
2384 inputs[ir->ninputs++] = NULL;
2385 inputs[ir->ninputs++] = NULL;
2386 }
2387
2388 /* since we don't know where to set the regid for frag_coord,
2389 * we have to use r0.x for it. But we don't want to *always*
2390 * use r1.x for frag_pos as that could increase the register
2391 * footprint on simple shaders:
2392 */
2393 if (so->frag_coord) {
2394 ctx->frag_coord[0]->regs[0]->num = regid++;
2395 ctx->frag_coord[1]->regs[0]->num = regid++;
2396 ctx->frag_coord[2]->regs[0]->num = regid++;
2397 ctx->frag_coord[3]->regs[0]->num = regid++;
2398
2399 inputs[ir->ninputs++] = ctx->frag_coord[0];
2400 inputs[ir->ninputs++] = ctx->frag_coord[1];
2401 inputs[ir->ninputs++] = ctx->frag_coord[2];
2402 inputs[ir->ninputs++] = ctx->frag_coord[3];
2403 }
2404
2405 /* we always have frag_pos: */
2406 so->pos_regid = regid;
2407
2408 /* r0.x */
2409 instr = create_input(ctx->in_block, ir->ninputs);
2410 instr->regs[0]->num = regid++;
2411 inputs[ir->ninputs++] = instr;
2412 ctx->frag_pos->regs[1]->instr = instr;
2413
2414 /* r0.y */
2415 instr = create_input(ctx->in_block, ir->ninputs);
2416 instr->regs[0]->num = regid++;
2417 inputs[ir->ninputs++] = instr;
2418 ctx->frag_pos->regs[2]->instr = instr;
2419
2420 ir->inputs = inputs;
2421 }
2422
2423 int
2424 ir3_compile_shader_nir(struct ir3_compiler *compiler,
2425 struct ir3_shader_variant *so)
2426 {
2427 struct ir3_compile *ctx;
2428 struct ir3 *ir;
2429 struct ir3_instruction **inputs;
2430 unsigned i, j, actual_in;
2431 int ret = 0, max_bary;
2432
2433 assert(!so->ir);
2434
2435 ctx = compile_init(compiler, so, so->shader->tokens);
2436 if (!ctx) {
2437 DBG("INIT failed!");
2438 ret = -1;
2439 goto out;
2440 }
2441
2442 emit_instructions(ctx);
2443
2444 if (ctx->error) {
2445 DBG("EMIT failed!");
2446 ret = -1;
2447 goto out;
2448 }
2449
2450 ir = so->ir = ctx->ir;
2451
2452 /* keep track of the inputs from TGSI perspective.. */
2453 inputs = ir->inputs;
2454
2455 /* but fixup actual inputs for frag shader: */
2456 if (so->type == SHADER_FRAGMENT)
2457 fixup_frag_inputs(ctx);
2458
2459 /* at this point, for binning pass, throw away unneeded outputs: */
2460 if (so->key.binning_pass) {
2461 for (i = 0, j = 0; i < so->outputs_count; i++) {
2462 unsigned slot = so->outputs[i].slot;
2463
2464 /* throw away everything but first position/psize */
2465 if ((slot == VARYING_SLOT_POS) || (slot == VARYING_SLOT_PSIZ)) {
2466 if (i != j) {
2467 so->outputs[j] = so->outputs[i];
2468 ir->outputs[(j*4)+0] = ir->outputs[(i*4)+0];
2469 ir->outputs[(j*4)+1] = ir->outputs[(i*4)+1];
2470 ir->outputs[(j*4)+2] = ir->outputs[(i*4)+2];
2471 ir->outputs[(j*4)+3] = ir->outputs[(i*4)+3];
2472 }
2473 j++;
2474 }
2475 }
2476 so->outputs_count = j;
2477 ir->noutputs = j * 4;
2478 }
2479
2480 /* if we want half-precision outputs, mark the output registers
2481 * as half:
2482 */
2483 if (so->key.half_precision) {
2484 for (i = 0; i < ir->noutputs; i++) {
2485 struct ir3_instruction *out = ir->outputs[i];
2486 if (!out)
2487 continue;
2488 out->regs[0]->flags |= IR3_REG_HALF;
2489 /* output could be a fanout (ie. texture fetch output)
2490 * in which case we need to propagate the half-reg flag
2491 * up to the definer so that RA sees it:
2492 */
2493 if (is_meta(out) && (out->opc == OPC_META_FO)) {
2494 out = out->regs[1]->instr;
2495 out->regs[0]->flags |= IR3_REG_HALF;
2496 }
2497
2498 if (out->category == 1) {
2499 out->cat1.dst_type = half_type(out->cat1.dst_type);
2500 }
2501 }
2502 }
2503
2504 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2505 printf("BEFORE CP:\n");
2506 ir3_print(ir);
2507 }
2508
2509 ir3_cp(ir);
2510
2511 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2512 printf("BEFORE GROUPING:\n");
2513 ir3_print(ir);
2514 }
2515
2516 /* Group left/right neighbors, inserting mov's where needed to
2517 * solve conflicts:
2518 */
2519 ir3_group(ir);
2520
2521 ir3_depth(ir);
2522
2523 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2524 printf("AFTER DEPTH:\n");
2525 ir3_print(ir);
2526 }
2527
2528 ret = ir3_sched(ir);
2529 if (ret) {
2530 DBG("SCHED failed!");
2531 goto out;
2532 }
2533
2534 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2535 printf("AFTER SCHED:\n");
2536 ir3_print(ir);
2537 }
2538
2539 ret = ir3_ra(ir, so->type, so->frag_coord, so->frag_face);
2540 if (ret) {
2541 DBG("RA failed!");
2542 goto out;
2543 }
2544
2545 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2546 printf("AFTER RA:\n");
2547 ir3_print(ir);
2548 }
2549
2550 ir3_legalize(ir, &so->has_samp, &max_bary);
2551
2552 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2553 printf("AFTER LEGALIZE:\n");
2554 ir3_print(ir);
2555 }
2556
2557 /* fixup input/outputs: */
2558 for (i = 0; i < so->outputs_count; i++) {
2559 so->outputs[i].regid = ir->outputs[i*4]->regs[0]->num;
2560 /* preserve hack for depth output.. tgsi writes depth to .z,
2561 * but what we give the hw is the scalar register:
2562 */
2563 if ((so->type == SHADER_FRAGMENT) &&
2564 (so->outputs[i].slot == FRAG_RESULT_DEPTH))
2565 so->outputs[i].regid += 2;
2566 }
2567
2568 /* Note that some or all channels of an input may be unused: */
2569 actual_in = 0;
2570 for (i = 0; i < so->inputs_count; i++) {
2571 unsigned j, regid = ~0, compmask = 0;
2572 so->inputs[i].ncomp = 0;
2573 for (j = 0; j < 4; j++) {
2574 struct ir3_instruction *in = inputs[(i*4) + j];
2575 if (in) {
2576 compmask |= (1 << j);
2577 regid = in->regs[0]->num - j;
2578 actual_in++;
2579 so->inputs[i].ncomp++;
2580 }
2581 }
2582 so->inputs[i].regid = regid;
2583 so->inputs[i].compmask = compmask;
2584 }
2585
2586 /* fragment shader always gets full vec4's even if it doesn't
2587 * fetch all components, but vertex shader we need to update
2588 * with the actual number of components fetch, otherwise thing
2589 * will hang due to mismaptch between VFD_DECODE's and
2590 * TOTALATTRTOVS
2591 */
2592 if (so->type == SHADER_VERTEX)
2593 so->total_in = actual_in;
2594 else
2595 so->total_in = align(max_bary + 1, 4);
2596
2597 out:
2598 if (ret) {
2599 if (so->ir)
2600 ir3_destroy(so->ir);
2601 so->ir = NULL;
2602 }
2603 compile_free(ctx);
2604
2605 return ret;
2606 }