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