v3d: Convert to using nir_src_as_uint() from const_value derefs.
[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 static void
36 ntq_emit_cf_list(struct v3d_compile *c, struct exec_list *list);
37
38 static void
39 resize_qreg_array(struct v3d_compile *c,
40 struct qreg **regs,
41 uint32_t *size,
42 uint32_t decl_size)
43 {
44 if (*size >= decl_size)
45 return;
46
47 uint32_t old_size = *size;
48 *size = MAX2(*size * 2, decl_size);
49 *regs = reralloc(c, *regs, struct qreg, *size);
50 if (!*regs) {
51 fprintf(stderr, "Malloc failure\n");
52 abort();
53 }
54
55 for (uint32_t i = old_size; i < *size; i++)
56 (*regs)[i] = c->undef;
57 }
58
59 void
60 vir_emit_thrsw(struct v3d_compile *c)
61 {
62 if (c->threads == 1)
63 return;
64
65 /* Always thread switch after each texture operation for now.
66 *
67 * We could do better by batching a bunch of texture fetches up and
68 * then doing one thread switch and collecting all their results
69 * afterward.
70 */
71 c->last_thrsw = vir_NOP(c);
72 c->last_thrsw->qpu.sig.thrsw = true;
73 c->last_thrsw_at_top_level = (c->execute.file == QFILE_NULL);
74 }
75
76 static struct qreg
77 indirect_uniform_load(struct v3d_compile *c, nir_intrinsic_instr *intr)
78 {
79 struct qreg indirect_offset = ntq_get_src(c, intr->src[0], 0);
80 uint32_t offset = nir_intrinsic_base(intr);
81 struct v3d_ubo_range *range = NULL;
82 unsigned i;
83
84 for (i = 0; i < c->num_ubo_ranges; i++) {
85 range = &c->ubo_ranges[i];
86 if (offset >= range->src_offset &&
87 offset < range->src_offset + range->size) {
88 break;
89 }
90 }
91 /* The driver-location-based offset always has to be within a declared
92 * uniform range.
93 */
94 assert(i != c->num_ubo_ranges);
95 if (!c->ubo_range_used[i]) {
96 c->ubo_range_used[i] = true;
97 range->dst_offset = c->next_ubo_dst_offset;
98 c->next_ubo_dst_offset += range->size;
99 }
100
101 offset -= range->src_offset;
102
103 if (range->dst_offset + offset != 0) {
104 indirect_offset = vir_ADD(c, indirect_offset,
105 vir_uniform_ui(c, range->dst_offset +
106 offset));
107 }
108
109 /* Adjust for where we stored the TGSI register base. */
110 vir_ADD_dest(c,
111 vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_TMUA),
112 vir_uniform(c, QUNIFORM_UBO_ADDR, 0),
113 indirect_offset);
114
115 vir_emit_thrsw(c);
116 return vir_LDTMU(c);
117 }
118
119 static struct qreg *
120 ntq_init_ssa_def(struct v3d_compile *c, nir_ssa_def *def)
121 {
122 struct qreg *qregs = ralloc_array(c->def_ht, struct qreg,
123 def->num_components);
124 _mesa_hash_table_insert(c->def_ht, def, qregs);
125 return qregs;
126 }
127
128 /**
129 * This function is responsible for getting VIR results into the associated
130 * storage for a NIR instruction.
131 *
132 * If it's a NIR SSA def, then we just set the associated hash table entry to
133 * the new result.
134 *
135 * If it's a NIR reg, then we need to update the existing qreg assigned to the
136 * NIR destination with the incoming value. To do that without introducing
137 * new MOVs, we require that the incoming qreg either be a uniform, or be
138 * SSA-defined by the previous VIR instruction in the block and rewritable by
139 * this function. That lets us sneak ahead and insert the SF flag beforehand
140 * (knowing that the previous instruction doesn't depend on flags) and rewrite
141 * its destination to be the NIR reg's destination
142 */
143 void
144 ntq_store_dest(struct v3d_compile *c, nir_dest *dest, int chan,
145 struct qreg result)
146 {
147 struct qinst *last_inst = NULL;
148 if (!list_empty(&c->cur_block->instructions))
149 last_inst = (struct qinst *)c->cur_block->instructions.prev;
150
151 assert(result.file == QFILE_UNIF ||
152 (result.file == QFILE_TEMP &&
153 last_inst && last_inst == c->defs[result.index]));
154
155 if (dest->is_ssa) {
156 assert(chan < dest->ssa.num_components);
157
158 struct qreg *qregs;
159 struct hash_entry *entry =
160 _mesa_hash_table_search(c->def_ht, &dest->ssa);
161
162 if (entry)
163 qregs = entry->data;
164 else
165 qregs = ntq_init_ssa_def(c, &dest->ssa);
166
167 qregs[chan] = result;
168 } else {
169 nir_register *reg = dest->reg.reg;
170 assert(dest->reg.base_offset == 0);
171 assert(reg->num_array_elems == 0);
172 struct hash_entry *entry =
173 _mesa_hash_table_search(c->def_ht, reg);
174 struct qreg *qregs = entry->data;
175
176 /* Insert a MOV if the source wasn't an SSA def in the
177 * previous instruction.
178 */
179 if (result.file == QFILE_UNIF) {
180 result = vir_MOV(c, result);
181 last_inst = c->defs[result.index];
182 }
183
184 /* We know they're both temps, so just rewrite index. */
185 c->defs[last_inst->dst.index] = NULL;
186 last_inst->dst.index = qregs[chan].index;
187
188 /* If we're in control flow, then make this update of the reg
189 * conditional on the execution mask.
190 */
191 if (c->execute.file != QFILE_NULL) {
192 last_inst->dst.index = qregs[chan].index;
193
194 /* Set the flags to the current exec mask.
195 */
196 c->cursor = vir_before_inst(last_inst);
197 vir_PF(c, c->execute, V3D_QPU_PF_PUSHZ);
198 c->cursor = vir_after_inst(last_inst);
199
200 vir_set_cond(last_inst, V3D_QPU_COND_IFA);
201 last_inst->cond_is_exec_mask = true;
202 }
203 }
204 }
205
206 struct qreg
207 ntq_get_src(struct v3d_compile *c, nir_src src, int i)
208 {
209 struct hash_entry *entry;
210 if (src.is_ssa) {
211 entry = _mesa_hash_table_search(c->def_ht, src.ssa);
212 assert(i < src.ssa->num_components);
213 } else {
214 nir_register *reg = src.reg.reg;
215 entry = _mesa_hash_table_search(c->def_ht, reg);
216 assert(reg->num_array_elems == 0);
217 assert(src.reg.base_offset == 0);
218 assert(i < reg->num_components);
219 }
220
221 struct qreg *qregs = entry->data;
222 return qregs[i];
223 }
224
225 static struct qreg
226 ntq_get_alu_src(struct v3d_compile *c, nir_alu_instr *instr,
227 unsigned src)
228 {
229 assert(util_is_power_of_two_or_zero(instr->dest.write_mask));
230 unsigned chan = ffs(instr->dest.write_mask) - 1;
231 struct qreg r = ntq_get_src(c, instr->src[src].src,
232 instr->src[src].swizzle[chan]);
233
234 assert(!instr->src[src].abs);
235 assert(!instr->src[src].negate);
236
237 return r;
238 };
239
240 static struct qreg
241 ntq_minify(struct v3d_compile *c, struct qreg size, struct qreg level)
242 {
243 return vir_MAX(c, vir_SHR(c, size, level), vir_uniform_ui(c, 1));
244 }
245
246 static void
247 ntq_emit_txs(struct v3d_compile *c, nir_tex_instr *instr)
248 {
249 unsigned unit = instr->texture_index;
250 int lod_index = nir_tex_instr_src_index(instr, nir_tex_src_lod);
251 int dest_size = nir_tex_instr_dest_size(instr);
252
253 struct qreg lod = c->undef;
254 if (lod_index != -1)
255 lod = ntq_get_src(c, instr->src[lod_index].src, 0);
256
257 for (int i = 0; i < dest_size; i++) {
258 assert(i < 3);
259 enum quniform_contents contents;
260
261 if (instr->is_array && i == dest_size - 1)
262 contents = QUNIFORM_TEXTURE_ARRAY_SIZE;
263 else
264 contents = QUNIFORM_TEXTURE_WIDTH + i;
265
266 struct qreg size = vir_uniform(c, contents, unit);
267
268 switch (instr->sampler_dim) {
269 case GLSL_SAMPLER_DIM_1D:
270 case GLSL_SAMPLER_DIM_2D:
271 case GLSL_SAMPLER_DIM_3D:
272 case GLSL_SAMPLER_DIM_CUBE:
273 /* Don't minify the array size. */
274 if (!(instr->is_array && i == dest_size - 1)) {
275 size = ntq_minify(c, size, lod);
276 }
277 break;
278
279 case GLSL_SAMPLER_DIM_RECT:
280 /* There's no LOD field for rects */
281 break;
282
283 default:
284 unreachable("Bad sampler type");
285 }
286
287 ntq_store_dest(c, &instr->dest, i, size);
288 }
289 }
290
291 static void
292 ntq_emit_tex(struct v3d_compile *c, nir_tex_instr *instr)
293 {
294 unsigned unit = instr->texture_index;
295
296 /* Since each texture sampling op requires uploading uniforms to
297 * reference the texture, there's no HW support for texture size and
298 * you just upload uniforms containing the size.
299 */
300 switch (instr->op) {
301 case nir_texop_query_levels:
302 ntq_store_dest(c, &instr->dest, 0,
303 vir_uniform(c, QUNIFORM_TEXTURE_LEVELS, unit));
304 return;
305 case nir_texop_txs:
306 ntq_emit_txs(c, instr);
307 return;
308 default:
309 break;
310 }
311
312 if (c->devinfo->ver >= 40)
313 v3d40_vir_emit_tex(c, instr);
314 else
315 v3d33_vir_emit_tex(c, instr);
316 }
317
318 static struct qreg
319 ntq_fsincos(struct v3d_compile *c, struct qreg src, bool is_cos)
320 {
321 struct qreg input = vir_FMUL(c, src, vir_uniform_f(c, 1.0f / M_PI));
322 if (is_cos)
323 input = vir_FADD(c, input, vir_uniform_f(c, 0.5));
324
325 struct qreg periods = vir_FROUND(c, input);
326 struct qreg sin_output = vir_SIN(c, vir_FSUB(c, input, periods));
327 return vir_XOR(c, sin_output, vir_SHL(c,
328 vir_FTOIN(c, periods),
329 vir_uniform_ui(c, -1)));
330 }
331
332 static struct qreg
333 ntq_fsign(struct v3d_compile *c, struct qreg src)
334 {
335 struct qreg t = vir_get_temp(c);
336
337 vir_MOV_dest(c, t, vir_uniform_f(c, 0.0));
338 vir_PF(c, vir_FMOV(c, src), V3D_QPU_PF_PUSHZ);
339 vir_MOV_cond(c, V3D_QPU_COND_IFNA, t, vir_uniform_f(c, 1.0));
340 vir_PF(c, vir_FMOV(c, src), V3D_QPU_PF_PUSHN);
341 vir_MOV_cond(c, V3D_QPU_COND_IFA, t, vir_uniform_f(c, -1.0));
342 return vir_MOV(c, t);
343 }
344
345 static struct qreg
346 ntq_isign(struct v3d_compile *c, struct qreg src)
347 {
348 struct qreg t = vir_get_temp(c);
349
350 vir_MOV_dest(c, t, vir_uniform_ui(c, 0));
351 vir_PF(c, vir_MOV(c, src), V3D_QPU_PF_PUSHZ);
352 vir_MOV_cond(c, V3D_QPU_COND_IFNA, t, vir_uniform_ui(c, 1));
353 vir_PF(c, vir_MOV(c, src), V3D_QPU_PF_PUSHN);
354 vir_MOV_cond(c, V3D_QPU_COND_IFA, t, vir_uniform_ui(c, -1));
355 return vir_MOV(c, t);
356 }
357
358 static void
359 emit_fragcoord_input(struct v3d_compile *c, int attr)
360 {
361 c->inputs[attr * 4 + 0] = vir_FXCD(c);
362 c->inputs[attr * 4 + 1] = vir_FYCD(c);
363 c->inputs[attr * 4 + 2] = c->payload_z;
364 c->inputs[attr * 4 + 3] = vir_RECIP(c, c->payload_w);
365 }
366
367 static struct qreg
368 emit_fragment_varying(struct v3d_compile *c, nir_variable *var,
369 uint8_t swizzle)
370 {
371 struct qreg r3 = vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_R3);
372 struct qreg r5 = vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_R5);
373
374 struct qreg vary;
375 if (c->devinfo->ver >= 41) {
376 struct qinst *ldvary = vir_add_inst(V3D_QPU_A_NOP, c->undef,
377 c->undef, c->undef);
378 ldvary->qpu.sig.ldvary = true;
379 vary = vir_emit_def(c, ldvary);
380 } else {
381 vir_NOP(c)->qpu.sig.ldvary = true;
382 vary = r3;
383 }
384
385 /* For gl_PointCoord input or distance along a line, we'll be called
386 * with no nir_variable, and we don't count toward VPM size so we
387 * don't track an input slot.
388 */
389 if (!var) {
390 return vir_FADD(c, vir_FMUL(c, vary, c->payload_w), r5);
391 }
392
393 int i = c->num_inputs++;
394 c->input_slots[i] = v3d_slot_from_slot_and_component(var->data.location,
395 swizzle);
396
397 switch (var->data.interpolation) {
398 case INTERP_MODE_NONE:
399 /* If a gl_FrontColor or gl_BackColor input has no interp
400 * qualifier, then if we're using glShadeModel(GL_FLAT) it
401 * needs to be flat shaded.
402 */
403 switch (var->data.location) {
404 case VARYING_SLOT_COL0:
405 case VARYING_SLOT_COL1:
406 case VARYING_SLOT_BFC0:
407 case VARYING_SLOT_BFC1:
408 if (c->fs_key->shade_model_flat) {
409 BITSET_SET(c->flat_shade_flags, i);
410 vir_MOV_dest(c, c->undef, vary);
411 return vir_MOV(c, r5);
412 } else {
413 return vir_FADD(c, vir_FMUL(c, vary,
414 c->payload_w), r5);
415 }
416 default:
417 break;
418 }
419 /* FALLTHROUGH */
420 case INTERP_MODE_SMOOTH:
421 if (var->data.centroid) {
422 BITSET_SET(c->centroid_flags, i);
423 return vir_FADD(c, vir_FMUL(c, vary,
424 c->payload_w_centroid), r5);
425 } else {
426 return vir_FADD(c, vir_FMUL(c, vary, c->payload_w), r5);
427 }
428 case INTERP_MODE_NOPERSPECTIVE:
429 BITSET_SET(c->noperspective_flags, i);
430 return vir_FADD(c, vir_MOV(c, vary), r5);
431 case INTERP_MODE_FLAT:
432 BITSET_SET(c->flat_shade_flags, i);
433 vir_MOV_dest(c, c->undef, vary);
434 return vir_MOV(c, r5);
435 default:
436 unreachable("Bad interp mode");
437 }
438 }
439
440 static void
441 emit_fragment_input(struct v3d_compile *c, int attr, nir_variable *var)
442 {
443 for (int i = 0; i < glsl_get_vector_elements(var->type); i++) {
444 int chan = var->data.location_frac + i;
445 c->inputs[attr * 4 + chan] =
446 emit_fragment_varying(c, var, chan);
447 }
448 }
449
450 static void
451 add_output(struct v3d_compile *c,
452 uint32_t decl_offset,
453 uint8_t slot,
454 uint8_t swizzle)
455 {
456 uint32_t old_array_size = c->outputs_array_size;
457 resize_qreg_array(c, &c->outputs, &c->outputs_array_size,
458 decl_offset + 1);
459
460 if (old_array_size != c->outputs_array_size) {
461 c->output_slots = reralloc(c,
462 c->output_slots,
463 struct v3d_varying_slot,
464 c->outputs_array_size);
465 }
466
467 c->output_slots[decl_offset] =
468 v3d_slot_from_slot_and_component(slot, swizzle);
469 }
470
471 static void
472 declare_uniform_range(struct v3d_compile *c, uint32_t start, uint32_t size)
473 {
474 unsigned array_id = c->num_ubo_ranges++;
475 if (array_id >= c->ubo_ranges_array_size) {
476 c->ubo_ranges_array_size = MAX2(c->ubo_ranges_array_size * 2,
477 array_id + 1);
478 c->ubo_ranges = reralloc(c, c->ubo_ranges,
479 struct v3d_ubo_range,
480 c->ubo_ranges_array_size);
481 c->ubo_range_used = reralloc(c, c->ubo_range_used,
482 bool,
483 c->ubo_ranges_array_size);
484 }
485
486 c->ubo_ranges[array_id].dst_offset = 0;
487 c->ubo_ranges[array_id].src_offset = start;
488 c->ubo_ranges[array_id].size = size;
489 c->ubo_range_used[array_id] = false;
490 }
491
492 /**
493 * If compare_instr is a valid comparison instruction, emits the
494 * compare_instr's comparison and returns the sel_instr's return value based
495 * on the compare_instr's result.
496 */
497 static bool
498 ntq_emit_comparison(struct v3d_compile *c, struct qreg *dest,
499 nir_alu_instr *compare_instr,
500 nir_alu_instr *sel_instr)
501 {
502 struct qreg src0 = ntq_get_alu_src(c, compare_instr, 0);
503 struct qreg src1;
504 if (nir_op_infos[compare_instr->op].num_inputs > 1)
505 src1 = ntq_get_alu_src(c, compare_instr, 1);
506 bool cond_invert = false;
507
508 switch (compare_instr->op) {
509 case nir_op_feq:
510 case nir_op_seq:
511 vir_PF(c, vir_FCMP(c, src0, src1), V3D_QPU_PF_PUSHZ);
512 break;
513 case nir_op_ieq:
514 vir_PF(c, vir_XOR(c, src0, src1), V3D_QPU_PF_PUSHZ);
515 break;
516
517 case nir_op_fne:
518 case nir_op_sne:
519 vir_PF(c, vir_FCMP(c, src0, src1), V3D_QPU_PF_PUSHZ);
520 cond_invert = true;
521 break;
522 case nir_op_ine:
523 vir_PF(c, vir_XOR(c, src0, src1), V3D_QPU_PF_PUSHZ);
524 cond_invert = true;
525 break;
526
527 case nir_op_fge:
528 case nir_op_sge:
529 vir_PF(c, vir_FCMP(c, src1, src0), V3D_QPU_PF_PUSHC);
530 break;
531 case nir_op_ige:
532 vir_PF(c, vir_MIN(c, src1, src0), V3D_QPU_PF_PUSHC);
533 cond_invert = true;
534 break;
535 case nir_op_uge:
536 vir_PF(c, vir_SUB(c, src0, src1), V3D_QPU_PF_PUSHC);
537 cond_invert = true;
538 break;
539
540 case nir_op_slt:
541 case nir_op_flt:
542 vir_PF(c, vir_FCMP(c, src0, src1), V3D_QPU_PF_PUSHN);
543 break;
544 case nir_op_ilt:
545 vir_PF(c, vir_MIN(c, src1, src0), V3D_QPU_PF_PUSHC);
546 break;
547 case nir_op_ult:
548 vir_PF(c, vir_SUB(c, src0, src1), V3D_QPU_PF_PUSHC);
549 break;
550
551 default:
552 return false;
553 }
554
555 enum v3d_qpu_cond cond = (cond_invert ?
556 V3D_QPU_COND_IFNA :
557 V3D_QPU_COND_IFA);
558
559 switch (sel_instr->op) {
560 case nir_op_seq:
561 case nir_op_sne:
562 case nir_op_sge:
563 case nir_op_slt:
564 *dest = vir_SEL(c, cond,
565 vir_uniform_f(c, 1.0), vir_uniform_f(c, 0.0));
566 break;
567
568 case nir_op_bcsel:
569 *dest = vir_SEL(c, cond,
570 ntq_get_alu_src(c, sel_instr, 1),
571 ntq_get_alu_src(c, sel_instr, 2));
572 break;
573
574 default:
575 *dest = vir_SEL(c, cond,
576 vir_uniform_ui(c, ~0), vir_uniform_ui(c, 0));
577 break;
578 }
579
580 /* Make the temporary for nir_store_dest(). */
581 *dest = vir_MOV(c, *dest);
582
583 return true;
584 }
585
586 /**
587 * Attempts to fold a comparison generating a boolean result into the
588 * condition code for selecting between two values, instead of comparing the
589 * boolean result against 0 to generate the condition code.
590 */
591 static struct qreg ntq_emit_bcsel(struct v3d_compile *c, nir_alu_instr *instr,
592 struct qreg *src)
593 {
594 if (!instr->src[0].src.is_ssa)
595 goto out;
596 if (instr->src[0].src.ssa->parent_instr->type != nir_instr_type_alu)
597 goto out;
598 nir_alu_instr *compare =
599 nir_instr_as_alu(instr->src[0].src.ssa->parent_instr);
600 if (!compare)
601 goto out;
602
603 struct qreg dest;
604 if (ntq_emit_comparison(c, &dest, compare, instr))
605 return dest;
606
607 out:
608 vir_PF(c, src[0], V3D_QPU_PF_PUSHZ);
609 return vir_MOV(c, vir_SEL(c, V3D_QPU_COND_IFNA, src[1], src[2]));
610 }
611
612
613 static void
614 ntq_emit_alu(struct v3d_compile *c, nir_alu_instr *instr)
615 {
616 /* This should always be lowered to ALU operations for V3D. */
617 assert(!instr->dest.saturate);
618
619 /* Vectors are special in that they have non-scalarized writemasks,
620 * and just take the first swizzle channel for each argument in order
621 * into each writemask channel.
622 */
623 if (instr->op == nir_op_vec2 ||
624 instr->op == nir_op_vec3 ||
625 instr->op == nir_op_vec4) {
626 struct qreg srcs[4];
627 for (int i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
628 srcs[i] = ntq_get_src(c, instr->src[i].src,
629 instr->src[i].swizzle[0]);
630 for (int i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
631 ntq_store_dest(c, &instr->dest.dest, i,
632 vir_MOV(c, srcs[i]));
633 return;
634 }
635
636 /* General case: We can just grab the one used channel per src. */
637 struct qreg src[nir_op_infos[instr->op].num_inputs];
638 for (int i = 0; i < nir_op_infos[instr->op].num_inputs; i++) {
639 src[i] = ntq_get_alu_src(c, instr, i);
640 }
641
642 struct qreg result;
643
644 switch (instr->op) {
645 case nir_op_fmov:
646 case nir_op_imov:
647 result = vir_MOV(c, src[0]);
648 break;
649
650 case nir_op_fneg:
651 result = vir_XOR(c, src[0], vir_uniform_ui(c, 1 << 31));
652 break;
653 case nir_op_ineg:
654 result = vir_NEG(c, src[0]);
655 break;
656
657 case nir_op_fmul:
658 result = vir_FMUL(c, src[0], src[1]);
659 break;
660 case nir_op_fadd:
661 result = vir_FADD(c, src[0], src[1]);
662 break;
663 case nir_op_fsub:
664 result = vir_FSUB(c, src[0], src[1]);
665 break;
666 case nir_op_fmin:
667 result = vir_FMIN(c, src[0], src[1]);
668 break;
669 case nir_op_fmax:
670 result = vir_FMAX(c, src[0], src[1]);
671 break;
672
673 case nir_op_f2i32:
674 result = vir_FTOIZ(c, src[0]);
675 break;
676 case nir_op_f2u32:
677 result = vir_FTOUZ(c, src[0]);
678 break;
679 case nir_op_i2f32:
680 result = vir_ITOF(c, src[0]);
681 break;
682 case nir_op_u2f32:
683 result = vir_UTOF(c, src[0]);
684 break;
685 case nir_op_b2f32:
686 result = vir_AND(c, src[0], vir_uniform_f(c, 1.0));
687 break;
688 case nir_op_b2i32:
689 result = vir_AND(c, src[0], vir_uniform_ui(c, 1));
690 break;
691 case nir_op_i2b32:
692 case nir_op_f2b32:
693 vir_PF(c, src[0], V3D_QPU_PF_PUSHZ);
694 result = vir_MOV(c, vir_SEL(c, V3D_QPU_COND_IFNA,
695 vir_uniform_ui(c, ~0),
696 vir_uniform_ui(c, 0)));
697 break;
698
699 case nir_op_iadd:
700 result = vir_ADD(c, src[0], src[1]);
701 break;
702 case nir_op_ushr:
703 result = vir_SHR(c, src[0], src[1]);
704 break;
705 case nir_op_isub:
706 result = vir_SUB(c, src[0], src[1]);
707 break;
708 case nir_op_ishr:
709 result = vir_ASR(c, src[0], src[1]);
710 break;
711 case nir_op_ishl:
712 result = vir_SHL(c, src[0], src[1]);
713 break;
714 case nir_op_imin:
715 result = vir_MIN(c, src[0], src[1]);
716 break;
717 case nir_op_umin:
718 result = vir_UMIN(c, src[0], src[1]);
719 break;
720 case nir_op_imax:
721 result = vir_MAX(c, src[0], src[1]);
722 break;
723 case nir_op_umax:
724 result = vir_UMAX(c, src[0], src[1]);
725 break;
726 case nir_op_iand:
727 result = vir_AND(c, src[0], src[1]);
728 break;
729 case nir_op_ior:
730 result = vir_OR(c, src[0], src[1]);
731 break;
732 case nir_op_ixor:
733 result = vir_XOR(c, src[0], src[1]);
734 break;
735 case nir_op_inot:
736 result = vir_NOT(c, src[0]);
737 break;
738
739 case nir_op_ufind_msb:
740 result = vir_SUB(c, vir_uniform_ui(c, 31), vir_CLZ(c, src[0]));
741 break;
742
743 case nir_op_imul:
744 result = vir_UMUL(c, src[0], src[1]);
745 break;
746
747 case nir_op_seq:
748 case nir_op_sne:
749 case nir_op_sge:
750 case nir_op_slt:
751 case nir_op_feq:
752 case nir_op_fne:
753 case nir_op_fge:
754 case nir_op_flt:
755 case nir_op_ieq:
756 case nir_op_ine:
757 case nir_op_ige:
758 case nir_op_uge:
759 case nir_op_ilt:
760 case nir_op_ult:
761 if (!ntq_emit_comparison(c, &result, instr, instr)) {
762 fprintf(stderr, "Bad comparison instruction\n");
763 }
764 break;
765
766 case nir_op_bcsel:
767 result = ntq_emit_bcsel(c, instr, src);
768 break;
769 case nir_op_fcsel:
770 vir_PF(c, src[0], V3D_QPU_PF_PUSHZ);
771 result = vir_MOV(c, vir_SEL(c, V3D_QPU_COND_IFNA,
772 src[1], src[2]));
773 break;
774
775 case nir_op_frcp:
776 result = vir_RECIP(c, src[0]);
777 break;
778 case nir_op_frsq:
779 result = vir_RSQRT(c, src[0]);
780 break;
781 case nir_op_fexp2:
782 result = vir_EXP(c, src[0]);
783 break;
784 case nir_op_flog2:
785 result = vir_LOG(c, src[0]);
786 break;
787
788 case nir_op_fceil:
789 result = vir_FCEIL(c, src[0]);
790 break;
791 case nir_op_ffloor:
792 result = vir_FFLOOR(c, src[0]);
793 break;
794 case nir_op_fround_even:
795 result = vir_FROUND(c, src[0]);
796 break;
797 case nir_op_ftrunc:
798 result = vir_FTRUNC(c, src[0]);
799 break;
800 case nir_op_ffract:
801 result = vir_FSUB(c, src[0], vir_FFLOOR(c, src[0]));
802 break;
803
804 case nir_op_fsin:
805 result = ntq_fsincos(c, src[0], false);
806 break;
807 case nir_op_fcos:
808 result = ntq_fsincos(c, src[0], true);
809 break;
810
811 case nir_op_fsign:
812 result = ntq_fsign(c, src[0]);
813 break;
814 case nir_op_isign:
815 result = ntq_isign(c, src[0]);
816 break;
817
818 case nir_op_fabs: {
819 result = vir_FMOV(c, src[0]);
820 vir_set_unpack(c->defs[result.index], 0, V3D_QPU_UNPACK_ABS);
821 break;
822 }
823
824 case nir_op_iabs:
825 result = vir_MAX(c, src[0],
826 vir_SUB(c, vir_uniform_ui(c, 0), src[0]));
827 break;
828
829 case nir_op_fddx:
830 case nir_op_fddx_coarse:
831 case nir_op_fddx_fine:
832 result = vir_FDX(c, src[0]);
833 break;
834
835 case nir_op_fddy:
836 case nir_op_fddy_coarse:
837 case nir_op_fddy_fine:
838 result = vir_FDY(c, src[0]);
839 break;
840
841 case nir_op_uadd_carry:
842 vir_PF(c, vir_ADD(c, src[0], src[1]), V3D_QPU_PF_PUSHC);
843 result = vir_MOV(c, vir_SEL(c, V3D_QPU_COND_IFA,
844 vir_uniform_ui(c, ~0),
845 vir_uniform_ui(c, 0)));
846 break;
847
848 case nir_op_pack_half_2x16_split:
849 result = vir_VFPACK(c, src[0], src[1]);
850 break;
851
852 case nir_op_unpack_half_2x16_split_x:
853 result = vir_FMOV(c, src[0]);
854 vir_set_unpack(c->defs[result.index], 0, V3D_QPU_UNPACK_L);
855 break;
856
857 case nir_op_unpack_half_2x16_split_y:
858 result = vir_FMOV(c, src[0]);
859 vir_set_unpack(c->defs[result.index], 0, V3D_QPU_UNPACK_H);
860 break;
861
862 default:
863 fprintf(stderr, "unknown NIR ALU inst: ");
864 nir_print_instr(&instr->instr, stderr);
865 fprintf(stderr, "\n");
866 abort();
867 }
868
869 /* We have a scalar result, so the instruction should only have a
870 * single channel written to.
871 */
872 assert(util_is_power_of_two_or_zero(instr->dest.write_mask));
873 ntq_store_dest(c, &instr->dest.dest,
874 ffs(instr->dest.write_mask) - 1, result);
875 }
876
877 /* Each TLB read/write setup (a render target or depth buffer) takes an 8-bit
878 * specifier. They come from a register that's preloaded with 0xffffffff
879 * (0xff gets you normal vec4 f16 RT0 writes), and when one is neaded the low
880 * 8 bits are shifted off the bottom and 0xff shifted in from the top.
881 */
882 #define TLB_TYPE_F16_COLOR (3 << 6)
883 #define TLB_TYPE_I32_COLOR (1 << 6)
884 #define TLB_TYPE_F32_COLOR (0 << 6)
885 #define TLB_RENDER_TARGET_SHIFT 3 /* Reversed! 7 = RT 0, 0 = RT 7. */
886 #define TLB_SAMPLE_MODE_PER_SAMPLE (0 << 2)
887 #define TLB_SAMPLE_MODE_PER_PIXEL (1 << 2)
888 #define TLB_F16_SWAP_HI_LO (1 << 1)
889 #define TLB_VEC_SIZE_4_F16 (1 << 0)
890 #define TLB_VEC_SIZE_2_F16 (0 << 0)
891 #define TLB_VEC_SIZE_MINUS_1_SHIFT 0
892
893 /* Triggers Z/Stencil testing, used when the shader state's "FS modifies Z"
894 * flag is set.
895 */
896 #define TLB_TYPE_DEPTH ((2 << 6) | (0 << 4))
897 #define TLB_DEPTH_TYPE_INVARIANT (0 << 2) /* Unmodified sideband input used */
898 #define TLB_DEPTH_TYPE_PER_PIXEL (1 << 2) /* QPU result used */
899 #define TLB_V42_DEPTH_TYPE_INVARIANT (0 << 3) /* Unmodified sideband input used */
900 #define TLB_V42_DEPTH_TYPE_PER_PIXEL (1 << 3) /* QPU result used */
901
902 /* Stencil is a single 32-bit write. */
903 #define TLB_TYPE_STENCIL_ALPHA ((2 << 6) | (1 << 4))
904
905 static void
906 emit_frag_end(struct v3d_compile *c)
907 {
908 /* XXX
909 if (c->output_sample_mask_index != -1) {
910 vir_MS_MASK(c, c->outputs[c->output_sample_mask_index]);
911 }
912 */
913
914 bool has_any_tlb_color_write = false;
915 for (int rt = 0; rt < c->fs_key->nr_cbufs; rt++) {
916 if (c->output_color_var[rt])
917 has_any_tlb_color_write = true;
918 }
919
920 if (c->fs_key->sample_alpha_to_coverage && c->output_color_var[0]) {
921 struct nir_variable *var = c->output_color_var[0];
922 struct qreg *color = &c->outputs[var->data.driver_location * 4];
923
924 vir_SETMSF_dest(c, vir_reg(QFILE_NULL, 0),
925 vir_AND(c,
926 vir_MSF(c),
927 vir_FTOC(c, color[3])));
928 }
929
930 if (c->output_position_index != -1) {
931 struct qinst *inst = vir_MOV_dest(c,
932 vir_reg(QFILE_TLBU, 0),
933 c->outputs[c->output_position_index]);
934 uint8_t tlb_specifier = TLB_TYPE_DEPTH;
935
936 if (c->devinfo->ver >= 42) {
937 tlb_specifier |= (TLB_V42_DEPTH_TYPE_PER_PIXEL |
938 TLB_SAMPLE_MODE_PER_PIXEL);
939 } else
940 tlb_specifier |= TLB_DEPTH_TYPE_PER_PIXEL;
941
942 inst->src[vir_get_implicit_uniform_src(inst)] =
943 vir_uniform_ui(c, tlb_specifier | 0xffffff00);
944 } else if (c->s->info.fs.uses_discard ||
945 c->fs_key->sample_alpha_to_coverage ||
946 !has_any_tlb_color_write) {
947 /* Emit passthrough Z if it needed to be delayed until shader
948 * end due to potential discards.
949 *
950 * Since (single-threaded) fragment shaders always need a TLB
951 * write, emit passthrouh Z if we didn't have any color
952 * buffers and flag us as potentially discarding, so that we
953 * can use Z as the TLB write.
954 */
955 c->s->info.fs.uses_discard = true;
956
957 struct qinst *inst = vir_MOV_dest(c,
958 vir_reg(QFILE_TLBU, 0),
959 vir_reg(QFILE_NULL, 0));
960 uint8_t tlb_specifier = TLB_TYPE_DEPTH;
961
962 if (c->devinfo->ver >= 42) {
963 /* The spec says the PER_PIXEL flag is ignored for
964 * invariant writes, but the simulator demands it.
965 */
966 tlb_specifier |= (TLB_V42_DEPTH_TYPE_INVARIANT |
967 TLB_SAMPLE_MODE_PER_PIXEL);
968 } else {
969 tlb_specifier |= TLB_DEPTH_TYPE_INVARIANT;
970 }
971
972 inst->src[vir_get_implicit_uniform_src(inst)] =
973 vir_uniform_ui(c, tlb_specifier | 0xffffff00);
974 }
975
976 /* XXX: Performance improvement: Merge Z write and color writes TLB
977 * uniform setup
978 */
979
980 for (int rt = 0; rt < c->fs_key->nr_cbufs; rt++) {
981 if (!c->output_color_var[rt])
982 continue;
983
984 nir_variable *var = c->output_color_var[rt];
985 struct qreg *color = &c->outputs[var->data.driver_location * 4];
986 int num_components = glsl_get_vector_elements(var->type);
987 uint32_t conf = 0xffffff00;
988 struct qinst *inst;
989
990 conf |= TLB_SAMPLE_MODE_PER_PIXEL;
991 conf |= (7 - rt) << TLB_RENDER_TARGET_SHIFT;
992
993 if (c->fs_key->swap_color_rb & (1 << rt))
994 num_components = MAX2(num_components, 3);
995
996 assert(num_components != 0);
997 switch (glsl_get_base_type(var->type)) {
998 case GLSL_TYPE_UINT:
999 case GLSL_TYPE_INT:
1000 /* The F32 vs I32 distinction was dropped in 4.2. */
1001 if (c->devinfo->ver < 42)
1002 conf |= TLB_TYPE_I32_COLOR;
1003 else
1004 conf |= TLB_TYPE_F32_COLOR;
1005 conf |= ((num_components - 1) <<
1006 TLB_VEC_SIZE_MINUS_1_SHIFT);
1007
1008 inst = vir_MOV_dest(c, vir_reg(QFILE_TLBU, 0), color[0]);
1009 inst->src[vir_get_implicit_uniform_src(inst)] =
1010 vir_uniform_ui(c, conf);
1011
1012 for (int i = 1; i < num_components; i++) {
1013 inst = vir_MOV_dest(c, vir_reg(QFILE_TLB, 0),
1014 color[i]);
1015 }
1016 break;
1017
1018 default: {
1019 struct qreg r = color[0];
1020 struct qreg g = color[1];
1021 struct qreg b = color[2];
1022 struct qreg a = color[3];
1023
1024 if (c->fs_key->f32_color_rb & (1 << rt)) {
1025 conf |= TLB_TYPE_F32_COLOR;
1026 conf |= ((num_components - 1) <<
1027 TLB_VEC_SIZE_MINUS_1_SHIFT);
1028 } else {
1029 conf |= TLB_TYPE_F16_COLOR;
1030 conf |= TLB_F16_SWAP_HI_LO;
1031 if (num_components >= 3)
1032 conf |= TLB_VEC_SIZE_4_F16;
1033 else
1034 conf |= TLB_VEC_SIZE_2_F16;
1035 }
1036
1037 if (c->fs_key->swap_color_rb & (1 << rt)) {
1038 r = color[2];
1039 b = color[0];
1040 }
1041
1042 if (c->fs_key->sample_alpha_to_one)
1043 a = vir_uniform_f(c, 1.0);
1044
1045 if (c->fs_key->f32_color_rb & (1 << rt)) {
1046 inst = vir_MOV_dest(c, vir_reg(QFILE_TLBU, 0), r);
1047 inst->src[vir_get_implicit_uniform_src(inst)] =
1048 vir_uniform_ui(c, conf);
1049
1050 if (num_components >= 2)
1051 vir_MOV_dest(c, vir_reg(QFILE_TLB, 0), g);
1052 if (num_components >= 3)
1053 vir_MOV_dest(c, vir_reg(QFILE_TLB, 0), b);
1054 if (num_components >= 4)
1055 vir_MOV_dest(c, vir_reg(QFILE_TLB, 0), a);
1056 } else {
1057 inst = vir_VFPACK_dest(c, vir_reg(QFILE_TLB, 0), r, g);
1058 if (conf != ~0) {
1059 inst->dst.file = QFILE_TLBU;
1060 inst->src[vir_get_implicit_uniform_src(inst)] =
1061 vir_uniform_ui(c, conf);
1062 }
1063
1064 if (num_components >= 3)
1065 inst = vir_VFPACK_dest(c, vir_reg(QFILE_TLB, 0), b, a);
1066 }
1067 break;
1068 }
1069 }
1070 }
1071 }
1072
1073 static void
1074 vir_VPM_WRITE(struct v3d_compile *c, struct qreg val, uint32_t *vpm_index)
1075 {
1076 if (c->devinfo->ver >= 40) {
1077 vir_STVPMV(c, vir_uniform_ui(c, *vpm_index), val);
1078 *vpm_index = *vpm_index + 1;
1079 } else {
1080 vir_MOV_dest(c, vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_VPM), val);
1081 }
1082
1083 c->num_vpm_writes++;
1084 }
1085
1086 static void
1087 emit_scaled_viewport_write(struct v3d_compile *c, struct qreg rcp_w,
1088 uint32_t *vpm_index)
1089 {
1090 for (int i = 0; i < 2; i++) {
1091 struct qreg coord = c->outputs[c->output_position_index + i];
1092 coord = vir_FMUL(c, coord,
1093 vir_uniform(c, QUNIFORM_VIEWPORT_X_SCALE + i,
1094 0));
1095 coord = vir_FMUL(c, coord, rcp_w);
1096 vir_VPM_WRITE(c, vir_FTOIN(c, coord), vpm_index);
1097 }
1098
1099 }
1100
1101 static void
1102 emit_zs_write(struct v3d_compile *c, struct qreg rcp_w, uint32_t *vpm_index)
1103 {
1104 struct qreg zscale = vir_uniform(c, QUNIFORM_VIEWPORT_Z_SCALE, 0);
1105 struct qreg zoffset = vir_uniform(c, QUNIFORM_VIEWPORT_Z_OFFSET, 0);
1106
1107 struct qreg z = c->outputs[c->output_position_index + 2];
1108 z = vir_FMUL(c, z, zscale);
1109 z = vir_FMUL(c, z, rcp_w);
1110 z = vir_FADD(c, z, zoffset);
1111 vir_VPM_WRITE(c, z, vpm_index);
1112 }
1113
1114 static void
1115 emit_rcp_wc_write(struct v3d_compile *c, struct qreg rcp_w, uint32_t *vpm_index)
1116 {
1117 vir_VPM_WRITE(c, rcp_w, vpm_index);
1118 }
1119
1120 static void
1121 emit_point_size_write(struct v3d_compile *c, uint32_t *vpm_index)
1122 {
1123 struct qreg point_size;
1124
1125 if (c->output_point_size_index != -1)
1126 point_size = c->outputs[c->output_point_size_index];
1127 else
1128 point_size = vir_uniform_f(c, 1.0);
1129
1130 /* Workaround: HW-2726 PTB does not handle zero-size points (BCM2835,
1131 * BCM21553).
1132 */
1133 point_size = vir_FMAX(c, point_size, vir_uniform_f(c, .125));
1134
1135 vir_VPM_WRITE(c, point_size, vpm_index);
1136 }
1137
1138 static void
1139 emit_vpm_write_setup(struct v3d_compile *c)
1140 {
1141 if (c->devinfo->ver >= 40)
1142 return;
1143
1144 v3d33_vir_vpm_write_setup(c);
1145 }
1146
1147 /**
1148 * Sets up c->outputs[c->output_position_index] for the vertex shader
1149 * epilogue, if an output vertex position wasn't specified in the user's
1150 * shader. This may be the case for transform feedback with rasterizer
1151 * discard enabled.
1152 */
1153 static void
1154 setup_default_position(struct v3d_compile *c)
1155 {
1156 if (c->output_position_index != -1)
1157 return;
1158
1159 c->output_position_index = c->outputs_array_size;
1160 for (int i = 0; i < 4; i++) {
1161 add_output(c,
1162 c->output_position_index + i,
1163 VARYING_SLOT_POS, i);
1164 }
1165 }
1166
1167 static void
1168 emit_vert_end(struct v3d_compile *c)
1169 {
1170 setup_default_position(c);
1171
1172 uint32_t vpm_index = 0;
1173 struct qreg rcp_w = vir_RECIP(c,
1174 c->outputs[c->output_position_index + 3]);
1175
1176 emit_vpm_write_setup(c);
1177
1178 if (c->vs_key->is_coord) {
1179 for (int i = 0; i < 4; i++)
1180 vir_VPM_WRITE(c, c->outputs[c->output_position_index + i],
1181 &vpm_index);
1182 emit_scaled_viewport_write(c, rcp_w, &vpm_index);
1183 if (c->vs_key->per_vertex_point_size) {
1184 emit_point_size_write(c, &vpm_index);
1185 /* emit_rcp_wc_write(c, rcp_w); */
1186 }
1187 /* XXX: Z-only rendering */
1188 if (0)
1189 emit_zs_write(c, rcp_w, &vpm_index);
1190 } else {
1191 emit_scaled_viewport_write(c, rcp_w, &vpm_index);
1192 emit_zs_write(c, rcp_w, &vpm_index);
1193 emit_rcp_wc_write(c, rcp_w, &vpm_index);
1194 if (c->vs_key->per_vertex_point_size)
1195 emit_point_size_write(c, &vpm_index);
1196 }
1197
1198 for (int i = 0; i < c->vs_key->num_fs_inputs; i++) {
1199 struct v3d_varying_slot input = c->vs_key->fs_inputs[i];
1200 int j;
1201
1202 for (j = 0; j < c->num_outputs; j++) {
1203 struct v3d_varying_slot output = c->output_slots[j];
1204
1205 if (!memcmp(&input, &output, sizeof(input))) {
1206 vir_VPM_WRITE(c, c->outputs[j],
1207 &vpm_index);
1208 break;
1209 }
1210 }
1211 /* Emit padding if we didn't find a declared VS output for
1212 * this FS input.
1213 */
1214 if (j == c->num_outputs)
1215 vir_VPM_WRITE(c, vir_uniform_f(c, 0.0),
1216 &vpm_index);
1217 }
1218
1219 /* GFXH-1684: VPM writes need to be complete by the end of the shader.
1220 */
1221 if (c->devinfo->ver >= 40 && c->devinfo->ver <= 42)
1222 vir_VPMWT(c);
1223 }
1224
1225 void
1226 v3d_optimize_nir(struct nir_shader *s)
1227 {
1228 bool progress;
1229
1230 do {
1231 progress = false;
1232
1233 NIR_PASS_V(s, nir_lower_vars_to_ssa);
1234 NIR_PASS(progress, s, nir_lower_alu_to_scalar);
1235 NIR_PASS(progress, s, nir_lower_phis_to_scalar);
1236 NIR_PASS(progress, s, nir_copy_prop);
1237 NIR_PASS(progress, s, nir_opt_remove_phis);
1238 NIR_PASS(progress, s, nir_opt_dce);
1239 NIR_PASS(progress, s, nir_opt_dead_cf);
1240 NIR_PASS(progress, s, nir_opt_cse);
1241 NIR_PASS(progress, s, nir_opt_peephole_select, 8);
1242 NIR_PASS(progress, s, nir_opt_algebraic);
1243 NIR_PASS(progress, s, nir_opt_constant_folding);
1244 NIR_PASS(progress, s, nir_opt_undef);
1245 } while (progress);
1246
1247 NIR_PASS(progress, s, nir_opt_move_load_ubo);
1248 }
1249
1250 static int
1251 driver_location_compare(const void *in_a, const void *in_b)
1252 {
1253 const nir_variable *const *a = in_a;
1254 const nir_variable *const *b = in_b;
1255
1256 return (*a)->data.driver_location - (*b)->data.driver_location;
1257 }
1258
1259 static struct qreg
1260 ntq_emit_vpm_read(struct v3d_compile *c,
1261 uint32_t *num_components_queued,
1262 uint32_t *remaining,
1263 uint32_t vpm_index)
1264 {
1265 struct qreg vpm = vir_reg(QFILE_VPM, vpm_index);
1266
1267 if (c->devinfo->ver >= 40 ) {
1268 return vir_LDVPMV_IN(c,
1269 vir_uniform_ui(c,
1270 (*num_components_queued)++));
1271 }
1272
1273 if (*num_components_queued != 0) {
1274 (*num_components_queued)--;
1275 c->num_inputs++;
1276 return vir_MOV(c, vpm);
1277 }
1278
1279 uint32_t num_components = MIN2(*remaining, 32);
1280
1281 v3d33_vir_vpm_read_setup(c, num_components);
1282
1283 *num_components_queued = num_components - 1;
1284 *remaining -= num_components;
1285 c->num_inputs++;
1286
1287 return vir_MOV(c, vpm);
1288 }
1289
1290 static void
1291 ntq_setup_vpm_inputs(struct v3d_compile *c)
1292 {
1293 /* Figure out how many components of each vertex attribute the shader
1294 * uses. Each variable should have been split to individual
1295 * components and unused ones DCEed. The vertex fetcher will load
1296 * from the start of the attribute to the number of components we
1297 * declare we need in c->vattr_sizes[].
1298 */
1299 nir_foreach_variable(var, &c->s->inputs) {
1300 /* No VS attribute array support. */
1301 assert(MAX2(glsl_get_length(var->type), 1) == 1);
1302
1303 unsigned loc = var->data.driver_location;
1304 int start_component = var->data.location_frac;
1305 int num_components = glsl_get_components(var->type);
1306
1307 c->vattr_sizes[loc] = MAX2(c->vattr_sizes[loc],
1308 start_component + num_components);
1309 }
1310
1311 unsigned num_components = 0;
1312 uint32_t vpm_components_queued = 0;
1313 bool uses_iid = c->s->info.system_values_read &
1314 (1ull << SYSTEM_VALUE_INSTANCE_ID);
1315 bool uses_vid = c->s->info.system_values_read &
1316 (1ull << SYSTEM_VALUE_VERTEX_ID);
1317 num_components += uses_iid;
1318 num_components += uses_vid;
1319
1320 for (int i = 0; i < ARRAY_SIZE(c->vattr_sizes); i++)
1321 num_components += c->vattr_sizes[i];
1322
1323 if (uses_iid) {
1324 c->iid = ntq_emit_vpm_read(c, &vpm_components_queued,
1325 &num_components, ~0);
1326 }
1327
1328 if (uses_vid) {
1329 c->vid = ntq_emit_vpm_read(c, &vpm_components_queued,
1330 &num_components, ~0);
1331 }
1332
1333 for (int loc = 0; loc < ARRAY_SIZE(c->vattr_sizes); loc++) {
1334 resize_qreg_array(c, &c->inputs, &c->inputs_array_size,
1335 (loc + 1) * 4);
1336
1337 for (int i = 0; i < c->vattr_sizes[loc]; i++) {
1338 c->inputs[loc * 4 + i] =
1339 ntq_emit_vpm_read(c,
1340 &vpm_components_queued,
1341 &num_components,
1342 loc * 4 + i);
1343
1344 }
1345 }
1346
1347 if (c->devinfo->ver >= 40) {
1348 assert(vpm_components_queued == num_components);
1349 } else {
1350 assert(vpm_components_queued == 0);
1351 assert(num_components == 0);
1352 }
1353 }
1354
1355 static void
1356 ntq_setup_fs_inputs(struct v3d_compile *c)
1357 {
1358 unsigned num_entries = 0;
1359 unsigned num_components = 0;
1360 nir_foreach_variable(var, &c->s->inputs) {
1361 num_entries++;
1362 num_components += glsl_get_components(var->type);
1363 }
1364
1365 nir_variable *vars[num_entries];
1366
1367 unsigned i = 0;
1368 nir_foreach_variable(var, &c->s->inputs)
1369 vars[i++] = var;
1370
1371 /* Sort the variables so that we emit the input setup in
1372 * driver_location order. This is required for VPM reads, whose data
1373 * is fetched into the VPM in driver_location (TGSI register index)
1374 * order.
1375 */
1376 qsort(&vars, num_entries, sizeof(*vars), driver_location_compare);
1377
1378 for (unsigned i = 0; i < num_entries; i++) {
1379 nir_variable *var = vars[i];
1380 unsigned array_len = MAX2(glsl_get_length(var->type), 1);
1381 unsigned loc = var->data.driver_location;
1382
1383 assert(array_len == 1);
1384 (void)array_len;
1385 resize_qreg_array(c, &c->inputs, &c->inputs_array_size,
1386 (loc + 1) * 4);
1387
1388 if (var->data.location == VARYING_SLOT_POS) {
1389 emit_fragcoord_input(c, loc);
1390 } else if (var->data.location == VARYING_SLOT_PNTC ||
1391 (var->data.location >= VARYING_SLOT_VAR0 &&
1392 (c->fs_key->point_sprite_mask &
1393 (1 << (var->data.location -
1394 VARYING_SLOT_VAR0))))) {
1395 c->inputs[loc * 4 + 0] = c->point_x;
1396 c->inputs[loc * 4 + 1] = c->point_y;
1397 } else {
1398 emit_fragment_input(c, loc, var);
1399 }
1400 }
1401 }
1402
1403 static void
1404 ntq_setup_outputs(struct v3d_compile *c)
1405 {
1406 nir_foreach_variable(var, &c->s->outputs) {
1407 unsigned array_len = MAX2(glsl_get_length(var->type), 1);
1408 unsigned loc = var->data.driver_location * 4;
1409
1410 assert(array_len == 1);
1411 (void)array_len;
1412
1413 for (int i = 0; i < 4 - var->data.location_frac; i++) {
1414 add_output(c, loc + var->data.location_frac + i,
1415 var->data.location,
1416 var->data.location_frac + i);
1417 }
1418
1419 if (c->s->info.stage == MESA_SHADER_FRAGMENT) {
1420 switch (var->data.location) {
1421 case FRAG_RESULT_COLOR:
1422 c->output_color_var[0] = var;
1423 c->output_color_var[1] = var;
1424 c->output_color_var[2] = var;
1425 c->output_color_var[3] = var;
1426 break;
1427 case FRAG_RESULT_DATA0:
1428 case FRAG_RESULT_DATA1:
1429 case FRAG_RESULT_DATA2:
1430 case FRAG_RESULT_DATA3:
1431 c->output_color_var[var->data.location -
1432 FRAG_RESULT_DATA0] = var;
1433 break;
1434 case FRAG_RESULT_DEPTH:
1435 c->output_position_index = loc;
1436 break;
1437 case FRAG_RESULT_SAMPLE_MASK:
1438 c->output_sample_mask_index = loc;
1439 break;
1440 }
1441 } else {
1442 switch (var->data.location) {
1443 case VARYING_SLOT_POS:
1444 c->output_position_index = loc;
1445 break;
1446 case VARYING_SLOT_PSIZ:
1447 c->output_point_size_index = loc;
1448 break;
1449 }
1450 }
1451 }
1452 }
1453
1454 static void
1455 ntq_setup_uniforms(struct v3d_compile *c)
1456 {
1457 nir_foreach_variable(var, &c->s->uniforms) {
1458 uint32_t vec4_count = glsl_count_attribute_slots(var->type,
1459 false);
1460 unsigned vec4_size = 4 * sizeof(float);
1461
1462 declare_uniform_range(c, var->data.driver_location * vec4_size,
1463 vec4_count * vec4_size);
1464
1465 }
1466 }
1467
1468 /**
1469 * Sets up the mapping from nir_register to struct qreg *.
1470 *
1471 * Each nir_register gets a struct qreg per 32-bit component being stored.
1472 */
1473 static void
1474 ntq_setup_registers(struct v3d_compile *c, struct exec_list *list)
1475 {
1476 foreach_list_typed(nir_register, nir_reg, node, list) {
1477 unsigned array_len = MAX2(nir_reg->num_array_elems, 1);
1478 struct qreg *qregs = ralloc_array(c->def_ht, struct qreg,
1479 array_len *
1480 nir_reg->num_components);
1481
1482 _mesa_hash_table_insert(c->def_ht, nir_reg, qregs);
1483
1484 for (int i = 0; i < array_len * nir_reg->num_components; i++)
1485 qregs[i] = vir_get_temp(c);
1486 }
1487 }
1488
1489 static void
1490 ntq_emit_load_const(struct v3d_compile *c, nir_load_const_instr *instr)
1491 {
1492 struct qreg *qregs = ntq_init_ssa_def(c, &instr->def);
1493 for (int i = 0; i < instr->def.num_components; i++)
1494 qregs[i] = vir_uniform_ui(c, instr->value.u32[i]);
1495
1496 _mesa_hash_table_insert(c->def_ht, &instr->def, qregs);
1497 }
1498
1499 static void
1500 ntq_emit_ssa_undef(struct v3d_compile *c, nir_ssa_undef_instr *instr)
1501 {
1502 struct qreg *qregs = ntq_init_ssa_def(c, &instr->def);
1503
1504 /* VIR needs there to be *some* value, so pick 0 (same as for
1505 * ntq_setup_registers().
1506 */
1507 for (int i = 0; i < instr->def.num_components; i++)
1508 qregs[i] = vir_uniform_ui(c, 0);
1509 }
1510
1511 static void
1512 ntq_emit_intrinsic(struct v3d_compile *c, nir_intrinsic_instr *instr)
1513 {
1514 unsigned offset;
1515
1516 switch (instr->intrinsic) {
1517 case nir_intrinsic_load_uniform:
1518 assert(instr->num_components == 1);
1519 if (nir_src_is_const(instr->src[0])) {
1520 offset = (nir_intrinsic_base(instr) +
1521 nir_src_as_uint(instr->src[0]));
1522 assert(offset % 4 == 0);
1523 /* We need dwords */
1524 offset = offset / 4;
1525 ntq_store_dest(c, &instr->dest, 0,
1526 vir_uniform(c, QUNIFORM_UNIFORM,
1527 offset));
1528 } else {
1529 ntq_store_dest(c, &instr->dest, 0,
1530 indirect_uniform_load(c, instr));
1531 }
1532 break;
1533
1534 case nir_intrinsic_load_ubo:
1535 for (int i = 0; i < instr->num_components; i++) {
1536 int ubo = nir_src_as_uint(instr->src[0]);
1537
1538 /* Adjust for where we stored the TGSI register base. */
1539 vir_ADD_dest(c,
1540 vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_TMUA),
1541 vir_uniform(c, QUNIFORM_UBO_ADDR, 1 + ubo),
1542 vir_ADD(c,
1543 ntq_get_src(c, instr->src[1], 0),
1544 vir_uniform_ui(c, i * 4)));
1545
1546 vir_emit_thrsw(c);
1547
1548 ntq_store_dest(c, &instr->dest, i, vir_LDTMU(c));
1549 }
1550 break;
1551
1552 if (nir_src_is_const(instr->src[0])) {
1553 offset = (nir_intrinsic_base(instr) +
1554 nir_src_as_uint(instr->src[0]));
1555 assert(offset % 4 == 0);
1556 /* We need dwords */
1557 offset = offset / 4;
1558 ntq_store_dest(c, &instr->dest, 0,
1559 vir_uniform(c, QUNIFORM_UNIFORM,
1560 offset));
1561 } else {
1562 ntq_store_dest(c, &instr->dest, 0,
1563 indirect_uniform_load(c, instr));
1564 }
1565 break;
1566
1567 case nir_intrinsic_load_user_clip_plane:
1568 for (int i = 0; i < instr->num_components; i++) {
1569 ntq_store_dest(c, &instr->dest, i,
1570 vir_uniform(c, QUNIFORM_USER_CLIP_PLANE,
1571 nir_intrinsic_ucp_id(instr) *
1572 4 + i));
1573 }
1574 break;
1575
1576 case nir_intrinsic_load_alpha_ref_float:
1577 ntq_store_dest(c, &instr->dest, 0,
1578 vir_uniform(c, QUNIFORM_ALPHA_REF, 0));
1579 break;
1580
1581 case nir_intrinsic_load_sample_mask_in:
1582 ntq_store_dest(c, &instr->dest, 0,
1583 vir_uniform(c, QUNIFORM_SAMPLE_MASK, 0));
1584 break;
1585
1586 case nir_intrinsic_load_front_face:
1587 /* The register contains 0 (front) or 1 (back), and we need to
1588 * turn it into a NIR bool where true means front.
1589 */
1590 ntq_store_dest(c, &instr->dest, 0,
1591 vir_ADD(c,
1592 vir_uniform_ui(c, -1),
1593 vir_REVF(c)));
1594 break;
1595
1596 case nir_intrinsic_load_instance_id:
1597 ntq_store_dest(c, &instr->dest, 0, vir_MOV(c, c->iid));
1598 break;
1599
1600 case nir_intrinsic_load_vertex_id:
1601 ntq_store_dest(c, &instr->dest, 0, vir_MOV(c, c->vid));
1602 break;
1603
1604 case nir_intrinsic_load_input:
1605 for (int i = 0; i < instr->num_components; i++) {
1606 offset = (nir_intrinsic_base(instr) +
1607 nir_src_as_uint(instr->src[0]));
1608 int comp = nir_intrinsic_component(instr) + i;
1609 ntq_store_dest(c, &instr->dest, i,
1610 vir_MOV(c, c->inputs[offset * 4 + comp]));
1611 }
1612 break;
1613
1614 case nir_intrinsic_store_output:
1615 offset = ((nir_intrinsic_base(instr) +
1616 nir_src_as_uint(instr->src[1])) * 4 +
1617 nir_intrinsic_component(instr));
1618
1619 for (int i = 0; i < instr->num_components; i++) {
1620 c->outputs[offset + i] =
1621 vir_MOV(c, ntq_get_src(c, instr->src[0], i));
1622 }
1623 c->num_outputs = MAX2(c->num_outputs,
1624 offset + instr->num_components);
1625 break;
1626
1627 case nir_intrinsic_discard:
1628 if (c->execute.file != QFILE_NULL) {
1629 vir_PF(c, c->execute, V3D_QPU_PF_PUSHZ);
1630 vir_set_cond(vir_SETMSF_dest(c, vir_reg(QFILE_NULL, 0),
1631 vir_uniform_ui(c, 0)),
1632 V3D_QPU_COND_IFA);
1633 } else {
1634 vir_SETMSF_dest(c, vir_reg(QFILE_NULL, 0),
1635 vir_uniform_ui(c, 0));
1636 }
1637 break;
1638
1639 case nir_intrinsic_discard_if: {
1640 /* true (~0) if we're discarding */
1641 struct qreg cond = ntq_get_src(c, instr->src[0], 0);
1642
1643 if (c->execute.file != QFILE_NULL) {
1644 /* execute == 0 means the channel is active. Invert
1645 * the condition so that we can use zero as "executing
1646 * and discarding."
1647 */
1648 vir_PF(c, vir_OR(c, c->execute, vir_NOT(c, cond)),
1649 V3D_QPU_PF_PUSHZ);
1650 vir_set_cond(vir_SETMSF_dest(c, vir_reg(QFILE_NULL, 0),
1651 vir_uniform_ui(c, 0)),
1652 V3D_QPU_COND_IFA);
1653 } else {
1654 vir_PF(c, cond, V3D_QPU_PF_PUSHZ);
1655 vir_set_cond(vir_SETMSF_dest(c, vir_reg(QFILE_NULL, 0),
1656 vir_uniform_ui(c, 0)),
1657 V3D_QPU_COND_IFNA);
1658 }
1659
1660 break;
1661 }
1662
1663 default:
1664 fprintf(stderr, "Unknown intrinsic: ");
1665 nir_print_instr(&instr->instr, stderr);
1666 fprintf(stderr, "\n");
1667 break;
1668 }
1669 }
1670
1671 /* Clears (activates) the execute flags for any channels whose jump target
1672 * matches this block.
1673 */
1674 static void
1675 ntq_activate_execute_for_block(struct v3d_compile *c)
1676 {
1677 vir_PF(c, vir_XOR(c, c->execute, vir_uniform_ui(c, c->cur_block->index)),
1678 V3D_QPU_PF_PUSHZ);
1679
1680 vir_MOV_cond(c, V3D_QPU_COND_IFA, c->execute, vir_uniform_ui(c, 0));
1681 }
1682
1683 static void
1684 ntq_emit_if(struct v3d_compile *c, nir_if *if_stmt)
1685 {
1686 nir_block *nir_else_block = nir_if_first_else_block(if_stmt);
1687 bool empty_else_block =
1688 (nir_else_block == nir_if_last_else_block(if_stmt) &&
1689 exec_list_is_empty(&nir_else_block->instr_list));
1690
1691 struct qblock *then_block = vir_new_block(c);
1692 struct qblock *after_block = vir_new_block(c);
1693 struct qblock *else_block;
1694 if (empty_else_block)
1695 else_block = after_block;
1696 else
1697 else_block = vir_new_block(c);
1698
1699 bool was_top_level = false;
1700 if (c->execute.file == QFILE_NULL) {
1701 c->execute = vir_MOV(c, vir_uniform_ui(c, 0));
1702 was_top_level = true;
1703 }
1704
1705 /* Set A for executing (execute == 0) and jumping (if->condition ==
1706 * 0) channels, and then update execute flags for those to point to
1707 * the ELSE block.
1708 */
1709 vir_PF(c, vir_OR(c,
1710 c->execute,
1711 ntq_get_src(c, if_stmt->condition, 0)),
1712 V3D_QPU_PF_PUSHZ);
1713 vir_MOV_cond(c, V3D_QPU_COND_IFA,
1714 c->execute,
1715 vir_uniform_ui(c, else_block->index));
1716
1717 /* Jump to ELSE if nothing is active for THEN, otherwise fall
1718 * through.
1719 */
1720 vir_PF(c, c->execute, V3D_QPU_PF_PUSHZ);
1721 vir_BRANCH(c, V3D_QPU_BRANCH_COND_ALLNA);
1722 vir_link_blocks(c->cur_block, else_block);
1723 vir_link_blocks(c->cur_block, then_block);
1724
1725 /* Process the THEN block. */
1726 vir_set_emit_block(c, then_block);
1727 ntq_emit_cf_list(c, &if_stmt->then_list);
1728
1729 if (!empty_else_block) {
1730 /* Handle the end of the THEN block. First, all currently
1731 * active channels update their execute flags to point to
1732 * ENDIF
1733 */
1734 vir_PF(c, c->execute, V3D_QPU_PF_PUSHZ);
1735 vir_MOV_cond(c, V3D_QPU_COND_IFA, c->execute,
1736 vir_uniform_ui(c, after_block->index));
1737
1738 /* If everything points at ENDIF, then jump there immediately. */
1739 vir_PF(c, vir_XOR(c, c->execute,
1740 vir_uniform_ui(c, after_block->index)),
1741 V3D_QPU_PF_PUSHZ);
1742 vir_BRANCH(c, V3D_QPU_BRANCH_COND_ALLA);
1743 vir_link_blocks(c->cur_block, after_block);
1744 vir_link_blocks(c->cur_block, else_block);
1745
1746 vir_set_emit_block(c, else_block);
1747 ntq_activate_execute_for_block(c);
1748 ntq_emit_cf_list(c, &if_stmt->else_list);
1749 }
1750
1751 vir_link_blocks(c->cur_block, after_block);
1752
1753 vir_set_emit_block(c, after_block);
1754 if (was_top_level)
1755 c->execute = c->undef;
1756 else
1757 ntq_activate_execute_for_block(c);
1758 }
1759
1760 static void
1761 ntq_emit_jump(struct v3d_compile *c, nir_jump_instr *jump)
1762 {
1763 switch (jump->type) {
1764 case nir_jump_break:
1765 vir_PF(c, c->execute, V3D_QPU_PF_PUSHZ);
1766 vir_MOV_cond(c, V3D_QPU_COND_IFA, c->execute,
1767 vir_uniform_ui(c, c->loop_break_block->index));
1768 break;
1769
1770 case nir_jump_continue:
1771 vir_PF(c, c->execute, V3D_QPU_PF_PUSHZ);
1772 vir_MOV_cond(c, V3D_QPU_COND_IFA, c->execute,
1773 vir_uniform_ui(c, c->loop_cont_block->index));
1774 break;
1775
1776 case nir_jump_return:
1777 unreachable("All returns shouold be lowered\n");
1778 }
1779 }
1780
1781 static void
1782 ntq_emit_instr(struct v3d_compile *c, nir_instr *instr)
1783 {
1784 switch (instr->type) {
1785 case nir_instr_type_alu:
1786 ntq_emit_alu(c, nir_instr_as_alu(instr));
1787 break;
1788
1789 case nir_instr_type_intrinsic:
1790 ntq_emit_intrinsic(c, nir_instr_as_intrinsic(instr));
1791 break;
1792
1793 case nir_instr_type_load_const:
1794 ntq_emit_load_const(c, nir_instr_as_load_const(instr));
1795 break;
1796
1797 case nir_instr_type_ssa_undef:
1798 ntq_emit_ssa_undef(c, nir_instr_as_ssa_undef(instr));
1799 break;
1800
1801 case nir_instr_type_tex:
1802 ntq_emit_tex(c, nir_instr_as_tex(instr));
1803 break;
1804
1805 case nir_instr_type_jump:
1806 ntq_emit_jump(c, nir_instr_as_jump(instr));
1807 break;
1808
1809 default:
1810 fprintf(stderr, "Unknown NIR instr type: ");
1811 nir_print_instr(instr, stderr);
1812 fprintf(stderr, "\n");
1813 abort();
1814 }
1815 }
1816
1817 static void
1818 ntq_emit_block(struct v3d_compile *c, nir_block *block)
1819 {
1820 nir_foreach_instr(instr, block) {
1821 ntq_emit_instr(c, instr);
1822 }
1823 }
1824
1825 static void ntq_emit_cf_list(struct v3d_compile *c, struct exec_list *list);
1826
1827 static void
1828 ntq_emit_loop(struct v3d_compile *c, nir_loop *loop)
1829 {
1830 bool was_top_level = false;
1831 if (c->execute.file == QFILE_NULL) {
1832 c->execute = vir_MOV(c, vir_uniform_ui(c, 0));
1833 was_top_level = true;
1834 }
1835
1836 struct qblock *save_loop_cont_block = c->loop_cont_block;
1837 struct qblock *save_loop_break_block = c->loop_break_block;
1838
1839 c->loop_cont_block = vir_new_block(c);
1840 c->loop_break_block = vir_new_block(c);
1841
1842 vir_link_blocks(c->cur_block, c->loop_cont_block);
1843 vir_set_emit_block(c, c->loop_cont_block);
1844 ntq_activate_execute_for_block(c);
1845
1846 ntq_emit_cf_list(c, &loop->body);
1847
1848 /* Re-enable any previous continues now, so our ANYA check below
1849 * works.
1850 *
1851 * XXX: Use the .ORZ flags update, instead.
1852 */
1853 vir_PF(c, vir_XOR(c,
1854 c->execute,
1855 vir_uniform_ui(c, c->loop_cont_block->index)),
1856 V3D_QPU_PF_PUSHZ);
1857 vir_MOV_cond(c, V3D_QPU_COND_IFA, c->execute, vir_uniform_ui(c, 0));
1858
1859 vir_PF(c, c->execute, V3D_QPU_PF_PUSHZ);
1860
1861 struct qinst *branch = vir_BRANCH(c, V3D_QPU_BRANCH_COND_ANYA);
1862 /* Pixels that were not dispatched or have been discarded should not
1863 * contribute to looping again.
1864 */
1865 branch->qpu.branch.msfign = V3D_QPU_MSFIGN_P;
1866 vir_link_blocks(c->cur_block, c->loop_cont_block);
1867 vir_link_blocks(c->cur_block, c->loop_break_block);
1868
1869 vir_set_emit_block(c, c->loop_break_block);
1870 if (was_top_level)
1871 c->execute = c->undef;
1872 else
1873 ntq_activate_execute_for_block(c);
1874
1875 c->loop_break_block = save_loop_break_block;
1876 c->loop_cont_block = save_loop_cont_block;
1877 }
1878
1879 static void
1880 ntq_emit_function(struct v3d_compile *c, nir_function_impl *func)
1881 {
1882 fprintf(stderr, "FUNCTIONS not handled.\n");
1883 abort();
1884 }
1885
1886 static void
1887 ntq_emit_cf_list(struct v3d_compile *c, struct exec_list *list)
1888 {
1889 foreach_list_typed(nir_cf_node, node, node, list) {
1890 switch (node->type) {
1891 case nir_cf_node_block:
1892 ntq_emit_block(c, nir_cf_node_as_block(node));
1893 break;
1894
1895 case nir_cf_node_if:
1896 ntq_emit_if(c, nir_cf_node_as_if(node));
1897 break;
1898
1899 case nir_cf_node_loop:
1900 ntq_emit_loop(c, nir_cf_node_as_loop(node));
1901 break;
1902
1903 case nir_cf_node_function:
1904 ntq_emit_function(c, nir_cf_node_as_function(node));
1905 break;
1906
1907 default:
1908 fprintf(stderr, "Unknown NIR node type\n");
1909 abort();
1910 }
1911 }
1912 }
1913
1914 static void
1915 ntq_emit_impl(struct v3d_compile *c, nir_function_impl *impl)
1916 {
1917 ntq_setup_registers(c, &impl->registers);
1918 ntq_emit_cf_list(c, &impl->body);
1919 }
1920
1921 static void
1922 nir_to_vir(struct v3d_compile *c)
1923 {
1924 if (c->s->info.stage == MESA_SHADER_FRAGMENT) {
1925 c->payload_w = vir_MOV(c, vir_reg(QFILE_REG, 0));
1926 c->payload_w_centroid = vir_MOV(c, vir_reg(QFILE_REG, 1));
1927 c->payload_z = vir_MOV(c, vir_reg(QFILE_REG, 2));
1928
1929 if (c->fs_key->is_points) {
1930 c->point_x = emit_fragment_varying(c, NULL, 0);
1931 c->point_y = emit_fragment_varying(c, NULL, 0);
1932 } else if (c->fs_key->is_lines) {
1933 c->line_x = emit_fragment_varying(c, NULL, 0);
1934 }
1935 }
1936
1937 if (c->s->info.stage == MESA_SHADER_FRAGMENT)
1938 ntq_setup_fs_inputs(c);
1939 else
1940 ntq_setup_vpm_inputs(c);
1941
1942 ntq_setup_outputs(c);
1943 ntq_setup_uniforms(c);
1944 ntq_setup_registers(c, &c->s->registers);
1945
1946 /* Find the main function and emit the body. */
1947 nir_foreach_function(function, c->s) {
1948 assert(strcmp(function->name, "main") == 0);
1949 assert(function->impl);
1950 ntq_emit_impl(c, function->impl);
1951 }
1952 }
1953
1954 const nir_shader_compiler_options v3d_nir_options = {
1955 .lower_all_io_to_temps = true,
1956 .lower_extract_byte = true,
1957 .lower_extract_word = true,
1958 .lower_bfm = true,
1959 .lower_bitfield_insert_to_shifts = true,
1960 .lower_bitfield_extract_to_shifts = true,
1961 .lower_bitfield_reverse = true,
1962 .lower_bit_count = true,
1963 .lower_pack_unorm_2x16 = true,
1964 .lower_pack_snorm_2x16 = true,
1965 .lower_pack_unorm_4x8 = true,
1966 .lower_pack_snorm_4x8 = true,
1967 .lower_unpack_unorm_4x8 = true,
1968 .lower_unpack_snorm_4x8 = true,
1969 .lower_pack_half_2x16 = true,
1970 .lower_unpack_half_2x16 = true,
1971 .lower_fdiv = true,
1972 .lower_find_lsb = true,
1973 .lower_ffma = true,
1974 .lower_flrp32 = true,
1975 .lower_fpow = true,
1976 .lower_fsat = true,
1977 .lower_fsqrt = true,
1978 .lower_ifind_msb = true,
1979 .lower_ldexp = true,
1980 .lower_mul_high = true,
1981 .lower_wpos_pntc = true,
1982 .native_integers = true,
1983 };
1984
1985
1986 #if 0
1987 static int
1988 count_nir_instrs(nir_shader *nir)
1989 {
1990 int count = 0;
1991 nir_foreach_function(function, nir) {
1992 if (!function->impl)
1993 continue;
1994 nir_foreach_block(block, function->impl) {
1995 nir_foreach_instr(instr, block)
1996 count++;
1997 }
1998 }
1999 return count;
2000 }
2001 #endif
2002
2003 /**
2004 * When demoting a shader down to single-threaded, removes the THRSW
2005 * instructions (one will still be inserted at v3d_vir_to_qpu() for the
2006 * program end).
2007 */
2008 static void
2009 vir_remove_thrsw(struct v3d_compile *c)
2010 {
2011 vir_for_each_block(block, c) {
2012 vir_for_each_inst_safe(inst, block) {
2013 if (inst->qpu.sig.thrsw)
2014 vir_remove_instruction(c, inst);
2015 }
2016 }
2017
2018 c->last_thrsw = NULL;
2019 }
2020
2021 void
2022 vir_emit_last_thrsw(struct v3d_compile *c)
2023 {
2024 /* On V3D before 4.1, we need a TMU op to be outstanding when thread
2025 * switching, so disable threads if we didn't do any TMU ops (each of
2026 * which would have emitted a THRSW).
2027 */
2028 if (!c->last_thrsw_at_top_level && c->devinfo->ver < 41) {
2029 c->threads = 1;
2030 if (c->last_thrsw)
2031 vir_remove_thrsw(c);
2032 return;
2033 }
2034
2035 /* If we're threaded and the last THRSW was in conditional code, then
2036 * we need to emit another one so that we can flag it as the last
2037 * thrsw.
2038 */
2039 if (c->last_thrsw && !c->last_thrsw_at_top_level) {
2040 assert(c->devinfo->ver >= 41);
2041 vir_emit_thrsw(c);
2042 }
2043
2044 /* If we're threaded, then we need to mark the last THRSW instruction
2045 * so we can emit a pair of them at QPU emit time.
2046 *
2047 * For V3D 4.x, we can spawn the non-fragment shaders already in the
2048 * post-last-THRSW state, so we can skip this.
2049 */
2050 if (!c->last_thrsw && c->s->info.stage == MESA_SHADER_FRAGMENT) {
2051 assert(c->devinfo->ver >= 41);
2052 vir_emit_thrsw(c);
2053 }
2054
2055 if (c->last_thrsw)
2056 c->last_thrsw->is_last_thrsw = true;
2057 }
2058
2059 /* There's a flag in the shader for "center W is needed for reasons other than
2060 * non-centroid varyings", so we just walk the program after VIR optimization
2061 * to see if it's used. It should be harmless to set even if we only use
2062 * center W for varyings.
2063 */
2064 static void
2065 vir_check_payload_w(struct v3d_compile *c)
2066 {
2067 if (c->s->info.stage != MESA_SHADER_FRAGMENT)
2068 return;
2069
2070 vir_for_each_inst_inorder(inst, c) {
2071 for (int i = 0; i < vir_get_nsrc(inst); i++) {
2072 if (inst->src[i].file == QFILE_REG &&
2073 inst->src[i].index == 0) {
2074 c->uses_center_w = true;
2075 return;
2076 }
2077 }
2078 }
2079
2080 }
2081
2082 void
2083 v3d_nir_to_vir(struct v3d_compile *c)
2084 {
2085 if (V3D_DEBUG & (V3D_DEBUG_NIR |
2086 v3d_debug_flag_for_shader_stage(c->s->info.stage))) {
2087 fprintf(stderr, "%s prog %d/%d NIR:\n",
2088 vir_get_stage_name(c),
2089 c->program_id, c->variant_id);
2090 nir_print_shader(c->s, stderr);
2091 }
2092
2093 nir_to_vir(c);
2094
2095 /* Emit the last THRSW before STVPM and TLB writes. */
2096 vir_emit_last_thrsw(c);
2097
2098 switch (c->s->info.stage) {
2099 case MESA_SHADER_FRAGMENT:
2100 emit_frag_end(c);
2101 break;
2102 case MESA_SHADER_VERTEX:
2103 emit_vert_end(c);
2104 break;
2105 default:
2106 unreachable("bad stage");
2107 }
2108
2109 if (V3D_DEBUG & (V3D_DEBUG_VIR |
2110 v3d_debug_flag_for_shader_stage(c->s->info.stage))) {
2111 fprintf(stderr, "%s prog %d/%d pre-opt VIR:\n",
2112 vir_get_stage_name(c),
2113 c->program_id, c->variant_id);
2114 vir_dump(c);
2115 fprintf(stderr, "\n");
2116 }
2117
2118 vir_optimize(c);
2119 vir_lower_uniforms(c);
2120
2121 vir_check_payload_w(c);
2122
2123 /* XXX: vir_schedule_instructions(c); */
2124
2125 if (V3D_DEBUG & (V3D_DEBUG_VIR |
2126 v3d_debug_flag_for_shader_stage(c->s->info.stage))) {
2127 fprintf(stderr, "%s prog %d/%d VIR:\n",
2128 vir_get_stage_name(c),
2129 c->program_id, c->variant_id);
2130 vir_dump(c);
2131 fprintf(stderr, "\n");
2132 }
2133
2134 /* Attempt to allocate registers for the temporaries. If we fail,
2135 * reduce thread count and try again.
2136 */
2137 int min_threads = (c->devinfo->ver >= 41) ? 2 : 1;
2138 struct qpu_reg *temp_registers;
2139 while (true) {
2140 bool spilled;
2141 temp_registers = v3d_register_allocate(c, &spilled);
2142 if (spilled)
2143 continue;
2144
2145 if (temp_registers)
2146 break;
2147
2148 if (c->threads == min_threads) {
2149 fprintf(stderr, "Failed to register allocate at %d threads:\n",
2150 c->threads);
2151 vir_dump(c);
2152 c->failed = true;
2153 return;
2154 }
2155
2156 c->threads /= 2;
2157
2158 if (c->threads == 1)
2159 vir_remove_thrsw(c);
2160 }
2161
2162 v3d_vir_to_qpu(c, temp_registers);
2163 }