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