freedreno: use tgsi_lowering
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_cmdline.c
1 /*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/mman.h>
30 #include <fcntl.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <err.h>
34
35 #include "tgsi/tgsi_parse.h"
36 #include "tgsi/tgsi_text.h"
37 #include "tgsi/tgsi_dump.h"
38
39 #include "freedreno_util.h"
40
41 #include "ir3_compiler.h"
42 #include "instr-a3xx.h"
43 #include "ir3.h"
44
45 static void dump_info(struct ir3_shader_variant *so)
46 {
47 struct ir3_info info;
48 uint32_t *bin;
49 const char *type = (so->type == SHADER_VERTEX) ? "VERT" : "FRAG";
50
51 // for debug, dump some before/after info:
52 bin = ir3_assemble(so->ir, &info);
53 if (fd_mesa_debug & FD_DBG_DISASM) {
54 unsigned i;
55
56 debug_printf("%s: disasm:\n", type);
57 disasm_a3xx(bin, info.sizedwords, 0, so->type);
58
59 debug_printf("%s: outputs:", type);
60 for (i = 0; i < so->outputs_count; i++) {
61 uint8_t regid = so->outputs[i].regid;
62 ir3_semantic sem = so->outputs[i].semantic;
63 debug_printf(" r%d.%c (%u:%u)",
64 (regid >> 2), "xyzw"[regid & 0x3],
65 sem2name(sem), sem2idx(sem));
66 }
67 debug_printf("\n");
68 debug_printf("%s: inputs:", type);
69 for (i = 0; i < so->inputs_count; i++) {
70 uint8_t regid = so->inputs[i].regid;
71 ir3_semantic sem = so->inputs[i].semantic;
72 debug_printf(" r%d.%c (%u:%u,cm=%x,il=%u,b=%u)",
73 (regid >> 2), "xyzw"[regid & 0x3],
74 sem2name(sem), sem2idx(sem),
75 so->inputs[i].compmask,
76 so->inputs[i].inloc,
77 so->inputs[i].bary);
78 }
79 debug_printf("\n");
80 }
81 debug_printf("%s: %u instructions, %d half, %d full\n\n",
82 type, info.instrs_count, info.max_half_reg + 1, info.max_reg + 1);
83 free(bin);
84 }
85
86
87 static int
88 read_file(const char *filename, void **ptr, size_t *size)
89 {
90 int fd, ret;
91 struct stat st;
92
93 *ptr = MAP_FAILED;
94
95 fd = open(filename, O_RDONLY);
96 if (fd == -1) {
97 warnx("couldn't open `%s'", filename);
98 return 1;
99 }
100
101 ret = fstat(fd, &st);
102 if (ret)
103 errx(1, "couldn't stat `%s'", filename);
104
105 *size = st.st_size;
106 *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
107 if (*ptr == MAP_FAILED)
108 errx(1, "couldn't map `%s'", filename);
109
110 close(fd);
111
112 return 0;
113 }
114
115 static void reset_variant(struct ir3_shader_variant *v, const char *msg)
116 {
117 debug_error(msg);
118 v->inputs_count = 0;
119 v->outputs_count = 0;
120 v->total_in = 0;
121 v->has_samp = false;
122 v->immediates_count = 0;
123 }
124
125 static void print_usage(void)
126 {
127 printf("Usage: ir3_compiler [OPTIONS]... FILE\n");
128 printf(" --verbose - verbose compiler/debug messages\n");
129 printf(" --binning-pass - generate binning pass shader (VERT)\n");
130 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
131 printf(" --half-precision - use half-precision\n");
132 printf(" --alpha - generate render-to-alpha shader (FRAG)\n");
133 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
134 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
135 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
136 printf(" --help - show this message\n");
137 }
138
139 int main(int argc, char **argv)
140 {
141 int ret = 0, n = 1;
142 const char *filename;
143 struct tgsi_token toks[65536];
144 struct tgsi_parse_context parse;
145 struct ir3_shader_variant v;
146 struct ir3_shader_key key = {
147 };
148 void *ptr;
149 size_t size;
150
151 fd_mesa_debug |= FD_DBG_DISASM;
152
153 while (n < argc) {
154 if (!strcmp(argv[n], "--verbose")) {
155 fd_mesa_debug |= FD_DBG_OPTDUMP | FD_DBG_MSGS | FD_DBG_OPTMSGS;
156 n++;
157 continue;
158 }
159
160 if (!strcmp(argv[n], "--binning-pass")) {
161 key.binning_pass = true;
162 n++;
163 continue;
164 }
165
166 if (!strcmp(argv[n], "--color-two-side")) {
167 key.color_two_side = true;
168 n++;
169 continue;
170 }
171
172 if (!strcmp(argv[n], "--half-precision")) {
173 key.half_precision = true;
174 n++;
175 continue;
176 }
177
178 if (!strcmp(argv[n], "--alpha")) {
179 key.alpha = true;
180 n++;
181 continue;
182 }
183
184 if (!strcmp(argv[n], "--saturate-s")) {
185 key.vsaturate_s = key.fsaturate_s = strtol(argv[n+1], NULL, 0);
186 n += 2;
187 continue;
188 }
189
190 if (!strcmp(argv[n], "--saturate-t")) {
191 key.vsaturate_t = key.fsaturate_t = strtol(argv[n+1], NULL, 0);
192 n += 2;
193 continue;
194 }
195
196 if (!strcmp(argv[n], "--saturate-r")) {
197 key.vsaturate_r = key.fsaturate_r = strtol(argv[n+1], NULL, 0);
198 n += 2;
199 continue;
200 }
201
202 if (!strcmp(argv[n], "--help")) {
203 print_usage();
204 return 0;
205 }
206
207 break;
208 }
209
210 filename = argv[n];
211
212 memset(&v, 0, sizeof(v));
213 v.key = key;
214
215 ret = read_file(filename, &ptr, &size);
216 if (ret) {
217 print_usage();
218 return ret;
219 }
220
221 if (!tgsi_text_translate(ptr, toks, Elements(toks)))
222 errx(1, "could not parse `%s'", filename);
223
224 tgsi_parse_init(&parse, toks);
225 switch (parse.FullHeader.Processor.Processor) {
226 case TGSI_PROCESSOR_FRAGMENT:
227 v.type = SHADER_FRAGMENT;
228 break;
229 case TGSI_PROCESSOR_VERTEX:
230 v.type = SHADER_VERTEX;
231 break;
232 case TGSI_PROCESSOR_COMPUTE:
233 v.type = SHADER_COMPUTE;
234 break;
235 }
236
237 if (!(fd_mesa_debug & FD_DBG_NOOPT)) {
238 /* with new compiler: */
239 ret = ir3_compile_shader(&v, toks, key, true);
240
241 if (ret) {
242 reset_variant(&v, "new compiler failed, trying without copy propagation!");
243 ret = ir3_compile_shader(&v, toks, key, false);
244 if (ret)
245 reset_variant(&v, "new compiler failed, trying fallback!\n");
246 }
247 }
248
249 if (ret)
250 ret = ir3_compile_shader_old(&v, toks, key);
251
252 if (ret) {
253 fprintf(stderr, "old compiler failed!\n");
254 return ret;
255 }
256 dump_info(&v);
257 }