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_memory.h"
30 #include "util/format/u_format.h"
32 #include "drm/freedreno_drmif.h"
34 #include "ir3_shader.h"
35 #include "ir3_compiler.h"
39 ir3_glsl_type_size(const struct glsl_type
*type
, bool bindless
)
41 return glsl_count_attribute_slots(type
, false);
45 delete_variant(struct ir3_shader_variant
*v
)
52 delete_variant(v
->binning
);
56 /* for vertex shader, the inputs are loaded into registers before the shader
57 * is executed, so max_regs from the shader instructions might not properly
58 * reflect the # of registers actually used, especially in case passthrough
61 * Likewise, for fragment shader, we can have some regs which are passed
62 * input values but never touched by the resulting shader (ie. as result
63 * of dead code elimination or simply because we don't know how to turn
67 fixup_regfootprint(struct ir3_shader_variant
*v
, uint32_t gpu_id
)
71 for (i
= 0; i
< v
->inputs_count
; i
++) {
72 /* skip frag inputs fetch via bary.f since their reg's are
73 * not written by gpu before shader starts (and in fact the
74 * regid's might not even be valid)
76 if (v
->inputs
[i
].bary
)
79 /* ignore high regs that are global to all threads in a warp
80 * (they exist by default) (a5xx+)
82 if (v
->inputs
[i
].regid
>= regid(48,0))
85 if (v
->inputs
[i
].compmask
) {
86 unsigned n
= util_last_bit(v
->inputs
[i
].compmask
) - 1;
87 int32_t regid
= v
->inputs
[i
].regid
+ n
;
88 if (v
->inputs
[i
].half
) {
90 v
->info
.max_half_reg
= MAX2(v
->info
.max_half_reg
, regid
>> 2);
92 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 3);
95 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 2);
100 for (i
= 0; i
< v
->outputs_count
; i
++) {
101 /* for ex, VS shaders with tess don't have normal varying outs: */
102 if (!VALIDREG(v
->outputs
[i
].regid
))
104 int32_t regid
= v
->outputs
[i
].regid
+ 3;
105 if (v
->outputs
[i
].half
) {
107 v
->info
.max_half_reg
= MAX2(v
->info
.max_half_reg
, regid
>> 2);
109 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 3);
112 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 2);
116 for (i
= 0; i
< v
->num_sampler_prefetch
; i
++) {
117 unsigned n
= util_last_bit(v
->sampler_prefetch
[i
].wrmask
) - 1;
118 int32_t regid
= v
->sampler_prefetch
[i
].dst
+ n
;
119 if (v
->sampler_prefetch
[i
].half_precision
) {
121 v
->info
.max_half_reg
= MAX2(v
->info
.max_half_reg
, regid
>> 2);
123 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 3);
126 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 2);
131 /* wrapper for ir3_assemble() which does some info fixup based on
132 * shader state. Non-static since used by ir3_cmdline too.
134 void * ir3_shader_assemble(struct ir3_shader_variant
*v
, uint32_t gpu_id
)
138 bin
= ir3_assemble(v
->ir
, &v
->info
, gpu_id
);
143 v
->instrlen
= v
->info
.sizedwords
/ (2 * 16);
145 v
->instrlen
= v
->info
.sizedwords
/ (2 * 4);
148 /* NOTE: if relative addressing is used, we set constlen in
149 * the compiler (to worst-case value) since we don't know in
150 * the assembler what the max addr reg value can be:
152 v
->constlen
= MAX2(v
->constlen
, v
->info
.max_const
+ 1);
154 fixup_regfootprint(v
, gpu_id
);
160 assemble_variant(struct ir3_shader_variant
*v
)
162 struct ir3_compiler
*compiler
= v
->shader
->compiler
;
163 struct shader_info
*info
= &v
->shader
->nir
->info
;
164 uint32_t gpu_id
= compiler
->gpu_id
;
167 bin
= ir3_shader_assemble(v
, gpu_id
);
168 sz
= v
->info
.sizedwords
* 4;
170 v
->bo
= fd_bo_new(compiler
->dev
, sz
,
171 DRM_FREEDRENO_GEM_CACHE_WCOMBINE
|
172 DRM_FREEDRENO_GEM_TYPE_KMEM
,
173 "%s:%s", ir3_shader_stage(v
), info
->name
);
175 memcpy(fd_bo_map(v
->bo
), bin
, sz
);
177 if (shader_debug_enabled(v
->shader
->type
)) {
178 fprintf(stdout
, "Native code for unnamed %s shader %s:\n",
179 ir3_shader_stage(v
), v
->shader
->nir
->info
.name
);
180 if (v
->shader
->type
== MESA_SHADER_FRAGMENT
)
181 fprintf(stdout
, "SIMD0\n");
182 ir3_shader_disasm(v
, bin
, stdout
);
187 /* no need to keep the ir around beyond this point: */
193 * For creating normal shader variants, 'nonbinning' is NULL. For
194 * creating binning pass shader, it is link to corresponding normal
195 * (non-binning) variant.
197 static struct ir3_shader_variant
*
198 create_variant(struct ir3_shader
*shader
, struct ir3_shader_key
*key
,
199 struct ir3_shader_variant
*nonbinning
)
201 struct ir3_shader_variant
*v
= CALLOC_STRUCT(ir3_shader_variant
);
207 v
->id
= ++shader
->variant_count
;
209 v
->binning_pass
= !!nonbinning
;
210 v
->nonbinning
= nonbinning
;
212 v
->type
= shader
->type
;
214 ret
= ir3_compile_shader_nir(shader
->compiler
, v
);
216 debug_error("compile failed!");
222 debug_error("assemble failed!");
233 static inline struct ir3_shader_variant
*
234 shader_variant(struct ir3_shader
*shader
, struct ir3_shader_key
*key
,
237 struct ir3_shader_variant
*v
;
241 for (v
= shader
->variants
; v
; v
= v
->next
)
242 if (ir3_shader_key_equal(key
, &v
->key
))
245 /* compile new variant if it doesn't exist already: */
246 v
= create_variant(shader
, key
, NULL
);
248 v
->next
= shader
->variants
;
249 shader
->variants
= v
;
256 struct ir3_shader_variant
*
257 ir3_shader_get_variant(struct ir3_shader
*shader
, struct ir3_shader_key
*key
,
258 bool binning_pass
, bool *created
)
260 mtx_lock(&shader
->variants_lock
);
261 struct ir3_shader_variant
*v
=
262 shader_variant(shader
, key
, created
);
264 if (v
&& binning_pass
) {
266 v
->binning
= create_variant(shader
, key
, v
);
269 mtx_unlock(&shader
->variants_lock
);
272 mtx_unlock(&shader
->variants_lock
);
278 ir3_shader_destroy(struct ir3_shader
*shader
)
280 struct ir3_shader_variant
*v
, *t
;
281 for (v
= shader
->variants
; v
; ) {
286 free(shader
->const_state
.immediates
);
287 ralloc_free(shader
->nir
);
288 mtx_destroy(&shader
->variants_lock
);
293 * Creates a bitmask of the used bits of the shader key by this particular
294 * shader. Used by the gallium driver to skip state-dependent recompiles when
298 ir3_setup_used_key(struct ir3_shader
*shader
)
300 nir_shader
*nir
= shader
->nir
;
301 struct shader_info
*info
= &nir
->info
;
302 struct ir3_shader_key
*key
= &shader
->key_mask
;
304 /* This key flag is just used to make for a cheaper ir3_shader_key_equal
305 * check in the common case.
307 key
->has_per_samp
= true;
309 if (info
->stage
== MESA_SHADER_FRAGMENT
) {
310 key
->fsaturate_s
= ~0;
311 key
->fsaturate_t
= ~0;
312 key
->fsaturate_r
= ~0;
313 key
->fastc_srgb
= ~0;
316 if (info
->inputs_read
& VARYING_BITS_COLOR
) {
317 key
->rasterflat
= true;
318 key
->color_two_side
= true;
321 if ((info
->outputs_written
& ~(FRAG_RESULT_DEPTH
|
322 FRAG_RESULT_STENCIL
|
323 FRAG_RESULT_SAMPLE_MASK
)) != 0) {
324 key
->fclamp_color
= true;
327 /* Only used for deciding on behavior of
328 * nir_intrinsic_load_barycentric_sample
330 key
->msaa
= info
->fs
.uses_sample_qualifier
;
332 key
->tessellation
= ~0;
335 if (info
->outputs_written
& VARYING_BITS_COLOR
)
336 key
->vclamp_color
= true;
338 if (info
->stage
== MESA_SHADER_VERTEX
) {
339 key
->vsaturate_s
= ~0;
340 key
->vsaturate_t
= ~0;
341 key
->vsaturate_r
= ~0;
342 key
->vastc_srgb
= ~0;
349 ir3_shader_from_nir(struct ir3_compiler
*compiler
, nir_shader
*nir
,
350 struct ir3_stream_output_info
*stream_output
)
352 struct ir3_shader
*shader
= CALLOC_STRUCT(ir3_shader
);
354 mtx_init(&shader
->variants_lock
, mtx_plain
);
355 shader
->compiler
= compiler
;
356 shader
->id
= p_atomic_inc_return(&shader
->compiler
->shader_count
);
357 shader
->type
= nir
->info
.stage
;
359 memcpy(&shader
->stream_output
, stream_output
, sizeof(shader
->stream_output
));
361 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
)
362 NIR_PASS_V(nir
, ir3_nir_lower_gs
);
364 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, ir3_glsl_type_size
,
365 (nir_lower_io_options
)0);
367 if (compiler
->gpu_id
>= 600 &&
368 nir
->info
.stage
== MESA_SHADER_FRAGMENT
&&
369 !(ir3_shader_debug
& IR3_DBG_NOFP16
))
370 NIR_PASS_V(nir
, nir_lower_mediump_outputs
);
372 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
373 /* NOTE: lower load_barycentric_at_sample first, since it
374 * produces load_barycentric_at_offset:
376 NIR_PASS_V(nir
, ir3_nir_lower_load_barycentric_at_sample
);
377 NIR_PASS_V(nir
, ir3_nir_lower_load_barycentric_at_offset
);
379 NIR_PASS_V(nir
, ir3_nir_move_varying_inputs
);
382 NIR_PASS_V(nir
, nir_lower_io_arrays_to_elements_no_indirects
, false);
384 NIR_PASS_V(nir
, nir_lower_amul
, ir3_glsl_type_size
);
386 /* do first pass optimization, ignoring the key: */
387 ir3_optimize_nir(shader
, nir
, NULL
);
390 if (ir3_shader_debug
& IR3_DBG_DISASM
) {
391 printf("dump nir%d: type=%d", shader
->id
, shader
->type
);
392 nir_print_shader(shader
->nir
, stdout
);
395 ir3_setup_used_key(shader
);
400 static void dump_reg(FILE *out
, const char *name
, uint32_t r
)
402 if (r
!= regid(63,0)) {
403 const char *reg_type
= (r
& HALF_REG_ID
) ? "hr" : "r";
404 fprintf(out
, "; %s: %s%d.%c\n", name
, reg_type
,
405 (r
& ~HALF_REG_ID
) >> 2, "xyzw"[r
& 0x3]);
409 static void dump_output(FILE *out
, struct ir3_shader_variant
*so
,
410 unsigned slot
, const char *name
)
413 regid
= ir3_find_output_regid(so
, slot
);
414 dump_reg(out
, name
, regid
);
418 input_name(struct ir3_shader_variant
*so
, int i
)
420 if (so
->inputs
[i
].sysval
) {
421 return gl_system_value_name(so
->inputs
[i
].slot
);
422 } else if (so
->type
== MESA_SHADER_VERTEX
) {
423 return gl_vert_attrib_name(so
->inputs
[i
].slot
);
425 return gl_varying_slot_name(so
->inputs
[i
].slot
);
430 output_name(struct ir3_shader_variant
*so
, int i
)
432 if (so
->type
== MESA_SHADER_FRAGMENT
) {
433 return gl_frag_result_name(so
->outputs
[i
].slot
);
435 switch (so
->outputs
[i
].slot
) {
436 case VARYING_SLOT_GS_HEADER_IR3
:
438 case VARYING_SLOT_GS_VERTEX_FLAGS_IR3
:
439 return "GS_VERTEX_FLAGS";
440 case VARYING_SLOT_TCS_HEADER_IR3
:
443 return gl_varying_slot_name(so
->outputs
[i
].slot
);
449 ir3_shader_disasm(struct ir3_shader_variant
*so
, uint32_t *bin
, FILE *out
)
451 struct ir3
*ir
= so
->ir
;
452 struct ir3_register
*reg
;
453 const char *type
= ir3_shader_stage(so
);
457 struct ir3_instruction
*instr
;
458 foreach_input_n (instr
, i
, ir
) {
459 reg
= instr
->regs
[0];
461 fprintf(out
, "@in(%sr%d.%c)\tin%d",
462 (reg
->flags
& IR3_REG_HALF
) ? "h" : "",
463 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
465 if (reg
->wrmask
> 0x1)
466 fprintf(out
, " (wrmask=0x%x)", reg
->wrmask
);
470 /* print pre-dispatch texture fetches: */
471 for (i
= 0; i
< so
->num_sampler_prefetch
; i
++) {
472 const struct ir3_sampler_prefetch
*fetch
= &so
->sampler_prefetch
[i
];
473 fprintf(out
, "@tex(%sr%d.%c)\tsrc=%u, samp=%u, tex=%u, wrmask=0x%x, cmd=%u\n",
474 fetch
->half_precision
? "h" : "",
475 fetch
->dst
>> 2, "xyzw"[fetch
->dst
& 0x3],
476 fetch
->src
, fetch
->samp_id
, fetch
->tex_id
,
477 fetch
->wrmask
, fetch
->cmd
);
480 foreach_output_n (instr
, i
, ir
) {
481 reg
= instr
->regs
[0];
483 fprintf(out
, "@out(%sr%d.%c)\tout%d",
484 (reg
->flags
& IR3_REG_HALF
) ? "h" : "",
485 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
486 if (reg
->wrmask
> 0x1)
487 fprintf(out
, " (wrmask=0x%x)", reg
->wrmask
);
491 struct ir3_const_state
*const_state
= &so
->shader
->const_state
;
492 for (i
= 0; i
< const_state
->immediates_count
; i
++) {
493 fprintf(out
, "@const(c%d.x)\t", const_state
->offsets
.immediate
+ i
);
494 fprintf(out
, "0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
495 const_state
->immediates
[i
].val
[0],
496 const_state
->immediates
[i
].val
[1],
497 const_state
->immediates
[i
].val
[2],
498 const_state
->immediates
[i
].val
[3]);
501 disasm_a3xx(bin
, so
->info
.sizedwords
, 0, out
, ir
->compiler
->gpu_id
);
503 fprintf(out
, "; %s: outputs:", type
);
504 for (i
= 0; i
< so
->outputs_count
; i
++) {
505 uint8_t regid
= so
->outputs
[i
].regid
;
506 const char *reg_type
= so
->outputs
[i
].half
? "hr" : "r";
507 fprintf(out
, " %s%d.%c (%s)",
508 reg_type
, (regid
>> 2), "xyzw"[regid
& 0x3],
513 fprintf(out
, "; %s: inputs:", type
);
514 for (i
= 0; i
< so
->inputs_count
; i
++) {
515 uint8_t regid
= so
->inputs
[i
].regid
;
516 fprintf(out
, " r%d.%c (%s slot=%d cm=%x,il=%u,b=%u)",
517 (regid
>> 2), "xyzw"[regid
& 0x3],
520 so
->inputs
[i
].compmask
,
526 /* print generic shader info: */
527 fprintf(out
, "; %s prog %d/%d: %u instr, %u nops, %u non-nops, %u mov, %u cov, %u dwords\n",
528 type
, so
->shader
->id
, so
->id
,
529 so
->info
.instrs_count
,
531 so
->info
.instrs_count
- so
->info
.nops_count
,
532 so
->info
.mov_count
, so
->info
.cov_count
,
533 so
->info
.sizedwords
);
535 fprintf(out
, "; %s prog %d/%d: %u last-baryf, %d half, %d full, %u constlen\n",
536 type
, so
->shader
->id
, so
->id
,
538 so
->info
.max_half_reg
+ 1,
539 so
->info
.max_reg
+ 1,
542 fprintf(out
, "; %s prog %d/%d: %u sstall, %u (ss), %u (sy), %d max_sun, %d loops\n",
543 type
, so
->shader
->id
, so
->id
,
550 /* print shader type specific info: */
552 case MESA_SHADER_VERTEX
:
553 dump_output(out
, so
, VARYING_SLOT_POS
, "pos");
554 dump_output(out
, so
, VARYING_SLOT_PSIZ
, "psize");
556 case MESA_SHADER_FRAGMENT
:
557 dump_reg(out
, "pos (ij_pixel)",
558 ir3_find_sysval_regid(so
, SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL
));
559 dump_reg(out
, "pos (ij_centroid)",
560 ir3_find_sysval_regid(so
, SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID
));
561 dump_reg(out
, "pos (ij_size)",
562 ir3_find_sysval_regid(so
, SYSTEM_VALUE_BARYCENTRIC_PERSP_SIZE
));
563 dump_output(out
, so
, FRAG_RESULT_DEPTH
, "posz");
564 if (so
->color0_mrt
) {
565 dump_output(out
, so
, FRAG_RESULT_COLOR
, "color");
567 dump_output(out
, so
, FRAG_RESULT_DATA0
, "data0");
568 dump_output(out
, so
, FRAG_RESULT_DATA1
, "data1");
569 dump_output(out
, so
, FRAG_RESULT_DATA2
, "data2");
570 dump_output(out
, so
, FRAG_RESULT_DATA3
, "data3");
571 dump_output(out
, so
, FRAG_RESULT_DATA4
, "data4");
572 dump_output(out
, so
, FRAG_RESULT_DATA5
, "data5");
573 dump_output(out
, so
, FRAG_RESULT_DATA6
, "data6");
574 dump_output(out
, so
, FRAG_RESULT_DATA7
, "data7");
576 dump_reg(out
, "fragcoord",
577 ir3_find_sysval_regid(so
, SYSTEM_VALUE_FRAG_COORD
));
578 dump_reg(out
, "fragface",
579 ir3_find_sysval_regid(so
, SYSTEM_VALUE_FRONT_FACE
));
590 ir3_shader_outputs(const struct ir3_shader
*so
)
592 return so
->nir
->info
.outputs_written
;