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