broadcom/vc5: Add lowering for txf_ms to a txf on a 2x2-scaled texture.
[mesa.git] / src / broadcom / compiler / v3d_compiler.h
1 /*
2 * Copyright © 2016 Broadcom
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef V3D_COMPILER_H
25 #define V3D_COMPILER_H
26
27 #include <assert.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdbool.h>
31 #include <stdint.h>
32 #include <string.h>
33
34 #include "util/macros.h"
35 #include "common/v3d_debug.h"
36 #include "compiler/nir/nir.h"
37 #include "util/list.h"
38 #include "util/u_math.h"
39
40 #include "qpu/qpu_instr.h"
41 #include "pipe/p_state.h"
42
43 #define V3D_MAX_TEXTURE_SAMPLERS 32
44 #define V3D_MAX_SAMPLES 4
45 #define V3D_MAX_FS_INPUTS 64
46 #define V3D_MAX_VS_INPUTS 64
47
48 struct nir_builder;
49
50 struct v3d_fs_inputs {
51 /**
52 * Array of the meanings of the VPM inputs this shader needs.
53 *
54 * It doesn't include those that aren't part of the VPM, like
55 * point/line coordinates.
56 */
57 struct v3d_varying_slot *input_slots;
58 uint32_t num_inputs;
59 };
60
61 enum qfile {
62 /** An unused source or destination register. */
63 QFILE_NULL,
64
65 /** A physical register, such as the W coordinate payload. */
66 QFILE_REG,
67 /** One of the regsiters for fixed function interactions. */
68 QFILE_MAGIC,
69
70 /**
71 * A virtual register, that will be allocated to actual accumulator
72 * or physical registers later.
73 */
74 QFILE_TEMP,
75 QFILE_VARY,
76 QFILE_UNIF,
77 QFILE_TLB,
78 QFILE_TLBU,
79
80 /**
81 * VPM reads use this with an index value to say what part of the VPM
82 * is being read.
83 */
84 QFILE_VPM,
85
86 /**
87 * Stores an immediate value in the index field that will be used
88 * directly by qpu_load_imm().
89 */
90 QFILE_LOAD_IMM,
91
92 /**
93 * Stores an immediate value in the index field that can be turned
94 * into a small immediate field by qpu_encode_small_immediate().
95 */
96 QFILE_SMALL_IMM,
97 };
98
99 /**
100 * A reference to a QPU register or a virtual temp register.
101 */
102 struct qreg {
103 enum qfile file;
104 uint32_t index;
105 };
106
107 static inline struct qreg vir_reg(enum qfile file, uint32_t index)
108 {
109 return (struct qreg){file, index};
110 }
111
112 /**
113 * A reference to an actual register at the QPU level, for register
114 * allocation.
115 */
116 struct qpu_reg {
117 bool magic;
118 int index;
119 };
120
121 struct qinst {
122 /** Entry in qblock->instructions */
123 struct list_head link;
124
125 /**
126 * The instruction being wrapped. Its condition codes, pack flags,
127 * signals, etc. will all be used, with just the register references
128 * being replaced by the contents of qinst->dst and qinst->src[].
129 */
130 struct v3d_qpu_instr qpu;
131
132 /* Pre-register-allocation references to src/dst registers */
133 struct qreg dst;
134 struct qreg src[3];
135 bool cond_is_exec_mask;
136 bool has_implicit_uniform;
137
138 /* After vir_to_qpu.c: If instr reads a uniform, which uniform from
139 * the uncompiled stream it is.
140 */
141 int uniform;
142 };
143
144 enum quniform_contents {
145 /**
146 * Indicates that a constant 32-bit value is copied from the program's
147 * uniform contents.
148 */
149 QUNIFORM_CONSTANT,
150 /**
151 * Indicates that the program's uniform contents are used as an index
152 * into the GL uniform storage.
153 */
154 QUNIFORM_UNIFORM,
155
156 /** @{
157 * Scaling factors from clip coordinates to relative to the viewport
158 * center.
159 *
160 * This is used by the coordinate and vertex shaders to produce the
161 * 32-bit entry consisting of 2 16-bit fields with 12.4 signed fixed
162 * point offsets from the viewport ccenter.
163 */
164 QUNIFORM_VIEWPORT_X_SCALE,
165 QUNIFORM_VIEWPORT_Y_SCALE,
166 /** @} */
167
168 QUNIFORM_VIEWPORT_Z_OFFSET,
169 QUNIFORM_VIEWPORT_Z_SCALE,
170
171 QUNIFORM_USER_CLIP_PLANE,
172
173 /**
174 * A reference to a texture config parameter 0 uniform.
175 *
176 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
177 * defines texture type, miplevels, and such. It will be found as a
178 * parameter to the first QOP_TEX_[STRB] instruction in a sequence.
179 */
180 QUNIFORM_TEXTURE_CONFIG_P0_0,
181 QUNIFORM_TEXTURE_CONFIG_P0_1,
182 QUNIFORM_TEXTURE_CONFIG_P0_2,
183 QUNIFORM_TEXTURE_CONFIG_P0_3,
184 QUNIFORM_TEXTURE_CONFIG_P0_4,
185 QUNIFORM_TEXTURE_CONFIG_P0_5,
186 QUNIFORM_TEXTURE_CONFIG_P0_6,
187 QUNIFORM_TEXTURE_CONFIG_P0_7,
188 QUNIFORM_TEXTURE_CONFIG_P0_8,
189 QUNIFORM_TEXTURE_CONFIG_P0_9,
190 QUNIFORM_TEXTURE_CONFIG_P0_10,
191 QUNIFORM_TEXTURE_CONFIG_P0_11,
192 QUNIFORM_TEXTURE_CONFIG_P0_12,
193 QUNIFORM_TEXTURE_CONFIG_P0_13,
194 QUNIFORM_TEXTURE_CONFIG_P0_14,
195 QUNIFORM_TEXTURE_CONFIG_P0_15,
196 QUNIFORM_TEXTURE_CONFIG_P0_16,
197 QUNIFORM_TEXTURE_CONFIG_P0_17,
198 QUNIFORM_TEXTURE_CONFIG_P0_18,
199 QUNIFORM_TEXTURE_CONFIG_P0_19,
200 QUNIFORM_TEXTURE_CONFIG_P0_20,
201 QUNIFORM_TEXTURE_CONFIG_P0_21,
202 QUNIFORM_TEXTURE_CONFIG_P0_22,
203 QUNIFORM_TEXTURE_CONFIG_P0_23,
204 QUNIFORM_TEXTURE_CONFIG_P0_24,
205 QUNIFORM_TEXTURE_CONFIG_P0_25,
206 QUNIFORM_TEXTURE_CONFIG_P0_26,
207 QUNIFORM_TEXTURE_CONFIG_P0_27,
208 QUNIFORM_TEXTURE_CONFIG_P0_28,
209 QUNIFORM_TEXTURE_CONFIG_P0_29,
210 QUNIFORM_TEXTURE_CONFIG_P0_30,
211 QUNIFORM_TEXTURE_CONFIG_P0_31,
212 QUNIFORM_TEXTURE_CONFIG_P0_32,
213
214 /**
215 * A reference to a texture config parameter 1 uniform.
216 *
217 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
218 * defines texture width, height, filters, and wrap modes. It will be
219 * found as a parameter to the second QOP_TEX_[STRB] instruction in a
220 * sequence.
221 */
222 QUNIFORM_TEXTURE_CONFIG_P1,
223
224 QUNIFORM_TEXTURE_FIRST_LEVEL,
225
226 QUNIFORM_TEXTURE_WIDTH,
227 QUNIFORM_TEXTURE_HEIGHT,
228 QUNIFORM_TEXTURE_DEPTH,
229 QUNIFORM_TEXTURE_ARRAY_SIZE,
230 QUNIFORM_TEXTURE_LEVELS,
231
232 QUNIFORM_UBO_ADDR,
233
234 QUNIFORM_TEXRECT_SCALE_X,
235 QUNIFORM_TEXRECT_SCALE_Y,
236
237 QUNIFORM_TEXTURE_BORDER_COLOR,
238
239 QUNIFORM_STENCIL,
240
241 QUNIFORM_ALPHA_REF,
242 QUNIFORM_SAMPLE_MASK,
243 };
244
245 struct v3d_varying_slot {
246 uint8_t slot_and_component;
247 };
248
249 static inline struct v3d_varying_slot
250 v3d_slot_from_slot_and_component(uint8_t slot, uint8_t component)
251 {
252 assert(slot < 255 / 4);
253 return (struct v3d_varying_slot){ (slot << 2) + component };
254 }
255
256 static inline uint8_t v3d_slot_get_slot(struct v3d_varying_slot slot)
257 {
258 return slot.slot_and_component >> 2;
259 }
260
261 static inline uint8_t v3d_slot_get_component(struct v3d_varying_slot slot)
262 {
263 return slot.slot_and_component & 3;
264 }
265
266 struct v3d_ubo_range {
267 /**
268 * offset in bytes from the start of the ubo where this range is
269 * uploaded.
270 *
271 * Only set once used is set.
272 */
273 uint32_t dst_offset;
274
275 /**
276 * offset in bytes from the start of the gallium uniforms where the
277 * data comes from.
278 */
279 uint32_t src_offset;
280
281 /** size in bytes of this ubo range */
282 uint32_t size;
283 };
284
285 struct v3d_key {
286 void *shader_state;
287 struct {
288 uint8_t swizzle[4];
289 uint8_t return_size;
290 uint8_t return_channels;
291 union {
292 struct {
293 unsigned compare_mode:1;
294 unsigned compare_func:3;
295 bool clamp_s:1;
296 bool clamp_t:1;
297 bool clamp_r:1;
298 };
299 struct {
300 uint16_t msaa_width, msaa_height;
301 };
302 };
303 } tex[V3D_MAX_TEXTURE_SAMPLERS];
304 uint8_t ucp_enables;
305 };
306
307 struct v3d_fs_key {
308 struct v3d_key base;
309 bool depth_enabled;
310 bool is_points;
311 bool is_lines;
312 bool alpha_test;
313 bool point_coord_upper_left;
314 bool light_twoside;
315 bool msaa;
316 bool sample_coverage;
317 bool sample_alpha_to_coverage;
318 bool sample_alpha_to_one;
319 bool clamp_color;
320 uint8_t nr_cbufs;
321 uint8_t swap_color_rb;
322 /* Mask of which render targets need to be written as 32-bit floats */
323 uint8_t f32_color_rb;
324 uint8_t alpha_test_func;
325 uint8_t logicop_func;
326 uint32_t point_sprite_mask;
327
328 struct pipe_rt_blend_state blend;
329 };
330
331 struct v3d_vs_key {
332 struct v3d_key base;
333
334 struct v3d_varying_slot fs_inputs[V3D_MAX_FS_INPUTS];
335 uint8_t num_fs_inputs;
336
337 bool is_coord;
338 bool per_vertex_point_size;
339 bool clamp_color;
340 };
341
342 /** A basic block of VIR intructions. */
343 struct qblock {
344 struct list_head link;
345
346 struct list_head instructions;
347
348 struct set *predecessors;
349 struct qblock *successors[2];
350
351 int index;
352
353 /* Instruction IPs for the first and last instruction of the block.
354 * Set by qpu_schedule.c.
355 */
356 uint32_t start_qpu_ip;
357 uint32_t end_qpu_ip;
358
359 /* Instruction IP for the branch instruction of the block. Set by
360 * qpu_schedule.c.
361 */
362 uint32_t branch_qpu_ip;
363
364 /** Offset within the uniform stream at the start of the block. */
365 uint32_t start_uniform;
366 /** Offset within the uniform stream of the branch instruction */
367 uint32_t branch_uniform;
368
369 /** @{ used by v3d_vir_live_variables.c */
370 BITSET_WORD *def;
371 BITSET_WORD *use;
372 BITSET_WORD *live_in;
373 BITSET_WORD *live_out;
374 int start_ip, end_ip;
375 /** @} */
376 };
377
378 /**
379 * Compiler state saved across compiler invocations, for any expensive global
380 * setup.
381 */
382 struct v3d_compiler {
383 const struct v3d_device_info *devinfo;
384 struct ra_regs *regs;
385 unsigned int reg_class[3];
386 };
387
388 struct v3d_compile {
389 const struct v3d_device_info *devinfo;
390 nir_shader *s;
391 nir_function_impl *impl;
392 struct exec_list *cf_node_list;
393 const struct v3d_compiler *compiler;
394
395 /**
396 * Mapping from nir_register * or nir_ssa_def * to array of struct
397 * qreg for the values.
398 */
399 struct hash_table *def_ht;
400
401 /* For each temp, the instruction generating its value. */
402 struct qinst **defs;
403 uint32_t defs_array_size;
404
405 /**
406 * Inputs to the shader, arranged by TGSI declaration order.
407 *
408 * Not all fragment shader QFILE_VARY reads are present in this array.
409 */
410 struct qreg *inputs;
411 struct qreg *outputs;
412 bool msaa_per_sample_output;
413 struct qreg color_reads[V3D_MAX_SAMPLES];
414 struct qreg sample_colors[V3D_MAX_SAMPLES];
415 uint32_t inputs_array_size;
416 uint32_t outputs_array_size;
417 uint32_t uniforms_array_size;
418
419 /* Booleans for whether the corresponding QFILE_VARY[i] is
420 * flat-shaded. This doesn't count gl_FragColor flat-shading, which is
421 * controlled by shader->color_inputs and rasterizer->flatshade in the
422 * gallium driver.
423 */
424 BITSET_WORD flat_shade_flags[BITSET_WORDS(V3D_MAX_FS_INPUTS)];
425
426 /* Booleans for whether the corresponding QFILE_VARY[i] uses the
427 * default glShadeModel() behavior.
428 */
429 BITSET_WORD shade_model_flags[BITSET_WORDS(V3D_MAX_FS_INPUTS)];
430
431 struct v3d_ubo_range *ubo_ranges;
432 bool *ubo_range_used;
433 uint32_t ubo_ranges_array_size;
434 /** Number of uniform areas tracked in ubo_ranges. */
435 uint32_t num_ubo_ranges;
436 uint32_t next_ubo_dst_offset;
437
438 /* State for whether we're executing on each channel currently. 0 if
439 * yes, otherwise a block number + 1 that the channel jumped to.
440 */
441 struct qreg execute;
442
443 struct qreg line_x, point_x, point_y;
444
445 /**
446 * Instance ID, which comes in before the vertex attribute payload if
447 * the shader record requests it.
448 */
449 struct qreg iid;
450
451 /**
452 * Vertex ID, which comes in before the vertex attribute payload
453 * (after Instance ID) if the shader record requests it.
454 */
455 struct qreg vid;
456
457 /* Fragment shader payload regs. */
458 struct qreg payload_w, payload_w_centroid, payload_z;
459
460 uint8_t vattr_sizes[V3D_MAX_VS_INPUTS];
461 uint32_t num_vpm_writes;
462
463 /**
464 * Array of the VARYING_SLOT_* of all FS QFILE_VARY reads.
465 *
466 * This includes those that aren't part of the VPM varyings, like
467 * point/line coordinates.
468 */
469 struct v3d_varying_slot input_slots[V3D_MAX_FS_INPUTS];
470
471 /**
472 * An entry per outputs[] in the VS indicating what the VARYING_SLOT_*
473 * of the output is. Used to emit from the VS in the order that the
474 * FS needs.
475 */
476 struct v3d_varying_slot *output_slots;
477
478 struct pipe_shader_state *shader_state;
479 struct v3d_key *key;
480 struct v3d_fs_key *fs_key;
481 struct v3d_vs_key *vs_key;
482
483 /* Live ranges of temps. */
484 int *temp_start, *temp_end;
485
486 uint32_t *uniform_data;
487 enum quniform_contents *uniform_contents;
488 uint32_t uniform_array_size;
489 uint32_t num_uniforms;
490 uint32_t num_outputs;
491 uint32_t output_position_index;
492 nir_variable *output_color_var[4];
493 uint32_t output_point_size_index;
494 uint32_t output_sample_mask_index;
495
496 struct qreg undef;
497 uint32_t num_temps;
498
499 struct list_head blocks;
500 int next_block_index;
501 struct qblock *cur_block;
502 struct qblock *loop_cont_block;
503 struct qblock *loop_break_block;
504
505 uint64_t *qpu_insts;
506 uint32_t qpu_inst_count;
507 uint32_t qpu_inst_size;
508
509 /* For the FS, the number of varying inputs not counting the
510 * point/line varyings payload
511 */
512 uint32_t num_inputs;
513
514 /**
515 * Number of inputs from num_inputs remaining to be queued to the read
516 * FIFO in the VS/CS.
517 */
518 uint32_t num_inputs_remaining;
519
520 /* Number of inputs currently in the read FIFO for the VS/CS */
521 uint32_t num_inputs_in_fifo;
522
523 /** Next offset in the VPM to read from in the VS/CS */
524 uint32_t vpm_read_offset;
525
526 uint32_t program_id;
527 uint32_t variant_id;
528
529 /* Set to compile program in threaded FS mode, where SIG_THREAD_SWITCH
530 * is used to hide texturing latency at the cost of limiting ourselves
531 * to the bottom half of physical reg space.
532 */
533 bool fs_threaded;
534
535 bool last_thrsw_at_top_level;
536
537 bool failed;
538 };
539
540 struct v3d_uniform_list {
541 enum quniform_contents *contents;
542 uint32_t *data;
543 uint32_t count;
544 };
545
546 struct v3d_prog_data {
547 struct v3d_uniform_list uniforms;
548
549 struct v3d_ubo_range *ubo_ranges;
550 uint32_t num_ubo_ranges;
551 uint32_t ubo_size;
552
553 uint8_t num_inputs;
554
555 };
556
557 struct v3d_vs_prog_data {
558 struct v3d_prog_data base;
559
560 bool uses_iid, uses_vid;
561
562 /* Number of components read from each vertex attribute. */
563 uint8_t vattr_sizes[32];
564
565 /* Total number of components read, for the shader state record. */
566 uint32_t vpm_input_size;
567
568 /* Total number of components written, for the shader state record. */
569 uint32_t vpm_output_size;
570 };
571
572 struct v3d_fs_prog_data {
573 struct v3d_prog_data base;
574
575 struct v3d_varying_slot input_slots[V3D_MAX_FS_INPUTS];
576
577 /* Bitmask for whether the corresponding input is flat-shaded,
578 * independent of rasterizer (gl_FragColor) flat-shading.
579 */
580 BITSET_WORD flat_shade_flags[BITSET_WORDS(V3D_MAX_FS_INPUTS)];
581 /* Bitmask for whether the corresponding input uses the default
582 * glShadeModel() behavior.
583 */
584 BITSET_WORD shade_model_flags[BITSET_WORDS(V3D_MAX_FS_INPUTS)];
585
586 bool writes_z;
587 bool discard;
588 };
589
590 /* Special nir_load_input intrinsic index for loading the current TLB
591 * destination color.
592 */
593 #define V3D_NIR_TLB_COLOR_READ_INPUT 2000000000
594
595 #define V3D_NIR_MS_MASK_OUTPUT 2000000000
596
597 extern const nir_shader_compiler_options v3d_nir_options;
598
599 const struct v3d_compiler *v3d_compiler_init(const struct v3d_device_info *devinfo);
600 void v3d_compiler_free(const struct v3d_compiler *compiler);
601 void v3d_optimize_nir(struct nir_shader *s);
602
603 uint64_t *v3d_compile_vs(const struct v3d_compiler *compiler,
604 struct v3d_vs_key *key,
605 struct v3d_vs_prog_data *prog_data,
606 nir_shader *s,
607 int program_id, int variant_id,
608 uint32_t *final_assembly_size);
609
610 uint64_t *v3d_compile_fs(const struct v3d_compiler *compiler,
611 struct v3d_fs_key *key,
612 struct v3d_fs_prog_data *prog_data,
613 nir_shader *s,
614 int program_id, int variant_id,
615 uint32_t *final_assembly_size);
616
617 void v3d_nir_to_vir(struct v3d_compile *c);
618
619 void vir_compile_destroy(struct v3d_compile *c);
620 const char *vir_get_stage_name(struct v3d_compile *c);
621 struct qblock *vir_new_block(struct v3d_compile *c);
622 void vir_set_emit_block(struct v3d_compile *c, struct qblock *block);
623 void vir_link_blocks(struct qblock *predecessor, struct qblock *successor);
624 struct qblock *vir_entry_block(struct v3d_compile *c);
625 struct qblock *vir_exit_block(struct v3d_compile *c);
626 struct qinst *vir_add_inst(enum v3d_qpu_add_op op, struct qreg dst,
627 struct qreg src0, struct qreg src1);
628 struct qinst *vir_mul_inst(enum v3d_qpu_mul_op op, struct qreg dst,
629 struct qreg src0, struct qreg src1);
630 struct qinst *vir_branch_inst(enum v3d_qpu_branch_cond cond, struct qreg src0);
631 void vir_remove_instruction(struct v3d_compile *c, struct qinst *qinst);
632 struct qreg vir_uniform(struct v3d_compile *c,
633 enum quniform_contents contents,
634 uint32_t data);
635 void vir_schedule_instructions(struct v3d_compile *c);
636 struct v3d_qpu_instr v3d_qpu_nop(void);
637
638 struct qreg vir_emit_def(struct v3d_compile *c, struct qinst *inst);
639 struct qinst *vir_emit_nondef(struct v3d_compile *c, struct qinst *inst);
640 void vir_set_cond(struct qinst *inst, enum v3d_qpu_cond cond);
641 void vir_set_pf(struct qinst *inst, enum v3d_qpu_pf pf);
642 void vir_set_unpack(struct qinst *inst, int src,
643 enum v3d_qpu_input_unpack unpack);
644
645 struct qreg vir_get_temp(struct v3d_compile *c);
646 void vir_calculate_live_intervals(struct v3d_compile *c);
647 bool vir_has_implicit_uniform(struct qinst *inst);
648 int vir_get_implicit_uniform_src(struct qinst *inst);
649 int vir_get_non_sideband_nsrc(struct qinst *inst);
650 int vir_get_nsrc(struct qinst *inst);
651 bool vir_has_side_effects(struct v3d_compile *c, struct qinst *inst);
652 bool vir_get_add_op(struct qinst *inst, enum v3d_qpu_add_op *op);
653 bool vir_get_mul_op(struct qinst *inst, enum v3d_qpu_mul_op *op);
654 bool vir_is_raw_mov(struct qinst *inst);
655 bool vir_is_tex(struct qinst *inst);
656 bool vir_is_add(struct qinst *inst);
657 bool vir_is_mul(struct qinst *inst);
658 bool vir_is_float_input(struct qinst *inst);
659 bool vir_depends_on_flags(struct qinst *inst);
660 bool vir_writes_r3(struct qinst *inst);
661 bool vir_writes_r4(struct qinst *inst);
662 struct qreg vir_follow_movs(struct v3d_compile *c, struct qreg reg);
663 uint8_t vir_channels_written(struct qinst *inst);
664
665 void vir_dump(struct v3d_compile *c);
666 void vir_dump_inst(struct v3d_compile *c, struct qinst *inst);
667
668 void vir_validate(struct v3d_compile *c);
669
670 void vir_optimize(struct v3d_compile *c);
671 bool vir_opt_algebraic(struct v3d_compile *c);
672 bool vir_opt_constant_folding(struct v3d_compile *c);
673 bool vir_opt_copy_propagate(struct v3d_compile *c);
674 bool vir_opt_dead_code(struct v3d_compile *c);
675 bool vir_opt_peephole_sf(struct v3d_compile *c);
676 bool vir_opt_small_immediates(struct v3d_compile *c);
677 bool vir_opt_vpm(struct v3d_compile *c);
678 void v3d_nir_lower_blend(nir_shader *s, struct v3d_compile *c);
679 void v3d_nir_lower_io(nir_shader *s, struct v3d_compile *c);
680 void v3d_nir_lower_txf_ms(nir_shader *s, struct v3d_compile *c);
681 void vir_lower_uniforms(struct v3d_compile *c);
682
683 void v3d_vir_to_qpu(struct v3d_compile *c);
684 uint32_t v3d_qpu_schedule_instructions(struct v3d_compile *c);
685 void qpu_validate(struct v3d_compile *c);
686 struct qpu_reg *v3d_register_allocate(struct v3d_compile *c);
687 bool vir_init_reg_sets(struct v3d_compiler *compiler);
688
689 void vir_PF(struct v3d_compile *c, struct qreg src, enum v3d_qpu_pf pf);
690
691 static inline bool
692 quniform_contents_is_texture_p0(enum quniform_contents contents)
693 {
694 return (contents >= QUNIFORM_TEXTURE_CONFIG_P0_0 &&
695 contents < (QUNIFORM_TEXTURE_CONFIG_P0_0 +
696 V3D_MAX_TEXTURE_SAMPLERS));
697 }
698
699 static inline struct qreg
700 vir_uniform_ui(struct v3d_compile *c, uint32_t ui)
701 {
702 return vir_uniform(c, QUNIFORM_CONSTANT, ui);
703 }
704
705 static inline struct qreg
706 vir_uniform_f(struct v3d_compile *c, float f)
707 {
708 return vir_uniform(c, QUNIFORM_CONSTANT, fui(f));
709 }
710
711 #define VIR_ALU0(name, vir_inst, op) \
712 static inline struct qreg \
713 vir_##name(struct v3d_compile *c) \
714 { \
715 return vir_emit_def(c, vir_inst(op, c->undef, \
716 c->undef, c->undef)); \
717 } \
718 static inline struct qinst * \
719 vir_##name##_dest(struct v3d_compile *c, struct qreg dest) \
720 { \
721 return vir_emit_nondef(c, vir_inst(op, dest, \
722 c->undef, c->undef)); \
723 }
724
725 #define VIR_ALU1(name, vir_inst, op) \
726 static inline struct qreg \
727 vir_##name(struct v3d_compile *c, struct qreg a) \
728 { \
729 return vir_emit_def(c, vir_inst(op, c->undef, \
730 a, c->undef)); \
731 } \
732 static inline struct qinst * \
733 vir_##name##_dest(struct v3d_compile *c, struct qreg dest, \
734 struct qreg a) \
735 { \
736 return vir_emit_nondef(c, vir_inst(op, dest, a, \
737 c->undef)); \
738 }
739
740 #define VIR_ALU2(name, vir_inst, op) \
741 static inline struct qreg \
742 vir_##name(struct v3d_compile *c, struct qreg a, struct qreg b) \
743 { \
744 return vir_emit_def(c, vir_inst(op, c->undef, a, b)); \
745 } \
746 static inline struct qinst * \
747 vir_##name##_dest(struct v3d_compile *c, struct qreg dest, \
748 struct qreg a, struct qreg b) \
749 { \
750 return vir_emit_nondef(c, vir_inst(op, dest, a, b)); \
751 }
752
753 #define VIR_NODST_1(name, vir_inst, op) \
754 static inline struct qinst * \
755 vir_##name(struct v3d_compile *c, struct qreg a) \
756 { \
757 return vir_emit_nondef(c, vir_inst(op, c->undef, \
758 a, c->undef)); \
759 }
760
761 #define VIR_NODST_2(name, vir_inst, op) \
762 static inline struct qinst * \
763 vir_##name(struct v3d_compile *c, struct qreg a, struct qreg b) \
764 { \
765 return vir_emit_nondef(c, vir_inst(op, c->undef, \
766 a, b)); \
767 }
768
769 #define VIR_A_ALU2(name) VIR_ALU2(name, vir_add_inst, V3D_QPU_A_##name)
770 #define VIR_M_ALU2(name) VIR_ALU2(name, vir_mul_inst, V3D_QPU_M_##name)
771 #define VIR_A_ALU1(name) VIR_ALU1(name, vir_add_inst, V3D_QPU_A_##name)
772 #define VIR_M_ALU1(name) VIR_ALU1(name, vir_mul_inst, V3D_QPU_M_##name)
773 #define VIR_A_ALU0(name) VIR_ALU0(name, vir_add_inst, V3D_QPU_A_##name)
774 #define VIR_M_ALU0(name) VIR_ALU0(name, vir_mul_inst, V3D_QPU_M_##name)
775 #define VIR_A_NODST_2(name) VIR_NODST_2(name, vir_add_inst, V3D_QPU_A_##name)
776 #define VIR_M_NODST_2(name) VIR_NODST_2(name, vir_mul_inst, V3D_QPU_M_##name)
777 #define VIR_A_NODST_1(name) VIR_NODST_1(name, vir_add_inst, V3D_QPU_A_##name)
778 #define VIR_M_NODST_1(name) VIR_NODST_1(name, vir_mul_inst, V3D_QPU_M_##name)
779
780 VIR_A_ALU2(FADD)
781 VIR_A_ALU2(VFPACK)
782 VIR_A_ALU2(FSUB)
783 VIR_A_ALU2(FMIN)
784 VIR_A_ALU2(FMAX)
785
786 VIR_A_ALU2(ADD)
787 VIR_A_ALU2(SUB)
788 VIR_A_ALU2(SHL)
789 VIR_A_ALU2(SHR)
790 VIR_A_ALU2(ASR)
791 VIR_A_ALU2(ROR)
792 VIR_A_ALU2(MIN)
793 VIR_A_ALU2(MAX)
794 VIR_A_ALU2(UMIN)
795 VIR_A_ALU2(UMAX)
796 VIR_A_ALU2(AND)
797 VIR_A_ALU2(OR)
798 VIR_A_ALU2(XOR)
799 VIR_A_ALU2(VADD)
800 VIR_A_ALU2(VSUB)
801 VIR_A_ALU1(NOT)
802 VIR_A_ALU1(NEG)
803 VIR_A_ALU1(FLAPUSH)
804 VIR_A_ALU1(FLBPUSH)
805 VIR_A_ALU1(FLBPOP)
806 VIR_A_ALU1(SETMSF)
807 VIR_A_ALU1(SETREVF)
808 VIR_A_ALU1(TIDX)
809 VIR_A_ALU1(EIDX)
810
811 VIR_A_ALU0(FXCD)
812 VIR_A_ALU0(XCD)
813 VIR_A_ALU0(FYCD)
814 VIR_A_ALU0(YCD)
815 VIR_A_ALU0(MSF)
816 VIR_A_ALU0(REVF)
817 VIR_A_NODST_1(VPMSETUP)
818 VIR_A_ALU2(FCMP)
819 VIR_A_ALU2(VFMAX)
820
821 VIR_A_ALU1(FROUND)
822 VIR_A_ALU1(FTOIN)
823 VIR_A_ALU1(FTRUNC)
824 VIR_A_ALU1(FTOIZ)
825 VIR_A_ALU1(FFLOOR)
826 VIR_A_ALU1(FTOUZ)
827 VIR_A_ALU1(FCEIL)
828 VIR_A_ALU1(FTOC)
829
830 VIR_A_ALU1(FDX)
831 VIR_A_ALU1(FDY)
832
833 VIR_A_ALU1(ITOF)
834 VIR_A_ALU1(CLZ)
835 VIR_A_ALU1(UTOF)
836
837 VIR_M_ALU2(UMUL24)
838 VIR_M_ALU2(FMUL)
839 VIR_M_ALU2(SMUL24)
840 VIR_M_NODST_2(MULTOP)
841
842 VIR_M_ALU1(MOV)
843 VIR_M_ALU1(FMOV)
844
845 static inline struct qinst *
846 vir_MOV_cond(struct v3d_compile *c, enum v3d_qpu_cond cond,
847 struct qreg dest, struct qreg src)
848 {
849 struct qinst *mov = vir_MOV_dest(c, dest, src);
850 vir_set_cond(mov, cond);
851 return mov;
852 }
853
854 static inline struct qreg
855 vir_SEL(struct v3d_compile *c, enum v3d_qpu_cond cond,
856 struct qreg src0, struct qreg src1)
857 {
858 struct qreg t = vir_get_temp(c);
859 vir_MOV_dest(c, t, src1);
860 vir_MOV_cond(c, cond, t, src0);
861 return t;
862 }
863
864 static inline void
865 vir_VPM_WRITE(struct v3d_compile *c, struct qreg val)
866 {
867 vir_MOV_dest(c, vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_VPM), val);
868 }
869
870 static inline struct qinst *
871 vir_NOP(struct v3d_compile *c)
872 {
873 return vir_emit_nondef(c, vir_add_inst(V3D_QPU_A_NOP,
874 c->undef, c->undef, c->undef));
875 }
876 /*
877 static inline struct qreg
878 vir_LOAD_IMM(struct v3d_compile *c, uint32_t val)
879 {
880 return vir_emit_def(c, vir_inst(QOP_LOAD_IMM, c->undef,
881 vir_reg(QFILE_LOAD_IMM, val), c->undef));
882 }
883
884 static inline struct qreg
885 vir_LOAD_IMM_U2(struct v3d_compile *c, uint32_t val)
886 {
887 return vir_emit_def(c, vir_inst(QOP_LOAD_IMM_U2, c->undef,
888 vir_reg(QFILE_LOAD_IMM, val),
889 c->undef));
890 }
891 static inline struct qreg
892 vir_LOAD_IMM_I2(struct v3d_compile *c, uint32_t val)
893 {
894 return vir_emit_def(c, vir_inst(QOP_LOAD_IMM_I2, c->undef,
895 vir_reg(QFILE_LOAD_IMM, val),
896 c->undef));
897 }
898 */
899
900 static inline struct qinst *
901 vir_BRANCH(struct v3d_compile *c, enum v3d_qpu_cond cond)
902 {
903 /* The actual uniform_data value will be set at scheduling time */
904 return vir_emit_nondef(c, vir_branch_inst(cond, vir_uniform_ui(c, 0)));
905 }
906
907 #define vir_for_each_block(block, c) \
908 list_for_each_entry(struct qblock, block, &c->blocks, link)
909
910 #define vir_for_each_block_rev(block, c) \
911 list_for_each_entry_rev(struct qblock, block, &c->blocks, link)
912
913 /* Loop over the non-NULL members of the successors array. */
914 #define vir_for_each_successor(succ, block) \
915 for (struct qblock *succ = block->successors[0]; \
916 succ != NULL; \
917 succ = (succ == block->successors[1] ? NULL : \
918 block->successors[1]))
919
920 #define vir_for_each_inst(inst, block) \
921 list_for_each_entry(struct qinst, inst, &block->instructions, link)
922
923 #define vir_for_each_inst_rev(inst, block) \
924 list_for_each_entry_rev(struct qinst, inst, &block->instructions, link)
925
926 #define vir_for_each_inst_safe(inst, block) \
927 list_for_each_entry_safe(struct qinst, inst, &block->instructions, link)
928
929 #define vir_for_each_inst_inorder(inst, c) \
930 vir_for_each_block(_block, c) \
931 vir_for_each_inst(inst, _block)
932
933 #endif /* V3D_COMPILER_H */