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