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>
36 #include "tgsi/tgsi_parse.h"
37 #include "tgsi/tgsi_text.h"
38 #include "tgsi/tgsi_dump.h"
40 #include "ir3_compiler.h"
42 #include "instr-a3xx.h"
45 #include "compiler/glsl/standalone.h"
46 #include "compiler/glsl/glsl_to_nir.h"
47 #include "compiler/glsl/gl_nir.h"
48 #include "compiler/nir_types.h"
49 #include "compiler/spirv/nir_spirv.h"
51 static void dump_info(struct ir3_shader_variant
*so
, const char *str
)
54 const char *type
= ir3_shader_stage(so
->shader
);
55 bin
= ir3_shader_assemble(so
, so
->shader
->compiler
->gpu_id
);
56 debug_printf("; %s: %s\n", type
, str
);
57 ir3_shader_disasm(so
, bin
, stdout
);
62 insert_sorted(struct exec_list
*var_list
, nir_variable
*new_var
)
64 nir_foreach_variable(var
, var_list
) {
65 if (var
->data
.location
> new_var
->data
.location
) {
66 exec_node_insert_node_before(&var
->node
, &new_var
->node
);
70 exec_list_push_tail(var_list
, &new_var
->node
);
74 sort_varyings(struct exec_list
*var_list
)
76 struct exec_list new_list
;
77 exec_list_make_empty(&new_list
);
78 nir_foreach_variable_safe(var
, var_list
) {
79 exec_node_remove(&var
->node
);
80 insert_sorted(&new_list
, var
);
82 exec_list_move_nodes_to(&new_list
, var_list
);
86 fixup_varying_slots(struct exec_list
*var_list
)
88 nir_foreach_variable(var
, var_list
) {
89 if (var
->data
.location
>= VARYING_SLOT_VAR0
) {
90 var
->data
.location
+= 9;
91 } else if ((var
->data
.location
>= VARYING_SLOT_TEX0
) &&
92 (var
->data
.location
<= VARYING_SLOT_TEX7
)) {
93 var
->data
.location
+= VARYING_SLOT_VAR0
- VARYING_SLOT_TEX0
;
98 static struct ir3_compiler
*compiler
;
101 load_glsl(unsigned num_files
, char* const* files
, gl_shader_stage stage
)
103 static const struct standalone_options options
= {
107 struct gl_shader_program
*prog
;
108 const nir_shader_compiler_options
*nir_options
=
109 ir3_get_compiler_options(compiler
);
111 prog
= standalone_compile_shader(&options
, num_files
, files
);
113 errx(1, "couldn't parse `%s'", files
[0]);
115 nir_shader
*nir
= glsl_to_nir(prog
, stage
, nir_options
);
117 /* required NIR passes: */
118 if (nir_options
->lower_all_io_to_temps
||
119 nir
->info
.stage
== MESA_SHADER_VERTEX
||
120 nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
121 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
122 nir_shader_get_entrypoint(nir
),
124 } else if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
125 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
126 nir_shader_get_entrypoint(nir
),
130 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
131 NIR_PASS_V(nir
, nir_split_var_copies
);
132 NIR_PASS_V(nir
, nir_lower_var_copies
);
134 NIR_PASS_V(nir
, nir_split_var_copies
);
135 NIR_PASS_V(nir
, nir_lower_var_copies
);
136 nir_print_shader(nir
, stdout
);
137 NIR_PASS_V(nir
, gl_nir_lower_atomics
, prog
, true);
138 NIR_PASS_V(nir
, nir_lower_atomics_to_ssbo
, 8);
139 nir_print_shader(nir
, stdout
);
142 case MESA_SHADER_VERTEX
:
143 nir_assign_var_locations(&nir
->inputs
,
147 /* Re-lower global vars, to deal with any dead VS inputs. */
148 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
150 sort_varyings(&nir
->outputs
);
151 nir_assign_var_locations(&nir
->outputs
,
154 fixup_varying_slots(&nir
->outputs
);
156 case MESA_SHADER_FRAGMENT
:
157 sort_varyings(&nir
->inputs
);
158 nir_assign_var_locations(&nir
->inputs
,
161 fixup_varying_slots(&nir
->inputs
);
162 nir_assign_var_locations(&nir
->outputs
,
166 case MESA_SHADER_COMPUTE
:
169 errx(1, "unhandled shader stage: %d", stage
);
172 nir_assign_var_locations(&nir
->uniforms
,
176 NIR_PASS_V(nir
, nir_lower_system_values
);
177 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, ir3_glsl_type_size
, 0);
178 NIR_PASS_V(nir
, gl_nir_lower_samplers
, prog
);
184 read_file(const char *filename
, void **ptr
, size_t *size
)
191 fd
= open(filename
, O_RDONLY
);
193 warnx("couldn't open `%s'", filename
);
197 ret
= fstat(fd
, &st
);
199 errx(1, "couldn't stat `%s'", filename
);
202 *ptr
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
203 if (*ptr
== MAP_FAILED
)
204 errx(1, "couldn't map `%s'", filename
);
211 static void debug_func(void *priv
, enum nir_spirv_debug_level level
,
212 size_t spirv_offset
, const char *message
)
214 // printf("%s\n", message);
218 load_spirv(const char *filename
, const char *entry
, gl_shader_stage stage
)
220 const struct spirv_to_nir_options spirv_options
= {
221 /* these caps are just make-believe */
223 .draw_parameters
= true,
225 .image_read_without_format
= true,
226 .image_write_without_format
= true,
228 .variable_pointers
= true,
230 .lower_workgroup_access_to_offsets
= true,
235 nir_function
*entry_point
;
239 read_file(filename
, &buf
, &size
);
241 entry_point
= spirv_to_nir(buf
, size
/ 4,
242 NULL
, 0, /* spec_entries */
245 ir3_get_compiler_options(compiler
));
247 nir_print_shader(entry_point
->shader
, stdout
);
249 return entry_point
->shader
;
252 static void print_usage(void)
254 printf("Usage: ir3_compiler [OPTIONS]... <file.tgsi | file.spv entry_point | (file.vert | file.frag)*>\n");
255 printf(" --verbose - verbose compiler/debug messages\n");
256 printf(" --binning-pass - generate binning pass shader (VERT)\n");
257 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
258 printf(" --half-precision - use half-precision\n");
259 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
260 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
261 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
262 printf(" --astc-srgb MASK - bitmask of samplers to enable astc-srgb workaround\n");
263 printf(" --stream-out - enable stream-out (aka transform feedback)\n");
264 printf(" --ucp MASK - bitmask of enabled user-clip-planes\n");
265 printf(" --gpu GPU_ID - specify gpu-id (default 320)\n");
266 printf(" --help - show this message\n");
269 int main(int argc
, char **argv
)
275 struct ir3_shader_variant v
;
277 struct ir3_shader_key key
= {};
278 /* TODO cmdline option to target different gpus: */
279 unsigned gpu_id
= 320;
283 bool from_spirv
= false;
286 memset(&s
, 0, sizeof(s
));
287 memset(&v
, 0, sizeof(v
));
289 /* cmdline args which impact shader variant get spit out in a
290 * comment on the first line.. a quick/dirty way to preserve
291 * that info so when ir3test recompiles the shader with a new
292 * compiler version, we use the same shader-key settings:
294 debug_printf("; options:");
297 if (!strcmp(argv
[n
], "--verbose")) {
298 ir3_shader_debug
|= IR3_DBG_OPTMSGS
| IR3_DBG_DISASM
;
303 if (!strcmp(argv
[n
], "--binning-pass")) {
304 debug_printf(" %s", argv
[n
]);
305 v
.binning_pass
= true;
310 if (!strcmp(argv
[n
], "--color-two-side")) {
311 debug_printf(" %s", argv
[n
]);
312 key
.color_two_side
= true;
317 if (!strcmp(argv
[n
], "--half-precision")) {
318 debug_printf(" %s", argv
[n
]);
319 key
.half_precision
= true;
324 if (!strcmp(argv
[n
], "--saturate-s")) {
325 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
326 key
.vsaturate_s
= key
.fsaturate_s
= strtol(argv
[n
+1], NULL
, 0);
331 if (!strcmp(argv
[n
], "--saturate-t")) {
332 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
333 key
.vsaturate_t
= key
.fsaturate_t
= strtol(argv
[n
+1], NULL
, 0);
338 if (!strcmp(argv
[n
], "--saturate-r")) {
339 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
340 key
.vsaturate_r
= key
.fsaturate_r
= strtol(argv
[n
+1], NULL
, 0);
345 if (!strcmp(argv
[n
], "--astc-srgb")) {
346 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
347 key
.vastc_srgb
= key
.fastc_srgb
= strtol(argv
[n
+1], NULL
, 0);
352 if (!strcmp(argv
[n
], "--stream-out")) {
353 struct ir3_stream_output_info
*so
= &s
.stream_output
;
354 debug_printf(" %s", argv
[n
]);
355 /* TODO more dynamic config based on number of outputs, etc
356 * rather than just hard-code for first output:
360 so
->output
[0].register_index
= 0;
361 so
->output
[0].start_component
= 0;
362 so
->output
[0].num_components
= 4;
363 so
->output
[0].output_buffer
= 0;
364 so
->output
[0].dst_offset
= 2;
365 so
->output
[0].stream
= 0;
370 if (!strcmp(argv
[n
], "--ucp")) {
371 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
372 key
.ucp_enables
= strtol(argv
[n
+1], NULL
, 0);
377 if (!strcmp(argv
[n
], "--gpu")) {
378 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
379 gpu_id
= strtol(argv
[n
+1], NULL
, 0);
384 if (!strcmp(argv
[n
], "--help")) {
394 char *filename
= argv
[n
];
395 char *ext
= strrchr(filename
, '.');
397 if (strcmp(ext
, ".tgsi") == 0) {
399 errx(1, "in TGSI mode, only a single file may be specified");
401 } else if (strcmp(ext
, ".spv") == 0) {
403 errx(1, "in SPIR-V mode, only a single file may be specified");
404 stage
= MESA_SHADER_COMPUTE
;
406 filenames
[num_files
++] = filename
;
409 errx(1, "in SPIR-V mode, an entry point must be specified");
412 } else if (strcmp(ext
, ".comp") == 0) {
413 if (s
.from_tgsi
|| from_spirv
)
414 errx(1, "cannot mix GLSL/TGSI/SPIRV");
415 if (num_files
>= ARRAY_SIZE(filenames
))
416 errx(1, "too many GLSL files");
417 stage
= MESA_SHADER_COMPUTE
;
418 } else if (strcmp(ext
, ".frag") == 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_FRAGMENT
;
424 } else if (strcmp(ext
, ".vert") == 0) {
426 errx(1, "cannot mix GLSL and TGSI");
427 if (num_files
>= ARRAY_SIZE(filenames
))
428 errx(1, "too many GLSL files");
429 stage
= MESA_SHADER_VERTEX
;
435 filenames
[num_files
++] = filename
;
442 compiler
= ir3_compiler_create(NULL
, gpu_id
);
445 struct tgsi_token toks
[65536];
447 ret
= read_file(filenames
[0], &ptr
, &size
);
453 if (ir3_shader_debug
& IR3_DBG_OPTMSGS
)
454 debug_printf("%s\n", (char *)ptr
);
456 if (!tgsi_text_translate(ptr
, toks
, ARRAY_SIZE(toks
)))
457 errx(1, "could not parse `%s'", filenames
[0]);
459 if (ir3_shader_debug
& IR3_DBG_OPTMSGS
)
462 nir
= ir3_tgsi_to_nir(toks
);
463 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
464 } else if (from_spirv
) {
465 nir
= load_spirv(filenames
[0], entry
, stage
);
467 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, ir3_glsl_type_size
,
468 (nir_lower_io_options
)0);
470 /* TODO do this somewhere else */
471 nir_lower_int64(nir
, ~0);
472 nir_lower_system_values(nir
);
473 } else if (num_files
> 0) {
474 nir
= load_glsl(num_files
, filenames
, stage
);
480 s
.compiler
= compiler
;
481 s
.nir
= ir3_optimize_nir(&s
, nir
, NULL
);
485 s
.type
= v
.type
= nir
->info
.stage
;
487 info
= "NIR compiler";
488 ret
= ir3_compile_shader_nir(s
.compiler
, &v
);
490 fprintf(stderr
, "compiler failed!\n");