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