broadcom: Add VC5 NIR compiler.
[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_TEXTURE_MSAA_ADDR,
233
234 QUNIFORM_UBO_ADDR,
235
236 QUNIFORM_TEXRECT_SCALE_X,
237 QUNIFORM_TEXRECT_SCALE_Y,
238
239 QUNIFORM_TEXTURE_BORDER_COLOR,
240
241 QUNIFORM_STENCIL,
242
243 QUNIFORM_ALPHA_REF,
244 QUNIFORM_SAMPLE_MASK,
245 };
246
247 struct v3d_varying_slot {
248 uint8_t slot_and_component;
249 };
250
251 static inline struct v3d_varying_slot
252 v3d_slot_from_slot_and_component(uint8_t slot, uint8_t component)
253 {
254 assert(slot < 255 / 4);
255 return (struct v3d_varying_slot){ (slot << 2) + component };
256 }
257
258 static inline uint8_t v3d_slot_get_slot(struct v3d_varying_slot slot)
259 {
260 return slot.slot_and_component >> 2;
261 }
262
263 static inline uint8_t v3d_slot_get_component(struct v3d_varying_slot slot)
264 {
265 return slot.slot_and_component & 3;
266 }
267
268 struct v3d_ubo_range {
269 /**
270 * offset in bytes from the start of the ubo where this range is
271 * uploaded.
272 *
273 * Only set once used is set.
274 */
275 uint32_t dst_offset;
276
277 /**
278 * offset in bytes from the start of the gallium uniforms where the
279 * data comes from.
280 */
281 uint32_t src_offset;
282
283 /** size in bytes of this ubo range */
284 uint32_t size;
285 };
286
287 struct v3d_key {
288 void *shader_state;
289 struct {
290 uint8_t swizzle[4];
291 uint8_t return_size;
292 uint8_t return_channels;
293 union {
294 struct {
295 unsigned compare_mode:1;
296 unsigned compare_func:3;
297 unsigned wrap_s:3;
298 unsigned wrap_t:3;
299 };
300 struct {
301 uint16_t msaa_width, msaa_height;
302 };
303 };
304 } tex[V3D_MAX_TEXTURE_SAMPLERS];
305 uint8_t ucp_enables;
306 };
307
308 struct v3d_fs_key {
309 struct v3d_key base;
310 bool depth_enabled;
311 bool is_points;
312 bool is_lines;
313 bool alpha_test;
314 bool point_coord_upper_left;
315 bool light_twoside;
316 bool msaa;
317 bool sample_coverage;
318 bool sample_alpha_to_coverage;
319 bool sample_alpha_to_one;
320 bool clamp_color;
321 bool swap_color_rb;
322 uint8_t alpha_test_func;
323 uint8_t logicop_func;
324 uint32_t point_sprite_mask;
325
326 struct pipe_rt_blend_state blend;
327 };
328
329 struct v3d_vs_key {
330 struct v3d_key base;
331
332 struct v3d_varying_slot fs_inputs[V3D_MAX_FS_INPUTS];
333 uint8_t num_fs_inputs;
334
335 bool is_coord;
336 bool per_vertex_point_size;
337 bool clamp_color;
338 };
339
340 /** A basic block of VIR intructions. */
341 struct qblock {
342 struct list_head link;
343
344 struct list_head instructions;
345
346 struct set *predecessors;
347 struct qblock *successors[2];
348
349 int index;
350
351 /* Instruction IPs for the first and last instruction of the block.
352 * Set by qpu_schedule.c.
353 */
354 uint32_t start_qpu_ip;
355 uint32_t end_qpu_ip;
356
357 /* Instruction IP for the branch instruction of the block. Set by
358 * qpu_schedule.c.
359 */
360 uint32_t branch_qpu_ip;
361
362 /** Offset within the uniform stream at the start of the block. */
363 uint32_t start_uniform;
364 /** Offset within the uniform stream of the branch instruction */
365 uint32_t branch_uniform;
366
367 /** @{ used by v3d_vir_live_variables.c */
368 BITSET_WORD *def;
369 BITSET_WORD *use;
370 BITSET_WORD *live_in;
371 BITSET_WORD *live_out;
372 int start_ip, end_ip;
373 /** @} */
374 };
375
376 /**
377 * Compiler state saved across compiler invocations, for any expensive global
378 * setup.
379 */
380 struct v3d_compiler {
381 const struct v3d_device_info *devinfo;
382 struct ra_regs *regs;
383 unsigned int reg_class[3];
384 };
385
386 struct v3d_compile {
387 const struct v3d_device_info *devinfo;
388 nir_shader *s;
389 nir_function_impl *impl;
390 struct exec_list *cf_node_list;
391 const struct v3d_compiler *compiler;
392
393 /**
394 * Mapping from nir_register * or nir_ssa_def * to array of struct
395 * qreg for the values.
396 */
397 struct hash_table *def_ht;
398
399 /* For each temp, the instruction generating its value. */
400 struct qinst **defs;
401 uint32_t defs_array_size;
402
403 /**
404 * Inputs to the shader, arranged by TGSI declaration order.
405 *
406 * Not all fragment shader QFILE_VARY reads are present in this array.
407 */
408 struct qreg *inputs;
409 struct qreg *outputs;
410 bool msaa_per_sample_output;
411 struct qreg color_reads[V3D_MAX_SAMPLES];
412 struct qreg sample_colors[V3D_MAX_SAMPLES];
413 uint32_t inputs_array_size;
414 uint32_t outputs_array_size;
415 uint32_t uniforms_array_size;
416
417 /* Booleans for whether the corresponding QFILE_VARY[i] is
418 * flat-shaded. This doesn't count gl_FragColor flat-shading, which is
419 * controlled by shader->color_inputs and rasterizer->flatshade in the
420 * gallium driver.
421 */
422 BITSET_WORD flat_shade_flags[BITSET_WORDS(V3D_MAX_FS_INPUTS)];
423
424 struct v3d_ubo_range *ubo_ranges;
425 bool *ubo_range_used;
426 uint32_t ubo_ranges_array_size;
427 /** Number of uniform areas tracked in ubo_ranges. */
428 uint32_t num_ubo_ranges;
429 uint32_t next_ubo_dst_offset;
430
431 /* State for whether we're executing on each channel currently. 0 if
432 * yes, otherwise a block number + 1 that the channel jumped to.
433 */
434 struct qreg execute;
435
436 struct qreg line_x, point_x, point_y;
437
438 /**
439 * Instance ID, which comes in before the vertex attribute payload if
440 * the shader record requests it.
441 */
442 struct qreg iid;
443
444 /**
445 * Vertex ID, which comes in before the vertex attribute payload
446 * (after Instance ID) if the shader record requests it.
447 */
448 struct qreg vid;
449
450 /* Fragment shader payload regs. */
451 struct qreg payload_w, payload_w_centroid, payload_z;
452
453 /** boolean (~0 -> true) if the fragment has been discarded. */
454 struct qreg discard;
455
456 uint8_t vattr_sizes[V3D_MAX_VS_INPUTS];
457 uint32_t num_vpm_writes;
458
459 /**
460 * Array of the VARYING_SLOT_* of all FS QFILE_VARY reads.
461 *
462 * This includes those that aren't part of the VPM varyings, like
463 * point/line coordinates.
464 */
465 struct v3d_varying_slot input_slots[V3D_MAX_FS_INPUTS];
466
467 /**
468 * An entry per outputs[] in the VS indicating what the VARYING_SLOT_*
469 * of the output is. Used to emit from the VS in the order that the
470 * FS needs.
471 */
472 struct v3d_varying_slot *output_slots;
473
474 struct pipe_shader_state *shader_state;
475 struct v3d_key *key;
476 struct v3d_fs_key *fs_key;
477 struct v3d_vs_key *vs_key;
478
479 /* Live ranges of temps. */
480 int *temp_start, *temp_end;
481
482 uint32_t *uniform_data;
483 enum quniform_contents *uniform_contents;
484 uint32_t uniform_array_size;
485 uint32_t num_uniforms;
486 uint32_t num_outputs;
487 uint32_t output_position_index;
488 nir_variable *output_color_var;
489 uint32_t output_point_size_index;
490 uint32_t output_sample_mask_index;
491
492 struct qreg undef;
493 uint32_t num_temps;
494
495 struct list_head blocks;
496 int next_block_index;
497 struct qblock *cur_block;
498 struct qblock *loop_cont_block;
499 struct qblock *loop_break_block;
500
501 uint64_t *qpu_insts;
502 uint32_t qpu_inst_count;
503 uint32_t qpu_inst_size;
504
505 /* For the FS, the number of varying inputs not counting the
506 * point/line varyings payload
507 */
508 uint32_t num_inputs;
509
510 /**
511 * Number of inputs from num_inputs remaining to be queued to the read
512 * FIFO in the VS/CS.
513 */
514 uint32_t num_inputs_remaining;
515
516 /* Number of inputs currently in the read FIFO for the VS/CS */
517 uint32_t num_inputs_in_fifo;
518
519 /** Next offset in the VPM to read from in the VS/CS */
520 uint32_t vpm_read_offset;
521
522 uint32_t program_id;
523 uint32_t variant_id;
524
525 /* Set to compile program in threaded FS mode, where SIG_THREAD_SWITCH
526 * is used to hide texturing latency at the cost of limiting ourselves
527 * to the bottom half of physical reg space.
528 */
529 bool fs_threaded;
530
531 bool last_thrsw_at_top_level;
532
533 bool failed;
534 };
535
536 struct v3d_uniform_list {
537 enum quniform_contents *contents;
538 uint32_t *data;
539 uint32_t count;
540 };
541
542 struct v3d_prog_data {
543 struct v3d_uniform_list uniforms;
544
545 struct v3d_ubo_range *ubo_ranges;
546 uint32_t num_ubo_ranges;
547 uint32_t ubo_size;
548
549 uint8_t num_inputs;
550
551 };
552
553 struct v3d_vs_prog_data {
554 struct v3d_prog_data base;
555
556 bool uses_iid, uses_vid;
557
558 /* Number of components read from each vertex attribute. */
559 uint8_t vattr_sizes[32];
560
561 /* Total number of components read, for the shader state record. */
562 uint32_t vpm_input_size;
563
564 /* Total number of components written, for the shader state record. */
565 uint32_t vpm_output_size;
566 };
567
568 struct v3d_fs_prog_data {
569 struct v3d_prog_data base;
570
571 struct v3d_varying_slot input_slots[V3D_MAX_FS_INPUTS];
572
573 /** bitmask of which inputs are color inputs, for flat shade handling. */
574 uint32_t color_inputs[BITSET_WORDS(V3D_MAX_FS_INPUTS)];
575
576 /* Bitmask for whether the corresponding input is flat-shaded,
577 * independent of rasterizer (gl_FragColor) flat-shading.
578 */
579 BITSET_WORD flat_shade_flags[BITSET_WORDS(V3D_MAX_FS_INPUTS)];
580
581 bool writes_z;
582 };
583
584 /* Special nir_load_input intrinsic index for loading the current TLB
585 * destination color.
586 */
587 #define V3D_NIR_TLB_COLOR_READ_INPUT 2000000000
588
589 #define V3D_NIR_MS_MASK_OUTPUT 2000000000
590
591 extern const nir_shader_compiler_options v3d_nir_options;
592
593 const struct v3d_compiler *v3d_compiler_init(const struct v3d_device_info *devinfo);
594 void v3d_compiler_free(const struct v3d_compiler *compiler);
595 void v3d_optimize_nir(struct nir_shader *s);
596
597 uint64_t *v3d_compile_vs(const struct v3d_compiler *compiler,
598 struct v3d_vs_key *key,
599 struct v3d_vs_prog_data *prog_data,
600 nir_shader *s,
601 int program_id, int variant_id,
602 uint32_t *final_assembly_size);
603
604 uint64_t *v3d_compile_fs(const struct v3d_compiler *compiler,
605 struct v3d_fs_key *key,
606 struct v3d_fs_prog_data *prog_data,
607 nir_shader *s,
608 int program_id, int variant_id,
609 uint32_t *final_assembly_size);
610
611 void v3d_nir_to_vir(struct v3d_compile *c);
612
613 void vir_compile_destroy(struct v3d_compile *c);
614 const char *vir_get_stage_name(struct v3d_compile *c);
615 struct qblock *vir_new_block(struct v3d_compile *c);
616 void vir_set_emit_block(struct v3d_compile *c, struct qblock *block);
617 void vir_link_blocks(struct qblock *predecessor, struct qblock *successor);
618 struct qblock *vir_entry_block(struct v3d_compile *c);
619 struct qblock *vir_exit_block(struct v3d_compile *c);
620 struct qinst *vir_add_inst(enum v3d_qpu_add_op op, struct qreg dst,
621 struct qreg src0, struct qreg src1);
622 struct qinst *vir_mul_inst(enum v3d_qpu_mul_op op, struct qreg dst,
623 struct qreg src0, struct qreg src1);
624 struct qinst *vir_branch_inst(enum v3d_qpu_branch_cond cond, struct qreg src0);
625 void vir_remove_instruction(struct v3d_compile *c, struct qinst *qinst);
626 struct qreg vir_uniform(struct v3d_compile *c,
627 enum quniform_contents contents,
628 uint32_t data);
629 void vir_schedule_instructions(struct v3d_compile *c);
630 struct v3d_qpu_instr v3d_qpu_nop(void);
631
632 struct qreg vir_emit_def(struct v3d_compile *c, struct qinst *inst);
633 struct qinst *vir_emit_nondef(struct v3d_compile *c, struct qinst *inst);
634 void vir_set_cond(struct qinst *inst, enum v3d_qpu_cond cond);
635 void vir_set_pf(struct qinst *inst, enum v3d_qpu_pf pf);
636 void vir_set_unpack(struct qinst *inst, int src,
637 enum v3d_qpu_input_unpack unpack);
638
639 struct qreg vir_get_temp(struct v3d_compile *c);
640 void vir_calculate_live_intervals(struct v3d_compile *c);
641 bool vir_has_implicit_uniform(struct qinst *inst);
642 int vir_get_implicit_uniform_src(struct qinst *inst);
643 int vir_get_non_sideband_nsrc(struct qinst *inst);
644 int vir_get_nsrc(struct qinst *inst);
645 bool vir_has_side_effects(struct v3d_compile *c, struct qinst *inst);
646 bool vir_get_add_op(struct qinst *inst, enum v3d_qpu_add_op *op);
647 bool vir_get_mul_op(struct qinst *inst, enum v3d_qpu_mul_op *op);
648 bool vir_is_raw_mov(struct qinst *inst);
649 bool vir_is_tex(struct qinst *inst);
650 bool vir_is_add(struct qinst *inst);
651 bool vir_is_mul(struct qinst *inst);
652 bool vir_is_float_input(struct qinst *inst);
653 bool vir_depends_on_flags(struct qinst *inst);
654 bool vir_writes_r3(struct qinst *inst);
655 bool vir_writes_r4(struct qinst *inst);
656 struct qreg vir_follow_movs(struct v3d_compile *c, struct qreg reg);
657 uint8_t vir_channels_written(struct qinst *inst);
658
659 void vir_dump(struct v3d_compile *c);
660 void vir_dump_inst(struct v3d_compile *c, struct qinst *inst);
661
662 void vir_validate(struct v3d_compile *c);
663
664 void vir_optimize(struct v3d_compile *c);
665 bool vir_opt_algebraic(struct v3d_compile *c);
666 bool vir_opt_constant_folding(struct v3d_compile *c);
667 bool vir_opt_copy_propagate(struct v3d_compile *c);
668 bool vir_opt_dead_code(struct v3d_compile *c);
669 bool vir_opt_peephole_sf(struct v3d_compile *c);
670 bool vir_opt_small_immediates(struct v3d_compile *c);
671 bool vir_opt_vpm(struct v3d_compile *c);
672 void v3d_nir_lower_blend(nir_shader *s, struct v3d_compile *c);
673 void v3d_nir_lower_io(nir_shader *s, struct v3d_compile *c);
674 void v3d_nir_lower_txf_ms(nir_shader *s, struct v3d_compile *c);
675 void vir_lower_uniforms(struct v3d_compile *c);
676
677 void v3d_vir_to_qpu(struct v3d_compile *c);
678 uint32_t v3d_qpu_schedule_instructions(struct v3d_compile *c);
679 void qpu_validate(struct v3d_compile *c);
680 struct qpu_reg *v3d_register_allocate(struct v3d_compile *c);
681 bool vir_init_reg_sets(struct v3d_compiler *compiler);
682
683 void vir_PF(struct v3d_compile *c, struct qreg src, enum v3d_qpu_pf pf);
684
685 static inline bool
686 quniform_contents_is_texture_p0(enum quniform_contents contents)
687 {
688 return (contents >= QUNIFORM_TEXTURE_CONFIG_P0_0 &&
689 contents < (QUNIFORM_TEXTURE_CONFIG_P0_0 +
690 V3D_MAX_TEXTURE_SAMPLERS));
691 }
692
693 static inline struct qreg
694 vir_uniform_ui(struct v3d_compile *c, uint32_t ui)
695 {
696 return vir_uniform(c, QUNIFORM_CONSTANT, ui);
697 }
698
699 static inline struct qreg
700 vir_uniform_f(struct v3d_compile *c, float f)
701 {
702 return vir_uniform(c, QUNIFORM_CONSTANT, fui(f));
703 }
704
705 #define VIR_ALU0(name, vir_inst, op) \
706 static inline struct qreg \
707 vir_##name(struct v3d_compile *c) \
708 { \
709 return vir_emit_def(c, vir_inst(op, c->undef, \
710 c->undef, c->undef)); \
711 } \
712 static inline struct qinst * \
713 vir_##name##_dest(struct v3d_compile *c, struct qreg dest) \
714 { \
715 return vir_emit_nondef(c, vir_inst(op, dest, \
716 c->undef, c->undef)); \
717 }
718
719 #define VIR_ALU1(name, vir_inst, op) \
720 static inline struct qreg \
721 vir_##name(struct v3d_compile *c, struct qreg a) \
722 { \
723 return vir_emit_def(c, vir_inst(op, c->undef, \
724 a, c->undef)); \
725 } \
726 static inline struct qinst * \
727 vir_##name##_dest(struct v3d_compile *c, struct qreg dest, \
728 struct qreg a) \
729 { \
730 return vir_emit_nondef(c, vir_inst(op, dest, a, \
731 c->undef)); \
732 }
733
734 #define VIR_ALU2(name, vir_inst, op) \
735 static inline struct qreg \
736 vir_##name(struct v3d_compile *c, struct qreg a, struct qreg b) \
737 { \
738 return vir_emit_def(c, vir_inst(op, c->undef, a, b)); \
739 } \
740 static inline struct qinst * \
741 vir_##name##_dest(struct v3d_compile *c, struct qreg dest, \
742 struct qreg a, struct qreg b) \
743 { \
744 return vir_emit_nondef(c, vir_inst(op, dest, a, b)); \
745 }
746
747 #define VIR_NODST_1(name, vir_inst, op) \
748 static inline struct qinst * \
749 vir_##name(struct v3d_compile *c, struct qreg a) \
750 { \
751 return vir_emit_nondef(c, vir_inst(op, c->undef, \
752 a, c->undef)); \
753 }
754
755 #define VIR_NODST_2(name, vir_inst, op) \
756 static inline struct qinst * \
757 vir_##name(struct v3d_compile *c, struct qreg a, struct qreg b) \
758 { \
759 return vir_emit_nondef(c, vir_inst(op, c->undef, \
760 a, b)); \
761 }
762
763 #define VIR_A_ALU2(name) VIR_ALU2(name, vir_add_inst, V3D_QPU_A_##name)
764 #define VIR_M_ALU2(name) VIR_ALU2(name, vir_mul_inst, V3D_QPU_M_##name)
765 #define VIR_A_ALU1(name) VIR_ALU1(name, vir_add_inst, V3D_QPU_A_##name)
766 #define VIR_M_ALU1(name) VIR_ALU1(name, vir_mul_inst, V3D_QPU_M_##name)
767 #define VIR_A_ALU0(name) VIR_ALU0(name, vir_add_inst, V3D_QPU_A_##name)
768 #define VIR_M_ALU0(name) VIR_ALU0(name, vir_mul_inst, V3D_QPU_M_##name)
769 #define VIR_A_NODST_2(name) VIR_NODST_2(name, vir_add_inst, V3D_QPU_A_##name)
770 #define VIR_M_NODST_2(name) VIR_NODST_2(name, vir_mul_inst, V3D_QPU_M_##name)
771 #define VIR_A_NODST_1(name) VIR_NODST_1(name, vir_add_inst, V3D_QPU_A_##name)
772 #define VIR_M_NODST_1(name) VIR_NODST_1(name, vir_mul_inst, V3D_QPU_M_##name)
773
774 VIR_A_ALU2(FADD)
775 VIR_A_ALU2(VFPACK)
776 VIR_A_ALU2(FSUB)
777 VIR_A_ALU2(FMIN)
778 VIR_A_ALU2(FMAX)
779
780 VIR_A_ALU2(ADD)
781 VIR_A_ALU2(SUB)
782 VIR_A_ALU2(SHL)
783 VIR_A_ALU2(SHR)
784 VIR_A_ALU2(ASR)
785 VIR_A_ALU2(ROR)
786 VIR_A_ALU2(MIN)
787 VIR_A_ALU2(MAX)
788 VIR_A_ALU2(UMIN)
789 VIR_A_ALU2(UMAX)
790 VIR_A_ALU2(AND)
791 VIR_A_ALU2(OR)
792 VIR_A_ALU2(XOR)
793 VIR_A_ALU2(VADD)
794 VIR_A_ALU2(VSUB)
795 VIR_A_ALU1(NOT)
796 VIR_A_ALU1(NEG)
797 VIR_A_ALU1(FLAPUSH)
798 VIR_A_ALU1(FLBPUSH)
799 VIR_A_ALU1(FLBPOP)
800 VIR_A_ALU1(SETMSF)
801 VIR_A_ALU1(SETREVF)
802 VIR_A_ALU1(TIDX)
803 VIR_A_ALU1(EIDX)
804
805 VIR_A_ALU0(FXCD)
806 VIR_A_ALU0(XCD)
807 VIR_A_ALU0(FYCD)
808 VIR_A_ALU0(YCD)
809 VIR_A_ALU0(MSF)
810 VIR_A_ALU0(REVF)
811 VIR_A_NODST_1(VPMSETUP)
812 VIR_A_ALU2(FCMP)
813 VIR_A_ALU2(VFMAX)
814
815 VIR_A_ALU1(FROUND)
816 VIR_A_ALU1(FTOIN)
817 VIR_A_ALU1(FTRUNC)
818 VIR_A_ALU1(FTOIZ)
819 VIR_A_ALU1(FFLOOR)
820 VIR_A_ALU1(FTOUZ)
821 VIR_A_ALU1(FCEIL)
822 VIR_A_ALU1(FTOC)
823
824 VIR_A_ALU1(FDX)
825 VIR_A_ALU1(FDY)
826
827 VIR_A_ALU1(ITOF)
828 VIR_A_ALU1(CLZ)
829 VIR_A_ALU1(UTOF)
830
831 VIR_M_ALU2(UMUL24)
832 VIR_M_ALU2(FMUL)
833 VIR_M_ALU2(SMUL24)
834 VIR_M_NODST_2(MULTOP)
835
836 VIR_M_ALU1(MOV)
837 VIR_M_ALU1(FMOV)
838
839 static inline struct qinst *
840 vir_MOV_cond(struct v3d_compile *c, enum v3d_qpu_cond cond,
841 struct qreg dest, struct qreg src)
842 {
843 struct qinst *mov = vir_MOV_dest(c, dest, src);
844 vir_set_cond(mov, cond);
845 return mov;
846 }
847
848 static inline struct qreg
849 vir_SEL(struct v3d_compile *c, enum v3d_qpu_cond cond,
850 struct qreg src0, struct qreg src1)
851 {
852 struct qreg t = vir_get_temp(c);
853 vir_MOV_dest(c, t, src1);
854 vir_MOV_cond(c, cond, t, src0);
855 return t;
856 }
857
858 static inline void
859 vir_VPM_WRITE(struct v3d_compile *c, struct qreg val)
860 {
861 vir_MOV_dest(c, vir_reg(QFILE_MAGIC, V3D_QPU_WADDR_VPM), val);
862 }
863
864 static inline struct qinst *
865 vir_NOP(struct v3d_compile *c)
866 {
867 return vir_emit_nondef(c, vir_add_inst(V3D_QPU_A_NOP,
868 c->undef, c->undef, c->undef));
869 }
870 /*
871 static inline struct qreg
872 vir_LOAD_IMM(struct v3d_compile *c, uint32_t val)
873 {
874 return vir_emit_def(c, vir_inst(QOP_LOAD_IMM, c->undef,
875 vir_reg(QFILE_LOAD_IMM, val), c->undef));
876 }
877
878 static inline struct qreg
879 vir_LOAD_IMM_U2(struct v3d_compile *c, uint32_t val)
880 {
881 return vir_emit_def(c, vir_inst(QOP_LOAD_IMM_U2, c->undef,
882 vir_reg(QFILE_LOAD_IMM, val),
883 c->undef));
884 }
885 static inline struct qreg
886 vir_LOAD_IMM_I2(struct v3d_compile *c, uint32_t val)
887 {
888 return vir_emit_def(c, vir_inst(QOP_LOAD_IMM_I2, c->undef,
889 vir_reg(QFILE_LOAD_IMM, val),
890 c->undef));
891 }
892 */
893
894 static inline struct qinst *
895 vir_BRANCH(struct v3d_compile *c, enum v3d_qpu_cond cond)
896 {
897 /* The actual uniform_data value will be set at scheduling time */
898 return vir_emit_nondef(c, vir_branch_inst(cond, vir_uniform_ui(c, 0)));
899 }
900
901 #define vir_for_each_block(block, c) \
902 list_for_each_entry(struct qblock, block, &c->blocks, link)
903
904 #define vir_for_each_block_rev(block, c) \
905 list_for_each_entry_rev(struct qblock, block, &c->blocks, link)
906
907 /* Loop over the non-NULL members of the successors array. */
908 #define vir_for_each_successor(succ, block) \
909 for (struct qblock *succ = block->successors[0]; \
910 succ != NULL; \
911 succ = (succ == block->successors[1] ? NULL : \
912 block->successors[1]))
913
914 #define vir_for_each_inst(inst, block) \
915 list_for_each_entry(struct qinst, inst, &block->instructions, link)
916
917 #define vir_for_each_inst_rev(inst, block) \
918 list_for_each_entry_rev(struct qinst, inst, &block->instructions, link)
919
920 #define vir_for_each_inst_safe(inst, block) \
921 list_for_each_entry_safe(struct qinst, inst, &block->instructions, link)
922
923 #define vir_for_each_inst_inorder(inst, c) \
924 vir_for_each_block(_block, c) \
925 vir_for_each_inst(inst, _block)
926
927 #endif /* V3D_COMPILER_H */