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 "util/simple_list.h"
36 #include "util/u_math.h"
37 #include "tgsi/tgsi_parse.h"
47 * Stores an immediate value in the index field that can be turned
48 * into a small immediate field by qpu_encode_small_immediate().
81 /* Note: Orderings of these compares must be the same as in
82 * qpu_defines.h. Selects the src[0] if the ns flag bit is set,
88 /* Selects the src[0] if the ns flag bit is set, otherwise src[1]. */
108 QOP_TLB_DISCARD_SETUP
,
109 QOP_TLB_STENCIL_SETUP
,
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
164 struct queued_qpu_inst
{
165 struct simple_node link
;
170 struct simple_node link
;
180 * Coordinate shader, runs during binning, before the VS, and just
188 enum quniform_contents
{
190 * Indicates that a constant 32-bit value is copied from the program's
195 * Indicates that the program's uniform contents are used as an index
196 * into the GL uniform storage.
201 * Scaling factors from clip coordinates to relative to the viewport
204 * This is used by the coordinate and vertex shaders to produce the
205 * 32-bit entry consisting of 2 16-bit fields with 12.4 signed fixed
206 * point offsets from the viewport ccenter.
208 QUNIFORM_VIEWPORT_X_SCALE
,
209 QUNIFORM_VIEWPORT_Y_SCALE
,
212 QUNIFORM_VIEWPORT_Z_OFFSET
,
213 QUNIFORM_VIEWPORT_Z_SCALE
,
215 QUNIFORM_USER_CLIP_PLANE
,
218 * A reference to a texture config parameter 0 uniform.
220 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
221 * defines texture type, miplevels, and such. It will be found as a
222 * parameter to the first QOP_TEX_[STRB] instruction in a sequence.
224 QUNIFORM_TEXTURE_CONFIG_P0
,
227 * A reference to a texture config parameter 1 uniform.
229 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
230 * defines texture width, height, filters, and wrap modes. It will be
231 * found as a parameter to the second QOP_TEX_[STRB] instruction in a
234 QUNIFORM_TEXTURE_CONFIG_P1
,
236 /** A reference to a texture config parameter 2 cubemap stride uniform */
237 QUNIFORM_TEXTURE_CONFIG_P2
,
241 QUNIFORM_TEXRECT_SCALE_X
,
242 QUNIFORM_TEXRECT_SCALE_Y
,
244 QUNIFORM_TEXTURE_BORDER_COLOR
,
246 QUNIFORM_BLEND_CONST_COLOR
,
252 struct vc4_varying_semantic
{
258 struct vc4_compiler_ubo_range
{
260 * offset in bytes from the start of the ubo where this range is
263 * Only set once used is set.
268 * offset in bytes from the start of the gallium uniforms where the
273 /** size in bytes of this ubo range */
277 * Set if this range is used by the shader for indirect uniforms
284 struct vc4_context
*vc4
;
285 struct tgsi_parse_context parser
;
287 /* For each temp, the instruction generating its value. */
289 uint32_t defs_array_size
;
291 * Inputs to the shader, arranged by TGSI declaration order.
293 * Not all fragment shader QFILE_VARY reads are present in this array.
296 struct qreg
*outputs
;
298 struct qreg addr
[4]; /* TGSI ARL destination. */
299 uint32_t temps_array_size
;
300 uint32_t inputs_array_size
;
301 uint32_t outputs_array_size
;
302 uint32_t uniforms_array_size
;
303 uint32_t consts_array_size
;
306 struct vc4_compiler_ubo_range
*ubo_ranges
;
307 uint32_t ubo_ranges_array_size
;
308 uint32_t num_ubo_ranges
;
309 uint32_t next_ubo_dst_offset
;
311 struct qreg line_x
, point_x
, point_y
;
314 uint8_t vattr_sizes
[8];
317 * Array of the TGSI semantics of all FS QFILE_VARY reads.
319 * This includes those that aren't part of the VPM varyings, like
320 * point/line coordinates.
322 struct vc4_varying_semantic
*input_semantics
;
323 uint32_t num_input_semantics
;
324 uint32_t input_semantics_array_size
;
327 * An entry per outputs[] in the VS indicating what the semantic of
328 * the output is. Used to emit from the VS in the order that the FS
331 struct vc4_varying_semantic
*output_semantics
;
333 struct pipe_shader_state
*shader_state
;
335 struct vc4_fs_key
*fs_key
;
336 struct vc4_vs_key
*vs_key
;
338 uint32_t *uniform_data
;
339 enum quniform_contents
*uniform_contents
;
340 uint32_t uniform_array_size
;
341 uint32_t num_uniforms
;
342 uint32_t num_outputs
;
343 uint32_t num_texture_samples
;
344 uint32_t output_position_index
;
345 uint32_t output_clipvertex_index
;
346 uint32_t output_color_index
;
347 uint32_t output_point_size_index
;
352 struct simple_node instructions
;
353 uint32_t immediates
[1024];
355 struct simple_node qpu_inst_list
;
357 uint32_t qpu_inst_count
;
358 uint32_t qpu_inst_size
;
365 struct vc4_compile
*qir_compile_init(void);
366 void qir_compile_destroy(struct vc4_compile
*c
);
367 struct qinst
*qir_inst(enum qop op
, struct qreg dst
,
368 struct qreg src0
, struct qreg src1
);
369 struct qinst
*qir_inst4(enum qop op
, struct qreg dst
,
374 void qir_remove_instruction(struct vc4_compile
*c
, struct qinst
*qinst
);
375 struct qreg
qir_uniform(struct vc4_compile
*c
,
376 enum quniform_contents contents
,
378 void qir_reorder_uniforms(struct vc4_compile
*c
);
379 void qir_emit(struct vc4_compile
*c
, struct qinst
*inst
);
380 struct qreg
qir_get_temp(struct vc4_compile
*c
);
381 int qir_get_op_nsrc(enum qop qop
);
382 bool qir_reg_equals(struct qreg a
, struct qreg b
);
383 bool qir_has_side_effects(struct vc4_compile
*c
, struct qinst
*inst
);
384 bool qir_has_side_effect_reads(struct vc4_compile
*c
, struct qinst
*inst
);
385 bool qir_is_multi_instruction(struct qinst
*inst
);
386 bool qir_is_tex(struct qinst
*inst
);
387 bool qir_depends_on_flags(struct qinst
*inst
);
388 bool qir_writes_r4(struct qinst
*inst
);
389 bool qir_reads_r4(struct qinst
*inst
);
390 bool qir_src_needs_a_file(struct qinst
*inst
);
391 struct qreg
qir_follow_movs(struct vc4_compile
*c
, struct qreg reg
);
393 void qir_dump(struct vc4_compile
*c
);
394 void qir_dump_inst(struct vc4_compile
*c
, struct qinst
*inst
);
395 const char *qir_get_stage_name(enum qstage stage
);
397 void qir_optimize(struct vc4_compile
*c
);
398 bool qir_opt_algebraic(struct vc4_compile
*c
);
399 bool qir_opt_copy_propagation(struct vc4_compile
*c
);
400 bool qir_opt_cse(struct vc4_compile
*c
);
401 bool qir_opt_dead_code(struct vc4_compile
*c
);
402 bool qir_opt_small_immediates(struct vc4_compile
*c
);
403 bool qir_opt_vpm_writes(struct vc4_compile
*c
);
404 void qir_lower_uniforms(struct vc4_compile
*c
);
406 void qpu_schedule_instructions(struct vc4_compile
*c
);
408 void qir_SF(struct vc4_compile
*c
, struct qreg src
);
410 static inline struct qreg
411 qir_uniform_ui(struct vc4_compile
*c
, uint32_t ui
)
413 return qir_uniform(c
, QUNIFORM_CONSTANT
, ui
);
416 static inline struct qreg
417 qir_uniform_f(struct vc4_compile
*c
, float f
)
419 return qir_uniform(c
, QUNIFORM_CONSTANT
, fui(f
));
422 #define QIR_ALU0(name) \
423 static inline struct qreg \
424 qir_##name(struct vc4_compile *c) \
426 struct qreg t = qir_get_temp(c); \
427 qir_emit(c, qir_inst(QOP_##name, t, c->undef, c->undef)); \
431 #define QIR_ALU1(name) \
432 static inline struct qreg \
433 qir_##name(struct vc4_compile *c, struct qreg a) \
435 struct qreg t = qir_get_temp(c); \
436 qir_emit(c, qir_inst(QOP_##name, t, a, c->undef)); \
440 #define QIR_ALU2(name) \
441 static inline struct qreg \
442 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
444 struct qreg t = qir_get_temp(c); \
445 qir_emit(c, qir_inst(QOP_##name, t, a, b)); \
449 #define QIR_NODST_1(name) \
451 qir_##name(struct vc4_compile *c, struct qreg a) \
453 qir_emit(c, qir_inst(QOP_##name, c->undef, a, c->undef)); \
456 #define QIR_NODST_2(name) \
458 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
460 qir_emit(c, qir_inst(QOP_##name, c->undef, a, b)); \
499 QIR_ALU2(PACK_SCALED
)
500 QIR_ALU1(PACK_8888_F
)
510 QIR_NODST_2(TEX_DIRECT
)
515 QIR_ALU0(FRAG_REV_FLAG
)
517 QIR_ALU0(TLB_COLOR_READ
)
518 QIR_NODST_1(TLB_Z_WRITE
)
519 QIR_NODST_1(TLB_DISCARD_SETUP
)
520 QIR_NODST_1(TLB_STENCIL_SETUP
)
522 static inline struct qreg
523 qir_R4_UNPACK(struct vc4_compile
*c
, struct qreg r4
, int i
)
525 struct qreg t
= qir_get_temp(c
);
526 qir_emit(c
, qir_inst(QOP_R4_UNPACK_A
+ i
, t
, r4
, c
->undef
));
530 static inline struct qreg
531 qir_SEL_X_0_COND(struct vc4_compile
*c
, int i
)
533 struct qreg t
= qir_get_temp(c
);
534 qir_emit(c
, qir_inst(QOP_R4_UNPACK_A
+ i
, t
, c
->undef
, c
->undef
));
538 static inline struct qreg
539 qir_UNPACK_8_F(struct vc4_compile
*c
, struct qreg src
, int i
)
541 struct qreg t
= qir_get_temp(c
);
542 qir_emit(c
, qir_inst(QOP_UNPACK_8A_F
+ i
, t
, src
, c
->undef
));
546 static inline struct qreg
547 qir_UNPACK_8_I(struct vc4_compile
*c
, struct qreg src
, int i
)
549 struct qreg t
= qir_get_temp(c
);
550 qir_emit(c
, qir_inst(QOP_UNPACK_8A_I
+ i
, t
, src
, c
->undef
));
554 static inline struct qreg
555 qir_UNPACK_16_F(struct vc4_compile
*c
, struct qreg src
, int i
)
557 struct qreg t
= qir_get_temp(c
);
558 qir_emit(c
, qir_inst(QOP_UNPACK_16A_F
+ i
, t
, src
, c
->undef
));
562 static inline struct qreg
563 qir_UNPACK_16_I(struct vc4_compile
*c
, struct qreg src
, int i
)
565 struct qreg t
= qir_get_temp(c
);
566 qir_emit(c
, qir_inst(QOP_UNPACK_16A_I
+ i
, t
, src
, c
->undef
));
570 static inline struct qreg
571 qir_PACK_8_F(struct vc4_compile
*c
, struct qreg rest
, struct qreg val
, int chan
)
573 struct qreg t
= qir_get_temp(c
);
574 qir_emit(c
, qir_inst(QOP_PACK_8A_F
+ chan
, t
, rest
, val
));
578 static inline struct qreg
579 qir_POW(struct vc4_compile
*c
, struct qreg x
, struct qreg y
)
581 return qir_EXP2(c
, qir_FMUL(c
,
587 qir_VPM_WRITE(struct vc4_compile
*c
, struct qreg val
)
589 static const struct qreg vpm
= { QFILE_VPM
, 0 };
590 qir_emit(c
, qir_inst(QOP_MOV
, vpm
, val
, c
->undef
));
593 #endif /* VC4_QIR_H */