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