freedreno/ir3: support glsl linking for cmdline compiler
[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(unsigned num_files, char* const* files, 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, num_files, files);
72 if (!prog)
73 errx(1, "couldn't parse `%s'", files[0]);
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 char *filenames[2];
151 int num_files = 0;
152 unsigned stage = 0;
153 struct ir3_shader_variant v;
154 struct ir3_shader s;
155 struct ir3_shader_key key = {};
156 /* TODO cmdline option to target different gpus: */
157 unsigned gpu_id = 320;
158 const char *info;
159 void *ptr;
160 size_t size;
161
162 memset(&s, 0, sizeof(s));
163 memset(&v, 0, sizeof(v));
164
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:
169 */
170 debug_printf("; options:");
171
172 while (n < argc) {
173 if (!strcmp(argv[n], "--verbose")) {
174 fd_mesa_debug |= FD_DBG_MSGS | FD_DBG_OPTMSGS | FD_DBG_DISASM;
175 n++;
176 continue;
177 }
178
179 if (!strcmp(argv[n], "--binning-pass")) {
180 debug_printf(" %s", argv[n]);
181 key.binning_pass = true;
182 n++;
183 continue;
184 }
185
186 if (!strcmp(argv[n], "--color-two-side")) {
187 debug_printf(" %s", argv[n]);
188 key.color_two_side = true;
189 n++;
190 continue;
191 }
192
193 if (!strcmp(argv[n], "--half-precision")) {
194 debug_printf(" %s", argv[n]);
195 key.half_precision = true;
196 n++;
197 continue;
198 }
199
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);
203 n += 2;
204 continue;
205 }
206
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);
210 n += 2;
211 continue;
212 }
213
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);
217 n += 2;
218 continue;
219 }
220
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);
224 n += 2;
225 continue;
226 }
227
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:
233 */
234 so->num_outputs = 1;
235 so->stride[0] = 4;
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;
242 n++;
243 continue;
244 }
245
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);
249 n += 2;
250 continue;
251 }
252
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);
256 n += 2;
257 continue;
258 }
259
260 if (!strcmp(argv[n], "--help")) {
261 print_usage();
262 return 0;
263 }
264
265 break;
266 }
267 debug_printf("\n");
268
269 while (n < argc) {
270 char *filename = argv[n];
271 char *ext = rindex(filename, '.');
272
273 if (strcmp(ext, ".tgsi") == 0) {
274 if (num_files != 0)
275 errx(1, "in TGSI mode, only a single file may be specified");
276 s.from_tgsi = true;
277 } else if (strcmp(ext, ".frag") == 0) {
278 if (s.from_tgsi)
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) {
284 if (s.from_tgsi)
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;
289 } else {
290 print_usage();
291 return -1;
292 }
293
294 filenames[num_files++] = filename;
295
296 n++;
297 }
298
299 nir_shader *nir;
300
301 if (s.from_tgsi) {
302 struct tgsi_token toks[65536];
303
304 ret = read_file(filenames[0], &ptr, &size);
305 if (ret) {
306 print_usage();
307 return ret;
308 }
309
310 if (fd_mesa_debug & FD_DBG_OPTMSGS)
311 debug_printf("%s\n", (char *)ptr);
312
313 if (!tgsi_text_translate(ptr, toks, ARRAY_SIZE(toks)))
314 errx(1, "could not parse `%s'", filenames[0]);
315
316 if (fd_mesa_debug & FD_DBG_OPTMSGS)
317 tgsi_dump(toks, 0);
318
319 nir = ir3_tgsi_to_nir(toks);
320 } else if (num_files > 0) {
321 nir = load_glsl(num_files, filenames, stage);
322 } else {
323 print_usage();
324 return -1;
325 }
326
327 s.compiler = ir3_compiler_create(NULL, gpu_id);
328 s.nir = ir3_optimize_nir(&s, nir, NULL);
329
330 v.key = key;
331 v.shader = &s;
332
333 switch (nir->stage) {
334 case MESA_SHADER_FRAGMENT:
335 s.type = v.type = SHADER_FRAGMENT;
336 break;
337 case MESA_SHADER_VERTEX:
338 s.type = v.type = SHADER_VERTEX;
339 break;
340 case MESA_SHADER_COMPUTE:
341 s.type = v.type = SHADER_COMPUTE;
342 break;
343 default:
344 errx(1, "unhandled shader stage: %d", nir->stage);
345 }
346
347 info = "NIR compiler";
348 ret = ir3_compile_shader_nir(s.compiler, &v);
349 if (ret) {
350 fprintf(stderr, "compiler failed!\n");
351 return ret;
352 }
353 dump_info(&v, info);
354 }