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().
86 /* Note: Orderings of these compares must be the same as in
87 * qpu_defines.h. Selects the src[0] if the ns flag bit is set,
93 /* Selects the src[0] if the ns flag bit is set, otherwise src[1]. */
113 QOP_TLB_DISCARD_SETUP
,
114 QOP_TLB_STENCIL_SETUP
,
140 /** Texture x coordinate parameter write */
142 /** Texture y coordinate parameter write */
144 /** Texture border color parameter or cube map z coordinate write */
146 /** Texture LOD bias parameter write */
150 * Texture-unit 4-byte read with address provided direct in S
153 * The first operand is the offset from the start of the UBO, and the
154 * second is the uniform that has the UBO's base pointer.
159 * Signal of texture read being necessary and then reading r4 into
165 struct queued_qpu_inst
{
166 struct list_head link
;
171 struct list_head link
;
181 * Coordinate shader, runs during binning, before the VS, and just
189 enum quniform_contents
{
191 * Indicates that a constant 32-bit value is copied from the program's
196 * Indicates that the program's uniform contents are used as an index
197 * into the GL uniform storage.
202 * Scaling factors from clip coordinates to relative to the viewport
205 * This is used by the coordinate and vertex shaders to produce the
206 * 32-bit entry consisting of 2 16-bit fields with 12.4 signed fixed
207 * point offsets from the viewport ccenter.
209 QUNIFORM_VIEWPORT_X_SCALE
,
210 QUNIFORM_VIEWPORT_Y_SCALE
,
213 QUNIFORM_VIEWPORT_Z_OFFSET
,
214 QUNIFORM_VIEWPORT_Z_SCALE
,
216 QUNIFORM_USER_CLIP_PLANE
,
219 * A reference to a texture config parameter 0 uniform.
221 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
222 * defines texture type, miplevels, and such. It will be found as a
223 * parameter to the first QOP_TEX_[STRB] instruction in a sequence.
225 QUNIFORM_TEXTURE_CONFIG_P0
,
228 * A reference to a texture config parameter 1 uniform.
230 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
231 * defines texture width, height, filters, and wrap modes. It will be
232 * found as a parameter to the second QOP_TEX_[STRB] instruction in a
235 QUNIFORM_TEXTURE_CONFIG_P1
,
237 /** A reference to a texture config parameter 2 cubemap stride uniform */
238 QUNIFORM_TEXTURE_CONFIG_P2
,
242 QUNIFORM_TEXRECT_SCALE_X
,
243 QUNIFORM_TEXRECT_SCALE_Y
,
245 QUNIFORM_TEXTURE_BORDER_COLOR
,
247 QUNIFORM_BLEND_CONST_COLOR_X
,
248 QUNIFORM_BLEND_CONST_COLOR_Y
,
249 QUNIFORM_BLEND_CONST_COLOR_Z
,
250 QUNIFORM_BLEND_CONST_COLOR_W
,
257 struct vc4_varying_semantic
{
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
;
292 unsigned compare_mode
:1;
293 unsigned compare_func
:3;
297 } tex
[VC4_MAX_TEXTURE_SAMPLERS
];
303 enum pipe_format color_format
;
305 bool stencil_enabled
;
306 bool stencil_twoside
;
307 bool stencil_full_writemasks
;
311 bool point_coord_upper_left
;
313 uint8_t alpha_test_func
;
314 uint8_t logicop_func
;
315 uint32_t point_sprite_mask
;
317 struct pipe_rt_blend_state blend
;
324 * This is a proxy for the array of FS input semantics, which is
325 * larger than we would want to put in the key.
327 uint64_t compiled_fs_id
;
329 enum pipe_format attr_formats
[8];
331 bool per_vertex_point_size
;
335 struct vc4_context
*vc4
;
337 nir_function_impl
*impl
;
338 struct exec_list
*cf_node_list
;
341 * Mapping from nir_register * or nir_ssa_def * to array of struct
342 * qreg for the values.
344 struct hash_table
*def_ht
;
346 /* For each temp, the instruction generating its value. */
348 uint32_t defs_array_size
;
351 * Inputs to the shader, arranged by TGSI declaration order.
353 * Not all fragment shader QFILE_VARY reads are present in this array.
356 struct qreg
*outputs
;
357 uint32_t inputs_array_size
;
358 uint32_t outputs_array_size
;
359 uint32_t uniforms_array_size
;
361 struct vc4_compiler_ubo_range
*ubo_ranges
;
362 uint32_t ubo_ranges_array_size
;
363 /** Number of uniform areas declared in ubo_ranges. */
364 uint32_t num_uniform_ranges
;
365 /** Number of uniform areas used for indirect addressed loads. */
366 uint32_t num_ubo_ranges
;
367 uint32_t next_ubo_dst_offset
;
369 struct qreg line_x
, point_x
, point_y
;
372 uint8_t vattr_sizes
[8];
375 * Array of the TGSI semantics of all FS QFILE_VARY reads.
377 * This includes those that aren't part of the VPM varyings, like
378 * point/line coordinates.
380 struct vc4_varying_semantic
*input_semantics
;
381 uint32_t num_input_semantics
;
382 uint32_t input_semantics_array_size
;
385 * An entry per outputs[] in the VS indicating what the semantic of
386 * the output is. Used to emit from the VS in the order that the FS
389 struct vc4_varying_semantic
*output_semantics
;
391 struct pipe_shader_state
*shader_state
;
393 struct vc4_fs_key
*fs_key
;
394 struct vc4_vs_key
*vs_key
;
396 uint32_t *uniform_data
;
397 enum quniform_contents
*uniform_contents
;
398 uint32_t uniform_array_size
;
399 uint32_t num_uniforms
;
400 uint32_t num_outputs
;
401 uint32_t num_texture_samples
;
402 uint32_t output_position_index
;
403 uint32_t output_clipvertex_index
;
404 uint32_t output_color_index
;
405 uint32_t output_point_size_index
;
410 struct list_head instructions
;
411 uint32_t immediates
[1024];
413 struct list_head qpu_inst_list
;
415 uint32_t qpu_inst_count
;
416 uint32_t qpu_inst_size
;
423 /* Special nir_load_input intrinsic index for loading the current TLB
426 #define VC4_NIR_TLB_COLOR_READ_INPUT 2000000000
428 /* Special offset for nir_load_uniform values to get a QUNIFORM_*
429 * state-dependent value.
431 #define VC4_NIR_STATE_UNIFORM_OFFSET 2000000000
433 struct vc4_compile
*qir_compile_init(void);
434 void qir_compile_destroy(struct vc4_compile
*c
);
435 struct qinst
*qir_inst(enum qop op
, struct qreg dst
,
436 struct qreg src0
, struct qreg src1
);
437 struct qinst
*qir_inst4(enum qop op
, struct qreg dst
,
442 void qir_remove_instruction(struct vc4_compile
*c
, struct qinst
*qinst
);
443 struct qreg
qir_uniform(struct vc4_compile
*c
,
444 enum quniform_contents contents
,
446 void qir_reorder_uniforms(struct vc4_compile
*c
);
447 void qir_emit(struct vc4_compile
*c
, struct qinst
*inst
);
448 struct qreg
qir_get_temp(struct vc4_compile
*c
);
449 int qir_get_op_nsrc(enum qop qop
);
450 bool qir_reg_equals(struct qreg a
, struct qreg b
);
451 bool qir_has_side_effects(struct vc4_compile
*c
, struct qinst
*inst
);
452 bool qir_has_side_effect_reads(struct vc4_compile
*c
, struct qinst
*inst
);
453 bool qir_is_multi_instruction(struct qinst
*inst
);
454 bool qir_is_tex(struct qinst
*inst
);
455 bool qir_depends_on_flags(struct qinst
*inst
);
456 bool qir_writes_r4(struct qinst
*inst
);
457 bool qir_src_needs_a_file(struct qinst
*inst
);
458 struct qreg
qir_follow_movs(struct vc4_compile
*c
, struct qreg reg
);
460 void qir_dump(struct vc4_compile
*c
);
461 void qir_dump_inst(struct vc4_compile
*c
, struct qinst
*inst
);
462 const char *qir_get_stage_name(enum qstage stage
);
464 void qir_optimize(struct vc4_compile
*c
);
465 bool qir_opt_algebraic(struct vc4_compile
*c
);
466 bool qir_opt_constant_folding(struct vc4_compile
*c
);
467 bool qir_opt_copy_propagation(struct vc4_compile
*c
);
468 bool qir_opt_cse(struct vc4_compile
*c
);
469 bool qir_opt_dead_code(struct vc4_compile
*c
);
470 bool qir_opt_small_immediates(struct vc4_compile
*c
);
471 bool qir_opt_vpm_writes(struct vc4_compile
*c
);
472 void vc4_nir_lower_blend(struct vc4_compile
*c
);
473 void vc4_nir_lower_io(struct vc4_compile
*c
);
474 nir_ssa_def
*vc4_nir_get_state_uniform(struct nir_builder
*b
,
475 enum quniform_contents contents
);
476 nir_ssa_def
*vc4_nir_get_swizzled_channel(struct nir_builder
*b
,
477 nir_ssa_def
**srcs
, int swiz
);
478 void qir_lower_uniforms(struct vc4_compile
*c
);
480 void qpu_schedule_instructions(struct vc4_compile
*c
);
482 void qir_SF(struct vc4_compile
*c
, struct qreg src
);
484 static inline struct qreg
485 qir_uniform_ui(struct vc4_compile
*c
, uint32_t ui
)
487 return qir_uniform(c
, QUNIFORM_CONSTANT
, ui
);
490 static inline struct qreg
491 qir_uniform_f(struct vc4_compile
*c
, float f
)
493 return qir_uniform(c
, QUNIFORM_CONSTANT
, fui(f
));
496 #define QIR_ALU0(name) \
497 static inline struct qreg \
498 qir_##name(struct vc4_compile *c) \
500 struct qreg t = qir_get_temp(c); \
501 qir_emit(c, qir_inst(QOP_##name, t, c->undef, c->undef)); \
505 #define QIR_ALU1(name) \
506 static inline struct qreg \
507 qir_##name(struct vc4_compile *c, struct qreg a) \
509 struct qreg t = qir_get_temp(c); \
510 qir_emit(c, qir_inst(QOP_##name, t, a, c->undef)); \
514 #define QIR_ALU2(name) \
515 static inline struct qreg \
516 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
518 struct qreg t = qir_get_temp(c); \
519 qir_emit(c, qir_inst(QOP_##name, t, a, b)); \
523 #define QIR_NODST_1(name) \
525 qir_##name(struct vc4_compile *c, struct qreg a) \
527 qir_emit(c, qir_inst(QOP_##name, c->undef, a, c->undef)); \
530 #define QIR_NODST_2(name) \
532 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
534 qir_emit(c, qir_inst(QOP_##name, c->undef, a, b)); \
537 #define QIR_PACK(name) \
538 static inline struct qreg \
539 qir_##name(struct vc4_compile *c, struct qreg dest, struct qreg a) \
541 qir_emit(c, qir_inst(QOP_##name, dest, a, c->undef)); \
542 if (dest.file == QFILE_TEMP) \
543 c->defs[dest.index] = NULL; \
583 QIR_ALU2(PACK_SCALED
)
584 QIR_ALU1(PACK_8888_F
)
594 QIR_NODST_2(TEX_DIRECT
)
599 QIR_ALU0(FRAG_REV_FLAG
)
601 QIR_ALU0(TLB_COLOR_READ
)
602 QIR_NODST_1(TLB_COLOR_WRITE
)
603 QIR_NODST_1(TLB_Z_WRITE
)
604 QIR_NODST_1(TLB_DISCARD_SETUP
)
605 QIR_NODST_1(TLB_STENCIL_SETUP
)
607 static inline struct qreg
608 qir_UNPACK_8_F(struct vc4_compile
*c
, struct qreg src
, int i
)
610 struct qreg t
= qir_get_temp(c
);
611 qir_emit(c
, qir_inst(QOP_UNPACK_8A_F
+ i
, t
, src
, c
->undef
));
615 static inline struct qreg
616 qir_UNPACK_8_I(struct vc4_compile
*c
, struct qreg src
, int i
)
618 struct qreg t
= qir_get_temp(c
);
619 qir_emit(c
, qir_inst(QOP_UNPACK_8A_I
+ i
, t
, src
, c
->undef
));
623 static inline struct qreg
624 qir_UNPACK_16_F(struct vc4_compile
*c
, struct qreg src
, int i
)
626 struct qreg t
= qir_get_temp(c
);
627 qir_emit(c
, qir_inst(QOP_UNPACK_16A_F
+ i
, t
, src
, c
->undef
));
631 static inline struct qreg
632 qir_UNPACK_16_I(struct vc4_compile
*c
, struct qreg src
, int i
)
634 struct qreg t
= qir_get_temp(c
);
635 qir_emit(c
, qir_inst(QOP_UNPACK_16A_I
+ i
, t
, src
, c
->undef
));
639 static inline struct qreg
640 qir_PACK_8_F(struct vc4_compile
*c
, struct qreg dest
, struct qreg val
, int chan
)
642 qir_emit(c
, qir_inst(QOP_PACK_8A_F
+ chan
, dest
, val
, c
->undef
));
643 if (dest
.file
== QFILE_TEMP
)
644 c
->defs
[dest
.index
] = NULL
;
648 static inline struct qreg
649 qir_POW(struct vc4_compile
*c
, struct qreg x
, struct qreg y
)
651 return qir_EXP2(c
, qir_FMUL(c
,
657 qir_VPM_WRITE(struct vc4_compile
*c
, struct qreg val
)
659 static const struct qreg vpm
= { QFILE_VPM
, 0 };
660 qir_emit(c
, qir_inst(QOP_MOV
, vpm
, val
, c
->undef
));
663 #endif /* VC4_QIR_H */