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