Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_shader.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "pipe/p_state.h"
30 #include "util/u_string.h"
31 #include "util/u_memory.h"
32 #include "util/u_inlines.h"
33 #include "util/u_format.h"
34 #include "tgsi/tgsi_dump.h"
35 #include "tgsi/tgsi_parse.h"
36
37 #include "freedreno_context.h"
38 #include "freedreno_util.h"
39
40 #include "ir3_shader.h"
41 #include "ir3_compiler.h"
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)
66 {
67 if (v->type == SHADER_VERTEX) {
68 unsigned i;
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 if (v->inputs[i].compmask) {
78 int32_t regid = (v->inputs[i].regid + 3) >> 2;
79 v->info.max_reg = MAX2(v->info.max_reg, regid);
80 }
81 }
82 for (i = 0; i < v->outputs_count; i++) {
83 int32_t regid = (v->outputs[i].regid + 3) >> 2;
84 v->info.max_reg = MAX2(v->info.max_reg, regid);
85 }
86 } else if (v->type == SHADER_FRAGMENT) {
87 /* NOTE: not sure how to turn pos_regid off.. but this could
88 * be, for example, r1.x while max reg used by the shader is
89 * r0.*, in which case we need to fixup the reg footprint:
90 */
91 v->info.max_reg = MAX2(v->info.max_reg, v->pos_regid >> 2);
92 if (v->frag_coord)
93 debug_assert(v->info.max_reg >= 0); /* hard coded r0.x */
94 if (v->frag_face)
95 debug_assert(v->info.max_half_reg >= 0); /* hr0.x */
96 }
97 }
98
99 /* wrapper for ir3_assemble() which does some info fixup based on
100 * shader state. Non-static since used by ir3_cmdline too.
101 */
102 void * ir3_shader_assemble(struct ir3_shader_variant *v, uint32_t gpu_id)
103 {
104 void *bin;
105
106 bin = ir3_assemble(v->ir, &v->info, gpu_id);
107 if (!bin)
108 return NULL;
109
110 if (gpu_id >= 400) {
111 v->instrlen = v->info.sizedwords / (2 * 16);
112 } else {
113 v->instrlen = v->info.sizedwords / (2 * 4);
114 }
115
116 /* NOTE: if relative addressing is used, we set constlen in
117 * the compiler (to worst-case value) since we don't know in
118 * the assembler what the max addr reg value can be:
119 */
120 v->constlen = MIN2(255, MAX2(v->constlen, v->info.max_const + 1));
121
122 fixup_regfootprint(v);
123
124 return bin;
125 }
126
127 static void
128 assemble_variant(struct ir3_shader_variant *v)
129 {
130 struct fd_context *ctx = fd_context(v->shader->pctx);
131 uint32_t gpu_id = v->shader->compiler->gpu_id;
132 uint32_t sz, *bin;
133
134 bin = ir3_shader_assemble(v, gpu_id);
135 sz = v->info.sizedwords * 4;
136
137 v->bo = fd_bo_new(ctx->dev, sz,
138 DRM_FREEDRENO_GEM_CACHE_WCOMBINE |
139 DRM_FREEDRENO_GEM_TYPE_KMEM);
140
141 memcpy(fd_bo_map(v->bo), bin, sz);
142
143 if (fd_mesa_debug & FD_DBG_DISASM) {
144 struct ir3_shader_key key = v->key;
145 DBG("disassemble: type=%d, k={bp=%u,cts=%u,hp=%u}", v->type,
146 key.binning_pass, key.color_two_side, key.half_precision);
147 ir3_shader_disasm(v, bin);
148 }
149
150 if (fd_mesa_debug & FD_DBG_SHADERDB) {
151 /* print generic shader info: */
152 fprintf(stderr, "SHADER-DB: %s prog %d/%d: %u instructions, %u dwords\n",
153 ir3_shader_stage(v->shader),
154 v->shader->id, v->id,
155 v->info.instrs_count,
156 v->info.sizedwords);
157 fprintf(stderr, "SHADER-DB: %s prog %d/%d: %u half, %u full\n",
158 ir3_shader_stage(v->shader),
159 v->shader->id, v->id,
160 v->info.max_half_reg + 1,
161 v->info.max_reg + 1);
162 fprintf(stderr, "SHADER-DB: %s prog %d/%d: %u const, %u constlen\n",
163 ir3_shader_stage(v->shader),
164 v->shader->id, v->id,
165 v->info.max_const + 1,
166 v->constlen);
167 }
168
169 free(bin);
170
171 /* no need to keep the ir around beyond this point: */
172 ir3_destroy(v->ir);
173 v->ir = NULL;
174 }
175
176 static struct ir3_shader_variant *
177 create_variant(struct ir3_shader *shader, struct ir3_shader_key key)
178 {
179 struct ir3_shader_variant *v = CALLOC_STRUCT(ir3_shader_variant);
180 int ret;
181
182 if (!v)
183 return NULL;
184
185 v->id = ++shader->variant_count;
186 v->shader = shader;
187 v->key = key;
188 v->type = shader->type;
189
190 if (fd_mesa_debug & FD_DBG_DISASM) {
191 DBG("dump tgsi: type=%d, k={bp=%u,cts=%u,hp=%u}", shader->type,
192 key.binning_pass, key.color_two_side, key.half_precision);
193 tgsi_dump(shader->tokens, 0);
194 }
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 struct ir3_shader_variant *
216 ir3_shader_variant(struct ir3_shader *shader, struct ir3_shader_key key)
217 {
218 struct ir3_shader_variant *v;
219
220 /* some shader key values only apply to vertex or frag shader,
221 * so normalize the key to avoid constructing multiple identical
222 * variants:
223 */
224 switch (shader->type) {
225 case SHADER_FRAGMENT:
226 case SHADER_COMPUTE:
227 key.binning_pass = false;
228 if (key.has_per_samp) {
229 key.vsaturate_s = 0;
230 key.vsaturate_t = 0;
231 key.vsaturate_r = 0;
232 }
233 break;
234 case SHADER_VERTEX:
235 key.color_two_side = false;
236 key.half_precision = false;
237 key.rasterflat = false;
238 if (key.has_per_samp) {
239 key.fsaturate_s = 0;
240 key.fsaturate_t = 0;
241 key.fsaturate_r = 0;
242 }
243 break;
244 }
245
246 for (v = shader->variants; v; v = v->next)
247 if (ir3_shader_key_equal(&key, &v->key))
248 return v;
249
250 /* compile new variant if it doesn't exist already: */
251 v = create_variant(shader, key);
252 if (v) {
253 v->next = shader->variants;
254 shader->variants = v;
255 }
256
257 return v;
258 }
259
260
261 void
262 ir3_shader_destroy(struct ir3_shader *shader)
263 {
264 struct ir3_shader_variant *v, *t;
265 for (v = shader->variants; v; ) {
266 t = v;
267 v = v->next;
268 delete_variant(t);
269 }
270 free((void *)shader->tokens);
271 free(shader);
272 }
273
274 struct ir3_shader *
275 ir3_shader_create(struct pipe_context *pctx,
276 const struct pipe_shader_state *cso,
277 enum shader_t type)
278 {
279 struct ir3_shader *shader = CALLOC_STRUCT(ir3_shader);
280 shader->compiler = fd_context(pctx)->screen->compiler;
281 shader->id = ++shader->compiler->shader_count;
282 shader->pctx = pctx;
283 shader->type = type;
284 shader->tokens = tgsi_dup_tokens(cso->tokens);
285 shader->stream_output = cso->stream_output;
286 if (fd_mesa_debug & FD_DBG_SHADERDB) {
287 /* if shader-db run, create a standard variant immediately
288 * (as otherwise nothing will trigger the shader to be
289 * actually compiled)
290 */
291 static struct ir3_shader_key key = {};
292 ir3_shader_variant(shader, key);
293 }
294 return shader;
295 }
296
297 static void dump_reg(const char *name, uint32_t r)
298 {
299 if (r != regid(63,0))
300 debug_printf("; %s: r%d.%c\n", name, r >> 2, "xyzw"[r & 0x3]);
301 }
302
303 static void dump_semantic(struct ir3_shader_variant *so,
304 unsigned sem, const char *name)
305 {
306 uint32_t regid;
307 regid = ir3_find_output_regid(so, ir3_semantic_name(sem, 0));
308 dump_reg(name, regid);
309 }
310
311 void
312 ir3_shader_disasm(struct ir3_shader_variant *so, uint32_t *bin)
313 {
314 struct ir3 *ir = so->ir;
315 struct ir3_register *reg;
316 const char *type = ir3_shader_stage(so->shader);
317 uint8_t regid;
318 unsigned i;
319
320 for (i = 0; i < ir->ninputs; i++) {
321 if (!ir->inputs[i]) {
322 debug_printf("; in%d unused\n", i);
323 continue;
324 }
325 reg = ir->inputs[i]->regs[0];
326 regid = reg->num;
327 debug_printf("@in(%sr%d.%c)\tin%d\n",
328 (reg->flags & IR3_REG_HALF) ? "h" : "",
329 (regid >> 2), "xyzw"[regid & 0x3], i);
330 }
331
332 for (i = 0; i < ir->noutputs; i++) {
333 if (!ir->outputs[i]) {
334 debug_printf("; out%d unused\n", i);
335 continue;
336 }
337 /* kill shows up as a virtual output.. skip it! */
338 if (is_kill(ir->outputs[i]))
339 continue;
340 reg = ir->outputs[i]->regs[0];
341 regid = reg->num;
342 debug_printf("@out(%sr%d.%c)\tout%d\n",
343 (reg->flags & IR3_REG_HALF) ? "h" : "",
344 (regid >> 2), "xyzw"[regid & 0x3], i);
345 }
346
347 for (i = 0; i < so->immediates_count; i++) {
348 debug_printf("@const(c%d.x)\t", so->first_immediate + i);
349 debug_printf("0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
350 so->immediates[i].val[0],
351 so->immediates[i].val[1],
352 so->immediates[i].val[2],
353 so->immediates[i].val[3]);
354 }
355
356 disasm_a3xx(bin, so->info.sizedwords, 0, so->type);
357
358 debug_printf("; %s: outputs:", type);
359 for (i = 0; i < so->outputs_count; i++) {
360 uint8_t regid = so->outputs[i].regid;
361 ir3_semantic sem = so->outputs[i].semantic;
362 debug_printf(" r%d.%c (%u:%u)",
363 (regid >> 2), "xyzw"[regid & 0x3],
364 sem2name(sem), sem2idx(sem));
365 }
366 debug_printf("\n");
367 debug_printf("; %s: inputs:", type);
368 for (i = 0; i < so->inputs_count; i++) {
369 uint8_t regid = so->inputs[i].regid;
370 ir3_semantic sem = so->inputs[i].semantic;
371 debug_printf(" r%d.%c (%u:%u,cm=%x,il=%u,b=%u)",
372 (regid >> 2), "xyzw"[regid & 0x3],
373 sem2name(sem), sem2idx(sem),
374 so->inputs[i].compmask,
375 so->inputs[i].inloc,
376 so->inputs[i].bary);
377 }
378 debug_printf("\n");
379
380 /* print generic shader info: */
381 debug_printf("; %s prog %d/%d: %u instructions, %d half, %d full\n",
382 type, so->shader->id, so->id,
383 so->info.instrs_count,
384 so->info.max_half_reg + 1,
385 so->info.max_reg + 1);
386
387 debug_printf("; %d const, %u constlen\n",
388 so->info.max_const + 1,
389 so->constlen);
390
391 /* print shader type specific info: */
392 switch (so->type) {
393 case SHADER_VERTEX:
394 dump_semantic(so, TGSI_SEMANTIC_POSITION, "pos");
395 dump_semantic(so, TGSI_SEMANTIC_PSIZE, "psize");
396 break;
397 case SHADER_FRAGMENT:
398 dump_reg("pos (bary)", so->pos_regid);
399 dump_semantic(so, TGSI_SEMANTIC_POSITION, "posz");
400 dump_semantic(so, TGSI_SEMANTIC_COLOR, "color");
401 /* these two are hard-coded since we don't know how to
402 * program them to anything but all 0's...
403 */
404 if (so->frag_coord)
405 debug_printf("; fragcoord: r0.x\n");
406 if (so->frag_face)
407 debug_printf("; fragface: hr0.x\n");
408 break;
409 case SHADER_COMPUTE:
410 break;
411 }
412
413 debug_printf("\n");
414 }
415
416 /* This has to reach into the fd_context a bit more than the rest of
417 * ir3, but it needs to be aligned with the compiler, so both agree
418 * on which const regs hold what. And the logic is identical between
419 * a3xx/a4xx, the only difference is small details in the actual
420 * CP_LOAD_STATE packets (which is handled inside the generation
421 * specific ctx->emit_const(_bo)() fxns)
422 */
423
424 #include "freedreno_resource.h"
425
426 static void
427 emit_user_consts(struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
428 struct fd_constbuf_stateobj *constbuf)
429 {
430 struct fd_context *ctx = fd_context(v->shader->pctx);
431 const unsigned index = 0; /* user consts are index 0 */
432 /* TODO save/restore dirty_mask for binning pass instead: */
433 uint32_t dirty_mask = constbuf->enabled_mask;
434
435 if (dirty_mask & (1 << index)) {
436 struct pipe_constant_buffer *cb = &constbuf->cb[index];
437 unsigned size = align(cb->buffer_size, 4) / 4; /* size in dwords */
438
439 /* in particular, with binning shader we may end up with
440 * unused consts, ie. we could end up w/ constlen that is
441 * smaller than first_driver_param. In that case truncate
442 * the user consts early to avoid HLSQ lockup caused by
443 * writing too many consts
444 */
445 uint32_t max_const = MIN2(v->first_driver_param, v->constlen);
446
447 // I expect that size should be a multiple of vec4's:
448 assert(size == align(size, 4));
449
450 /* and even if the start of the const buffer is before
451 * first_immediate, the end may not be:
452 */
453 size = MIN2(size, 4 * max_const);
454
455 if (size > 0) {
456 fd_wfi(ctx, ring);
457 ctx->emit_const(ring, v->type, 0,
458 cb->buffer_offset, size,
459 cb->user_buffer, cb->buffer);
460 constbuf->dirty_mask &= ~(1 << index);
461 }
462 }
463 }
464
465 static void
466 emit_ubos(struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
467 struct fd_constbuf_stateobj *constbuf)
468 {
469 uint32_t offset = v->first_driver_param; /* UBOs after user consts */
470 if (v->constlen > offset) {
471 struct fd_context *ctx = fd_context(v->shader->pctx);
472 uint32_t params = MIN2(4, v->constlen - offset) * 4;
473 uint32_t offsets[params];
474 struct fd_bo *bos[params];
475
476 for (uint32_t i = 0; i < params; i++) {
477 const uint32_t index = i + 1; /* UBOs start at index 1 */
478 struct pipe_constant_buffer *cb = &constbuf->cb[index];
479 assert(!cb->user_buffer);
480
481 if ((constbuf->enabled_mask & (1 << index)) && cb->buffer) {
482 offsets[i] = cb->buffer_offset;
483 bos[i] = fd_resource(cb->buffer)->bo;
484 } else {
485 offsets[i] = 0;
486 bos[i] = NULL;
487 }
488 }
489
490 fd_wfi(ctx, ring);
491 ctx->emit_const_bo(ring, v->type, false, offset * 4, params, bos, offsets);
492 }
493 }
494
495 static void
496 emit_immediates(struct ir3_shader_variant *v, struct fd_ringbuffer *ring)
497 {
498 struct fd_context *ctx = fd_context(v->shader->pctx);
499 int size = v->immediates_count;
500 uint32_t base = v->first_immediate;
501
502 /* truncate size to avoid writing constants that shader
503 * does not use:
504 */
505 size = MIN2(size + base, v->constlen) - base;
506
507 /* convert out of vec4: */
508 base *= 4;
509 size *= 4;
510
511 if (size > 0) {
512 fd_wfi(ctx, ring);
513 ctx->emit_const(ring, v->type, base,
514 0, size, v->immediates[0].val, NULL);
515 }
516 }
517
518 /* emit stream-out buffers: */
519 static void
520 emit_tfbos(struct ir3_shader_variant *v, struct fd_ringbuffer *ring)
521 {
522 uint32_t offset = v->first_driver_param + 5; /* streamout addresses after driver-params*/
523 if (v->constlen > offset) {
524 struct fd_context *ctx = fd_context(v->shader->pctx);
525 struct fd_streamout_stateobj *so = &ctx->streamout;
526 struct pipe_stream_output_info *info = &v->shader->stream_output;
527 uint32_t params = 4;
528 uint32_t offsets[params];
529 struct fd_bo *bos[params];
530
531 for (uint32_t i = 0; i < params; i++) {
532 struct pipe_stream_output_target *target = so->targets[i];
533
534 if (target) {
535 offsets[i] = (so->offsets[i] * info->stride[i] * 4) +
536 target->buffer_offset;
537 bos[i] = fd_resource(target->buffer)->bo;
538 } else {
539 offsets[i] = 0;
540 bos[i] = NULL;
541 }
542 }
543
544 fd_wfi(ctx, ring);
545 ctx->emit_const_bo(ring, v->type, true, offset * 4, params, bos, offsets);
546 }
547 }
548
549 static uint32_t
550 max_tf_vtx(struct ir3_shader_variant *v)
551 {
552 struct fd_context *ctx = fd_context(v->shader->pctx);
553 struct fd_streamout_stateobj *so = &ctx->streamout;
554 struct pipe_stream_output_info *info = &v->shader->stream_output;
555 uint32_t maxvtxcnt = 0x7fffffff;
556
557 if (v->key.binning_pass)
558 return 0;
559 if (v->shader->stream_output.num_outputs == 0)
560 return 0;
561 if (so->num_targets == 0)
562 return 0;
563
564 /* offset to write to is:
565 *
566 * total_vtxcnt = vtxcnt + offsets[i]
567 * offset = total_vtxcnt * stride[i]
568 *
569 * offset = vtxcnt * stride[i] ; calculated in shader
570 * + offsets[i] * stride[i] ; calculated at emit_tfbos()
571 *
572 * assuming for each vtx, each target buffer will have data written
573 * up to 'offset + stride[i]', that leaves maxvtxcnt as:
574 *
575 * buffer_size = (maxvtxcnt * stride[i]) + stride[i]
576 * maxvtxcnt = (buffer_size - stride[i]) / stride[i]
577 *
578 * but shader is actually doing a less-than (rather than less-than-
579 * equal) check, so we can drop the -stride[i].
580 *
581 * TODO is assumption about `offset + stride[i]` legit?
582 */
583 for (unsigned i = 0; i < so->num_targets; i++) {
584 struct pipe_stream_output_target *target = so->targets[i];
585 unsigned stride = info->stride[i] * 4; /* convert dwords->bytes */
586 if (target) {
587 uint32_t max = target->buffer_size / stride;
588 maxvtxcnt = MIN2(maxvtxcnt, max);
589 }
590 }
591
592 return maxvtxcnt;
593 }
594
595 void
596 ir3_emit_consts(struct ir3_shader_variant *v, struct fd_ringbuffer *ring,
597 const struct pipe_draw_info *info, uint32_t dirty)
598 {
599 struct fd_context *ctx = fd_context(v->shader->pctx);
600
601 if (dirty & (FD_DIRTY_PROG | FD_DIRTY_CONSTBUF)) {
602 struct fd_constbuf_stateobj *constbuf;
603 bool shader_dirty;
604
605 if (v->type == SHADER_VERTEX) {
606 constbuf = &ctx->constbuf[PIPE_SHADER_VERTEX];
607 shader_dirty = !!(ctx->prog.dirty & FD_SHADER_DIRTY_VP);
608 } else if (v->type == SHADER_FRAGMENT) {
609 constbuf = &ctx->constbuf[PIPE_SHADER_FRAGMENT];
610 shader_dirty = !!(ctx->prog.dirty & FD_SHADER_DIRTY_FP);
611 } else {
612 unreachable("bad shader type");
613 return;
614 }
615
616 emit_user_consts(v, ring, constbuf);
617 emit_ubos(v, ring, constbuf);
618 if (shader_dirty)
619 emit_immediates(v, ring);
620 }
621
622 /* emit driver params every time: */
623 /* TODO skip emit if shader doesn't use driver params to avoid WFI.. */
624 if (info && (v->type == SHADER_VERTEX)) {
625 uint32_t offset = v->first_driver_param + 4; /* driver params after UBOs */
626 if (v->constlen >= offset) {
627 uint32_t vertex_params[4] = {
628 [IR3_DP_VTXID_BASE] = info->indexed ?
629 info->index_bias : info->start,
630 [IR3_DP_VTXCNT_MAX] = max_tf_vtx(v),
631 };
632
633 fd_wfi(ctx, ring);
634 ctx->emit_const(ring, SHADER_VERTEX, offset * 4, 0,
635 ARRAY_SIZE(vertex_params), vertex_params, NULL);
636
637 /* if needed, emit stream-out buffer addresses: */
638 if (vertex_params[IR3_DP_VTXCNT_MAX] > 0) {
639 emit_tfbos(v, ring);
640 }
641 }
642 }
643 }