compiler: Move glsl_to_nir to libglsl.la
[mesa.git] / src / gallium / drivers / freedreno / ir3 / ir3_cmdline.c
1 /*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/mman.h>
30 #include <fcntl.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <err.h>
35
36 #include "tgsi/tgsi_parse.h"
37 #include "tgsi/tgsi_text.h"
38 #include "tgsi/tgsi_dump.h"
39
40 #include "freedreno_util.h"
41
42 #include "ir3_compiler.h"
43 #include "ir3_nir.h"
44 #include "instr-a3xx.h"
45 #include "ir3.h"
46
47 #include "compiler/glsl/standalone.h"
48 #include "compiler/glsl/glsl_to_nir.h"
49
50 static void dump_info(struct ir3_shader_variant *so, const char *str)
51 {
52 uint32_t *bin;
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);
57 free(bin);
58 }
59
60 int st_glsl_type_size(const struct glsl_type *type);
61
62 static nir_shader *
63 load_glsl(const char *filename, gl_shader_stage stage)
64 {
65 static const struct standalone_options options = {
66 .glsl_version = 140,
67 .do_link = true,
68 };
69 struct gl_shader_program *prog;
70
71 prog = standalone_compile_shader(&options, 1, (char * const*)&filename);
72 if (!prog)
73 errx(1, "couldn't parse `%s'", filename);
74
75 nir_shader *nir = glsl_to_nir(prog, stage, ir3_get_compiler_options());
76
77 standalone_compiler_cleanup(prog);
78
79 /* required NIR passes: */
80 /* TODO cmdline args for some of the conditional lowering passes? */
81
82 NIR_PASS_V(nir, nir_lower_io_to_temporaries,
83 nir_shader_get_entrypoint(nir),
84 true, true);
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);
88
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);
92
93 // TODO nir_assign_var_locations??
94
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);
98
99 return nir;
100 }
101
102 static int
103 read_file(const char *filename, void **ptr, size_t *size)
104 {
105 int fd, ret;
106 struct stat st;
107
108 *ptr = MAP_FAILED;
109
110 fd = open(filename, O_RDONLY);
111 if (fd == -1) {
112 warnx("couldn't open `%s'", filename);
113 return 1;
114 }
115
116 ret = fstat(fd, &st);
117 if (ret)
118 errx(1, "couldn't stat `%s'", filename);
119
120 *size = st.st_size;
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);
124
125 close(fd);
126
127 return 0;
128 }
129
130 static void print_usage(void)
131 {
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");
145 }
146
147 int main(int argc, char **argv)
148 {
149 int ret = 0, n = 1;
150 const char *filename;
151 struct ir3_shader_variant v;
152 struct ir3_shader s;
153 struct ir3_shader_key key = {};
154 /* TODO cmdline option to target different gpus: */
155 unsigned gpu_id = 320;
156 const char *info;
157 void *ptr;
158 size_t size;
159
160 memset(&s, 0, sizeof(s));
161 memset(&v, 0, sizeof(v));
162
163 /* cmdline args which impact shader variant get spit out in a
164 * comment on the first line.. a quick/dirty way to preserve
165 * that info so when ir3test recompiles the shader with a new
166 * compiler version, we use the same shader-key settings:
167 */
168 debug_printf("; options:");
169
170 while (n < argc) {
171 if (!strcmp(argv[n], "--verbose")) {
172 fd_mesa_debug |= FD_DBG_MSGS | FD_DBG_OPTMSGS | FD_DBG_DISASM;
173 n++;
174 continue;
175 }
176
177 if (!strcmp(argv[n], "--binning-pass")) {
178 debug_printf(" %s", argv[n]);
179 key.binning_pass = true;
180 n++;
181 continue;
182 }
183
184 if (!strcmp(argv[n], "--color-two-side")) {
185 debug_printf(" %s", argv[n]);
186 key.color_two_side = true;
187 n++;
188 continue;
189 }
190
191 if (!strcmp(argv[n], "--half-precision")) {
192 debug_printf(" %s", argv[n]);
193 key.half_precision = true;
194 n++;
195 continue;
196 }
197
198 if (!strcmp(argv[n], "--saturate-s")) {
199 debug_printf(" %s %s", argv[n], argv[n+1]);
200 key.vsaturate_s = key.fsaturate_s = strtol(argv[n+1], NULL, 0);
201 n += 2;
202 continue;
203 }
204
205 if (!strcmp(argv[n], "--saturate-t")) {
206 debug_printf(" %s %s", argv[n], argv[n+1]);
207 key.vsaturate_t = key.fsaturate_t = strtol(argv[n+1], NULL, 0);
208 n += 2;
209 continue;
210 }
211
212 if (!strcmp(argv[n], "--saturate-r")) {
213 debug_printf(" %s %s", argv[n], argv[n+1]);
214 key.vsaturate_r = key.fsaturate_r = strtol(argv[n+1], NULL, 0);
215 n += 2;
216 continue;
217 }
218
219 if (!strcmp(argv[n], "--astc-srgb")) {
220 debug_printf(" %s %s", argv[n], argv[n+1]);
221 key.vastc_srgb = key.fastc_srgb = strtol(argv[n+1], NULL, 0);
222 n += 2;
223 continue;
224 }
225
226 if (!strcmp(argv[n], "--stream-out")) {
227 struct pipe_stream_output_info *so = &s.stream_output;
228 debug_printf(" %s", argv[n]);
229 /* TODO more dynamic config based on number of outputs, etc
230 * rather than just hard-code for first output:
231 */
232 so->num_outputs = 1;
233 so->stride[0] = 4;
234 so->output[0].register_index = 0;
235 so->output[0].start_component = 0;
236 so->output[0].num_components = 4;
237 so->output[0].output_buffer = 0;
238 so->output[0].dst_offset = 2;
239 so->output[0].stream = 0;
240 n++;
241 continue;
242 }
243
244 if (!strcmp(argv[n], "--ucp")) {
245 debug_printf(" %s %s", argv[n], argv[n+1]);
246 key.ucp_enables = strtol(argv[n+1], NULL, 0);
247 n += 2;
248 continue;
249 }
250
251 if (!strcmp(argv[n], "--gpu")) {
252 debug_printf(" %s %s", argv[n], argv[n+1]);
253 gpu_id = strtol(argv[n+1], NULL, 0);
254 n += 2;
255 continue;
256 }
257
258 if (!strcmp(argv[n], "--help")) {
259 print_usage();
260 return 0;
261 }
262
263 break;
264 }
265 debug_printf("\n");
266
267 filename = argv[n];
268
269 ret = read_file(filename, &ptr, &size);
270 if (ret) {
271 print_usage();
272 return ret;
273 }
274
275 if (fd_mesa_debug & FD_DBG_OPTMSGS)
276 debug_printf("%s\n", (char *)ptr);
277
278 nir_shader *nir;
279
280 char *ext = rindex(filename, '.');
281
282 if (strcmp(ext, ".tgsi") == 0) {
283 struct tgsi_token toks[65536];
284
285 if (!tgsi_text_translate(ptr, toks, ARRAY_SIZE(toks)))
286 errx(1, "could not parse `%s'", filename);
287
288 if (fd_mesa_debug & FD_DBG_OPTMSGS)
289 tgsi_dump(toks, 0);
290
291 nir = ir3_tgsi_to_nir(toks);
292 s.from_tgsi = true;
293 } else if (strcmp(ext, ".frag") == 0) {
294 nir = load_glsl(filename, MESA_SHADER_FRAGMENT);
295 s.from_tgsi = false;
296 } else if (strcmp(ext, ".vert") == 0) {
297 nir = load_glsl(filename, MESA_SHADER_FRAGMENT);
298 s.from_tgsi = false;
299 } else {
300 print_usage();
301 return -1;
302 }
303
304 s.compiler = ir3_compiler_create(NULL, gpu_id);
305 s.nir = ir3_optimize_nir(&s, nir, NULL);
306
307 v.key = key;
308 v.shader = &s;
309
310 switch (nir->stage) {
311 case MESA_SHADER_FRAGMENT:
312 s.type = v.type = SHADER_FRAGMENT;
313 break;
314 case MESA_SHADER_VERTEX:
315 s.type = v.type = SHADER_VERTEX;
316 break;
317 case MESA_SHADER_COMPUTE:
318 s.type = v.type = SHADER_COMPUTE;
319 break;
320 default:
321 errx(1, "unhandled shader stage: %d", nir->stage);
322 }
323
324 info = "NIR compiler";
325 ret = ir3_compile_shader_nir(s.compiler, &v);
326 if (ret) {
327 fprintf(stderr, "compiler failed!\n");
328 return ret;
329 }
330 dump_info(&v, info);
331 }