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