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