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"
50 static void dump_info(struct ir3_shader_variant
*so
, const char *str
)
53 const char *type
= ir3_shader_stage(so
->shader
);
54 bin
= ir3_shader_assemble(so
, so
->shader
->compiler
->gpu_id
);
55 debug_printf("; %s: %s\n", type
, str
);
56 ir3_shader_disasm(so
, bin
);
60 int st_glsl_type_size(const struct glsl_type
*type
);
63 load_glsl(unsigned num_files
, char* const* files
, gl_shader_stage stage
)
65 static const struct standalone_options options
= {
69 struct gl_shader_program
*prog
;
71 prog
= standalone_compile_shader(&options
, num_files
, files
);
73 errx(1, "couldn't parse `%s'", files
[0]);
75 nir_shader
*nir
= glsl_to_nir(prog
, stage
, ir3_get_compiler_options());
77 standalone_compiler_cleanup(prog
);
79 /* required NIR passes: */
80 /* TODO cmdline args for some of the conditional lowering passes? */
82 NIR_PASS_V(nir
, nir_lower_io_to_temporaries
,
83 nir_shader_get_entrypoint(nir
),
85 NIR_PASS_V(nir
, nir_lower_global_vars_to_local
);
86 NIR_PASS_V(nir
, nir_split_var_copies
);
87 NIR_PASS_V(nir
, nir_lower_var_copies
);
89 NIR_PASS_V(nir
, nir_split_var_copies
);
90 NIR_PASS_V(nir
, nir_lower_var_copies
);
91 NIR_PASS_V(nir
, nir_lower_io_types
);
93 // TODO nir_assign_var_locations??
95 NIR_PASS_V(nir
, nir_lower_system_values
);
96 NIR_PASS_V(nir
, nir_lower_io
, nir_var_all
, st_glsl_type_size
);
97 NIR_PASS_V(nir
, nir_lower_samplers
, prog
);
103 read_file(const char *filename
, void **ptr
, size_t *size
)
110 fd
= open(filename
, O_RDONLY
);
112 warnx("couldn't open `%s'", filename
);
116 ret
= fstat(fd
, &st
);
118 errx(1, "couldn't stat `%s'", filename
);
121 *ptr
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
122 if (*ptr
== MAP_FAILED
)
123 errx(1, "couldn't map `%s'", filename
);
130 static void print_usage(void)
132 printf("Usage: ir3_compiler [OPTIONS]... <file.tgsi | (file.vert | file.frag)*>\n");
133 printf(" --verbose - verbose compiler/debug messages\n");
134 printf(" --binning-pass - generate binning pass shader (VERT)\n");
135 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
136 printf(" --half-precision - use half-precision\n");
137 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
138 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
139 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
140 printf(" --astc-srgb MASK - bitmask of samplers to enable astc-srgb workaround\n");
141 printf(" --stream-out - enable stream-out (aka transform feedback)\n");
142 printf(" --ucp MASK - bitmask of enabled user-clip-planes\n");
143 printf(" --gpu GPU_ID - specify gpu-id (default 320)\n");
144 printf(" --help - show this message\n");
147 int main(int argc
, char **argv
)
153 struct ir3_shader_variant v
;
155 struct ir3_shader_key key
= {};
156 /* TODO cmdline option to target different gpus: */
157 unsigned gpu_id
= 320;
162 memset(&s
, 0, sizeof(s
));
163 memset(&v
, 0, sizeof(v
));
165 /* cmdline args which impact shader variant get spit out in a
166 * comment on the first line.. a quick/dirty way to preserve
167 * that info so when ir3test recompiles the shader with a new
168 * compiler version, we use the same shader-key settings:
170 debug_printf("; options:");
173 if (!strcmp(argv
[n
], "--verbose")) {
174 fd_mesa_debug
|= FD_DBG_MSGS
| FD_DBG_OPTMSGS
| FD_DBG_DISASM
;
179 if (!strcmp(argv
[n
], "--binning-pass")) {
180 debug_printf(" %s", argv
[n
]);
181 key
.binning_pass
= true;
186 if (!strcmp(argv
[n
], "--color-two-side")) {
187 debug_printf(" %s", argv
[n
]);
188 key
.color_two_side
= true;
193 if (!strcmp(argv
[n
], "--half-precision")) {
194 debug_printf(" %s", argv
[n
]);
195 key
.half_precision
= true;
200 if (!strcmp(argv
[n
], "--saturate-s")) {
201 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
202 key
.vsaturate_s
= key
.fsaturate_s
= strtol(argv
[n
+1], NULL
, 0);
207 if (!strcmp(argv
[n
], "--saturate-t")) {
208 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
209 key
.vsaturate_t
= key
.fsaturate_t
= strtol(argv
[n
+1], NULL
, 0);
214 if (!strcmp(argv
[n
], "--saturate-r")) {
215 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
216 key
.vsaturate_r
= key
.fsaturate_r
= strtol(argv
[n
+1], NULL
, 0);
221 if (!strcmp(argv
[n
], "--astc-srgb")) {
222 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
223 key
.vastc_srgb
= key
.fastc_srgb
= strtol(argv
[n
+1], NULL
, 0);
228 if (!strcmp(argv
[n
], "--stream-out")) {
229 struct pipe_stream_output_info
*so
= &s
.stream_output
;
230 debug_printf(" %s", argv
[n
]);
231 /* TODO more dynamic config based on number of outputs, etc
232 * rather than just hard-code for first output:
236 so
->output
[0].register_index
= 0;
237 so
->output
[0].start_component
= 0;
238 so
->output
[0].num_components
= 4;
239 so
->output
[0].output_buffer
= 0;
240 so
->output
[0].dst_offset
= 2;
241 so
->output
[0].stream
= 0;
246 if (!strcmp(argv
[n
], "--ucp")) {
247 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
248 key
.ucp_enables
= strtol(argv
[n
+1], NULL
, 0);
253 if (!strcmp(argv
[n
], "--gpu")) {
254 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
255 gpu_id
= strtol(argv
[n
+1], NULL
, 0);
260 if (!strcmp(argv
[n
], "--help")) {
270 char *filename
= argv
[n
];
271 char *ext
= rindex(filename
, '.');
273 if (strcmp(ext
, ".tgsi") == 0) {
275 errx(1, "in TGSI mode, only a single file may be specified");
277 } else if (strcmp(ext
, ".frag") == 0) {
279 errx(1, "cannot mix GLSL and TGSI");
280 if (num_files
>= ARRAY_SIZE(filenames
))
281 errx(1, "too many GLSL files");
282 stage
= MESA_SHADER_FRAGMENT
;
283 } else if (strcmp(ext
, ".vert") == 0) {
285 errx(1, "cannot mix GLSL and TGSI");
286 if (num_files
>= ARRAY_SIZE(filenames
))
287 errx(1, "too many GLSL files");
288 stage
= MESA_SHADER_VERTEX
;
294 filenames
[num_files
++] = filename
;
302 struct tgsi_token toks
[65536];
304 ret
= read_file(filenames
[0], &ptr
, &size
);
310 if (fd_mesa_debug
& FD_DBG_OPTMSGS
)
311 debug_printf("%s\n", (char *)ptr
);
313 if (!tgsi_text_translate(ptr
, toks
, ARRAY_SIZE(toks
)))
314 errx(1, "could not parse `%s'", filenames
[0]);
316 if (fd_mesa_debug
& FD_DBG_OPTMSGS
)
319 nir
= ir3_tgsi_to_nir(toks
);
320 } else if (num_files
> 0) {
321 nir
= load_glsl(num_files
, filenames
, stage
);
327 s
.compiler
= ir3_compiler_create(NULL
, gpu_id
);
328 s
.nir
= ir3_optimize_nir(&s
, nir
, NULL
);
333 switch (nir
->stage
) {
334 case MESA_SHADER_FRAGMENT
:
335 s
.type
= v
.type
= SHADER_FRAGMENT
;
337 case MESA_SHADER_VERTEX
:
338 s
.type
= v
.type
= SHADER_VERTEX
;
340 case MESA_SHADER_COMPUTE
:
341 s
.type
= v
.type
= SHADER_COMPUTE
;
344 errx(1, "unhandled shader stage: %d", nir
->stage
);
347 info
= "NIR compiler";
348 ret
= ir3_compile_shader_nir(s
.compiler
, &v
);
350 fprintf(stderr
, "compiler failed!\n");