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