vc4: Drop pointless raddr conflict handling on SF.
[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_PASSTHROUGH_Z_WRITE,
101 QOP_TLB_COLOR_WRITE,
102 QOP_TLB_COLOR_READ,
103 QOP_VARY_ADD_C,
104
105 QOP_FRAG_X,
106 QOP_FRAG_Y,
107 QOP_FRAG_Z,
108 QOP_FRAG_RCP_W,
109
110 /** Texture x coordinate parameter write */
111 QOP_TEX_S,
112 /** Texture y coordinate parameter write */
113 QOP_TEX_T,
114 /** Texture border color parameter or cube map z coordinate write */
115 QOP_TEX_R,
116 /** Texture LOD bias parameter write */
117 QOP_TEX_B,
118 /**
119 * Signal of texture read being necessary and then reading r4 into
120 * the destination
121 */
122 QOP_TEX_RESULT,
123 QOP_R4_UNPACK_A,
124 QOP_R4_UNPACK_B,
125 QOP_R4_UNPACK_C,
126 QOP_R4_UNPACK_D
127 };
128
129 struct simple_node {
130 struct simple_node *next;
131 struct simple_node *prev;
132 };
133
134 struct qinst {
135 struct simple_node link;
136
137 enum qop op;
138 struct qreg dst;
139 struct qreg *src;
140 };
141
142 enum qstage {
143 /**
144 * Coordinate shader, runs during binning, before the VS, and just
145 * outputs position.
146 */
147 QSTAGE_COORD,
148 QSTAGE_VERT,
149 QSTAGE_FRAG,
150 };
151
152 enum quniform_contents {
153 /**
154 * Indicates that a constant 32-bit value is copied from the program's
155 * uniform contents.
156 */
157 QUNIFORM_CONSTANT,
158 /**
159 * Indicates that the program's uniform contents are used as an index
160 * into the GL uniform storage.
161 */
162 QUNIFORM_UNIFORM,
163
164 /** @{
165 * Scaling factors from clip coordinates to relative to the viewport
166 * center.
167 *
168 * This is used by the coordinate and vertex shaders to produce the
169 * 32-bit entry consisting of 2 16-bit fields with 12.4 signed fixed
170 * point offsets from the viewport ccenter.
171 */
172 QUNIFORM_VIEWPORT_X_SCALE,
173 QUNIFORM_VIEWPORT_Y_SCALE,
174 /** @} */
175
176 QUNIFORM_VIEWPORT_Z_OFFSET,
177 QUNIFORM_VIEWPORT_Z_SCALE,
178
179 /**
180 * A reference to a texture config parameter 0 uniform.
181 *
182 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
183 * defines texture type, miplevels, and such. It will be found as a
184 * parameter to the first QOP_TEX_[STRB] instruction in a sequence.
185 */
186 QUNIFORM_TEXTURE_CONFIG_P0,
187
188 /**
189 * A reference to a texture config parameter 1 uniform.
190 *
191 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
192 * defines texture width, height, filters, and wrap modes. It will be
193 * found as a parameter to the second QOP_TEX_[STRB] instruction in a
194 * sequence.
195 */
196 QUNIFORM_TEXTURE_CONFIG_P1,
197
198 QUNIFORM_TEXRECT_SCALE_X,
199 QUNIFORM_TEXRECT_SCALE_Y,
200
201 QUNIFORM_BLEND_CONST_COLOR,
202 };
203
204 struct vc4_compile {
205 struct tgsi_parse_context parser;
206 struct qreg *temps;
207 struct qreg *inputs;
208 struct qreg *outputs;
209 struct qreg *uniforms;
210 struct qreg *consts;
211 uint32_t num_consts;
212 struct qreg line_x, point_x, point_y;
213 struct qreg discard;
214
215 struct pipe_shader_state *shader_state;
216 struct vc4_key *key;
217 struct vc4_fs_key *fs_key;
218 struct vc4_vs_key *vs_key;
219
220 uint32_t *uniform_data;
221 enum quniform_contents *uniform_contents;
222 uint32_t num_uniforms;
223 uint32_t num_outputs;
224 uint32_t num_texture_samples;
225
226 struct qreg undef;
227 enum qstage stage;
228 uint32_t num_temps;
229 struct simple_node instructions;
230 uint32_t immediates[1024];
231
232 struct simple_node qpu_inst_list;
233 uint64_t *qpu_insts;
234 uint32_t qpu_inst_count;
235 uint32_t qpu_inst_size;
236 uint32_t num_inputs;
237 };
238
239 struct vc4_compile *qir_compile_init(void);
240 void qir_compile_destroy(struct vc4_compile *c);
241 struct qinst *qir_inst(enum qop op, struct qreg dst,
242 struct qreg src0, struct qreg src1);
243 struct qinst *qir_inst4(enum qop op, struct qreg dst,
244 struct qreg a,
245 struct qreg b,
246 struct qreg c,
247 struct qreg d);
248 void qir_emit(struct vc4_compile *c, struct qinst *inst);
249 struct qreg qir_get_temp(struct vc4_compile *c);
250 int qir_get_op_nsrc(enum qop qop);
251 bool qir_reg_equals(struct qreg a, struct qreg b);
252 bool qir_has_side_effects(struct qinst *inst);
253 bool qir_depends_on_flags(struct qinst *inst);
254 bool qir_writes_r4(struct qinst *inst);
255 bool qir_reads_r4(struct qinst *inst);
256
257 void qir_dump(struct vc4_compile *c);
258 void qir_dump_inst(struct qinst *inst);
259 const char *qir_get_stage_name(enum qstage stage);
260
261 void qir_optimize(struct vc4_compile *c);
262 bool qir_opt_algebraic(struct vc4_compile *c);
263 bool qir_opt_copy_propagation(struct vc4_compile *c);
264 bool qir_opt_cse(struct vc4_compile *c);
265 bool qir_opt_dead_code(struct vc4_compile *c);
266
267 #define QIR_ALU0(name) \
268 static inline struct qreg \
269 qir_##name(struct vc4_compile *c) \
270 { \
271 struct qreg t = qir_get_temp(c); \
272 qir_emit(c, qir_inst(QOP_##name, t, c->undef, c->undef)); \
273 return t; \
274 }
275
276 #define QIR_ALU1(name) \
277 static inline struct qreg \
278 qir_##name(struct vc4_compile *c, struct qreg a) \
279 { \
280 struct qreg t = qir_get_temp(c); \
281 qir_emit(c, qir_inst(QOP_##name, t, a, c->undef)); \
282 return t; \
283 }
284
285 #define QIR_ALU2(name) \
286 static inline struct qreg \
287 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
288 { \
289 struct qreg t = qir_get_temp(c); \
290 qir_emit(c, qir_inst(QOP_##name, t, a, b)); \
291 return t; \
292 }
293
294 #define QIR_NODST_1(name) \
295 static inline void \
296 qir_##name(struct vc4_compile *c, struct qreg a) \
297 { \
298 qir_emit(c, qir_inst(QOP_##name, c->undef, a, c->undef)); \
299 }
300
301 #define QIR_NODST_2(name) \
302 static inline void \
303 qir_##name(struct vc4_compile *c, struct qreg a, struct qreg b) \
304 { \
305 qir_emit(c, qir_inst(QOP_##name, c->undef, a, b)); \
306 }
307
308 QIR_ALU1(MOV)
309 QIR_ALU2(FADD)
310 QIR_ALU2(FSUB)
311 QIR_ALU2(FMUL)
312 QIR_ALU2(MUL24)
313 QIR_NODST_1(SF)
314 QIR_ALU1(SEL_X_0_ZS)
315 QIR_ALU1(SEL_X_0_ZC)
316 QIR_ALU1(SEL_X_0_NS)
317 QIR_ALU1(SEL_X_0_NC)
318 QIR_ALU2(SEL_X_Y_ZS)
319 QIR_ALU2(SEL_X_Y_ZC)
320 QIR_ALU2(SEL_X_Y_NS)
321 QIR_ALU2(SEL_X_Y_NC)
322 QIR_ALU2(FMIN)
323 QIR_ALU2(FMAX)
324 QIR_ALU2(FMINABS)
325 QIR_ALU2(FMAXABS)
326 QIR_ALU1(FTOI)
327 QIR_ALU1(ITOF)
328
329 QIR_ALU2(ADD)
330 QIR_ALU2(SUB)
331 QIR_ALU2(SHL)
332 QIR_ALU2(SHR)
333 QIR_ALU2(ASR)
334 QIR_ALU2(MIN)
335 QIR_ALU2(MAX)
336 QIR_ALU2(AND)
337 QIR_ALU2(OR)
338 QIR_ALU2(XOR)
339 QIR_ALU1(NOT)
340
341 QIR_ALU1(RCP)
342 QIR_ALU1(RSQ)
343 QIR_ALU1(EXP2)
344 QIR_ALU1(LOG2)
345 QIR_ALU2(PACK_SCALED)
346 QIR_ALU1(VARY_ADD_C)
347 QIR_NODST_1(VPM_WRITE)
348 QIR_NODST_2(TEX_S)
349 QIR_NODST_2(TEX_T)
350 QIR_NODST_2(TEX_R)
351 QIR_NODST_2(TEX_B)
352 QIR_ALU0(FRAG_X)
353 QIR_ALU0(FRAG_Y)
354 QIR_ALU0(FRAG_Z)
355 QIR_ALU0(FRAG_RCP_W)
356 QIR_NODST_1(TLB_DISCARD_SETUP)
357
358 static inline struct qreg
359 qir_R4_UNPACK(struct vc4_compile *c, int i)
360 {
361 struct qreg t = qir_get_temp(c);
362 qir_emit(c, qir_inst(QOP_R4_UNPACK_A + i, t, c->undef, c->undef));
363 return t;
364 }
365
366 static inline struct qreg
367 qir_SEL_X_0_COND(struct vc4_compile *c, int i)
368 {
369 struct qreg t = qir_get_temp(c);
370 qir_emit(c, qir_inst(QOP_R4_UNPACK_A + i, t, c->undef, c->undef));
371 return t;
372 }
373
374 #endif /* VC4_QIR_H */