nir/lower_idiv: add new llvm-based path
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_compiler_nir.c
1 /*
2 * Copyright (c) 2012-2019 Etnaviv Project
3 * Copyright (c) 2019 Zodiac Inflight Innovations
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Jonathan Marek <jonathan@marek.ca>
26 * Wladimir J. van der Laan <laanwj@gmail.com>
27 */
28
29 #include "etnaviv_compiler.h"
30 #include "etnaviv_asm.h"
31 #include "etnaviv_context.h"
32 #include "etnaviv_debug.h"
33 #include "etnaviv_disasm.h"
34 #include "etnaviv_uniforms.h"
35 #include "etnaviv_util.h"
36
37 #include <math.h>
38 #include "util/u_memory.h"
39 #include "util/register_allocate.h"
40 #include "compiler/nir/nir_builder.h"
41 #include "compiler/nir/nir_worklist.h"
42
43 #include "tgsi/tgsi_strings.h"
44 #include "util/u_half.h"
45
46 struct etna_compile {
47 nir_shader *nir;
48 #define is_fs(c) ((c)->nir->info.stage == MESA_SHADER_FRAGMENT)
49 const struct etna_specs *specs;
50 struct etna_shader_variant *variant;
51
52 /* block # to instr index */
53 unsigned *block_ptr;
54
55 /* Code generation */
56 int inst_ptr; /* current instruction pointer */
57 struct etna_inst code[ETNA_MAX_INSTRUCTIONS * ETNA_INST_SIZE];
58
59 /* constants */
60 uint64_t consts[ETNA_MAX_IMM];
61
62 /* There was an error during compilation */
63 bool error;
64 };
65
66 /* io related lowering
67 * run after lower_int_to_float because it adds i2f/f2i ops
68 */
69 static void
70 etna_lower_io(nir_shader *shader, struct etna_shader_variant *v)
71 {
72 nir_foreach_function(function, shader) {
73 nir_builder b;
74 nir_builder_init(&b, function->impl);
75
76 nir_foreach_block(block, function->impl) {
77 nir_foreach_instr_safe(instr, block) {
78 if (instr->type == nir_instr_type_intrinsic) {
79 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
80
81 switch (intr->intrinsic) {
82 case nir_intrinsic_load_front_face: {
83 /* HW front_face is 0.0/1.0, not 0/~0u for bool
84 * lower with a comparison with 0
85 */
86 intr->dest.ssa.bit_size = 32;
87
88 b.cursor = nir_after_instr(instr);
89
90 nir_ssa_def *ssa = nir_ine(&b, &intr->dest.ssa, nir_imm_int(&b, 0));
91 if (v->key.front_ccw)
92 nir_instr_as_alu(ssa->parent_instr)->op = nir_op_ieq;
93
94 nir_ssa_def_rewrite_uses_after(&intr->dest.ssa,
95 nir_src_for_ssa(ssa),
96 ssa->parent_instr);
97 } break;
98 case nir_intrinsic_store_deref: {
99 if (shader->info.stage != MESA_SHADER_FRAGMENT || !v->key.frag_rb_swap)
100 break;
101
102 nir_deref_instr *deref = nir_src_as_deref(intr->src[0]);
103 assert(deref->deref_type == nir_deref_type_var);
104
105 if (deref->var->data.location != FRAG_RESULT_COLOR &&
106 deref->var->data.location != FRAG_RESULT_DATA0)
107 break;
108
109 b.cursor = nir_before_instr(instr);
110
111 nir_ssa_def *ssa = nir_mov(&b, intr->src[1].ssa);
112 nir_alu_instr *alu = nir_instr_as_alu(ssa->parent_instr);
113 alu->src[0].swizzle[0] = 2;
114 alu->src[0].swizzle[2] = 0;
115 nir_instr_rewrite_src(instr, &intr->src[1], nir_src_for_ssa(ssa));
116 } break;
117 case nir_intrinsic_load_uniform: {
118 /* multiply by 16 and convert to int */
119 b.cursor = nir_before_instr(instr);
120 nir_ssa_def *ssa = nir_imul(&b, intr->src[0].ssa, nir_imm_int(&b, 16));
121 nir_instr_rewrite_src(instr, &intr->src[0], nir_src_for_ssa(ssa));
122 } break;
123 default:
124 break;
125 }
126 }
127
128 if (instr->type != nir_instr_type_tex)
129 continue;
130
131 nir_tex_instr *tex = nir_instr_as_tex(instr);
132 nir_src *coord = NULL;
133 nir_src *lod_bias = NULL;
134 unsigned lod_bias_idx;
135
136 assert(tex->sampler_index == tex->texture_index);
137
138 for (unsigned i = 0; i < tex->num_srcs; i++) {
139 switch (tex->src[i].src_type) {
140 case nir_tex_src_coord:
141 coord = &tex->src[i].src;
142 break;
143 case nir_tex_src_bias:
144 case nir_tex_src_lod:
145 assert(!lod_bias);
146 lod_bias = &tex->src[i].src;
147 lod_bias_idx = i;
148 break;
149 case nir_tex_src_comparator:
150 break;
151 default:
152 assert(0);
153 break;
154 }
155 }
156
157 if (tex->sampler_dim == GLSL_SAMPLER_DIM_RECT) {
158 /* use a dummy load_uniform here to represent texcoord scale */
159 b.cursor = nir_before_instr(instr);
160 nir_intrinsic_instr *load =
161 nir_intrinsic_instr_create(b.shader, nir_intrinsic_load_uniform);
162 nir_intrinsic_set_base(load, ~tex->sampler_index);
163 load->num_components = 2;
164 load->src[0] = nir_src_for_ssa(nir_imm_float(&b, 0.0f));
165 nir_ssa_dest_init(&load->instr, &load->dest, 2, 32, NULL);
166 nir_intrinsic_set_type(load, nir_type_float);
167
168 nir_builder_instr_insert(&b, &load->instr);
169
170 nir_ssa_def *new_coord = nir_fmul(&b, coord->ssa, &load->dest.ssa);
171 nir_instr_rewrite_src(&tex->instr, coord, nir_src_for_ssa(new_coord));
172 }
173
174 /* pre HALTI5 needs texture sources in a single source */
175
176 if (!lod_bias || v->shader->specs->halti >= 5)
177 continue;
178
179 assert(coord && lod_bias && tex->coord_components < 4);
180
181 nir_alu_instr *vec = nir_alu_instr_create(shader, nir_op_vec4);
182 for (unsigned i = 0; i < tex->coord_components; i++) {
183 vec->src[i].src = nir_src_for_ssa(coord->ssa);
184 vec->src[i].swizzle[0] = i;
185 }
186 for (unsigned i = tex->coord_components; i < 4; i++)
187 vec->src[i].src = nir_src_for_ssa(lod_bias->ssa);
188
189 vec->dest.write_mask = 0xf;
190 nir_ssa_dest_init(&vec->instr, &vec->dest.dest, 4, 32, NULL);
191
192 nir_tex_instr_remove_src(tex, lod_bias_idx);
193 nir_instr_rewrite_src(&tex->instr, coord, nir_src_for_ssa(&vec->dest.dest.ssa));
194 tex->coord_components = 4;
195
196 nir_instr_insert_before(&tex->instr, &vec->instr);
197 }
198 }
199 }
200 }
201
202 static bool
203 etna_alu_to_scalar_filter_cb(const nir_instr *instr, const void *data)
204 {
205 const struct etna_specs *specs = data;
206
207 if (instr->type != nir_instr_type_alu)
208 return false;
209
210 nir_alu_instr *alu = nir_instr_as_alu(instr);
211 switch (alu->op) {
212 case nir_op_frsq:
213 case nir_op_frcp:
214 case nir_op_flog2:
215 case nir_op_fexp2:
216 case nir_op_fsqrt:
217 case nir_op_fcos:
218 case nir_op_fsin:
219 case nir_op_fdiv:
220 case nir_op_imul:
221 return true;
222 /* TODO: can do better than alu_to_scalar for vector compares */
223 case nir_op_b32all_fequal2:
224 case nir_op_b32all_fequal3:
225 case nir_op_b32all_fequal4:
226 case nir_op_b32any_fnequal2:
227 case nir_op_b32any_fnequal3:
228 case nir_op_b32any_fnequal4:
229 case nir_op_b32all_iequal2:
230 case nir_op_b32all_iequal3:
231 case nir_op_b32all_iequal4:
232 case nir_op_b32any_inequal2:
233 case nir_op_b32any_inequal3:
234 case nir_op_b32any_inequal4:
235 return true;
236 case nir_op_fdot2:
237 if (!specs->has_halti2_instructions)
238 return true;
239 break;
240 default:
241 break;
242 }
243
244 return false;
245 }
246
247 static void
248 etna_lower_alu_impl(nir_function_impl *impl, struct etna_compile *c)
249 {
250 nir_shader *shader = impl->function->shader;
251
252 nir_builder b;
253 nir_builder_init(&b, impl);
254
255 /* in a seperate loop so we can apply the multiple-uniform logic to the new fmul */
256 nir_foreach_block(block, impl) {
257 nir_foreach_instr_safe(instr, block) {
258 if (instr->type != nir_instr_type_alu)
259 continue;
260
261 nir_alu_instr *alu = nir_instr_as_alu(instr);
262 /* multiply sin/cos src by constant
263 * TODO: do this earlier (but it breaks const_prop opt)
264 */
265 if (alu->op == nir_op_fsin || alu->op == nir_op_fcos) {
266 b.cursor = nir_before_instr(instr);
267
268 nir_ssa_def *imm = c->specs->has_new_transcendentals ?
269 nir_imm_float(&b, 1.0 / M_PI) :
270 nir_imm_float(&b, 2.0 / M_PI);
271
272 nir_instr_rewrite_src(instr, &alu->src[0].src,
273 nir_src_for_ssa(nir_fmul(&b, alu->src[0].src.ssa, imm)));
274 }
275
276 /* change transcendental ops to vec2 and insert vec1 mul for the result
277 * TODO: do this earlier (but it breaks with optimizations)
278 */
279 if (c->specs->has_new_transcendentals && (
280 alu->op == nir_op_fdiv || alu->op == nir_op_flog2 ||
281 alu->op == nir_op_fsin || alu->op == nir_op_fcos)) {
282 nir_ssa_def *ssa = &alu->dest.dest.ssa;
283
284 assert(ssa->num_components == 1);
285
286 nir_alu_instr *mul = nir_alu_instr_create(shader, nir_op_fmul);
287 mul->src[0].src = mul->src[1].src = nir_src_for_ssa(ssa);
288 mul->src[1].swizzle[0] = 1;
289
290 mul->dest.write_mask = 1;
291 nir_ssa_dest_init(&mul->instr, &mul->dest.dest, 1, 32, NULL);
292
293 ssa->num_components = 2;
294
295 mul->dest.saturate = alu->dest.saturate;
296 alu->dest.saturate = 0;
297
298 nir_instr_insert_after(instr, &mul->instr);
299
300 nir_ssa_def_rewrite_uses_after(ssa, nir_src_for_ssa(&mul->dest.dest.ssa), &mul->instr);
301 }
302 }
303 }
304 }
305
306 static void etna_lower_alu(nir_shader *shader, struct etna_compile *c)
307 {
308 nir_foreach_function(function, shader) {
309 if (function->impl)
310 etna_lower_alu_impl(function->impl, c);
311 }
312 }
313
314 static void
315 emit_inst(struct etna_compile *c, struct etna_inst *inst)
316 {
317 c->code[c->inst_ptr++] = *inst;
318 }
319
320 /* to map nir srcs should to etna_inst srcs */
321 enum {
322 SRC_0_1_2 = (0 << 0) | (1 << 2) | (2 << 4),
323 SRC_0_1_X = (0 << 0) | (1 << 2) | (3 << 4),
324 SRC_0_X_X = (0 << 0) | (3 << 2) | (3 << 4),
325 SRC_0_X_1 = (0 << 0) | (3 << 2) | (1 << 4),
326 SRC_0_1_0 = (0 << 0) | (1 << 2) | (0 << 4),
327 SRC_X_X_0 = (3 << 0) | (3 << 2) | (0 << 4),
328 SRC_0_X_0 = (0 << 0) | (3 << 2) | (0 << 4),
329 };
330
331 /* info to translate a nir op to etna_inst */
332 struct etna_op_info {
333 uint8_t opcode; /* INST_OPCODE_ */
334 uint8_t src; /* SRC_ enum */
335 uint8_t cond; /* INST_CONDITION_ */
336 uint8_t type; /* INST_TYPE_ */
337 };
338
339 static const struct etna_op_info etna_ops[] = {
340 [0 ... nir_num_opcodes - 1] = {0xff},
341 #undef TRUE
342 #undef FALSE
343 #define OPCT(nir, op, src, cond, type) [nir_op_##nir] = { \
344 INST_OPCODE_##op, \
345 SRC_##src, \
346 INST_CONDITION_##cond, \
347 INST_TYPE_##type \
348 }
349 #define OPC(nir, op, src, cond) OPCT(nir, op, src, cond, F32)
350 #define IOPC(nir, op, src, cond) OPCT(nir, op, src, cond, S32)
351 #define UOPC(nir, op, src, cond) OPCT(nir, op, src, cond, U32)
352 #define OP(nir, op, src) OPC(nir, op, src, TRUE)
353 #define IOP(nir, op, src) IOPC(nir, op, src, TRUE)
354 #define UOP(nir, op, src) UOPC(nir, op, src, TRUE)
355 OP(mov, MOV, X_X_0), OP(fneg, MOV, X_X_0), OP(fabs, MOV, X_X_0), OP(fsat, MOV, X_X_0),
356 OP(fmul, MUL, 0_1_X), OP(fadd, ADD, 0_X_1), OP(ffma, MAD, 0_1_2),
357 OP(fdot2, DP2, 0_1_X), OP(fdot3, DP3, 0_1_X), OP(fdot4, DP4, 0_1_X),
358 OPC(fmin, SELECT, 0_1_0, GT), OPC(fmax, SELECT, 0_1_0, LT),
359 OP(ffract, FRC, X_X_0), OP(frcp, RCP, X_X_0), OP(frsq, RSQ, X_X_0),
360 OP(fsqrt, SQRT, X_X_0), OP(fsin, SIN, X_X_0), OP(fcos, COS, X_X_0),
361 OP(fsign, SIGN, X_X_0), OP(ffloor, FLOOR, X_X_0), OP(fceil, CEIL, X_X_0),
362 OP(flog2, LOG, X_X_0), OP(fexp2, EXP, X_X_0),
363 OPC(seq, SET, 0_1_X, EQ), OPC(sne, SET, 0_1_X, NE), OPC(sge, SET, 0_1_X, GE), OPC(slt, SET, 0_1_X, LT),
364 OPC(fcsel, SELECT, 0_1_2, NZ),
365 OP(fdiv, DIV, 0_1_X),
366 OP(fddx, DSX, 0_X_0), OP(fddy, DSY, 0_X_0),
367
368 /* type convert */
369 IOP(i2f32, I2F, 0_X_X),
370 UOP(u2f32, I2F, 0_X_X),
371 IOP(f2i32, F2I, 0_X_X),
372 UOP(f2u32, F2I, 0_X_X),
373 UOP(b2f32, AND, 0_X_X), /* AND with fui(1.0f) */
374 UOP(b2i32, AND, 0_X_X), /* AND with 1 */
375 OPC(f2b32, CMP, 0_X_X, NE), /* != 0.0 */
376 UOPC(i2b32, CMP, 0_X_X, NE), /* != 0 */
377
378 /* arithmetic */
379 IOP(iadd, ADD, 0_X_1),
380 IOP(imul, IMULLO0, 0_1_X),
381 /* IOP(imad, IMADLO0, 0_1_2), */
382 IOP(ineg, ADD, X_X_0), /* ADD 0, -x */
383 IOP(iabs, IABS, X_X_0),
384 IOP(isign, SIGN, X_X_0),
385 IOPC(imin, SELECT, 0_1_0, GT),
386 IOPC(imax, SELECT, 0_1_0, LT),
387 UOPC(umin, SELECT, 0_1_0, GT),
388 UOPC(umax, SELECT, 0_1_0, LT),
389
390 /* select */
391 UOPC(b32csel, SELECT, 0_1_2, NZ),
392
393 /* compare with int result */
394 OPC(feq32, CMP, 0_1_X, EQ),
395 OPC(fne32, CMP, 0_1_X, NE),
396 OPC(fge32, CMP, 0_1_X, GE),
397 OPC(flt32, CMP, 0_1_X, LT),
398 IOPC(ieq32, CMP, 0_1_X, EQ),
399 IOPC(ine32, CMP, 0_1_X, NE),
400 IOPC(ige32, CMP, 0_1_X, GE),
401 IOPC(ilt32, CMP, 0_1_X, LT),
402 UOPC(uge32, CMP, 0_1_X, GE),
403 UOPC(ult32, CMP, 0_1_X, LT),
404
405 /* bit ops */
406 IOP(ior, OR, 0_X_1),
407 IOP(iand, AND, 0_X_1),
408 IOP(ixor, XOR, 0_X_1),
409 IOP(inot, NOT, X_X_0),
410 IOP(ishl, LSHIFT, 0_X_1),
411 IOP(ishr, RSHIFT, 0_X_1),
412 UOP(ushr, RSHIFT, 0_X_1),
413 };
414
415 static void
416 etna_emit_block_start(struct etna_compile *c, unsigned block)
417 {
418 c->block_ptr[block] = c->inst_ptr;
419 }
420
421 static void
422 etna_emit_alu(struct etna_compile *c, nir_op op, struct etna_inst_dst dst,
423 struct etna_inst_src src[3], bool saturate)
424 {
425 struct etna_op_info ei = etna_ops[op];
426 unsigned swiz_scalar = INST_SWIZ_BROADCAST(ffs(dst.write_mask) - 1);
427
428 assert(ei.opcode != 0xff);
429
430 struct etna_inst inst = {
431 .opcode = ei.opcode,
432 .type = ei.type,
433 .cond = ei.cond,
434 .dst = dst,
435 .sat = saturate,
436 };
437
438 switch (op) {
439 case nir_op_fdiv:
440 case nir_op_flog2:
441 case nir_op_fsin:
442 case nir_op_fcos:
443 if (c->specs->has_new_transcendentals)
444 inst.tex.amode = 1;
445 /* fall through */
446 case nir_op_frsq:
447 case nir_op_frcp:
448 case nir_op_fexp2:
449 case nir_op_fsqrt:
450 case nir_op_imul:
451 /* scalar instructions we want src to be in x component */
452 src[0].swiz = inst_swiz_compose(src[0].swiz, swiz_scalar);
453 src[1].swiz = inst_swiz_compose(src[1].swiz, swiz_scalar);
454 break;
455 /* deal with instructions which don't have 1:1 mapping */
456 case nir_op_b2f32:
457 inst.src[2] = etna_immediate_float(1.0f);
458 break;
459 case nir_op_b2i32:
460 inst.src[2] = etna_immediate_int(1);
461 break;
462 case nir_op_f2b32:
463 inst.src[1] = etna_immediate_float(0.0f);
464 break;
465 case nir_op_i2b32:
466 inst.src[1] = etna_immediate_int(0);
467 break;
468 case nir_op_ineg:
469 inst.src[0] = etna_immediate_int(0);
470 src[0].neg = 1;
471 break;
472 default:
473 break;
474 }
475
476 /* set the "true" value for CMP instructions */
477 if (inst.opcode == INST_OPCODE_CMP)
478 inst.src[2] = etna_immediate_int(-1);
479
480 for (unsigned j = 0; j < 3; j++) {
481 unsigned i = ((ei.src >> j*2) & 3);
482 if (i < 3)
483 inst.src[j] = src[i];
484 }
485
486 emit_inst(c, &inst);
487 }
488
489 static void
490 etna_emit_tex(struct etna_compile *c, nir_texop op, unsigned texid, unsigned dst_swiz,
491 struct etna_inst_dst dst, struct etna_inst_src coord,
492 struct etna_inst_src lod_bias, struct etna_inst_src compare)
493 {
494 struct etna_inst inst = {
495 .dst = dst,
496 .tex.id = texid + (is_fs(c) ? 0 : c->specs->vertex_sampler_offset),
497 .tex.swiz = dst_swiz,
498 .src[0] = coord,
499 };
500
501 if (lod_bias.use)
502 inst.src[1] = lod_bias;
503
504 if (compare.use)
505 inst.src[2] = compare;
506
507 switch (op) {
508 case nir_texop_tex: inst.opcode = INST_OPCODE_TEXLD; break;
509 case nir_texop_txb: inst.opcode = INST_OPCODE_TEXLDB; break;
510 case nir_texop_txl: inst.opcode = INST_OPCODE_TEXLDL; break;
511 default:
512 assert(0);
513 }
514
515 emit_inst(c, &inst);
516 }
517
518 static void
519 etna_emit_jump(struct etna_compile *c, unsigned block, struct etna_inst_src condition)
520 {
521 if (!condition.use) {
522 emit_inst(c, &(struct etna_inst) {.opcode = INST_OPCODE_BRANCH, .imm = block });
523 return;
524 }
525
526 struct etna_inst inst = {
527 .opcode = INST_OPCODE_BRANCH,
528 .cond = INST_CONDITION_NOT,
529 .type = INST_TYPE_U32,
530 .src[0] = condition,
531 .imm = block,
532 };
533 inst.src[0].swiz = INST_SWIZ_BROADCAST(inst.src[0].swiz & 3);
534 emit_inst(c, &inst);
535 }
536
537 static void
538 etna_emit_discard(struct etna_compile *c, struct etna_inst_src condition)
539 {
540 if (!condition.use) {
541 emit_inst(c, &(struct etna_inst) { .opcode = INST_OPCODE_TEXKILL });
542 return;
543 }
544
545 struct etna_inst inst = {
546 .opcode = INST_OPCODE_TEXKILL,
547 .cond = INST_CONDITION_NZ,
548 .type = (c->specs->halti < 2) ? INST_TYPE_F32 : INST_TYPE_U32,
549 .src[0] = condition,
550 };
551 inst.src[0].swiz = INST_SWIZ_BROADCAST(inst.src[0].swiz & 3);
552 emit_inst(c, &inst);
553 }
554
555 static void
556 etna_emit_output(struct etna_compile *c, nir_variable *var, struct etna_inst_src src)
557 {
558 struct etna_shader_io_file *sf = &c->variant->outfile;
559
560 if (is_fs(c)) {
561 switch (var->data.location) {
562 case FRAG_RESULT_COLOR:
563 case FRAG_RESULT_DATA0: /* DATA0 is used by gallium shaders for color */
564 c->variant->ps_color_out_reg = src.reg;
565 break;
566 case FRAG_RESULT_DEPTH:
567 c->variant->ps_depth_out_reg = src.reg;
568 break;
569 default:
570 unreachable("Unsupported fs output");
571 }
572 return;
573 }
574
575 switch (var->data.location) {
576 case VARYING_SLOT_POS:
577 c->variant->vs_pos_out_reg = src.reg;
578 break;
579 case VARYING_SLOT_PSIZ:
580 c->variant->vs_pointsize_out_reg = src.reg;
581 break;
582 default:
583 sf->reg[sf->num_reg].reg = src.reg;
584 sf->reg[sf->num_reg].slot = var->data.location;
585 sf->reg[sf->num_reg].num_components = glsl_get_components(var->type);
586 sf->num_reg++;
587 break;
588 }
589 }
590
591 static void
592 etna_emit_load_ubo(struct etna_compile *c, struct etna_inst_dst dst,
593 struct etna_inst_src src, struct etna_inst_src base)
594 {
595 /* convert float offset back to integer */
596 if (c->specs->halti < 2) {
597 emit_inst(c, &(struct etna_inst) {
598 .opcode = INST_OPCODE_F2I,
599 .type = INST_TYPE_U32,
600 .dst = dst,
601 .src[0] = src,
602 });
603
604 emit_inst(c, &(struct etna_inst) {
605 .opcode = INST_OPCODE_LOAD,
606 .type = INST_TYPE_U32,
607 .dst = dst,
608 .src[0] = {
609 .use = 1,
610 .rgroup = INST_RGROUP_TEMP,
611 .reg = dst.reg,
612 .swiz = INST_SWIZ_BROADCAST(ffs(dst.write_mask) - 1)
613 },
614 .src[1] = base,
615 });
616
617 return;
618 }
619
620 emit_inst(c, &(struct etna_inst) {
621 .opcode = INST_OPCODE_LOAD,
622 .type = INST_TYPE_U32,
623 .dst = dst,
624 .src[0] = src,
625 .src[1] = base,
626 });
627 }
628
629 #define OPT(nir, pass, ...) ({ \
630 bool this_progress = false; \
631 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
632 this_progress; \
633 })
634 #define OPT_V(nir, pass, ...) NIR_PASS_V(nir, pass, ##__VA_ARGS__)
635
636 static void
637 etna_optimize_loop(nir_shader *s)
638 {
639 bool progress;
640 do {
641 progress = false;
642
643 OPT_V(s, nir_lower_vars_to_ssa);
644 progress |= OPT(s, nir_opt_copy_prop_vars);
645 progress |= OPT(s, nir_copy_prop);
646 progress |= OPT(s, nir_opt_dce);
647 progress |= OPT(s, nir_opt_cse);
648 progress |= OPT(s, nir_opt_peephole_select, 16, true, true);
649 progress |= OPT(s, nir_opt_intrinsics);
650 progress |= OPT(s, nir_opt_algebraic);
651 progress |= OPT(s, nir_opt_constant_folding);
652 progress |= OPT(s, nir_opt_dead_cf);
653 if (OPT(s, nir_opt_trivial_continues)) {
654 progress = true;
655 /* If nir_opt_trivial_continues makes progress, then we need to clean
656 * things up if we want any hope of nir_opt_if or nir_opt_loop_unroll
657 * to make progress.
658 */
659 OPT(s, nir_copy_prop);
660 OPT(s, nir_opt_dce);
661 }
662 progress |= OPT(s, nir_opt_loop_unroll, nir_var_all);
663 progress |= OPT(s, nir_opt_if, false);
664 progress |= OPT(s, nir_opt_remove_phis);
665 progress |= OPT(s, nir_opt_undef);
666 }
667 while (progress);
668 }
669
670 static int
671 etna_glsl_type_size(const struct glsl_type *type, bool bindless)
672 {
673 return glsl_count_attribute_slots(type, false);
674 }
675
676 static void
677 copy_uniform_state_to_shader(struct etna_shader_variant *sobj, uint64_t *consts, unsigned count)
678 {
679 struct etna_shader_uniform_info *uinfo = &sobj->uniforms;
680
681 uinfo->imm_count = count * 4;
682 uinfo->imm_data = MALLOC(uinfo->imm_count * sizeof(*uinfo->imm_data));
683 uinfo->imm_contents = MALLOC(uinfo->imm_count * sizeof(*uinfo->imm_contents));
684
685 for (unsigned i = 0; i < uinfo->imm_count; i++) {
686 uinfo->imm_data[i] = consts[i];
687 uinfo->imm_contents[i] = consts[i] >> 32;
688 }
689
690 etna_set_shader_uniforms_dirty_flags(sobj);
691 }
692
693 #include "etnaviv_compiler_nir_emit.h"
694
695 bool
696 etna_compile_shader_nir(struct etna_shader_variant *v)
697 {
698 if (unlikely(!v))
699 return false;
700
701 struct etna_compile *c = CALLOC_STRUCT(etna_compile);
702 if (!c)
703 return false;
704
705 c->variant = v;
706 c->specs = v->shader->specs;
707 c->nir = nir_shader_clone(NULL, v->shader->nir);
708
709 nir_shader *s = c->nir;
710 const struct etna_specs *specs = c->specs;
711
712 v->stage = s->info.stage;
713 v->num_loops = 0; /* TODO */
714 v->vs_id_in_reg = -1;
715 v->vs_pos_out_reg = -1;
716 v->vs_pointsize_out_reg = -1;
717 v->ps_color_out_reg = 0; /* 0 for shader that doesn't write fragcolor.. */
718 v->ps_depth_out_reg = -1;
719
720 /* setup input linking */
721 struct etna_shader_io_file *sf = &v->infile;
722 if (s->info.stage == MESA_SHADER_VERTEX) {
723 nir_foreach_variable(var, &s->inputs) {
724 unsigned idx = var->data.driver_location;
725 sf->reg[idx].reg = idx;
726 sf->reg[idx].slot = var->data.location;
727 sf->reg[idx].num_components = glsl_get_components(var->type);
728 sf->num_reg = MAX2(sf->num_reg, idx+1);
729 }
730 } else {
731 unsigned count = 0;
732 nir_foreach_variable(var, &s->inputs) {
733 unsigned idx = var->data.driver_location;
734 sf->reg[idx].reg = idx + 1;
735 sf->reg[idx].slot = var->data.location;
736 sf->reg[idx].num_components = glsl_get_components(var->type);
737 sf->num_reg = MAX2(sf->num_reg, idx+1);
738 count++;
739 }
740 assert(sf->num_reg == count);
741 }
742
743 NIR_PASS_V(s, nir_lower_io, ~nir_var_shader_out, etna_glsl_type_size,
744 (nir_lower_io_options)0);
745
746 OPT_V(s, nir_lower_regs_to_ssa);
747 OPT_V(s, nir_lower_vars_to_ssa);
748 OPT_V(s, nir_lower_indirect_derefs, nir_var_all);
749 OPT_V(s, nir_lower_tex, &(struct nir_lower_tex_options) { .lower_txp = ~0u });
750 OPT_V(s, nir_lower_alu_to_scalar, etna_alu_to_scalar_filter_cb, specs);
751
752 etna_optimize_loop(s);
753
754 OPT_V(s, etna_lower_io, v);
755
756 /* lower pre-halti2 to float (halti0 has integers, but only scalar..) */
757 if (c->specs->halti < 2) {
758 /* use opt_algebraic between int_to_float and boot_to_float because
759 * int_to_float emits ftrunc, and ftrunc lowering generates bool ops
760 */
761 OPT_V(s, nir_lower_int_to_float);
762 OPT_V(s, nir_opt_algebraic);
763 OPT_V(s, nir_lower_bool_to_float);
764 } else {
765 OPT_V(s, nir_lower_idiv, nir_lower_idiv_fast);
766 OPT_V(s, nir_lower_bool_to_int32);
767 }
768
769 etna_optimize_loop(s);
770
771 if (DBG_ENABLED(ETNA_DBG_DUMP_SHADERS))
772 nir_print_shader(s, stdout);
773
774 while( OPT(s, nir_opt_vectorize) );
775 OPT_V(s, nir_lower_alu_to_scalar, etna_alu_to_scalar_filter_cb, specs);
776
777 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
778 NIR_PASS_V(s, nir_opt_algebraic_late);
779
780 NIR_PASS_V(s, nir_move_vec_src_uses_to_dest);
781 NIR_PASS_V(s, nir_copy_prop);
782 /* only HW supported integer source mod is ineg for iadd instruction (?) */
783 NIR_PASS_V(s, nir_lower_to_source_mods, ~nir_lower_int_source_mods);
784 /* need copy prop after uses_to_dest, and before src mods: see
785 * dEQP-GLES2.functional.shaders.random.all_features.fragment.95
786 */
787
788 NIR_PASS_V(s, nir_opt_dce);
789
790 NIR_PASS_V(s, etna_lower_alu, c);
791
792 if (DBG_ENABLED(ETNA_DBG_DUMP_SHADERS))
793 nir_print_shader(s, stdout);
794
795 unsigned block_ptr[nir_shader_get_entrypoint(s)->num_blocks];
796 c->block_ptr = block_ptr;
797
798 unsigned num_consts;
799 ASSERTED bool ok = emit_shader(c, &v->num_temps, &num_consts);
800 assert(ok);
801
802 /* empty shader, emit NOP */
803 if (!c->inst_ptr)
804 emit_inst(c, &(struct etna_inst) { .opcode = INST_OPCODE_NOP });
805
806 /* assemble instructions, fixing up labels */
807 uint32_t *code = MALLOC(c->inst_ptr * 16);
808 for (unsigned i = 0; i < c->inst_ptr; i++) {
809 struct etna_inst *inst = &c->code[i];
810 if (inst->opcode == INST_OPCODE_BRANCH)
811 inst->imm = block_ptr[inst->imm];
812
813 inst->halti5 = specs->halti >= 5;
814 etna_assemble(&code[i * 4], inst);
815 }
816
817 v->code_size = c->inst_ptr * 4;
818 v->code = code;
819 v->needs_icache = c->inst_ptr > specs->max_instructions;
820
821 copy_uniform_state_to_shader(v, c->consts, num_consts);
822
823 if (s->info.stage == MESA_SHADER_FRAGMENT) {
824 v->input_count_unk8 = 31; /* XXX what is this */
825 assert(v->ps_depth_out_reg <= 0);
826 ralloc_free(c->nir);
827 FREE(c);
828 return true;
829 }
830
831 v->input_count_unk8 = DIV_ROUND_UP(v->infile.num_reg + 4, 16); /* XXX what is this */
832
833 /* fill in "mystery meat" load balancing value. This value determines how
834 * work is scheduled between VS and PS
835 * in the unified shader architecture. More precisely, it is determined from
836 * the number of VS outputs, as well as chip-specific
837 * vertex output buffer size, vertex cache size, and the number of shader
838 * cores.
839 *
840 * XXX this is a conservative estimate, the "optimal" value is only known for
841 * sure at link time because some
842 * outputs may be unused and thus unmapped. Then again, in the general use
843 * case with GLSL the vertex and fragment
844 * shaders are linked already before submitting to Gallium, thus all outputs
845 * are used.
846 *
847 * note: TGSI compiler counts all outputs (including position and pointsize), here
848 * v->outfile.num_reg only counts varyings, +1 to compensate for the position output
849 * TODO: might have a problem that we don't count pointsize when it is used
850 */
851
852 int half_out = v->outfile.num_reg / 2 + 1;
853 assert(half_out);
854
855 uint32_t b = ((20480 / (specs->vertex_output_buffer_size -
856 2 * half_out * specs->vertex_cache_size)) +
857 9) /
858 10;
859 uint32_t a = (b + 256 / (specs->shader_core_count * half_out)) / 2;
860 v->vs_load_balancing = VIVS_VS_LOAD_BALANCING_A(MIN2(a, 255)) |
861 VIVS_VS_LOAD_BALANCING_B(MIN2(b, 255)) |
862 VIVS_VS_LOAD_BALANCING_C(0x3f) |
863 VIVS_VS_LOAD_BALANCING_D(0x0f);
864
865 ralloc_free(c->nir);
866 FREE(c);
867 return true;
868 }
869
870 void
871 etna_destroy_shader_nir(struct etna_shader_variant *shader)
872 {
873 assert(shader);
874
875 FREE(shader->code);
876 FREE(shader->uniforms.imm_data);
877 FREE(shader->uniforms.imm_contents);
878 FREE(shader);
879 }
880
881 extern const char *tgsi_swizzle_names[];
882 void
883 etna_dump_shader_nir(const struct etna_shader_variant *shader)
884 {
885 if (shader->stage == MESA_SHADER_VERTEX)
886 printf("VERT\n");
887 else
888 printf("FRAG\n");
889
890 etna_disasm(shader->code, shader->code_size, PRINT_RAW);
891
892 printf("num loops: %i\n", shader->num_loops);
893 printf("num temps: %i\n", shader->num_temps);
894 printf("immediates:\n");
895 for (int idx = 0; idx < shader->uniforms.imm_count; ++idx) {
896 printf(" [%i].%s = %f (0x%08x) (%d)\n",
897 idx / 4,
898 tgsi_swizzle_names[idx % 4],
899 *((float *)&shader->uniforms.imm_data[idx]),
900 shader->uniforms.imm_data[idx],
901 shader->uniforms.imm_contents[idx]);
902 }
903 printf("inputs:\n");
904 for (int idx = 0; idx < shader->infile.num_reg; ++idx) {
905 printf(" [%i] name=%s comps=%i\n", shader->infile.reg[idx].reg,
906 (shader->stage == MESA_SHADER_VERTEX) ?
907 gl_vert_attrib_name(shader->infile.reg[idx].slot) :
908 gl_varying_slot_name(shader->infile.reg[idx].slot),
909 shader->infile.reg[idx].num_components);
910 }
911 printf("outputs:\n");
912 for (int idx = 0; idx < shader->outfile.num_reg; ++idx) {
913 printf(" [%i] name=%s comps=%i\n", shader->outfile.reg[idx].reg,
914 (shader->stage == MESA_SHADER_VERTEX) ?
915 gl_varying_slot_name(shader->outfile.reg[idx].slot) :
916 gl_frag_result_name(shader->outfile.reg[idx].slot),
917 shader->outfile.reg[idx].num_components);
918 }
919 printf("special:\n");
920 if (shader->stage == MESA_SHADER_VERTEX) {
921 printf(" vs_pos_out_reg=%i\n", shader->vs_pos_out_reg);
922 printf(" vs_pointsize_out_reg=%i\n", shader->vs_pointsize_out_reg);
923 printf(" vs_load_balancing=0x%08x\n", shader->vs_load_balancing);
924 } else {
925 printf(" ps_color_out_reg=%i\n", shader->ps_color_out_reg);
926 printf(" ps_depth_out_reg=%i\n", shader->ps_depth_out_reg);
927 }
928 printf(" input_count_unk8=0x%08x\n", shader->input_count_unk8);
929 }
930
931 static const struct etna_shader_inout *
932 etna_shader_vs_lookup(const struct etna_shader_variant *sobj,
933 const struct etna_shader_inout *in)
934 {
935 for (int i = 0; i < sobj->outfile.num_reg; i++)
936 if (sobj->outfile.reg[i].slot == in->slot)
937 return &sobj->outfile.reg[i];
938
939 return NULL;
940 }
941
942 bool
943 etna_link_shader_nir(struct etna_shader_link_info *info,
944 const struct etna_shader_variant *vs,
945 const struct etna_shader_variant *fs)
946 {
947 int comp_ofs = 0;
948 /* For each fragment input we need to find the associated vertex shader
949 * output, which can be found by matching on semantic name and index. A
950 * binary search could be used because the vs outputs are sorted by their
951 * semantic index and grouped by semantic type by fill_in_vs_outputs.
952 */
953 assert(fs->infile.num_reg < ETNA_NUM_INPUTS);
954 info->pcoord_varying_comp_ofs = -1;
955
956 for (int idx = 0; idx < fs->infile.num_reg; ++idx) {
957 const struct etna_shader_inout *fsio = &fs->infile.reg[idx];
958 const struct etna_shader_inout *vsio = etna_shader_vs_lookup(vs, fsio);
959 struct etna_varying *varying;
960 bool interpolate_always = true;
961
962 assert(fsio->reg > 0 && fsio->reg <= ARRAY_SIZE(info->varyings));
963
964 if (fsio->reg > info->num_varyings)
965 info->num_varyings = fsio->reg;
966
967 varying = &info->varyings[fsio->reg - 1];
968 varying->num_components = fsio->num_components;
969
970 if (!interpolate_always) /* colors affected by flat shading */
971 varying->pa_attributes = 0x200;
972 else /* texture coord or other bypasses flat shading */
973 varying->pa_attributes = 0x2f1;
974
975 varying->use[0] = VARYING_COMPONENT_USE_UNUSED;
976 varying->use[1] = VARYING_COMPONENT_USE_UNUSED;
977 varying->use[2] = VARYING_COMPONENT_USE_UNUSED;
978 varying->use[3] = VARYING_COMPONENT_USE_UNUSED;
979
980 /* point coord is an input to the PS without matching VS output,
981 * so it gets a varying slot without being assigned a VS register.
982 */
983 if (fsio->slot == VARYING_SLOT_PNTC) {
984 varying->use[0] = VARYING_COMPONENT_USE_POINTCOORD_X;
985 varying->use[1] = VARYING_COMPONENT_USE_POINTCOORD_Y;
986
987 info->pcoord_varying_comp_ofs = comp_ofs;
988 } else {
989 if (vsio == NULL) { /* not found -- link error */
990 BUG("Semantic value not found in vertex shader outputs\n");
991 return true;
992 }
993 varying->reg = vsio->reg;
994 }
995
996 comp_ofs += varying->num_components;
997 }
998
999 assert(info->num_varyings == fs->infile.num_reg);
1000
1001 return false;
1002 }