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