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