2 * Copyright (c) 2012-2019 Etnaviv Project
3 * Copyright (c) 2019 Zodiac Inflight Innovations
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Jonathan Marek <jonathan@marek.ca>
26 * Wladimir J. van der Laan <laanwj@gmail.com>
29 #include "etnaviv_compiler.h"
30 #include "etnaviv_asm.h"
31 #include "etnaviv_context.h"
32 #include "etnaviv_debug.h"
33 #include "etnaviv_disasm.h"
34 #include "etnaviv_uniforms.h"
35 #include "etnaviv_util.h"
38 #include "util/u_memory.h"
39 #include "util/register_allocate.h"
40 #include "compiler/nir/nir_builder.h"
41 #include "compiler/nir/nir_worklist.h"
43 #include "tgsi/tgsi_strings.h"
44 #include "util/u_half.h"
48 #define is_fs(c) ((c)->nir->info.stage == MESA_SHADER_FRAGMENT)
49 const struct etna_specs
*specs
;
50 struct etna_shader_variant
*variant
;
52 /* register assigned to each output, indexed by driver_location */
53 unsigned output_reg
[ETNA_NUM_INPUTS
];
55 /* block # to instr index */
59 int inst_ptr
; /* current instruction pointer */
60 struct etna_inst code
[ETNA_MAX_INSTRUCTIONS
* ETNA_INST_SIZE
];
62 /* There was an error during compilation */
66 #define compile_error(ctx, args...) ({ \
72 /* io related lowering
73 * run after lower_int_to_float because it adds i2f/f2i ops
76 etna_lower_io(nir_shader
*shader
, struct etna_shader_variant
*v
)
78 bool rb_swap
= shader
->info
.stage
== MESA_SHADER_FRAGMENT
&& v
->key
.frag_rb_swap
;
80 unsigned color_location
= 0;
81 nir_foreach_variable(var
, &shader
->outputs
) {
82 switch (var
->data
.location
) {
83 case FRAG_RESULT_COLOR
:
84 case FRAG_RESULT_DATA0
:
85 color_location
= var
->data
.driver_location
;
90 nir_foreach_function(function
, shader
) {
92 nir_builder_init(&b
, function
->impl
);
94 nir_foreach_block(block
, function
->impl
) {
95 nir_foreach_instr_safe(instr
, block
) {
96 if (instr
->type
== nir_instr_type_intrinsic
) {
97 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(instr
);
99 switch (intr
->intrinsic
) {
100 case nir_intrinsic_load_front_face
: {
101 /* front face inverted (run after int_to_float, so invert as float) */
102 b
.cursor
= nir_after_instr(instr
);
104 nir_ssa_def
*ssa
= nir_seq(&b
, &intr
->dest
.ssa
, nir_imm_float(&b
, 0.0));
105 nir_ssa_def_rewrite_uses_after(&intr
->dest
.ssa
,
106 nir_src_for_ssa(ssa
),
109 case nir_intrinsic_store_output
: {
110 if (!rb_swap
|| nir_intrinsic_base(intr
) != color_location
)
112 b
.cursor
= nir_before_instr(instr
);
114 nir_ssa_def
*ssa
= nir_mov(&b
, intr
->src
[0].ssa
);
115 nir_alu_instr
*alu
= nir_instr_as_alu(ssa
->parent_instr
);
116 alu
->src
[0].swizzle
[0] = 2;
117 alu
->src
[0].swizzle
[2] = 0;
118 nir_instr_rewrite_src(instr
, &intr
->src
[0], nir_src_for_ssa(ssa
));
120 case nir_intrinsic_load_instance_id
: {
121 b
.cursor
= nir_after_instr(instr
);
122 nir_ssa_def
*ssa
= nir_i2f32(&b
, &intr
->dest
.ssa
);
123 nir_ssa_def_rewrite_uses_after(&intr
->dest
.ssa
,
124 nir_src_for_ssa(ssa
),
127 case nir_intrinsic_load_uniform
: {
128 /* multiply by 16 and convert to int */
129 b
.cursor
= nir_before_instr(instr
);
130 nir_ssa_def
*ssa
= nir_f2u32(&b
, nir_fmul(&b
, intr
->src
[0].ssa
,
131 nir_imm_float(&b
, 16.0f
)));
132 nir_instr_rewrite_src(instr
, &intr
->src
[0], nir_src_for_ssa(ssa
));
139 if (instr
->type
!= nir_instr_type_tex
)
142 nir_tex_instr
*tex
= nir_instr_as_tex(instr
);
143 nir_src
*coord
= NULL
;
144 nir_src
*lod_bias
= NULL
;
145 unsigned lod_bias_idx
;
147 assert(tex
->sampler_index
== tex
->texture_index
);
149 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
150 switch (tex
->src
[i
].src_type
) {
151 case nir_tex_src_coord
:
152 coord
= &tex
->src
[i
].src
;
154 case nir_tex_src_bias
:
155 case nir_tex_src_lod
:
157 lod_bias
= &tex
->src
[i
].src
;
166 if (tex
->sampler_dim
== GLSL_SAMPLER_DIM_RECT
) {
167 /* use a dummy load_uniform here to represent texcoord scale */
168 b
.cursor
= nir_before_instr(instr
);
169 nir_intrinsic_instr
*load
=
170 nir_intrinsic_instr_create(b
.shader
, nir_intrinsic_load_uniform
);
171 nir_intrinsic_set_base(load
, ~tex
->sampler_index
);
172 load
->num_components
= 2;
173 load
->src
[0] = nir_src_for_ssa(nir_imm_float(&b
, 0.0f
));
174 nir_ssa_dest_init(&load
->instr
, &load
->dest
, 2, 32, NULL
);
175 nir_intrinsic_set_type(load
, nir_type_float
);
177 nir_builder_instr_insert(&b
, &load
->instr
);
179 nir_ssa_def
*new_coord
= nir_fmul(&b
, coord
->ssa
, &load
->dest
.ssa
);
180 nir_instr_rewrite_src(&tex
->instr
, coord
, nir_src_for_ssa(new_coord
));
183 /* pre HALTI5 needs texture sources in a single source */
185 if (!lod_bias
|| v
->shader
->specs
->halti
>= 5)
188 assert(coord
&& lod_bias
&& tex
->coord_components
< 4);
190 nir_alu_instr
*vec
= nir_alu_instr_create(shader
, nir_op_vec4
);
191 for (unsigned i
= 0; i
< tex
->coord_components
; i
++) {
192 vec
->src
[i
].src
= nir_src_for_ssa(coord
->ssa
);
193 vec
->src
[i
].swizzle
[0] = i
;
195 for (unsigned i
= tex
->coord_components
; i
< 4; i
++)
196 vec
->src
[i
].src
= nir_src_for_ssa(lod_bias
->ssa
);
198 vec
->dest
.write_mask
= 0xf;
199 nir_ssa_dest_init(&vec
->instr
, &vec
->dest
.dest
, 4, 32, NULL
);
201 nir_tex_instr_remove_src(tex
, lod_bias_idx
);
202 nir_instr_rewrite_src(&tex
->instr
, coord
, nir_src_for_ssa(&vec
->dest
.dest
.ssa
));
203 tex
->coord_components
= 4;
205 nir_instr_insert_before(&tex
->instr
, &vec
->instr
);
212 etna_lower_alu_to_scalar(nir_shader
*shader
, const struct etna_specs
*specs
)
214 BITSET_DECLARE(scalar_ops
, nir_num_opcodes
);
215 BITSET_ZERO(scalar_ops
);
217 BITSET_SET(scalar_ops
, nir_op_frsq
);
218 BITSET_SET(scalar_ops
, nir_op_frcp
);
219 BITSET_SET(scalar_ops
, nir_op_flog2
);
220 BITSET_SET(scalar_ops
, nir_op_fexp2
);
221 BITSET_SET(scalar_ops
, nir_op_fsqrt
);
222 BITSET_SET(scalar_ops
, nir_op_fcos
);
223 BITSET_SET(scalar_ops
, nir_op_fsin
);
224 BITSET_SET(scalar_ops
, nir_op_fdiv
);
226 if (!specs
->has_halti2_instructions
)
227 BITSET_SET(scalar_ops
, nir_op_fdot2
);
229 nir_lower_alu_to_scalar(shader
, scalar_ops
);
233 etna_lower_alu_impl(nir_function_impl
*impl
, struct etna_compile
*c
)
235 nir_shader
*shader
= impl
->function
->shader
;
238 nir_builder_init(&b
, impl
);
240 /* in a seperate loop so we can apply the multiple-uniform logic to the new fmul */
241 nir_foreach_block(block
, impl
) {
242 nir_foreach_instr_safe(instr
, block
) {
243 if (instr
->type
!= nir_instr_type_alu
)
246 nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
247 /* multiply sin/cos src by constant
248 * TODO: do this earlier (but it breaks const_prop opt)
250 if (alu
->op
== nir_op_fsin
|| alu
->op
== nir_op_fcos
) {
251 b
.cursor
= nir_before_instr(instr
);
253 nir_ssa_def
*imm
= c
->specs
->has_new_transcendentals
?
254 nir_imm_float(&b
, 1.0 / M_PI
) :
255 nir_imm_float(&b
, 2.0 / M_PI
);
257 nir_instr_rewrite_src(instr
, &alu
->src
[0].src
,
258 nir_src_for_ssa(nir_fmul(&b
, alu
->src
[0].src
.ssa
, imm
)));
261 /* change transcendental ops to vec2 and insert vec1 mul for the result
262 * TODO: do this earlier (but it breaks with optimizations)
264 if (c
->specs
->has_new_transcendentals
&& (
265 alu
->op
== nir_op_fdiv
|| alu
->op
== nir_op_flog2
||
266 alu
->op
== nir_op_fsin
|| alu
->op
== nir_op_fcos
)) {
267 nir_ssa_def
*ssa
= &alu
->dest
.dest
.ssa
;
269 assert(ssa
->num_components
== 1);
271 nir_alu_instr
*mul
= nir_alu_instr_create(shader
, nir_op_fmul
);
272 mul
->src
[0].src
= mul
->src
[1].src
= nir_src_for_ssa(ssa
);
273 mul
->src
[1].swizzle
[0] = 1;
275 mul
->dest
.write_mask
= 1;
276 nir_ssa_dest_init(&mul
->instr
, &mul
->dest
.dest
, 1, 32, NULL
);
278 ssa
->num_components
= 2;
280 mul
->dest
.saturate
= alu
->dest
.saturate
;
281 alu
->dest
.saturate
= 0;
283 nir_instr_insert_after(instr
, &mul
->instr
);
285 nir_ssa_def_rewrite_uses_after(ssa
, nir_src_for_ssa(&mul
->dest
.dest
.ssa
), &mul
->instr
);
291 static void etna_lower_alu(nir_shader
*shader
, struct etna_compile
*c
)
293 nir_foreach_function(function
, shader
) {
295 etna_lower_alu_impl(function
->impl
, c
);
300 emit_inst(struct etna_compile
*c
, struct etna_inst
*inst
)
302 c
->code
[c
->inst_ptr
++] = *inst
;
305 /* to map nir srcs should to etna_inst srcs */
307 SRC_0_1_2
= (0 << 0) | (1 << 2) | (2 << 4),
308 SRC_0_1_X
= (0 << 0) | (1 << 2) | (3 << 4),
309 SRC_0_X_X
= (0 << 0) | (3 << 2) | (3 << 4),
310 SRC_0_X_1
= (0 << 0) | (3 << 2) | (1 << 4),
311 SRC_0_1_0
= (0 << 0) | (1 << 2) | (0 << 4),
312 SRC_X_X_0
= (3 << 0) | (3 << 2) | (0 << 4),
313 SRC_0_X_0
= (0 << 0) | (3 << 2) | (0 << 4),
316 /* info to translate a nir op to etna_inst */
317 struct etna_op_info
{
318 uint8_t opcode
; /* INST_OPCODE_ */
319 uint8_t src
; /* SRC_ enum */
320 uint8_t cond
; /* INST_CONDITION_ */
321 uint8_t type
; /* INST_TYPE_ */
324 static const struct etna_op_info etna_ops
[] = {
325 [0 ... nir_num_opcodes
- 1] = {0xff},
328 #define OPCT(nir, op, src, cond, type) [nir_op_##nir] = { \
331 INST_CONDITION_##cond, \
334 #define OPC(nir, op, src, cond) OPCT(nir, op, src, cond, F32)
335 #define OP(nir, op, src) OPC(nir, op, src, TRUE)
336 OP(mov
, MOV
, X_X_0
), OP(fneg
, MOV
, X_X_0
), OP(fabs
, MOV
, X_X_0
), OP(fsat
, MOV
, X_X_0
),
337 OP(fmul
, MUL
, 0_1_X
), OP(fadd
, ADD
, 0_X_1
), OP(ffma
, MAD
, 0_1_2
),
338 OP(fdot2
, DP2
, 0_1_X
), OP(fdot3
, DP3
, 0_1_X
), OP(fdot4
, DP4
, 0_1_X
),
339 OPC(fmin
, SELECT
, 0_1_0
, GT
), OPC(fmax
, SELECT
, 0_1_0
, LT
),
340 OP(ffract
, FRC
, X_X_0
), OP(frcp
, RCP
, X_X_0
), OP(frsq
, RSQ
, X_X_0
),
341 OP(fsqrt
, SQRT
, X_X_0
), OP(fsin
, SIN
, X_X_0
), OP(fcos
, COS
, X_X_0
),
342 OP(fsign
, SIGN
, X_X_0
), OP(ffloor
, FLOOR
, X_X_0
), OP(fceil
, CEIL
, X_X_0
),
343 OP(flog2
, LOG
, X_X_0
), OP(fexp2
, EXP
, X_X_0
),
344 OPC(seq
, SET
, 0_1_X
, EQ
), OPC(sne
, SET
, 0_1_X
, NE
), OPC(sge
, SET
, 0_1_X
, GE
), OPC(slt
, SET
, 0_1_X
, LT
),
345 OPC(fcsel
, SELECT
, 0_1_2
, NZ
),
346 OP(fdiv
, DIV
, 0_1_X
),
347 OP(fddx
, DSX
, 0_X_0
), OP(fddy
, DSY
, 0_X_0
),
349 /* integer opcodes */
350 OPCT(i2f32
, I2F
, 0_X_X
, TRUE
, S32
),
351 OPCT(f2u32
, F2I
, 0_X_X
, TRUE
, U32
),
355 etna_emit_block_start(struct etna_compile
*c
, unsigned block
)
357 c
->block_ptr
[block
] = c
->inst_ptr
;
361 etna_emit_alu(struct etna_compile
*c
, nir_op op
, struct etna_inst_dst dst
,
362 struct etna_inst_src src
[3], bool saturate
)
364 struct etna_op_info ei
= etna_ops
[op
];
366 assert(ei
.opcode
!= 0xff);
368 struct etna_inst inst
= {
381 if (c
->specs
->has_new_transcendentals
)
390 /* for these instructions we want src to be in x component
391 * note: on HALTI2+ i2f/f2u are not scalar but we only use them this way currently
393 src
[0].swiz
= inst_swiz_compose(src
[0].swiz
,
394 INST_SWIZ_BROADCAST(ffs(inst
.dst
.write_mask
)-1));
399 for (unsigned j
= 0; j
< 3; j
++) {
400 unsigned i
= ((ei
.src
>> j
*2) & 3);
402 inst
.src
[j
] = src
[i
];
409 etna_emit_tex(struct etna_compile
*c
, nir_texop op
, unsigned texid
, unsigned dst_swiz
,
410 struct etna_inst_dst dst
, struct etna_inst_src coord
,
411 struct etna_inst_src lod_bias
)
413 struct etna_inst inst
= {
415 .tex
.id
= texid
+ (is_fs(c
) ? 0 : c
->specs
->vertex_sampler_offset
),
416 .tex
.swiz
= dst_swiz
,
421 inst
.src
[1] = lod_bias
;
424 case nir_texop_tex
: inst
.opcode
= INST_OPCODE_TEXLD
; break;
425 case nir_texop_txb
: inst
.opcode
= INST_OPCODE_TEXLDB
; break;
426 case nir_texop_txl
: inst
.opcode
= INST_OPCODE_TEXLDL
; break;
435 etna_emit_jump(struct etna_compile
*c
, unsigned block
, struct etna_inst_src condition
)
437 if (!condition
.use
) {
438 emit_inst(c
, &(struct etna_inst
) {.opcode
= INST_OPCODE_BRANCH
, .imm
= block
});
442 struct etna_inst inst
= {
443 .opcode
= INST_OPCODE_BRANCH
,
444 .cond
= INST_CONDITION_NOT
,
445 .type
= INST_TYPE_U32
,
449 inst
.src
[0].swiz
= INST_SWIZ_BROADCAST(inst
.src
[0].swiz
& 3);
454 etna_emit_discard(struct etna_compile
*c
, struct etna_inst_src condition
)
456 if (!condition
.use
) {
457 emit_inst(c
, &(struct etna_inst
) { .opcode
= INST_OPCODE_TEXKILL
});
461 struct etna_inst inst
= {
462 .opcode
= INST_OPCODE_TEXKILL
,
463 .cond
= INST_CONDITION_GZ
,
466 inst
.src
[0].swiz
= INST_SWIZ_BROADCAST(inst
.src
[0].swiz
& 3);
471 etna_emit_output(struct etna_compile
*c
, unsigned index
, struct etna_inst_src src
)
473 c
->output_reg
[index
] = src
.reg
;
477 etna_emit_load_ubo(struct etna_compile
*c
, struct etna_inst_dst dst
,
478 struct etna_inst_src src
, struct etna_inst_src base
)
480 emit_inst(c
, &(struct etna_inst
) {
481 .opcode
= INST_OPCODE_LOAD
,
482 .type
= INST_TYPE_U32
,
489 #define OPT(nir, pass, ...) ({ \
490 bool this_progress = false; \
491 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
494 #define OPT_V(nir, pass, ...) NIR_PASS_V(nir, pass, ##__VA_ARGS__)
497 etna_optimize_loop(nir_shader
*s
)
503 OPT_V(s
, nir_lower_vars_to_ssa
);
504 progress
|= OPT(s
, nir_opt_copy_prop_vars
);
505 progress
|= OPT(s
, nir_copy_prop
);
506 progress
|= OPT(s
, nir_opt_dce
);
507 progress
|= OPT(s
, nir_opt_cse
);
508 progress
|= OPT(s
, nir_opt_peephole_select
, 16, true, true);
509 progress
|= OPT(s
, nir_opt_intrinsics
);
510 progress
|= OPT(s
, nir_opt_algebraic
);
511 progress
|= OPT(s
, nir_opt_constant_folding
);
512 progress
|= OPT(s
, nir_opt_dead_cf
);
513 if (OPT(s
, nir_opt_trivial_continues
)) {
515 /* If nir_opt_trivial_continues makes progress, then we need to clean
516 * things up if we want any hope of nir_opt_if or nir_opt_loop_unroll
519 OPT(s
, nir_copy_prop
);
522 progress
|= OPT(s
, nir_opt_loop_unroll
, nir_var_all
);
523 progress
|= OPT(s
, nir_opt_if
, false);
524 progress
|= OPT(s
, nir_opt_remove_phis
);
525 progress
|= OPT(s
, nir_opt_undef
);
531 etna_glsl_type_size(const struct glsl_type
*type
, bool bindless
)
533 return glsl_count_attribute_slots(type
, false);
537 copy_uniform_state_to_shader(struct etna_shader_variant
*sobj
, uint64_t *consts
, unsigned count
)
539 struct etna_shader_uniform_info
*uinfo
= &sobj
->uniforms
;
541 uinfo
->imm_count
= count
* 4;
542 uinfo
->imm_data
= MALLOC(uinfo
->imm_count
* sizeof(*uinfo
->imm_data
));
543 uinfo
->imm_contents
= MALLOC(uinfo
->imm_count
* sizeof(*uinfo
->imm_contents
));
545 for (unsigned i
= 0; i
< uinfo
->imm_count
; i
++) {
546 uinfo
->imm_data
[i
] = consts
[i
];
547 uinfo
->imm_contents
[i
] = consts
[i
] >> 32;
550 etna_set_shader_uniforms_dirty_flags(sobj
);
553 #include "etnaviv_compiler_nir_emit.h"
556 etna_compile_shader_nir(struct etna_shader_variant
*v
)
561 struct etna_compile
*c
= CALLOC_STRUCT(etna_compile
);
566 c
->specs
= v
->shader
->specs
;
567 c
->nir
= nir_shader_clone(NULL
, v
->shader
->nir
);
569 nir_shader
*s
= c
->nir
;
570 const struct etna_specs
*specs
= c
->specs
;
572 v
->stage
= s
->info
.stage
;
573 v
->num_loops
= 0; /* TODO */
574 v
->vs_id_in_reg
= -1;
575 v
->vs_pos_out_reg
= -1;
576 v
->vs_pointsize_out_reg
= -1;
577 v
->ps_color_out_reg
= 0; /* 0 for shader that doesn't write fragcolor.. */
578 v
->ps_depth_out_reg
= -1;
580 /* setup input linking */
581 struct etna_shader_io_file
*sf
= &v
->infile
;
582 if (s
->info
.stage
== MESA_SHADER_VERTEX
) {
583 nir_foreach_variable(var
, &s
->inputs
) {
584 unsigned idx
= var
->data
.driver_location
;
585 sf
->reg
[idx
].reg
= idx
;
586 sf
->reg
[idx
].slot
= var
->data
.location
;
587 sf
->reg
[idx
].num_components
= 4; /* TODO */
588 sf
->num_reg
= MAX2(sf
->num_reg
, idx
+1);
592 nir_foreach_variable(var
, &s
->inputs
) {
593 unsigned idx
= var
->data
.driver_location
;
594 sf
->reg
[idx
].reg
= idx
+ 1;
595 sf
->reg
[idx
].slot
= var
->data
.location
;
596 sf
->reg
[idx
].num_components
= 4; /* TODO */
597 sf
->num_reg
= MAX2(sf
->num_reg
, idx
+1);
600 assert(sf
->num_reg
== count
);
603 NIR_PASS_V(s
, nir_lower_io
, nir_var_all
, etna_glsl_type_size
,
604 (nir_lower_io_options
)0);
606 OPT_V(s
, nir_lower_regs_to_ssa
);
607 OPT_V(s
, nir_lower_vars_to_ssa
);
608 OPT_V(s
, nir_lower_indirect_derefs
, nir_var_all
);
609 OPT_V(s
, nir_lower_tex
, &(struct nir_lower_tex_options
) { .lower_txp
= ~0u });
610 OPT_V(s
, etna_lower_alu_to_scalar
, specs
);
612 etna_optimize_loop(s
);
614 /* use opt_algebraic between int_to_float and boot_to_float because
615 * int_to_float emits ftrunc, and ftrunc lowering generates bool ops
617 OPT_V(s
, nir_lower_int_to_float
);
618 OPT_V(s
, nir_opt_algebraic
);
619 OPT_V(s
, nir_lower_bool_to_float
);
621 /* after int to float because insert i2f for instance_id */
622 OPT_V(s
, etna_lower_io
, v
);
624 etna_optimize_loop(s
);
626 if (DBG_ENABLED(ETNA_DBG_DUMP_SHADERS
))
627 nir_print_shader(s
, stdout
);
629 while( OPT(s
, nir_opt_vectorize
) );
630 OPT_V(s
, etna_lower_alu_to_scalar
, specs
);
632 NIR_PASS_V(s
, nir_remove_dead_variables
, nir_var_function_temp
);
633 NIR_PASS_V(s
, nir_opt_algebraic_late
);
635 NIR_PASS_V(s
, nir_move_vec_src_uses_to_dest
);
636 NIR_PASS_V(s
, nir_copy_prop
);
637 NIR_PASS_V(s
, nir_lower_to_source_mods
, ~nir_lower_int_source_mods
);
638 /* need copy prop after uses_to_dest, and before src mods: see
639 * dEQP-GLES2.functional.shaders.random.all_features.fragment.95
642 NIR_PASS_V(s
, nir_opt_dce
);
644 NIR_PASS_V(s
, etna_lower_alu
, c
);
646 if (DBG_ENABLED(ETNA_DBG_DUMP_SHADERS
))
647 nir_print_shader(s
, stdout
);
649 uint64_t consts
[ETNA_MAX_IMM
] = {};
651 unsigned block_ptr
[nir_shader_get_entrypoint(s
)->num_blocks
];
652 c
->block_ptr
= block_ptr
;
653 struct emit_options options
= {
654 .max_temps
= ETNA_MAX_TEMPS
,
655 .max_consts
= ETNA_MAX_IMM
/ 4,
656 .id_reg
= sf
->num_reg
,
657 .single_const_src
= c
->specs
->halti
< 5,
658 .etna_new_transcendentals
= c
->specs
->has_new_transcendentals
,
664 ASSERTED
bool ok
= emit_shader(c
->nir
, &options
, &v
->num_temps
, &num_consts
);
667 /* empty shader, emit NOP */
669 emit_inst(c
, &(struct etna_inst
) { .opcode
= INST_OPCODE_NOP
});
671 /* assemble instructions, fixing up labels */
672 uint32_t *code
= MALLOC(c
->inst_ptr
* 16 + 1024);
673 for (unsigned i
= 0; i
< c
->inst_ptr
; i
++) {
674 struct etna_inst
*inst
= &c
->code
[i
];
675 if (inst
->opcode
== INST_OPCODE_BRANCH
)
676 inst
->imm
= block_ptr
[inst
->imm
];
678 inst
->halti5
= specs
->halti
>= 5;
679 etna_assemble(&code
[i
* 4], inst
);
682 v
->code_size
= c
->inst_ptr
* 4;
684 v
->needs_icache
= c
->inst_ptr
> specs
->max_instructions
;
686 copy_uniform_state_to_shader(v
, consts
, num_consts
);
688 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
689 v
->input_count_unk8
= 31; /* XXX what is this */
691 nir_foreach_variable(var
, &s
->outputs
) {
692 unsigned reg
= c
->output_reg
[var
->data
.driver_location
];
693 switch (var
->data
.location
) {
694 case FRAG_RESULT_COLOR
:
695 case FRAG_RESULT_DATA0
: /* DATA0 is used by gallium shaders for color */
696 v
->ps_color_out_reg
= reg
;
698 case FRAG_RESULT_DEPTH
:
699 v
->ps_depth_out_reg
= reg
;
702 compile_error(c
, "Unsupported fs output %s\n", gl_frag_result_name(var
->data
.location
));
705 assert(v
->ps_depth_out_reg
<= 0);
706 v
->outfile
.num_reg
= 0;
712 v
->input_count_unk8
= DIV_ROUND_UP(v
->infile
.num_reg
+ 4, 16); /* XXX what is this */
716 nir_foreach_variable(var
, &s
->outputs
) {
717 unsigned native
= c
->output_reg
[var
->data
.driver_location
];
719 if (var
->data
.location
== VARYING_SLOT_POS
) {
720 v
->vs_pos_out_reg
= native
;
724 if (var
->data
.location
== VARYING_SLOT_PSIZ
) {
725 v
->vs_pointsize_out_reg
= native
;
729 sf
->reg
[sf
->num_reg
].reg
= native
;
730 sf
->reg
[sf
->num_reg
].slot
= var
->data
.location
;
731 sf
->reg
[sf
->num_reg
].num_components
= 4; /* TODO */
735 /* fill in "mystery meat" load balancing value. This value determines how
736 * work is scheduled between VS and PS
737 * in the unified shader architecture. More precisely, it is determined from
738 * the number of VS outputs, as well as chip-specific
739 * vertex output buffer size, vertex cache size, and the number of shader
742 * XXX this is a conservative estimate, the "optimal" value is only known for
743 * sure at link time because some
744 * outputs may be unused and thus unmapped. Then again, in the general use
745 * case with GLSL the vertex and fragment
746 * shaders are linked already before submitting to Gallium, thus all outputs
749 * note: TGSI compiler counts all outputs (including position and pointsize), here
750 * v->outfile.num_reg only counts varyings, +1 to compensate for the position output
751 * TODO: might have a problem that we don't count pointsize when it is used
754 int half_out
= v
->outfile
.num_reg
/ 2 + 1;
757 uint32_t b
= ((20480 / (specs
->vertex_output_buffer_size
-
758 2 * half_out
* specs
->vertex_cache_size
)) +
761 uint32_t a
= (b
+ 256 / (specs
->shader_core_count
* half_out
)) / 2;
762 v
->vs_load_balancing
= VIVS_VS_LOAD_BALANCING_A(MIN2(a
, 255)) |
763 VIVS_VS_LOAD_BALANCING_B(MIN2(b
, 255)) |
764 VIVS_VS_LOAD_BALANCING_C(0x3f) |
765 VIVS_VS_LOAD_BALANCING_D(0x0f);
773 etna_destroy_shader_nir(struct etna_shader_variant
*shader
)
778 FREE(shader
->uniforms
.imm_data
);
779 FREE(shader
->uniforms
.imm_contents
);
783 extern const char *tgsi_swizzle_names
[];
785 etna_dump_shader_nir(const struct etna_shader_variant
*shader
)
787 if (shader
->stage
== MESA_SHADER_VERTEX
)
792 etna_disasm(shader
->code
, shader
->code_size
, PRINT_RAW
);
794 printf("num loops: %i\n", shader
->num_loops
);
795 printf("num temps: %i\n", shader
->num_temps
);
796 printf("immediates:\n");
797 for (int idx
= 0; idx
< shader
->uniforms
.imm_count
; ++idx
) {
798 printf(" [%i].%s = %f (0x%08x) (%d)\n",
800 tgsi_swizzle_names
[idx
% 4],
801 *((float *)&shader
->uniforms
.imm_data
[idx
]),
802 shader
->uniforms
.imm_data
[idx
],
803 shader
->uniforms
.imm_contents
[idx
]);
806 for (int idx
= 0; idx
< shader
->infile
.num_reg
; ++idx
) {
807 printf(" [%i] name=%s comps=%i\n", shader
->infile
.reg
[idx
].reg
,
808 (shader
->stage
== MESA_SHADER_VERTEX
) ?
809 gl_vert_attrib_name(shader
->infile
.reg
[idx
].slot
) :
810 gl_varying_slot_name(shader
->infile
.reg
[idx
].slot
),
811 shader
->infile
.reg
[idx
].num_components
);
813 printf("outputs:\n");
814 for (int idx
= 0; idx
< shader
->outfile
.num_reg
; ++idx
) {
815 printf(" [%i] name=%s comps=%i\n", shader
->outfile
.reg
[idx
].reg
,
816 (shader
->stage
== MESA_SHADER_VERTEX
) ?
817 gl_varying_slot_name(shader
->outfile
.reg
[idx
].slot
) :
818 gl_frag_result_name(shader
->outfile
.reg
[idx
].slot
),
819 shader
->outfile
.reg
[idx
].num_components
);
821 printf("special:\n");
822 if (shader
->stage
== MESA_SHADER_VERTEX
) {
823 printf(" vs_pos_out_reg=%i\n", shader
->vs_pos_out_reg
);
824 printf(" vs_pointsize_out_reg=%i\n", shader
->vs_pointsize_out_reg
);
825 printf(" vs_load_balancing=0x%08x\n", shader
->vs_load_balancing
);
827 printf(" ps_color_out_reg=%i\n", shader
->ps_color_out_reg
);
828 printf(" ps_depth_out_reg=%i\n", shader
->ps_depth_out_reg
);
830 printf(" input_count_unk8=0x%08x\n", shader
->input_count_unk8
);
833 static const struct etna_shader_inout
*
834 etna_shader_vs_lookup(const struct etna_shader_variant
*sobj
,
835 const struct etna_shader_inout
*in
)
837 for (int i
= 0; i
< sobj
->outfile
.num_reg
; i
++)
838 if (sobj
->outfile
.reg
[i
].slot
== in
->slot
)
839 return &sobj
->outfile
.reg
[i
];
845 etna_link_shader_nir(struct etna_shader_link_info
*info
,
846 const struct etna_shader_variant
*vs
,
847 const struct etna_shader_variant
*fs
)
850 /* For each fragment input we need to find the associated vertex shader
851 * output, which can be found by matching on semantic name and index. A
852 * binary search could be used because the vs outputs are sorted by their
853 * semantic index and grouped by semantic type by fill_in_vs_outputs.
855 assert(fs
->infile
.num_reg
< ETNA_NUM_INPUTS
);
856 info
->pcoord_varying_comp_ofs
= -1;
858 for (int idx
= 0; idx
< fs
->infile
.num_reg
; ++idx
) {
859 const struct etna_shader_inout
*fsio
= &fs
->infile
.reg
[idx
];
860 const struct etna_shader_inout
*vsio
= etna_shader_vs_lookup(vs
, fsio
);
861 struct etna_varying
*varying
;
862 bool interpolate_always
= true;
864 assert(fsio
->reg
> 0 && fsio
->reg
<= ARRAY_SIZE(info
->varyings
));
866 if (fsio
->reg
> info
->num_varyings
)
867 info
->num_varyings
= fsio
->reg
;
869 varying
= &info
->varyings
[fsio
->reg
- 1];
870 varying
->num_components
= fsio
->num_components
;
872 if (!interpolate_always
) /* colors affected by flat shading */
873 varying
->pa_attributes
= 0x200;
874 else /* texture coord or other bypasses flat shading */
875 varying
->pa_attributes
= 0x2f1;
877 varying
->use
[0] = VARYING_COMPONENT_USE_UNUSED
;
878 varying
->use
[1] = VARYING_COMPONENT_USE_UNUSED
;
879 varying
->use
[2] = VARYING_COMPONENT_USE_UNUSED
;
880 varying
->use
[3] = VARYING_COMPONENT_USE_UNUSED
;
882 /* point coord is an input to the PS without matching VS output,
883 * so it gets a varying slot without being assigned a VS register.
885 if (fsio
->slot
== VARYING_SLOT_PNTC
) {
886 varying
->use
[0] = VARYING_COMPONENT_USE_POINTCOORD_X
;
887 varying
->use
[1] = VARYING_COMPONENT_USE_POINTCOORD_Y
;
889 info
->pcoord_varying_comp_ofs
= comp_ofs
;
891 if (vsio
== NULL
) { /* not found -- link error */
892 BUG("Semantic value not found in vertex shader outputs\n");
895 varying
->reg
= vsio
->reg
;
898 comp_ofs
+= varying
->num_components
;
901 assert(info
->num_varyings
== fs
->infile
.num_reg
);