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
);
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
;
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
, ir3_get_compiler_options(compiler
));
117 /* required NIR passes: */
118 /* TODO cmdline args for some of the conditional lowering passes? */
120 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
121 nir_shader_get_entrypoint(nir
),
123 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
124 NIR_PASS_V(nir
, nir_split_var_copies
);
125 NIR_PASS_V(nir
, nir_lower_var_copies
);
127 NIR_PASS_V(nir
, nir_split_var_copies
);
128 NIR_PASS_V(nir
, nir_lower_var_copies
);
129 NIR_PASS_V(nir
, nir_lower_io_types
);
132 case MESA_SHADER_VERTEX
:
133 nir_assign_var_locations(&nir
->inputs
,
137 /* Re-lower global vars, to deal with any dead VS inputs. */
138 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
140 sort_varyings(&nir
->outputs
);
141 nir_assign_var_locations(&nir
->outputs
,
144 fixup_varying_slots(&nir
->outputs
);
146 case MESA_SHADER_FRAGMENT
:
147 sort_varyings(&nir
->inputs
);
148 nir_assign_var_locations(&nir
->inputs
,
151 fixup_varying_slots(&nir
->inputs
);
152 nir_assign_var_locations(&nir
->outputs
,
157 errx(1, "unhandled shader stage: %d", stage
);
160 nir_assign_var_locations(&nir
->uniforms
,
164 NIR_PASS_V(nir
, nir_lower_system_values
);
165 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, ir3_glsl_type_size
, 0);
166 NIR_PASS_V(nir
, gl_nir_lower_samplers
, prog
);
172 read_file(const char *filename
, void **ptr
, size_t *size
)
179 fd
= open(filename
, O_RDONLY
);
181 warnx("couldn't open `%s'", filename
);
185 ret
= fstat(fd
, &st
);
187 errx(1, "couldn't stat `%s'", filename
);
190 *ptr
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
191 if (*ptr
== MAP_FAILED
)
192 errx(1, "couldn't map `%s'", filename
);
199 static void debug_func(void *priv
, enum nir_spirv_debug_level level
,
200 size_t spirv_offset
, const char *message
)
202 // printf("%s\n", message);
206 load_spirv(const char *filename
, const char *entry
, gl_shader_stage stage
)
208 const struct spirv_to_nir_options spirv_options
= {
209 /* these caps are just make-believe */
211 .draw_parameters
= true,
213 .image_read_without_format
= true,
214 .image_write_without_format
= true,
216 .variable_pointers
= true,
218 .lower_workgroup_access_to_offsets
= true,
223 nir_function
*entry_point
;
227 read_file(filename
, &buf
, &size
);
229 entry_point
= spirv_to_nir(buf
, size
/ 4,
230 NULL
, 0, /* spec_entries */
233 ir3_get_compiler_options(compiler
));
235 nir_print_shader(entry_point
->shader
, stdout
);
237 return entry_point
->shader
;
240 static void print_usage(void)
242 printf("Usage: ir3_compiler [OPTIONS]... <file.tgsi | file.spv entry_point | (file.vert | file.frag)*>\n");
243 printf(" --verbose - verbose compiler/debug messages\n");
244 printf(" --binning-pass - generate binning pass shader (VERT)\n");
245 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
246 printf(" --half-precision - use half-precision\n");
247 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
248 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
249 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
250 printf(" --astc-srgb MASK - bitmask of samplers to enable astc-srgb workaround\n");
251 printf(" --stream-out - enable stream-out (aka transform feedback)\n");
252 printf(" --ucp MASK - bitmask of enabled user-clip-planes\n");
253 printf(" --gpu GPU_ID - specify gpu-id (default 320)\n");
254 printf(" --help - show this message\n");
257 int main(int argc
, char **argv
)
263 struct ir3_shader_variant v
;
265 struct ir3_shader_key key
= {};
266 /* TODO cmdline option to target different gpus: */
267 unsigned gpu_id
= 320;
271 bool from_spirv
= false;
274 memset(&s
, 0, sizeof(s
));
275 memset(&v
, 0, sizeof(v
));
277 /* cmdline args which impact shader variant get spit out in a
278 * comment on the first line.. a quick/dirty way to preserve
279 * that info so when ir3test recompiles the shader with a new
280 * compiler version, we use the same shader-key settings:
282 debug_printf("; options:");
285 if (!strcmp(argv
[n
], "--verbose")) {
286 fd_mesa_debug
|= FD_DBG_MSGS
| FD_DBG_OPTMSGS
| FD_DBG_DISASM
;
291 if (!strcmp(argv
[n
], "--binning-pass")) {
292 debug_printf(" %s", argv
[n
]);
293 key
.binning_pass
= true;
298 if (!strcmp(argv
[n
], "--color-two-side")) {
299 debug_printf(" %s", argv
[n
]);
300 key
.color_two_side
= true;
305 if (!strcmp(argv
[n
], "--half-precision")) {
306 debug_printf(" %s", argv
[n
]);
307 key
.half_precision
= true;
312 if (!strcmp(argv
[n
], "--saturate-s")) {
313 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
314 key
.vsaturate_s
= key
.fsaturate_s
= strtol(argv
[n
+1], NULL
, 0);
319 if (!strcmp(argv
[n
], "--saturate-t")) {
320 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
321 key
.vsaturate_t
= key
.fsaturate_t
= strtol(argv
[n
+1], NULL
, 0);
326 if (!strcmp(argv
[n
], "--saturate-r")) {
327 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
328 key
.vsaturate_r
= key
.fsaturate_r
= strtol(argv
[n
+1], NULL
, 0);
333 if (!strcmp(argv
[n
], "--astc-srgb")) {
334 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
335 key
.vastc_srgb
= key
.fastc_srgb
= strtol(argv
[n
+1], NULL
, 0);
340 if (!strcmp(argv
[n
], "--stream-out")) {
341 struct pipe_stream_output_info
*so
= &s
.stream_output
;
342 debug_printf(" %s", argv
[n
]);
343 /* TODO more dynamic config based on number of outputs, etc
344 * rather than just hard-code for first output:
348 so
->output
[0].register_index
= 0;
349 so
->output
[0].start_component
= 0;
350 so
->output
[0].num_components
= 4;
351 so
->output
[0].output_buffer
= 0;
352 so
->output
[0].dst_offset
= 2;
353 so
->output
[0].stream
= 0;
358 if (!strcmp(argv
[n
], "--ucp")) {
359 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
360 key
.ucp_enables
= strtol(argv
[n
+1], NULL
, 0);
365 if (!strcmp(argv
[n
], "--gpu")) {
366 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
367 gpu_id
= strtol(argv
[n
+1], NULL
, 0);
372 if (!strcmp(argv
[n
], "--help")) {
382 char *filename
= argv
[n
];
383 char *ext
= rindex(filename
, '.');
385 if (strcmp(ext
, ".tgsi") == 0) {
387 errx(1, "in TGSI mode, only a single file may be specified");
389 } else if (strcmp(ext
, ".spv") == 0) {
391 errx(1, "in SPIR-V mode, only a single file may be specified");
392 stage
= MESA_SHADER_COMPUTE
;
394 filenames
[num_files
++] = filename
;
397 errx(1, "in SPIR-V mode, an entry point must be specified");
400 } else if (strcmp(ext
, ".frag") == 0) {
401 if (s
.from_tgsi
|| from_spirv
)
402 errx(1, "cannot mix GLSL/TGSI/SPIRV");
403 if (num_files
>= ARRAY_SIZE(filenames
))
404 errx(1, "too many GLSL files");
405 stage
= MESA_SHADER_FRAGMENT
;
406 } else if (strcmp(ext
, ".vert") == 0) {
408 errx(1, "cannot mix GLSL and TGSI");
409 if (num_files
>= ARRAY_SIZE(filenames
))
410 errx(1, "too many GLSL files");
411 stage
= MESA_SHADER_VERTEX
;
417 filenames
[num_files
++] = filename
;
424 compiler
= ir3_compiler_create(NULL
, gpu_id
);
427 struct tgsi_token toks
[65536];
429 ret
= read_file(filenames
[0], &ptr
, &size
);
435 if (fd_mesa_debug
& FD_DBG_OPTMSGS
)
436 debug_printf("%s\n", (char *)ptr
);
438 if (!tgsi_text_translate(ptr
, toks
, ARRAY_SIZE(toks
)))
439 errx(1, "could not parse `%s'", filenames
[0]);
441 if (fd_mesa_debug
& FD_DBG_OPTMSGS
)
444 nir
= ir3_tgsi_to_nir(toks
);
445 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
446 } else if (from_spirv
) {
447 nir
= load_spirv(filenames
[0], entry
, stage
);
449 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, ir3_glsl_type_size
,
450 (nir_lower_io_options
)0);
452 /* TODO do this somewhere else */
453 nir_lower_int64(nir
, ~0);
454 nir_lower_system_values(nir
);
455 } else if (num_files
> 0) {
456 nir
= load_glsl(num_files
, filenames
, stage
);
462 s
.compiler
= compiler
;
463 s
.nir
= ir3_optimize_nir(&s
, nir
, NULL
);
468 switch (nir
->info
.stage
) {
469 case MESA_SHADER_FRAGMENT
:
470 s
.type
= v
.type
= SHADER_FRAGMENT
;
472 case MESA_SHADER_VERTEX
:
473 s
.type
= v
.type
= SHADER_VERTEX
;
475 case MESA_SHADER_COMPUTE
:
476 s
.type
= v
.type
= SHADER_COMPUTE
;
479 errx(1, "unhandled shader stage: %d", nir
->info
.stage
);
482 info
= "NIR compiler";
483 ret
= ir3_compile_shader_nir(s
.compiler
, &v
);
485 fprintf(stderr
, "compiler failed!\n");