0e4b78c02b6f4f645f230bcb74f0164e4f7e27a9
[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 };
42
43 struct qreg {
44 enum qfile file;
45 uint32_t index;
46 };
47
48 enum qop {
49 QOP_UNDEF,
50 QOP_MOV,
51 QOP_FADD,
52 QOP_FSUB,
53 QOP_FMUL,
54 QOP_MUL24,
55 QOP_FMIN,
56 QOP_FMAX,
57 QOP_FMINABS,
58 QOP_FMAXABS,
59 QOP_ADD,
60 QOP_SUB,
61 QOP_SHL,
62 QOP_SHR,
63 QOP_ASR,
64 QOP_MIN,
65 QOP_MAX,
66 QOP_AND,
67 QOP_OR,
68 QOP_XOR,
69 QOP_NOT,
70
71 /* Sets the flag register according to src. */
72 QOP_SF,
73
74 /* Note: Orderings of these compares must be the same as in
75 * qpu_defines.h. Selects the src[0] if the ns flag bit is set,
76 * otherwise 0. */
77 QOP_SEL_X_0_ZS,
78 QOP_SEL_X_0_ZC,
79 QOP_SEL_X_0_NS,
80 QOP_SEL_X_0_NC,
81 /* Selects the src[0] if the ns flag bit is set, otherwise src[1]. */
82 QOP_SEL_X_Y_ZS,
83 QOP_SEL_X_Y_ZC,
84 QOP_SEL_X_Y_NS,
85 QOP_SEL_X_Y_NC,
86
87 QOP_FTOI,
88 QOP_ITOF,
89 QOP_RCP,
90 QOP_RSQ,
91 QOP_EXP2,
92 QOP_LOG2,
93 QOP_VW_SETUP,
94 QOP_VR_SETUP,
95 QOP_PACK_SCALED,
96 QOP_PACK_COLORS,
97 QOP_VPM_WRITE,
98 QOP_VPM_READ,
99 QOP_TLB_DISCARD_SETUP,
100 QOP_TLB_STENCIL_SETUP,
101 QOP_TLB_Z_WRITE,
102 QOP_TLB_COLOR_WRITE,
103 QOP_TLB_COLOR_READ,
104 QOP_VARY_ADD_C,
105
106 QOP_FRAG_X,
107 QOP_FRAG_Y,
108 QOP_FRAG_Z,
109 QOP_FRAG_W,
110 QOP_FRAG_REV_FLAG,
111
112 QOP_UNPACK_8A,
113 QOP_UNPACK_8B,
114 QOP_UNPACK_8C,
115 QOP_UNPACK_8D,
116
117 /** Texture x coordinate parameter write */
118 QOP_TEX_S,
119 /** Texture y coordinate parameter write */
120 QOP_TEX_T,
121 /** Texture border color parameter or cube map z coordinate write */
122 QOP_TEX_R,
123 /** Texture LOD bias parameter write */
124 QOP_TEX_B,
125 /**
126 * Signal of texture read being necessary and then reading r4 into
127 * the destination
128 */
129 QOP_TEX_RESULT,
130 QOP_R4_UNPACK_A,
131 QOP_R4_UNPACK_B,
132 QOP_R4_UNPACK_C,
133 QOP_R4_UNPACK_D
134 };
135
136 struct simple_node {
137 struct simple_node *next;
138 struct simple_node *prev;
139 };
140
141 struct qinst {
142 struct simple_node link;
143
144 enum qop op;
145 struct qreg dst;
146 struct qreg *src;
147 };
148
149 enum qstage {
150 /**
151 * Coordinate shader, runs during binning, before the VS, and just
152 * outputs position.
153 */
154 QSTAGE_COORD,
155 QSTAGE_VERT,
156 QSTAGE_FRAG,
157 };
158
159 enum quniform_contents {
160 /**
161 * Indicates that a constant 32-bit value is copied from the program's
162 * uniform contents.
163 */
164 QUNIFORM_CONSTANT,
165 /**
166 * Indicates that the program's uniform contents are used as an index
167 * into the GL uniform storage.
168 */
169 QUNIFORM_UNIFORM,
170
171 /** @{
172 * Scaling factors from clip coordinates to relative to the viewport
173 * center.
174 *
175 * This is used by the coordinate and vertex shaders to produce the
176 * 32-bit entry consisting of 2 16-bit fields with 12.4 signed fixed
177 * point offsets from the viewport ccenter.
178 */
179 QUNIFORM_VIEWPORT_X_SCALE,
180 QUNIFORM_VIEWPORT_Y_SCALE,
181 /** @} */
182
183 QUNIFORM_VIEWPORT_Z_OFFSET,
184 QUNIFORM_VIEWPORT_Z_SCALE,
185
186 /**
187 * A reference to a texture config parameter 0 uniform.
188 *
189 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
190 * defines texture type, miplevels, and such. It will be found as a
191 * parameter to the first QOP_TEX_[STRB] instruction in a sequence.
192 */
193 QUNIFORM_TEXTURE_CONFIG_P0,
194
195 /**
196 * A reference to a texture config parameter 1 uniform.
197 *
198 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
199 * defines texture width, height, filters, and wrap modes. It will be
200 * found as a parameter to the second QOP_TEX_[STRB] instruction in a
201 * sequence.
202 */
203 QUNIFORM_TEXTURE_CONFIG_P1,
204
205 /** A reference to a texture config parameter 2 cubemap stride uniform */
206 QUNIFORM_TEXTURE_CONFIG_P2,
207
208 QUNIFORM_TEXRECT_SCALE_X,
209 QUNIFORM_TEXRECT_SCALE_Y,
210
211 QUNIFORM_TEXTURE_BORDER_COLOR,
212
213 QUNIFORM_BLEND_CONST_COLOR,
214 QUNIFORM_STENCIL,
215
216 QUNIFORM_ALPHA_REF,
217 };
218
219 struct vc4_varying_semantic {
220 uint8_t semantic;
221 uint8_t index;
222 uint8_t swizzle;
223 };
224
225 struct vc4_compile {
226 struct vc4_context *vc4;
227 struct tgsi_parse_context parser;
228 struct qreg *temps;
229 /**
230 * Inputs to the shader, arranged by TGSI declaration order.
231 *
232 * Not all fragment shader QFILE_VARY reads are present in this array.
233 */
234 struct qreg *inputs;
235 struct qreg *outputs;
236 struct qreg *consts;
237 uint32_t temps_array_size;
238 uint32_t inputs_array_size;
239 uint32_t outputs_array_size;
240 uint32_t uniforms_array_size;
241 uint32_t consts_array_size;
242 uint32_t num_consts;
243 struct qreg line_x, point_x, point_y;
244 struct qreg discard;
245
246 /**
247 * Array of the TGSI semantics of all FS QFILE_VARY reads.
248 *
249 * This includes those that aren't part of the VPM varyings, like
250 * point/line coordinates.
251 */
252 struct vc4_varying_semantic *input_semantics;
253 uint32_t num_input_semantics;
254 uint32_t input_semantics_array_size;
255
256 /**
257 * An entry per outputs[] in the VS indicating what the semantic of
258 * the output is. Used to emit from the VS in the order that the FS
259 * needs.
260 */
261 struct vc4_varying_semantic *output_semantics;
262
263 struct pipe_shader_state *shader_state;
264 struct vc4_key *key;
265 struct vc4_fs_key *fs_key;
266 struct vc4_vs_key *vs_key;
267
268 uint32_t *uniform_data;
269 enum quniform_contents *uniform_contents;
270 uint32_t uniform_array_size;
271 uint32_t num_uniforms;
272 uint32_t num_outputs;
273 uint32_t num_texture_samples;
274 uint32_t output_position_index;
275 uint32_t output_color_index;
276 uint32_t output_point_size_index;
277
278 struct qreg undef;
279 enum qstage stage;
280 uint32_t num_temps;
281 struct simple_node instructions;
282 uint32_t immediates[1024];
283
284 struct simple_node qpu_inst_list;
285 uint64_t *qpu_insts;
286 uint32_t qpu_inst_count;
287 uint32_t qpu_inst_size;
288 uint32_t num_inputs;
289 };
290
291 struct vc4_compile *qir_compile_init(void);
292 void qir_compile_destroy(struct vc4_compile *c);
293 struct qinst *qir_inst(enum qop op, struct qreg dst,
294 struct qreg src0, struct qreg src1);
295 struct qinst *qir_inst4(enum qop op, struct qreg dst,
296 struct qreg a,
297 struct qreg b,
298 struct qreg c,
299 struct qreg d);
300 void qir_remove_instruction(struct qinst *qinst);
301 void qir_reorder_uniforms(struct vc4_compile *c);
302 void qir_emit(struct vc4_compile *c, struct qinst *inst);
303 struct qreg qir_get_temp(struct vc4_compile *c);
304 int qir_get_op_nsrc(enum qop qop);
305 bool qir_reg_equals(struct qreg a, struct qreg b);
306 bool qir_has_side_effects(struct qinst *inst);
307 bool qir_depends_on_flags(struct qinst *inst);
308 bool qir_writes_r4(struct qinst *inst);
309 bool qir_reads_r4(struct qinst *inst);
310
311 void qir_dump(struct vc4_compile *c);
312 void qir_dump_inst(struct vc4_compile *c, struct qinst *inst);
313 const char *qir_get_stage_name(enum qstage stage);
314
315 void qir_optimize(struct vc4_compile *c);
316 bool qir_opt_algebraic(struct vc4_compile *c);
317 bool qir_opt_copy_propagation(struct vc4_compile *c);
318 bool qir_opt_cse(struct vc4_compile *c);
319 bool qir_opt_dead_code(struct vc4_compile *c);
320
321 #define QIR_ALU0(name) \
322 static inline struct qreg \
323 qir_##name(struct vc4_compile *c) \
324 { \
325 struct qreg t = qir_get_temp(c); \
326 qir_emit(c, qir_inst(QOP_##name, t, c->undef, c->undef)); \
327 return t; \
328 }
329
330 #define QIR_ALU1(name) \
331 static inline struct qreg \
332 qir_##name(struct vc4_compile *c, struct qreg a) \
333 { \
334 struct qreg t = qir_get_temp(c); \
335 qir_emit(c, qir_inst(QOP_##name, t, a, c->undef)); \
336 return t; \
337 }
338
339 #define QIR_ALU2(name) \
340 static inline struct qreg \
341 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
342 { \
343 struct qreg t = qir_get_temp(c); \
344 qir_emit(c, qir_inst(QOP_##name, t, a, b)); \
345 return t; \
346 }
347
348 #define QIR_NODST_1(name) \
349 static inline void \
350 qir_##name(struct vc4_compile *c, struct qreg a) \
351 { \
352 qir_emit(c, qir_inst(QOP_##name, c->undef, a, c->undef)); \
353 }
354
355 #define QIR_NODST_2(name) \
356 static inline void \
357 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
358 { \
359 qir_emit(c, qir_inst(QOP_##name, c->undef, a, b)); \
360 }
361
362 QIR_ALU1(MOV)
363 QIR_ALU2(FADD)
364 QIR_ALU2(FSUB)
365 QIR_ALU2(FMUL)
366 QIR_ALU2(MUL24)
367 QIR_NODST_1(SF)
368 QIR_ALU1(SEL_X_0_ZS)
369 QIR_ALU1(SEL_X_0_ZC)
370 QIR_ALU1(SEL_X_0_NS)
371 QIR_ALU1(SEL_X_0_NC)
372 QIR_ALU2(SEL_X_Y_ZS)
373 QIR_ALU2(SEL_X_Y_ZC)
374 QIR_ALU2(SEL_X_Y_NS)
375 QIR_ALU2(SEL_X_Y_NC)
376 QIR_ALU2(FMIN)
377 QIR_ALU2(FMAX)
378 QIR_ALU2(FMINABS)
379 QIR_ALU2(FMAXABS)
380 QIR_ALU1(FTOI)
381 QIR_ALU1(ITOF)
382
383 QIR_ALU2(ADD)
384 QIR_ALU2(SUB)
385 QIR_ALU2(SHL)
386 QIR_ALU2(SHR)
387 QIR_ALU2(ASR)
388 QIR_ALU2(MIN)
389 QIR_ALU2(MAX)
390 QIR_ALU2(AND)
391 QIR_ALU2(OR)
392 QIR_ALU2(XOR)
393 QIR_ALU1(NOT)
394
395 QIR_ALU1(RCP)
396 QIR_ALU1(RSQ)
397 QIR_ALU1(EXP2)
398 QIR_ALU1(LOG2)
399 QIR_ALU2(PACK_SCALED)
400 QIR_ALU1(VARY_ADD_C)
401 QIR_NODST_1(VPM_WRITE)
402 QIR_NODST_2(TEX_S)
403 QIR_NODST_2(TEX_T)
404 QIR_NODST_2(TEX_R)
405 QIR_NODST_2(TEX_B)
406 QIR_ALU0(FRAG_X)
407 QIR_ALU0(FRAG_Y)
408 QIR_ALU0(FRAG_Z)
409 QIR_ALU0(FRAG_W)
410 QIR_ALU0(FRAG_REV_FLAG)
411 QIR_ALU0(TEX_RESULT)
412 QIR_ALU0(TLB_COLOR_READ)
413 QIR_NODST_1(TLB_Z_WRITE)
414 QIR_NODST_1(TLB_DISCARD_SETUP)
415 QIR_NODST_1(TLB_STENCIL_SETUP)
416
417 static inline struct qreg
418 qir_R4_UNPACK(struct vc4_compile *c, struct qreg r4, int i)
419 {
420 struct qreg t = qir_get_temp(c);
421 qir_emit(c, qir_inst(QOP_R4_UNPACK_A + i, t, r4, c->undef));
422 return t;
423 }
424
425 static inline struct qreg
426 qir_SEL_X_0_COND(struct vc4_compile *c, int i)
427 {
428 struct qreg t = qir_get_temp(c);
429 qir_emit(c, qir_inst(QOP_R4_UNPACK_A + i, t, c->undef, c->undef));
430 return t;
431 }
432
433 static inline struct qreg
434 qir_UNPACK_8(struct vc4_compile *c, struct qreg src, int i)
435 {
436 struct qreg t = qir_get_temp(c);
437 qir_emit(c, qir_inst(QOP_UNPACK_8A + i, t, src, c->undef));
438 return t;
439 }
440
441 static inline struct qreg
442 qir_POW(struct vc4_compile *c, struct qreg x, struct qreg y)
443 {
444 return qir_EXP2(c, qir_FMUL(c,
445 y,
446 qir_LOG2(c, x)));
447 }
448
449 #endif /* VC4_QIR_H */