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 "glsl/nir/nir.h"
36 #include "util/list.h"
37 #include "util/u_math.h"
39 #include "vc4_screen.h"
40 #include "pipe/p_state.h"
52 * Stores an immediate value in the index field that can be turned
53 * into a small immediate field by qpu_encode_small_immediate().
87 /* Note: Orderings of these compares must be the same as in
88 * qpu_defines.h. Selects the src[0] if the ns flag bit is set,
94 /* Selects the src[0] if the ns flag bit is set, otherwise src[1]. */
115 QOP_TLB_DISCARD_SETUP
,
116 QOP_TLB_STENCIL_SETUP
,
142 /** Texture x coordinate parameter write */
144 /** Texture y coordinate parameter write */
146 /** Texture border color parameter or cube map z coordinate write */
148 /** Texture LOD bias parameter write */
152 * Texture-unit 4-byte read with address provided direct in S
155 * The first operand is the offset from the start of the UBO, and the
156 * second is the uniform that has the UBO's base pointer.
161 * Signal of texture read being necessary and then reading r4 into
167 struct queued_qpu_inst
{
168 struct list_head link
;
173 struct list_head link
;
183 * Coordinate shader, runs during binning, before the VS, and just
191 enum quniform_contents
{
193 * Indicates that a constant 32-bit value is copied from the program's
198 * Indicates that the program's uniform contents are used as an index
199 * into the GL uniform storage.
204 * Scaling factors from clip coordinates to relative to the viewport
207 * This is used by the coordinate and vertex shaders to produce the
208 * 32-bit entry consisting of 2 16-bit fields with 12.4 signed fixed
209 * point offsets from the viewport ccenter.
211 QUNIFORM_VIEWPORT_X_SCALE
,
212 QUNIFORM_VIEWPORT_Y_SCALE
,
215 QUNIFORM_VIEWPORT_Z_OFFSET
,
216 QUNIFORM_VIEWPORT_Z_SCALE
,
218 QUNIFORM_USER_CLIP_PLANE
,
221 * A reference to a texture config parameter 0 uniform.
223 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
224 * defines texture type, miplevels, and such. It will be found as a
225 * parameter to the first QOP_TEX_[STRB] instruction in a sequence.
227 QUNIFORM_TEXTURE_CONFIG_P0
,
230 * A reference to a texture config parameter 1 uniform.
232 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
233 * defines texture width, height, filters, and wrap modes. It will be
234 * found as a parameter to the second QOP_TEX_[STRB] instruction in a
237 QUNIFORM_TEXTURE_CONFIG_P1
,
239 /** A reference to a texture config parameter 2 cubemap stride uniform */
240 QUNIFORM_TEXTURE_CONFIG_P2
,
244 QUNIFORM_TEXRECT_SCALE_X
,
245 QUNIFORM_TEXRECT_SCALE_Y
,
247 QUNIFORM_TEXTURE_BORDER_COLOR
,
249 QUNIFORM_BLEND_CONST_COLOR_X
,
250 QUNIFORM_BLEND_CONST_COLOR_Y
,
251 QUNIFORM_BLEND_CONST_COLOR_Z
,
252 QUNIFORM_BLEND_CONST_COLOR_W
,
259 struct vc4_varying_semantic
{
265 struct vc4_compiler_ubo_range
{
267 * offset in bytes from the start of the ubo where this range is
270 * Only set once used is set.
275 * offset in bytes from the start of the gallium uniforms where the
280 /** size in bytes of this ubo range */
284 * Set if this range is used by the shader for indirect uniforms
291 struct vc4_uncompiled_shader
*shader_state
;
293 enum pipe_format format
;
294 unsigned compare_mode
:1;
295 unsigned compare_func
:3;
299 } tex
[VC4_MAX_TEXTURE_SAMPLERS
];
305 enum pipe_format color_format
;
307 bool stencil_enabled
;
308 bool stencil_twoside
;
309 bool stencil_full_writemasks
;
313 bool point_coord_upper_left
;
315 uint8_t alpha_test_func
;
316 uint8_t logicop_func
;
317 uint32_t point_sprite_mask
;
319 struct pipe_rt_blend_state blend
;
326 * This is a proxy for the array of FS input semantics, which is
327 * larger than we would want to put in the key.
329 uint64_t compiled_fs_id
;
331 enum pipe_format attr_formats
[8];
333 bool per_vertex_point_size
;
337 struct vc4_context
*vc4
;
339 nir_function_impl
*impl
;
340 struct exec_list
*cf_node_list
;
343 * Mapping from nir_register * or nir_ssa_def * to array of struct
344 * qreg for the values.
346 struct hash_table
*def_ht
;
348 /* For each temp, the instruction generating its value. */
350 uint32_t defs_array_size
;
353 * Inputs to the shader, arranged by TGSI declaration order.
355 * Not all fragment shader QFILE_VARY reads are present in this array.
358 struct qreg
*outputs
;
359 uint32_t inputs_array_size
;
360 uint32_t outputs_array_size
;
361 uint32_t uniforms_array_size
;
363 struct vc4_compiler_ubo_range
*ubo_ranges
;
364 uint32_t ubo_ranges_array_size
;
365 /** Number of uniform areas declared in ubo_ranges. */
366 uint32_t num_uniform_ranges
;
367 /** Number of uniform areas used for indirect addressed loads. */
368 uint32_t num_ubo_ranges
;
369 uint32_t next_ubo_dst_offset
;
371 struct qreg line_x
, point_x
, point_y
;
374 uint8_t vattr_sizes
[8];
377 * Array of the TGSI semantics of all FS QFILE_VARY reads.
379 * This includes those that aren't part of the VPM varyings, like
380 * point/line coordinates.
382 struct vc4_varying_semantic
*input_semantics
;
383 uint32_t num_input_semantics
;
384 uint32_t input_semantics_array_size
;
387 * An entry per outputs[] in the VS indicating what the semantic of
388 * the output is. Used to emit from the VS in the order that the FS
391 struct vc4_varying_semantic
*output_semantics
;
393 struct pipe_shader_state
*shader_state
;
395 struct vc4_fs_key
*fs_key
;
396 struct vc4_vs_key
*vs_key
;
398 uint32_t *uniform_data
;
399 enum quniform_contents
*uniform_contents
;
400 uint32_t uniform_array_size
;
401 uint32_t num_uniforms
;
402 uint32_t num_outputs
;
403 uint32_t num_texture_samples
;
404 uint32_t output_position_index
;
405 uint32_t output_clipvertex_index
;
406 uint32_t output_color_index
;
407 uint32_t output_point_size_index
;
412 struct list_head instructions
;
413 uint32_t immediates
[1024];
415 struct list_head qpu_inst_list
;
417 uint32_t qpu_inst_count
;
418 uint32_t qpu_inst_size
;
425 /* Special nir_load_input intrinsic index for loading the current TLB
428 #define VC4_NIR_TLB_COLOR_READ_INPUT 2000000000
430 /* Special offset for nir_load_uniform values to get a QUNIFORM_*
431 * state-dependent value.
433 #define VC4_NIR_STATE_UNIFORM_OFFSET 2000000000
435 struct vc4_compile
*qir_compile_init(void);
436 void qir_compile_destroy(struct vc4_compile
*c
);
437 struct qinst
*qir_inst(enum qop op
, struct qreg dst
,
438 struct qreg src0
, struct qreg src1
);
439 struct qinst
*qir_inst4(enum qop op
, struct qreg dst
,
444 void qir_remove_instruction(struct vc4_compile
*c
, struct qinst
*qinst
);
445 struct qreg
qir_uniform(struct vc4_compile
*c
,
446 enum quniform_contents contents
,
448 void qir_reorder_uniforms(struct vc4_compile
*c
);
449 void qir_emit(struct vc4_compile
*c
, struct qinst
*inst
);
450 struct qreg
qir_get_temp(struct vc4_compile
*c
);
451 int qir_get_op_nsrc(enum qop qop
);
452 bool qir_reg_equals(struct qreg a
, struct qreg b
);
453 bool qir_has_side_effects(struct vc4_compile
*c
, struct qinst
*inst
);
454 bool qir_has_side_effect_reads(struct vc4_compile
*c
, struct qinst
*inst
);
455 bool qir_is_multi_instruction(struct qinst
*inst
);
456 bool qir_is_mul(struct qinst
*inst
);
457 bool qir_is_tex(struct qinst
*inst
);
458 bool qir_depends_on_flags(struct qinst
*inst
);
459 bool qir_writes_r4(struct qinst
*inst
);
460 bool qir_src_needs_a_file(struct qinst
*inst
);
461 struct qreg
qir_follow_movs(struct vc4_compile
*c
, struct qreg reg
);
463 void qir_dump(struct vc4_compile
*c
);
464 void qir_dump_inst(struct vc4_compile
*c
, struct qinst
*inst
);
465 const char *qir_get_stage_name(enum qstage stage
);
467 void qir_optimize(struct vc4_compile
*c
);
468 bool qir_opt_algebraic(struct vc4_compile
*c
);
469 bool qir_opt_constant_folding(struct vc4_compile
*c
);
470 bool qir_opt_copy_propagation(struct vc4_compile
*c
);
471 bool qir_opt_cse(struct vc4_compile
*c
);
472 bool qir_opt_dead_code(struct vc4_compile
*c
);
473 bool qir_opt_small_immediates(struct vc4_compile
*c
);
474 bool qir_opt_vpm_writes(struct vc4_compile
*c
);
475 void vc4_nir_lower_blend(struct vc4_compile
*c
);
476 void vc4_nir_lower_io(struct vc4_compile
*c
);
477 nir_ssa_def
*vc4_nir_get_state_uniform(struct nir_builder
*b
,
478 enum quniform_contents contents
);
479 nir_ssa_def
*vc4_nir_get_swizzled_channel(struct nir_builder
*b
,
480 nir_ssa_def
**srcs
, int swiz
);
481 void qir_lower_uniforms(struct vc4_compile
*c
);
483 void qpu_schedule_instructions(struct vc4_compile
*c
);
485 void qir_SF(struct vc4_compile
*c
, struct qreg src
);
487 static inline struct qreg
488 qir_uniform_ui(struct vc4_compile
*c
, uint32_t ui
)
490 return qir_uniform(c
, QUNIFORM_CONSTANT
, ui
);
493 static inline struct qreg
494 qir_uniform_f(struct vc4_compile
*c
, float f
)
496 return qir_uniform(c
, QUNIFORM_CONSTANT
, fui(f
));
499 #define QIR_ALU0(name) \
500 static inline struct qreg \
501 qir_##name(struct vc4_compile *c) \
503 struct qreg t = qir_get_temp(c); \
504 qir_emit(c, qir_inst(QOP_##name, t, c->undef, c->undef)); \
508 #define QIR_ALU1(name) \
509 static inline struct qreg \
510 qir_##name(struct vc4_compile *c, struct qreg a) \
512 struct qreg t = qir_get_temp(c); \
513 qir_emit(c, qir_inst(QOP_##name, t, a, c->undef)); \
517 #define QIR_ALU2(name) \
518 static inline struct qreg \
519 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
521 struct qreg t = qir_get_temp(c); \
522 qir_emit(c, qir_inst(QOP_##name, t, a, b)); \
526 #define QIR_NODST_1(name) \
528 qir_##name(struct vc4_compile *c, struct qreg a) \
530 qir_emit(c, qir_inst(QOP_##name, c->undef, a, c->undef)); \
533 #define QIR_NODST_2(name) \
535 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
537 qir_emit(c, qir_inst(QOP_##name, c->undef, a, b)); \
540 #define QIR_PACK(name) \
541 static inline struct qreg \
542 qir_##name(struct vc4_compile *c, struct qreg dest, struct qreg a) \
544 qir_emit(c, qir_inst(QOP_##name, dest, a, c->undef)); \
545 if (dest.file == QFILE_TEMP) \
546 c->defs[dest.index] = NULL; \
586 QIR_ALU1(PACK_8888_F
)
598 QIR_NODST_2(TEX_DIRECT
)
603 QIR_ALU0(FRAG_REV_FLAG
)
605 QIR_ALU0(TLB_COLOR_READ
)
606 QIR_NODST_1(TLB_COLOR_WRITE
)
607 QIR_NODST_1(TLB_Z_WRITE
)
608 QIR_NODST_1(TLB_DISCARD_SETUP
)
609 QIR_NODST_1(TLB_STENCIL_SETUP
)
611 static inline struct qreg
612 qir_UNPACK_8_F(struct vc4_compile
*c
, struct qreg src
, int i
)
614 struct qreg t
= qir_get_temp(c
);
615 qir_emit(c
, qir_inst(QOP_UNPACK_8A_F
+ i
, t
, src
, c
->undef
));
619 static inline struct qreg
620 qir_UNPACK_8_I(struct vc4_compile
*c
, struct qreg src
, int i
)
622 struct qreg t
= qir_get_temp(c
);
623 qir_emit(c
, qir_inst(QOP_UNPACK_8A_I
+ i
, t
, src
, c
->undef
));
627 static inline struct qreg
628 qir_UNPACK_16_F(struct vc4_compile
*c
, struct qreg src
, int i
)
630 struct qreg t
= qir_get_temp(c
);
631 qir_emit(c
, qir_inst(QOP_UNPACK_16A_F
+ i
, t
, src
, c
->undef
));
635 static inline struct qreg
636 qir_UNPACK_16_I(struct vc4_compile
*c
, struct qreg src
, int i
)
638 struct qreg t
= qir_get_temp(c
);
639 qir_emit(c
, qir_inst(QOP_UNPACK_16A_I
+ i
, t
, src
, c
->undef
));
643 static inline struct qreg
644 qir_PACK_8_F(struct vc4_compile
*c
, struct qreg dest
, struct qreg val
, int chan
)
646 qir_emit(c
, qir_inst(QOP_PACK_8A_F
+ chan
, dest
, val
, c
->undef
));
647 if (dest
.file
== QFILE_TEMP
)
648 c
->defs
[dest
.index
] = NULL
;
652 static inline struct qreg
653 qir_POW(struct vc4_compile
*c
, struct qreg x
, struct qreg y
)
655 return qir_EXP2(c
, qir_FMUL(c
,
661 qir_VPM_WRITE(struct vc4_compile
*c
, struct qreg val
)
663 static const struct qreg vpm
= { QFILE_VPM
, 0 };
664 qir_emit(c
, qir_inst(QOP_MOV
, vpm
, val
, c
->undef
));
667 #endif /* VC4_QIR_H */