81d0180933fc5945004fc0714c7f55ed2064bf8e
[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_reg(const char *name, uint32_t r)
46 {
47 if (r != regid(63,0))
48 debug_printf("; %s: r%d.%c\n", name, r >> 2, "xyzw"[r & 0x3]);
49 }
50
51 static void dump_semantic(struct ir3_shader_variant *so,
52 unsigned sem, const char *name)
53 {
54 uint32_t regid;
55 regid = ir3_find_output_regid(so, ir3_semantic_name(sem, 0));
56 dump_reg(name, regid);
57 }
58
59 static void dump_info(struct ir3_shader_variant *so, const char *str)
60 {
61 uint32_t *bin;
62 const char *type = (so->type == SHADER_VERTEX) ? "VERT" : "FRAG";
63
64 // for debug, dump some before/after info:
65 // TODO make gpu_id configurable on cmdline
66 bin = ir3_shader_assemble(so, 320);
67 if (fd_mesa_debug & FD_DBG_DISASM) {
68 struct ir3_block *block = so->ir->block;
69 struct ir3_register *reg;
70 uint8_t regid;
71 unsigned i;
72
73 debug_printf("; %s: %s\n", type, str);
74
75 for (i = 0; i < block->ninputs; i++) {
76 if (!block->inputs[i]) {
77 debug_printf("; in%d unused\n", i);
78 continue;
79 }
80 reg = block->inputs[i]->regs[0];
81 regid = reg->num;
82 debug_printf("@in(%sr%d.%c)\tin%d\n",
83 (reg->flags & IR3_REG_HALF) ? "h" : "",
84 (regid >> 2), "xyzw"[regid & 0x3], i);
85 }
86
87 for (i = 0; i < block->noutputs; i++) {
88 if (!block->outputs[i]) {
89 debug_printf("; out%d unused\n", i);
90 continue;
91 }
92 /* kill shows up as a virtual output.. skip it! */
93 if (is_kill(block->outputs[i]))
94 continue;
95 reg = block->outputs[i]->regs[0];
96 regid = reg->num;
97 debug_printf("@out(%sr%d.%c)\tout%d\n",
98 (reg->flags & IR3_REG_HALF) ? "h" : "",
99 (regid >> 2), "xyzw"[regid & 0x3], i);
100 }
101
102 for (i = 0; i < so->immediates_count; i++) {
103 debug_printf("@const(c%d.x)\t", so->first_immediate + i);
104 debug_printf("0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
105 so->immediates[i].val[0],
106 so->immediates[i].val[1],
107 so->immediates[i].val[2],
108 so->immediates[i].val[3]);
109 }
110
111 disasm_a3xx(bin, so->info.sizedwords, 0, so->type);
112
113 debug_printf("; %s: outputs:", type);
114 for (i = 0; i < so->outputs_count; i++) {
115 uint8_t regid = so->outputs[i].regid;
116 ir3_semantic sem = so->outputs[i].semantic;
117 debug_printf(" r%d.%c (%u:%u)",
118 (regid >> 2), "xyzw"[regid & 0x3],
119 sem2name(sem), sem2idx(sem));
120 }
121 debug_printf("\n");
122 debug_printf("; %s: inputs:", type);
123 for (i = 0; i < so->inputs_count; i++) {
124 uint8_t regid = so->inputs[i].regid;
125 ir3_semantic sem = so->inputs[i].semantic;
126 debug_printf(" r%d.%c (%u:%u,cm=%x,il=%u,b=%u)",
127 (regid >> 2), "xyzw"[regid & 0x3],
128 sem2name(sem), sem2idx(sem),
129 so->inputs[i].compmask,
130 so->inputs[i].inloc,
131 so->inputs[i].bary);
132 }
133 debug_printf("\n");
134 }
135
136 /* print generic shader info: */
137 debug_printf("; %s: %u instructions, %d half, %d full\n", type,
138 so->info.instrs_count,
139 so->info.max_half_reg + 1,
140 so->info.max_reg + 1);
141
142 /* print shader type specific info: */
143 switch (so->type) {
144 case SHADER_VERTEX:
145 dump_semantic(so, TGSI_SEMANTIC_POSITION, "pos");
146 dump_semantic(so, TGSI_SEMANTIC_PSIZE, "psize");
147 break;
148 case SHADER_FRAGMENT:
149 dump_reg("pos (bary)", so->pos_regid);
150 dump_semantic(so, TGSI_SEMANTIC_POSITION, "posz");
151 dump_semantic(so, TGSI_SEMANTIC_COLOR, "color");
152 /* these two are hard-coded since we don't know how to
153 * program them to anything but all 0's...
154 */
155 if (so->frag_coord)
156 debug_printf("; fragcoord: r0.x\n");
157 if (so->frag_face)
158 debug_printf("; fragface: hr0.x\n");
159 break;
160 case SHADER_COMPUTE:
161 break;
162 }
163 free(bin);
164
165 debug_printf("\n");
166 }
167
168
169 static int
170 read_file(const char *filename, void **ptr, size_t *size)
171 {
172 int fd, ret;
173 struct stat st;
174
175 *ptr = MAP_FAILED;
176
177 fd = open(filename, O_RDONLY);
178 if (fd == -1) {
179 warnx("couldn't open `%s'", filename);
180 return 1;
181 }
182
183 ret = fstat(fd, &st);
184 if (ret)
185 errx(1, "couldn't stat `%s'", filename);
186
187 *size = st.st_size;
188 *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
189 if (*ptr == MAP_FAILED)
190 errx(1, "couldn't map `%s'", filename);
191
192 close(fd);
193
194 return 0;
195 }
196
197 static void reset_variant(struct ir3_shader_variant *v, const char *msg)
198 {
199 printf("; %s\n", msg);
200 v->inputs_count = 0;
201 v->outputs_count = 0;
202 v->total_in = 0;
203 v->has_samp = false;
204 v->immediates_count = 0;
205 }
206
207 static void print_usage(void)
208 {
209 printf("Usage: ir3_compiler [OPTIONS]... FILE\n");
210 printf(" --verbose - verbose compiler/debug messages\n");
211 printf(" --binning-pass - generate binning pass shader (VERT)\n");
212 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
213 printf(" --half-precision - use half-precision\n");
214 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
215 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
216 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
217 printf(" --nocp - disable copy propagation\n");
218 printf(" --help - show this message\n");
219 }
220
221 int main(int argc, char **argv)
222 {
223 int ret = 0, n = 1;
224 const char *filename;
225 struct tgsi_token toks[65536];
226 struct tgsi_parse_context parse;
227 struct ir3_shader_variant v;
228 struct ir3_shader_key key = {};
229 const char *info;
230 void *ptr;
231 size_t size;
232
233 fd_mesa_debug |= FD_DBG_DISASM;
234
235 /* cmdline args which impact shader variant get spit out in a
236 * comment on the first line.. a quick/dirty way to preserve
237 * that info so when ir3test recompiles the shader with a new
238 * compiler version, we use the same shader-key settings:
239 */
240 debug_printf("; options:");
241
242 while (n < argc) {
243 if (!strcmp(argv[n], "--verbose")) {
244 fd_mesa_debug |= FD_DBG_OPTDUMP | FD_DBG_MSGS | FD_DBG_OPTMSGS;
245 n++;
246 continue;
247 }
248
249 if (!strcmp(argv[n], "--binning-pass")) {
250 debug_printf(" %s", argv[n]);
251 key.binning_pass = true;
252 n++;
253 continue;
254 }
255
256 if (!strcmp(argv[n], "--color-two-side")) {
257 debug_printf(" %s", argv[n]);
258 key.color_two_side = true;
259 n++;
260 continue;
261 }
262
263 if (!strcmp(argv[n], "--half-precision")) {
264 debug_printf(" %s", argv[n]);
265 key.half_precision = true;
266 n++;
267 continue;
268 }
269
270 if (!strcmp(argv[n], "--saturate-s")) {
271 debug_printf(" %s %s", argv[n], argv[n+1]);
272 key.vsaturate_s = key.fsaturate_s = strtol(argv[n+1], NULL, 0);
273 n += 2;
274 continue;
275 }
276
277 if (!strcmp(argv[n], "--saturate-t")) {
278 debug_printf(" %s %s", argv[n], argv[n+1]);
279 key.vsaturate_t = key.fsaturate_t = strtol(argv[n+1], NULL, 0);
280 n += 2;
281 continue;
282 }
283
284 if (!strcmp(argv[n], "--saturate-r")) {
285 debug_printf(" %s %s", argv[n], argv[n+1]);
286 key.vsaturate_r = key.fsaturate_r = strtol(argv[n+1], NULL, 0);
287 n += 2;
288 continue;
289 }
290
291 if (!strcmp(argv[n], "--nocp")) {
292 fd_mesa_debug |= FD_DBG_NOCP;
293 n++;
294 continue;
295 }
296
297 if (!strcmp(argv[n], "--help")) {
298 print_usage();
299 return 0;
300 }
301
302 break;
303 }
304 debug_printf("\n");
305
306 filename = argv[n];
307
308 memset(&v, 0, sizeof(v));
309 v.key = key;
310
311 ret = read_file(filename, &ptr, &size);
312 if (ret) {
313 print_usage();
314 return ret;
315 }
316
317 if (fd_mesa_debug & FD_DBG_OPTMSGS)
318 debug_printf("%s\n", (char *)ptr);
319
320 if (!tgsi_text_translate(ptr, toks, Elements(toks)))
321 errx(1, "could not parse `%s'", filename);
322
323 tgsi_parse_init(&parse, toks);
324 switch (parse.FullHeader.Processor.Processor) {
325 case TGSI_PROCESSOR_FRAGMENT:
326 v.type = SHADER_FRAGMENT;
327 break;
328 case TGSI_PROCESSOR_VERTEX:
329 v.type = SHADER_VERTEX;
330 break;
331 case TGSI_PROCESSOR_COMPUTE:
332 v.type = SHADER_COMPUTE;
333 break;
334 }
335
336 info = "compiler";
337 ret = ir3_compile_shader(&v, toks, key, true);
338
339 if (ret) {
340 reset_variant(&v, "compiler failed, trying without copy propagation!");
341 info = "compiler (no copy propagation)";
342 ret = ir3_compile_shader(&v, toks, key, false);
343 }
344
345 if (ret) {
346 fprintf(stderr, "compiler failed!\n");
347 return ret;
348 }
349 dump_info(&v, info);
350 }