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/nir_types.h"
50 #include "compiler/spirv/nir_spirv.h"
52 static void dump_info(struct ir3_shader_variant
*so
, const char *str
)
55 const char *type
= ir3_shader_stage(so
->shader
);
56 bin
= ir3_shader_assemble(so
, so
->shader
->compiler
->gpu_id
);
57 debug_printf("; %s: %s\n", type
, str
);
58 ir3_shader_disasm(so
, bin
);
63 insert_sorted(struct exec_list
*var_list
, nir_variable
*new_var
)
65 nir_foreach_variable(var
, var_list
) {
66 if (var
->data
.location
> new_var
->data
.location
) {
67 exec_node_insert_node_before(&var
->node
, &new_var
->node
);
71 exec_list_push_tail(var_list
, &new_var
->node
);
75 sort_varyings(struct exec_list
*var_list
)
77 struct exec_list new_list
;
78 exec_list_make_empty(&new_list
);
79 nir_foreach_variable_safe(var
, var_list
) {
80 exec_node_remove(&var
->node
);
81 insert_sorted(&new_list
, var
);
83 exec_list_move_nodes_to(&new_list
, var_list
);
87 fixup_varying_slots(struct exec_list
*var_list
)
89 nir_foreach_variable(var
, var_list
) {
90 if (var
->data
.location
>= VARYING_SLOT_VAR0
) {
91 var
->data
.location
+= 9;
92 } else if ((var
->data
.location
>= VARYING_SLOT_TEX0
) &&
93 (var
->data
.location
<= VARYING_SLOT_TEX7
)) {
94 var
->data
.location
+= VARYING_SLOT_VAR0
- VARYING_SLOT_TEX0
;
99 static struct ir3_compiler
*compiler
;
102 load_glsl(unsigned num_files
, char* const* files
, gl_shader_stage stage
)
104 static const struct standalone_options options
= {
108 struct gl_shader_program
*prog
;
110 prog
= standalone_compile_shader(&options
, num_files
, files
);
112 errx(1, "couldn't parse `%s'", files
[0]);
114 nir_shader
*nir
= glsl_to_nir(prog
, stage
, ir3_get_compiler_options(compiler
));
116 /* required NIR passes: */
117 /* TODO cmdline args for some of the conditional lowering passes? */
119 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
120 nir_shader_get_entrypoint(nir
),
122 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
123 NIR_PASS_V(nir
, nir_split_var_copies
);
124 NIR_PASS_V(nir
, nir_lower_var_copies
);
126 NIR_PASS_V(nir
, nir_split_var_copies
);
127 NIR_PASS_V(nir
, nir_lower_var_copies
);
128 NIR_PASS_V(nir
, nir_lower_io_types
);
131 case MESA_SHADER_VERTEX
:
132 nir_assign_var_locations(&nir
->inputs
,
136 /* Re-lower global vars, to deal with any dead VS inputs. */
137 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
139 sort_varyings(&nir
->outputs
);
140 nir_assign_var_locations(&nir
->outputs
,
143 fixup_varying_slots(&nir
->outputs
);
145 case MESA_SHADER_FRAGMENT
:
146 sort_varyings(&nir
->inputs
);
147 nir_assign_var_locations(&nir
->inputs
,
150 fixup_varying_slots(&nir
->inputs
);
151 nir_assign_var_locations(&nir
->outputs
,
156 errx(1, "unhandled shader stage: %d", stage
);
159 nir_assign_var_locations(&nir
->uniforms
,
163 NIR_PASS_V(nir
, nir_lower_system_values
);
164 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, ir3_glsl_type_size
, 0);
165 NIR_PASS_V(nir
, nir_lower_samplers
, prog
);
171 read_file(const char *filename
, void **ptr
, size_t *size
)
178 fd
= open(filename
, O_RDONLY
);
180 warnx("couldn't open `%s'", filename
);
184 ret
= fstat(fd
, &st
);
186 errx(1, "couldn't stat `%s'", filename
);
189 *ptr
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
190 if (*ptr
== MAP_FAILED
)
191 errx(1, "couldn't map `%s'", filename
);
198 static void debug_func(void *priv
, enum nir_spirv_debug_level level
,
199 size_t spirv_offset
, const char *message
)
201 // printf("%s\n", message);
205 load_spirv(const char *filename
, const char *entry
, gl_shader_stage stage
)
207 const struct spirv_to_nir_options spirv_options
= {
208 /* these caps are just make-believe */
210 .draw_parameters
= true,
212 .image_read_without_format
= true,
213 .image_write_without_format
= true,
215 .variable_pointers
= true,
217 .lower_workgroup_access_to_offsets
= true,
222 nir_function
*entry_point
;
226 read_file(filename
, &buf
, &size
);
228 entry_point
= spirv_to_nir(buf
, size
/ 4,
229 NULL
, 0, /* spec_entries */
232 ir3_get_compiler_options(compiler
));
234 nir_print_shader(entry_point
->shader
, stdout
);
236 return entry_point
->shader
;
239 static void print_usage(void)
241 printf("Usage: ir3_compiler [OPTIONS]... <file.tgsi | file.spv entry_point | (file.vert | file.frag)*>\n");
242 printf(" --verbose - verbose compiler/debug messages\n");
243 printf(" --binning-pass - generate binning pass shader (VERT)\n");
244 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
245 printf(" --half-precision - use half-precision\n");
246 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
247 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
248 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
249 printf(" --astc-srgb MASK - bitmask of samplers to enable astc-srgb workaround\n");
250 printf(" --stream-out - enable stream-out (aka transform feedback)\n");
251 printf(" --ucp MASK - bitmask of enabled user-clip-planes\n");
252 printf(" --gpu GPU_ID - specify gpu-id (default 320)\n");
253 printf(" --help - show this message\n");
256 int main(int argc
, char **argv
)
262 struct ir3_shader_variant v
;
264 struct ir3_shader_key key
= {};
265 /* TODO cmdline option to target different gpus: */
266 unsigned gpu_id
= 320;
270 bool from_spirv
= false;
273 memset(&s
, 0, sizeof(s
));
274 memset(&v
, 0, sizeof(v
));
276 /* cmdline args which impact shader variant get spit out in a
277 * comment on the first line.. a quick/dirty way to preserve
278 * that info so when ir3test recompiles the shader with a new
279 * compiler version, we use the same shader-key settings:
281 debug_printf("; options:");
284 if (!strcmp(argv
[n
], "--verbose")) {
285 fd_mesa_debug
|= FD_DBG_MSGS
| FD_DBG_OPTMSGS
| FD_DBG_DISASM
;
290 if (!strcmp(argv
[n
], "--binning-pass")) {
291 debug_printf(" %s", argv
[n
]);
292 key
.binning_pass
= true;
297 if (!strcmp(argv
[n
], "--color-two-side")) {
298 debug_printf(" %s", argv
[n
]);
299 key
.color_two_side
= true;
304 if (!strcmp(argv
[n
], "--half-precision")) {
305 debug_printf(" %s", argv
[n
]);
306 key
.half_precision
= true;
311 if (!strcmp(argv
[n
], "--saturate-s")) {
312 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
313 key
.vsaturate_s
= key
.fsaturate_s
= strtol(argv
[n
+1], NULL
, 0);
318 if (!strcmp(argv
[n
], "--saturate-t")) {
319 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
320 key
.vsaturate_t
= key
.fsaturate_t
= strtol(argv
[n
+1], NULL
, 0);
325 if (!strcmp(argv
[n
], "--saturate-r")) {
326 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
327 key
.vsaturate_r
= key
.fsaturate_r
= strtol(argv
[n
+1], NULL
, 0);
332 if (!strcmp(argv
[n
], "--astc-srgb")) {
333 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
334 key
.vastc_srgb
= key
.fastc_srgb
= strtol(argv
[n
+1], NULL
, 0);
339 if (!strcmp(argv
[n
], "--stream-out")) {
340 struct pipe_stream_output_info
*so
= &s
.stream_output
;
341 debug_printf(" %s", argv
[n
]);
342 /* TODO more dynamic config based on number of outputs, etc
343 * rather than just hard-code for first output:
347 so
->output
[0].register_index
= 0;
348 so
->output
[0].start_component
= 0;
349 so
->output
[0].num_components
= 4;
350 so
->output
[0].output_buffer
= 0;
351 so
->output
[0].dst_offset
= 2;
352 so
->output
[0].stream
= 0;
357 if (!strcmp(argv
[n
], "--ucp")) {
358 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
359 key
.ucp_enables
= strtol(argv
[n
+1], NULL
, 0);
364 if (!strcmp(argv
[n
], "--gpu")) {
365 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
366 gpu_id
= strtol(argv
[n
+1], NULL
, 0);
371 if (!strcmp(argv
[n
], "--help")) {
381 char *filename
= argv
[n
];
382 char *ext
= rindex(filename
, '.');
384 if (strcmp(ext
, ".tgsi") == 0) {
386 errx(1, "in TGSI mode, only a single file may be specified");
388 } else if (strcmp(ext
, ".spv") == 0) {
390 errx(1, "in SPIR-V mode, only a single file may be specified");
391 stage
= MESA_SHADER_COMPUTE
;
393 filenames
[num_files
++] = filename
;
396 errx(1, "in SPIR-V mode, an entry point must be specified");
399 } else if (strcmp(ext
, ".frag") == 0) {
400 if (s
.from_tgsi
|| from_spirv
)
401 errx(1, "cannot mix GLSL/TGSI/SPIRV");
402 if (num_files
>= ARRAY_SIZE(filenames
))
403 errx(1, "too many GLSL files");
404 stage
= MESA_SHADER_FRAGMENT
;
405 } else if (strcmp(ext
, ".vert") == 0) {
407 errx(1, "cannot mix GLSL and TGSI");
408 if (num_files
>= ARRAY_SIZE(filenames
))
409 errx(1, "too many GLSL files");
410 stage
= MESA_SHADER_VERTEX
;
416 filenames
[num_files
++] = filename
;
423 compiler
= ir3_compiler_create(NULL
, gpu_id
);
426 struct tgsi_token toks
[65536];
428 ret
= read_file(filenames
[0], &ptr
, &size
);
434 if (fd_mesa_debug
& FD_DBG_OPTMSGS
)
435 debug_printf("%s\n", (char *)ptr
);
437 if (!tgsi_text_translate(ptr
, toks
, ARRAY_SIZE(toks
)))
438 errx(1, "could not parse `%s'", filenames
[0]);
440 if (fd_mesa_debug
& FD_DBG_OPTMSGS
)
443 nir
= ir3_tgsi_to_nir(toks
);
444 } else if (from_spirv
) {
445 nir
= load_spirv(filenames
[0], entry
, stage
);
446 } else if (num_files
> 0) {
447 nir
= load_glsl(num_files
, filenames
, stage
);
453 s
.compiler
= compiler
;
454 s
.nir
= ir3_optimize_nir(&s
, nir
, NULL
);
459 switch (nir
->info
.stage
) {
460 case MESA_SHADER_FRAGMENT
:
461 s
.type
= v
.type
= SHADER_FRAGMENT
;
463 case MESA_SHADER_VERTEX
:
464 s
.type
= v
.type
= SHADER_VERTEX
;
466 case MESA_SHADER_COMPUTE
:
467 s
.type
= v
.type
= SHADER_COMPUTE
;
470 errx(1, "unhandled shader stage: %d", nir
->info
.stage
);
473 info
= "NIR compiler";
474 ret
= ir3_compile_shader_nir(s
.compiler
, &v
);
476 fprintf(stderr
, "compiler failed!\n");