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