b578e7e0b9ec3f6d55a4f5b8f6e00dc08bcb1d83
[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 <stdbool.h>
28 #include <stdint.h>
29
30 #include "util/u_simple_list.h"
31
32 enum qfile {
33 QFILE_NULL,
34 QFILE_TEMP,
35 QFILE_VARY,
36 QFILE_UNIF,
37 };
38
39 struct qreg {
40 enum qfile file;
41 uint32_t index;
42 };
43
44 enum qop {
45 QOP_UNDEF,
46 QOP_MOV,
47 QOP_FADD,
48 QOP_FSUB,
49 QOP_FMUL,
50 QOP_FMIN,
51 QOP_FMAX,
52 QOP_FMINABS,
53 QOP_FMAXABS,
54
55 QOP_SEQ,
56 QOP_SNE,
57 QOP_SGE,
58 QOP_SLT,
59 QOP_CMP,
60
61 QOP_FTOI,
62 QOP_ITOF,
63 QOP_RCP,
64 QOP_RSQ,
65 QOP_EXP2,
66 QOP_LOG2,
67 QOP_VW_SETUP,
68 QOP_VR_SETUP,
69 QOP_PACK_SCALED,
70 QOP_PACK_COLORS,
71 QOP_VPM_WRITE,
72 QOP_VPM_READ,
73 QOP_TLB_PASSTHROUGH_Z_WRITE,
74 QOP_TLB_COLOR_WRITE,
75 QOP_VARY_ADD_C,
76
77 /** Texture x coordinate parameter write */
78 QOP_TEX_S,
79 /** Texture y coordinate parameter write */
80 QOP_TEX_T,
81 /** Texture border color parameter or cube map z coordinate write */
82 QOP_TEX_R,
83 /** Texture LOD bias parameter write */
84 QOP_TEX_B,
85 /**
86 * Signal of texture read being necessary and then reading r4 into
87 * the destination
88 */
89 QOP_TEX_RESULT,
90 QOP_R4_UNPACK_A,
91 QOP_R4_UNPACK_B,
92 QOP_R4_UNPACK_C,
93 QOP_R4_UNPACK_D
94 };
95
96 struct simple_node {
97 struct simple_node *next;
98 struct simple_node *prev;
99 };
100
101 struct qinst {
102 struct simple_node link;
103
104 enum qop op;
105 struct qreg dst;
106 struct qreg *src;
107 };
108
109 enum qstage {
110 /**
111 * Coordinate shader, runs during binning, before the VS, and just
112 * outputs position.
113 */
114 QSTAGE_COORD,
115 QSTAGE_VERT,
116 QSTAGE_FRAG,
117 };
118
119 enum quniform_contents {
120 /**
121 * Indicates that a constant 32-bit value is copied from the program's
122 * uniform contents.
123 */
124 QUNIFORM_CONSTANT,
125 /**
126 * Indicates that the program's uniform contents are used as an index
127 * into the GL uniform storage.
128 */
129 QUNIFORM_UNIFORM,
130
131 /** @{
132 * Scaling factors from clip coordinates to relative to the viewport
133 * center.
134 *
135 * This is used by the coordinate and vertex shaders to produce the
136 * 32-bit entry consisting of 2 16-bit fields with 12.4 signed fixed
137 * point offsets from the viewport ccenter.
138 */
139 QUNIFORM_VIEWPORT_X_SCALE,
140 QUNIFORM_VIEWPORT_Y_SCALE,
141 /** @} */
142
143 /**
144 * A reference to a texture config parameter 0 uniform.
145 *
146 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
147 * defines texture type, miplevels, and such. It will be found as a
148 * parameter to the first QOP_TEX_[STRB] instruction in a sequence.
149 */
150 QUNIFORM_TEXTURE_CONFIG_P0,
151
152 /**
153 * A reference to a texture config parameter 1 uniform.
154 *
155 * This is a uniform implicitly loaded with a QPU_W_TMU* write, which
156 * defines texture width, height, filters, and wrap modes. It will be
157 * found as a parameter to the second QOP_TEX_[STRB] instruction in a
158 * sequence.
159 */
160 QUNIFORM_TEXTURE_CONFIG_P1,
161
162 QUNIFORM_TEXRECT_SCALE_X,
163 QUNIFORM_TEXRECT_SCALE_Y,
164 };
165
166 struct qcompile {
167 struct qreg undef;
168 enum qstage stage;
169 uint32_t num_temps;
170 struct simple_node instructions;
171 uint32_t immediates[1024];
172
173 struct simple_node qpu_inst_list;
174 uint64_t *qpu_insts;
175 uint32_t qpu_inst_count;
176 uint32_t qpu_inst_size;
177 uint32_t num_inputs;
178 };
179
180 struct qcompile *qir_compile_init(void);
181 void qir_compile_destroy(struct qcompile *c);
182 struct qinst *qir_inst(enum qop op, struct qreg dst,
183 struct qreg src0, struct qreg src1);
184 struct qinst *qir_inst4(enum qop op, struct qreg dst,
185 struct qreg a,
186 struct qreg b,
187 struct qreg c,
188 struct qreg d);
189 void qir_emit(struct qcompile *c, struct qinst *inst);
190 struct qreg qir_get_temp(struct qcompile *c);
191 int qir_get_op_nsrc(enum qop qop);
192 bool qir_reg_equals(struct qreg a, struct qreg b);
193 bool qir_has_side_effects(struct qinst *inst);
194
195 void qir_dump(struct qcompile *c);
196 void qir_dump_inst(struct qinst *inst);
197 const char *qir_get_stage_name(enum qstage stage);
198
199 void qir_optimize(struct qcompile *c);
200 bool qir_opt_algebraic(struct qcompile *c);
201 bool qir_opt_copy_propagation(struct qcompile *c);
202 bool qir_opt_dead_code(struct qcompile *c);
203
204 #define QIR_ALU1(name) \
205 static inline struct qreg \
206 qir_##name(struct qcompile *c, struct qreg a) \
207 { \
208 struct qreg t = qir_get_temp(c); \
209 qir_emit(c, qir_inst(QOP_##name, t, a, c->undef)); \
210 return t; \
211 }
212
213 #define QIR_ALU2(name) \
214 static inline struct qreg \
215 qir_##name(struct qcompile *c, struct qreg a, struct qreg b) \
216 { \
217 struct qreg t = qir_get_temp(c); \
218 qir_emit(c, qir_inst(QOP_##name, t, a, b)); \
219 return t; \
220 }
221
222 #define QIR_NODST_1(name) \
223 static inline void \
224 qir_##name(struct qcompile *c, struct qreg a) \
225 { \
226 qir_emit(c, qir_inst(QOP_##name, c->undef, a, c->undef)); \
227 }
228
229 #define QIR_NODST_2(name) \
230 static inline void \
231 qir_##name(struct qcompile *c, struct qreg a, struct qreg b) \
232 { \
233 qir_emit(c, qir_inst(QOP_##name, c->undef, a, b)); \
234 }
235
236 QIR_ALU1(MOV)
237 QIR_ALU2(FADD)
238 QIR_ALU2(FSUB)
239 QIR_ALU2(FMUL)
240 QIR_ALU2(FMIN)
241 QIR_ALU2(FMAX)
242 QIR_ALU2(FMINABS)
243 QIR_ALU2(FMAXABS)
244 QIR_ALU1(FTOI)
245 QIR_ALU1(ITOF)
246 QIR_ALU1(RCP)
247 QIR_ALU1(RSQ)
248 QIR_ALU1(EXP2)
249 QIR_ALU1(LOG2)
250 QIR_ALU2(PACK_SCALED)
251 QIR_ALU1(VARY_ADD_C)
252 QIR_NODST_1(VPM_WRITE)
253 QIR_NODST_2(TEX_S)
254 QIR_NODST_2(TEX_T)
255 QIR_NODST_2(TEX_R)
256 QIR_NODST_2(TEX_B)
257
258 static inline struct qreg
259 qir_CMP(struct qcompile *c, struct qreg cmp, struct qreg a, struct qreg b)
260 {
261 struct qreg t = qir_get_temp(c);
262 qir_emit(c, qir_inst4(QOP_CMP, t, cmp, a, b, c->undef));
263 return t;
264 }
265
266 #endif /* VC4_QIR_H */