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