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_nir.h"
35 #include "etnaviv_uniforms.h"
36 #include "etnaviv_util.h"
39 #include "util/u_memory.h"
40 #include "util/register_allocate.h"
41 #include "compiler/nir/nir_builder.h"
42 #include "compiler/nir/nir_worklist.h"
44 #include "tgsi/tgsi_strings.h"
45 #include "util/u_half.h"
49 #define is_fs(c) ((c)->nir->info.stage == MESA_SHADER_FRAGMENT)
50 const struct etna_specs
*specs
;
51 struct etna_shader_variant
*variant
;
53 /* block # to instr index */
57 int inst_ptr
; /* current instruction pointer */
58 struct etna_inst code
[ETNA_MAX_INSTRUCTIONS
* ETNA_INST_SIZE
];
61 uint64_t consts
[ETNA_MAX_IMM
];
63 /* There was an error during compilation */
70 etna_alu_to_scalar_filter_cb(const nir_instr
*instr
, const void *data
)
72 const struct etna_specs
*specs
= data
;
74 if (instr
->type
!= nir_instr_type_alu
)
77 nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
89 /* TODO: can do better than alu_to_scalar for vector compares */
90 case nir_op_b32all_fequal2
:
91 case nir_op_b32all_fequal3
:
92 case nir_op_b32all_fequal4
:
93 case nir_op_b32any_fnequal2
:
94 case nir_op_b32any_fnequal3
:
95 case nir_op_b32any_fnequal4
:
96 case nir_op_b32all_iequal2
:
97 case nir_op_b32all_iequal3
:
98 case nir_op_b32all_iequal4
:
99 case nir_op_b32any_inequal2
:
100 case nir_op_b32any_inequal3
:
101 case nir_op_b32any_inequal4
:
104 if (!specs
->has_halti2_instructions
)
115 emit_inst(struct etna_compile
*c
, struct etna_inst
*inst
)
117 c
->code
[c
->inst_ptr
++] = *inst
;
120 /* to map nir srcs should to etna_inst srcs */
122 SRC_0_1_2
= (0 << 0) | (1 << 2) | (2 << 4),
123 SRC_0_1_X
= (0 << 0) | (1 << 2) | (3 << 4),
124 SRC_0_X_X
= (0 << 0) | (3 << 2) | (3 << 4),
125 SRC_0_X_1
= (0 << 0) | (3 << 2) | (1 << 4),
126 SRC_0_1_0
= (0 << 0) | (1 << 2) | (0 << 4),
127 SRC_X_X_0
= (3 << 0) | (3 << 2) | (0 << 4),
128 SRC_0_X_0
= (0 << 0) | (3 << 2) | (0 << 4),
131 /* info to translate a nir op to etna_inst */
132 struct etna_op_info
{
133 uint8_t opcode
; /* INST_OPCODE_ */
134 uint8_t src
; /* SRC_ enum */
135 uint8_t cond
; /* INST_CONDITION_ */
136 uint8_t type
; /* INST_TYPE_ */
139 static const struct etna_op_info etna_ops
[] = {
140 [0 ... nir_num_opcodes
- 1] = {0xff},
143 #define OPCT(nir, op, src, cond, type) [nir_op_##nir] = { \
146 INST_CONDITION_##cond, \
149 #define OPC(nir, op, src, cond) OPCT(nir, op, src, cond, F32)
150 #define IOPC(nir, op, src, cond) OPCT(nir, op, src, cond, S32)
151 #define UOPC(nir, op, src, cond) OPCT(nir, op, src, cond, U32)
152 #define OP(nir, op, src) OPC(nir, op, src, TRUE)
153 #define IOP(nir, op, src) IOPC(nir, op, src, TRUE)
154 #define UOP(nir, op, src) UOPC(nir, op, src, TRUE)
155 OP(mov
, MOV
, X_X_0
), OP(fneg
, MOV
, X_X_0
), OP(fabs
, MOV
, X_X_0
), OP(fsat
, MOV
, X_X_0
),
156 OP(fmul
, MUL
, 0_1_X
), OP(fadd
, ADD
, 0_X_1
), OP(ffma
, MAD
, 0_1_2
),
157 OP(fdot2
, DP2
, 0_1_X
), OP(fdot3
, DP3
, 0_1_X
), OP(fdot4
, DP4
, 0_1_X
),
158 OPC(fmin
, SELECT
, 0_1_0
, GT
), OPC(fmax
, SELECT
, 0_1_0
, LT
),
159 OP(ffract
, FRC
, X_X_0
), OP(frcp
, RCP
, X_X_0
), OP(frsq
, RSQ
, X_X_0
),
160 OP(fsqrt
, SQRT
, X_X_0
), OP(fsin
, SIN
, X_X_0
), OP(fcos
, COS
, X_X_0
),
161 OP(fsign
, SIGN
, X_X_0
), OP(ffloor
, FLOOR
, X_X_0
), OP(fceil
, CEIL
, X_X_0
),
162 OP(flog2
, LOG
, X_X_0
), OP(fexp2
, EXP
, X_X_0
),
163 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
),
164 OPC(fcsel
, SELECT
, 0_1_2
, NZ
),
165 OP(fdiv
, DIV
, 0_1_X
),
166 OP(fddx
, DSX
, 0_X_0
), OP(fddy
, DSY
, 0_X_0
),
169 IOP(i2f32
, I2F
, 0_X_X
),
170 UOP(u2f32
, I2F
, 0_X_X
),
171 IOP(f2i32
, F2I
, 0_X_X
),
172 UOP(f2u32
, F2I
, 0_X_X
),
173 UOP(b2f32
, AND
, 0_X_X
), /* AND with fui(1.0f) */
174 UOP(b2i32
, AND
, 0_X_X
), /* AND with 1 */
175 OPC(f2b32
, CMP
, 0_X_X
, NE
), /* != 0.0 */
176 UOPC(i2b32
, CMP
, 0_X_X
, NE
), /* != 0 */
179 IOP(iadd
, ADD
, 0_X_1
),
180 IOP(imul
, IMULLO0
, 0_1_X
),
181 /* IOP(imad, IMADLO0, 0_1_2), */
182 IOP(ineg
, ADD
, X_X_0
), /* ADD 0, -x */
183 IOP(iabs
, IABS
, X_X_0
),
184 IOP(isign
, SIGN
, X_X_0
),
185 IOPC(imin
, SELECT
, 0_1_0
, GT
),
186 IOPC(imax
, SELECT
, 0_1_0
, LT
),
187 UOPC(umin
, SELECT
, 0_1_0
, GT
),
188 UOPC(umax
, SELECT
, 0_1_0
, LT
),
191 UOPC(b32csel
, SELECT
, 0_1_2
, NZ
),
193 /* compare with int result */
194 OPC(feq32
, CMP
, 0_1_X
, EQ
),
195 OPC(fne32
, CMP
, 0_1_X
, NE
),
196 OPC(fge32
, CMP
, 0_1_X
, GE
),
197 OPC(flt32
, CMP
, 0_1_X
, LT
),
198 IOPC(ieq32
, CMP
, 0_1_X
, EQ
),
199 IOPC(ine32
, CMP
, 0_1_X
, NE
),
200 IOPC(ige32
, CMP
, 0_1_X
, GE
),
201 IOPC(ilt32
, CMP
, 0_1_X
, LT
),
202 UOPC(uge32
, CMP
, 0_1_X
, GE
),
203 UOPC(ult32
, CMP
, 0_1_X
, LT
),
207 IOP(iand
, AND
, 0_X_1
),
208 IOP(ixor
, XOR
, 0_X_1
),
209 IOP(inot
, NOT
, X_X_0
),
210 IOP(ishl
, LSHIFT
, 0_X_1
),
211 IOP(ishr
, RSHIFT
, 0_X_1
),
212 UOP(ushr
, RSHIFT
, 0_X_1
),
216 etna_emit_block_start(struct etna_compile
*c
, unsigned block
)
218 c
->block_ptr
[block
] = c
->inst_ptr
;
222 etna_emit_alu(struct etna_compile
*c
, nir_op op
, struct etna_inst_dst dst
,
223 struct etna_inst_src src
[3], bool saturate
)
225 struct etna_op_info ei
= etna_ops
[op
];
226 unsigned swiz_scalar
= INST_SWIZ_BROADCAST(ffs(dst
.write_mask
) - 1);
228 assert(ei
.opcode
!= 0xff);
230 struct etna_inst inst
= {
243 if (c
->specs
->has_new_transcendentals
)
251 /* scalar instructions we want src to be in x component */
252 src
[0].swiz
= inst_swiz_compose(src
[0].swiz
, swiz_scalar
);
253 src
[1].swiz
= inst_swiz_compose(src
[1].swiz
, swiz_scalar
);
255 /* deal with instructions which don't have 1:1 mapping */
257 inst
.src
[2] = etna_immediate_float(1.0f
);
260 inst
.src
[2] = etna_immediate_int(1);
263 inst
.src
[1] = etna_immediate_float(0.0f
);
266 inst
.src
[1] = etna_immediate_int(0);
269 inst
.src
[0] = etna_immediate_int(0);
276 /* set the "true" value for CMP instructions */
277 if (inst
.opcode
== INST_OPCODE_CMP
)
278 inst
.src
[2] = etna_immediate_int(-1);
280 for (unsigned j
= 0; j
< 3; j
++) {
281 unsigned i
= ((ei
.src
>> j
*2) & 3);
283 inst
.src
[j
] = src
[i
];
290 etna_emit_tex(struct etna_compile
*c
, nir_texop op
, unsigned texid
, unsigned dst_swiz
,
291 struct etna_inst_dst dst
, struct etna_inst_src coord
,
292 struct etna_inst_src lod_bias
, struct etna_inst_src compare
)
294 struct etna_inst inst
= {
296 .tex
.id
= texid
+ (is_fs(c
) ? 0 : c
->specs
->vertex_sampler_offset
),
297 .tex
.swiz
= dst_swiz
,
302 inst
.src
[1] = lod_bias
;
305 inst
.src
[2] = compare
;
308 case nir_texop_tex
: inst
.opcode
= INST_OPCODE_TEXLD
; break;
309 case nir_texop_txb
: inst
.opcode
= INST_OPCODE_TEXLDB
; break;
310 case nir_texop_txl
: inst
.opcode
= INST_OPCODE_TEXLDL
; break;
319 etna_emit_jump(struct etna_compile
*c
, unsigned block
, struct etna_inst_src condition
)
321 if (!condition
.use
) {
322 emit_inst(c
, &(struct etna_inst
) {.opcode
= INST_OPCODE_BRANCH
, .imm
= block
});
326 struct etna_inst inst
= {
327 .opcode
= INST_OPCODE_BRANCH
,
328 .cond
= INST_CONDITION_NOT
,
329 .type
= INST_TYPE_U32
,
333 inst
.src
[0].swiz
= INST_SWIZ_BROADCAST(inst
.src
[0].swiz
& 3);
338 etna_emit_discard(struct etna_compile
*c
, struct etna_inst_src condition
)
340 if (!condition
.use
) {
341 emit_inst(c
, &(struct etna_inst
) { .opcode
= INST_OPCODE_TEXKILL
});
345 struct etna_inst inst
= {
346 .opcode
= INST_OPCODE_TEXKILL
,
347 .cond
= INST_CONDITION_NZ
,
348 .type
= (c
->specs
->halti
< 2) ? INST_TYPE_F32
: INST_TYPE_U32
,
351 inst
.src
[0].swiz
= INST_SWIZ_BROADCAST(inst
.src
[0].swiz
& 3);
356 etna_emit_output(struct etna_compile
*c
, nir_variable
*var
, struct etna_inst_src src
)
358 struct etna_shader_io_file
*sf
= &c
->variant
->outfile
;
361 switch (var
->data
.location
) {
362 case FRAG_RESULT_COLOR
:
363 case FRAG_RESULT_DATA0
: /* DATA0 is used by gallium shaders for color */
364 c
->variant
->ps_color_out_reg
= src
.reg
;
366 case FRAG_RESULT_DEPTH
:
367 c
->variant
->ps_depth_out_reg
= src
.reg
;
370 unreachable("Unsupported fs output");
375 switch (var
->data
.location
) {
376 case VARYING_SLOT_POS
:
377 c
->variant
->vs_pos_out_reg
= src
.reg
;
379 case VARYING_SLOT_PSIZ
:
380 c
->variant
->vs_pointsize_out_reg
= src
.reg
;
383 sf
->reg
[sf
->num_reg
].reg
= src
.reg
;
384 sf
->reg
[sf
->num_reg
].slot
= var
->data
.location
;
385 sf
->reg
[sf
->num_reg
].num_components
= glsl_get_components(var
->type
);
391 #define OPT(nir, pass, ...) ({ \
392 bool this_progress = false; \
393 NIR_PASS(this_progress, nir, pass, ##__VA_ARGS__); \
398 etna_optimize_loop(nir_shader
*s
)
404 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
405 progress
|= OPT(s
, nir_opt_copy_prop_vars
);
406 progress
|= OPT(s
, nir_copy_prop
);
407 progress
|= OPT(s
, nir_opt_dce
);
408 progress
|= OPT(s
, nir_opt_cse
);
409 progress
|= OPT(s
, nir_opt_peephole_select
, 16, true, true);
410 progress
|= OPT(s
, nir_opt_intrinsics
);
411 progress
|= OPT(s
, nir_opt_algebraic
);
412 progress
|= OPT(s
, nir_opt_constant_folding
);
413 progress
|= OPT(s
, nir_opt_dead_cf
);
414 if (OPT(s
, nir_opt_trivial_continues
)) {
416 /* If nir_opt_trivial_continues makes progress, then we need to clean
417 * things up if we want any hope of nir_opt_if or nir_opt_loop_unroll
420 OPT(s
, nir_copy_prop
);
423 progress
|= OPT(s
, nir_opt_loop_unroll
, nir_var_all
);
424 progress
|= OPT(s
, nir_opt_if
, false);
425 progress
|= OPT(s
, nir_opt_remove_phis
);
426 progress
|= OPT(s
, nir_opt_undef
);
432 etna_glsl_type_size(const struct glsl_type
*type
, bool bindless
)
434 return glsl_count_attribute_slots(type
, false);
438 copy_uniform_state_to_shader(struct etna_shader_variant
*sobj
, uint64_t *consts
, unsigned count
)
440 struct etna_shader_uniform_info
*uinfo
= &sobj
->uniforms
;
442 uinfo
->imm_count
= count
* 4;
443 uinfo
->imm_data
= MALLOC(uinfo
->imm_count
* sizeof(*uinfo
->imm_data
));
444 uinfo
->imm_contents
= MALLOC(uinfo
->imm_count
* sizeof(*uinfo
->imm_contents
));
446 for (unsigned i
= 0; i
< uinfo
->imm_count
; i
++) {
447 uinfo
->imm_data
[i
] = consts
[i
];
448 uinfo
->imm_contents
[i
] = consts
[i
] >> 32;
451 etna_set_shader_uniforms_dirty_flags(sobj
);
454 #include "etnaviv_compiler_nir_emit.h"
457 etna_compile_check_limits(struct etna_shader_variant
*v
)
459 const struct etna_specs
*specs
= v
->shader
->specs
;
460 int max_uniforms
= (v
->stage
== MESA_SHADER_VERTEX
)
461 ? specs
->max_vs_uniforms
462 : specs
->max_ps_uniforms
;
464 if (!specs
->has_icache
&& v
->needs_icache
) {
465 DBG("Number of instructions (%d) exceeds maximum %d", v
->code_size
/ 4,
466 specs
->max_instructions
);
470 if (v
->num_temps
> specs
->max_registers
) {
471 DBG("Number of registers (%d) exceeds maximum %d", v
->num_temps
,
472 specs
->max_registers
);
476 if (v
->uniforms
.imm_count
/ 4 > max_uniforms
) {
477 DBG("Number of uniforms (%d) exceeds maximum %d",
478 v
->uniforms
.imm_count
/ 4, max_uniforms
);
486 fill_vs_mystery(struct etna_shader_variant
*v
)
488 const struct etna_specs
*specs
= v
->shader
->specs
;
490 v
->input_count_unk8
= DIV_ROUND_UP(v
->infile
.num_reg
+ 4, 16); /* XXX what is this */
492 /* fill in "mystery meat" load balancing value. This value determines how
493 * work is scheduled between VS and PS
494 * in the unified shader architecture. More precisely, it is determined from
495 * the number of VS outputs, as well as chip-specific
496 * vertex output buffer size, vertex cache size, and the number of shader
499 * XXX this is a conservative estimate, the "optimal" value is only known for
500 * sure at link time because some
501 * outputs may be unused and thus unmapped. Then again, in the general use
502 * case with GLSL the vertex and fragment
503 * shaders are linked already before submitting to Gallium, thus all outputs
506 * note: TGSI compiler counts all outputs (including position and pointsize), here
507 * v->outfile.num_reg only counts varyings, +1 to compensate for the position output
508 * TODO: might have a problem that we don't count pointsize when it is used
511 int half_out
= v
->outfile
.num_reg
/ 2 + 1;
514 uint32_t b
= ((20480 / (specs
->vertex_output_buffer_size
-
515 2 * half_out
* specs
->vertex_cache_size
)) +
518 uint32_t a
= (b
+ 256 / (specs
->shader_core_count
* half_out
)) / 2;
519 v
->vs_load_balancing
= VIVS_VS_LOAD_BALANCING_A(MIN2(a
, 255)) |
520 VIVS_VS_LOAD_BALANCING_B(MIN2(b
, 255)) |
521 VIVS_VS_LOAD_BALANCING_C(0x3f) |
522 VIVS_VS_LOAD_BALANCING_D(0x0f);
526 etna_compile_shader_nir(struct etna_shader_variant
*v
)
531 struct etna_compile
*c
= CALLOC_STRUCT(etna_compile
);
536 c
->specs
= v
->shader
->specs
;
537 c
->nir
= nir_shader_clone(NULL
, v
->shader
->nir
);
539 nir_shader
*s
= c
->nir
;
540 const struct etna_specs
*specs
= c
->specs
;
542 v
->stage
= s
->info
.stage
;
543 v
->num_loops
= 0; /* TODO */
544 v
->vs_id_in_reg
= -1;
545 v
->vs_pos_out_reg
= -1;
546 v
->vs_pointsize_out_reg
= -1;
547 v
->ps_color_out_reg
= 0; /* 0 for shader that doesn't write fragcolor.. */
548 v
->ps_depth_out_reg
= -1;
550 /* setup input linking */
551 struct etna_shader_io_file
*sf
= &v
->infile
;
552 if (s
->info
.stage
== MESA_SHADER_VERTEX
) {
553 nir_foreach_variable(var
, &s
->inputs
) {
554 unsigned idx
= var
->data
.driver_location
;
555 sf
->reg
[idx
].reg
= idx
;
556 sf
->reg
[idx
].slot
= var
->data
.location
;
557 sf
->reg
[idx
].num_components
= glsl_get_components(var
->type
);
558 sf
->num_reg
= MAX2(sf
->num_reg
, idx
+1);
562 nir_foreach_variable(var
, &s
->inputs
) {
563 unsigned idx
= var
->data
.driver_location
;
564 sf
->reg
[idx
].reg
= idx
+ 1;
565 sf
->reg
[idx
].slot
= var
->data
.location
;
566 sf
->reg
[idx
].num_components
= glsl_get_components(var
->type
);
567 sf
->num_reg
= MAX2(sf
->num_reg
, idx
+1);
570 assert(sf
->num_reg
== count
);
573 NIR_PASS_V(s
, nir_lower_io
, ~nir_var_shader_out
, etna_glsl_type_size
,
574 (nir_lower_io_options
)0);
576 NIR_PASS_V(s
, nir_lower_regs_to_ssa
);
577 NIR_PASS_V(s
, nir_lower_vars_to_ssa
);
578 NIR_PASS_V(s
, nir_lower_indirect_derefs
, nir_var_all
);
579 NIR_PASS_V(s
, nir_lower_tex
, &(struct nir_lower_tex_options
) { .lower_txp
= ~0u });
580 NIR_PASS_V(s
, nir_lower_alu_to_scalar
, etna_alu_to_scalar_filter_cb
, specs
);
582 etna_optimize_loop(s
);
584 NIR_PASS_V(s
, etna_lower_io
, v
);
586 if (v
->shader
->specs
->vs_need_z_div
)
587 NIR_PASS_V(s
, nir_lower_clip_halfz
);
589 /* lower pre-halti2 to float (halti0 has integers, but only scalar..) */
590 if (c
->specs
->halti
< 2) {
591 /* use opt_algebraic between int_to_float and boot_to_float because
592 * int_to_float emits ftrunc, and ftrunc lowering generates bool ops
594 NIR_PASS_V(s
, nir_lower_int_to_float
);
595 NIR_PASS_V(s
, nir_opt_algebraic
);
596 NIR_PASS_V(s
, nir_lower_bool_to_float
);
598 NIR_PASS_V(s
, nir_lower_idiv
, nir_lower_idiv_fast
);
599 NIR_PASS_V(s
, nir_lower_bool_to_int32
);
602 etna_optimize_loop(s
);
604 if (DBG_ENABLED(ETNA_DBG_DUMP_SHADERS
))
605 nir_print_shader(s
, stdout
);
607 while( OPT(s
, nir_opt_vectorize
) );
608 NIR_PASS_V(s
, nir_lower_alu_to_scalar
, etna_alu_to_scalar_filter_cb
, specs
);
610 NIR_PASS_V(s
, nir_remove_dead_variables
, nir_var_function_temp
, NULL
);
611 NIR_PASS_V(s
, nir_opt_algebraic_late
);
613 NIR_PASS_V(s
, nir_move_vec_src_uses_to_dest
);
614 NIR_PASS_V(s
, nir_copy_prop
);
615 /* only HW supported integer source mod is ineg for iadd instruction (?) */
616 NIR_PASS_V(s
, nir_lower_to_source_mods
, ~nir_lower_int_source_mods
);
617 /* need copy prop after uses_to_dest, and before src mods: see
618 * dEQP-GLES2.functional.shaders.random.all_features.fragment.95
621 NIR_PASS_V(s
, nir_opt_dce
);
623 NIR_PASS_V(s
, etna_lower_alu
, c
->specs
->has_new_transcendentals
);
625 if (DBG_ENABLED(ETNA_DBG_DUMP_SHADERS
))
626 nir_print_shader(s
, stdout
);
628 unsigned block_ptr
[nir_shader_get_entrypoint(s
)->num_blocks
];
629 c
->block_ptr
= block_ptr
;
632 ASSERTED
bool ok
= emit_shader(c
, &v
->num_temps
, &num_consts
);
635 /* empty shader, emit NOP */
637 emit_inst(c
, &(struct etna_inst
) { .opcode
= INST_OPCODE_NOP
});
639 /* assemble instructions, fixing up labels */
640 uint32_t *code
= MALLOC(c
->inst_ptr
* 16);
641 for (unsigned i
= 0; i
< c
->inst_ptr
; i
++) {
642 struct etna_inst
*inst
= &c
->code
[i
];
643 if (inst
->opcode
== INST_OPCODE_BRANCH
)
644 inst
->imm
= block_ptr
[inst
->imm
];
646 inst
->halti5
= specs
->halti
>= 5;
647 etna_assemble(&code
[i
* 4], inst
);
650 v
->code_size
= c
->inst_ptr
* 4;
652 v
->needs_icache
= c
->inst_ptr
> specs
->max_instructions
;
654 copy_uniform_state_to_shader(v
, c
->consts
, num_consts
);
656 if (s
->info
.stage
== MESA_SHADER_FRAGMENT
) {
657 v
->input_count_unk8
= 31; /* XXX what is this */
658 assert(v
->ps_depth_out_reg
<= 0);
663 bool result
= etna_compile_check_limits(v
);
670 etna_destroy_shader_nir(struct etna_shader_variant
*shader
)
675 FREE(shader
->uniforms
.imm_data
);
676 FREE(shader
->uniforms
.imm_contents
);
680 extern const char *tgsi_swizzle_names
[];
682 etna_dump_shader_nir(const struct etna_shader_variant
*shader
)
684 if (shader
->stage
== MESA_SHADER_VERTEX
)
689 etna_disasm(shader
->code
, shader
->code_size
, PRINT_RAW
);
691 printf("num loops: %i\n", shader
->num_loops
);
692 printf("num temps: %i\n", shader
->num_temps
);
693 printf("immediates:\n");
694 for (int idx
= 0; idx
< shader
->uniforms
.imm_count
; ++idx
) {
695 printf(" [%i].%s = %f (0x%08x) (%d)\n",
697 tgsi_swizzle_names
[idx
% 4],
698 *((float *)&shader
->uniforms
.imm_data
[idx
]),
699 shader
->uniforms
.imm_data
[idx
],
700 shader
->uniforms
.imm_contents
[idx
]);
703 for (int idx
= 0; idx
< shader
->infile
.num_reg
; ++idx
) {
704 printf(" [%i] name=%s comps=%i\n", shader
->infile
.reg
[idx
].reg
,
705 (shader
->stage
== MESA_SHADER_VERTEX
) ?
706 gl_vert_attrib_name(shader
->infile
.reg
[idx
].slot
) :
707 gl_varying_slot_name(shader
->infile
.reg
[idx
].slot
),
708 shader
->infile
.reg
[idx
].num_components
);
710 printf("outputs:\n");
711 for (int idx
= 0; idx
< shader
->outfile
.num_reg
; ++idx
) {
712 printf(" [%i] name=%s comps=%i\n", shader
->outfile
.reg
[idx
].reg
,
713 (shader
->stage
== MESA_SHADER_VERTEX
) ?
714 gl_varying_slot_name(shader
->outfile
.reg
[idx
].slot
) :
715 gl_frag_result_name(shader
->outfile
.reg
[idx
].slot
),
716 shader
->outfile
.reg
[idx
].num_components
);
718 printf("special:\n");
719 if (shader
->stage
== MESA_SHADER_VERTEX
) {
720 printf(" vs_pos_out_reg=%i\n", shader
->vs_pos_out_reg
);
721 printf(" vs_pointsize_out_reg=%i\n", shader
->vs_pointsize_out_reg
);
722 printf(" vs_load_balancing=0x%08x\n", shader
->vs_load_balancing
);
724 printf(" ps_color_out_reg=%i\n", shader
->ps_color_out_reg
);
725 printf(" ps_depth_out_reg=%i\n", shader
->ps_depth_out_reg
);
727 printf(" input_count_unk8=0x%08x\n", shader
->input_count_unk8
);
730 static const struct etna_shader_inout
*
731 etna_shader_vs_lookup(const struct etna_shader_variant
*sobj
,
732 const struct etna_shader_inout
*in
)
734 for (int i
= 0; i
< sobj
->outfile
.num_reg
; i
++)
735 if (sobj
->outfile
.reg
[i
].slot
== in
->slot
)
736 return &sobj
->outfile
.reg
[i
];
742 etna_link_shader_nir(struct etna_shader_link_info
*info
,
743 const struct etna_shader_variant
*vs
,
744 const struct etna_shader_variant
*fs
)
747 /* For each fragment input we need to find the associated vertex shader
748 * output, which can be found by matching on semantic name and index. A
749 * binary search could be used because the vs outputs are sorted by their
750 * semantic index and grouped by semantic type by fill_in_vs_outputs.
752 assert(fs
->infile
.num_reg
< ETNA_NUM_INPUTS
);
753 info
->pcoord_varying_comp_ofs
= -1;
755 for (int idx
= 0; idx
< fs
->infile
.num_reg
; ++idx
) {
756 const struct etna_shader_inout
*fsio
= &fs
->infile
.reg
[idx
];
757 const struct etna_shader_inout
*vsio
= etna_shader_vs_lookup(vs
, fsio
);
758 struct etna_varying
*varying
;
759 bool interpolate_always
= true;
761 assert(fsio
->reg
> 0 && fsio
->reg
<= ARRAY_SIZE(info
->varyings
));
763 if (fsio
->reg
> info
->num_varyings
)
764 info
->num_varyings
= fsio
->reg
;
766 varying
= &info
->varyings
[fsio
->reg
- 1];
767 varying
->num_components
= fsio
->num_components
;
769 if (!interpolate_always
) /* colors affected by flat shading */
770 varying
->pa_attributes
= 0x200;
771 else /* texture coord or other bypasses flat shading */
772 varying
->pa_attributes
= 0x2f1;
774 varying
->use
[0] = VARYING_COMPONENT_USE_UNUSED
;
775 varying
->use
[1] = VARYING_COMPONENT_USE_UNUSED
;
776 varying
->use
[2] = VARYING_COMPONENT_USE_UNUSED
;
777 varying
->use
[3] = VARYING_COMPONENT_USE_UNUSED
;
779 /* point coord is an input to the PS without matching VS output,
780 * so it gets a varying slot without being assigned a VS register.
782 if (fsio
->slot
== VARYING_SLOT_PNTC
) {
783 varying
->use
[0] = VARYING_COMPONENT_USE_POINTCOORD_X
;
784 varying
->use
[1] = VARYING_COMPONENT_USE_POINTCOORD_Y
;
786 info
->pcoord_varying_comp_ofs
= comp_ofs
;
788 if (vsio
== NULL
) { /* not found -- link error */
789 BUG("Semantic value not found in vertex shader outputs\n");
792 varying
->reg
= vsio
->reg
;
795 comp_ofs
+= varying
->num_components
;
798 assert(info
->num_varyings
== fs
->infile
.num_reg
);