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