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 static void dump_info(struct ir3_shader_variant
*so
, const char *str
)
50 const char *type
= ir3_shader_stage(so
->shader
);
51 // TODO make gpu_id configurable on cmdline
52 bin
= ir3_shader_assemble(so
, 320);
53 debug_printf("; %s: %s\n", type
, str
);
54 ir3_shader_disasm(so
, bin
);
60 read_file(const char *filename
, void **ptr
, size_t *size
)
67 fd
= open(filename
, O_RDONLY
);
69 warnx("couldn't open `%s'", filename
);
75 errx(1, "couldn't stat `%s'", filename
);
78 *ptr
= mmap(NULL
, st
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
79 if (*ptr
== MAP_FAILED
)
80 errx(1, "couldn't map `%s'", filename
);
87 static void print_usage(void)
89 printf("Usage: ir3_compiler [OPTIONS]... FILE\n");
90 printf(" --verbose - verbose compiler/debug messages\n");
91 printf(" --binning-pass - generate binning pass shader (VERT)\n");
92 printf(" --color-two-side - emulate two-sided color (FRAG)\n");
93 printf(" --half-precision - use half-precision\n");
94 printf(" --saturate-s MASK - bitmask of samplers to saturate S coord\n");
95 printf(" --saturate-t MASK - bitmask of samplers to saturate T coord\n");
96 printf(" --saturate-r MASK - bitmask of samplers to saturate R coord\n");
97 printf(" --astc-srgb MASK - bitmask of samplers to enable astc-srgb workaround\n");
98 printf(" --stream-out - enable stream-out (aka transform feedback)\n");
99 printf(" --ucp MASK - bitmask of enabled user-clip-planes\n");
100 printf(" --gpu GPU_ID - specify gpu-id (default 320)\n");
101 printf(" --help - show this message\n");
104 int main(int argc
, char **argv
)
107 const char *filename
;
108 struct tgsi_token toks
[65536];
109 struct tgsi_parse_context parse
;
110 struct ir3_shader_variant v
;
112 struct ir3_shader_key key
= {};
113 /* TODO cmdline option to target different gpus: */
114 unsigned gpu_id
= 320;
119 memset(&s
, 0, sizeof(s
));
120 memset(&v
, 0, sizeof(v
));
122 /* cmdline args which impact shader variant get spit out in a
123 * comment on the first line.. a quick/dirty way to preserve
124 * that info so when ir3test recompiles the shader with a new
125 * compiler version, we use the same shader-key settings:
127 debug_printf("; options:");
130 if (!strcmp(argv
[n
], "--verbose")) {
131 fd_mesa_debug
|= FD_DBG_MSGS
| FD_DBG_OPTMSGS
| FD_DBG_DISASM
;
136 if (!strcmp(argv
[n
], "--binning-pass")) {
137 debug_printf(" %s", argv
[n
]);
138 key
.binning_pass
= true;
143 if (!strcmp(argv
[n
], "--color-two-side")) {
144 debug_printf(" %s", argv
[n
]);
145 key
.color_two_side
= true;
150 if (!strcmp(argv
[n
], "--half-precision")) {
151 debug_printf(" %s", argv
[n
]);
152 key
.half_precision
= true;
157 if (!strcmp(argv
[n
], "--saturate-s")) {
158 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
159 key
.vsaturate_s
= key
.fsaturate_s
= strtol(argv
[n
+1], NULL
, 0);
164 if (!strcmp(argv
[n
], "--saturate-t")) {
165 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
166 key
.vsaturate_t
= key
.fsaturate_t
= strtol(argv
[n
+1], NULL
, 0);
171 if (!strcmp(argv
[n
], "--saturate-r")) {
172 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
173 key
.vsaturate_r
= key
.fsaturate_r
= strtol(argv
[n
+1], NULL
, 0);
178 if (!strcmp(argv
[n
], "--astc-srgb")) {
179 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
180 key
.vastc_srgb
= key
.fastc_srgb
= strtol(argv
[n
+1], NULL
, 0);
185 if (!strcmp(argv
[n
], "--stream-out")) {
186 struct pipe_stream_output_info
*so
= &s
.stream_output
;
187 debug_printf(" %s", argv
[n
]);
188 /* TODO more dynamic config based on number of outputs, etc
189 * rather than just hard-code for first output:
193 so
->output
[0].register_index
= 0;
194 so
->output
[0].start_component
= 0;
195 so
->output
[0].num_components
= 4;
196 so
->output
[0].output_buffer
= 0;
197 so
->output
[0].dst_offset
= 2;
198 so
->output
[0].stream
= 0;
203 if (!strcmp(argv
[n
], "--ucp")) {
204 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
205 key
.ucp_enables
= strtol(argv
[n
+1], NULL
, 0);
210 if (!strcmp(argv
[n
], "--gpu")) {
211 debug_printf(" %s %s", argv
[n
], argv
[n
+1]);
212 gpu_id
= strtol(argv
[n
+1], NULL
, 0);
217 if (!strcmp(argv
[n
], "--help")) {
228 ret
= read_file(filename
, &ptr
, &size
);
234 if (fd_mesa_debug
& FD_DBG_OPTMSGS
)
235 debug_printf("%s\n", (char *)ptr
);
237 if (!tgsi_text_translate(ptr
, toks
, Elements(toks
)))
238 errx(1, "could not parse `%s'", filename
);
240 if (fd_mesa_debug
& FD_DBG_OPTMSGS
)
243 nir_shader
*nir
= ir3_tgsi_to_nir(toks
);
244 s
.compiler
= ir3_compiler_create(NULL
, gpu_id
);
245 s
.nir
= ir3_optimize_nir(&s
, nir
, NULL
);
250 tgsi_parse_init(&parse
, toks
);
251 switch (parse
.FullHeader
.Processor
.Processor
) {
252 case PIPE_SHADER_FRAGMENT
:
253 s
.type
= v
.type
= SHADER_FRAGMENT
;
255 case PIPE_SHADER_VERTEX
:
256 s
.type
= v
.type
= SHADER_VERTEX
;
258 case PIPE_SHADER_COMPUTE
:
259 s
.type
= v
.type
= SHADER_COMPUTE
;
263 info
= "NIR compiler";
264 ret
= ir3_compile_shader_nir(s
.compiler
, &v
);
266 fprintf(stderr
, "compiler failed!\n");