2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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:
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
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
24 * Rob Clark <robclark@freedesktop.org>
27 #include <sys/types.h>
37 #include "nir/tgsi_to_nir.h"
38 #include "tgsi/tgsi_parse.h"
39 #include "tgsi/tgsi_text.h"
40 #include "tgsi/tgsi_dump.h"
42 #include "ir3/ir3_compiler.h"
43 #include "ir3/ir3_gallium.h"
44 #include "ir3/ir3_nir.h"
45 #include "ir3/instr-a3xx.h"
48 #include "main/mtypes.h"
50 #include "compiler/glsl/standalone.h"
51 #include "compiler/glsl/glsl_to_nir.h"
52 #include "compiler/glsl/gl_nir.h"
53 #include "compiler/nir_types.h"
54 #include "compiler/spirv/nir_spirv.h"
56 #include "pipe/p_context.h"
59 dump_info(struct ir3_shader_variant
*so
, const char *str
)
62 const char *type
= ir3_shader_stage(so
);
63 bin
= ir3_shader_assemble(so
);
64 printf("; %s: %s\n", type
, str
);
65 ir3_shader_disasm(so
, bin
, stdout
);
69 insert_sorted(struct exec_list
*var_list
, nir_variable
*new_var
)
71 nir_foreach_variable_in_list(var
, var_list
) {
72 if (var
->data
.location
> new_var
->data
.location
) {
73 exec_node_insert_node_before(&var
->node
, &new_var
->node
);
77 exec_list_push_tail(var_list
, &new_var
->node
);
81 sort_varyings(nir_shader
*nir
, nir_variable_mode mode
)
83 struct exec_list new_list
;
84 exec_list_make_empty(&new_list
);
85 nir_foreach_variable_with_modes_safe(var
, nir
, mode
) {
86 exec_node_remove(&var
->node
);
87 insert_sorted(&new_list
, var
);
89 exec_list_append(&nir
->variables
, &new_list
);
93 fixup_varying_slots(nir_shader
*nir
, nir_variable_mode mode
)
95 nir_foreach_variable_with_modes(var
, nir
, mode
) {
96 if (var
->data
.location
>= VARYING_SLOT_VAR0
) {
97 var
->data
.location
+= 9;
98 } else if ((var
->data
.location
>= VARYING_SLOT_TEX0
) &&
99 (var
->data
.location
<= VARYING_SLOT_TEX7
)) {
100 var
->data
.location
+= VARYING_SLOT_VAR0
- VARYING_SLOT_TEX0
;
105 static struct ir3_compiler
*compiler
;
108 load_glsl(unsigned num_files
, char* const* files
, gl_shader_stage stage
)
110 static const struct standalone_options options
= {
113 .lower_precision
= true,
115 struct gl_shader_program
*prog
;
116 const nir_shader_compiler_options
*nir_options
=
117 ir3_get_compiler_options(compiler
);
118 static struct gl_context local_ctx
;
120 prog
= standalone_compile_shader(&options
, num_files
, files
, &local_ctx
);
122 errx(1, "couldn't parse `%s'", files
[0]);
124 nir_shader
*nir
= glsl_to_nir(&local_ctx
, prog
, stage
, nir_options
);
126 /* required NIR passes: */
127 if (nir_options
->lower_all_io_to_temps
||
128 nir
->info
.stage
== MESA_SHADER_VERTEX
||
129 nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
130 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
131 nir_shader_get_entrypoint(nir
),
133 } else if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
134 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
135 nir_shader_get_entrypoint(nir
),
139 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
140 NIR_PASS_V(nir
, nir_split_var_copies
);
141 NIR_PASS_V(nir
, nir_lower_var_copies
);
143 NIR_PASS_V(nir
, nir_split_var_copies
);
144 NIR_PASS_V(nir
, nir_lower_var_copies
);
145 nir_print_shader(nir
, stdout
);
146 NIR_PASS_V(nir
, gl_nir_lower_atomics
, prog
, true);
147 NIR_PASS_V(nir
, gl_nir_lower_buffers
, prog
);
148 NIR_PASS_V(nir
, nir_lower_atomics_to_ssbo
);
149 nir_print_shader(nir
, stdout
);
152 case MESA_SHADER_VERTEX
:
153 nir_assign_var_locations(nir
, nir_var_shader_in
,
157 /* Re-lower global vars, to deal with any dead VS inputs. */
158 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
160 sort_varyings(nir
, nir_var_shader_out
);
161 nir_assign_var_locations(nir
, nir_var_shader_out
,
164 fixup_varying_slots(nir
, nir_var_shader_out
);
166 case MESA_SHADER_FRAGMENT
:
167 sort_varyings(nir
, nir_var_shader_in
);
168 nir_assign_var_locations(nir
, nir_var_shader_in
,
171 fixup_varying_slots(nir
, nir_var_shader_in
);
172 nir_assign_var_locations(nir
, nir_var_shader_out
,
176 case MESA_SHADER_COMPUTE
:
177 case MESA_SHADER_KERNEL
:
180 errx(1, "unhandled shader stage: %d", stage
);
183 nir_assign_var_locations(nir
, nir_var_uniform
,
187 NIR_PASS_V(nir
, nir_lower_system_values
);
188 NIR_PASS_V(nir
, nir_lower_compute_system_values
, NULL
);
190 NIR_PASS_V(nir
, nir_lower_frexp
);
191 NIR_PASS_V(nir
, nir_lower_io
,
192 nir_var_shader_in
| nir_var_shader_out
| nir_var_uniform
,
193 ir3_glsl_type_size
, (nir_lower_io_options
)0);
194 NIR_PASS_V(nir
, gl_nir_lower_samplers
, prog
);
200 read_file(const char *filename
, void **ptr
, size_t *size
)
207 fd
= open(filename
, O_RDONLY
);
209 warnx("couldn't open `%s'", filename
);
213 ret
= fstat(fd
, &st
);
215 errx(1, "couldn't stat `%s'", filename
);
218 *ptr
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
219 if (*ptr
== MAP_FAILED
)
220 errx(1, "couldn't map `%s'", filename
);
227 static void debug_func(void *priv
, enum nir_spirv_debug_level level
,
228 size_t spirv_offset
, const char *message
)
230 // printf("%s\n", message);
234 load_spirv(const char *filename
, const char *entry
, gl_shader_stage stage
)
236 const struct spirv_to_nir_options spirv_options
= {
237 /* these caps are just make-believe */
239 .draw_parameters
= true,
241 .image_read_without_format
= true,
242 .image_write_without_format
= true,
244 .variable_pointers
= true,
246 .lower_ubo_ssbo_access_to_offsets
= true,
255 read_file(filename
, &buf
, &size
);
257 nir
= spirv_to_nir(buf
, size
/ 4,
258 NULL
, 0, /* spec_entries */
261 ir3_get_compiler_options(compiler
));
263 nir_print_shader(nir
, stdout
);
268 static const char *shortopts
= "g:hv";
270 static const struct option longopts
[] = {
271 { "gpu", required_argument
, 0, 'g' },
272 { "help", no_argument
, 0, 'h' },
273 { "verbose", no_argument
, 0, 'v' },
279 printf("Usage: ir3_compiler [OPTIONS]... <file.tgsi | file.spv entry_point | (file.vert | file.frag)*>\n");
280 printf(" -g, --gpu GPU_ID - specify gpu-id (default 320)\n");
281 printf(" -h, --help - show this message\n");
282 printf(" -v, --verbose - verbose compiler/debug messages\n");
286 main(int argc
, char **argv
)
292 struct ir3_shader_key key
= {};
293 unsigned gpu_id
= 320;
295 const char *spirv_entry
= NULL
;
297 bool from_tgsi
= false;
300 while ((opt
= getopt_long_only(argc
, argv
, shortopts
, longopts
, NULL
)) != -1) {
303 gpu_id
= strtol(optarg
, NULL
, 0);
306 ir3_shader_debug
|= IR3_DBG_OPTMSGS
| IR3_DBG_DISASM
;
309 printf("unrecognized arg: %c\n", opt
);
317 if (optind
>= argc
) {
318 fprintf(stderr
, "no file specified!\n");
325 char *filename
= argv
[n
];
326 char *ext
= strrchr(filename
, '.');
328 if (strcmp(ext
, ".tgsi") == 0) {
330 errx(1, "in TGSI mode, only a single file may be specified");
332 } else if (strcmp(ext
, ".spv") == 0) {
334 errx(1, "in SPIR-V mode, only a single file may be specified");
335 stage
= MESA_SHADER_COMPUTE
;
336 filenames
[num_files
++] = filename
;
339 errx(1, "in SPIR-V mode, an entry point must be specified");
340 spirv_entry
= argv
[n
];
342 } else if (strcmp(ext
, ".comp") == 0) {
343 if (from_tgsi
|| spirv_entry
)
344 errx(1, "cannot mix GLSL/TGSI/SPIRV");
345 if (num_files
>= ARRAY_SIZE(filenames
))
346 errx(1, "too many GLSL files");
347 stage
= MESA_SHADER_COMPUTE
;
348 } else if (strcmp(ext
, ".frag") == 0) {
349 if (from_tgsi
|| spirv_entry
)
350 errx(1, "cannot mix GLSL/TGSI/SPIRV");
351 if (num_files
>= ARRAY_SIZE(filenames
))
352 errx(1, "too many GLSL files");
353 stage
= MESA_SHADER_FRAGMENT
;
354 } else if (strcmp(ext
, ".vert") == 0) {
356 errx(1, "cannot mix GLSL and TGSI");
357 if (num_files
>= ARRAY_SIZE(filenames
))
358 errx(1, "too many GLSL files");
359 stage
= MESA_SHADER_VERTEX
;
365 filenames
[num_files
++] = filename
;
372 compiler
= ir3_compiler_create(NULL
, gpu_id
);
375 struct tgsi_token toks
[65536];
376 const nir_shader_compiler_options
*nir_options
=
377 ir3_get_compiler_options(compiler
);
379 ret
= read_file(filenames
[0], &ptr
, &size
);
385 if (ir3_shader_debug
& IR3_DBG_OPTMSGS
)
386 printf("%s\n", (char *)ptr
);
388 if (!tgsi_text_translate(ptr
, toks
, ARRAY_SIZE(toks
)))
389 errx(1, "could not parse `%s'", filenames
[0]);
391 if (ir3_shader_debug
& IR3_DBG_OPTMSGS
)
394 nir
= tgsi_to_nir_noscreen(toks
, nir_options
);
395 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
396 } else if (spirv_entry
) {
397 nir
= load_spirv(filenames
[0], spirv_entry
, stage
);
399 NIR_PASS_V(nir
, nir_lower_io
,
400 nir_var_shader_in
| nir_var_shader_out
,
401 ir3_glsl_type_size
, (nir_lower_io_options
)0);
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
);
414 ir3_finalize_nir(compiler
, nir
);
415 ir3_nir_post_finalize(compiler
, nir
);
417 struct ir3_shader
*shader
= rzalloc_size(NULL
, sizeof(*shader
));
418 shader
->compiler
= compiler
;
419 shader
->type
= stage
;
422 struct ir3_shader_variant
*v
= rzalloc_size(shader
, sizeof(*v
));
423 v
->type
= shader
->type
;
426 v
->const_state
= rzalloc_size(v
, sizeof(*v
->const_state
));
428 shader
->variants
= v
;
429 shader
->variant_count
= 1;
431 ir3_nir_lower_variant(v
, nir
);
433 info
= "NIR compiler";
434 ret
= ir3_compile_shader_nir(compiler
, v
);
436 fprintf(stderr
, "compiler failed!\n");