dafc39911f26313500223294898db1ed6cced60a
[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 "nir/tgsi_to_nir.h"
37 #include "tgsi/tgsi_parse.h"
38 #include "tgsi/tgsi_text.h"
39 #include "tgsi/tgsi_dump.h"
40
41 #include "ir3/ir3_compiler.h"
42 #include "ir3/ir3_gallium.h"
43 #include "ir3/ir3_nir.h"
44 #include "ir3/instr-a3xx.h"
45 #include "ir3/ir3.h"
46
47 #include "main/mtypes.h"
48
49 #include "compiler/glsl/standalone.h"
50 #include "compiler/glsl/glsl_to_nir.h"
51 #include "compiler/glsl/gl_nir.h"
52 #include "compiler/nir_types.h"
53 #include "compiler/spirv/nir_spirv.h"
54
55 #include "pipe/p_context.h"
56
57 static void dump_info(struct ir3_shader_variant *so, const char *str)
58 {
59 uint32_t *bin;
60 const char *type = ir3_shader_stage(so);
61 bin = ir3_shader_assemble(so);
62 debug_printf("; %s: %s\n", type, str);
63 ir3_shader_disasm(so, bin, stdout);
64 free(bin);
65 }
66
67 static void
68 insert_sorted(struct exec_list *var_list, nir_variable *new_var)
69 {
70 nir_foreach_variable(var, var_list) {
71 if (var->data.location > new_var->data.location) {
72 exec_node_insert_node_before(&var->node, &new_var->node);
73 return;
74 }
75 }
76 exec_list_push_tail(var_list, &new_var->node);
77 }
78
79 static void
80 sort_varyings(nir_shader *nir, nir_variable_mode mode)
81 {
82 struct exec_list new_list;
83 exec_list_make_empty(&new_list);
84 nir_foreach_variable_with_modes_safe(var, nir, mode) {
85 exec_node_remove(&var->node);
86 insert_sorted(&new_list, var);
87 }
88 exec_list_move_nodes_to(&new_list, var_list);
89 }
90
91 static void
92 fixup_varying_slots(nir_shader *nir, nir_variable_mode mode)
93 {
94 nir_foreach_variable_with_modes(var, nir, mode) {
95 if (var->data.location >= VARYING_SLOT_VAR0) {
96 var->data.location += 9;
97 } else if ((var->data.location >= VARYING_SLOT_TEX0) &&
98 (var->data.location <= VARYING_SLOT_TEX7)) {
99 var->data.location += VARYING_SLOT_VAR0 - VARYING_SLOT_TEX0;
100 }
101 }
102 }
103
104 static struct ir3_compiler *compiler;
105
106 static nir_shader *
107 load_glsl(unsigned num_files, char* const* files, gl_shader_stage stage)
108 {
109 static const struct standalone_options options = {
110 .glsl_version = 460,
111 .do_link = true,
112 };
113 struct gl_shader_program *prog;
114 const nir_shader_compiler_options *nir_options =
115 ir3_get_compiler_options(compiler);
116 static struct gl_context local_ctx;
117
118 prog = standalone_compile_shader(&options, num_files, files, &local_ctx);
119 if (!prog)
120 errx(1, "couldn't parse `%s'", files[0]);
121
122 nir_shader *nir = glsl_to_nir(&local_ctx, prog, stage, nir_options);
123
124 /* required NIR passes: */
125 if (nir_options->lower_all_io_to_temps ||
126 nir->info.stage == MESA_SHADER_VERTEX ||
127 nir->info.stage == MESA_SHADER_GEOMETRY) {
128 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
129 nir_shader_get_entrypoint(nir),
130 true, true);
131 } else if (nir->info.stage == MESA_SHADER_FRAGMENT) {
132 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
133 nir_shader_get_entrypoint(nir),
134 true, false);
135 }
136
137 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
138 NIR_PASS_V(nir, nir_split_var_copies);
139 NIR_PASS_V(nir, nir_lower_var_copies);
140
141 NIR_PASS_V(nir, nir_split_var_copies);
142 NIR_PASS_V(nir, nir_lower_var_copies);
143 nir_print_shader(nir, stdout);
144 NIR_PASS_V(nir, gl_nir_lower_atomics, prog, true);
145 NIR_PASS_V(nir, nir_lower_atomics_to_ssbo);
146 nir_print_shader(nir, stdout);
147
148 switch (stage) {
149 case MESA_SHADER_VERTEX:
150 nir_assign_var_locations(nir, nir_var_shader_in,
151 &nir->num_inputs,
152 ir3_glsl_type_size);
153
154 /* Re-lower global vars, to deal with any dead VS inputs. */
155 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
156
157 sort_varyings(nir, nir_var_shader_out);
158 nir_assign_var_locations(nir, nir_var_shader_out,
159 &nir->num_outputs,
160 ir3_glsl_type_size);
161 fixup_varying_slots(nir, nir_var_shader_out);
162 break;
163 case MESA_SHADER_FRAGMENT:
164 sort_varyings(nir, nir_var_shader_in);
165 nir_assign_var_locations(nir, nir_var_shader_in,
166 &nir->num_inputs,
167 ir3_glsl_type_size);
168 fixup_varying_slots(nir, nir_var_shader_in);
169 nir_assign_var_locations(nir, nir_var_shader_out,
170 &nir->num_outputs,
171 ir3_glsl_type_size);
172 break;
173 case MESA_SHADER_COMPUTE:
174 case MESA_SHADER_KERNEL:
175 break;
176 default:
177 errx(1, "unhandled shader stage: %d", stage);
178 }
179
180 nir_assign_var_locations(nir, nir_var_uniform,
181 &nir->num_uniforms,
182 ir3_glsl_type_size);
183
184 NIR_PASS_V(nir, nir_lower_system_values);
185 NIR_PASS_V(nir, nir_lower_frexp);
186 NIR_PASS_V(nir, nir_lower_io,
187 nir_var_shader_in | nir_var_shader_out,
188 ir3_glsl_type_size, (nir_lower_io_options)0);
189 NIR_PASS_V(nir, gl_nir_lower_samplers, prog);
190
191 return nir;
192 }
193
194 static int
195 read_file(const char *filename, void **ptr, size_t *size)
196 {
197 int fd, ret;
198 struct stat st;
199
200 *ptr = MAP_FAILED;
201
202 fd = open(filename, O_RDONLY);
203 if (fd == -1) {
204 warnx("couldn't open `%s'", filename);
205 return 1;
206 }
207
208 ret = fstat(fd, &st);
209 if (ret)
210 errx(1, "couldn't stat `%s'", filename);
211
212 *size = st.st_size;
213 *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
214 if (*ptr == MAP_FAILED)
215 errx(1, "couldn't map `%s'", filename);
216
217 close(fd);
218
219 return 0;
220 }
221
222 static void debug_func(void *priv, enum nir_spirv_debug_level level,
223 size_t spirv_offset, const char *message)
224 {
225 // printf("%s\n", message);
226 }
227
228 static nir_shader *
229 load_spirv(const char *filename, const char *entry, gl_shader_stage stage)
230 {
231 const struct spirv_to_nir_options spirv_options = {
232 /* these caps are just make-believe */
233 .caps = {
234 .draw_parameters = true,
235 .float64 = true,
236 .image_read_without_format = true,
237 .image_write_without_format = true,
238 .int64 = true,
239 .variable_pointers = true,
240 },
241 .lower_ubo_ssbo_access_to_offsets = true,
242 .debug = {
243 .func = debug_func,
244 }
245 };
246 nir_shader *nir;
247 void *buf;
248 size_t size;
249
250 read_file(filename, &buf, &size);
251
252 nir = spirv_to_nir(buf, size / 4,
253 NULL, 0, /* spec_entries */
254 stage, entry,
255 &spirv_options,
256 ir3_get_compiler_options(compiler));
257
258 nir_print_shader(nir, stdout);
259
260 return nir;
261 }
262
263 static void print_usage(void)
264 {
265 printf("Usage: ir3_compiler [OPTIONS]... <file.tgsi | file.spv entry_point | (file.vert | file.frag)*>\n");
266 printf(" --verbose - verbose compiler/debug messages\n");
267 printf(" --binning-pass - generate binning pass shader (VERT)\n");
268 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
269 printf(" --half-precision - use half-precision\n");
270 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
271 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
272 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
273 printf(" --astc-srgb MASK - bitmask of samplers to enable astc-srgb workaround\n");
274 printf(" --stream-out - enable stream-out (aka transform feedback)\n");
275 printf(" --ucp MASK - bitmask of enabled user-clip-planes\n");
276 printf(" --gpu GPU_ID - specify gpu-id (default 320)\n");
277 printf(" --help - show this message\n");
278 }
279
280 int main(int argc, char **argv)
281 {
282 int ret = 0, n = 1;
283 char *filenames[2];
284 int num_files = 0;
285 unsigned stage = 0;
286 struct ir3_shader_variant v;
287 struct ir3_shader s;
288 struct ir3_shader_key key = {};
289 /* TODO cmdline option to target different gpus: */
290 unsigned gpu_id = 320;
291 const char *info;
292 const char *spirv_entry = NULL;
293 void *ptr;
294 bool from_tgsi = false;
295 size_t size;
296
297 memset(&s, 0, sizeof(s));
298 memset(&v, 0, sizeof(v));
299
300 /* cmdline args which impact shader variant get spit out in a
301 * comment on the first line.. a quick/dirty way to preserve
302 * that info so when ir3test recompiles the shader with a new
303 * compiler version, we use the same shader-key settings:
304 */
305 debug_printf("; options:");
306
307 while (n < argc) {
308 if (!strcmp(argv[n], "--verbose")) {
309 ir3_shader_debug |= IR3_DBG_OPTMSGS | IR3_DBG_DISASM;
310 n++;
311 continue;
312 }
313
314 if (!strcmp(argv[n], "--binning-pass")) {
315 debug_printf(" %s", argv[n]);
316 v.binning_pass = true;
317 n++;
318 continue;
319 }
320
321 if (!strcmp(argv[n], "--color-two-side")) {
322 debug_printf(" %s", argv[n]);
323 key.color_two_side = true;
324 n++;
325 continue;
326 }
327
328 if (!strcmp(argv[n], "--half-precision")) {
329 debug_printf(" %s", argv[n]);
330 n++;
331 continue;
332 }
333
334 if (!strcmp(argv[n], "--saturate-s")) {
335 debug_printf(" %s %s", argv[n], argv[n+1]);
336 key.vsaturate_s = key.fsaturate_s = strtol(argv[n+1], NULL, 0);
337 n += 2;
338 continue;
339 }
340
341 if (!strcmp(argv[n], "--saturate-t")) {
342 debug_printf(" %s %s", argv[n], argv[n+1]);
343 key.vsaturate_t = key.fsaturate_t = strtol(argv[n+1], NULL, 0);
344 n += 2;
345 continue;
346 }
347
348 if (!strcmp(argv[n], "--saturate-r")) {
349 debug_printf(" %s %s", argv[n], argv[n+1]);
350 key.vsaturate_r = key.fsaturate_r = strtol(argv[n+1], NULL, 0);
351 n += 2;
352 continue;
353 }
354
355 if (!strcmp(argv[n], "--astc-srgb")) {
356 debug_printf(" %s %s", argv[n], argv[n+1]);
357 key.vastc_srgb = key.fastc_srgb = strtol(argv[n+1], NULL, 0);
358 n += 2;
359 continue;
360 }
361
362 if (!strcmp(argv[n], "--stream-out")) {
363 struct ir3_stream_output_info *so = &s.stream_output;
364 debug_printf(" %s", argv[n]);
365 /* TODO more dynamic config based on number of outputs, etc
366 * rather than just hard-code for first output:
367 */
368 so->num_outputs = 1;
369 so->stride[0] = 4;
370 so->output[0].register_index = 0;
371 so->output[0].start_component = 0;
372 so->output[0].num_components = 4;
373 so->output[0].output_buffer = 0;
374 so->output[0].dst_offset = 2;
375 so->output[0].stream = 0;
376 n++;
377 continue;
378 }
379
380 if (!strcmp(argv[n], "--ucp")) {
381 debug_printf(" %s %s", argv[n], argv[n+1]);
382 key.ucp_enables = strtol(argv[n+1], NULL, 0);
383 n += 2;
384 continue;
385 }
386
387 if (!strcmp(argv[n], "--gpu")) {
388 debug_printf(" %s %s", argv[n], argv[n+1]);
389 gpu_id = strtol(argv[n+1], NULL, 0);
390 n += 2;
391 continue;
392 }
393
394 if (!strcmp(argv[n], "--help")) {
395 print_usage();
396 return 0;
397 }
398
399 break;
400 }
401 debug_printf("\n");
402
403 while (n < argc) {
404 char *filename = argv[n];
405 char *ext = strrchr(filename, '.');
406
407 if (strcmp(ext, ".tgsi") == 0) {
408 if (num_files != 0)
409 errx(1, "in TGSI mode, only a single file may be specified");
410 from_tgsi = true;
411 } else if (strcmp(ext, ".spv") == 0) {
412 if (num_files != 0)
413 errx(1, "in SPIR-V mode, only a single file may be specified");
414 stage = MESA_SHADER_COMPUTE;
415 filenames[num_files++] = filename;
416 n++;
417 if (n == argc)
418 errx(1, "in SPIR-V mode, an entry point must be specified");
419 spirv_entry = argv[n];
420 n++;
421 } else if (strcmp(ext, ".comp") == 0) {
422 if (from_tgsi || spirv_entry)
423 errx(1, "cannot mix GLSL/TGSI/SPIRV");
424 if (num_files >= ARRAY_SIZE(filenames))
425 errx(1, "too many GLSL files");
426 stage = MESA_SHADER_COMPUTE;
427 } else if (strcmp(ext, ".frag") == 0) {
428 if (from_tgsi || spirv_entry)
429 errx(1, "cannot mix GLSL/TGSI/SPIRV");
430 if (num_files >= ARRAY_SIZE(filenames))
431 errx(1, "too many GLSL files");
432 stage = MESA_SHADER_FRAGMENT;
433 } else if (strcmp(ext, ".vert") == 0) {
434 if (from_tgsi)
435 errx(1, "cannot mix GLSL and TGSI");
436 if (num_files >= ARRAY_SIZE(filenames))
437 errx(1, "too many GLSL files");
438 stage = MESA_SHADER_VERTEX;
439 } else {
440 print_usage();
441 return -1;
442 }
443
444 filenames[num_files++] = filename;
445
446 n++;
447 }
448
449 nir_shader *nir;
450
451 compiler = ir3_compiler_create(NULL, gpu_id);
452
453 if (from_tgsi) {
454 struct tgsi_token toks[65536];
455 const nir_shader_compiler_options *nir_options =
456 ir3_get_compiler_options(compiler);
457
458 ret = read_file(filenames[0], &ptr, &size);
459 if (ret) {
460 print_usage();
461 return ret;
462 }
463
464 if (ir3_shader_debug & IR3_DBG_OPTMSGS)
465 debug_printf("%s\n", (char *)ptr);
466
467 if (!tgsi_text_translate(ptr, toks, ARRAY_SIZE(toks)))
468 errx(1, "could not parse `%s'", filenames[0]);
469
470 if (ir3_shader_debug & IR3_DBG_OPTMSGS)
471 tgsi_dump(toks, 0);
472
473 nir = tgsi_to_nir_noscreen(toks, nir_options);
474 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
475 } else if (spirv_entry) {
476 nir = load_spirv(filenames[0], spirv_entry, stage);
477
478 NIR_PASS_V(nir, nir_lower_io,
479 nir_var_shader_in | nir_var_shader_out,
480 ir3_glsl_type_size, (nir_lower_io_options)0);
481
482 /* TODO do this somewhere else */
483 nir_lower_int64(nir, ~0);
484 nir_lower_system_values(nir);
485 } else if (num_files > 0) {
486 nir = load_glsl(num_files, filenames, stage);
487 } else {
488 print_usage();
489 return -1;
490 }
491
492 s.compiler = compiler;
493 s.nir = nir;
494
495 ir3_finalize_nir(compiler, nir);
496
497 v.key = key;
498 v.shader = &s;
499 s.type = v.type = nir->info.stage;
500
501 ir3_nir_lower_variant(&v, nir);
502
503 info = "NIR compiler";
504 ret = ir3_compile_shader_nir(s.compiler, &v);
505 if (ret) {
506 fprintf(stderr, "compiler failed!\n");
507 return ret;
508 }
509 dump_info(&v, info);
510 }