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 "freedreno_util.h"
42 #include "ir3_compiler.h"
44 #include "instr-a3xx.h"
47 #include "compiler/glsl/standalone.h"
48 #include "compiler/glsl/glsl_to_nir.h"
49 #include "compiler/glsl/gl_nir.h"
50 #include "compiler/nir_types.h"
51 #include "compiler/spirv/nir_spirv.h"
53 static void dump_info(struct ir3_shader_variant
*so
, const char *str
)
56 const char *type
= ir3_shader_stage(so
->shader
);
57 bin
= ir3_shader_assemble(so
, so
->shader
->compiler
->gpu_id
);
58 debug_printf("; %s: %s\n", type
, str
);
59 ir3_shader_disasm(so
, bin
, stdout
);
64 insert_sorted(struct exec_list
*var_list
, nir_variable
*new_var
)
66 nir_foreach_variable(var
, var_list
) {
67 if (var
->data
.location
> new_var
->data
.location
) {
68 exec_node_insert_node_before(&var
->node
, &new_var
->node
);
72 exec_list_push_tail(var_list
, &new_var
->node
);
76 sort_varyings(struct exec_list
*var_list
)
78 struct exec_list new_list
;
79 exec_list_make_empty(&new_list
);
80 nir_foreach_variable_safe(var
, var_list
) {
81 exec_node_remove(&var
->node
);
82 insert_sorted(&new_list
, var
);
84 exec_list_move_nodes_to(&new_list
, var_list
);
88 fixup_varying_slots(struct exec_list
*var_list
)
90 nir_foreach_variable(var
, var_list
) {
91 if (var
->data
.location
>= VARYING_SLOT_VAR0
) {
92 var
->data
.location
+= 9;
93 } else if ((var
->data
.location
>= VARYING_SLOT_TEX0
) &&
94 (var
->data
.location
<= VARYING_SLOT_TEX7
)) {
95 var
->data
.location
+= VARYING_SLOT_VAR0
- VARYING_SLOT_TEX0
;
100 static struct ir3_compiler
*compiler
;
103 load_glsl(unsigned num_files
, char* const* files
, gl_shader_stage stage
)
105 static const struct standalone_options options
= {
109 struct gl_shader_program
*prog
;
110 const nir_shader_compiler_options
*nir_options
=
111 ir3_get_compiler_options(compiler
);
113 prog
= standalone_compile_shader(&options
, num_files
, files
);
115 errx(1, "couldn't parse `%s'", files
[0]);
117 nir_shader
*nir
= glsl_to_nir(prog
, stage
, nir_options
);
119 /* required NIR passes: */
120 if (nir_options
->lower_all_io_to_temps
||
121 nir
->info
.stage
== MESA_SHADER_VERTEX
||
122 nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
123 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
124 nir_shader_get_entrypoint(nir
),
126 } else if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
127 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
128 nir_shader_get_entrypoint(nir
),
132 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
133 NIR_PASS_V(nir
, nir_split_var_copies
);
134 NIR_PASS_V(nir
, nir_lower_var_copies
);
136 NIR_PASS_V(nir
, nir_split_var_copies
);
137 NIR_PASS_V(nir
, nir_lower_var_copies
);
138 nir_print_shader(nir
, stdout
);
139 NIR_PASS_V(nir
, gl_nir_lower_atomics
, prog
, true);
140 NIR_PASS_V(nir
, nir_lower_atomics_to_ssbo
, 8);
141 nir_print_shader(nir
, stdout
);
144 case MESA_SHADER_VERTEX
:
145 nir_assign_var_locations(&nir
->inputs
,
149 /* Re-lower global vars, to deal with any dead VS inputs. */
150 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
152 sort_varyings(&nir
->outputs
);
153 nir_assign_var_locations(&nir
->outputs
,
156 fixup_varying_slots(&nir
->outputs
);
158 case MESA_SHADER_FRAGMENT
:
159 sort_varyings(&nir
->inputs
);
160 nir_assign_var_locations(&nir
->inputs
,
163 fixup_varying_slots(&nir
->inputs
);
164 nir_assign_var_locations(&nir
->outputs
,
168 case MESA_SHADER_COMPUTE
:
171 errx(1, "unhandled shader stage: %d", stage
);
174 nir_assign_var_locations(&nir
->uniforms
,
178 NIR_PASS_V(nir
, nir_lower_system_values
);
179 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, ir3_glsl_type_size
, 0);
180 NIR_PASS_V(nir
, gl_nir_lower_samplers
, prog
);
186 read_file(const char *filename
, void **ptr
, size_t *size
)
193 fd
= open(filename
, O_RDONLY
);
195 warnx("couldn't open `%s'", filename
);
199 ret
= fstat(fd
, &st
);
201 errx(1, "couldn't stat `%s'", filename
);
204 *ptr
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
205 if (*ptr
== MAP_FAILED
)
206 errx(1, "couldn't map `%s'", filename
);
213 static void debug_func(void *priv
, enum nir_spirv_debug_level level
,
214 size_t spirv_offset
, const char *message
)
216 // printf("%s\n", message);
220 load_spirv(const char *filename
, const char *entry
, gl_shader_stage stage
)
222 const struct spirv_to_nir_options spirv_options
= {
223 /* these caps are just make-believe */
225 .draw_parameters
= true,
227 .image_read_without_format
= true,
228 .image_write_without_format
= true,
230 .variable_pointers
= true,
232 .lower_workgroup_access_to_offsets
= true,
237 nir_function
*entry_point
;
241 read_file(filename
, &buf
, &size
);
243 entry_point
= spirv_to_nir(buf
, size
/ 4,
244 NULL
, 0, /* spec_entries */
247 ir3_get_compiler_options(compiler
));
249 nir_print_shader(entry_point
->shader
, stdout
);
251 return entry_point
->shader
;
254 static void print_usage(void)
256 printf("Usage: ir3_compiler [OPTIONS]... <file.tgsi | file.spv entry_point | (file.vert | file.frag)*>\n");
257 printf(" --verbose - verbose compiler/debug messages\n");
258 printf(" --binning-pass - generate binning pass shader (VERT)\n");
259 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
260 printf(" --half-precision - use half-precision\n");
261 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
262 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
263 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
264 printf(" --astc-srgb MASK - bitmask of samplers to enable astc-srgb workaround\n");
265 printf(" --stream-out - enable stream-out (aka transform feedback)\n");
266 printf(" --ucp MASK - bitmask of enabled user-clip-planes\n");
267 printf(" --gpu GPU_ID - specify gpu-id (default 320)\n");
268 printf(" --help - show this message\n");
271 int main(int argc
, char **argv
)
277 struct ir3_shader_variant v
;
279 struct ir3_shader_key key
= {};
280 /* TODO cmdline option to target different gpus: */
281 unsigned gpu_id
= 320;
285 bool from_spirv
= false;
288 memset(&s
, 0, sizeof(s
));
289 memset(&v
, 0, sizeof(v
));
291 /* cmdline args which impact shader variant get spit out in a
292 * comment on the first line.. a quick/dirty way to preserve
293 * that info so when ir3test recompiles the shader with a new
294 * compiler version, we use the same shader-key settings:
296 debug_printf("; options:");
299 if (!strcmp(argv
[n
], "--verbose")) {
300 fd_mesa_debug
|= FD_DBG_MSGS
| FD_DBG_OPTMSGS
| FD_DBG_DISASM
;
305 if (!strcmp(argv
[n
], "--binning-pass")) {
306 debug_printf(" %s", argv
[n
]);
307 v
.binning_pass
= true;
312 if (!strcmp(argv
[n
], "--color-two-side")) {
313 debug_printf(" %s", argv
[n
]);
314 key
.color_two_side
= true;
319 if (!strcmp(argv
[n
], "--half-precision")) {
320 debug_printf(" %s", argv
[n
]);
321 key
.half_precision
= true;
326 if (!strcmp(argv
[n
], "--saturate-s")) {
327 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
328 key
.vsaturate_s
= key
.fsaturate_s
= strtol(argv
[n
+1], NULL
, 0);
333 if (!strcmp(argv
[n
], "--saturate-t")) {
334 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
335 key
.vsaturate_t
= key
.fsaturate_t
= strtol(argv
[n
+1], NULL
, 0);
340 if (!strcmp(argv
[n
], "--saturate-r")) {
341 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
342 key
.vsaturate_r
= key
.fsaturate_r
= strtol(argv
[n
+1], NULL
, 0);
347 if (!strcmp(argv
[n
], "--astc-srgb")) {
348 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
349 key
.vastc_srgb
= key
.fastc_srgb
= strtol(argv
[n
+1], NULL
, 0);
354 if (!strcmp(argv
[n
], "--stream-out")) {
355 struct pipe_stream_output_info
*so
= &s
.stream_output
;
356 debug_printf(" %s", argv
[n
]);
357 /* TODO more dynamic config based on number of outputs, etc
358 * rather than just hard-code for first output:
362 so
->output
[0].register_index
= 0;
363 so
->output
[0].start_component
= 0;
364 so
->output
[0].num_components
= 4;
365 so
->output
[0].output_buffer
= 0;
366 so
->output
[0].dst_offset
= 2;
367 so
->output
[0].stream
= 0;
372 if (!strcmp(argv
[n
], "--ucp")) {
373 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
374 key
.ucp_enables
= strtol(argv
[n
+1], NULL
, 0);
379 if (!strcmp(argv
[n
], "--gpu")) {
380 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
381 gpu_id
= strtol(argv
[n
+1], NULL
, 0);
386 if (!strcmp(argv
[n
], "--help")) {
396 char *filename
= argv
[n
];
397 char *ext
= strrchr(filename
, '.');
399 if (strcmp(ext
, ".tgsi") == 0) {
401 errx(1, "in TGSI mode, only a single file may be specified");
403 } else if (strcmp(ext
, ".spv") == 0) {
405 errx(1, "in SPIR-V mode, only a single file may be specified");
406 stage
= MESA_SHADER_COMPUTE
;
408 filenames
[num_files
++] = filename
;
411 errx(1, "in SPIR-V mode, an entry point must be specified");
414 } else if (strcmp(ext
, ".comp") == 0) {
415 if (s
.from_tgsi
|| from_spirv
)
416 errx(1, "cannot mix GLSL/TGSI/SPIRV");
417 if (num_files
>= ARRAY_SIZE(filenames
))
418 errx(1, "too many GLSL files");
419 stage
= MESA_SHADER_COMPUTE
;
420 } else if (strcmp(ext
, ".frag") == 0) {
421 if (s
.from_tgsi
|| from_spirv
)
422 errx(1, "cannot mix GLSL/TGSI/SPIRV");
423 if (num_files
>= ARRAY_SIZE(filenames
))
424 errx(1, "too many GLSL files");
425 stage
= MESA_SHADER_FRAGMENT
;
426 } else if (strcmp(ext
, ".vert") == 0) {
428 errx(1, "cannot mix GLSL and TGSI");
429 if (num_files
>= ARRAY_SIZE(filenames
))
430 errx(1, "too many GLSL files");
431 stage
= MESA_SHADER_VERTEX
;
437 filenames
[num_files
++] = filename
;
444 compiler
= ir3_compiler_create(NULL
, gpu_id
);
447 struct tgsi_token toks
[65536];
449 ret
= read_file(filenames
[0], &ptr
, &size
);
455 if (fd_mesa_debug
& FD_DBG_OPTMSGS
)
456 debug_printf("%s\n", (char *)ptr
);
458 if (!tgsi_text_translate(ptr
, toks
, ARRAY_SIZE(toks
)))
459 errx(1, "could not parse `%s'", filenames
[0]);
461 if (fd_mesa_debug
& FD_DBG_OPTMSGS
)
464 nir
= ir3_tgsi_to_nir(toks
);
465 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
466 } else if (from_spirv
) {
467 nir
= load_spirv(filenames
[0], entry
, stage
);
469 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, ir3_glsl_type_size
,
470 (nir_lower_io_options
)0);
472 /* TODO do this somewhere else */
473 nir_lower_int64(nir
, ~0);
474 nir_lower_system_values(nir
);
475 } else if (num_files
> 0) {
476 nir
= load_glsl(num_files
, filenames
, stage
);
482 s
.compiler
= compiler
;
483 s
.nir
= ir3_optimize_nir(&s
, nir
, NULL
);
487 s
.type
= v
.type
= nir
->info
.stage
;
489 info
= "NIR compiler";
490 ret
= ir3_compile_shader_nir(s
.compiler
, &v
);
492 fprintf(stderr
, "compiler failed!\n");