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 switch (inst
->dst
.index
) {
66 case V3D_QPU_WADDR_TLBU
:
67 case V3D_QPU_WADDR_TMUAU
:
68 case V3D_QPU_WADDR_SYNCU
:
75 return inst
->has_implicit_uniform
;
81 /* The sideband uniform for textures gets stored after the normal ALU
85 vir_get_implicit_uniform_src(struct qinst
*inst
)
87 if (!vir_has_implicit_uniform(inst
))
89 return vir_get_nsrc(inst
) - 1;
93 * Returns whether the instruction has any side effects that must be
97 vir_has_side_effects(struct v3d_compile
*c
, struct qinst
*inst
)
99 switch (inst
->qpu
.type
) {
100 case V3D_QPU_INSTR_TYPE_BRANCH
:
102 case V3D_QPU_INSTR_TYPE_ALU
:
103 switch (inst
->qpu
.alu
.add
.op
) {
104 case V3D_QPU_A_SETREVF
:
105 case V3D_QPU_A_SETMSF
:
106 case V3D_QPU_A_VPMSETUP
:
107 case V3D_QPU_A_STVPMV
:
108 case V3D_QPU_A_STVPMD
:
109 case V3D_QPU_A_STVPMP
:
110 case V3D_QPU_A_VPMWT
:
111 case V3D_QPU_A_TMUWT
:
117 switch (inst
->qpu
.alu
.mul
.op
) {
118 case V3D_QPU_M_MULTOP
:
125 if (inst
->qpu
.sig
.ldtmu
||
126 inst
->qpu
.sig
.ldvary
||
127 inst
->qpu
.sig
.wrtmuc
||
128 inst
->qpu
.sig
.thrsw
) {
136 vir_is_raw_mov(struct qinst
*inst
)
138 if (inst
->qpu
.type
!= V3D_QPU_INSTR_TYPE_ALU
||
139 (inst
->qpu
.alu
.mul
.op
!= V3D_QPU_M_FMOV
&&
140 inst
->qpu
.alu
.mul
.op
!= V3D_QPU_M_MOV
)) {
144 if (inst
->qpu
.alu
.add
.output_pack
!= V3D_QPU_PACK_NONE
||
145 inst
->qpu
.alu
.mul
.output_pack
!= V3D_QPU_PACK_NONE
) {
149 if (inst
->qpu
.alu
.add
.a_unpack
!= V3D_QPU_UNPACK_NONE
||
150 inst
->qpu
.alu
.add
.b_unpack
!= V3D_QPU_UNPACK_NONE
||
151 inst
->qpu
.alu
.mul
.a_unpack
!= V3D_QPU_UNPACK_NONE
||
152 inst
->qpu
.alu
.mul
.b_unpack
!= V3D_QPU_UNPACK_NONE
) {
156 if (inst
->qpu
.flags
.ac
!= V3D_QPU_COND_NONE
||
157 inst
->qpu
.flags
.mc
!= V3D_QPU_COND_NONE
)
164 vir_is_add(struct qinst
*inst
)
166 return (inst
->qpu
.type
== V3D_QPU_INSTR_TYPE_ALU
&&
167 inst
->qpu
.alu
.add
.op
!= V3D_QPU_A_NOP
);
171 vir_is_mul(struct qinst
*inst
)
173 return (inst
->qpu
.type
== V3D_QPU_INSTR_TYPE_ALU
&&
174 inst
->qpu
.alu
.mul
.op
!= V3D_QPU_M_NOP
);
178 vir_is_tex(struct qinst
*inst
)
180 if (inst
->dst
.file
== QFILE_MAGIC
)
181 return v3d_qpu_magic_waddr_is_tmu(inst
->dst
.index
);
183 if (inst
->qpu
.type
== V3D_QPU_INSTR_TYPE_ALU
&&
184 inst
->qpu
.alu
.add
.op
== V3D_QPU_A_TMUWT
) {
192 vir_writes_r3(const struct v3d_device_info
*devinfo
, struct qinst
*inst
)
194 for (int i
= 0; i
< vir_get_nsrc(inst
); i
++) {
195 switch (inst
->src
[i
].file
) {
203 if (devinfo
->ver
< 41 && (inst
->qpu
.sig
.ldvary
||
204 inst
->qpu
.sig
.ldtlb
||
205 inst
->qpu
.sig
.ldtlbu
||
206 inst
->qpu
.sig
.ldvpm
)) {
214 vir_writes_r4(const struct v3d_device_info
*devinfo
, struct qinst
*inst
)
216 switch (inst
->dst
.file
) {
218 switch (inst
->dst
.index
) {
219 case V3D_QPU_WADDR_RECIP
:
220 case V3D_QPU_WADDR_RSQRT
:
221 case V3D_QPU_WADDR_EXP
:
222 case V3D_QPU_WADDR_LOG
:
223 case V3D_QPU_WADDR_SIN
:
231 if (devinfo
->ver
< 41 && inst
->qpu
.sig
.ldtmu
)
238 vir_set_unpack(struct qinst
*inst
, int src
,
239 enum v3d_qpu_input_unpack unpack
)
241 assert(src
== 0 || src
== 1);
243 if (vir_is_add(inst
)) {
245 inst
->qpu
.alu
.add
.a_unpack
= unpack
;
247 inst
->qpu
.alu
.add
.b_unpack
= unpack
;
249 assert(vir_is_mul(inst
));
251 inst
->qpu
.alu
.mul
.a_unpack
= unpack
;
253 inst
->qpu
.alu
.mul
.b_unpack
= unpack
;
258 vir_set_cond(struct qinst
*inst
, enum v3d_qpu_cond cond
)
260 if (vir_is_add(inst
)) {
261 inst
->qpu
.flags
.ac
= cond
;
263 assert(vir_is_mul(inst
));
264 inst
->qpu
.flags
.mc
= cond
;
269 vir_set_pf(struct qinst
*inst
, enum v3d_qpu_pf pf
)
271 if (vir_is_add(inst
)) {
272 inst
->qpu
.flags
.apf
= pf
;
274 assert(vir_is_mul(inst
));
275 inst
->qpu
.flags
.mpf
= pf
;
280 vir_set_uf(struct qinst
*inst
, enum v3d_qpu_uf uf
)
282 if (vir_is_add(inst
)) {
283 inst
->qpu
.flags
.auf
= uf
;
285 assert(vir_is_mul(inst
));
286 inst
->qpu
.flags
.muf
= uf
;
292 vir_channels_written(struct qinst
*inst
)
294 if (vir_is_mul(inst
)) {
295 switch (inst
->dst
.pack
) {
296 case QPU_PACK_MUL_NOP
:
297 case QPU_PACK_MUL_8888
:
299 case QPU_PACK_MUL_8A
:
301 case QPU_PACK_MUL_8B
:
303 case QPU_PACK_MUL_8C
:
305 case QPU_PACK_MUL_8D
:
309 switch (inst
->dst
.pack
) {
311 case QPU_PACK_A_8888
:
312 case QPU_PACK_A_8888_SAT
:
313 case QPU_PACK_A_32_SAT
:
316 case QPU_PACK_A_8A_SAT
:
319 case QPU_PACK_A_8B_SAT
:
322 case QPU_PACK_A_8C_SAT
:
325 case QPU_PACK_A_8D_SAT
:
328 case QPU_PACK_A_16A_SAT
:
331 case QPU_PACK_A_16B_SAT
:
335 unreachable("Bad pack field");
340 vir_get_temp(struct v3d_compile
*c
)
344 reg
.file
= QFILE_TEMP
;
345 reg
.index
= c
->num_temps
++;
347 if (c
->num_temps
> c
->defs_array_size
) {
348 uint32_t old_size
= c
->defs_array_size
;
349 c
->defs_array_size
= MAX2(old_size
* 2, 16);
351 c
->defs
= reralloc(c
, c
->defs
, struct qinst
*,
353 memset(&c
->defs
[old_size
], 0,
354 sizeof(c
->defs
[0]) * (c
->defs_array_size
- old_size
));
356 c
->spillable
= reralloc(c
, c
->spillable
,
358 BITSET_WORDS(c
->defs_array_size
));
359 for (int i
= old_size
; i
< c
->defs_array_size
; i
++)
360 BITSET_SET(c
->spillable
, i
);
367 vir_add_inst(enum v3d_qpu_add_op op
, struct qreg dst
, struct qreg src0
, struct qreg src1
)
369 struct qinst
*inst
= calloc(1, sizeof(*inst
));
371 inst
->qpu
= v3d_qpu_nop();
372 inst
->qpu
.alu
.add
.op
= op
;
383 vir_mul_inst(enum v3d_qpu_mul_op op
, struct qreg dst
, struct qreg src0
, struct qreg src1
)
385 struct qinst
*inst
= calloc(1, sizeof(*inst
));
387 inst
->qpu
= v3d_qpu_nop();
388 inst
->qpu
.alu
.mul
.op
= op
;
399 vir_branch_inst(enum v3d_qpu_branch_cond cond
, struct qreg src
)
401 struct qinst
*inst
= calloc(1, sizeof(*inst
));
403 inst
->qpu
= v3d_qpu_nop();
404 inst
->qpu
.type
= V3D_QPU_INSTR_TYPE_BRANCH
;
405 inst
->qpu
.branch
.cond
= cond
;
406 inst
->qpu
.branch
.msfign
= V3D_QPU_MSFIGN_NONE
;
407 inst
->qpu
.branch
.bdi
= V3D_QPU_BRANCH_DEST_REL
;
408 inst
->qpu
.branch
.ub
= true;
409 inst
->qpu
.branch
.bdu
= V3D_QPU_BRANCH_DEST_REL
;
411 inst
->dst
= vir_nop_reg();
419 vir_emit(struct v3d_compile
*c
, struct qinst
*inst
)
421 switch (c
->cursor
.mode
) {
423 list_add(&inst
->link
, c
->cursor
.link
);
425 case vir_cursor_addtail
:
426 list_addtail(&inst
->link
, c
->cursor
.link
);
430 c
->cursor
= vir_after_inst(inst
);
431 c
->live_intervals_valid
= false;
434 /* Updates inst to write to a new temporary, emits it, and notes the def. */
436 vir_emit_def(struct v3d_compile
*c
, struct qinst
*inst
)
438 assert(inst
->dst
.file
== QFILE_NULL
);
440 /* If we're emitting an instruction that's a def, it had better be
441 * writing a register.
443 if (inst
->qpu
.type
== V3D_QPU_INSTR_TYPE_ALU
) {
444 assert(inst
->qpu
.alu
.add
.op
== V3D_QPU_A_NOP
||
445 v3d_qpu_add_op_has_dst(inst
->qpu
.alu
.add
.op
));
446 assert(inst
->qpu
.alu
.mul
.op
== V3D_QPU_M_NOP
||
447 v3d_qpu_mul_op_has_dst(inst
->qpu
.alu
.mul
.op
));
450 inst
->dst
= vir_get_temp(c
);
452 if (inst
->dst
.file
== QFILE_TEMP
)
453 c
->defs
[inst
->dst
.index
] = inst
;
461 vir_emit_nondef(struct v3d_compile
*c
, struct qinst
*inst
)
463 if (inst
->dst
.file
== QFILE_TEMP
)
464 c
->defs
[inst
->dst
.index
] = NULL
;
472 vir_new_block(struct v3d_compile
*c
)
474 struct qblock
*block
= rzalloc(c
, struct qblock
);
476 list_inithead(&block
->instructions
);
478 block
->predecessors
= _mesa_set_create(block
,
480 _mesa_key_pointer_equal
);
482 block
->index
= c
->next_block_index
++;
488 vir_set_emit_block(struct v3d_compile
*c
, struct qblock
*block
)
490 c
->cur_block
= block
;
491 c
->cursor
= vir_after_block(block
);
492 list_addtail(&block
->link
, &c
->blocks
);
496 vir_entry_block(struct v3d_compile
*c
)
498 return list_first_entry(&c
->blocks
, struct qblock
, link
);
502 vir_exit_block(struct v3d_compile
*c
)
504 return list_last_entry(&c
->blocks
, struct qblock
, link
);
508 vir_link_blocks(struct qblock
*predecessor
, struct qblock
*successor
)
510 _mesa_set_add(successor
->predecessors
, predecessor
);
511 if (predecessor
->successors
[0]) {
512 assert(!predecessor
->successors
[1]);
513 predecessor
->successors
[1] = successor
;
515 predecessor
->successors
[0] = successor
;
519 const struct v3d_compiler
*
520 v3d_compiler_init(const struct v3d_device_info
*devinfo
)
522 struct v3d_compiler
*compiler
= rzalloc(NULL
, struct v3d_compiler
);
526 compiler
->devinfo
= devinfo
;
528 if (!vir_init_reg_sets(compiler
)) {
529 ralloc_free(compiler
);
537 v3d_compiler_free(const struct v3d_compiler
*compiler
)
539 ralloc_free((void *)compiler
);
542 static struct v3d_compile
*
543 vir_compile_init(const struct v3d_compiler
*compiler
,
546 void (*debug_output
)(const char *msg
,
547 void *debug_output_data
),
548 void *debug_output_data
,
549 int program_id
, int variant_id
)
551 struct v3d_compile
*c
= rzalloc(NULL
, struct v3d_compile
);
553 c
->compiler
= compiler
;
554 c
->devinfo
= compiler
->devinfo
;
556 c
->program_id
= program_id
;
557 c
->variant_id
= variant_id
;
559 c
->debug_output
= debug_output
;
560 c
->debug_output_data
= debug_output_data
;
562 s
= nir_shader_clone(c
, s
);
565 list_inithead(&c
->blocks
);
566 vir_set_emit_block(c
, vir_new_block(c
));
568 c
->output_position_index
= -1;
569 c
->output_point_size_index
= -1;
570 c
->output_sample_mask_index
= -1;
572 c
->def_ht
= _mesa_hash_table_create(c
, _mesa_hash_pointer
,
573 _mesa_key_pointer_equal
);
579 type_size_vec4(const struct glsl_type
*type
)
581 return glsl_count_attribute_slots(type
, false);
585 v3d_lower_nir(struct v3d_compile
*c
)
587 struct nir_lower_tex_options tex_options
= {
589 .lower_tg4_broadcom_swizzle
= true,
591 .lower_rect
= false, /* XXX: Use this on V3D 3.x */
593 /* Apply swizzles to all samplers. */
594 .swizzle_result
= ~0,
597 /* Lower the format swizzle and (for 32-bit returns)
598 * ARB_texture_swizzle-style swizzle.
600 for (int i
= 0; i
< ARRAY_SIZE(c
->key
->tex
); i
++) {
601 for (int j
= 0; j
< 4; j
++)
602 tex_options
.swizzles
[i
][j
] = c
->key
->tex
[i
].swizzle
[j
];
604 if (c
->key
->tex
[i
].clamp_s
)
605 tex_options
.saturate_s
|= 1 << i
;
606 if (c
->key
->tex
[i
].clamp_t
)
607 tex_options
.saturate_t
|= 1 << i
;
608 if (c
->key
->tex
[i
].clamp_r
)
609 tex_options
.saturate_r
|= 1 << i
;
610 if (c
->key
->tex
[i
].return_size
== 16) {
611 tex_options
.lower_tex_packing
[i
] =
612 nir_lower_tex_packing_16
;
616 NIR_PASS_V(c
->s
, nir_lower_tex
, &tex_options
);
617 NIR_PASS_V(c
->s
, nir_lower_system_values
);
621 v3d_set_prog_data_uniforms(struct v3d_compile
*c
,
622 struct v3d_prog_data
*prog_data
)
624 int count
= c
->num_uniforms
;
625 struct v3d_uniform_list
*ulist
= &prog_data
->uniforms
;
627 ulist
->count
= count
;
628 ulist
->data
= ralloc_array(prog_data
, uint32_t, count
);
629 memcpy(ulist
->data
, c
->uniform_data
,
630 count
* sizeof(*ulist
->data
));
631 ulist
->contents
= ralloc_array(prog_data
, enum quniform_contents
, count
);
632 memcpy(ulist
->contents
, c
->uniform_contents
,
633 count
* sizeof(*ulist
->contents
));
636 /* Copy the compiler UBO range state to the compiled shader, dropping out
637 * arrays that were never referenced by an indirect load.
639 * (Note that QIR dead code elimination of an array access still leaves that
640 * array alive, though)
643 v3d_set_prog_data_ubo(struct v3d_compile
*c
,
644 struct v3d_prog_data
*prog_data
)
646 if (!c
->num_ubo_ranges
)
649 prog_data
->num_ubo_ranges
= 0;
650 prog_data
->ubo_ranges
= ralloc_array(prog_data
, struct v3d_ubo_range
,
652 for (int i
= 0; i
< c
->num_ubo_ranges
; i
++) {
653 if (!c
->ubo_range_used
[i
])
656 struct v3d_ubo_range
*range
= &c
->ubo_ranges
[i
];
657 prog_data
->ubo_ranges
[prog_data
->num_ubo_ranges
++] = *range
;
658 prog_data
->ubo_size
+= range
->size
;
661 if (prog_data
->ubo_size
) {
662 if (V3D_DEBUG
& V3D_DEBUG_SHADERDB
) {
663 fprintf(stderr
, "SHADER-DB: %s prog %d/%d: %d UBO uniforms\n",
664 vir_get_stage_name(c
),
665 c
->program_id
, c
->variant_id
,
666 prog_data
->ubo_size
/ 4);
672 v3d_vs_set_prog_data(struct v3d_compile
*c
,
673 struct v3d_vs_prog_data
*prog_data
)
675 /* The vertex data gets format converted by the VPM so that
676 * each attribute channel takes up a VPM column. Precompute
677 * the sizes for the shader record.
679 for (int i
= 0; i
< ARRAY_SIZE(prog_data
->vattr_sizes
); i
++) {
680 prog_data
->vattr_sizes
[i
] = c
->vattr_sizes
[i
];
681 prog_data
->vpm_input_size
+= c
->vattr_sizes
[i
];
684 prog_data
->uses_vid
= (c
->s
->info
.system_values_read
&
685 (1ull << SYSTEM_VALUE_VERTEX_ID
));
686 prog_data
->uses_iid
= (c
->s
->info
.system_values_read
&
687 (1ull << SYSTEM_VALUE_INSTANCE_ID
));
689 if (prog_data
->uses_vid
)
690 prog_data
->vpm_input_size
++;
691 if (prog_data
->uses_iid
)
692 prog_data
->vpm_input_size
++;
694 /* Input/output segment size are in sectors (8 rows of 32 bits per
697 prog_data
->vpm_input_size
= align(prog_data
->vpm_input_size
, 8) / 8;
698 prog_data
->vpm_output_size
= align(c
->num_vpm_writes
, 8) / 8;
700 /* Set us up for shared input/output segments. This is apparently
701 * necessary for our VCM setup to avoid varying corruption.
703 prog_data
->separate_segments
= false;
704 prog_data
->vpm_output_size
= MAX2(prog_data
->vpm_output_size
,
705 prog_data
->vpm_input_size
);
706 prog_data
->vpm_input_size
= 0;
708 /* Compute VCM cache size. We set up our program to take up less than
709 * half of the VPM, so that any set of bin and render programs won't
710 * run out of space. We need space for at least one input segment,
711 * and then allocate the rest to output segments (one for the current
712 * program, the rest to VCM). The valid range of the VCM cache size
713 * field is 1-4 16-vertex batches, but GFXH-1744 limits us to 2-4
716 assert(c
->devinfo
->vpm_size
);
717 int sector_size
= 16 * sizeof(uint32_t) * 8;
718 int vpm_size_in_sectors
= c
->devinfo
->vpm_size
/ sector_size
;
719 int half_vpm
= vpm_size_in_sectors
/ 2;
720 int vpm_output_sectors
= half_vpm
- prog_data
->vpm_input_size
;
721 int vpm_output_batches
= vpm_output_sectors
/ prog_data
->vpm_output_size
;
722 assert(vpm_output_batches
>= 2);
723 prog_data
->vcm_cache_size
= CLAMP(vpm_output_batches
- 1, 2, 4);
727 v3d_set_fs_prog_data_inputs(struct v3d_compile
*c
,
728 struct v3d_fs_prog_data
*prog_data
)
730 prog_data
->num_inputs
= c
->num_inputs
;
731 memcpy(prog_data
->input_slots
, c
->input_slots
,
732 c
->num_inputs
* sizeof(*c
->input_slots
));
734 STATIC_ASSERT(ARRAY_SIZE(prog_data
->flat_shade_flags
) >
735 (V3D_MAX_FS_INPUTS
- 1) / 24);
736 for (int i
= 0; i
< V3D_MAX_FS_INPUTS
; i
++) {
737 if (BITSET_TEST(c
->flat_shade_flags
, i
))
738 prog_data
->flat_shade_flags
[i
/ 24] |= 1 << (i
% 24);
740 if (BITSET_TEST(c
->noperspective_flags
, i
))
741 prog_data
->noperspective_flags
[i
/ 24] |= 1 << (i
% 24);
743 if (BITSET_TEST(c
->centroid_flags
, i
))
744 prog_data
->centroid_flags
[i
/ 24] |= 1 << (i
% 24);
749 v3d_fs_set_prog_data(struct v3d_compile
*c
,
750 struct v3d_fs_prog_data
*prog_data
)
752 v3d_set_fs_prog_data_inputs(c
, prog_data
);
753 prog_data
->writes_z
= c
->writes_z
;
754 prog_data
->disable_ez
= !c
->s
->info
.fs
.early_fragment_tests
;
755 prog_data
->uses_center_w
= c
->uses_center_w
;
759 v3d_set_prog_data(struct v3d_compile
*c
,
760 struct v3d_prog_data
*prog_data
)
762 prog_data
->threads
= c
->threads
;
763 prog_data
->single_seg
= !c
->last_thrsw
;
764 prog_data
->spill_size
= c
->spill_size
;
766 v3d_set_prog_data_uniforms(c
, prog_data
);
767 v3d_set_prog_data_ubo(c
, prog_data
);
769 if (c
->s
->info
.stage
== MESA_SHADER_VERTEX
) {
770 v3d_vs_set_prog_data(c
, (struct v3d_vs_prog_data
*)prog_data
);
772 assert(c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
);
773 v3d_fs_set_prog_data(c
, (struct v3d_fs_prog_data
*)prog_data
);
778 v3d_return_qpu_insts(struct v3d_compile
*c
, uint32_t *final_assembly_size
)
780 *final_assembly_size
= c
->qpu_inst_count
* sizeof(uint64_t);
782 uint64_t *qpu_insts
= malloc(*final_assembly_size
);
786 memcpy(qpu_insts
, c
->qpu_insts
, *final_assembly_size
);
788 vir_compile_destroy(c
);
794 v3d_nir_lower_vs_early(struct v3d_compile
*c
)
796 /* Split our I/O vars and dead code eliminate the unused
799 NIR_PASS_V(c
->s
, nir_lower_io_to_scalar_early
,
800 nir_var_shader_in
| nir_var_shader_out
);
801 uint64_t used_outputs
[4] = {0};
802 for (int i
= 0; i
< c
->vs_key
->num_fs_inputs
; i
++) {
803 int slot
= v3d_slot_get_slot(c
->vs_key
->fs_inputs
[i
]);
804 int comp
= v3d_slot_get_component(c
->vs_key
->fs_inputs
[i
]);
805 used_outputs
[comp
] |= 1ull << slot
;
807 NIR_PASS_V(c
->s
, nir_remove_unused_io_vars
,
808 &c
->s
->outputs
, used_outputs
, NULL
); /* demotes to globals */
809 NIR_PASS_V(c
->s
, nir_lower_global_vars_to_local
);
810 v3d_optimize_nir(c
->s
);
811 NIR_PASS_V(c
->s
, nir_remove_dead_variables
, nir_var_shader_in
);
812 NIR_PASS_V(c
->s
, nir_lower_io
, nir_var_shader_in
| nir_var_shader_out
,
814 (nir_lower_io_options
)0);
818 v3d_fixup_fs_output_types(struct v3d_compile
*c
)
820 nir_foreach_variable(var
, &c
->s
->outputs
) {
823 switch (var
->data
.location
) {
824 case FRAG_RESULT_COLOR
:
827 case FRAG_RESULT_DATA0
:
828 case FRAG_RESULT_DATA1
:
829 case FRAG_RESULT_DATA2
:
830 case FRAG_RESULT_DATA3
:
831 mask
= 1 << (var
->data
.location
- FRAG_RESULT_DATA0
);
835 if (c
->fs_key
->int_color_rb
& mask
) {
837 glsl_vector_type(GLSL_TYPE_INT
,
838 glsl_get_components(var
->type
));
839 } else if (c
->fs_key
->uint_color_rb
& mask
) {
841 glsl_vector_type(GLSL_TYPE_UINT
,
842 glsl_get_components(var
->type
));
848 v3d_nir_lower_fs_early(struct v3d_compile
*c
)
850 if (c
->fs_key
->int_color_rb
|| c
->fs_key
->uint_color_rb
)
851 v3d_fixup_fs_output_types(c
);
853 /* If the shader has no non-TLB side effects, we can promote it to
854 * enabling early_fragment_tests even if the user didn't.
856 if (!(c
->s
->info
.num_images
||
857 c
->s
->info
.num_ssbos
||
858 c
->s
->info
.num_abos
)) {
859 c
->s
->info
.fs
.early_fragment_tests
= true;
864 v3d_nir_lower_vs_late(struct v3d_compile
*c
)
866 if (c
->vs_key
->clamp_color
)
867 NIR_PASS_V(c
->s
, nir_lower_clamp_color_outputs
);
869 if (c
->key
->ucp_enables
) {
870 NIR_PASS_V(c
->s
, nir_lower_clip_vs
, c
->key
->ucp_enables
,
872 NIR_PASS_V(c
->s
, nir_lower_io_to_scalar
,
876 /* Note: VS output scalarizing must happen after nir_lower_clip_vs. */
877 NIR_PASS_V(c
->s
, nir_lower_io_to_scalar
, nir_var_shader_out
);
881 v3d_nir_lower_fs_late(struct v3d_compile
*c
)
883 if (c
->fs_key
->light_twoside
)
884 NIR_PASS_V(c
->s
, nir_lower_two_sided_color
);
886 if (c
->fs_key
->clamp_color
)
887 NIR_PASS_V(c
->s
, nir_lower_clamp_color_outputs
);
889 if (c
->fs_key
->alpha_test
) {
890 NIR_PASS_V(c
->s
, nir_lower_alpha_test
,
891 c
->fs_key
->alpha_test_func
,
895 if (c
->key
->ucp_enables
)
896 NIR_PASS_V(c
->s
, nir_lower_clip_fs
, c
->key
->ucp_enables
);
898 /* Note: FS input scalarizing must happen after
899 * nir_lower_two_sided_color, which only handles a vec4 at a time.
901 NIR_PASS_V(c
->s
, nir_lower_io_to_scalar
, nir_var_shader_in
);
904 uint64_t *v3d_compile(const struct v3d_compiler
*compiler
,
906 struct v3d_prog_data
**out_prog_data
,
908 void (*debug_output
)(const char *msg
,
909 void *debug_output_data
),
910 void *debug_output_data
,
911 int program_id
, int variant_id
,
912 uint32_t *final_assembly_size
)
914 struct v3d_prog_data
*prog_data
;
915 struct v3d_compile
*c
= vir_compile_init(compiler
, key
, s
,
916 debug_output
, debug_output_data
,
917 program_id
, variant_id
);
919 switch (c
->s
->info
.stage
) {
920 case MESA_SHADER_VERTEX
:
921 c
->vs_key
= (struct v3d_vs_key
*)key
;
922 prog_data
= rzalloc_size(NULL
, sizeof(struct v3d_vs_prog_data
));
924 case MESA_SHADER_FRAGMENT
:
925 c
->fs_key
= (struct v3d_fs_key
*)key
;
926 prog_data
= rzalloc_size(NULL
, sizeof(struct v3d_fs_prog_data
));
929 unreachable("unsupported shader stage");
932 if (c
->s
->info
.stage
== MESA_SHADER_VERTEX
) {
933 v3d_nir_lower_vs_early(c
);
935 assert(c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
);
936 v3d_nir_lower_fs_early(c
);
941 if (c
->s
->info
.stage
== MESA_SHADER_VERTEX
) {
942 v3d_nir_lower_vs_late(c
);
944 assert(c
->s
->info
.stage
== MESA_SHADER_FRAGMENT
);
945 v3d_nir_lower_fs_late(c
);
948 NIR_PASS_V(c
->s
, v3d_nir_lower_io
, c
);
949 NIR_PASS_V(c
->s
, v3d_nir_lower_txf_ms
, c
);
950 NIR_PASS_V(c
->s
, v3d_nir_lower_image_load_store
);
951 NIR_PASS_V(c
->s
, nir_lower_idiv
);
953 v3d_optimize_nir(c
->s
);
954 NIR_PASS_V(c
->s
, nir_lower_bool_to_int32
);
955 NIR_PASS_V(c
->s
, nir_convert_from_ssa
, true);
959 v3d_set_prog_data(c
, prog_data
);
961 *out_prog_data
= prog_data
;
964 int ret
= asprintf(&shaderdb
,
965 "%s shader: %d inst, %d threads, %d loops, "
966 "%d uniforms, %d:%d spills:fills",
967 vir_get_stage_name(c
),
975 c
->debug_output(shaderdb
, c
->debug_output_data
);
979 return v3d_return_qpu_insts(c
, final_assembly_size
);
983 vir_remove_instruction(struct v3d_compile
*c
, struct qinst
*qinst
)
985 if (qinst
->dst
.file
== QFILE_TEMP
)
986 c
->defs
[qinst
->dst
.index
] = NULL
;
988 assert(&qinst
->link
!= c
->cursor
.link
);
990 list_del(&qinst
->link
);
993 c
->live_intervals_valid
= false;
997 vir_follow_movs(struct v3d_compile
*c
, struct qreg reg
)
1000 int pack = reg.pack;
1002 while (reg.file == QFILE_TEMP &&
1003 c->defs[reg.index] &&
1004 (c->defs[reg.index]->op == QOP_MOV ||
1005 c->defs[reg.index]->op == QOP_FMOV) &&
1006 !c->defs[reg.index]->dst.pack &&
1007 !c->defs[reg.index]->src[0].pack) {
1008 reg = c->defs[reg.index]->src[0];
1017 vir_compile_destroy(struct v3d_compile
*c
)
1019 /* Defuse the assert that we aren't removing the cursor's instruction.
1021 c
->cursor
.link
= NULL
;
1023 vir_for_each_block(block
, c
) {
1024 while (!list_empty(&block
->instructions
)) {
1025 struct qinst
*qinst
=
1026 list_first_entry(&block
->instructions
,
1027 struct qinst
, link
);
1028 vir_remove_instruction(c
, qinst
);
1036 vir_uniform(struct v3d_compile
*c
,
1037 enum quniform_contents contents
,
1040 for (int i
= 0; i
< c
->num_uniforms
; i
++) {
1041 if (c
->uniform_contents
[i
] == contents
&&
1042 c
->uniform_data
[i
] == data
) {
1043 return vir_reg(QFILE_UNIF
, i
);
1047 uint32_t uniform
= c
->num_uniforms
++;
1049 if (uniform
>= c
->uniform_array_size
) {
1050 c
->uniform_array_size
= MAX2(MAX2(16, uniform
+ 1),
1051 c
->uniform_array_size
* 2);
1053 c
->uniform_data
= reralloc(c
, c
->uniform_data
,
1055 c
->uniform_array_size
);
1056 c
->uniform_contents
= reralloc(c
, c
->uniform_contents
,
1057 enum quniform_contents
,
1058 c
->uniform_array_size
);
1061 c
->uniform_contents
[uniform
] = contents
;
1062 c
->uniform_data
[uniform
] = data
;
1064 return vir_reg(QFILE_UNIF
, uniform
);
1067 #define OPTPASS(func) \
1069 bool stage_progress = func(c); \
1070 if (stage_progress) { \
1072 if (print_opt_debug) { \
1074 "VIR opt pass %2d: %s progress\n", \
1077 /*XXX vir_validate(c);*/ \
1082 vir_optimize(struct v3d_compile
*c
)
1084 bool print_opt_debug
= false;
1088 bool progress
= false;
1090 OPTPASS(vir_opt_copy_propagate
);
1091 OPTPASS(vir_opt_dead_code
);
1092 OPTPASS(vir_opt_small_immediates
);
1102 vir_get_stage_name(struct v3d_compile
*c
)
1104 if (c
->vs_key
&& c
->vs_key
->is_coord
)
1105 return "MESA_SHADER_COORD";
1107 return gl_shader_stage_name(c
->s
->info
.stage
);