2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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:
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
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
24 * Rob Clark <robclark@freedesktop.org>
27 #include "util/u_atomic.h"
28 #include "util/u_string.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/format/u_format.h"
33 #include "drm/freedreno_drmif.h"
35 #include "ir3_shader.h"
36 #include "ir3_compiler.h"
42 ir3_glsl_type_size(const struct glsl_type
*type
, bool bindless
)
44 return glsl_count_attribute_slots(type
, false);
47 /* for vertex shader, the inputs are loaded into registers before the shader
48 * is executed, so max_regs from the shader instructions might not properly
49 * reflect the # of registers actually used, especially in case passthrough
52 * Likewise, for fragment shader, we can have some regs which are passed
53 * input values but never touched by the resulting shader (ie. as result
54 * of dead code elimination or simply because we don't know how to turn
58 fixup_regfootprint(struct ir3_shader_variant
*v
)
62 for (i
= 0; i
< v
->inputs_count
; i
++) {
63 /* skip frag inputs fetch via bary.f since their reg's are
64 * not written by gpu before shader starts (and in fact the
65 * regid's might not even be valid)
67 if (v
->inputs
[i
].bary
)
70 /* ignore high regs that are global to all threads in a warp
71 * (they exist by default) (a5xx+)
73 if (v
->inputs
[i
].regid
>= regid(48,0))
76 if (v
->inputs
[i
].compmask
) {
77 unsigned n
= util_last_bit(v
->inputs
[i
].compmask
) - 1;
78 int32_t regid
= v
->inputs
[i
].regid
+ n
;
79 if (v
->inputs
[i
].half
) {
81 v
->info
.max_half_reg
= MAX2(v
->info
.max_half_reg
, regid
>> 2);
83 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 3);
86 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 2);
91 for (i
= 0; i
< v
->outputs_count
; i
++) {
92 /* for ex, VS shaders with tess don't have normal varying outs: */
93 if (!VALIDREG(v
->outputs
[i
].regid
))
95 int32_t regid
= v
->outputs
[i
].regid
+ 3;
96 if (v
->outputs
[i
].half
) {
98 v
->info
.max_half_reg
= MAX2(v
->info
.max_half_reg
, regid
>> 2);
100 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 3);
103 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 2);
107 for (i
= 0; i
< v
->num_sampler_prefetch
; i
++) {
108 unsigned n
= util_last_bit(v
->sampler_prefetch
[i
].wrmask
) - 1;
109 int32_t regid
= v
->sampler_prefetch
[i
].dst
+ n
;
110 if (v
->sampler_prefetch
[i
].half_precision
) {
111 if (!v
->mergedregs
) {
112 v
->info
.max_half_reg
= MAX2(v
->info
.max_half_reg
, regid
>> 2);
114 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 3);
117 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 2);
122 /* wrapper for ir3_assemble() which does some info fixup based on
123 * shader state. Non-static since used by ir3_cmdline too.
125 void * ir3_shader_assemble(struct ir3_shader_variant
*v
)
127 const struct ir3_compiler
*compiler
= v
->shader
->compiler
;
130 bin
= ir3_assemble(v
);
134 /* NOTE: if relative addressing is used, we set constlen in
135 * the compiler (to worst-case value) since we don't know in
136 * the assembler what the max addr reg value can be:
138 v
->constlen
= MAX2(v
->constlen
, v
->info
.max_const
+ 1);
140 /* On a4xx and newer, constlen must be a multiple of 16 dwords even though
141 * uploads are in units of 4 dwords. Round it up here to make calculations
142 * regarding the shared constlen simpler.
144 if (compiler
->gpu_id
>= 400)
145 v
->constlen
= align(v
->constlen
, 4);
147 fixup_regfootprint(v
);
153 assemble_variant(struct ir3_shader_variant
*v
)
155 v
->bin
= ir3_shader_assemble(v
);
157 if (shader_debug_enabled(v
->shader
->type
)) {
158 fprintf(stdout
, "Native code for unnamed %s shader %s:\n",
159 ir3_shader_stage(v
), v
->shader
->nir
->info
.name
);
160 if (v
->shader
->type
== MESA_SHADER_FRAGMENT
)
161 fprintf(stdout
, "SIMD0\n");
162 ir3_shader_disasm(v
, v
->bin
, stdout
);
165 /* no need to keep the ir around beyond this point: */
171 compile_variant(struct ir3_shader_variant
*v
)
173 int ret
= ir3_compile_shader_nir(v
->shader
->compiler
, v
);
175 _debug_printf("compile failed! (%s:%s)", v
->shader
->nir
->info
.name
,
176 v
->shader
->nir
->info
.label
);
182 _debug_printf("assemble failed! (%s:%s)", v
->shader
->nir
->info
.name
,
183 v
->shader
->nir
->info
.label
);
191 * For creating normal shader variants, 'nonbinning' is NULL. For
192 * creating binning pass shader, it is link to corresponding normal
193 * (non-binning) variant.
195 static struct ir3_shader_variant
*
196 alloc_variant(struct ir3_shader
*shader
, const struct ir3_shader_key
*key
,
197 struct ir3_shader_variant
*nonbinning
)
199 void *mem_ctx
= shader
;
200 /* hang the binning variant off it's non-binning counterpart instead
201 * of the shader, to simplify the error cleanup paths
204 mem_ctx
= nonbinning
;
205 struct ir3_shader_variant
*v
= rzalloc_size(mem_ctx
, sizeof(*v
));
210 v
->id
= ++shader
->variant_count
;
212 v
->binning_pass
= !!nonbinning
;
213 v
->nonbinning
= nonbinning
;
215 v
->type
= shader
->type
;
216 v
->mergedregs
= shader
->compiler
->gpu_id
>= 600;
218 if (!v
->binning_pass
)
219 v
->const_state
= rzalloc_size(v
, sizeof(*v
->const_state
));
225 needs_binning_variant(struct ir3_shader_variant
*v
)
227 if ((v
->type
== MESA_SHADER_VERTEX
) && ir3_has_binning_vs(&v
->key
))
232 static struct ir3_shader_variant
*
233 create_variant(struct ir3_shader
*shader
, const struct ir3_shader_key
*key
)
235 struct ir3_shader_variant
*v
= alloc_variant(shader
, key
, NULL
);
240 if (needs_binning_variant(v
)) {
241 v
->binning
= alloc_variant(shader
, key
, v
);
246 if (ir3_disk_cache_retrieve(shader
->compiler
, v
))
249 if (!shader
->nir_finalized
) {
250 ir3_nir_post_finalize(shader
->compiler
, shader
->nir
);
252 if (ir3_shader_debug
& IR3_DBG_DISASM
) {
253 printf("dump nir%d: type=%d", shader
->id
, shader
->type
);
254 nir_print_shader(shader
->nir
, stdout
);
257 shader
->nir_finalized
= true;
260 if (!compile_variant(v
))
263 if (needs_binning_variant(v
) && !compile_variant(v
->binning
))
266 ir3_disk_cache_store(shader
->compiler
, v
);
275 static inline struct ir3_shader_variant
*
276 shader_variant(struct ir3_shader
*shader
, const struct ir3_shader_key
*key
)
278 struct ir3_shader_variant
*v
;
280 for (v
= shader
->variants
; v
; v
= v
->next
)
281 if (ir3_shader_key_equal(key
, &v
->key
))
287 struct ir3_shader_variant
*
288 ir3_shader_get_variant(struct ir3_shader
*shader
, const struct ir3_shader_key
*key
,
289 bool binning_pass
, bool *created
)
291 mtx_lock(&shader
->variants_lock
);
292 struct ir3_shader_variant
*v
= shader_variant(shader
, key
);
295 /* compile new variant if it doesn't exist already: */
296 v
= create_variant(shader
, key
);
298 v
->next
= shader
->variants
;
299 shader
->variants
= v
;
304 if (v
&& binning_pass
) {
309 mtx_unlock(&shader
->variants_lock
);
315 ir3_shader_destroy(struct ir3_shader
*shader
)
317 ralloc_free(shader
->nir
);
318 mtx_destroy(&shader
->variants_lock
);
323 * Creates a bitmask of the used bits of the shader key by this particular
324 * shader. Used by the gallium driver to skip state-dependent recompiles when
328 ir3_setup_used_key(struct ir3_shader
*shader
)
330 nir_shader
*nir
= shader
->nir
;
331 struct shader_info
*info
= &nir
->info
;
332 struct ir3_shader_key
*key
= &shader
->key_mask
;
334 /* This key flag is just used to make for a cheaper ir3_shader_key_equal
335 * check in the common case.
337 key
->has_per_samp
= true;
339 key
->safe_constlen
= true;
341 key
->ucp_enables
= 0xff;
343 if (info
->stage
== MESA_SHADER_FRAGMENT
) {
344 key
->fsaturate_s
= ~0;
345 key
->fsaturate_t
= ~0;
346 key
->fsaturate_r
= ~0;
347 key
->fastc_srgb
= ~0;
350 if (info
->inputs_read
& VARYING_BITS_COLOR
) {
351 key
->rasterflat
= true;
352 key
->color_two_side
= true;
355 if (info
->inputs_read
& VARYING_BIT_LAYER
) {
356 key
->layer_zero
= true;
359 if ((info
->outputs_written
& ~(FRAG_RESULT_DEPTH
|
360 FRAG_RESULT_STENCIL
|
361 FRAG_RESULT_SAMPLE_MASK
)) != 0) {
362 key
->fclamp_color
= true;
365 /* Only used for deciding on behavior of
366 * nir_intrinsic_load_barycentric_sample
368 key
->msaa
= info
->fs
.uses_sample_qualifier
;
370 key
->tessellation
= ~0;
373 if (info
->outputs_written
& VARYING_BITS_COLOR
)
374 key
->vclamp_color
= true;
376 if (info
->stage
== MESA_SHADER_VERTEX
) {
377 key
->vsaturate_s
= ~0;
378 key
->vsaturate_t
= ~0;
379 key
->vsaturate_r
= ~0;
380 key
->vastc_srgb
= ~0;
387 /* Given an array of constlen's, decrease some of them so that the sum stays
388 * within "combined_limit" while trying to fairly share the reduction. Returns
389 * a bitfield of which stages should be trimmed.
392 trim_constlens(unsigned *constlens
,
393 unsigned first_stage
, unsigned last_stage
,
394 unsigned combined_limit
, unsigned safe_limit
)
396 unsigned cur_total
= 0;
397 for (unsigned i
= first_stage
; i
<= last_stage
; i
++) {
398 cur_total
+= constlens
[i
];
401 unsigned max_stage
= 0;
402 unsigned max_const
= 0;
403 uint32_t trimmed
= 0;
405 while (cur_total
> combined_limit
) {
406 for (unsigned i
= first_stage
; i
<= last_stage
; i
++) {
407 if (constlens
[i
] >= max_const
) {
409 max_const
= constlens
[i
];
413 assert(max_const
> safe_limit
);
414 trimmed
|= 1 << max_stage
;
415 cur_total
= cur_total
- max_const
+ safe_limit
;
416 constlens
[max_stage
] = safe_limit
;
422 /* Figures out which stages in the pipeline to use the "safe" constlen for, in
423 * order to satisfy all shared constlen limits.
426 ir3_trim_constlen(struct ir3_shader_variant
**variants
,
427 const struct ir3_compiler
*compiler
)
429 unsigned constlens
[MESA_SHADER_STAGES
] = {};
431 for (unsigned i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
433 constlens
[i
] = variants
[i
]->constlen
;
436 uint32_t trimmed
= 0;
437 STATIC_ASSERT(MESA_SHADER_STAGES
<= 8 * sizeof(trimmed
));
439 /* There are two shared limits to take into account, the geometry limit on
440 * a6xx and the total limit. The frag limit on a6xx only matters for a
441 * single stage, so it's always satisfied with the first variant.
443 if (compiler
->gpu_id
>= 600) {
445 trim_constlens(constlens
, MESA_SHADER_VERTEX
, MESA_SHADER_GEOMETRY
,
446 compiler
->max_const_geom
, compiler
->max_const_safe
);
449 trim_constlens(constlens
, MESA_SHADER_VERTEX
, MESA_SHADER_FRAGMENT
,
450 compiler
->max_const_pipeline
, compiler
->max_const_safe
);
456 ir3_shader_from_nir(struct ir3_compiler
*compiler
, nir_shader
*nir
,
457 unsigned reserved_user_consts
, struct ir3_stream_output_info
*stream_output
)
459 struct ir3_shader
*shader
= rzalloc_size(NULL
, sizeof(*shader
));
461 mtx_init(&shader
->variants_lock
, mtx_plain
);
462 shader
->compiler
= compiler
;
463 shader
->id
= p_atomic_inc_return(&shader
->compiler
->shader_count
);
464 shader
->type
= nir
->info
.stage
;
466 memcpy(&shader
->stream_output
, stream_output
, sizeof(shader
->stream_output
));
467 shader
->num_reserved_user_consts
= reserved_user_consts
;
470 ir3_disk_cache_init_shader_key(compiler
, shader
);
472 ir3_setup_used_key(shader
);
477 static void dump_reg(FILE *out
, const char *name
, uint32_t r
)
479 if (r
!= regid(63,0)) {
480 const char *reg_type
= (r
& HALF_REG_ID
) ? "hr" : "r";
481 fprintf(out
, "; %s: %s%d.%c\n", name
, reg_type
,
482 (r
& ~HALF_REG_ID
) >> 2, "xyzw"[r
& 0x3]);
486 static void dump_output(FILE *out
, struct ir3_shader_variant
*so
,
487 unsigned slot
, const char *name
)
490 regid
= ir3_find_output_regid(so
, slot
);
491 dump_reg(out
, name
, regid
);
495 input_name(struct ir3_shader_variant
*so
, int i
)
497 if (so
->inputs
[i
].sysval
) {
498 return gl_system_value_name(so
->inputs
[i
].slot
);
499 } else if (so
->type
== MESA_SHADER_VERTEX
) {
500 return gl_vert_attrib_name(so
->inputs
[i
].slot
);
502 return gl_varying_slot_name(so
->inputs
[i
].slot
);
507 output_name(struct ir3_shader_variant
*so
, int i
)
509 if (so
->type
== MESA_SHADER_FRAGMENT
) {
510 return gl_frag_result_name(so
->outputs
[i
].slot
);
512 switch (so
->outputs
[i
].slot
) {
513 case VARYING_SLOT_GS_HEADER_IR3
:
515 case VARYING_SLOT_GS_VERTEX_FLAGS_IR3
:
516 return "GS_VERTEX_FLAGS";
517 case VARYING_SLOT_TCS_HEADER_IR3
:
520 return gl_varying_slot_name(so
->outputs
[i
].slot
);
526 ir3_shader_disasm(struct ir3_shader_variant
*so
, uint32_t *bin
, FILE *out
)
528 struct ir3
*ir
= so
->ir
;
529 struct ir3_register
*reg
;
530 const char *type
= ir3_shader_stage(so
);
534 foreach_input_n (instr
, i
, ir
) {
535 reg
= instr
->regs
[0];
537 fprintf(out
, "@in(%sr%d.%c)\tin%d",
538 (reg
->flags
& IR3_REG_HALF
) ? "h" : "",
539 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
541 if (reg
->wrmask
> 0x1)
542 fprintf(out
, " (wrmask=0x%x)", reg
->wrmask
);
546 /* print pre-dispatch texture fetches: */
547 for (i
= 0; i
< so
->num_sampler_prefetch
; i
++) {
548 const struct ir3_sampler_prefetch
*fetch
= &so
->sampler_prefetch
[i
];
549 fprintf(out
, "@tex(%sr%d.%c)\tsrc=%u, samp=%u, tex=%u, wrmask=0x%x, cmd=%u\n",
550 fetch
->half_precision
? "h" : "",
551 fetch
->dst
>> 2, "xyzw"[fetch
->dst
& 0x3],
552 fetch
->src
, fetch
->samp_id
, fetch
->tex_id
,
553 fetch
->wrmask
, fetch
->cmd
);
556 foreach_output_n (instr
, i
, ir
) {
557 reg
= instr
->regs
[0];
559 fprintf(out
, "@out(%sr%d.%c)\tout%d",
560 (reg
->flags
& IR3_REG_HALF
) ? "h" : "",
561 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
562 if (reg
->wrmask
> 0x1)
563 fprintf(out
, " (wrmask=0x%x)", reg
->wrmask
);
567 const struct ir3_const_state
*const_state
= ir3_const_state(so
);
568 for (i
= 0; i
< DIV_ROUND_UP(const_state
->immediates_count
, 4); i
++) {
569 fprintf(out
, "@const(c%d.x)\t", const_state
->offsets
.immediate
+ i
);
570 fprintf(out
, "0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
571 const_state
->immediates
[i
* 4 + 0],
572 const_state
->immediates
[i
* 4 + 1],
573 const_state
->immediates
[i
* 4 + 2],
574 const_state
->immediates
[i
* 4 + 3]);
577 disasm_a3xx(bin
, so
->info
.sizedwords
, 0, out
, ir
->compiler
->gpu_id
);
579 fprintf(out
, "; %s: outputs:", type
);
580 for (i
= 0; i
< so
->outputs_count
; i
++) {
581 uint8_t regid
= so
->outputs
[i
].regid
;
582 const char *reg_type
= so
->outputs
[i
].half
? "hr" : "r";
583 fprintf(out
, " %s%d.%c (%s)",
584 reg_type
, (regid
>> 2), "xyzw"[regid
& 0x3],
589 fprintf(out
, "; %s: inputs:", type
);
590 for (i
= 0; i
< so
->inputs_count
; i
++) {
591 uint8_t regid
= so
->inputs
[i
].regid
;
592 fprintf(out
, " r%d.%c (%s slot=%d cm=%x,il=%u,b=%u)",
593 (regid
>> 2), "xyzw"[regid
& 0x3],
596 so
->inputs
[i
].compmask
,
602 /* print generic shader info: */
603 fprintf(out
, "; %s prog %d/%d: %u instr, %u nops, %u non-nops, %u mov, %u cov, %u dwords\n",
604 type
, so
->shader
->id
, so
->id
,
605 so
->info
.instrs_count
,
607 so
->info
.instrs_count
- so
->info
.nops_count
,
608 so
->info
.mov_count
, so
->info
.cov_count
,
609 so
->info
.sizedwords
);
611 fprintf(out
, "; %s prog %d/%d: %u last-baryf, %d half, %d full, %u constlen\n",
612 type
, so
->shader
->id
, so
->id
,
614 so
->info
.max_half_reg
+ 1,
615 so
->info
.max_reg
+ 1,
618 fprintf(out
, "; %s prog %d/%d: %u cat0, %u cat1, %u cat2, %u cat3, %u cat4, %u cat5, %u cat6, %u cat7, \n",
619 type
, so
->shader
->id
, so
->id
,
620 so
->info
.instrs_per_cat
[0],
621 so
->info
.instrs_per_cat
[1],
622 so
->info
.instrs_per_cat
[2],
623 so
->info
.instrs_per_cat
[3],
624 so
->info
.instrs_per_cat
[4],
625 so
->info
.instrs_per_cat
[5],
626 so
->info
.instrs_per_cat
[6],
627 so
->info
.instrs_per_cat
[7]);
629 fprintf(out
, "; %s prog %d/%d: %u sstall, %u (ss), %u (sy), %d max_sun, %d loops\n",
630 type
, so
->shader
->id
, so
->id
,
637 /* print shader type specific info: */
639 case MESA_SHADER_VERTEX
:
640 dump_output(out
, so
, VARYING_SLOT_POS
, "pos");
641 dump_output(out
, so
, VARYING_SLOT_PSIZ
, "psize");
643 case MESA_SHADER_FRAGMENT
:
644 dump_reg(out
, "pos (ij_pixel)",
645 ir3_find_sysval_regid(so
, SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL
));
646 dump_reg(out
, "pos (ij_centroid)",
647 ir3_find_sysval_regid(so
, SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID
));
648 dump_reg(out
, "pos (ij_size)",
649 ir3_find_sysval_regid(so
, SYSTEM_VALUE_BARYCENTRIC_PERSP_SIZE
));
650 dump_output(out
, so
, FRAG_RESULT_DEPTH
, "posz");
651 if (so
->color0_mrt
) {
652 dump_output(out
, so
, FRAG_RESULT_COLOR
, "color");
654 dump_output(out
, so
, FRAG_RESULT_DATA0
, "data0");
655 dump_output(out
, so
, FRAG_RESULT_DATA1
, "data1");
656 dump_output(out
, so
, FRAG_RESULT_DATA2
, "data2");
657 dump_output(out
, so
, FRAG_RESULT_DATA3
, "data3");
658 dump_output(out
, so
, FRAG_RESULT_DATA4
, "data4");
659 dump_output(out
, so
, FRAG_RESULT_DATA5
, "data5");
660 dump_output(out
, so
, FRAG_RESULT_DATA6
, "data6");
661 dump_output(out
, so
, FRAG_RESULT_DATA7
, "data7");
663 dump_reg(out
, "fragcoord",
664 ir3_find_sysval_regid(so
, SYSTEM_VALUE_FRAG_COORD
));
665 dump_reg(out
, "fragface",
666 ir3_find_sysval_regid(so
, SYSTEM_VALUE_FRONT_FACE
));
677 ir3_shader_outputs(const struct ir3_shader
*so
)
679 return so
->nir
->info
.outputs_written
;