freedreno/ir3: fix load_front_face conversion
[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 * 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_compile *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_compile *ctx, nir_block *nblock);
156
157
158 static struct ir3_compile *
159 compile_init(struct ir3_compiler *compiler,
160 struct ir3_shader_variant *so)
161 {
162 struct ir3_compile *ctx = rzalloc(NULL, struct ir3_compile);
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_compile *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_compile *ctx)
288 {
289 ralloc_free(ctx);
290 }
291
292 static void
293 declare_array(struct ir3_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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_compile *ctx, struct ir3_instruction *src, int align);
411
412 static struct ir3_instruction * const *
413 get_src(struct ir3_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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_compile *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 dst = get_dst(ctx, &intr->dest, intr->num_components);
1422 } else {
1423 dst = NULL;
1424 }
1425
1426 switch (intr->intrinsic) {
1427 case nir_intrinsic_load_uniform:
1428 idx = nir_intrinsic_base(intr);
1429 const_offset = nir_src_as_const_value(intr->src[0]);
1430 if (const_offset) {
1431 idx += const_offset->u32[0];
1432 for (int i = 0; i < intr->num_components; i++) {
1433 unsigned n = idx * 4 + i;
1434 dst[i] = create_uniform(ctx, n);
1435 }
1436 } else {
1437 src = get_src(ctx, &intr->src[0]);
1438 for (int i = 0; i < intr->num_components; i++) {
1439 int n = idx * 4 + i;
1440 dst[i] = create_uniform_indirect(ctx, n,
1441 get_addr(ctx, src[0], 4));
1442 }
1443 /* NOTE: if relative addressing is used, we set
1444 * constlen in the compiler (to worst-case value)
1445 * since we don't know in the assembler what the max
1446 * addr reg value can be:
1447 */
1448 ctx->so->constlen = ctx->s->num_uniforms;
1449 }
1450 break;
1451 case nir_intrinsic_load_ubo:
1452 emit_intrinsic_load_ubo(ctx, intr, dst);
1453 break;
1454 case nir_intrinsic_load_input:
1455 idx = nir_intrinsic_base(intr);
1456 const_offset = nir_src_as_const_value(intr->src[0]);
1457 if (const_offset) {
1458 idx += const_offset->u32[0];
1459 for (int i = 0; i < intr->num_components; i++) {
1460 unsigned n = idx * 4 + i;
1461 dst[i] = ctx->ir->inputs[n];
1462 }
1463 } else {
1464 src = get_src(ctx, &intr->src[0]);
1465 struct ir3_instruction *collect =
1466 create_collect(b, ctx->ir->inputs, ctx->ir->ninputs);
1467 struct ir3_instruction *addr = get_addr(ctx, src[0], 4);
1468 for (int i = 0; i < intr->num_components; i++) {
1469 unsigned n = idx * 4 + i;
1470 dst[i] = create_indirect_load(ctx, ctx->ir->ninputs,
1471 n, addr, collect);
1472 }
1473 }
1474 break;
1475 case nir_intrinsic_load_ssbo:
1476 emit_intrinsic_load_ssbo(ctx, intr, dst);
1477 break;
1478 case nir_intrinsic_store_ssbo:
1479 emit_intrinsic_store_ssbo(ctx, intr);
1480 break;
1481 case nir_intrinsic_ssbo_atomic_add:
1482 case nir_intrinsic_ssbo_atomic_imin:
1483 case nir_intrinsic_ssbo_atomic_umin:
1484 case nir_intrinsic_ssbo_atomic_imax:
1485 case nir_intrinsic_ssbo_atomic_umax:
1486 case nir_intrinsic_ssbo_atomic_and:
1487 case nir_intrinsic_ssbo_atomic_or:
1488 case nir_intrinsic_ssbo_atomic_xor:
1489 case nir_intrinsic_ssbo_atomic_exchange:
1490 case nir_intrinsic_ssbo_atomic_comp_swap:
1491 if (info->has_dest) {
1492 compile_assert(ctx, intr->num_components == 1);
1493 dst[0] = emit_intrinsic_atomic(ctx, intr);
1494 } else {
1495 emit_intrinsic_atomic(ctx, intr);
1496 }
1497 break;
1498 case nir_intrinsic_store_output:
1499 idx = nir_intrinsic_base(intr);
1500 const_offset = nir_src_as_const_value(intr->src[1]);
1501 compile_assert(ctx, const_offset != NULL);
1502 idx += const_offset->u32[0];
1503
1504 src = get_src(ctx, &intr->src[0]);
1505 for (int i = 0; i < intr->num_components; i++) {
1506 unsigned n = idx * 4 + i;
1507 ctx->ir->outputs[n] = src[i];
1508 }
1509 break;
1510 case nir_intrinsic_load_base_vertex:
1511 if (!ctx->basevertex) {
1512 ctx->basevertex = create_driver_param(ctx, IR3_DP_VTXID_BASE);
1513 add_sysval_input(ctx, SYSTEM_VALUE_BASE_VERTEX,
1514 ctx->basevertex);
1515 }
1516 dst[0] = ctx->basevertex;
1517 break;
1518 case nir_intrinsic_load_vertex_id_zero_base:
1519 case nir_intrinsic_load_vertex_id:
1520 if (!ctx->vertex_id) {
1521 gl_system_value sv = (intr->intrinsic == nir_intrinsic_load_vertex_id) ?
1522 SYSTEM_VALUE_VERTEX_ID : SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
1523 ctx->vertex_id = create_input(b, 0);
1524 add_sysval_input(ctx, sv, ctx->vertex_id);
1525 }
1526 dst[0] = ctx->vertex_id;
1527 break;
1528 case nir_intrinsic_load_instance_id:
1529 if (!ctx->instance_id) {
1530 ctx->instance_id = create_input(b, 0);
1531 add_sysval_input(ctx, SYSTEM_VALUE_INSTANCE_ID,
1532 ctx->instance_id);
1533 }
1534 dst[0] = ctx->instance_id;
1535 break;
1536 case nir_intrinsic_load_user_clip_plane:
1537 idx = nir_intrinsic_ucp_id(intr);
1538 for (int i = 0; i < intr->num_components; i++) {
1539 unsigned n = idx * 4 + i;
1540 dst[i] = create_driver_param(ctx, IR3_DP_UCP0_X + n);
1541 }
1542 break;
1543 case nir_intrinsic_load_front_face:
1544 if (!ctx->frag_face) {
1545 ctx->so->frag_face = true;
1546 ctx->frag_face = create_input(b, 0);
1547 ctx->frag_face->regs[0]->flags |= IR3_REG_HALF;
1548 }
1549 /* for fragface, we get -1 for back and 0 for front. However this is
1550 * the inverse of what nir expects (where ~0 is true).
1551 */
1552 dst[0] = ir3_COV(b, ctx->frag_face, TYPE_S16, TYPE_S32);
1553 dst[0] = ir3_NOT_B(b, dst[0], 0);
1554 break;
1555 case nir_intrinsic_load_local_invocation_id:
1556 if (!ctx->local_invocation_id) {
1557 ctx->local_invocation_id = create_input_compmask(b, 0, 0x7);
1558 add_sysval_input_compmask(ctx, SYSTEM_VALUE_LOCAL_INVOCATION_ID,
1559 0x7, ctx->local_invocation_id);
1560 }
1561 split_dest(b, dst, ctx->local_invocation_id, 0, 3);
1562 break;
1563 case nir_intrinsic_load_work_group_id:
1564 if (!ctx->work_group_id) {
1565 ctx->work_group_id = create_input_compmask(b, 0, 0x7);
1566 add_sysval_input_compmask(ctx, SYSTEM_VALUE_WORK_GROUP_ID,
1567 0x7, ctx->work_group_id);
1568 ctx->work_group_id->regs[0]->flags |= IR3_REG_HIGH;
1569 }
1570 split_dest(b, dst, ctx->work_group_id, 0, 3);
1571 break;
1572 case nir_intrinsic_load_num_work_groups:
1573 for (int i = 0; i < intr->num_components; i++) {
1574 dst[i] = create_driver_param(ctx, IR3_DP_NUM_WORK_GROUPS_X + i);
1575 }
1576 break;
1577 case nir_intrinsic_discard_if:
1578 case nir_intrinsic_discard: {
1579 struct ir3_instruction *cond, *kill;
1580
1581 if (intr->intrinsic == nir_intrinsic_discard_if) {
1582 /* conditional discard: */
1583 src = get_src(ctx, &intr->src[0]);
1584 cond = ir3_b2n(b, src[0]);
1585 } else {
1586 /* unconditional discard: */
1587 cond = create_immed(b, 1);
1588 }
1589
1590 /* NOTE: only cmps.*.* can write p0.x: */
1591 cond = ir3_CMPS_S(b, cond, 0, create_immed(b, 0), 0);
1592 cond->cat2.condition = IR3_COND_NE;
1593
1594 /* condition always goes in predicate register: */
1595 cond->regs[0]->num = regid(REG_P0, 0);
1596
1597 kill = ir3_KILL(b, cond, 0);
1598 array_insert(ctx->ir, ctx->ir->predicates, kill);
1599
1600 array_insert(b, b->keeps, kill);
1601 ctx->so->has_kill = true;
1602
1603 break;
1604 }
1605 default:
1606 compile_error(ctx, "Unhandled intrinsic type: %s\n",
1607 nir_intrinsic_infos[intr->intrinsic].name);
1608 break;
1609 }
1610
1611 if (info->has_dest)
1612 put_dst(ctx, &intr->dest);
1613 }
1614
1615 static void
1616 emit_load_const(struct ir3_compile *ctx, nir_load_const_instr *instr)
1617 {
1618 struct ir3_instruction **dst = get_dst_ssa(ctx, &instr->def,
1619 instr->def.num_components);
1620 for (int i = 0; i < instr->def.num_components; i++)
1621 dst[i] = create_immed(ctx->block, instr->value.u32[i]);
1622 }
1623
1624 static void
1625 emit_undef(struct ir3_compile *ctx, nir_ssa_undef_instr *undef)
1626 {
1627 struct ir3_instruction **dst = get_dst_ssa(ctx, &undef->def,
1628 undef->def.num_components);
1629 /* backend doesn't want undefined instructions, so just plug
1630 * in 0.0..
1631 */
1632 for (int i = 0; i < undef->def.num_components; i++)
1633 dst[i] = create_immed(ctx->block, fui(0.0));
1634 }
1635
1636 /*
1637 * texture fetch/sample instructions:
1638 */
1639
1640 static void
1641 tex_info(nir_tex_instr *tex, unsigned *flagsp, unsigned *coordsp)
1642 {
1643 unsigned coords, flags = 0;
1644
1645 /* note: would use tex->coord_components.. except txs.. also,
1646 * since array index goes after shadow ref, we don't want to
1647 * count it:
1648 */
1649 switch (tex->sampler_dim) {
1650 case GLSL_SAMPLER_DIM_1D:
1651 case GLSL_SAMPLER_DIM_BUF:
1652 coords = 1;
1653 break;
1654 case GLSL_SAMPLER_DIM_2D:
1655 case GLSL_SAMPLER_DIM_RECT:
1656 case GLSL_SAMPLER_DIM_EXTERNAL:
1657 case GLSL_SAMPLER_DIM_MS:
1658 coords = 2;
1659 break;
1660 case GLSL_SAMPLER_DIM_3D:
1661 case GLSL_SAMPLER_DIM_CUBE:
1662 coords = 3;
1663 flags |= IR3_INSTR_3D;
1664 break;
1665 default:
1666 unreachable("bad sampler_dim");
1667 }
1668
1669 if (tex->is_shadow && tex->op != nir_texop_lod)
1670 flags |= IR3_INSTR_S;
1671
1672 if (tex->is_array && tex->op != nir_texop_lod)
1673 flags |= IR3_INSTR_A;
1674
1675 *flagsp = flags;
1676 *coordsp = coords;
1677 }
1678
1679 static void
1680 emit_tex(struct ir3_compile *ctx, nir_tex_instr *tex)
1681 {
1682 struct ir3_block *b = ctx->block;
1683 struct ir3_instruction **dst, *sam, *src0[12], *src1[4];
1684 struct ir3_instruction * const *coord, * const *off, * const *ddx, * const *ddy;
1685 struct ir3_instruction *lod, *compare, *proj;
1686 bool has_bias = false, has_lod = false, has_proj = false, has_off = false;
1687 unsigned i, coords, flags;
1688 unsigned nsrc0 = 0, nsrc1 = 0;
1689 type_t type;
1690 opc_t opc = 0;
1691
1692 coord = off = ddx = ddy = NULL;
1693 lod = proj = compare = NULL;
1694
1695 /* TODO: might just be one component for gathers? */
1696 dst = get_dst(ctx, &tex->dest, 4);
1697
1698 for (unsigned i = 0; i < tex->num_srcs; i++) {
1699 switch (tex->src[i].src_type) {
1700 case nir_tex_src_coord:
1701 coord = get_src(ctx, &tex->src[i].src);
1702 break;
1703 case nir_tex_src_bias:
1704 lod = get_src(ctx, &tex->src[i].src)[0];
1705 has_bias = true;
1706 break;
1707 case nir_tex_src_lod:
1708 lod = get_src(ctx, &tex->src[i].src)[0];
1709 has_lod = true;
1710 break;
1711 case nir_tex_src_comparator: /* shadow comparator */
1712 compare = get_src(ctx, &tex->src[i].src)[0];
1713 break;
1714 case nir_tex_src_projector:
1715 proj = get_src(ctx, &tex->src[i].src)[0];
1716 has_proj = true;
1717 break;
1718 case nir_tex_src_offset:
1719 off = get_src(ctx, &tex->src[i].src);
1720 has_off = true;
1721 break;
1722 case nir_tex_src_ddx:
1723 ddx = get_src(ctx, &tex->src[i].src);
1724 break;
1725 case nir_tex_src_ddy:
1726 ddy = get_src(ctx, &tex->src[i].src);
1727 break;
1728 default:
1729 compile_error(ctx, "Unhandled NIR tex src type: %d\n",
1730 tex->src[i].src_type);
1731 return;
1732 }
1733 }
1734
1735 switch (tex->op) {
1736 case nir_texop_tex: opc = OPC_SAM; break;
1737 case nir_texop_txb: opc = OPC_SAMB; break;
1738 case nir_texop_txl: opc = OPC_SAML; break;
1739 case nir_texop_txd: opc = OPC_SAMGQ; break;
1740 case nir_texop_txf: opc = OPC_ISAML; break;
1741 case nir_texop_lod: opc = OPC_GETLOD; break;
1742 case nir_texop_txf_ms:
1743 case nir_texop_txs:
1744 case nir_texop_tg4:
1745 case nir_texop_query_levels:
1746 case nir_texop_texture_samples:
1747 case nir_texop_samples_identical:
1748 case nir_texop_txf_ms_mcs:
1749 compile_error(ctx, "Unhandled NIR tex type: %d\n", tex->op);
1750 return;
1751 }
1752
1753 tex_info(tex, &flags, &coords);
1754
1755 /*
1756 * lay out the first argument in the proper order:
1757 * - actual coordinates first
1758 * - shadow reference
1759 * - array index
1760 * - projection w
1761 * - starting at offset 4, dpdx.xy, dpdy.xy
1762 *
1763 * bias/lod go into the second arg
1764 */
1765
1766 /* insert tex coords: */
1767 for (i = 0; i < coords; i++)
1768 src0[i] = coord[i];
1769
1770 nsrc0 = i;
1771
1772 /* scale up integer coords for TXF based on the LOD */
1773 if (ctx->unminify_coords && (opc == OPC_ISAML)) {
1774 assert(has_lod);
1775 for (i = 0; i < coords; i++)
1776 src0[i] = ir3_SHL_B(b, src0[i], 0, lod, 0);
1777 }
1778
1779 if (coords == 1) {
1780 /* hw doesn't do 1d, so we treat it as 2d with
1781 * height of 1, and patch up the y coord.
1782 * TODO: y coord should be (int)0 in some cases..
1783 */
1784 src0[nsrc0++] = create_immed(b, fui(0.5));
1785 }
1786
1787 if (tex->is_shadow && tex->op != nir_texop_lod)
1788 src0[nsrc0++] = compare;
1789
1790 if (tex->is_array && tex->op != nir_texop_lod) {
1791 struct ir3_instruction *idx = coord[coords];
1792
1793 /* the array coord for cube arrays needs 0.5 added to it */
1794 if (ctx->array_index_add_half && (opc != OPC_ISAML))
1795 idx = ir3_ADD_F(b, idx, 0, create_immed(b, fui(0.5)), 0);
1796
1797 src0[nsrc0++] = idx;
1798 }
1799
1800 if (has_proj) {
1801 src0[nsrc0++] = proj;
1802 flags |= IR3_INSTR_P;
1803 }
1804
1805 /* pad to 4, then ddx/ddy: */
1806 if (tex->op == nir_texop_txd) {
1807 while (nsrc0 < 4)
1808 src0[nsrc0++] = create_immed(b, fui(0.0));
1809 for (i = 0; i < coords; i++)
1810 src0[nsrc0++] = ddx[i];
1811 if (coords < 2)
1812 src0[nsrc0++] = create_immed(b, fui(0.0));
1813 for (i = 0; i < coords; i++)
1814 src0[nsrc0++] = ddy[i];
1815 if (coords < 2)
1816 src0[nsrc0++] = create_immed(b, fui(0.0));
1817 }
1818
1819 /*
1820 * second argument (if applicable):
1821 * - offsets
1822 * - lod
1823 * - bias
1824 */
1825 if (has_off | has_lod | has_bias) {
1826 if (has_off) {
1827 for (i = 0; i < coords; i++)
1828 src1[nsrc1++] = off[i];
1829 if (coords < 2)
1830 src1[nsrc1++] = create_immed(b, fui(0.0));
1831 flags |= IR3_INSTR_O;
1832 }
1833
1834 if (has_lod | has_bias)
1835 src1[nsrc1++] = lod;
1836 }
1837
1838 switch (tex->dest_type) {
1839 case nir_type_invalid:
1840 case nir_type_float:
1841 type = TYPE_F32;
1842 break;
1843 case nir_type_int:
1844 type = TYPE_S32;
1845 break;
1846 case nir_type_uint:
1847 case nir_type_bool:
1848 type = TYPE_U32;
1849 break;
1850 default:
1851 unreachable("bad dest_type");
1852 }
1853
1854 if (opc == OPC_GETLOD)
1855 type = TYPE_U32;
1856
1857 unsigned tex_idx = tex->texture_index;
1858
1859 ctx->max_texture_index = MAX2(ctx->max_texture_index, tex_idx);
1860
1861 struct ir3_instruction *col0 = create_collect(b, src0, nsrc0);
1862 struct ir3_instruction *col1 = create_collect(b, src1, nsrc1);
1863
1864 sam = ir3_SAM(b, opc, type, TGSI_WRITEMASK_XYZW, flags,
1865 tex_idx, tex_idx, col0, col1);
1866
1867 if ((ctx->astc_srgb & (1 << tex_idx)) && !nir_tex_instr_is_query(tex)) {
1868 /* only need first 3 components: */
1869 sam->regs[0]->wrmask = 0x7;
1870 split_dest(b, dst, sam, 0, 3);
1871
1872 /* we need to sample the alpha separately with a non-ASTC
1873 * texture state:
1874 */
1875 sam = ir3_SAM(b, opc, type, TGSI_WRITEMASK_W, flags,
1876 tex_idx, tex_idx, col0, col1);
1877
1878 array_insert(ctx->ir, ctx->ir->astc_srgb, sam);
1879
1880 /* fixup .w component: */
1881 split_dest(b, &dst[3], sam, 3, 1);
1882 } else {
1883 /* normal (non-workaround) case: */
1884 split_dest(b, dst, sam, 0, 4);
1885 }
1886
1887 /* GETLOD returns results in 4.8 fixed point */
1888 if (opc == OPC_GETLOD) {
1889 struct ir3_instruction *factor = create_immed(b, fui(1.0 / 256));
1890
1891 compile_assert(ctx, tex->dest_type == nir_type_float);
1892 for (i = 0; i < 2; i++) {
1893 dst[i] = ir3_MUL_F(b, ir3_COV(b, dst[i], TYPE_U32, TYPE_F32), 0,
1894 factor, 0);
1895 }
1896 }
1897
1898 put_dst(ctx, &tex->dest);
1899 }
1900
1901 static void
1902 emit_tex_query_levels(struct ir3_compile *ctx, nir_tex_instr *tex)
1903 {
1904 struct ir3_block *b = ctx->block;
1905 struct ir3_instruction **dst, *sam;
1906
1907 dst = get_dst(ctx, &tex->dest, 1);
1908
1909 sam = ir3_SAM(b, OPC_GETINFO, TYPE_U32, TGSI_WRITEMASK_Z, 0,
1910 tex->texture_index, tex->texture_index, NULL, NULL);
1911
1912 /* even though there is only one component, since it ends
1913 * up in .z rather than .x, we need a split_dest()
1914 */
1915 split_dest(b, dst, sam, 0, 3);
1916
1917 /* The # of levels comes from getinfo.z. We need to add 1 to it, since
1918 * the value in TEX_CONST_0 is zero-based.
1919 */
1920 if (ctx->levels_add_one)
1921 dst[0] = ir3_ADD_U(b, dst[0], 0, create_immed(b, 1), 0);
1922
1923 put_dst(ctx, &tex->dest);
1924 }
1925
1926 static void
1927 emit_tex_txs(struct ir3_compile *ctx, nir_tex_instr *tex)
1928 {
1929 struct ir3_block *b = ctx->block;
1930 struct ir3_instruction **dst, *sam;
1931 struct ir3_instruction *lod;
1932 unsigned flags, coords;
1933
1934 tex_info(tex, &flags, &coords);
1935
1936 /* Actually we want the number of dimensions, not coordinates. This
1937 * distinction only matters for cubes.
1938 */
1939 if (tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE)
1940 coords = 2;
1941
1942 dst = get_dst(ctx, &tex->dest, 4);
1943
1944 compile_assert(ctx, tex->num_srcs == 1);
1945 compile_assert(ctx, tex->src[0].src_type == nir_tex_src_lod);
1946
1947 lod = get_src(ctx, &tex->src[0].src)[0];
1948
1949 sam = ir3_SAM(b, OPC_GETSIZE, TYPE_U32, TGSI_WRITEMASK_XYZW, flags,
1950 tex->texture_index, tex->texture_index, lod, NULL);
1951
1952 split_dest(b, dst, sam, 0, 4);
1953
1954 /* Array size actually ends up in .w rather than .z. This doesn't
1955 * matter for miplevel 0, but for higher mips the value in z is
1956 * minified whereas w stays. Also, the value in TEX_CONST_3_DEPTH is
1957 * returned, which means that we have to add 1 to it for arrays.
1958 */
1959 if (tex->is_array) {
1960 if (ctx->levels_add_one) {
1961 dst[coords] = ir3_ADD_U(b, dst[3], 0, create_immed(b, 1), 0);
1962 } else {
1963 dst[coords] = ir3_MOV(b, dst[3], TYPE_U32);
1964 }
1965 }
1966
1967 put_dst(ctx, &tex->dest);
1968 }
1969
1970 static void
1971 emit_phi(struct ir3_compile *ctx, nir_phi_instr *nphi)
1972 {
1973 struct ir3_instruction *phi, **dst;
1974
1975 /* NOTE: phi's should be lowered to scalar at this point */
1976 compile_assert(ctx, nphi->dest.ssa.num_components == 1);
1977
1978 dst = get_dst(ctx, &nphi->dest, 1);
1979
1980 phi = ir3_instr_create2(ctx->block, OPC_META_PHI,
1981 1 + exec_list_length(&nphi->srcs));
1982 ir3_reg_create(phi, 0, 0); /* dst */
1983 phi->phi.nphi = nphi;
1984
1985 dst[0] = phi;
1986
1987 put_dst(ctx, &nphi->dest);
1988 }
1989
1990 /* phi instructions are left partially constructed. We don't resolve
1991 * their srcs until the end of the block, since (eg. loops) one of
1992 * the phi's srcs might be defined after the phi due to back edges in
1993 * the CFG.
1994 */
1995 static void
1996 resolve_phis(struct ir3_compile *ctx, struct ir3_block *block)
1997 {
1998 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
1999 nir_phi_instr *nphi;
2000
2001 /* phi's only come at start of block: */
2002 if (instr->opc != OPC_META_PHI)
2003 break;
2004
2005 if (!instr->phi.nphi)
2006 break;
2007
2008 nphi = instr->phi.nphi;
2009 instr->phi.nphi = NULL;
2010
2011 foreach_list_typed(nir_phi_src, nsrc, node, &nphi->srcs) {
2012 struct ir3_instruction *src = get_src(ctx, &nsrc->src)[0];
2013
2014 /* NOTE: src might not be in the same block as it comes from
2015 * according to the phi.. but in the end the backend assumes
2016 * it will be able to assign the same register to each (which
2017 * only works if it is assigned in the src block), so insert
2018 * an extra mov to make sure the phi src is assigned in the
2019 * block it comes from:
2020 */
2021 src = ir3_MOV(get_block(ctx, nsrc->pred), src, TYPE_U32);
2022
2023 ir3_reg_create(instr, 0, IR3_REG_SSA)->instr = src;
2024 }
2025 }
2026 }
2027
2028 static void
2029 emit_jump(struct ir3_compile *ctx, nir_jump_instr *jump)
2030 {
2031 switch (jump->type) {
2032 case nir_jump_break:
2033 case nir_jump_continue:
2034 /* I *think* we can simply just ignore this, and use the
2035 * successor block link to figure out where we need to
2036 * jump to for break/continue
2037 */
2038 break;
2039 default:
2040 compile_error(ctx, "Unhandled NIR jump type: %d\n", jump->type);
2041 break;
2042 }
2043 }
2044
2045 static void
2046 emit_instr(struct ir3_compile *ctx, nir_instr *instr)
2047 {
2048 switch (instr->type) {
2049 case nir_instr_type_alu:
2050 emit_alu(ctx, nir_instr_as_alu(instr));
2051 break;
2052 case nir_instr_type_intrinsic:
2053 emit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
2054 break;
2055 case nir_instr_type_load_const:
2056 emit_load_const(ctx, nir_instr_as_load_const(instr));
2057 break;
2058 case nir_instr_type_ssa_undef:
2059 emit_undef(ctx, nir_instr_as_ssa_undef(instr));
2060 break;
2061 case nir_instr_type_tex: {
2062 nir_tex_instr *tex = nir_instr_as_tex(instr);
2063 /* couple tex instructions get special-cased:
2064 */
2065 switch (tex->op) {
2066 case nir_texop_txs:
2067 emit_tex_txs(ctx, tex);
2068 break;
2069 case nir_texop_query_levels:
2070 emit_tex_query_levels(ctx, tex);
2071 break;
2072 default:
2073 emit_tex(ctx, tex);
2074 break;
2075 }
2076 break;
2077 }
2078 case nir_instr_type_phi:
2079 emit_phi(ctx, nir_instr_as_phi(instr));
2080 break;
2081 case nir_instr_type_jump:
2082 emit_jump(ctx, nir_instr_as_jump(instr));
2083 break;
2084 case nir_instr_type_call:
2085 case nir_instr_type_parallel_copy:
2086 compile_error(ctx, "Unhandled NIR instruction type: %d\n", instr->type);
2087 break;
2088 }
2089 }
2090
2091 static struct ir3_block *
2092 get_block(struct ir3_compile *ctx, nir_block *nblock)
2093 {
2094 struct ir3_block *block;
2095 struct hash_entry *entry;
2096 entry = _mesa_hash_table_search(ctx->block_ht, nblock);
2097 if (entry)
2098 return entry->data;
2099
2100 block = ir3_block_create(ctx->ir);
2101 block->nblock = nblock;
2102 _mesa_hash_table_insert(ctx->block_ht, nblock, block);
2103
2104 return block;
2105 }
2106
2107 static void
2108 emit_block(struct ir3_compile *ctx, nir_block *nblock)
2109 {
2110 struct ir3_block *block = get_block(ctx, nblock);
2111
2112 for (int i = 0; i < ARRAY_SIZE(block->successors); i++) {
2113 if (nblock->successors[i]) {
2114 block->successors[i] =
2115 get_block(ctx, nblock->successors[i]);
2116 }
2117 }
2118
2119 ctx->block = block;
2120 list_addtail(&block->node, &ctx->ir->block_list);
2121
2122 /* re-emit addr register in each block if needed: */
2123 for (int i = 0; i < ARRAY_SIZE(ctx->addr_ht); i++) {
2124 _mesa_hash_table_destroy(ctx->addr_ht[i], NULL);
2125 ctx->addr_ht[i] = NULL;
2126 }
2127
2128 nir_foreach_instr(instr, nblock) {
2129 emit_instr(ctx, instr);
2130 if (ctx->error)
2131 return;
2132 }
2133 }
2134
2135 static void emit_cf_list(struct ir3_compile *ctx, struct exec_list *list);
2136
2137 static void
2138 emit_if(struct ir3_compile *ctx, nir_if *nif)
2139 {
2140 struct ir3_instruction *condition = get_src(ctx, &nif->condition)[0];
2141
2142 ctx->block->condition =
2143 get_predicate(ctx, ir3_b2n(condition->block, condition));
2144
2145 emit_cf_list(ctx, &nif->then_list);
2146 emit_cf_list(ctx, &nif->else_list);
2147 }
2148
2149 static void
2150 emit_loop(struct ir3_compile *ctx, nir_loop *nloop)
2151 {
2152 emit_cf_list(ctx, &nloop->body);
2153 }
2154
2155 static void
2156 emit_cf_list(struct ir3_compile *ctx, struct exec_list *list)
2157 {
2158 foreach_list_typed(nir_cf_node, node, node, list) {
2159 switch (node->type) {
2160 case nir_cf_node_block:
2161 emit_block(ctx, nir_cf_node_as_block(node));
2162 break;
2163 case nir_cf_node_if:
2164 emit_if(ctx, nir_cf_node_as_if(node));
2165 break;
2166 case nir_cf_node_loop:
2167 emit_loop(ctx, nir_cf_node_as_loop(node));
2168 break;
2169 case nir_cf_node_function:
2170 compile_error(ctx, "TODO\n");
2171 break;
2172 }
2173 }
2174 }
2175
2176 /* emit stream-out code. At this point, the current block is the original
2177 * (nir) end block, and nir ensures that all flow control paths terminate
2178 * into the end block. We re-purpose the original end block to generate
2179 * the 'if (vtxcnt < maxvtxcnt)' condition, then append the conditional
2180 * block holding stream-out write instructions, followed by the new end
2181 * block:
2182 *
2183 * blockOrigEnd {
2184 * p0.x = (vtxcnt < maxvtxcnt)
2185 * // succs: blockStreamOut, blockNewEnd
2186 * }
2187 * blockStreamOut {
2188 * ... stream-out instructions ...
2189 * // succs: blockNewEnd
2190 * }
2191 * blockNewEnd {
2192 * }
2193 */
2194 static void
2195 emit_stream_out(struct ir3_compile *ctx)
2196 {
2197 struct ir3_shader_variant *v = ctx->so;
2198 struct ir3 *ir = ctx->ir;
2199 struct pipe_stream_output_info *strmout =
2200 &ctx->so->shader->stream_output;
2201 struct ir3_block *orig_end_block, *stream_out_block, *new_end_block;
2202 struct ir3_instruction *vtxcnt, *maxvtxcnt, *cond;
2203 struct ir3_instruction *bases[PIPE_MAX_SO_BUFFERS];
2204
2205 /* create vtxcnt input in input block at top of shader,
2206 * so that it is seen as live over the entire duration
2207 * of the shader:
2208 */
2209 vtxcnt = create_input(ctx->in_block, 0);
2210 add_sysval_input(ctx, SYSTEM_VALUE_VERTEX_CNT, vtxcnt);
2211
2212 maxvtxcnt = create_driver_param(ctx, IR3_DP_VTXCNT_MAX);
2213
2214 /* at this point, we are at the original 'end' block,
2215 * re-purpose this block to stream-out condition, then
2216 * append stream-out block and new-end block
2217 */
2218 orig_end_block = ctx->block;
2219
2220 stream_out_block = ir3_block_create(ir);
2221 list_addtail(&stream_out_block->node, &ir->block_list);
2222
2223 new_end_block = ir3_block_create(ir);
2224 list_addtail(&new_end_block->node, &ir->block_list);
2225
2226 orig_end_block->successors[0] = stream_out_block;
2227 orig_end_block->successors[1] = new_end_block;
2228 stream_out_block->successors[0] = new_end_block;
2229
2230 /* setup 'if (vtxcnt < maxvtxcnt)' condition: */
2231 cond = ir3_CMPS_S(ctx->block, vtxcnt, 0, maxvtxcnt, 0);
2232 cond->regs[0]->num = regid(REG_P0, 0);
2233 cond->cat2.condition = IR3_COND_LT;
2234
2235 /* condition goes on previous block to the conditional,
2236 * since it is used to pick which of the two successor
2237 * paths to take:
2238 */
2239 orig_end_block->condition = cond;
2240
2241 /* switch to stream_out_block to generate the stream-out
2242 * instructions:
2243 */
2244 ctx->block = stream_out_block;
2245
2246 /* Calculate base addresses based on vtxcnt. Instructions
2247 * generated for bases not used in following loop will be
2248 * stripped out in the backend.
2249 */
2250 for (unsigned i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
2251 unsigned stride = strmout->stride[i];
2252 struct ir3_instruction *base, *off;
2253
2254 base = create_uniform(ctx, regid(v->constbase.tfbo, i));
2255
2256 /* 24-bit should be enough: */
2257 off = ir3_MUL_U(ctx->block, vtxcnt, 0,
2258 create_immed(ctx->block, stride * 4), 0);
2259
2260 bases[i] = ir3_ADD_S(ctx->block, off, 0, base, 0);
2261 }
2262
2263 /* Generate the per-output store instructions: */
2264 for (unsigned i = 0; i < strmout->num_outputs; i++) {
2265 for (unsigned j = 0; j < strmout->output[i].num_components; j++) {
2266 unsigned c = j + strmout->output[i].start_component;
2267 struct ir3_instruction *base, *out, *stg;
2268
2269 base = bases[strmout->output[i].output_buffer];
2270 out = ctx->ir->outputs[regid(strmout->output[i].register_index, c)];
2271
2272 stg = ir3_STG(ctx->block, base, 0, out, 0,
2273 create_immed(ctx->block, 1), 0);
2274 stg->cat6.type = TYPE_U32;
2275 stg->cat6.dst_offset = (strmout->output[i].dst_offset + j) * 4;
2276
2277 array_insert(ctx->block, ctx->block->keeps, stg);
2278 }
2279 }
2280
2281 /* and finally switch to the new_end_block: */
2282 ctx->block = new_end_block;
2283 }
2284
2285 static void
2286 emit_function(struct ir3_compile *ctx, nir_function_impl *impl)
2287 {
2288 nir_metadata_require(impl, nir_metadata_block_index);
2289
2290 emit_cf_list(ctx, &impl->body);
2291 emit_block(ctx, impl->end_block);
2292
2293 /* at this point, we should have a single empty block,
2294 * into which we emit the 'end' instruction.
2295 */
2296 compile_assert(ctx, list_empty(&ctx->block->instr_list));
2297
2298 /* If stream-out (aka transform-feedback) enabled, emit the
2299 * stream-out instructions, followed by a new empty block (into
2300 * which the 'end' instruction lands).
2301 *
2302 * NOTE: it is done in this order, rather than inserting before
2303 * we emit end_block, because NIR guarantees that all blocks
2304 * flow into end_block, and that end_block has no successors.
2305 * So by re-purposing end_block as the first block of stream-
2306 * out, we guarantee that all exit paths flow into the stream-
2307 * out instructions.
2308 */
2309 if ((ctx->compiler->gpu_id < 500) &&
2310 (ctx->so->shader->stream_output.num_outputs > 0) &&
2311 !ctx->so->key.binning_pass) {
2312 debug_assert(ctx->so->type == SHADER_VERTEX);
2313 emit_stream_out(ctx);
2314 }
2315
2316 ir3_END(ctx->block);
2317 }
2318
2319 static void
2320 setup_input(struct ir3_compile *ctx, nir_variable *in)
2321 {
2322 struct ir3_shader_variant *so = ctx->so;
2323 unsigned array_len = MAX2(glsl_get_length(in->type), 1);
2324 unsigned ncomp = glsl_get_components(in->type);
2325 unsigned n = in->data.driver_location;
2326 unsigned slot = in->data.location;
2327
2328 DBG("; in: slot=%u, len=%ux%u, drvloc=%u",
2329 slot, array_len, ncomp, n);
2330
2331 /* let's pretend things other than vec4 don't exist: */
2332 ncomp = MAX2(ncomp, 4);
2333 compile_assert(ctx, ncomp == 4);
2334
2335 so->inputs[n].slot = slot;
2336 so->inputs[n].compmask = (1 << ncomp) - 1;
2337 so->inputs_count = MAX2(so->inputs_count, n + 1);
2338 so->inputs[n].interpolate = in->data.interpolation;
2339
2340 if (ctx->so->type == SHADER_FRAGMENT) {
2341 for (int i = 0; i < ncomp; i++) {
2342 struct ir3_instruction *instr = NULL;
2343 unsigned idx = (n * 4) + i;
2344
2345 if (slot == VARYING_SLOT_POS) {
2346 so->inputs[n].bary = false;
2347 so->frag_coord = true;
2348 instr = create_frag_coord(ctx, i);
2349 } else if (slot == VARYING_SLOT_PNTC) {
2350 /* see for example st_get_generic_varying_index().. this is
2351 * maybe a bit mesa/st specific. But we need things to line
2352 * up for this in fdN_program:
2353 * unsigned texmask = 1 << (slot - VARYING_SLOT_VAR0);
2354 * if (emit->sprite_coord_enable & texmask) {
2355 * ...
2356 * }
2357 */
2358 so->inputs[n].slot = VARYING_SLOT_VAR8;
2359 so->inputs[n].bary = true;
2360 instr = create_frag_input(ctx, false);
2361 } else {
2362 bool use_ldlv = false;
2363
2364 /* detect the special case for front/back colors where
2365 * we need to do flat vs smooth shading depending on
2366 * rast state:
2367 */
2368 if (in->data.interpolation == INTERP_MODE_NONE) {
2369 switch (slot) {
2370 case VARYING_SLOT_COL0:
2371 case VARYING_SLOT_COL1:
2372 case VARYING_SLOT_BFC0:
2373 case VARYING_SLOT_BFC1:
2374 so->inputs[n].rasterflat = true;
2375 break;
2376 default:
2377 break;
2378 }
2379 }
2380
2381 if (ctx->flat_bypass) {
2382 if ((so->inputs[n].interpolate == INTERP_MODE_FLAT) ||
2383 (so->inputs[n].rasterflat && ctx->so->key.rasterflat))
2384 use_ldlv = true;
2385 }
2386
2387 so->inputs[n].bary = true;
2388
2389 instr = create_frag_input(ctx, use_ldlv);
2390 }
2391
2392 compile_assert(ctx, idx < ctx->ir->ninputs);
2393
2394 ctx->ir->inputs[idx] = instr;
2395 }
2396 } else if (ctx->so->type == SHADER_VERTEX) {
2397 for (int i = 0; i < ncomp; i++) {
2398 unsigned idx = (n * 4) + i;
2399 compile_assert(ctx, idx < ctx->ir->ninputs);
2400 ctx->ir->inputs[idx] = create_input(ctx->block, idx);
2401 }
2402 } else {
2403 compile_error(ctx, "unknown shader type: %d\n", ctx->so->type);
2404 }
2405
2406 if (so->inputs[n].bary || (ctx->so->type == SHADER_VERTEX)) {
2407 so->total_in += ncomp;
2408 }
2409 }
2410
2411 static void
2412 setup_output(struct ir3_compile *ctx, nir_variable *out)
2413 {
2414 struct ir3_shader_variant *so = ctx->so;
2415 unsigned array_len = MAX2(glsl_get_length(out->type), 1);
2416 unsigned ncomp = glsl_get_components(out->type);
2417 unsigned n = out->data.driver_location;
2418 unsigned slot = out->data.location;
2419 unsigned comp = 0;
2420
2421 DBG("; out: slot=%u, len=%ux%u, drvloc=%u",
2422 slot, array_len, ncomp, n);
2423
2424 /* let's pretend things other than vec4 don't exist: */
2425 ncomp = MAX2(ncomp, 4);
2426 compile_assert(ctx, ncomp == 4);
2427
2428 if (ctx->so->type == SHADER_FRAGMENT) {
2429 switch (slot) {
2430 case FRAG_RESULT_DEPTH:
2431 comp = 2; /* tgsi will write to .z component */
2432 so->writes_pos = true;
2433 break;
2434 case FRAG_RESULT_COLOR:
2435 so->color0_mrt = 1;
2436 break;
2437 default:
2438 if (slot >= FRAG_RESULT_DATA0)
2439 break;
2440 compile_error(ctx, "unknown FS output name: %s\n",
2441 gl_frag_result_name(slot));
2442 }
2443 } else if (ctx->so->type == SHADER_VERTEX) {
2444 switch (slot) {
2445 case VARYING_SLOT_POS:
2446 so->writes_pos = true;
2447 break;
2448 case VARYING_SLOT_PSIZ:
2449 so->writes_psize = true;
2450 break;
2451 case VARYING_SLOT_COL0:
2452 case VARYING_SLOT_COL1:
2453 case VARYING_SLOT_BFC0:
2454 case VARYING_SLOT_BFC1:
2455 case VARYING_SLOT_FOGC:
2456 case VARYING_SLOT_CLIP_DIST0:
2457 case VARYING_SLOT_CLIP_DIST1:
2458 case VARYING_SLOT_CLIP_VERTEX:
2459 break;
2460 default:
2461 if (slot >= VARYING_SLOT_VAR0)
2462 break;
2463 if ((VARYING_SLOT_TEX0 <= slot) && (slot <= VARYING_SLOT_TEX7))
2464 break;
2465 compile_error(ctx, "unknown VS output name: %s\n",
2466 gl_varying_slot_name(slot));
2467 }
2468 } else {
2469 compile_error(ctx, "unknown shader type: %d\n", ctx->so->type);
2470 }
2471
2472 compile_assert(ctx, n < ARRAY_SIZE(so->outputs));
2473
2474 so->outputs[n].slot = slot;
2475 so->outputs[n].regid = regid(n, comp);
2476 so->outputs_count = MAX2(so->outputs_count, n + 1);
2477
2478 for (int i = 0; i < ncomp; i++) {
2479 unsigned idx = (n * 4) + i;
2480 compile_assert(ctx, idx < ctx->ir->noutputs);
2481 ctx->ir->outputs[idx] = create_immed(ctx->block, fui(0.0));
2482 }
2483 }
2484
2485 static int
2486 max_drvloc(struct exec_list *vars)
2487 {
2488 int drvloc = -1;
2489 nir_foreach_variable(var, vars) {
2490 drvloc = MAX2(drvloc, (int)var->data.driver_location);
2491 }
2492 return drvloc;
2493 }
2494
2495 static const unsigned max_sysvals[SHADER_MAX] = {
2496 [SHADER_VERTEX] = 16,
2497 [SHADER_COMPUTE] = 16, // TODO how many do we actually need?
2498 };
2499
2500 static void
2501 emit_instructions(struct ir3_compile *ctx)
2502 {
2503 unsigned ninputs, noutputs;
2504 nir_function_impl *fxn = nir_shader_get_entrypoint(ctx->s);
2505
2506 ninputs = (max_drvloc(&ctx->s->inputs) + 1) * 4;
2507 noutputs = (max_drvloc(&ctx->s->outputs) + 1) * 4;
2508
2509 /* we need to leave room for sysvals:
2510 */
2511 ninputs += max_sysvals[ctx->so->type];
2512
2513 ctx->ir = ir3_create(ctx->compiler, ninputs, noutputs);
2514
2515 /* Create inputs in first block: */
2516 ctx->block = get_block(ctx, nir_start_block(fxn));
2517 ctx->in_block = ctx->block;
2518 list_addtail(&ctx->block->node, &ctx->ir->block_list);
2519
2520 ninputs -= max_sysvals[ctx->so->type];
2521
2522 /* for fragment shader, we have a single input register (usually
2523 * r0.xy) which is used as the base for bary.f varying fetch instrs:
2524 */
2525 if (ctx->so->type == SHADER_FRAGMENT) {
2526 // TODO maybe a helper for fi since we need it a few places..
2527 struct ir3_instruction *instr;
2528 instr = ir3_instr_create(ctx->block, OPC_META_FI);
2529 ir3_reg_create(instr, 0, 0);
2530 ir3_reg_create(instr, 0, IR3_REG_SSA); /* r0.x */
2531 ir3_reg_create(instr, 0, IR3_REG_SSA); /* r0.y */
2532 ctx->frag_pos = instr;
2533 }
2534
2535 /* Setup inputs: */
2536 nir_foreach_variable(var, &ctx->s->inputs) {
2537 setup_input(ctx, var);
2538 }
2539
2540 /* Setup outputs: */
2541 nir_foreach_variable(var, &ctx->s->outputs) {
2542 setup_output(ctx, var);
2543 }
2544
2545 /* Setup registers (which should only be arrays): */
2546 nir_foreach_register(reg, &ctx->s->registers) {
2547 declare_array(ctx, reg);
2548 }
2549
2550 /* NOTE: need to do something more clever when we support >1 fxn */
2551 nir_foreach_register(reg, &fxn->registers) {
2552 declare_array(ctx, reg);
2553 }
2554 /* And emit the body: */
2555 ctx->impl = fxn;
2556 emit_function(ctx, fxn);
2557
2558 list_for_each_entry (struct ir3_block, block, &ctx->ir->block_list, node) {
2559 resolve_phis(ctx, block);
2560 }
2561 }
2562
2563 /* from NIR perspective, we actually have inputs. But most of the "inputs"
2564 * for a fragment shader are just bary.f instructions. The *actual* inputs
2565 * from the hw perspective are the frag_pos and optionally frag_coord and
2566 * frag_face.
2567 */
2568 static void
2569 fixup_frag_inputs(struct ir3_compile *ctx)
2570 {
2571 struct ir3_shader_variant *so = ctx->so;
2572 struct ir3 *ir = ctx->ir;
2573 struct ir3_instruction **inputs;
2574 struct ir3_instruction *instr;
2575 int n, regid = 0;
2576
2577 ir->ninputs = 0;
2578
2579 n = 4; /* always have frag_pos */
2580 n += COND(so->frag_face, 4);
2581 n += COND(so->frag_coord, 4);
2582
2583 inputs = ir3_alloc(ctx->ir, n * (sizeof(struct ir3_instruction *)));
2584
2585 if (so->frag_face) {
2586 /* this ultimately gets assigned to hr0.x so doesn't conflict
2587 * with frag_coord/frag_pos..
2588 */
2589 inputs[ir->ninputs++] = ctx->frag_face;
2590 ctx->frag_face->regs[0]->num = 0;
2591
2592 /* remaining channels not used, but let's avoid confusing
2593 * other parts that expect inputs to come in groups of vec4
2594 */
2595 inputs[ir->ninputs++] = NULL;
2596 inputs[ir->ninputs++] = NULL;
2597 inputs[ir->ninputs++] = NULL;
2598 }
2599
2600 /* since we don't know where to set the regid for frag_coord,
2601 * we have to use r0.x for it. But we don't want to *always*
2602 * use r1.x for frag_pos as that could increase the register
2603 * footprint on simple shaders:
2604 */
2605 if (so->frag_coord) {
2606 ctx->frag_coord[0]->regs[0]->num = regid++;
2607 ctx->frag_coord[1]->regs[0]->num = regid++;
2608 ctx->frag_coord[2]->regs[0]->num = regid++;
2609 ctx->frag_coord[3]->regs[0]->num = regid++;
2610
2611 inputs[ir->ninputs++] = ctx->frag_coord[0];
2612 inputs[ir->ninputs++] = ctx->frag_coord[1];
2613 inputs[ir->ninputs++] = ctx->frag_coord[2];
2614 inputs[ir->ninputs++] = ctx->frag_coord[3];
2615 }
2616
2617 /* we always have frag_pos: */
2618 so->pos_regid = regid;
2619
2620 /* r0.x */
2621 instr = create_input(ctx->in_block, ir->ninputs);
2622 instr->regs[0]->num = regid++;
2623 inputs[ir->ninputs++] = instr;
2624 ctx->frag_pos->regs[1]->instr = instr;
2625
2626 /* r0.y */
2627 instr = create_input(ctx->in_block, ir->ninputs);
2628 instr->regs[0]->num = regid++;
2629 inputs[ir->ninputs++] = instr;
2630 ctx->frag_pos->regs[2]->instr = instr;
2631
2632 ir->inputs = inputs;
2633 }
2634
2635 /* Fixup tex sampler state for astc/srgb workaround instructions. We
2636 * need to assign the tex state indexes for these after we know the
2637 * max tex index.
2638 */
2639 static void
2640 fixup_astc_srgb(struct ir3_compile *ctx)
2641 {
2642 struct ir3_shader_variant *so = ctx->so;
2643 /* indexed by original tex idx, value is newly assigned alpha sampler
2644 * state tex idx. Zero is invalid since there is at least one sampler
2645 * if we get here.
2646 */
2647 unsigned alt_tex_state[16] = {0};
2648 unsigned tex_idx = ctx->max_texture_index + 1;
2649 unsigned idx = 0;
2650
2651 so->astc_srgb.base = tex_idx;
2652
2653 for (unsigned i = 0; i < ctx->ir->astc_srgb_count; i++) {
2654 struct ir3_instruction *sam = ctx->ir->astc_srgb[i];
2655
2656 compile_assert(ctx, sam->cat5.tex < ARRAY_SIZE(alt_tex_state));
2657
2658 if (alt_tex_state[sam->cat5.tex] == 0) {
2659 /* assign new alternate/alpha tex state slot: */
2660 alt_tex_state[sam->cat5.tex] = tex_idx++;
2661 so->astc_srgb.orig_idx[idx++] = sam->cat5.tex;
2662 so->astc_srgb.count++;
2663 }
2664
2665 sam->cat5.tex = alt_tex_state[sam->cat5.tex];
2666 }
2667 }
2668
2669 int
2670 ir3_compile_shader_nir(struct ir3_compiler *compiler,
2671 struct ir3_shader_variant *so)
2672 {
2673 struct ir3_compile *ctx;
2674 struct ir3 *ir;
2675 struct ir3_instruction **inputs;
2676 unsigned i, j, actual_in, inloc;
2677 int ret = 0, max_bary;
2678
2679 assert(!so->ir);
2680
2681 ctx = compile_init(compiler, so);
2682 if (!ctx) {
2683 DBG("INIT failed!");
2684 ret = -1;
2685 goto out;
2686 }
2687
2688 emit_instructions(ctx);
2689
2690 if (ctx->error) {
2691 DBG("EMIT failed!");
2692 ret = -1;
2693 goto out;
2694 }
2695
2696 ir = so->ir = ctx->ir;
2697
2698 /* keep track of the inputs from TGSI perspective.. */
2699 inputs = ir->inputs;
2700
2701 /* but fixup actual inputs for frag shader: */
2702 if (so->type == SHADER_FRAGMENT)
2703 fixup_frag_inputs(ctx);
2704
2705 /* at this point, for binning pass, throw away unneeded outputs: */
2706 if (so->key.binning_pass) {
2707 for (i = 0, j = 0; i < so->outputs_count; i++) {
2708 unsigned slot = so->outputs[i].slot;
2709
2710 /* throw away everything but first position/psize */
2711 if ((slot == VARYING_SLOT_POS) || (slot == VARYING_SLOT_PSIZ)) {
2712 if (i != j) {
2713 so->outputs[j] = so->outputs[i];
2714 ir->outputs[(j*4)+0] = ir->outputs[(i*4)+0];
2715 ir->outputs[(j*4)+1] = ir->outputs[(i*4)+1];
2716 ir->outputs[(j*4)+2] = ir->outputs[(i*4)+2];
2717 ir->outputs[(j*4)+3] = ir->outputs[(i*4)+3];
2718 }
2719 j++;
2720 }
2721 }
2722 so->outputs_count = j;
2723 ir->noutputs = j * 4;
2724 }
2725
2726 /* if we want half-precision outputs, mark the output registers
2727 * as half:
2728 */
2729 if (so->key.half_precision) {
2730 for (i = 0; i < ir->noutputs; i++) {
2731 struct ir3_instruction *out = ir->outputs[i];
2732
2733 if (!out)
2734 continue;
2735
2736 /* if frag shader writes z, that needs to be full precision: */
2737 if (so->outputs[i/4].slot == FRAG_RESULT_DEPTH)
2738 continue;
2739
2740 out->regs[0]->flags |= IR3_REG_HALF;
2741 /* output could be a fanout (ie. texture fetch output)
2742 * in which case we need to propagate the half-reg flag
2743 * up to the definer so that RA sees it:
2744 */
2745 if (out->opc == OPC_META_FO) {
2746 out = out->regs[1]->instr;
2747 out->regs[0]->flags |= IR3_REG_HALF;
2748 }
2749
2750 if (out->opc == OPC_MOV) {
2751 out->cat1.dst_type = half_type(out->cat1.dst_type);
2752 }
2753 }
2754 }
2755
2756 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2757 printf("BEFORE CP:\n");
2758 ir3_print(ir);
2759 }
2760
2761 ir3_cp(ir, so);
2762
2763 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2764 printf("BEFORE GROUPING:\n");
2765 ir3_print(ir);
2766 }
2767
2768 /* Group left/right neighbors, inserting mov's where needed to
2769 * solve conflicts:
2770 */
2771 ir3_group(ir);
2772
2773 ir3_depth(ir);
2774
2775 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2776 printf("AFTER DEPTH:\n");
2777 ir3_print(ir);
2778 }
2779
2780 ret = ir3_sched(ir);
2781 if (ret) {
2782 DBG("SCHED failed!");
2783 goto out;
2784 }
2785
2786 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2787 printf("AFTER SCHED:\n");
2788 ir3_print(ir);
2789 }
2790
2791 ret = ir3_ra(ir, so->type, so->frag_coord, so->frag_face);
2792 if (ret) {
2793 DBG("RA failed!");
2794 goto out;
2795 }
2796
2797 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2798 printf("AFTER RA:\n");
2799 ir3_print(ir);
2800 }
2801
2802 /* fixup input/outputs: */
2803 for (i = 0; i < so->outputs_count; i++) {
2804 so->outputs[i].regid = ir->outputs[i*4]->regs[0]->num;
2805 }
2806
2807 /* Note that some or all channels of an input may be unused: */
2808 actual_in = 0;
2809 inloc = 0;
2810 for (i = 0; i < so->inputs_count; i++) {
2811 unsigned j, regid = ~0, compmask = 0, maxcomp = 0;
2812 so->inputs[i].ncomp = 0;
2813 so->inputs[i].inloc = inloc;
2814 for (j = 0; j < 4; j++) {
2815 struct ir3_instruction *in = inputs[(i*4) + j];
2816 if (in && !(in->flags & IR3_INSTR_UNUSED)) {
2817 compmask |= (1 << j);
2818 regid = in->regs[0]->num - j;
2819 actual_in++;
2820 so->inputs[i].ncomp++;
2821 if ((so->type == SHADER_FRAGMENT) && so->inputs[i].bary) {
2822 /* assign inloc: */
2823 assert(in->regs[1]->flags & IR3_REG_IMMED);
2824 in->regs[1]->iim_val = inloc + j;
2825 maxcomp = j + 1;
2826 }
2827 }
2828 }
2829 if ((so->type == SHADER_FRAGMENT) && compmask && so->inputs[i].bary) {
2830 so->varying_in++;
2831 so->inputs[i].compmask = (1 << maxcomp) - 1;
2832 inloc += maxcomp;
2833 } else {
2834 so->inputs[i].compmask = compmask;
2835 }
2836 so->inputs[i].regid = regid;
2837 }
2838
2839 if (ctx->astc_srgb)
2840 fixup_astc_srgb(ctx);
2841
2842 /* We need to do legalize after (for frag shader's) the "bary.f"
2843 * offsets (inloc) have been assigned.
2844 */
2845 ir3_legalize(ir, &so->has_samp, &so->has_ssbo, &max_bary);
2846
2847 if (fd_mesa_debug & FD_DBG_OPTMSGS) {
2848 printf("AFTER LEGALIZE:\n");
2849 ir3_print(ir);
2850 }
2851
2852 /* Note that actual_in counts inputs that are not bary.f'd for FS: */
2853 if (so->type == SHADER_VERTEX)
2854 so->total_in = actual_in;
2855 else
2856 so->total_in = max_bary + 1;
2857
2858 out:
2859 if (ret) {
2860 if (so->ir)
2861 ir3_destroy(so->ir);
2862 so->ir = NULL;
2863 }
2864 compile_free(ctx);
2865
2866 return ret;
2867 }