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.
66 * Stores an immediate value in the index field that will be used
67 * directly by qpu_load_imm().
72 * Stores an immediate value in the index field that can be turned
73 * into a small immediate field by qpu_encode_small_immediate().
84 static inline struct qreg
qir_reg(enum qfile file
, uint32_t index
)
86 return (struct qreg
){file
, index
};
134 /** Texture x coordinate parameter write */
136 /** Texture y coordinate parameter write */
138 /** Texture border color parameter or cube map z coordinate write */
140 /** Texture LOD bias parameter write */
144 * Texture-unit 4-byte read with address provided direct in S
147 * The first operand is the offset from the start of the UBO, and the
148 * second is the uniform that has the UBO's base pointer.
153 * Signal of texture read being necessary and then reading r4 into
160 /* Jumps to block->successor[0] if the qinst->cond (as a
161 * QPU_COND_BRANCH_*) passes, or block->successor[1] if not. Note
162 * that block->successor[1] may be unset if the condition is ALWAYS.
166 /* Emits an ADD from src[0] to src[1], where src[0] must be a
167 * QOP_LOAD_IMM result and src[1] is a QUNIFORM_UNIFORMS_ADDRESS,
168 * required by the kernel as part of its branch validation.
173 struct queued_qpu_inst
{
174 struct list_head link
;
179 struct list_head link
;
190 * Coordinate shader, runs during binning, before the VS, and just
198 enum quniform_contents
{
200 * Indicates that a constant 32-bit value is copied from the program's
205 * Indicates that the program's uniform contents are used as an index
206 * into the GL uniform storage.
211 * Scaling factors from clip coordinates to relative to the viewport
214 * This is used by the coordinate and vertex shaders to produce the
215 * 32-bit entry consisting of 2 16-bit fields with 12.4 signed fixed
216 * point offsets from the viewport ccenter.
218 QUNIFORM_VIEWPORT_X_SCALE
,
219 QUNIFORM_VIEWPORT_Y_SCALE
,
222 QUNIFORM_VIEWPORT_Z_OFFSET
,
223 QUNIFORM_VIEWPORT_Z_SCALE
,
225 QUNIFORM_USER_CLIP_PLANE
,
228 * A reference to a texture config parameter 0 uniform.
230 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
231 * defines texture type, miplevels, and such. It will be found as a
232 * parameter to the first QOP_TEX_[STRB] instruction in a sequence.
234 QUNIFORM_TEXTURE_CONFIG_P0
,
237 * A reference to a texture config parameter 1 uniform.
239 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
240 * defines texture width, height, filters, and wrap modes. It will be
241 * found as a parameter to the second QOP_TEX_[STRB] instruction in a
244 QUNIFORM_TEXTURE_CONFIG_P1
,
246 /** A reference to a texture config parameter 2 cubemap stride uniform */
247 QUNIFORM_TEXTURE_CONFIG_P2
,
249 QUNIFORM_TEXTURE_FIRST_LEVEL
,
251 QUNIFORM_TEXTURE_MSAA_ADDR
,
255 QUNIFORM_TEXRECT_SCALE_X
,
256 QUNIFORM_TEXRECT_SCALE_Y
,
258 QUNIFORM_TEXTURE_BORDER_COLOR
,
260 QUNIFORM_BLEND_CONST_COLOR_X
,
261 QUNIFORM_BLEND_CONST_COLOR_Y
,
262 QUNIFORM_BLEND_CONST_COLOR_Z
,
263 QUNIFORM_BLEND_CONST_COLOR_W
,
264 QUNIFORM_BLEND_CONST_COLOR_RGBA
,
265 QUNIFORM_BLEND_CONST_COLOR_AAAA
,
270 QUNIFORM_SAMPLE_MASK
,
272 /* Placeholder uniform that will be updated by the kernel when used by
273 * an instruction writing to QPU_W_UNIFORMS_ADDRESS.
275 QUNIFORM_UNIFORMS_ADDRESS
,
278 struct vc4_varying_slot
{
283 struct vc4_compiler_ubo_range
{
285 * offset in bytes from the start of the ubo where this range is
288 * Only set once used is set.
293 * offset in bytes from the start of the gallium uniforms where the
298 /** size in bytes of this ubo range */
302 * Set if this range is used by the shader for indirect uniforms
309 struct vc4_uncompiled_shader
*shader_state
;
311 enum pipe_format format
;
315 unsigned compare_mode
:1;
316 unsigned compare_func
:3;
319 bool force_first_level
:1;
322 uint16_t msaa_width
, msaa_height
;
325 } tex
[VC4_MAX_TEXTURE_SAMPLERS
];
331 enum pipe_format color_format
;
333 bool stencil_enabled
;
334 bool stencil_twoside
;
335 bool stencil_full_writemasks
;
339 bool point_coord_upper_left
;
342 bool sample_coverage
;
343 bool sample_alpha_to_coverage
;
344 bool sample_alpha_to_one
;
345 uint8_t alpha_test_func
;
346 uint8_t logicop_func
;
347 uint32_t point_sprite_mask
;
349 struct pipe_rt_blend_state blend
;
356 * This is a proxy for the array of FS input semantics, which is
357 * larger than we would want to put in the key.
359 uint64_t compiled_fs_id
;
361 enum pipe_format attr_formats
[8];
363 bool per_vertex_point_size
;
367 /** A basic block of QIR intructions. */
369 struct list_head link
;
371 struct list_head instructions
;
372 struct list_head qpu_inst_list
;
374 struct set
*predecessors
;
375 struct qblock
*successors
[2];
379 /* Instruction IPs for the first and last instruction of the block.
380 * Set by vc4_qpu_schedule.c.
382 uint32_t start_qpu_ip
;
385 /* Instruction IP for the branch instruction of the block. Set by
386 * vc4_qpu_schedule.c.
388 uint32_t branch_qpu_ip
;
390 /** @{ used by vc4_qir_live_variables.c */
393 BITSET_WORD
*live_in
;
394 BITSET_WORD
*live_out
;
395 int start_ip
, end_ip
;
400 struct vc4_context
*vc4
;
402 nir_function_impl
*impl
;
403 struct exec_list
*cf_node_list
;
406 * Mapping from nir_register * or nir_ssa_def * to array of struct
407 * qreg for the values.
409 struct hash_table
*def_ht
;
411 /* For each temp, the instruction generating its value. */
413 uint32_t defs_array_size
;
416 * Inputs to the shader, arranged by TGSI declaration order.
418 * Not all fragment shader QFILE_VARY reads are present in this array.
421 struct qreg
*outputs
;
422 bool msaa_per_sample_output
;
423 struct qreg color_reads
[VC4_MAX_SAMPLES
];
424 struct qreg sample_colors
[VC4_MAX_SAMPLES
];
425 uint32_t inputs_array_size
;
426 uint32_t outputs_array_size
;
427 uint32_t uniforms_array_size
;
429 struct vc4_compiler_ubo_range
*ubo_ranges
;
430 uint32_t ubo_ranges_array_size
;
431 /** Number of uniform areas declared in ubo_ranges. */
432 uint32_t num_uniform_ranges
;
433 /** Number of uniform areas used for indirect addressed loads. */
434 uint32_t num_ubo_ranges
;
435 uint32_t next_ubo_dst_offset
;
437 /* State for whether we're executing on each channel currently. 0 if
438 * yes, otherwise a block number + 1 that the channel jumped to.
442 struct qreg line_x
, point_x
, point_y
;
444 struct qreg payload_FRAG_Z
;
445 struct qreg payload_FRAG_W
;
447 uint8_t vattr_sizes
[8];
450 * Array of the VARYING_SLOT_* of all FS QFILE_VARY reads.
452 * This includes those that aren't part of the VPM varyings, like
453 * point/line coordinates.
455 struct vc4_varying_slot
*input_slots
;
456 uint32_t num_input_slots
;
457 uint32_t input_slots_array_size
;
460 * An entry per outputs[] in the VS indicating what the VARYING_SLOT_*
461 * of the output is. Used to emit from the VS in the order that the
464 struct vc4_varying_slot
*output_slots
;
466 struct pipe_shader_state
*shader_state
;
468 struct vc4_fs_key
*fs_key
;
469 struct vc4_vs_key
*vs_key
;
471 /* Live ranges of temps. */
472 int *temp_start
, *temp_end
;
474 uint32_t *uniform_data
;
475 enum quniform_contents
*uniform_contents
;
476 uint32_t uniform_array_size
;
477 uint32_t num_uniforms
;
478 uint32_t num_outputs
;
479 uint32_t num_texture_samples
;
480 uint32_t output_position_index
;
481 uint32_t output_color_index
;
482 uint32_t output_point_size_index
;
483 uint32_t output_sample_mask_index
;
489 struct list_head blocks
;
490 int next_block_index
;
491 struct qblock
*cur_block
;
492 struct qblock
*loop_cont_block
;
493 struct qblock
*loop_break_block
;
495 struct list_head qpu_inst_list
;
498 uint32_t qpu_inst_count
;
499 uint32_t qpu_inst_size
;
506 /* Special nir_load_input intrinsic index for loading the current TLB
509 #define VC4_NIR_TLB_COLOR_READ_INPUT 2000000000
511 #define VC4_NIR_MS_MASK_OUTPUT 2000000000
513 /* Special offset for nir_load_uniform values to get a QUNIFORM_*
514 * state-dependent value.
516 #define VC4_NIR_STATE_UNIFORM_OFFSET 1000000000
518 struct vc4_compile
*qir_compile_init(void);
519 void qir_compile_destroy(struct vc4_compile
*c
);
520 struct qblock
*qir_new_block(struct vc4_compile
*c
);
521 void qir_set_emit_block(struct vc4_compile
*c
, struct qblock
*block
);
522 void qir_link_blocks(struct qblock
*predecessor
, struct qblock
*successor
);
523 struct qblock
*qir_entry_block(struct vc4_compile
*c
);
524 struct qblock
*qir_exit_block(struct vc4_compile
*c
);
525 struct qinst
*qir_inst(enum qop op
, struct qreg dst
,
526 struct qreg src0
, struct qreg src1
);
527 struct qinst
*qir_inst4(enum qop op
, struct qreg dst
,
532 void qir_remove_instruction(struct vc4_compile
*c
, struct qinst
*qinst
);
533 struct qreg
qir_uniform(struct vc4_compile
*c
,
534 enum quniform_contents contents
,
536 void qir_schedule_instructions(struct vc4_compile
*c
);
537 void qir_reorder_uniforms(struct vc4_compile
*c
);
538 void qir_emit_uniform_stream_resets(struct vc4_compile
*c
);
540 struct qreg
qir_emit_def(struct vc4_compile
*c
, struct qinst
*inst
);
541 struct qinst
*qir_emit_nondef(struct vc4_compile
*c
, struct qinst
*inst
);
543 struct qreg
qir_get_temp(struct vc4_compile
*c
);
544 void qir_calculate_live_intervals(struct vc4_compile
*c
);
545 int qir_get_op_nsrc(enum qop qop
);
546 bool qir_reg_equals(struct qreg a
, struct qreg b
);
547 bool qir_has_side_effects(struct vc4_compile
*c
, struct qinst
*inst
);
548 bool qir_has_side_effect_reads(struct vc4_compile
*c
, struct qinst
*inst
);
549 bool qir_is_mul(struct qinst
*inst
);
550 bool qir_is_raw_mov(struct qinst
*inst
);
551 bool qir_is_tex(struct qinst
*inst
);
552 bool qir_is_float_input(struct qinst
*inst
);
553 bool qir_depends_on_flags(struct qinst
*inst
);
554 bool qir_writes_r4(struct qinst
*inst
);
555 struct qreg
qir_follow_movs(struct vc4_compile
*c
, struct qreg reg
);
556 uint8_t qir_channels_written(struct qinst
*inst
);
558 void qir_dump(struct vc4_compile
*c
);
559 void qir_dump_inst(struct vc4_compile
*c
, struct qinst
*inst
);
560 const char *qir_get_stage_name(enum qstage stage
);
562 void qir_validate(struct vc4_compile
*c
);
564 void qir_optimize(struct vc4_compile
*c
);
565 bool qir_opt_algebraic(struct vc4_compile
*c
);
566 bool qir_opt_constant_folding(struct vc4_compile
*c
);
567 bool qir_opt_copy_propagation(struct vc4_compile
*c
);
568 bool qir_opt_dead_code(struct vc4_compile
*c
);
569 bool qir_opt_peephole_sf(struct vc4_compile
*c
);
570 bool qir_opt_small_immediates(struct vc4_compile
*c
);
571 bool qir_opt_vpm(struct vc4_compile
*c
);
572 void vc4_nir_lower_blend(nir_shader
*s
, struct vc4_compile
*c
);
573 void vc4_nir_lower_io(nir_shader
*s
, struct vc4_compile
*c
);
574 nir_ssa_def
*vc4_nir_get_state_uniform(struct nir_builder
*b
,
575 enum quniform_contents contents
);
576 nir_ssa_def
*vc4_nir_get_swizzled_channel(struct nir_builder
*b
,
577 nir_ssa_def
**srcs
, int swiz
);
578 void vc4_nir_lower_txf_ms(nir_shader
*s
, struct vc4_compile
*c
);
579 void qir_lower_uniforms(struct vc4_compile
*c
);
581 uint32_t qpu_schedule_instructions(struct vc4_compile
*c
);
583 void qir_SF(struct vc4_compile
*c
, struct qreg src
);
585 static inline struct qreg
586 qir_uniform_ui(struct vc4_compile
*c
, uint32_t ui
)
588 return qir_uniform(c
, QUNIFORM_CONSTANT
, ui
);
591 static inline struct qreg
592 qir_uniform_f(struct vc4_compile
*c
, float f
)
594 return qir_uniform(c
, QUNIFORM_CONSTANT
, fui(f
));
597 #define QIR_ALU0(name) \
598 static inline struct qreg \
599 qir_##name(struct vc4_compile *c) \
601 return qir_emit_def(c, qir_inst(QOP_##name, c->undef, \
602 c->undef, c->undef)); \
604 static inline struct qinst * \
605 qir_##name##_dest(struct vc4_compile *c, struct qreg dest) \
607 return qir_emit_nondef(c, qir_inst(QOP_##name, dest, \
608 c->undef, c->undef)); \
611 #define QIR_ALU1(name) \
612 static inline struct qreg \
613 qir_##name(struct vc4_compile *c, struct qreg a) \
615 return qir_emit_def(c, qir_inst(QOP_##name, c->undef, \
618 static inline struct qinst * \
619 qir_##name##_dest(struct vc4_compile *c, struct qreg dest, \
622 return qir_emit_nondef(c, qir_inst(QOP_##name, dest, a, \
626 #define QIR_ALU2(name) \
627 static inline struct qreg \
628 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
630 return qir_emit_def(c, qir_inst(QOP_##name, c->undef, a, b)); \
632 static inline struct qinst * \
633 qir_##name##_dest(struct vc4_compile *c, struct qreg dest, \
634 struct qreg a, struct qreg b) \
636 return qir_emit_nondef(c, qir_inst(QOP_##name, dest, a, b)); \
639 #define QIR_NODST_1(name) \
640 static inline struct qinst * \
641 qir_##name(struct vc4_compile *c, struct qreg a) \
643 return qir_emit_nondef(c, qir_inst(QOP_##name, c->undef, \
647 #define QIR_NODST_2(name) \
648 static inline struct qinst * \
649 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
651 return qir_emit_nondef(c, qir_inst(QOP_##name, c->undef, \
655 #define QIR_PAYLOAD(name) \
656 static inline struct qreg \
657 qir_##name(struct vc4_compile *c) \
659 struct qreg *payload = &c->payload_##name; \
660 if (payload->file != QFILE_NULL) \
662 *payload = qir_get_temp(c); \
663 struct qinst *inst = qir_inst(QOP_##name, *payload, \
664 c->undef, c->undef); \
665 struct qblock *entry = qir_entry_block(c); \
666 list_add(&inst->link, &entry->instructions); \
667 c->defs[payload->index] = inst; \
711 QIR_NODST_2(TEX_DIRECT
)
715 QIR_ALU0(TLB_COLOR_READ
)
718 static inline struct qreg
719 qir_SEL(struct vc4_compile
*c
, uint8_t cond
, struct qreg src0
, struct qreg src1
)
721 struct qreg t
= qir_get_temp(c
);
722 struct qinst
*a
= qir_MOV_dest(c
, t
, src0
);
723 struct qinst
*b
= qir_MOV_dest(c
, t
, src1
);
725 b
->cond
= qpu_cond_complement(cond
);
729 static inline struct qreg
730 qir_UNPACK_8_F(struct vc4_compile
*c
, struct qreg src
, int i
)
732 struct qreg t
= qir_FMOV(c
, src
);
733 c
->defs
[t
.index
]->src
[0].pack
= QPU_UNPACK_8A
+ i
;
737 static inline struct qreg
738 qir_UNPACK_8_I(struct vc4_compile
*c
, struct qreg src
, int i
)
740 struct qreg t
= qir_MOV(c
, src
);
741 c
->defs
[t
.index
]->src
[0].pack
= QPU_UNPACK_8A
+ i
;
745 static inline struct qreg
746 qir_UNPACK_16_F(struct vc4_compile
*c
, struct qreg src
, int i
)
748 struct qreg t
= qir_FMOV(c
, src
);
749 c
->defs
[t
.index
]->src
[0].pack
= QPU_UNPACK_16A
+ i
;
753 static inline struct qreg
754 qir_UNPACK_16_I(struct vc4_compile
*c
, struct qreg src
, int i
)
756 struct qreg t
= qir_MOV(c
, src
);
757 c
->defs
[t
.index
]->src
[0].pack
= QPU_UNPACK_16A
+ i
;
762 qir_PACK_8_F(struct vc4_compile
*c
, struct qreg dest
, struct qreg val
, int chan
)
765 dest
.pack
= QPU_PACK_MUL_8A
+ chan
;
766 qir_emit_nondef(c
, qir_inst(QOP_MMOV
, dest
, val
, c
->undef
));
769 static inline struct qreg
770 qir_PACK_8888_F(struct vc4_compile
*c
, struct qreg val
)
772 struct qreg dest
= qir_MMOV(c
, val
);
773 c
->defs
[dest
.index
]->dst
.pack
= QPU_PACK_MUL_8888
;
777 static inline struct qreg
778 qir_POW(struct vc4_compile
*c
, struct qreg x
, struct qreg y
)
780 return qir_EXP2(c
, qir_FMUL(c
,
786 qir_VPM_WRITE(struct vc4_compile
*c
, struct qreg val
)
788 qir_MOV_dest(c
, qir_reg(QFILE_VPM
, 0), val
);
791 static inline struct qreg
792 qir_LOAD_IMM(struct vc4_compile
*c
, uint32_t val
)
794 return qir_emit_def(c
, qir_inst(QOP_LOAD_IMM
, c
->undef
,
795 qir_reg(QFILE_LOAD_IMM
, val
), c
->undef
));
799 qir_MOV_cond(struct vc4_compile
*c
, uint8_t cond
,
800 struct qreg dest
, struct qreg src
)
802 qir_MOV_dest(c
, dest
, src
)->cond
= cond
;
805 static inline struct qinst
*
806 qir_BRANCH(struct vc4_compile
*c
, uint8_t cond
)
808 struct qinst
*inst
= qir_inst(QOP_BRANCH
, c
->undef
, c
->undef
, c
->undef
);
810 qir_emit_nondef(c
, inst
);
814 #define qir_for_each_block(block, c) \
815 list_for_each_entry(struct qblock, block, &c->blocks, link)
817 #define qir_for_each_block_rev(block, c) \
818 list_for_each_entry_rev(struct qblock, block, &c->blocks, link)
820 /* Loop over the non-NULL members of the successors array. */
821 #define qir_for_each_successor(succ, block) \
822 for (struct qblock *succ = block->successors[0]; \
824 succ = (succ == block->successors[1] ? NULL : \
825 block->successors[1]))
827 #define qir_for_each_inst(inst, block) \
828 list_for_each_entry(struct qinst, inst, &block->instructions, link)
830 #define qir_for_each_inst_rev(inst, block) \
831 list_for_each_entry_rev(struct qinst, inst, &block->instructions, link)
833 #define qir_for_each_inst_safe(inst, block) \
834 list_for_each_entry_safe(struct qinst, inst, &block->instructions, link)
836 #define qir_for_each_inst_inorder(inst, c) \
837 qir_for_each_block(_block, c) \
838 qir_for_each_inst(inst, _block)
840 #endif /* VC4_QIR_H */