7a0c28fd1a3776c339270cf7fc0869d754dc99de
[mesa.git] / src / freedreno / ir3 / ir3_shader.c
1 /*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "util/u_atomic.h"
28 #include "util/u_string.h"
29 #include "util/u_memory.h"
30 #include "util/u_format.h"
31
32 #include "drm/freedreno_drmif.h"
33
34 #include "ir3_shader.h"
35 #include "ir3_compiler.h"
36 #include "ir3_nir.h"
37
38 int
39 ir3_glsl_type_size(const struct glsl_type *type, bool bindless)
40 {
41 return glsl_count_attribute_slots(type, false);
42 }
43
44 static void
45 delete_variant(struct ir3_shader_variant *v)
46 {
47 if (v->ir)
48 ir3_destroy(v->ir);
49 if (v->bo)
50 fd_bo_del(v->bo);
51 free(v);
52 }
53
54 /* for vertex shader, the inputs are loaded into registers before the shader
55 * is executed, so max_regs from the shader instructions might not properly
56 * reflect the # of registers actually used, especially in case passthrough
57 * varyings.
58 *
59 * Likewise, for fragment shader, we can have some regs which are passed
60 * input values but never touched by the resulting shader (ie. as result
61 * of dead code elimination or simply because we don't know how to turn
62 * the reg off.
63 */
64 static void
65 fixup_regfootprint(struct ir3_shader_variant *v, uint32_t gpu_id)
66 {
67 unsigned i;
68
69 for (i = 0; i < v->inputs_count; i++) {
70 /* skip frag inputs fetch via bary.f since their reg's are
71 * not written by gpu before shader starts (and in fact the
72 * regid's might not even be valid)
73 */
74 if (v->inputs[i].bary)
75 continue;
76
77 /* ignore high regs that are global to all threads in a warp
78 * (they exist by default) (a5xx+)
79 */
80 if (v->inputs[i].regid >= regid(48,0))
81 continue;
82
83 if (v->inputs[i].compmask) {
84 unsigned n = util_last_bit(v->inputs[i].compmask) - 1;
85 int32_t regid = v->inputs[i].regid + n;
86 if (v->inputs[i].half) {
87 if (gpu_id < 500) {
88 v->info.max_half_reg = MAX2(v->info.max_half_reg, regid >> 2);
89 } else {
90 v->info.max_reg = MAX2(v->info.max_reg, regid >> 3);
91 }
92 } else {
93 v->info.max_reg = MAX2(v->info.max_reg, regid >> 2);
94 }
95 }
96 }
97
98 for (i = 0; i < v->outputs_count; i++) {
99 int32_t regid = v->outputs[i].regid + 3;
100 if (v->outputs[i].half) {
101 if (gpu_id < 500) {
102 v->info.max_half_reg = MAX2(v->info.max_half_reg, regid >> 2);
103 } else {
104 v->info.max_reg = MAX2(v->info.max_reg, regid >> 3);
105 }
106 } else {
107 v->info.max_reg = MAX2(v->info.max_reg, regid >> 2);
108 }
109 }
110
111 for (i = 0; i < v->num_sampler_prefetch; i++) {
112 unsigned n = util_last_bit(v->sampler_prefetch[i].wrmask) - 1;
113 int32_t regid = v->sampler_prefetch[i].dst + n;
114 if (v->sampler_prefetch[i].half_precision) {
115 if (gpu_id < 500) {
116 v->info.max_half_reg = MAX2(v->info.max_half_reg, regid >> 2);
117 } else {
118 v->info.max_reg = MAX2(v->info.max_reg, regid >> 3);
119 }
120 } else {
121 v->info.max_reg = MAX2(v->info.max_reg, regid >> 2);
122 }
123 }
124 }
125
126 /* wrapper for ir3_assemble() which does some info fixup based on
127 * shader state. Non-static since used by ir3_cmdline too.
128 */
129 void * ir3_shader_assemble(struct ir3_shader_variant *v, uint32_t gpu_id)
130 {
131 void *bin;
132
133 bin = ir3_assemble(v->ir, &v->info, gpu_id);
134 if (!bin)
135 return NULL;
136
137 if (gpu_id >= 400) {
138 v->instrlen = v->info.sizedwords / (2 * 16);
139 } else {
140 v->instrlen = v->info.sizedwords / (2 * 4);
141 }
142
143 /* NOTE: if relative addressing is used, we set constlen in
144 * the compiler (to worst-case value) since we don't know in
145 * the assembler what the max addr reg value can be:
146 */
147 v->constlen = MAX2(v->constlen, v->info.max_const + 1);
148
149 fixup_regfootprint(v, gpu_id);
150
151 return bin;
152 }
153
154 static void
155 assemble_variant(struct ir3_shader_variant *v)
156 {
157 struct ir3_compiler *compiler = v->shader->compiler;
158 struct shader_info *info = &v->shader->nir->info;
159 uint32_t gpu_id = compiler->gpu_id;
160 uint32_t sz, *bin;
161
162 bin = ir3_shader_assemble(v, gpu_id);
163 sz = v->info.sizedwords * 4;
164
165 v->bo = fd_bo_new(compiler->dev, sz,
166 DRM_FREEDRENO_GEM_CACHE_WCOMBINE |
167 DRM_FREEDRENO_GEM_TYPE_KMEM,
168 "%s:%s", ir3_shader_stage(v->shader), info->name);
169
170 memcpy(fd_bo_map(v->bo), bin, sz);
171
172 if (ir3_shader_debug & IR3_DBG_DISASM) {
173 struct ir3_shader_key key = v->key;
174 printf("disassemble: type=%d, k={bp=%u,cts=%u,hp=%u}\n", v->type,
175 v->binning_pass, key.color_two_side, key.half_precision);
176 ir3_shader_disasm(v, bin, stdout);
177 }
178
179 if (shader_debug_enabled(v->shader->type)) {
180 fprintf(stderr, "Native code for unnamed %s shader %s:\n",
181 _mesa_shader_stage_to_string(v->shader->type),
182 v->shader->nir->info.name);
183 if (v->shader->type == MESA_SHADER_FRAGMENT)
184 fprintf(stderr, "SIMD0\n");
185 ir3_shader_disasm(v, bin, stderr);
186 }
187
188 free(bin);
189
190 /* no need to keep the ir around beyond this point: */
191 ir3_destroy(v->ir);
192 v->ir = NULL;
193 }
194
195 /*
196 * For creating normal shader variants, 'nonbinning' is NULL. For
197 * creating binning pass shader, it is link to corresponding normal
198 * (non-binning) variant.
199 */
200 static struct ir3_shader_variant *
201 create_variant(struct ir3_shader *shader, struct ir3_shader_key *key,
202 struct ir3_shader_variant *nonbinning)
203 {
204 struct ir3_shader_variant *v = CALLOC_STRUCT(ir3_shader_variant);
205 int ret;
206
207 if (!v)
208 return NULL;
209
210 v->id = ++shader->variant_count;
211 v->shader = shader;
212 v->binning_pass = !!nonbinning;
213 v->nonbinning = nonbinning;
214 v->key = *key;
215 v->type = shader->type;
216
217 ret = ir3_compile_shader_nir(shader->compiler, v);
218 if (ret) {
219 debug_error("compile failed!");
220 goto fail;
221 }
222
223 assemble_variant(v);
224 if (!v->bo) {
225 debug_error("assemble failed!");
226 goto fail;
227 }
228
229 return v;
230
231 fail:
232 delete_variant(v);
233 return NULL;
234 }
235
236 static inline struct ir3_shader_variant *
237 shader_variant(struct ir3_shader *shader, struct ir3_shader_key *key,
238 bool *created)
239 {
240 struct ir3_shader_variant *v;
241
242 *created = false;
243
244 for (v = shader->variants; v; v = v->next)
245 if (ir3_shader_key_equal(key, &v->key))
246 return v;
247
248 /* compile new variant if it doesn't exist already: */
249 v = create_variant(shader, key, NULL);
250 if (v) {
251 v->next = shader->variants;
252 shader->variants = v;
253 *created = true;
254 }
255
256 return v;
257 }
258
259 struct ir3_shader_variant *
260 ir3_shader_get_variant(struct ir3_shader *shader, struct ir3_shader_key *key,
261 bool binning_pass, bool *created)
262 {
263 mtx_lock(&shader->variants_lock);
264 struct ir3_shader_variant *v =
265 shader_variant(shader, key, created);
266
267 if (v && binning_pass) {
268 if (!v->binning) {
269 v->binning = create_variant(shader, key, v);
270 *created = true;
271 }
272 mtx_unlock(&shader->variants_lock);
273 return v->binning;
274 }
275 mtx_unlock(&shader->variants_lock);
276
277 return v;
278 }
279
280 void
281 ir3_shader_destroy(struct ir3_shader *shader)
282 {
283 struct ir3_shader_variant *v, *t;
284 for (v = shader->variants; v; ) {
285 t = v;
286 v = v->next;
287 delete_variant(t);
288 }
289 free(shader->const_state.immediates);
290 ralloc_free(shader->nir);
291 mtx_destroy(&shader->variants_lock);
292 free(shader);
293 }
294
295 struct ir3_shader *
296 ir3_shader_from_nir(struct ir3_compiler *compiler, nir_shader *nir)
297 {
298 struct ir3_shader *shader = CALLOC_STRUCT(ir3_shader);
299
300 mtx_init(&shader->variants_lock, mtx_plain);
301 shader->compiler = compiler;
302 shader->id = p_atomic_inc_return(&shader->compiler->shader_count);
303 shader->type = nir->info.stage;
304
305 NIR_PASS_V(nir, nir_lower_io, nir_var_all, ir3_glsl_type_size,
306 (nir_lower_io_options)0);
307
308 if (nir->info.stage == MESA_SHADER_FRAGMENT) {
309 /* NOTE: lower load_barycentric_at_sample first, since it
310 * produces load_barycentric_at_offset:
311 */
312 NIR_PASS_V(nir, ir3_nir_lower_load_barycentric_at_sample);
313 NIR_PASS_V(nir, ir3_nir_lower_load_barycentric_at_offset);
314
315 NIR_PASS_V(nir, ir3_nir_move_varying_inputs);
316 }
317
318 NIR_PASS_V(nir, nir_lower_io_arrays_to_elements_no_indirects, false);
319
320 NIR_PASS_V(nir, nir_lower_amul, ir3_glsl_type_size);
321
322 /* do first pass optimization, ignoring the key: */
323 ir3_optimize_nir(shader, nir, NULL);
324
325 shader->nir = nir;
326 if (ir3_shader_debug & IR3_DBG_DISASM) {
327 printf("dump nir%d: type=%d", shader->id, shader->type);
328 nir_print_shader(shader->nir, stdout);
329 }
330
331 return shader;
332 }
333
334 static void dump_reg(FILE *out, const char *name, uint32_t r)
335 {
336 if (r != regid(63,0)) {
337 const char *reg_type = (r & HALF_REG_ID) ? "hr" : "r";
338 fprintf(out, "; %s: %s%d.%c\n", name, reg_type,
339 (r & ~HALF_REG_ID) >> 2, "xyzw"[r & 0x3]);
340 }
341 }
342
343 static void dump_output(FILE *out, struct ir3_shader_variant *so,
344 unsigned slot, const char *name)
345 {
346 uint32_t regid;
347 regid = ir3_find_output_regid(so, slot);
348 dump_reg(out, name, regid);
349 }
350
351 static const char *
352 input_name(struct ir3_shader_variant *so, int i)
353 {
354 if (so->inputs[i].sysval) {
355 return gl_system_value_name(so->inputs[i].slot);
356 } else if (so->type == MESA_SHADER_VERTEX) {
357 return gl_vert_attrib_name(so->inputs[i].slot);
358 } else {
359 return gl_varying_slot_name(so->inputs[i].slot);
360 }
361 }
362
363 static const char *
364 output_name(struct ir3_shader_variant *so, int i)
365 {
366 if (so->type == MESA_SHADER_FRAGMENT) {
367 return gl_frag_result_name(so->outputs[i].slot);
368 } else {
369 switch (so->outputs[i].slot) {
370 case VARYING_SLOT_GS_HEADER_IR3:
371 return "GS_HEADER";
372 case VARYING_SLOT_GS_VERTEX_FLAGS_IR3:
373 return "GS_VERTEX_FLAGS";
374 default:
375 return gl_varying_slot_name(so->outputs[i].slot);
376 }
377 }
378 }
379
380 void
381 ir3_shader_disasm(struct ir3_shader_variant *so, uint32_t *bin, FILE *out)
382 {
383 struct ir3 *ir = so->ir;
384 struct ir3_register *reg;
385 const char *type = ir3_shader_stage(so->shader);
386 uint8_t regid;
387 unsigned i;
388
389 for (i = 0; i < ir->ninputs; i++) {
390 if (!ir->inputs[i]) {
391 fprintf(out, "; in%d unused\n", i);
392 continue;
393 }
394 reg = ir->inputs[i]->regs[0];
395 regid = reg->num;
396 fprintf(out, "@in(%sr%d.%c)\tin%d\n",
397 (reg->flags & IR3_REG_HALF) ? "h" : "",
398 (regid >> 2), "xyzw"[regid & 0x3], i);
399 }
400
401 /* print pre-dispatch texture fetches: */
402 for (i = 0; i < so->num_sampler_prefetch; i++) {
403 const struct ir3_sampler_prefetch *fetch = &so->sampler_prefetch[i];
404 fprintf(out, "@tex(%sr%d.%c)\tsrc=%u, samp=%u, tex=%u, wrmask=%x, cmd=%u\n",
405 fetch->half_precision ? "h" : "",
406 fetch->dst >> 2, "xyzw"[fetch->dst & 0x3],
407 fetch->src, fetch->samp_id, fetch->tex_id,
408 fetch->wrmask, fetch->cmd);
409 }
410
411 for (i = 0; i < ir->noutputs; i++) {
412 if (!ir->outputs[i]) {
413 fprintf(out, "; out%d unused\n", i);
414 continue;
415 }
416 /* kill shows up as a virtual output.. skip it! */
417 if (is_kill(ir->outputs[i]))
418 continue;
419 reg = ir->outputs[i]->regs[0];
420 regid = reg->num;
421 fprintf(out, "@out(%sr%d.%c)\tout%d\n",
422 (reg->flags & IR3_REG_HALF) ? "h" : "",
423 (regid >> 2), "xyzw"[regid & 0x3], i);
424 }
425
426 struct ir3_const_state *const_state = &so->shader->const_state;
427 for (i = 0; i < const_state->immediates_count; i++) {
428 fprintf(out, "@const(c%d.x)\t", const_state->offsets.immediate + i);
429 fprintf(out, "0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
430 const_state->immediates[i].val[0],
431 const_state->immediates[i].val[1],
432 const_state->immediates[i].val[2],
433 const_state->immediates[i].val[3]);
434 }
435
436 disasm_a3xx(bin, so->info.sizedwords, 0, out, ir->compiler->gpu_id);
437
438 fprintf(out, "; %s: outputs:", type);
439 for (i = 0; i < so->outputs_count; i++) {
440 uint8_t regid = so->outputs[i].regid;
441 fprintf(out, " r%d.%c (%s)",
442 (regid >> 2), "xyzw"[regid & 0x3],
443 output_name(so, i));
444 }
445 fprintf(out, "\n");
446
447 fprintf(out, "; %s: inputs:", type);
448 for (i = 0; i < so->inputs_count; i++) {
449 uint8_t regid = so->inputs[i].regid;
450 fprintf(out, " r%d.%c (%s slot=%d cm=%x,il=%u,b=%u)",
451 (regid >> 2), "xyzw"[regid & 0x3],
452 input_name(so, i),
453 so->inputs[i].slot,
454 so->inputs[i].compmask,
455 so->inputs[i].inloc,
456 so->inputs[i].bary);
457 }
458 fprintf(out, "\n");
459
460 /* print generic shader info: */
461 fprintf(out, "; %s prog %d/%d: %u instructions, %d half, %d full\n",
462 type, so->shader->id, so->id,
463 so->info.instrs_count,
464 so->info.max_half_reg + 1,
465 so->info.max_reg + 1);
466
467 fprintf(out, "; %u constlen\n", so->constlen);
468
469 fprintf(out, "; %u (ss), %u (sy)\n", so->info.ss, so->info.sy);
470
471 fprintf(out, "; max_sun=%u\n", ir->max_sun);
472
473 /* print shader type specific info: */
474 switch (so->type) {
475 case MESA_SHADER_VERTEX:
476 dump_output(out, so, VARYING_SLOT_POS, "pos");
477 dump_output(out, so, VARYING_SLOT_PSIZ, "psize");
478 break;
479 case MESA_SHADER_FRAGMENT:
480 dump_reg(out, "pos (ij_pixel)",
481 ir3_find_sysval_regid(so, SYSTEM_VALUE_BARYCENTRIC_PIXEL));
482 dump_reg(out, "pos (ij_centroid)",
483 ir3_find_sysval_regid(so, SYSTEM_VALUE_BARYCENTRIC_CENTROID));
484 dump_reg(out, "pos (ij_size)",
485 ir3_find_sysval_regid(so, SYSTEM_VALUE_BARYCENTRIC_SIZE));
486 dump_output(out, so, FRAG_RESULT_DEPTH, "posz");
487 if (so->color0_mrt) {
488 dump_output(out, so, FRAG_RESULT_COLOR, "color");
489 } else {
490 dump_output(out, so, FRAG_RESULT_DATA0, "data0");
491 dump_output(out, so, FRAG_RESULT_DATA1, "data1");
492 dump_output(out, so, FRAG_RESULT_DATA2, "data2");
493 dump_output(out, so, FRAG_RESULT_DATA3, "data3");
494 dump_output(out, so, FRAG_RESULT_DATA4, "data4");
495 dump_output(out, so, FRAG_RESULT_DATA5, "data5");
496 dump_output(out, so, FRAG_RESULT_DATA6, "data6");
497 dump_output(out, so, FRAG_RESULT_DATA7, "data7");
498 }
499 /* these two are hard-coded since we don't know how to
500 * program them to anything but all 0's...
501 */
502 if (so->frag_coord)
503 fprintf(out, "; fragcoord: r0.x\n");
504 if (so->frag_face)
505 fprintf(out, "; fragface: hr0.x\n");
506 break;
507 default:
508 /* TODO */
509 break;
510 }
511
512 fprintf(out, "\n");
513 }
514
515 uint64_t
516 ir3_shader_outputs(const struct ir3_shader *so)
517 {
518 return so->nir->info.outputs_written;
519 }