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