freedreno/ir3: ir3_cmdline updates
[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 #include <getopt.h>
36
37 #include "nir/tgsi_to_nir.h"
38 #include "tgsi/tgsi_parse.h"
39 #include "tgsi/tgsi_text.h"
40 #include "tgsi/tgsi_dump.h"
41
42 #include "ir3/ir3_compiler.h"
43 #include "ir3/ir3_gallium.h"
44 #include "ir3/ir3_nir.h"
45 #include "ir3/instr-a3xx.h"
46 #include "ir3/ir3.h"
47
48 #include "main/mtypes.h"
49
50 #include "compiler/glsl/standalone.h"
51 #include "compiler/glsl/glsl_to_nir.h"
52 #include "compiler/glsl/gl_nir.h"
53 #include "compiler/nir_types.h"
54 #include "compiler/spirv/nir_spirv.h"
55
56 #include "pipe/p_context.h"
57
58 static void
59 dump_info(struct ir3_shader_variant *so, const char *str)
60 {
61 uint32_t *bin;
62 const char *type = ir3_shader_stage(so);
63 bin = ir3_shader_assemble(so);
64 printf("; %s: %s\n", type, str);
65 ir3_shader_disasm(so, bin, stdout);
66 }
67
68 static void
69 insert_sorted(struct exec_list *var_list, nir_variable *new_var)
70 {
71 nir_foreach_variable_in_list(var, var_list) {
72 if (var->data.location > new_var->data.location) {
73 exec_node_insert_node_before(&var->node, &new_var->node);
74 return;
75 }
76 }
77 exec_list_push_tail(var_list, &new_var->node);
78 }
79
80 static void
81 sort_varyings(nir_shader *nir, nir_variable_mode mode)
82 {
83 struct exec_list new_list;
84 exec_list_make_empty(&new_list);
85 nir_foreach_variable_with_modes_safe(var, nir, mode) {
86 exec_node_remove(&var->node);
87 insert_sorted(&new_list, var);
88 }
89 exec_list_append(&nir->variables, &new_list);
90 }
91
92 static void
93 fixup_varying_slots(nir_shader *nir, nir_variable_mode mode)
94 {
95 nir_foreach_variable_with_modes(var, nir, mode) {
96 if (var->data.location >= VARYING_SLOT_VAR0) {
97 var->data.location += 9;
98 } else if ((var->data.location >= VARYING_SLOT_TEX0) &&
99 (var->data.location <= VARYING_SLOT_TEX7)) {
100 var->data.location += VARYING_SLOT_VAR0 - VARYING_SLOT_TEX0;
101 }
102 }
103 }
104
105 static struct ir3_compiler *compiler;
106
107 static nir_shader *
108 load_glsl(unsigned num_files, char* const* files, gl_shader_stage stage)
109 {
110 static const struct standalone_options options = {
111 .glsl_version = 310,
112 .do_link = true,
113 .lower_precision = true,
114 };
115 struct gl_shader_program *prog;
116 const nir_shader_compiler_options *nir_options =
117 ir3_get_compiler_options(compiler);
118 static struct gl_context local_ctx;
119
120 prog = standalone_compile_shader(&options, num_files, files, &local_ctx);
121 if (!prog)
122 errx(1, "couldn't parse `%s'", files[0]);
123
124 nir_shader *nir = glsl_to_nir(&local_ctx, prog, stage, nir_options);
125
126 /* required NIR passes: */
127 if (nir_options->lower_all_io_to_temps ||
128 nir->info.stage == MESA_SHADER_VERTEX ||
129 nir->info.stage == MESA_SHADER_GEOMETRY) {
130 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
131 nir_shader_get_entrypoint(nir),
132 true, true);
133 } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
134 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
135 nir_shader_get_entrypoint(nir),
136 true, false);
137 }
138
139 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
140 NIR_PASS_V(nir, nir_split_var_copies);
141 NIR_PASS_V(nir, nir_lower_var_copies);
142
143 NIR_PASS_V(nir, nir_split_var_copies);
144 NIR_PASS_V(nir, nir_lower_var_copies);
145 nir_print_shader(nir, stdout);
146 NIR_PASS_V(nir, gl_nir_lower_atomics, prog, true);
147 NIR_PASS_V(nir, gl_nir_lower_buffers, prog);
148 NIR_PASS_V(nir, nir_lower_atomics_to_ssbo);
149 nir_print_shader(nir, stdout);
150
151 switch (stage) {
152 case MESA_SHADER_VERTEX:
153 nir_assign_var_locations(nir, nir_var_shader_in,
154 &nir->num_inputs,
155 ir3_glsl_type_size);
156
157 /* Re-lower global vars, to deal with any dead VS inputs. */
158 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
159
160 sort_varyings(nir, nir_var_shader_out);
161 nir_assign_var_locations(nir, nir_var_shader_out,
162 &nir->num_outputs,
163 ir3_glsl_type_size);
164 fixup_varying_slots(nir, nir_var_shader_out);
165 break;
166 case MESA_SHADER_FRAGMENT:
167 sort_varyings(nir, nir_var_shader_in);
168 nir_assign_var_locations(nir, nir_var_shader_in,
169 &nir->num_inputs,
170 ir3_glsl_type_size);
171 fixup_varying_slots(nir, nir_var_shader_in);
172 nir_assign_var_locations(nir, nir_var_shader_out,
173 &nir->num_outputs,
174 ir3_glsl_type_size);
175 break;
176 case MESA_SHADER_COMPUTE:
177 case MESA_SHADER_KERNEL:
178 break;
179 default:
180 errx(1, "unhandled shader stage: %d", stage);
181 }
182
183 nir_assign_var_locations(nir, nir_var_uniform,
184 &nir->num_uniforms,
185 ir3_glsl_type_size);
186
187 NIR_PASS_V(nir, nir_lower_system_values);
188 NIR_PASS_V(nir, nir_lower_frexp);
189 NIR_PASS_V(nir, nir_lower_io,
190 nir_var_shader_in | nir_var_shader_out | nir_var_uniform,
191 ir3_glsl_type_size, (nir_lower_io_options)0);
192 NIR_PASS_V(nir, gl_nir_lower_samplers, prog);
193
194 return nir;
195 }
196
197 static int
198 read_file(const char *filename, void **ptr, size_t *size)
199 {
200 int fd, ret;
201 struct stat st;
202
203 *ptr = MAP_FAILED;
204
205 fd = open(filename, O_RDONLY);
206 if (fd == -1) {
207 warnx("couldn't open `%s'", filename);
208 return 1;
209 }
210
211 ret = fstat(fd, &st);
212 if (ret)
213 errx(1, "couldn't stat `%s'", filename);
214
215 *size = st.st_size;
216 *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
217 if (*ptr == MAP_FAILED)
218 errx(1, "couldn't map `%s'", filename);
219
220 close(fd);
221
222 return 0;
223 }
224
225 static void debug_func(void *priv, enum nir_spirv_debug_level level,
226 size_t spirv_offset, const char *message)
227 {
228 // printf("%s\n", message);
229 }
230
231 static nir_shader *
232 load_spirv(const char *filename, const char *entry, gl_shader_stage stage)
233 {
234 const struct spirv_to_nir_options spirv_options = {
235 /* these caps are just make-believe */
236 .caps = {
237 .draw_parameters = true,
238 .float64 = true,
239 .image_read_without_format = true,
240 .image_write_without_format = true,
241 .int64 = true,
242 .variable_pointers = true,
243 },
244 .lower_ubo_ssbo_access_to_offsets = true,
245 .debug = {
246 .func = debug_func,
247 }
248 };
249 nir_shader *nir;
250 void *buf;
251 size_t size;
252
253 read_file(filename, &buf, &size);
254
255 nir = spirv_to_nir(buf, size / 4,
256 NULL, 0, /* spec_entries */
257 stage, entry,
258 &spirv_options,
259 ir3_get_compiler_options(compiler));
260
261 nir_print_shader(nir, stdout);
262
263 return nir;
264 }
265
266 static const char *shortopts = "g:hv";
267
268 static const struct option longopts[] = {
269 { "gpu", required_argument, 0, 'g' },
270 { "help", no_argument, 0, 'h' },
271 { "verbose", no_argument, 0, 'v' },
272 };
273
274 static void
275 print_usage(void)
276 {
277 printf("Usage: ir3_compiler [OPTIONS]... <file.tgsi | file.spv entry_point | (file.vert | file.frag)*>\n");
278 printf(" -g, --gpu GPU_ID - specify gpu-id (default 320)\n");
279 printf(" -h, --help - show this message\n");
280 printf(" -v, --verbose - verbose compiler/debug messages\n");
281 }
282
283 int
284 main(int argc, char **argv)
285 {
286 int ret = 0, opt;
287 char *filenames[2];
288 int num_files = 0;
289 unsigned stage = 0;
290 struct ir3_shader_key key = {};
291 unsigned gpu_id = 320;
292 const char *info;
293 const char *spirv_entry = NULL;
294 void *ptr;
295 bool from_tgsi = false;
296 size_t size;
297
298 while ((opt = getopt_long_only(argc, argv, shortopts, longopts, NULL)) != -1) {
299 switch (opt) {
300 case 'g':
301 gpu_id = strtol(optarg, NULL, 0);
302 break;
303 case 'v':
304 ir3_shader_debug |= IR3_DBG_OPTMSGS | IR3_DBG_DISASM;
305 break;
306 default:
307 printf("unrecognized arg: %c\n", opt);
308 /* fallthrough */
309 case 'h':
310 print_usage();
311 return 0;
312 }
313 }
314
315 if (optind >= argc) {
316 fprintf(stderr, "no file specified!\n");
317 print_usage();
318 return 0;
319 }
320
321 unsigned n = optind;
322 while (n < argc) {
323 char *filename = argv[n];
324 char *ext = strrchr(filename, '.');
325
326 if (strcmp(ext, ".tgsi") == 0) {
327 if (num_files != 0)
328 errx(1, "in TGSI mode, only a single file may be specified");
329 from_tgsi = true;
330 } else if (strcmp(ext, ".spv") == 0) {
331 if (num_files != 0)
332 errx(1, "in SPIR-V mode, only a single file may be specified");
333 stage = MESA_SHADER_COMPUTE;
334 filenames[num_files++] = filename;
335 n++;
336 if (n == argc)
337 errx(1, "in SPIR-V mode, an entry point must be specified");
338 spirv_entry = argv[n];
339 n++;
340 } else if (strcmp(ext, ".comp") == 0) {
341 if (from_tgsi || spirv_entry)
342 errx(1, "cannot mix GLSL/TGSI/SPIRV");
343 if (num_files >= ARRAY_SIZE(filenames))
344 errx(1, "too many GLSL files");
345 stage = MESA_SHADER_COMPUTE;
346 } else if (strcmp(ext, ".frag") == 0) {
347 if (from_tgsi || spirv_entry)
348 errx(1, "cannot mix GLSL/TGSI/SPIRV");
349 if (num_files >= ARRAY_SIZE(filenames))
350 errx(1, "too many GLSL files");
351 stage = MESA_SHADER_FRAGMENT;
352 } else if (strcmp(ext, ".vert") == 0) {
353 if (from_tgsi)
354 errx(1, "cannot mix GLSL and TGSI");
355 if (num_files >= ARRAY_SIZE(filenames))
356 errx(1, "too many GLSL files");
357 stage = MESA_SHADER_VERTEX;
358 } else {
359 print_usage();
360 return -1;
361 }
362
363 filenames[num_files++] = filename;
364
365 n++;
366 }
367
368 nir_shader *nir;
369
370 compiler = ir3_compiler_create(NULL, gpu_id);
371
372 if (from_tgsi) {
373 struct tgsi_token toks[65536];
374 const nir_shader_compiler_options *nir_options =
375 ir3_get_compiler_options(compiler);
376
377 ret = read_file(filenames[0], &ptr, &size);
378 if (ret) {
379 print_usage();
380 return ret;
381 }
382
383 if (ir3_shader_debug & IR3_DBG_OPTMSGS)
384 printf("%s\n", (char *)ptr);
385
386 if (!tgsi_text_translate(ptr, toks, ARRAY_SIZE(toks)))
387 errx(1, "could not parse `%s'", filenames[0]);
388
389 if (ir3_shader_debug & IR3_DBG_OPTMSGS)
390 tgsi_dump(toks, 0);
391
392 nir = tgsi_to_nir_noscreen(toks, nir_options);
393 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
394 } else if (spirv_entry) {
395 nir = load_spirv(filenames[0], spirv_entry, stage);
396
397 NIR_PASS_V(nir, nir_lower_io,
398 nir_var_shader_in | nir_var_shader_out,
399 ir3_glsl_type_size, (nir_lower_io_options)0);
400
401 /* TODO do this somewhere else */
402 nir_lower_int64(nir);
403 nir_lower_system_values(nir);
404 } else if (num_files > 0) {
405 nir = load_glsl(num_files, filenames, stage);
406 } else {
407 print_usage();
408 return -1;
409 }
410
411 ir3_finalize_nir(compiler, nir);
412 ir3_nir_post_finalize(compiler, nir);
413
414 struct ir3_shader *shader = rzalloc_size(NULL, sizeof(*shader));
415 shader->compiler = compiler;
416 shader->type = stage;
417 shader->nir = nir;
418
419 struct ir3_shader_variant *v = rzalloc_size(shader, sizeof(*v));
420 v->type = shader->type;
421 v->shader = shader;
422 v->key = key;
423 v->const_state = rzalloc_size(v, sizeof(*v->const_state));
424
425 shader->variants = v;
426 shader->variant_count = 1;
427
428 ir3_nir_lower_variant(v, nir);
429
430 info = "NIR compiler";
431 ret = ir3_compile_shader_nir(compiler, v);
432 if (ret) {
433 fprintf(stderr, "compiler failed!\n");
434 return ret;
435 }
436 dump_info(v, info);
437
438 return 0;
439 }