2 * Copyright © 2016-2017 Broadcom
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 #include "broadcom/common/v3d_device_info.h"
25 #include "v3d_compiler.h"
28 vir_get_non_sideband_nsrc(struct qinst
*inst
)
30 switch (inst
->qpu
.type
) {
31 case V3D_QPU_INSTR_TYPE_BRANCH
:
33 case V3D_QPU_INSTR_TYPE_ALU
:
34 if (inst
->qpu
.alu
.add
.op
!= V3D_QPU_A_NOP
)
35 return v3d_qpu_add_op_num_src(inst
->qpu
.alu
.add
.op
);
37 return v3d_qpu_mul_op_num_src(inst
->qpu
.alu
.mul
.op
);
44 vir_get_nsrc(struct qinst
*inst
)
46 int nsrc
= vir_get_non_sideband_nsrc(inst
);
48 if (vir_has_implicit_uniform(inst
))
55 vir_has_implicit_uniform(struct qinst
*inst
)
57 switch (inst
->qpu
.type
) {
58 case V3D_QPU_INSTR_TYPE_BRANCH
:
60 case V3D_QPU_INSTR_TYPE_ALU
:
61 switch (inst
->dst
.file
) {
65 return inst
->has_implicit_uniform
;
71 /* The sideband uniform for textures gets stored after the normal ALU
75 vir_get_implicit_uniform_src(struct qinst
*inst
)
77 if (!vir_has_implicit_uniform(inst
))
79 return vir_get_nsrc(inst
) - 1;
83 * Returns whether the instruction has any side effects that must be
87 vir_has_side_effects(struct v3d_compile
*c
, struct qinst
*inst
)
89 switch (inst
->qpu
.type
) {
90 case V3D_QPU_INSTR_TYPE_BRANCH
:
92 case V3D_QPU_INSTR_TYPE_ALU
:
93 switch (inst
->qpu
.alu
.add
.op
) {
94 case V3D_QPU_A_SETREVF
:
95 case V3D_QPU_A_SETMSF
:
96 case V3D_QPU_A_VPMSETUP
:
97 case V3D_QPU_A_STVPMV
:
98 case V3D_QPU_A_STVPMD
:
99 case V3D_QPU_A_STVPMP
:
100 case V3D_QPU_A_VPMWT
:
106 switch (inst
->qpu
.alu
.mul
.op
) {
107 case V3D_QPU_M_MULTOP
:
114 if (inst
->qpu
.sig
.ldtmu
||
115 inst
->qpu
.sig
.ldvary
||
116 inst
->qpu
.sig
.wrtmuc
||
117 inst
->qpu
.sig
.thrsw
) {
125 vir_is_float_input(struct qinst
*inst
)
127 /* XXX: More instrs */
128 switch (inst
->qpu
.type
) {
129 case V3D_QPU_INSTR_TYPE_BRANCH
:
131 case V3D_QPU_INSTR_TYPE_ALU
:
132 switch (inst
->qpu
.alu
.add
.op
) {
137 case V3D_QPU_A_FTOIN
:
143 switch (inst
->qpu
.alu
.mul
.op
) {
145 case V3D_QPU_M_VFMUL
:
157 vir_is_raw_mov(struct qinst
*inst
)
159 if (inst
->qpu
.type
!= V3D_QPU_INSTR_TYPE_ALU
||
160 (inst
->qpu
.alu
.mul
.op
!= V3D_QPU_M_FMOV
&&
161 inst
->qpu
.alu
.mul
.op
!= V3D_QPU_M_MOV
)) {
165 if (inst
->qpu
.alu
.add
.output_pack
!= V3D_QPU_PACK_NONE
||
166 inst
->qpu
.alu
.mul
.output_pack
!= V3D_QPU_PACK_NONE
) {
170 if (inst
->qpu
.flags
.ac
!= V3D_QPU_COND_NONE
||
171 inst
->qpu
.flags
.mc
!= V3D_QPU_COND_NONE
)
178 vir_is_add(struct qinst
*inst
)
180 return (inst
->qpu
.type
== V3D_QPU_INSTR_TYPE_ALU
&&
181 inst
->qpu
.alu
.add
.op
!= V3D_QPU_A_NOP
);
185 vir_is_mul(struct qinst
*inst
)
187 return (inst
->qpu
.type
== V3D_QPU_INSTR_TYPE_ALU
&&
188 inst
->qpu
.alu
.mul
.op
!= V3D_QPU_M_NOP
);
192 vir_is_tex(struct qinst
*inst
)
194 if (inst
->dst
.file
== QFILE_MAGIC
)
195 return v3d_qpu_magic_waddr_is_tmu(inst
->dst
.index
);
201 vir_depends_on_flags(struct qinst
*inst
)
203 if (inst
->qpu
.type
== V3D_QPU_INSTR_TYPE_BRANCH
) {
204 return (inst
->qpu
.branch
.cond
!= V3D_QPU_BRANCH_COND_ALWAYS
);
206 return (inst
->qpu
.flags
.ac
!= V3D_QPU_COND_NONE
&&
207 inst
->qpu
.flags
.mc
!= V3D_QPU_COND_NONE
);
212 vir_writes_r3(const struct v3d_device_info
*devinfo
, struct qinst
*inst
)
214 for (int i
= 0; i
< vir_get_nsrc(inst
); i
++) {
215 switch (inst
->src
[i
].file
) {
223 if (devinfo
->ver
< 41 && (inst
->qpu
.sig
.ldvary
||
224 inst
->qpu
.sig
.ldtlb
||
225 inst
->qpu
.sig
.ldtlbu
||
226 inst
->qpu
.sig
.ldvpm
)) {
234 vir_writes_r4(const struct v3d_device_info
*devinfo
, struct qinst
*inst
)
236 switch (inst
->dst
.file
) {
238 switch (inst
->dst
.index
) {
239 case V3D_QPU_WADDR_RECIP
:
240 case V3D_QPU_WADDR_RSQRT
:
241 case V3D_QPU_WADDR_EXP
:
242 case V3D_QPU_WADDR_LOG
:
243 case V3D_QPU_WADDR_SIN
:
251 if (devinfo
->ver
< 41 && inst
->qpu
.sig
.ldtmu
)
258 vir_set_unpack(struct qinst
*inst
, int src
,
259 enum v3d_qpu_input_unpack unpack
)
261 assert(src
== 0 || src
== 1);
263 if (vir_is_add(inst
)) {
265 inst
->qpu
.alu
.add
.a_unpack
= unpack
;
267 inst
->qpu
.alu
.add
.b_unpack
= unpack
;
269 assert(vir_is_mul(inst
));
271 inst
->qpu
.alu
.mul
.a_unpack
= unpack
;
273 inst
->qpu
.alu
.mul
.b_unpack
= unpack
;
278 vir_set_cond(struct qinst
*inst
, enum v3d_qpu_cond cond
)
280 if (vir_is_add(inst
)) {
281 inst
->qpu
.flags
.ac
= cond
;
283 assert(vir_is_mul(inst
));
284 inst
->qpu
.flags
.mc
= cond
;
289 vir_set_pf(struct qinst
*inst
, enum v3d_qpu_pf pf
)
291 if (vir_is_add(inst
)) {
292 inst
->qpu
.flags
.apf
= pf
;
294 assert(vir_is_mul(inst
));
295 inst
->qpu
.flags
.mpf
= pf
;
301 vir_channels_written(struct qinst
*inst
)
303 if (vir_is_mul(inst
)) {
304 switch (inst
->dst
.pack
) {
305 case QPU_PACK_MUL_NOP
:
306 case QPU_PACK_MUL_8888
:
308 case QPU_PACK_MUL_8A
:
310 case QPU_PACK_MUL_8B
:
312 case QPU_PACK_MUL_8C
:
314 case QPU_PACK_MUL_8D
:
318 switch (inst
->dst
.pack
) {
320 case QPU_PACK_A_8888
:
321 case QPU_PACK_A_8888_SAT
:
322 case QPU_PACK_A_32_SAT
:
325 case QPU_PACK_A_8A_SAT
:
328 case QPU_PACK_A_8B_SAT
:
331 case QPU_PACK_A_8C_SAT
:
334 case QPU_PACK_A_8D_SAT
:
337 case QPU_PACK_A_16A_SAT
:
340 case QPU_PACK_A_16B_SAT
:
344 unreachable("Bad pack field");
349 vir_get_temp(struct v3d_compile
*c
)
353 reg
.file
= QFILE_TEMP
;
354 reg
.index
= c
->num_temps
++;
356 if (c
->num_temps
> c
->defs_array_size
) {
357 uint32_t old_size
= c
->defs_array_size
;
358 c
->defs_array_size
= MAX2(old_size
* 2, 16);
360 c
->defs
= reralloc(c
, c
->defs
, struct qinst
*,
362 memset(&c
->defs
[old_size
], 0,
363 sizeof(c
->defs
[0]) * (c
->defs_array_size
- old_size
));
365 c
->spillable
= reralloc(c
, c
->spillable
,
367 BITSET_WORDS(c
->defs_array_size
));
368 for (int i
= old_size
; i
< c
->defs_array_size
; i
++)
369 BITSET_SET(c
->spillable
, i
);
376 vir_add_inst(enum v3d_qpu_add_op op
, struct qreg dst
, struct qreg src0
, struct qreg src1
)
378 struct qinst
*inst
= calloc(1, sizeof(*inst
));
380 inst
->qpu
= v3d_qpu_nop();
381 inst
->qpu
.alu
.add
.op
= op
;
392 vir_mul_inst(enum v3d_qpu_mul_op op
, struct qreg dst
, struct qreg src0
, struct qreg src1
)
394 struct qinst
*inst
= calloc(1, sizeof(*inst
));
396 inst
->qpu
= v3d_qpu_nop();
397 inst
->qpu
.alu
.mul
.op
= op
;
408 vir_branch_inst(enum v3d_qpu_branch_cond cond
, struct qreg src
)
410 struct qinst
*inst
= calloc(1, sizeof(*inst
));
412 inst
->qpu
= v3d_qpu_nop();
413 inst
->qpu
.type
= V3D_QPU_INSTR_TYPE_BRANCH
;
414 inst
->qpu
.branch
.cond
= cond
;
415 inst
->qpu
.branch
.msfign
= V3D_QPU_MSFIGN_NONE
;
416 inst
->qpu
.branch
.bdi
= V3D_QPU_BRANCH_DEST_REL
;
417 inst
->qpu
.branch
.ub
= true;
418 inst
->qpu
.branch
.bdu
= V3D_QPU_BRANCH_DEST_REL
;
420 inst
->dst
= vir_reg(QFILE_NULL
, 0);
428 vir_emit(struct v3d_compile
*c
, struct qinst
*inst
)
430 switch (c
->cursor
.mode
) {
432 list_add(&inst
->link
, c
->cursor
.link
);
434 case vir_cursor_addtail
:
435 list_addtail(&inst
->link
, c
->cursor
.link
);
439 c
->cursor
= vir_after_inst(inst
);
440 c
->live_intervals_valid
= false;
443 /* Updates inst to write to a new temporary, emits it, and notes the def. */
445 vir_emit_def(struct v3d_compile
*c
, struct qinst
*inst
)
447 assert(inst
->dst
.file
== QFILE_NULL
);
449 inst
->dst
= vir_get_temp(c
);
451 if (inst
->dst
.file
== QFILE_TEMP
)
452 c
->defs
[inst
->dst
.index
] = inst
;
460 vir_emit_nondef(struct v3d_compile
*c
, struct qinst
*inst
)
462 if (inst
->dst
.file
== QFILE_TEMP
)
463 c
->defs
[inst
->dst
.index
] = NULL
;
471 vir_new_block(struct v3d_compile
*c
)
473 struct qblock
*block
= rzalloc(c
, struct qblock
);
475 list_inithead(&block
->instructions
);
477 block
->predecessors
= _mesa_set_create(block
,
479 _mesa_key_pointer_equal
);
481 block
->index
= c
->next_block_index
++;
487 vir_set_emit_block(struct v3d_compile
*c
, struct qblock
*block
)
489 c
->cur_block
= block
;
490 c
->cursor
= vir_after_block(block
);
491 list_addtail(&block
->link
, &c
->blocks
);
495 vir_entry_block(struct v3d_compile
*c
)
497 return list_first_entry(&c
->blocks
, struct qblock
, link
);
501 vir_exit_block(struct v3d_compile
*c
)
503 return list_last_entry(&c
->blocks
, struct qblock
, link
);
507 vir_link_blocks(struct qblock
*predecessor
, struct qblock
*successor
)
509 _mesa_set_add(successor
->predecessors
, predecessor
);
510 if (predecessor
->successors
[0]) {
511 assert(!predecessor
->successors
[1]);
512 predecessor
->successors
[1] = successor
;
514 predecessor
->successors
[0] = successor
;
518 const struct v3d_compiler
*
519 v3d_compiler_init(const struct v3d_device_info
*devinfo
)
521 struct v3d_compiler
*compiler
= rzalloc(NULL
, struct v3d_compiler
);
525 compiler
->devinfo
= devinfo
;
527 if (!vir_init_reg_sets(compiler
)) {
528 ralloc_free(compiler
);
536 v3d_compiler_free(const struct v3d_compiler
*compiler
)
538 ralloc_free((void *)compiler
);
541 static struct v3d_compile
*
542 vir_compile_init(const struct v3d_compiler
*compiler
,
545 int program_id
, int variant_id
)
547 struct v3d_compile
*c
= rzalloc(NULL
, struct v3d_compile
);
549 c
->compiler
= compiler
;
550 c
->devinfo
= compiler
->devinfo
;
552 c
->program_id
= program_id
;
553 c
->variant_id
= variant_id
;
556 s
= nir_shader_clone(c
, s
);
559 list_inithead(&c
->blocks
);
560 vir_set_emit_block(c
, vir_new_block(c
));
562 c
->output_position_index
= -1;
563 c
->output_point_size_index
= -1;
564 c
->output_sample_mask_index
= -1;
566 c
->def_ht
= _mesa_hash_table_create(c
, _mesa_hash_pointer
,
567 _mesa_key_pointer_equal
);
573 v3d_lower_nir(struct v3d_compile
*c
)
575 struct nir_lower_tex_options tex_options
= {
577 .lower_rect
= false, /* XXX: Use this on V3D 3.x */
579 /* Apply swizzles to all samplers. */
580 .swizzle_result
= ~0,
583 /* Lower the format swizzle and (for 32-bit returns)
584 * ARB_texture_swizzle-style swizzle.
586 for (int i
= 0; i
< ARRAY_SIZE(c
->key
->tex
); i
++) {
587 for (int j
= 0; j
< 4; j
++)
588 tex_options
.swizzles
[i
][j
] = c
->key
->tex
[i
].swizzle
[j
];
590 if (c
->key
->tex
[i
].clamp_s
)
591 tex_options
.saturate_s
|= 1 << i
;
592 if (c
->key
->tex
[i
].clamp_t
)
593 tex_options
.saturate_t
|= 1 << i
;
594 if (c
->key
->tex
[i
].clamp_r
)
595 tex_options
.saturate_r
|= 1 << i
;
598 NIR_PASS_V(c
->s
, nir_lower_tex
, &tex_options
);
602 v3d_lower_nir_late(struct v3d_compile
*c
)
604 NIR_PASS_V(c
->s
, v3d_nir_lower_io
, c
);
605 NIR_PASS_V(c
->s
, v3d_nir_lower_txf_ms
, c
);
606 NIR_PASS_V(c
->s
, nir_lower_idiv
);
610 v3d_set_prog_data_uniforms(struct v3d_compile
*c
,
611 struct v3d_prog_data
*prog_data
)
613 int count
= c
->num_uniforms
;
614 struct v3d_uniform_list
*ulist
= &prog_data
->uniforms
;
616 ulist
->count
= count
;
617 ulist
->data
= ralloc_array(prog_data
, uint32_t, count
);
618 memcpy(ulist
->data
, c
->uniform_data
,
619 count
* sizeof(*ulist
->data
));
620 ulist
->contents
= ralloc_array(prog_data
, enum quniform_contents
, count
);
621 memcpy(ulist
->contents
, c
->uniform_contents
,
622 count
* sizeof(*ulist
->contents
));
625 /* Copy the compiler UBO range state to the compiled shader, dropping out
626 * arrays that were never referenced by an indirect load.
628 * (Note that QIR dead code elimination of an array access still leaves that
629 * array alive, though)
632 v3d_set_prog_data_ubo(struct v3d_compile
*c
,
633 struct v3d_prog_data
*prog_data
)
635 if (!c
->num_ubo_ranges
)
638 prog_data
->num_ubo_ranges
= 0;
639 prog_data
->ubo_ranges
= ralloc_array(prog_data
, struct v3d_ubo_range
,
641 for (int i
= 0; i
< c
->num_ubo_ranges
; i
++) {
642 if (!c
->ubo_range_used
[i
])
645 struct v3d_ubo_range
*range
= &c
->ubo_ranges
[i
];
646 prog_data
->ubo_ranges
[prog_data
->num_ubo_ranges
++] = *range
;
647 prog_data
->ubo_size
+= range
->size
;
650 if (prog_data
->ubo_size
) {
651 if (V3D_DEBUG
& V3D_DEBUG_SHADERDB
) {
652 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d UBO uniforms\n",
653 vir_get_stage_name(c
),
654 c
->program_id
, c
->variant_id
,
655 prog_data
->ubo_size
/ 4);
661 v3d_set_prog_data(struct v3d_compile
*c
,
662 struct v3d_prog_data
*prog_data
)
664 prog_data
->threads
= c
->threads
;
665 prog_data
->single_seg
= !c
->last_thrsw
;
666 prog_data
->spill_size
= c
->spill_size
;
668 v3d_set_prog_data_uniforms(c
, prog_data
);
669 v3d_set_prog_data_ubo(c
, prog_data
);
673 v3d_return_qpu_insts(struct v3d_compile
*c
, uint32_t *final_assembly_size
)
675 *final_assembly_size
= c
->qpu_inst_count
* sizeof(uint64_t);
677 uint64_t *qpu_insts
= malloc(*final_assembly_size
);
681 memcpy(qpu_insts
, c
->qpu_insts
, *final_assembly_size
);
683 vir_compile_destroy(c
);
688 uint64_t *v3d_compile_vs(const struct v3d_compiler
*compiler
,
689 struct v3d_vs_key
*key
,
690 struct v3d_vs_prog_data
*prog_data
,
692 int program_id
, int variant_id
,
693 uint32_t *final_assembly_size
)
695 struct v3d_compile
*c
= vir_compile_init(compiler
, &key
->base
, s
,
696 program_id
, variant_id
);
702 if (key
->clamp_color
)
703 NIR_PASS_V(c
->s
, nir_lower_clamp_color_outputs
);
705 if (key
->base
.ucp_enables
) {
706 NIR_PASS_V(c
->s
, nir_lower_clip_vs
, key
->base
.ucp_enables
);
707 NIR_PASS_V(c
->s
, nir_lower_io_to_scalar
,
711 /* Note: VS output scalarizing must happen after nir_lower_clip_vs. */
712 NIR_PASS_V(c
->s
, nir_lower_io_to_scalar
, nir_var_shader_out
);
714 v3d_lower_nir_late(c
);
715 v3d_optimize_nir(c
->s
);
716 NIR_PASS_V(c
->s
, nir_convert_from_ssa
, true);
720 v3d_set_prog_data(c
, &prog_data
->base
);
722 prog_data
->base
.num_inputs
= c
->num_inputs
;
724 /* The vertex data gets format converted by the VPM so that
725 * each attribute channel takes up a VPM column. Precompute
726 * the sizes for the shader record.
728 for (int i
= 0; i
< ARRAY_SIZE(prog_data
->vattr_sizes
); i
++) {
729 prog_data
->vattr_sizes
[i
] = c
->vattr_sizes
[i
];
730 prog_data
->vpm_input_size
+= c
->vattr_sizes
[i
];
733 prog_data
->uses_vid
= (s
->info
.system_values_read
&
734 (1ull << SYSTEM_VALUE_VERTEX_ID
));
735 prog_data
->uses_iid
= (s
->info
.system_values_read
&
736 (1ull << SYSTEM_VALUE_INSTANCE_ID
));
738 if (prog_data
->uses_vid
)
739 prog_data
->vpm_input_size
++;
740 if (prog_data
->uses_iid
)
741 prog_data
->vpm_input_size
++;
743 /* Input/output segment size are in 8x32-bit multiples. */
744 prog_data
->vpm_input_size
= align(prog_data
->vpm_input_size
, 8) / 8;
745 prog_data
->vpm_output_size
= align(c
->num_vpm_writes
, 8) / 8;
747 return v3d_return_qpu_insts(c
, final_assembly_size
);
751 v3d_set_fs_prog_data_inputs(struct v3d_compile
*c
,
752 struct v3d_fs_prog_data
*prog_data
)
754 prog_data
->base
.num_inputs
= c
->num_inputs
;
755 memcpy(prog_data
->input_slots
, c
->input_slots
,
756 c
->num_inputs
* sizeof(*c
->input_slots
));
758 STATIC_ASSERT(ARRAY_SIZE(prog_data
->flat_shade_flags
) >
759 (V3D_MAX_FS_INPUTS
- 1) / 24);
760 for (int i
= 0; i
< V3D_MAX_FS_INPUTS
; i
++) {
761 if (BITSET_TEST(c
->flat_shade_flags
, i
))
762 prog_data
->flat_shade_flags
[i
/ 24] |= 1 << (i
% 24);
764 if (BITSET_TEST(c
->noperspective_flags
, i
))
765 prog_data
->noperspective_flags
[i
/ 24] |= 1 << (i
% 24);
767 if (BITSET_TEST(c
->centroid_flags
, i
))
768 prog_data
->centroid_flags
[i
/ 24] |= 1 << (i
% 24);
773 v3d_fixup_fs_output_types(struct v3d_compile
*c
)
775 nir_foreach_variable(var
, &c
->s
->outputs
) {
778 switch (var
->data
.location
) {
779 case FRAG_RESULT_COLOR
:
782 case FRAG_RESULT_DATA0
:
783 case FRAG_RESULT_DATA1
:
784 case FRAG_RESULT_DATA2
:
785 case FRAG_RESULT_DATA3
:
786 mask
= 1 << (var
->data
.location
- FRAG_RESULT_DATA0
);
790 if (c
->fs_key
->int_color_rb
& mask
) {
792 glsl_vector_type(GLSL_TYPE_INT
,
793 glsl_get_components(var
->type
));
794 } else if (c
->fs_key
->uint_color_rb
& mask
) {
796 glsl_vector_type(GLSL_TYPE_UINT
,
797 glsl_get_components(var
->type
));
802 uint64_t *v3d_compile_fs(const struct v3d_compiler
*compiler
,
803 struct v3d_fs_key
*key
,
804 struct v3d_fs_prog_data
*prog_data
,
806 int program_id
, int variant_id
,
807 uint32_t *final_assembly_size
)
809 struct v3d_compile
*c
= vir_compile_init(compiler
, &key
->base
, s
,
810 program_id
, variant_id
);
814 if (key
->int_color_rb
|| key
->uint_color_rb
)
815 v3d_fixup_fs_output_types(c
);
819 if (key
->light_twoside
)
820 NIR_PASS_V(c
->s
, nir_lower_two_sided_color
);
822 if (key
->clamp_color
)
823 NIR_PASS_V(c
->s
, nir_lower_clamp_color_outputs
);
825 if (key
->alpha_test
) {
826 NIR_PASS_V(c
->s
, nir_lower_alpha_test
, key
->alpha_test_func
,
830 if (key
->base
.ucp_enables
)
831 NIR_PASS_V(c
->s
, nir_lower_clip_fs
, key
->base
.ucp_enables
);
833 /* Note: FS input scalarizing must happen after
834 * nir_lower_two_sided_color, which only handles a vec4 at a time.
836 NIR_PASS_V(c
->s
, nir_lower_io_to_scalar
, nir_var_shader_in
);
838 v3d_lower_nir_late(c
);
839 v3d_optimize_nir(c
->s
);
840 NIR_PASS_V(c
->s
, nir_convert_from_ssa
, true);
844 v3d_set_prog_data(c
, &prog_data
->base
);
845 v3d_set_fs_prog_data_inputs(c
, prog_data
);
846 prog_data
->writes_z
= (c
->s
->info
.outputs_written
&
847 (1 << FRAG_RESULT_DEPTH
));
848 prog_data
->discard
= (c
->s
->info
.fs
.uses_discard
||
849 c
->fs_key
->sample_alpha_to_coverage
);
850 prog_data
->uses_center_w
= c
->uses_center_w
;
852 return v3d_return_qpu_insts(c
, final_assembly_size
);
856 vir_remove_instruction(struct v3d_compile
*c
, struct qinst
*qinst
)
858 if (qinst
->dst
.file
== QFILE_TEMP
)
859 c
->defs
[qinst
->dst
.index
] = NULL
;
861 assert(&qinst
->link
!= c
->cursor
.link
);
863 list_del(&qinst
->link
);
866 c
->live_intervals_valid
= false;
870 vir_follow_movs(struct v3d_compile
*c
, struct qreg reg
)
875 while (reg.file == QFILE_TEMP &&
876 c->defs[reg.index] &&
877 (c->defs[reg.index]->op == QOP_MOV ||
878 c->defs[reg.index]->op == QOP_FMOV) &&
879 !c->defs[reg.index]->dst.pack &&
880 !c->defs[reg.index]->src[0].pack) {
881 reg = c->defs[reg.index]->src[0];
890 vir_compile_destroy(struct v3d_compile
*c
)
892 /* Defuse the assert that we aren't removing the cursor's instruction.
894 c
->cursor
.link
= NULL
;
896 vir_for_each_block(block
, c
) {
897 while (!list_empty(&block
->instructions
)) {
898 struct qinst
*qinst
=
899 list_first_entry(&block
->instructions
,
901 vir_remove_instruction(c
, qinst
);
909 vir_uniform(struct v3d_compile
*c
,
910 enum quniform_contents contents
,
913 for (int i
= 0; i
< c
->num_uniforms
; i
++) {
914 if (c
->uniform_contents
[i
] == contents
&&
915 c
->uniform_data
[i
] == data
) {
916 return vir_reg(QFILE_UNIF
, i
);
920 uint32_t uniform
= c
->num_uniforms
++;
922 if (uniform
>= c
->uniform_array_size
) {
923 c
->uniform_array_size
= MAX2(MAX2(16, uniform
+ 1),
924 c
->uniform_array_size
* 2);
926 c
->uniform_data
= reralloc(c
, c
->uniform_data
,
928 c
->uniform_array_size
);
929 c
->uniform_contents
= reralloc(c
, c
->uniform_contents
,
930 enum quniform_contents
,
931 c
->uniform_array_size
);
934 c
->uniform_contents
[uniform
] = contents
;
935 c
->uniform_data
[uniform
] = data
;
937 return vir_reg(QFILE_UNIF
, uniform
);
941 vir_can_set_flags(struct v3d_compile
*c
, struct qinst
*inst
)
943 if (c
->devinfo
->ver
>= 40 && (v3d_qpu_reads_vpm(&inst
->qpu
) ||
944 v3d_qpu_uses_sfu(&inst
->qpu
))) {
952 vir_PF(struct v3d_compile
*c
, struct qreg src
, enum v3d_qpu_pf pf
)
954 struct qinst
*last_inst
= NULL
;
956 if (!list_empty(&c
->cur_block
->instructions
)) {
957 last_inst
= (struct qinst
*)c
->cur_block
->instructions
.prev
;
959 /* Can't stuff the PF into the last last inst if our cursor
960 * isn't pointing after it.
962 struct vir_cursor after_inst
= vir_after_inst(last_inst
);
963 if (c
->cursor
.mode
!= after_inst
.mode
||
964 c
->cursor
.link
!= after_inst
.link
)
968 if (src
.file
!= QFILE_TEMP
||
969 !c
->defs
[src
.index
] ||
970 last_inst
!= c
->defs
[src
.index
] ||
971 !vir_can_set_flags(c
, last_inst
)) {
972 /* XXX: Make the MOV be the appropriate type */
973 last_inst
= vir_MOV_dest(c
, vir_reg(QFILE_NULL
, 0), src
);
976 vir_set_pf(last_inst
, pf
);
979 #define OPTPASS(func) \
981 bool stage_progress = func(c); \
982 if (stage_progress) { \
984 if (print_opt_debug) { \
986 "VIR opt pass %2d: %s progress\n", \
989 /*XXX vir_validate(c);*/ \
994 vir_optimize(struct v3d_compile
*c
)
996 bool print_opt_debug
= false;
1000 bool progress
= false;
1002 OPTPASS(vir_opt_copy_propagate
);
1003 OPTPASS(vir_opt_dead_code
);
1013 vir_get_stage_name(struct v3d_compile
*c
)
1015 if (c
->vs_key
&& c
->vs_key
->is_coord
)
1016 return "MESA_SHADER_COORD";
1018 return gl_shader_stage_name(c
->s
->info
.stage
);