2 * Copyright © 2014 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
34 #include "util/macros.h"
35 #include "compiler/nir/nir.h"
36 #include "util/list.h"
37 #include "util/u_math.h"
39 #include "vc4_screen.h"
40 #include "vc4_qpu_defines.h"
42 #include "kernel/vc4_packet.h"
43 #include "pipe/p_state.h"
53 QFILE_TLB_COLOR_WRITE
,
54 QFILE_TLB_COLOR_WRITE_MS
,
56 QFILE_TLB_STENCIL_SETUP
,
58 /* Payload registers that aren't in the physical register file, so we
59 * can just use the corresponding qpu_reg at qpu_emit time.
67 * Stores an immediate value in the index field that will be used
68 * directly by qpu_load_imm().
73 * Stores an immediate value in the index field that can be turned
74 * into a small immediate field by qpu_encode_small_immediate().
85 static inline struct qreg
qir_reg(enum qfile file
, uint32_t index
)
87 return (struct qreg
){file
, index
};
135 /** Texture x coordinate parameter write */
137 /** Texture y coordinate parameter write */
139 /** Texture border color parameter or cube map z coordinate write */
141 /** Texture LOD bias parameter write */
145 * Texture-unit 4-byte read with address provided direct in S
148 * The first operand is the offset from the start of the UBO, and the
149 * second is the uniform that has the UBO's base pointer.
154 * Signal of texture read being necessary and then reading r4 into
159 /* 32-bit immediate loaded to each SIMD channel */
162 /* 32-bit immediate divided into 16 2-bit unsigned int values and
163 * loaded to each corresponding SIMD channel.
166 /* 32-bit immediate divided into 16 2-bit signed int values and
167 * loaded to each corresponding SIMD channel.
173 /* Jumps to block->successor[0] if the qinst->cond (as a
174 * QPU_COND_BRANCH_*) passes, or block->successor[1] if not. Note
175 * that block->successor[1] may be unset if the condition is ALWAYS.
179 /* Emits an ADD from src[0] to src[1], where src[0] must be a
180 * QOP_LOAD_IMM result and src[1] is a QUNIFORM_UNIFORMS_ADDRESS,
181 * required by the kernel as part of its branch validation.
186 struct queued_qpu_inst
{
187 struct list_head link
;
192 struct list_head link
;
203 * Coordinate shader, runs during binning, before the VS, and just
211 enum quniform_contents
{
213 * Indicates that a constant 32-bit value is copied from the program's
218 * Indicates that the program's uniform contents are used as an index
219 * into the GL uniform storage.
224 * Scaling factors from clip coordinates to relative to the viewport
227 * This is used by the coordinate and vertex shaders to produce the
228 * 32-bit entry consisting of 2 16-bit fields with 12.4 signed fixed
229 * point offsets from the viewport ccenter.
231 QUNIFORM_VIEWPORT_X_SCALE
,
232 QUNIFORM_VIEWPORT_Y_SCALE
,
235 QUNIFORM_VIEWPORT_Z_OFFSET
,
236 QUNIFORM_VIEWPORT_Z_SCALE
,
238 QUNIFORM_USER_CLIP_PLANE
,
241 * A reference to a texture config parameter 0 uniform.
243 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
244 * defines texture type, miplevels, and such. It will be found as a
245 * parameter to the first QOP_TEX_[STRB] instruction in a sequence.
247 QUNIFORM_TEXTURE_CONFIG_P0
,
250 * A reference to a texture config parameter 1 uniform.
252 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
253 * defines texture width, height, filters, and wrap modes. It will be
254 * found as a parameter to the second QOP_TEX_[STRB] instruction in a
257 QUNIFORM_TEXTURE_CONFIG_P1
,
259 /** A reference to a texture config parameter 2 cubemap stride uniform */
260 QUNIFORM_TEXTURE_CONFIG_P2
,
262 QUNIFORM_TEXTURE_FIRST_LEVEL
,
264 QUNIFORM_TEXTURE_MSAA_ADDR
,
268 QUNIFORM_TEXRECT_SCALE_X
,
269 QUNIFORM_TEXRECT_SCALE_Y
,
271 QUNIFORM_TEXTURE_BORDER_COLOR
,
273 QUNIFORM_BLEND_CONST_COLOR_X
,
274 QUNIFORM_BLEND_CONST_COLOR_Y
,
275 QUNIFORM_BLEND_CONST_COLOR_Z
,
276 QUNIFORM_BLEND_CONST_COLOR_W
,
277 QUNIFORM_BLEND_CONST_COLOR_RGBA
,
278 QUNIFORM_BLEND_CONST_COLOR_AAAA
,
283 QUNIFORM_SAMPLE_MASK
,
285 /* Placeholder uniform that will be updated by the kernel when used by
286 * an instruction writing to QPU_W_UNIFORMS_ADDRESS.
288 QUNIFORM_UNIFORMS_ADDRESS
,
291 struct vc4_varying_slot
{
296 struct vc4_compiler_ubo_range
{
298 * offset in bytes from the start of the ubo where this range is
301 * Only set once used is set.
306 * offset in bytes from the start of the gallium uniforms where the
311 /** size in bytes of this ubo range */
315 * Set if this range is used by the shader for indirect uniforms
322 struct vc4_uncompiled_shader
*shader_state
;
324 enum pipe_format format
;
328 unsigned compare_mode
:1;
329 unsigned compare_func
:3;
332 bool force_first_level
:1;
335 uint16_t msaa_width
, msaa_height
;
338 } tex
[VC4_MAX_TEXTURE_SAMPLERS
];
344 enum pipe_format color_format
;
346 bool stencil_enabled
;
347 bool stencil_twoside
;
348 bool stencil_full_writemasks
;
352 bool point_coord_upper_left
;
355 bool sample_coverage
;
356 bool sample_alpha_to_coverage
;
357 bool sample_alpha_to_one
;
358 uint8_t alpha_test_func
;
359 uint8_t logicop_func
;
360 uint32_t point_sprite_mask
;
362 struct pipe_rt_blend_state blend
;
368 const struct vc4_fs_inputs
*fs_inputs
;
369 enum pipe_format attr_formats
[8];
371 bool per_vertex_point_size
;
375 /** A basic block of QIR intructions. */
377 struct list_head link
;
379 struct list_head instructions
;
380 struct list_head qpu_inst_list
;
382 struct set
*predecessors
;
383 struct qblock
*successors
[2];
387 /* Instruction IPs for the first and last instruction of the block.
388 * Set by vc4_qpu_schedule.c.
390 uint32_t start_qpu_ip
;
393 /* Instruction IP for the branch instruction of the block. Set by
394 * vc4_qpu_schedule.c.
396 uint32_t branch_qpu_ip
;
398 /** @{ used by vc4_qir_live_variables.c */
401 BITSET_WORD
*live_in
;
402 BITSET_WORD
*live_out
;
403 int start_ip
, end_ip
;
408 struct vc4_context
*vc4
;
410 nir_function_impl
*impl
;
411 struct exec_list
*cf_node_list
;
414 * Mapping from nir_register * or nir_ssa_def * to array of struct
415 * qreg for the values.
417 struct hash_table
*def_ht
;
419 /* For each temp, the instruction generating its value. */
421 uint32_t defs_array_size
;
424 * Inputs to the shader, arranged by TGSI declaration order.
426 * Not all fragment shader QFILE_VARY reads are present in this array.
429 struct qreg
*outputs
;
430 bool msaa_per_sample_output
;
431 struct qreg color_reads
[VC4_MAX_SAMPLES
];
432 struct qreg sample_colors
[VC4_MAX_SAMPLES
];
433 uint32_t inputs_array_size
;
434 uint32_t outputs_array_size
;
435 uint32_t uniforms_array_size
;
437 struct vc4_compiler_ubo_range
*ubo_ranges
;
438 uint32_t ubo_ranges_array_size
;
439 /** Number of uniform areas declared in ubo_ranges. */
440 uint32_t num_uniform_ranges
;
441 /** Number of uniform areas used for indirect addressed loads. */
442 uint32_t num_ubo_ranges
;
443 uint32_t next_ubo_dst_offset
;
445 /* State for whether we're executing on each channel currently. 0 if
446 * yes, otherwise a block number + 1 that the channel jumped to.
450 struct qreg line_x
, point_x
, point_y
;
451 /** boolean (~0 -> true) if the fragment has been discarded. */
453 struct qreg payload_FRAG_Z
;
454 struct qreg payload_FRAG_W
;
456 uint8_t vattr_sizes
[8];
459 * Array of the VARYING_SLOT_* of all FS QFILE_VARY reads.
461 * This includes those that aren't part of the VPM varyings, like
462 * point/line coordinates.
464 struct vc4_varying_slot
*input_slots
;
465 uint32_t num_input_slots
;
466 uint32_t input_slots_array_size
;
469 * An entry per outputs[] in the VS indicating what the VARYING_SLOT_*
470 * of the output is. Used to emit from the VS in the order that the
473 struct vc4_varying_slot
*output_slots
;
475 struct pipe_shader_state
*shader_state
;
477 struct vc4_fs_key
*fs_key
;
478 struct vc4_vs_key
*vs_key
;
480 /* Live ranges of temps. */
481 int *temp_start
, *temp_end
;
483 uint32_t *uniform_data
;
484 enum quniform_contents
*uniform_contents
;
485 uint32_t uniform_array_size
;
486 uint32_t num_uniforms
;
487 uint32_t num_outputs
;
488 uint32_t num_texture_samples
;
489 uint32_t output_position_index
;
490 uint32_t output_color_index
;
491 uint32_t output_point_size_index
;
492 uint32_t output_sample_mask_index
;
498 struct list_head blocks
;
499 int next_block_index
;
500 struct qblock
*cur_block
;
501 struct qblock
*loop_cont_block
;
502 struct qblock
*loop_break_block
;
504 struct list_head qpu_inst_list
;
507 uint32_t qpu_inst_count
;
508 uint32_t qpu_inst_size
;
512 * Number of inputs from num_inputs remaining to be queued to the read
515 uint32_t num_inputs_remaining
;
517 /* Number of inputs currently in the read FIFO for the VS/CS */
518 uint32_t num_inputs_in_fifo
;
520 /** Next offset in the VPM to read from in the VS/CS */
521 uint32_t vpm_read_offset
;
527 /* Special nir_load_input intrinsic index for loading the current TLB
530 #define VC4_NIR_TLB_COLOR_READ_INPUT 2000000000
532 #define VC4_NIR_MS_MASK_OUTPUT 2000000000
534 struct vc4_compile
*qir_compile_init(void);
535 void qir_compile_destroy(struct vc4_compile
*c
);
536 struct qblock
*qir_new_block(struct vc4_compile
*c
);
537 void qir_set_emit_block(struct vc4_compile
*c
, struct qblock
*block
);
538 void qir_link_blocks(struct qblock
*predecessor
, struct qblock
*successor
);
539 struct qblock
*qir_entry_block(struct vc4_compile
*c
);
540 struct qblock
*qir_exit_block(struct vc4_compile
*c
);
541 struct qinst
*qir_inst(enum qop op
, struct qreg dst
,
542 struct qreg src0
, struct qreg src1
);
543 struct qinst
*qir_inst4(enum qop op
, struct qreg dst
,
548 void qir_remove_instruction(struct vc4_compile
*c
, struct qinst
*qinst
);
549 struct qreg
qir_uniform(struct vc4_compile
*c
,
550 enum quniform_contents contents
,
552 void qir_schedule_instructions(struct vc4_compile
*c
);
553 void qir_reorder_uniforms(struct vc4_compile
*c
);
554 void qir_emit_uniform_stream_resets(struct vc4_compile
*c
);
556 struct qreg
qir_emit_def(struct vc4_compile
*c
, struct qinst
*inst
);
557 struct qinst
*qir_emit_nondef(struct vc4_compile
*c
, struct qinst
*inst
);
559 struct qreg
qir_get_temp(struct vc4_compile
*c
);
560 void qir_calculate_live_intervals(struct vc4_compile
*c
);
561 int qir_get_op_nsrc(enum qop qop
);
562 bool qir_reg_equals(struct qreg a
, struct qreg b
);
563 bool qir_has_side_effects(struct vc4_compile
*c
, struct qinst
*inst
);
564 bool qir_has_side_effect_reads(struct vc4_compile
*c
, struct qinst
*inst
);
565 bool qir_is_mul(struct qinst
*inst
);
566 bool qir_is_raw_mov(struct qinst
*inst
);
567 bool qir_is_tex(struct qinst
*inst
);
568 bool qir_is_float_input(struct qinst
*inst
);
569 bool qir_depends_on_flags(struct qinst
*inst
);
570 bool qir_writes_r4(struct qinst
*inst
);
571 struct qreg
qir_follow_movs(struct vc4_compile
*c
, struct qreg reg
);
572 uint8_t qir_channels_written(struct qinst
*inst
);
574 void qir_dump(struct vc4_compile
*c
);
575 void qir_dump_inst(struct vc4_compile
*c
, struct qinst
*inst
);
576 const char *qir_get_stage_name(enum qstage stage
);
578 void qir_validate(struct vc4_compile
*c
);
580 void qir_optimize(struct vc4_compile
*c
);
581 bool qir_opt_algebraic(struct vc4_compile
*c
);
582 bool qir_opt_constant_folding(struct vc4_compile
*c
);
583 bool qir_opt_copy_propagation(struct vc4_compile
*c
);
584 bool qir_opt_dead_code(struct vc4_compile
*c
);
585 bool qir_opt_peephole_sf(struct vc4_compile
*c
);
586 bool qir_opt_small_immediates(struct vc4_compile
*c
);
587 bool qir_opt_vpm(struct vc4_compile
*c
);
588 void vc4_nir_lower_blend(nir_shader
*s
, struct vc4_compile
*c
);
589 void vc4_nir_lower_io(nir_shader
*s
, struct vc4_compile
*c
);
590 nir_ssa_def
*vc4_nir_get_swizzled_channel(struct nir_builder
*b
,
591 nir_ssa_def
**srcs
, int swiz
);
592 void vc4_nir_lower_txf_ms(nir_shader
*s
, struct vc4_compile
*c
);
593 void qir_lower_uniforms(struct vc4_compile
*c
);
595 uint32_t qpu_schedule_instructions(struct vc4_compile
*c
);
597 void qir_SF(struct vc4_compile
*c
, struct qreg src
);
599 static inline struct qreg
600 qir_uniform_ui(struct vc4_compile
*c
, uint32_t ui
)
602 return qir_uniform(c
, QUNIFORM_CONSTANT
, ui
);
605 static inline struct qreg
606 qir_uniform_f(struct vc4_compile
*c
, float f
)
608 return qir_uniform(c
, QUNIFORM_CONSTANT
, fui(f
));
611 #define QIR_ALU0(name) \
612 static inline struct qreg \
613 qir_##name(struct vc4_compile *c) \
615 return qir_emit_def(c, qir_inst(QOP_##name, c->undef, \
616 c->undef, c->undef)); \
618 static inline struct qinst * \
619 qir_##name##_dest(struct vc4_compile *c, struct qreg dest) \
621 return qir_emit_nondef(c, qir_inst(QOP_##name, dest, \
622 c->undef, c->undef)); \
625 #define QIR_ALU1(name) \
626 static inline struct qreg \
627 qir_##name(struct vc4_compile *c, struct qreg a) \
629 return qir_emit_def(c, qir_inst(QOP_##name, c->undef, \
632 static inline struct qinst * \
633 qir_##name##_dest(struct vc4_compile *c, struct qreg dest, \
636 return qir_emit_nondef(c, qir_inst(QOP_##name, dest, a, \
640 #define QIR_ALU2(name) \
641 static inline struct qreg \
642 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
644 return qir_emit_def(c, qir_inst(QOP_##name, c->undef, a, b)); \
646 static inline struct qinst * \
647 qir_##name##_dest(struct vc4_compile *c, struct qreg dest, \
648 struct qreg a, struct qreg b) \
650 return qir_emit_nondef(c, qir_inst(QOP_##name, dest, a, b)); \
653 #define QIR_NODST_1(name) \
654 static inline struct qinst * \
655 qir_##name(struct vc4_compile *c, struct qreg a) \
657 return qir_emit_nondef(c, qir_inst(QOP_##name, c->undef, \
661 #define QIR_NODST_2(name) \
662 static inline struct qinst * \
663 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
665 return qir_emit_nondef(c, qir_inst(QOP_##name, c->undef, \
669 #define QIR_PAYLOAD(name) \
670 static inline struct qreg \
671 qir_##name(struct vc4_compile *c) \
673 struct qreg *payload = &c->payload_##name; \
674 if (payload->file != QFILE_NULL) \
676 *payload = qir_get_temp(c); \
677 struct qinst *inst = qir_inst(QOP_##name, *payload, \
678 c->undef, c->undef); \
679 struct qblock *entry = qir_entry_block(c); \
680 list_add(&inst->link, &entry->instructions); \
681 c->defs[payload->index] = inst; \
725 QIR_NODST_2(TEX_DIRECT
)
729 QIR_ALU0(TLB_COLOR_READ
)
732 static inline struct qreg
733 qir_SEL(struct vc4_compile
*c
, uint8_t cond
, struct qreg src0
, struct qreg src1
)
735 struct qreg t
= qir_get_temp(c
);
736 struct qinst
*a
= qir_MOV_dest(c
, t
, src0
);
737 struct qinst
*b
= qir_MOV_dest(c
, t
, src1
);
739 b
->cond
= qpu_cond_complement(cond
);
743 static inline struct qreg
744 qir_UNPACK_8_F(struct vc4_compile
*c
, struct qreg src
, int i
)
746 struct qreg t
= qir_FMOV(c
, src
);
747 c
->defs
[t
.index
]->src
[0].pack
= QPU_UNPACK_8A
+ i
;
751 static inline struct qreg
752 qir_UNPACK_8_I(struct vc4_compile
*c
, struct qreg src
, int i
)
754 struct qreg t
= qir_MOV(c
, src
);
755 c
->defs
[t
.index
]->src
[0].pack
= QPU_UNPACK_8A
+ i
;
759 static inline struct qreg
760 qir_UNPACK_16_F(struct vc4_compile
*c
, struct qreg src
, int i
)
762 struct qreg t
= qir_FMOV(c
, src
);
763 c
->defs
[t
.index
]->src
[0].pack
= QPU_UNPACK_16A
+ i
;
767 static inline struct qreg
768 qir_UNPACK_16_I(struct vc4_compile
*c
, struct qreg src
, int i
)
770 struct qreg t
= qir_MOV(c
, src
);
771 c
->defs
[t
.index
]->src
[0].pack
= QPU_UNPACK_16A
+ i
;
776 qir_PACK_8_F(struct vc4_compile
*c
, struct qreg dest
, struct qreg val
, int chan
)
779 dest
.pack
= QPU_PACK_MUL_8A
+ chan
;
780 qir_emit_nondef(c
, qir_inst(QOP_MMOV
, dest
, val
, c
->undef
));
783 static inline struct qreg
784 qir_PACK_8888_F(struct vc4_compile
*c
, struct qreg val
)
786 struct qreg dest
= qir_MMOV(c
, val
);
787 c
->defs
[dest
.index
]->dst
.pack
= QPU_PACK_MUL_8888
;
791 static inline struct qreg
792 qir_POW(struct vc4_compile
*c
, struct qreg x
, struct qreg y
)
794 return qir_EXP2(c
, qir_FMUL(c
,
800 qir_VPM_WRITE(struct vc4_compile
*c
, struct qreg val
)
802 qir_MOV_dest(c
, qir_reg(QFILE_VPM
, 0), val
);
805 static inline struct qreg
806 qir_LOAD_IMM(struct vc4_compile
*c
, uint32_t val
)
808 return qir_emit_def(c
, qir_inst(QOP_LOAD_IMM
, c
->undef
,
809 qir_reg(QFILE_LOAD_IMM
, val
), c
->undef
));
812 static inline struct qreg
813 qir_LOAD_IMM_U2(struct vc4_compile
*c
, uint32_t val
)
815 return qir_emit_def(c
, qir_inst(QOP_LOAD_IMM_U2
, c
->undef
,
816 qir_reg(QFILE_LOAD_IMM
, val
),
820 static inline struct qreg
821 qir_LOAD_IMM_I2(struct vc4_compile
*c
, uint32_t val
)
823 return qir_emit_def(c
, qir_inst(QOP_LOAD_IMM_I2
, c
->undef
,
824 qir_reg(QFILE_LOAD_IMM
, val
),
828 /** Shifts the multiply output to the right by rot channels */
829 static inline struct qreg
830 qir_ROT_MUL(struct vc4_compile
*c
, struct qreg val
, uint32_t rot
)
832 return qir_emit_def(c
, qir_inst(QOP_ROT_MUL
, c
->undef
,
834 qir_reg(QFILE_LOAD_IMM
,
835 QPU_SMALL_IMM_MUL_ROT
+ rot
)));
839 qir_MOV_cond(struct vc4_compile
*c
, uint8_t cond
,
840 struct qreg dest
, struct qreg src
)
842 qir_MOV_dest(c
, dest
, src
)->cond
= cond
;
845 static inline struct qinst
*
846 qir_BRANCH(struct vc4_compile
*c
, uint8_t cond
)
848 struct qinst
*inst
= qir_inst(QOP_BRANCH
, c
->undef
, c
->undef
, c
->undef
);
850 qir_emit_nondef(c
, inst
);
854 #define qir_for_each_block(block, c) \
855 list_for_each_entry(struct qblock, block, &c->blocks, link)
857 #define qir_for_each_block_rev(block, c) \
858 list_for_each_entry_rev(struct qblock, block, &c->blocks, link)
860 /* Loop over the non-NULL members of the successors array. */
861 #define qir_for_each_successor(succ, block) \
862 for (struct qblock *succ = block->successors[0]; \
864 succ = (succ == block->successors[1] ? NULL : \
865 block->successors[1]))
867 #define qir_for_each_inst(inst, block) \
868 list_for_each_entry(struct qinst, inst, &block->instructions, link)
870 #define qir_for_each_inst_rev(inst, block) \
871 list_for_each_entry_rev(struct qinst, inst, &block->instructions, link)
873 #define qir_for_each_inst_safe(inst, block) \
874 list_for_each_entry_safe(struct qinst, inst, &block->instructions, link)
876 #define qir_for_each_inst_inorder(inst, c) \
877 qir_for_each_block(_block, c) \
878 qir_for_each_inst(inst, _block)
880 #endif /* VC4_QIR_H */