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