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