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