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 "nir/tgsi_to_nir.h"
37 #include "tgsi/tgsi_parse.h"
38 #include "tgsi/tgsi_text.h"
39 #include "tgsi/tgsi_dump.h"
41 #include "ir3/ir3_compiler.h"
42 #include "ir3/ir3_gallium.h"
43 #include "ir3/ir3_nir.h"
44 #include "ir3/instr-a3xx.h"
47 #include "main/mtypes.h"
49 #include "compiler/glsl/standalone.h"
50 #include "compiler/glsl/glsl_to_nir.h"
51 #include "compiler/glsl/gl_nir.h"
52 #include "compiler/nir_types.h"
53 #include "compiler/spirv/nir_spirv.h"
55 #include "pipe/p_context.h"
57 static void dump_info(struct ir3_shader_variant
*so
, const char *str
)
60 const char *type
= ir3_shader_stage(so
->shader
);
61 bin
= ir3_shader_assemble(so
, so
->shader
->compiler
->gpu_id
);
62 debug_printf("; %s: %s\n", type
, str
);
63 ir3_shader_disasm(so
, bin
, stdout
);
68 insert_sorted(struct exec_list
*var_list
, nir_variable
*new_var
)
70 nir_foreach_variable(var
, var_list
) {
71 if (var
->data
.location
> new_var
->data
.location
) {
72 exec_node_insert_node_before(&var
->node
, &new_var
->node
);
76 exec_list_push_tail(var_list
, &new_var
->node
);
80 sort_varyings(struct exec_list
*var_list
)
82 struct exec_list new_list
;
83 exec_list_make_empty(&new_list
);
84 nir_foreach_variable_safe(var
, var_list
) {
85 exec_node_remove(&var
->node
);
86 insert_sorted(&new_list
, var
);
88 exec_list_move_nodes_to(&new_list
, var_list
);
92 fixup_varying_slots(struct exec_list
*var_list
)
94 nir_foreach_variable(var
, var_list
) {
95 if (var
->data
.location
>= VARYING_SLOT_VAR0
) {
96 var
->data
.location
+= 9;
97 } else if ((var
->data
.location
>= VARYING_SLOT_TEX0
) &&
98 (var
->data
.location
<= VARYING_SLOT_TEX7
)) {
99 var
->data
.location
+= VARYING_SLOT_VAR0
- VARYING_SLOT_TEX0
;
104 static struct ir3_compiler
*compiler
;
107 load_glsl(unsigned num_files
, char* const* files
, gl_shader_stage stage
)
109 static const struct standalone_options options
= {
113 struct gl_shader_program
*prog
;
114 const nir_shader_compiler_options
*nir_options
=
115 ir3_get_compiler_options(compiler
);
116 static struct gl_context local_ctx
;
118 prog
= standalone_compile_shader(&options
, num_files
, files
, &local_ctx
);
120 errx(1, "couldn't parse `%s'", files
[0]);
122 nir_shader
*nir
= glsl_to_nir(&local_ctx
, prog
, stage
, nir_options
);
124 /* required NIR passes: */
125 if (nir_options
->lower_all_io_to_temps
||
126 nir
->info
.stage
== MESA_SHADER_VERTEX
||
127 nir
->info
.stage
== MESA_SHADER_GEOMETRY
) {
128 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
129 nir_shader_get_entrypoint(nir
),
131 } else if (nir
->info
.stage
== MESA_SHADER_FRAGMENT
) {
132 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
133 nir_shader_get_entrypoint(nir
),
137 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
138 NIR_PASS_V(nir
, nir_split_var_copies
);
139 NIR_PASS_V(nir
, nir_lower_var_copies
);
141 NIR_PASS_V(nir
, nir_split_var_copies
);
142 NIR_PASS_V(nir
, nir_lower_var_copies
);
143 nir_print_shader(nir
, stdout
);
144 NIR_PASS_V(nir
, gl_nir_lower_atomics
, prog
, true);
145 NIR_PASS_V(nir
, nir_lower_atomics_to_ssbo
, 8);
146 nir_print_shader(nir
, stdout
);
149 case MESA_SHADER_VERTEX
:
150 nir_assign_var_locations(&nir
->inputs
,
154 /* Re-lower global vars, to deal with any dead VS inputs. */
155 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
157 sort_varyings(&nir
->outputs
);
158 nir_assign_var_locations(&nir
->outputs
,
161 fixup_varying_slots(&nir
->outputs
);
163 case MESA_SHADER_FRAGMENT
:
164 sort_varyings(&nir
->inputs
);
165 nir_assign_var_locations(&nir
->inputs
,
168 fixup_varying_slots(&nir
->inputs
);
169 nir_assign_var_locations(&nir
->outputs
,
173 case MESA_SHADER_COMPUTE
:
174 case MESA_SHADER_KERNEL
:
177 errx(1, "unhandled shader stage: %d", stage
);
180 nir_assign_var_locations(&nir
->uniforms
,
184 NIR_PASS_V(nir
, nir_lower_system_values
);
185 NIR_PASS_V(nir
, nir_lower_frexp
);
186 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, ir3_glsl_type_size
, 0);
187 NIR_PASS_V(nir
, gl_nir_lower_samplers
, prog
);
193 read_file(const char *filename
, void **ptr
, size_t *size
)
200 fd
= open(filename
, O_RDONLY
);
202 warnx("couldn't open `%s'", filename
);
206 ret
= fstat(fd
, &st
);
208 errx(1, "couldn't stat `%s'", filename
);
211 *ptr
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
212 if (*ptr
== MAP_FAILED
)
213 errx(1, "couldn't map `%s'", filename
);
220 static void debug_func(void *priv
, enum nir_spirv_debug_level level
,
221 size_t spirv_offset
, const char *message
)
223 // printf("%s\n", message);
227 load_spirv(const char *filename
, const char *entry
, gl_shader_stage stage
)
229 const struct spirv_to_nir_options spirv_options
= {
230 /* these caps are just make-believe */
232 .draw_parameters
= true,
234 .image_read_without_format
= true,
235 .image_write_without_format
= true,
237 .variable_pointers
= true,
239 .lower_workgroup_access_to_offsets
= true,
240 .lower_ubo_ssbo_access_to_offsets
= true,
249 read_file(filename
, &buf
, &size
);
251 nir
= spirv_to_nir(buf
, size
/ 4,
252 NULL
, 0, /* spec_entries */
255 ir3_get_compiler_options(compiler
));
257 nir_print_shader(nir
, stdout
);
262 static void print_usage(void)
264 printf("Usage: ir3_compiler [OPTIONS]... <file.tgsi | file.spv entry_point | (file.vert | file.frag)*>\n");
265 printf(" --verbose - verbose compiler/debug messages\n");
266 printf(" --binning-pass - generate binning pass shader (VERT)\n");
267 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
268 printf(" --half-precision - use half-precision\n");
269 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
270 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
271 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
272 printf(" --astc-srgb MASK - bitmask of samplers to enable astc-srgb workaround\n");
273 printf(" --stream-out - enable stream-out (aka transform feedback)\n");
274 printf(" --ucp MASK - bitmask of enabled user-clip-planes\n");
275 printf(" --gpu GPU_ID - specify gpu-id (default 320)\n");
276 printf(" --help - show this message\n");
279 int main(int argc
, char **argv
)
285 struct ir3_shader_variant v
;
287 struct ir3_shader_key key
= {};
288 /* TODO cmdline option to target different gpus: */
289 unsigned gpu_id
= 320;
293 bool from_spirv
= false;
296 memset(&s
, 0, sizeof(s
));
297 memset(&v
, 0, sizeof(v
));
299 /* cmdline args which impact shader variant get spit out in a
300 * comment on the first line.. a quick/dirty way to preserve
301 * that info so when ir3test recompiles the shader with a new
302 * compiler version, we use the same shader-key settings:
304 debug_printf("; options:");
307 if (!strcmp(argv
[n
], "--verbose")) {
308 ir3_shader_debug
|= IR3_DBG_OPTMSGS
| IR3_DBG_DISASM
;
313 if (!strcmp(argv
[n
], "--binning-pass")) {
314 debug_printf(" %s", argv
[n
]);
315 v
.binning_pass
= true;
320 if (!strcmp(argv
[n
], "--color-two-side")) {
321 debug_printf(" %s", argv
[n
]);
322 key
.color_two_side
= true;
327 if (!strcmp(argv
[n
], "--half-precision")) {
328 debug_printf(" %s", argv
[n
]);
329 key
.half_precision
= true;
334 if (!strcmp(argv
[n
], "--saturate-s")) {
335 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
336 key
.vsaturate_s
= key
.fsaturate_s
= strtol(argv
[n
+1], NULL
, 0);
341 if (!strcmp(argv
[n
], "--saturate-t")) {
342 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
343 key
.vsaturate_t
= key
.fsaturate_t
= strtol(argv
[n
+1], NULL
, 0);
348 if (!strcmp(argv
[n
], "--saturate-r")) {
349 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
350 key
.vsaturate_r
= key
.fsaturate_r
= strtol(argv
[n
+1], NULL
, 0);
355 if (!strcmp(argv
[n
], "--astc-srgb")) {
356 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
357 key
.vastc_srgb
= key
.fastc_srgb
= strtol(argv
[n
+1], NULL
, 0);
362 if (!strcmp(argv
[n
], "--stream-out")) {
363 struct ir3_stream_output_info
*so
= &s
.stream_output
;
364 debug_printf(" %s", argv
[n
]);
365 /* TODO more dynamic config based on number of outputs, etc
366 * rather than just hard-code for first output:
370 so
->output
[0].register_index
= 0;
371 so
->output
[0].start_component
= 0;
372 so
->output
[0].num_components
= 4;
373 so
->output
[0].output_buffer
= 0;
374 so
->output
[0].dst_offset
= 2;
375 so
->output
[0].stream
= 0;
380 if (!strcmp(argv
[n
], "--ucp")) {
381 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
382 key
.ucp_enables
= strtol(argv
[n
+1], NULL
, 0);
387 if (!strcmp(argv
[n
], "--gpu")) {
388 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
389 gpu_id
= strtol(argv
[n
+1], NULL
, 0);
394 if (!strcmp(argv
[n
], "--help")) {
404 char *filename
= argv
[n
];
405 char *ext
= strrchr(filename
, '.');
407 if (strcmp(ext
, ".tgsi") == 0) {
409 errx(1, "in TGSI mode, only a single file may be specified");
411 } else if (strcmp(ext
, ".spv") == 0) {
413 errx(1, "in SPIR-V mode, only a single file may be specified");
414 stage
= MESA_SHADER_COMPUTE
;
416 filenames
[num_files
++] = filename
;
419 errx(1, "in SPIR-V mode, an entry point must be specified");
422 } else if (strcmp(ext
, ".comp") == 0) {
423 if (s
.from_tgsi
|| from_spirv
)
424 errx(1, "cannot mix GLSL/TGSI/SPIRV");
425 if (num_files
>= ARRAY_SIZE(filenames
))
426 errx(1, "too many GLSL files");
427 stage
= MESA_SHADER_COMPUTE
;
428 } else if (strcmp(ext
, ".frag") == 0) {
429 if (s
.from_tgsi
|| from_spirv
)
430 errx(1, "cannot mix GLSL/TGSI/SPIRV");
431 if (num_files
>= ARRAY_SIZE(filenames
))
432 errx(1, "too many GLSL files");
433 stage
= MESA_SHADER_FRAGMENT
;
434 } else if (strcmp(ext
, ".vert") == 0) {
436 errx(1, "cannot mix GLSL and TGSI");
437 if (num_files
>= ARRAY_SIZE(filenames
))
438 errx(1, "too many GLSL files");
439 stage
= MESA_SHADER_VERTEX
;
445 filenames
[num_files
++] = filename
;
452 compiler
= ir3_compiler_create(NULL
, gpu_id
);
455 struct tgsi_token toks
[65536];
456 const nir_shader_compiler_options
*nir_options
=
457 ir3_get_compiler_options(compiler
);
459 ret
= read_file(filenames
[0], &ptr
, &size
);
465 if (ir3_shader_debug
& IR3_DBG_OPTMSGS
)
466 debug_printf("%s\n", (char *)ptr
);
468 if (!tgsi_text_translate(ptr
, toks
, ARRAY_SIZE(toks
)))
469 errx(1, "could not parse `%s'", filenames
[0]);
471 if (ir3_shader_debug
& IR3_DBG_OPTMSGS
)
474 nir
= tgsi_to_nir_noscreen(toks
, nir_options
);
475 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
476 } else if (from_spirv
) {
477 nir
= load_spirv(filenames
[0], entry
, stage
);
479 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, ir3_glsl_type_size
,
480 (nir_lower_io_options
)0);
482 /* TODO do this somewhere else */
483 nir_lower_int64(nir
, ~0);
484 nir_lower_system_values(nir
);
485 } else if (num_files
> 0) {
486 nir
= load_glsl(num_files
, filenames
, stage
);
492 s
.compiler
= compiler
;
493 s
.nir
= ir3_optimize_nir(&s
, nir
, NULL
);
497 s
.type
= v
.type
= nir
->info
.stage
;
499 info
= "NIR compiler";
500 ret
= ir3_compile_shader_nir(s
.compiler
, &v
);
502 fprintf(stderr
, "compiler failed!\n");