• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2014 Rob Clark <robclark@freedesktop.org>
3  * SPDX-License-Identifier: MIT
4  *
5  * Authors:
6  *    Rob Clark <robclark@freedesktop.org>
7  */
8 
9 #include <err.h>
10 #include <fcntl.h>
11 #include <getopt.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/mman.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 
19 #include "nir/tgsi_to_nir.h"
20 #include "tgsi/tgsi_dump.h"
21 #include "tgsi/tgsi_text.h"
22 
23 #include "ir3/instr-a3xx.h"
24 #include "ir3/ir3.h"
25 #include "ir3/ir3_compiler.h"
26 #include "ir3/ir3_gallium.h"
27 #include "ir3/ir3_nir.h"
28 
29 #include "main/mtypes.h"
30 
31 #include "compiler/glsl_types.h"
32 #include "compiler/glsl/gl_nir.h"
33 #include "compiler/glsl/glsl_to_nir.h"
34 #include "compiler/glsl/standalone.h"
35 #include "compiler/spirv/nir_spirv.h"
36 #include "compiler/spirv/spirv_info.h"
37 
38 #include "pipe/p_context.h"
39 
40 static void
dump_info(struct ir3_shader_variant * so,const char * str)41 dump_info(struct ir3_shader_variant *so, const char *str)
42 {
43    uint32_t *bin;
44    const char *type = ir3_shader_stage(so);
45    bin = ir3_shader_assemble(so);
46    printf("; %s: %s\n", type, str);
47    ir3_shader_disasm(so, bin, stdout);
48 }
49 
50 static void
insert_sorted(struct exec_list * var_list,nir_variable * new_var)51 insert_sorted(struct exec_list *var_list, nir_variable *new_var)
52 {
53    nir_foreach_variable_in_list (var, var_list) {
54       if (var->data.location > new_var->data.location) {
55          exec_node_insert_node_before(&var->node, &new_var->node);
56          return;
57       }
58    }
59    exec_list_push_tail(var_list, &new_var->node);
60 }
61 
62 static void
sort_varyings(nir_shader * nir,nir_variable_mode mode)63 sort_varyings(nir_shader *nir, nir_variable_mode mode)
64 {
65    struct exec_list new_list;
66    exec_list_make_empty(&new_list);
67    nir_foreach_variable_with_modes_safe (var, nir, mode) {
68       exec_node_remove(&var->node);
69       insert_sorted(&new_list, var);
70    }
71    exec_list_append(&nir->variables, &new_list);
72 }
73 
74 static void
fixup_varying_slots(nir_shader * nir,nir_variable_mode mode)75 fixup_varying_slots(nir_shader *nir, nir_variable_mode mode)
76 {
77    nir_foreach_variable_with_modes (var, nir, mode) {
78       if (var->data.location >= VARYING_SLOT_VAR0) {
79          var->data.location += 9;
80       } else if ((var->data.location >= VARYING_SLOT_TEX0) &&
81                  (var->data.location <= VARYING_SLOT_TEX7)) {
82          var->data.location += VARYING_SLOT_VAR0 - VARYING_SLOT_TEX0;
83       }
84    }
85 }
86 
87 static struct ir3_compiler *compiler;
88 
89 static nir_shader *
load_glsl(unsigned num_files,char * const * files,gl_shader_stage stage)90 load_glsl(unsigned num_files, char *const *files, gl_shader_stage stage)
91 {
92    static const struct standalone_options options = {
93       .glsl_version = 310,
94       .do_link = true,
95       .lower_precision = true,
96    };
97    struct gl_shader_program *prog;
98    const nir_shader_compiler_options *nir_options =
99       ir3_get_compiler_options(compiler);
100    static struct gl_context local_ctx;
101 
102    prog = standalone_compile_shader(&options, num_files, files, &local_ctx);
103    if (!prog)
104       errx(1, "couldn't parse `%s'", files[0]);
105 
106    nir_shader *nir = prog->_LinkedShaders[stage]->Program->nir;
107 
108    /* required NIR passes: */
109    if (nir_options->lower_all_io_to_temps ||
110        nir->info.stage == MESA_SHADER_VERTEX ||
111        nir->info.stage == MESA_SHADER_GEOMETRY) {
112       NIR_PASS_V(nir, nir_lower_io_to_temporaries,
113                  nir_shader_get_entrypoint(nir), true, true);
114    } else if (nir->info.stage == MESA_SHADER_TESS_EVAL ||
115               nir->info.stage == MESA_SHADER_FRAGMENT) {
116       NIR_PASS_V(nir, nir_lower_io_to_temporaries,
117                  nir_shader_get_entrypoint(nir), true, false);
118    }
119 
120    NIR_PASS_V(nir, nir_lower_global_vars_to_local);
121    NIR_PASS_V(nir, nir_split_var_copies);
122    NIR_PASS_V(nir, nir_lower_var_copies);
123 
124    NIR_PASS_V(nir, nir_split_var_copies);
125    NIR_PASS_V(nir, nir_lower_var_copies);
126    nir_print_shader(nir, stdout);
127    NIR_PASS_V(nir, gl_nir_lower_atomics, prog, true);
128    NIR_PASS_V(nir, gl_nir_lower_buffers, prog);
129    NIR_PASS_V(nir, nir_lower_atomics_to_ssbo, 0);
130    nir_print_shader(nir, stdout);
131 
132    switch (stage) {
133    case MESA_SHADER_VERTEX:
134       nir_assign_var_locations(nir, nir_var_shader_in, &nir->num_inputs,
135                                ir3_glsl_type_size);
136 
137       /* Re-lower global vars, to deal with any dead VS inputs. */
138       NIR_PASS_V(nir, nir_lower_global_vars_to_local);
139 
140       sort_varyings(nir, nir_var_shader_out);
141       nir_assign_var_locations(nir, nir_var_shader_out, &nir->num_outputs,
142                                ir3_glsl_type_size);
143       fixup_varying_slots(nir, nir_var_shader_out);
144       break;
145    case MESA_SHADER_FRAGMENT:
146       sort_varyings(nir, nir_var_shader_in);
147       nir_assign_var_locations(nir, nir_var_shader_in, &nir->num_inputs,
148                                ir3_glsl_type_size);
149       fixup_varying_slots(nir, nir_var_shader_in);
150       nir_assign_var_locations(nir, nir_var_shader_out, &nir->num_outputs,
151                                ir3_glsl_type_size);
152       break;
153    case MESA_SHADER_COMPUTE:
154    case MESA_SHADER_KERNEL:
155       break;
156    default:
157       errx(1, "unhandled shader stage: %d", stage);
158    }
159 
160    nir_assign_var_locations(nir, nir_var_uniform, &nir->num_uniforms,
161                             ir3_glsl_type_size);
162 
163    NIR_PASS_V(nir, nir_lower_system_values);
164    NIR_PASS_V(nir, nir_lower_compute_system_values, NULL);
165 
166    NIR_PASS_V(nir, nir_lower_frexp);
167    NIR_PASS_V(nir, nir_lower_io,
168               nir_var_shader_in | nir_var_shader_out | nir_var_uniform,
169               ir3_glsl_type_size,
170               nir_lower_io_use_interpolated_input_intrinsics);
171    NIR_PASS_V(nir, gl_nir_lower_samplers, prog);
172 
173    return nir;
174 }
175 
176 static int
read_file(const char * filename,void ** ptr,size_t * size)177 read_file(const char *filename, void **ptr, size_t *size)
178 {
179    int fd, ret;
180    struct stat st;
181 
182    *ptr = MAP_FAILED;
183 
184    fd = open(filename, O_RDONLY);
185    if (fd == -1) {
186       warnx("couldn't open `%s'", filename);
187       return 1;
188    }
189 
190    ret = fstat(fd, &st);
191    if (ret)
192       errx(1, "couldn't stat `%s'", filename);
193 
194    *size = st.st_size;
195    *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
196    if (*ptr == MAP_FAILED)
197       errx(1, "couldn't map `%s'", filename);
198 
199    close(fd);
200 
201    return 0;
202 }
203 
204 static void
debug_func(void * priv,enum nir_spirv_debug_level level,size_t spirv_offset,const char * message)205 debug_func(void *priv, enum nir_spirv_debug_level level, size_t spirv_offset,
206            const char *message)
207 {
208    //	printf("%s\n", message);
209 }
210 
211 static nir_shader *
load_spirv(const char * filename,const char * entry,gl_shader_stage stage)212 load_spirv(const char *filename, const char *entry, gl_shader_stage stage)
213 {
214    const struct spirv_capabilities spirv_caps = {
215       /* these caps are just make-believe */
216       .DrawParameters = true,
217       .Float64 = true,
218       .StorageImageReadWithoutFormat = true,
219       .StorageImageWriteWithoutFormat = true,
220       .Int64 = true,
221       .VariablePointers = true,
222    };
223    const struct spirv_to_nir_options spirv_options = {
224       .capabilities = &spirv_caps,
225       .debug = {
226          .func = debug_func,
227       }
228    };
229    nir_shader *nir;
230    void *buf;
231    size_t size;
232 
233    read_file(filename, &buf, &size);
234 
235    nir = spirv_to_nir(buf, size / 4, NULL, 0, /* spec_entries */
236                       stage, entry, &spirv_options,
237                       ir3_get_compiler_options(compiler));
238 
239    const struct nir_lower_sysvals_to_varyings_options sysvals_to_varyings = {
240       .frag_coord = true,
241       .point_coord = true,
242    };
243    NIR_PASS_V(nir, nir_lower_sysvals_to_varyings, &sysvals_to_varyings);
244 
245    nir_print_shader(nir, stdout);
246 
247    return nir;
248 }
249 
250 static const char *shortopts = "g:hv";
251 
252 static const struct option longopts[] = {
253    {"gpu",     required_argument, 0, 'g'},
254    {"help",    no_argument,       0, 'h'},
255    {"verbose", no_argument,       0, 'v'},
256 };
257 
258 static void
print_usage(void)259 print_usage(void)
260 {
261    printf("Usage: ir3_compiler [OPTIONS]... <file.tgsi | file.spv entry_point "
262           "| (file.vert | file.frag)*>\n");
263    printf("    -g, --gpu GPU_ID - specify gpu-id (default 320)\n");
264    printf("    -h, --help       - show this message\n");
265    printf("    -v, --verbose    - verbose compiler/debug messages\n");
266 }
267 
268 int
main(int argc,char ** argv)269 main(int argc, char **argv)
270 {
271    int ret = 0, opt;
272    char *filenames[2];
273    int num_files = 0;
274    unsigned stage = 0;
275    struct ir3_shader_key key = {};
276    unsigned gpu_id = 320;
277    const char *info;
278    const char *spirv_entry = NULL;
279    void *ptr;
280    bool from_tgsi = false;
281    size_t size;
282 
283    while ((opt = getopt_long_only(argc, argv, shortopts, longopts, NULL)) !=
284           -1) {
285       switch (opt) {
286       case 'g':
287          gpu_id = strtol(optarg, NULL, 0);
288          break;
289       case 'v':
290          ir3_shader_debug |= IR3_DBG_OPTMSGS | IR3_DBG_DISASM;
291          break;
292       default:
293          printf("unrecognized arg: %c\n", opt);
294          FALLTHROUGH;
295       case 'h':
296          print_usage();
297          return 0;
298       }
299    }
300 
301    if (optind >= argc) {
302       fprintf(stderr, "no file specified!\n");
303       print_usage();
304       return 0;
305    }
306 
307    unsigned n = optind;
308    while (n < argc) {
309       char *filename = argv[n];
310       char *ext = strrchr(filename, '.');
311 
312       if (strcmp(ext, ".tgsi") == 0) {
313          if (num_files != 0)
314             errx(1, "in TGSI mode, only a single file may be specified");
315          from_tgsi = true;
316       } else if (strcmp(ext, ".spv") == 0) {
317          if (num_files != 0)
318             errx(1, "in SPIR-V mode, only a single file may be specified");
319          stage = MESA_SHADER_COMPUTE;
320          filenames[num_files++] = filename;
321          n++;
322          if (n == argc)
323             errx(1, "in SPIR-V mode, an entry point must be specified");
324          spirv_entry = argv[n];
325          n++;
326       } else if (strcmp(ext, ".comp") == 0) {
327          if (from_tgsi || spirv_entry)
328             errx(1, "cannot mix GLSL/TGSI/SPIRV");
329          if (num_files >= ARRAY_SIZE(filenames))
330             errx(1, "too many GLSL files");
331          stage = MESA_SHADER_COMPUTE;
332       } else if (strcmp(ext, ".frag") == 0) {
333          if (from_tgsi || spirv_entry)
334             errx(1, "cannot mix GLSL/TGSI/SPIRV");
335          if (num_files >= ARRAY_SIZE(filenames))
336             errx(1, "too many GLSL files");
337          stage = MESA_SHADER_FRAGMENT;
338       } else if (strcmp(ext, ".vert") == 0) {
339          if (from_tgsi)
340             errx(1, "cannot mix GLSL and TGSI");
341          if (num_files >= ARRAY_SIZE(filenames))
342             errx(1, "too many GLSL files");
343          stage = MESA_SHADER_VERTEX;
344       } else {
345          print_usage();
346          return -1;
347       }
348 
349       filenames[num_files++] = filename;
350 
351       n++;
352    }
353 
354    nir_shader *nir;
355 
356    struct fd_dev_id dev_id = {
357          .gpu_id = gpu_id,
358    };
359    compiler = ir3_compiler_create(NULL, &dev_id, fd_dev_info_raw(&dev_id),
360                                   &(struct ir3_compiler_options) {});
361 
362    if (from_tgsi) {
363       struct tgsi_token toks[65536];
364       const nir_shader_compiler_options *nir_options =
365          ir3_get_compiler_options(compiler);
366 
367       ret = read_file(filenames[0], &ptr, &size);
368       if (ret) {
369          print_usage();
370          return ret;
371       }
372 
373       if (ir3_shader_debug & IR3_DBG_OPTMSGS)
374          printf("%s\n", (char *)ptr);
375 
376       if (!tgsi_text_translate(ptr, toks, ARRAY_SIZE(toks)))
377          errx(1, "could not parse `%s'", filenames[0]);
378 
379       if (ir3_shader_debug & IR3_DBG_OPTMSGS)
380          tgsi_dump(toks, 0);
381 
382       nir = tgsi_to_nir_noscreen(toks, nir_options);
383       NIR_PASS_V(nir, nir_lower_global_vars_to_local);
384    } else if (spirv_entry) {
385       nir = load_spirv(filenames[0], spirv_entry, stage);
386 
387       NIR_PASS_V(nir, nir_lower_io, nir_var_shader_in | nir_var_shader_out,
388                  ir3_glsl_type_size,
389                  nir_lower_io_use_interpolated_input_intrinsics);
390 
391       /* TODO do this somewhere else */
392       nir_lower_int64(nir);
393       nir_lower_system_values(nir);
394       nir_lower_compute_system_values(nir, NULL);
395    } else if (num_files > 0) {
396       nir = load_glsl(num_files, filenames, stage);
397    } else {
398       print_usage();
399       return -1;
400    }
401 
402    const struct ir3_shader_nir_options options = {};
403 
404    ir3_nir_lower_io_to_temporaries(nir);
405    ir3_finalize_nir(compiler, &options, nir);
406 
407    struct ir3_shader *shader = rzalloc_size(NULL, sizeof(*shader));
408    shader->compiler = compiler;
409    shader->type = stage;
410    shader->nir = nir;
411 
412    ir3_nir_post_finalize(shader);
413 
414    struct ir3_shader_variant *v = rzalloc_size(shader, sizeof(*v));
415    v->type = shader->type;
416    v->compiler = compiler;
417    v->key = key;
418    v->const_state = rzalloc_size(v, sizeof(*v->const_state));
419 
420    shader->variants = v;
421    shader->variant_count = 1;
422 
423    ir3_nir_lower_variant(v, &options, nir);
424 
425    info = "NIR compiler";
426    ret = ir3_compile_shader_nir(compiler, shader, v);
427    if (ret) {
428       fprintf(stderr, "compiler failed!\n");
429       return ret;
430    }
431    dump_info(v, info);
432 
433    return 0;
434 }
435