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