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