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