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