1 /*
2 * Copyright © 2014 Rob Clark <[email protected]>
3 * SPDX-License-Identifier: MIT
4 *
5 * Authors:
6 * Rob Clark <[email protected]>
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 = glsl_to_nir(&local_ctx.Const,
107 &prog->_LinkedShaders[stage]->ir,
108 &prog->_LinkedShaders[stage]->Program->info,
109 stage, nir_options);
110
111 if (nir->info.stage == MESA_SHADER_FRAGMENT) {
112 nir->info.fs.pixel_center_integer =
113 prog->_LinkedShaders[stage]->Program->info.fs.pixel_center_integer;
114 nir->info.fs.origin_upper_left =
115 prog->_LinkedShaders[stage]->Program->info.fs.origin_upper_left;
116 nir->info.fs.advanced_blend_modes =
117 prog->_LinkedShaders[stage]->Program->info.fs.advanced_blend_modes;
118 }
119
120 gl_nir_inline_functions(nir);
121
122 /* required NIR passes: */
123 if (nir_options->lower_all_io_to_temps ||
124 nir->info.stage == MESA_SHADER_VERTEX ||
125 nir->info.stage == MESA_SHADER_GEOMETRY) {
126 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
127 nir_shader_get_entrypoint(nir), true, true);
128 } else if (nir->info.stage == MESA_SHADER_TESS_EVAL ||
129 nir->info.stage == MESA_SHADER_FRAGMENT) {
130 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
131 nir_shader_get_entrypoint(nir), 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, gl_nir_lower_buffers, prog);
143 NIR_PASS_V(nir, nir_lower_atomics_to_ssbo, 0);
144 nir_print_shader(nir, stdout);
145
146 switch (stage) {
147 case MESA_SHADER_VERTEX:
148 nir_assign_var_locations(nir, nir_var_shader_in, &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, nir_var_shader_out);
155 nir_assign_var_locations(nir, nir_var_shader_out, &nir->num_outputs,
156 ir3_glsl_type_size);
157 fixup_varying_slots(nir, nir_var_shader_out);
158 break;
159 case MESA_SHADER_FRAGMENT:
160 sort_varyings(nir, nir_var_shader_in);
161 nir_assign_var_locations(nir, nir_var_shader_in, &nir->num_inputs,
162 ir3_glsl_type_size);
163 fixup_varying_slots(nir, nir_var_shader_in);
164 nir_assign_var_locations(nir, nir_var_shader_out, &nir->num_outputs,
165 ir3_glsl_type_size);
166 break;
167 case MESA_SHADER_COMPUTE:
168 case MESA_SHADER_KERNEL:
169 break;
170 default:
171 errx(1, "unhandled shader stage: %d", stage);
172 }
173
174 nir_assign_var_locations(nir, nir_var_uniform, &nir->num_uniforms,
175 ir3_glsl_type_size);
176
177 NIR_PASS_V(nir, nir_lower_system_values);
178 NIR_PASS_V(nir, nir_lower_compute_system_values, NULL);
179
180 NIR_PASS_V(nir, nir_lower_frexp);
181 NIR_PASS_V(nir, nir_lower_io,
182 nir_var_shader_in | nir_var_shader_out | nir_var_uniform,
183 ir3_glsl_type_size, (nir_lower_io_options)0);
184 NIR_PASS_V(nir, gl_nir_lower_samplers, prog);
185
186 return nir;
187 }
188
189 static int
read_file(const char * filename,void ** ptr,size_t * size)190 read_file(const char *filename, void **ptr, size_t *size)
191 {
192 int fd, ret;
193 struct stat st;
194
195 *ptr = MAP_FAILED;
196
197 fd = open(filename, O_RDONLY);
198 if (fd == -1) {
199 warnx("couldn't open `%s'", filename);
200 return 1;
201 }
202
203 ret = fstat(fd, &st);
204 if (ret)
205 errx(1, "couldn't stat `%s'", filename);
206
207 *size = st.st_size;
208 *ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
209 if (*ptr == MAP_FAILED)
210 errx(1, "couldn't map `%s'", filename);
211
212 close(fd);
213
214 return 0;
215 }
216
217 static void
debug_func(void * priv,enum nir_spirv_debug_level level,size_t spirv_offset,const char * message)218 debug_func(void *priv, enum nir_spirv_debug_level level, size_t spirv_offset,
219 const char *message)
220 {
221 // printf("%s\n", message);
222 }
223
224 static nir_shader *
load_spirv(const char * filename,const char * entry,gl_shader_stage stage)225 load_spirv(const char *filename, const char *entry, gl_shader_stage stage)
226 {
227 const struct spirv_capabilities spirv_caps = {
228 /* these caps are just make-believe */
229 .DrawParameters = true,
230 .Float64 = true,
231 .StorageImageReadWithoutFormat = true,
232 .StorageImageWriteWithoutFormat = true,
233 .Int64 = true,
234 .VariablePointers = true,
235 };
236 const struct spirv_to_nir_options spirv_options = {
237 .capabilities = &spirv_caps,
238 .debug = {
239 .func = debug_func,
240 }
241 };
242 nir_shader *nir;
243 void *buf;
244 size_t size;
245
246 read_file(filename, &buf, &size);
247
248 nir = spirv_to_nir(buf, size / 4, NULL, 0, /* spec_entries */
249 stage, entry, &spirv_options,
250 ir3_get_compiler_options(compiler));
251
252 const struct nir_lower_sysvals_to_varyings_options sysvals_to_varyings = {
253 .frag_coord = true,
254 .point_coord = true,
255 };
256 NIR_PASS_V(nir, nir_lower_sysvals_to_varyings, &sysvals_to_varyings);
257
258 nir_print_shader(nir, stdout);
259
260 return nir;
261 }
262
263 static const char *shortopts = "g:hv";
264
265 static const struct option longopts[] = {
266 {"gpu", required_argument, 0, 'g'},
267 {"help", no_argument, 0, 'h'},
268 {"verbose", no_argument, 0, 'v'},
269 };
270
271 static void
print_usage(void)272 print_usage(void)
273 {
274 printf("Usage: ir3_compiler [OPTIONS]... <file.tgsi | file.spv entry_point "
275 "| (file.vert | file.frag)*>\n");
276 printf(" -g, --gpu GPU_ID - specify gpu-id (default 320)\n");
277 printf(" -h, --help - show this message\n");
278 printf(" -v, --verbose - verbose compiler/debug messages\n");
279 }
280
281 int
main(int argc,char ** argv)282 main(int argc, char **argv)
283 {
284 int ret = 0, opt;
285 char *filenames[2];
286 int num_files = 0;
287 unsigned stage = 0;
288 struct ir3_shader_key key = {};
289 unsigned gpu_id = 320;
290 const char *info;
291 const char *spirv_entry = NULL;
292 void *ptr;
293 bool from_tgsi = false;
294 size_t size;
295
296 while ((opt = getopt_long_only(argc, argv, shortopts, longopts, NULL)) !=
297 -1) {
298 switch (opt) {
299 case 'g':
300 gpu_id = strtol(optarg, NULL, 0);
301 break;
302 case 'v':
303 ir3_shader_debug |= IR3_DBG_OPTMSGS | IR3_DBG_DISASM;
304 break;
305 default:
306 printf("unrecognized arg: %c\n", opt);
307 FALLTHROUGH;
308 case 'h':
309 print_usage();
310 return 0;
311 }
312 }
313
314 if (optind >= argc) {
315 fprintf(stderr, "no file specified!\n");
316 print_usage();
317 return 0;
318 }
319
320 unsigned n = optind;
321 while (n < argc) {
322 char *filename = argv[n];
323 char *ext = strrchr(filename, '.');
324
325 if (strcmp(ext, ".tgsi") == 0) {
326 if (num_files != 0)
327 errx(1, "in TGSI mode, only a single file may be specified");
328 from_tgsi = true;
329 } else if (strcmp(ext, ".spv") == 0) {
330 if (num_files != 0)
331 errx(1, "in SPIR-V mode, only a single file may be specified");
332 stage = MESA_SHADER_COMPUTE;
333 filenames[num_files++] = filename;
334 n++;
335 if (n == argc)
336 errx(1, "in SPIR-V mode, an entry point must be specified");
337 spirv_entry = argv[n];
338 n++;
339 } else if (strcmp(ext, ".comp") == 0) {
340 if (from_tgsi || spirv_entry)
341 errx(1, "cannot mix GLSL/TGSI/SPIRV");
342 if (num_files >= ARRAY_SIZE(filenames))
343 errx(1, "too many GLSL files");
344 stage = MESA_SHADER_COMPUTE;
345 } else if (strcmp(ext, ".frag") == 0) {
346 if (from_tgsi || spirv_entry)
347 errx(1, "cannot mix GLSL/TGSI/SPIRV");
348 if (num_files >= ARRAY_SIZE(filenames))
349 errx(1, "too many GLSL files");
350 stage = MESA_SHADER_FRAGMENT;
351 } else if (strcmp(ext, ".vert") == 0) {
352 if (from_tgsi)
353 errx(1, "cannot mix GLSL and TGSI");
354 if (num_files >= ARRAY_SIZE(filenames))
355 errx(1, "too many GLSL files");
356 stage = MESA_SHADER_VERTEX;
357 } else {
358 print_usage();
359 return -1;
360 }
361
362 filenames[num_files++] = filename;
363
364 n++;
365 }
366
367 nir_shader *nir;
368
369 struct fd_dev_id dev_id = {
370 .gpu_id = gpu_id,
371 };
372 compiler = ir3_compiler_create(NULL, &dev_id, fd_dev_info_raw(&dev_id),
373 &(struct ir3_compiler_options) {});
374
375 if (from_tgsi) {
376 struct tgsi_token toks[65536];
377 const nir_shader_compiler_options *nir_options =
378 ir3_get_compiler_options(compiler);
379
380 ret = read_file(filenames[0], &ptr, &size);
381 if (ret) {
382 print_usage();
383 return ret;
384 }
385
386 if (ir3_shader_debug & IR3_DBG_OPTMSGS)
387 printf("%s\n", (char *)ptr);
388
389 if (!tgsi_text_translate(ptr, toks, ARRAY_SIZE(toks)))
390 errx(1, "could not parse `%s'", filenames[0]);
391
392 if (ir3_shader_debug & IR3_DBG_OPTMSGS)
393 tgsi_dump(toks, 0);
394
395 nir = tgsi_to_nir_noscreen(toks, nir_options);
396 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
397 } else if (spirv_entry) {
398 nir = load_spirv(filenames[0], spirv_entry, stage);
399
400 NIR_PASS_V(nir, nir_lower_io, nir_var_shader_in | nir_var_shader_out,
401 ir3_glsl_type_size, (nir_lower_io_options)0);
402
403 /* TODO do this somewhere else */
404 nir_lower_int64(nir);
405 nir_lower_system_values(nir);
406 nir_lower_compute_system_values(nir, NULL);
407 } else if (num_files > 0) {
408 nir = load_glsl(num_files, filenames, stage);
409 } else {
410 print_usage();
411 return -1;
412 }
413
414 ir3_nir_lower_io_to_temporaries(nir);
415 ir3_finalize_nir(compiler, nir);
416
417 struct ir3_shader *shader = rzalloc_size(NULL, sizeof(*shader));
418 shader->compiler = compiler;
419 shader->type = stage;
420 shader->nir = nir;
421
422 ir3_nir_post_finalize(shader);
423
424 struct ir3_shader_variant *v = rzalloc_size(shader, sizeof(*v));
425 v->type = shader->type;
426 v->compiler = compiler;
427 v->key = key;
428 v->const_state = rzalloc_size(v, sizeof(*v->const_state));
429
430 shader->variants = v;
431 shader->variant_count = 1;
432
433 ir3_nir_lower_variant(v, nir);
434
435 info = "NIR compiler";
436 ret = ir3_compile_shader_nir(compiler, shader, v);
437 if (ret) {
438 fprintf(stderr, "compiler failed!\n");
439 return ret;
440 }
441 dump_info(v, info);
442
443 return 0;
444 }
445