v3d: Retry with the fallback scheduler when RA fails
[mesa.git] / src / broadcom / compiler / nir_to_vir.c
1 /*
2 * Copyright © 2016 Broadcom
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <inttypes.h>
25 #include "util/format/u_format.h"
26 #include "util/u_helpers.h"
27 #include "util/u_math.h"
28 #include "util/u_memory.h"
29 #include "util/ralloc.h"
30 #include "util/hash_table.h"
31 #include "compiler/nir/nir.h"
32 #include "compiler/nir/nir_builder.h"
33 #include "common/v3d_device_info.h"
34 #include "v3d_compiler.h"
35
36 /* We don't do any address packing. */
37 #define __gen_user_data void
38 #define __gen_address_type uint32_t
39 #define __gen_address_offset(reloc) (*reloc)
40 #define __gen_emit_reloc(cl, reloc)
41 #include "cle/v3d_packet_v41_pack.h"
42
43 #define GENERAL_TMU_LOOKUP_PER_QUAD (0 << 7)
44 #define GENERAL_TMU_LOOKUP_PER_PIXEL (1 << 7)
45 #define GENERAL_TMU_LOOKUP_TYPE_8BIT_I (0 << 0)
46 #define GENERAL_TMU_LOOKUP_TYPE_16BIT_I (1 << 0)
47 #define GENERAL_TMU_LOOKUP_TYPE_VEC2 (2 << 0)
48 #define GENERAL_TMU_LOOKUP_TYPE_VEC3 (3 << 0)
49 #define GENERAL_TMU_LOOKUP_TYPE_VEC4 (4 << 0)
50 #define GENERAL_TMU_LOOKUP_TYPE_8BIT_UI (5 << 0)
51 #define GENERAL_TMU_LOOKUP_TYPE_16BIT_UI (6 << 0)
52 #define GENERAL_TMU_LOOKUP_TYPE_32BIT_UI (7 << 0)
53
54 #define V3D_TSY_SET_QUORUM 0
55 #define V3D_TSY_INC_WAITERS 1
56 #define V3D_TSY_DEC_WAITERS 2
57 #define V3D_TSY_INC_QUORUM 3
58 #define V3D_TSY_DEC_QUORUM 4
59 #define V3D_TSY_FREE_ALL 5
60 #define V3D_TSY_RELEASE 6
61 #define V3D_TSY_ACQUIRE 7
62 #define V3D_TSY_WAIT 8
63 #define V3D_TSY_WAIT_INC 9
64 #define V3D_TSY_WAIT_CHECK 10
65 #define V3D_TSY_WAIT_INC_CHECK 11
66 #define V3D_TSY_WAIT_CV 12
67 #define V3D_TSY_INC_SEMAPHORE 13
68 #define V3D_TSY_DEC_SEMAPHORE 14
69 #define V3D_TSY_SET_QUORUM_FREE_ALL 15
70
71 static void
72 ntq_emit_cf_list(struct v3d_compile *c, struct exec_list *list);
73
74 static void
75 resize_qreg_array(struct v3d_compile *c,
76 struct qreg **regs,
77 uint32_t *size,
78 uint32_t decl_size)
79 {
80 if (*size >= decl_size)
81 return;
82
83 uint32_t old_size = *size;
84 *size = MAX2(*size * 2, decl_size);
85 *regs = reralloc(c, *regs, struct qreg, *size);
86 if (!*regs) {
87 fprintf(stderr, "Malloc failure\n");
88 abort();
89 }
90
91 for (uint32_t i = old_size; i < *size; i++)
92 (*regs)[i] = c->undef;
93 }
94
95 void
96 vir_emit_thrsw(struct v3d_compile *c)
97 {
98 if (c->threads == 1)
99 return;
100
101 /* Always thread switch after each texture operation for now.
102 *
103 * We could do better by batching a bunch of texture fetches up and
104 * then doing one thread switch and collecting all their results
105 * afterward.
106 */
107 c->last_thrsw = vir_NOP(c);
108 c->last_thrsw->qpu.sig.thrsw = true;
109 c->last_thrsw_at_top_level = !c->in_control_flow;
110
111 /* We need to lock the scoreboard before any tlb acess happens. If this
112 * thread switch comes after we have emitted a tlb load, then it means
113 * that we can't lock on the last thread switch any more.
114 */
115 if (c->emitted_tlb_load)
116 c->lock_scoreboard_on_first_thrsw = true;
117 }
118
119 uint32_t
120 v3d_get_op_for_atomic_add(nir_intrinsic_instr *instr, unsigned src)
121 {
122 if (nir_src_is_const(instr->src[src])) {
123 int64_t add_val = nir_src_as_int(instr->src[src]);
124 if (add_val == 1)
125 return V3D_TMU_OP_WRITE_AND_READ_INC;
126 else if (add_val == -1)
127 return V3D_TMU_OP_WRITE_OR_READ_DEC;
128 }
129
130 return V3D_TMU_OP_WRITE_ADD_READ_PREFETCH;
131 }
132
133 static uint32_t
134 v3d_general_tmu_op(nir_intrinsic_instr *instr)
135 {
136 switch (instr->intrinsic) {
137 case nir_intrinsic_load_ssbo:
138 case nir_intrinsic_load_ubo:
139 case nir_intrinsic_load_uniform:
140 case nir_intrinsic_load_shared:
141 case nir_intrinsic_load_scratch:
142 case nir_intrinsic_store_ssbo:
143 case nir_intrinsic_store_shared:
144 case nir_intrinsic_store_scratch:
145 return V3D_TMU_OP_REGULAR;
146 case nir_intrinsic_ssbo_atomic_add:
147 return v3d_get_op_for_atomic_add(instr, 2);
148 case nir_intrinsic_shared_atomic_add:
149 return v3d_get_op_for_atomic_add(instr, 1);
150 case nir_intrinsic_ssbo_atomic_imin:
151 case nir_intrinsic_shared_atomic_imin:
152 return V3D_TMU_OP_WRITE_SMIN;
153 case nir_intrinsic_ssbo_atomic_umin:
154 case nir_intrinsic_shared_atomic_umin:
155 return V3D_TMU_OP_WRITE_UMIN_FULL_L1_CLEAR;
156 case nir_intrinsic_ssbo_atomic_imax:
157 case nir_intrinsic_shared_atomic_imax:
158 return V3D_TMU_OP_WRITE_SMAX;
159 case nir_intrinsic_ssbo_atomic_umax:
160 case nir_intrinsic_shared_atomic_umax:
161 return V3D_TMU_OP_WRITE_UMAX;
162 case nir_intrinsic_ssbo_atomic_and:
163 case nir_intrinsic_shared_atomic_and:
164 return V3D_TMU_OP_WRITE_AND_READ_INC;
165 case nir_intrinsic_ssbo_atomic_or:
166 case nir_intrinsic_shared_atomic_or:
167 return V3D_TMU_OP_WRITE_OR_READ_DEC;
168 case nir_intrinsic_ssbo_atomic_xor:
169 case nir_intrinsic_shared_atomic_xor:
170 return V3D_TMU_OP_WRITE_XOR_READ_NOT;
171 case nir_intrinsic_ssbo_atomic_exchange:
172 case nir_intrinsic_shared_atomic_exchange:
173 return V3D_TMU_OP_WRITE_XCHG_READ_FLUSH;
174 case nir_intrinsic_ssbo_atomic_comp_swap:
175 case nir_intrinsic_shared_atomic_comp_swap:
176 return V3D_TMU_OP_WRITE_CMPXCHG_READ_FLUSH;
177 default:
178 unreachable("unknown intrinsic op");
179 }
180 }
181
182 /**
183 * Implements indirect uniform loads and SSBO accesses through the TMU general
184 * memory access interface.
185 */
186 static void
187 ntq_emit_tmu_general(struct v3d_compile *c, nir_intrinsic_instr *instr,
188 bool is_shared_or_scratch)
189 {
190 uint32_t tmu_op = v3d_general_tmu_op(instr);
191
192 /* If we were able to replace atomic_add for an inc/dec, then we
193 * need/can to do things slightly different, like not loading the
194 * amount to add/sub, as that is implicit.
195 */
196 bool atomic_add_replaced =
197 ((instr->intrinsic == nir_intrinsic_ssbo_atomic_add ||
198 instr->intrinsic == nir_intrinsic_shared_atomic_add) &&
199 (tmu_op == V3D_TMU_OP_WRITE_AND_READ_INC ||
200 tmu_op == V3D_TMU_OP_WRITE_OR_READ_DEC));
201
202 bool is_store = (instr->intrinsic == nir_intrinsic_store_ssbo ||
203 instr->intrinsic == nir_intrinsic_store_scratch ||
204 instr->intrinsic == nir_intrinsic_store_shared);
205
206 bool is_load = (instr->intrinsic == nir_intrinsic_load_uniform ||
207 instr->intrinsic == nir_intrinsic_load_ubo ||
208 instr->intrinsic == nir_intrinsic_load_ssbo ||
209 instr->intrinsic == nir_intrinsic_load_scratch ||
210 instr->intrinsic == nir_intrinsic_load_shared);
211
212 if (!is_load)
213 c->tmu_dirty_rcl = true;
214
215 bool has_index = !is_shared_or_scratch;
216
217 int offset_src;
218 if (instr->intrinsic == nir_intrinsic_load_uniform) {
219 offset_src = 0;
220 } else if (instr->intrinsic == nir_intrinsic_load_ssbo ||
221 instr->intrinsic == nir_intrinsic_load_ubo ||
222 instr->intrinsic == nir_intrinsic_load_scratch ||
223 instr->intrinsic == nir_intrinsic_load_shared ||
224 atomic_add_replaced) {
225 offset_src = 0 + has_index;
226 } else if (is_store) {
227 offset_src = 1 + has_index;
228 } else {
229 offset_src = 0 + has_index;
230 }
231
232 bool dynamic_src = !nir_src_is_const(instr->src[offset_src]);
233 uint32_t const_offset = 0;
234 if (!dynamic_src)
235 const_offset = nir_src_as_uint(instr->src[offset_src]);
236
237 struct qreg base_offset;
238 if (instr->intrinsic == nir_intrinsic_load_uniform) {
239 const_offset += nir_intrinsic_base(instr);
240 base_offset = vir_uniform(c, QUNIFORM_UBO_ADDR,
241 v3d_unit_data_create(0, const_offset));
242 const_offset = 0;
243 } else if (instr->intrinsic == nir_intrinsic_load_ubo) {
244 uint32_t index = nir_src_as_uint(instr->src[0]) + 1;
245 /* Note that QUNIFORM_UBO_ADDR takes a UBO index shifted up by
246 * 1 (0 is gallium's constant buffer 0).
247 */
248 base_offset =
249 vir_uniform(c, QUNIFORM_UBO_ADDR,
250 v3d_unit_data_create(index, const_offset));
251 const_offset = 0;
252 } else if (is_shared_or_scratch) {
253 /* Shared and scratch variables have no buffer index, and all
254 * start from a common base that we set up at the start of
255 * dispatch.
256 */
257 if (instr->intrinsic == nir_intrinsic_load_scratch ||
258 instr->intrinsic == nir_intrinsic_store_scratch) {
259 base_offset = c->spill_base;
260 } else {
261 base_offset = c->cs_shared_offset;
262 const_offset += nir_intrinsic_base(instr);
263 }
264 } else {
265 base_offset = vir_uniform(c, QUNIFORM_SSBO_OFFSET,
266 nir_src_as_uint(instr->src[is_store ?
267 1 : 0]));
268 }
269
270 struct qreg tmud = vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_TMUD);
271 unsigned writemask = is_store ? nir_intrinsic_write_mask(instr) : 0;
272 uint32_t base_const_offset = const_offset;
273 int first_component = -1;
274 int last_component = -1;
275 do {
276 int tmu_writes = 1; /* address */
277
278 if (is_store) {
279 /* Find the first set of consecutive components that
280 * are enabled in the writemask and emit the TMUD
281 * instructions for them.
282 */
283 first_component = ffs(writemask) - 1;
284 last_component = first_component;
285 while (writemask & BITFIELD_BIT(last_component + 1))
286 last_component++;
287
288 assert(first_component >= 0 &&
289 first_component <= last_component &&
290 last_component < instr->num_components);
291
292 struct qreg tmud = vir_reg(QFILE_MAGIC,
293 V3D_QPU_WADDR_TMUD);
294 for (int i = first_component; i <= last_component; i++) {
295 struct qreg data =
296 ntq_get_src(c, instr->src[0], i);
297 vir_MOV_dest(c, tmud, data);
298 tmu_writes++;
299 }
300
301 /* Update the offset for the TMU write based on the
302 * the first component we are writing.
303 */
304 const_offset = base_const_offset + first_component * 4;
305
306 /* Clear these components from the writemask */
307 uint32_t written_mask =
308 BITFIELD_RANGE(first_component, tmu_writes - 1);
309 writemask &= ~written_mask;
310 } else if (!is_load && !atomic_add_replaced) {
311 struct qreg data =
312 ntq_get_src(c, instr->src[1 + has_index], 0);
313 vir_MOV_dest(c, tmud, data);
314 tmu_writes++;
315 if (tmu_op == V3D_TMU_OP_WRITE_CMPXCHG_READ_FLUSH) {
316 data = ntq_get_src(c, instr->src[2 + has_index],
317 0);
318 vir_MOV_dest(c, tmud, data);
319 tmu_writes++;
320 }
321 }
322
323 /* Make sure we won't exceed the 16-entry TMU fifo if each
324 * thread is storing at the same time.
325 */
326 while (tmu_writes > 16 / c->threads)
327 c->threads /= 2;
328
329 /* The spec says that for atomics, the TYPE field is ignored,
330 * but that doesn't seem to be the case for CMPXCHG. Just use
331 * the number of tmud writes we did to decide the type (or
332 * choose "32bit" for atomic reads, which has been fine).
333 */
334 uint32_t num_components;
335 if (is_load || atomic_add_replaced) {
336 num_components = instr->num_components;
337 } else {
338 assert(tmu_writes > 1);
339 num_components = tmu_writes - 1;
340 }
341
342 uint32_t config = (0xffffff00 |
343 tmu_op << 3|
344 GENERAL_TMU_LOOKUP_PER_PIXEL);
345 if (num_components == 1) {
346 config |= GENERAL_TMU_LOOKUP_TYPE_32BIT_UI;
347 } else {
348 config |= GENERAL_TMU_LOOKUP_TYPE_VEC2 +
349 num_components - 2;
350 }
351
352 if (vir_in_nonuniform_control_flow(c)) {
353 vir_set_pf(vir_MOV_dest(c, vir_nop_reg(), c->execute),
354 V3D_QPU_PF_PUSHZ);
355 }
356
357 struct qreg tmua;
358 if (config == ~0)
359 tmua = vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_TMUA);
360 else
361 tmua = vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_TMUAU);
362
363 struct qinst *tmu;
364 if (dynamic_src) {
365 struct qreg offset = base_offset;
366 if (const_offset != 0) {
367 offset = vir_ADD(c, offset,
368 vir_uniform_ui(c, const_offset));
369 }
370 struct qreg data =
371 ntq_get_src(c, instr->src[offset_src], 0);
372 tmu = vir_ADD_dest(c, tmua, offset, data);
373 } else {
374 if (const_offset != 0) {
375 tmu = vir_ADD_dest(c, tmua, base_offset,
376 vir_uniform_ui(c, const_offset));
377 } else {
378 tmu = vir_MOV_dest(c, tmua, base_offset);
379 }
380 }
381
382 if (config != ~0) {
383 tmu->uniform =
384 vir_get_uniform_index(c, QUNIFORM_CONSTANT,
385 config);
386 }
387
388 if (vir_in_nonuniform_control_flow(c))
389 vir_set_cond(tmu, V3D_QPU_COND_IFA);
390
391 vir_emit_thrsw(c);
392
393 /* Read the result, or wait for the TMU op to complete. */
394 for (int i = 0; i < nir_intrinsic_dest_components(instr); i++) {
395 ntq_store_dest(c, &instr->dest, i,
396 vir_MOV(c, vir_LDTMU(c)));
397 }
398
399 if (nir_intrinsic_dest_components(instr) == 0)
400 vir_TMUWT(c);
401 } while (is_store && writemask != 0);
402 }
403
404 static struct qreg *
405 ntq_init_ssa_def(struct v3d_compile *c, nir_ssa_def *def)
406 {
407 struct qreg *qregs = ralloc_array(c->def_ht, struct qreg,
408 def->num_components);
409 _mesa_hash_table_insert(c->def_ht, def, qregs);
410 return qregs;
411 }
412
413 static bool
414 is_ld_signal(const struct v3d_qpu_sig *sig)
415 {
416 return (sig->ldunif ||
417 sig->ldunifa ||
418 sig->ldunifrf ||
419 sig->ldunifarf ||
420 sig->ldtmu ||
421 sig->ldvary ||
422 sig->ldvpm ||
423 sig->ldtlb ||
424 sig->ldtlbu);
425 }
426
427 /**
428 * This function is responsible for getting VIR results into the associated
429 * storage for a NIR instruction.
430 *
431 * If it's a NIR SSA def, then we just set the associated hash table entry to
432 * the new result.
433 *
434 * If it's a NIR reg, then we need to update the existing qreg assigned to the
435 * NIR destination with the incoming value. To do that without introducing
436 * new MOVs, we require that the incoming qreg either be a uniform, or be
437 * SSA-defined by the previous VIR instruction in the block and rewritable by
438 * this function. That lets us sneak ahead and insert the SF flag beforehand
439 * (knowing that the previous instruction doesn't depend on flags) and rewrite
440 * its destination to be the NIR reg's destination
441 */
442 void
443 ntq_store_dest(struct v3d_compile *c, nir_dest *dest, int chan,
444 struct qreg result)
445 {
446 struct qinst *last_inst = NULL;
447 if (!list_is_empty(&c->cur_block->instructions))
448 last_inst = (struct qinst *)c->cur_block->instructions.prev;
449
450 assert((result.file == QFILE_TEMP &&
451 last_inst && last_inst == c->defs[result.index]));
452
453 if (dest->is_ssa) {
454 assert(chan < dest->ssa.num_components);
455
456 struct qreg *qregs;
457 struct hash_entry *entry =
458 _mesa_hash_table_search(c->def_ht, &dest->ssa);
459
460 if (entry)
461 qregs = entry->data;
462 else
463 qregs = ntq_init_ssa_def(c, &dest->ssa);
464
465 qregs[chan] = result;
466 } else {
467 nir_register *reg = dest->reg.reg;
468 assert(dest->reg.base_offset == 0);
469 assert(reg->num_array_elems == 0);
470 struct hash_entry *entry =
471 _mesa_hash_table_search(c->def_ht, reg);
472 struct qreg *qregs = entry->data;
473
474 /* If the previous instruction can't be predicated for
475 * the store into the nir_register, then emit a MOV
476 * that can be.
477 */
478 if (vir_in_nonuniform_control_flow(c) &&
479 is_ld_signal(&c->defs[last_inst->dst.index]->qpu.sig)) {
480 result = vir_MOV(c, result);
481 last_inst = c->defs[result.index];
482 }
483
484 /* We know they're both temps, so just rewrite index. */
485 c->defs[last_inst->dst.index] = NULL;
486 last_inst->dst.index = qregs[chan].index;
487
488 /* If we're in control flow, then make this update of the reg
489 * conditional on the execution mask.
490 */
491 if (vir_in_nonuniform_control_flow(c)) {
492 last_inst->dst.index = qregs[chan].index;
493
494 /* Set the flags to the current exec mask.
495 */
496 c->cursor = vir_before_inst(last_inst);
497 vir_set_pf(vir_MOV_dest(c, vir_nop_reg(), c->execute),
498 V3D_QPU_PF_PUSHZ);
499 c->cursor = vir_after_inst(last_inst);
500
501 vir_set_cond(last_inst, V3D_QPU_COND_IFA);
502 }
503 }
504 }
505
506 struct qreg
507 ntq_get_src(struct v3d_compile *c, nir_src src, int i)
508 {
509 struct hash_entry *entry;
510 if (src.is_ssa) {
511 entry = _mesa_hash_table_search(c->def_ht, src.ssa);
512 assert(i < src.ssa->num_components);
513 } else {
514 nir_register *reg = src.reg.reg;
515 entry = _mesa_hash_table_search(c->def_ht, reg);
516 assert(reg->num_array_elems == 0);
517 assert(src.reg.base_offset == 0);
518 assert(i < reg->num_components);
519 }
520
521 struct qreg *qregs = entry->data;
522 return qregs[i];
523 }
524
525 static struct qreg
526 ntq_get_alu_src(struct v3d_compile *c, nir_alu_instr *instr,
527 unsigned src)
528 {
529 assert(util_is_power_of_two_or_zero(instr->dest.write_mask));
530 unsigned chan = ffs(instr->dest.write_mask) - 1;
531 struct qreg r = ntq_get_src(c, instr->src[src].src,
532 instr->src[src].swizzle[chan]);
533
534 assert(!instr->src[src].abs);
535 assert(!instr->src[src].negate);
536
537 return r;
538 };
539
540 static struct qreg
541 ntq_minify(struct v3d_compile *c, struct qreg size, struct qreg level)
542 {
543 return vir_MAX(c, vir_SHR(c, size, level), vir_uniform_ui(c, 1));
544 }
545
546 static void
547 ntq_emit_txs(struct v3d_compile *c, nir_tex_instr *instr)
548 {
549 unsigned unit = instr->texture_index;
550 int lod_index = nir_tex_instr_src_index(instr, nir_tex_src_lod);
551 int dest_size = nir_tex_instr_dest_size(instr);
552
553 struct qreg lod = c->undef;
554 if (lod_index != -1)
555 lod = ntq_get_src(c, instr->src[lod_index].src, 0);
556
557 for (int i = 0; i < dest_size; i++) {
558 assert(i < 3);
559 enum quniform_contents contents;
560
561 if (instr->is_array && i == dest_size - 1)
562 contents = QUNIFORM_TEXTURE_ARRAY_SIZE;
563 else
564 contents = QUNIFORM_TEXTURE_WIDTH + i;
565
566 struct qreg size = vir_uniform(c, contents, unit);
567
568 switch (instr->sampler_dim) {
569 case GLSL_SAMPLER_DIM_1D:
570 case GLSL_SAMPLER_DIM_2D:
571 case GLSL_SAMPLER_DIM_MS:
572 case GLSL_SAMPLER_DIM_3D:
573 case GLSL_SAMPLER_DIM_CUBE:
574 /* Don't minify the array size. */
575 if (!(instr->is_array && i == dest_size - 1)) {
576 size = ntq_minify(c, size, lod);
577 }
578 break;
579
580 case GLSL_SAMPLER_DIM_RECT:
581 /* There's no LOD field for rects */
582 break;
583
584 default:
585 unreachable("Bad sampler type");
586 }
587
588 ntq_store_dest(c, &instr->dest, i, size);
589 }
590 }
591
592 static void
593 ntq_emit_tex(struct v3d_compile *c, nir_tex_instr *instr)
594 {
595 unsigned unit = instr->texture_index;
596
597 /* Since each texture sampling op requires uploading uniforms to
598 * reference the texture, there's no HW support for texture size and
599 * you just upload uniforms containing the size.
600 */
601 switch (instr->op) {
602 case nir_texop_query_levels:
603 ntq_store_dest(c, &instr->dest, 0,
604 vir_uniform(c, QUNIFORM_TEXTURE_LEVELS, unit));
605 return;
606 case nir_texop_txs:
607 ntq_emit_txs(c, instr);
608 return;
609 default:
610 break;
611 }
612
613 if (c->devinfo->ver >= 40)
614 v3d40_vir_emit_tex(c, instr);
615 else
616 v3d33_vir_emit_tex(c, instr);
617 }
618
619 static struct qreg
620 ntq_fsincos(struct v3d_compile *c, struct qreg src, bool is_cos)
621 {
622 struct qreg input = vir_FMUL(c, src, vir_uniform_f(c, 1.0f / M_PI));
623 if (is_cos)
624 input = vir_FADD(c, input, vir_uniform_f(c, 0.5));
625
626 struct qreg periods = vir_FROUND(c, input);
627 struct qreg sin_output = vir_SIN(c, vir_FSUB(c, input, periods));
628 return vir_XOR(c, sin_output, vir_SHL(c,
629 vir_FTOIN(c, periods),
630 vir_uniform_ui(c, -1)));
631 }
632
633 static struct qreg
634 ntq_fsign(struct v3d_compile *c, struct qreg src)
635 {
636 struct qreg t = vir_get_temp(c);
637
638 vir_MOV_dest(c, t, vir_uniform_f(c, 0.0));
639 vir_set_pf(vir_FMOV_dest(c, vir_nop_reg(), src), V3D_QPU_PF_PUSHZ);
640 vir_MOV_cond(c, V3D_QPU_COND_IFNA, t, vir_uniform_f(c, 1.0));
641 vir_set_pf(vir_FMOV_dest(c, vir_nop_reg(), src), V3D_QPU_PF_PUSHN);
642 vir_MOV_cond(c, V3D_QPU_COND_IFA, t, vir_uniform_f(c, -1.0));
643 return vir_MOV(c, t);
644 }
645
646 static void
647 emit_fragcoord_input(struct v3d_compile *c, int attr)
648 {
649 c->inputs[attr * 4 + 0] = vir_FXCD(c);
650 c->inputs[attr * 4 + 1] = vir_FYCD(c);
651 c->inputs[attr * 4 + 2] = c->payload_z;
652 c->inputs[attr * 4 + 3] = vir_RECIP(c, c->payload_w);
653 }
654
655 static struct qreg
656 emit_fragment_varying(struct v3d_compile *c, nir_variable *var,
657 uint8_t swizzle, int array_index)
658 {
659 struct qreg r3 = vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_R3);
660 struct qreg r5 = vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_R5);
661
662 struct qreg vary;
663 if (c->devinfo->ver >= 41) {
664 struct qinst *ldvary = vir_add_inst(V3D_QPU_A_NOP, c->undef,
665 c->undef, c->undef);
666 ldvary->qpu.sig.ldvary = true;
667 vary = vir_emit_def(c, ldvary);
668 } else {
669 vir_NOP(c)->qpu.sig.ldvary = true;
670 vary = r3;
671 }
672
673 /* For gl_PointCoord input or distance along a line, we'll be called
674 * with no nir_variable, and we don't count toward VPM size so we
675 * don't track an input slot.
676 */
677 if (!var) {
678 return vir_FADD(c, vir_FMUL(c, vary, c->payload_w), r5);
679 }
680
681 int i = c->num_inputs++;
682 c->input_slots[i] =
683 v3d_slot_from_slot_and_component(var->data.location +
684 array_index, swizzle);
685
686 switch (var->data.interpolation) {
687 case INTERP_MODE_NONE:
688 /* If a gl_FrontColor or gl_BackColor input has no interp
689 * qualifier, then if we're using glShadeModel(GL_FLAT) it
690 * needs to be flat shaded.
691 */
692 switch (var->data.location + array_index) {
693 case VARYING_SLOT_COL0:
694 case VARYING_SLOT_COL1:
695 case VARYING_SLOT_BFC0:
696 case VARYING_SLOT_BFC1:
697 if (c->fs_key->shade_model_flat) {
698 BITSET_SET(c->flat_shade_flags, i);
699 vir_MOV_dest(c, c->undef, vary);
700 return vir_MOV(c, r5);
701 } else {
702 return vir_FADD(c, vir_FMUL(c, vary,
703 c->payload_w), r5);
704 }
705 default:
706 break;
707 }
708 /* FALLTHROUGH */
709 case INTERP_MODE_SMOOTH:
710 if (var->data.centroid) {
711 BITSET_SET(c->centroid_flags, i);
712 return vir_FADD(c, vir_FMUL(c, vary,
713 c->payload_w_centroid), r5);
714 } else {
715 return vir_FADD(c, vir_FMUL(c, vary, c->payload_w), r5);
716 }
717 case INTERP_MODE_NOPERSPECTIVE:
718 BITSET_SET(c->noperspective_flags, i);
719 return vir_FADD(c, vir_MOV(c, vary), r5);
720 case INTERP_MODE_FLAT:
721 BITSET_SET(c->flat_shade_flags, i);
722 vir_MOV_dest(c, c->undef, vary);
723 return vir_MOV(c, r5);
724 default:
725 unreachable("Bad interp mode");
726 }
727 }
728
729 static void
730 emit_fragment_input(struct v3d_compile *c, int attr, nir_variable *var,
731 int array_index)
732 {
733 for (int i = 0; i < glsl_get_vector_elements(var->type); i++) {
734 int chan = var->data.location_frac + i;
735 c->inputs[attr * 4 + chan] =
736 emit_fragment_varying(c, var, chan, array_index);
737 }
738 }
739
740 static void
741 add_output(struct v3d_compile *c,
742 uint32_t decl_offset,
743 uint8_t slot,
744 uint8_t swizzle)
745 {
746 uint32_t old_array_size = c->outputs_array_size;
747 resize_qreg_array(c, &c->outputs, &c->outputs_array_size,
748 decl_offset + 1);
749
750 if (old_array_size != c->outputs_array_size) {
751 c->output_slots = reralloc(c,
752 c->output_slots,
753 struct v3d_varying_slot,
754 c->outputs_array_size);
755 }
756
757 c->output_slots[decl_offset] =
758 v3d_slot_from_slot_and_component(slot, swizzle);
759 }
760
761 /**
762 * If compare_instr is a valid comparison instruction, emits the
763 * compare_instr's comparison and returns the sel_instr's return value based
764 * on the compare_instr's result.
765 */
766 static bool
767 ntq_emit_comparison(struct v3d_compile *c,
768 nir_alu_instr *compare_instr,
769 enum v3d_qpu_cond *out_cond)
770 {
771 struct qreg src0 = ntq_get_alu_src(c, compare_instr, 0);
772 struct qreg src1;
773 if (nir_op_infos[compare_instr->op].num_inputs > 1)
774 src1 = ntq_get_alu_src(c, compare_instr, 1);
775 bool cond_invert = false;
776 struct qreg nop = vir_nop_reg();
777
778 switch (compare_instr->op) {
779 case nir_op_feq32:
780 case nir_op_seq:
781 vir_set_pf(vir_FCMP_dest(c, nop, src0, src1), V3D_QPU_PF_PUSHZ);
782 break;
783 case nir_op_ieq32:
784 vir_set_pf(vir_XOR_dest(c, nop, src0, src1), V3D_QPU_PF_PUSHZ);
785 break;
786
787 case nir_op_fne32:
788 case nir_op_sne:
789 vir_set_pf(vir_FCMP_dest(c, nop, src0, src1), V3D_QPU_PF_PUSHZ);
790 cond_invert = true;
791 break;
792 case nir_op_ine32:
793 vir_set_pf(vir_XOR_dest(c, nop, src0, src1), V3D_QPU_PF_PUSHZ);
794 cond_invert = true;
795 break;
796
797 case nir_op_fge32:
798 case nir_op_sge:
799 vir_set_pf(vir_FCMP_dest(c, nop, src1, src0), V3D_QPU_PF_PUSHC);
800 break;
801 case nir_op_ige32:
802 vir_set_pf(vir_MIN_dest(c, nop, src1, src0), V3D_QPU_PF_PUSHC);
803 cond_invert = true;
804 break;
805 case nir_op_uge32:
806 vir_set_pf(vir_SUB_dest(c, nop, src0, src1), V3D_QPU_PF_PUSHC);
807 cond_invert = true;
808 break;
809
810 case nir_op_slt:
811 case nir_op_flt32:
812 vir_set_pf(vir_FCMP_dest(c, nop, src0, src1), V3D_QPU_PF_PUSHN);
813 break;
814 case nir_op_ilt32:
815 vir_set_pf(vir_MIN_dest(c, nop, src1, src0), V3D_QPU_PF_PUSHC);
816 break;
817 case nir_op_ult32:
818 vir_set_pf(vir_SUB_dest(c, nop, src0, src1), V3D_QPU_PF_PUSHC);
819 break;
820
821 case nir_op_i2b32:
822 vir_set_pf(vir_MOV_dest(c, nop, src0), V3D_QPU_PF_PUSHZ);
823 cond_invert = true;
824 break;
825
826 case nir_op_f2b32:
827 vir_set_pf(vir_FMOV_dest(c, nop, src0), V3D_QPU_PF_PUSHZ);
828 cond_invert = true;
829 break;
830
831 default:
832 return false;
833 }
834
835 *out_cond = cond_invert ? V3D_QPU_COND_IFNA : V3D_QPU_COND_IFA;
836
837 return true;
838 }
839
840 /* Finds an ALU instruction that generates our src value that could
841 * (potentially) be greedily emitted in the consuming instruction.
842 */
843 static struct nir_alu_instr *
844 ntq_get_alu_parent(nir_src src)
845 {
846 if (!src.is_ssa || src.ssa->parent_instr->type != nir_instr_type_alu)
847 return NULL;
848 nir_alu_instr *instr = nir_instr_as_alu(src.ssa->parent_instr);
849 if (!instr)
850 return NULL;
851
852 /* If the ALU instr's srcs are non-SSA, then we would have to avoid
853 * moving emission of the ALU instr down past another write of the
854 * src.
855 */
856 for (int i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
857 if (!instr->src[i].src.is_ssa)
858 return NULL;
859 }
860
861 return instr;
862 }
863
864 /* Turns a NIR bool into a condition code to predicate on. */
865 static enum v3d_qpu_cond
866 ntq_emit_bool_to_cond(struct v3d_compile *c, nir_src src)
867 {
868 nir_alu_instr *compare = ntq_get_alu_parent(src);
869 if (!compare)
870 goto out;
871
872 enum v3d_qpu_cond cond;
873 if (ntq_emit_comparison(c, compare, &cond))
874 return cond;
875
876 out:
877 vir_set_pf(vir_MOV_dest(c, vir_nop_reg(), ntq_get_src(c, src, 0)),
878 V3D_QPU_PF_PUSHZ);
879 return V3D_QPU_COND_IFNA;
880 }
881
882 static void
883 ntq_emit_alu(struct v3d_compile *c, nir_alu_instr *instr)
884 {
885 /* This should always be lowered to ALU operations for V3D. */
886 assert(!instr->dest.saturate);
887
888 /* Vectors are special in that they have non-scalarized writemasks,
889 * and just take the first swizzle channel for each argument in order
890 * into each writemask channel.
891 */
892 if (instr->op == nir_op_vec2 ||
893 instr->op == nir_op_vec3 ||
894 instr->op == nir_op_vec4) {
895 struct qreg srcs[4];
896 for (int i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
897 srcs[i] = ntq_get_src(c, instr->src[i].src,
898 instr->src[i].swizzle[0]);
899 for (int i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
900 ntq_store_dest(c, &instr->dest.dest, i,
901 vir_MOV(c, srcs[i]));
902 return;
903 }
904
905 /* General case: We can just grab the one used channel per src. */
906 struct qreg src[nir_op_infos[instr->op].num_inputs];
907 for (int i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
908 src[i] = ntq_get_alu_src(c, instr, i);
909 }
910
911 struct qreg result;
912
913 switch (instr->op) {
914 case nir_op_mov:
915 result = vir_MOV(c, src[0]);
916 break;
917
918 case nir_op_fneg:
919 result = vir_XOR(c, src[0], vir_uniform_ui(c, 1 << 31));
920 break;
921 case nir_op_ineg:
922 result = vir_NEG(c, src[0]);
923 break;
924
925 case nir_op_fmul:
926 result = vir_FMUL(c, src[0], src[1]);
927 break;
928 case nir_op_fadd:
929 result = vir_FADD(c, src[0], src[1]);
930 break;
931 case nir_op_fsub:
932 result = vir_FSUB(c, src[0], src[1]);
933 break;
934 case nir_op_fmin:
935 result = vir_FMIN(c, src[0], src[1]);
936 break;
937 case nir_op_fmax:
938 result = vir_FMAX(c, src[0], src[1]);
939 break;
940
941 case nir_op_f2i32: {
942 nir_alu_instr *src0_alu = ntq_get_alu_parent(instr->src[0].src);
943 if (src0_alu && src0_alu->op == nir_op_fround_even) {
944 result = vir_FTOIN(c, ntq_get_alu_src(c, src0_alu, 0));
945 } else {
946 result = vir_FTOIZ(c, src[0]);
947 }
948 break;
949 }
950
951 case nir_op_f2u32:
952 result = vir_FTOUZ(c, src[0]);
953 break;
954 case nir_op_i2f32:
955 result = vir_ITOF(c, src[0]);
956 break;
957 case nir_op_u2f32:
958 result = vir_UTOF(c, src[0]);
959 break;
960 case nir_op_b2f32:
961 result = vir_AND(c, src[0], vir_uniform_f(c, 1.0));
962 break;
963 case nir_op_b2i32:
964 result = vir_AND(c, src[0], vir_uniform_ui(c, 1));
965 break;
966
967 case nir_op_iadd:
968 result = vir_ADD(c, src[0], src[1]);
969 break;
970 case nir_op_ushr:
971 result = vir_SHR(c, src[0], src[1]);
972 break;
973 case nir_op_isub:
974 result = vir_SUB(c, src[0], src[1]);
975 break;
976 case nir_op_ishr:
977 result = vir_ASR(c, src[0], src[1]);
978 break;
979 case nir_op_ishl:
980 result = vir_SHL(c, src[0], src[1]);
981 break;
982 case nir_op_imin:
983 result = vir_MIN(c, src[0], src[1]);
984 break;
985 case nir_op_umin:
986 result = vir_UMIN(c, src[0], src[1]);
987 break;
988 case nir_op_imax:
989 result = vir_MAX(c, src[0], src[1]);
990 break;
991 case nir_op_umax:
992 result = vir_UMAX(c, src[0], src[1]);
993 break;
994 case nir_op_iand:
995 result = vir_AND(c, src[0], src[1]);
996 break;
997 case nir_op_ior:
998 result = vir_OR(c, src[0], src[1]);
999 break;
1000 case nir_op_ixor:
1001 result = vir_XOR(c, src[0], src[1]);
1002 break;
1003 case nir_op_inot:
1004 result = vir_NOT(c, src[0]);
1005 break;
1006
1007 case nir_op_ufind_msb:
1008 result = vir_SUB(c, vir_uniform_ui(c, 31), vir_CLZ(c, src[0]));
1009 break;
1010
1011 case nir_op_imul:
1012 result = vir_UMUL(c, src[0], src[1]);
1013 break;
1014
1015 case nir_op_seq:
1016 case nir_op_sne:
1017 case nir_op_sge:
1018 case nir_op_slt: {
1019 enum v3d_qpu_cond cond;
1020 ASSERTED bool ok = ntq_emit_comparison(c, instr, &cond);
1021 assert(ok);
1022 result = vir_MOV(c, vir_SEL(c, cond,
1023 vir_uniform_f(c, 1.0),
1024 vir_uniform_f(c, 0.0)));
1025 break;
1026 }
1027
1028 case nir_op_i2b32:
1029 case nir_op_f2b32:
1030 case nir_op_feq32:
1031 case nir_op_fne32:
1032 case nir_op_fge32:
1033 case nir_op_flt32:
1034 case nir_op_ieq32:
1035 case nir_op_ine32:
1036 case nir_op_ige32:
1037 case nir_op_uge32:
1038 case nir_op_ilt32:
1039 case nir_op_ult32: {
1040 enum v3d_qpu_cond cond;
1041 ASSERTED bool ok = ntq_emit_comparison(c, instr, &cond);
1042 assert(ok);
1043 result = vir_MOV(c, vir_SEL(c, cond,
1044 vir_uniform_ui(c, ~0),
1045 vir_uniform_ui(c, 0)));
1046 break;
1047 }
1048
1049 case nir_op_b32csel:
1050 result = vir_MOV(c,
1051 vir_SEL(c,
1052 ntq_emit_bool_to_cond(c, instr->src[0].src),
1053 src[1], src[2]));
1054 break;
1055
1056 case nir_op_fcsel:
1057 vir_set_pf(vir_MOV_dest(c, vir_nop_reg(), src[0]),
1058 V3D_QPU_PF_PUSHZ);
1059 result = vir_MOV(c, vir_SEL(c, V3D_QPU_COND_IFNA,
1060 src[1], src[2]));
1061 break;
1062
1063 case nir_op_frcp:
1064 result = vir_RECIP(c, src[0]);
1065 break;
1066 case nir_op_frsq:
1067 result = vir_RSQRT(c, src[0]);
1068 break;
1069 case nir_op_fexp2:
1070 result = vir_EXP(c, src[0]);
1071 break;
1072 case nir_op_flog2:
1073 result = vir_LOG(c, src[0]);
1074 break;
1075
1076 case nir_op_fceil:
1077 result = vir_FCEIL(c, src[0]);
1078 break;
1079 case nir_op_ffloor:
1080 result = vir_FFLOOR(c, src[0]);
1081 break;
1082 case nir_op_fround_even:
1083 result = vir_FROUND(c, src[0]);
1084 break;
1085 case nir_op_ftrunc:
1086 result = vir_FTRUNC(c, src[0]);
1087 break;
1088
1089 case nir_op_fsin:
1090 result = ntq_fsincos(c, src[0], false);
1091 break;
1092 case nir_op_fcos:
1093 result = ntq_fsincos(c, src[0], true);
1094 break;
1095
1096 case nir_op_fsign:
1097 result = ntq_fsign(c, src[0]);
1098 break;
1099
1100 case nir_op_fabs: {
1101 result = vir_FMOV(c, src[0]);
1102 vir_set_unpack(c->defs[result.index], 0, V3D_QPU_UNPACK_ABS);
1103 break;
1104 }
1105
1106 case nir_op_iabs:
1107 result = vir_MAX(c, src[0], vir_NEG(c, src[0]));
1108 break;
1109
1110 case nir_op_fddx:
1111 case nir_op_fddx_coarse:
1112 case nir_op_fddx_fine:
1113 result = vir_FDX(c, src[0]);
1114 break;
1115
1116 case nir_op_fddy:
1117 case nir_op_fddy_coarse:
1118 case nir_op_fddy_fine:
1119 result = vir_FDY(c, src[0]);
1120 break;
1121
1122 case nir_op_uadd_carry:
1123 vir_set_pf(vir_ADD_dest(c, vir_nop_reg(), src[0], src[1]),
1124 V3D_QPU_PF_PUSHC);
1125 result = vir_MOV(c, vir_SEL(c, V3D_QPU_COND_IFA,
1126 vir_uniform_ui(c, ~0),
1127 vir_uniform_ui(c, 0)));
1128 break;
1129
1130 case nir_op_pack_half_2x16_split:
1131 result = vir_VFPACK(c, src[0], src[1]);
1132 break;
1133
1134 case nir_op_unpack_half_2x16_split_x:
1135 result = vir_FMOV(c, src[0]);
1136 vir_set_unpack(c->defs[result.index], 0, V3D_QPU_UNPACK_L);
1137 break;
1138
1139 case nir_op_unpack_half_2x16_split_y:
1140 result = vir_FMOV(c, src[0]);
1141 vir_set_unpack(c->defs[result.index], 0, V3D_QPU_UNPACK_H);
1142 break;
1143
1144 default:
1145 fprintf(stderr, "unknown NIR ALU inst: ");
1146 nir_print_instr(&instr->instr, stderr);
1147 fprintf(stderr, "\n");
1148 abort();
1149 }
1150
1151 /* We have a scalar result, so the instruction should only have a
1152 * single channel written to.
1153 */
1154 assert(util_is_power_of_two_or_zero(instr->dest.write_mask));
1155 ntq_store_dest(c, &instr->dest.dest,
1156 ffs(instr->dest.write_mask) - 1, result);
1157 }
1158
1159 /* Each TLB read/write setup (a render target or depth buffer) takes an 8-bit
1160 * specifier. They come from a register that's preloaded with 0xffffffff
1161 * (0xff gets you normal vec4 f16 RT0 writes), and when one is neaded the low
1162 * 8 bits are shifted off the bottom and 0xff shifted in from the top.
1163 */
1164 #define TLB_TYPE_F16_COLOR (3 << 6)
1165 #define TLB_TYPE_I32_COLOR (1 << 6)
1166 #define TLB_TYPE_F32_COLOR (0 << 6)
1167 #define TLB_RENDER_TARGET_SHIFT 3 /* Reversed! 7 = RT 0, 0 = RT 7. */
1168 #define TLB_SAMPLE_MODE_PER_SAMPLE (0 << 2)
1169 #define TLB_SAMPLE_MODE_PER_PIXEL (1 << 2)
1170 #define TLB_F16_SWAP_HI_LO (1 << 1)
1171 #define TLB_VEC_SIZE_4_F16 (1 << 0)
1172 #define TLB_VEC_SIZE_2_F16 (0 << 0)
1173 #define TLB_VEC_SIZE_MINUS_1_SHIFT 0
1174
1175 /* Triggers Z/Stencil testing, used when the shader state's "FS modifies Z"
1176 * flag is set.
1177 */
1178 #define TLB_TYPE_DEPTH ((2 << 6) | (0 << 4))
1179 #define TLB_DEPTH_TYPE_INVARIANT (0 << 2) /* Unmodified sideband input used */
1180 #define TLB_DEPTH_TYPE_PER_PIXEL (1 << 2) /* QPU result used */
1181 #define TLB_V42_DEPTH_TYPE_INVARIANT (0 << 3) /* Unmodified sideband input used */
1182 #define TLB_V42_DEPTH_TYPE_PER_PIXEL (1 << 3) /* QPU result used */
1183
1184 /* Stencil is a single 32-bit write. */
1185 #define TLB_TYPE_STENCIL_ALPHA ((2 << 6) | (1 << 4))
1186
1187 static void
1188 vir_emit_tlb_color_write(struct v3d_compile *c, unsigned rt)
1189 {
1190 if (!(c->fs_key->cbufs & (1 << rt)) || !c->output_color_var[rt])
1191 return;
1192
1193 struct qreg tlb_reg = vir_magic_reg(V3D_QPU_WADDR_TLB);
1194 struct qreg tlbu_reg = vir_magic_reg(V3D_QPU_WADDR_TLBU);
1195
1196 nir_variable *var = c->output_color_var[rt];
1197 int num_components = glsl_get_vector_elements(var->type);
1198 uint32_t conf = 0xffffff00;
1199 struct qinst *inst;
1200
1201 conf |= c->msaa_per_sample_output ? TLB_SAMPLE_MODE_PER_SAMPLE :
1202 TLB_SAMPLE_MODE_PER_PIXEL;
1203 conf |= (7 - rt) << TLB_RENDER_TARGET_SHIFT;
1204
1205 if (c->fs_key->swap_color_rb & (1 << rt))
1206 num_components = MAX2(num_components, 3);
1207 assert(num_components != 0);
1208
1209 enum glsl_base_type type = glsl_get_base_type(var->type);
1210 bool is_int_format = type == GLSL_TYPE_INT || type == GLSL_TYPE_UINT;
1211 bool is_32b_tlb_format = is_int_format ||
1212 (c->fs_key->f32_color_rb & (1 << rt));
1213
1214 if (is_int_format) {
1215 /* The F32 vs I32 distinction was dropped in 4.2. */
1216 if (c->devinfo->ver < 42)
1217 conf |= TLB_TYPE_I32_COLOR;
1218 else
1219 conf |= TLB_TYPE_F32_COLOR;
1220 conf |= ((num_components - 1) << TLB_VEC_SIZE_MINUS_1_SHIFT);
1221 } else {
1222 if (c->fs_key->f32_color_rb & (1 << rt)) {
1223 conf |= TLB_TYPE_F32_COLOR;
1224 conf |= ((num_components - 1) <<
1225 TLB_VEC_SIZE_MINUS_1_SHIFT);
1226 } else {
1227 conf |= TLB_TYPE_F16_COLOR;
1228 conf |= TLB_F16_SWAP_HI_LO;
1229 if (num_components >= 3)
1230 conf |= TLB_VEC_SIZE_4_F16;
1231 else
1232 conf |= TLB_VEC_SIZE_2_F16;
1233 }
1234 }
1235
1236 int num_samples = c->msaa_per_sample_output ? V3D_MAX_SAMPLES : 1;
1237 for (int i = 0; i < num_samples; i++) {
1238 struct qreg *color = c->msaa_per_sample_output ?
1239 &c->sample_colors[(rt * V3D_MAX_SAMPLES + i) * 4] :
1240 &c->outputs[var->data.driver_location * 4];
1241
1242 struct qreg r = color[0];
1243 struct qreg g = color[1];
1244 struct qreg b = color[2];
1245 struct qreg a = color[3];
1246
1247 if (c->fs_key->swap_color_rb & (1 << rt)) {
1248 r = color[2];
1249 b = color[0];
1250 }
1251
1252 if (c->fs_key->sample_alpha_to_one)
1253 a = vir_uniform_f(c, 1.0);
1254
1255 if (is_32b_tlb_format) {
1256 if (i == 0) {
1257 inst = vir_MOV_dest(c, tlbu_reg, r);
1258 inst->uniform =
1259 vir_get_uniform_index(c,
1260 QUNIFORM_CONSTANT,
1261 conf);
1262 } else {
1263 inst = vir_MOV_dest(c, tlb_reg, r);
1264 }
1265
1266 if (num_components >= 2)
1267 vir_MOV_dest(c, tlb_reg, g);
1268 if (num_components >= 3)
1269 vir_MOV_dest(c, tlb_reg, b);
1270 if (num_components >= 4)
1271 vir_MOV_dest(c, tlb_reg, a);
1272 } else {
1273 inst = vir_VFPACK_dest(c, tlb_reg, r, g);
1274 if (conf != ~0 && i == 0) {
1275 inst->dst = tlbu_reg;
1276 inst->uniform =
1277 vir_get_uniform_index(c,
1278 QUNIFORM_CONSTANT,
1279 conf);
1280 }
1281
1282 if (num_components >= 3)
1283 inst = vir_VFPACK_dest(c, tlb_reg, b, a);
1284 }
1285 }
1286 }
1287
1288 static void
1289 emit_frag_end(struct v3d_compile *c)
1290 {
1291 /* XXX
1292 if (c->output_sample_mask_index != -1) {
1293 vir_MS_MASK(c, c->outputs[c->output_sample_mask_index]);
1294 }
1295 */
1296
1297 bool has_any_tlb_color_write = false;
1298 for (int rt = 0; rt < V3D_MAX_DRAW_BUFFERS; rt++) {
1299 if (c->fs_key->cbufs & (1 << rt) && c->output_color_var[rt])
1300 has_any_tlb_color_write = true;
1301 }
1302
1303 if (c->fs_key->sample_alpha_to_coverage && c->output_color_var[0]) {
1304 struct nir_variable *var = c->output_color_var[0];
1305 struct qreg *color = &c->outputs[var->data.driver_location * 4];
1306
1307 vir_SETMSF_dest(c, vir_nop_reg(),
1308 vir_AND(c,
1309 vir_MSF(c),
1310 vir_FTOC(c, color[3])));
1311 }
1312
1313 struct qreg tlbu_reg = vir_magic_reg(V3D_QPU_WADDR_TLBU);
1314 if (c->output_position_index != -1) {
1315 struct qinst *inst = vir_MOV_dest(c, tlbu_reg,
1316 c->outputs[c->output_position_index]);
1317 uint8_t tlb_specifier = TLB_TYPE_DEPTH;
1318
1319 if (c->devinfo->ver >= 42) {
1320 tlb_specifier |= (TLB_V42_DEPTH_TYPE_PER_PIXEL |
1321 TLB_SAMPLE_MODE_PER_PIXEL);
1322 } else
1323 tlb_specifier |= TLB_DEPTH_TYPE_PER_PIXEL;
1324
1325 inst->uniform = vir_get_uniform_index(c, QUNIFORM_CONSTANT,
1326 tlb_specifier |
1327 0xffffff00);
1328 c->writes_z = true;
1329 } else if (c->s->info.fs.uses_discard ||
1330 !c->s->info.fs.early_fragment_tests ||
1331 c->fs_key->sample_alpha_to_coverage ||
1332 !has_any_tlb_color_write) {
1333 /* Emit passthrough Z if it needed to be delayed until shader
1334 * end due to potential discards.
1335 *
1336 * Since (single-threaded) fragment shaders always need a TLB
1337 * write, emit passthrouh Z if we didn't have any color
1338 * buffers and flag us as potentially discarding, so that we
1339 * can use Z as the TLB write.
1340 */
1341 c->s->info.fs.uses_discard = true;
1342
1343 struct qinst *inst = vir_MOV_dest(c, tlbu_reg,
1344 vir_nop_reg());
1345 uint8_t tlb_specifier = TLB_TYPE_DEPTH;
1346
1347 if (c->devinfo->ver >= 42) {
1348 /* The spec says the PER_PIXEL flag is ignored for
1349 * invariant writes, but the simulator demands it.
1350 */
1351 tlb_specifier |= (TLB_V42_DEPTH_TYPE_INVARIANT |
1352 TLB_SAMPLE_MODE_PER_PIXEL);
1353 } else {
1354 tlb_specifier |= TLB_DEPTH_TYPE_INVARIANT;
1355 }
1356
1357 inst->uniform = vir_get_uniform_index(c,
1358 QUNIFORM_CONSTANT,
1359 tlb_specifier |
1360 0xffffff00);
1361 c->writes_z = true;
1362 }
1363
1364 /* XXX: Performance improvement: Merge Z write and color writes TLB
1365 * uniform setup
1366 */
1367 for (int rt = 0; rt < V3D_MAX_DRAW_BUFFERS; rt++)
1368 vir_emit_tlb_color_write(c, rt);
1369 }
1370
1371 static inline void
1372 vir_VPM_WRITE_indirect(struct v3d_compile *c,
1373 struct qreg val,
1374 struct qreg vpm_index)
1375 {
1376 assert(c->devinfo->ver >= 40);
1377 vir_STVPMV(c, vpm_index, val);
1378 }
1379
1380 static void
1381 vir_VPM_WRITE(struct v3d_compile *c, struct qreg val, uint32_t vpm_index)
1382 {
1383 if (c->devinfo->ver >= 40) {
1384 vir_VPM_WRITE_indirect(c, val, vir_uniform_ui(c, vpm_index));
1385 } else {
1386 /* XXX: v3d33_vir_vpm_write_setup(c); */
1387 vir_MOV_dest(c, vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_VPM), val);
1388 }
1389 }
1390
1391 static void
1392 emit_vert_end(struct v3d_compile *c)
1393 {
1394 /* GFXH-1684: VPM writes need to be complete by the end of the shader.
1395 */
1396 if (c->devinfo->ver >= 40 && c->devinfo->ver <= 42)
1397 vir_VPMWT(c);
1398 }
1399
1400 static void
1401 emit_geom_end(struct v3d_compile *c)
1402 {
1403 /* GFXH-1684: VPM writes need to be complete by the end of the shader.
1404 */
1405 if (c->devinfo->ver >= 40 && c->devinfo->ver <= 42)
1406 vir_VPMWT(c);
1407 }
1408
1409 void
1410 v3d_optimize_nir(struct nir_shader *s)
1411 {
1412 bool progress;
1413 unsigned lower_flrp =
1414 (s->options->lower_flrp16 ? 16 : 0) |
1415 (s->options->lower_flrp32 ? 32 : 0) |
1416 (s->options->lower_flrp64 ? 64 : 0);
1417
1418 do {
1419 progress = false;
1420
1421 NIR_PASS_V(s, nir_lower_vars_to_ssa);
1422 NIR_PASS(progress, s, nir_lower_alu_to_scalar, NULL, NULL);
1423 NIR_PASS(progress, s, nir_lower_phis_to_scalar);
1424 NIR_PASS(progress, s, nir_copy_prop);
1425 NIR_PASS(progress, s, nir_opt_remove_phis);
1426 NIR_PASS(progress, s, nir_opt_dce);
1427 NIR_PASS(progress, s, nir_opt_dead_cf);
1428 NIR_PASS(progress, s, nir_opt_cse);
1429 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
1430 NIR_PASS(progress, s, nir_opt_algebraic);
1431 NIR_PASS(progress, s, nir_opt_constant_folding);
1432
1433 if (lower_flrp != 0) {
1434 bool lower_flrp_progress = false;
1435
1436 NIR_PASS(lower_flrp_progress, s, nir_lower_flrp,
1437 lower_flrp,
1438 false /* always_precise */,
1439 s->options->lower_ffma);
1440 if (lower_flrp_progress) {
1441 NIR_PASS(progress, s, nir_opt_constant_folding);
1442 progress = true;
1443 }
1444
1445 /* Nothing should rematerialize any flrps, so we only
1446 * need to do this lowering once.
1447 */
1448 lower_flrp = 0;
1449 }
1450
1451 NIR_PASS(progress, s, nir_opt_undef);
1452 } while (progress);
1453
1454 NIR_PASS(progress, s, nir_opt_move, nir_move_load_ubo);
1455 }
1456
1457 static int
1458 driver_location_compare(const void *in_a, const void *in_b)
1459 {
1460 const nir_variable *const *a = in_a;
1461 const nir_variable *const *b = in_b;
1462
1463 if ((*a)->data.driver_location == (*b)->data.driver_location)
1464 return (*a)->data.location_frac - (*b)->data.location_frac;
1465
1466 return (*a)->data.driver_location - (*b)->data.driver_location;
1467 }
1468
1469 static struct qreg
1470 ntq_emit_vpm_read(struct v3d_compile *c,
1471 uint32_t *num_components_queued,
1472 uint32_t *remaining,
1473 uint32_t vpm_index)
1474 {
1475 struct qreg vpm = vir_reg(QFILE_VPM, vpm_index);
1476
1477 if (c->devinfo->ver >= 40 ) {
1478 return vir_LDVPMV_IN(c,
1479 vir_uniform_ui(c,
1480 (*num_components_queued)++));
1481 }
1482
1483 if (*num_components_queued != 0) {
1484 (*num_components_queued)--;
1485 return vir_MOV(c, vpm);
1486 }
1487
1488 uint32_t num_components = MIN2(*remaining, 32);
1489
1490 v3d33_vir_vpm_read_setup(c, num_components);
1491
1492 *num_components_queued = num_components - 1;
1493 *remaining -= num_components;
1494
1495 return vir_MOV(c, vpm);
1496 }
1497
1498 static void
1499 ntq_setup_vs_inputs(struct v3d_compile *c)
1500 {
1501 /* Figure out how many components of each vertex attribute the shader
1502 * uses. Each variable should have been split to individual
1503 * components and unused ones DCEed. The vertex fetcher will load
1504 * from the start of the attribute to the number of components we
1505 * declare we need in c->vattr_sizes[].
1506 */
1507 nir_foreach_variable(var, &c->s->inputs) {
1508 /* No VS attribute array support. */
1509 assert(MAX2(glsl_get_length(var->type), 1) == 1);
1510
1511 unsigned loc = var->data.driver_location;
1512 int start_component = var->data.location_frac;
1513 int num_components = glsl_get_components(var->type);
1514
1515 c->vattr_sizes[loc] = MAX2(c->vattr_sizes[loc],
1516 start_component + num_components);
1517 }
1518
1519 unsigned num_components = 0;
1520 uint32_t vpm_components_queued = 0;
1521 bool uses_iid = c->s->info.system_values_read &
1522 (1ull << SYSTEM_VALUE_INSTANCE_ID);
1523 bool uses_vid = c->s->info.system_values_read &
1524 (1ull << SYSTEM_VALUE_VERTEX_ID);
1525 num_components += uses_iid;
1526 num_components += uses_vid;
1527
1528 for (int i = 0; i < ARRAY_SIZE(c->vattr_sizes); i++)
1529 num_components += c->vattr_sizes[i];
1530
1531 if (uses_iid) {
1532 c->iid = ntq_emit_vpm_read(c, &vpm_components_queued,
1533 &num_components, ~0);
1534 }
1535
1536 if (uses_vid) {
1537 c->vid = ntq_emit_vpm_read(c, &vpm_components_queued,
1538 &num_components, ~0);
1539 }
1540
1541 /* The actual loads will happen directly in nir_intrinsic_load_input
1542 * on newer versions.
1543 */
1544 if (c->devinfo->ver >= 40)
1545 return;
1546
1547 for (int loc = 0; loc < ARRAY_SIZE(c->vattr_sizes); loc++) {
1548 resize_qreg_array(c, &c->inputs, &c->inputs_array_size,
1549 (loc + 1) * 4);
1550
1551 for (int i = 0; i < c->vattr_sizes[loc]; i++) {
1552 c->inputs[loc * 4 + i] =
1553 ntq_emit_vpm_read(c,
1554 &vpm_components_queued,
1555 &num_components,
1556 loc * 4 + i);
1557
1558 }
1559 }
1560
1561 if (c->devinfo->ver >= 40) {
1562 assert(vpm_components_queued == num_components);
1563 } else {
1564 assert(vpm_components_queued == 0);
1565 assert(num_components == 0);
1566 }
1567 }
1568
1569 static bool
1570 program_reads_point_coord(struct v3d_compile *c)
1571 {
1572 nir_foreach_variable(var, &c->s->inputs) {
1573 if (util_varying_is_point_coord(var->data.location,
1574 c->fs_key->point_sprite_mask)) {
1575 return true;
1576 }
1577 }
1578
1579 return false;
1580 }
1581
1582 static void
1583 get_sorted_input_variables(struct v3d_compile *c,
1584 unsigned *num_entries,
1585 nir_variable ***vars)
1586 {
1587 *num_entries = 0;
1588 nir_foreach_variable(var, &c->s->inputs)
1589 (*num_entries)++;
1590
1591 *vars = ralloc_array(c, nir_variable *, *num_entries);
1592
1593 unsigned i = 0;
1594 nir_foreach_variable(var, &c->s->inputs)
1595 (*vars)[i++] = var;
1596
1597 /* Sort the variables so that we emit the input setup in
1598 * driver_location order. This is required for VPM reads, whose data
1599 * is fetched into the VPM in driver_location (TGSI register index)
1600 * order.
1601 */
1602 qsort(*vars, *num_entries, sizeof(**vars), driver_location_compare);
1603 }
1604
1605 static void
1606 ntq_setup_gs_inputs(struct v3d_compile *c)
1607 {
1608 nir_variable **vars;
1609 unsigned num_entries;
1610 get_sorted_input_variables(c, &num_entries, &vars);
1611
1612 for (unsigned i = 0; i < num_entries; i++) {
1613 nir_variable *var = vars[i];
1614
1615 /* All GS inputs are arrays with as many entries as vertices
1616 * in the input primitive, but here we only care about the
1617 * per-vertex input type.
1618 */
1619 const struct glsl_type *type = glsl_without_array(var->type);
1620 unsigned array_len = MAX2(glsl_get_length(type), 1);
1621 unsigned loc = var->data.driver_location;
1622
1623 resize_qreg_array(c, &c->inputs, &c->inputs_array_size,
1624 (loc + array_len) * 4);
1625
1626 for (unsigned j = 0; j < array_len; j++) {
1627 unsigned num_elements = glsl_get_vector_elements(type);
1628 for (unsigned k = 0; k < num_elements; k++) {
1629 unsigned chan = var->data.location_frac + k;
1630 unsigned input_idx = c->num_inputs++;
1631 struct v3d_varying_slot slot =
1632 v3d_slot_from_slot_and_component(var->data.location + j, chan);
1633 c->input_slots[input_idx] = slot;
1634 }
1635 }
1636 }
1637 }
1638
1639
1640 static void
1641 ntq_setup_fs_inputs(struct v3d_compile *c)
1642 {
1643 nir_variable **vars;
1644 unsigned num_entries;
1645 get_sorted_input_variables(c, &num_entries, &vars);
1646
1647 for (unsigned i = 0; i < num_entries; i++) {
1648 nir_variable *var = vars[i];
1649 unsigned array_len = MAX2(glsl_get_length(var->type), 1);
1650 unsigned loc = var->data.driver_location;
1651
1652 resize_qreg_array(c, &c->inputs, &c->inputs_array_size,
1653 (loc + array_len) * 4);
1654
1655 if (var->data.location == VARYING_SLOT_POS) {
1656 emit_fragcoord_input(c, loc);
1657 } else if (util_varying_is_point_coord(var->data.location,
1658 c->fs_key->point_sprite_mask)) {
1659 c->inputs[loc * 4 + 0] = c->point_x;
1660 c->inputs[loc * 4 + 1] = c->point_y;
1661 } else {
1662 for (int j = 0; j < array_len; j++)
1663 emit_fragment_input(c, loc + j, var, j);
1664 }
1665 }
1666 }
1667
1668 static void
1669 ntq_setup_outputs(struct v3d_compile *c)
1670 {
1671 if (c->s->info.stage != MESA_SHADER_FRAGMENT)
1672 return;
1673
1674 nir_foreach_variable(var, &c->s->outputs) {
1675 unsigned array_len = MAX2(glsl_get_length(var->type), 1);
1676 unsigned loc = var->data.driver_location * 4;
1677
1678 assert(array_len == 1);
1679 (void)array_len;
1680
1681 for (int i = 0; i < 4 - var->data.location_frac; i++) {
1682 add_output(c, loc + var->data.location_frac + i,
1683 var->data.location,
1684 var->data.location_frac + i);
1685 }
1686
1687 switch (var->data.location) {
1688 case FRAG_RESULT_COLOR:
1689 c->output_color_var[0] = var;
1690 c->output_color_var[1] = var;
1691 c->output_color_var[2] = var;
1692 c->output_color_var[3] = var;
1693 break;
1694 case FRAG_RESULT_DATA0:
1695 case FRAG_RESULT_DATA1:
1696 case FRAG_RESULT_DATA2:
1697 case FRAG_RESULT_DATA3:
1698 c->output_color_var[var->data.location -
1699 FRAG_RESULT_DATA0] = var;
1700 break;
1701 case FRAG_RESULT_DEPTH:
1702 c->output_position_index = loc;
1703 break;
1704 case FRAG_RESULT_SAMPLE_MASK:
1705 c->output_sample_mask_index = loc;
1706 break;
1707 }
1708 }
1709 }
1710
1711 /**
1712 * Sets up the mapping from nir_register to struct qreg *.
1713 *
1714 * Each nir_register gets a struct qreg per 32-bit component being stored.
1715 */
1716 static void
1717 ntq_setup_registers(struct v3d_compile *c, struct exec_list *list)
1718 {
1719 foreach_list_typed(nir_register, nir_reg, node, list) {
1720 unsigned array_len = MAX2(nir_reg->num_array_elems, 1);
1721 struct qreg *qregs = ralloc_array(c->def_ht, struct qreg,
1722 array_len *
1723 nir_reg->num_components);
1724
1725 _mesa_hash_table_insert(c->def_ht, nir_reg, qregs);
1726
1727 for (int i = 0; i < array_len * nir_reg->num_components; i++)
1728 qregs[i] = vir_get_temp(c);
1729 }
1730 }
1731
1732 static void
1733 ntq_emit_load_const(struct v3d_compile *c, nir_load_const_instr *instr)
1734 {
1735 /* XXX perf: Experiment with using immediate loads to avoid having
1736 * these end up in the uniform stream. Watch out for breaking the
1737 * small immediates optimization in the process!
1738 */
1739 struct qreg *qregs = ntq_init_ssa_def(c, &instr->def);
1740 for (int i = 0; i < instr->def.num_components; i++)
1741 qregs[i] = vir_uniform_ui(c, instr->value[i].u32);
1742
1743 _mesa_hash_table_insert(c->def_ht, &instr->def, qregs);
1744 }
1745
1746 static void
1747 ntq_emit_ssa_undef(struct v3d_compile *c, nir_ssa_undef_instr *instr)
1748 {
1749 struct qreg *qregs = ntq_init_ssa_def(c, &instr->def);
1750
1751 /* VIR needs there to be *some* value, so pick 0 (same as for
1752 * ntq_setup_registers().
1753 */
1754 for (int i = 0; i < instr->def.num_components; i++)
1755 qregs[i] = vir_uniform_ui(c, 0);
1756 }
1757
1758 static void
1759 ntq_emit_image_size(struct v3d_compile *c, nir_intrinsic_instr *instr)
1760 {
1761 unsigned image_index = nir_src_as_uint(instr->src[0]);
1762 bool is_array = nir_intrinsic_image_array(instr);
1763
1764 ntq_store_dest(c, &instr->dest, 0,
1765 vir_uniform(c, QUNIFORM_IMAGE_WIDTH, image_index));
1766 if (instr->num_components > 1) {
1767 ntq_store_dest(c, &instr->dest, 1,
1768 vir_uniform(c,
1769 instr->num_components == 2 && is_array ?
1770 QUNIFORM_IMAGE_ARRAY_SIZE :
1771 QUNIFORM_IMAGE_HEIGHT,
1772 image_index));
1773 }
1774 if (instr->num_components > 2) {
1775 ntq_store_dest(c, &instr->dest, 2,
1776 vir_uniform(c,
1777 is_array ?
1778 QUNIFORM_IMAGE_ARRAY_SIZE :
1779 QUNIFORM_IMAGE_DEPTH,
1780 image_index));
1781 }
1782 }
1783
1784 static void
1785 vir_emit_tlb_color_read(struct v3d_compile *c, nir_intrinsic_instr *instr)
1786 {
1787 assert(c->s->info.stage == MESA_SHADER_FRAGMENT);
1788
1789 int rt = nir_src_as_uint(instr->src[0]);
1790 assert(rt < V3D_MAX_DRAW_BUFFERS);
1791
1792 int sample_index = nir_intrinsic_base(instr) ;
1793 assert(sample_index < V3D_MAX_SAMPLES);
1794
1795 int component = nir_intrinsic_component(instr);
1796 assert(component < 4);
1797
1798 /* We need to emit our TLB reads after we have acquired the scoreboard
1799 * lock, or the GPU will hang. Usually, we do our scoreboard locking on
1800 * the last thread switch to improve parallelism, however, that is only
1801 * guaranteed to happen before the tlb color writes.
1802 *
1803 * To fix that, we make sure we always emit a thread switch before the
1804 * first tlb color read. If that happens to be the last thread switch
1805 * we emit, then everything is fine, but otherwsie, if any code after
1806 * this point needs to emit additional thread switches, then we will
1807 * switch the strategy to locking the scoreboard on the first thread
1808 * switch instead -- see vir_emit_thrsw().
1809 */
1810 if (!c->emitted_tlb_load) {
1811 if (!c->last_thrsw_at_top_level) {
1812 assert(c->devinfo->ver >= 41);
1813 vir_emit_thrsw(c);
1814 }
1815
1816 c->emitted_tlb_load = true;
1817 }
1818
1819 struct qreg *color_reads_for_sample =
1820 &c->color_reads[(rt * V3D_MAX_SAMPLES + sample_index) * 4];
1821
1822 if (color_reads_for_sample[component].file == QFILE_NULL) {
1823 enum pipe_format rt_format = c->fs_key->color_fmt[rt].format;
1824 int num_components =
1825 util_format_get_nr_components(rt_format);
1826
1827 const bool swap_rb = c->fs_key->swap_color_rb & (1 << rt);
1828 if (swap_rb)
1829 num_components = MAX2(num_components, 3);
1830
1831 nir_variable *var = c->output_color_var[rt];
1832 enum glsl_base_type type = glsl_get_base_type(var->type);
1833
1834 bool is_int_format = type == GLSL_TYPE_INT ||
1835 type == GLSL_TYPE_UINT;
1836
1837 bool is_32b_tlb_format = is_int_format ||
1838 (c->fs_key->f32_color_rb & (1 << rt));
1839
1840 int num_samples = c->fs_key->msaa ? V3D_MAX_SAMPLES : 1;
1841
1842 uint32_t conf = 0xffffff00;
1843 conf |= c->fs_key->msaa ? TLB_SAMPLE_MODE_PER_SAMPLE :
1844 TLB_SAMPLE_MODE_PER_PIXEL;
1845 conf |= (7 - rt) << TLB_RENDER_TARGET_SHIFT;
1846
1847 if (is_32b_tlb_format) {
1848 /* The F32 vs I32 distinction was dropped in 4.2. */
1849 conf |= (c->devinfo->ver < 42 && is_int_format) ?
1850 TLB_TYPE_I32_COLOR : TLB_TYPE_F32_COLOR;
1851
1852 conf |= ((num_components - 1) <<
1853 TLB_VEC_SIZE_MINUS_1_SHIFT);
1854 } else {
1855 conf |= TLB_TYPE_F16_COLOR;
1856 conf |= TLB_F16_SWAP_HI_LO;
1857
1858 if (num_components >= 3)
1859 conf |= TLB_VEC_SIZE_4_F16;
1860 else
1861 conf |= TLB_VEC_SIZE_2_F16;
1862 }
1863
1864
1865 for (int i = 0; i < num_samples; i++) {
1866 struct qreg r, g, b, a;
1867 if (is_32b_tlb_format) {
1868 r = conf != 0xffffffff && i == 0?
1869 vir_TLBU_COLOR_READ(c, conf) :
1870 vir_TLB_COLOR_READ(c);
1871 if (num_components >= 2)
1872 g = vir_TLB_COLOR_READ(c);
1873 if (num_components >= 3)
1874 b = vir_TLB_COLOR_READ(c);
1875 if (num_components >= 4)
1876 a = vir_TLB_COLOR_READ(c);
1877 } else {
1878 struct qreg rg = conf != 0xffffffff && i == 0 ?
1879 vir_TLBU_COLOR_READ(c, conf) :
1880 vir_TLB_COLOR_READ(c);
1881 r = vir_FMOV(c, rg);
1882 vir_set_unpack(c->defs[r.index], 0,
1883 V3D_QPU_UNPACK_L);
1884 g = vir_FMOV(c, rg);
1885 vir_set_unpack(c->defs[g.index], 0,
1886 V3D_QPU_UNPACK_H);
1887
1888 if (num_components > 2) {
1889 struct qreg ba = vir_TLB_COLOR_READ(c);
1890 b = vir_FMOV(c, ba);
1891 vir_set_unpack(c->defs[b.index], 0,
1892 V3D_QPU_UNPACK_L);
1893 a = vir_FMOV(c, ba);
1894 vir_set_unpack(c->defs[a.index], 0,
1895 V3D_QPU_UNPACK_H);
1896 }
1897 }
1898
1899 struct qreg *color_reads =
1900 &c->color_reads[(rt * V3D_MAX_SAMPLES + i) * 4];
1901
1902 color_reads[0] = swap_rb ? b : r;
1903 if (num_components >= 2)
1904 color_reads[1] = g;
1905 if (num_components >= 3)
1906 color_reads[2] = swap_rb ? r : b;
1907 if (num_components >= 4)
1908 color_reads[3] = a;
1909 }
1910 }
1911
1912 assert(color_reads_for_sample[component].file != QFILE_NULL);
1913 ntq_store_dest(c, &instr->dest, 0,
1914 vir_MOV(c, color_reads_for_sample[component]));
1915 }
1916
1917 static void
1918 ntq_emit_load_uniform(struct v3d_compile *c, nir_intrinsic_instr *instr)
1919 {
1920 if (nir_src_is_const(instr->src[0])) {
1921 int offset = (nir_intrinsic_base(instr) +
1922 nir_src_as_uint(instr->src[0]));
1923 assert(offset % 4 == 0);
1924 /* We need dwords */
1925 offset = offset / 4;
1926 for (int i = 0; i < instr->num_components; i++) {
1927 ntq_store_dest(c, &instr->dest, i,
1928 vir_uniform(c, QUNIFORM_UNIFORM,
1929 offset + i));
1930 }
1931 } else {
1932 ntq_emit_tmu_general(c, instr, false);
1933 }
1934 }
1935
1936 static void
1937 ntq_emit_load_input(struct v3d_compile *c, nir_intrinsic_instr *instr)
1938 {
1939 /* XXX: Use ldvpmv (uniform offset) or ldvpmd (non-uniform offset)
1940 * and enable PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR.
1941 */
1942 unsigned offset =
1943 nir_intrinsic_base(instr) + nir_src_as_uint(instr->src[0]);
1944
1945 if (c->s->info.stage != MESA_SHADER_FRAGMENT && c->devinfo->ver >= 40) {
1946 /* Emit the LDVPM directly now, rather than at the top
1947 * of the shader like we did for V3D 3.x (which needs
1948 * vpmsetup when not just taking the next offset).
1949 *
1950 * Note that delaying like this may introduce stalls,
1951 * as LDVPMV takes a minimum of 1 instruction but may
1952 * be slower if the VPM unit is busy with another QPU.
1953 */
1954 int index = 0;
1955 if (c->s->info.system_values_read &
1956 (1ull << SYSTEM_VALUE_INSTANCE_ID)) {
1957 index++;
1958 }
1959 if (c->s->info.system_values_read &
1960 (1ull << SYSTEM_VALUE_VERTEX_ID)) {
1961 index++;
1962 }
1963 for (int i = 0; i < offset; i++)
1964 index += c->vattr_sizes[i];
1965 index += nir_intrinsic_component(instr);
1966 for (int i = 0; i < instr->num_components; i++) {
1967 struct qreg vpm_offset = vir_uniform_ui(c, index++);
1968 ntq_store_dest(c, &instr->dest, i,
1969 vir_LDVPMV_IN(c, vpm_offset));
1970 }
1971 } else {
1972 for (int i = 0; i < instr->num_components; i++) {
1973 int comp = nir_intrinsic_component(instr) + i;
1974 ntq_store_dest(c, &instr->dest, i,
1975 vir_MOV(c, c->inputs[offset * 4 + comp]));
1976 }
1977 }
1978 }
1979
1980 static void
1981 ntq_emit_per_sample_color_write(struct v3d_compile *c,
1982 nir_intrinsic_instr *instr)
1983 {
1984 assert(instr->intrinsic == nir_intrinsic_store_tlb_sample_color_v3d);
1985
1986 unsigned rt = nir_src_as_uint(instr->src[1]);
1987 assert(rt < V3D_MAX_DRAW_BUFFERS);
1988
1989 unsigned sample_idx = nir_intrinsic_base(instr);
1990 assert(sample_idx < V3D_MAX_SAMPLES);
1991
1992 unsigned offset = (rt * V3D_MAX_SAMPLES + sample_idx) * 4;
1993 for (int i = 0; i < instr->num_components; i++) {
1994 c->sample_colors[offset + i] =
1995 vir_MOV(c, ntq_get_src(c, instr->src[0], i));
1996 }
1997 }
1998
1999 static void
2000 ntq_emit_color_write(struct v3d_compile *c,
2001 nir_intrinsic_instr *instr)
2002 {
2003 unsigned offset = (nir_intrinsic_base(instr) +
2004 nir_src_as_uint(instr->src[1])) * 4 +
2005 nir_intrinsic_component(instr);
2006 for (int i = 0; i < instr->num_components; i++) {
2007 c->outputs[offset + i] =
2008 vir_MOV(c, ntq_get_src(c, instr->src[0], i));
2009 }
2010 }
2011
2012 static void
2013 emit_store_output_gs(struct v3d_compile *c, nir_intrinsic_instr *instr)
2014 {
2015 assert(instr->num_components == 1);
2016
2017 struct qreg offset = ntq_get_src(c, instr->src[1], 0);
2018
2019 uint32_t base_offset = nir_intrinsic_base(instr);
2020
2021 if (base_offset)
2022 offset = vir_ADD(c, vir_uniform_ui(c, base_offset), offset);
2023
2024 /* Usually, for VS or FS, we only emit outputs once at program end so
2025 * our VPM writes are never in non-uniform control flow, but this
2026 * is not true for GS, where we are emitting multiple vertices.
2027 */
2028 if (vir_in_nonuniform_control_flow(c)) {
2029 vir_set_pf(vir_MOV_dest(c, vir_nop_reg(), c->execute),
2030 V3D_QPU_PF_PUSHZ);
2031 }
2032
2033 struct qreg val = ntq_get_src(c, instr->src[0], 0);
2034
2035 /* The offset isn’t necessarily dynamically uniform for a geometry
2036 * shader. This can happen if the shader sometimes doesn’t emit one of
2037 * the vertices. In that case subsequent vertices will be written to
2038 * different offsets in the VPM and we need to use the scatter write
2039 * instruction to have a different offset for each lane.
2040 */
2041 if (nir_src_is_dynamically_uniform(instr->src[1]))
2042 vir_VPM_WRITE_indirect(c, val, offset);
2043 else
2044 vir_STVPMD(c, offset, val);
2045
2046 if (vir_in_nonuniform_control_flow(c)) {
2047 struct qinst *last_inst =
2048 (struct qinst *)c->cur_block->instructions.prev;
2049 vir_set_cond(last_inst, V3D_QPU_COND_IFA);
2050 }
2051 }
2052
2053 static void
2054 ntq_emit_store_output(struct v3d_compile *c, nir_intrinsic_instr *instr)
2055 {
2056 /* XXX perf: Use stvpmv with uniform non-constant offsets and
2057 * stvpmd with non-uniform offsets and enable
2058 * PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR.
2059 */
2060 if (c->s->info.stage == MESA_SHADER_FRAGMENT) {
2061 ntq_emit_color_write(c, instr);
2062 } else if (c->s->info.stage == MESA_SHADER_GEOMETRY) {
2063 emit_store_output_gs(c, instr);
2064 } else {
2065 assert(c->s->info.stage == MESA_SHADER_VERTEX);
2066 assert(instr->num_components == 1);
2067
2068 vir_VPM_WRITE(c,
2069 ntq_get_src(c, instr->src[0], 0),
2070 nir_intrinsic_base(instr));
2071 }
2072 }
2073
2074 static void
2075 ntq_emit_intrinsic(struct v3d_compile *c, nir_intrinsic_instr *instr)
2076 {
2077 switch (instr->intrinsic) {
2078 case nir_intrinsic_load_uniform:
2079 ntq_emit_load_uniform(c, instr);
2080 break;
2081
2082 case nir_intrinsic_load_ubo:
2083 ntq_emit_tmu_general(c, instr, false);
2084 break;
2085
2086 case nir_intrinsic_ssbo_atomic_add:
2087 case nir_intrinsic_ssbo_atomic_imin:
2088 case nir_intrinsic_ssbo_atomic_umin:
2089 case nir_intrinsic_ssbo_atomic_imax:
2090 case nir_intrinsic_ssbo_atomic_umax:
2091 case nir_intrinsic_ssbo_atomic_and:
2092 case nir_intrinsic_ssbo_atomic_or:
2093 case nir_intrinsic_ssbo_atomic_xor:
2094 case nir_intrinsic_ssbo_atomic_exchange:
2095 case nir_intrinsic_ssbo_atomic_comp_swap:
2096 case nir_intrinsic_load_ssbo:
2097 case nir_intrinsic_store_ssbo:
2098 ntq_emit_tmu_general(c, instr, false);
2099 break;
2100
2101 case nir_intrinsic_shared_atomic_add:
2102 case nir_intrinsic_shared_atomic_imin:
2103 case nir_intrinsic_shared_atomic_umin:
2104 case nir_intrinsic_shared_atomic_imax:
2105 case nir_intrinsic_shared_atomic_umax:
2106 case nir_intrinsic_shared_atomic_and:
2107 case nir_intrinsic_shared_atomic_or:
2108 case nir_intrinsic_shared_atomic_xor:
2109 case nir_intrinsic_shared_atomic_exchange:
2110 case nir_intrinsic_shared_atomic_comp_swap:
2111 case nir_intrinsic_load_shared:
2112 case nir_intrinsic_store_shared:
2113 case nir_intrinsic_load_scratch:
2114 case nir_intrinsic_store_scratch:
2115 ntq_emit_tmu_general(c, instr, true);
2116 break;
2117
2118 case nir_intrinsic_image_load:
2119 case nir_intrinsic_image_store:
2120 case nir_intrinsic_image_atomic_add:
2121 case nir_intrinsic_image_atomic_imin:
2122 case nir_intrinsic_image_atomic_umin:
2123 case nir_intrinsic_image_atomic_imax:
2124 case nir_intrinsic_image_atomic_umax:
2125 case nir_intrinsic_image_atomic_and:
2126 case nir_intrinsic_image_atomic_or:
2127 case nir_intrinsic_image_atomic_xor:
2128 case nir_intrinsic_image_atomic_exchange:
2129 case nir_intrinsic_image_atomic_comp_swap:
2130 v3d40_vir_emit_image_load_store(c, instr);
2131 break;
2132
2133 case nir_intrinsic_get_buffer_size:
2134 ntq_store_dest(c, &instr->dest, 0,
2135 vir_uniform(c, QUNIFORM_GET_BUFFER_SIZE,
2136 nir_src_as_uint(instr->src[0])));
2137 break;
2138
2139 case nir_intrinsic_load_user_clip_plane:
2140 for (int i = 0; i < nir_intrinsic_dest_components(instr); i++) {
2141 ntq_store_dest(c, &instr->dest, i,
2142 vir_uniform(c, QUNIFORM_USER_CLIP_PLANE,
2143 nir_intrinsic_ucp_id(instr) *
2144 4 + i));
2145 }
2146 break;
2147
2148 case nir_intrinsic_load_viewport_x_scale:
2149 ntq_store_dest(c, &instr->dest, 0,
2150 vir_uniform(c, QUNIFORM_VIEWPORT_X_SCALE, 0));
2151 break;
2152
2153 case nir_intrinsic_load_viewport_y_scale:
2154 ntq_store_dest(c, &instr->dest, 0,
2155 vir_uniform(c, QUNIFORM_VIEWPORT_Y_SCALE, 0));
2156 break;
2157
2158 case nir_intrinsic_load_viewport_z_scale:
2159 ntq_store_dest(c, &instr->dest, 0,
2160 vir_uniform(c, QUNIFORM_VIEWPORT_Z_SCALE, 0));
2161 break;
2162
2163 case nir_intrinsic_load_viewport_z_offset:
2164 ntq_store_dest(c, &instr->dest, 0,
2165 vir_uniform(c, QUNIFORM_VIEWPORT_Z_OFFSET, 0));
2166 break;
2167
2168 case nir_intrinsic_load_alpha_ref_float:
2169 ntq_store_dest(c, &instr->dest, 0,
2170 vir_uniform(c, QUNIFORM_ALPHA_REF, 0));
2171 break;
2172
2173 case nir_intrinsic_load_line_coord:
2174 ntq_store_dest(c, &instr->dest, 0, vir_MOV(c, c->line_x));
2175 break;
2176
2177 case nir_intrinsic_load_line_width:
2178 ntq_store_dest(c, &instr->dest, 0,
2179 vir_uniform(c, QUNIFORM_LINE_WIDTH, 0));
2180 break;
2181
2182 case nir_intrinsic_load_aa_line_width:
2183 ntq_store_dest(c, &instr->dest, 0,
2184 vir_uniform(c, QUNIFORM_AA_LINE_WIDTH, 0));
2185 break;
2186
2187 case nir_intrinsic_load_sample_mask_in:
2188 ntq_store_dest(c, &instr->dest, 0, vir_MSF(c));
2189 break;
2190
2191 case nir_intrinsic_load_helper_invocation:
2192 vir_set_pf(vir_MSF_dest(c, vir_nop_reg()), V3D_QPU_PF_PUSHZ);
2193 ntq_store_dest(c, &instr->dest, 0,
2194 vir_MOV(c, vir_SEL(c, V3D_QPU_COND_IFA,
2195 vir_uniform_ui(c, ~0),
2196 vir_uniform_ui(c, 0))));
2197 break;
2198
2199 case nir_intrinsic_load_front_face:
2200 /* The register contains 0 (front) or 1 (back), and we need to
2201 * turn it into a NIR bool where true means front.
2202 */
2203 ntq_store_dest(c, &instr->dest, 0,
2204 vir_ADD(c,
2205 vir_uniform_ui(c, -1),
2206 vir_REVF(c)));
2207 break;
2208
2209 case nir_intrinsic_load_instance_id:
2210 ntq_store_dest(c, &instr->dest, 0, vir_MOV(c, c->iid));
2211 break;
2212
2213 case nir_intrinsic_load_vertex_id:
2214 ntq_store_dest(c, &instr->dest, 0, vir_MOV(c, c->vid));
2215 break;
2216
2217 case nir_intrinsic_load_tlb_color_v3d:
2218 vir_emit_tlb_color_read(c, instr);
2219 break;
2220
2221 case nir_intrinsic_load_input:
2222 ntq_emit_load_input(c, instr);
2223 break;
2224
2225 case nir_intrinsic_store_tlb_sample_color_v3d:
2226 ntq_emit_per_sample_color_write(c, instr);
2227 break;
2228
2229 case nir_intrinsic_store_output:
2230 ntq_emit_store_output(c, instr);
2231 break;
2232
2233 case nir_intrinsic_image_size:
2234 ntq_emit_image_size(c, instr);
2235 break;
2236
2237 case nir_intrinsic_discard:
2238 if (vir_in_nonuniform_control_flow(c)) {
2239 vir_set_pf(vir_MOV_dest(c, vir_nop_reg(), c->execute),
2240 V3D_QPU_PF_PUSHZ);
2241 vir_set_cond(vir_SETMSF_dest(c, vir_nop_reg(),
2242 vir_uniform_ui(c, 0)),
2243 V3D_QPU_COND_IFA);
2244 } else {
2245 vir_SETMSF_dest(c, vir_nop_reg(),
2246 vir_uniform_ui(c, 0));
2247 }
2248 break;
2249
2250 case nir_intrinsic_discard_if: {
2251 enum v3d_qpu_cond cond = ntq_emit_bool_to_cond(c, instr->src[0]);
2252
2253 if (vir_in_nonuniform_control_flow(c)) {
2254 struct qinst *exec_flag = vir_MOV_dest(c, vir_nop_reg(),
2255 c->execute);
2256 if (cond == V3D_QPU_COND_IFA) {
2257 vir_set_uf(exec_flag, V3D_QPU_UF_ANDZ);
2258 } else {
2259 vir_set_uf(exec_flag, V3D_QPU_UF_NORNZ);
2260 cond = V3D_QPU_COND_IFA;
2261 }
2262 }
2263
2264 vir_set_cond(vir_SETMSF_dest(c, vir_nop_reg(),
2265 vir_uniform_ui(c, 0)), cond);
2266
2267 break;
2268 }
2269
2270 case nir_intrinsic_memory_barrier:
2271 case nir_intrinsic_memory_barrier_buffer:
2272 case nir_intrinsic_memory_barrier_image:
2273 case nir_intrinsic_memory_barrier_shared:
2274 case nir_intrinsic_memory_barrier_tcs_patch:
2275 case nir_intrinsic_group_memory_barrier:
2276 /* We don't do any instruction scheduling of these NIR
2277 * instructions between each other, so we just need to make
2278 * sure that the TMU operations before the barrier are flushed
2279 * before the ones after the barrier. That is currently
2280 * handled by having a THRSW in each of them and a LDTMU
2281 * series or a TMUWT after.
2282 */
2283 break;
2284
2285 case nir_intrinsic_control_barrier:
2286 /* Emit a TSY op to get all invocations in the workgroup
2287 * (actually supergroup) to block until the last invocation
2288 * reaches the TSY op.
2289 */
2290 if (c->devinfo->ver >= 42) {
2291 vir_BARRIERID_dest(c, vir_reg(QFILE_MAGIC,
2292 V3D_QPU_WADDR_SYNCB));
2293 } else {
2294 struct qinst *sync =
2295 vir_BARRIERID_dest(c,
2296 vir_reg(QFILE_MAGIC,
2297 V3D_QPU_WADDR_SYNCU));
2298 sync->uniform =
2299 vir_get_uniform_index(c, QUNIFORM_CONSTANT,
2300 0xffffff00 |
2301 V3D_TSY_WAIT_INC_CHECK);
2302
2303 }
2304
2305 /* The blocking of a TSY op only happens at the next thread
2306 * switch. No texturing may be outstanding at the time of a
2307 * TSY blocking operation.
2308 */
2309 vir_emit_thrsw(c);
2310 break;
2311
2312 case nir_intrinsic_load_num_work_groups:
2313 for (int i = 0; i < 3; i++) {
2314 ntq_store_dest(c, &instr->dest, i,
2315 vir_uniform(c, QUNIFORM_NUM_WORK_GROUPS,
2316 i));
2317 }
2318 break;
2319
2320 case nir_intrinsic_load_local_invocation_index:
2321 ntq_store_dest(c, &instr->dest, 0,
2322 vir_SHR(c, c->cs_payload[1],
2323 vir_uniform_ui(c, 32 - c->local_invocation_index_bits)));
2324 break;
2325
2326 case nir_intrinsic_load_work_group_id:
2327 ntq_store_dest(c, &instr->dest, 0,
2328 vir_AND(c, c->cs_payload[0],
2329 vir_uniform_ui(c, 0xffff)));
2330 ntq_store_dest(c, &instr->dest, 1,
2331 vir_SHR(c, c->cs_payload[0],
2332 vir_uniform_ui(c, 16)));
2333 ntq_store_dest(c, &instr->dest, 2,
2334 vir_AND(c, c->cs_payload[1],
2335 vir_uniform_ui(c, 0xffff)));
2336 break;
2337
2338 case nir_intrinsic_load_subgroup_id:
2339 ntq_store_dest(c, &instr->dest, 0, vir_EIDX(c));
2340 break;
2341
2342 case nir_intrinsic_load_per_vertex_input: {
2343 /* col: vertex index, row = varying index */
2344 struct qreg col = ntq_get_src(c, instr->src[0], 0);
2345 uint32_t row_idx = nir_intrinsic_base(instr) * 4 +
2346 nir_intrinsic_component(instr);
2347 for (int i = 0; i < instr->num_components; i++) {
2348 struct qreg row = vir_uniform_ui(c, row_idx++);
2349 ntq_store_dest(c, &instr->dest, i,
2350 vir_LDVPMG_IN(c, row, col));
2351 }
2352 break;
2353 }
2354
2355 case nir_intrinsic_emit_vertex:
2356 case nir_intrinsic_end_primitive:
2357 unreachable("Should have been lowered in v3d_nir_lower_io");
2358 break;
2359
2360 case nir_intrinsic_load_primitive_id: {
2361 /* gl_PrimitiveIdIn is written by the GBG in the first word of
2362 * VPM output header. According to docs, we should read this
2363 * using ldvpm(v,d)_in (See Table 71).
2364 */
2365 ntq_store_dest(c, &instr->dest, 0,
2366 vir_LDVPMV_IN(c, vir_uniform_ui(c, 0)));
2367 break;
2368 }
2369
2370 case nir_intrinsic_load_invocation_id:
2371 ntq_store_dest(c, &instr->dest, 0, vir_IID(c));
2372 break;
2373
2374 case nir_intrinsic_load_fb_layers_v3d:
2375 ntq_store_dest(c, &instr->dest, 0,
2376 vir_uniform(c, QUNIFORM_FB_LAYERS, 0));
2377 break;
2378
2379 default:
2380 fprintf(stderr, "Unknown intrinsic: ");
2381 nir_print_instr(&instr->instr, stderr);
2382 fprintf(stderr, "\n");
2383 break;
2384 }
2385 }
2386
2387 /* Clears (activates) the execute flags for any channels whose jump target
2388 * matches this block.
2389 *
2390 * XXX perf: Could we be using flpush/flpop somehow for our execution channel
2391 * enabling?
2392 *
2393 * XXX perf: For uniform control flow, we should be able to skip c->execute
2394 * handling entirely.
2395 */
2396 static void
2397 ntq_activate_execute_for_block(struct v3d_compile *c)
2398 {
2399 vir_set_pf(vir_XOR_dest(c, vir_nop_reg(),
2400 c->execute, vir_uniform_ui(c, c->cur_block->index)),
2401 V3D_QPU_PF_PUSHZ);
2402
2403 vir_MOV_cond(c, V3D_QPU_COND_IFA, c->execute, vir_uniform_ui(c, 0));
2404 }
2405
2406 static void
2407 ntq_emit_uniform_if(struct v3d_compile *c, nir_if *if_stmt)
2408 {
2409 nir_block *nir_else_block = nir_if_first_else_block(if_stmt);
2410 bool empty_else_block =
2411 (nir_else_block == nir_if_last_else_block(if_stmt) &&
2412 exec_list_is_empty(&nir_else_block->instr_list));
2413
2414 struct qblock *then_block = vir_new_block(c);
2415 struct qblock *after_block = vir_new_block(c);
2416 struct qblock *else_block;
2417 if (empty_else_block)
2418 else_block = after_block;
2419 else
2420 else_block = vir_new_block(c);
2421
2422 /* Set up the flags for the IF condition (taking the THEN branch). */
2423 enum v3d_qpu_cond cond = ntq_emit_bool_to_cond(c, if_stmt->condition);
2424
2425 /* Jump to ELSE. */
2426 vir_BRANCH(c, cond == V3D_QPU_COND_IFA ?
2427 V3D_QPU_BRANCH_COND_ALLNA :
2428 V3D_QPU_BRANCH_COND_ALLA);
2429 vir_link_blocks(c->cur_block, else_block);
2430 vir_link_blocks(c->cur_block, then_block);
2431
2432 /* Process the THEN block. */
2433 vir_set_emit_block(c, then_block);
2434 ntq_emit_cf_list(c, &if_stmt->then_list);
2435
2436 if (!empty_else_block) {
2437 /* At the end of the THEN block, jump to ENDIF */
2438 vir_BRANCH(c, V3D_QPU_BRANCH_COND_ALWAYS);
2439 vir_link_blocks(c->cur_block, after_block);
2440
2441 /* Emit the else block. */
2442 vir_set_emit_block(c, else_block);
2443 ntq_emit_cf_list(c, &if_stmt->else_list);
2444 }
2445
2446 vir_link_blocks(c->cur_block, after_block);
2447
2448 vir_set_emit_block(c, after_block);
2449 }
2450
2451 static void
2452 ntq_emit_nonuniform_if(struct v3d_compile *c, nir_if *if_stmt)
2453 {
2454 nir_block *nir_else_block = nir_if_first_else_block(if_stmt);
2455 bool empty_else_block =
2456 (nir_else_block == nir_if_last_else_block(if_stmt) &&
2457 exec_list_is_empty(&nir_else_block->instr_list));
2458
2459 struct qblock *then_block = vir_new_block(c);
2460 struct qblock *after_block = vir_new_block(c);
2461 struct qblock *else_block;
2462 if (empty_else_block)
2463 else_block = after_block;
2464 else
2465 else_block = vir_new_block(c);
2466
2467 bool was_uniform_control_flow = false;
2468 if (!vir_in_nonuniform_control_flow(c)) {
2469 c->execute = vir_MOV(c, vir_uniform_ui(c, 0));
2470 was_uniform_control_flow = true;
2471 }
2472
2473 /* Set up the flags for the IF condition (taking the THEN branch). */
2474 enum v3d_qpu_cond cond = ntq_emit_bool_to_cond(c, if_stmt->condition);
2475
2476 /* Update the flags+cond to mean "Taking the ELSE branch (!cond) and
2477 * was previously active (execute Z) for updating the exec flags.
2478 */
2479 if (was_uniform_control_flow) {
2480 cond = v3d_qpu_cond_invert(cond);
2481 } else {
2482 struct qinst *inst = vir_MOV_dest(c, vir_nop_reg(), c->execute);
2483 if (cond == V3D_QPU_COND_IFA) {
2484 vir_set_uf(inst, V3D_QPU_UF_NORNZ);
2485 } else {
2486 vir_set_uf(inst, V3D_QPU_UF_ANDZ);
2487 cond = V3D_QPU_COND_IFA;
2488 }
2489 }
2490
2491 vir_MOV_cond(c, cond,
2492 c->execute,
2493 vir_uniform_ui(c, else_block->index));
2494
2495 /* Jump to ELSE if nothing is active for THEN, otherwise fall
2496 * through.
2497 */
2498 vir_set_pf(vir_MOV_dest(c, vir_nop_reg(), c->execute), V3D_QPU_PF_PUSHZ);
2499 vir_BRANCH(c, V3D_QPU_BRANCH_COND_ALLNA);
2500 vir_link_blocks(c->cur_block, else_block);
2501 vir_link_blocks(c->cur_block, then_block);
2502
2503 /* Process the THEN block. */
2504 vir_set_emit_block(c, then_block);
2505 ntq_emit_cf_list(c, &if_stmt->then_list);
2506
2507 if (!empty_else_block) {
2508 /* Handle the end of the THEN block. First, all currently
2509 * active channels update their execute flags to point to
2510 * ENDIF
2511 */
2512 vir_set_pf(vir_MOV_dest(c, vir_nop_reg(), c->execute),
2513 V3D_QPU_PF_PUSHZ);
2514 vir_MOV_cond(c, V3D_QPU_COND_IFA, c->execute,
2515 vir_uniform_ui(c, after_block->index));
2516
2517 /* If everything points at ENDIF, then jump there immediately. */
2518 vir_set_pf(vir_XOR_dest(c, vir_nop_reg(),
2519 c->execute,
2520 vir_uniform_ui(c, after_block->index)),
2521 V3D_QPU_PF_PUSHZ);
2522 vir_BRANCH(c, V3D_QPU_BRANCH_COND_ALLA);
2523 vir_link_blocks(c->cur_block, after_block);
2524 vir_link_blocks(c->cur_block, else_block);
2525
2526 vir_set_emit_block(c, else_block);
2527 ntq_activate_execute_for_block(c);
2528 ntq_emit_cf_list(c, &if_stmt->else_list);
2529 }
2530
2531 vir_link_blocks(c->cur_block, after_block);
2532
2533 vir_set_emit_block(c, after_block);
2534 if (was_uniform_control_flow)
2535 c->execute = c->undef;
2536 else
2537 ntq_activate_execute_for_block(c);
2538 }
2539
2540 static void
2541 ntq_emit_if(struct v3d_compile *c, nir_if *nif)
2542 {
2543 bool was_in_control_flow = c->in_control_flow;
2544 c->in_control_flow = true;
2545 if (!vir_in_nonuniform_control_flow(c) &&
2546 nir_src_is_dynamically_uniform(nif->condition)) {
2547 ntq_emit_uniform_if(c, nif);
2548 } else {
2549 ntq_emit_nonuniform_if(c, nif);
2550 }
2551 c->in_control_flow = was_in_control_flow;
2552 }
2553
2554 static void
2555 ntq_emit_jump(struct v3d_compile *c, nir_jump_instr *jump)
2556 {
2557 switch (jump->type) {
2558 case nir_jump_break:
2559 vir_set_pf(vir_MOV_dest(c, vir_nop_reg(), c->execute),
2560 V3D_QPU_PF_PUSHZ);
2561 vir_MOV_cond(c, V3D_QPU_COND_IFA, c->execute,
2562 vir_uniform_ui(c, c->loop_break_block->index));
2563 break;
2564
2565 case nir_jump_continue:
2566 vir_set_pf(vir_MOV_dest(c, vir_nop_reg(), c->execute),
2567 V3D_QPU_PF_PUSHZ);
2568 vir_MOV_cond(c, V3D_QPU_COND_IFA, c->execute,
2569 vir_uniform_ui(c, c->loop_cont_block->index));
2570 break;
2571
2572 case nir_jump_return:
2573 unreachable("All returns shouold be lowered\n");
2574 }
2575 }
2576
2577 static void
2578 ntq_emit_instr(struct v3d_compile *c, nir_instr *instr)
2579 {
2580 switch (instr->type) {
2581 case nir_instr_type_alu:
2582 ntq_emit_alu(c, nir_instr_as_alu(instr));
2583 break;
2584
2585 case nir_instr_type_intrinsic:
2586 ntq_emit_intrinsic(c, nir_instr_as_intrinsic(instr));
2587 break;
2588
2589 case nir_instr_type_load_const:
2590 ntq_emit_load_const(c, nir_instr_as_load_const(instr));
2591 break;
2592
2593 case nir_instr_type_ssa_undef:
2594 ntq_emit_ssa_undef(c, nir_instr_as_ssa_undef(instr));
2595 break;
2596
2597 case nir_instr_type_tex:
2598 ntq_emit_tex(c, nir_instr_as_tex(instr));
2599 break;
2600
2601 case nir_instr_type_jump:
2602 ntq_emit_jump(c, nir_instr_as_jump(instr));
2603 break;
2604
2605 default:
2606 fprintf(stderr, "Unknown NIR instr type: ");
2607 nir_print_instr(instr, stderr);
2608 fprintf(stderr, "\n");
2609 abort();
2610 }
2611 }
2612
2613 static void
2614 ntq_emit_block(struct v3d_compile *c, nir_block *block)
2615 {
2616 nir_foreach_instr(instr, block) {
2617 ntq_emit_instr(c, instr);
2618 }
2619 }
2620
2621 static void ntq_emit_cf_list(struct v3d_compile *c, struct exec_list *list);
2622
2623 static void
2624 ntq_emit_loop(struct v3d_compile *c, nir_loop *loop)
2625 {
2626 bool was_in_control_flow = c->in_control_flow;
2627 c->in_control_flow = true;
2628
2629 bool was_uniform_control_flow = false;
2630 if (!vir_in_nonuniform_control_flow(c)) {
2631 c->execute = vir_MOV(c, vir_uniform_ui(c, 0));
2632 was_uniform_control_flow = true;
2633 }
2634
2635 struct qblock *save_loop_cont_block = c->loop_cont_block;
2636 struct qblock *save_loop_break_block = c->loop_break_block;
2637
2638 c->loop_cont_block = vir_new_block(c);
2639 c->loop_break_block = vir_new_block(c);
2640
2641 vir_link_blocks(c->cur_block, c->loop_cont_block);
2642 vir_set_emit_block(c, c->loop_cont_block);
2643 ntq_activate_execute_for_block(c);
2644
2645 ntq_emit_cf_list(c, &loop->body);
2646
2647 /* Re-enable any previous continues now, so our ANYA check below
2648 * works.
2649 *
2650 * XXX: Use the .ORZ flags update, instead.
2651 */
2652 vir_set_pf(vir_XOR_dest(c,
2653 vir_nop_reg(),
2654 c->execute,
2655 vir_uniform_ui(c, c->loop_cont_block->index)),
2656 V3D_QPU_PF_PUSHZ);
2657 vir_MOV_cond(c, V3D_QPU_COND_IFA, c->execute, vir_uniform_ui(c, 0));
2658
2659 vir_set_pf(vir_MOV_dest(c, vir_nop_reg(), c->execute), V3D_QPU_PF_PUSHZ);
2660
2661 struct qinst *branch = vir_BRANCH(c, V3D_QPU_BRANCH_COND_ANYA);
2662 /* Pixels that were not dispatched or have been discarded should not
2663 * contribute to looping again.
2664 */
2665 branch->qpu.branch.msfign = V3D_QPU_MSFIGN_P;
2666 vir_link_blocks(c->cur_block, c->loop_cont_block);
2667 vir_link_blocks(c->cur_block, c->loop_break_block);
2668
2669 vir_set_emit_block(c, c->loop_break_block);
2670 if (was_uniform_control_flow)
2671 c->execute = c->undef;
2672 else
2673 ntq_activate_execute_for_block(c);
2674
2675 c->loop_break_block = save_loop_break_block;
2676 c->loop_cont_block = save_loop_cont_block;
2677
2678 c->loops++;
2679
2680 c->in_control_flow = was_in_control_flow;
2681 }
2682
2683 static void
2684 ntq_emit_function(struct v3d_compile *c, nir_function_impl *func)
2685 {
2686 fprintf(stderr, "FUNCTIONS not handled.\n");
2687 abort();
2688 }
2689
2690 static void
2691 ntq_emit_cf_list(struct v3d_compile *c, struct exec_list *list)
2692 {
2693 foreach_list_typed(nir_cf_node, node, node, list) {
2694 switch (node->type) {
2695 case nir_cf_node_block:
2696 ntq_emit_block(c, nir_cf_node_as_block(node));
2697 break;
2698
2699 case nir_cf_node_if:
2700 ntq_emit_if(c, nir_cf_node_as_if(node));
2701 break;
2702
2703 case nir_cf_node_loop:
2704 ntq_emit_loop(c, nir_cf_node_as_loop(node));
2705 break;
2706
2707 case nir_cf_node_function:
2708 ntq_emit_function(c, nir_cf_node_as_function(node));
2709 break;
2710
2711 default:
2712 fprintf(stderr, "Unknown NIR node type\n");
2713 abort();
2714 }
2715 }
2716 }
2717
2718 static void
2719 ntq_emit_impl(struct v3d_compile *c, nir_function_impl *impl)
2720 {
2721 ntq_setup_registers(c, &impl->registers);
2722 ntq_emit_cf_list(c, &impl->body);
2723 }
2724
2725 static void
2726 nir_to_vir(struct v3d_compile *c)
2727 {
2728 switch (c->s->info.stage) {
2729 case MESA_SHADER_FRAGMENT:
2730 c->payload_w = vir_MOV(c, vir_reg(QFILE_REG, 0));
2731 c->payload_w_centroid = vir_MOV(c, vir_reg(QFILE_REG, 1));
2732 c->payload_z = vir_MOV(c, vir_reg(QFILE_REG, 2));
2733
2734 /* V3D 4.x can disable implicit point coordinate varyings if
2735 * they are not used.
2736 */
2737 if (c->fs_key->is_points &&
2738 (c->devinfo->ver < 40 || program_reads_point_coord(c))) {
2739 c->point_x = emit_fragment_varying(c, NULL, 0, 0);
2740 c->point_y = emit_fragment_varying(c, NULL, 0, 0);
2741 c->uses_implicit_point_line_varyings = true;
2742 } else if (c->fs_key->is_lines &&
2743 (c->devinfo->ver < 40 ||
2744 (c->s->info.system_values_read &
2745 BITFIELD64_BIT(SYSTEM_VALUE_LINE_COORD)))) {
2746 c->line_x = emit_fragment_varying(c, NULL, 0, 0);
2747 c->uses_implicit_point_line_varyings = true;
2748 }
2749 break;
2750 case MESA_SHADER_COMPUTE:
2751 /* Set up the TSO for barriers, assuming we do some. */
2752 if (c->devinfo->ver < 42) {
2753 vir_BARRIERID_dest(c, vir_reg(QFILE_MAGIC,
2754 V3D_QPU_WADDR_SYNC));
2755 }
2756
2757 c->cs_payload[0] = vir_MOV(c, vir_reg(QFILE_REG, 0));
2758 c->cs_payload[1] = vir_MOV(c, vir_reg(QFILE_REG, 2));
2759
2760 /* Set up the division between gl_LocalInvocationIndex and
2761 * wg_in_mem in the payload reg.
2762 */
2763 int wg_size = (c->s->info.cs.local_size[0] *
2764 c->s->info.cs.local_size[1] *
2765 c->s->info.cs.local_size[2]);
2766 c->local_invocation_index_bits =
2767 ffs(util_next_power_of_two(MAX2(wg_size, 64))) - 1;
2768 assert(c->local_invocation_index_bits <= 8);
2769
2770 if (c->s->info.cs.shared_size) {
2771 struct qreg wg_in_mem = vir_SHR(c, c->cs_payload[1],
2772 vir_uniform_ui(c, 16));
2773 if (c->s->info.cs.local_size[0] != 1 ||
2774 c->s->info.cs.local_size[1] != 1 ||
2775 c->s->info.cs.local_size[2] != 1) {
2776 int wg_bits = (16 -
2777 c->local_invocation_index_bits);
2778 int wg_mask = (1 << wg_bits) - 1;
2779 wg_in_mem = vir_AND(c, wg_in_mem,
2780 vir_uniform_ui(c, wg_mask));
2781 }
2782 struct qreg shared_per_wg =
2783 vir_uniform_ui(c, c->s->info.cs.shared_size);
2784
2785 c->cs_shared_offset =
2786 vir_ADD(c,
2787 vir_uniform(c, QUNIFORM_SHARED_OFFSET,0),
2788 vir_UMUL(c, wg_in_mem, shared_per_wg));
2789 }
2790 break;
2791 default:
2792 break;
2793 }
2794
2795 if (c->s->scratch_size) {
2796 v3d_setup_spill_base(c);
2797 c->spill_size += V3D_CHANNELS * c->s->scratch_size;
2798 }
2799
2800 switch (c->s->info.stage) {
2801 case MESA_SHADER_VERTEX:
2802 ntq_setup_vs_inputs(c);
2803 break;
2804 case MESA_SHADER_GEOMETRY:
2805 ntq_setup_gs_inputs(c);
2806 break;
2807 case MESA_SHADER_FRAGMENT:
2808 ntq_setup_fs_inputs(c);
2809 break;
2810 case MESA_SHADER_COMPUTE:
2811 break;
2812 default:
2813 unreachable("unsupported shader stage");
2814 }
2815
2816 ntq_setup_outputs(c);
2817
2818 /* Find the main function and emit the body. */
2819 nir_foreach_function(function, c->s) {
2820 assert(strcmp(function->name, "main") == 0);
2821 assert(function->impl);
2822 ntq_emit_impl(c, function->impl);
2823 }
2824 }
2825
2826 const nir_shader_compiler_options v3d_nir_options = {
2827 .lower_all_io_to_temps = true,
2828 .lower_extract_byte = true,
2829 .lower_extract_word = true,
2830 .lower_bitfield_insert_to_shifts = true,
2831 .lower_bitfield_extract_to_shifts = true,
2832 .lower_bitfield_reverse = true,
2833 .lower_bit_count = true,
2834 .lower_cs_local_id_from_index = true,
2835 .lower_ffract = true,
2836 .lower_fmod = true,
2837 .lower_pack_unorm_2x16 = true,
2838 .lower_pack_snorm_2x16 = true,
2839 .lower_pack_unorm_4x8 = true,
2840 .lower_pack_snorm_4x8 = true,
2841 .lower_unpack_unorm_4x8 = true,
2842 .lower_unpack_snorm_4x8 = true,
2843 .lower_pack_half_2x16 = true,
2844 .lower_unpack_half_2x16 = true,
2845 .lower_fdiv = true,
2846 .lower_find_lsb = true,
2847 .lower_ffma = true,
2848 .lower_flrp32 = true,
2849 .lower_fpow = true,
2850 .lower_fsat = true,
2851 .lower_fsqrt = true,
2852 .lower_ifind_msb = true,
2853 .lower_isign = true,
2854 .lower_ldexp = true,
2855 .lower_mul_high = true,
2856 .lower_wpos_pntc = true,
2857 .lower_rotate = true,
2858 .lower_to_scalar = true,
2859 };
2860
2861 /**
2862 * When demoting a shader down to single-threaded, removes the THRSW
2863 * instructions (one will still be inserted at v3d_vir_to_qpu() for the
2864 * program end).
2865 */
2866 static void
2867 vir_remove_thrsw(struct v3d_compile *c)
2868 {
2869 vir_for_each_block(block, c) {
2870 vir_for_each_inst_safe(inst, block) {
2871 if (inst->qpu.sig.thrsw)
2872 vir_remove_instruction(c, inst);
2873 }
2874 }
2875
2876 c->last_thrsw = NULL;
2877 }
2878
2879 void
2880 vir_emit_last_thrsw(struct v3d_compile *c)
2881 {
2882 /* On V3D before 4.1, we need a TMU op to be outstanding when thread
2883 * switching, so disable threads if we didn't do any TMU ops (each of
2884 * which would have emitted a THRSW).
2885 */
2886 if (!c->last_thrsw_at_top_level && c->devinfo->ver < 41) {
2887 c->threads = 1;
2888 if (c->last_thrsw)
2889 vir_remove_thrsw(c);
2890 return;
2891 }
2892
2893 /* If we're threaded and the last THRSW was in conditional code, then
2894 * we need to emit another one so that we can flag it as the last
2895 * thrsw.
2896 */
2897 if (c->last_thrsw && !c->last_thrsw_at_top_level) {
2898 assert(c->devinfo->ver >= 41);
2899 vir_emit_thrsw(c);
2900 }
2901
2902 /* If we're threaded, then we need to mark the last THRSW instruction
2903 * so we can emit a pair of them at QPU emit time.
2904 *
2905 * For V3D 4.x, we can spawn the non-fragment shaders already in the
2906 * post-last-THRSW state, so we can skip this.
2907 */
2908 if (!c->last_thrsw && c->s->info.stage == MESA_SHADER_FRAGMENT) {
2909 assert(c->devinfo->ver >= 41);
2910 vir_emit_thrsw(c);
2911 }
2912
2913 if (c->last_thrsw)
2914 c->last_thrsw->is_last_thrsw = true;
2915 }
2916
2917 /* There's a flag in the shader for "center W is needed for reasons other than
2918 * non-centroid varyings", so we just walk the program after VIR optimization
2919 * to see if it's used. It should be harmless to set even if we only use
2920 * center W for varyings.
2921 */
2922 static void
2923 vir_check_payload_w(struct v3d_compile *c)
2924 {
2925 if (c->s->info.stage != MESA_SHADER_FRAGMENT)
2926 return;
2927
2928 vir_for_each_inst_inorder(inst, c) {
2929 for (int i = 0; i < vir_get_nsrc(inst); i++) {
2930 if (inst->src[i].file == QFILE_REG &&
2931 inst->src[i].index == 0) {
2932 c->uses_center_w = true;
2933 return;
2934 }
2935 }
2936 }
2937
2938 }
2939
2940 void
2941 v3d_nir_to_vir(struct v3d_compile *c)
2942 {
2943 if (V3D_DEBUG & (V3D_DEBUG_NIR |
2944 v3d_debug_flag_for_shader_stage(c->s->info.stage))) {
2945 fprintf(stderr, "%s prog %d/%d NIR:\n",
2946 vir_get_stage_name(c),
2947 c->program_id, c->variant_id);
2948 nir_print_shader(c->s, stderr);
2949 }
2950
2951 nir_to_vir(c);
2952
2953 /* Emit the last THRSW before STVPM and TLB writes. */
2954 vir_emit_last_thrsw(c);
2955
2956 switch (c->s->info.stage) {
2957 case MESA_SHADER_FRAGMENT:
2958 emit_frag_end(c);
2959 break;
2960 case MESA_SHADER_GEOMETRY:
2961 emit_geom_end(c);
2962 break;
2963 case MESA_SHADER_VERTEX:
2964 emit_vert_end(c);
2965 break;
2966 case MESA_SHADER_COMPUTE:
2967 break;
2968 default:
2969 unreachable("bad stage");
2970 }
2971
2972 if (V3D_DEBUG & (V3D_DEBUG_VIR |
2973 v3d_debug_flag_for_shader_stage(c->s->info.stage))) {
2974 fprintf(stderr, "%s prog %d/%d pre-opt VIR:\n",
2975 vir_get_stage_name(c),
2976 c->program_id, c->variant_id);
2977 vir_dump(c);
2978 fprintf(stderr, "\n");
2979 }
2980
2981 vir_optimize(c);
2982
2983 vir_check_payload_w(c);
2984
2985 /* XXX perf: On VC4, we do a VIR-level instruction scheduling here.
2986 * We used that on that platform to pipeline TMU writes and reduce the
2987 * number of thread switches, as well as try (mostly successfully) to
2988 * reduce maximum register pressure to allow more threads. We should
2989 * do something of that sort for V3D -- either instruction scheduling
2990 * here, or delay the the THRSW and LDTMUs from our texture
2991 * instructions until the results are needed.
2992 */
2993
2994 if (V3D_DEBUG & (V3D_DEBUG_VIR |
2995 v3d_debug_flag_for_shader_stage(c->s->info.stage))) {
2996 fprintf(stderr, "%s prog %d/%d VIR:\n",
2997 vir_get_stage_name(c),
2998 c->program_id, c->variant_id);
2999 vir_dump(c);
3000 fprintf(stderr, "\n");
3001 }
3002
3003 /* Attempt to allocate registers for the temporaries. If we fail,
3004 * reduce thread count and try again.
3005 */
3006 int min_threads = (c->devinfo->ver >= 41) ? 2 : 1;
3007 struct qpu_reg *temp_registers;
3008 while (true) {
3009 bool spilled;
3010 temp_registers = v3d_register_allocate(c, &spilled);
3011 if (spilled)
3012 continue;
3013
3014 if (temp_registers)
3015 break;
3016
3017 if (c->threads == min_threads) {
3018 if (c->fallback_scheduler) {
3019 fprintf(stderr,
3020 "Failed to register allocate at %d "
3021 "threads:\n",
3022 c->threads);
3023 vir_dump(c);
3024 }
3025 c->compilation_result =
3026 V3D_COMPILATION_FAILED_REGISTER_ALLOCATION;
3027 return;
3028 }
3029
3030 c->threads /= 2;
3031
3032 if (c->threads == 1)
3033 vir_remove_thrsw(c);
3034 }
3035
3036 if (c->spills &&
3037 (V3D_DEBUG & (V3D_DEBUG_VIR |
3038 v3d_debug_flag_for_shader_stage(c->s->info.stage)))) {
3039 fprintf(stderr, "%s prog %d/%d spilled VIR:\n",
3040 vir_get_stage_name(c),
3041 c->program_id, c->variant_id);
3042 vir_dump(c);
3043 fprintf(stderr, "\n");
3044 }
3045
3046 v3d_vir_to_qpu(c, temp_registers);
3047 }