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);
44 /* for vertex shader, the inputs are loaded into registers before the shader
45 * is executed, so max_regs from the shader instructions might not properly
46 * reflect the # of registers actually used, especially in case passthrough
49 * Likewise, for fragment shader, we can have some regs which are passed
50 * input values but never touched by the resulting shader (ie. as result
51 * of dead code elimination or simply because we don't know how to turn
55 fixup_regfootprint(struct ir3_shader_variant
*v
)
59 for (i
= 0; i
< v
->inputs_count
; i
++) {
60 /* skip frag inputs fetch via bary.f since their reg's are
61 * not written by gpu before shader starts (and in fact the
62 * regid's might not even be valid)
64 if (v
->inputs
[i
].bary
)
67 /* ignore high regs that are global to all threads in a warp
68 * (they exist by default) (a5xx+)
70 if (v
->inputs
[i
].regid
>= regid(48,0))
73 if (v
->inputs
[i
].compmask
) {
74 unsigned n
= util_last_bit(v
->inputs
[i
].compmask
) - 1;
75 int32_t regid
= v
->inputs
[i
].regid
+ n
;
76 if (v
->inputs
[i
].half
) {
78 v
->info
.max_half_reg
= MAX2(v
->info
.max_half_reg
, regid
>> 2);
80 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 3);
83 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 2);
88 for (i
= 0; i
< v
->outputs_count
; i
++) {
89 /* for ex, VS shaders with tess don't have normal varying outs: */
90 if (!VALIDREG(v
->outputs
[i
].regid
))
92 int32_t regid
= v
->outputs
[i
].regid
+ 3;
93 if (v
->outputs
[i
].half
) {
95 v
->info
.max_half_reg
= MAX2(v
->info
.max_half_reg
, regid
>> 2);
97 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 3);
100 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 2);
104 for (i
= 0; i
< v
->num_sampler_prefetch
; i
++) {
105 unsigned n
= util_last_bit(v
->sampler_prefetch
[i
].wrmask
) - 1;
106 int32_t regid
= v
->sampler_prefetch
[i
].dst
+ n
;
107 if (v
->sampler_prefetch
[i
].half_precision
) {
108 if (!v
->mergedregs
) {
109 v
->info
.max_half_reg
= MAX2(v
->info
.max_half_reg
, regid
>> 2);
111 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 3);
114 v
->info
.max_reg
= MAX2(v
->info
.max_reg
, regid
>> 2);
119 /* wrapper for ir3_assemble() which does some info fixup based on
120 * shader state. Non-static since used by ir3_cmdline too.
122 void * ir3_shader_assemble(struct ir3_shader_variant
*v
)
124 unsigned gpu_id
= v
->shader
->compiler
->gpu_id
;
127 bin
= ir3_assemble(v
);
132 v
->instrlen
= v
->info
.sizedwords
/ (2 * 16);
134 v
->instrlen
= v
->info
.sizedwords
/ (2 * 4);
137 /* NOTE: if relative addressing is used, we set constlen in
138 * the compiler (to worst-case value) since we don't know in
139 * the assembler what the max addr reg value can be:
141 v
->constlen
= MAX2(v
->constlen
, v
->info
.max_const
+ 1);
143 fixup_regfootprint(v
);
149 assemble_variant(struct ir3_shader_variant
*v
)
151 v
->bin
= ir3_shader_assemble(v
);
153 if (shader_debug_enabled(v
->shader
->type
)) {
154 fprintf(stdout
, "Native code for unnamed %s shader %s:\n",
155 ir3_shader_stage(v
), v
->shader
->nir
->info
.name
);
156 if (v
->shader
->type
== MESA_SHADER_FRAGMENT
)
157 fprintf(stdout
, "SIMD0\n");
158 ir3_shader_disasm(v
, v
->bin
, stdout
);
161 /* no need to keep the ir around beyond this point: */
167 * For creating normal shader variants, 'nonbinning' is NULL. For
168 * creating binning pass shader, it is link to corresponding normal
169 * (non-binning) variant.
171 static struct ir3_shader_variant
*
172 create_variant(struct ir3_shader
*shader
, const struct ir3_shader_key
*key
,
173 struct ir3_shader_variant
*nonbinning
)
175 struct ir3_shader_variant
*v
= rzalloc_size(shader
, sizeof(*v
));
181 v
->id
= ++shader
->variant_count
;
183 v
->binning_pass
= !!nonbinning
;
184 v
->nonbinning
= nonbinning
;
186 v
->type
= shader
->type
;
187 v
->mergedregs
= shader
->compiler
->gpu_id
>= 600;
189 if (!v
->binning_pass
)
190 v
->const_state
= rzalloc_size(v
, sizeof(*v
->const_state
));
192 ret
= ir3_compile_shader_nir(shader
->compiler
, v
);
194 debug_error("compile failed!");
200 debug_error("assemble failed!");
211 static inline struct ir3_shader_variant
*
212 shader_variant(struct ir3_shader
*shader
, const struct ir3_shader_key
*key
,
215 struct ir3_shader_variant
*v
;
219 for (v
= shader
->variants
; v
; v
= v
->next
)
220 if (ir3_shader_key_equal(key
, &v
->key
))
223 /* compile new variant if it doesn't exist already: */
224 v
= create_variant(shader
, key
, NULL
);
226 v
->next
= shader
->variants
;
227 shader
->variants
= v
;
234 struct ir3_shader_variant
*
235 ir3_shader_get_variant(struct ir3_shader
*shader
, const struct ir3_shader_key
*key
,
236 bool binning_pass
, bool *created
)
238 mtx_lock(&shader
->variants_lock
);
239 struct ir3_shader_variant
*v
=
240 shader_variant(shader
, key
, created
);
242 if (v
&& binning_pass
) {
244 v
->binning
= create_variant(shader
, key
, v
);
247 mtx_unlock(&shader
->variants_lock
);
250 mtx_unlock(&shader
->variants_lock
);
256 ir3_shader_destroy(struct ir3_shader
*shader
)
258 ralloc_free(shader
->nir
);
259 mtx_destroy(&shader
->variants_lock
);
264 * Creates a bitmask of the used bits of the shader key by this particular
265 * shader. Used by the gallium driver to skip state-dependent recompiles when
269 ir3_setup_used_key(struct ir3_shader
*shader
)
271 nir_shader
*nir
= shader
->nir
;
272 struct shader_info
*info
= &nir
->info
;
273 struct ir3_shader_key
*key
= &shader
->key_mask
;
275 /* This key flag is just used to make for a cheaper ir3_shader_key_equal
276 * check in the common case.
278 key
->has_per_samp
= true;
280 if (info
->stage
== MESA_SHADER_FRAGMENT
) {
281 key
->fsaturate_s
= ~0;
282 key
->fsaturate_t
= ~0;
283 key
->fsaturate_r
= ~0;
284 key
->fastc_srgb
= ~0;
287 if (info
->inputs_read
& VARYING_BITS_COLOR
) {
288 key
->rasterflat
= true;
289 key
->color_two_side
= true;
292 if ((info
->outputs_written
& ~(FRAG_RESULT_DEPTH
|
293 FRAG_RESULT_STENCIL
|
294 FRAG_RESULT_SAMPLE_MASK
)) != 0) {
295 key
->fclamp_color
= true;
298 /* Only used for deciding on behavior of
299 * nir_intrinsic_load_barycentric_sample
301 key
->msaa
= info
->fs
.uses_sample_qualifier
;
303 key
->tessellation
= ~0;
306 if (info
->outputs_written
& VARYING_BITS_COLOR
)
307 key
->vclamp_color
= true;
309 if (info
->stage
== MESA_SHADER_VERTEX
) {
310 key
->vsaturate_s
= ~0;
311 key
->vsaturate_t
= ~0;
312 key
->vsaturate_r
= ~0;
313 key
->vastc_srgb
= ~0;
320 ir3_shader_from_nir(struct ir3_compiler
*compiler
, nir_shader
*nir
,
321 unsigned reserved_user_consts
, struct ir3_stream_output_info
*stream_output
)
323 struct ir3_shader
*shader
= rzalloc_size(NULL
, sizeof(*shader
));
325 mtx_init(&shader
->variants_lock
, mtx_plain
);
326 shader
->compiler
= compiler
;
327 shader
->id
= p_atomic_inc_return(&shader
->compiler
->shader_count
);
328 shader
->type
= nir
->info
.stage
;
330 memcpy(&shader
->stream_output
, stream_output
, sizeof(shader
->stream_output
));
331 shader
->num_reserved_user_consts
= reserved_user_consts
;
333 if (nir
->info
.stage
== MESA_SHADER_GEOMETRY
)
334 NIR_PASS_V(nir
, ir3_nir_lower_gs
);
336 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, ir3_glsl_type_size
,
337 (nir_lower_io_options
)0);
339 if (compiler
->gpu_id
>= 600 &&
340 nir
->info
.stage
== MESA_SHADER_FRAGMENT
&&
341 !(ir3_shader_debug
& IR3_DBG_NOFP16
))
342 NIR_PASS_V(nir
, nir_lower_mediump_outputs
);
344 if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
345 /* NOTE: lower load_barycentric_at_sample first, since it
346 * produces load_barycentric_at_offset:
348 NIR_PASS_V(nir
, ir3_nir_lower_load_barycentric_at_sample
);
349 NIR_PASS_V(nir
, ir3_nir_lower_load_barycentric_at_offset
);
351 NIR_PASS_V(nir
, ir3_nir_move_varying_inputs
);
354 NIR_PASS_V(nir
, nir_lower_io_arrays_to_elements_no_indirects
, false);
356 NIR_PASS_V(nir
, nir_lower_amul
, ir3_glsl_type_size
);
358 /* do first pass optimization, ignoring the key: */
359 ir3_optimize_nir(shader
, nir
);
362 if (ir3_shader_debug
& IR3_DBG_DISASM
) {
363 printf("dump nir%d: type=%d", shader
->id
, shader
->type
);
364 nir_print_shader(shader
->nir
, stdout
);
367 ir3_setup_used_key(shader
);
372 static void dump_reg(FILE *out
, const char *name
, uint32_t r
)
374 if (r
!= regid(63,0)) {
375 const char *reg_type
= (r
& HALF_REG_ID
) ? "hr" : "r";
376 fprintf(out
, "; %s: %s%d.%c\n", name
, reg_type
,
377 (r
& ~HALF_REG_ID
) >> 2, "xyzw"[r
& 0x3]);
381 static void dump_output(FILE *out
, struct ir3_shader_variant
*so
,
382 unsigned slot
, const char *name
)
385 regid
= ir3_find_output_regid(so
, slot
);
386 dump_reg(out
, name
, regid
);
390 input_name(struct ir3_shader_variant
*so
, int i
)
392 if (so
->inputs
[i
].sysval
) {
393 return gl_system_value_name(so
->inputs
[i
].slot
);
394 } else if (so
->type
== MESA_SHADER_VERTEX
) {
395 return gl_vert_attrib_name(so
->inputs
[i
].slot
);
397 return gl_varying_slot_name(so
->inputs
[i
].slot
);
402 output_name(struct ir3_shader_variant
*so
, int i
)
404 if (so
->type
== MESA_SHADER_FRAGMENT
) {
405 return gl_frag_result_name(so
->outputs
[i
].slot
);
407 switch (so
->outputs
[i
].slot
) {
408 case VARYING_SLOT_GS_HEADER_IR3
:
410 case VARYING_SLOT_GS_VERTEX_FLAGS_IR3
:
411 return "GS_VERTEX_FLAGS";
412 case VARYING_SLOT_TCS_HEADER_IR3
:
415 return gl_varying_slot_name(so
->outputs
[i
].slot
);
421 ir3_shader_disasm(struct ir3_shader_variant
*so
, uint32_t *bin
, FILE *out
)
423 struct ir3
*ir
= so
->ir
;
424 struct ir3_register
*reg
;
425 const char *type
= ir3_shader_stage(so
);
429 foreach_input_n (instr
, i
, ir
) {
430 reg
= instr
->regs
[0];
432 fprintf(out
, "@in(%sr%d.%c)\tin%d",
433 (reg
->flags
& IR3_REG_HALF
) ? "h" : "",
434 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
436 if (reg
->wrmask
> 0x1)
437 fprintf(out
, " (wrmask=0x%x)", reg
->wrmask
);
441 /* print pre-dispatch texture fetches: */
442 for (i
= 0; i
< so
->num_sampler_prefetch
; i
++) {
443 const struct ir3_sampler_prefetch
*fetch
= &so
->sampler_prefetch
[i
];
444 fprintf(out
, "@tex(%sr%d.%c)\tsrc=%u, samp=%u, tex=%u, wrmask=0x%x, cmd=%u\n",
445 fetch
->half_precision
? "h" : "",
446 fetch
->dst
>> 2, "xyzw"[fetch
->dst
& 0x3],
447 fetch
->src
, fetch
->samp_id
, fetch
->tex_id
,
448 fetch
->wrmask
, fetch
->cmd
);
451 foreach_output_n (instr
, i
, ir
) {
452 reg
= instr
->regs
[0];
454 fprintf(out
, "@out(%sr%d.%c)\tout%d",
455 (reg
->flags
& IR3_REG_HALF
) ? "h" : "",
456 (regid
>> 2), "xyzw"[regid
& 0x3], i
);
457 if (reg
->wrmask
> 0x1)
458 fprintf(out
, " (wrmask=0x%x)", reg
->wrmask
);
462 const struct ir3_const_state
*const_state
= ir3_const_state(so
);
463 for (i
= 0; i
< const_state
->immediates_count
; i
++) {
464 fprintf(out
, "@const(c%d.x)\t", const_state
->offsets
.immediate
+ i
);
465 fprintf(out
, "0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
466 const_state
->immediates
[i
].val
[0],
467 const_state
->immediates
[i
].val
[1],
468 const_state
->immediates
[i
].val
[2],
469 const_state
->immediates
[i
].val
[3]);
472 disasm_a3xx(bin
, so
->info
.sizedwords
, 0, out
, ir
->compiler
->gpu_id
);
474 fprintf(out
, "; %s: outputs:", type
);
475 for (i
= 0; i
< so
->outputs_count
; i
++) {
476 uint8_t regid
= so
->outputs
[i
].regid
;
477 const char *reg_type
= so
->outputs
[i
].half
? "hr" : "r";
478 fprintf(out
, " %s%d.%c (%s)",
479 reg_type
, (regid
>> 2), "xyzw"[regid
& 0x3],
484 fprintf(out
, "; %s: inputs:", type
);
485 for (i
= 0; i
< so
->inputs_count
; i
++) {
486 uint8_t regid
= so
->inputs
[i
].regid
;
487 fprintf(out
, " r%d.%c (%s slot=%d cm=%x,il=%u,b=%u)",
488 (regid
>> 2), "xyzw"[regid
& 0x3],
491 so
->inputs
[i
].compmask
,
497 /* print generic shader info: */
498 fprintf(out
, "; %s prog %d/%d: %u instr, %u nops, %u non-nops, %u mov, %u cov, %u dwords\n",
499 type
, so
->shader
->id
, so
->id
,
500 so
->info
.instrs_count
,
502 so
->info
.instrs_count
- so
->info
.nops_count
,
503 so
->info
.mov_count
, so
->info
.cov_count
,
504 so
->info
.sizedwords
);
506 fprintf(out
, "; %s prog %d/%d: %u last-baryf, %d half, %d full, %u constlen\n",
507 type
, so
->shader
->id
, so
->id
,
509 so
->info
.max_half_reg
+ 1,
510 so
->info
.max_reg
+ 1,
513 fprintf(out
, "; %s prog %d/%d: %u sstall, %u (ss), %u (sy), %d max_sun, %d loops\n",
514 type
, so
->shader
->id
, so
->id
,
521 /* print shader type specific info: */
523 case MESA_SHADER_VERTEX
:
524 dump_output(out
, so
, VARYING_SLOT_POS
, "pos");
525 dump_output(out
, so
, VARYING_SLOT_PSIZ
, "psize");
527 case MESA_SHADER_FRAGMENT
:
528 dump_reg(out
, "pos (ij_pixel)",
529 ir3_find_sysval_regid(so
, SYSTEM_VALUE_BARYCENTRIC_PERSP_PIXEL
));
530 dump_reg(out
, "pos (ij_centroid)",
531 ir3_find_sysval_regid(so
, SYSTEM_VALUE_BARYCENTRIC_PERSP_CENTROID
));
532 dump_reg(out
, "pos (ij_size)",
533 ir3_find_sysval_regid(so
, SYSTEM_VALUE_BARYCENTRIC_PERSP_SIZE
));
534 dump_output(out
, so
, FRAG_RESULT_DEPTH
, "posz");
535 if (so
->color0_mrt
) {
536 dump_output(out
, so
, FRAG_RESULT_COLOR
, "color");
538 dump_output(out
, so
, FRAG_RESULT_DATA0
, "data0");
539 dump_output(out
, so
, FRAG_RESULT_DATA1
, "data1");
540 dump_output(out
, so
, FRAG_RESULT_DATA2
, "data2");
541 dump_output(out
, so
, FRAG_RESULT_DATA3
, "data3");
542 dump_output(out
, so
, FRAG_RESULT_DATA4
, "data4");
543 dump_output(out
, so
, FRAG_RESULT_DATA5
, "data5");
544 dump_output(out
, so
, FRAG_RESULT_DATA6
, "data6");
545 dump_output(out
, so
, FRAG_RESULT_DATA7
, "data7");
547 dump_reg(out
, "fragcoord",
548 ir3_find_sysval_regid(so
, SYSTEM_VALUE_FRAG_COORD
));
549 dump_reg(out
, "fragface",
550 ir3_find_sysval_regid(so
, SYSTEM_VALUE_FRONT_FACE
));
561 ir3_shader_outputs(const struct ir3_shader
*so
)
563 return so
->nir
->info
.outputs_written
;