vc4: Coalesce MOVs into VPM with the instructions generating the values.
[mesa.git] / src / gallium / drivers / vc4 / vc4_qir.h
1 /*
2 * Copyright © 2014 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 VC4_QIR_H
25 #define VC4_QIR_H
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdbool.h>
30 #include <stdint.h>
31 #include <string.h>
32
33 #include "util/u_simple_list.h"
34 #include "tgsi/tgsi_parse.h"
35
36 enum qfile {
37 QFILE_NULL,
38 QFILE_TEMP,
39 QFILE_VARY,
40 QFILE_UNIF,
41 QFILE_VPM,
42
43 /**
44 * Stores an immediate value in the index field that can be turned
45 * into a small immediate field by qpu_encode_small_immediate().
46 */
47 QFILE_SMALL_IMM,
48 };
49
50 struct qreg {
51 enum qfile file;
52 uint32_t index;
53 };
54
55 enum qop {
56 QOP_UNDEF,
57 QOP_MOV,
58 QOP_FADD,
59 QOP_FSUB,
60 QOP_FMUL,
61 QOP_MUL24,
62 QOP_FMIN,
63 QOP_FMAX,
64 QOP_FMINABS,
65 QOP_FMAXABS,
66 QOP_ADD,
67 QOP_SUB,
68 QOP_SHL,
69 QOP_SHR,
70 QOP_ASR,
71 QOP_MIN,
72 QOP_MAX,
73 QOP_AND,
74 QOP_OR,
75 QOP_XOR,
76 QOP_NOT,
77
78 /* Sets the flag register according to src. */
79 QOP_SF,
80
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,
83 * otherwise 0. */
84 QOP_SEL_X_0_ZS,
85 QOP_SEL_X_0_ZC,
86 QOP_SEL_X_0_NS,
87 QOP_SEL_X_0_NC,
88 /* Selects the src[0] if the ns flag bit is set, otherwise src[1]. */
89 QOP_SEL_X_Y_ZS,
90 QOP_SEL_X_Y_ZC,
91 QOP_SEL_X_Y_NS,
92 QOP_SEL_X_Y_NC,
93
94 QOP_FTOI,
95 QOP_ITOF,
96 QOP_RCP,
97 QOP_RSQ,
98 QOP_EXP2,
99 QOP_LOG2,
100 QOP_VW_SETUP,
101 QOP_VR_SETUP,
102 QOP_PACK_SCALED,
103 QOP_PACK_COLORS,
104 QOP_VPM_READ,
105 QOP_TLB_DISCARD_SETUP,
106 QOP_TLB_STENCIL_SETUP,
107 QOP_TLB_Z_WRITE,
108 QOP_TLB_COLOR_WRITE,
109 QOP_TLB_COLOR_READ,
110 QOP_VARY_ADD_C,
111
112 QOP_FRAG_X,
113 QOP_FRAG_Y,
114 QOP_FRAG_Z,
115 QOP_FRAG_W,
116 QOP_FRAG_REV_FLAG,
117
118 QOP_UNPACK_8A_F,
119 QOP_UNPACK_8B_F,
120 QOP_UNPACK_8C_F,
121 QOP_UNPACK_8D_F,
122 QOP_UNPACK_16A_F,
123 QOP_UNPACK_16B_F,
124
125 QOP_UNPACK_8A_I,
126 QOP_UNPACK_8B_I,
127 QOP_UNPACK_8C_I,
128 QOP_UNPACK_8D_I,
129 QOP_UNPACK_16A_I,
130 QOP_UNPACK_16B_I,
131
132 /** Texture x coordinate parameter write */
133 QOP_TEX_S,
134 /** Texture y coordinate parameter write */
135 QOP_TEX_T,
136 /** Texture border color parameter or cube map z coordinate write */
137 QOP_TEX_R,
138 /** Texture LOD bias parameter write */
139 QOP_TEX_B,
140
141 /**
142 * Texture-unit 4-byte read with address provided direct in S
143 * cooordinate.
144 *
145 * The first operand is the offset from the start of the UBO, and the
146 * second is the uniform that has the UBO's base pointer.
147 */
148 QOP_TEX_DIRECT,
149
150 /**
151 * Signal of texture read being necessary and then reading r4 into
152 * the destination
153 */
154 QOP_TEX_RESULT,
155 QOP_R4_UNPACK_A,
156 QOP_R4_UNPACK_B,
157 QOP_R4_UNPACK_C,
158 QOP_R4_UNPACK_D
159 };
160
161 struct simple_node {
162 struct simple_node *next;
163 struct simple_node *prev;
164 };
165
166 struct queued_qpu_inst {
167 struct simple_node link;
168 uint64_t inst;
169 };
170
171 struct qinst {
172 struct simple_node link;
173
174 enum qop op;
175 struct qreg dst;
176 struct qreg *src;
177 };
178
179 enum qstage {
180 /**
181 * Coordinate shader, runs during binning, before the VS, and just
182 * outputs position.
183 */
184 QSTAGE_COORD,
185 QSTAGE_VERT,
186 QSTAGE_FRAG,
187 };
188
189 enum quniform_contents {
190 /**
191 * Indicates that a constant 32-bit value is copied from the program's
192 * uniform contents.
193 */
194 QUNIFORM_CONSTANT,
195 /**
196 * Indicates that the program's uniform contents are used as an index
197 * into the GL uniform storage.
198 */
199 QUNIFORM_UNIFORM,
200
201 /** @{
202 * Scaling factors from clip coordinates to relative to the viewport
203 * center.
204 *
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.
208 */
209 QUNIFORM_VIEWPORT_X_SCALE,
210 QUNIFORM_VIEWPORT_Y_SCALE,
211 /** @} */
212
213 QUNIFORM_VIEWPORT_Z_OFFSET,
214 QUNIFORM_VIEWPORT_Z_SCALE,
215
216 QUNIFORM_USER_CLIP_PLANE,
217
218 /**
219 * A reference to a texture config parameter 0 uniform.
220 *
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.
224 */
225 QUNIFORM_TEXTURE_CONFIG_P0,
226
227 /**
228 * A reference to a texture config parameter 1 uniform.
229 *
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
233 * sequence.
234 */
235 QUNIFORM_TEXTURE_CONFIG_P1,
236
237 /** A reference to a texture config parameter 2 cubemap stride uniform */
238 QUNIFORM_TEXTURE_CONFIG_P2,
239
240 QUNIFORM_UBO_ADDR,
241
242 QUNIFORM_TEXRECT_SCALE_X,
243 QUNIFORM_TEXRECT_SCALE_Y,
244
245 QUNIFORM_TEXTURE_BORDER_COLOR,
246
247 QUNIFORM_BLEND_CONST_COLOR,
248 QUNIFORM_STENCIL,
249
250 QUNIFORM_ALPHA_REF,
251 };
252
253 struct vc4_varying_semantic {
254 uint8_t semantic;
255 uint8_t index;
256 uint8_t swizzle;
257 };
258
259 struct vc4_compiler_ubo_range {
260 /**
261 * offset in bytes from the start of the ubo where this range is
262 * uploaded.
263 *
264 * Only set once used is set.
265 */
266 uint32_t dst_offset;
267
268 /**
269 * offset in bytes from the start of the gallium uniforms where the
270 * data comes from.
271 */
272 uint32_t src_offset;
273
274 /** size in bytes of this ubo range */
275 uint32_t size;
276
277 /**
278 * Set if this range is used by the shader for indirect uniforms
279 * access.
280 */
281 bool used;
282 };
283
284 struct vc4_compile {
285 struct vc4_context *vc4;
286 struct tgsi_parse_context parser;
287 struct qreg *temps;
288 /**
289 * Inputs to the shader, arranged by TGSI declaration order.
290 *
291 * Not all fragment shader QFILE_VARY reads are present in this array.
292 */
293 struct qreg *inputs;
294 struct qreg *outputs;
295 struct qreg *consts;
296 struct qreg addr[4]; /* TGSI ARL destination. */
297 uint32_t temps_array_size;
298 uint32_t inputs_array_size;
299 uint32_t outputs_array_size;
300 uint32_t uniforms_array_size;
301 uint32_t consts_array_size;
302 uint32_t num_consts;
303
304 struct vc4_compiler_ubo_range *ubo_ranges;
305 uint32_t ubo_ranges_array_size;
306 uint32_t num_ubo_ranges;
307 uint32_t next_ubo_dst_offset;
308
309 struct qreg line_x, point_x, point_y;
310 struct qreg discard;
311
312 /**
313 * Array of the TGSI semantics of all FS QFILE_VARY reads.
314 *
315 * This includes those that aren't part of the VPM varyings, like
316 * point/line coordinates.
317 */
318 struct vc4_varying_semantic *input_semantics;
319 uint32_t num_input_semantics;
320 uint32_t input_semantics_array_size;
321
322 /**
323 * An entry per outputs[] in the VS indicating what the semantic of
324 * the output is. Used to emit from the VS in the order that the FS
325 * needs.
326 */
327 struct vc4_varying_semantic *output_semantics;
328
329 struct pipe_shader_state *shader_state;
330 struct vc4_key *key;
331 struct vc4_fs_key *fs_key;
332 struct vc4_vs_key *vs_key;
333
334 uint32_t *uniform_data;
335 enum quniform_contents *uniform_contents;
336 uint32_t uniform_array_size;
337 uint32_t num_uniforms;
338 uint32_t num_outputs;
339 uint32_t num_texture_samples;
340 uint32_t output_position_index;
341 uint32_t output_clipvertex_index;
342 uint32_t output_color_index;
343 uint32_t output_point_size_index;
344
345 struct qreg undef;
346 enum qstage stage;
347 uint32_t num_temps;
348 struct simple_node instructions;
349 uint32_t immediates[1024];
350
351 struct simple_node qpu_inst_list;
352 uint64_t *qpu_insts;
353 uint32_t qpu_inst_count;
354 uint32_t qpu_inst_size;
355 uint32_t num_inputs;
356
357 uint32_t program_id;
358 uint32_t variant_id;
359 };
360
361 struct vc4_compile *qir_compile_init(void);
362 void qir_compile_destroy(struct vc4_compile *c);
363 struct qinst *qir_inst(enum qop op, struct qreg dst,
364 struct qreg src0, struct qreg src1);
365 struct qinst *qir_inst4(enum qop op, struct qreg dst,
366 struct qreg a,
367 struct qreg b,
368 struct qreg c,
369 struct qreg d);
370 void qir_remove_instruction(struct qinst *qinst);
371 void qir_reorder_uniforms(struct vc4_compile *c);
372 void qir_emit(struct vc4_compile *c, struct qinst *inst);
373 struct qreg qir_get_temp(struct vc4_compile *c);
374 int qir_get_op_nsrc(enum qop qop);
375 bool qir_reg_equals(struct qreg a, struct qreg b);
376 bool qir_has_side_effects(struct vc4_compile *c, struct qinst *inst);
377 bool qir_is_multi_instruction(struct qinst *inst);
378 bool qir_depends_on_flags(struct qinst *inst);
379 bool qir_writes_r4(struct qinst *inst);
380 bool qir_reads_r4(struct qinst *inst);
381 struct qreg qir_follow_movs(struct qinst **defs, struct qreg reg);
382
383 void qir_dump(struct vc4_compile *c);
384 void qir_dump_inst(struct vc4_compile *c, struct qinst *inst);
385 const char *qir_get_stage_name(enum qstage stage);
386
387 void qir_optimize(struct vc4_compile *c);
388 bool qir_opt_algebraic(struct vc4_compile *c);
389 bool qir_opt_copy_propagation(struct vc4_compile *c);
390 bool qir_opt_cse(struct vc4_compile *c);
391 bool qir_opt_dead_code(struct vc4_compile *c);
392 bool qir_opt_small_immediates(struct vc4_compile *c);
393 bool qir_opt_vpm_writes(struct vc4_compile *c);
394
395 void qpu_schedule_instructions(struct vc4_compile *c);
396
397 #define QIR_ALU0(name) \
398 static inline struct qreg \
399 qir_##name(struct vc4_compile *c) \
400 { \
401 struct qreg t = qir_get_temp(c); \
402 qir_emit(c, qir_inst(QOP_##name, t, c->undef, c->undef)); \
403 return t; \
404 }
405
406 #define QIR_ALU1(name) \
407 static inline struct qreg \
408 qir_##name(struct vc4_compile *c, struct qreg a) \
409 { \
410 struct qreg t = qir_get_temp(c); \
411 qir_emit(c, qir_inst(QOP_##name, t, a, c->undef)); \
412 return t; \
413 }
414
415 #define QIR_ALU2(name) \
416 static inline struct qreg \
417 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
418 { \
419 struct qreg t = qir_get_temp(c); \
420 qir_emit(c, qir_inst(QOP_##name, t, a, b)); \
421 return t; \
422 }
423
424 #define QIR_NODST_1(name) \
425 static inline void \
426 qir_##name(struct vc4_compile *c, struct qreg a) \
427 { \
428 qir_emit(c, qir_inst(QOP_##name, c->undef, a, c->undef)); \
429 }
430
431 #define QIR_NODST_2(name) \
432 static inline void \
433 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
434 { \
435 qir_emit(c, qir_inst(QOP_##name, c->undef, a, b)); \
436 }
437
438 QIR_ALU1(MOV)
439 QIR_ALU2(FADD)
440 QIR_ALU2(FSUB)
441 QIR_ALU2(FMUL)
442 QIR_ALU2(MUL24)
443 QIR_NODST_1(SF)
444 QIR_ALU1(SEL_X_0_ZS)
445 QIR_ALU1(SEL_X_0_ZC)
446 QIR_ALU1(SEL_X_0_NS)
447 QIR_ALU1(SEL_X_0_NC)
448 QIR_ALU2(SEL_X_Y_ZS)
449 QIR_ALU2(SEL_X_Y_ZC)
450 QIR_ALU2(SEL_X_Y_NS)
451 QIR_ALU2(SEL_X_Y_NC)
452 QIR_ALU2(FMIN)
453 QIR_ALU2(FMAX)
454 QIR_ALU2(FMINABS)
455 QIR_ALU2(FMAXABS)
456 QIR_ALU1(FTOI)
457 QIR_ALU1(ITOF)
458
459 QIR_ALU2(ADD)
460 QIR_ALU2(SUB)
461 QIR_ALU2(SHL)
462 QIR_ALU2(SHR)
463 QIR_ALU2(ASR)
464 QIR_ALU2(MIN)
465 QIR_ALU2(MAX)
466 QIR_ALU2(AND)
467 QIR_ALU2(OR)
468 QIR_ALU2(XOR)
469 QIR_ALU1(NOT)
470
471 QIR_ALU1(RCP)
472 QIR_ALU1(RSQ)
473 QIR_ALU1(EXP2)
474 QIR_ALU1(LOG2)
475 QIR_ALU2(PACK_SCALED)
476 QIR_ALU1(VARY_ADD_C)
477 QIR_NODST_2(TEX_S)
478 QIR_NODST_2(TEX_T)
479 QIR_NODST_2(TEX_R)
480 QIR_NODST_2(TEX_B)
481 QIR_NODST_2(TEX_DIRECT)
482 QIR_ALU0(FRAG_X)
483 QIR_ALU0(FRAG_Y)
484 QIR_ALU0(FRAG_Z)
485 QIR_ALU0(FRAG_W)
486 QIR_ALU0(FRAG_REV_FLAG)
487 QIR_ALU0(TEX_RESULT)
488 QIR_ALU0(TLB_COLOR_READ)
489 QIR_NODST_1(TLB_Z_WRITE)
490 QIR_NODST_1(TLB_DISCARD_SETUP)
491 QIR_NODST_1(TLB_STENCIL_SETUP)
492
493 static inline struct qreg
494 qir_R4_UNPACK(struct vc4_compile *c, struct qreg r4, int i)
495 {
496 struct qreg t = qir_get_temp(c);
497 qir_emit(c, qir_inst(QOP_R4_UNPACK_A + i, t, r4, c->undef));
498 return t;
499 }
500
501 static inline struct qreg
502 qir_SEL_X_0_COND(struct vc4_compile *c, int i)
503 {
504 struct qreg t = qir_get_temp(c);
505 qir_emit(c, qir_inst(QOP_R4_UNPACK_A + i, t, c->undef, c->undef));
506 return t;
507 }
508
509 static inline struct qreg
510 qir_UNPACK_8_F(struct vc4_compile *c, struct qreg src, int i)
511 {
512 struct qreg t = qir_get_temp(c);
513 qir_emit(c, qir_inst(QOP_UNPACK_8A_F + i, t, src, c->undef));
514 return t;
515 }
516
517 static inline struct qreg
518 qir_UNPACK_8_I(struct vc4_compile *c, struct qreg src, int i)
519 {
520 struct qreg t = qir_get_temp(c);
521 qir_emit(c, qir_inst(QOP_UNPACK_8A_I + i, t, src, c->undef));
522 return t;
523 }
524
525 static inline struct qreg
526 qir_UNPACK_16_F(struct vc4_compile *c, struct qreg src, int i)
527 {
528 struct qreg t = qir_get_temp(c);
529 qir_emit(c, qir_inst(QOP_UNPACK_16A_F + i, t, src, c->undef));
530 return t;
531 }
532
533 static inline struct qreg
534 qir_UNPACK_16_I(struct vc4_compile *c, struct qreg src, int i)
535 {
536 struct qreg t = qir_get_temp(c);
537 qir_emit(c, qir_inst(QOP_UNPACK_16A_I + i, t, src, c->undef));
538 return t;
539 }
540
541 static inline struct qreg
542 qir_POW(struct vc4_compile *c, struct qreg x, struct qreg y)
543 {
544 return qir_EXP2(c, qir_FMUL(c,
545 y,
546 qir_LOG2(c, x)));
547 }
548
549 static inline void
550 qir_VPM_WRITE(struct vc4_compile *c, struct qreg val)
551 {
552 static const struct qreg vpm = { QFILE_VPM, 0 };
553 qir_emit(c, qir_inst(QOP_MOV, vpm, val, c->undef));
554 }
555
556 #endif /* VC4_QIR_H */