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