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"
41 #include "kernel/vc4_packet.h"
42 #include "pipe/p_state.h"
52 QFILE_TLB_COLOR_WRITE
,
53 QFILE_TLB_COLOR_WRITE_MS
,
55 QFILE_TLB_STENCIL_SETUP
,
57 /* Payload registers that aren't in the physical register file, so we
58 * can just use the corresponding qpu_reg at qpu_emit time.
65 * Stores an immediate value in the index field that will be used
66 * directly by qpu_load_imm().
71 * Stores an immediate value in the index field that can be turned
72 * into a small immediate field by qpu_encode_small_immediate().
83 static inline struct qreg
qir_reg(enum qfile file
, uint32_t index
)
85 return (struct qreg
){file
, index
};
133 /** Texture x coordinate parameter write */
135 /** Texture y coordinate parameter write */
137 /** Texture border color parameter or cube map z coordinate write */
139 /** Texture LOD bias parameter write */
143 * Texture-unit 4-byte read with address provided direct in S
146 * The first operand is the offset from the start of the UBO, and the
147 * second is the uniform that has the UBO's base pointer.
152 * Signal of texture read being necessary and then reading r4 into
160 struct queued_qpu_inst
{
161 struct list_head link
;
166 struct list_head link
;
177 * Coordinate shader, runs during binning, before the VS, and just
185 enum quniform_contents
{
187 * Indicates that a constant 32-bit value is copied from the program's
192 * Indicates that the program's uniform contents are used as an index
193 * into the GL uniform storage.
198 * Scaling factors from clip coordinates to relative to the viewport
201 * This is used by the coordinate and vertex shaders to produce the
202 * 32-bit entry consisting of 2 16-bit fields with 12.4 signed fixed
203 * point offsets from the viewport ccenter.
205 QUNIFORM_VIEWPORT_X_SCALE
,
206 QUNIFORM_VIEWPORT_Y_SCALE
,
209 QUNIFORM_VIEWPORT_Z_OFFSET
,
210 QUNIFORM_VIEWPORT_Z_SCALE
,
212 QUNIFORM_USER_CLIP_PLANE
,
215 * A reference to a texture config parameter 0 uniform.
217 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
218 * defines texture type, miplevels, and such. It will be found as a
219 * parameter to the first QOP_TEX_[STRB] instruction in a sequence.
221 QUNIFORM_TEXTURE_CONFIG_P0
,
224 * A reference to a texture config parameter 1 uniform.
226 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
227 * defines texture width, height, filters, and wrap modes. It will be
228 * found as a parameter to the second QOP_TEX_[STRB] instruction in a
231 QUNIFORM_TEXTURE_CONFIG_P1
,
233 /** A reference to a texture config parameter 2 cubemap stride uniform */
234 QUNIFORM_TEXTURE_CONFIG_P2
,
236 QUNIFORM_TEXTURE_MSAA_ADDR
,
240 QUNIFORM_TEXRECT_SCALE_X
,
241 QUNIFORM_TEXRECT_SCALE_Y
,
243 QUNIFORM_TEXTURE_BORDER_COLOR
,
245 QUNIFORM_BLEND_CONST_COLOR_X
,
246 QUNIFORM_BLEND_CONST_COLOR_Y
,
247 QUNIFORM_BLEND_CONST_COLOR_Z
,
248 QUNIFORM_BLEND_CONST_COLOR_W
,
249 QUNIFORM_BLEND_CONST_COLOR_RGBA
,
250 QUNIFORM_BLEND_CONST_COLOR_AAAA
,
255 QUNIFORM_SAMPLE_MASK
,
258 struct vc4_varying_slot
{
263 struct vc4_compiler_ubo_range
{
265 * offset in bytes from the start of the ubo where this range is
268 * Only set once used is set.
273 * offset in bytes from the start of the gallium uniforms where the
278 /** size in bytes of this ubo range */
282 * Set if this range is used by the shader for indirect uniforms
289 struct vc4_uncompiled_shader
*shader_state
;
291 enum pipe_format format
;
295 unsigned compare_mode
:1;
296 unsigned compare_func
:3;
301 uint16_t msaa_width
, msaa_height
;
304 } tex
[VC4_MAX_TEXTURE_SAMPLERS
];
310 enum pipe_format color_format
;
312 bool stencil_enabled
;
313 bool stencil_twoside
;
314 bool stencil_full_writemasks
;
318 bool point_coord_upper_left
;
321 bool sample_coverage
;
322 bool sample_alpha_to_coverage
;
323 bool sample_alpha_to_one
;
324 uint8_t alpha_test_func
;
325 uint8_t logicop_func
;
326 uint32_t point_sprite_mask
;
328 struct pipe_rt_blend_state blend
;
335 * This is a proxy for the array of FS input semantics, which is
336 * larger than we would want to put in the key.
338 uint64_t compiled_fs_id
;
340 enum pipe_format attr_formats
[8];
342 bool per_vertex_point_size
;
347 struct vc4_context
*vc4
;
349 nir_function_impl
*impl
;
350 struct exec_list
*cf_node_list
;
353 * Mapping from nir_register * or nir_ssa_def * to array of struct
354 * qreg for the values.
356 struct hash_table
*def_ht
;
358 /* For each temp, the instruction generating its value. */
360 uint32_t defs_array_size
;
363 * Inputs to the shader, arranged by TGSI declaration order.
365 * Not all fragment shader QFILE_VARY reads are present in this array.
368 struct qreg
*outputs
;
369 bool msaa_per_sample_output
;
370 struct qreg color_reads
[VC4_MAX_SAMPLES
];
371 struct qreg sample_colors
[VC4_MAX_SAMPLES
];
372 uint32_t inputs_array_size
;
373 uint32_t outputs_array_size
;
374 uint32_t uniforms_array_size
;
376 struct vc4_compiler_ubo_range
*ubo_ranges
;
377 uint32_t ubo_ranges_array_size
;
378 /** Number of uniform areas declared in ubo_ranges. */
379 uint32_t num_uniform_ranges
;
380 /** Number of uniform areas used for indirect addressed loads. */
381 uint32_t num_ubo_ranges
;
382 uint32_t next_ubo_dst_offset
;
384 struct qreg line_x
, point_x
, point_y
;
386 struct qreg payload_FRAG_Z
;
387 struct qreg payload_FRAG_W
;
389 uint8_t vattr_sizes
[8];
392 * Array of the VARYING_SLOT_* of all FS QFILE_VARY reads.
394 * This includes those that aren't part of the VPM varyings, like
395 * point/line coordinates.
397 struct vc4_varying_slot
*input_slots
;
398 uint32_t num_input_slots
;
399 uint32_t input_slots_array_size
;
402 * An entry per outputs[] in the VS indicating what the VARYING_SLOT_*
403 * of the output is. Used to emit from the VS in the order that the
406 struct vc4_varying_slot
*output_slots
;
408 struct pipe_shader_state
*shader_state
;
410 struct vc4_fs_key
*fs_key
;
411 struct vc4_vs_key
*vs_key
;
413 uint32_t *uniform_data
;
414 enum quniform_contents
*uniform_contents
;
415 uint32_t uniform_array_size
;
416 uint32_t num_uniforms
;
417 uint32_t num_outputs
;
418 uint32_t num_texture_samples
;
419 uint32_t output_position_index
;
420 uint32_t output_color_index
;
421 uint32_t output_point_size_index
;
422 uint32_t output_sample_mask_index
;
427 struct list_head instructions
;
429 struct list_head qpu_inst_list
;
431 uint32_t qpu_inst_count
;
432 uint32_t qpu_inst_size
;
439 /* Special nir_load_input intrinsic index for loading the current TLB
442 #define VC4_NIR_TLB_COLOR_READ_INPUT 2000000000
444 #define VC4_NIR_MS_MASK_OUTPUT 2000000000
446 /* Special offset for nir_load_uniform values to get a QUNIFORM_*
447 * state-dependent value.
449 #define VC4_NIR_STATE_UNIFORM_OFFSET 1000000000
451 struct vc4_compile
*qir_compile_init(void);
452 void qir_compile_destroy(struct vc4_compile
*c
);
453 struct qinst
*qir_inst(enum qop op
, struct qreg dst
,
454 struct qreg src0
, struct qreg src1
);
455 struct qinst
*qir_inst4(enum qop op
, struct qreg dst
,
460 void qir_remove_instruction(struct vc4_compile
*c
, struct qinst
*qinst
);
461 struct qreg
qir_uniform(struct vc4_compile
*c
,
462 enum quniform_contents contents
,
464 void qir_schedule_instructions(struct vc4_compile
*c
);
465 void qir_reorder_uniforms(struct vc4_compile
*c
);
467 struct qreg
qir_emit_def(struct vc4_compile
*c
, struct qinst
*inst
);
468 struct qinst
*qir_emit_nondef(struct vc4_compile
*c
, struct qinst
*inst
);
470 struct qreg
qir_get_temp(struct vc4_compile
*c
);
471 int qir_get_op_nsrc(enum qop qop
);
472 bool qir_reg_equals(struct qreg a
, struct qreg b
);
473 bool qir_has_side_effects(struct vc4_compile
*c
, struct qinst
*inst
);
474 bool qir_has_side_effect_reads(struct vc4_compile
*c
, struct qinst
*inst
);
475 bool qir_is_mul(struct qinst
*inst
);
476 bool qir_is_raw_mov(struct qinst
*inst
);
477 bool qir_is_tex(struct qinst
*inst
);
478 bool qir_is_float_input(struct qinst
*inst
);
479 bool qir_depends_on_flags(struct qinst
*inst
);
480 bool qir_writes_r4(struct qinst
*inst
);
481 struct qreg
qir_follow_movs(struct vc4_compile
*c
, struct qreg reg
);
483 void qir_dump(struct vc4_compile
*c
);
484 void qir_dump_inst(struct vc4_compile
*c
, struct qinst
*inst
);
485 const char *qir_get_stage_name(enum qstage stage
);
487 void qir_validate(struct vc4_compile
*c
);
489 void qir_optimize(struct vc4_compile
*c
);
490 bool qir_opt_algebraic(struct vc4_compile
*c
);
491 bool qir_opt_constant_folding(struct vc4_compile
*c
);
492 bool qir_opt_copy_propagation(struct vc4_compile
*c
);
493 bool qir_opt_dead_code(struct vc4_compile
*c
);
494 bool qir_opt_peephole_sf(struct vc4_compile
*c
);
495 bool qir_opt_small_immediates(struct vc4_compile
*c
);
496 bool qir_opt_vpm(struct vc4_compile
*c
);
497 void vc4_nir_lower_blend(nir_shader
*s
, struct vc4_compile
*c
);
498 void vc4_nir_lower_io(nir_shader
*s
, struct vc4_compile
*c
);
499 nir_ssa_def
*vc4_nir_get_state_uniform(struct nir_builder
*b
,
500 enum quniform_contents contents
);
501 nir_ssa_def
*vc4_nir_get_swizzled_channel(struct nir_builder
*b
,
502 nir_ssa_def
**srcs
, int swiz
);
503 void vc4_nir_lower_txf_ms(nir_shader
*s
, struct vc4_compile
*c
);
504 void qir_lower_uniforms(struct vc4_compile
*c
);
506 uint32_t qpu_schedule_instructions(struct vc4_compile
*c
);
508 void qir_SF(struct vc4_compile
*c
, struct qreg src
);
510 static inline struct qreg
511 qir_uniform_ui(struct vc4_compile
*c
, uint32_t ui
)
513 return qir_uniform(c
, QUNIFORM_CONSTANT
, ui
);
516 static inline struct qreg
517 qir_uniform_f(struct vc4_compile
*c
, float f
)
519 return qir_uniform(c
, QUNIFORM_CONSTANT
, fui(f
));
522 #define QIR_ALU0(name) \
523 static inline struct qreg \
524 qir_##name(struct vc4_compile *c) \
526 return qir_emit_def(c, qir_inst(QOP_##name, c->undef, \
527 c->undef, c->undef)); \
529 static inline struct qinst * \
530 qir_##name##_dest(struct vc4_compile *c, struct qreg dest) \
532 return qir_emit_nondef(c, qir_inst(QOP_##name, dest, \
533 c->undef, c->undef)); \
536 #define QIR_ALU1(name) \
537 static inline struct qreg \
538 qir_##name(struct vc4_compile *c, struct qreg a) \
540 return qir_emit_def(c, qir_inst(QOP_##name, c->undef, \
543 static inline struct qinst * \
544 qir_##name##_dest(struct vc4_compile *c, struct qreg dest, \
547 return qir_emit_nondef(c, qir_inst(QOP_##name, dest, a, \
551 #define QIR_ALU2(name) \
552 static inline struct qreg \
553 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
555 return qir_emit_def(c, qir_inst(QOP_##name, c->undef, a, b)); \
557 static inline struct qinst * \
558 qir_##name##_dest(struct vc4_compile *c, struct qreg dest, \
559 struct qreg a, struct qreg b) \
561 return qir_emit_nondef(c, qir_inst(QOP_##name, dest, a, b)); \
564 #define QIR_NODST_1(name) \
565 static inline struct qinst * \
566 qir_##name(struct vc4_compile *c, struct qreg a) \
568 return qir_emit_nondef(c, qir_inst(QOP_##name, c->undef, \
572 #define QIR_NODST_2(name) \
573 static inline struct qinst * \
574 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
576 return qir_emit_nondef(c, qir_inst(QOP_##name, c->undef, \
580 #define QIR_PAYLOAD(name) \
581 static inline struct qreg \
582 qir_##name(struct vc4_compile *c) \
584 struct qreg *payload = &c->payload_##name; \
585 if (payload->file != QFILE_NULL) \
587 *payload = qir_get_temp(c); \
588 struct qinst *inst = qir_inst(QOP_##name, *payload, \
589 c->undef, c->undef); \
590 list_add(&inst->link, &c->instructions); \
591 c->defs[payload->index] = inst; \
635 QIR_NODST_2(TEX_DIRECT
)
639 QIR_ALU0(TLB_COLOR_READ
)
642 static inline struct qreg
643 qir_SEL(struct vc4_compile
*c
, uint8_t cond
, struct qreg src0
, struct qreg src1
)
645 struct qreg t
= qir_get_temp(c
);
646 struct qinst
*a
= qir_MOV_dest(c
, t
, src0
);
647 struct qinst
*b
= qir_MOV_dest(c
, t
, src1
);
653 static inline struct qreg
654 qir_UNPACK_8_F(struct vc4_compile
*c
, struct qreg src
, int i
)
656 struct qreg t
= qir_FMOV(c
, src
);
657 c
->defs
[t
.index
]->src
[0].pack
= QPU_UNPACK_8A
+ i
;
661 static inline struct qreg
662 qir_UNPACK_8_I(struct vc4_compile
*c
, struct qreg src
, int i
)
664 struct qreg t
= qir_MOV(c
, src
);
665 c
->defs
[t
.index
]->src
[0].pack
= QPU_UNPACK_8A
+ i
;
669 static inline struct qreg
670 qir_UNPACK_16_F(struct vc4_compile
*c
, struct qreg src
, int i
)
672 struct qreg t
= qir_FMOV(c
, src
);
673 c
->defs
[t
.index
]->src
[0].pack
= QPU_UNPACK_16A
+ i
;
677 static inline struct qreg
678 qir_UNPACK_16_I(struct vc4_compile
*c
, struct qreg src
, int i
)
680 struct qreg t
= qir_MOV(c
, src
);
681 c
->defs
[t
.index
]->src
[0].pack
= QPU_UNPACK_16A
+ i
;
686 qir_PACK_8_F(struct vc4_compile
*c
, struct qreg dest
, struct qreg val
, int chan
)
689 dest
.pack
= QPU_PACK_MUL_8A
+ chan
;
690 qir_emit_nondef(c
, qir_inst(QOP_MMOV
, dest
, val
, c
->undef
));
693 static inline struct qreg
694 qir_PACK_8888_F(struct vc4_compile
*c
, struct qreg val
)
696 struct qreg dest
= qir_MMOV(c
, val
);
697 c
->defs
[dest
.index
]->dst
.pack
= QPU_PACK_MUL_8888
;
701 static inline struct qreg
702 qir_POW(struct vc4_compile
*c
, struct qreg x
, struct qreg y
)
704 return qir_EXP2(c
, qir_FMUL(c
,
710 qir_VPM_WRITE(struct vc4_compile
*c
, struct qreg val
)
712 qir_MOV_dest(c
, qir_reg(QFILE_VPM
, 0), val
);
715 static inline struct qreg
716 qir_LOAD_IMM(struct vc4_compile
*c
, uint32_t val
)
718 return qir_emit_def(c
, qir_inst(QOP_LOAD_IMM
, c
->undef
,
719 qir_reg(QFILE_LOAD_IMM
, val
), c
->undef
));
722 #define qir_for_each_inst_inorder(inst, c) \
723 list_for_each_entry(struct qinst, inst, &c->instructions, link)
725 #endif /* VC4_QIR_H */